From e6c33b0256b7dc0569aff36005eb24dab4ca0244 Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Thu, 8 Aug 2013 10:32:34 +0100 Subject: Made player drowning implementation --- source/Player.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- source/Player.h | 13 ++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/source/Player.cpp b/source/Player.cpp index ae119a1fb..0ec6bb5ca 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -40,6 +40,8 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) , m_IP("") , m_LastBlockActionTime( 0 ) , m_LastBlockActionCnt( 0 ) + , m_AirLevel( MAX_AIR_LEVEL ) + , m_AirTickTimer( DROWNING_TICKS ) , m_bVisible( true ) , m_LastGroundHeight( 0 ) , m_bTouchGround( false ) @@ -178,6 +180,10 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) } super::Tick(a_Dt, a_Chunk); + + //handle air drowning stuff + HandleAir(a_Chunk); + if (m_bDirtyPosition) { // Apply food exhaustion from movement: @@ -1200,7 +1206,7 @@ bool cPlayer::LoadFromDisk() } m_Health = root.get("health", 0).asInt(); - + m_AirLevel = root.get("air", MAX_AIR_LEVEL).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(); @@ -1246,6 +1252,7 @@ bool cPlayer::SaveToDisk() root["rotation"] = JSON_PlayerRotation; root["inventory"] = JSON_Inventory; root["health"] = m_Health; + root["air"] = m_AirLevel; root["food"] = m_FoodLevel; root["foodSaturation"] = m_FoodSaturationLevel; root["foodTickTimer"] = m_FoodTickTimer; @@ -1313,7 +1320,43 @@ void cPlayer::UseEquippedItem() } +void cPlayer::HandleAir(cChunk & a_Chunk) +{ + //Ref.: http://www.minecraftwiki.net/wiki/Chunk_format + //see if the player is /submerged/ water (block above is water) + // 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 + 1.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)); + + if (IsBlockWater(BlockIn)) + { + //either reduce air level or damage player + if(m_AirLevel < 1) + { + if(m_AirTickTimer < 1) + { + //damage player + TakeDamage(dtDrowning, NULL, 1, 1, 0); + //reset timer + m_AirTickTimer = DROWNING_TICKS; + }else{ + m_AirTickTimer -= 1; + } + }else{ + //reduce air supply + m_AirLevel -= 1; + } + }else{ + //set the air back to maximum + m_AirLevel = MAX_AIR_LEVEL; + m_AirTickTimer = DROWNING_TICKS; + } +} void cPlayer::HandleFood(void) diff --git a/source/Player.h b/source/Player.h index c8d66b842..c0fe31863 100644 --- a/source/Player.h +++ b/source/Player.h @@ -29,6 +29,8 @@ public: MAX_HEALTH = 20, MAX_FOOD_LEVEL = 20, EATING_TICKS = 30, ///< Number of ticks it takes to eat an item + MAX_AIR_LEVEL = 300, + DROWNING_TICKS = 10, //number of ticks per heart of damage } ; // tolua_end @@ -160,6 +162,8 @@ public: int GetFoodTickTimer (void) const { return m_FoodTickTimer; } double GetFoodExhaustionLevel (void) const { return m_FoodExhaustionLevel; } int GetFoodPoisonedTicksRemaining(void) const { return m_FoodPoisonedTicksRemaining; } + + int GetAirLevel (void) const { return m_AirLevel; } /// Returns true if the player is satiated, i. e. their foodlevel is at the max and they cannot eat anymore bool IsSatiated(void) const { return (m_FoodLevel >= MAX_FOOD_LEVEL); } @@ -267,6 +271,11 @@ protected: std::string m_PlayerName; std::string m_LoadedWorldName; + //Player's air level (for swimming) + int m_AirLevel; + //used to time ticks between damage taken via drowning/suffocation + int m_AirTickTimer; + bool m_bVisible; // Food-related variables: @@ -329,7 +338,6 @@ protected: /// The world tick in which eating will be finished. -1 if not eating Int64 m_EatingFinishTick; - virtual void Destroyed(void); /// Filters out damage for creative mode @@ -338,6 +346,9 @@ protected: /// Called in each tick to handle food-related processing void HandleFood(void); + /// Called in each tick to handle air-related processing i.e. drowning + void HandleAir(cChunk & a_Chunk); + /// Adds food exhaustion based on the difference between Pos and LastPos, sprinting status and swimming (in water block) void ApplyFoodExhaustionFromMovement(cChunk & a_Chunk); } ; // tolua_export -- cgit v1.2.3 From 9a6442a20652249248c8c631550718c314230ec7 Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Thu, 8 Aug 2013 10:57:36 +0100 Subject: Fixed the formatting issues in player cpp files --- source/Player.cpp | 61 +++++++++++++++++++++++++++---------------------------- source/Player.h | 16 +++++++-------- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/source/Player.cpp b/source/Player.cpp index 0ec6bb5ca..21f31c555 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -40,8 +40,8 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) , m_IP("") , m_LastBlockActionTime( 0 ) , m_LastBlockActionCnt( 0 ) - , m_AirLevel( MAX_AIR_LEVEL ) - , m_AirTickTimer( DROWNING_TICKS ) + , m_AirLevel( MAX_AIR_LEVEL ) + , m_AirTickTimer( DROWNING_TICKS ) , m_bVisible( true ) , m_LastGroundHeight( 0 ) , m_bTouchGround( false ) @@ -181,8 +181,8 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) super::Tick(a_Dt, a_Chunk); - //handle air drowning stuff - HandleAir(a_Chunk); + //handle air drowning stuff + HandleAir(a_Chunk); if (m_bDirtyPosition) { @@ -1252,7 +1252,7 @@ bool cPlayer::SaveToDisk() root["rotation"] = JSON_PlayerRotation; root["inventory"] = JSON_Inventory; root["health"] = m_Health; - root["air"] = m_AirLevel; + root["air"] = m_AirLevel; root["food"] = m_FoodLevel; root["foodSaturation"] = m_FoodSaturationLevel; root["foodTickTimer"] = m_FoodTickTimer; @@ -1322,10 +1322,9 @@ void cPlayer::UseEquippedItem() void cPlayer::HandleAir(cChunk & a_Chunk) { - //Ref.: http://www.minecraftwiki.net/wiki/Chunk_format - - //see if the player is /submerged/ water (block above is water) - // Get the type of block the player's standing in: + //Ref.: http://www.minecraftwiki.net/wiki/Chunk_format + //see if the player is /submerged/ water (block above is water) + // 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 + 1.1); @@ -1333,29 +1332,29 @@ void cPlayer::HandleAir(cChunk & a_Chunk) // 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)); - if (IsBlockWater(BlockIn)) - { - //either reduce air level or damage player - if(m_AirLevel < 1) - { - if(m_AirTickTimer < 1) - { - //damage player - TakeDamage(dtDrowning, NULL, 1, 1, 0); - //reset timer - m_AirTickTimer = DROWNING_TICKS; - }else{ - m_AirTickTimer -= 1; - } - }else{ - //reduce air supply - m_AirLevel -= 1; - } + if (IsBlockWater(BlockIn)) + { + //either reduce air level or damage player + if(m_AirLevel < 1) + { + if(m_AirTickTimer < 1) + { + //damage player + TakeDamage(dtDrowning, NULL, 1, 1, 0); + //reset timer + m_AirTickTimer = DROWNING_TICKS; + }else{ + m_AirTickTimer -= 1; + } + }else{ + //reduce air supply + m_AirLevel -= 1; + } }else{ - //set the air back to maximum - m_AirLevel = MAX_AIR_LEVEL; - m_AirTickTimer = DROWNING_TICKS; - } + //set the air back to maximum + m_AirLevel = MAX_AIR_LEVEL; + m_AirTickTimer = DROWNING_TICKS; + } } diff --git a/source/Player.h b/source/Player.h index c0fe31863..f1d746318 100644 --- a/source/Player.h +++ b/source/Player.h @@ -29,8 +29,8 @@ public: MAX_HEALTH = 20, MAX_FOOD_LEVEL = 20, EATING_TICKS = 30, ///< Number of ticks it takes to eat an item - MAX_AIR_LEVEL = 300, - DROWNING_TICKS = 10, //number of ticks per heart of damage + MAX_AIR_LEVEL = 300, + DROWNING_TICKS = 10, //number of ticks per heart of damage } ; // tolua_end @@ -271,10 +271,10 @@ protected: std::string m_PlayerName; std::string m_LoadedWorldName; - //Player's air level (for swimming) - int m_AirLevel; - //used to time ticks between damage taken via drowning/suffocation - int m_AirTickTimer; + //Player's air level (for swimming) + int m_AirLevel; + //used to time ticks between damage taken via drowning/suffocation + int m_AirTickTimer; bool m_bVisible; @@ -346,8 +346,8 @@ protected: /// Called in each tick to handle food-related processing void HandleFood(void); - /// Called in each tick to handle air-related processing i.e. drowning - void HandleAir(cChunk & a_Chunk); + /// Called in each tick to handle air-related processing i.e. drowning + void HandleAir(cChunk & a_Chunk); /// Adds food exhaustion based on the difference between Pos and LastPos, sprinting status and swimming (in water block) void ApplyFoodExhaustionFromMovement(cChunk & a_Chunk); -- cgit v1.2.3 From 864bdd2626fbbdca10d1ddfe934334c41ccc3571 Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Thu, 8 Aug 2013 11:01:45 +0100 Subject: Made comments more readable by adding space between // and text content. Added /// doxygen syntax comments to declarations --- source/Player.cpp | 14 +++++++------- source/Player.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/Player.cpp b/source/Player.cpp index 21f31c555..fb752ed87 100644 --- a/source/Player.cpp +++ b/source/Player.cpp @@ -1322,8 +1322,8 @@ void cPlayer::UseEquippedItem() void cPlayer::HandleAir(cChunk & a_Chunk) { - //Ref.: http://www.minecraftwiki.net/wiki/Chunk_format - //see if the player is /submerged/ water (block above is water) + // Ref.: http://www.minecraftwiki.net/wiki/Chunk_format + // see if the player is /submerged/ water (block above is water) // Get the type of block the player's standing in: BLOCKTYPE BlockIn; int RelX = (int)floor(m_LastPosX) - a_Chunk.GetPosX() * cChunkDef::Width; @@ -1334,24 +1334,24 @@ void cPlayer::HandleAir(cChunk & a_Chunk) if (IsBlockWater(BlockIn)) { - //either reduce air level or damage player + // either reduce air level or damage player if(m_AirLevel < 1) { if(m_AirTickTimer < 1) { - //damage player + // damage player TakeDamage(dtDrowning, NULL, 1, 1, 0); - //reset timer + // reset timer m_AirTickTimer = DROWNING_TICKS; }else{ m_AirTickTimer -= 1; } }else{ - //reduce air supply + // reduce air supply m_AirLevel -= 1; } }else{ - //set the air back to maximum + // set the air back to maximum m_AirLevel = MAX_AIR_LEVEL; m_AirTickTimer = DROWNING_TICKS; } diff --git a/source/Player.h b/source/Player.h index f1d746318..542656b5a 100644 --- a/source/Player.h +++ b/source/Player.h @@ -271,9 +271,9 @@ protected: std::string m_PlayerName; std::string m_LoadedWorldName; - //Player's air level (for swimming) + /// Player's air level (for swimming) int m_AirLevel; - //used to time ticks between damage taken via drowning/suffocation + /// used to time ticks between damage taken via drowning/suffocation int m_AirTickTimer; bool m_bVisible; -- cgit v1.2.3