content_sao.cpp 38 KB

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