content_sao.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "content_sao.h"
  17. #include "util/serialize.h"
  18. #include "collision.h"
  19. #include "environment.h"
  20. #include "tool.h" // For ToolCapabilities
  21. #include "gamedef.h"
  22. #include "nodedef.h"
  23. #include "remoteplayer.h"
  24. #include "server.h"
  25. #include "scripting_game.h"
  26. #include "genericobject.h"
  27. std::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
  28. /*
  29. TestSAO
  30. */
  31. class TestSAO : public ServerActiveObject
  32. {
  33. public:
  34. TestSAO(ServerEnvironment *env, v3f pos):
  35. ServerActiveObject(env, pos),
  36. m_timer1(0),
  37. m_age(0)
  38. {
  39. ServerActiveObject::registerType(getType(), create);
  40. }
  41. ActiveObjectType getType() const
  42. { return ACTIVEOBJECT_TYPE_TEST; }
  43. static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
  44. const std::string &data)
  45. {
  46. return new TestSAO(env, pos);
  47. }
  48. void step(float dtime, bool send_recommended)
  49. {
  50. m_age += dtime;
  51. if(m_age > 10)
  52. {
  53. m_removed = true;
  54. return;
  55. }
  56. m_base_position.Y += dtime * BS * 2;
  57. if(m_base_position.Y > 8*BS)
  58. m_base_position.Y = 2*BS;
  59. if(send_recommended == false)
  60. return;
  61. m_timer1 -= dtime;
  62. if(m_timer1 < 0.0)
  63. {
  64. m_timer1 += 0.125;
  65. std::string data;
  66. data += itos(0); // 0 = position
  67. data += " ";
  68. data += itos(m_base_position.X);
  69. data += " ";
  70. data += itos(m_base_position.Y);
  71. data += " ";
  72. data += itos(m_base_position.Z);
  73. ActiveObjectMessage aom(getId(), false, data);
  74. m_messages_out.push(aom);
  75. }
  76. }
  77. bool getCollisionBox(aabb3f *toset) const { return false; }
  78. bool collideWithObjects() const { return false; }
  79. private:
  80. float m_timer1;
  81. float m_age;
  82. };
  83. // Prototype (registers item for deserialization)
  84. TestSAO proto_TestSAO(NULL, v3f(0,0,0));
  85. /*
  86. UnitSAO
  87. */
  88. UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos):
  89. ServerActiveObject(env, pos),
  90. m_hp(-1),
  91. m_yaw(0),
  92. m_properties_sent(true),
  93. m_armor_groups_sent(false),
  94. m_animation_range(0,0),
  95. m_animation_speed(0),
  96. m_animation_blend(0),
  97. m_animation_loop(true),
  98. m_animation_sent(false),
  99. m_bone_position_sent(false),
  100. m_attachment_parent_id(0),
  101. m_attachment_sent(false)
  102. {
  103. // Initialize something to armor groups
  104. m_armor_groups["fleshy"] = 100;
  105. }
  106. bool UnitSAO::isAttached() const
  107. {
  108. if (!m_attachment_parent_id)
  109. return false;
  110. // Check if the parent still exists
  111. ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
  112. if (obj)
  113. return true;
  114. return false;
  115. }
  116. void UnitSAO::setArmorGroups(const ItemGroupList &armor_groups)
  117. {
  118. m_armor_groups = armor_groups;
  119. m_armor_groups_sent = false;
  120. }
  121. const ItemGroupList &UnitSAO::getArmorGroups()
  122. {
  123. return m_armor_groups;
  124. }
  125. void UnitSAO::setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop)
  126. {
  127. // store these so they can be updated to clients
  128. m_animation_range = frame_range;
  129. m_animation_speed = frame_speed;
  130. m_animation_blend = frame_blend;
  131. m_animation_loop = frame_loop;
  132. m_animation_sent = false;
  133. }
  134. void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop)
  135. {
  136. *frame_range = m_animation_range;
  137. *frame_speed = m_animation_speed;
  138. *frame_blend = m_animation_blend;
  139. *frame_loop = m_animation_loop;
  140. }
  141. void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
  142. {
  143. // store these so they can be updated to clients
  144. m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
  145. m_bone_position_sent = false;
  146. }
  147. void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
  148. {
  149. *position = m_bone_position[bone].X;
  150. *rotation = m_bone_position[bone].Y;
  151. }
  152. void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
  153. {
  154. // Attachments need to be handled on both the server and client.
  155. // If we just attach on the server, we can only copy the position of the parent. Attachments
  156. // are still sent to clients at an interval so players might see them lagging, plus we can't
  157. // read and attach to skeletal bones.
  158. // If we just attach on the client, the server still sees the child at its original location.
  159. // This breaks some things so we also give the server the most accurate representation
  160. // even if players only see the client changes.
  161. m_attachment_parent_id = parent_id;
  162. m_attachment_bone = bone;
  163. m_attachment_position = position;
  164. m_attachment_rotation = rotation;
  165. m_attachment_sent = false;
  166. }
  167. void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
  168. v3f *rotation)
  169. {
  170. *parent_id = m_attachment_parent_id;
  171. *bone = m_attachment_bone;
  172. *position = m_attachment_position;
  173. *rotation = m_attachment_rotation;
  174. }
  175. void UnitSAO::addAttachmentChild(int child_id)
  176. {
  177. m_attachment_child_ids.insert(child_id);
  178. }
  179. void UnitSAO::removeAttachmentChild(int child_id)
  180. {
  181. m_attachment_child_ids.erase(child_id);
  182. }
  183. const UNORDERED_SET<int> &UnitSAO::getAttachmentChildIds()
  184. {
  185. return m_attachment_child_ids;
  186. }
  187. ObjectProperties* UnitSAO::accessObjectProperties()
  188. {
  189. return &m_prop;
  190. }
  191. void UnitSAO::notifyObjectPropertiesModified()
  192. {
  193. m_properties_sent = false;
  194. }
  195. /*
  196. LuaEntitySAO
  197. */
  198. // Prototype (registers item for deserialization)
  199. LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
  200. LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
  201. const std::string &name, const std::string &state):
  202. UnitSAO(env, pos),
  203. m_init_name(name),
  204. m_init_state(state),
  205. m_registered(false),
  206. m_velocity(0,0,0),
  207. m_acceleration(0,0,0),
  208. m_last_sent_yaw(0),
  209. m_last_sent_position(0,0,0),
  210. m_last_sent_velocity(0,0,0),
  211. m_last_sent_position_timer(0),
  212. m_last_sent_move_precision(0)
  213. {
  214. // Only register type if no environment supplied
  215. if(env == NULL){
  216. ServerActiveObject::registerType(getType(), create);
  217. return;
  218. }
  219. }
  220. LuaEntitySAO::~LuaEntitySAO()
  221. {
  222. if(m_registered){
  223. m_env->getScriptIface()->luaentity_Remove(m_id);
  224. }
  225. for (UNORDERED_SET<u32>::iterator it = m_attached_particle_spawners.begin();
  226. it != m_attached_particle_spawners.end(); ++it) {
  227. m_env->deleteParticleSpawner(*it, false);
  228. }
  229. }
  230. void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
  231. {
  232. ServerActiveObject::addedToEnvironment(dtime_s);
  233. // Create entity from name
  234. m_registered = m_env->getScriptIface()->
  235. luaentity_Add(m_id, m_init_name.c_str());
  236. if(m_registered){
  237. // Get properties
  238. m_env->getScriptIface()->
  239. luaentity_GetProperties(m_id, &m_prop);
  240. // Initialize HP from properties
  241. m_hp = m_prop.hp_max;
  242. // Activate entity, supplying serialized state
  243. m_env->getScriptIface()->
  244. luaentity_Activate(m_id, m_init_state.c_str(), dtime_s);
  245. } else {
  246. m_prop.infotext = m_init_name;
  247. }
  248. }
  249. ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
  250. const std::string &data)
  251. {
  252. std::string name;
  253. std::string state;
  254. s16 hp = 1;
  255. v3f velocity;
  256. float yaw = 0;
  257. if(data != ""){
  258. std::istringstream is(data, std::ios::binary);
  259. // read version
  260. u8 version = readU8(is);
  261. // check if version is supported
  262. if(version == 0){
  263. name = deSerializeString(is);
  264. state = deSerializeLongString(is);
  265. }
  266. else if(version == 1){
  267. name = deSerializeString(is);
  268. state = deSerializeLongString(is);
  269. hp = readS16(is);
  270. velocity = readV3F1000(is);
  271. yaw = readF1000(is);
  272. }
  273. }
  274. // create object
  275. infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
  276. <<state<<"\")"<<std::endl;
  277. LuaEntitySAO *sao = new LuaEntitySAO(env, pos, name, state);
  278. sao->m_hp = hp;
  279. sao->m_velocity = velocity;
  280. sao->m_yaw = yaw;
  281. return sao;
  282. }
  283. void LuaEntitySAO::step(float dtime, bool send_recommended)
  284. {
  285. if(!m_properties_sent)
  286. {
  287. m_properties_sent = true;
  288. std::string str = getPropertyPacket();
  289. // create message and add to list
  290. ActiveObjectMessage aom(getId(), true, str);
  291. m_messages_out.push(aom);
  292. }
  293. // If attached, check that our parent is still there. If it isn't, detach.
  294. if(m_attachment_parent_id && !isAttached())
  295. {
  296. m_attachment_parent_id = 0;
  297. m_attachment_bone = "";
  298. m_attachment_position = v3f(0,0,0);
  299. m_attachment_rotation = v3f(0,0,0);
  300. sendPosition(false, true);
  301. }
  302. m_last_sent_position_timer += dtime;
  303. // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
  304. // If the object gets detached this comes into effect automatically from the last known origin
  305. if(isAttached())
  306. {
  307. v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
  308. m_base_position = pos;
  309. m_velocity = v3f(0,0,0);
  310. m_acceleration = v3f(0,0,0);
  311. }
  312. else
  313. {
  314. if(m_prop.physical){
  315. aabb3f box = m_prop.collisionbox;
  316. box.MinEdge *= BS;
  317. box.MaxEdge *= BS;
  318. collisionMoveResult moveresult;
  319. f32 pos_max_d = BS*0.25; // Distance per iteration
  320. v3f p_pos = m_base_position;
  321. v3f p_velocity = m_velocity;
  322. v3f p_acceleration = m_acceleration;
  323. moveresult = collisionMoveSimple(m_env, m_env->getGameDef(),
  324. pos_max_d, box, m_prop.stepheight, dtime,
  325. &p_pos, &p_velocity, p_acceleration,
  326. this, m_prop.collideWithObjects);
  327. // Apply results
  328. m_base_position = p_pos;
  329. m_velocity = p_velocity;
  330. m_acceleration = p_acceleration;
  331. } else {
  332. m_base_position += dtime * m_velocity + 0.5 * dtime
  333. * dtime * m_acceleration;
  334. m_velocity += dtime * m_acceleration;
  335. }
  336. if((m_prop.automatic_face_movement_dir) &&
  337. (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
  338. {
  339. float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
  340. + m_prop.automatic_face_movement_dir_offset;
  341. float max_rotation_delta =
  342. dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
  343. if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
  344. (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
  345. m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
  346. } else {
  347. m_yaw = optimal_yaw;
  348. }
  349. }
  350. }
  351. if(m_registered){
  352. m_env->getScriptIface()->luaentity_Step(m_id, dtime);
  353. }
  354. if(send_recommended == false)
  355. return;
  356. if(!isAttached())
  357. {
  358. // TODO: force send when acceleration changes enough?
  359. float minchange = 0.2*BS;
  360. if(m_last_sent_position_timer > 1.0){
  361. minchange = 0.01*BS;
  362. } else if(m_last_sent_position_timer > 0.2){
  363. minchange = 0.05*BS;
  364. }
  365. float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
  366. move_d += m_last_sent_move_precision;
  367. float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
  368. if(move_d > minchange || vel_d > minchange ||
  369. fabs(m_yaw - m_last_sent_yaw) > 1.0){
  370. sendPosition(true, false);
  371. }
  372. }
  373. if(m_armor_groups_sent == false){
  374. m_armor_groups_sent = true;
  375. std::string str = gob_cmd_update_armor_groups(
  376. m_armor_groups);
  377. // create message and add to list
  378. ActiveObjectMessage aom(getId(), true, str);
  379. m_messages_out.push(aom);
  380. }
  381. if(m_animation_sent == false){
  382. m_animation_sent = true;
  383. std::string str = gob_cmd_update_animation(
  384. m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop);
  385. // create message and add to list
  386. ActiveObjectMessage aom(getId(), true, str);
  387. m_messages_out.push(aom);
  388. }
  389. if(m_bone_position_sent == false){
  390. m_bone_position_sent = true;
  391. for (UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
  392. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
  393. std::string str = gob_cmd_update_bone_position((*ii).first,
  394. (*ii).second.X, (*ii).second.Y);
  395. // create message and add to list
  396. ActiveObjectMessage aom(getId(), true, str);
  397. m_messages_out.push(aom);
  398. }
  399. }
  400. if(m_attachment_sent == false){
  401. m_attachment_sent = true;
  402. std::string str = gob_cmd_update_attachment(m_attachment_parent_id, m_attachment_bone, m_attachment_position, m_attachment_rotation);
  403. // create message and add to list
  404. ActiveObjectMessage aom(getId(), true, str);
  405. m_messages_out.push(aom);
  406. }
  407. }
  408. std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
  409. {
  410. std::ostringstream os(std::ios::binary);
  411. if(protocol_version >= 14)
  412. {
  413. writeU8(os, 1); // version
  414. os<<serializeString(""); // name
  415. writeU8(os, 0); // is_player
  416. writeS16(os, getId()); //id
  417. writeV3F1000(os, m_base_position);
  418. writeF1000(os, m_yaw);
  419. writeS16(os, m_hp);
  420. std::ostringstream msg_os(std::ios::binary);
  421. msg_os << serializeLongString(getPropertyPacket()); // message 1
  422. msg_os << serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
  423. msg_os << serializeLongString(gob_cmd_update_animation(
  424. m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop)); // 3
  425. for (UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
  426. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
  427. msg_os << serializeLongString(gob_cmd_update_bone_position((*ii).first,
  428. (*ii).second.X, (*ii).second.Y)); // m_bone_position.size
  429. }
  430. msg_os << serializeLongString(gob_cmd_update_attachment(m_attachment_parent_id,
  431. m_attachment_bone, m_attachment_position, m_attachment_rotation)); // 4
  432. int message_count = 4 + m_bone_position.size();
  433. for (UNORDERED_SET<int>::const_iterator ii = m_attachment_child_ids.begin();
  434. (ii != m_attachment_child_ids.end()); ++ii) {
  435. if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) {
  436. message_count++;
  437. msg_os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(),
  438. obj->getClientInitializationData(protocol_version)));
  439. }
  440. }
  441. writeU8(os, message_count);
  442. os.write(msg_os.str().c_str(), msg_os.str().size());
  443. }
  444. else
  445. {
  446. writeU8(os, 0); // version
  447. os<<serializeString(""); // name
  448. writeU8(os, 0); // is_player
  449. writeV3F1000(os, m_base_position);
  450. writeF1000(os, m_yaw);
  451. writeS16(os, m_hp);
  452. writeU8(os, 2); // number of messages stuffed in here
  453. os<<serializeLongString(getPropertyPacket()); // message 1
  454. os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
  455. }
  456. // return result
  457. return os.str();
  458. }
  459. void LuaEntitySAO::getStaticData(std::string *result) const
  460. {
  461. verbosestream<<FUNCTION_NAME<<std::endl;
  462. std::ostringstream os(std::ios::binary);
  463. // version
  464. writeU8(os, 1);
  465. // name
  466. os<<serializeString(m_init_name);
  467. // state
  468. if(m_registered){
  469. std::string state = m_env->getScriptIface()->
  470. luaentity_GetStaticdata(m_id);
  471. os<<serializeLongString(state);
  472. } else {
  473. os<<serializeLongString(m_init_state);
  474. }
  475. // hp
  476. writeS16(os, m_hp);
  477. // velocity
  478. writeV3F1000(os, m_velocity);
  479. // yaw
  480. writeF1000(os, m_yaw);
  481. *result = os.str();
  482. }
  483. int LuaEntitySAO::punch(v3f dir,
  484. const ToolCapabilities *toolcap,
  485. ServerActiveObject *puncher,
  486. float time_from_last_punch)
  487. {
  488. if (!m_registered){
  489. // Delete unknown LuaEntities when punched
  490. m_removed = true;
  491. return 0;
  492. }
  493. // It's best that attachments cannot be punched
  494. if (isAttached())
  495. return 0;
  496. ItemStack *punchitem = NULL;
  497. ItemStack punchitem_static;
  498. if (puncher) {
  499. punchitem_static = puncher->getWieldedItem();
  500. punchitem = &punchitem_static;
  501. }
  502. PunchDamageResult result = getPunchDamage(
  503. m_armor_groups,
  504. toolcap,
  505. punchitem,
  506. time_from_last_punch);
  507. if (result.did_punch) {
  508. setHP(getHP() - result.damage);
  509. if (result.damage > 0) {
  510. std::string punchername = puncher ? puncher->getDescription() : "nil";
  511. actionstream << getDescription() << " punched by "
  512. << punchername << ", damage " << result.damage
  513. << " hp, health now " << getHP() << " hp" << std::endl;
  514. }
  515. std::string str = gob_cmd_punched(result.damage, getHP());
  516. // create message and add to list
  517. ActiveObjectMessage aom(getId(), true, str);
  518. m_messages_out.push(aom);
  519. }
  520. if (getHP() == 0)
  521. m_removed = true;
  522. m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
  523. time_from_last_punch, toolcap, dir);
  524. return result.wear;
  525. }
  526. void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
  527. {
  528. if (!m_registered)
  529. return;
  530. // It's best that attachments cannot be clicked
  531. if (isAttached())
  532. return;
  533. m_env->getScriptIface()->luaentity_Rightclick(m_id, clicker);
  534. }
  535. void LuaEntitySAO::setPos(const v3f &pos)
  536. {
  537. if(isAttached())
  538. return;
  539. m_base_position = pos;
  540. sendPosition(false, true);
  541. }
  542. void LuaEntitySAO::moveTo(v3f pos, bool continuous)
  543. {
  544. if(isAttached())
  545. return;
  546. m_base_position = pos;
  547. if(!continuous)
  548. sendPosition(true, true);
  549. }
  550. float LuaEntitySAO::getMinimumSavedMovement()
  551. {
  552. return 0.1 * BS;
  553. }
  554. std::string LuaEntitySAO::getDescription()
  555. {
  556. std::ostringstream os(std::ios::binary);
  557. os<<"LuaEntitySAO at (";
  558. os<<(m_base_position.X/BS)<<",";
  559. os<<(m_base_position.Y/BS)<<",";
  560. os<<(m_base_position.Z/BS);
  561. os<<")";
  562. return os.str();
  563. }
  564. void LuaEntitySAO::setHP(s16 hp)
  565. {
  566. if(hp < 0) hp = 0;
  567. m_hp = hp;
  568. }
  569. s16 LuaEntitySAO::getHP() const
  570. {
  571. return m_hp;
  572. }
  573. void LuaEntitySAO::setVelocity(v3f velocity)
  574. {
  575. m_velocity = velocity;
  576. }
  577. v3f LuaEntitySAO::getVelocity()
  578. {
  579. return m_velocity;
  580. }
  581. void LuaEntitySAO::setAcceleration(v3f acceleration)
  582. {
  583. m_acceleration = acceleration;
  584. }
  585. v3f LuaEntitySAO::getAcceleration()
  586. {
  587. return m_acceleration;
  588. }
  589. void LuaEntitySAO::setTextureMod(const std::string &mod)
  590. {
  591. std::string str = gob_cmd_set_texture_mod(mod);
  592. // create message and add to list
  593. ActiveObjectMessage aom(getId(), true, str);
  594. m_messages_out.push(aom);
  595. }
  596. void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
  597. bool select_horiz_by_yawpitch)
  598. {
  599. std::string str = gob_cmd_set_sprite(
  600. p,
  601. num_frames,
  602. framelength,
  603. select_horiz_by_yawpitch
  604. );
  605. // create message and add to list
  606. ActiveObjectMessage aom(getId(), true, str);
  607. m_messages_out.push(aom);
  608. }
  609. std::string LuaEntitySAO::getName()
  610. {
  611. return m_init_name;
  612. }
  613. std::string LuaEntitySAO::getPropertyPacket()
  614. {
  615. return gob_cmd_set_properties(m_prop);
  616. }
  617. void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
  618. {
  619. // If the object is attached client-side, don't waste bandwidth sending its position to clients
  620. if(isAttached())
  621. return;
  622. m_last_sent_move_precision = m_base_position.getDistanceFrom(
  623. m_last_sent_position);
  624. m_last_sent_position_timer = 0;
  625. m_last_sent_yaw = m_yaw;
  626. m_last_sent_position = m_base_position;
  627. m_last_sent_velocity = m_velocity;
  628. //m_last_sent_acceleration = m_acceleration;
  629. float update_interval = m_env->getSendRecommendedInterval();
  630. std::string str = gob_cmd_update_position(
  631. m_base_position,
  632. m_velocity,
  633. m_acceleration,
  634. m_yaw,
  635. do_interpolate,
  636. is_movement_end,
  637. update_interval
  638. );
  639. // create message and add to list
  640. ActiveObjectMessage aom(getId(), false, str);
  641. m_messages_out.push(aom);
  642. }
  643. bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
  644. {
  645. if (m_prop.physical)
  646. {
  647. //update collision box
  648. toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
  649. toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
  650. toset->MinEdge += m_base_position;
  651. toset->MaxEdge += m_base_position;
  652. return true;
  653. }
  654. return false;
  655. }
  656. bool LuaEntitySAO::collideWithObjects() const
  657. {
  658. return m_prop.collideWithObjects;
  659. }
  660. /*
  661. PlayerSAO
  662. */
  663. // No prototype, PlayerSAO does not need to be deserialized
  664. PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer):
  665. UnitSAO(env_, v3f(0,0,0)),
  666. m_player(NULL),
  667. m_peer_id(peer_id_),
  668. m_inventory(NULL),
  669. m_damage(0),
  670. m_last_good_position(0,0,0),
  671. m_time_from_last_punch(0),
  672. m_nocheat_dig_pos(32767, 32767, 32767),
  673. m_nocheat_dig_time(0),
  674. m_wield_index(0),
  675. m_position_not_sent(false),
  676. m_is_singleplayer(is_singleplayer),
  677. m_breath(PLAYER_MAX_BREATH),
  678. m_pitch(0),
  679. m_fov(0),
  680. m_wanted_range(0),
  681. // public
  682. m_physics_override_speed(1),
  683. m_physics_override_jump(1),
  684. m_physics_override_gravity(1),
  685. m_physics_override_sneak(true),
  686. m_physics_override_sneak_glitch(true),
  687. m_physics_override_sent(false)
  688. {
  689. assert(m_peer_id != 0); // pre-condition
  690. m_prop.hp_max = PLAYER_MAX_HP;
  691. m_prop.physical = false;
  692. m_prop.weight = 75;
  693. m_prop.collisionbox = aabb3f(-1/3.,-1.0,-1/3., 1/3.,1.0,1/3.);
  694. // start of default appearance, this should be overwritten by LUA
  695. m_prop.visual = "upright_sprite";
  696. m_prop.visual_size = v2f(1, 2);
  697. m_prop.textures.clear();
  698. m_prop.textures.push_back("player.png");
  699. m_prop.textures.push_back("player_back.png");
  700. m_prop.colors.clear();
  701. m_prop.colors.push_back(video::SColor(255, 255, 255, 255));
  702. m_prop.spritediv = v2s16(1,1);
  703. // end of default appearance
  704. m_prop.is_visible = true;
  705. m_prop.makes_footstep_sound = true;
  706. m_hp = PLAYER_MAX_HP;
  707. }
  708. PlayerSAO::~PlayerSAO()
  709. {
  710. if(m_inventory != &m_player->inventory)
  711. delete m_inventory;
  712. }
  713. void PlayerSAO::initialize(RemotePlayer *player, const std::set<std::string> &privs)
  714. {
  715. assert(player);
  716. m_player = player;
  717. m_privs = privs;
  718. m_inventory = &m_player->inventory;
  719. }
  720. v3f PlayerSAO::getEyeOffset() const
  721. {
  722. return v3f(0, BS * 1.625f, 0);
  723. }
  724. std::string PlayerSAO::getDescription()
  725. {
  726. return std::string("player ") + m_player->getName();
  727. }
  728. // Called after id has been set and has been inserted in environment
  729. void PlayerSAO::addedToEnvironment(u32 dtime_s)
  730. {
  731. ServerActiveObject::addedToEnvironment(dtime_s);
  732. ServerActiveObject::setBasePosition(m_base_position);
  733. m_player->setPlayerSAO(this);
  734. m_player->peer_id = m_peer_id;
  735. m_last_good_position = m_base_position;
  736. }
  737. // Called before removing from environment
  738. void PlayerSAO::removingFromEnvironment()
  739. {
  740. ServerActiveObject::removingFromEnvironment();
  741. if (m_player->getPlayerSAO() == this) {
  742. unlinkPlayerSessionAndSave();
  743. for (UNORDERED_SET<u32>::iterator it = m_attached_particle_spawners.begin();
  744. it != m_attached_particle_spawners.end(); ++it) {
  745. m_env->deleteParticleSpawner(*it, false);
  746. }
  747. }
  748. }
  749. std::string PlayerSAO::getClientInitializationData(u16 protocol_version)
  750. {
  751. std::ostringstream os(std::ios::binary);
  752. if(protocol_version >= 15)
  753. {
  754. writeU8(os, 1); // version
  755. os<<serializeString(m_player->getName()); // name
  756. writeU8(os, 1); // is_player
  757. writeS16(os, getId()); //id
  758. writeV3F1000(os, m_base_position + v3f(0,BS*1,0));
  759. writeF1000(os, m_yaw);
  760. writeS16(os, getHP());
  761. std::ostringstream msg_os(std::ios::binary);
  762. msg_os << serializeLongString(getPropertyPacket()); // message 1
  763. msg_os << serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
  764. msg_os << serializeLongString(gob_cmd_update_animation(
  765. m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop)); // 3
  766. for (UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
  767. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
  768. msg_os << serializeLongString(gob_cmd_update_bone_position((*ii).first,
  769. (*ii).second.X, (*ii).second.Y)); // m_bone_position.size
  770. }
  771. msg_os << serializeLongString(gob_cmd_update_attachment(m_attachment_parent_id,
  772. m_attachment_bone, m_attachment_position, m_attachment_rotation)); // 4
  773. msg_os << serializeLongString(gob_cmd_update_physics_override(m_physics_override_speed,
  774. m_physics_override_jump, m_physics_override_gravity, m_physics_override_sneak,
  775. m_physics_override_sneak_glitch)); // 5
  776. // (GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) : Deprecated, for backwards compatibility only.
  777. msg_os << serializeLongString(gob_cmd_update_nametag_attributes(m_prop.nametag_color)); // 6
  778. int message_count = 6 + m_bone_position.size();
  779. for (UNORDERED_SET<int>::const_iterator ii = m_attachment_child_ids.begin();
  780. ii != m_attachment_child_ids.end(); ++ii) {
  781. if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) {
  782. message_count++;
  783. msg_os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(),
  784. obj->getClientInitializationData(protocol_version)));
  785. }
  786. }
  787. writeU8(os, message_count);
  788. os.write(msg_os.str().c_str(), msg_os.str().size());
  789. }
  790. else
  791. {
  792. writeU8(os, 0); // version
  793. os<<serializeString(m_player->getName()); // name
  794. writeU8(os, 1); // is_player
  795. writeV3F1000(os, m_base_position + v3f(0,BS*1,0));
  796. writeF1000(os, m_yaw);
  797. writeS16(os, getHP());
  798. writeU8(os, 2); // number of messages stuffed in here
  799. os<<serializeLongString(getPropertyPacket()); // message 1
  800. os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
  801. }
  802. // return result
  803. return os.str();
  804. }
  805. void PlayerSAO::getStaticData(std::string *result) const
  806. {
  807. FATAL_ERROR("Deprecated function");
  808. }
  809. void PlayerSAO::step(float dtime, bool send_recommended)
  810. {
  811. if (m_drowning_interval.step(dtime, 2.0)) {
  812. // get head position
  813. v3s16 p = floatToInt(m_base_position + v3f(0, BS * 1.6, 0), BS);
  814. MapNode n = m_env->getMap().getNodeNoEx(p);
  815. const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
  816. // If node generates drown
  817. if (c.drowning > 0) {
  818. if (m_hp > 0 && m_breath > 0)
  819. setBreath(m_breath - 1);
  820. // No more breath, damage player
  821. if (m_breath == 0) {
  822. setHP(m_hp - c.drowning);
  823. m_env->getGameDef()->SendPlayerHPOrDie(this);
  824. }
  825. }
  826. }
  827. if (m_breathing_interval.step(dtime, 0.5)) {
  828. // get head position
  829. v3s16 p = floatToInt(m_base_position + v3f(0, BS * 1.6, 0), BS);
  830. MapNode n = m_env->getMap().getNodeNoEx(p);
  831. const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
  832. // If player is alive & no drowning, breath
  833. if (m_hp > 0 && c.drowning == 0)
  834. setBreath(m_breath + 1);
  835. }
  836. if (!m_properties_sent) {
  837. m_properties_sent = true;
  838. std::string str = getPropertyPacket();
  839. // create message and add to list
  840. ActiveObjectMessage aom(getId(), true, str);
  841. m_messages_out.push(aom);
  842. }
  843. // If attached, check that our parent is still there. If it isn't, detach.
  844. if(m_attachment_parent_id && !isAttached())
  845. {
  846. m_attachment_parent_id = 0;
  847. m_attachment_bone = "";
  848. m_attachment_position = v3f(0,0,0);
  849. m_attachment_rotation = v3f(0,0,0);
  850. setBasePosition(m_last_good_position);
  851. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  852. }
  853. //dstream<<"PlayerSAO::step: dtime: "<<dtime<<std::endl;
  854. // Set lag pool maximums based on estimated lag
  855. const float LAG_POOL_MIN = 5.0;
  856. float lag_pool_max = m_env->getMaxLagEstimate() * 2.0;
  857. if(lag_pool_max < LAG_POOL_MIN)
  858. lag_pool_max = LAG_POOL_MIN;
  859. m_dig_pool.setMax(lag_pool_max);
  860. m_move_pool.setMax(lag_pool_max);
  861. // Increment cheat prevention timers
  862. m_dig_pool.add(dtime);
  863. m_move_pool.add(dtime);
  864. m_time_from_last_punch += dtime;
  865. m_nocheat_dig_time += dtime;
  866. // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
  867. // If the object gets detached this comes into effect automatically from the last known origin
  868. if (isAttached()) {
  869. v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
  870. m_last_good_position = pos;
  871. setBasePosition(pos);
  872. }
  873. if (!send_recommended)
  874. return;
  875. // If the object is attached client-side, don't waste bandwidth sending its position to clients
  876. if(m_position_not_sent && !isAttached())
  877. {
  878. m_position_not_sent = false;
  879. float update_interval = m_env->getSendRecommendedInterval();
  880. v3f pos;
  881. if(isAttached()) // Just in case we ever do send attachment position too
  882. pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
  883. else
  884. pos = m_base_position + v3f(0,BS*1,0);
  885. std::string str = gob_cmd_update_position(
  886. pos,
  887. v3f(0,0,0),
  888. v3f(0,0,0),
  889. m_yaw,
  890. true,
  891. false,
  892. update_interval
  893. );
  894. // create message and add to list
  895. ActiveObjectMessage aom(getId(), false, str);
  896. m_messages_out.push(aom);
  897. }
  898. if (!m_armor_groups_sent) {
  899. m_armor_groups_sent = true;
  900. std::string str = gob_cmd_update_armor_groups(
  901. m_armor_groups);
  902. // create message and add to list
  903. ActiveObjectMessage aom(getId(), true, str);
  904. m_messages_out.push(aom);
  905. }
  906. if (!m_physics_override_sent) {
  907. m_physics_override_sent = true;
  908. std::string str = gob_cmd_update_physics_override(m_physics_override_speed,
  909. m_physics_override_jump, m_physics_override_gravity,
  910. m_physics_override_sneak, m_physics_override_sneak_glitch);
  911. // create message and add to list
  912. ActiveObjectMessage aom(getId(), true, str);
  913. m_messages_out.push(aom);
  914. }
  915. if (!m_animation_sent) {
  916. m_animation_sent = true;
  917. std::string str = gob_cmd_update_animation(
  918. m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop);
  919. // create message and add to list
  920. ActiveObjectMessage aom(getId(), true, str);
  921. m_messages_out.push(aom);
  922. }
  923. if (!m_bone_position_sent) {
  924. m_bone_position_sent = true;
  925. for (UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
  926. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
  927. std::string str = gob_cmd_update_bone_position((*ii).first,
  928. (*ii).second.X, (*ii).second.Y);
  929. // create message and add to list
  930. ActiveObjectMessage aom(getId(), true, str);
  931. m_messages_out.push(aom);
  932. }
  933. }
  934. if (!m_attachment_sent){
  935. m_attachment_sent = true;
  936. std::string str = gob_cmd_update_attachment(m_attachment_parent_id,
  937. m_attachment_bone, m_attachment_position, m_attachment_rotation);
  938. // create message and add to list
  939. ActiveObjectMessage aom(getId(), true, str);
  940. m_messages_out.push(aom);
  941. }
  942. }
  943. void PlayerSAO::setBasePosition(const v3f &position)
  944. {
  945. if (m_player && position != m_base_position)
  946. m_player->setDirty(true);
  947. // This needs to be ran for attachments too
  948. ServerActiveObject::setBasePosition(position);
  949. m_position_not_sent = true;
  950. }
  951. void PlayerSAO::setPos(const v3f &pos)
  952. {
  953. if(isAttached())
  954. return;
  955. setBasePosition(pos);
  956. // Movement caused by this command is always valid
  957. m_last_good_position = pos;
  958. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  959. }
  960. void PlayerSAO::moveTo(v3f pos, bool continuous)
  961. {
  962. if(isAttached())
  963. return;
  964. setBasePosition(pos);
  965. // Movement caused by this command is always valid
  966. m_last_good_position = pos;
  967. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  968. }
  969. void PlayerSAO::setYaw(const float yaw)
  970. {
  971. if (m_player && yaw != m_yaw)
  972. m_player->setDirty(true);
  973. UnitSAO::setYaw(yaw);
  974. }
  975. void PlayerSAO::setFov(const float fov)
  976. {
  977. if (m_player && fov != m_fov)
  978. m_player->setDirty(true);
  979. m_fov = fov;
  980. }
  981. void PlayerSAO::setWantedRange(const s16 range)
  982. {
  983. if (m_player && range != m_wanted_range)
  984. m_player->setDirty(true);
  985. m_wanted_range = range;
  986. }
  987. void PlayerSAO::setYawAndSend(const float yaw)
  988. {
  989. setYaw(yaw);
  990. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  991. }
  992. void PlayerSAO::setPitch(const float pitch)
  993. {
  994. if (m_player && pitch != m_pitch)
  995. m_player->setDirty(true);
  996. m_pitch = pitch;
  997. }
  998. void PlayerSAO::setPitchAndSend(const float pitch)
  999. {
  1000. setPitch(pitch);
  1001. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  1002. }
  1003. int PlayerSAO::punch(v3f dir,
  1004. const ToolCapabilities *toolcap,
  1005. ServerActiveObject *puncher,
  1006. float time_from_last_punch)
  1007. {
  1008. // It's best that attachments cannot be punched
  1009. if (isAttached())
  1010. return 0;
  1011. if (!toolcap)
  1012. return 0;
  1013. // No effect if PvP disabled
  1014. if (g_settings->getBool("enable_pvp") == false) {
  1015. if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
  1016. std::string str = gob_cmd_punched(0, getHP());
  1017. // create message and add to list
  1018. ActiveObjectMessage aom(getId(), true, str);
  1019. m_messages_out.push(aom);
  1020. return 0;
  1021. }
  1022. }
  1023. HitParams hitparams = getHitParams(m_armor_groups, toolcap,
  1024. time_from_last_punch);
  1025. std::string punchername = "nil";
  1026. if (puncher != 0)
  1027. punchername = puncher->getDescription();
  1028. PlayerSAO *playersao = m_player->getPlayerSAO();
  1029. bool damage_handled = m_env->getScriptIface()->on_punchplayer(playersao,
  1030. puncher, time_from_last_punch, toolcap, dir,
  1031. hitparams.hp);
  1032. if (!damage_handled) {
  1033. setHP(getHP() - hitparams.hp);
  1034. } else { // override client prediction
  1035. if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
  1036. std::string str = gob_cmd_punched(0, getHP());
  1037. // create message and add to list
  1038. ActiveObjectMessage aom(getId(), true, str);
  1039. m_messages_out.push(aom);
  1040. }
  1041. }
  1042. actionstream << "Player " << m_player->getName() << " punched by "
  1043. << punchername;
  1044. if (!damage_handled) {
  1045. actionstream << ", damage " << hitparams.hp << " HP";
  1046. } else {
  1047. actionstream << ", damage handled by lua";
  1048. }
  1049. actionstream << std::endl;
  1050. return hitparams.wear;
  1051. }
  1052. s16 PlayerSAO::readDamage()
  1053. {
  1054. s16 damage = m_damage;
  1055. m_damage = 0;
  1056. return damage;
  1057. }
  1058. void PlayerSAO::setHP(s16 hp)
  1059. {
  1060. s16 oldhp = m_hp;
  1061. s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp);
  1062. if (hp_change == 0)
  1063. return;
  1064. hp = oldhp + hp_change;
  1065. if (hp < 0)
  1066. hp = 0;
  1067. else if (hp > PLAYER_MAX_HP)
  1068. hp = PLAYER_MAX_HP;
  1069. if (hp < oldhp && !g_settings->getBool("enable_damage")) {
  1070. return;
  1071. }
  1072. m_hp = hp;
  1073. if (oldhp > hp)
  1074. m_damage += (oldhp - hp);
  1075. // Update properties on death
  1076. if ((hp == 0) != (oldhp == 0))
  1077. m_properties_sent = false;
  1078. }
  1079. void PlayerSAO::setBreath(const u16 breath, bool send)
  1080. {
  1081. if (m_player && breath != m_breath)
  1082. m_player->setDirty(true);
  1083. m_breath = MYMIN(breath, PLAYER_MAX_BREATH);
  1084. if (send)
  1085. m_env->getGameDef()->SendPlayerBreath(this);
  1086. }
  1087. Inventory* PlayerSAO::getInventory()
  1088. {
  1089. return m_inventory;
  1090. }
  1091. const Inventory* PlayerSAO::getInventory() const
  1092. {
  1093. return m_inventory;
  1094. }
  1095. InventoryLocation PlayerSAO::getInventoryLocation() const
  1096. {
  1097. InventoryLocation loc;
  1098. loc.setPlayer(m_player->getName());
  1099. return loc;
  1100. }
  1101. std::string PlayerSAO::getWieldList() const
  1102. {
  1103. return "main";
  1104. }
  1105. ItemStack PlayerSAO::getWieldedItem() const
  1106. {
  1107. const Inventory *inv = getInventory();
  1108. ItemStack ret;
  1109. const InventoryList *mlist = inv->getList(getWieldList());
  1110. if (mlist && getWieldIndex() < (s32)mlist->getSize())
  1111. ret = mlist->getItem(getWieldIndex());
  1112. if (ret.name.empty()) {
  1113. const InventoryList *hlist = inv->getList("hand");
  1114. if (hlist)
  1115. ret = hlist->getItem(0);
  1116. }
  1117. return ret;
  1118. }
  1119. bool PlayerSAO::setWieldedItem(const ItemStack &item)
  1120. {
  1121. Inventory *inv = getInventory();
  1122. if (inv) {
  1123. InventoryList *mlist = inv->getList(getWieldList());
  1124. if (mlist) {
  1125. ItemStack olditem = mlist->getItem(getWieldIndex());
  1126. if (olditem.name.empty()) {
  1127. InventoryList *hlist = inv->getList("hand");
  1128. if (hlist) {
  1129. hlist->changeItem(0, item);
  1130. return true;
  1131. }
  1132. }
  1133. mlist->changeItem(getWieldIndex(), item);
  1134. return true;
  1135. }
  1136. }
  1137. return false;
  1138. }
  1139. int PlayerSAO::getWieldIndex() const
  1140. {
  1141. return m_wield_index;
  1142. }
  1143. void PlayerSAO::setWieldIndex(int i)
  1144. {
  1145. if(i != m_wield_index) {
  1146. m_wield_index = i;
  1147. }
  1148. }
  1149. // Erase the peer id and make the object for removal
  1150. void PlayerSAO::disconnected()
  1151. {
  1152. m_peer_id = 0;
  1153. m_removed = true;
  1154. }
  1155. void PlayerSAO::unlinkPlayerSessionAndSave()
  1156. {
  1157. assert(m_player->getPlayerSAO() == this);
  1158. m_player->peer_id = 0;
  1159. m_env->savePlayer(m_player);
  1160. m_player->setPlayerSAO(NULL);
  1161. m_env->removePlayer(m_player);
  1162. }
  1163. std::string PlayerSAO::getPropertyPacket()
  1164. {
  1165. m_prop.is_visible = (true);
  1166. return gob_cmd_set_properties(m_prop);
  1167. }
  1168. bool PlayerSAO::checkMovementCheat()
  1169. {
  1170. if (isAttached() || m_is_singleplayer ||
  1171. g_settings->getBool("disable_anticheat")) {
  1172. m_last_good_position = m_base_position;
  1173. return false;
  1174. }
  1175. bool cheated = false;
  1176. /*
  1177. Check player movements
  1178. NOTE: Actually the server should handle player physics like the
  1179. client does and compare player's position to what is calculated
  1180. on our side. This is required when eg. players fly due to an
  1181. explosion. Altough a node-based alternative might be possible
  1182. too, and much more lightweight.
  1183. */
  1184. float player_max_speed = 0;
  1185. if (m_privs.count("fast") != 0) {
  1186. // Fast speed
  1187. player_max_speed = m_player->movement_speed_fast * m_physics_override_speed;
  1188. } else {
  1189. // Normal speed
  1190. player_max_speed = m_player->movement_speed_walk * m_physics_override_speed;
  1191. }
  1192. // Tolerance. The lag pool does this a bit.
  1193. //player_max_speed *= 2.5;
  1194. v3f diff = (m_base_position - m_last_good_position);
  1195. float d_vert = diff.Y;
  1196. diff.Y = 0;
  1197. float d_horiz = diff.getLength();
  1198. float required_time = d_horiz / player_max_speed;
  1199. if (d_vert > 0 && d_vert / player_max_speed > required_time)
  1200. required_time = d_vert / player_max_speed; // Moving upwards
  1201. if (m_move_pool.grab(required_time)) {
  1202. m_last_good_position = m_base_position;
  1203. } else {
  1204. actionstream << "Player " << m_player->getName()
  1205. << " moved too fast; resetting position"
  1206. << std::endl;
  1207. setBasePosition(m_last_good_position);
  1208. cheated = true;
  1209. }
  1210. return cheated;
  1211. }
  1212. bool PlayerSAO::getCollisionBox(aabb3f *toset) const
  1213. {
  1214. *toset = aabb3f(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30);
  1215. toset->MinEdge += m_base_position;
  1216. toset->MaxEdge += m_base_position;
  1217. return true;
  1218. }