localplayer.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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 "localplayer.h"
  17. #include <cmath>
  18. #include "event.h"
  19. #include "collision.h"
  20. #include "nodedef.h"
  21. #include "settings.h"
  22. #include "environment.h"
  23. #include "map.h"
  24. #include "client.h"
  25. #include "content_cao.h"
  26. /*
  27. LocalPlayer
  28. */
  29. LocalPlayer::LocalPlayer(Client *client, const char *name):
  30. Player(name, client->idef()),
  31. m_client(client)
  32. {
  33. }
  34. static aabb3f getNodeBoundingBox(const std::vector<aabb3f> &nodeboxes)
  35. {
  36. if (nodeboxes.empty())
  37. return aabb3f(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  38. aabb3f b_max;
  39. std::vector<aabb3f>::const_iterator it = nodeboxes.begin();
  40. b_max = aabb3f(it->MinEdge, it->MaxEdge);
  41. ++it;
  42. for (; it != nodeboxes.end(); ++it)
  43. b_max.addInternalBox(*it);
  44. return b_max;
  45. }
  46. bool LocalPlayer::updateSneakNode(Map *map, const v3f &position,
  47. const v3f &sneak_max)
  48. {
  49. static const v3s16 dir9_center[9] = {
  50. v3s16( 0, 0, 0),
  51. v3s16( 1, 0, 0),
  52. v3s16(-1, 0, 0),
  53. v3s16( 0, 0, 1),
  54. v3s16( 0, 0, -1),
  55. v3s16( 1, 0, 1),
  56. v3s16(-1, 0, 1),
  57. v3s16( 1, 0, -1),
  58. v3s16(-1, 0, -1)
  59. };
  60. const NodeDefManager *nodemgr = m_client->ndef();
  61. MapNode node;
  62. bool is_valid_position;
  63. bool new_sneak_node_exists = m_sneak_node_exists;
  64. // We want the top of the sneak node to be below the players feet
  65. f32 position_y_mod = 0.05f * BS;
  66. if (m_sneak_node_exists)
  67. position_y_mod = m_sneak_node_bb_top.MaxEdge.Y - position_y_mod;
  68. // Get position of current standing node
  69. const v3s16 current_node = floatToInt(position - v3f(0.0f, position_y_mod, 0.0f), BS);
  70. if (current_node != m_sneak_node) {
  71. new_sneak_node_exists = false;
  72. } else {
  73. node = map->getNode(current_node, &is_valid_position);
  74. if (!is_valid_position || !nodemgr->get(node).walkable)
  75. new_sneak_node_exists = false;
  76. }
  77. // Keep old sneak node
  78. if (new_sneak_node_exists)
  79. return true;
  80. // Get new sneak node
  81. m_sneak_ladder_detected = false;
  82. f32 min_distance_f = 100000.0f * BS;
  83. for (const auto &d : dir9_center) {
  84. const v3s16 p = current_node + d;
  85. const v3f pf = intToFloat(p, BS);
  86. const v2f diff(position.X - pf.X, position.Z - pf.Z);
  87. f32 distance_f = diff.getLength();
  88. if (distance_f > min_distance_f ||
  89. fabs(diff.X) > (0.5f + 0.1f) * BS + sneak_max.X ||
  90. fabs(diff.Y) > (0.5f + 0.1f) * BS + sneak_max.Z)
  91. continue;
  92. // The node to be sneaked on has to be walkable
  93. node = map->getNode(p, &is_valid_position);
  94. if (!is_valid_position || !nodemgr->get(node).walkable)
  95. continue;
  96. // And the node(s) above have to be nonwalkable
  97. bool ok = true;
  98. if (!physics_override_sneak_glitch) {
  99. u16 height =
  100. ceilf((m_collisionbox.MaxEdge.Y - m_collisionbox.MinEdge.Y) / BS);
  101. for (u16 y = 1; y <= height; y++) {
  102. node = map->getNode(p + v3s16(0, y, 0), &is_valid_position);
  103. if (!is_valid_position || nodemgr->get(node).walkable) {
  104. ok = false;
  105. break;
  106. }
  107. }
  108. } else {
  109. // legacy behaviour: check just one node
  110. node = map->getNode(p + v3s16(0, 1, 0), &is_valid_position);
  111. ok = is_valid_position && !nodemgr->get(node).walkable;
  112. }
  113. if (!ok)
  114. continue;
  115. min_distance_f = distance_f;
  116. m_sneak_node = p;
  117. new_sneak_node_exists = true;
  118. }
  119. if (!new_sneak_node_exists)
  120. return false;
  121. // Update saved top bounding box of sneak node
  122. node = map->getNode(m_sneak_node);
  123. std::vector<aabb3f> nodeboxes;
  124. node.getCollisionBoxes(nodemgr, &nodeboxes);
  125. m_sneak_node_bb_top = getNodeBoundingBox(nodeboxes);
  126. if (physics_override_sneak_glitch) {
  127. // Detect sneak ladder:
  128. // Node two meters above sneak node must be solid
  129. node = map->getNode(m_sneak_node + v3s16(0, 2, 0),
  130. &is_valid_position);
  131. if (is_valid_position && nodemgr->get(node).walkable) {
  132. // Node three meters above: must be non-solid
  133. node = map->getNode(m_sneak_node + v3s16(0, 3, 0),
  134. &is_valid_position);
  135. m_sneak_ladder_detected = is_valid_position &&
  136. !nodemgr->get(node).walkable;
  137. }
  138. }
  139. return true;
  140. }
  141. void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
  142. std::vector<CollisionInfo> *collision_info)
  143. {
  144. // Node at feet position, update each ClientEnvironment::step()
  145. if (!collision_info || collision_info->empty())
  146. m_standing_node = floatToInt(m_position, BS);
  147. // Temporary option for old move code
  148. if (!physics_override_new_move) {
  149. old_move(dtime, env, pos_max_d, collision_info);
  150. return;
  151. }
  152. Map *map = &env->getMap();
  153. const NodeDefManager *nodemgr = m_client->ndef();
  154. v3f position = getPosition();
  155. // Copy parent position if local player is attached
  156. if (getParent()) {
  157. setPosition(m_cao->getPosition());
  158. added_velocity = v3f(0.0f); // ignored
  159. return;
  160. }
  161. PlayerSettings &player_settings = getPlayerSettings();
  162. // Skip collision detection if noclip mode is used
  163. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  164. bool noclip = m_client->checkLocalPrivilege("noclip") && player_settings.noclip;
  165. bool free_move = player_settings.free_move && fly_allowed;
  166. if (noclip && free_move) {
  167. position += m_speed * dtime;
  168. setPosition(position);
  169. touching_ground = false;
  170. added_velocity = v3f(0.0f); // ignored
  171. return;
  172. }
  173. m_speed += added_velocity;
  174. added_velocity = v3f(0.0f);
  175. /*
  176. Collision detection
  177. */
  178. bool is_valid_position;
  179. MapNode node;
  180. v3s16 pp;
  181. /*
  182. Check if player is in liquid (the oscillating value)
  183. */
  184. // If in liquid, the threshold of coming out is at higher y
  185. if (in_liquid)
  186. {
  187. pp = floatToInt(position + v3f(0.0f, BS * 0.1f, 0.0f), BS);
  188. node = map->getNode(pp, &is_valid_position);
  189. if (is_valid_position) {
  190. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  191. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  192. } else {
  193. in_liquid = false;
  194. }
  195. } else {
  196. // If not in liquid, the threshold of going in is at lower y
  197. pp = floatToInt(position + v3f(0.0f, BS * 0.5f, 0.0f), BS);
  198. node = map->getNode(pp, &is_valid_position);
  199. if (is_valid_position) {
  200. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  201. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  202. } else {
  203. in_liquid = false;
  204. }
  205. }
  206. /*
  207. Check if player is in liquid (the stable value)
  208. */
  209. pp = floatToInt(position + v3f(0.0f), BS);
  210. node = map->getNode(pp, &is_valid_position);
  211. if (is_valid_position) {
  212. in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
  213. } else {
  214. in_liquid_stable = false;
  215. }
  216. /*
  217. Check if player is climbing
  218. */
  219. pp = floatToInt(position + v3f(0.0f, 0.5f * BS, 0.0f), BS);
  220. v3s16 pp2 = floatToInt(position + v3f(0.0f, -0.2f * BS, 0.0f), BS);
  221. node = map->getNode(pp, &is_valid_position);
  222. bool is_valid_position2;
  223. MapNode node2 = map->getNode(pp2, &is_valid_position2);
  224. if (!(is_valid_position && is_valid_position2)) {
  225. is_climbing = false;
  226. } else {
  227. is_climbing = (nodemgr->get(node.getContent()).climbable ||
  228. nodemgr->get(node2.getContent()).climbable) && !free_move;
  229. }
  230. /*
  231. Collision uncertainty radius
  232. Make it a bit larger than the maximum distance of movement
  233. */
  234. //f32 d = pos_max_d * 1.1;
  235. // A fairly large value in here makes moving smoother
  236. f32 d = 0.15f * BS;
  237. // This should always apply, otherwise there are glitches
  238. sanity_check(d > pos_max_d);
  239. // Player object property step height is multiplied by BS in
  240. // /src/script/common/c_content.cpp and /src/content_sao.cpp
  241. float player_stepheight = (m_cao == nullptr) ? 0.0f :
  242. (touching_ground ? m_cao->getStepHeight() : (0.2f * BS));
  243. v3f accel_f;
  244. const v3f initial_position = position;
  245. const v3f initial_speed = m_speed;
  246. collisionMoveResult result = collisionMoveSimple(env, m_client,
  247. pos_max_d, m_collisionbox, player_stepheight, dtime,
  248. &position, &m_speed, accel_f);
  249. bool could_sneak = control.sneak && !free_move && !in_liquid &&
  250. !is_climbing && physics_override_sneak;
  251. // Add new collisions to the vector
  252. if (collision_info && !free_move) {
  253. v3f diff = intToFloat(m_standing_node, BS) - position;
  254. f32 distance = diff.getLength();
  255. // Force update each ClientEnvironment::step()
  256. bool is_first = collision_info->empty();
  257. for (const auto &colinfo : result.collisions) {
  258. collision_info->push_back(colinfo);
  259. if (colinfo.type != COLLISION_NODE ||
  260. colinfo.axis != COLLISION_AXIS_Y ||
  261. (could_sneak && m_sneak_node_exists))
  262. continue;
  263. diff = intToFloat(colinfo.node_p, BS) - position;
  264. // Find nearest colliding node
  265. f32 len = diff.getLength();
  266. if (is_first || len < distance) {
  267. m_standing_node = colinfo.node_p;
  268. distance = len;
  269. is_first = false;
  270. }
  271. }
  272. }
  273. /*
  274. If the player's feet touch the topside of any node, this is
  275. set to true.
  276. Player is allowed to jump when this is true.
  277. */
  278. bool touching_ground_was = touching_ground;
  279. touching_ground = result.touching_ground;
  280. bool sneak_can_jump = false;
  281. // Max. distance (X, Z) over border for sneaking determined by collision box
  282. // * 0.49 to keep the center just barely on the node
  283. v3f sneak_max = m_collisionbox.getExtent() * 0.49;
  284. if (m_sneak_ladder_detected) {
  285. // restore legacy behaviour (this makes the m_speed.Y hack necessary)
  286. sneak_max = v3f(0.4f * BS, 0.0f, 0.4f * BS);
  287. }
  288. /*
  289. If sneaking, keep on top of last walked node and don't fall off
  290. */
  291. if (could_sneak && m_sneak_node_exists) {
  292. const v3f sn_f = intToFloat(m_sneak_node, BS);
  293. const v3f bmin = sn_f + m_sneak_node_bb_top.MinEdge;
  294. const v3f bmax = sn_f + m_sneak_node_bb_top.MaxEdge;
  295. const v3f old_pos = position;
  296. const v3f old_speed = m_speed;
  297. f32 y_diff = bmax.Y - position.Y;
  298. m_standing_node = m_sneak_node;
  299. // (BS * 0.6f) is the basic stepheight while standing on ground
  300. if (y_diff < BS * 0.6f) {
  301. // Only center player when they're on the node
  302. position.X = rangelim(position.X,
  303. bmin.X - sneak_max.X, bmax.X + sneak_max.X);
  304. position.Z = rangelim(position.Z,
  305. bmin.Z - sneak_max.Z, bmax.Z + sneak_max.Z);
  306. if (position.X != old_pos.X)
  307. m_speed.X = 0.0f;
  308. if (position.Z != old_pos.Z)
  309. m_speed.Z = 0.0f;
  310. }
  311. if (y_diff > 0 && m_speed.Y <= 0.0f &&
  312. (physics_override_sneak_glitch || y_diff < BS * 0.6f)) {
  313. // Move player to the maximal height when falling or when
  314. // the ledge is climbed on the next step.
  315. // Smoothen the movement (based on 'position.Y = bmax.Y')
  316. position.Y += y_diff * dtime * 22.0f + BS * 0.01f;
  317. position.Y = std::min(position.Y, bmax.Y);
  318. m_speed.Y = 0.0f;
  319. }
  320. // Allow jumping on node edges while sneaking
  321. if (m_speed.Y == 0.0f || m_sneak_ladder_detected)
  322. sneak_can_jump = true;
  323. if (collision_info &&
  324. m_speed.Y - old_speed.Y > BS) {
  325. // Collide with sneak node, report fall damage
  326. CollisionInfo sn_info;
  327. sn_info.node_p = m_sneak_node;
  328. sn_info.old_speed = old_speed;
  329. sn_info.new_speed = m_speed;
  330. collision_info->push_back(sn_info);
  331. }
  332. }
  333. /*
  334. Find the next sneak node if necessary
  335. */
  336. bool new_sneak_node_exists = false;
  337. if (could_sneak)
  338. new_sneak_node_exists = updateSneakNode(map, position, sneak_max);
  339. /*
  340. Set new position but keep sneak node set
  341. */
  342. setPosition(position);
  343. m_sneak_node_exists = new_sneak_node_exists;
  344. /*
  345. Report collisions
  346. */
  347. if (!result.standing_on_object && !touching_ground_was && touching_ground) {
  348. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
  349. // Set camera impact value to be used for view bobbing
  350. camera_impact = getSpeed().Y * -1;
  351. }
  352. {
  353. camera_barely_in_ceiling = false;
  354. v3s16 camera_np = floatToInt(getEyePosition(), BS);
  355. MapNode n = map->getNode(camera_np);
  356. if (n.getContent() != CONTENT_IGNORE) {
  357. if (nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2)
  358. camera_barely_in_ceiling = true;
  359. }
  360. }
  361. /*
  362. Check properties of the node on which the player is standing
  363. */
  364. const ContentFeatures &f = nodemgr->get(map->getNode(m_standing_node));
  365. const ContentFeatures &f1 = nodemgr->get(map->getNode(m_standing_node + v3s16(0, 1, 0)));
  366. // Determine if jumping is possible
  367. m_disable_jump = itemgroup_get(f.groups, "disable_jump") ||
  368. itemgroup_get(f1.groups, "disable_jump");
  369. m_can_jump = ((touching_ground && !is_climbing) || sneak_can_jump) && !m_disable_jump;
  370. // Jump key pressed while jumping off from a bouncy block
  371. if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") &&
  372. m_speed.Y >= -0.5f * BS) {
  373. float jumpspeed = movement_speed_jump * physics_override_jump;
  374. if (m_speed.Y > 1.0f) {
  375. // Reduce boost when speed already is high
  376. m_speed.Y += jumpspeed / (1.0f + (m_speed.Y / 16.0f));
  377. } else {
  378. m_speed.Y += jumpspeed;
  379. }
  380. setSpeed(m_speed);
  381. m_can_jump = false;
  382. }
  383. // Autojump
  384. handleAutojump(dtime, env, result, initial_position, initial_speed, pos_max_d);
  385. }
  386. void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d)
  387. {
  388. move(dtime, env, pos_max_d, NULL);
  389. }
  390. void LocalPlayer::applyControl(float dtime, Environment *env)
  391. {
  392. // Clear stuff
  393. swimming_vertical = false;
  394. swimming_pitch = false;
  395. setPitch(control.pitch);
  396. setYaw(control.yaw);
  397. // Nullify speed and don't run positioning code if the player is attached
  398. if (getParent()) {
  399. setSpeed(v3f(0.0f));
  400. return;
  401. }
  402. PlayerSettings &player_settings = getPlayerSettings();
  403. // All vectors are relative to the player's yaw,
  404. // (and pitch if pitch move mode enabled),
  405. // and will be rotated at the end
  406. v3f speedH, speedV; // Horizontal (X, Z) and Vertical (Y)
  407. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  408. bool fast_allowed = m_client->checkLocalPrivilege("fast");
  409. bool free_move = fly_allowed && player_settings.free_move;
  410. bool fast_move = fast_allowed && player_settings.fast_move;
  411. bool pitch_move = (free_move || in_liquid) && player_settings.pitch_move;
  412. // When aux1_descends is enabled the fast key is used to go down, so fast isn't possible
  413. bool fast_climb = fast_move && control.aux1 && !player_settings.aux1_descends;
  414. bool always_fly_fast = player_settings.always_fly_fast;
  415. // Whether superspeed mode is used or not
  416. bool superspeed = false;
  417. if (always_fly_fast && free_move && fast_move)
  418. superspeed = true;
  419. // Old descend control
  420. if (player_settings.aux1_descends) {
  421. // If free movement and fast movement, always move fast
  422. if (free_move && fast_move)
  423. superspeed = true;
  424. // Auxiliary button 1 (E)
  425. if (control.aux1) {
  426. if (free_move) {
  427. // In free movement mode, aux1 descends
  428. if (fast_move)
  429. speedV.Y = -movement_speed_fast;
  430. else
  431. speedV.Y = -movement_speed_walk;
  432. } else if (in_liquid || in_liquid_stable) {
  433. speedV.Y = -movement_speed_walk;
  434. swimming_vertical = true;
  435. } else if (is_climbing) {
  436. speedV.Y = -movement_speed_climb;
  437. } else {
  438. // If not free movement but fast is allowed, aux1 is
  439. // "Turbo button"
  440. if (fast_move)
  441. superspeed = true;
  442. }
  443. }
  444. } else {
  445. // New minecraft-like descend control
  446. // Auxiliary button 1 (E)
  447. if (control.aux1) {
  448. if (!is_climbing) {
  449. // aux1 is "Turbo button"
  450. if (fast_move)
  451. superspeed = true;
  452. }
  453. }
  454. if (control.sneak) {
  455. if (free_move) {
  456. // In free movement mode, sneak descends
  457. if (fast_move && (control.aux1 || always_fly_fast))
  458. speedV.Y = -movement_speed_fast;
  459. else
  460. speedV.Y = -movement_speed_walk;
  461. } else if (in_liquid || in_liquid_stable) {
  462. if (fast_climb)
  463. speedV.Y = -movement_speed_fast;
  464. else
  465. speedV.Y = -movement_speed_walk;
  466. swimming_vertical = true;
  467. } else if (is_climbing) {
  468. if (fast_climb)
  469. speedV.Y = -movement_speed_fast;
  470. else
  471. speedV.Y = -movement_speed_climb;
  472. }
  473. }
  474. }
  475. if (control.up)
  476. speedH += v3f(0.0f, 0.0f, 1.0f);
  477. if (control.down)
  478. speedH -= v3f(0.0f, 0.0f, 1.0f);
  479. if (!control.up && !control.down)
  480. speedH -= v3f(0.0f, 0.0f, 1.0f) * (control.forw_move_joystick_axis / 32767.f);
  481. if (control.left)
  482. speedH += v3f(-1.0f, 0.0f, 0.0f);
  483. if (control.right)
  484. speedH += v3f(1.0f, 0.0f, 0.0f);
  485. if (!control.left && !control.right)
  486. speedH += v3f(1.0f, 0.0f, 0.0f) * (control.sidew_move_joystick_axis / 32767.f);
  487. if (m_autojump) {
  488. // release autojump after a given time
  489. m_autojump_time -= dtime;
  490. if (m_autojump_time <= 0.0f)
  491. m_autojump = false;
  492. }
  493. if (control.jump) {
  494. if (free_move) {
  495. if (player_settings.aux1_descends || always_fly_fast) {
  496. if (fast_move)
  497. speedV.Y = movement_speed_fast;
  498. else
  499. speedV.Y = movement_speed_walk;
  500. } else {
  501. if (fast_move && control.aux1)
  502. speedV.Y = movement_speed_fast;
  503. else
  504. speedV.Y = movement_speed_walk;
  505. }
  506. } else if (m_can_jump) {
  507. /*
  508. NOTE: The d value in move() affects jump height by
  509. raising the height at which the jump speed is kept
  510. at its starting value
  511. */
  512. v3f speedJ = getSpeed();
  513. if (speedJ.Y >= -0.5f * BS) {
  514. speedJ.Y = movement_speed_jump * physics_override_jump;
  515. setSpeed(speedJ);
  516. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_JUMP));
  517. }
  518. } else if (in_liquid && !m_disable_jump) {
  519. if (fast_climb)
  520. speedV.Y = movement_speed_fast;
  521. else
  522. speedV.Y = movement_speed_walk;
  523. swimming_vertical = true;
  524. } else if (is_climbing && !m_disable_jump) {
  525. if (fast_climb)
  526. speedV.Y = movement_speed_fast;
  527. else
  528. speedV.Y = movement_speed_climb;
  529. }
  530. }
  531. // The speed of the player (Y is ignored)
  532. if (superspeed || (is_climbing && fast_climb) ||
  533. ((in_liquid || in_liquid_stable) && fast_climb))
  534. speedH = speedH.normalize() * movement_speed_fast;
  535. else if (control.sneak && !free_move && !in_liquid && !in_liquid_stable)
  536. speedH = speedH.normalize() * movement_speed_crouch;
  537. else
  538. speedH = speedH.normalize() * movement_speed_walk;
  539. // Acceleration increase
  540. f32 incH = 0.0f; // Horizontal (X, Z)
  541. f32 incV = 0.0f; // Vertical (Y)
  542. if ((!touching_ground && !free_move && !is_climbing && !in_liquid) ||
  543. (!free_move && m_can_jump && control.jump)) {
  544. // Jumping and falling
  545. if (superspeed || (fast_move && control.aux1))
  546. incH = movement_acceleration_fast * BS * dtime;
  547. else
  548. incH = movement_acceleration_air * BS * dtime;
  549. incV = 0.0f; // No vertical acceleration in air
  550. } else if (superspeed || (is_climbing && fast_climb) ||
  551. ((in_liquid || in_liquid_stable) && fast_climb)) {
  552. incH = incV = movement_acceleration_fast * BS * dtime;
  553. } else {
  554. incH = incV = movement_acceleration_default * BS * dtime;
  555. }
  556. float slip_factor = 1.0f;
  557. if (!free_move && !in_liquid && !in_liquid_stable)
  558. slip_factor = getSlipFactor(env, speedH);
  559. // Don't sink when swimming in pitch mode
  560. if (pitch_move && in_liquid) {
  561. v3f controlSpeed = speedH + speedV;
  562. if (controlSpeed.getLength() > 0.01f)
  563. swimming_pitch = true;
  564. }
  565. // Accelerate to target speed with maximum increment
  566. accelerate((speedH + speedV) * physics_override_speed,
  567. incH * physics_override_speed * slip_factor, incV * physics_override_speed,
  568. pitch_move);
  569. }
  570. v3s16 LocalPlayer::getStandingNodePos()
  571. {
  572. if (m_sneak_node_exists)
  573. return m_sneak_node;
  574. return m_standing_node;
  575. }
  576. v3s16 LocalPlayer::getFootstepNodePos()
  577. {
  578. // Emit swimming sound if the player is in liquid
  579. if (in_liquid_stable)
  580. return floatToInt(getPosition(), BS);
  581. // BS * 0.05 below the player's feet ensures a 1/16th height
  582. // nodebox is detected instead of the node below it.
  583. if (touching_ground)
  584. return floatToInt(getPosition() - v3f(0.0f, BS * 0.05f, 0.0f), BS);
  585. // A larger distance below is necessary for a footstep sound
  586. // when landing after a jump or fall. BS * 0.5 ensures water
  587. // sounds when swimming in 1 node deep water.
  588. return floatToInt(getPosition() - v3f(0.0f, BS * 0.5f, 0.0f), BS);
  589. }
  590. v3s16 LocalPlayer::getLightPosition() const
  591. {
  592. return floatToInt(m_position + v3f(0.0f, BS * 1.5f, 0.0f), BS);
  593. }
  594. v3f LocalPlayer::getEyeOffset() const
  595. {
  596. float eye_height = camera_barely_in_ceiling ? m_eye_height - 0.125f : m_eye_height;
  597. return v3f(0.0f, BS * eye_height, 0.0f);
  598. }
  599. ClientActiveObject *LocalPlayer::getParent() const
  600. {
  601. return m_cao ? m_cao->getParent() : nullptr;
  602. }
  603. bool LocalPlayer::isDead() const
  604. {
  605. FATAL_ERROR_IF(!getCAO(), "LocalPlayer's CAO isn't initialized");
  606. return !getCAO()->isImmortal() && hp == 0;
  607. }
  608. // 3D acceleration
  609. void LocalPlayer::accelerate(const v3f &target_speed, const f32 max_increase_H,
  610. const f32 max_increase_V, const bool use_pitch)
  611. {
  612. const f32 yaw = getYaw();
  613. const f32 pitch = getPitch();
  614. v3f flat_speed = m_speed;
  615. // Rotate speed vector by -yaw and -pitch to make it relative to the player's yaw and pitch
  616. flat_speed.rotateXZBy(-yaw);
  617. if (use_pitch)
  618. flat_speed.rotateYZBy(-pitch);
  619. v3f d_wanted = target_speed - flat_speed;
  620. v3f d;
  621. // Then compare the horizontal and vertical components with the wanted speed
  622. if (max_increase_H > 0.0f) {
  623. v3f d_wanted_H = d_wanted * v3f(1.0f, 0.0f, 1.0f);
  624. if (d_wanted_H.getLength() > max_increase_H)
  625. d += d_wanted_H.normalize() * max_increase_H;
  626. else
  627. d += d_wanted_H;
  628. }
  629. if (max_increase_V > 0.0f) {
  630. f32 d_wanted_V = d_wanted.Y;
  631. if (d_wanted_V > max_increase_V)
  632. d.Y += max_increase_V;
  633. else if (d_wanted_V < -max_increase_V)
  634. d.Y -= max_increase_V;
  635. else
  636. d.Y += d_wanted_V;
  637. }
  638. // Finally rotate it again
  639. if (use_pitch)
  640. d.rotateYZBy(pitch);
  641. d.rotateXZBy(yaw);
  642. m_speed += d;
  643. }
  644. // Temporary option for old move code
  645. void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
  646. std::vector<CollisionInfo> *collision_info)
  647. {
  648. Map *map = &env->getMap();
  649. const NodeDefManager *nodemgr = m_client->ndef();
  650. v3f position = getPosition();
  651. // Copy parent position if local player is attached
  652. if (getParent()) {
  653. setPosition(m_cao->getPosition());
  654. m_sneak_node_exists = false;
  655. added_velocity = v3f(0.0f);
  656. return;
  657. }
  658. PlayerSettings &player_settings = getPlayerSettings();
  659. // Skip collision detection if noclip mode is used
  660. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  661. bool noclip = m_client->checkLocalPrivilege("noclip") && player_settings.noclip;
  662. bool free_move = noclip && fly_allowed && player_settings.free_move;
  663. if (free_move) {
  664. position += m_speed * dtime;
  665. setPosition(position);
  666. touching_ground = false;
  667. m_sneak_node_exists = false;
  668. added_velocity = v3f(0.0f);
  669. return;
  670. }
  671. m_speed += added_velocity;
  672. added_velocity = v3f(0.0f);
  673. /*
  674. Collision detection
  675. */
  676. bool is_valid_position;
  677. MapNode node;
  678. v3s16 pp;
  679. /*
  680. Check if player is in liquid (the oscillating value)
  681. */
  682. if (in_liquid) {
  683. // If in liquid, the threshold of coming out is at higher y
  684. pp = floatToInt(position + v3f(0.0f, BS * 0.1f, 0.0f), BS);
  685. node = map->getNode(pp, &is_valid_position);
  686. if (is_valid_position) {
  687. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  688. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  689. } else {
  690. in_liquid = false;
  691. }
  692. } else {
  693. // If not in liquid, the threshold of going in is at lower y
  694. pp = floatToInt(position + v3f(0.0f, BS * 0.5f, 0.0f), BS);
  695. node = map->getNode(pp, &is_valid_position);
  696. if (is_valid_position) {
  697. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  698. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  699. } else {
  700. in_liquid = false;
  701. }
  702. }
  703. /*
  704. Check if player is in liquid (the stable value)
  705. */
  706. pp = floatToInt(position + v3f(0.0f), BS);
  707. node = map->getNode(pp, &is_valid_position);
  708. if (is_valid_position)
  709. in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
  710. else
  711. in_liquid_stable = false;
  712. /*
  713. Check if player is climbing
  714. */
  715. pp = floatToInt(position + v3f(0.0f, 0.5f * BS, 0.0f), BS);
  716. v3s16 pp2 = floatToInt(position + v3f(0.0f, -0.2f * BS, 0.0f), BS);
  717. node = map->getNode(pp, &is_valid_position);
  718. bool is_valid_position2;
  719. MapNode node2 = map->getNode(pp2, &is_valid_position2);
  720. if (!(is_valid_position && is_valid_position2))
  721. is_climbing = false;
  722. else
  723. is_climbing = (nodemgr->get(node.getContent()).climbable ||
  724. nodemgr->get(node2.getContent()).climbable) && !free_move;
  725. /*
  726. Collision uncertainty radius
  727. Make it a bit larger than the maximum distance of movement
  728. */
  729. //f32 d = pos_max_d * 1.1;
  730. // A fairly large value in here makes moving smoother
  731. f32 d = 0.15f * BS;
  732. // This should always apply, otherwise there are glitches
  733. sanity_check(d > pos_max_d);
  734. // Maximum distance over border for sneaking
  735. f32 sneak_max = BS * 0.4f;
  736. /*
  737. If sneaking, keep in range from the last walked node and don't
  738. fall off from it
  739. */
  740. if (control.sneak && m_sneak_node_exists &&
  741. !(fly_allowed && player_settings.free_move) && !in_liquid &&
  742. physics_override_sneak) {
  743. f32 maxd = 0.5f * BS + sneak_max;
  744. v3f lwn_f = intToFloat(m_sneak_node, BS);
  745. position.X = rangelim(position.X, lwn_f.X - maxd, lwn_f.X + maxd);
  746. position.Z = rangelim(position.Z, lwn_f.Z - maxd, lwn_f.Z + maxd);
  747. if (!is_climbing) {
  748. // Move up if necessary
  749. f32 new_y = (lwn_f.Y - 0.5f * BS) + m_sneak_node_bb_ymax;
  750. if (position.Y < new_y)
  751. position.Y = new_y;
  752. /*
  753. Collision seems broken, since player is sinking when
  754. sneaking over the edges of current sneaking_node.
  755. TODO (when fixed): Set Y-speed only to 0 when position.Y < new_y.
  756. */
  757. if (m_speed.Y < 0.0f)
  758. m_speed.Y = 0.0f;
  759. }
  760. }
  761. // TODO: This shouldn't be hardcoded but decided by the server
  762. float player_stepheight = touching_ground ? (BS * 0.6f) : (BS * 0.2f);
  763. v3f accel_f;
  764. const v3f initial_position = position;
  765. const v3f initial_speed = m_speed;
  766. collisionMoveResult result = collisionMoveSimple(env, m_client,
  767. pos_max_d, m_collisionbox, player_stepheight, dtime,
  768. &position, &m_speed, accel_f);
  769. // Positition was slightly changed; update standing node pos
  770. if (touching_ground)
  771. m_standing_node = floatToInt(m_position - v3f(0.0f, 0.1f * BS, 0.0f), BS);
  772. else
  773. m_standing_node = floatToInt(m_position, BS);
  774. /*
  775. If the player's feet touch the topside of any node, this is
  776. set to true.
  777. Player is allowed to jump when this is true.
  778. */
  779. bool touching_ground_was = touching_ground;
  780. touching_ground = result.touching_ground;
  781. //bool standing_on_unloaded = result.standing_on_unloaded;
  782. /*
  783. Check the nodes under the player to see from which node the
  784. player is sneaking from, if any. If the node from under
  785. the player has been removed, the player falls.
  786. */
  787. f32 position_y_mod = 0.05f * BS;
  788. if (m_sneak_node_bb_ymax > 0.0f)
  789. position_y_mod = m_sneak_node_bb_ymax - position_y_mod;
  790. v3s16 current_node = floatToInt(position - v3f(0.0f, position_y_mod, 0.0f), BS);
  791. if (m_sneak_node_exists &&
  792. nodemgr->get(map->getNode(m_old_node_below)).name == "air" &&
  793. m_old_node_below_type != "air") {
  794. // Old node appears to have been removed; that is,
  795. // it wasn't air before but now it is
  796. m_need_to_get_new_sneak_node = false;
  797. m_sneak_node_exists = false;
  798. } else if (nodemgr->get(map->getNode(current_node)).name != "air") {
  799. // We are on something, so make sure to recalculate the sneak
  800. // node.
  801. m_need_to_get_new_sneak_node = true;
  802. }
  803. if (m_need_to_get_new_sneak_node && physics_override_sneak) {
  804. m_sneak_node_bb_ymax = 0.0f;
  805. v3s16 pos_i_bottom = floatToInt(position - v3f(0.0f, position_y_mod, 0.0f), BS);
  806. v2f player_p2df(position.X, position.Z);
  807. f32 min_distance_f = 100000.0f * BS;
  808. // If already seeking from some node, compare to it.
  809. v3s16 new_sneak_node = m_sneak_node;
  810. for (s16 x= -1; x <= 1; x++)
  811. for (s16 z= -1; z <= 1; z++) {
  812. v3s16 p = pos_i_bottom + v3s16(x, 0, z);
  813. v3f pf = intToFloat(p, BS);
  814. v2f node_p2df(pf.X, pf.Z);
  815. f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
  816. f32 max_axis_distance_f = MYMAX(
  817. std::fabs(player_p2df.X - node_p2df.X),
  818. std::fabs(player_p2df.Y - node_p2df.Y));
  819. if (distance_f > min_distance_f ||
  820. max_axis_distance_f > 0.5f * BS + sneak_max + 0.1f * BS)
  821. continue;
  822. // The node to be sneaked on has to be walkable
  823. node = map->getNode(p, &is_valid_position);
  824. if (!is_valid_position || !nodemgr->get(node).walkable)
  825. continue;
  826. // And the node above it has to be nonwalkable
  827. node = map->getNode(p + v3s16(0, 1, 0), &is_valid_position);
  828. if (!is_valid_position || nodemgr->get(node).walkable)
  829. continue;
  830. // If not 'sneak_glitch' the node 2 nodes above it has to be nonwalkable
  831. if (!physics_override_sneak_glitch) {
  832. node = map->getNode(p + v3s16(0, 2, 0), &is_valid_position);
  833. if (!is_valid_position || nodemgr->get(node).walkable)
  834. continue;
  835. }
  836. min_distance_f = distance_f;
  837. new_sneak_node = p;
  838. }
  839. bool sneak_node_found = (min_distance_f < 100000.0f * BS * 0.9f);
  840. m_sneak_node = new_sneak_node;
  841. m_sneak_node_exists = sneak_node_found;
  842. if (sneak_node_found) {
  843. f32 cb_max = 0.0f;
  844. MapNode n = map->getNode(m_sneak_node);
  845. std::vector<aabb3f> nodeboxes;
  846. n.getCollisionBoxes(nodemgr, &nodeboxes);
  847. for (const auto &box : nodeboxes) {
  848. if (box.MaxEdge.Y > cb_max)
  849. cb_max = box.MaxEdge.Y;
  850. }
  851. m_sneak_node_bb_ymax = cb_max;
  852. }
  853. /*
  854. If sneaking, the player's collision box can be in air, so
  855. this has to be set explicitly
  856. */
  857. if (sneak_node_found && control.sneak)
  858. touching_ground = true;
  859. }
  860. /*
  861. Set new position but keep sneak node set
  862. */
  863. bool sneak_node_exists = m_sneak_node_exists;
  864. setPosition(position);
  865. m_sneak_node_exists = sneak_node_exists;
  866. /*
  867. Report collisions
  868. */
  869. // Don't report if flying
  870. if (collision_info && !(player_settings.free_move && fly_allowed)) {
  871. for (const auto &info : result.collisions) {
  872. collision_info->push_back(info);
  873. }
  874. }
  875. if (!result.standing_on_object && !touching_ground_was && touching_ground) {
  876. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_REGAIN_GROUND));
  877. // Set camera impact value to be used for view bobbing
  878. camera_impact = getSpeed().Y * -1.0f;
  879. }
  880. {
  881. camera_barely_in_ceiling = false;
  882. v3s16 camera_np = floatToInt(getEyePosition(), BS);
  883. MapNode n = map->getNode(camera_np);
  884. if (n.getContent() != CONTENT_IGNORE) {
  885. if (nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2)
  886. camera_barely_in_ceiling = true;
  887. }
  888. }
  889. /*
  890. Update the node last under the player
  891. */
  892. m_old_node_below = floatToInt(position - v3f(0.0f, BS / 2.0f, 0.0f), BS);
  893. m_old_node_below_type = nodemgr->get(map->getNode(m_old_node_below)).name;
  894. /*
  895. Check properties of the node on which the player is standing
  896. */
  897. const ContentFeatures &f = nodemgr->get(map->getNode(getStandingNodePos()));
  898. // Determine if jumping is possible
  899. m_disable_jump = itemgroup_get(f.groups, "disable_jump");
  900. m_can_jump = touching_ground && !m_disable_jump;
  901. // Jump key pressed while jumping off from a bouncy block
  902. if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") &&
  903. m_speed.Y >= -0.5f * BS) {
  904. float jumpspeed = movement_speed_jump * physics_override_jump;
  905. if (m_speed.Y > 1.0f) {
  906. // Reduce boost when speed already is high
  907. m_speed.Y += jumpspeed / (1.0f + (m_speed.Y / 16.0f));
  908. } else {
  909. m_speed.Y += jumpspeed;
  910. }
  911. setSpeed(m_speed);
  912. m_can_jump = false;
  913. }
  914. // Autojump
  915. handleAutojump(dtime, env, result, initial_position, initial_speed, pos_max_d);
  916. }
  917. float LocalPlayer::getSlipFactor(Environment *env, const v3f &speedH)
  918. {
  919. // Slip on slippery nodes
  920. const NodeDefManager *nodemgr = env->getGameDef()->ndef();
  921. Map *map = &env->getMap();
  922. const ContentFeatures &f = nodemgr->get(map->getNode(getStandingNodePos()));
  923. int slippery = 0;
  924. if (f.walkable)
  925. slippery = itemgroup_get(f.groups, "slippery");
  926. if (slippery >= 1) {
  927. if (speedH == v3f(0.0f))
  928. slippery *= 2;
  929. return core::clamp(1.0f / (slippery + 1), 0.001f, 1.0f);
  930. }
  931. return 1.0f;
  932. }
  933. void LocalPlayer::handleAutojump(f32 dtime, Environment *env,
  934. const collisionMoveResult &result, const v3f &initial_position,
  935. const v3f &initial_speed, f32 pos_max_d)
  936. {
  937. PlayerSettings &player_settings = getPlayerSettings();
  938. if (!player_settings.autojump)
  939. return;
  940. if (m_autojump)
  941. return;
  942. bool control_forward = control.up ||
  943. (!control.up && !control.down &&
  944. control.forw_move_joystick_axis < -0.05f);
  945. bool could_autojump =
  946. m_can_jump && !control.jump && !control.sneak && control_forward;
  947. if (!could_autojump)
  948. return;
  949. bool horizontal_collision = false;
  950. for (const auto &colinfo : result.collisions) {
  951. if (colinfo.type == COLLISION_NODE && colinfo.plane != 1) {
  952. horizontal_collision = true;
  953. break; // one is enough
  954. }
  955. }
  956. // must be running against something to trigger autojumping
  957. if (!horizontal_collision)
  958. return;
  959. // check for nodes above
  960. v3f headpos_min = m_position + m_collisionbox.MinEdge * 0.99f;
  961. v3f headpos_max = m_position + m_collisionbox.MaxEdge * 0.99f;
  962. headpos_min.Y = headpos_max.Y; // top face of collision box
  963. v3s16 ceilpos_min = floatToInt(headpos_min, BS) + v3s16(0, 1, 0);
  964. v3s16 ceilpos_max = floatToInt(headpos_max, BS) + v3s16(0, 1, 0);
  965. const NodeDefManager *ndef = env->getGameDef()->ndef();
  966. bool is_position_valid;
  967. for (s16 z = ceilpos_min.Z; z <= ceilpos_max.Z; ++z) {
  968. for (s16 x = ceilpos_min.X; x <= ceilpos_max.X; ++x) {
  969. MapNode n = env->getMap().getNode(v3s16(x, ceilpos_max.Y, z), &is_position_valid);
  970. if (!is_position_valid)
  971. break; // won't collide with the void outside
  972. if (n.getContent() == CONTENT_IGNORE)
  973. return; // players collide with ignore blocks -> same as walkable
  974. const ContentFeatures &f = ndef->get(n);
  975. if (f.walkable)
  976. return; // would bump head, don't jump
  977. }
  978. }
  979. float jump_height = 1.1f; // TODO: better than a magic number
  980. v3f jump_pos = initial_position + v3f(0.0f, jump_height * BS, 0.0f);
  981. v3f jump_speed = initial_speed;
  982. // try at peak of jump, zero step height
  983. collisionMoveResult jump_result = collisionMoveSimple(env, m_client, pos_max_d,
  984. m_collisionbox, 0.0f, dtime, &jump_pos, &jump_speed, v3f(0.0f));
  985. // see if we can get a little bit farther horizontally if we had
  986. // jumped
  987. v3f run_delta = m_position - initial_position;
  988. run_delta.Y = 0.0f;
  989. v3f jump_delta = jump_pos - initial_position;
  990. jump_delta.Y = 0.0f;
  991. if (jump_delta.getLengthSQ() > run_delta.getLengthSQ() * 1.01f) {
  992. m_autojump = true;
  993. m_autojump_time = 0.1f;
  994. }
  995. }