[refactor] Remaining ICombatant implementation

This commit is contained in:
gsemaj
2022-04-13 15:09:43 -04:00
committed by gsemaj
parent 07fe8ca367
commit 4b834579c5
19 changed files with 412 additions and 1196 deletions

View File

@@ -6,7 +6,7 @@
#include <stdint.h>
#include <set>
enum class EntityType : uint8_t {
enum EntityKind {
INVALID,
PLAYER,
SIMPLE_NPC,
@@ -27,7 +27,7 @@ enum class AIState {
class Chunk;
struct Entity {
EntityType kind = EntityType::INVALID;
EntityKind kind = EntityKind::INVALID;
int x = 0, y = 0, z = 0;
uint64_t instanceID = 0;
ChunkPos chunkPos = {};
@@ -44,7 +44,7 @@ struct Entity {
};
struct EntityRef {
EntityType type;
EntityKind kind;
union {
CNSocket *sock;
int32_t id;
@@ -57,10 +57,10 @@ struct EntityRef {
Entity *getEntity() const;
bool operator==(const EntityRef& other) const {
if (type != other.type)
if (kind != other.kind)
return false;
if (type == EntityType::PLAYER)
if (kind == EntityKind::PLAYER)
return sock == other.sock;
return id == other.id;
@@ -68,21 +68,20 @@ struct EntityRef {
// arbitrary ordering
bool operator<(const EntityRef& other) const {
if (type == other.type) {
if (type == EntityType::PLAYER)
if (kind == other.kind) {
if (kind == EntityKind::PLAYER)
return sock < other.sock;
else
return id < other.id;
}
return type < other.type;
return kind < other.kind;
}
};
/*
* Interfaces
*/
class ICombatant {
public:
ICombatant() {}
@@ -93,7 +92,6 @@ public:
virtual bool isAlive() = 0;
virtual int getCurrentHP() = 0;
virtual int32_t getID() = 0;
virtual void step(time_t currTime) = 0;
};
@@ -134,11 +132,17 @@ struct CombatNPC : public BaseNPC, public ICombatant {
AIState state = AIState::INACTIVE;
int playersInView = 0; // for optimizing away AI in empty chunks
std::map<AIState, void (*)(CombatNPC*, time_t)> stateHandlers;
std::map<AIState, void (*)(CombatNPC*, EntityRef)> transitionHandlers;
CombatNPC(int x, int y, int z, int angle, uint64_t iID, int t, int id, int maxHP)
: BaseNPC(angle, iID, t, id), maxHealth(maxHP) {
spawnX = x;
spawnY = y;
spawnZ = z;
stateHandlers[AIState::INACTIVE] = {};
transitionHandlers[AIState::INACTIVE] = {};
}
virtual bool isExtant() override { return hp > 0; }
@@ -148,19 +152,9 @@ struct CombatNPC : public BaseNPC, public ICombatant {
virtual bool isAlive() override;
virtual int getCurrentHP() override;
virtual int32_t getID() override;
virtual void step(time_t currTime) override;
virtual void roamingStep(time_t currTime) {} // no-ops by default
virtual void combatStep(time_t currTime) {}
virtual void retreatStep(time_t currTime) {}
virtual void deadStep(time_t currTime) {}
virtual void transition(AIState newState, EntityRef src);
virtual void onInactive() {} // no-ops by default
virtual void onRoamStart() {}
virtual void onCombatStart(EntityRef src) {}
virtual void onRetreat() {}
virtual void onDeath(EntityRef src) {}
};
// Mob is in MobAI.hpp, Player is in Player.hpp
@@ -173,7 +167,7 @@ struct Egg : public BaseNPC {
Egg(uint64_t iID, int t, int32_t id, bool summon)
: BaseNPC(0, iID, t, id) {
summoned = summon;
kind = EntityType::EGG;
kind = EntityKind::EGG;
}
virtual bool isExtant() override { return !dead; }
@@ -185,7 +179,7 @@ struct Egg : public BaseNPC {
struct Bus : public BaseNPC {
Bus(int angle, uint64_t iID, int t, int id) :
BaseNPC(angle, iID, t, id) {
kind = EntityType::BUS;
kind = EntityKind::BUS;
loopingPath = true;
}