content_sao.cpp 37 KB

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