LuaEntity armor groups
authorPerttu Ahola <celeron55@gmail.com>
Fri, 9 Mar 2012 18:46:56 +0000 (20:46 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sat, 10 Mar 2012 09:28:13 +0000 (11:28 +0200)
data/mods/default/init.lua
data/mods/experimental/init.lua
src/content_cao.cpp
src/content_sao.cpp
src/content_sao.h
src/luaentity_common.h
src/scriptapi.cpp

index bcd633b3432612cc9b4b97c048d7f3acfadb876c..5ffed25ff63b519c73417ccf42806ff08aeef9be 100644 (file)
 -- -           select_horiz_by_yawpitch=false)
 -- - ^ Select sprite from spritesheet with optional animation and DM-style
 -- -   texture selection based on yaw relative to camera
+-- - set_armor_groups({group1=rating, group2=rating, ...})
 -- - get_entity_name() (DEPRECATED: Will be removed in a future version)
 -- - get_luaentity()
 -- Player-only: (no-op for other objects)
index ddc89f92c713f171299b20e2133451deab0b0587..c37488d0425bd059cd424e8c63ed6786f26db3aa 100644 (file)
@@ -330,6 +330,7 @@ function TNT:on_activate(staticdata)
        self.object:setvelocity({x=0, y=4, z=0})
        self.object:setacceleration({x=0, y=-10, z=0})
        self.object:settexturemod("^[brighten")
+       self.object:set_armor_groups({foo=1,bar=2})
 end
 
 -- Called periodically
@@ -355,7 +356,7 @@ function TNT:on_punch(hitter)
        if self.health <= 0 then
                self.object:remove()
                hitter:get_inventory():add_item("main", "experimental:tnt")
-               hitter:set_hp(hitter:get_hp() - 1)
+               --hitter:set_hp(hitter:get_hp() - 1)
        end
 end
 
index 836f719a3352bafb9ee6f63ddc39cb10b0624265..33079fd118513213bd30ff91b11ac026d5bc2057 100644 (file)
@@ -240,6 +240,7 @@ private:
        int m_anim_num_frames;
        float m_anim_framelength;
        float m_anim_timer;
+       ItemGroupList m_armor_groups;
 
 public:
        LuaEntityCAO(IGameDef *gamedef, ClientEnvironment *env):
@@ -594,14 +595,21 @@ public:
                        m_hp = result_hp;
                        // TODO: Execute defined fast response
                }
+               else if(cmd == LUAENTITY_CMD_UPDATE_ARMOR_GROUPS)
+               {
+                       m_armor_groups.clear();
+                       int armor_groups_size = readU16(is);
+                       for(int i=0; i<armor_groups_size; i++){
+                               std::string name = deSerializeString(is);
+                               int rating = readS16(is);
+                               m_armor_groups[name] = rating;
+                       }
+               }
        }
        
        bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
                        float time_from_last_punch=1000000)
        {
-               // TODO: Transfer this from the server
-               ItemGroupList m_armor_groups;
-               
                assert(punchitem);
                const ToolCapabilities *toolcap =
                                &punchitem->getToolCapabilities(m_gamedef->idef());
@@ -613,7 +621,6 @@ public:
                
                if(result.did_punch)
                {
-                       // TODO: Decrease hp by 
                        if(result.damage < m_hp)
                                m_hp -= result.damage;
                        else
@@ -623,6 +630,19 @@ public:
                
                return false;
        }
+       
+       std::string debugInfoText()
+       {
+               std::ostringstream os(std::ios::binary);
+               os<<"LuaEntityCAO \n";
+               os<<"armor={";
+               for(ItemGroupList::const_iterator i = m_armor_groups.begin();
+                               i != m_armor_groups.end(); i++){
+                       os<<i->first<<"="<<i->second<<", ";
+               }
+               os<<"}";
+               return os.str();
+       }
 };
 
 // Prototype
