From 8e0b84a470796b8ba072ed9cdcd4a07fb9e82f94 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Fri, 29 Jan 2016 22:42:03 +0000 Subject: [PATCH] Add more documentation and fix rubocop warnings --- data/plugins/combat/ammo.rb | 2 +- data/plugins/combat/attack.rb | 27 +++++++--------- data/plugins/combat/attack_requirements.rb | 32 +++++++++---------- data/plugins/combat/bonuses.rb | 1 + data/plugins/combat/combat.rb | 6 ++-- data/plugins/combat/combat_action.rb | 11 +++---- data/plugins/combat/combat_spell.rb | 17 ++++------ data/plugins/combat/combat_state.rb | 8 ++--- data/plugins/combat/spellbooks.rb | 2 +- data/plugins/combat/spells/bolts.rb | 12 +++---- data/plugins/combat/util.rb | 2 +- data/plugins/combat/weapon.rb | 13 ++++++-- data/plugins/combat/weapon_class.rb | 11 ++++--- data/plugins/combat/weapons/melee/daggers.rb | 4 +-- .../plugins/combat/weapons/melee/scimitars.rb | 2 +- .../combat/weapons/melee/two_handed_swords.rb | 5 +-- .../combat/weapons/ranged/crossbows.rb | 2 +- 17 files changed, 77 insertions(+), 80 deletions(-) diff --git a/data/plugins/combat/ammo.rb b/data/plugins/combat/ammo.rb index f151555c..277d4900 100644 --- a/data/plugins/combat/ammo.rb +++ b/data/plugins/combat/ammo.rb @@ -11,7 +11,7 @@ def create_ammo(item_matcher, properties, &block) items.each do |item_id| AMMO[item_id] = Ammo.new(item_id, properties) - AMMO[item_id].instance_eval &block if block_given? + AMMO[item_id].instance_eval(&block) if block_given? end end diff --git a/data/plugins/combat/attack.rb b/data/plugins/combat/attack.rb index b991a2a5..649c064d 100644 --- a/data/plugins/combat/attack.rb +++ b/data/plugins/combat/attack.rb @@ -13,29 +13,29 @@ java_import 'org.apollo.game.model.Graphic' class BaseAttack ## # The {@code AttackRequirement}s a {@code Player} must meet to use this attack. - + attr_reader :requirements ## # The maximum range this {@code Attack} can be executed from. - + attr_reader :range ## # How often this {@code Attack} can be executed in ticks. - + attr_reader :speed ## # Create a new {@code Attack} with the given properties. # - # @param [Number] speed The minimum number of ticks to wait before this attack can be executed after + # @param [Number] speed The minimum number of ticks to wait before this attack can be executed after # a previous {@code Attack}. # @param [Number] animation The {@code Animation} to play on the player when executing this {@code Attack}. # @param [Hash] graphic The {@code Graphic} to play on the player when executing this {@code Attack}. # @param [Number] range The maximum distance this {@code Attack} can be executed from. # @param [Array] requirements The requirements that must be met to execute this {@code Attack}. - + def initialize(speed:, animation:, graphic: nil, range: 1, requirements: []) fail 'Attack speed must be a non-negative number' if speed < 0 fail 'Attack range must be a non-negative number' if range < 0 @@ -49,10 +49,10 @@ class BaseAttack ## # Execute this {@code Attack} and apply its effect. - # + # # @param [Mob] source The attacker. # @param [Mob] target The target. - + def do(source, target) source.play_animation(Animation.new(@animation)) @@ -95,11 +95,10 @@ class RangedAttack < BaseAttack end ## -# A simple magic attack, which sends a projectile based on the current spell that the player is casting and deals +# A simple magic attack, which sends a projectile based on the current spell that the player is casting and deals # damage delayed by the speed and travel distance of the projectile. class MagicAttack < BaseAttack - ## # The maximum distance that a magic attack can be performed from. MAX_DISTANCE = 8 @@ -118,7 +117,6 @@ class MagicAttack < BaseAttack end def apply(source, target) - projectile!(source, target, @projectile, @projectile_type) distance = source.position.get_distance(target.position) @@ -136,7 +134,6 @@ class MagicAttack < BaseAttack end end - end ## @@ -154,7 +151,7 @@ end class AttackDSL attr_accessor :animation, :speed, :range, :graphic - def initialize() + def initialize @requirements = [] @subattacks = [] end @@ -200,11 +197,11 @@ end private -def schedule_damage!(source, target, amount, delay, secondary = false, &callback) +def schedule_damage!(source, target, amount, delay, secondary = false, &_callback) schedule delay do |task| do_damage!(source, target, amount, secondary) - callback.call if block_given? + yield if block_given? task.stop end @@ -225,7 +222,7 @@ end def auto_retaliate!(source, target) target_combat_state = target.get_combat_state - if target.auto_retaliate && !target_combat_state.is_attacking? + if target.auto_retaliate && !target_combat_state.attacking? target_combat_state.target = source target.start_action(CombatAction.new(target)) end diff --git a/data/plugins/combat/attack_requirements.rb b/data/plugins/combat/attack_requirements.rb index 09554dcf..eb944fe3 100644 --- a/data/plugins/combat/attack_requirements.rb +++ b/data/plugins/combat/attack_requirements.rb @@ -32,9 +32,9 @@ end class AttackRequirementDSL RUNES = { - :air => 556, :water => 555, :earth => 557, :fire => 554, - :mind => 558, :chaos => 562, :death => 560, :blood => 565, - :cosmic => 564, :law => 563, :nature => 561, :soul => 566 + air: 556, water: 555, earth: 557, fire: 554, + mind: 558, chaos: 562, death: 560, blood: 565, + cosmic: 564, law: 563, nature: 561, soul: 566 } attr_reader :requirements @@ -65,7 +65,7 @@ class SpecialEnergyRequirement < AttackRequirement player.using_special = false update_special_bar(player) - fail AttackRequirementException.new('Not enough special attack energy.') + fail AttackRequirementException, 'Not enough special attack energy.' end end @@ -79,11 +79,11 @@ end class LevelRequirement < AttackRequirement SKILLS = { - :magic => Skill::MAGIC + magic: Skill::MAGIC } def initialize(type, level) - fail "Could not find '#{type}' skill." unless SKILLS.has_key?(type) + fail "Could not find '#{type}' skill." unless SKILLS.key?(type) @skill = SKILLS[type] @level = level @@ -91,8 +91,8 @@ class LevelRequirement < AttackRequirement def validate(player) current_level = player.skill_set.current_level(@skill) - - throw AttackRequirementException.new(skill_requirement_message) unless current_level >= @level + + fail AttackRequirementException, skill_requirement_message unless current_level >= @level end def apply!(player) @@ -101,7 +101,7 @@ class LevelRequirement < AttackRequirement private def skill_requirement_message - "TODO" + 'TODO' end end @@ -112,7 +112,7 @@ class ItemRequirement < AttackRequirement end def validate(player) - throw AttackRequirementException.new(item_missing_message) unless player.inventory.get_amount(@item) >= @amount + fail AttackRequirementException, item_missing_message unless player.inventory.get_amount(@item) >= @amount end def apply!(player) @@ -122,8 +122,6 @@ class ItemRequirement < AttackRequirement private def item_missing_message - definition = ItemDefinition.lookup(@item) - "You don't have enough #{lookup_item(@item).name}s" end end @@ -141,22 +139,22 @@ class AmmoRequirement < AttackRequirement equipped_ammo_amt = player.equipment.get(EquipmentConstants::AMMO).amount if equipped_ammo.nil? - fail AttackRequirementException.new('You have no ammo left in your quiver!') + fail AttackRequirementException, 'You have no ammo left in your quiver!' end if @amount > 1 && equipped_ammo_amt < @amount - fail AttackRequirementException.new('You don\'t have enough ammo left in your quiver!') + fail AttackRequirementException, 'You don\'t have enough ammo left in your quiver!' end unless equipped_ammo.weapon_classes.include? equipped_weapon.weapon_class.name - fail AttackRequirementException.new('You can\'t use this ammo with this weapon.') + fail AttackRequirementException, 'You can\'t use this ammo with this weapon.' end if equipped_ammo.level_requirement > equipped_weapon_def.ranged_level - fail AttackRequirementException.new('You can\'t use this ammo with this weapon.') + fail AttackRequirementException, 'You can\'t use this ammo with this weapon.' end end - + def apply!(player) equipped_ammo = EquipmentUtil.equipped_ammo player diff --git a/data/plugins/combat/bonuses.rb b/data/plugins/combat/bonuses.rb index 2d6a8f00..7099fbc1 100644 --- a/data/plugins/combat/bonuses.rb +++ b/data/plugins/combat/bonuses.rb @@ -1,4 +1,5 @@ module Combat + ## # A module for units which can have their own bonuses. E.G., weapons, equipment, ammo. module BonusContainer def attack_bonus(type) diff --git a/data/plugins/combat/combat.rb b/data/plugins/combat/combat.rb index 7aa05b9f..1e563d98 100644 --- a/data/plugins/combat/combat.rb +++ b/data/plugins/combat/combat.rb @@ -20,8 +20,8 @@ end on :message, :magic_on_mob do |player, message| target = $world.npc_repository.get(message.index) - - next unless SPELLBOOKS.has_key? message.interface_id + + next unless SPELLBOOKS.key? message.interface_id player_combat_state = player.get_combat_state player_combat_state.target = target @@ -29,7 +29,7 @@ on :message, :magic_on_mob do |player, message| spellbook = SPELLBOOKS[message.interface_id] spell = spell_for(spellbook, message.spell_id) player_combat_state.queue_attack(spell.attack) - + player.walking_queue.clear player.start_action CombatAction.new(player, true) end diff --git a/data/plugins/combat/combat_action.rb b/data/plugins/combat/combat_action.rb index aad85afc..ce39dada 100644 --- a/data/plugins/combat/combat_action.rb +++ b/data/plugins/combat/combat_action.rb @@ -4,22 +4,21 @@ java_import 'org.apollo.game.model.entity.EntityType' class CombatAction < Action def initialize(source, once = false) super(0, true, source) - - mob.attacking = true - @combat_state = source.get_combat_state @attack_timer = 100 @once = once + mob.attacking = true end def can_attack? next_attack = @combat_state.next_attack true target = @combat_state.target current_distance = mob.position.get_distance target.position - attack_collision_type = next_attack.range > 1 ? EntityType::PROJECTILE : EntityType::NPC - - in_range = current_distance <= next_attack.range# && !$world.intersects(mob.position, target.position, attack_collision_type) + in_range = current_distance <= next_attack.range + in_range = in_range && !$world.intersects(mob.position, target.position, + next_attack.range > 1 ? EntityType::PROJECTILE : EntityType::NPC) + unless in_range mob.follow @combat_state.target, next_attack.range diff --git a/data/plugins/combat/combat_spell.rb b/data/plugins/combat/combat_spell.rb index 58dc0e80..ed70f1be 100644 --- a/data/plugins/combat/combat_spell.rb +++ b/data/plugins/combat/combat_spell.rb @@ -1,9 +1,8 @@ class CombatSpell - attr_reader :button attr_reader :spellbook - + attr_reader :requirements attr_reader :damage @@ -29,11 +28,9 @@ class CombatSpell @projectile = projectile @projectile_type = projectile_type end - end class CombatSpellDSL - def initialize(&block) instance_eval(&block) end @@ -53,10 +50,10 @@ class CombatSpellDSL @projectile = id @projectile_type = type end - + def requirements(&block) fail 'Block not given' unless block_given? - + @requirements = AttackRequirementDSL.new(&block).requirements end @@ -73,9 +70,8 @@ class CombatSpellDSL end def to_combat_spell - return CombatSpell.new(@button, @spellbook, @requirements, @damage, @animation, @graphic, @hit_graphic, @projectile, @projectile_type) + CombatSpell.new(@button, @spellbook, @requirements, @damage, @animation, @graphic, @hit_graphic, @projectile, @projectile_type) end - end COMBAT_SPELLS = {} @@ -89,10 +85,9 @@ def create_combat_spell(name, &block) end def spell_for(spellbook, button) - - COMBAT_SPELLS.each do |name, spell| + COMBAT_SPELLS.each do |_name, spell| return spell if spell.spellbook == spellbook && spell.button == button end fail "Unable to find a spell in spellbook '#{spellbook}' with button id '#{button}'" -end \ No newline at end of file +end diff --git a/data/plugins/combat/combat_state.rb b/data/plugins/combat/combat_state.rb index 16f6edeb..be31462f 100644 --- a/data/plugins/combat/combat_state.rb +++ b/data/plugins/combat/combat_state.rb @@ -2,9 +2,7 @@ java_import 'org.apollo.game.model.entity.Mob' module MobCombatState def get_combat_state - if @combat_state.nil? - @combat_state = CombatState.new(self, true) - end + @combat_state = CombatState.new(self, true) if @combat_state.nil? @combat_state end @@ -30,10 +28,10 @@ class CombatState end def will_attack? - is_attacking? && next_attack(true).speed >= @mob.attack_timer + attacking? && next_attack(true).speed >= @mob.attack_timer end - def is_attacking? + def attacking? !target.nil? && @mob.attacking end diff --git a/data/plugins/combat/spellbooks.rb b/data/plugins/combat/spellbooks.rb index 189da025..48c6df12 100644 --- a/data/plugins/combat/spellbooks.rb +++ b/data/plugins/combat/spellbooks.rb @@ -4,4 +4,4 @@ def create_spellbook(identifier, interface_id:) SPELLBOOKS[interface_id] = identifier end -create_spellbook :modern, interface_id: 192 \ No newline at end of file +create_spellbook :modern, interface_id: 192 diff --git a/data/plugins/combat/spells/bolts.rb b/data/plugins/combat/spells/bolts.rb index fcad6cb5..fb68071b 100644 --- a/data/plugins/combat/spells/bolts.rb +++ b/data/plugins/combat/spells/bolts.rb @@ -1,18 +1,18 @@ create_combat_spell :wind_bolt do - + spellbook :modern, button: 10 max_damage 8 - - effects animation: 1162, graphic: {id: 117, height: 100}, hit_graphic: {id: 119, delay: 100} + + effects animation: 1162, graphic: { id: 117, height: 100 }, hit_graphic: { id: 119, delay: 100 } projectile id: 118, type: :bolt_spells - + requirements do rune :air, amount: 3 rune :chaos, amount: 1 - + skill :magic, level: 17 end end -create_projectile_type :bolt_spells, start_height: 46, end_height: 36, delay: 51, speed: 12, slope: 15, radius: 86 \ No newline at end of file +create_projectile_type :bolt_spells, start_height: 46, end_height: 36, delay: 51, speed: 12, slope: 15, radius: 86 diff --git a/data/plugins/combat/util.rb b/data/plugins/combat/util.rb index e5252843..dd2f1e12 100644 --- a/data/plugins/combat/util.rb +++ b/data/plugins/combat/util.rb @@ -8,7 +8,7 @@ class CombatUtil strength = source.skill_set.get_skill(Skill::STRENGTH) strength_stat = 5 # source.bonus_stat(:other, :strength) - effective_strength_damage = (strength.current_level) # * prayer_multiplier + effective_strength_damage = strength.current_level # * prayer_multiplier if [:aggressive, :alt_aggressive].include? source.combat_style effective_strength_damage += 3 diff --git a/data/plugins/combat/weapon.rb b/data/plugins/combat/weapon.rb index b0069a6b..390d5cd2 100644 --- a/data/plugins/combat/weapon.rb +++ b/data/plugins/combat/weapon.rb @@ -8,6 +8,13 @@ java_import 'org.apollo.game.model.inv.SynchronizationInventoryListener' WEAPONS = {} NAMED_WEAPONS = {} +## +# Create a new {@code Weapon} and configure it with the Weapon DSL. +# +# @param [Symbol] identifier A unique identifier for the weapon if {@code named} is true, otherwise the item name +# of the weapon +# @param [Symbol] class_name A symbol representing the {@code WeaponClass} this weapon belongs to. +# @param [Boolean] True if this is a uniquely identified weapon, false if it belongs to an item. def create_weapon(identifier, class_name = nil, named: false, &block) if named @@ -52,13 +59,13 @@ def create_normal_weapon(item_matcher, class_name = nil, &block) end WEAPONS[item_id] = Weapon.new(definition.name, WEAPON_CLASSES[class_name]) - WEAPONS[item_id].instance_eval &block if block_given? + WEAPONS[item_id].instance_eval(&block) if block_given? end end def create_named_weapon(name, class_name, &block) NAMED_WEAPONS[name] = Weapon.new name.to_s.capitalize, WEAPON_CLASSES[class_name] - NAMED_WEAPONS[name].instance_eval &block if block_given? + NAMED_WEAPONS[name].instance_eval(&block) if block_given? end # Represents an equippable weapon, and the class it belongs to. @@ -87,7 +94,7 @@ class Weapon attack_dsl.animation = animation attack_dsl.graphic = graphic attack_dsl.add_requirement SpecialEnergyRequirement.new(energy_requirement) - attack_dsl.instance_eval &block + attack_dsl.instance_eval(&block) @special_attack = attack_dsl.to_attack end diff --git a/data/plugins/combat/weapon_class.rb b/data/plugins/combat/weapon_class.rb index 64a347d1..b936a82b 100644 --- a/data/plugins/combat/weapon_class.rb +++ b/data/plugins/combat/weapon_class.rb @@ -88,12 +88,12 @@ class WeaponClass def style(style, properties = {}, &block) fail 'Invalid combat style given' unless COMBAT_STYLES.include? style - properties = @defaults.merge properties + properties = @defaults.merge properties ## The config ID used to set the active combat style, typically 0-3. TODO: does this always work? config = @styles.size button, attack_type, block_animation = properties[:button], properties[:attack_type], properties[:block_animation] - + @styles[style] = CombatStyle.new(button, config, attack_type, block_animation) if block_given? @@ -102,7 +102,7 @@ class WeaponClass attack_dsl.graphic = properties[:graphic] attack_dsl.animation = properties[:animation] attack_dsl.range = properties[:range] - properties[:requirements].each {|requirement| attack_dsl.add_requirement requirement } + properties[:requirements].each { |requirement| attack_dsl.add_requirement requirement } attack_dsl.instance_eval &block @styles[style].attack = attack_dsl.to_attack @@ -111,7 +111,7 @@ class WeaponClass return unless @styles[style].attack.nil? ## Get rid of any properties which aren't included in the keyword argument list - properties.delete_if {|key| ![:speed, :animation, :range, :requirements, :graphic].include? key } + properties.delete_if { |key| ![:speed, :animation, :range, :requirements, :graphic].include? key } if @type == :melee @styles[style].attack = Attack.new(properties) @@ -157,7 +157,8 @@ class WeaponClass end ## - + # Set the character animations associated with this weapon class. + def animations(stand: nil, walk: nil, run: nil, idle_turn: nil, turn_around: nil, turn_left: nil, turn_right: nil) @animations = { stand: stand, diff --git a/data/plugins/combat/weapons/melee/daggers.rb b/data/plugins/combat/weapons/melee/daggers.rb index b8d531cc..278ceec3 100644 --- a/data/plugins/combat/weapons/melee/daggers.rb +++ b/data/plugins/combat/weapons/melee/daggers.rb @@ -29,9 +29,9 @@ create_weapon_class :dragon_dagger, widget: DAGGER_WIDGET_ID do style :defensive, button: 5 end -create_weapon /(?:drag|dragon) dagger.*/, :dragon_dagger do +create_weapon(/(?:drag|dragon) dagger.*/, :dragon_dagger) do set_special_attack speed: 4, energy_requirement: 25, animation: 1062, graphic: { id: 252, height: 100 } do - damage! + damage! damage! secondary: true end end diff --git a/data/plugins/combat/weapons/melee/scimitars.rb b/data/plugins/combat/weapons/melee/scimitars.rb index ffee0053..eb4b9cb6 100644 --- a/data/plugins/combat/weapons/melee/scimitars.rb +++ b/data/plugins/combat/weapons/melee/scimitars.rb @@ -25,7 +25,7 @@ create_weapon :steel_scimitar do other_bonuses melee_strength: 14 end -create_weapon /(black|white) scimitar/ do +create_weapon(/(black|white) scimitar/) do attack_bonuses stab: 4, slash: 19 other_bonuses melee_strength: 14 end diff --git a/data/plugins/combat/weapons/melee/two_handed_swords.rb b/data/plugins/combat/weapons/melee/two_handed_swords.rb index 18eb8195..d95b62b1 100644 --- a/data/plugins/combat/weapons/melee/two_handed_swords.rb +++ b/data/plugins/combat/weapons/melee/two_handed_swords.rb @@ -6,7 +6,8 @@ create_weapon_class :two_handed_sword, widget: TWO_HANDED_SWORD_WIDGET_ID do defaults speed: 7, animation: 7041, attack_type: :slash special_bar TWO_HANDED_SWORD_SPECIAL_CONFIG_ID, TWO_HANDED_SWORD_SPECIAL_BUTTON_ID - animations stand: 7047, walk: 7046, run: 7039, idle_turn: 7044, turn_around: 7044, turn_left: 7043, turn_right: 7044 + animations stand: 7047, walk: 7046, run: 7039, idle_turn: 7044, turn_around: 7044, + turn_left: 7043, turn_right: 7044 attack_bonuses stab: -4, magic: -4 defence_bonuses range: -1 @@ -27,7 +28,7 @@ create_weapon :steel_2h_sword do other_bonuses melee_strength: 22 end -create_weapon /(?:black|white) 2h sword/ do +create_weapon(/(?:black|white) 2h sword/) do attack_bonuses slash: 27, crush: 21 other_bonuses melee_strength: 26 end diff --git a/data/plugins/combat/weapons/ranged/crossbows.rb b/data/plugins/combat/weapons/ranged/crossbows.rb index a7dc8bf3..b09dea10 100644 --- a/data/plugins/combat/weapons/ranged/crossbows.rb +++ b/data/plugins/combat/weapons/ranged/crossbows.rb @@ -8,4 +8,4 @@ create_weapon_class :crossbow, widget: CROSSBOW_WIDGET_ID, type: :ranged do style :long_range, range: 9, button: 4 end -create_weapon /rune c'bow/, :crossbow +create_weapon(/rune c'bow/, :crossbow)