2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Player.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "ClientHandle.h"
|
2012-09-23 13:19:34 -04:00
|
|
|
#include "UI/Window.h"
|
|
|
|
#include "UI/WindowOwner.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "World.h"
|
|
|
|
#include "Pickup.h"
|
|
|
|
#include "PluginManager.h"
|
2013-05-28 15:12:47 -04:00
|
|
|
#include "BlockEntities/BlockEntity.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "GroupManager.h"
|
|
|
|
#include "Group.h"
|
|
|
|
#include "ChatColor.h"
|
|
|
|
#include "Item.h"
|
|
|
|
#include "Tracer.h"
|
|
|
|
#include "Root.h"
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "OSSupport/MakeDir.h"
|
|
|
|
#include "OSSupport/Timer.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "MersenneTwister.h"
|
2013-07-12 16:01:25 -04:00
|
|
|
#include "Chunk.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Vector3d.h"
|
|
|
|
#include "Vector3f.h"
|
|
|
|
|
|
|
|
#include "../iniFile/iniFile.h"
|
|
|
|
#include <json/json.h>
|
|
|
|
|
|
|
|
#define float2int(x) ((x)<0 ? ((int)(x))-1 : (int)(x))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
|
2013-07-01 06:39:56 -04:00
|
|
|
: super(etPlayer, 0.6, 1.8)
|
2012-12-21 07:52:14 -05:00
|
|
|
, m_GameMode(eGameMode_NotSet)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_IP("")
|
|
|
|
, m_LastBlockActionTime( 0 )
|
|
|
|
, m_LastBlockActionCnt( 0 )
|
|
|
|
, m_bVisible( true )
|
|
|
|
, m_LastGroundHeight( 0 )
|
|
|
|
, m_bTouchGround( false )
|
|
|
|
, m_Stance( 0.0 )
|
2012-09-20 09:25:54 -04:00
|
|
|
, m_Inventory(*this)
|
|
|
|
, m_CurrentWindow(NULL)
|
|
|
|
, m_InventoryWindow(NULL)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_TimeLastPickupCheck( 0.f )
|
|
|
|
, m_Color('-')
|
|
|
|
, m_ClientHandle( a_Client )
|
2013-07-12 16:01:25 -04:00
|
|
|
, m_FoodLevel(20)
|
|
|
|
, m_FoodSaturationLevel(5)
|
2012-07-17 08:02:03 -04:00
|
|
|
, m_FoodTickTimer(0)
|
2013-07-12 16:01:25 -04:00
|
|
|
, m_FoodExhaustionLevel(0)
|
|
|
|
, m_FoodPoisonedTicksRemaining(0)
|
2013-07-07 10:13:43 -04:00
|
|
|
, m_NormalMaxSpeed(0.1)
|
|
|
|
, m_SprintingMaxSpeed(0.13)
|
2013-06-04 10:18:03 -04:00
|
|
|
, m_IsCrouched(false)
|
2013-07-07 10:13:43 -04:00
|
|
|
, m_IsSprinting(false)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d",
|
2012-09-04 15:05:35 -04:00
|
|
|
a_PlayerName.c_str(), a_Client->GetIPString().c_str(),
|
2012-06-14 09:06:06 -04:00
|
|
|
this, GetUniqueID()
|
|
|
|
);
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
m_InventoryWindow = new cInventoryWindow(*this);
|
|
|
|
m_CurrentWindow = m_InventoryWindow;
|
2012-09-20 16:10:46 -04:00
|
|
|
m_InventoryWindow->OpenedByPlayer(*this);
|
2012-07-17 08:02:03 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
SetMaxHealth(20);
|
2012-12-21 06:04:08 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cTimer t1;
|
|
|
|
m_LastPlayerListTime = t1.GetNowTime();
|
|
|
|
|
2012-11-01 17:38:20 -04:00
|
|
|
m_TimeLastTeleportPacket = 0;
|
|
|
|
m_TimeLastPickupCheck = 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
m_PlayerName = a_PlayerName;
|
|
|
|
m_bDirtyPosition = true; // So chunks are streamed to player at spawn
|
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
if (!LoadFromDisk())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
m_Inventory.Clear();
|
2013-03-22 02:33:10 -04:00
|
|
|
SetPosX(cRoot::Get()->GetDefaultWorld()->GetSpawnX());
|
|
|
|
SetPosY(cRoot::Get()->GetDefaultWorld()->GetSpawnY());
|
|
|
|
SetPosZ(cRoot::Get()->GetDefaultWorld()->GetSpawnZ());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
LOGD("Player \"%s\" is connecting for the first time, spawning at default world spawn {%.2f, %.2f, %.2f}",
|
2013-03-22 02:33:10 -04:00
|
|
|
a_PlayerName.c_str(), GetPosX(), GetPosY(), GetPosZ()
|
2012-06-14 09:06:06 -04:00
|
|
|
);
|
|
|
|
}
|
2013-03-22 02:33:10 -04:00
|
|
|
m_LastJumpHeight = (float)(GetPosY());
|
|
|
|
m_LastGroundHeight = (float)(GetPosY());
|
|
|
|
m_Stance = GetPosY() + 1.62;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cPlayer::~cPlayer(void)
|
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
LOG("Deleting cPlayer \"%s\" at %p, ID %d", m_PlayerName.c_str(), this, GetUniqueID());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
SaveToDisk();
|
|
|
|
|
|
|
|
m_World->RemovePlayer( this );
|
|
|
|
|
|
|
|
m_ClientHandle = NULL;
|
|
|
|
|
2012-09-20 16:10:46 -04:00
|
|
|
delete m_InventoryWindow;
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
LOGD("Player %p deleted", this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-03 03:47:35 -04:00
|
|
|
void cPlayer::Initialize(cWorld * a_World)
|
2012-08-24 03:58:26 -04:00
|
|
|
{
|
2013-07-03 03:47:35 -04:00
|
|
|
super::Initialize(a_World);
|
2013-04-13 17:02:10 -04:00
|
|
|
GetWorld()->AddPlayer(this);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cPlayer::Destroyed()
|
|
|
|
{
|
2013-06-02 17:59:25 -04:00
|
|
|
CloseWindow(false);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_ClientHandle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cPlayer::SpawnOn(cClientHandle & a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
/*
|
|
|
|
LOGD("cPlayer::SpawnOn(%s) for \"%s\" at pos {%.2f, %.2f, %.2f}",
|
|
|
|
a_Client.GetUsername().c_str(), m_PlayerName.c_str(), m_Pos.x, m_Pos.y, m_Pos.z
|
2012-06-14 09:06:06 -04:00
|
|
|
);
|
2012-08-24 03:58:26 -04:00
|
|
|
*/
|
|
|
|
|
2012-09-29 17:54:17 -04:00
|
|
|
if (m_bVisible && (m_ClientHandle != (&a_Client)))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
a_Client.SendPlayerSpawn(*this);
|
2013-07-07 09:06:06 -04:00
|
|
|
a_Client.SendEntityHeadLook(*this);
|
2012-09-29 17:54:17 -04:00
|
|
|
a_Client.SendEntityEquipment(*this, 0, m_Inventory.GetEquippedItem() );
|
2013-05-19 14:22:37 -04:00
|
|
|
a_Client.SendEntityEquipment(*this, 1, m_Inventory.GetEquippedBoots() );
|
|
|
|
a_Client.SendEntityEquipment(*this, 2, m_Inventory.GetEquippedLeggings() );
|
|
|
|
a_Client.SendEntityEquipment(*this, 3, m_Inventory.GetEquippedChestplate() );
|
|
|
|
a_Client.SendEntityEquipment(*this, 4, m_Inventory.GetEquippedHelmet() );
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cPlayer::Tick(float a_Dt, cChunk & a_Chunk)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if (!m_ClientHandle->IsPlaying())
|
|
|
|
{
|
|
|
|
// We're not yet in the game, ignore everything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
super::Tick(a_Dt, a_Chunk);
|
2013-04-19 00:32:18 -04:00
|
|
|
if (m_bDirtyPosition)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
// Apply food exhaustion from movement:
|
|
|
|
ApplyFoodExhaustionFromMovement(a_Chunk);
|
|
|
|
|
2013-01-26 02:50:06 -05:00
|
|
|
cRoot::Get()->GetPluginManager()->CallHookPlayerMoving(*this);
|
2013-04-19 00:32:18 -04:00
|
|
|
BroadcastMovementUpdate(m_ClientHandle);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_ClientHandle->StreamChunks();
|
|
|
|
}
|
2013-04-19 00:32:18 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
BroadcastMovementUpdate(m_ClientHandle);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
if (m_Health > 0) // make sure player is alive
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_World->CollectPickupsByPlayer(this);
|
2012-07-17 08:02:03 -04:00
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
HandleFood();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send Player List (Once per m_LastPlayerListTime/1000 ms)
|
2013-07-12 16:01:25 -04:00
|
|
|
cTimer t1;
|
2012-06-14 09:06:06 -04:00
|
|
|
if (m_LastPlayerListTime + cPlayer::PLAYER_LIST_TIME_MS <= t1.GetNowTime())
|
|
|
|
{
|
|
|
|
m_World->SendPlayerList(this);
|
|
|
|
m_LastPlayerListTime = t1.GetNowTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cPlayer::SetTouchGround(bool a_bTouchGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
// If just
|
2012-06-14 09:06:06 -04:00
|
|
|
m_bTouchGround = a_bTouchGround;
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
if (!m_bTouchGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-01 14:05:41 -04:00
|
|
|
if (GetPosY() > m_LastJumpHeight)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-01 14:05:41 -04:00
|
|
|
m_LastJumpHeight = (float)GetPosY();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-04-01 14:05:41 -04:00
|
|
|
cWorld * World = GetWorld();
|
|
|
|
if ((GetPosY() >= 0) && (GetPosY() < 256))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-01 14:05:41 -04:00
|
|
|
BLOCKTYPE BlockType = World->GetBlock( float2int(GetPosX()), float2int(GetPosY()), float2int(GetPosZ()) );
|
|
|
|
if (BlockType != E_BLOCK_AIR)
|
|
|
|
{
|
|
|
|
// LOGD("TouchGround set to true by server");
|
|
|
|
m_bTouchGround = true;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(BlockType == E_BLOCK_WATER) ||
|
|
|
|
(BlockType == E_BLOCK_STATIONARY_WATER) ||
|
|
|
|
(BlockType == E_BLOCK_LADDER) ||
|
|
|
|
(BlockType == E_BLOCK_VINES)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// LOGD("Water / Ladder / Torch");
|
|
|
|
m_LastGroundHeight = (float)GetPosY();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
if (m_bTouchGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
float Dist = (float)(m_LastGroundHeight - floor(GetPosY()));
|
2012-11-26 05:03:34 -05:00
|
|
|
int Damage = (int)(Dist - 3.f);
|
2012-11-27 14:55:23 -05:00
|
|
|
if(m_LastJumpHeight > m_LastGroundHeight) Damage++;
|
2013-03-22 02:33:10 -04:00
|
|
|
m_LastJumpHeight = (float)GetPosY();
|
2012-08-19 15:42:32 -04:00
|
|
|
if (Damage > 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-01 06:39:56 -04:00
|
|
|
TakeDamage(dtFalling, NULL, Damage, Damage, 0);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-03-22 02:33:10 -04:00
|
|
|
m_LastGroundHeight = (float)GetPosY();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-07 15:59:45 -04:00
|
|
|
void cPlayer::Heal(int a_Health)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-07 15:59:45 -04:00
|
|
|
if (m_Health < GetMaxHealth())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-07 15:59:45 -04:00
|
|
|
m_Health = (short)std::min((int)a_Health + m_Health, (int)GetMaxHealth());
|
2012-07-17 08:02:03 -04:00
|
|
|
SendHealth();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
void cPlayer::SetFoodLevel(int a_FoodLevel)
|
|
|
|
{
|
|
|
|
m_FoodLevel = std::max(0, std::min(a_FoodLevel, (int)MAX_FOOD_LEVEL));
|
|
|
|
SendHealth();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetFoodSaturationLevel(double a_FoodSaturationLevel)
|
|
|
|
{
|
|
|
|
m_FoodSaturationLevel = std::max(0.0, std::min(a_FoodSaturationLevel, (double)m_FoodLevel));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetFoodTickTimer(int a_FoodTickTimer)
|
|
|
|
{
|
|
|
|
m_FoodTickTimer = a_FoodTickTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetFoodExhaustionLevel(double a_FoodSaturationLevel)
|
|
|
|
{
|
|
|
|
m_FoodExhaustionLevel = std::max(0.0, std::min(a_FoodSaturationLevel, 4.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetFoodPoisonedTicksRemaining(int a_FoodPoisonedTicksRemaining)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
m_FoodPoisonedTicksRemaining = a_FoodPoisonedTicksRemaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPlayer::Feed(int a_Food, double a_Saturation)
|
|
|
|
{
|
|
|
|
if (m_FoodLevel >= MAX_FOOD_LEVEL)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
m_FoodLevel = std::min(a_Food + m_FoodLevel, (int)MAX_FOOD_LEVEL);
|
|
|
|
m_FoodSaturationLevel = std::min(m_FoodSaturationLevel + a_Saturation, (double)m_FoodLevel);
|
2012-07-17 08:02:03 -04:00
|
|
|
|
|
|
|
SendHealth();
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
void cPlayer::FoodPoison(int a_NumTicks)
|
|
|
|
{
|
|
|
|
bool HasBeenFoodPoisoned = (m_FoodPoisonedTicksRemaining > 0);
|
|
|
|
m_FoodPoisonedTicksRemaining = std::max(m_FoodPoisonedTicksRemaining, a_NumTicks);
|
|
|
|
if (!HasBeenFoodPoisoned)
|
|
|
|
{
|
|
|
|
// TODO: Send the poisoning indication to the client - how?
|
|
|
|
SendHealth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SendHealth(void)
|
2012-07-17 08:02:03 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
if (m_ClientHandle != NULL)
|
|
|
|
{
|
|
|
|
m_ClientHandle->SendHealth();
|
|
|
|
}
|
2012-07-17 08:02:03 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-30 04:40:13 -04:00
|
|
|
void cPlayer::ClearInventoryPaintSlots(void)
|
|
|
|
{
|
|
|
|
// Clear the list of slots that are being inventory-painted. Used by cWindow only
|
|
|
|
m_InventoryPaintSlots.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::AddInventoryPaintSlot(int a_SlotNum)
|
|
|
|
{
|
|
|
|
// Add a slot to the list for inventory painting. Used by cWindow only
|
|
|
|
m_InventoryPaintSlots.push_back(a_SlotNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cSlotNums & cPlayer::GetInventoryPaintSlots(void) const
|
|
|
|
{
|
|
|
|
// Return the list of slots currently stored for inventory painting. Used by cWindow only
|
|
|
|
return m_InventoryPaintSlots;
|
2013-06-04 10:18:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 10:13:43 -04:00
|
|
|
double cPlayer::GetMaxSpeed(void) const
|
|
|
|
{
|
|
|
|
return m_IsSprinting ? m_SprintingMaxSpeed : m_NormalMaxSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetNormalMaxSpeed(double a_Speed)
|
|
|
|
{
|
|
|
|
m_NormalMaxSpeed = a_Speed;
|
|
|
|
if (!m_IsSprinting)
|
|
|
|
{
|
|
|
|
m_ClientHandle->SendPlayerMaxSpeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetSprintingMaxSpeed(double a_Speed)
|
|
|
|
{
|
|
|
|
m_SprintingMaxSpeed = a_Speed;
|
|
|
|
if (m_IsSprinting)
|
|
|
|
{
|
|
|
|
m_ClientHandle->SendPlayerMaxSpeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-04 10:18:03 -04:00
|
|
|
void cPlayer::SetCrouch(bool a_IsCrouched)
|
|
|
|
{
|
|
|
|
// Set the crouch status, broadcast to all visible players
|
|
|
|
|
|
|
|
if (a_IsCrouched == m_IsCrouched)
|
|
|
|
{
|
|
|
|
// No change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_IsCrouched = a_IsCrouched;
|
2013-07-07 09:06:06 -04:00
|
|
|
m_World->BroadcastEntityMetadata(*this);
|
2013-05-30 04:40:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 10:13:43 -04:00
|
|
|
void cPlayer::SetSprint(bool a_IsSprinting)
|
|
|
|
{
|
|
|
|
if (a_IsSprinting == m_IsSprinting)
|
|
|
|
{
|
|
|
|
// No change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_IsSprinting = a_IsSprinting;
|
|
|
|
m_ClientHandle->SendPlayerMaxSpeed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-21 06:04:08 -05:00
|
|
|
void cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-21 06:04:08 -05:00
|
|
|
if (m_GameMode == eGameMode_Creative)
|
2012-07-17 08:02:03 -04:00
|
|
|
{
|
2012-12-21 06:04:08 -05:00
|
|
|
// No damage / health in creative mode
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
super::DoTakeDamage(a_TDI);
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
// Any kind of damage adds food exhaustion
|
|
|
|
AddFoodExhaustion(0.3f);
|
2012-12-21 06:04:08 -05:00
|
|
|
|
|
|
|
SendHealth();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
void cPlayer::KilledBy(cEntity * a_Killer)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-01 06:39:56 -04:00
|
|
|
super::KilledBy(a_Killer);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
if (m_Health > 0)
|
|
|
|
{
|
|
|
|
return; // not dead yet =]
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bVisible = false; // So new clients don't see the player
|
|
|
|
|
|
|
|
// Puke out all the items
|
|
|
|
cItems Pickups;
|
2013-05-24 03:30:39 -04:00
|
|
|
m_Inventory.CopyToItems(Pickups);
|
2013-05-19 14:22:37 -04:00
|
|
|
m_Inventory.Clear();
|
2013-03-22 02:33:10 -04:00
|
|
|
m_World->SpawnItemPickups(Pickups, GetPosX(), GetPosY(), GetPosZ(), 10);
|
2013-05-24 03:30:39 -04:00
|
|
|
SaveToDisk(); // Save it, yeah the world is a tough place !
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-24 03:30:39 -04:00
|
|
|
void cPlayer::Respawn(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_Health = GetMaxHealth();
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
m_ClientHandle->SendRespawn();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
// Extinguish the fire:
|
|
|
|
StopBurning();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
TeleportToCoords(GetWorld()->GetSpawnX(), GetWorld()->GetSpawnY(), GetWorld()->GetSpawnZ());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
SetVisible(true);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
double cPlayer::GetEyeHeight(void) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return m_Stance;
|
|
|
|
}
|
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vector3d cPlayer::GetEyePosition(void) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
return Vector3d( GetPosX(), m_Stance, GetPosZ() );
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-30 15:34:09 -04:00
|
|
|
void cPlayer::OpenWindow(cWindow * a_Window)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-30 16:40:43 -04:00
|
|
|
if (a_Window != m_CurrentWindow)
|
|
|
|
{
|
2013-06-02 17:59:25 -04:00
|
|
|
CloseWindow(false);
|
2013-05-30 16:40:43 -04:00
|
|
|
}
|
2012-09-20 09:25:54 -04:00
|
|
|
a_Window->OpenedByPlayer(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_CurrentWindow = a_Window;
|
2013-05-30 15:42:19 -04:00
|
|
|
a_Window->SendWholeWindow(*GetClientHandle());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 17:59:25 -04:00
|
|
|
void cPlayer::CloseWindow(bool a_CanRefuse)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-30 15:34:09 -04:00
|
|
|
if (m_CurrentWindow == NULL)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-30 15:34:09 -04:00
|
|
|
m_CurrentWindow = m_InventoryWindow;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-02 17:59:25 -04:00
|
|
|
if (m_CurrentWindow->ClosedByPlayer(*this, a_CanRefuse) || !a_CanRefuse)
|
2013-05-30 16:40:43 -04:00
|
|
|
{
|
|
|
|
// Close accepted, go back to inventory window (the default):
|
|
|
|
m_CurrentWindow = m_InventoryWindow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Re-open the window
|
|
|
|
m_CurrentWindow->OpenedByPlayer(*this);
|
|
|
|
m_CurrentWindow->SendWholeWindow(*GetClientHandle());
|
|
|
|
}
|
2013-05-30 15:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 17:59:25 -04:00
|
|
|
void cPlayer::CloseWindowIfID(char a_WindowID, bool a_CanRefuse)
|
2013-05-30 15:34:09 -04:00
|
|
|
{
|
|
|
|
if ((m_CurrentWindow == NULL) || (m_CurrentWindow->GetWindowID() != a_WindowID))
|
|
|
|
{
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-05-30 16:40:43 -04:00
|
|
|
CloseWindow();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetLastBlockActionTime()
|
|
|
|
{
|
|
|
|
if (m_World != NULL)
|
|
|
|
{
|
2012-11-01 17:38:20 -04:00
|
|
|
m_LastBlockActionTime = m_World->GetWorldAge() / 20.0f;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::SetLastBlockActionCnt( int a_LastBlockActionCnt )
|
|
|
|
{
|
|
|
|
m_LastBlockActionCnt = a_LastBlockActionCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cPlayer::SetGameMode(eGameMode a_GameMode)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-11-27 14:55:23 -05:00
|
|
|
if ((a_GameMode >= 3) || (a_GameMode < 0))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
LOGWARNING("%s: Setting invalid gamemode: %d", GetName().c_str(), a_GameMode);
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
if (m_GameMode == a_GameMode)
|
|
|
|
{
|
|
|
|
// Gamemode already set
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_GameMode = a_GameMode;
|
|
|
|
m_ClientHandle->SendGameMode(a_GameMode);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::LoginSetGameMode( eGameMode a_GameMode )
|
|
|
|
{
|
|
|
|
m_GameMode = a_GameMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cPlayer::SetIP(const AString & a_IP)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_IP = a_IP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cPlayer::SendMessage(const AString & a_Message)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
m_ClientHandle->SendChat(a_Message);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-01 06:39:56 -04:00
|
|
|
void cPlayer::TeleportToCoords(double a_PosX, double a_PosY, double a_PosZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
SetPosition( a_PosX, a_PosY, a_PosZ );
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
m_World->BroadcastTeleportEntity(*this, GetClientHandle());
|
|
|
|
m_ClientHandle->SendPlayerMoveLook();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::MoveTo( const Vector3d & a_NewPos )
|
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
if ((a_NewPos.y < -990) && (GetPosY() > -100))
|
2013-03-03 14:05:11 -05:00
|
|
|
{
|
|
|
|
// When attached to an entity, the client sends position packets with weird coords:
|
|
|
|
// Y = -999 and X, Z = attempting to create speed, usually up to 0.03
|
2013-03-05 15:49:07 -05:00
|
|
|
// We cannot test m_AttachedTo, because when deattaching, the server thinks the client is already deattached while
|
|
|
|
// the client may still send more of these nonsensical packets.
|
|
|
|
if (m_AttachedTo != NULL)
|
|
|
|
{
|
|
|
|
Vector3d AddSpeed(a_NewPos);
|
|
|
|
AddSpeed.y = 0;
|
|
|
|
m_AttachedTo->AddSpeed(AddSpeed);
|
|
|
|
}
|
2013-03-03 14:05:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// TODO: should do some checks to see if player is not moving through terrain
|
|
|
|
// TODO: Official server refuses position packets too far away from each other, kicking "hacked" clients; we should, too
|
|
|
|
|
|
|
|
SetPosition( a_NewPos );
|
2012-08-19 15:42:32 -04:00
|
|
|
SetStance(a_NewPos.y + 1.62);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cPlayer::SetVisible(bool a_bVisible)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if (a_bVisible && !m_bVisible) // Make visible
|
|
|
|
{
|
|
|
|
m_bVisible = true;
|
2013-07-07 09:06:06 -04:00
|
|
|
m_World->BroadcastSpawnEntity(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
if (!a_bVisible && m_bVisible)
|
|
|
|
{
|
|
|
|
m_bVisible = false;
|
2012-08-19 15:42:32 -04:00
|
|
|
m_World->BroadcastDestroyEntity(*this, m_ClientHandle); // Destroy on all clients
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-18 17:54:56 -04:00
|
|
|
void cPlayer::AddToGroup( const AString & a_GroupName )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cGroup* Group = cRoot::Get()->GetGroupManager()->GetGroup( a_GroupName );
|
|
|
|
m_Groups.push_back( Group );
|
2012-10-18 17:54:56 -04:00
|
|
|
LOGD("Added %s to group %s", m_PlayerName.c_str(), a_GroupName.c_str() );
|
2012-06-14 09:06:06 -04:00
|
|
|
ResolveGroups();
|
|
|
|
ResolvePermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-18 17:54:56 -04:00
|
|
|
void cPlayer::RemoveFromGroup( const AString & a_GroupName )
|
|
|
|
{
|
|
|
|
bool bRemoved = false;
|
|
|
|
for( GroupList::iterator itr = m_Groups.begin(); itr != m_Groups.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( (*itr)->GetName().compare(a_GroupName ) == 0 )
|
|
|
|
{
|
|
|
|
m_Groups.erase( itr );
|
|
|
|
bRemoved = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bRemoved )
|
|
|
|
{
|
|
|
|
LOGD("Removed %s from group %s", m_PlayerName.c_str(), a_GroupName.c_str() );
|
|
|
|
ResolveGroups();
|
|
|
|
ResolvePermissions();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGWARN("Tried to remove %s from group %s but was not in that group", m_PlayerName.c_str(), a_GroupName.c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPlayer::CanUseCommand( const AString & a_Command )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
for( GroupList::iterator itr = m_Groups.begin(); itr != m_Groups.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( (*itr)->HasCommand( a_Command ) ) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-19 11:06:28 -04:00
|
|
|
bool cPlayer::HasPermission(const AString & a_Permission)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-19 11:06:28 -04:00
|
|
|
if (a_Permission.empty())
|
|
|
|
{
|
|
|
|
// Empty permission request is always granted
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
AStringVector Split = StringSplit( a_Permission, "." );
|
|
|
|
PermissionMap Possibilities = m_ResolvedPermissions;
|
|
|
|
// Now search the namespaces
|
|
|
|
while( Possibilities.begin() != Possibilities.end() )
|
|
|
|
{
|
|
|
|
PermissionMap::iterator itr = Possibilities.begin();
|
|
|
|
if( itr->second )
|
|
|
|
{
|
|
|
|
AStringVector OtherSplit = StringSplit( itr->first, "." );
|
|
|
|
if( OtherSplit.size() <= Split.size() )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
for( i = 0; i < OtherSplit.size(); ++i )
|
|
|
|
{
|
|
|
|
if( OtherSplit[i].compare( Split[i] ) != 0 )
|
|
|
|
{
|
|
|
|
if( OtherSplit[i].compare("*") == 0 ) return true; // WildCard man!! WildCard!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( i == Split.size() ) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Possibilities.erase( itr );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing that matched :(
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-18 17:54:56 -04:00
|
|
|
bool cPlayer::IsInGroup( const AString & a_Group )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
for( GroupList::iterator itr = m_ResolvedGroups.begin(); itr != m_ResolvedGroups.end(); ++itr )
|
|
|
|
{
|
2012-10-18 17:54:56 -04:00
|
|
|
if( a_Group.compare( (*itr)->GetName().c_str() ) == 0 )
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::ResolvePermissions()
|
|
|
|
{
|
|
|
|
m_ResolvedPermissions.clear(); // Start with an empty map yo~
|
|
|
|
|
|
|
|
// Copy all player specific permissions into the resolved permissions map
|
|
|
|
for( PermissionMap::iterator itr = m_Permissions.begin(); itr != m_Permissions.end(); ++itr )
|
|
|
|
{
|
|
|
|
m_ResolvedPermissions[ itr->first ] = itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( GroupList::iterator GroupItr = m_ResolvedGroups.begin(); GroupItr != m_ResolvedGroups.end(); ++GroupItr )
|
|
|
|
{
|
|
|
|
const cGroup::PermissionMap & Permissions = (*GroupItr)->GetPermissions();
|
|
|
|
for( cGroup::PermissionMap::const_iterator itr = Permissions.begin(); itr != Permissions.end(); ++itr )
|
|
|
|
{
|
|
|
|
m_ResolvedPermissions[ itr->first ] = itr->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::ResolveGroups()
|
|
|
|
{
|
|
|
|
// Clear resolved groups first
|
|
|
|
m_ResolvedGroups.clear();
|
|
|
|
|
|
|
|
// Get a complete resolved list of all groups the player is in
|
|
|
|
std::map< cGroup*, bool > AllGroups; // Use a map, because it's faster than iterating through a list to find duplicates
|
|
|
|
GroupList ToIterate;
|
|
|
|
for( GroupList::iterator GroupItr = m_Groups.begin(); GroupItr != m_Groups.end(); ++GroupItr )
|
|
|
|
{
|
|
|
|
ToIterate.push_back( *GroupItr );
|
|
|
|
}
|
|
|
|
while( ToIterate.begin() != ToIterate.end() )
|
|
|
|
{
|
|
|
|
cGroup* CurrentGroup = *ToIterate.begin();
|
|
|
|
if( AllGroups.find( CurrentGroup ) != AllGroups.end() )
|
|
|
|
{
|
|
|
|
LOGWARNING("ERROR: Player \"%s\" is in the group multiple times (\"%s\"). Please fix your settings in users.ini!",
|
|
|
|
m_PlayerName.c_str(), CurrentGroup->GetName().c_str()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AllGroups[ CurrentGroup ] = true;
|
|
|
|
m_ResolvedGroups.push_back( CurrentGroup ); // Add group to resolved list
|
|
|
|
const cGroup::GroupList & Inherits = CurrentGroup->GetInherits();
|
|
|
|
for( cGroup::GroupList::const_iterator itr = Inherits.begin(); itr != Inherits.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( AllGroups.find( *itr ) != AllGroups.end() )
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Player %s is in the same group multiple times due to inheritance (%s). FIX IT!", m_PlayerName.c_str(), (*itr)->GetName().c_str() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ToIterate.push_back( *itr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ToIterate.erase( ToIterate.begin() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AString cPlayer::GetColor(void) const
|
|
|
|
{
|
|
|
|
if ( m_Color != '-' )
|
|
|
|
{
|
|
|
|
return cChatColor::MakeColor( m_Color );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_Groups.size() < 1 )
|
|
|
|
{
|
|
|
|
return cChatColor::White;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*m_Groups.begin())->GetColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-10 15:01:36 -04:00
|
|
|
void cPlayer::TossItem(
|
|
|
|
bool a_bDraggingItem,
|
2012-08-28 17:59:49 -04:00
|
|
|
char a_Amount /* = 1 */,
|
|
|
|
short a_CreateType /* = 0 */,
|
|
|
|
short a_CreateHealth /* = 0 */
|
2012-08-10 15:01:36 -04:00
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cItems Drops;
|
2013-05-24 03:30:39 -04:00
|
|
|
if (a_CreateType != 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-10 15:01:36 -04:00
|
|
|
// Just create item without touching the inventory (used in creative mode)
|
2012-08-28 17:59:49 -04:00
|
|
|
Drops.push_back(cItem(a_CreateType, a_Amount, a_CreateHealth));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-10 15:01:36 -04:00
|
|
|
// Drop an item from the inventory:
|
|
|
|
if (a_bDraggingItem)
|
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
cItem & Item = GetDraggingItem();
|
|
|
|
if (!Item.IsEmpty())
|
2012-08-10 15:01:36 -04:00
|
|
|
{
|
2012-10-24 08:48:25 -04:00
|
|
|
char OriginalItemAmount = Item.m_ItemCount;
|
2013-05-07 15:59:45 -04:00
|
|
|
Item.m_ItemCount = std::min(OriginalItemAmount, a_Amount);
|
2012-09-20 09:25:54 -04:00
|
|
|
Drops.push_back(Item);
|
2012-10-24 08:48:25 -04:00
|
|
|
if (OriginalItemAmount > a_Amount)
|
2012-09-20 09:25:54 -04:00
|
|
|
{
|
2012-10-24 08:48:25 -04:00
|
|
|
Item.m_ItemCount = OriginalItemAmount - (char)a_Amount;
|
2012-09-20 09:25:54 -04:00
|
|
|
}
|
2012-08-10 15:01:36 -04:00
|
|
|
else
|
2012-09-20 09:25:54 -04:00
|
|
|
{
|
|
|
|
Item.Empty();
|
|
|
|
}
|
2012-08-10 15:01:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-10 15:01:36 -04:00
|
|
|
// Else drop equipped item
|
2013-05-19 14:22:37 -04:00
|
|
|
cItem DroppedItem(GetInventory().GetEquippedItem());
|
2012-08-10 15:01:36 -04:00
|
|
|
if (!DroppedItem.IsEmpty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-24 03:30:39 -04:00
|
|
|
if (GetInventory().RemoveOneEquippedItem())
|
2012-08-10 15:01:36 -04:00
|
|
|
{
|
|
|
|
DroppedItem.m_ItemCount = 1; // RemoveItem decreases the count, so set it to 1 again
|
|
|
|
Drops.push_back(DroppedItem);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-09 09:35:43 -05:00
|
|
|
double vX = 0, vY = 0, vZ = 0;
|
2012-08-10 15:01:36 -04:00
|
|
|
EulerToVector(-GetRotation(), GetPitch(), vZ, vX, vY);
|
|
|
|
vY = -vY * 2 + 1.f;
|
2012-06-14 09:06:06 -04:00
|
|
|
m_World->SpawnItemPickups(Drops, GetPosX(), GetPosY() + 1.6f, GetPosZ(), vX * 2, vY * 2, vZ * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-03 03:47:35 -04:00
|
|
|
bool cPlayer::MoveToWorld(const char * a_WorldName)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-03 03:47:35 -04:00
|
|
|
cWorld * World = cRoot::Get()->GetWorld(a_WorldName);
|
|
|
|
if (World == NULL)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-07-05 13:04:12 -04:00
|
|
|
LOG("%s: Couldn't find world \"%s\".", __FUNCTION__, a_WorldName);
|
2013-07-03 03:47:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
eDimension OldDimension = m_World->GetDimension();
|
|
|
|
|
|
|
|
// Remove all links to the old world
|
|
|
|
m_World->RemovePlayer(this);
|
|
|
|
m_ClientHandle->RemoveFromAllChunks();
|
|
|
|
m_World->RemoveEntity(this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-03 03:47:35 -04:00
|
|
|
// Add player to all the necessary parts of the new world
|
|
|
|
SetWorld(World);
|
|
|
|
World->AddEntity(this);
|
|
|
|
World->AddPlayer(this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-03 03:47:35 -04:00
|
|
|
// If the dimension is different, we can send the respawn packet
|
|
|
|
// http://wiki.vg/Protocol#0x09 says "don't send if dimension is the same" as of 2013_07_02
|
|
|
|
if (OldDimension != World->GetDimension())
|
|
|
|
{
|
|
|
|
m_ClientHandle->SendRespawn();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-07-03 03:47:35 -04:00
|
|
|
|
|
|
|
// Stream the new chunks:
|
|
|
|
m_ClientHandle->StreamChunks();
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::LoadPermissionsFromDisk()
|
|
|
|
{
|
|
|
|
m_Groups.clear();
|
|
|
|
m_Permissions.clear();
|
|
|
|
|
|
|
|
cIniFile IniFile("users.ini");
|
|
|
|
if( IniFile.ReadFile() )
|
|
|
|
{
|
|
|
|
std::string Groups = IniFile.GetValue(m_PlayerName, "Groups", "");
|
|
|
|
if( Groups.size() > 0 )
|
|
|
|
{
|
|
|
|
AStringVector Split = StringSplit( Groups, "," );
|
|
|
|
for( unsigned int i = 0; i < Split.size(); i++ )
|
|
|
|
{
|
|
|
|
AddToGroup( Split[i].c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddToGroup("Default");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Color = IniFile.GetValue(m_PlayerName, "Color", "-")[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGWARN("WARNING: Failed to read ini file users.ini");
|
|
|
|
AddToGroup("Default");
|
|
|
|
}
|
|
|
|
ResolvePermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPlayer::LoadFromDisk()
|
|
|
|
{
|
|
|
|
LoadPermissionsFromDisk();
|
|
|
|
|
|
|
|
// Log player permissions, cause it's what the cool kids do
|
|
|
|
LOGINFO("Player %s has permissions:", m_PlayerName.c_str() );
|
|
|
|
for( PermissionMap::iterator itr = m_ResolvedPermissions.begin(); itr != m_ResolvedPermissions.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( itr->second ) LOGINFO("%s", itr->first.c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
AString SourceFile;
|
|
|
|
Printf(SourceFile, "players/%s.json", m_PlayerName.c_str() );
|
|
|
|
|
|
|
|
cFile f;
|
|
|
|
if (!f.Open(SourceFile, cFile::fmRead))
|
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
LOGWARNING("Cannot open player data file \"%s\" for reading", SourceFile.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AString buffer;
|
|
|
|
if (f.ReadRestOfFile(buffer) != f.GetSize())
|
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
LOGWARNING("Cannot read player data from file \"%s\"", SourceFile.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
f.Close();
|
|
|
|
|
|
|
|
Json::Value root;
|
|
|
|
Json::Reader reader;
|
|
|
|
if (!reader.parse(buffer, root, false))
|
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
LOGWARNING("Cannot parse player data in file \"%s\", player will be reset", SourceFile.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value & JSON_PlayerPosition = root["position"];
|
2012-09-20 09:25:54 -04:00
|
|
|
if (JSON_PlayerPosition.size() == 3)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
SetPosX(JSON_PlayerPosition[(unsigned int)0].asDouble());
|
|
|
|
SetPosY(JSON_PlayerPosition[(unsigned int)1].asDouble());
|
|
|
|
SetPosZ(JSON_PlayerPosition[(unsigned int)2].asDouble());
|
2013-07-12 16:01:25 -04:00
|
|
|
m_LastPosX = GetPosX();
|
|
|
|
m_LastPosY = GetPosY();
|
|
|
|
m_LastPosZ = GetPosZ();
|
|
|
|
m_LastFoodPos = GetPosition();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value & JSON_PlayerRotation = root["rotation"];
|
2012-09-20 09:25:54 -04:00
|
|
|
if (JSON_PlayerRotation.size() == 3)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-22 02:33:10 -04:00
|
|
|
SetRotation ((float)JSON_PlayerRotation[(unsigned int)0].asDouble());
|
|
|
|
SetPitch ((float)JSON_PlayerRotation[(unsigned int)1].asDouble());
|
|
|
|
SetRoll ((float)JSON_PlayerRotation[(unsigned int)2].asDouble());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
m_Health = root.get("health", 0).asInt();
|
|
|
|
|
|
|
|
m_FoodLevel = root.get("food", MAX_FOOD_LEVEL).asInt();
|
|
|
|
m_FoodSaturationLevel = root.get("foodSaturation", MAX_FOOD_LEVEL).asDouble();
|
|
|
|
m_FoodTickTimer = root.get("foodTickTimer", 0).asInt();
|
|
|
|
m_FoodExhaustionLevel = root.get("foodExhaustion", 0).asDouble();
|
2012-07-13 11:26:27 -04:00
|
|
|
|
|
|
|
m_GameMode = (eGameMode) root.get("gamemode", eGameMode_NotSet).asInt();
|
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
m_Inventory.LoadFromJson(root["inventory"]);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
m_LoadedWorldName = root.get("world", "world").asString();
|
|
|
|
|
|
|
|
LOGD("Player \"%s\" was read from file, spawning at {%.2f, %.2f, %.2f} in world \"%s\"",
|
2013-03-22 02:33:10 -04:00
|
|
|
m_PlayerName.c_str(), GetPosX(), GetPosY(), GetPosZ(), m_LoadedWorldName.c_str()
|
2012-06-14 09:06:06 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPlayer::SaveToDisk()
|
|
|
|
{
|
|
|
|
cMakeDir::MakeDir("players");
|
|
|
|
|
|
|
|
// create the JSON data
|
|
|
|
Json::Value JSON_PlayerPosition;
|
2013-07-12 16:01:25 -04:00
|
|
|
JSON_PlayerPosition.append(Json::Value(GetPosX()));
|
|
|
|
JSON_PlayerPosition.append(Json::Value(GetPosY()));
|
|
|
|
JSON_PlayerPosition.append(Json::Value(GetPosZ()));
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
Json::Value JSON_PlayerRotation;
|
2013-07-12 16:01:25 -04:00
|
|
|
JSON_PlayerRotation.append(Json::Value(GetRotation()));
|
|
|
|
JSON_PlayerRotation.append(Json::Value(GetPitch()));
|
|
|
|
JSON_PlayerRotation.append(Json::Value(GetRoll()));
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
Json::Value JSON_Inventory;
|
2013-07-12 16:01:25 -04:00
|
|
|
m_Inventory.SaveToJson(JSON_Inventory);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
Json::Value root;
|
2013-07-12 16:01:25 -04:00
|
|
|
root["position"] = JSON_PlayerPosition;
|
|
|
|
root["rotation"] = JSON_PlayerRotation;
|
|
|
|
root["inventory"] = JSON_Inventory;
|
|
|
|
root["health"] = m_Health;
|
|
|
|
root["food"] = m_FoodLevel;
|
2012-07-17 08:02:03 -04:00
|
|
|
root["foodSaturation"] = m_FoodSaturationLevel;
|
2013-07-12 16:01:25 -04:00
|
|
|
root["foodTickTimer"] = m_FoodTickTimer;
|
|
|
|
root["foodExhaustion"] = m_FoodExhaustionLevel;
|
2012-06-14 09:06:06 -04:00
|
|
|
root["world"] = GetWorld()->GetName();
|
2012-07-13 11:26:27 -04:00
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
if (m_GameMode == GetWorld()->GetGameMode())
|
2012-07-13 11:26:27 -04:00
|
|
|
{
|
|
|
|
root["gamemode"] = (int) eGameMode_NotSet;
|
2012-09-20 09:25:54 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-13 11:26:27 -04:00
|
|
|
root["gamemode"] = (int) m_GameMode;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
Json::StyledWriter writer;
|
2013-07-12 16:01:25 -04:00
|
|
|
std::string JsonData = writer.write(root);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
AString SourceFile;
|
|
|
|
Printf(SourceFile, "players/%s.json", m_PlayerName.c_str() );
|
|
|
|
|
|
|
|
cFile f;
|
|
|
|
if (!f.Open(SourceFile, cFile::fmWrite))
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR WRITING PLAYER \"%s\" TO FILE \"%s\" - cannot open file", m_PlayerName.c_str(), SourceFile.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (f.Write(JsonData.c_str(), JsonData.size()) != (int)JsonData.size())
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR WRITING PLAYER JSON TO FILE \"%s\"", SourceFile.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cPlayer::StringList cPlayer::GetResolvedPermissions()
|
|
|
|
{
|
|
|
|
StringList Permissions;
|
|
|
|
|
|
|
|
const PermissionMap& ResolvedPermissions = m_ResolvedPermissions;
|
|
|
|
for( PermissionMap::const_iterator itr = ResolvedPermissions.begin(); itr != ResolvedPermissions.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( itr->second ) Permissions.push_back( itr->first );
|
|
|
|
}
|
|
|
|
|
|
|
|
return Permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::UseEquippedItem()
|
|
|
|
{
|
2013-05-19 14:22:37 -04:00
|
|
|
if (GetGameMode() == gmCreative) // No damage in creative
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-19 14:22:37 -04:00
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-05-19 14:22:37 -04:00
|
|
|
|
|
|
|
GetInventory().DamageEquippedItem();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
|
|
|
|
void cPlayer::HandleFood(void)
|
|
|
|
{
|
|
|
|
// Ref.: http://www.minecraftwiki.net/wiki/Hunger
|
|
|
|
|
|
|
|
// Remember the food level before processing, for later comparison
|
|
|
|
int LastFoodLevel = m_FoodLevel;
|
|
|
|
|
|
|
|
// Heal or damage, based on the food level, using the m_FoodTickTimer:
|
|
|
|
if ((m_FoodLevel > 17) || (m_FoodLevel <= 0))
|
|
|
|
{
|
|
|
|
m_FoodTickTimer++;
|
|
|
|
if (m_FoodTickTimer >= 80)
|
|
|
|
{
|
|
|
|
m_FoodTickTimer = 0;
|
|
|
|
|
|
|
|
if (m_FoodLevel >= 17)
|
|
|
|
{
|
|
|
|
// Regenerate health from food, incur 3 pts of food exhaustion:
|
|
|
|
Heal(1);
|
|
|
|
m_FoodExhaustionLevel += 3;
|
|
|
|
}
|
|
|
|
else if (m_FoodLevel <= 0)
|
|
|
|
{
|
|
|
|
// Damage from starving
|
|
|
|
TakeDamage(dtStarving, NULL, 1, 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply food poisoning food exhaustion:
|
|
|
|
if (m_FoodPoisonedTicksRemaining > 0)
|
|
|
|
{
|
|
|
|
m_FoodPoisonedTicksRemaining--;
|
|
|
|
m_FoodExhaustionLevel += 0.025; // 0.5 per second = 0.025 per tick
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply food exhaustion that has accumulated:
|
|
|
|
if (m_FoodExhaustionLevel >= 4)
|
|
|
|
{
|
|
|
|
m_FoodExhaustionLevel -= 4;
|
|
|
|
|
|
|
|
if (m_FoodSaturationLevel >= 1)
|
|
|
|
{
|
|
|
|
m_FoodSaturationLevel -= 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_FoodLevel = std::max(m_FoodLevel - 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_FoodLevel != LastFoodLevel)
|
|
|
|
{
|
|
|
|
SendHealth();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlayer::ApplyFoodExhaustionFromMovement(cChunk & a_Chunk)
|
|
|
|
{
|
|
|
|
// Calculate the distance travelled, update the last pos:
|
|
|
|
Vector3d Movement(GetPosition() - m_LastFoodPos);
|
|
|
|
m_LastFoodPos = GetPosition();
|
|
|
|
|
|
|
|
// If riding anything, apply no food exhaustion
|
|
|
|
if (m_AttachedTo != NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the type of block the player's standing in:
|
|
|
|
BLOCKTYPE BlockIn;
|
|
|
|
int RelX = (int)floor(m_LastPosX) - a_Chunk.GetPosX() * cChunkDef::Width;
|
|
|
|
int RelY = (int)floor(m_LastPosY + 0.1);
|
|
|
|
int RelZ = (int)floor(m_LastPosZ) - a_Chunk.GetPosZ() * cChunkDef::Width;
|
|
|
|
// Use Unbounded, because we're being called *after* processing super::Tick(), which could have changed our chunk
|
|
|
|
VERIFY(a_Chunk.UnboundedRelGetBlockType(RelX, RelY, RelZ, BlockIn));
|
|
|
|
|
|
|
|
// Apply the exhaustion based on distance travelled:
|
|
|
|
double BaseExhaustion = Movement.Length();
|
|
|
|
LOGD("Movement: %.03f m", BaseExhaustion);
|
|
|
|
if (IsSprinting())
|
|
|
|
{
|
|
|
|
// 0.1 pt per meter sprinted
|
|
|
|
BaseExhaustion = BaseExhaustion * 0.1;
|
|
|
|
}
|
|
|
|
else if (IsBlockWater(BlockIn))
|
|
|
|
{
|
|
|
|
// 0.015 pt per meter swum
|
|
|
|
BaseExhaustion = BaseExhaustion * 0.015;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 0.01 pt per meter walked / sneaked
|
|
|
|
BaseExhaustion = BaseExhaustion * 0.01;
|
|
|
|
}
|
|
|
|
m_FoodExhaustionLevel += BaseExhaustion;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|