3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_sao.h"
21 #include "collision.h"
22 #include "environment.h"
24 #include "main.h" // For g_profiler
26 #include "serialization.h" // For compressZlib
27 #include "tool.h" // For ToolCapabilities
30 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
36 class DummyLoadSAO : public ServerActiveObject
39 DummyLoadSAO(ServerEnvironment *env, v3f pos, u8 type):
40 ServerActiveObject(env, pos)
42 ServerActiveObject::registerType(type, create);
44 // Pretend to be the test object (to fool the client)
46 { return ACTIVEOBJECT_TYPE_TEST; }
47 // And never save to disk
48 bool isStaticAllowed() const
51 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
52 const std::string &data)
54 return new DummyLoadSAO(env, pos, 0);
57 void step(float dtime, bool send_recommended)
60 infostream<<"DummyLoadSAO step"<<std::endl;
66 // Prototype (registers item for deserialization)
67 DummyLoadSAO proto1_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_RAT);
68 DummyLoadSAO proto2_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_OERKKI1);
69 DummyLoadSAO proto3_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_FIREFLY);
70 DummyLoadSAO proto4_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_MOBV2);
76 class TestSAO : public ServerActiveObject
79 TestSAO(ServerEnvironment *env, v3f pos):
80 ServerActiveObject(env, pos),
84 ServerActiveObject::registerType(getType(), create);
87 { return ACTIVEOBJECT_TYPE_TEST; }
89 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
90 const std::string &data)
92 return new TestSAO(env, pos);
95 void step(float dtime, bool send_recommended)
104 m_base_position.Y += dtime * BS * 2;
105 if(m_base_position.Y > 8*BS)
106 m_base_position.Y = 2*BS;
108 if(send_recommended == false)
118 data += itos(0); // 0 = position
120 data += itos(m_base_position.X);
122 data += itos(m_base_position.Y);
124 data += itos(m_base_position.Z);
126 ActiveObjectMessage aom(getId(), false, data);
127 m_messages_out.push_back(aom);
136 // Prototype (registers item for deserialization)
137 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
143 class ItemSAO : public ServerActiveObject
147 { return ACTIVEOBJECT_TYPE_ITEM; }
149 float getMinimumSavedMovement()
152 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
153 const std::string &data)
155 std::istringstream is(data, std::ios::binary);
160 // check if version is supported
163 std::string itemstring = deSerializeString(is);
164 infostream<<"create(): Creating item \""
165 <<itemstring<<"\""<<std::endl;
166 return new ItemSAO(env, pos, itemstring);
169 ItemSAO(ServerEnvironment *env, v3f pos,
170 const std::string itemstring):
171 ServerActiveObject(env, pos),
172 m_itemstring(itemstring),
173 m_itemstring_changed(false),
175 m_last_sent_position(0,0,0)
177 ServerActiveObject::registerType(getType(), create);
180 void step(float dtime, bool send_recommended)
182 ScopeProfiler sp2(g_profiler, "step avg", SPT_AVG);
186 const float interval = 0.2;
187 if(m_move_interval.step(dtime, interval)==false)
191 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
192 collisionMoveResult moveresult;
194 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
195 // Maximum movement without glitches
196 f32 pos_max_d = BS*0.25;
198 if(m_speed_f.getLength()*dtime > pos_max_d)
199 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
200 v3f pos_f = getBasePosition();
201 v3f pos_f_old = pos_f;
202 IGameDef *gamedef = m_env->getGameDef();
203 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
204 pos_max_d, box, dtime, pos_f, m_speed_f);
206 if(send_recommended == false)
209 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
211 setBasePosition(pos_f);
212 m_last_sent_position = pos_f;
214 std::ostringstream os(std::ios::binary);
215 // command (0 = update position)
218 writeV3F1000(os, m_base_position);
219 // create message and add to list
220 ActiveObjectMessage aom(getId(), false, os.str());
221 m_messages_out.push_back(aom);
223 if(m_itemstring_changed)
225 m_itemstring_changed = false;
227 std::ostringstream os(std::ios::binary);
228 // command (1 = update itemstring)
231 os<<serializeString(m_itemstring);
232 // create message and add to list
233 ActiveObjectMessage aom(getId(), false, os.str());
234 m_messages_out.push_back(aom);
238 std::string getClientInitializationData()
240 std::ostringstream os(std::ios::binary);
244 writeV3F1000(os, m_base_position);
246 os<<serializeString(m_itemstring);
250 std::string getStaticData()
252 infostream<<__FUNCTION_NAME<<std::endl;
253 std::ostringstream os(std::ios::binary);
257 os<<serializeString(m_itemstring);
261 ItemStack createItemStack()
264 IItemDefManager *idef = m_env->getGameDef()->idef();
266 item.deSerialize(m_itemstring, idef);
267 infostream<<__FUNCTION_NAME<<": m_itemstring=\""<<m_itemstring
268 <<"\" -> item=\""<<item.getItemString()<<"\""
272 catch(SerializationError &e)
274 infostream<<__FUNCTION_NAME<<": serialization error: "
275 <<"m_itemstring=\""<<m_itemstring<<"\""<<std::endl;
281 const ToolCapabilities *toolcap,
282 ServerActiveObject *puncher,
283 float time_from_last_punch)
285 // Directly delete item in creative mode
286 if(g_settings->getBool("creative_mode") == true)
292 // Take item into inventory
293 ItemStack item = createItemStack();
294 Inventory *inv = puncher->getInventory();
297 std::string wieldlist = puncher->getWieldList();
298 ItemStack leftover = inv->addItem(wieldlist, item);
299 puncher->setInventoryModified();
306 m_itemstring = leftover.getItemString();
307 m_itemstring_changed = true;
316 std::string m_itemstring;
317 bool m_itemstring_changed;
319 v3f m_last_sent_position;
320 IntervalLimiter m_move_interval;
323 // Prototype (registers item for deserialization)
324 ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), "");
326 ServerActiveObject* createItemSAO(ServerEnvironment *env, v3f pos,
327 const std::string itemstring)
329 return new ItemSAO(env, pos, itemstring);
336 #include "scriptapi.h"
337 #include "luaentity_common.h"
339 // Prototype (registers item for deserialization)
340 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
342 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
343 const std::string &name, const std::string &state):
344 ServerActiveObject(env, pos),
348 m_prop(new LuaEntityProperties),
351 m_acceleration(0,0,0),
354 m_last_sent_position(0,0,0),
355 m_last_sent_velocity(0,0,0),
356 m_last_sent_position_timer(0),
357 m_last_sent_move_precision(0),
358 m_armor_groups_sent(false)
360 // Only register type if no environment supplied
362 ServerActiveObject::registerType(getType(), create);
366 // Initialize something to armor groups
367 m_armor_groups["fleshy"] = 3;
368 m_armor_groups["snappy"] = 2;
371 LuaEntitySAO::~LuaEntitySAO()
374 lua_State *L = m_env->getLua();
375 scriptapi_luaentity_rm(L, m_id);
380 void LuaEntitySAO::addedToEnvironment()
382 ServerActiveObject::addedToEnvironment();
384 // Create entity from name and state
385 lua_State *L = m_env->getLua();
386 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str(),
387 m_init_state.c_str());
391 scriptapi_luaentity_get_properties(L, m_id, m_prop);
395 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
396 const std::string &data)
398 std::istringstream is(data, std::ios::binary);
400 u8 version = readU8(is);
406 // check if version is supported
408 name = deSerializeString(is);
409 state = deSerializeLongString(is);
411 else if(version == 1){
412 name = deSerializeString(is);
413 state = deSerializeLongString(is);
415 velocity = readV3F1000(is);
422 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
423 <<state<<"\")"<<std::endl;
424 LuaEntitySAO *sao = new LuaEntitySAO(env, pos, name, state);
426 sao->m_velocity = velocity;
431 void LuaEntitySAO::step(float dtime, bool send_recommended)
433 m_last_sent_position_timer += dtime;
435 if(m_prop->physical){
436 core::aabbox3d<f32> box = m_prop->collisionbox;
439 collisionMoveResult moveresult;
440 f32 pos_max_d = BS*0.25; // Distance per iteration
441 v3f p_pos = getBasePosition();
442 v3f p_velocity = m_velocity;
443 IGameDef *gamedef = m_env->getGameDef();
444 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
445 pos_max_d, box, dtime, p_pos, p_velocity);
447 setBasePosition(p_pos);
448 m_velocity = p_velocity;
450 m_velocity += dtime * m_acceleration;
452 m_base_position += dtime * m_velocity + 0.5 * dtime
453 * dtime * m_acceleration;
454 m_velocity += dtime * m_acceleration;
458 lua_State *L = m_env->getLua();
459 scriptapi_luaentity_step(L, m_id, dtime);
462 if(send_recommended == false)
465 // TODO: force send when acceleration changes enough?
466 float minchange = 0.2*BS;
467 if(m_last_sent_position_timer > 1.0){
469 } else if(m_last_sent_position_timer > 0.2){
472 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
473 move_d += m_last_sent_move_precision;
474 float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
475 if(move_d > minchange || vel_d > minchange ||
476 fabs(m_yaw - m_last_sent_yaw) > 1.0){
477 sendPosition(true, false);
480 if(m_armor_groups_sent == false){
481 m_armor_groups_sent = true;
482 std::ostringstream os(std::ios::binary);
483 writeU8(os, LUAENTITY_CMD_UPDATE_ARMOR_GROUPS);
484 writeU16(os, m_armor_groups.size());
485 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
486 i != m_armor_groups.end(); i++){
487 os<<serializeString(i->first);
488 writeS16(os, i->second);
490 // create message and add to list
491 ActiveObjectMessage aom(getId(), true, os.str());
492 m_messages_out.push_back(aom);
496 std::string LuaEntitySAO::getClientInitializationData()
498 std::ostringstream os(std::ios::binary);
502 writeV3F1000(os, m_base_position);
504 writeF1000(os, m_yaw);
508 std::ostringstream prop_os(std::ios::binary);
509 m_prop->serialize(prop_os);
510 os<<serializeLongString(prop_os.str());
515 std::string LuaEntitySAO::getStaticData()
517 infostream<<__FUNCTION_NAME<<std::endl;
518 std::ostringstream os(std::ios::binary);
522 os<<serializeString(m_init_name);
525 lua_State *L = m_env->getLua();
526 std::string state = scriptapi_luaentity_get_staticdata(L, m_id);
527 os<<serializeLongString(state);
529 os<<serializeLongString(m_init_state);
534 writeV3F1000(os, m_velocity);
536 writeF1000(os, m_yaw);
540 int LuaEntitySAO::punch(v3f dir,
541 const ToolCapabilities *toolcap,
542 ServerActiveObject *puncher,
543 float time_from_last_punch)
546 // Delete unknown LuaEntities when punched
551 ItemStack *punchitem = NULL;
552 ItemStack punchitem_static;
554 punchitem_static = puncher->getWieldedItem();
555 punchitem = &punchitem_static;
558 PunchDamageResult result = getPunchDamage(
562 time_from_last_punch);
566 actionstream<<getDescription()<<" punched by "
567 <<puncher->getDescription()<<", damage "<<result.damage
570 setHP(getHP() - result.damage);
573 std::ostringstream os(std::ios::binary);
575 writeU8(os, LUAENTITY_CMD_PUNCHED);
577 writeS16(os, result.damage);
579 writeS16(os, getHP());
580 // create message and add to list
581 ActiveObjectMessage aom(getId(), true, os.str());
582 m_messages_out.push_back(aom);
586 lua_State *L = m_env->getLua();
587 scriptapi_luaentity_punch(L, m_id, puncher,
588 time_from_last_punch, toolcap, dir);
593 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
597 lua_State *L = m_env->getLua();
598 scriptapi_luaentity_rightclick(L, m_id, clicker);
601 void LuaEntitySAO::setHP(s16 hp)
607 s16 LuaEntitySAO::getHP()
612 void LuaEntitySAO::setPos(v3f pos)
614 m_base_position = pos;
615 sendPosition(false, true);
618 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
620 m_base_position = pos;
622 sendPosition(true, true);
625 float LuaEntitySAO::getMinimumSavedMovement()
630 std::string LuaEntitySAO::getDescription()
632 std::ostringstream os(std::ios::binary);
633 os<<"LuaEntitySAO at (";
634 os<<(m_base_position.X/BS)<<",";
635 os<<(m_base_position.Y/BS)<<",";
636 os<<(m_base_position.Z/BS);
638 return std::string("LuaEntitySAO");
641 void LuaEntitySAO::setVelocity(v3f velocity)
643 m_velocity = velocity;
646 v3f LuaEntitySAO::getVelocity()
651 void LuaEntitySAO::setAcceleration(v3f acceleration)
653 m_acceleration = acceleration;
656 v3f LuaEntitySAO::getAcceleration()
658 return m_acceleration;
661 void LuaEntitySAO::setYaw(float yaw)
666 float LuaEntitySAO::getYaw()
671 void LuaEntitySAO::setTextureMod(const std::string &mod)
673 std::ostringstream os(std::ios::binary);
675 writeU8(os, LUAENTITY_CMD_SET_TEXTURE_MOD);
677 os<<serializeString(mod);
678 // create message and add to list
679 ActiveObjectMessage aom(getId(), false, os.str());
680 m_messages_out.push_back(aom);
683 void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
684 bool select_horiz_by_yawpitch)
686 std::ostringstream os(std::ios::binary);
688 writeU8(os, LUAENTITY_CMD_SET_SPRITE);
691 writeU16(os, num_frames);
692 writeF1000(os, framelength);
693 writeU8(os, select_horiz_by_yawpitch);
694 // create message and add to list
695 ActiveObjectMessage aom(getId(), false, os.str());
696 m_messages_out.push_back(aom);
699 std::string LuaEntitySAO::getName()
704 void LuaEntitySAO::setArmorGroups(const ItemGroupList &armor_groups)
706 m_armor_groups = armor_groups;
707 m_armor_groups_sent = false;
710 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
712 m_last_sent_move_precision = m_base_position.getDistanceFrom(
713 m_last_sent_position);
714 m_last_sent_position_timer = 0;
715 m_last_sent_yaw = m_yaw;
716 m_last_sent_position = m_base_position;
717 m_last_sent_velocity = m_velocity;
718 //m_last_sent_acceleration = m_acceleration;
720 float update_interval = m_env->getSendRecommendedInterval();
722 std::ostringstream os(std::ios::binary);
724 writeU8(os, LUAENTITY_CMD_UPDATE_POSITION);
727 writeU8(os, do_interpolate);
729 writeV3F1000(os, m_base_position);
731 writeV3F1000(os, m_velocity);
733 writeV3F1000(os, m_acceleration);
735 writeF1000(os, m_yaw);
736 // is_end_position (for interpolation)
737 writeU8(os, is_movement_end);
738 // update_interval (for interpolation)
739 writeF1000(os, update_interval);
741 // create message and add to list
742 ActiveObjectMessage aom(getId(), false, os.str());
743 m_messages_out.push_back(aom);