Scripting WIP
authorPerttu Ahola <celeron55@gmail.com>
Sat, 12 Nov 2011 11:59:56 +0000 (13:59 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:39 +0000 (19:13 +0200)
data/scripts/default.lua
src/content_cao.cpp
src/content_cao.h
src/content_sao.cpp
src/content_sao.h
src/environment.cpp
src/environment.h
src/scriptapi.cpp
src/serverobject.h

index c355583415bd944cc7bbd65e7a820c86cf99a7a5..7d3242837895c8c28e1a4cddab392ff8c55109e0 100644 (file)
@@ -166,7 +166,7 @@ end
 function TNT:on_rightclick(clicker)
        pos = self.object:getpos()
        pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
-       self.object:moveto(pos)
+       self.object:moveto(pos, false)
 end
 --[[
 function TNT:on_rightclick(clicker)
index 0db9803cce71659f5c7556f910bca8ee6ec29dd3..1f4f6fbe83d0d1da8781bca5cec6bfde99c214cd 100644 (file)
@@ -1368,9 +1368,11 @@ void LuaEntityCAO::processMessage(const std::string &data)
                m_yaw = readF1000(is);
                // is_end_position (for interpolation)
                bool is_end_position = readU8(is);
+               // update_interval
+               float update_interval = readF1000(is);
                
                if(do_interpolate)
-                       pos_translator.update(m_position, is_end_position);
+                       pos_translator.update(m_position, is_end_position, update_interval);
                else
                        pos_translator.init(m_position);
                updateNodePos();
index 72cb94eb71d35960691268691e95bcf696718cb7..5310127a6147556ad8231fe8c514e490ec227db0 100644 (file)
@@ -35,10 +35,10 @@ struct SmoothTranslator
        v3f vect_old;
        v3f vect_show;
        v3f vect_aim;
-       bool aim_is_end;
        f32 anim_counter;
        f32 anim_time;
        f32 anim_time_counter;
+       bool aim_is_end;
 
        SmoothTranslator():
                vect_old(0,0,0),
@@ -46,7 +46,8 @@ struct SmoothTranslator
                vect_aim(0,0,0),
                anim_counter(0),
                anim_time(0),
-               anim_time_counter(0)
+               anim_time_counter(0),
+               aim_is_end(true)
        {}
 
        void init(v3f vect)
@@ -54,10 +55,10 @@ struct SmoothTranslator
                vect_old = vect;
                vect_show = vect;
                vect_aim = vect;
-               aim_is_end = true;
                anim_counter = 0;
                anim_time = 0;
                anim_time_counter = 0;
+               aim_is_end = true;
        }
 
        void sharpen()
@@ -65,15 +66,19 @@ struct SmoothTranslator
                init(vect_show);
        }
 
-       void update(v3f vect_new, bool is_end_position=false)
+       void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
        {
                aim_is_end = is_end_position;
                vect_old = vect_show;
                vect_aim = vect_new;
-               if(anim_time < 0.001 || anim_time > 1.0)
-                       anim_time = anim_time_counter;
-               else
-                       anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
+               if(update_interval > 0){
+                       anim_time = update_interval;
+               } else {
+                       if(anim_time < 0.001 || anim_time > 1.0)
+                               anim_time = anim_time_counter;
+                       else
+                               anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
+               }
                anim_time_counter = 0;
                anim_counter = 0;
        }
index 6d9d5a9482f6b7802fa13b3d3f87638b7840e88c..b053274f15984ef16d4a87eb866cc7a7dac8bf29 100644 (file)
@@ -1510,7 +1510,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
        m_yaw(0),
        m_last_sent_yaw(0),
        m_last_sent_position(0,0,0),
-       m_last_sent_position_timer(0)
+       m_last_sent_position_timer(0),
+       m_last_sent_move_precision(0)
 {
        // Only register type if no environment supplied
        if(env == NULL){
@@ -1572,17 +1573,16 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
        if(send_recommended == false)
                return;
        
-       bool move_end = false;
        float minchange = 0.2*BS;
        if(m_last_sent_position_timer > 1.0){
                minchange = 0.01*BS;
-               move_end = true;
        } else if(m_last_sent_position_timer > 0.2){
                minchange = 0.05*BS;
        }
-       if(m_base_position.getDistanceFrom(m_last_sent_position) > minchange
-                       || fabs(m_yaw - m_last_sent_yaw) > 1.0){
-               sendPosition(true, move_end);
+       float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
+       move_d += m_last_sent_move_precision;
+       if(move_d > minchange || fabs(m_yaw - m_last_sent_yaw) > 1.0){
+               sendPosition(true, false);
        }
 }
 
@@ -1649,17 +1649,22 @@ void LuaEntitySAO::setPos(v3f pos)
        sendPosition(false, true);
 }
 
-void LuaEntitySAO::moveTo(v3f pos)
+void LuaEntitySAO::moveTo(v3f pos, bool continuous)
 {
        m_base_position = pos;
-       sendPosition(true, true);
+       if(!continuous)
+               sendPosition(true, true);
 }
 
 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
 {
+       m_last_sent_move_precision = m_base_position.getDistanceFrom(
+                       m_last_sent_position);
+       m_last_sent_position_timer = 0;
        m_last_sent_yaw = m_yaw;
        m_last_sent_position = m_base_position;
-       m_last_sent_position_timer = 0;
+
+       float update_interval = m_env->getSendRecommendedInterval();
 
        std::ostringstream os(std::ios::binary);
        // command (0 = update position)
@@ -1673,6 +1678,8 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
        writeF1000(os, m_yaw);
        // is_end_position (for interpolation)
        writeU8(os, is_movement_end);
+       // update_interval (for interpolation)
+       writeF1000(os, update_interval);
 
        // create message and add to list
        ActiveObjectMessage aom(getId(), false, os.str());
index 2316aba8dc83a20500a93ccff2937feda1320631..103b4cc8eb6785ba2bd6df87ab38265299d10a2f 100644 (file)
@@ -217,7 +217,7 @@ public:
        void rightClick(Player *player);
 
        void setPos(v3f pos);
-       void moveTo(v3f pos);
+       void moveTo(v3f pos, bool continuous);
 private:
        void sendPosition(bool do_interpolate, bool is_movement_end);
 
@@ -230,6 +230,7 @@ private:
        float m_last_sent_yaw;
        v3f m_last_sent_position;
        float m_last_sent_position_timer;
+       float m_last_sent_move_precision;
 };
 
 #endif
index ed45cee69be7b1a617ae3fa41ab7dfd17afb6b1b..e75e967c28dc4fee98e6e89646f965f4191b14d7 100644 (file)
@@ -1153,9 +1153,9 @@ void ServerEnvironment::step(float dtime)
                // This helps the objects to send data at the same time
                bool send_recommended = false;
                m_send_recommended_timer += dtime;
-               if(m_send_recommended_timer > 0.10)
+               if(m_send_recommended_timer > getSendRecommendedInterval())
                {
-                       m_send_recommended_timer = 0;
+                       m_send_recommended_timer -= getSendRecommendedInterval();
                        send_recommended = true;
                }
 
index 0e0a5510e960120d12fe5f8b6f9a33964fc6ba4f..7a4cc37777eec591a107ce99571cd171bf51c2e6 100644 (file)
@@ -145,6 +145,11 @@ public:
                return m_lua;
        }
 
+       float getSendRecommendedInterval()
+       {
+               return 0.10;
+       }
+
        /*
                Save players
        */
index dc9c832d2f07ffd8488ed527cfe8ed189bd31fe3..428810117b8931aef24880b8a29ec3b5722e7555 100644 (file)
@@ -367,7 +367,8 @@ private:
        }
        
        // Exported functions
