localplayer.cpp 38 KB

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