content_sao.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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 || !m_prop.pointable) {
  649. return false;
  650. }
  651. toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
  652. toset->MaxEdge = m_prop.selectionbox.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_DEFAULT;
  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. m_prop.selectionbox = aabb3f(-0.3f, 0.0f, -0.3f, 0.3f, 1.77f, 0.3f);
  676. m_prop.pointable = true;
  677. // start of default appearance, this should be overwritten by LUA
  678. m_prop.visual = "upright_sprite";
  679. m_prop.visual_size = v2f(1, 2);
  680. m_prop.textures.clear();
  681. m_prop.textures.emplace_back("player.png");
  682. m_prop.textures.emplace_back("player_back.png");
  683. m_prop.colors.clear();
  684. m_prop.colors.emplace_back(255, 255, 255, 255);
  685. m_prop.spritediv = v2s16(1,1);
  686. // end of default appearance
  687. m_prop.is_visible = true;
  688. m_prop.makes_footstep_sound = true;
  689. m_prop.stepheight = PLAYER_DEFAULT_STEPHEIGHT * BS;
  690. m_hp = m_prop.hp_max;
  691. }
  692. PlayerSAO::~PlayerSAO()
  693. {
  694. if(m_inventory != &m_player->inventory)
  695. delete m_inventory;
  696. }
  697. void PlayerSAO::finalize(RemotePlayer *player, const std::set<std::string> &privs)
  698. {
  699. assert(player);
  700. m_player = player;
  701. m_privs = privs;
  702. m_inventory = &m_player->inventory;
  703. }
  704. v3f PlayerSAO::getEyeOffset() const
  705. {
  706. return v3f(0, BS * 1.625f, 0);
  707. }
  708. std::string PlayerSAO::getDescription()
  709. {
  710. return std::string("player ") + m_player->getName();
  711. }
  712. // Called after id has been set and has been inserted in environment
  713. void PlayerSAO::addedToEnvironment(u32 dtime_s)
  714. {
  715. ServerActiveObject::addedToEnvironment(dtime_s);
  716. ServerActiveObject::setBasePosition(m_base_position);
  717. m_player->setPlayerSAO(this);
  718. m_player->peer_id = m_peer_id;
  719. m_last_good_position = m_base_position;
  720. }
  721. // Called before removing from environment
  722. void PlayerSAO::removingFromEnvironment()
  723. {
  724. ServerActiveObject::removingFromEnvironment();
  725. if (m_player->getPlayerSAO() == this) {
  726. unlinkPlayerSessionAndSave();
  727. for (u32 attached_particle_spawner : m_attached_particle_spawners) {
  728. m_env->deleteParticleSpawner(attached_particle_spawner, false);
  729. }
  730. }
  731. }
  732. std::string PlayerSAO::getClientInitializationData(u16 protocol_version)
  733. {
  734. std::ostringstream os(std::ios::binary);
  735. // Protocol >= 15
  736. writeU8(os, 1); // version
  737. os << serializeString(m_player->getName()); // name
  738. writeU8(os, 1); // is_player
  739. writeS16(os, getId()); //id
  740. writeV3F1000(os, m_base_position);
  741. writeF1000(os, m_yaw);
  742. writeS16(os, getHP());
  743. std::ostringstream msg_os(std::ios::binary);
  744. msg_os << serializeLongString(getPropertyPacket()); // message 1
  745. msg_os << serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
  746. msg_os << serializeLongString(gob_cmd_update_animation(
  747. m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop)); // 3
  748. for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
  749. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
  750. msg_os << serializeLongString(gob_cmd_update_bone_position((*ii).first,
  751. (*ii).second.X, (*ii).second.Y)); // m_bone_position.size
  752. }
  753. msg_os << serializeLongString(gob_cmd_update_attachment(m_attachment_parent_id,
  754. m_attachment_bone, m_attachment_position, m_attachment_rotation)); // 4
  755. msg_os << serializeLongString(gob_cmd_update_physics_override(m_physics_override_speed,
  756. m_physics_override_jump, m_physics_override_gravity, m_physics_override_sneak,
  757. m_physics_override_sneak_glitch, m_physics_override_new_move)); // 5
  758. // (GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) : Deprecated, for backwards compatibility only.
  759. msg_os << serializeLongString(gob_cmd_update_nametag_attributes(m_prop.nametag_color)); // 6
  760. int message_count = 6 + m_bone_position.size();
  761. for (std::unordered_set<int>::const_iterator ii = m_attachment_child_ids.begin();
  762. ii != m_attachment_child_ids.end(); ++ii) {
  763. if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) {
  764. message_count++;
  765. msg_os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(),
  766. obj->getClientInitializationData(protocol_version)));
  767. }
  768. }
  769. writeU8(os, message_count);
  770. os.write(msg_os.str().c_str(), msg_os.str().size());
  771. // return result
  772. return os.str();
  773. }
  774. void PlayerSAO::getStaticData(std::string * result) const
  775. {
  776. FATAL_ERROR("Deprecated function");
  777. }
  778. void PlayerSAO::step(float dtime, bool send_recommended)
  779. {
  780. if (m_drowning_interval.step(dtime, 2.0)) {
  781. // get head position
  782. v3s16 p = floatToInt(m_base_position + v3f(0, BS * 1.6, 0), BS);
  783. MapNode n = m_env->getMap().getNodeNoEx(p);
  784. const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
  785. // If node generates drown
  786. if (c.drowning > 0 && m_hp > 0) {
  787. if (m_breath > 0)
  788. setBreath(m_breath - 1);
  789. // No more breath, damage player
  790. if (m_breath == 0) {
  791. setHP(m_hp - c.drowning);
  792. m_env->getGameDef()->SendPlayerHPOrDie(this);
  793. }
  794. }
  795. }
  796. if (m_breathing_interval.step(dtime, 0.5)) {
  797. // get head position
  798. v3s16 p = floatToInt(m_base_position + v3f(0, BS * 1.6, 0), BS);
  799. MapNode n = m_env->getMap().getNodeNoEx(p);
  800. const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
  801. // If player is alive & no drowning, breath
  802. if (m_hp > 0 && m_breath < PLAYER_MAX_BREATH && c.drowning == 0)
  803. setBreath(m_breath + 1);
  804. }
  805. if (m_node_hurt_interval.step(dtime, 1.0)) {
  806. // Feet, middle and head
  807. v3s16 p1 = floatToInt(m_base_position + v3f(0, BS*0.1, 0), BS);
  808. MapNode n1 = m_env->getMap().getNodeNoEx(p1);
  809. v3s16 p2 = floatToInt(m_base_position + v3f(0, BS*0.8, 0), BS);
  810. MapNode n2 = m_env->getMap().getNodeNoEx(p2);
  811. v3s16 p3 = floatToInt(m_base_position + v3f(0, BS*1.6, 0), BS);
  812. MapNode n3 = m_env->getMap().getNodeNoEx(p3);
  813. u32 damage_per_second = 0;
  814. damage_per_second = MYMAX(damage_per_second,
  815. m_env->getGameDef()->ndef()->get(n1).damage_per_second);
  816. damage_per_second = MYMAX(damage_per_second,
  817. m_env->getGameDef()->ndef()->get(n2).damage_per_second);
  818. damage_per_second = MYMAX(damage_per_second,
  819. m_env->getGameDef()->ndef()->get(n3).damage_per_second);
  820. if (damage_per_second != 0 && m_hp > 0) {
  821. s16 newhp = ((s32) damage_per_second > m_hp ? 0 : m_hp - damage_per_second);
  822. setHP(newhp);
  823. m_env->getGameDef()->SendPlayerHPOrDie(this);
  824. }
  825. }
  826. if (!m_properties_sent) {
  827. m_properties_sent = true;
  828. std::string str = getPropertyPacket();
  829. // create message and add to list
  830. ActiveObjectMessage aom(getId(), true, str);
  831. m_messages_out.push(aom);
  832. }
  833. // If attached, check that our parent is still there. If it isn't, detach.
  834. if(m_attachment_parent_id && !isAttached())
  835. {
  836. m_attachment_parent_id = 0;
  837. m_attachment_bone = "";
  838. m_attachment_position = v3f(0,0,0);
  839. m_attachment_rotation = v3f(0,0,0);
  840. setBasePosition(m_last_good_position);
  841. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  842. }
  843. //dstream<<"PlayerSAO::step: dtime: "<<dtime<<std::endl;
  844. // Set lag pool maximums based on estimated lag
  845. const float LAG_POOL_MIN = 5.0;
  846. float lag_pool_max = m_env->getMaxLagEstimate() * 2.0;
  847. if(lag_pool_max < LAG_POOL_MIN)
  848. lag_pool_max = LAG_POOL_MIN;
  849. m_dig_pool.setMax(lag_pool_max);
  850. m_move_pool.setMax(lag_pool_max);
  851. // Increment cheat prevention timers
  852. m_dig_pool.add(dtime);
  853. m_move_pool.add(dtime);
  854. m_time_from_last_teleport += dtime;
  855. m_time_from_last_punch += dtime;
  856. m_nocheat_dig_time += dtime;
  857. // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
  858. // If the object gets detached this comes into effect automatically from the last known origin
  859. if (isAttached()) {
  860. v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
  861. m_last_good_position = pos;
  862. setBasePosition(pos);
  863. }
  864. if (!send_recommended)
  865. return;
  866. // If the object is attached client-side, don't waste bandwidth sending its position to clients
  867. if(m_position_not_sent && !isAttached())
  868. {
  869. m_position_not_sent = false;
  870. float update_interval = m_env->getSendRecommendedInterval();
  871. v3f pos;
  872. if(isAttached()) // Just in case we ever do send attachment position too
  873. pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
  874. else
  875. pos = m_base_position;
  876. std::string str = gob_cmd_update_position(
  877. pos,
  878. v3f(0,0,0),
  879. v3f(0,0,0),
  880. m_yaw,
  881. true,
  882. false,
  883. update_interval
  884. );
  885. // create message and add to list
  886. ActiveObjectMessage aom(getId(), false, str);
  887. m_messages_out.push(aom);
  888. }
  889. if (!m_armor_groups_sent) {
  890. m_armor_groups_sent = true;
  891. std::string str = gob_cmd_update_armor_groups(
  892. m_armor_groups);
  893. // create message and add to list
  894. ActiveObjectMessage aom(getId(), true, str);
  895. m_messages_out.push(aom);
  896. }
  897. if (!m_physics_override_sent) {
  898. m_physics_override_sent = true;
  899. std::string str = gob_cmd_update_physics_override(m_physics_override_speed,
  900. m_physics_override_jump, m_physics_override_gravity,
  901. m_physics_override_sneak, m_physics_override_sneak_glitch,
  902. m_physics_override_new_move);
  903. // create message and add to list
  904. ActiveObjectMessage aom(getId(), true, str);
  905. m_messages_out.push(aom);
  906. }
  907. if (!m_animation_sent) {
  908. m_animation_sent = true;
  909. std::string str = gob_cmd_update_animation(
  910. m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop);
  911. // create message and add to list
  912. ActiveObjectMessage aom(getId(), true, str);
  913. m_messages_out.push(aom);
  914. }
  915. if (!m_bone_position_sent) {
  916. m_bone_position_sent = true;
  917. for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
  918. ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
  919. std::string str = gob_cmd_update_bone_position((*ii).first,
  920. (*ii).second.X, (*ii).second.Y);
  921. // create message and add to list
  922. ActiveObjectMessage aom(getId(), true, str);
  923. m_messages_out.push(aom);
  924. }
  925. }
  926. if (!m_attachment_sent){
  927. m_attachment_sent = true;
  928. std::string str = gob_cmd_update_attachment(m_attachment_parent_id,
  929. m_attachment_bone, m_attachment_position, m_attachment_rotation);
  930. // create message and add to list
  931. ActiveObjectMessage aom(getId(), true, str);
  932. m_messages_out.push(aom);
  933. }
  934. }
  935. void PlayerSAO::setBasePosition(const v3f &position)
  936. {
  937. if (m_player && position != m_base_position)
  938. m_player->setDirty(true);
  939. // This needs to be ran for attachments too
  940. ServerActiveObject::setBasePosition(position);
  941. m_position_not_sent = true;
  942. }
  943. void PlayerSAO::setPos(const v3f &pos)
  944. {
  945. if(isAttached())
  946. return;
  947. setBasePosition(pos);
  948. // Movement caused by this command is always valid
  949. m_last_good_position = pos;
  950. m_move_pool.empty();
  951. m_time_from_last_teleport = 0.0;
  952. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  953. }
  954. void PlayerSAO::moveTo(v3f pos, bool continuous)
  955. {
  956. if(isAttached())
  957. return;
  958. setBasePosition(pos);
  959. // Movement caused by this command is always valid
  960. m_last_good_position = pos;
  961. m_move_pool.empty();
  962. m_time_from_last_teleport = 0.0;
  963. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  964. }
  965. void PlayerSAO::setYaw(const float yaw)
  966. {
  967. if (m_player && yaw != m_yaw)
  968. m_player->setDirty(true);
  969. UnitSAO::setYaw(yaw);
  970. }
  971. void PlayerSAO::setFov(const float fov)
  972. {
  973. if (m_player && fov != m_fov)
  974. m_player->setDirty(true);
  975. m_fov = fov;
  976. }
  977. void PlayerSAO::setWantedRange(const s16 range)
  978. {
  979. if (m_player && range != m_wanted_range)
  980. m_player->setDirty(true);
  981. m_wanted_range = range;
  982. }
  983. void PlayerSAO::setYawAndSend(const float yaw)
  984. {
  985. setYaw(yaw);
  986. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  987. }
  988. void PlayerSAO::setPitch(const float pitch)
  989. {
  990. if (m_player && pitch != m_pitch)
  991. m_player->setDirty(true);
  992. m_pitch = pitch;
  993. }
  994. void PlayerSAO::setPitchAndSend(const float pitch)
  995. {
  996. setPitch(pitch);
  997. m_env->getGameDef()->SendMovePlayer(m_peer_id);
  998. }
  999. int PlayerSAO::punch(v3f dir,
  1000. const ToolCapabilities *toolcap,
  1001. ServerActiveObject *puncher,
  1002. float time_from_last_punch)
  1003. {
  1004. // It's best that attachments cannot be punched
  1005. if (isAttached())
  1006. return 0;
  1007. if (!toolcap)
  1008. return 0;
  1009. // No effect if PvP disabled
  1010. if (!g_settings->getBool("enable_pvp")) {
  1011. if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
  1012. std::string str = gob_cmd_punched(0, getHP());
  1013. // create message and add to list
  1014. ActiveObjectMessage aom(getId(), true, str);
  1015. m_messages_out.push(aom);
  1016. return 0;
  1017. }
  1018. }
  1019. HitParams hitparams = getHitParams(m_armor_groups, toolcap,
  1020. time_from_last_punch);
  1021. std::string punchername = "nil";
  1022. if (puncher != 0)
  1023. punchername = puncher->getDescription();
  1024. PlayerSAO *playersao = m_player->getPlayerSAO();
  1025. bool damage_handled = m_env->getScriptIface()->on_punchplayer(playersao,
  1026. puncher, time_from_last_punch, toolcap, dir,
  1027. hitparams.hp);
  1028. if (!damage_handled) {
  1029. setHP(getHP() - hitparams.hp);
  1030. } else { // override client prediction
  1031. if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
  1032. std::string str = gob_cmd_punched(0, getHP());
  1033. // create message and add to list
  1034. ActiveObjectMessage aom(getId(), true, str);
  1035. m_messages_out.push(aom);
  1036. }
  1037. }
  1038. actionstream << "Player " << m_player->getName() << " punched by "
  1039. << punchername;
  1040. if (!damage_handled) {
  1041. actionstream << ", damage " << hitparams.hp << " HP";
  1042. } else {
  1043. actionstream << ", damage handled by lua";
  1044. }
  1045. actionstream << std::endl;
  1046. return hitparams.wear;
  1047. }
  1048. s16 PlayerSAO::readDamage()
  1049. {
  1050. s16 damage = m_damage;
  1051. m_damage = 0;
  1052. return damage;
  1053. }
  1054. void PlayerSAO::setHP(s16 hp)
  1055. {
  1056. s16 oldhp = m_hp;
  1057. s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp);
  1058. if (hp_change == 0)
  1059. return;
  1060. hp = oldhp + hp_change;
  1061. if (hp < 0)
  1062. hp = 0;
  1063. else if (hp > m_prop.hp_max)
  1064. hp = m_prop.hp_max;
  1065. if (hp < oldhp && !g_settings->getBool("enable_damage")) {
  1066. return;
  1067. }
  1068. m_hp = hp;
  1069. if (oldhp > hp)
  1070. m_damage += (oldhp - hp);
  1071. // Update properties on death
  1072. if ((hp == 0) != (oldhp == 0))
  1073. m_properties_sent = false;
  1074. }
  1075. void PlayerSAO::setBreath(const u16 breath, bool send)
  1076. {
  1077. if (m_player && breath != m_breath)
  1078. m_player->setDirty(true);
  1079. m_breath = MYMIN(breath, PLAYER_MAX_BREATH);
  1080. if (send)
  1081. m_env->getGameDef()->SendPlayerBreath(this);
  1082. }
  1083. Inventory* PlayerSAO::getInventory()
  1084. {
  1085. return m_inventory;
  1086. }
  1087. const Inventory* PlayerSAO::getInventory() const
  1088. {
  1089. return m_inventory;
  1090. }
  1091. InventoryLocation PlayerSAO::getInventoryLocation() const
  1092. {
  1093. InventoryLocation loc;
  1094. loc.setPlayer(m_player->getName());
  1095. return loc;
  1096. }
  1097. std::string PlayerSAO::getWieldList() const
  1098. {
  1099. return "main";
  1100. }
  1101. ItemStack PlayerSAO::getWieldedItem() const
  1102. {
  1103. const Inventory *inv = getInventory();
  1104. ItemStack ret;
  1105. const InventoryList *mlist = inv->getList(getWieldList());
  1106. if (mlist && getWieldIndex() < (s32)mlist->getSize())
  1107. ret = mlist->getItem(getWieldIndex());
  1108. return ret;
  1109. }
  1110. ItemStack PlayerSAO::getWieldedItemOrHand() const
  1111. {
  1112. const Inventory *inv = getInventory();
  1113. ItemStack ret;
  1114. const InventoryList *mlist = inv->getList(getWieldList());
  1115. if (mlist && getWieldIndex() < (s32)mlist->getSize())
  1116. ret = mlist->getItem(getWieldIndex());
  1117. if (ret.name.empty()) {
  1118. const InventoryList *hlist = inv->getList("hand");
  1119. if (hlist)
  1120. ret = hlist->getItem(0);
  1121. }
  1122. return ret;
  1123. }
  1124. bool PlayerSAO::setWieldedItem(const ItemStack &item)
  1125. {
  1126. Inventory *inv = getInventory();
  1127. if (inv) {
  1128. InventoryList *mlist = inv->getList(getWieldList());
  1129. if (mlist) {
  1130. mlist->changeItem(getWieldIndex(), item);
  1131. return true;
  1132. }
  1133. }
  1134. return false;
  1135. }
  1136. int PlayerSAO::getWieldIndex() const
  1137. {
  1138. return m_wield_index;
  1139. }
  1140. void PlayerSAO::setWieldIndex(int i)
  1141. {
  1142. if(i != m_wield_index) {
  1143. m_wield_index = i;
  1144. }
  1145. }
  1146. // Erase the peer id and make the object for removal
  1147. void PlayerSAO::disconnected()
  1148. {
  1149. m_peer_id = 0;
  1150. m_removed = true;
  1151. }
  1152. void PlayerSAO::unlinkPlayerSessionAndSave()
  1153. {
  1154. assert(m_player->getPlayerSAO() == this);
  1155. m_player->peer_id = 0;
  1156. m_env->savePlayer(m_player);
  1157. m_player->setPlayerSAO(NULL);
  1158. m_env->removePlayer(m_player);
  1159. }
  1160. std::string PlayerSAO::getPropertyPacket()
  1161. {
  1162. m_prop.is_visible = (true);
  1163. return gob_cmd_set_properties(m_prop);
  1164. }
  1165. bool PlayerSAO::checkMovementCheat()
  1166. {
  1167. if (isAttached() || m_is_singleplayer ||
  1168. g_settings->getBool("disable_anticheat")) {
  1169. m_last_good_position = m_base_position;
  1170. return false;
  1171. }
  1172. bool cheated = false;
  1173. /*
  1174. Check player movements
  1175. NOTE: Actually the server should handle player physics like the
  1176. client does and compare player's position to what is calculated
  1177. on our side. This is required when eg. players fly due to an
  1178. explosion. Altough a node-based alternative might be possible
  1179. too, and much more lightweight.
  1180. */
  1181. float player_max_speed = 0;
  1182. if (m_privs.count("fast") != 0) {
  1183. // Fast speed
  1184. player_max_speed = m_player->movement_speed_fast * m_physics_override_speed;
  1185. } else {
  1186. // Normal speed
  1187. player_max_speed = m_player->movement_speed_walk * m_physics_override_speed;
  1188. }
  1189. // Tolerance. The lag pool does this a bit.
  1190. //player_max_speed *= 2.5;
  1191. v3f diff = (m_base_position - m_last_good_position);
  1192. float d_vert = diff.Y;
  1193. diff.Y = 0;
  1194. float d_horiz = diff.getLength();
  1195. float required_time = d_horiz / player_max_speed;
  1196. if (d_vert > 0 && d_vert / player_max_speed > required_time)
  1197. required_time = d_vert / player_max_speed; // Moving upwards
  1198. if (m_move_pool.grab(required_time)) {
  1199. m_last_good_position = m_base_position;
  1200. } else {
  1201. const float LAG_POOL_MIN = 5.0;
  1202. float lag_pool_max = m_env->getMaxLagEstimate() * 2.0;
  1203. lag_pool_max = MYMAX(lag_pool_max, LAG_POOL_MIN);
  1204. if (m_time_from_last_teleport > lag_pool_max) {
  1205. actionstream << "Player " << m_player->getName()
  1206. << " moved too fast; resetting position"
  1207. << std::endl;
  1208. cheated = true;
  1209. }
  1210. setBasePosition(m_last_good_position);
  1211. }
  1212. return cheated;
  1213. }
  1214. bool PlayerSAO::getCollisionBox(aabb3f *toset) const
  1215. {
  1216. //update collision box
  1217. toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
  1218. toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
  1219. toset->MinEdge += m_base_position;
  1220. toset->MaxEdge += m_base_position;
  1221. return true;
  1222. }
  1223. bool PlayerSAO::getSelectionBox(aabb3f *toset) const
  1224. {
  1225. if (!m_prop.is_visible || !m_prop.pointable) {
  1226. return false;
  1227. }
  1228. toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
  1229. toset->MaxEdge = m_prop.selectionbox.MaxEdge * BS;
  1230. return true;
  1231. }