content_sao.cpp 37 KB

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