localplayer.cpp 30 KB

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