diff --git a/creature.cpp b/creature.cpp index b387b9465..a25a10d47 100644 --- a/creature.cpp +++ b/creature.cpp @@ -1267,6 +1267,12 @@ CreatureAction Creature::payFor(const vector& items) const { .append([=](WCreature) { for (auto it : items) it->setShopkeeper(nullptr); }); } +int Creature::getMaximumRange() const { + auto weapon = getEquipment().getSlotItems(EquipmentSlot::RANGED_WEAPON); + if (weapon.empty()) return 0; + return weapon.getOnlyElement()->getRangedWeapon()->getMaxDistance(); +} + CreatureAction Creature::fire(Vec2 direction) const { CHECK(direction.length8() == 1); if (getEquipment().getItems(ItemIndex::RANGED_WEAPON).empty()) diff --git a/creature.h b/creature.h index dae50550c..00933f76a 100644 --- a/creature.h +++ b/creature.h @@ -93,6 +93,7 @@ class Creature : public Renderable, public UniqueEntity, public OwnedO bool canSee(Position) const; bool canSee(Vec2) const; bool isEnemy(WConstCreature) const; + int getMaximumRange() const; void tick(); const CreatureName& getName() const; diff --git a/item_type.cpp b/item_type.cpp index 7dc4c927d..7288f7ec5 100644 --- a/item_type.cpp +++ b/item_type.cpp @@ -745,7 +745,7 @@ ItemAttributes ItemType::ElvenBow::getAttributes() const { i.viewId = ViewId::ELVEN_BOW; i.itemClass = ItemClass::RANGED_WEAPON; i.equipmentSlot = EquipmentSlot::RANGED_WEAPON; - i.rangedWeapon = RangedWeapon(AttrType::RANGED_DAMAGE, "arrow", ViewId::ARROW); + i.rangedWeapon = RangedWeapon(AttrType::RANGED_DAMAGE, "arrow", ViewId::ARROW, 12); i.weaponInfo.twoHanded = true; i.weight = 1; i.modifiers[AttrType::RANGED_DAMAGE] = 16; @@ -760,7 +760,7 @@ ItemAttributes ItemType::Bow::getAttributes() const { i.name = "short bow"; i.itemClass = ItemClass::RANGED_WEAPON; i.equipmentSlot = EquipmentSlot::RANGED_WEAPON; - i.rangedWeapon = RangedWeapon(AttrType::RANGED_DAMAGE, "arrow", ViewId::ARROW); + i.rangedWeapon = RangedWeapon(AttrType::RANGED_DAMAGE, "arrow", ViewId::ARROW, 10); i.weaponInfo.twoHanded = true; i.weight = 1; i.modifiers[AttrType::RANGED_DAMAGE] = 10 + maybePlusMinusOne(4); diff --git a/monster_ai.cpp b/monster_ai.cpp index e9ceb2613..f60127778 100644 --- a/monster_ai.cpp +++ b/monster_ai.cpp @@ -37,7 +37,6 @@ #include "furniture.h" #include "furniture_factory.h" #include "file_path.h" -#include "ranged_weapon.h" class Behaviour { public: @@ -582,7 +581,7 @@ class Fighter : public Behaviour { if (auto move = considerBuffs()) return move; if (distance > 1) { - if (distance <= MAX_RANGED_DISTANCE) { + if (distance <= creature->getMaximumRange()) { if (MoveInfo move = getFireMove(enemyDir, other)) return move; if (MoveInfo move = getThrowMove(enemyDir, other)) diff --git a/ranged_weapon.cpp b/ranged_weapon.cpp index d4acd87dc..b587abf60 100644 --- a/ranged_weapon.cpp +++ b/ranged_weapon.cpp @@ -31,11 +31,11 @@ #include "event_listener.h" #include "vision.h" -SERIALIZE_DEF(RangedWeapon, damageAttr, projectileName, projectileViewId) +SERIALIZE_DEF(RangedWeapon, damageAttr, projectileName, projectileViewId, maxDistance) SERIALIZATION_CONSTRUCTOR_IMPL(RangedWeapon) -RangedWeapon::RangedWeapon(AttrType attr, const string& name, ViewId id) - : damageAttr(attr), projectileName(name), projectileViewId(id) {} +RangedWeapon::RangedWeapon(AttrType attr, const string& name, ViewId id, int dist) + : damageAttr(attr), projectileName(name), projectileViewId(id), maxDistance(dist) {} void RangedWeapon::fire(WCreature c, Vec2 dir) const { CHECK(dir.length8() == 1); @@ -59,8 +59,7 @@ void RangedWeapon::fire(WCreature c, Vec2 dir) const { pos.globalMessage("the " + projectileName + " hits the " + pos.getName()); break; } - if (distance >= MAX_RANGED_DISTANCE) - { + if (distance >= maxDistance) { pos.globalMessage("the " + projectileName + " falls short."); break; } @@ -71,3 +70,8 @@ void RangedWeapon::fire(WCreature c, Vec2 dir) const { AttrType RangedWeapon::getDamageAttr() const { return damageAttr; } + +int RangedWeapon::getMaxDistance() const { + return maxDistance; +} + diff --git a/ranged_weapon.h b/ranged_weapon.h index 7012f9d5e..d70d2d255 100644 --- a/ranged_weapon.h +++ b/ranged_weapon.h @@ -17,14 +17,14 @@ #include "util.h" #include "item.h" -#define MAX_RANGED_DISTANCE 10 class RangedWeapon { public: - RangedWeapon(AttrType damageAttr, const string& projectileName, ViewId projectileViewId); + RangedWeapon(AttrType damageAttr, const string& projectileName, ViewId projectileViewId, int maxDistance); void fire(WCreature c, Vec2 dir) const; AttrType getDamageAttr() const; + int getMaxDistance() const; SERIALIZATION_DECL(RangedWeapon); @@ -32,4 +32,5 @@ class RangedWeapon { AttrType SERIAL(damageAttr); string SERIAL(projectileName); ViewId SERIAL(projectileViewId); + int SERIAL(maxDistance); };