From 16b0ffaeebf4712349e8e1d636055aecf21bdece Mon Sep 17 00:00:00 2001 From: Major- Date: Sun, 23 Feb 2014 22:58:56 +0000 Subject: [PATCH] Add consumables base and support food consumption. --- data/plugins/consumables/consumable.rb | 41 +++++++++++++++++ data/plugins/consumables/food.rb | 61 ++++++++++++++++++++++++++ data/plugins/consumables/plugin.xml | 15 +++++++ 3 files changed, 117 insertions(+) create mode 100644 data/plugins/consumables/consumable.rb create mode 100644 data/plugins/consumables/food.rb create mode 100644 data/plugins/consumables/plugin.xml diff --git a/data/plugins/consumables/consumable.rb b/data/plugins/consumables/consumable.rb new file mode 100644 index 00000000..e75245b1 --- /dev/null +++ b/data/plugins/consumables/consumable.rb @@ -0,0 +1,41 @@ +require 'java' + +# A map of item ids to consumables. +CONSUMABLES = {} + +CONSUME_ANIMATION_ID = 829 + +# An item that can be consumed to produce a skill effect. +class Consumable + attr_reader :name, :id + + def initialize(name, id, sound_id) + @name = name.to_s.gsub(/_/, ' ') + @id = id + @sound_id = sound_id + end + + def consume(player) + # Override to provide specific functionality. + end + +end + +# Appends a consumable to the map, with its id as the key. +def append_consumable(consumable) + CONSUMABLES[consumable.id] = consumable +end + +# Intercepts the first item option event and consumes the consumable, if necessary. +on :event, :item_option do |ctx, player, event| + if (event.option == 1) + consumable = CONSUMABLES[event.id] + unless consumable == nil + player.inventory.reset(event.slot) + player.play_animation(Animation.new(CONSUME_ANIMATION_ID)) + consumable.consume(player) + + ctx.break_handler_chain + end + end +end \ No newline at end of file diff --git a/data/plugins/consumables/food.rb b/data/plugins/consumables/food.rb new file mode 100644 index 00000000..c08711a5 --- /dev/null +++ b/data/plugins/consumables/food.rb @@ -0,0 +1,61 @@ +require 'java' + +java_import 'org.apollo.game.model.Animation' +java_import 'org.apollo.game.model.Player' +java_import 'org.apollo.game.model.Skill' + +EAT_FOOD_SOUND = 317 + +# Todo support other foods (e.g. pizza) + +# Represents an edible piece of food, such as bread or fish. +class Food < Consumable + + def initialize(name, id, restoration) + super(name, id, EAT_FOOD_SOUND) + @restoration = restoration + end + + # Restore the appropriate amount of hitpoints when consumed. + def consume(player) + hitpoints = player.skill_set.skill(HITPOINTS_SKILL_ID) + new_curr = hitpoints.current_level + @restoration + new_curr = hitpoints.maximum_level if new_curr > hitpoints.maximum_level + + player.skill_set.set_skill(HITPOINTS_SKILL_ID, Skill.new(hitpoints.experience, new_curr, hitpoints.maximum_level)) + player.send_message("You eat the #{name}.", true) + end + +end + +# Appends a food item to the list of consumables. +def append_food(hash) + raise 'Hash must contain a name, id, and a restoration value.' unless (hash.has_key?(:name) && hash.has_key?(:id) && hash.has_key?(:restoration)) + name = hash[:name]; id = hash[:id]; restoration = hash[:restoration] + + append_consumable(Food.new(name, id, restoration)) +end + + +append_food :name => :bread, :id => 2309, :restoration => 4 +append_food :name => :cooked_meat, :id => 2142, :restoration => 3 +append_food :name => :cooked_chicken, :id => 2140, :restoration => 3 +append_food :name => :ugthanki_meat, :id => 1861, :restoration => 3 + +append_food :name => :anchovies, :id => 319, :restoration => 1 +append_food :name => :shrimps, :id => 315, :restoration => 3 +append_food :name => :sardine, :id => 325, :restoration => 3 +append_food :name => :herring, :id => 347, :restoration => 5 +append_food :name => :mackeral, :id => 355, :restoration => 5 +append_food :name => :trout, :id => 333, :restoration => 7 +append_food :name => :cod, :id => 339, :restoration => 7 +append_food :name => :pike, :id => 351, :restoration => 8 +append_food :name => :salmon, :id => 329, :restoration => 9 +append_food :name => :tuna, :id => 361, :restoration => 10 +append_food :name => :lobster, :id => 379, :restoration => 12 +append_food :name => :bass, :id => 365, :restoration => 13 +append_food :name => :swordfish, :id => 373, :restoration => 14 +append_food :name => :monkfish, :id => 7946, :restoration => 16 +append_food :name => :shark, :id => 385, :restoration => 20 +append_food :name => :sea_turtle, :id => 397, :restoration => 21 +append_food :name => :manta_ray, :id => 391, :restoration => 22 \ No newline at end of file diff --git a/data/plugins/consumables/plugin.xml b/data/plugins/consumables/plugin.xml new file mode 100644 index 00000000..ca218a0a --- /dev/null +++ b/data/plugins/consumables/plugin.xml @@ -0,0 +1,15 @@ + + + consumables + 0.1 + Consumables + Adds support for consumables (e.g. potions, food). + + Major + + + + + + + \ No newline at end of file