From ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 23 Sep 2012 22:09:57 +0000 Subject: Source files cleanup: The rest of the files renamed. git-svn-id: http://mc-server.googlecode.com/svn/trunk@887 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/items/Item.cpp | 418 ------------------------------------ source/items/Item.h | 83 ------- source/items/ItemBucket.h | 5 +- source/items/ItemCloth.h | 2 +- source/items/ItemDoor.h | 5 +- source/items/ItemDye.h | 8 +- source/items/ItemFood.h | 3 +- source/items/ItemHandler.cpp | 418 ++++++++++++++++++++++++++++++++++++ source/items/ItemHandler.h | 83 +++++++ source/items/ItemHoe.h | 8 +- source/items/ItemLeaves.h | 2 +- source/items/ItemLighter.h | 6 +- source/items/ItemPickaxe.h | 8 +- source/items/ItemRedstoneDust.h | 3 +- source/items/ItemRedstoneRepeater.h | 3 +- source/items/ItemSapling.h | 2 +- source/items/ItemSeeds.h | 4 +- source/items/ItemShears.h | 7 +- source/items/ItemShovel.h | 14 +- source/items/ItemSign.h | 5 +- source/items/ItemSlab.h | 4 +- source/items/ItemSugarcane.h | 4 +- source/items/ItemSword.h | 8 +- source/items/ItemWood.h | 2 +- 24 files changed, 563 insertions(+), 542 deletions(-) delete mode 100644 source/items/Item.cpp delete mode 100644 source/items/Item.h create mode 100644 source/items/ItemHandler.cpp create mode 100644 source/items/ItemHandler.h (limited to 'source/items') diff --git a/source/items/Item.cpp b/source/items/Item.cpp deleted file mode 100644 index f1b376493..000000000 --- a/source/items/Item.cpp +++ /dev/null @@ -1,418 +0,0 @@ - -#include "Globals.h" -#include "Item.h" -#include "../cItem.h" -#include "../cWorld.h" -#include "../cPlayer.h" - -//Handler -#include "ItemCloth.h" -#include "ItemHoe.h" -#include "ItemSlab.h" -#include "ItemWood.h" -#include "ItemShears.h" -#include "ItemLeaves.h" -#include "ItemSapling.h" -#include "ItemBucket.h" -#include "ItemLighter.h" -#include "ItemRedstoneDust.h" -#include "ItemRedstoneRepeater.h" -#include "ItemSeeds.h" -#include "ItemDye.h" -#include "ItemSugarcane.h" -#include "ItemPickaxe.h" -#include "ItemShovel.h" -#include "ItemSword.h" -#include "ItemDoor.h" -#include "ItemFood.h" -#include "ItemSign.h" - -#include "../blocks/Block.h" - - - - - -bool cItemHandler::m_HandlerInitialized = false; -cItemHandler * cItemHandler::m_ItemHandler[2266]; - - - - - -cItemHandler *cItemHandler::GetItemHandler(int a_ItemID) -{ - if(a_ItemID < 0) a_ItemID = 0; - - if(!m_HandlerInitialized) - { //We have to initialize - memset(m_ItemHandler, 0, sizeof(m_ItemHandler)); - m_HandlerInitialized = true; - } - if(m_ItemHandler[a_ItemID]) - return m_ItemHandler[a_ItemID]; - m_ItemHandler[a_ItemID] = CreateItemHandler(a_ItemID); - return m_ItemHandler[a_ItemID]; -} - - - - - -cItemHandler *cItemHandler::CreateItemHandler(int a_ItemID) -{ - switch(a_ItemID) - { - default: return new cItemHandler(a_ItemID); - - // Single item per handler: - case E_ITEM_SHEARS: return new cItemShearsHandler(a_ItemID); - case E_ITEM_LEAVES: return new cItemLeavesHandler(a_ItemID); - case E_ITEM_SAPLING: return new cItemSaplingHandler(a_ItemID); - case E_ITEM_DYE: return new cItemDyeHandler(a_ItemID); - case E_ITEM_SUGARCANE: return new cItemSugarcaneHandler(a_ItemID); - case E_ITEM_FLINT_AND_STEEL: return new cItemLighterHandler(a_ItemID); - case E_ITEM_REDSTONE_DUST: return new cItemRedstoneDustHandler(a_ItemID); - case E_ITEM_REDSTONE_REPEATER: return new cItemRedstoneRepeaterHandler(a_ItemID); - case E_ITEM_WOOL: return new cItemClothHandler(a_ItemID); - - case E_ITEM_WOODEN_HOE: - case E_ITEM_STONE_HOE: - case E_ITEM_IRON_HOE: - case E_ITEM_GOLD_HOE: - case E_ITEM_DIAMOND_HOE: - { - return new cItemHoeHandler(a_ItemID); - } - - case E_ITEM_WOODEN_PICKAXE: - case E_ITEM_STONE_PICKAXE: - case E_ITEM_IRON_PICKAXE: - case E_ITEM_GOLD_PICKAXE: - case E_ITEM_DIAMOND_PICKAXE: - { - return new cItemPickaxeHandler(a_ItemID); - } - - case E_ITEM_WOODEN_SHOVEL: - case E_ITEM_STONE_SHOVEL: - case E_ITEM_IRON_SHOVEL: - case E_ITEM_GOLD_SHOVEL: - case E_ITEM_DIAMOND_SHOVEL: - { - return new cItemShovelHandler(a_ItemID); - } - - case E_ITEM_WOODEN_SWORD: - case E_ITEM_STONE_SWORD: - case E_ITEM_IRON_SWORD: - case E_ITEM_GOLD_SWORD: - case E_ITEM_DIAMOND_SWORD: - { - return new cItemSwordHandler(a_ItemID); - } - - case E_ITEM_STONE_SLAB: - case E_ITEM_WOODEN_SLAB: - { - return new cItemSlabHandler(a_ItemID); - } - - case E_ITEM_LOG: - case E_ITEM_PLANKS: - { - return new cItemWoodHandler(a_ItemID); - } - - case E_ITEM_BUCKET: - case E_ITEM_WATER_BUCKET: - case E_ITEM_LAVA_BUCKET: - { - return new cItemBucketHandler(a_ItemID); - } - - case E_ITEM_PUMPKIN_SEEDS: - case E_ITEM_MELON_SEEDS: - case E_ITEM_SEEDS: - { - return new cItemSeedsHandler(a_ItemID); - } - - case E_ITEM_IRON_DOOR: - case E_ITEM_WOODEN_DOOR: - { - return new cItemDoorHandler(a_ItemID); - } - - // Food: - case E_ITEM_BREAD: - case E_ITEM_COOKIE: - case E_ITEM_MELON_SLICE: - case E_ITEM_RAW_CHICKEN: - case E_ITEM_COOKED_CHICKEN: - case E_ITEM_RAW_BEEF: - case E_ITEM_RAW_MEAT: - case E_ITEM_STEAK: - case E_ITEM_COOKED_MEAT: - case E_ITEM_RAW_FISH: - case E_ITEM_COOKED_FISH: - case E_ITEM_RED_APPLE: - case E_ITEM_GOLDEN_APPLE: - case E_ITEM_ROTTEN_FLESH: - case E_ITEM_SPIDER_EYE: - { - return new cItemFoodHandler(a_ItemID); - } - - case E_ITEM_SIGN: - return new cItemSignHandler(a_ItemID); - } -} - - - - - -void cItemHandler::Deinit() -{ - for(int i = 0; i < 2266; i++) - { - delete m_ItemHandler[i]; - } - m_HandlerInitialized = false; -} - - - - - -cItemHandler::cItemHandler(int a_ItemID) -{ - m_ItemID = a_ItemID; -} - - - - - -bool cItemHandler::OnItemUse(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir) -{ - return false; -} - - - - - -bool cItemHandler::OnDiggingBlock(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir) -{ - return false; -} - - - - - -void cItemHandler::OnBlockDestroyed(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z) -{ - char Block = a_World->GetBlock(a_X, a_Y, a_Z); - cBlockHandler *Handler = cBlockHandler::GetBlockHandler(Block); - - if(a_Player->GetGameMode() == eGameMode_Survival) - { - if(!BlockRequiresSpecialTool(Block) || CanHarvestBlock(Block)) - { - Handler->DropBlock(a_World, a_X, a_Y, a_Z); - } - } - - a_Player->UseEquippedItem(); -} - - - - - -void cItemHandler::OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item) -{ - -} - - - - - -char cItemHandler::GetMaxStackSize(void) -{ - if (m_ItemID < 256) - { - // All blocks can stack up to 64 - return 64; - } - - switch (m_ItemID) - { - case E_ITEM_APPLE: return 64; - case E_ITEM_ARROW: return 64; - case E_ITEM_BLAZE_POWDER: return 64; - case E_ITEM_BLAZE_ROD: return 64; - case E_ITEM_BONE: return 64; - case E_ITEM_BOOK: return 64; - case E_ITEM_BOWL: return 64; - case E_ITEM_BREAD: return 64; - case E_ITEM_BROWN_MUSHROOM: return 64; - case E_ITEM_BUCKET: return 1; // TODO: change this to 16 when turning compatibility to 1.3 - case E_ITEM_COAL: return 64; - case E_ITEM_COOKED_CHICKEN: return 64; - case E_ITEM_COOKED_FISH: return 64; - case E_ITEM_COOKED_PORKCHOP: return 64; - case E_ITEM_DIAMOND: return 64; - case E_ITEM_FEATHER: return 64; - case E_ITEM_FLINT: return 64; - case E_ITEM_GOLD: return 64; - case E_ITEM_GUNPOWDER: return 64; - case E_ITEM_IRON: return 64; - case E_ITEM_RAW_PORKCHOP: return 64; - case E_ITEM_SEEDS: return 64; - case E_ITEM_STICK: return 64; - case E_ITEM_STRING: return 64; - case E_ITEM_WHEAT: return 64; - } - // By default items don't stack: - return 1; -} - - - - - -bool cItemHandler::IsTool() -{ - return - (m_ItemID >= 256 && m_ItemID <= 259) - || (m_ItemID == 261) - || (m_ItemID >= 267 && m_ItemID <= 279) - || (m_ItemID >= 283 && m_ItemID <= 286) - || (m_ItemID >= 290 && m_ItemID <= 294) - || (m_ItemID >= 256 && m_ItemID <= 259) - || (m_ItemID == 325) - || (m_ItemID == 346); -} - - - - - -bool cItemHandler::IsFood() -{ - return - (m_ItemID == 260) - || (m_ItemID == 282) - || (m_ItemID == 297) - || (m_ItemID >= 319 && m_ItemID <= 320) - || (m_ItemID == 335) - || (m_ItemID >= 349 && m_ItemID <= 350) - || (m_ItemID == 357) - || (m_ItemID == 360) - || (m_ItemID >= 363 && m_ItemID <= 366); -} - - - - - -bool cItemHandler::IsPlaceable() -{ - return m_ItemID >= 1 && m_ItemID <= 136; -} - - - - - -bool cItemHandler::CanHarvestBlock(BLOCKTYPE a_BlockID) -{ - return false; -} - - - - - -BLOCKTYPE cItemHandler::GetBlockType() -{ - ASSERT(m_ItemID < 256); // Items with IDs above 255 should all be handled by specific handlers - - #ifdef _DEBUG - if (m_ItemID > 256) - { - LOGERROR("Item %d has no valid block!", m_ItemID); - } - #endif // _DEBUG - - return (BLOCKTYPE) m_ItemID; -} - - - - - -NIBBLETYPE cItemHandler::GetBlockMeta(short a_ItemDamage) -{ - return (NIBBLETYPE)a_ItemDamage & 0x0f; // This keeps most textures. The few other items have to override this -} - - - - - -void cItemHandler::PlaceBlock(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir) -{ - BLOCKTYPE Block = GetBlockType(); - cBlockHandler *Handler = cBlockHandler::GetBlockHandler(Block); - Handler->PlaceBlock(a_World, a_Player, GetBlockMeta(a_Item->m_ItemHealth), a_X, a_Y, a_Z, a_Dir); - if(a_Player->GetGameMode() == eGameMode_Survival) - { - cItem Item(a_Item->m_ItemID, 1); - a_Player->GetInventory().RemoveItem(Item); - } -} - - - - - -bool cItemHandler::EatItem(cPlayer *a_Player, cItem *a_Item) -{ - FoodInfo Info = GetFoodInfo(); - - if(Info.FoodLevel > 0 || Info.Saturation > 0.f) - { - bool Success = a_Player->Feed(Info.FoodLevel, Info.Saturation); - if(Success && Info.PoisionChance > 0) - { - MTRand r1; - if((r1.randInt(100) - Info.PoisionChance) <= 0) - { //Unlucky guy :D - //TODO: Make player ill - } - } - - return Success; - } - - return false; -} - - - - - -cItemHandler::FoodInfo cItemHandler::GetFoodInfo() -{ - return FoodInfo(0, 0.f); -} - - - - diff --git a/source/items/Item.h b/source/items/Item.h deleted file mode 100644 index 67935ecb4..000000000 --- a/source/items/Item.h +++ /dev/null @@ -1,83 +0,0 @@ - -#pragma once -#include "../Defines.h" - - - - - -// fwd: -class cWorld; -class cPlayer; - - - - - -class cItemHandler -{ -public: - cItemHandler(int a_ItemID); - // Called when the player tries to use the item. Return false to make the item unusable. DEFAULT: False - virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir); //eg for fishing or hoes - // Called while the player diggs a block using this item - virtual bool OnDiggingBlock(cWorld * a_World, cPlayer * a_Player, cItem * a_HeldItem, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace); - // Called when the player destroys a block using this item. This also calls the drop function for the destroyed block - virtual void OnBlockDestroyed(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z); - // Called after the player has eaten this item. - virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item); - - /// Returns the maximum stack size for a given item - virtual char GetMaxStackSize(void); - - struct FoodInfo - { - FoodInfo(short a_FoodLevel, float a_Saturation, char a_PoisionChance = 0) - { - FoodLevel = a_FoodLevel; - Saturation = a_Saturation; - PoisionChance = a_PoisionChance; - } - short FoodLevel; - float Saturation; - char PoisionChance; //0 - 100 - }; - - // Returns the FoodInfo for this item. (FoodRecovery, Saturation and PoisionChance) - virtual FoodInfo GetFoodInfo(); - - // Lets the player eat a selected item. Returns true if the player ate the item - virtual bool EatItem(cPlayer *a_Player, cItem *a_Item); - - // Places the current block and removes the item from the player inventory - virtual void PlaceBlock(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir); - - // Indicates if this item is a tool - virtual bool IsTool(); - // Indicates if this item is food - virtual bool IsFood(); - //Blocks simply get placed - virtual bool IsPlaceable(); - - // Returns the block type on placement - virtual BLOCKTYPE GetBlockType(); - //Returns the block meta on placement - virtual NIBBLETYPE GetBlockMeta(short a_ItemDamage); - // Returns whether this tool/item can harvest a specific block (e.g. wooden pickaxe can harvest stone, but wood canīt) DEFAULT: False - virtual bool CanHarvestBlock(BLOCKTYPE a_BlockID); - - static cItemHandler *GetItemHandler(int a_ItemID); - - static void Deinit(); - - -protected: - int m_ItemID; - static cItemHandler *CreateItemHandler(int m_ItemID); - - static cItemHandler *m_ItemHandler[2266]; - static bool m_HandlerInitialized; //used to detect if the itemhandlers are initialized -}; - -//Short function -inline cItemHandler *ItemHandler(int a_ItemID) { return cItemHandler::GetItemHandler(a_ItemID); } \ No newline at end of file diff --git a/source/items/ItemBucket.h b/source/items/ItemBucket.h index 3e233e4f6..736112960 100644 --- a/source/items/ItemBucket.h +++ b/source/items/ItemBucket.h @@ -1,7 +1,8 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" +#include "ItemHandler.h" +#include "../World.h" class cItemBucketHandler : public cItemHandler { diff --git a/source/items/ItemCloth.h b/source/items/ItemCloth.h index d67e14864..5de064397 100644 --- a/source/items/ItemCloth.h +++ b/source/items/ItemCloth.h @@ -1,7 +1,7 @@ #pragma once -#include "Item.h" +#include "ItemHandler.h" diff --git a/source/items/ItemDoor.h b/source/items/ItemDoor.h index ac5bc6872..6e841333f 100644 --- a/source/items/ItemDoor.h +++ b/source/items/ItemDoor.h @@ -1,7 +1,8 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" +#include "ItemHandler.h" +#include "../World.h" class cItemDoorHandler : public cItemHandler { diff --git a/source/items/ItemDye.h b/source/items/ItemDye.h index 2203f410b..5b21a501c 100644 --- a/source/items/ItemDye.h +++ b/source/items/ItemDye.h @@ -1,7 +1,9 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" + +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" class cItemDyeHandler : public cItemHandler { diff --git a/source/items/ItemFood.h b/source/items/ItemFood.h index 8998fd503..6e5a27d5d 100644 --- a/source/items/ItemFood.h +++ b/source/items/ItemFood.h @@ -1,6 +1,7 @@ + #pragma once -#include "Item.h" +#include "ItemHandler.h" class cItemFoodHandler : public cItemHandler diff --git a/source/items/ItemHandler.cpp b/source/items/ItemHandler.cpp new file mode 100644 index 000000000..44f6438e1 --- /dev/null +++ b/source/items/ItemHandler.cpp @@ -0,0 +1,418 @@ + +#include "Globals.h" +#include "ItemHandler.h" +#include "../Item.h" +#include "../World.h" +#include "../Player.h" + +//Handler +#include "ItemCloth.h" +#include "ItemHoe.h" +#include "ItemSlab.h" +#include "ItemWood.h" +#include "ItemShears.h" +#include "ItemLeaves.h" +#include "ItemSapling.h" +#include "ItemBucket.h" +#include "ItemLighter.h" +#include "ItemRedstoneDust.h" +#include "ItemRedstoneRepeater.h" +#include "ItemSeeds.h" +#include "ItemDye.h" +#include "ItemSugarcane.h" +#include "ItemPickaxe.h" +#include "ItemShovel.h" +#include "ItemSword.h" +#include "ItemDoor.h" +#include "ItemFood.h" +#include "ItemSign.h" + +#include "../blocks/BlockHandler.h" + + + + + +bool cItemHandler::m_HandlerInitialized = false; +cItemHandler * cItemHandler::m_ItemHandler[2266]; + + + + + +cItemHandler *cItemHandler::GetItemHandler(int a_ItemID) +{ + if(a_ItemID < 0) a_ItemID = 0; + + if(!m_HandlerInitialized) + { //We have to initialize + memset(m_ItemHandler, 0, sizeof(m_ItemHandler)); + m_HandlerInitialized = true; + } + if(m_ItemHandler[a_ItemID]) + return m_ItemHandler[a_ItemID]; + m_ItemHandler[a_ItemID] = CreateItemHandler(a_ItemID); + return m_ItemHandler[a_ItemID]; +} + + + + + +cItemHandler *cItemHandler::CreateItemHandler(int a_ItemID) +{ + switch(a_ItemID) + { + default: return new cItemHandler(a_ItemID); + + // Single item per handler: + case E_ITEM_SHEARS: return new cItemShearsHandler(a_ItemID); + case E_ITEM_LEAVES: return new cItemLeavesHandler(a_ItemID); + case E_ITEM_SAPLING: return new cItemSaplingHandler(a_ItemID); + case E_ITEM_DYE: return new cItemDyeHandler(a_ItemID); + case E_ITEM_SUGARCANE: return new cItemSugarcaneHandler(a_ItemID); + case E_ITEM_FLINT_AND_STEEL: return new cItemLighterHandler(a_ItemID); + case E_ITEM_REDSTONE_DUST: return new cItemRedstoneDustHandler(a_ItemID); + case E_ITEM_REDSTONE_REPEATER: return new cItemRedstoneRepeaterHandler(a_ItemID); + case E_ITEM_WOOL: return new cItemClothHandler(a_ItemID); + + case E_ITEM_WOODEN_HOE: + case E_ITEM_STONE_HOE: + case E_ITEM_IRON_HOE: + case E_ITEM_GOLD_HOE: + case E_ITEM_DIAMOND_HOE: + { + return new cItemHoeHandler(a_ItemID); + } + + case E_ITEM_WOODEN_PICKAXE: + case E_ITEM_STONE_PICKAXE: + case E_ITEM_IRON_PICKAXE: + case E_ITEM_GOLD_PICKAXE: + case E_ITEM_DIAMOND_PICKAXE: + { + return new cItemPickaxeHandler(a_ItemID); + } + + case E_ITEM_WOODEN_SHOVEL: + case E_ITEM_STONE_SHOVEL: + case E_ITEM_IRON_SHOVEL: + case E_ITEM_GOLD_SHOVEL: + case E_ITEM_DIAMOND_SHOVEL: + { + return new cItemShovelHandler(a_ItemID); + } + + case E_ITEM_WOODEN_SWORD: + case E_ITEM_STONE_SWORD: + case E_ITEM_IRON_SWORD: + case E_ITEM_GOLD_SWORD: + case E_ITEM_DIAMOND_SWORD: + { + return new cItemSwordHandler(a_ItemID); + } + + case E_ITEM_STONE_SLAB: + case E_ITEM_WOODEN_SLAB: + { + return new cItemSlabHandler(a_ItemID); + } + + case E_ITEM_LOG: + case E_ITEM_PLANKS: + { + return new cItemWoodHandler(a_ItemID); + } + + case E_ITEM_BUCKET: + case E_ITEM_WATER_BUCKET: + case E_ITEM_LAVA_BUCKET: + { + return new cItemBucketHandler(a_ItemID); + } + + case E_ITEM_PUMPKIN_SEEDS: + case E_ITEM_MELON_SEEDS: + case E_ITEM_SEEDS: + { + return new cItemSeedsHandler(a_ItemID); + } + + case E_ITEM_IRON_DOOR: + case E_ITEM_WOODEN_DOOR: + { + return new cItemDoorHandler(a_ItemID); + } + + // Food: + case E_ITEM_BREAD: + case E_ITEM_COOKIE: + case E_ITEM_MELON_SLICE: + case E_ITEM_RAW_CHICKEN: + case E_ITEM_COOKED_CHICKEN: + case E_ITEM_RAW_BEEF: + case E_ITEM_RAW_MEAT: + case E_ITEM_STEAK: + case E_ITEM_COOKED_MEAT: + case E_ITEM_RAW_FISH: + case E_ITEM_COOKED_FISH: + case E_ITEM_RED_APPLE: + case E_ITEM_GOLDEN_APPLE: + case E_ITEM_ROTTEN_FLESH: + case E_ITEM_SPIDER_EYE: + { + return new cItemFoodHandler(a_ItemID); + } + + case E_ITEM_SIGN: + return new cItemSignHandler(a_ItemID); + } +} + + + + + +void cItemHandler::Deinit() +{ + for(int i = 0; i < 2266; i++) + { + delete m_ItemHandler[i]; + } + m_HandlerInitialized = false; +} + + + + + +cItemHandler::cItemHandler(int a_ItemID) +{ + m_ItemID = a_ItemID; +} + + + + + +bool cItemHandler::OnItemUse(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir) +{ + return false; +} + + + + + +bool cItemHandler::OnDiggingBlock(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir) +{ + return false; +} + + + + + +void cItemHandler::OnBlockDestroyed(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z) +{ + char Block = a_World->GetBlock(a_X, a_Y, a_Z); + cBlockHandler *Handler = cBlockHandler::GetBlockHandler(Block); + + if(a_Player->GetGameMode() == eGameMode_Survival) + { + if(!BlockRequiresSpecialTool(Block) || CanHarvestBlock(Block)) + { + Handler->DropBlock(a_World, a_X, a_Y, a_Z); + } + } + + a_Player->UseEquippedItem(); +} + + + + + +void cItemHandler::OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item) +{ + +} + + + + + +char cItemHandler::GetMaxStackSize(void) +{ + if (m_ItemID < 256) + { + // All blocks can stack up to 64 + return 64; + } + + switch (m_ItemID) + { + case E_ITEM_APPLE: return 64; + case E_ITEM_ARROW: return 64; + case E_ITEM_BLAZE_POWDER: return 64; + case E_ITEM_BLAZE_ROD: return 64; + case E_ITEM_BONE: return 64; + case E_ITEM_BOOK: return 64; + case E_ITEM_BOWL: return 64; + case E_ITEM_BREAD: return 64; + case E_ITEM_BROWN_MUSHROOM: return 64; + case E_ITEM_BUCKET: return 1; // TODO: change this to 16 when turning compatibility to 1.3 + case E_ITEM_COAL: return 64; + case E_ITEM_COOKED_CHICKEN: return 64; + case E_ITEM_COOKED_FISH: return 64; + case E_ITEM_COOKED_PORKCHOP: return 64; + case E_ITEM_DIAMOND: return 64; + case E_ITEM_FEATHER: return 64; + case E_ITEM_FLINT: return 64; + case E_ITEM_GOLD: return 64; + case E_ITEM_GUNPOWDER: return 64; + case E_ITEM_IRON: return 64; + case E_ITEM_RAW_PORKCHOP: return 64; + case E_ITEM_SEEDS: return 64; + case E_ITEM_STICK: return 64; + case E_ITEM_STRING: return 64; + case E_ITEM_WHEAT: return 64; + } + // By default items don't stack: + return 1; +} + + + + + +bool cItemHandler::IsTool() +{ + return + (m_ItemID >= 256 && m_ItemID <= 259) + || (m_ItemID == 261) + || (m_ItemID >= 267 && m_ItemID <= 279) + || (m_ItemID >= 283 && m_ItemID <= 286) + || (m_ItemID >= 290 && m_ItemID <= 294) + || (m_ItemID >= 256 && m_ItemID <= 259) + || (m_ItemID == 325) + || (m_ItemID == 346); +} + + + + + +bool cItemHandler::IsFood() +{ + return + (m_ItemID == 260) + || (m_ItemID == 282) + || (m_ItemID == 297) + || (m_ItemID >= 319 && m_ItemID <= 320) + || (m_ItemID == 335) + || (m_ItemID >= 349 && m_ItemID <= 350) + || (m_ItemID == 357) + || (m_ItemID == 360) + || (m_ItemID >= 363 && m_ItemID <= 366); +} + + + + + +bool cItemHandler::IsPlaceable() +{ + return m_ItemID >= 1 && m_ItemID <= 136; +} + + + + + +bool cItemHandler::CanHarvestBlock(BLOCKTYPE a_BlockID) +{ + return false; +} + + + + + +BLOCKTYPE cItemHandler::GetBlockType() +{ + ASSERT(m_ItemID < 256); // Items with IDs above 255 should all be handled by specific handlers + + #ifdef _DEBUG + if (m_ItemID > 256) + { + LOGERROR("Item %d has no valid block!", m_ItemID); + } + #endif // _DEBUG + + return (BLOCKTYPE) m_ItemID; +} + + + + + +NIBBLETYPE cItemHandler::GetBlockMeta(short a_ItemDamage) +{ + return (NIBBLETYPE)a_ItemDamage & 0x0f; // This keeps most textures. The few other items have to override this +} + + + + + +void cItemHandler::PlaceBlock(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir) +{ + BLOCKTYPE Block = GetBlockType(); + cBlockHandler *Handler = cBlockHandler::GetBlockHandler(Block); + Handler->PlaceBlock(a_World, a_Player, GetBlockMeta(a_Item->m_ItemHealth), a_X, a_Y, a_Z, a_Dir); + if(a_Player->GetGameMode() == eGameMode_Survival) + { + cItem Item(a_Item->m_ItemID, 1); + a_Player->GetInventory().RemoveItem(Item); + } +} + + + + + +bool cItemHandler::EatItem(cPlayer *a_Player, cItem *a_Item) +{ + FoodInfo Info = GetFoodInfo(); + + if(Info.FoodLevel > 0 || Info.Saturation > 0.f) + { + bool Success = a_Player->Feed(Info.FoodLevel, Info.Saturation); + if(Success && Info.PoisionChance > 0) + { + MTRand r1; + if((r1.randInt(100) - Info.PoisionChance) <= 0) + { //Unlucky guy :D + //TODO: Make player ill + } + } + + return Success; + } + + return false; +} + + + + + +cItemHandler::FoodInfo cItemHandler::GetFoodInfo() +{ + return FoodInfo(0, 0.f); +} + + + + diff --git a/source/items/ItemHandler.h b/source/items/ItemHandler.h new file mode 100644 index 000000000..67935ecb4 --- /dev/null +++ b/source/items/ItemHandler.h @@ -0,0 +1,83 @@ + +#pragma once +#include "../Defines.h" + + + + + +// fwd: +class cWorld; +class cPlayer; + + + + + +class cItemHandler +{ +public: + cItemHandler(int a_ItemID); + // Called when the player tries to use the item. Return false to make the item unusable. DEFAULT: False + virtual bool OnItemUse(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir); //eg for fishing or hoes + // Called while the player diggs a block using this item + virtual bool OnDiggingBlock(cWorld * a_World, cPlayer * a_Player, cItem * a_HeldItem, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace); + // Called when the player destroys a block using this item. This also calls the drop function for the destroyed block + virtual void OnBlockDestroyed(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z); + // Called after the player has eaten this item. + virtual void OnFoodEaten(cWorld *a_World, cPlayer *a_Player, cItem *a_Item); + + /// Returns the maximum stack size for a given item + virtual char GetMaxStackSize(void); + + struct FoodInfo + { + FoodInfo(short a_FoodLevel, float a_Saturation, char a_PoisionChance = 0) + { + FoodLevel = a_FoodLevel; + Saturation = a_Saturation; + PoisionChance = a_PoisionChance; + } + short FoodLevel; + float Saturation; + char PoisionChance; //0 - 100 + }; + + // Returns the FoodInfo for this item. (FoodRecovery, Saturation and PoisionChance) + virtual FoodInfo GetFoodInfo(); + + // Lets the player eat a selected item. Returns true if the player ate the item + virtual bool EatItem(cPlayer *a_Player, cItem *a_Item); + + // Places the current block and removes the item from the player inventory + virtual void PlaceBlock(cWorld *a_World, cPlayer *a_Player, cItem *a_Item, int a_X, int a_Y, int a_Z, char a_Dir); + + // Indicates if this item is a tool + virtual bool IsTool(); + // Indicates if this item is food + virtual bool IsFood(); + //Blocks simply get placed + virtual bool IsPlaceable(); + + // Returns the block type on placement + virtual BLOCKTYPE GetBlockType(); + //Returns the block meta on placement + virtual NIBBLETYPE GetBlockMeta(short a_ItemDamage); + // Returns whether this tool/item can harvest a specific block (e.g. wooden pickaxe can harvest stone, but wood canīt) DEFAULT: False + virtual bool CanHarvestBlock(BLOCKTYPE a_BlockID); + + static cItemHandler *GetItemHandler(int a_ItemID); + + static void Deinit(); + + +protected: + int m_ItemID; + static cItemHandler *CreateItemHandler(int m_ItemID); + + static cItemHandler *m_ItemHandler[2266]; + static bool m_HandlerInitialized; //used to detect if the itemhandlers are initialized +}; + +//Short function +inline cItemHandler *ItemHandler(int a_ItemID) { return cItemHandler::GetItemHandler(a_ItemID); } \ No newline at end of file diff --git a/source/items/ItemHoe.h b/source/items/ItemHoe.h index 64dc4ca34..e7ea9135c 100644 --- a/source/items/ItemHoe.h +++ b/source/items/ItemHoe.h @@ -1,7 +1,9 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" + +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" class cItemHoeHandler : public cItemHandler { diff --git a/source/items/ItemLeaves.h b/source/items/ItemLeaves.h index 01b090a2f..f96adb0b8 100644 --- a/source/items/ItemLeaves.h +++ b/source/items/ItemLeaves.h @@ -1,7 +1,7 @@ #pragma once -#include "Item.h" +#include "ItemHandler.h" diff --git a/source/items/ItemLighter.h b/source/items/ItemLighter.h index 213677e4e..92279aafa 100644 --- a/source/items/ItemLighter.h +++ b/source/items/ItemLighter.h @@ -1,8 +1,8 @@ #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" class cItemLighterHandler : public cItemHandler { diff --git a/source/items/ItemPickaxe.h b/source/items/ItemPickaxe.h index a179d154f..158aca44f 100644 --- a/source/items/ItemPickaxe.h +++ b/source/items/ItemPickaxe.h @@ -1,7 +1,9 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" + +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" class cItemPickaxeHandler : public cItemHandler { diff --git a/source/items/ItemRedstoneDust.h b/source/items/ItemRedstoneDust.h index 4d1459bc6..1ce316d9e 100644 --- a/source/items/ItemRedstoneDust.h +++ b/source/items/ItemRedstoneDust.h @@ -1,6 +1,7 @@ #pragma once -#include "Item.h" + +#include "ItemHandler.h" diff --git a/source/items/ItemRedstoneRepeater.h b/source/items/ItemRedstoneRepeater.h index 85500c9f4..c31c2af53 100644 --- a/source/items/ItemRedstoneRepeater.h +++ b/source/items/ItemRedstoneRepeater.h @@ -1,6 +1,7 @@ #pragma once -#include "Item.h" + +#include "ItemHandler.h" diff --git a/source/items/ItemSapling.h b/source/items/ItemSapling.h index 7edbc76c9..b0c223f29 100644 --- a/source/items/ItemSapling.h +++ b/source/items/ItemSapling.h @@ -1,7 +1,7 @@ #pragma once -#include "Item.h" +#include "ItemHandler.h" diff --git a/source/items/ItemSeeds.h b/source/items/ItemSeeds.h index 07cf0e5de..3eb9d6e86 100644 --- a/source/items/ItemSeeds.h +++ b/source/items/ItemSeeds.h @@ -1,8 +1,8 @@ #pragma once -#include "Item.h" -#include "../cWorld.h" +#include "ItemHandler.h" +#include "../World.h" diff --git a/source/items/ItemShears.h b/source/items/ItemShears.h index ab2981aae..2f3476735 100644 --- a/source/items/ItemShears.h +++ b/source/items/ItemShears.h @@ -1,8 +1,9 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" class cItemShearsHandler : public cItemHandler { diff --git a/source/items/ItemShovel.h b/source/items/ItemShovel.h index 3279ef419..24928efb8 100644 --- a/source/items/ItemShovel.h +++ b/source/items/ItemShovel.h @@ -1,9 +1,15 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" -#include "../blocks/Block.h" +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" + +#include "../blocks/BlockHandler.h" + + + + class cItemShovelHandler : public cItemHandler { diff --git a/source/items/ItemSign.h b/source/items/ItemSign.h index 06116f0e8..a0c6aaa9c 100644 --- a/source/items/ItemSign.h +++ b/source/items/ItemSign.h @@ -1,7 +1,8 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" +#include "ItemHandler.h" +#include "../World.h" class cItemSignHandler : public cItemHandler { diff --git a/source/items/ItemSlab.h b/source/items/ItemSlab.h index 955d3acfc..0bab038a2 100644 --- a/source/items/ItemSlab.h +++ b/source/items/ItemSlab.h @@ -1,8 +1,8 @@ #pragma once -#include "Item.h" -#include "../cWorld.h" +#include "ItemHandler.h" +#include "../World.h" diff --git a/source/items/ItemSugarcane.h b/source/items/ItemSugarcane.h index 74557479e..4fdf43428 100644 --- a/source/items/ItemSugarcane.h +++ b/source/items/ItemSugarcane.h @@ -1,8 +1,8 @@ #pragma once -#include "Item.h" -#include "../cWorld.h" +#include "ItemHandler.h" +#include "../World.h" diff --git a/source/items/ItemSword.h b/source/items/ItemSword.h index cbe172c8f..eb4aa0ef9 100644 --- a/source/items/ItemSword.h +++ b/source/items/ItemSword.h @@ -1,7 +1,9 @@ + #pragma once -#include "Item.h" -#include "../cWorld.h" -#include "../cPlayer.h" + +#include "ItemHandler.h" +#include "../World.h" +#include "../Player.h" class cItemSwordHandler : public cItemHandler { diff --git a/source/items/ItemWood.h b/source/items/ItemWood.h index 7e874efc2..d6f66d1c5 100644 --- a/source/items/ItemWood.h +++ b/source/items/ItemWood.h @@ -1,7 +1,7 @@ #pragma once -#include "Item.h" +#include "ItemHandler.h" -- cgit v1.2.3