Scripting WIP

This commit is contained in:
Perttu Ahola 2011-11-12 13:59:56 +02:00
parent 23adfff4fe
commit 38944467d3
9 changed files with 59 additions and 29 deletions

@ -166,7 +166,7 @@ end
function TNT:on_rightclick(clicker) function TNT:on_rightclick(clicker)
pos = self.object:getpos() pos = self.object:getpos()
pos = {x=pos.x, y=pos.y+0.1, z=pos.z} pos = {x=pos.x, y=pos.y+0.1, z=pos.z}
self.object:moveto(pos) self.object:moveto(pos, false)
end end
--[[ --[[
function TNT:on_rightclick(clicker) function TNT:on_rightclick(clicker)

@ -1368,9 +1368,11 @@ void LuaEntityCAO::processMessage(const std::string &data)
m_yaw = readF1000(is); m_yaw = readF1000(is);
// is_end_position (for interpolation) // is_end_position (for interpolation)
bool is_end_position = readU8(is); bool is_end_position = readU8(is);
// update_interval
float update_interval = readF1000(is);
if(do_interpolate) if(do_interpolate)
pos_translator.update(m_position, is_end_position); pos_translator.update(m_position, is_end_position, update_interval);
else else
pos_translator.init(m_position); pos_translator.init(m_position);
updateNodePos(); updateNodePos();

@ -35,10 +35,10 @@ struct SmoothTranslator
v3f vect_old; v3f vect_old;
v3f vect_show; v3f vect_show;
v3f vect_aim; v3f vect_aim;
bool aim_is_end;
f32 anim_counter; f32 anim_counter;
f32 anim_time; f32 anim_time;
f32 anim_time_counter; f32 anim_time_counter;
bool aim_is_end;
SmoothTranslator(): SmoothTranslator():
vect_old(0,0,0), vect_old(0,0,0),
@ -46,7 +46,8 @@ struct SmoothTranslator
vect_aim(0,0,0), vect_aim(0,0,0),
anim_counter(0), anim_counter(0),
anim_time(0), anim_time(0),
anim_time_counter(0) anim_time_counter(0),
aim_is_end(true)
{} {}
void init(v3f vect) void init(v3f vect)
@ -54,10 +55,10 @@ struct SmoothTranslator
vect_old = vect; vect_old = vect;
vect_show = vect; vect_show = vect;
vect_aim = vect; vect_aim = vect;
aim_is_end = true;
anim_counter = 0; anim_counter = 0;
anim_time = 0; anim_time = 0;
anim_time_counter = 0; anim_time_counter = 0;
aim_is_end = true;
} }
void sharpen() void sharpen()
@ -65,15 +66,19 @@ struct SmoothTranslator
init(vect_show); 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; aim_is_end = is_end_position;
vect_old = vect_show; vect_old = vect_show;
vect_aim = vect_new; vect_aim = vect_new;
if(anim_time < 0.001 || anim_time > 1.0) if(update_interval > 0){
anim_time = anim_time_counter; anim_time = update_interval;
else } else {
anim_time = anim_time * 0.9 + anim_time_counter * 0.1; 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_time_counter = 0;
anim_counter = 0; anim_counter = 0;
} }

@ -1510,7 +1510,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
m_yaw(0), m_yaw(0),
m_last_sent_yaw(0), m_last_sent_yaw(0),
m_last_sent_position(0,0,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 // Only register type if no environment supplied
if(env == NULL){ if(env == NULL){
@ -1572,17 +1573,16 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
if(send_recommended == false) if(send_recommended == false)
return; return;
bool move_end = false;
float minchange = 0.2*BS; float minchange = 0.2*BS;
if(m_last_sent_position_timer > 1.0){ if(m_last_sent_position_timer > 1.0){
minchange = 0.01*BS; minchange = 0.01*BS;
move_end = true;
} else if(m_last_sent_position_timer > 0.2){ } else if(m_last_sent_position_timer > 0.2){
minchange = 0.05*BS; minchange = 0.05*BS;
} }
if(m_base_position.getDistanceFrom(m_last_sent_position) > minchange float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
|| fabs(m_yaw - m_last_sent_yaw) > 1.0){ move_d += m_last_sent_move_precision;
sendPosition(true, move_end); 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); sendPosition(false, true);
} }
void LuaEntitySAO::moveTo(v3f pos) void LuaEntitySAO::moveTo(v3f pos, bool continuous)
{ {
m_base_position = pos; m_base_position = pos;
sendPosition(true, true); if(!continuous)
sendPosition(true, true);
} }
void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end) 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_yaw = m_yaw;
m_last_sent_position = m_base_position; 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); std::ostringstream os(std::ios::binary);
// command (0 = update position) // command (0 = update position)
@ -1673,6 +1678,8 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
writeF1000(os, m_yaw); writeF1000(os, m_yaw);
// is_end_position (for interpolation) // is_end_position (for interpolation)
writeU8(os, is_movement_end); writeU8(os, is_movement_end);
// update_interval (for interpolation)
writeF1000(os, update_interval);
// create message and add to list // create message and add to list
ActiveObjectMessage aom(getId(), false, os.str()); ActiveObjectMessage aom(getId(), false, os.str());

