content_sao.cpp 37 KB

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