Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cooldown for ranged attacks without bugs #1138

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
template <class Archive>
void Creature::serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(OwnedObject<Creature>) & SUBCLASS(Renderable) & SUBCLASS(UniqueEntity);
ar(attributes, position, equipment, shortestPath, knownHiding, tribe, morale);
ar(attributes, position, ammoCooldownPoints, equipment, shortestPath, knownHiding, tribe, morale);
ar(deathTime, hidden, lastMoveCounter, captureHealth);
ar(deathReason, nextPosIntent, globalTime);
ar(unknownAttackers, privateEnemies, holding);
Expand Down Expand Up @@ -247,6 +247,8 @@ optional<MovementInfo> Creature::spendTime(TimeInterval t) {
MovementInfo ret(Vec2(0, 0), *getLocalTime(), *getLocalTime() + t, 0, MovementInfo::MOVE);
lastMoveCounter = ret.moveCounter = position.getModel()->getMoveCounter();
if (!isDead()) {
if (ammoCooldownPoints < 141)
ammoCooldownPoints++;
if (isAffected(LastingEffect::SPEED) && t == 1_visible) {
if (m->getTimeQueue().hasExtraMove(this))
ret.tBegin += 0.5;
Expand Down Expand Up @@ -1308,7 +1310,7 @@ CreatureAction Creature::payFor(const vector<WItem>& items) const {
.append([=](WCreature) { for (auto it : items) it->setShopkeeper(nullptr); });
}

CreatureAction Creature::fire(Vec2 direction) const {
CreatureAction Creature::fire(Vec2 direction) {
CHECK(direction.length8() == 1);
if (getEquipment().getItems(ItemIndex::RANGED_WEAPON).empty())
return CreatureAction("You need a ranged weapon.");
Expand All @@ -1319,7 +1321,18 @@ CreatureAction Creature::fire(Vec2 direction) const {
return CreatureAction(this, [=](WCreature self) {
auto& weapon = *self->getEquipment().getSlotItems(EquipmentSlot::RANGED_WEAPON).getOnlyElement()
->getRangedWeapon();
weapon.fire(self, direction);
if (ammoCooldownPoints < 10) {
getPosition().globalMessage("Getting ammunition.");
}
else {
weapon.fire(self, direction);
if (ammoCooldownPoints < 10) {
ammoCooldownPoints = -1;
}
else {
ammoCooldownPoints = ammoCooldownPoints - 10;
}
}
self->spendTime();
});
}
Expand Down Expand Up @@ -1818,3 +1831,6 @@ optional<Creature::CombatIntentInfo> Creature::getLastCombatIntent() const {
return lastCombatIntent;
}

int Creature::getAmmoCooldownPoints() const {
return ammoCooldownPoints;
}
4 changes: 3 additions & 1 deletion creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Creature : public Renderable, public UniqueEntity<Creature>, public OwnedO
bool canSee(Vec2) const;
bool isEnemy(WConstCreature) const;
void tick();
int getAmmoCooldownPoints() const;

const CreatureName& getName() const;
CreatureName& getName();
Expand Down Expand Up @@ -168,7 +169,7 @@ class Creature : public Renderable, public UniqueEntity<Creature>, public OwnedO
CreatureAction stealFrom(Vec2 direction, const vector<WItem>&) const;
CreatureAction give(WCreature whom, vector<WItem> items) const;
CreatureAction payFor(const vector<WItem>&) const;
CreatureAction fire(Vec2 direction) const;
CreatureAction fire(Vec2 direction);
CreatureAction construct(Vec2 direction, FurnitureType) const;
CreatureAction whip(const Position&) const;
bool canConstruct(FurnitureType) const;
Expand Down Expand Up @@ -298,6 +299,7 @@ class Creature : public Renderable, public UniqueEntity<Creature>, public OwnedO

HeapAllocated<CreatureAttributes> SERIAL(attributes);
Position SERIAL(position);
int SERIAL(ammoCooldownPoints);
HeapAllocated<Equipment> SERIAL(equipment);
HeapAllocated<optional<LevelShortestPath>> SERIAL(shortestPath);
EntitySet<Creature> SERIAL(knownHiding);
Expand Down
1 change: 1 addition & 0 deletions monster_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ class Fighter : public Behaviour {
auto weapon = c->getEquipment().getSlotItems(EquipmentSlot::RANGED_WEAPON);
if (weapon.empty())
return 0;
if (c->getAmmoCooldownPoints() < 1) return 0;
return weapon.getOnlyElement()->getRangedWeapon()->getMaxDistance();
}

Expand Down