From 38944467d31bbfa8a98008962f147cbc3c73f507 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 12 Nov 2011 13:59:56 +0200 Subject: [PATCH] Scripting WIP --- data/scripts/default.lua | 2 +- src/content_cao.cpp | 4 +++- src/content_cao.h | 21 +++++++++++++-------- src/content_sao.cpp | 25 ++++++++++++++++--------- src/content_sao.h | 3 ++- src/environment.cpp | 4 ++-- src/environment.h | 5 +++++ src/scriptapi.cpp | 17 ++++++++++++----- src/serverobject.h | 7 +++++-- 9 files changed, 59 insertions(+), 29 deletions(-) diff --git a/data/scripts/default.lua b/data/scripts/default.lua index c35558341..7d3242837 100644 --- a/data/scripts/default.lua +++ b/data/scripts/default.lua @@ -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) diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 0db9803cc..1f4f6fbe8 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -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(); diff --git a/src/content_cao.h b/src/content_cao.h index 72cb94eb7..5310127a6 100644 --- a/src/content_cao.h +++ b/src/content_cao.h @@ -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; } diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 6d9d5a948..b053274f1 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -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()); diff --git a/src/content_sao.h b/src/content_sao.h index 2316aba8d..103b4cc8e 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -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 diff --git a/src/environment.cpp b/src/environment.cpp index ed45cee69..e75e967c2 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -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; } diff --git a/src/environment.h b/src/environment.h index 0e0a5510e..7a4cc3777 100644 --- a/src/environment.h +++ b/src/environment.h @@ -145,6 +145,11 @@ public: return m_lua; } + float getSendRecommendedInterval() + { + return 0.10; + } + /* Save players */ diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index dc9c832d2..428810117 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -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; } diff --git a/src/serverobject.h b/src/serverobject.h index fce72ac64..cd98ba0aa 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -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.