From 6a76c226e10e92c3e3339096f07f8ab065e2098b Mon Sep 17 00:00:00 2001 From: Kahrl Date: Thu, 12 Jan 2012 06:10:39 +0100 Subject: The huge item definition and item namespace unification patch (itemdef), see http://c55.me/minetest/wiki/doku.php?id=changes:itemdef --- src/content_sao.cpp | 154 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 58 deletions(-) (limited to 'src/content_sao.cpp') diff --git a/src/content_sao.cpp b/src/content_sao.cpp index f195b16bd..02be64c64 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -24,8 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "main.h" // For g_profiler #include "profiler.h" #include "serialization.h" // For compressZlib -#include "materials.h" // For MaterialProperties -#include "tooldef.h" // ToolDiggingProperties +#include "materials.h" // For MaterialProperties and ToolDiggingProperties +#include "gamedef.h" core::map ServerActiveObject::m_types; @@ -114,9 +114,10 @@ void TestSAO::step(float dtime, bool send_recommended) ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), ""); ItemSAO::ItemSAO(ServerEnvironment *env, v3f pos, - const std::string inventorystring): + const std::string itemstring): ServerActiveObject(env, pos), - m_inventorystring(inventorystring), + m_itemstring(itemstring), + m_itemstring_changed(false), m_speed_f(0,0,0), m_last_sent_position(0,0,0) { @@ -134,10 +135,10 @@ ServerActiveObject* ItemSAO::create(ServerEnvironment *env, v3f pos, // check if version is supported if(version != 0) return NULL; - std::string inventorystring = deSerializeString(is); + std::string itemstring = deSerializeString(is); infostream<<"ItemSAO::create(): Creating item \"" - <getGameDef(); - InventoryItem *item = InventoryItem::deSerialize(is, gamedef); - infostream<<__FUNCTION_NAME<<": m_inventorystring=\"" - < item="<getGameDef()->idef(); + ItemStack item; + item.deSerialize(m_itemstring, idef); + infostream<<__FUNCTION_NAME<<": m_itemstring=\""< item=\""<addToInventory(item); - if(fits) + // Allow removing items in creative mode + if(g_settings->getBool("creative_mode") == true) + { m_removed = true; - else - delete item; + return; + } + + ItemStack item = createItemStack(); + Inventory *inv = puncher->getInventory(); + if(inv != NULL) + { + std::string wieldlist = puncher->getWieldList(); + ItemStack leftover = inv->addItem(wieldlist, item); + puncher->setInventoryModified(); + if(leftover.empty()) + { + m_removed = true; + } + else + { + m_itemstring = leftover.getItemString(); + m_itemstring_changed = true; + } + } } /* @@ -436,14 +452,24 @@ std::string RatSAO::getStaticData() void RatSAO::punch(ServerActiveObject *puncher, float time_from_last_punch) { - std::istringstream is("CraftItem rat 1", std::ios_base::binary); - IGameDef *gamedef = m_env->getGameDef(); - InventoryItem *item = InventoryItem::deSerialize(is, gamedef); - bool fits = puncher->addToInventory(item); - if(fits) + // Allow removing rats in creative mode + if(g_settings->getBool("creative_mode") == true) + { m_removed = true; - else - delete item; + return; + } + + IItemDefManager *idef = m_env->getGameDef()->idef(); + ItemStack item("rat", 1, 0, "", idef); + Inventory *inv = puncher->getInventory(); + if(inv != NULL) + { + std::string wieldlist = puncher->getWieldList(); + ItemStack leftover = inv->addItem(wieldlist, item); + puncher->setInventoryModified(); + if(leftover.empty()) + m_removed = true; + } } /* @@ -703,14 +729,20 @@ void Oerkki1SAO::punch(ServerActiveObject *puncher, float time_from_last_punch) mp.crackiness = -1.0; mp.cuttability = 1.0; - ToolDiggingProperties tp; - puncher->getWieldDiggingProperties(&tp); + IItemDefManager *idef = m_env->getGameDef()->idef(); + ItemStack punchitem = puncher->getWieldedItem(); + ToolDiggingProperties tp = + punchitem.getToolDiggingProperties(idef); HittingProperties hitprop = getHittingProperties(&mp, &tp, time_from_last_punch); doDamage(hitprop.hp); - puncher->damageWieldedItem(hitprop.wear); + if(g_settings->getBool("creative_mode") == false) + { + punchitem.addWear(hitprop.wear, idef); + puncher->setWieldedItem(punchitem); + } } void Oerkki1SAO::doDamage(u16 d) @@ -1393,14 +1425,20 @@ void MobV2SAO::punch(ServerActiveObject *puncher, float time_from_last_punch) mp.crackiness = -1.0; mp.cuttability = 1.0; - ToolDiggingProperties tp; - puncher->getWieldDiggingProperties(&tp); + IItemDefManager *idef = m_env->getGameDef()->idef(); + ItemStack punchitem = puncher->getWieldedItem(); + ToolDiggingProperties tp = + punchitem.getToolDiggingProperties(idef); HittingProperties hitprop = getHittingProperties(&mp, &tp, time_from_last_punch); doDamage(hitprop.hp); - puncher->damageWieldedItem(hitprop.wear); + if(g_settings->getBool("creative_mode") == false) + { + punchitem.addWear(hitprop.wear, idef); + puncher->setWieldedItem(punchitem); + } } bool MobV2SAO::isPeaceful() -- cgit v1.2.3