index 0c105bb0f69d4ff4f270e8a520adb3f08d2e4d4a..02a4eb1eb14d98b6722db38ae6e4a7329497db54 100644 (file)
@@ -354,7 +354,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
        m_last_sent_position(0,0,0),
        m_last_sent_velocity(0,0,0),
        m_last_sent_position_timer(0),
-       m_last_sent_move_precision(0)
+       m_last_sent_move_precision(0),
+       m_armor_groups_sent(false)
 {
        // Only register type if no environment supplied
        if(env == NULL){
@@ -475,6 +476,21 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
                        fabs(m_yaw - m_last_sent_yaw) > 1.0){
                sendPosition(true, false);
        }
+
+       if(m_armor_groups_sent == false){
+               m_armor_groups_sent = true;
+               std::ostringstream os(std::ios::binary);
+               writeU8(os, LUAENTITY_CMD_UPDATE_ARMOR_GROUPS);
+               writeU16(os, m_armor_groups.size());
+               for(ItemGroupList::const_iterator i = m_armor_groups.begin();
+                               i != m_armor_groups.end(); i++){
+                       os<<serializeString(i->first);
+                       writeS16(os, i->second);
+               }
+               // create message and add to list
+               ActiveObjectMessage aom(getId(), true, os.str());
+               m_messages_out.push_back(aom);
+       }
 }
 
 std::string LuaEntitySAO::getClientInitializationData()
@@ -685,6 +701,12 @@ std::string LuaEntitySAO::getName()
        return m_init_name;
 }
 
+void LuaEntitySAO::setArmorGroups(const ItemGroupList &armor_groups)
+{
+       m_armor_groups = armor_groups;
+       m_armor_groups_sent = false;
+}
+
 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
 {
        m_last_sent_move_precision = m_base_position.getDistanceFrom(
index 507631d2a37b2ee87a8846b1fb424c830f727389..53e701d8c3313c22d095fced8dc5217ee30fd5cc 100644 (file)
@@ -71,6 +71,7 @@ public:
        void setSprite(v2s16 p, int num_frames, float framelength,
                        bool select_horiz_by_yawpitch);
        std::string getName();
+       void setArmorGroups(const ItemGroupList &armor_groups);
 private:
        void sendPosition(bool do_interpolate, bool is_movement_end);
 
@@ -90,6 +91,7 @@ private:
        v3f m_last_sent_velocity;
        float m_last_sent_position_timer;
        float m_last_sent_move_precision;
+       bool m_armor_groups_sent;
 };
 
 #endif
index b6366382859e4a13f355cc3208ed1f668312bc85..d12ec9f0ef1bc33fbcba279867323b9a6937630e 100644 (file)
@@ -47,6 +47,7 @@ struct LuaEntityProperties
 #define LUAENTITY_CMD_SET_TEXTURE_MOD 1
 #define LUAENTITY_CMD_SET_SPRITE 2
 #define LUAENTITY_CMD_PUNCHED 3
+#define LUAENTITY_CMD_UPDATE_ARMOR_GROUPS 4
 
 #endif
 
index c3059ec55d2e81addec12e679c9a0c485770c5a1..541baa61c0b860fc47d30a4721d54d3e75631f82 100644 (file)
@@ -2538,6 +2538,19 @@ private:
                return 0;
        }
 
+       // set_armor_groups(self, groups)
+       static int l_set_armor_groups(lua_State *L)
+       {
+               ObjectRef *ref = checkobject(L, 1);
+               LuaEntitySAO *co = getluaobject(ref);
+               if(co == NULL) return 0;
+               // Do it
+               ItemGroupList groups;
+               read_groups(L, 2, groups);
+               co->setArmorGroups(groups);
+               return 0;
+       }
+
        // DEPRECATED
        // get_entity_name(self)
        static int l_get_entity_name(lua_State *L)
@@ -2700,6 +2713,7 @@ const luaL_reg ObjectRef::methods[] = {
        method(ObjectRef, getyaw),
        method(ObjectRef, settexturemod),
        method(ObjectRef, setsprite),
+       method(ObjectRef, set_armor_groups),
        method(ObjectRef, get_entity_name),
        method(ObjectRef, get_luaentity),
        // Player-only