From 92e0ba66459af6a5ac7ef53fb24a681db100f901 Mon Sep 17 00:00:00 2001 From: "lapayo94@gmail.com" Date: Mon, 26 Dec 2011 18:20:00 +0000 Subject: =?UTF-8?q?-=20improved=20ClosestPlayerDetection=20(Really=20the?= =?UTF-8?q?=20closest=20now=20;))=20-=20renamed=20function=20because=20the?= =?UTF-8?q?=20old=20one=20wasn=C2=B4t=20listing=20-=20improved=20some=20ti?= =?UTF-8?q?me=20vars=20so=20no=20move=20gets=20dropped=20when=20the=20serv?= =?UTF-8?q?er=20laggs=20for=20a=20few=20seconds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://mc-server.googlecode.com/svn/trunk@124 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cMonster.cpp | 74 ++++++++++++++++++++++++++++++++--------------------- source/cMonster.h | 2 +- 2 files changed, 46 insertions(+), 30 deletions(-) (limited to 'source') diff --git a/source/cMonster.cpp b/source/cMonster.cpp index 217cd5367..2f7567b15 100644 --- a/source/cMonster.cpp +++ b/source/cMonster.cpp @@ -154,6 +154,12 @@ void cMonster::Tick(float a_Dt) Distance *= 3; m_Speed->x = Distance.x; m_Speed->z = Distance.z; + + if (m_EMState == ESCAPING) + { //Runs Faster when escaping :D otherwise they just walk away + m_Speed->x *= 2.f; + m_Speed->z *= 2.f; + } } else { @@ -383,7 +389,12 @@ void cMonster::SetState(const char* a_str) //monster sez: Do I see the player void cMonster::CheckEventSeePlayer() { - cMonster::ListClosePlayers(this); + cPlayer *Closest = FindClosestPlayer(); + + if(Closest) + { + EventSeePlayer(Closest); + } } void cMonster::CheckEventLostPlayer() @@ -423,7 +434,7 @@ void cMonster::InStateIdle(float a_Dt) { MTRand r1; int rem = r1.randInt()%6 + 1; //LOG("Moving: int: %3.3f rem: %i",idle_interval,rem); - idle_interval = 0; + idle_interval -= 1; //So nothing gets dropped when the server hangs for a few seconds Vector3f Dist; Dist.x = (float)((r1.randInt()%11)-5); Dist.z = (float)((r1.randInt()%11)-5); @@ -444,7 +455,7 @@ void cMonster::InStateBurning(float a_Dt) { char bblock = GetWorld()->GetBlock( (int)m_Pos->x, (int)m_Pos->y +1, (int)m_Pos->z ); if(m_FireDamageInterval > 1) { - m_FireDamageInterval = 0; + m_FireDamageInterval -= 1; TakeDamage(1, this); m_BurnPeriod++; @@ -527,42 +538,47 @@ void cMonster::ListMonsters() { cRoot::Get()->GetWorld()->UnlockEntities(); } -//Checks for Players close by and if they are visible -void cMonster::ListClosePlayers(cMonster *m) { - int tries = 0; - cTracer LineOfSight(cRoot::Get()->GetWorld() ); - cWorld::EntityList Entities = cRoot::Get()->GetWorld()->GetEntities(); - for( cWorld::EntityList::iterator itr = Entities.begin(); itr != Entities.end(); ++itr) { - tries++; - if((*itr)->GetEntityType() == cEntity::E_PLAYER){ - Vector3f pos = (*itr)->GetPosition(); - if((pos - *(m->m_Pos)).Length() <= m->m_SightDistance){ - if(!LineOfSight.Trace(*(m->m_Pos),(pos - *(m->m_Pos)),(int)(pos - *(m->m_Pos)).Length())) - { - m->EventSeePlayer(*itr); - return; //get the first one in sight later we can reiterate and check - //for the closest out of all that match and make it more realistic - - } - } - - } - if(tries > 100) +//Checks for Players close by and if they are visible return the closest +cPlayer *cMonster::FindClosestPlayer() +{ + cTracer LineOfSight(cRoot::Get()->GetWorld()); + cWorld::PlayerList Players = cRoot::Get()->GetWorld()->GetAllPlayers(); + + float ClosestDistance = m_SightDistance + 1.f; //Something that is higher than the max :D + cPlayer* ClosestPlayer = 0; + + for( cWorld::PlayerList::iterator itr = Players.begin(); itr != Players.end(); ++itr) + { + Vector3f Pos = (*itr)->GetPosition(); + float Distance = (Pos - *(m_Pos)).Length(); + + if(Distance <= m_SightDistance) { - m->EventLosePlayer(); - return; + if(!LineOfSight.Trace(*(m_Pos),(Pos - *(m_Pos)),(int)(Pos - *(m_Pos)).Length())) + { + if(Distance < ClosestDistance) + { + ClosestDistance = Distance; + ClosestPlayer = *itr; + } + } } } + return ClosestPlayer; } -void cMonster::GetMonsterConfig(const char* pm_name) { +void cMonster::GetMonsterConfig(const char* pm_name) +{ cRoot::Get()->GetMonsterConfig()->Get()->AssignAttributes(this, pm_name); } -void cMonster::SetAttackRate(int ar) { +void cMonster::SetAttackRate(int ar) +{ m_AttackRate = (float)ar; } -void cMonster::SetAttackRange(float ar) { + +void cMonster::SetAttackRange(float ar) +{ m_AttackRange = ar; } void cMonster::SetAttackDamage(float ad) { diff --git a/source/cMonster.h b/source/cMonster.h index 6523c2e52..90340311e 100644 --- a/source/cMonster.h +++ b/source/cMonster.h @@ -35,7 +35,7 @@ public: virtual void CheckEventSeePlayer(); virtual void EventSeePlayer(cEntity *); float m_SightDistance; - static void ListClosePlayers(cMonster *); + virtual cPlayer *FindClosestPlayer(); //non static is easier. also virtual so other mobs can implement their own searching algo virtual void GetMonsterConfig(const char* pm_name); virtual void EventLosePlayer(); virtual void CheckEventLostPlayer(); -- cgit v1.2.3