2013-09-13 14:06:42 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2013-10-20 07:25:56 -04:00
|
|
|
#include "IncludeAllMonsters.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "../Root.h"
|
|
|
|
#include "../Server.h"
|
|
|
|
#include "../ClientHandle.h"
|
|
|
|
#include "../World.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2012-09-23 16:53:08 -04:00
|
|
|
#include "../Defines.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "../MonsterConfig.h"
|
2012-09-23 16:53:08 -04:00
|
|
|
#include "../MersenneTwister.h"
|
|
|
|
|
|
|
|
#include "../Vector3f.h"
|
|
|
|
#include "../Vector3i.h"
|
|
|
|
#include "../Vector3d.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "../Tracer.h"
|
2013-09-05 16:40:08 -04:00
|
|
|
#include "../Chunk.h"
|
2013-10-20 07:25:56 -04:00
|
|
|
#include "../FastRandom.h"
|
2013-04-22 03:18:03 -04:00
|
|
|
|
2013-09-10 09:09:45 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-20 07:25:56 -04:00
|
|
|
/** Map for eType <-> string
|
|
|
|
Needs to be alpha-sorted by the strings, because binary search is used in StringToMobType()
|
|
|
|
The strings need to be lowercase (for more efficient comparisons in StringToMobType())
|
|
|
|
*/
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
cMonster::eType m_Type;
|
|
|
|
const char * m_lcName;
|
|
|
|
} g_MobTypeNames[] =
|
|
|
|
{
|
|
|
|
{cMonster::mtBat, "bat"},
|
|
|
|
{cMonster::mtBlaze, "blaze"},
|
|
|
|
{cMonster::mtCaveSpider, "cavespider"},
|
|
|
|
{cMonster::mtChicken, "chicken"},
|
|
|
|
{cMonster::mtCow, "cow"},
|
|
|
|
{cMonster::mtCreeper, "creeper"},
|
|
|
|
{cMonster::mtEnderman, "enderman"},
|
|
|
|
{cMonster::mtGhast, "ghast"},
|
|
|
|
{cMonster::mtHorse, "horse"},
|
|
|
|
{cMonster::mtMagmaCube, "magmacube"},
|
|
|
|
{cMonster::mtMooshroom, "mooshroom"},
|
|
|
|
{cMonster::mtOcelot, "ocelot"},
|
|
|
|
{cMonster::mtPig, "pig"},
|
|
|
|
{cMonster::mtSheep, "sheep"},
|
|
|
|
{cMonster::mtSilverfish, "silverfish"},
|
|
|
|
{cMonster::mtSkeleton, "skeleton"},
|
|
|
|
{cMonster::mtSlime, "slime"},
|
|
|
|
{cMonster::mtSpider, "spider"},
|
|
|
|
{cMonster::mtSquid, "squid"},
|
|
|
|
{cMonster::mtVillager, "villager"},
|
|
|
|
{cMonster::mtWitch, "witch"},
|
|
|
|
{cMonster::mtWolf, "wolf"},
|
|
|
|
{cMonster::mtZombie, "zombie"},
|
|
|
|
{cMonster::mtZombiePigman, "zombiepigman"},
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cMonster:
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-20 04:23:30 -04:00
|
|
|
cMonster::cMonster(const AString & a_ConfigName, eType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, double a_Width, double a_Height)
|
2013-10-13 07:47:55 -04:00
|
|
|
: super(etMonster, a_Width, a_Height)
|
2012-12-21 07:52:14 -05:00
|
|
|
, m_Target(NULL)
|
2013-09-07 10:42:34 -04:00
|
|
|
, m_AttackRate(3)
|
|
|
|
, idle_interval(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_bMovingToDestination(false)
|
|
|
|
, m_DestinationTime( 0 )
|
|
|
|
, m_DestroyTimer( 0 )
|
|
|
|
, m_Jump(0)
|
2013-10-20 04:23:30 -04:00
|
|
|
, m_MobType(a_MobType)
|
2012-12-22 04:39:13 -05:00
|
|
|
, m_SoundHurt(a_SoundHurt)
|
|
|
|
, m_SoundDeath(a_SoundDeath)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_EMState(IDLE)
|
|
|
|
, m_SightDistance(25)
|
|
|
|
, m_SeePlayerInterval (0)
|
|
|
|
, m_EMPersonality(AGGRESSIVE)
|
|
|
|
, m_AttackDamage(1.0f)
|
2013-09-13 13:33:22 -04:00
|
|
|
, m_AttackRange(2.0f)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_AttackInterval(0)
|
2013-09-07 10:42:34 -04:00
|
|
|
, m_BurnsInDaylight(false)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-22 04:39:13 -05:00
|
|
|
if (!a_ConfigName.empty())
|
|
|
|
{
|
|
|
|
GetMonsterConfig(a_ConfigName);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cMonster::SpawnOn(cClientHandle & a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
a_Client.SendSpawnMob(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cMonster::MoveToPosition( const Vector3f & a_Position )
|
|
|
|
{
|
|
|
|
m_bMovingToDestination = true;
|
|
|
|
|
|
|
|
m_Destination = a_Position;
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
bool cMonster::ReachedDestination()
|
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
Vector3f Distance = (m_Destination) - GetPosition();
|
2012-06-14 09:06:06 -04:00
|
|
|
if( Distance.SqrLength() < 2.f )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::Tick(float a_Dt, cChunk & a_Chunk)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
super::Tick(a_Dt, a_Chunk);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
if (m_Health <= 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-22 05:15:53 -05: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 09:06:06 -04:00
|
|
|
{
|
2013-06-25 02:36:59 -04:00
|
|
|
Destroy(true);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:40:08 -04:00
|
|
|
// Burning in daylight
|
|
|
|
HandleDaylightBurning(a_Chunk);
|
|
|
|
|
2013-04-22 03:18:03 -04:00
|
|
|
HandlePhysics(a_Dt,a_Chunk);
|
|
|
|
BroadcastMovementUpdate();
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
a_Dt /= 1000;
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
if (m_bMovingToDestination)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
Vector3f Pos( GetPosition() );
|
2012-06-14 09:06:06 -04:00
|
|
|
Vector3f Distance = m_Destination - Pos;
|
|
|
|
if( !ReachedDestination() )
|
|
|
|
{
|
|
|
|
Distance.y = 0;
|
|
|
|
Distance.Normalize();
|
|
|
|
Distance *= 3;
|
2013-03-22 02:33:10 -04:00
|
|
|
SetSpeedX( Distance.x );
|
|
|
|
SetSpeedZ( Distance.z );
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
if (m_EMState == ESCAPING)
|
|
|
|
{ //Runs Faster when escaping :D otherwise they just walk away
|
2013-03-22 02:33:10 -04:00
|
|
|
SetSpeedX (GetSpeedX() * 2.f);
|
|
|
|
SetSpeedZ (GetSpeedZ() * 2.f);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_bMovingToDestination = false;
|
|
|
|
}
|
|
|
|
|
2013-03-22 02:33:10 -04:00
|
|
|
if( GetSpeed().SqrLength() > 0.f )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if( m_bOnGround )
|
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
Vector3f NormSpeed = Vector3f(GetSpeed()).NormalizeCopy();
|
|
|
|
Vector3f NextBlock = Vector3f( GetPosition() ) + NormSpeed;
|
2013-06-25 02:36:59 -04:00
|
|
|
int NextHeight;
|
|
|
|
if (!m_World->TryGetHeight((int)NextBlock.x, (int)NextBlock.z, NextHeight))
|
|
|
|
{
|
|
|
|
// The chunk at NextBlock is not loaded
|
|
|
|
return;
|
|
|
|
}
|
2013-03-22 02:33:10 -04:00
|
|
|
if( NextHeight > (GetPosY() - 1.0) && (NextHeight - GetPosY()) < 2.5 )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_bOnGround = false;
|
2013-03-22 02:33:10 -04:00
|
|
|
SetSpeedY(5.f); // Jump!!
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 00:33:47 -04:00
|
|
|
|
2013-03-22 02:33:10 -04:00
|
|
|
Vector3d Distance = m_Destination - GetPosition();
|
2012-08-19 15:42:32 -04:00
|
|
|
if (Distance.SqrLength() > 0.1f)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-09 09:35:43 -05:00
|
|
|
double Rotation, Pitch;
|
2012-06-14 09:06:06 -04:00
|
|
|
Distance.Normalize();
|
|
|
|
VectorToEuler( Distance.x, Distance.y, Distance.z, Rotation, Pitch );
|
2013-04-02 02:48:31 -04:00
|
|
|
SetHeadYaw (Rotation);
|
2012-06-14 09:06:06 -04:00
|
|
|
SetRotation( Rotation );
|
2013-04-22 03:18:03 -04:00
|
|
|
SetPitch( -Pitch );
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-03-22 02:33:10 -04:00
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
switch (m_EMState)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-12-22 05:15:53 -05:00
|
|
|
case IDLE:
|
|
|
|
{
|
|
|
|
// If enemy passive we ignore checks for player visibility
|
2013-04-13 17:02:10 -04:00
|
|
|
InStateIdle(a_Dt);
|
2012-12-22 05:15:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
case CHASING:
|
|
|
|
{
|
|
|
|
// If we do not see a player anymore skip chasing action
|
2013-04-13 17:02:10 -04:00
|
|
|
InStateChasing(a_Dt);
|
2012-12-22 05:15:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
case ESCAPING:
|
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
InStateEscaping(a_Dt);
|
2012-12-22 05:15:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // switch (m_EMState)
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
2012-12-21 06:04:08 -05:00
|
|
|
void cMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-21 06:04:08 -05:00
|
|
|
super::DoTakeDamage(a_TDI);
|
2013-03-22 02:33:10 -04: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 06:04:08 -05:00
|
|
|
if (a_TDI.Attacker != NULL)
|
|
|
|
{
|
|
|
|
m_Target = a_TDI.Attacker;
|
|
|
|
AddReference(m_Target);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
void cMonster::KilledBy(cEntity * a_Killer)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-21 06:04:08 -05:00
|
|
|
super::KilledBy(a_Killer);
|
2013-07-01 06:39:56 -04: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 09:06:06 -04:00
|
|
|
m_DestroyTimer = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
//----State Logic
|
|
|
|
|
2012-08-19 15:42:32 -04: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 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for debugging
|
|
|
|
void cMonster::SetState(const AString & a_State)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
if (a_State.compare("Idle") == 0)
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
m_EMState = IDLE;
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
else if (a_State.compare("Attacking") == 0)
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
m_EMState = ATTACKING;
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
else if (a_State.compare("Chasing") == 0)
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
m_EMState = CHASING;
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-06 15:00:03 -04:00
|
|
|
LOGD("cMonster::SetState(): Invalid state");
|
|
|
|
ASSERT(!"Invalid state");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
//Checks to see if EventSeePlayer should be fired
|
|
|
|
//monster sez: Do I see the player
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::CheckEventSeePlayer(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-22 05:15:53 -05:00
|
|
|
// TODO: Rewrite this to use cWorld's DoWithPlayers()
|
|
|
|
cPlayer * Closest = FindClosestPlayer();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
if (Closest != NULL)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
EventSeePlayer(Closest);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::CheckEventLostPlayer(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Vector3f pos;
|
2012-08-19 15:42:32 -04:00
|
|
|
cTracer LineOfSight(GetWorld());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
if (m_Target != NULL)
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
pos = m_Target->GetPosition();
|
2013-03-22 02:33:10 -04:00
|
|
|
if ((pos - GetPosition()).Length() > m_SightDistance || LineOfSight.Trace(GetPosition(),(pos - GetPosition()), (int)(pos - GetPosition()).Length()))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
EventLosePlayer();
|
|
|
|
}
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
EventLosePlayer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// What to do if player is seen
|
|
|
|
// default to change state to chasing
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::EventSeePlayer(cEntity * a_SeenPlayer)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_Target = a_SeenPlayer;
|
2013-04-13 17:02:10 -04:00
|
|
|
AddReference(m_Target);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
void cMonster::EventLosePlayer(void)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
Dereference(m_Target);
|
2013-04-13 17:02:10 -04:00
|
|
|
m_Target = NULL;
|
2012-06-14 09:06:06 -04:00
|
|
|
m_EMState = IDLE;
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 05:15:53 -05:00
|
|
|
// What to do if in Idle State
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::InStateIdle(float a_Dt)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
idle_interval += a_Dt;
|
2012-08-19 15:42:32 -04:00
|
|
|
if (idle_interval > 1)
|
|
|
|
{
|
|
|
|
// at this interval the results are predictable
|
2013-04-13 17:02:10 -04:00
|
|
|
int rem = m_World->GetTickRandomNumber(6) + 1;
|
2012-08-19 15:42:32 -04: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 09:06:06 -04:00
|
|
|
Vector3f Dist;
|
2013-04-13 17:02:10 -04:00
|
|
|
Dist.x = (float)(m_World->GetTickRandomNumber(10) - 5);
|
|
|
|
Dist.z = (float)(m_World->GetTickRandomNumber(10) - 5);
|
2012-12-22 05:15:53 -05:00
|
|
|
if ((Dist.SqrLength() > 2) && (rem >= 3))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
m_Destination.x = (float)(GetPosX() + Dist.x);
|
|
|
|
m_Destination.z = (float)(GetPosZ() + Dist.z);
|
2013-04-13 17:02:10 -04:00
|
|
|
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);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// What to do if in Chasing State
|
|
|
|
// This state should always be defined in each child class
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::InStateChasing(float a_Dt)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
|
|
|
UNUSED(a_Dt);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// What to do if in Escaping State
|
2013-04-13 17:02:10 -04:00
|
|
|
void cMonster::InStateEscaping(float a_Dt)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-12-22 05:15:53 -05:00
|
|
|
UNUSED(a_Dt);
|
|
|
|
|
|
|
|
if (m_Target != NULL)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
Vector3d newloc = GetPosition();
|
2012-06-14 09:06:06 -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);
|
|
|
|
}
|
2012-08-19 15:42:32 -04:00
|
|
|
else
|
|
|
|
{
|
2012-12-22 05:15:53 -05:00
|
|
|
m_EMState = IDLE; // This shouldnt be required but just to be safe
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
// Do attack here
|
|
|
|
// a_Dt is passed so we can set attack rate
|
|
|
|
void cMonster::Attack(float a_Dt)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04: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 06:04:08 -05:00
|
|
|
((cPawn *)m_Target)->TakeDamage(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
// Checks for Players close by and if they are visible return the closest
|
2012-06-14 09:06:06 -04:00
|
|
|
cPlayer * cMonster::FindClosestPlayer(void)
|
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
return m_World->FindClosestPlayer(GetPosition(), m_SightDistance);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-22 04:39:13 -05:00
|
|
|
void cMonster::GetMonsterConfig(const AString & a_Name)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-22 04:39:13 -05:00
|
|
|
cRoot::Get()->GetMonsterConfig()->AssignAttributes(this, a_Name);
|
2012-06-14 09:06:06 -04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-20 07:25:56 -04:00
|
|
|
AString cMonster::MobTypeToString(cMonster::eType a_MobType)
|
|
|
|
{
|
|
|
|
// Mob types aren't sorted, so we need to search linearly:
|
|
|
|
for (int i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
|
|
|
|
{
|
|
|
|
if (g_MobTypeNames[i].m_Type == a_MobType)
|
|
|
|
{
|
|
|
|
return g_MobTypeNames[i].m_lcName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cMonster::eType cMonster::StringToMobType(const AString & a_Name)
|
|
|
|
{
|
|
|
|
AString lcName(a_Name);
|
|
|
|
StrToLower(lcName);
|
|
|
|
|
|
|
|
// Binary-search for the lowercase name:
|
2013-10-20 07:42:59 -04:00
|
|
|
int lo = 0, hi = ARRAYCOUNT(g_MobTypeNames) - 1;
|
2013-10-20 07:25:56 -04:00
|
|
|
while (hi - lo > 1)
|
|
|
|
{
|
|
|
|
int mid = (lo + hi) / 2;
|
|
|
|
int res = strcmp(g_MobTypeNames[mid].m_lcName, lcName.c_str());
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
return g_MobTypeNames[mid].m_Type;
|
|
|
|
}
|
|
|
|
if (res < 0)
|
|
|
|
{
|
2013-10-20 07:42:59 -04:00
|
|
|
lo = mid;
|
2013-10-20 07:25:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-20 07:42:59 -04:00
|
|
|
hi = mid;
|
2013-10-20 07:25:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Range has collapsed to at most two elements, compare each:
|
|
|
|
if (strcmp(g_MobTypeNames[lo].m_lcName, lcName.c_str()) == 0)
|
|
|
|
{
|
|
|
|
return g_MobTypeNames[lo].m_Type;
|
|
|
|
}
|
|
|
|
if ((lo != hi) && (strcmp(g_MobTypeNames[hi].m_lcName, lcName.c_str()) == 0))
|
|
|
|
{
|
|
|
|
return g_MobTypeNames[hi].m_Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found:
|
|
|
|
return mtInvalidType;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cMonster::eFamily cMonster::FamilyFromType(eType a_Type)
|
|
|
|
{
|
2013-10-20 08:15:55 -04:00
|
|
|
switch (a_Type)
|
2013-10-20 07:25:56 -04:00
|
|
|
{
|
2013-10-20 08:15:55 -04:00
|
|
|
case mtBat: return mfAmbient;
|
|
|
|
case mtBlaze: return mfHostile;
|
|
|
|
case mtCaveSpider: return mfHostile;
|
|
|
|
case mtChicken: return mfPassive;
|
|
|
|
case mtCow: return mfPassive;
|
|
|
|
case mtCreeper: return mfHostile;
|
|
|
|
case mtEnderman: return mfHostile;
|
|
|
|
case mtGhast: return mfHostile;
|
|
|
|
case mtHorse: return mfPassive;
|
|
|
|
case mtMagmaCube: return mfHostile;
|
|
|
|
case mtMooshroom: return mfHostile;
|
|
|
|
case mtOcelot: return mfHostile;
|
|
|
|
case mtPig: return mfPassive;
|
|
|
|
case mtSheep: return mfPassive;
|
|
|
|
case mtSilverfish: return mfHostile;
|
|
|
|
case mtSkeleton: return mfHostile;
|
|
|
|
case mtSlime: return mfHostile;
|
|
|
|
case mtSpider: return mfHostile;
|
|
|
|
case mtSquid: return mfWater;
|
|
|
|
case mtVillager: return mfPassive;
|
|
|
|
case mtWitch: return mfHostile;
|
|
|
|
case mtWolf: return mfHostile;
|
|
|
|
case mtZombie: return mfHostile;
|
|
|
|
case mtZombiePigman: return mfHostile;
|
2013-10-20 07:25:56 -04:00
|
|
|
} ;
|
2013-10-20 08:15:55 -04:00
|
|
|
ASSERT(!"Unhandled mob type");
|
2013-10-20 07:25:56 -04:00
|
|
|
return mfMaxplusone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-20 08:00:45 -04:00
|
|
|
int cMonster::GetSpawnRate(cMonster::eFamily a_MobFamily)
|
|
|
|
{
|
|
|
|
switch (a_MobFamily)
|
|
|
|
{
|
|
|
|
case mfHostile: return 1;
|
|
|
|
case mfPassive: return 400;
|
|
|
|
case mfAmbient: return 400;
|
|
|
|
case mfWater: return 400;
|
|
|
|
}
|
|
|
|
ASSERT(!"Unhandled mob family");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-20 07:25:56 -04:00
|
|
|
cMonster * cMonster::NewMonsterFromType(cMonster::eType a_MobType, int a_Size)
|
|
|
|
{
|
|
|
|
cFastRandom Random;
|
|
|
|
|
|
|
|
cMonster * toReturn = NULL;
|
|
|
|
|
|
|
|
// unspecified size get rand[1,3] for Monsters that need size
|
|
|
|
switch (a_MobType)
|
|
|
|
{
|
|
|
|
case mtMagmaCube:
|
|
|
|
case mtSlime:
|
|
|
|
{
|
|
|
|
if (a_Size == -1)
|
|
|
|
{
|
|
|
|
a_Size = Random.NextInt(2, a_MobType) + 1;
|
|
|
|
}
|
|
|
|
if ((a_Size <= 0) || (a_Size >= 4))
|
|
|
|
{
|
|
|
|
ASSERT(!"Random for size was supposed to pick in [1..3] and picked outside");
|
|
|
|
a_Size = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
} // switch (a_MobType)
|
|
|
|
|
|
|
|
// Create the mob entity
|
|
|
|
switch (a_MobType)
|
|
|
|
{
|
|
|
|
case mtMagmaCube: toReturn = new cMagmaCube(a_Size); break;
|
|
|
|
case mtSlime: toReturn = new cSlime(a_Size); break;
|
|
|
|
case mtBat: toReturn = new cBat(); break;
|
|
|
|
case mtBlaze: toReturn = new cBlaze(); break;
|
|
|
|
case mtCaveSpider: toReturn = new cCavespider(); break;
|
|
|
|
case mtChicken: toReturn = new cChicken(); break;
|
|
|
|
case mtCow: toReturn = new cCow(); break;
|
|
|
|
case mtCreeper: toReturn = new cCreeper(); break;
|
|
|
|
case mtEnderman: toReturn = new cEnderman(); break;
|
|
|
|
case mtGhast: toReturn = new cGhast(); break;
|
|
|
|
// TODO:
|
|
|
|
// case cMonster::mtHorse: toReturn = new cHorse(); break;
|
|
|
|
case mtMooshroom: toReturn = new cMooshroom(); break;
|
|
|
|
case mtOcelot: toReturn = new cOcelot(); break;
|
|
|
|
case mtPig: toReturn = new cPig(); break;
|
|
|
|
// TODO: Implement sheep color
|
|
|
|
case mtSheep: toReturn = new cSheep(0); break;
|
|
|
|
case mtSilverfish: toReturn = new cSilverfish(); break;
|
|
|
|
// TODO: Implement wither skeleton geration
|
|
|
|
case mtSkeleton: toReturn = new cSkeleton(false); break;
|
|
|
|
case mtSpider: toReturn = new cSpider(); break;
|
|
|
|
case mtSquid: toReturn = new cSquid(); break;
|
|
|
|
case mtVillager: toReturn = new cVillager(cVillager::vtFarmer); break;
|
|
|
|
case mtWitch: toReturn = new cWitch(); break;
|
|
|
|
case mtWolf: toReturn = new cWolf(); break;
|
|
|
|
case mtZombie: toReturn = new cZombie(false); break;
|
|
|
|
case mtZombiePigman: toReturn = new cZombiePigman(); break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled Mob type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth)
|
2012-06-14 09:06:06 -04: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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-05 16:40:08 -04:00
|
|
|
|
|
|
|
void cMonster::HandleDaylightBurning(cChunk & a_Chunk)
|
|
|
|
{
|
|
|
|
if (!m_BurnsInDaylight)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RelY = (int)floor(GetPosY());
|
|
|
|
if ((RelY < 0) || (RelY >= cChunkDef::Height))
|
|
|
|
{
|
|
|
|
// Outside the world
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RelX = (int)floor(GetPosX()) - a_Chunk.GetPosX() * cChunkDef::Width;
|
|
|
|
int RelZ = (int)floor(GetPosZ()) - a_Chunk.GetPosZ() * cChunkDef::Width;
|
|
|
|
if (
|
|
|
|
(a_Chunk.GetSkyLight(RelX, RelY, RelZ) == 15) && // In the daylight
|
|
|
|
(a_Chunk.GetBlock(RelX, RelY, RelZ) != E_BLOCK_SOULSAND) && // Not on soulsand
|
|
|
|
(GetWorld()->GetTimeOfDay() < (12000 + 1000)) && // It is nighttime
|
|
|
|
!IsOnFire() // Not already burning
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Burn for 100 ticks, then decide again
|
|
|
|
StartBurning(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-10 09:09:45 -04:00
|
|
|
cMonster::eFamily cMonster::GetMobFamily(void) const
|
|
|
|
{
|
2013-10-20 07:25:56 -04:00
|
|
|
return FamilyFromType(m_MobType);
|
2013-09-10 09:09:45 -04:00
|
|
|
}
|
2013-10-20 04:23:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|