From 14b02ec5d2f3fc3cc84bec89f3f5c0141dafc5ee Mon Sep 17 00:00:00 2001 From: gsemaj Date: Mon, 11 Apr 2022 10:26:57 -0400 Subject: [PATCH] (WIP) Initial ICombatant draft --- src/Chunking.cpp | 8 ++++---- src/Entities.cpp | 13 +++++++++++++ src/Entities.hpp | 28 +++++++++++++++++++++++++--- src/Player.hpp | 6 +++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/Chunking.cpp b/src/Chunking.cpp index cbb439b..d26fe8d 100644 --- a/src/Chunking.cpp +++ b/src/Chunking.cpp @@ -77,7 +77,7 @@ void Chunking::untrackEntity(ChunkPos chunkPos, const EntityRef& ref) { void Chunking::addEntityToChunks(std::set chnks, const EntityRef& ref) { Entity *ent = ref.getEntity(); - bool alive = ent->isAlive(); + bool alive = ent->isExtant(); // TODO: maybe optimize this, potentially using AROUND packets? for (Chunk *chunk : chnks) { @@ -94,7 +94,7 @@ void Chunking::addEntityToChunks(std::set chnks, const EntityRef& ref) { } // notify this *player* of the existence of all visible Entities - if (ref.type == EntityType::PLAYER && other->isAlive()) { + if (ref.type == EntityType::PLAYER && other->isExtant()) { other->enterIntoViewOf(ref.sock); } @@ -109,7 +109,7 @@ void Chunking::addEntityToChunks(std::set chnks, const EntityRef& ref) { void Chunking::removeEntityFromChunks(std::set chnks, const EntityRef& ref) { Entity *ent = ref.getEntity(); - bool alive = ent->isAlive(); + bool alive = ent->isExtant(); // TODO: same as above for (Chunk *chunk : chnks) { @@ -126,7 +126,7 @@ void Chunking::removeEntityFromChunks(std::set chnks, const EntityRef& r } // notify this *player* of the departure of all visible Entities - if (ref.type == EntityType::PLAYER && other->isAlive()) { + if (ref.type == EntityType::PLAYER && other->isExtant()) { other->disappearFromViewOf(ref.sock); } diff --git a/src/Entities.cpp b/src/Entities.cpp index a859af2..51aaa27 100644 --- a/src/Entities.cpp +++ b/src/Entities.cpp @@ -103,6 +103,19 @@ sPCAppearanceData Player::getAppearanceData() { return data; } +// player combat methods; not sure if this is the right place to put them +void Player::takeDamage(EntityRef src, int amt) { + // stubbed +} + +void Player::heal(EntityRef src, int amt) { + // stubbed +} + +bool Player::isAlive() { + return HP > 0; +} + // TODO: this is less effiecient than it was, because of memset() void Player::enterIntoViewOf(CNSocket *sock) { INITSTRUCT(sP_FE2CL_PC_NEW, pkt); diff --git a/src/Entities.hpp b/src/Entities.hpp index 3e613ce..a39512a 100644 --- a/src/Entities.hpp +++ b/src/Entities.hpp @@ -26,7 +26,7 @@ struct Entity { // destructor must be virtual, apparently virtual ~Entity() {} - virtual bool isAlive() { return true; } + virtual bool isExtant() { return true; } // stubs virtual void enterIntoViewOf(CNSocket *sock) = 0; @@ -69,6 +69,20 @@ struct EntityRef { } }; +/* + * Interfaces + */ + +class ICombatant { +public: + ICombatant() {} + virtual ~ICombatant() {} + + virtual void takeDamage(EntityRef, int) = 0; + virtual void heal(EntityRef, int) = 0; + virtual bool isAlive() = 0; +}; + /* * Subclasses */ @@ -98,7 +112,7 @@ public: sNPCAppearanceData getAppearanceData(); }; -struct CombatNPC : public BaseNPC { +struct CombatNPC : public BaseNPC, public ICombatant { int maxHealth = 0; int spawnX = 0; int spawnY = 0; @@ -120,6 +134,14 @@ struct CombatNPC : public BaseNPC { _stepAI(this, currTime); } + virtual void takeDamage(EntityRef src, int amt) override { + // stubbed + } + + virtual void heal(EntityRef src, int amt) override { + // stubbed + } + virtual bool isAlive() override { return hp > 0; } }; @@ -137,7 +159,7 @@ struct Egg : public BaseNPC { kind = EntityType::EGG; } - virtual bool isAlive() override { return !dead; } + virtual bool isExtant() override { return !dead; } virtual void enterIntoViewOf(CNSocket *sock) override; virtual void disappearFromViewOf(CNSocket *sock) override; diff --git a/src/Player.hpp b/src/Player.hpp index 0c425d1..6fb451e 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -11,7 +11,7 @@ #define PC_MAXHEALTH(level) (925 + 75 * (level)) -struct Player : public Entity { +struct Player : public Entity, public ICombatant { int accountId = 0; int accountLevel = 0; // permission level (see CN_ACCOUNT_LEVEL enums) int32_t iID = 0; @@ -90,5 +90,9 @@ struct Player : public Entity { virtual void enterIntoViewOf(CNSocket *sock) override; virtual void disappearFromViewOf(CNSocket *sock) override; + virtual void takeDamage(EntityRef src, int amt) override; + virtual void heal(EntityRef src, int amt) override; + virtual bool isAlive() override; + sPCAppearanceData getAppearanceData(); };