]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/content_sao.cpp
Add time_from_last_punch to Lua API
[dragonfireclient.git] / src / content_sao.cpp
index 5d63b295e8b82dbe729efa7449de63b6ef3324cf..171e315af87ea40791c48da49603440eb8a3e9e6 100644 (file)
@@ -24,6 +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
 
 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
 
@@ -241,7 +243,7 @@ InventoryItem * ItemSAO::createInventoryItem()
        }
 }
 
-void ItemSAO::punch(ServerActiveObject *puncher)
+void ItemSAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
 {
        InventoryItem *item = createInventoryItem();
        bool fits = puncher->addToInventory(item);
@@ -251,24 +253,6 @@ void ItemSAO::punch(ServerActiveObject *puncher)
                delete item;
 }
 
-void ItemSAO::rightClick(ServerActiveObject *clicker)
-{
-       infostream<<__FUNCTION_NAME<<std::endl;
-       InventoryItem *item = createInventoryItem();
-       if(item == NULL)
-               return;
-       
-       bool to_be_deleted = item->use(m_env, clicker);
-
-       if(to_be_deleted)
-               m_removed = true;
-       else
-               // Reflect changes to the item here
-               m_inventorystring = item->getItemString();
-       
-       delete item; // Delete temporary item
-}
-
 /*
        RatSAO
 */
@@ -450,7 +434,7 @@ std::string RatSAO::getStaticData()
        return os.str();
 }
 
-void RatSAO::punch(ServerActiveObject *puncher)
+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();
@@ -705,36 +689,28 @@ std::string Oerkki1SAO::getStaticData()
        return os.str();
 }
 
-void Oerkki1SAO::punch(ServerActiveObject *puncher)
+void Oerkki1SAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
 {
+       if(!puncher)
+               return;
+       
        v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
+       m_speed_f += dir*12*BS;
 
-       std::string toolname = "";
-       InventoryItem *item = puncher->getWieldedItem();
-       if(item && (std::string)item->getName() == "ToolItem"){
-               ToolItem *titem = (ToolItem*)item;
-               toolname = titem->getToolName();
-       }
+       // "Material" properties of an oerkki
+       MaterialProperties mp;
+       mp.diggability = DIGGABLE_NORMAL;
+       mp.crackiness = -1.0;
+       mp.cuttability = 1.0;
 
-       m_speed_f += dir*12*BS;
+       ToolDiggingProperties tp;
+       puncher->getWieldDiggingProperties(&tp);
 
-       u16 amount = 5;
-       /* See tool names in inventory.h */
-       if(toolname == "WSword")
-               amount = 10;
-       if(toolname == "STSword")
-               amount = 12;
-       if(toolname == "SteelSword")
-               amount = 16;
-       if(toolname == "STAxe")
-               amount = 7;
-       if(toolname == "SteelAxe")
-               amount = 9;
-       if(toolname == "SteelPick")
-               amount = 7;
-       doDamage(amount);
-       
-       puncher->damageWieldedItem(65536/100);
+       HittingProperties hitprop = getHittingProperties(&mp, &tp,
+                       time_from_last_punch);
+
+       doDamage(hitprop.hp);
+       puncher->damageWieldedItem(hitprop.wear);
 }
 
 void Oerkki1SAO::doDamage(u16 d)
@@ -1383,25 +1359,20 @@ void MobV2SAO::step(float dtime, bool send_recommended)
        }
 }
 
-void MobV2SAO::punch(ServerActiveObject *puncher)
+void MobV2SAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
 {
+       if(!puncher)
+               return;
+       
        v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
 
-       std::string toolname = "";
-       InventoryItem *item = puncher->getWieldedItem();
-       if(item && (std::string)item->getName() == "ToolItem"){
-               ToolItem *titem = (ToolItem*)item;
-               toolname = titem->getToolName();
-       }
-       
        // A quick hack; SAO description is player name for player
        std::string playername = puncher->getDescription();
 
        Map *map = &m_env->getMap();
        
        actionstream<<playername<<" punches mob id="<<m_id
-                       <<" with a \""<<toolname<<"\" at "
-                       <<PP(m_base_position/BS)<<std::endl;
+                       <<" at "<<PP(m_base_position/BS)<<std::endl;
 
        m_disturb_timer = 0;
        m_disturbing_player = playername;
@@ -1423,23 +1394,21 @@ void MobV2SAO::punch(ServerActiveObject *puncher)
        }
        sendPosition();
        
-       u16 amount = 2;
-       /* See tool names in inventory.h */
-       if(toolname == "WSword")
-               amount = 4;
-       if(toolname == "STSword")
-               amount = 6;
-       if(toolname == "SteelSword")
-               amount = 8;
-       if(toolname == "STAxe")
-               amount = 3;
-       if(toolname == "SteelAxe")
-               amount = 4;
-       if(toolname == "SteelPick")
-               amount = 3;
-       doDamage(amount);
-       
-       puncher->damageWieldedItem(65536/100);
+
+       // "Material" properties of the MobV2
+       MaterialProperties mp;
+       mp.diggability = DIGGABLE_NORMAL;
+       mp.crackiness = -1.0;
+       mp.cuttability = 1.0;
+
+       ToolDiggingProperties tp;
+       puncher->getWieldDiggingProperties(&tp);
+
+       HittingProperties hitprop = getHittingProperties(&mp, &tp,
+                       time_from_last_punch);
+
+       doDamage(hitprop.hp);
+       puncher->damageWieldedItem(hitprop.wear);
 }
 
 bool MobV2SAO::isPeaceful()
@@ -1704,12 +1673,12 @@ InventoryItem* LuaEntitySAO::createPickedUpItem()
        return item;
 }
 
-void LuaEntitySAO::punch(ServerActiveObject *puncher)
+void LuaEntitySAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
 {
        if(!m_registered)
                return;
        lua_State *L = m_env->getLua();
-       scriptapi_luaentity_punch(L, m_id, puncher);
+       scriptapi_luaentity_punch(L, m_id, puncher, time_from_last_punch);
 }
 
 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
@@ -1748,6 +1717,39 @@ void LuaEntitySAO::setAcceleration(v3f acceleration)
        m_acceleration = acceleration;
 }
 
+v3f LuaEntitySAO::getAcceleration()
+{
+       return m_acceleration;
+}
+
+void LuaEntitySAO::setTextureMod(const std::string &mod)
+{
+       std::ostringstream os(std::ios::binary);
+       // command (1 = set texture modification)
+       writeU8(os, 1);
+       // parameters
+       os<<serializeString(mod);
+       // create message and add to list
+       ActiveObjectMessage aom(getId(), false, os.str());
+       m_messages_out.push_back(aom);
+}
+
+void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
+               bool select_horiz_by_yawpitch)
+{
+       std::ostringstream os(std::ios::binary);
+       // command (2 = set sprite)
+       writeU8(os, 2);
+       // parameters
+       writeV2S16(os, p);
+       writeU16(os, num_frames);
+       writeF1000(os, framelength);
+       writeU8(os, select_horiz_by_yawpitch);
+       // create message and add to list
+       ActiveObjectMessage aom(getId(), false, os.str());
+       m_messages_out.push_back(aom);
+}
+
 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
 {
        m_last_sent_move_precision = m_base_position.getDistanceFrom(
@@ -1784,4 +1786,3 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
        m_messages_out.push_back(aom);
 }
 
-