From a49c004278b0e300521e9cedf44a46ac843a958b Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 13 Apr 2013 21:02:10 +0000 Subject: Rewritten entities so that they are owned by individual chunks and ticked within their chunk's Tick() git-svn-id: http://mc-server.googlecode.com/svn/trunk@1385 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Mobs/AggressiveMonster.cpp | 18 +++++++------- source/Mobs/AggressiveMonster.h | 6 ++--- source/Mobs/Cavespider.cpp | 6 ++--- source/Mobs/Cavespider.h | 2 +- source/Mobs/Enderman.cpp | 15 ------------ source/Mobs/Enderman.h | 1 - source/Mobs/Monster.cpp | 51 ++++++++++++++++++--------------------- source/Mobs/Monster.h | 16 ++++++------ source/Mobs/PassiveMonster.cpp | 8 +++--- source/Mobs/PassiveMonster.h | 2 +- source/Mobs/Skeleton.cpp | 6 ++--- source/Mobs/Skeleton.h | 2 +- source/Mobs/Squid.cpp | 7 +++--- source/Mobs/Squid.h | 2 +- source/Mobs/Zombie.cpp | 6 ++--- source/Mobs/Zombie.h | 2 +- source/Mobs/Zombiepigman.cpp | 6 ++--- source/Mobs/Zombiepigman.h | 2 +- 18 files changed, 69 insertions(+), 89 deletions(-) (limited to 'source/Mobs') diff --git a/source/Mobs/AggressiveMonster.cpp b/source/Mobs/AggressiveMonster.cpp index 0a5461837..de73e6123 100644 --- a/source/Mobs/AggressiveMonster.cpp +++ b/source/Mobs/AggressiveMonster.cpp @@ -23,9 +23,9 @@ cAggressiveMonster::cAggressiveMonster(const AString & a_ConfigName, char a_Prot // What to do if in Chasing State -void cAggressiveMonster::InStateChasing(float a_Dt, MTRand & a_TickRandom) +void cAggressiveMonster::InStateChasing(float a_Dt) { - super::InStateChasing(a_Dt, a_TickRandom); + super::InStateChasing(a_Dt); m_ChaseTime += a_Dt; if (m_Target != NULL) { @@ -58,9 +58,9 @@ void cAggressiveMonster::InStateChasing(float a_Dt, MTRand & a_TickRandom) -void cAggressiveMonster::EventSeePlayer(cEntity * a_Entity, MTRand & a_TickRandom) +void cAggressiveMonster::EventSeePlayer(cEntity * a_Entity) { - super::EventSeePlayer(a_Entity, a_TickRandom); + super::EventSeePlayer(a_Entity); m_EMState = CHASING; } @@ -68,26 +68,26 @@ void cAggressiveMonster::EventSeePlayer(cEntity * a_Entity, MTRand & a_TickRando -void cAggressiveMonster::Tick(float a_Dt, MTRand & a_TickRandom) +void cAggressiveMonster::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + super::Tick(a_Dt, a_Chunk); m_SeePlayerInterval += a_Dt; if (m_SeePlayerInterval > 1) { - int rem = a_TickRandom.randInt() % 3 + 1; // Check most of the time but miss occasionally + int rem = m_World->GetTickRandomNumber(3) + 1; // Check most of the time but miss occasionally m_SeePlayerInterval = 0.0; if (rem >= 2) { if (m_EMState == CHASING) { - CheckEventLostPlayer(a_TickRandom); + CheckEventLostPlayer(); } else { - CheckEventSeePlayer(a_TickRandom); + CheckEventSeePlayer(); } } } diff --git a/source/Mobs/AggressiveMonster.h b/source/Mobs/AggressiveMonster.h index a9303a975..ed21c6344 100644 --- a/source/Mobs/AggressiveMonster.h +++ b/source/Mobs/AggressiveMonster.h @@ -15,10 +15,10 @@ class cAggressiveMonster : public: cAggressiveMonster(const AString & a_ConfigName, char a_ProtocolMobType, const AString & a_SoundHurt, const AString & a_SoundDeath); - virtual void Tick (float a_Dt, MTRand & a_TickRandom) override; - virtual void InStateChasing(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick (float a_Dt, cChunk & a_Chunk) override; + virtual void InStateChasing(float a_Dt) override; - virtual void EventSeePlayer(cEntity *, MTRand & a_TickRandom) override; + virtual void EventSeePlayer(cEntity *) override; protected: float m_ChaseTime; diff --git a/source/Mobs/Cavespider.cpp b/source/Mobs/Cavespider.cpp index b63e28f1a..e4030af57 100644 --- a/source/Mobs/Cavespider.cpp +++ b/source/Mobs/Cavespider.cpp @@ -16,11 +16,11 @@ cCavespider::cCavespider(void) : -void cCavespider::Tick(float a_Dt, MTRand & a_TickRandom) +void cCavespider::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + super::Tick(a_Dt, a_Chunk); - // TODO: Check vanilla if cavespiders really get passive during the day + // TODO: Check vanilla if cavespiders really get passive during the day / in daylight m_EMPersonality = (GetWorld()->GetTimeOfDay() < (12000 + 1000)) ? PASSIVE : AGGRESSIVE; } diff --git a/source/Mobs/Cavespider.h b/source/Mobs/Cavespider.h index b02318a0f..00a4e16df 100644 --- a/source/Mobs/Cavespider.h +++ b/source/Mobs/Cavespider.h @@ -17,7 +17,7 @@ public: CLASS_PROTODEF(cCaveSpider); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void GetDrops(cItems & a_Drops, cPawn * a_Killer = NULL) override; } ; diff --git a/source/Mobs/Enderman.cpp b/source/Mobs/Enderman.cpp index d7bbaa144..356815be0 100644 --- a/source/Mobs/Enderman.cpp +++ b/source/Mobs/Enderman.cpp @@ -16,21 +16,6 @@ cEnderman::cEnderman(void) : -void cEnderman::Tick(float a_Dt, MTRand & a_TickRandom) -{ - cMonster::Tick(a_Dt, a_TickRandom); - - // TODO Same as stated in cSkeleton - if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && (GetMetaData() != BURNING)) - { - SetMetaData(BURNING); // BURN, BABY, BURN! >:D - } -} - - - - - void cEnderman::GetDrops(cItems & a_Drops, cPawn * a_Killer) { AddRandomDropItem(a_Drops, 0, 1, E_ITEM_ENDER_PEARL); diff --git a/source/Mobs/Enderman.h b/source/Mobs/Enderman.h index 0703ca25e..44719ea5a 100644 --- a/source/Mobs/Enderman.h +++ b/source/Mobs/Enderman.h @@ -17,7 +17,6 @@ public: CLASS_PROTODEF(cEnderman); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; virtual void GetDrops(cItems & a_Drops, cPawn * a_Killer = NULL) override; } ; diff --git a/source/Mobs/Monster.cpp b/source/Mobs/Monster.cpp index 77bad8f51..8781858a6 100644 --- a/source/Mobs/Monster.cpp +++ b/source/Mobs/Monster.cpp @@ -87,9 +87,9 @@ bool cMonster::ReachedDestination() -void cMonster::Tick(float a_Dt, MTRand & a_TickRandom) +void cMonster::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + super::Tick(a_Dt, a_Chunk); if (m_Health <= 0) { @@ -145,7 +145,6 @@ void cMonster::Tick(float a_Dt, MTRand & a_TickRandom) HandlePhysics(a_Dt); BroadcastMovementUpdate(); - MoveToCorrectChunk(); Vector3d Distance = m_Destination - GetPosition(); if (Distance.SqrLength() > 0.1f) @@ -163,20 +162,20 @@ void cMonster::Tick(float a_Dt, MTRand & a_TickRandom) case IDLE: { // If enemy passive we ignore checks for player visibility - InStateIdle(a_Dt, a_TickRandom); + InStateIdle(a_Dt); break; } case CHASING: { // If we do not see a player anymore skip chasing action - InStateChasing(a_Dt, a_TickRandom); + InStateChasing(a_Dt); break; } case ESCAPING: { - InStateEscaping(a_Dt, a_TickRandom); + InStateEscaping(a_Dt); break; } } // switch (m_EMState) @@ -227,8 +226,6 @@ void cMonster::ReplicateMovement() m_LastPosZ = GetPosZ(); m_bDirtyPosition = false; } - - MoveToCorrectChunk(); } @@ -374,14 +371,14 @@ void cMonster::SetState(const AString & a_State) //Checks to see if EventSeePlayer should be fired //monster sez: Do I see the player -void cMonster::CheckEventSeePlayer(MTRand & a_TickRandom) +void cMonster::CheckEventSeePlayer(void) { // TODO: Rewrite this to use cWorld's DoWithPlayers() cPlayer * Closest = FindClosestPlayer(); if (Closest != NULL) { - EventSeePlayer(Closest, a_TickRandom); + EventSeePlayer(Closest); } } @@ -389,10 +386,8 @@ void cMonster::CheckEventSeePlayer(MTRand & a_TickRandom) -void cMonster::CheckEventLostPlayer(MTRand & a_TickRandom) +void cMonster::CheckEventLostPlayer(void) { - UNUSED(a_TickRandom); - Vector3f pos; cTracer LineOfSight(GetWorld()); @@ -416,12 +411,10 @@ void cMonster::CheckEventLostPlayer(MTRand & a_TickRandom) // What to do if player is seen // default to change state to chasing -void cMonster::EventSeePlayer(cEntity * a_SeenPlayer, MTRand & a_TickRandom) +void cMonster::EventSeePlayer(cEntity * a_SeenPlayer) { - UNUSED(a_TickRandom); - m_Target = a_SeenPlayer; - AddReference( m_Target ); + AddReference(m_Target); } @@ -431,7 +424,7 @@ void cMonster::EventSeePlayer(cEntity * a_SeenPlayer, MTRand & a_TickRandom) void cMonster::EventLosePlayer(void) { Dereference(m_Target); - m_Target = 0; + m_Target = NULL; m_EMState = IDLE; } @@ -440,24 +433,28 @@ void cMonster::EventLosePlayer(void) // What to do if in Idle State -void cMonster::InStateIdle(float a_Dt, MTRand & a_TickRandom) +void cMonster::InStateIdle(float a_Dt) { idle_interval += a_Dt; if (idle_interval > 1) { // at this interval the results are predictable - int rem = (a_TickRandom.randInt() % 6) + 1; + int rem = m_World->GetTickRandomNumber(6) + 1; // LOGD("Moving: int: %3.3f rem: %i",idle_interval,rem); idle_interval -= 1; // So nothing gets dropped when the server hangs for a few seconds Vector3f Dist; - Dist.x = (float)((a_TickRandom.randInt() % 11) - 5); - Dist.z = (float)((a_TickRandom.randInt() % 11) - 5); + Dist.x = (float)(m_World->GetTickRandomNumber(10) - 5); + Dist.z = (float)(m_World->GetTickRandomNumber(10) - 5); if ((Dist.SqrLength() > 2) && (rem >= 3)) { m_Destination.x = (float)(GetPosX() + Dist.x); m_Destination.z = (float)(GetPosZ() + Dist.z); - m_Destination.y = (float)GetWorld()->GetHeight((int)m_Destination.x, (int)m_Destination.z) + 1.2f; - MoveToPosition(m_Destination); + int PosY; + if (m_World->TryGetHeight((int)m_Destination.x, (int)m_Destination.z, PosY)) + { + m_Destination.y = (float)PosY + 1.2f; + MoveToPosition(m_Destination); + } } } } @@ -468,10 +465,9 @@ void cMonster::InStateIdle(float a_Dt, MTRand & a_TickRandom) // What to do if in Chasing State // This state should always be defined in each child class -void cMonster::InStateChasing(float a_Dt, MTRand & a_TickRandom) +void cMonster::InStateChasing(float a_Dt) { UNUSED(a_Dt); - UNUSED(a_TickRandom); } @@ -479,10 +475,9 @@ void cMonster::InStateChasing(float a_Dt, MTRand & a_TickRandom) // What to do if in Escaping State -void cMonster::InStateEscaping(float a_Dt, MTRand & a_TickRandom) +void cMonster::InStateEscaping(float a_Dt) { UNUSED(a_Dt); - UNUSED(a_TickRandom); if (m_Target != NULL) { diff --git a/source/Mobs/Monster.h b/source/Mobs/Monster.h index 5f30168f9..c7abeb243 100644 --- a/source/Mobs/Monster.h +++ b/source/Mobs/Monster.h @@ -24,6 +24,7 @@ class cMonster : typedef cPawn super; public: // tolua_end + float m_SightDistance; /** Creates the mob object. * If a_ConfigName is not empty, the configuration is loaded using GetMonsterConfig() @@ -36,7 +37,7 @@ public: virtual void SpawnOn(cClientHandle & a_ClientHandle) override; - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void HandlePhysics(float a_Dt); virtual void ReplicateMovement(void); @@ -53,20 +54,19 @@ public: const char * GetState(); void SetState(const AString & str); - virtual void CheckEventSeePlayer(MTRand & a_TickRandom); - virtual void EventSeePlayer(cEntity *, MTRand & a_TickRandom); - float m_SightDistance; + virtual void CheckEventSeePlayer(void); + virtual void EventSeePlayer(cEntity * a_Player); virtual cPlayer * FindClosestPlayer(); // non static is easier. also virtual so other mobs can implement their own searching algo /// Reads the monster configuration for the specified monster name and assigns it to this object. void GetMonsterConfig(const AString & a_Name); virtual void EventLosePlayer(void); - virtual void CheckEventLostPlayer(MTRand & a_TickRandom); + virtual void CheckEventLostPlayer(void); - virtual void InStateIdle (float a_Dt, MTRand & a_TickRandom); - virtual void InStateChasing (float a_Dt, MTRand & a_TickRandom); - virtual void InStateEscaping(float a_Dt, MTRand & a_TickRandom); + virtual void InStateIdle (float a_Dt); + virtual void InStateChasing (float a_Dt); + virtual void InStateEscaping(float a_Dt); virtual void Attack(float a_Dt); int GetMobType() {return m_MobType;} diff --git a/source/Mobs/PassiveMonster.cpp b/source/Mobs/PassiveMonster.cpp index aa2e6c118..22bc17f6f 100644 --- a/source/Mobs/PassiveMonster.cpp +++ b/source/Mobs/PassiveMonster.cpp @@ -31,22 +31,22 @@ void cPassiveMonster::DoTakeDamage(TakeDamageInfo & a_TDI) -void cPassiveMonster::Tick(float a_Dt, MTRand & a_TickRandom) +void cPassiveMonster::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + super::Tick(a_Dt, a_Chunk); m_SeePlayerInterval += a_Dt; if (m_SeePlayerInterval > 1) // Check every second { - int rem = a_TickRandom.randInt() % 3 + 1; // Check most of the time but miss occasionally + int rem = m_World->GetTickRandomNumber(3) + 1; // Check most of the time but miss occasionally m_SeePlayerInterval = 0.0; if (rem >= 2) { if (m_EMState == ESCAPING) { - CheckEventLostPlayer(a_TickRandom); + CheckEventLostPlayer(); } } } diff --git a/source/Mobs/PassiveMonster.h b/source/Mobs/PassiveMonster.h index 66e718ec3..8abbd8687 100644 --- a/source/Mobs/PassiveMonster.h +++ b/source/Mobs/PassiveMonster.h @@ -15,7 +15,7 @@ class cPassiveMonster : public: cPassiveMonster(const AString & a_ConfigName, char a_ProtocolMobType, const AString & a_SoundHurt, const AString & a_SoundDeath); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; /// When hit by someone, run away virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; diff --git a/source/Mobs/Skeleton.cpp b/source/Mobs/Skeleton.cpp index 785584fcd..b2b0c325d 100644 --- a/source/Mobs/Skeleton.cpp +++ b/source/Mobs/Skeleton.cpp @@ -16,13 +16,13 @@ cSkeleton::cSkeleton(void) : -void cSkeleton::Tick(float a_Dt, MTRand & a_TickRandom) +void cSkeleton::Tick(float a_Dt, cChunk & a_Chunk) { - cMonster::Tick(a_Dt, a_TickRandom); + cMonster::Tick(a_Dt, a_Chunk); // TODO Outsource // TODO should do SkyLight check, mobs in the dark donīt burn - if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && (GetMetaData() != BURNING)) + if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && !IsBurning()) { SetMetaData(BURNING); // BURN, BABY, BURN! >:D } diff --git a/source/Mobs/Skeleton.h b/source/Mobs/Skeleton.h index a02d1b5a6..e0b537cc8 100644 --- a/source/Mobs/Skeleton.h +++ b/source/Mobs/Skeleton.h @@ -17,7 +17,7 @@ public: CLASS_PROTODEF(cSkeleton); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void GetDrops(cItems & a_Drops, cPawn * a_Killer = NULL) override; } ; diff --git a/source/Mobs/Squid.cpp b/source/Mobs/Squid.cpp index 808bd359e..78dd37393 100644 --- a/source/Mobs/Squid.cpp +++ b/source/Mobs/Squid.cpp @@ -27,14 +27,15 @@ void cSquid::GetDrops(cItems & a_Drops, cPawn * a_Killer) -void cSquid::Tick(float a_Dt, MTRand & a_TickRandom) +void cSquid::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + // TODO: Rewrite this function to use a_Chunk instead of m_World + super::Tick(a_Dt, a_Chunk); Vector3d Pos = GetPosition(); // TODO: Not a real behavior, but cool :D - if (!IsBlockWater(GetWorld()->GetBlock((int) Pos.x, (int) Pos.y, (int) Pos.z)) && GetMetaData() != BURNING) + if (!IsBlockWater(GetWorld()->GetBlock((int) Pos.x, (int) Pos.y, (int) Pos.z)) && !IsBurning()) { SetMetaData(BURNING); } diff --git a/source/Mobs/Squid.h b/source/Mobs/Squid.h index 158fbea4a..de205f397 100644 --- a/source/Mobs/Squid.h +++ b/source/Mobs/Squid.h @@ -15,7 +15,7 @@ class cSquid : public: cSquid(); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; CLASS_PROTODEF(cSquid); diff --git a/source/Mobs/Zombie.cpp b/source/Mobs/Zombie.cpp index 8038c51f6..cf0391b4c 100644 --- a/source/Mobs/Zombie.cpp +++ b/source/Mobs/Zombie.cpp @@ -16,12 +16,12 @@ cZombie::cZombie(void) : -void cZombie::Tick(float a_Dt, MTRand & a_TickRandom) +void cZombie::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + super::Tick(a_Dt, a_Chunk); // TODO Same as in cSkeleton :D - if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && (GetMetaData() != BURNING)) + if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && !IsBurning()) { SetMetaData(BURNING); // BURN, BABY, BURN! >:D } diff --git a/source/Mobs/Zombie.h b/source/Mobs/Zombie.h index c4988af72..5813a900b 100644 --- a/source/Mobs/Zombie.h +++ b/source/Mobs/Zombie.h @@ -16,7 +16,7 @@ public: CLASS_PROTODEF(cZombie); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void GetDrops(cItems & a_Drops, cPawn * a_Killer = NULL) override; } ; diff --git a/source/Mobs/Zombiepigman.cpp b/source/Mobs/Zombiepigman.cpp index a542723c4..40fb0a593 100644 --- a/source/Mobs/Zombiepigman.cpp +++ b/source/Mobs/Zombiepigman.cpp @@ -16,12 +16,12 @@ cZombiepigman::cZombiepigman(void) : -void cZombiepigman::Tick(float a_Dt, MTRand & a_TickRandom) +void cZombiepigman::Tick(float a_Dt, cChunk & a_Chunk) { - super::Tick(a_Dt, a_TickRandom); + super::Tick(a_Dt, a_Chunk); // TODO Same as noticed in cSkeleton AND Do they really burn by sun?? :D In the neather is no sun :D - if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && (GetMetaData() != BURNING)) + if ((GetWorld()->GetTimeOfDay() < (12000 + 1000)) && !IsBurning()) { SetMetaData(BURNING); // BURN, BABY, BURN! >:D } diff --git a/source/Mobs/Zombiepigman.h b/source/Mobs/Zombiepigman.h index 201416dbd..a8c2e429d 100644 --- a/source/Mobs/Zombiepigman.h +++ b/source/Mobs/Zombiepigman.h @@ -17,7 +17,7 @@ public: CLASS_PROTODEF(cZombiepigman); - virtual void Tick(float a_Dt, MTRand & a_TickRandom) override; + virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void GetDrops(cItems & a_Drops, cPawn * a_Killer = NULL) override; virtual void KilledBy(cPawn * a_Killer) override; } ; -- cgit v1.2.3