From d3801c0296dd32f430a4074932bfabf27a8ade03 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 28 Jul 2013 11:30:54 +0200 Subject: Added cPlayer::IsGameModeXXX() and cWorld::IsGameModeXXX() functions. These are the preferred way of determining the gamemode, you should use those instead of doing manual comparisons to the gamemode value. --- source/Player.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'source/Player.cpp') diff --git a/source/Player.cpp b/source/Player.cpp index 83181dda6..5609807b0 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -505,7 +505,6 @@ void cPlayer::KilledBy(cEntity * a_Killer) void cPlayer::Respawn(void) { m_Health = GetMaxHealth(); - m_FoodLevel = 20; m_ClientHandle->SendRespawn(); @@ -538,6 +537,36 @@ Vector3d cPlayer::GetEyePosition(void) const +bool cPlayer::IsGameModeCreative(void) const +{ + return (m_GameMode == gmCreative) || // Either the player is explicitly in Creative + ((m_GameMode == gmNotSet) && m_World->IsGameModeCreative()); // or they inherit from the world and the world is Creative +} + + + + + +bool cPlayer::IsGameModeSurvival(void) const +{ + return (m_GameMode == gmSurvival) || // Either the player is explicitly in Survival + ((m_GameMode == gmNotSet) && m_World->IsGameModeSurvival()); // or they inherit from the world and the world is Survival +} + + + + + +bool cPlayer::IsGameModeAdventure(void) const +{ + return (m_GameMode == gmCreative) || // Either the player is explicitly in Adventure + ((m_GameMode == gmNotSet) && m_World->IsGameModeCreative()); // or they inherit from the world and the world is Adventure +} + + + + + void cPlayer::OpenWindow(cWindow * a_Window) { if (a_Window != m_CurrentWindow) @@ -1283,6 +1312,11 @@ void cPlayer::HandleFood(void) void cPlayer::ApplyFoodExhaustionFromMovement(cChunk & a_Chunk) { + if (IsGameModeCreative()) + { + return; + } + // Calculate the distance travelled, update the last pos: Vector3d Movement(GetPosition() - m_LastFoodPos); m_LastFoodPos = GetPosition(); -- cgit v1.2.3 From d155c1cb0043c86c0e1e6b1f41d7b194ce368f24 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 28 Jul 2013 11:52:24 +0200 Subject: Player food level is reset on respawn --- source/Player.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'source/Player.cpp') diff --git a/source/Player.cpp b/source/Player.cpp index 5609807b0..d5cfe8bcf 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -505,6 +505,10 @@ void cPlayer::KilledBy(cEntity * a_Killer) void cPlayer::Respawn(void) { m_Health = GetMaxHealth(); + + // Reset food level: + m_FoodLevel = 20; + m_FoodSaturationLevel = 5; m_ClientHandle->SendRespawn(); -- cgit v1.2.3 From 0caadbb25ce9c4c3f9d51a0898af182889802784 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 28 Jul 2013 11:54:37 +0200 Subject: Hunger-per-distance is calculated only for the XZ distance --- source/Player.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'source/Player.cpp') diff --git a/source/Player.cpp b/source/Player.cpp index d5cfe8bcf..2aa041ff4 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -1323,6 +1323,7 @@ void cPlayer::ApplyFoodExhaustionFromMovement(cChunk & a_Chunk) // Calculate the distance travelled, update the last pos: Vector3d Movement(GetPosition() - m_LastFoodPos); + Movement.y = 0; // Only take XZ movement into account m_LastFoodPos = GetPosition(); // If riding anything, apply no food exhaustion -- cgit v1.2.3 From 4746d2251c6204118e710c818d78b89c356a7427 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 28 Jul 2013 19:15:03 +0200 Subject: Implemented basic eating support. Food is now properly consumed and it takes 1.5 sec. --- source/Player.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'source/Player.cpp') diff --git a/source/Player.cpp b/source/Player.cpp index 2aa041ff4..c177b8cf4 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -20,6 +20,7 @@ #include "OSSupport/Timer.h" #include "MersenneTwister.h" #include "Chunk.h" +#include "Items/ItemHandler.h" #include "Vector3d.h" #include "Vector3f.h" @@ -58,6 +59,7 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) , m_SprintingMaxSpeed(0.13) , m_IsCrouched(false) , m_IsSprinting(false) + , m_EatingFinishTick(-1) { LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d", a_PlayerName.c_str(), a_Client->GetIPString().c_str(), @@ -189,6 +191,11 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) { m_World->CollectPickupsByPlayer(this); + if ((m_EatingFinishTick >= 0) && (m_EatingFinishTick <= m_World->GetWorldAge())) + { + FinishEating(); + } + HandleFood(); } @@ -349,6 +356,57 @@ void cPlayer::FoodPoison(int a_NumTicks) +void cPlayer::StartEating(void) +{ + // Set the timer: + m_EatingFinishTick = m_World->GetWorldAge() + EATING_TICKS; + + // Send the packets: + m_World->BroadcastPlayerAnimation(*this, 5); + m_World->BroadcastEntityMetadata(*this); +} + + + + + +void cPlayer::FinishEating(void) +{ + // Reset the timer: + m_EatingFinishTick = -1; + + // Send the packets: + m_ClientHandle->SendEntityStatus(*this, ENTITY_STATUS_EATING_ACCEPTED); + m_World->BroadcastPlayerAnimation(*this, 0); + m_World->BroadcastEntityMetadata(*this); + + // consume the item: + cItem Item(GetEquippedItem()); + Item.m_ItemCount = 1; + cItemHandler * ItemHandler = cItemHandler::GetItemHandler(Item.m_ItemType); + if (!ItemHandler->EatItem(this, &Item)) + { + return; + } + ItemHandler->OnFoodEaten(m_World, this, &Item); + GetInventory().RemoveOneEquippedItem(); +} + + + + + +void cPlayer::AbortEating(void) +{ + m_EatingFinishTick = -1; + m_World->BroadcastPlayerAnimation(*this, 0); + m_World->BroadcastEntityMetadata(*this); +} + + + + + void cPlayer::SendHealth(void) { if (m_ClientHandle != NULL) -- cgit v1.2.3 From 3bf4130e3fc05f84cf7be7e33656f3e29f0fe12c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 28 Jul 2013 22:55:09 +0200 Subject: Player eating is now properly broadcast to other players. Also fixed the API relating to food, determining player gamemode, and removed several unneeded API functions. --- source/Player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/Player.cpp') diff --git a/source/Player.cpp b/source/Player.cpp index c177b8cf4..c90f3c99c 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -705,7 +705,7 @@ void cPlayer::SetLastBlockActionCnt( int a_LastBlockActionCnt ) void cPlayer::SetGameMode(eGameMode a_GameMode) { - if ((a_GameMode >= 3) || (a_GameMode < 0)) + if ((a_GameMode >= gmMin) || (a_GameMode < gmMax)) { LOGWARNING("%s: Setting invalid gamemode: %d", GetName().c_str(), a_GameMode); return; -- cgit v1.2.3