2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 13:53:08 -07:00
|
|
|
#include "Monster.h"
|
2012-09-23 15:09:57 -07:00
|
|
|
#include "../Root.h"
|
|
|
|
#include "../Server.h"
|
|
|
|
#include "../ClientHandle.h"
|
|
|
|
#include "../World.h"
|
|
|
|
#include "../Player.h"
|
2012-09-23 13:53:08 -07:00
|
|
|
#include "../Defines.h"
|
2012-09-23 15:09:57 -07:00
|
|
|
#include "../MonsterConfig.h"
|
2012-09-23 13:53:08 -07:00
|
|
|
#include "../MersenneTwister.h"
|
|
|
|
|
|
|
|
#include "../Vector3f.h"
|
|
|
|
#include "../Vector3i.h"
|
|
|
|
#include "../Vector3d.h"
|
|
|
|
|
2012-09-23 15:09:57 -07:00
|
|
|
#include "../Tracer.h"
|
2012-12-22 01:39:13 -08:00
|
|
|
// #include "../../iniFile/iniFile.h"
|
2012-09-23 13:53:08 -07:00
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 01:39:13 -08:00
|
|
|
cMonster::cMonster(const AString & a_ConfigName, char a_ProtocolMobType, const AString & a_SoundHurt, const AString & a_SoundDeath)
|
2012-12-21 04:52:14 -08:00
|
|
|
: super(etMob)
|
|
|
|
, m_Target(NULL)
|
2012-06-14 06:06:06 -07:00
|
|
|
, m_bMovingToDestination(false)
|
|
|
|
, m_DestinationTime( 0 )
|
|
|
|
, m_Gravity( -9.81f)
|
|
|
|
, m_bOnGround( false )
|
|
|
|
, m_DestroyTimer( 0 )
|
|
|
|
, m_Jump(0)
|
2012-12-22 01:39:13 -08:00
|
|
|
, m_MobType(a_ProtocolMobType)
|
|
|
|
, m_SoundHurt(a_SoundHurt)
|
|
|
|
, m_SoundDeath(a_SoundDeath)
|
2012-06-14 06:06:06 -07:00
|
|
|
, m_EMState(IDLE)
|
|
|
|
, m_SightDistance(25)
|
|
|
|
, m_SeePlayerInterval (0)
|
|
|
|
, m_EMPersonality(AGGRESSIVE)
|
|
|
|
, m_AttackDamage(1.0f)
|
|
|
|
, m_AttackRange(5.0f)
|
|
|
|
, m_AttackInterval(0)
|
|
|
|
, m_AttackRate(3)
|
|
|
|
, idle_interval(0)
|
|
|
|
{
|
2012-12-22 01:39:13 -08:00
|
|
|
if (!a_ConfigName.empty())
|
|
|
|
{
|
|
|
|
GetMonsterConfig(a_ConfigName);
|
|
|
|
}
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 00:58:26 -07:00
|
|
|
void cMonster::SpawnOn(cClientHandle & a_Client)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-08-24 00:58:26 -07:00
|
|
|
a_Client.SendSpawnMob(*this);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::MoveToPosition( const Vector3f & a_Position )
|
|
|
|
{
|
|
|
|
m_bMovingToDestination = true;
|
|
|
|
|
|
|
|
m_Destination = a_Position;
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
bool cMonster::ReachedDestination()
|
|
|
|
{
|
2013-03-21 23:33:10 -07:00
|
|
|
Vector3f Distance = (m_Destination) - GetPosition();
|
2012-06-14 06:06:06 -07:00
|
|
|
if( Distance.SqrLength() < 2.f )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::Tick(float a_Dt, MTRand & a_TickRandom)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
super::Tick(a_Dt, a_TickRandom);
|
2012-06-14 06:06:06 -07:00
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
if (m_Health <= 0)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
// The mob is dead, but we're still animating the "puff" they leave when they die
|
|
|
|
m_DestroyTimer += a_Dt / 1000;
|
|
|
|
if (m_DestroyTimer > 1)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a_Dt /= 1000;
|
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
if (m_bMovingToDestination)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2013-03-21 23:33:10 -07:00
|
|
|
Vector3f Pos( GetPosition() );
|
2012-06-14 06:06:06 -07:00
|
|
|
Vector3f Distance = m_Destination - Pos;
|
|
|
|
if( !ReachedDestination() )
|
|
|
|
{
|
|
|
|
Distance.y = 0;
|
|
|
|
Distance.Normalize();
|
|
|
|
Distance *= 3;
|
2013-03-21 23:33:10 -07:00
|
|
|
SetSpeedX( Distance.x );
|
|
|
|
SetSpeedZ( Distance.z );
|
2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
if (m_EMState == ESCAPING)
|
|
|
|
{ //Runs Faster when escaping :D otherwise they just walk away
|
2013-03-21 23:33:10 -07:00
|
|
|
SetSpeedX (GetSpeedX() * 2.f);
|
|
|
|
SetSpeedZ (GetSpeedZ() * 2.f);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bMovingToDestination = false;
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:33:10 -07:00
|
|
|
if( GetSpeed().SqrLength() > 0.f )
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
if( m_bOnGround )
|
|
|
|
{
|
2013-03-21 23:33:10 -07:00
|
|
|
Vector3f NormSpeed = Vector3f(GetSpeed()).NormalizeCopy();
|
|
|
|
Vector3f NextBlock = Vector3f( GetPosition() ) + NormSpeed;
|
2012-06-14 06:06:06 -07:00
|
|
|
double NextHeight = (double)GetWorld()->GetHeight( (int)NextBlock.x, (int)NextBlock.z );
|
2013-03-21 23:33:10 -07:00
|
|
|
if( NextHeight > (GetPosY() - 1.0) && (NextHeight - GetPosY()) < 2.5 )
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
2013-03-21 23:33:10 -07:00
|
|
|
SetSpeedY(5.f); // Jump!!
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:33:10 -07:00
|
|
|
Vector3d Distance = m_Destination - GetPosition();
|
2012-08-19 12:42:32 -07:00
|
|
|
if (Distance.SqrLength() > 0.1f)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2013-03-09 06:35:43 -08:00
|
|
|
double Rotation, Pitch;
|
2012-06-14 06:06:06 -07:00
|
|
|
Distance.Normalize();
|
|
|
|
VectorToEuler( Distance.x, Distance.y, Distance.z, Rotation, Pitch );
|
|
|
|
SetRotation( Rotation );
|
|
|
|
SetPitch( Pitch );
|
|
|
|
}
|
2013-03-21 23:33:10 -07:00
|
|
|
|
|
|
|
HandlePhysics(a_Dt);
|
|
|
|
BroadcastMovementUpdate();
|
|
|
|
MoveToCorrectChunk();
|
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
switch (m_EMState)
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
case IDLE:
|
|
|
|
{
|
|
|
|
// If enemy passive we ignore checks for player visibility
|
|
|
|
InStateIdle(a_Dt, a_TickRandom);
|
|
|
|
break;
|
|
|
|
}
|
2012-06-14 06:06:06 -07:00
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
case CHASING:
|
|
|
|
{
|
|
|
|
// If we do not see a player anymore skip chasing action
|
|
|
|
InStateChasing(a_Dt, a_TickRandom);
|
|
|
|
break;
|
|
|
|
}
|
2012-06-14 06:06:06 -07:00
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
case ESCAPING:
|
|
|
|
{
|
|
|
|
InStateEscaping(a_Dt, a_TickRandom);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // switch (m_EMState)
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-21 23:33:10 -07:00
|
|
|
//Not used. Will remove later when we implement the AI.
|
2012-06-14 06:06:06 -07:00
|
|
|
void cMonster::ReplicateMovement()
|
|
|
|
{
|
2012-08-19 12:42:32 -07:00
|
|
|
if (m_bDirtyOrientation && !m_bDirtyPosition)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-08-19 12:42:32 -07:00
|
|
|
m_World->BroadcastEntLook(*this);
|
2012-06-14 06:06:06 -07:00
|
|
|
m_bDirtyOrientation = false;
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
if (m_bDirtyPosition)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-08-19 12:42:32 -07:00
|
|
|
float DiffX = (float)(GetPosX() - m_LastPosX);
|
|
|
|
float DiffY = (float)(GetPosY() - m_LastPosY);
|
|
|
|
float DiffZ = (float)(GetPosZ() - m_LastPosZ);
|
|
|
|
float SqrDist = DiffX * DiffX + DiffY * DiffY + DiffZ * DiffZ;
|
2012-06-14 06:06:06 -07:00
|
|
|
if (
|
2012-08-19 12:42:32 -07:00
|
|
|
(SqrDist > 4 * 4) // 4 blocks is max Relative Move
|
2012-11-01 14:38:20 -07:00
|
|
|
|| (m_World->GetWorldAge() - m_TimeLastTeleportPacket > 40) // Send an absolute position every 2 seconds
|
2012-06-14 06:06:06 -07:00
|
|
|
)
|
|
|
|
{
|
2012-08-19 12:42:32 -07:00
|
|
|
// LOGD("Teleported %f", sqrtf(SqrDist) );
|
|
|
|
m_World->BroadcastTeleportEntity(*this);
|
2012-11-01 14:38:20 -07:00
|
|
|
m_TimeLastTeleportPacket = m_World->GetWorldAge();
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
else
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
|
|
|
// Relative move sucks balls! It's always wrong wtf!
|
|
|
|
if (m_bDirtyOrientation)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-08-29 14:02:39 -07:00
|
|
|
m_World->BroadcastEntRelMoveLook(*this, (char)(DiffX * 32), (char)(DiffY * 32), (char)(DiffZ * 32));
|
2012-08-19 12:42:32 -07:00
|
|
|
m_bDirtyOrientation = false;
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-29 14:02:39 -07:00
|
|
|
m_World->BroadcastEntRelMove(*this, (char)(DiffX * 32), (char)(DiffY * 32), (char)(DiffZ * 32));
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_LastPosX = GetPosX();
|
|
|
|
m_LastPosY = GetPosY();
|
|
|
|
m_LastPosZ = GetPosZ();
|
|
|
|
m_bDirtyPosition = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MoveToCorrectChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::HandlePhysics(float a_Dt)
|
|
|
|
{
|
|
|
|
if( m_bOnGround ) // check if it's still on the ground
|
|
|
|
{
|
|
|
|
cWorld* World = GetWorld();
|
2013-03-21 23:33:10 -07:00
|
|
|
if( World->GetBlock( (int)GetPosX(), (int)GetPosY() -1, (int)GetPosZ() ) == E_BLOCK_AIR )
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
|
|
|
}
|
2013-03-21 23:33:10 -07:00
|
|
|
if( World->GetBlock( (int)GetPosX(), (int)GetPosY(), (int)GetPosZ() ) != E_BLOCK_AIR ) // If in ground itself, push it out
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
2013-03-21 23:33:10 -07:00
|
|
|
SetPosY(GetPosY() + 0.2);
|
2012-06-14 06:06:06 -07:00
|
|
|
m_bDirtyPosition = true;
|
|
|
|
}
|
2013-03-21 23:33:10 -07:00
|
|
|
SetSpeedX(GetSpeedX() * 0.7f/(1+a_Dt));
|
|
|
|
if( fabs(GetSpeedX()) < 0.05 ) SetSpeedX(0);
|
|
|
|
SetSpeedZ(GetSpeedZ() * 0.7f/(1+a_Dt));
|
|
|
|
if( fabs(GetSpeedZ()) < 0.05 ) SetSpeedZ(0);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_bOnGround )
|
|
|
|
{
|
|
|
|
float Gravity = -9.81f*a_Dt;
|
2013-03-21 23:33:10 -07:00
|
|
|
SetSpeedY(GetSpeedY() + Gravity);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:33:10 -07:00
|
|
|
if( GetSpeed().SqrLength() > 0.f )
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
cTracer Tracer( GetWorld() );
|
2013-03-21 23:33:10 -07:00
|
|
|
int Ret = Tracer.Trace( GetPosition(), GetSpeed(), 2 );
|
2012-06-14 06:06:06 -07:00
|
|
|
if( Ret ) // Oh noez! we hit something
|
|
|
|
{
|
|
|
|
// Set to hit position
|
2013-03-21 23:33:10 -07:00
|
|
|
if( (Tracer.RealHit - GetPosition()).SqrLength() <= ( GetSpeed() * a_Dt ).SqrLength() )
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
if( Ret == 1 )
|
|
|
|
{
|
|
|
|
|
2013-03-21 23:33:10 -07:00
|
|
|
if( Tracer.HitNormal.x != 0.f ) SetSpeedX(0.f);
|
|
|
|
if( Tracer.HitNormal.y != 0.f ) SetSpeedY(0.f);
|
|
|
|
if( Tracer.HitNormal.z != 0.f ) SetSpeedZ(0.f);
|
2012-06-14 06:06:06 -07:00
|
|
|
|
|
|
|
if( Tracer.HitNormal.y > 0 ) // means on ground
|
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 23:33:10 -07:00
|
|
|
SetPosition(Tracer.RealHit);
|
|
|
|
SetPosX(GetPosX() + (Tracer.HitNormal.x * 0.5f));
|
|
|
|
SetPosZ(GetPosZ() + (Tracer.HitNormal.z * 0.5f));
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
else
|
2013-03-21 23:33:10 -07:00
|
|
|
SetPosition(GetPosition() + (GetSpeed()*a_Dt));
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We didn't hit anything, so move =]
|
2013-03-21 23:33:10 -07:00
|
|
|
SetPosition(GetPosition() + (GetSpeed()*a_Dt));
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
m_bDirtyPosition = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-21 03:04:08 -08:00
|
|
|
void cMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-21 03:04:08 -08:00
|
|
|
super::DoTakeDamage(a_TDI);
|
2013-03-21 23:33:10 -07:00
|
|
|
if((m_SoundHurt != "") && (m_Health > 0)) m_World->BroadcastSoundEffect(m_SoundHurt, (int)(GetPosX() * 8), (int)(GetPosY() * 8), (int)(GetPosZ() * 8), 1.0f, 0.8f);
|
2012-12-21 03:04:08 -08:00
|
|
|
if (a_TDI.Attacker != NULL)
|
|
|
|
{
|
|
|
|
m_Target = a_TDI.Attacker;
|
|
|
|
AddReference(m_Target);
|
|
|
|
}
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-21 03:04:08 -08:00
|
|
|
void cMonster::KilledBy(cPawn * a_Killer)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-21 03:04:08 -08:00
|
|
|
super::KilledBy(a_Killer);
|
2013-03-21 23:33:10 -07:00
|
|
|
if(m_SoundHurt != "") m_World->BroadcastSoundEffect(m_SoundDeath, (int)(GetPosX() * 8), (int)(GetPosY() * 8), (int)(GetPosZ() * 8), 1.0f, 0.8f);
|
2012-06-14 06:06:06 -07:00
|
|
|
m_DestroyTimer = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
//----State Logic
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
const char *cMonster::GetState()
|
|
|
|
{
|
|
|
|
switch(m_EMState)
|
|
|
|
{
|
|
|
|
case IDLE: return "Idle";
|
|
|
|
case ATTACKING: return "Attacking";
|
|
|
|
case CHASING: return "Chasing";
|
|
|
|
default: return "Unknown";
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for debugging
|
|
|
|
void cMonster::SetState(const AString & a_State)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-08-19 12:42:32 -07:00
|
|
|
if (a_State.compare("Idle") == 0)
|
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
m_EMState = IDLE;
|
2012-08-19 12:42:32 -07:00
|
|
|
}
|
|
|
|
else if (a_State.compare("Attacking") == 0)
|
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
m_EMState = ATTACKING;
|
2012-08-19 12:42:32 -07:00
|
|
|
}
|
|
|
|
else if (a_State.compare("Chasing") == 0)
|
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
m_EMState = CHASING;
|
2012-08-19 12:42:32 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-06 12:00:03 -07:00
|
|
|
LOGD("cMonster::SetState(): Invalid state");
|
|
|
|
ASSERT(!"Invalid state");
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
//Checks to see if EventSeePlayer should be fired
|
|
|
|
//monster sez: Do I see the player
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::CheckEventSeePlayer(MTRand & a_TickRandom)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
// TODO: Rewrite this to use cWorld's DoWithPlayers()
|
|
|
|
cPlayer * Closest = FindClosestPlayer();
|
2012-06-14 06:06:06 -07:00
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
if (Closest != NULL)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
EventSeePlayer(Closest, a_TickRandom);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::CheckEventLostPlayer(MTRand & a_TickRandom)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
UNUSED(a_TickRandom);
|
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
Vector3f pos;
|
2012-08-19 12:42:32 -07:00
|
|
|
cTracer LineOfSight(GetWorld());
|
2012-06-14 06:06:06 -07:00
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
if (m_Target != NULL)
|
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
pos = m_Target->GetPosition();
|
2013-03-21 23:33:10 -07:00
|
|
|
if ((pos - GetPosition()).Length() > m_SightDistance || LineOfSight.Trace(GetPosition(),(pos - GetPosition()), (int)(pos - GetPosition()).Length()))
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
EventLosePlayer();
|
|
|
|
}
|
2012-08-19 12:42:32 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
EventLosePlayer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// What to do if player is seen
|
|
|
|
// default to change state to chasing
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::EventSeePlayer(cEntity * a_SeenPlayer, MTRand & a_TickRandom)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
UNUSED(a_TickRandom);
|
|
|
|
|
2012-06-14 06:06:06 -07:00
|
|
|
m_Target = a_SeenPlayer;
|
|
|
|
AddReference( m_Target );
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::EventLosePlayer(void)
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
Dereference(m_Target);
|
|
|
|
m_Target = 0;
|
|
|
|
m_EMState = IDLE;
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 02:15:53 -08:00
|
|
|
// What to do if in Idle State
|
|
|
|
void cMonster::InStateIdle(float a_Dt, MTRand & a_TickRandom)
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
2012-06-14 06:06:06 -07:00
|
|
|
idle_interval += a_Dt;
|
2012-08-19 12:42:32 -07:00
|
|
|
if (idle_interval > 1)
|
|
|
|
{
|
|
|
|
// at this interval the results are predictable
|
2012-12-22 02:15:53 -08:00
|
|
|
int rem = (a_TickRandom.randInt() % 6) + 1;
|
2012-08-19 12:42:32 -07:00
|
|
|
// 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
|
2012-06-14 06:06:06 -07:00
|
|
|
Vector3f Dist;
|
2012-12-22 02:15:53 -08:00
|
|
|
Dist.x = (float)((a_TickRandom.randInt() % 11) - 5);
|
|
|
|
Dist.z = (float)((a_TickRandom.randInt() % 11) - 5);
|
|
|
|
if ((Dist.SqrLength() > 2) && (rem >= 3))
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2013-03-21 23:33:10 -07:00
|
|
|
m_Destination.x = (float)(GetPosX() + Dist.x);
|
|
|
|
m_Destination.z = (float)(GetPosZ() + Dist.z);
|
2012-12-22 02:15:53 -08:00
|
|
|
m_Destination.y = (float)GetWorld()->GetHeight((int)m_Destination.x, (int)m_Destination.z) + 1.2f;
|
|
|
|
MoveToPosition(m_Destination);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// What to do if in Chasing State
|
|
|
|
// This state should always be defined in each child class
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::InStateChasing(float a_Dt, MTRand & a_TickRandom)
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
|
|
|
UNUSED(a_Dt);
|
2012-12-22 02:15:53 -08:00
|
|
|
UNUSED(a_TickRandom);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// What to do if in Escaping State
|
2012-12-22 02:15:53 -08:00
|
|
|
void cMonster::InStateEscaping(float a_Dt, MTRand & a_TickRandom)
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
UNUSED(a_Dt);
|
|
|
|
UNUSED(a_TickRandom);
|
|
|
|
|
|
|
|
if (m_Target != NULL)
|
2012-08-19 12:42:32 -07:00
|
|
|
{
|
2013-03-21 23:33:10 -07:00
|
|
|
Vector3d newloc = GetPosition();
|
2012-06-14 06:06:06 -07:00
|
|
|
newloc.x = (m_Target->GetPosition().x < newloc.x)? (newloc.x + m_SightDistance): (newloc.x - m_SightDistance);
|
|
|
|
newloc.z = (m_Target->GetPosition().z < newloc.z)? (newloc.z + m_SightDistance): (newloc.z - m_SightDistance);
|
|
|
|
MoveToPosition(newloc);
|
|
|
|
}
|
2012-08-19 12:42:32 -07:00
|
|
|
else
|
|
|
|
{
|
2012-12-22 02:15:53 -08:00
|
|
|
m_EMState = IDLE; // This shouldnt be required but just to be safe
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
// Do attack here
|
|
|
|
// a_Dt is passed so we can set attack rate
|
|
|
|
void cMonster::Attack(float a_Dt)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-08-19 12:42:32 -07:00
|
|
|
m_AttackInterval += a_Dt * m_AttackRate;
|
|
|
|
if ((m_Target != NULL) && (m_AttackInterval > 3.0))
|
|
|
|
{
|
|
|
|
// Setting this higher gives us more wiggle room for attackrate
|
|
|
|
m_AttackInterval = 0.0;
|
2012-12-21 03:04:08 -08:00
|
|
|
((cPawn *)m_Target)->TakeDamage(*this);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
// Checks for Players close by and if they are visible return the closest
|
2012-06-14 06:06:06 -07:00
|
|
|
cPlayer * cMonster::FindClosestPlayer(void)
|
|
|
|
{
|
2013-03-21 23:33:10 -07:00
|
|
|
return m_World->FindClosestPlayer(GetPosition(), m_SightDistance);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 01:39:13 -08:00
|
|
|
void cMonster::GetMonsterConfig(const AString & a_Name)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
2012-12-22 01:39:13 -08:00
|
|
|
cRoot::Get()->GetMonsterConfig()->AssignAttributes(this, a_Name);
|
2012-06-14 06:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::SetAttackRate(int ar)
|
|
|
|
{
|
|
|
|
m_AttackRate = (float)ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::SetAttackRange(float ar)
|
|
|
|
{
|
|
|
|
m_AttackRange = ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::SetAttackDamage(float ad)
|
|
|
|
{
|
|
|
|
m_AttackDamage = ad;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::SetSightDistance(float sd)
|
|
|
|
{
|
|
|
|
m_SightDistance = sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 12:42:32 -07:00
|
|
|
void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth)
|
2012-06-14 06:06:06 -07:00
|
|
|
{
|
|
|
|
MTRand r1;
|
|
|
|
int Count = r1.randInt() % (a_Max + 1 - a_Min) + a_Min;
|
|
|
|
if (Count > 0)
|
|
|
|
{
|
|
|
|
a_Drops.push_back(cItem(a_Item, Count, a_ItemHealth));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|