localplayer.cpp 32 KB

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