@ -217,7 +217,7 @@ public:
void rightClick(Player *player); void rightClick(Player *player);
void setPos(v3f pos); void setPos(v3f pos);
void moveTo(v3f pos); void moveTo(v3f pos, bool continuous);
private: private:
void sendPosition(bool do_interpolate, bool is_movement_end); void sendPosition(bool do_interpolate, bool is_movement_end);
@ -230,6 +230,7 @@ private:
float m_last_sent_yaw; float m_last_sent_yaw;
v3f m_last_sent_position; v3f m_last_sent_position;
float m_last_sent_position_timer; float m_last_sent_position_timer;
float m_last_sent_move_precision;
}; };
#endif #endif

@ -1153,9 +1153,9 @@ void ServerEnvironment::step(float dtime)
// This helps the objects to send data at the same time // This helps the objects to send data at the same time
bool send_recommended = false; bool send_recommended = false;
m_send_recommended_timer += dtime; 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; send_recommended = true;
} }

@ -145,6 +145,11 @@ public:
return m_lua; return m_lua;
} }
float getSendRecommendedInterval()
{
return 0.10;
}
/* /*
Save players Save players
*/ */

@ -368,6 +368,7 @@ private:
// Exported functions // Exported functions
// remove(self)
static int l_remove(lua_State *L) static int l_remove(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
@ -378,6 +379,8 @@ private:
return 0; return 0;
} }
// getpos(self)
// returns: {x=num, y=num, z=num}
static int l_getpos(lua_State *L) static int l_getpos(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
@ -394,6 +397,7 @@ private:
return 1; return 1;
} }
// setpos(self, pos)
static int l_setpos(lua_State *L) static int l_setpos(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
@ -407,6 +411,7 @@ private:
return 0; return 0;
} }
// moveto(self, pos, continuous=false)
static int l_moveto(lua_State *L) static int l_moveto(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
@ -415,8 +420,10 @@ private:
if(co == NULL) return 0; if(co == NULL) return 0;
// pos // pos
v3f pos = readFloatPos(L, 2); v3f pos = readFloatPos(L, 2);
// continuous
bool continuous = lua_toboolean(L, 3);
// Do it // Do it
co->moveTo(pos); co->moveTo(pos, continuous);
return 0; return 0;
} }

@ -71,8 +71,11 @@ public:
/* /*
Some more dynamic interface Some more dynamic interface
*/ */
virtual void setPos(v3f pos){ setBasePosition(pos); } virtual void setPos(v3f pos)
virtual void moveTo(v3f pos){ setBasePosition(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. Step object in time.