2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cMonster.h"
|
|
|
|
#include "cRoot.h"
|
|
|
|
#include "cServer.h"
|
|
|
|
#include "cClientHandle.h"
|
|
|
|
#include "cWorld.h"
|
|
|
|
#include "cChunk.h"
|
|
|
|
#include "cPlayer.h"
|
|
|
|
#include "BlockID.h"
|
|
|
|
#include "Defines.h"
|
2011-12-21 15:42:34 -05:00
|
|
|
#include "cPickup.h"
|
|
|
|
#include "cItem.h"
|
2011-12-25 17:47:12 -05:00
|
|
|
#include "cMonsterConfig.h"
|
2011-12-26 04:09:47 -05:00
|
|
|
#include "MersenneTwister.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
#include "packets/cPacket_SpawnMob.h"
|
|
|
|
#include "packets/cPacket_EntityLook.h"
|
|
|
|
#include "packets/cPacket_TeleportEntity.h"
|
|
|
|
#include "packets/cPacket_RelativeEntityMoveLook.h"
|
|
|
|
#include "packets/cPacket_RelativeEntityMove.h"
|
|
|
|
#include "packets/cPacket_Metadata.h"
|
|
|
|
|
|
|
|
#include "Vector3f.h"
|
|
|
|
#include "Vector3i.h"
|
|
|
|
#include "Vector3d.h"
|
|
|
|
|
|
|
|
#include "cTracer.h"
|
|
|
|
#include "../iniFile/iniFile.h"
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-01-29 14:28:19 -05:00
|
|
|
#include <cstdlib> // rand
|
|
|
|
#include <unistd.h>
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
cMonster::cMonster()
|
|
|
|
: m_Target(0)
|
|
|
|
, m_bMovingToDestination(false)
|
|
|
|
, m_DestinationTime( 0 )
|
2012-01-31 13:06:24 -05:00
|
|
|
, m_Gravity( -9.81f)
|
2011-10-03 14:41:19 -04:00
|
|
|
, m_bOnGround( false )
|
|
|
|
, m_DestroyTimer( 0 )
|
|
|
|
, m_Jump(0)
|
|
|
|
, m_MobType( 0 )
|
|
|
|
, m_EMState(IDLE)
|
|
|
|
, m_SightDistance(25)
|
2011-12-25 17:47:12 -05:00
|
|
|
, m_SeePlayerInterval (0)
|
|
|
|
, m_EMPersonality(AGGRESSIVE)
|
2012-01-31 13:06:24 -05:00
|
|
|
, m_AttackDamage(1.0f)
|
|
|
|
, m_AttackRange(5.0f)
|
2011-12-25 17:47:12 -05:00
|
|
|
, m_AttackInterval(0)
|
|
|
|
, m_AttackRate(3)
|
|
|
|
, idle_interval(0)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
LOG("cMonster::cMonster()");
|
2011-12-27 21:10:05 -05:00
|
|
|
LOG("In state: %s", GetState());
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-12-27 21:10:05 -05:00
|
|
|
m_bBurnable = true;
|
|
|
|
m_MetaData = NORMAL;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cMonster::~cMonster()
|
|
|
|
{
|
|
|
|
LOG("cMonster::~cMonster()");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cMonster::IsA( const char* a_EntityType )
|
|
|
|
{
|
|
|
|
if( strcmp( a_EntityType, "cMonster" ) == 0 ) return true;
|
|
|
|
return cPawn::IsA( a_EntityType );
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cPacket * cMonster::GetSpawnPacket(void) const
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
cPacket_SpawnMob * Spawn = new cPacket_SpawnMob;
|
|
|
|
Spawn->m_UniqueID = GetUniqueID();
|
|
|
|
Spawn->m_Type = m_MobType;
|
2012-02-15 17:50:00 -05:00
|
|
|
*Spawn->m_Pos = Vector3i((m_Pos) * 32);
|
2012-02-13 16:47:03 -05:00
|
|
|
Spawn->m_Yaw = 0;
|
|
|
|
Spawn->m_Pitch = 0;
|
|
|
|
Spawn->m_MetaDataSize = 1;
|
|
|
|
Spawn->m_MetaData = new char[Spawn->m_MetaDataSize];
|
|
|
|
Spawn->m_MetaData[0] = 0x7f; // not on fire/crouching/riding
|
|
|
|
return Spawn;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cMonster::MoveToPosition( const Vector3f & a_Position )
|
|
|
|
{
|
|
|
|
m_bMovingToDestination = true;
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Destination = a_Position;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cMonster::ReachedDestination()
|
|
|
|
{
|
2012-02-15 17:50:00 -05:00
|
|
|
Vector3f Distance = (m_Destination) - Vector3f( m_Pos );
|
2011-10-03 14:41:19 -04:00
|
|
|
if( Distance.SqrLength() < 2.f )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cMonster::Tick(float a_Dt)
|
|
|
|
{
|
2011-12-27 21:10:05 -05:00
|
|
|
cPawn::Tick(a_Dt);
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
if( m_Health <= 0 )
|
|
|
|
{
|
|
|
|
m_DestroyTimer += a_Dt/1000;
|
|
|
|
if( m_DestroyTimer > 1 )
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-25 17:47:12 -05:00
|
|
|
a_Dt /= 1000;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
if( m_bMovingToDestination )
|
|
|
|
{
|
|
|
|
Vector3f Pos( m_Pos );
|
2012-02-15 17:50:00 -05:00
|
|
|
Vector3f Distance = m_Destination - Pos;
|
2011-10-03 14:41:19 -04:00
|
|
|
if( !ReachedDestination() )
|
|
|
|
{
|
|
|
|
Distance.y = 0;
|
|
|
|
Distance.Normalize();
|
2011-12-25 17:47:12 -05:00
|
|
|
Distance *= 3;
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.x = Distance.x;
|
|
|
|
m_Speed.z = Distance.z;
|
2011-12-26 13:20:00 -05:00
|
|
|
|
|
|
|
if (m_EMState == ESCAPING)
|
|
|
|
{ //Runs Faster when escaping :D otherwise they just walk away
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.x *= 2.f;
|
|
|
|
m_Speed.z *= 2.f;
|
2011-12-26 13:20:00 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bMovingToDestination = false;
|
|
|
|
}
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( m_Speed.SqrLength() > 0.f )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
if( m_bOnGround )
|
|
|
|
{
|
2012-02-15 17:50:00 -05:00
|
|
|
Vector3f NormSpeed = m_Speed.NormalizeCopy();
|
|
|
|
Vector3f NextBlock = Vector3f( m_Pos ) + NormSpeed;
|
2011-10-30 20:52:20 -04:00
|
|
|
double NextHeight = (double)GetWorld()->GetHeight( (int)NextBlock.x, (int)NextBlock.z );
|
2012-02-15 17:50:00 -05:00
|
|
|
if( NextHeight > m_Pos.y - 1.2 && NextHeight - m_Pos.y < 2.5 )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.y = 7.f; // Jump!!
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HandlePhysics( a_Dt );
|
|
|
|
|
|
|
|
ReplicateMovement();
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
Vector3f Distance = m_Destination - Vector3f( m_Pos );
|
2011-10-03 14:41:19 -04:00
|
|
|
if( Distance.SqrLength() > 0.1f )
|
|
|
|
{
|
|
|
|
float Rotation, Pitch;
|
|
|
|
Distance.Normalize();
|
|
|
|
VectorToEuler( Distance.x, Distance.y, Distance.z, Rotation, Pitch );
|
|
|
|
SetRotation( Rotation );
|
|
|
|
SetPitch( Pitch );
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_EMState == IDLE) { //If enemy passive we ignore checks for player visibility
|
|
|
|
InStateIdle(a_Dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_EMState == CHASING) { //If we do not see a player anymore skip chasing action
|
|
|
|
InStateChasing(a_Dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_EMState == ESCAPING) {
|
|
|
|
InStateEscaping(a_Dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cMonster::ReplicateMovement()
|
|
|
|
{
|
|
|
|
if(m_bDirtyOrientation && !m_bDirtyPosition)
|
|
|
|
{
|
|
|
|
cPacket_EntityLook EntityLook( this );
|
2012-02-20 11:39:00 -05:00
|
|
|
m_World->BroadcastToChunk(m_ChunkX, m_ChunkY, m_ChunkZ, EntityLook );
|
2011-10-03 14:41:19 -04:00
|
|
|
m_bDirtyOrientation = false;
|
|
|
|
}
|
2012-02-20 11:39:00 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
if( m_bDirtyPosition )
|
|
|
|
{
|
|
|
|
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-02-20 11:39:00 -05:00
|
|
|
if (
|
|
|
|
(SqrDist > 4 * 4) // 4 blocks is max Relative Move
|
|
|
|
|| (cWorld::GetTime() - m_TimeLastTeleportPacket > 2 ) // Send an absolute position every 2 seconds
|
|
|
|
)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
//LOG("Teleported %f", sqrtf(SqrDist) );
|
|
|
|
cPacket_TeleportEntity TeleportEntity( this );
|
2012-02-20 11:39:00 -05:00
|
|
|
m_World->BroadcastToChunk(m_ChunkX, m_ChunkY, m_ChunkZ, TeleportEntity);
|
2011-10-03 14:41:19 -04:00
|
|
|
m_TimeLastTeleportPacket = cWorld::GetTime();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Relative move sucks balls! It's always wrong wtf!
|
|
|
|
if( m_bDirtyOrientation )
|
|
|
|
{
|
|
|
|
cPacket_RelativeEntityMoveLook RelativeEntityMoveLook;
|
|
|
|
RelativeEntityMoveLook.m_UniqueID = GetUniqueID();
|
2012-02-20 11:39:00 -05:00
|
|
|
RelativeEntityMoveLook.m_MoveX = (char)(DiffX*32);
|
|
|
|
RelativeEntityMoveLook.m_MoveY = (char)(DiffY*32);
|
|
|
|
RelativeEntityMoveLook.m_MoveZ = (char)(DiffZ*32);
|
|
|
|
RelativeEntityMoveLook.m_Yaw = (char)((GetRotation()/360.f)*256);
|
2011-10-03 14:41:19 -04:00
|
|
|
RelativeEntityMoveLook.m_Pitch = (char)((GetPitch()/360.f)*256);
|
2012-02-20 11:39:00 -05:00
|
|
|
m_World->BroadcastToChunk(m_ChunkX, m_ChunkY, m_ChunkZ, RelativeEntityMoveLook );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cPacket_RelativeEntityMove RelativeEntityMove;
|
|
|
|
RelativeEntityMove.m_UniqueID = GetUniqueID();
|
2012-02-20 11:39:00 -05:00
|
|
|
RelativeEntityMove.m_MoveX = (char)(DiffX*32);
|
|
|
|
RelativeEntityMove.m_MoveY = (char)(DiffY*32);
|
|
|
|
RelativeEntityMove.m_MoveZ = (char)(DiffZ*32);
|
|
|
|
m_World->BroadcastToChunk(m_ChunkX, m_ChunkY, m_ChunkZ, RelativeEntityMove );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_LastPosX = GetPosX();
|
|
|
|
m_LastPosY = GetPosY();
|
|
|
|
m_LastPosZ = GetPosZ();
|
|
|
|
m_bDirtyPosition = false;
|
|
|
|
}
|
2011-12-25 17:47:12 -05:00
|
|
|
|
|
|
|
MoveToCorrectChunk();
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cMonster::HandlePhysics(float a_Dt)
|
|
|
|
{
|
|
|
|
if( m_bOnGround ) // check if it's still on the ground
|
|
|
|
{
|
2011-10-30 20:52:20 -04:00
|
|
|
cWorld* World = GetWorld();
|
2012-02-15 17:50:00 -05:00
|
|
|
if( World->GetBlock( (int)m_Pos.x, (int)m_Pos.y -1, (int)m_Pos.z ) == E_BLOCK_AIR )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
|
|
|
}
|
2012-02-15 17:50:00 -05:00
|
|
|
if( World->GetBlock( (int)m_Pos.x, (int)m_Pos.y, (int)m_Pos.z ) != E_BLOCK_AIR ) // If in ground itself, push it out
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos.y += 0.2;
|
2011-10-03 14:41:19 -04:00
|
|
|
m_bDirtyPosition = true;
|
|
|
|
}
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.x *= 0.7f/(1+a_Dt);
|
|
|
|
if( fabs(m_Speed.x) < 0.05 ) m_Speed.x = 0;
|
|
|
|
m_Speed.z *= 0.7f/(1+a_Dt);
|
|
|
|
if( fabs(m_Speed.z) < 0.05 ) m_Speed.z = 0;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_bOnGround )
|
|
|
|
{
|
|
|
|
float Gravity = -9.81f*a_Dt;
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Speed.y += Gravity;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( m_Speed.SqrLength() > 0.f )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2011-10-30 20:52:20 -04:00
|
|
|
cTracer Tracer( GetWorld() );
|
2012-02-15 17:50:00 -05:00
|
|
|
int Ret = Tracer.Trace( m_Pos, m_Speed, 2 );
|
2011-10-03 14:41:19 -04:00
|
|
|
if( Ret ) // Oh noez! we hit something
|
|
|
|
{
|
|
|
|
// Set to hit position
|
2012-02-15 17:50:00 -05:00
|
|
|
if( (Tracer.RealHit - Vector3f(m_Pos)).SqrLength() <= ( m_Speed * a_Dt ).SqrLength() )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
if( Ret == 1 )
|
|
|
|
{
|
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( Tracer.HitNormal.x != 0.f ) m_Speed.x = 0.f;
|
|
|
|
if( Tracer.HitNormal.y != 0.f ) m_Speed.y = 0.f;
|
|
|
|
if( Tracer.HitNormal.z != 0.f ) m_Speed.z = 0.f;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-15 17:50:00 -05:00
|
|
|
if( Tracer.HitNormal.y > 0 ) // means on ground
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = true;
|
|
|
|
}
|
|
|
|
}
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos = Tracer.RealHit;
|
|
|
|
m_Pos += Tracer.HitNormal * 0.2f;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos += m_Speed*a_Dt;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We didn't hit anything, so move =]
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Pos += m_Speed*a_Dt;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_bDirtyPosition = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-25 17:47:12 -05:00
|
|
|
void cMonster::TakeDamage(int a_Damage, cEntity* a_Instigator)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
cPawn::TakeDamage( a_Damage, a_Instigator );
|
2011-12-25 17:47:12 -05:00
|
|
|
m_Target = a_Instigator;
|
|
|
|
AddReference( m_Target );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cMonster::KilledBy( cEntity* a_Killer )
|
|
|
|
{
|
|
|
|
cPawn::KilledBy( a_Killer );
|
|
|
|
m_DestroyTimer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----State Logic
|
|
|
|
|
|
|
|
const char *cMonster::GetState() {
|
|
|
|
switch(m_EMState) {
|
|
|
|
case IDLE:
|
|
|
|
return "Idle";
|
|
|
|
break;
|
|
|
|
case ATTACKING:
|
|
|
|
return "Attacking";
|
|
|
|
break;
|
|
|
|
case CHASING:
|
|
|
|
return "Chasing";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//for debugging
|
2011-12-25 17:47:12 -05:00
|
|
|
void cMonster::SetState(const char* a_str)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
std::string str = a_str;
|
|
|
|
if(str.compare("Idle") == 0 ) {
|
|
|
|
m_EMState = IDLE;
|
|
|
|
} else if(str.compare("Attacking") == 0 ) {
|
|
|
|
m_EMState = ATTACKING;
|
|
|
|
} else if(str.compare("Chasing") == 0 ) {
|
|
|
|
m_EMState = CHASING;
|
|
|
|
} else {
|
|
|
|
printf("Invalid State");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Checks to see if EventSeePlayer should be fired
|
|
|
|
//monster sez: Do I see the player
|
2011-12-25 17:47:12 -05:00
|
|
|
void cMonster::CheckEventSeePlayer()
|
|
|
|
{
|
2011-12-26 13:20:00 -05:00
|
|
|
cPlayer *Closest = FindClosestPlayer();
|
|
|
|
|
|
|
|
if(Closest)
|
|
|
|
{
|
|
|
|
EventSeePlayer(Closest);
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2011-12-25 17:47:12 -05:00
|
|
|
void cMonster::CheckEventLostPlayer()
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
Vector3f pos;
|
2011-10-30 20:52:20 -04:00
|
|
|
cTracer LineOfSight(GetWorld() );
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
if(m_Target != 0) {
|
|
|
|
pos = m_Target->GetPosition();
|
2012-02-15 17:50:00 -05:00
|
|
|
if((pos - m_Pos).Length() > m_SightDistance || LineOfSight.Trace(m_Pos,(pos - m_Pos), (int)(pos - m_Pos).Length()))
|
2011-12-25 17:47:12 -05:00
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
EventLosePlayer();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EventLosePlayer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//What to do if player is seen
|
|
|
|
//default to change state to chasing
|
2011-12-25 17:47:12 -05:00
|
|
|
void cMonster::EventSeePlayer(cEntity *a_SeenPlayer)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
m_Target = a_SeenPlayer;
|
|
|
|
AddReference( m_Target );
|
|
|
|
}
|
|
|
|
|
|
|
|
void cMonster::EventLosePlayer(){
|
|
|
|
Dereference(m_Target);
|
|
|
|
m_Target = 0;
|
|
|
|
m_EMState = IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//What to do if in Idle State
|
|
|
|
void cMonster::InStateIdle(float a_Dt) {
|
|
|
|
idle_interval += a_Dt;
|
|
|
|
if(idle_interval > 1) { //at this interval the results are predictable
|
2011-12-26 04:09:47 -05:00
|
|
|
MTRand r1;
|
|
|
|
int rem = r1.randInt()%6 + 1;
|
2011-10-03 14:41:19 -04:00
|
|
|
//LOG("Moving: int: %3.3f rem: %i",idle_interval,rem);
|
2011-12-26 13:20:00 -05:00
|
|
|
idle_interval -= 1; //So nothing gets dropped when the server hangs for a few seconds
|
2011-10-03 14:41:19 -04:00
|
|
|
Vector3f Dist;
|
2011-12-26 04:09:47 -05:00
|
|
|
Dist.x = (float)((r1.randInt()%11)-5);
|
|
|
|
Dist.z = (float)((r1.randInt()%11)-5);
|
2011-10-03 14:41:19 -04:00
|
|
|
if( Dist.SqrLength() > 2 && rem >= 3)
|
|
|
|
{
|
2012-02-15 17:50:00 -05:00
|
|
|
m_Destination.x = (float)(m_Pos.x + Dist.x);
|
|
|
|
m_Destination.z = (float)(m_Pos.z + Dist.z);
|
|
|
|
m_Destination.y = (float)GetWorld()->GetHeight( (int)m_Destination.x, (int)m_Destination.z ) + 1.2f;
|
|
|
|
MoveToPosition( m_Destination );
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//What to do if in Chasing State
|
|
|
|
//This state should always be defined in each child class
|
|
|
|
void cMonster::InStateChasing(float a_Dt) {
|
|
|
|
(void)a_Dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//What to do if in Escaping State
|
|
|
|
void cMonster::InStateEscaping(float a_Dt) {
|
|
|
|
(void)a_Dt;
|
|
|
|
if(m_Target) {
|
2012-02-15 17:50:00 -05:00
|
|
|
Vector3d newloc = m_Pos;
|
2011-10-03 14:41:19 -04: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);
|
|
|
|
} else {
|
|
|
|
m_EMState = IDLE; //this shouldnt be required but just to be safe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Do attack here
|
|
|
|
//a_Dt is passed so we can set attack rate
|
|
|
|
void cMonster::Attack(float a_Dt) {
|
2011-12-25 17:47:12 -05:00
|
|
|
m_AttackInterval += a_Dt * m_AttackRate;
|
2011-10-03 14:41:19 -04:00
|
|
|
if(m_Target != 0 && m_AttackInterval > 3.0) { //Setting this higher gives us more wiggle room for attackrate
|
|
|
|
m_AttackInterval = 0.0;
|
|
|
|
((cPawn *)m_Target)->TakeDamage((int)m_AttackDamage,this);
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 21:10:05 -05:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// TODO: Implement this debug function inside cWorld instead - the world owns the entities
|
|
|
|
void cMonster::ListMonsters()
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
cWorld::EntityList Entities = cRoot::Get()->GetWorld()->GetEntities();
|
|
|
|
cRoot::Get()->GetWorld()->LockEntities();
|
|
|
|
for( cWorld::EntityList::iterator itr = Entities.begin(); itr != Entities.end(); ++itr) {
|
|
|
|
if((*itr)->GetEntityType() == cEntity::E_ENTITY){
|
|
|
|
LOG("In state: %s type: %i attack rate: %i",((cMonster *)(*itr))->GetState(), ((cMonster *)(*itr))->GetMobType(),((cMonster *)(*itr))->GetAttackRate());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cRoot::Get()->GetWorld()->UnlockEntities();
|
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-12-26 13:20:00 -05:00
|
|
|
//Checks for Players close by and if they are visible return the closest
|
2012-02-13 16:47:03 -05:00
|
|
|
cPlayer * cMonster::FindClosestPlayer(void)
|
2011-12-26 13:20:00 -05:00
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
return m_World->FindClosestPlayer(m_Pos, m_SightDistance);
|
|
|
|
}
|
|
|
|
|
2011-12-26 13:20:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-12-26 13:20:00 -05:00
|
|
|
void cMonster::GetMonsterConfig(const char* pm_name)
|
|
|
|
{
|
2011-12-25 17:47:12 -05:00
|
|
|
cRoot::Get()->GetMonsterConfig()->Get()->AssignAttributes(this, pm_name);
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-26 13:20:00 -05:00
|
|
|
void cMonster::SetAttackRate(int ar)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
m_AttackRate = (float)ar;
|
|
|
|
}
|
2011-12-26 13:20:00 -05:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-26 13:20:00 -05:00
|
|
|
void cMonster::SetAttackRange(float ar)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
m_AttackRange = ar;
|
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::SetAttackDamage(float ad)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
m_AttackDamage = ad;
|
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::SetSightDistance(float sd)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
m_SightDistance = sd;
|
|
|
|
}
|
2011-12-21 15:42:34 -05:00
|
|
|
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-21 15:42:34 -05:00
|
|
|
void cMonster::DropItem(ENUM_ITEM_ID a_Item, unsigned int a_Count)
|
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
if (a_Count > 0)
|
2011-12-21 15:42:34 -05:00
|
|
|
{
|
2012-02-15 17:50:00 -05:00
|
|
|
cPickup * Pickup = new cPickup( (int)(m_Pos.x * 32), (int)(m_Pos.y * 32), (int)(m_Pos.z * 32), cItem( a_Item, (char) a_Count ) );
|
2011-12-21 15:42:34 -05:00
|
|
|
Pickup->Initialize( GetWorld() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-21 15:42:34 -05:00
|
|
|
void cMonster::RandomDropItem(ENUM_ITEM_ID a_Item, unsigned int a_Min, unsigned int a_Max)
|
|
|
|
{
|
2011-12-26 04:09:47 -05:00
|
|
|
MTRand r1;
|
2011-12-27 21:10:05 -05:00
|
|
|
return cMonster::DropItem(a_Item, r1.randInt() % (a_Max + 1 - a_Min) + a_Min);
|
2011-12-21 15:42:34 -05:00
|
|
|
}
|