2
0

localplayer.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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 "event.h"
  18. #include "collision.h"
  19. #include "nodedef.h"
  20. #include "settings.h"
  21. #include "environment.h"
  22. #include "map.h"
  23. #include "client.h"
  24. #include "content_cao.h"
  25. /*
  26. LocalPlayer
  27. */
  28. LocalPlayer::LocalPlayer(Client *client, const char *name):
  29. Player(name, client->idef()),
  30. m_client(client)
  31. {
  32. }
  33. static aabb3f getNodeBoundingBox(const std::vector<aabb3f> &nodeboxes)
  34. {
  35. if (nodeboxes.empty())
  36. return aabb3f(0, 0, 0, 0, 0, 0);
  37. aabb3f b_max;
  38. std::vector<aabb3f>::const_iterator it = nodeboxes.begin();
  39. b_max = aabb3f(it->MinEdge, it->MaxEdge);
  40. ++it;
  41. for (; it != nodeboxes.end(); ++it)
  42. b_max.addInternalBox(*it);
  43. return b_max;
  44. }
  45. bool LocalPlayer::updateSneakNode(Map *map, const v3f &position,
  46. const v3f &sneak_max)
  47. {
  48. static const v3s16 dir9_center[9] = {
  49. v3s16( 0, 0, 0),
  50. v3s16( 1, 0, 0),
  51. v3s16(-1, 0, 0),
  52. v3s16( 0, 0, 1),
  53. v3s16( 0, 0, -1),
  54. v3s16( 1, 0, 1),
  55. v3s16(-1, 0, 1),
  56. v3s16( 1, 0, -1),
  57. v3s16(-1, 0, -1)
  58. };
  59. INodeDefManager *nodemgr = m_client->ndef();
  60. MapNode node;
  61. bool is_valid_position;
  62. bool new_sneak_node_exists = m_sneak_node_exists;
  63. // We want the top of the sneak node to be below the players feet
  64. f32 position_y_mod = 0.05 * BS;
  65. if (m_sneak_node_exists)
  66. position_y_mod = m_sneak_node_bb_top.MaxEdge.Y - position_y_mod;
  67. // Get position of current standing node
  68. const v3s16 current_node = floatToInt(position - v3f(0, position_y_mod, 0), BS);
  69. if (current_node != m_sneak_node) {
  70. new_sneak_node_exists = false;
  71. } else {
  72. node = map->getNodeNoEx(current_node, &is_valid_position);
  73. if (!is_valid_position || !nodemgr->get(node).walkable)
  74. new_sneak_node_exists = false;
  75. }
  76. // Keep old sneak node
  77. if (new_sneak_node_exists)
  78. return true;
  79. // Get new sneak node
  80. m_sneak_ladder_detected = false;
  81. f32 min_distance_f = 100000.0 * BS;
  82. for (const auto &d : dir9_center) {
  83. const v3s16 p = current_node + d;
  84. const v3f pf = intToFloat(p, BS);
  85. const v2f diff(position.X - pf.X, position.Z - pf.Z);
  86. f32 distance_f = diff.getLength();
  87. if (distance_f > min_distance_f ||
  88. fabs(diff.X) > (.5 + .1) * BS + sneak_max.X ||
  89. fabs(diff.Y) > (.5 + .1) * BS + sneak_max.Z)
  90. continue;
  91. // The node to be sneaked on has to be walkable
  92. node = map->getNodeNoEx(p, &is_valid_position);
  93. if (!is_valid_position || !nodemgr->get(node).walkable)
  94. continue;
  95. // And the node(s) above have to be nonwalkable
  96. bool ok = true;
  97. if (!physics_override_sneak_glitch) {
  98. u16 height = ceilf(
  99. (m_collisionbox.MaxEdge.Y - m_collisionbox.MinEdge.Y) / BS
  100. );
  101. for (u16 y = 1; y <= height; y++) {
  102. node = map->getNodeNoEx(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->getNodeNoEx(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->getNodeNoEx(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->getNodeNoEx(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->getNodeNoEx(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. if (!collision_info || collision_info->empty()) {
  145. // Node below the feet, update each ClientEnvironment::step()
  146. m_standing_node = floatToInt(m_position, BS) - v3s16(0, 1, 0);
  147. }
  148. // Temporary option for old move code
  149. if (!physics_override_new_move) {
  150. old_move(dtime, env, pos_max_d, collision_info);
  151. return;
  152. }
  153. Map *map = &env->getMap();
  154. INodeDefManager *nodemgr = m_client->ndef();
  155. v3f position = getPosition();
  156. // Copy parent position if local player is attached
  157. if (isAttached) {
  158. setPosition(overridePosition);
  159. return;
  160. }
  161. // Skip collision detection if noclip mode is used
  162. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  163. bool noclip = m_client->checkLocalPrivilege("noclip") &&
  164. g_settings->getBool("noclip");
  165. bool free_move = g_settings->getBool("free_move") && fly_allowed;
  166. if (noclip && free_move) {
  167. position += m_speed * dtime;
  168. setPosition(position);
  169. return;
  170. }
  171. /*
  172. Collision detection
  173. */
  174. bool is_valid_position;
  175. MapNode node;
  176. v3s16 pp;
  177. /*
  178. Check if player is in liquid (the oscillating value)
  179. */
  180. // If in liquid, the threshold of coming out is at higher y
  181. if (in_liquid)
  182. {
  183. pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
  184. node = map->getNodeNoEx(pp, &is_valid_position);
  185. if (is_valid_position) {
  186. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  187. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  188. } else {
  189. in_liquid = false;
  190. }
  191. }
  192. // If not in liquid, the threshold of going in is at lower y
  193. else
  194. {
  195. pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
  196. node = map->getNodeNoEx(pp, &is_valid_position);
  197. if (is_valid_position) {
  198. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  199. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  200. } else {
  201. in_liquid = false;
  202. }
  203. }
  204. /*
  205. Check if player is in liquid (the stable value)
  206. */
  207. pp = floatToInt(position + v3f(0,0,0), BS);
  208. node = map->getNodeNoEx(pp, &is_valid_position);
  209. if (is_valid_position) {
  210. in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
  211. } else {
  212. in_liquid_stable = false;
  213. }
  214. /*
  215. Check if player is climbing
  216. */
  217. pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
  218. v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
  219. node = map->getNodeNoEx(pp, &is_valid_position);
  220. bool is_valid_position2;
  221. MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2);
  222. if (!(is_valid_position && is_valid_position2)) {
  223. is_climbing = false;
  224. } else {
  225. is_climbing = (nodemgr->get(node.getContent()).climbable
  226. || nodemgr->get(node2.getContent()).climbable) && !free_move;
  227. }
  228. /*
  229. Collision uncertainty radius
  230. Make it a bit larger than the maximum distance of movement
  231. */
  232. //f32 d = pos_max_d * 1.1;
  233. // A fairly large value in here makes moving smoother
  234. f32 d = 0.15*BS;
  235. // This should always apply, otherwise there are glitches
  236. sanity_check(d > pos_max_d);
  237. // Player object property step height is multiplied by BS in
  238. // /src/script/common/c_content.cpp and /src/content_sao.cpp
  239. float player_stepheight = (m_cao == nullptr) ? 0.0f :
  240. (touching_ground ? m_cao->getStepHeight() : (0.2f * BS));
  241. // TODO this is a problematic hack.
  242. // Use a better implementation for autojump, or apply a custom stepheight
  243. // to all players, as this currently creates unintended special movement
  244. // abilities and advantages for Android players on a server.
  245. #ifdef __ANDROID__
  246. if (touching_ground)
  247. player_stepheight += (0.6f * BS);
  248. #endif
  249. v3f accel_f = v3f(0,0,0);
  250. collisionMoveResult result = collisionMoveSimple(env, m_client,
  251. pos_max_d, m_collisionbox, player_stepheight, dtime,
  252. &position, &m_speed, accel_f);
  253. bool could_sneak = control.sneak && !free_move && !in_liquid &&
  254. !is_climbing && physics_override_sneak;
  255. // Add new collisions to the vector
  256. if (collision_info && !free_move) {
  257. v3f diff = intToFloat(m_standing_node, BS) - position;
  258. f32 distance = diff.getLength();
  259. // Force update each ClientEnvironment::step()
  260. bool is_first = collision_info->empty();
  261. for (const auto &colinfo : result.collisions) {
  262. collision_info->push_back(colinfo);
  263. if (colinfo.type != COLLISION_NODE ||
  264. colinfo.new_speed.Y != 0 ||
  265. (could_sneak && m_sneak_node_exists))
  266. continue;
  267. diff = intToFloat(colinfo.node_p, BS) - position;
  268. // Find nearest colliding node
  269. f32 len = diff.getLength();
  270. if (is_first || len < distance) {
  271. m_standing_node = colinfo.node_p;
  272. distance = len;
  273. }
  274. }
  275. }
  276. /*
  277. If the player's feet touch the topside of any node, this is
  278. set to true.
  279. Player is allowed to jump when this is true.
  280. */
  281. bool touching_ground_was = touching_ground;
  282. touching_ground = result.touching_ground;
  283. bool sneak_can_jump = false;
  284. // Max. distance (X, Z) over border for sneaking determined by collision box
  285. // * 0.49 to keep the center just barely on the node
  286. v3f sneak_max = m_collisionbox.getExtent() * 0.49;
  287. if (m_sneak_ladder_detected) {
  288. // restore legacy behaviour (this makes the m_speed.Y hack necessary)
  289. sneak_max = v3f(0.4 * BS, 0, 0.4 * BS);
  290. }
  291. /*
  292. If sneaking, keep on top of last walked node and don't fall off
  293. */
  294. if (could_sneak && m_sneak_node_exists) {
  295. const v3f sn_f = intToFloat(m_sneak_node, BS);
  296. const v3f bmin = sn_f + m_sneak_node_bb_top.MinEdge;
  297. const v3f bmax = sn_f + m_sneak_node_bb_top.MaxEdge;
  298. const v3f old_pos = position;
  299. const v3f old_speed = m_speed;
  300. f32 y_diff = bmax.Y - position.Y;
  301. m_standing_node = m_sneak_node;
  302. // (BS * 0.6f) is the basic stepheight while standing on ground
  303. if (y_diff < BS * 0.6f) {
  304. // Only center player when they're on the node
  305. position.X = rangelim(position.X,
  306. bmin.X - sneak_max.X, bmax.X + sneak_max.X);
  307. position.Z = rangelim(position.Z,
  308. bmin.Z - sneak_max.Z, bmax.Z + sneak_max.Z);
  309. if (position.X != old_pos.X)
  310. m_speed.X = 0;
  311. if (position.Z != old_pos.Z)
  312. m_speed.Z = 0;
  313. }
  314. if (y_diff > 0 && m_speed.Y < 0 &&
  315. (physics_override_sneak_glitch || y_diff < BS * 0.6f)) {
  316. // Move player to the maximal height when falling or when
  317. // the ledge is climbed on the next step.
  318. position.Y = bmax.Y;
  319. m_speed.Y = 0;
  320. }
  321. // Allow jumping on node edges while sneaking
  322. if (m_speed.Y == 0 || m_sneak_ladder_detected)
  323. sneak_can_jump = true;
  324. if (collision_info &&
  325. m_speed.Y - old_speed.Y > BS) {
  326. // Collide with sneak node, report fall damage
  327. CollisionInfo sn_info;
  328. sn_info.node_p = m_sneak_node;
  329. sn_info.old_speed = old_speed;
  330. sn_info.new_speed = m_speed;
  331. collision_info->push_back(sn_info);
  332. }
  333. }
  334. /*
  335. Find the next sneak node if necessary
  336. */
  337. bool new_sneak_node_exists = false;
  338. if (could_sneak)
  339. new_sneak_node_exists = updateSneakNode(map, position, sneak_max);
  340. /*
  341. Set new position but keep sneak node set
  342. */
  343. setPosition(position);
  344. m_sneak_node_exists = new_sneak_node_exists;
  345. /*
  346. Report collisions
  347. */
  348. if(!result.standing_on_object && !touching_ground_was && touching_ground) {
  349. MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
  350. m_client->event()->put(e);
  351. // Set camera impact value to be used for view bobbing
  352. camera_impact = getSpeed().Y * -1;
  353. }
  354. {
  355. camera_barely_in_ceiling = false;
  356. v3s16 camera_np = floatToInt(getEyePosition(), BS);
  357. MapNode n = map->getNodeNoEx(camera_np);
  358. if(n.getContent() != CONTENT_IGNORE){
  359. if(nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2){
  360. camera_barely_in_ceiling = true;
  361. }
  362. }
  363. }
  364. /*
  365. Check properties of the node on which the player is standing
  366. */
  367. const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(m_standing_node));
  368. // Determine if jumping is possible
  369. m_can_jump = (touching_ground && !in_liquid && !is_climbing)
  370. || sneak_can_jump;
  371. if (itemgroup_get(f.groups, "disable_jump"))
  372. m_can_jump = false;
  373. // Jump key pressed while jumping off from a bouncy block
  374. if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") &&
  375. m_speed.Y >= -0.5 * BS) {
  376. float jumpspeed = movement_speed_jump * physics_override_jump;
  377. if (m_speed.Y > 1) {
  378. // Reduce boost when speed already is high
  379. m_speed.Y += jumpspeed / (1 + (m_speed.Y / 16 ));
  380. } else {
  381. m_speed.Y += jumpspeed;
  382. }
  383. setSpeed(m_speed);
  384. m_can_jump = false;
  385. }
  386. }
  387. void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d)
  388. {
  389. move(dtime, env, pos_max_d, NULL);
  390. }
  391. void LocalPlayer::applyControl(float dtime, Environment *env)
  392. {
  393. // Clear stuff
  394. swimming_vertical = 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(isAttached)
  399. {
  400. setSpeed(v3f(0,0,0));
  401. return;
  402. }
  403. v3f move_direction = v3f(0,0,1);
  404. move_direction.rotateXZBy(getYaw());
  405. v3f speedH = v3f(0,0,0); // Horizontal (X, Z)
  406. v3f speedV = v3f(0,0,0); // Vertical (Y)
  407. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  408. bool fast_allowed = m_client->checkLocalPrivilege("fast");
  409. bool free_move = fly_allowed && g_settings->getBool("free_move");
  410. bool fast_move = fast_allowed && g_settings->getBool("fast_move");
  411. // When aux1_descends is enabled the fast key is used to go down, so fast isn't possible
  412. bool fast_climb = fast_move && control.aux1 && !g_settings->getBool("aux1_descends");
  413. bool continuous_forward = g_settings->getBool("continuous_forward");
  414. bool always_fly_fast = g_settings->getBool("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(g_settings->getBool("aux1_descends"))
  421. {
  422. // If free movement and fast movement, always move fast
  423. if(free_move && fast_move)
  424. superspeed = true;
  425. // Auxiliary button 1 (E)
  426. if(control.aux1)
  427. {
  428. if(free_move)
  429. {
  430. // In free movement mode, aux1 descends
  431. if(fast_move)
  432. speedV.Y = -movement_speed_fast;
  433. else
  434. speedV.Y = -movement_speed_walk;
  435. }
  436. else if(in_liquid || in_liquid_stable)
  437. {
  438. speedV.Y = -movement_speed_walk;
  439. swimming_vertical = true;
  440. }
  441. else if(is_climbing)
  442. {
  443. speedV.Y = -movement_speed_climb;
  444. }
  445. else
  446. {
  447. // If not free movement but fast is allowed, aux1 is
  448. // "Turbo button"
  449. if(fast_move)
  450. superspeed = true;
  451. }
  452. }
  453. }
  454. // New minecraft-like descend control
  455. else
  456. {
  457. // Auxiliary button 1 (E)
  458. if(control.aux1)
  459. {
  460. if(!is_climbing)
  461. {
  462. // aux1 is "Turbo button"
  463. if(fast_move)
  464. superspeed = true;
  465. }
  466. }
  467. if(control.sneak)
  468. {
  469. if(free_move)
  470. {
  471. // In free movement mode, sneak descends
  472. if (fast_move && (control.aux1 || always_fly_fast))
  473. speedV.Y = -movement_speed_fast;
  474. else
  475. speedV.Y = -movement_speed_walk;
  476. }
  477. else if(in_liquid || in_liquid_stable)
  478. {
  479. if(fast_climb)
  480. speedV.Y = -movement_speed_fast;
  481. else
  482. speedV.Y = -movement_speed_walk;
  483. swimming_vertical = true;
  484. }
  485. else if(is_climbing)
  486. {
  487. if(fast_climb)
  488. speedV.Y = -movement_speed_fast;
  489. else
  490. speedV.Y = -movement_speed_climb;
  491. }
  492. }
  493. }
  494. if (continuous_forward)
  495. speedH += move_direction;
  496. if (control.up) {
  497. if (continuous_forward) {
  498. if (fast_move)
  499. superspeed = true;
  500. } else {
  501. speedH += move_direction;
  502. }
  503. }
  504. if (control.down) {
  505. speedH -= move_direction;
  506. }
  507. if (!control.up && !control.down) {
  508. speedH -= move_direction *
  509. (control.forw_move_joystick_axis / 32767.f);
  510. }
  511. if (control.left) {
  512. speedH += move_direction.crossProduct(v3f(0,1,0));
  513. }
  514. if (control.right) {
  515. speedH += move_direction.crossProduct(v3f(0,-1,0));
  516. }
  517. if (!control.left && !control.right) {
  518. speedH -= move_direction.crossProduct(v3f(0,1,0)) *
  519. (control.sidew_move_joystick_axis / 32767.f);
  520. }
  521. if(control.jump)
  522. {
  523. if (free_move) {
  524. if (g_settings->getBool("aux1_descends") || always_fly_fast) {
  525. if (fast_move)
  526. speedV.Y = movement_speed_fast;
  527. else
  528. speedV.Y = movement_speed_walk;
  529. } else {
  530. if(fast_move && control.aux1)
  531. speedV.Y = movement_speed_fast;
  532. else
  533. speedV.Y = movement_speed_walk;
  534. }
  535. }
  536. else if(m_can_jump)
  537. {
  538. /*
  539. NOTE: The d value in move() affects jump height by
  540. raising the height at which the jump speed is kept
  541. at its starting value
  542. */
  543. v3f speedJ = getSpeed();
  544. if(speedJ.Y >= -0.5 * BS) {
  545. speedJ.Y = movement_speed_jump * physics_override_jump;
  546. setSpeed(speedJ);
  547. MtEvent *e = new SimpleTriggerEvent("PlayerJump");
  548. m_client->event()->put(e);
  549. }
  550. }
  551. else if(in_liquid)
  552. {
  553. if(fast_climb)
  554. speedV.Y = movement_speed_fast;
  555. else
  556. speedV.Y = movement_speed_walk;
  557. swimming_vertical = true;
  558. }
  559. else if(is_climbing)
  560. {
  561. if(fast_climb)
  562. speedV.Y = movement_speed_fast;
  563. else
  564. speedV.Y = movement_speed_climb;
  565. }
  566. }
  567. // The speed of the player (Y is ignored)
  568. if(superspeed || (is_climbing && fast_climb) || ((in_liquid || in_liquid_stable) && fast_climb))
  569. speedH = speedH.normalize() * movement_speed_fast;
  570. else if(control.sneak && !free_move && !in_liquid && !in_liquid_stable)
  571. speedH = speedH.normalize() * movement_speed_crouch;
  572. else
  573. speedH = speedH.normalize() * movement_speed_walk;
  574. // Acceleration increase
  575. f32 incH = 0; // Horizontal (X, Z)
  576. f32 incV = 0; // Vertical (Y)
  577. if((!touching_ground && !free_move && !is_climbing && !in_liquid) || (!free_move && m_can_jump && control.jump))
  578. {
  579. // Jumping and falling
  580. if(superspeed || (fast_move && control.aux1))
  581. incH = movement_acceleration_fast * BS * dtime;
  582. else
  583. incH = movement_acceleration_air * BS * dtime;
  584. incV = 0; // No vertical acceleration in air
  585. }
  586. else if (superspeed || (is_climbing && fast_climb) || ((in_liquid || in_liquid_stable) && fast_climb))
  587. incH = incV = movement_acceleration_fast * BS * dtime;
  588. else
  589. incH = incV = movement_acceleration_default * BS * dtime;
  590. float slip_factor = getSlipFactor(env, speedH);
  591. // Accelerate to target speed with maximum increment
  592. accelerateHorizontal(speedH * physics_override_speed,
  593. incH * physics_override_speed * slip_factor);
  594. accelerateVertical(speedV * physics_override_speed,
  595. incV * physics_override_speed);
  596. }
  597. v3s16 LocalPlayer::getStandingNodePos()
  598. {
  599. if(m_sneak_node_exists)
  600. return m_sneak_node;
  601. return m_standing_node;
  602. }
  603. v3s16 LocalPlayer::getFootstepNodePos()
  604. {
  605. if (in_liquid_stable)
  606. // Emit swimming sound if the player is in liquid
  607. return floatToInt(getPosition(), BS);
  608. if (touching_ground)
  609. // BS * 0.05 below the player's feet ensures a 1/16th height
  610. // nodebox is detected instead of the node below it.
  611. return floatToInt(getPosition() - v3f(0, BS * 0.05f, 0), BS);
  612. // A larger distance below is necessary for a footstep sound
  613. // when landing after a jump or fall. BS * 0.5 ensures water
  614. // sounds when swimming in 1 node deep water.
  615. return floatToInt(getPosition() - v3f(0, BS * 0.5f, 0), BS);
  616. }
  617. v3s16 LocalPlayer::getLightPosition() const
  618. {
  619. return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
  620. }
  621. v3f LocalPlayer::getEyeOffset() const
  622. {
  623. float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f;
  624. return v3f(0, BS * eye_height, 0);
  625. }
  626. // Horizontal acceleration (X and Z), Y direction is ignored
  627. void LocalPlayer::accelerateHorizontal(const v3f &target_speed,
  628. const f32 max_increase)
  629. {
  630. if (max_increase == 0)
  631. return;
  632. v3f d_wanted = target_speed - m_speed;
  633. d_wanted.Y = 0.0f;
  634. f32 dl = d_wanted.getLength();
  635. if (dl > max_increase)
  636. dl = max_increase;
  637. v3f d = d_wanted.normalize() * dl;
  638. m_speed.X += d.X;
  639. m_speed.Z += d.Z;
  640. }
  641. // Vertical acceleration (Y), X and Z directions are ignored
  642. void LocalPlayer::accelerateVertical(const v3f &target_speed, const f32 max_increase)
  643. {
  644. if (max_increase == 0)
  645. return;
  646. f32 d_wanted = target_speed.Y - m_speed.Y;
  647. if (d_wanted > max_increase)
  648. d_wanted = max_increase;
  649. else if (d_wanted < -max_increase)
  650. d_wanted = -max_increase;
  651. m_speed.Y += d_wanted;
  652. }
  653. // Temporary option for old move code
  654. void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
  655. std::vector<CollisionInfo> *collision_info)
  656. {
  657. Map *map = &env->getMap();
  658. INodeDefManager *nodemgr = m_client->ndef();
  659. v3f position = getPosition();
  660. // Copy parent position if local player is attached
  661. if (isAttached) {
  662. setPosition(overridePosition);
  663. m_sneak_node_exists = false;
  664. return;
  665. }
  666. // Skip collision detection if noclip mode is used
  667. bool fly_allowed = m_client->checkLocalPrivilege("fly");
  668. bool noclip = m_client->checkLocalPrivilege("noclip") &&
  669. g_settings->getBool("noclip");
  670. bool free_move = noclip && fly_allowed && g_settings->getBool("free_move");
  671. if (free_move) {
  672. position += m_speed * dtime;
  673. setPosition(position);
  674. m_sneak_node_exists = false;
  675. return;
  676. }
  677. /*
  678. Collision detection
  679. */
  680. bool is_valid_position;
  681. MapNode node;
  682. v3s16 pp;
  683. /*
  684. Check if player is in liquid (the oscillating value)
  685. */
  686. if (in_liquid) {
  687. // If in liquid, the threshold of coming out is at higher y
  688. pp = floatToInt(position + v3f(0, BS * 0.1, 0), BS);
  689. node = map->getNodeNoEx(pp, &is_valid_position);
  690. if (is_valid_position) {
  691. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  692. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  693. } else {
  694. in_liquid = false;
  695. }
  696. } else {
  697. // If not in liquid, the threshold of going in is at lower y
  698. pp = floatToInt(position + v3f(0, BS * 0.5, 0), BS);
  699. node = map->getNodeNoEx(pp, &is_valid_position);
  700. if (is_valid_position) {
  701. in_liquid = nodemgr->get(node.getContent()).isLiquid();
  702. liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
  703. } else {
  704. in_liquid = false;
  705. }
  706. }
  707. /*
  708. Check if player is in liquid (the stable value)
  709. */
  710. pp = floatToInt(position + v3f(0, 0, 0), BS);
  711. node = map->getNodeNoEx(pp, &is_valid_position);
  712. if (is_valid_position)
  713. in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
  714. else
  715. in_liquid_stable = false;
  716. /*
  717. Check if player is climbing
  718. */
  719. pp = floatToInt(position + v3f(0, 0.5 * BS, 0), BS);
  720. v3s16 pp2 = floatToInt(position + v3f(0, -0.2 * BS, 0), BS);
  721. node = map->getNodeNoEx(pp, &is_valid_position);
  722. bool is_valid_position2;
  723. MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2);
  724. if (!(is_valid_position && is_valid_position2))
  725. is_climbing = false;
  726. else
  727. is_climbing = (nodemgr->get(node.getContent()).climbable ||
  728. nodemgr->get(node2.getContent()).climbable) && !free_move;
  729. /*
  730. Collision uncertainty radius
  731. Make it a bit larger than the maximum distance of movement
  732. */
  733. //f32 d = pos_max_d * 1.1;
  734. // A fairly large value in here makes moving smoother
  735. f32 d = 0.15 * BS;
  736. // This should always apply, otherwise there are glitches
  737. sanity_check(d > pos_max_d);
  738. // Maximum distance over border for sneaking
  739. f32 sneak_max = BS * 0.4;
  740. /*
  741. If sneaking, keep in range from the last walked node and don't
  742. fall off from it
  743. */
  744. if (control.sneak && m_sneak_node_exists &&
  745. !(fly_allowed && g_settings->getBool("free_move")) && !in_liquid &&
  746. physics_override_sneak) {
  747. f32 maxd = 0.5 * BS + sneak_max;
  748. v3f lwn_f = intToFloat(m_sneak_node, BS);
  749. position.X = rangelim(position.X, lwn_f.X - maxd, lwn_f.X + maxd);
  750. position.Z = rangelim(position.Z, lwn_f.Z - maxd, lwn_f.Z + maxd);
  751. if (!is_climbing) {
  752. // Move up if necessary
  753. f32 new_y = (lwn_f.Y - 0.5 * BS) + m_sneak_node_bb_ymax;
  754. if (position.Y < new_y)
  755. position.Y = new_y;
  756. /*
  757. Collision seems broken, since player is sinking when
  758. sneaking over the edges of current sneaking_node.
  759. TODO (when fixed): Set Y-speed only to 0 when position.Y < new_y.
  760. */
  761. if (m_speed.Y < 0)
  762. m_speed.Y = 0;
  763. }
  764. }
  765. // this shouldn't be hardcoded but transmitted from server
  766. float player_stepheight = touching_ground ? (BS * 0.6) : (BS * 0.2);
  767. #ifdef __ANDROID__
  768. player_stepheight += (0.6 * BS);
  769. #endif
  770. v3f accel_f = v3f(0, 0, 0);
  771. collisionMoveResult result = collisionMoveSimple(env, m_client,
  772. pos_max_d, m_collisionbox, player_stepheight, dtime,
  773. &position, &m_speed, accel_f);
  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.05 * BS;
  788. if (m_sneak_node_bb_ymax > 0)
  789. position_y_mod = m_sneak_node_bb_ymax - position_y_mod;
  790. v3s16 current_node = floatToInt(position - v3f(0, position_y_mod, 0), BS);
  791. if (m_sneak_node_exists &&
  792. nodemgr->get(map->getNodeNoEx(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->getNodeNoEx(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;
  805. v3s16 pos_i_bottom = floatToInt(position - v3f(0, position_y_mod, 0), BS);
  806. v2f player_p2df(position.X, position.Z);
  807. f32 min_distance_f = 100000.0 * 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. fabs(player_p2df.X - node_p2df.X),
  818. fabs(player_p2df.Y - node_p2df.Y));
  819. if (distance_f > min_distance_f ||
  820. max_axis_distance_f > 0.5 * BS + sneak_max + 0.1 * BS)
  821. continue;
  822. // The node to be sneaked on has to be walkable
  823. node = map->getNodeNoEx(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->getNodeNoEx(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->getNodeNoEx(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.0 * BS * 0.9);
  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;
  844. MapNode n = map->getNodeNoEx(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. // Dont report if flying
  870. if (collision_info && !(g_settings->getBool("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. MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
  877. m_client->event()->put(e);
  878. // Set camera impact value to be used for view bobbing
  879. camera_impact = getSpeed().Y * -1;
  880. }
  881. {
  882. camera_barely_in_ceiling = false;
  883. v3s16 camera_np = floatToInt(getEyePosition(), BS);
  884. MapNode n = map->getNodeNoEx(camera_np);
  885. if (n.getContent() != CONTENT_IGNORE) {
  886. if (nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2)
  887. camera_barely_in_ceiling = true;
  888. }
  889. }
  890. /*
  891. Update the node last under the player
  892. */
  893. m_old_node_below = floatToInt(position - v3f(0, BS / 2, 0), BS);
  894. m_old_node_below_type = nodemgr->get(map->getNodeNoEx(m_old_node_below)).name;
  895. /*
  896. Check properties of the node on which the player is standing
  897. */
  898. const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(getStandingNodePos()));
  899. // Determine if jumping is possible
  900. m_can_jump = touching_ground && !in_liquid;
  901. if (itemgroup_get(f.groups, "disable_jump"))
  902. m_can_jump = false;
  903. // Jump key pressed while jumping off from a bouncy block
  904. if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") &&
  905. m_speed.Y >= -0.5 * BS) {
  906. float jumpspeed = movement_speed_jump * physics_override_jump;
  907. if (m_speed.Y > 1) {
  908. // Reduce boost when speed already is high
  909. m_speed.Y += jumpspeed / (1 + (m_speed.Y / 16 ));
  910. } else {
  911. m_speed.Y += jumpspeed;
  912. }
  913. setSpeed(m_speed);
  914. m_can_jump = false;
  915. }
  916. }
  917. float LocalPlayer::getSlipFactor(Environment *env, const v3f &speedH)
  918. {
  919. if (!touching_ground)
  920. return 1.0f;
  921. float slip_factor = 1.0f;
  922. // Slip on slippery nodes
  923. const INodeDefManager *nodemgr = env->getGameDef()->ndef();
  924. Map *map = &env->getMap();
  925. const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(
  926. floatToInt(getPosition() - v3f(0, 0.05f * BS, 0), BS)));
  927. int slippery = 0;
  928. if (f.walkable) {
  929. slippery = itemgroup_get(f.groups, "slippery");
  930. } else if (is_slipping) {
  931. // slipping over an edge? Check surroundings for slippery nodes
  932. slippery = 2 << 16; // guard value, bigger than all realistic ones
  933. for (int z = 0; z <= 1; z++) {
  934. for (int x = 0; x <= 1; x++) {
  935. // this should cover all nodes surrounding player position
  936. v3f offset((x - 0.5f) * BS, 0.05f * BS, (z - 0.5f) * BS);
  937. const ContentFeatures &f2 = nodemgr->get(map->getNodeNoEx(
  938. floatToInt(getPosition() - offset, BS)));
  939. if (f2.walkable) {
  940. // find least slippery node we might be standing on
  941. int s = itemgroup_get(f2.groups, "slippery");
  942. if (s < slippery)
  943. slippery = s;
  944. }
  945. }
  946. }
  947. // without any hits, ignore slippery
  948. if (slippery >= (2 << 16))
  949. slippery = 0;
  950. }
  951. if (slippery >= 1) {
  952. if (speedH == v3f(0.0f)) {
  953. slippery = slippery * 2;
  954. }
  955. slip_factor = core::clamp(1.0f / (slippery + 1), 0.001f, 1.0f);
  956. is_slipping = true;
  957. } else {
  958. // remember this to avoid checking the edge case above too often
  959. is_slipping = false;
  960. }
  961. return slip_factor;
  962. }