-
+       
+       // remove(self)
        static int l_remove(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
@@ -377,7 +378,9 @@ private:
                co->m_removed = true;
                return 0;
        }
-
+       
+       // getpos(self)
+       // returns: {x=num, y=num, z=num}
        static int l_getpos(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
@@ -393,7 +396,8 @@ private:
                lua_setfield(L, -2, "z");
                return 1;
        }
-
+       
+       // setpos(self, pos)
        static int l_setpos(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
@@ -406,7 +410,8 @@ private:
                co->setPos(pos);
                return 0;
        }
-
+       
+       // moveto(self, pos, continuous=false)
        static int l_moveto(lua_State *L)
        {
                ObjectRef *ref = checkobject(L, 1);
@@ -415,8 +420,10 @@ private:
                if(co == NULL) return 0;
                // pos
                v3f pos = readFloatPos(L, 2);
+               // continuous
+               bool continuous = lua_toboolean(L, 3);
                // Do it
-               co->moveTo(pos);
+               co->moveTo(pos, continuous);
                return 0;
        }
 
index fce72ac640b130df366b0d7cec5abd7e45c1b2e3..cd98ba0aa3e232197c6e5596def37b1aede5a1f4 100644 (file)
@@ -71,8 +71,11 @@ public:
        /*
                Some more dynamic interface
        */
-       virtual void setPos(v3f pos){ setBasePosition(pos); }
-       virtual void moveTo(v3f pos){ setBasePosition(pos); }
+       virtual void setPos(v3f pos)
+       { setBasePosition(pos); }
+       // continuous: if true, object does not stop immediately at pos
+       virtual void moveTo(v3f pos, bool continuous)
+       { setBasePosition(pos); }
 
        /*
                Step object in time.