From 322ba59c3a0500226e1b934c3390cffe4360ddb4 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 29 May 2012 19:56:07 +0000 Subject: Refactored item-eating from cClientHandle into cPlayer git-svn-id: http://mc-server.googlecode.com/svn/trunk@519 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cClientHandle.cpp | 73 ++---------------------------------------------- source/cPlayer.cpp | 72 +++++++++++++++++++++++++++++++++++++++++------ source/cPlayer.h | 9 ++++-- 3 files changed, 72 insertions(+), 82 deletions(-) diff --git a/source/cClientHandle.cpp b/source/cClientHandle.cpp index 9920b68f8..f5aa9024d 100644 --- a/source/cClientHandle.cpp +++ b/source/cClientHandle.cpp @@ -981,77 +981,7 @@ void cClientHandle::HandleBlockPlace(cPacket_BlockPlace * a_Packet) a_Packet->m_PosX, a_Packet->m_PosY, a_Packet->m_PosZ ); - // Hacked in edible items go!~ - // TODO: Handle hunger - bool bEat = false; - bool isDoor = false; - switch (Item.m_ItemID) - { - case E_ITEM_APPLE: - //m_Player->Heal(4); // 2 hearts - m_Player->Feed(24); // 2 food bars - bEat = true; - break; - case E_ITEM_GOLDEN_APPLE: - //m_Player->Heal(20); // 10 hearts - m_Player->Feed(60); // 5 food - bEat = true; - break; - case E_ITEM_MUSHROOM_SOUP: - ///m_Player->Heal(10); // 5 hearts - m_Player->Feed(48); // 4 food - bEat = true; - break; - case E_ITEM_BREAD: - //m_Player->Heal(5); // 2.5 hearts - m_Player->Feed(30); // 2.5 food - bEat = true; - break; - case E_ITEM_RAW_MEAT: - //m_Player->Heal(3); // 1.5 hearts - m_Player->Feed(18); // 1.5 food - bEat = true; - break; - case E_ITEM_COOKED_MEAT: - //m_Player->Heal(8); // 4 hearts - m_Player->Feed(48); // 4 food - bEat = true; - break; - case E_ITEM_RAW_FISH: - //m_Player->Heal(2); // 1 heart - m_Player->Feed(12); // 1 food - bEat = true; - break; - case E_ITEM_COOKED_FISH: - //m_Player->Heal(5); // 2.5 hearts - m_Player->Feed(30); // 2.5 food - bEat = true; - break; - case E_ITEM_RAW_CHICKEN: - //m_Player->Heal(3); - m_Player->Feed(12); // 1 food - bEat = true; - break; - case E_ITEM_COOKED_CHICKEN: - //m_Player->Heal(8); - m_Player->Feed(36); // 3 food - bEat = true; - break; - case E_ITEM_RAW_BEEF: - //m_Player->Heal(3); - m_Player->Feed(18); // 1.5 food - bEat = true; - break; - case E_ITEM_STEAK: - //m_Player->Heal(8); - m_Player->Feed(48); // 4 food - bEat = true; - break; - default: - break; - }; - - if (bEat) + if (m_Player->EatItem(Item.m_ItemID)) { m_Player->GetInventory().RemoveItem(Item); return; @@ -1062,6 +992,7 @@ void cClientHandle::HandleBlockPlace(cPacket_BlockPlace * a_Packet) // clicked in air return; } + bool isDoor = false; //TODO: Wrong Blocks! int ClickedBlock = (int)m_Player->GetWorld()->GetBlock(a_Packet->m_PosX, a_Packet->m_PosY, a_Packet->m_PosZ); diff --git a/source/cPlayer.cpp b/source/cPlayer.cpp index 87db2d19d..9cfe4d644 100644 --- a/source/cPlayer.cpp +++ b/source/cPlayer.cpp @@ -305,20 +305,31 @@ void cPlayer::Heal( int a_Health ) } } -void cPlayer::Feed( short a_Food ) + + + + +bool cPlayer::Feed(short a_Food) { - if( m_FoodLevel < GetMaxFoodLevel() ) + if (m_FoodLevel >= GetMaxFoodLevel()) { - m_FoodLevel = MIN(a_Food + m_FoodLevel, GetMaxFoodLevel()); - - cPacket_UpdateHealth Health; - Health.m_Health = m_Health; - Health.m_Food = GetFood(); - Health.m_Saturation = GetFoodSaturation(); - m_ClientHandle->Send( Health ); + return false; } + + m_FoodLevel = MIN(a_Food + m_FoodLevel, GetMaxFoodLevel()); + + cPacket_UpdateHealth Health; + Health.m_Health = m_Health; + Health.m_Food = GetFood(); + Health.m_Saturation = GetFoodSaturation(); + m_ClientHandle->Send( Health ); + return true; } + + + + void cPlayer::TakeDamage( int a_Damage, cEntity* a_Instigator ) { if ( !(m_GameMode == 1) ) { @@ -985,3 +996,46 @@ void cPlayer::UseEquippedItem() + +bool cPlayer::EatItem(int a_ItemType) +{ + // TODO: Handle hunger + switch (a_ItemType) + { + case E_ITEM_APPLE: return Feed(24); // 2 food bars + case E_ITEM_GOLDEN_APPLE: return Feed(60); // 5 food + case E_ITEM_MUSHROOM_SOUP: return Feed(48); // 4 food + case E_ITEM_BREAD: return Feed(30); // 2.5 food + case E_ITEM_RAW_MEAT: return Feed(18); // 1.5 food + case E_ITEM_COOKED_MEAT: return Feed(48); // 4 food + case E_ITEM_RAW_FISH: return Feed(12); // 1 food + case E_ITEM_COOKED_FISH: return Feed(30); // 2.5 food + case E_ITEM_COOKED_CHICKEN: return Feed(36); // 3 food + case E_ITEM_RAW_BEEF: return Feed(18); // 1.5 food + case E_ITEM_STEAK: return Feed(48); // 4 food + case E_ITEM_RAW_CHICKEN: + { + if (!Feed(12)) // 1 food + { + return false; + } + // TODO: A random chance to get food-poisoned + return true; + } + + case E_ITEM_ROTTEN_FLESH: + { + if (!Feed(24)) + { + return false; + } + // TODO: Food-poisoning + return true; + } + } + return false; +} + + + + diff --git a/source/cPlayer.h b/source/cPlayer.h index 830289046..815c31316 100644 --- a/source/cPlayer.h +++ b/source/cPlayer.h @@ -81,7 +81,10 @@ public: void TossItem( bool a_bDraggingItem, int a_Amount = 1 ); //tolua_export void Heal( int a_Health ); //tolua_export - void Feed( short a_Food ); + + /// Returns true if any food has been consumed, false if player "full" + bool Feed(short a_Food); + void TakeDamage( int a_Damage, cEntity* a_Instigator ); //tolua_export void KilledBy( cEntity* a_Killer ); //tolua_export void Respawn(); //tolua_export @@ -97,8 +100,10 @@ public: const AString & GetLoadedWorldName() { return m_LoadedWorldName; } - void UseEquippedItem(); + void UseEquippedItem(void); + /// Returns true if the item type is edible && it has been consumed, false otherwise + bool EatItem(int a_ItemType); protected: virtual void Destroyed(); -- cgit v1.2.3