summaryrefslogtreecommitdiffstats
path: root/src/Entities
diff options
context:
space:
mode:
Diffstat (limited to 'src/Entities')
-rw-r--r--src/Entities/Entity.cpp160
-rw-r--r--src/Entities/Entity.h22
-rw-r--r--src/Entities/Player.cpp92
-rw-r--r--src/Entities/Player.h36
4 files changed, 277 insertions, 33 deletions
diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp
index 28817428f..e5e4cf4cb 100644
--- a/src/Entities/Entity.cpp
+++ b/src/Entities/Entity.cpp
@@ -37,6 +37,7 @@ cEntity::cEntity(eEntityType a_EntityType, double a_X, double a_Y, double a_Z, d
, m_Gravity(-9.81f)
, m_LastPos(a_X, a_Y, a_Z)
, m_IsInitialized(false)
+ , m_IsTravellingThroughPortal(false)
, m_EntityType(a_EntityType)
, m_World(NULL)
, m_IsFireproof(false)
@@ -614,6 +615,7 @@ void cEntity::Tick(float a_Dt, cChunk & a_Chunk)
// Handle drowning
HandleAir();
}
+ DetectPortal();
// None of the above functions change position, we remain in the chunk of NextChunk
HandlePhysics(a_Dt, *NextChunk);
@@ -853,7 +855,7 @@ void cEntity::TickBurning(cChunk & a_Chunk)
// Remember the current burning state:
bool HasBeenBurning = (m_TicksLeftBurning > 0);
- if (m_World->IsWeatherWet())
+ if (GetWorld()->IsWeatherWetAt(POSX_TOINT, POSZ_TOINT))
{
if (POSY_TOINT > m_World->GetHeight(POSX_TOINT, POSZ_TOINT))
{
@@ -1024,6 +1026,162 @@ void cEntity::DetectCacti(void)
+void cEntity::DetectPortal()
+{
+ if (!GetWorld()->AreNetherPortalsEnabled() && !GetWorld()->AreEndPortalsEnabled())
+ {
+ return;
+ }
+
+ int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT;
+ if ((Y > 0) && (Y < cChunkDef::Height))
+ {
+ switch (GetWorld()->GetBlock(X, Y, Z))
+ {
+ case E_BLOCK_NETHER_PORTAL:
+ {
+ if (!GetWorld()->AreNetherPortalsEnabled() || m_PortalCooldownData.second)
+ {
+ return;
+ }
+
+ if (m_PortalCooldownData.first != 80)
+ {
+ m_PortalCooldownData.first++;
+ return;
+ }
+ m_PortalCooldownData.first = 0;
+
+ switch (GetWorld()->GetDimension())
+ {
+ case dimNether:
+ {
+ m_PortalCooldownData.second = true; // Stop portals from working on respawn
+
+ if (IsPlayer())
+ {
+ ((cPlayer *)this)->GetClientHandle()->SendRespawn(dimOverworld);
+ }
+ MoveToWorld(GetWorld()->GetLinkedOverworldName(), cRoot::Get()->CreateAndInitializeWorld(GetWorld()->GetLinkedOverworldName()), false);
+
+ return;
+ }
+ case dimOverworld:
+ {
+ m_PortalCooldownData.second = true; // Stop portals from working on respawn
+
+ if (IsPlayer())
+ {
+ ((cPlayer *)this)->AwardAchievement(achEnterPortal);
+ ((cPlayer *)this)->GetClientHandle()->SendRespawn(dimNether);
+ }
+ MoveToWorld(GetWorld()->GetNetherWorldName(), cRoot::Get()->CreateAndInitializeWorld(GetWorld()->GetNetherWorldName(), dimNether, GetWorld()->GetName()), false);
+
+ return;
+ }
+ default: break;
+ }
+ return;
+ }
+ case E_BLOCK_END_PORTAL:
+ {
+ if (!GetWorld()->AreEndPortalsEnabled() || m_PortalCooldownData.second)
+ {
+ return;
+ }
+
+ if (m_PortalCooldownData.first != 80)
+ {
+ m_PortalCooldownData.first++;
+ return;
+ }
+ m_PortalCooldownData.first = 0;
+
+ switch (GetWorld()->GetDimension())
+ {
+ case dimEnd:
+ {
+ m_PortalCooldownData.second = true; // Stop portals from working on respawn
+
+ if (IsPlayer())
+ {
+ cPlayer * Player = (cPlayer *)this;
+ Player->TeleportToCoords(Player->GetLastBedPos().x, Player->GetLastBedPos().y, Player->GetLastBedPos().z);
+ Player->GetClientHandle()->SendRespawn(dimOverworld);
+ }
+ MoveToWorld(GetWorld()->GetLinkedOverworldName(), cRoot::Get()->CreateAndInitializeWorld(GetWorld()->GetLinkedOverworldName()), false);
+
+ return;
+ }
+ case dimOverworld:
+ {
+ m_PortalCooldownData.second = true; // Stop portals from working on respawn
+
+ if (IsPlayer())
+ {
+ ((cPlayer *)this)->AwardAchievement(achEnterTheEnd);
+ ((cPlayer *)this)->GetClientHandle()->SendRespawn(dimEnd);
+ }
+ MoveToWorld(GetWorld()->GetEndWorldName(), cRoot::Get()->CreateAndInitializeWorld(GetWorld()->GetEndWorldName(), dimEnd, GetWorld()->GetName()), false);
+
+ return;
+ }
+ default: break;
+ }
+ return;
+ }
+ default: break;
+ }
+ }
+
+ // Allow portals to work again
+ m_PortalCooldownData.second = false;
+ m_PortalCooldownData.first = 0;
+}
+
+
+
+
+
+bool cEntity::MoveToWorld(const AString & a_WorldName, cWorld * a_World, bool a_ShouldSendRespawn)
+{
+ UNUSED(a_ShouldSendRespawn);
+
+ cWorld * World;
+ if (a_World == NULL)
+ {
+ World = cRoot::Get()->GetWorld(a_WorldName);
+ if (World == NULL)
+ {
+ LOG("%s: Couldn't find world \"%s\".", __FUNCTION__, a_WorldName.c_str());
+ return false;
+ }
+ }
+ else
+ {
+ World = a_World;
+ }
+
+ if (GetWorld() == World)
+ {
+ // Don't move to same world
+ return false;
+ }
+
+ // Remove all links to the old world
+ SetIsTravellingThroughPortal(true); // cChunk handles entity removal
+ GetWorld()->BroadcastDestroyEntity(*this);
+
+ // Queue add to new world
+ World->AddEntity(this);
+
+ return true;
+}
+
+
+
+
+
void cEntity::SetSwimState(cChunk & a_Chunk)
{
int RelY = (int)floor(GetPosY() + 0.1);
diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h
index 88f8528e9..8b6fc05f7 100644
--- a/src/Entities/Entity.h
+++ b/src/Entities/Entity.h
@@ -336,6 +336,9 @@ public:
/** Detects the time for application of cacti damage */
virtual void DetectCacti(void);
+
+ /** Detects whether we are in a portal block and begins teleportation procedures if so */
+ virtual void DetectPortal(void);
/// Handles when the entity is in the void
virtual void TickInVoid(cChunk & a_Chunk);
@@ -378,8 +381,17 @@ public:
/// Teleports to the coordinates specified
virtual void TeleportToCoords(double a_PosX, double a_PosY, double a_PosZ);
+
+ /** Moves entity to specified world */
+ virtual bool MoveToWorld(const AString & a_WorldName, cWorld * a_World = NULL, bool a_ShouldSendRespawn = true);
// tolua_end
+
+ /** Returns if the entity is travelling through a portal. Set to true by MoveToWorld and to false when the entity is removed by the old chunk */
+ bool IsTravellingThroughPortal(void) const { return m_IsTravellingThroughPortal; }
+
+ /** Sets if the entity has begun travelling through a portal or not */
+ void SetIsTravellingThroughPortal(bool a_Flag) { m_IsTravellingThroughPortal = a_Flag; }
/// Updates clients of changes in the entity.
virtual void BroadcastMovementUpdate(const cClientHandle * a_Exclude = NULL);
@@ -479,6 +491,9 @@ protected:
/** True when entity is initialised (Initialize()) and false when destroyed pending deletion (Destroy()) */
bool m_IsInitialized;
+ /** True when entity is being moved across worlds, false anytime else */
+ bool m_IsTravellingThroughPortal;
+
eEntityType m_EntityType;
cWorld * m_World;
@@ -500,7 +515,6 @@ protected:
/// Time, in ticks, since the last damage dealt by the void. Reset to zero when moving out of the void.
int m_TicksSinceLastVoidDamage;
-
/** Does the actual speed-setting. The default implementation just sets the member variable value;
overrides can provide further processing, such as forcing players to move at the given speed. */
@@ -520,6 +534,12 @@ protected:
/** Air level of a mobile */
int m_AirLevel;
int m_AirTickTimer;
+
+ /** Portal delay timer and cooldown boolean
+ First value is to delay sending the respawn packet (which triggers the Entering the {Dimension} screen).
+ Second value is to prevent a teleportation loop by ensuring we do not reenter a portal that we came out of.
+ */
+ std::pair<unsigned short, bool> m_PortalCooldownData;
private:
/** Measured in degrees, [-180, +180) */
diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp
index ea11926b8..1acc8a962 100644
--- a/src/Entities/Player.cpp
+++ b/src/Entities/Player.cpp
@@ -88,13 +88,14 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) :
m_PlayerName = a_PlayerName;
- if (!LoadFromDisk())
+ cWorld * World;
+ if (!LoadFromDisk(World))
{
m_Inventory.Clear();
- cWorld * DefaultWorld = cRoot::Get()->GetDefaultWorld();
- SetPosX(DefaultWorld->GetSpawnX());
- SetPosY(DefaultWorld->GetSpawnY());
- SetPosZ(DefaultWorld->GetSpawnZ());
+ SetPosX(World->GetSpawnX());
+ SetPosY(World->GetSpawnY());
+ SetPosZ(World->GetSpawnZ());
+ SetBedPos(Vector3i(World->GetSpawnX(), World->GetSpawnY(), World->GetSpawnZ()));
LOGD("Player \"%s\" is connecting for the first time, spawning at default world spawn {%.2f, %.2f, %.2f}",
a_PlayerName.c_str(), GetPosX(), GetPosY(), GetPosZ()
@@ -107,11 +108,6 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) :
if (m_GameMode == gmNotSet)
{
- cWorld * World = cRoot::Get()->GetWorld(GetLoadedWorldName());
- if (World == NULL)
- {
- World = cRoot::Get()->GetDefaultWorld();
- }
if (World->IsGameModeCreative())
{
m_CanFly = true;
@@ -140,7 +136,7 @@ cPlayer::~cPlayer(void)
SaveToDisk();
- m_World->RemovePlayer( this );
+ m_World->RemovePlayer(this);
m_ClientHandle = NULL;
@@ -157,8 +153,6 @@ cPlayer::~cPlayer(void)
void cPlayer::Destroyed()
{
CloseWindow(false);
-
- m_ClientHandle = NULL;
}
@@ -985,12 +979,12 @@ void cPlayer::Respawn(void)
m_LifetimeTotalXp = 0;
// ToDo: send score to client? How?
- m_ClientHandle->SendRespawn(*m_World, true);
+ m_ClientHandle->SendRespawn(GetWorld()->GetDimension());
// Extinguish the fire:
StopBurning();
- TeleportToCoords(GetWorld()->GetSpawnX(), GetWorld()->GetSpawnY(), GetWorld()->GetSpawnZ());
+ TeleportToCoords(GetLastBedPos().x, GetLastBedPos().y, GetLastBedPos().z);
SetVisible(true);
}
@@ -1619,27 +1613,39 @@ void cPlayer::TossItems(const cItems & a_Items)
-bool cPlayer::MoveToWorld(const char * a_WorldName)
+bool cPlayer::MoveToWorld(const AString & a_WorldName, cWorld * a_World, bool a_ShouldSendRespawn)
{
- cWorld * World = cRoot::Get()->GetWorld(a_WorldName);
- if (World == NULL)
+ cWorld * World;
+ if (a_World == NULL)
{
- LOG("%s: Couldn't find world \"%s\".", __FUNCTION__, a_WorldName);
+ World = cRoot::Get()->GetWorld(a_WorldName);
+ if (World == NULL)
+ {
+ LOG("%s: Couldn't find world \"%s\".", __FUNCTION__, a_WorldName.c_str());
+ return false;
+ }
+ }
+ else
+ {
+ World = a_World;
+ }
+
+ if (GetWorld() == World)
+ {
+ // Don't move to same world
return false;
}
// Send the respawn packet:
- if (m_ClientHandle != NULL)
+ if (a_ShouldSendRespawn && (m_ClientHandle != NULL))
{
- m_ClientHandle->SendRespawn(*World);
+ m_ClientHandle->SendRespawn(World->GetDimension());
}
- // Remove all links to the old world
+ // Remove player from the old world
+ SetIsTravellingThroughPortal(true); // cChunk handles entity removal
m_World->RemovePlayer(this);
- // 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
-
// Queue adding player to the new world, including all the necessary adjustments to the object
World->AddPlayer(this);
@@ -1689,9 +1695,19 @@ void cPlayer::LoadPermissionsFromDisk()
+<<<<<<< HEAD
+bool cPlayer::LoadFromDisk(cWorldPtr & a_World)
+=======
bool cPlayer::LoadFromDisk(void)
+>>>>>>> master
{
+ a_World = cRoot::Get()->GetWorld(GetLoadedWorldName());
+ if (a_World == NULL)
+ {
+ a_World = cRoot::Get()->GetDefaultWorld();
+ }
+
LoadPermissionsFromDisk();
// Load from the UUID file:
@@ -1789,6 +1805,9 @@ bool cPlayer::LoadFromFile(const AString & a_FileName)
m_LifetimeTotalXp = (short) root.get("xpTotal", 0).asInt();
m_CurrentXp = (short) root.get("xpCurrent", 0).asInt();
m_IsFlying = root.get("isflying", 0).asBool();
+ m_LastBedPos.x = root.get("SpawnX", a_World->GetSpawnX()).asInt();
+ m_LastBedPos.y = root.get("SpawnY", a_World->GetSpawnY()).asInt();
+ m_LastBedPos.z = root.get("SpawnZ", a_World->GetSpawnZ()).asInt();
m_GameMode = (eGameMode) root.get("gamemode", eGameMode_NotSet).asInt();
@@ -1841,6 +1860,28 @@ bool cPlayer::SaveToDisk()
cEnderChestEntity::SaveToJson(JSON_EnderChestInventory, m_EnderChestContents);
Json::Value root;
+<<<<<<< HEAD
+ root["position"] = JSON_PlayerPosition;
+ root["rotation"] = JSON_PlayerRotation;
+ root["inventory"] = JSON_Inventory;
+ root["health"] = m_Health;
+ root["xpTotal"] = m_LifetimeTotalXp;
+ root["xpCurrent"] = m_CurrentXp;
+ root["air"] = m_AirLevel;
+ root["food"] = m_FoodLevel;
+ root["foodSaturation"] = m_FoodSaturationLevel;
+ root["foodTickTimer"] = m_FoodTickTimer;
+ root["foodExhaustion"] = m_FoodExhaustionLevel;
+ root["world"] = GetWorld()->GetName();
+ root["isflying"] = IsFlying();
+ root["SpawnX"] = GetLastBedPos().x;
+ root["SpawnY"] = GetLastBedPos().y;
+ root["SpawnZ"] = GetLastBedPos().z;
+
+ if (m_GameMode == GetWorld()->GetGameMode())
+ {
+ root["gamemode"] = (int) eGameMode_NotSet;
+=======
root["position"] = JSON_PlayerPosition;
root["rotation"] = JSON_PlayerRotation;
root["inventory"] = JSON_Inventory;
@@ -1866,6 +1907,7 @@ bool cPlayer::SaveToDisk()
{
root["gamemode"] = (int) m_GameMode;
}
+>>>>>>> master
}
else
{
diff --git a/src/Entities/Player.h b/src/Entities/Player.h
index 7c287d41b..f84cc5f55 100644
--- a/src/Entities/Player.h
+++ b/src/Entities/Player.h
@@ -131,7 +131,7 @@ public:
inline const cItem & GetEquippedItem(void) const { return GetInventory().GetEquippedItem(); } // tolua_export
- /** Returns whether the player is climbing (ladders, vines e.t.c). */
+ /** Returns whether the player is climbing (ladders, vines etc.) */
bool IsClimbing(void) const;
virtual void TeleportToCoords(double a_PosX, double a_PosY, double a_PosZ) override;
@@ -325,7 +325,12 @@ public:
virtual void KilledBy(TakeDamageInfo & a_TDI) override;
virtual void Killed(cEntity * a_Victim) override;
-
+
+ void Respawn(void); // tolua_export
+
+ void SetVisible(bool a_bVisible); // tolua_export
+ bool IsVisible(void) const { return m_bVisible; } // tolua_export
+
void Respawn(void); // tolua_export
void SetVisible( bool a_bVisible ); // tolua_export
@@ -333,13 +338,17 @@ public:
/** Moves the player to the specified world.
Returns true if successful, false on failure (world not found). */
- bool MoveToWorld(const char * a_WorldName); // tolua_export
+ virtual bool MoveToWorld(const AString & a_WorldName, cWorld * a_World = NULL, bool a_ShouldSendRespawn = true) override; // tolua_export
+ /** Saves all player data, such as inventory, to JSON */
bool SaveToDisk(void);
+
+ typedef cWorld * cWorldPtr;
/** Loads the player data from the disk file.
+Takes a (NULL) cWorld pointer which it will assign a value to based on either the loaded world or default world
Returns true on success, false on failure. */
- bool LoadFromDisk(void);
+ bool LoadFromDisk(cWorldPtr & a_World);
/** Loads the player data from the specified file.
Returns true on success, false on failure. */
@@ -355,8 +364,7 @@ public:
void SendExperience(void);
- // In UI windows, the item that the player is dragging:
- bool IsDraggingItem(void) const { return !m_DraggingItem.IsEmpty(); }
+ /** In UI windows, get the item that the player is dragging */
cItem & GetDraggingItem(void) {return m_DraggingItem; }
// In UI windows, when inventory-painting:
@@ -404,11 +412,21 @@ public:
/** If true the player can fly even when he's not in creative. */
void SetCanFly(bool a_CanFly);
+ /** Gets the last position that the player slept in
+ This is initialised to the world spawn point if the player has not slept in a bed as of yet
+ */
+ Vector3i GetLastBedPos(void) const { return m_LastBedPos; }
+
+ /** Sets the player's bed (home) position */
+ void SetBedPos(const Vector3i & a_Pos) { m_LastBedPos = a_Pos; }
+
/** Update movement-related statistics. */
void UpdateMovementStats(const Vector3d & a_DeltaPos);
/** Returns wheter the player can fly or not. */
virtual bool CanFly(void) const { return m_CanFly; }
+
+
// tolua_end
// cEntity overrides:
@@ -466,6 +484,9 @@ protected:
cWindow * m_CurrentWindow;
cWindow * m_InventoryWindow;
+ /** The player's last saved bed position */
+ Vector3i m_LastBedPos;
+
char m_Color;
eGameMode m_GameMode;
@@ -523,6 +544,8 @@ protected:
cStatManager m_Stats;
+<<<<<<< HEAD
+=======
/** Flag representing whether the player is currently in a bed
Set by a right click on unoccupied bed, unset by a time fast forward or teleport */
bool m_bIsInBed;
@@ -541,6 +564,7 @@ protected:
AString m_UUID;
+>>>>>>> master
/** Sets the speed and sends it to the client, so that they are forced to move so. */
virtual void DoSetSpeed(double a_SpeedX, double a_SpeedY, double a_SpeedZ) override;