]> git.lizzy.rs Git - minetest.git/blob - src/client/camera.cpp
ba621d15bd7f9232537f76b471a51056780aea10
[minetest.git] / src / client / camera.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "camera.h"
21 #include "debug.h"
22 #include "client.h"
23 #include "config.h"
24 #include "map.h"
25 #include "clientmap.h"     // MapDrawControl
26 #include "player.h"
27 #include <cmath>
28 #include "client/renderingengine.h"
29 #include "client/content_cao.h"
30 #include "settings.h"
31 #include "wieldmesh.h"
32 #include "noise.h"         // easeCurve
33 #include "sound.h"
34 #include "mtevent.h"
35 #include "nodedef.h"
36 #include "util/numeric.h"
37 #include "constants.h"
38 #include "fontengine.h"
39 #include "script/scripting_client.h"
40 #include "gettext.h"
41 #include <SViewFrustum.h>
42
43 #define CAMERA_OFFSET_STEP 200
44 #define WIELDMESH_OFFSET_X 55.0f
45 #define WIELDMESH_OFFSET_Y -35.0f
46 #define WIELDMESH_AMPLITUDE_X 7.0f
47 #define WIELDMESH_AMPLITUDE_Y 10.0f
48
49 Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine):
50         m_draw_control(draw_control),
51         m_client(client),
52         m_player_light_color(0xFFFFFFFF)
53 {
54         auto smgr = rendering_engine->get_scene_manager();
55         // note: making the camera node a child of the player node
56         // would lead to unexpected behavior, so we don't do that.
57         m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
58         m_headnode = smgr->addEmptySceneNode(m_playernode);
59         m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
60         m_cameranode->bindTargetAndRotation(true);
61
62         // This needs to be in its own scene manager. It is drawn after
63         // all other 3D scene nodes and before the GUI.
64         m_wieldmgr = smgr->createNewSceneManager();
65         m_wieldmgr->addCameraSceneNode();
66         m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
67         m_wieldnode->setItem(ItemStack(), m_client);
68         m_wieldnode->drop(); // m_wieldmgr grabbed it
69
70         /* TODO: Add a callback function so these can be updated when a setting
71          *       changes.  At this point in time it doesn't matter (e.g. /set
72          *       is documented to change server settings only)
73          *
74          * TODO: Local caching of settings is not optimal and should at some stage
75          *       be updated to use a global settings object for getting thse values
76          *       (as opposed to the this local caching). This can be addressed in
77          *       a later release.
78          */
79         m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount", 0.0f, 100.0f);
80         m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount", 0.0f, 7.9f);
81         // 45 degrees is the lowest FOV that doesn't cause the server to treat this
82         // as a zoom FOV and load world beyond the set server limits.
83         m_cache_fov                 = g_settings->getFloat("fov", 45.0f, 160.0f);
84         m_arm_inertia               = g_settings->getBool("arm_inertia");
85         m_nametags.clear();
86         m_show_nametag_backgrounds  = g_settings->getBool("show_nametag_backgrounds");
87 }
88
89 Camera::~Camera()
90 {
91         m_wieldmgr->drop();
92 }
93
94 void Camera::notifyFovChange()
95 {
96         LocalPlayer *player = m_client->getEnv().getLocalPlayer();
97         assert(player);
98
99         PlayerFovSpec spec = player->getFov();
100
101         /*
102          * Update m_old_fov_degrees first - it serves as the starting point of the
103          * upcoming transition.
104          *
105          * If an FOV transition is already active, mark current FOV as the start of
106          * the new transition. If not, set it to the previous transition's target FOV.
107          */
108         if (m_fov_transition_active)
109                 m_old_fov_degrees = m_curr_fov_degrees;
110         else
111                 m_old_fov_degrees = m_server_sent_fov ? m_target_fov_degrees : m_cache_fov;
112
113         /*
114          * Update m_server_sent_fov next - it corresponds to the target FOV of the
115          * upcoming transition.
116          *
117          * Set it to m_cache_fov, if server-sent FOV is 0. Otherwise check if
118          * server-sent FOV is a multiplier, and multiply it with m_cache_fov instead
119          * of overriding.
120          */
121         if (spec.fov == 0.0f) {
122                 m_server_sent_fov = false;
123                 m_target_fov_degrees = m_cache_fov;
124         } else {
125                 m_server_sent_fov = true;
126                 m_target_fov_degrees = spec.is_multiplier ? m_cache_fov * spec.fov : spec.fov;
127         }
128
129         if (spec.transition_time > 0.0f)
130                 m_fov_transition_active = true;
131
132         // If FOV smooth transition is active, initialize required variables
133         if (m_fov_transition_active) {
134                 m_transition_time = spec.transition_time;
135                 m_fov_diff = m_target_fov_degrees - m_old_fov_degrees;
136         }
137 }
138
139 // Returns the fractional part of x
140 inline f32 my_modf(f32 x)
141 {
142         double dummy;
143         return modf(x, &dummy);
144 }
145
146 void Camera::step(f32 dtime)
147 {
148         if(m_view_bobbing_fall > 0)
149         {
150                 m_view_bobbing_fall -= 3 * dtime;
151                 if(m_view_bobbing_fall <= 0)
152                         m_view_bobbing_fall = -1; // Mark the effect as finished
153         }
154
155         bool was_under_zero = m_wield_change_timer < 0;
156         m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
157
158         if (m_wield_change_timer >= 0 && was_under_zero) {
159                 m_wieldnode->setItem(m_wield_item_next, m_client);
160                 m_wieldnode->setNodeLightColor(m_player_light_color);
161         }
162
163         if (m_view_bobbing_state != 0)
164         {
165                 //f32 offset = dtime * m_view_bobbing_speed * 0.035;
166                 f32 offset = dtime * m_view_bobbing_speed * 0.030;
167                 if (m_view_bobbing_state == 2) {
168                         // Animation is getting turned off
169                         if (m_view_bobbing_anim < 0.25) {
170                                 m_view_bobbing_anim -= offset;
171                         } else if (m_view_bobbing_anim > 0.75) {
172                                 m_view_bobbing_anim += offset;
173                         } else if (m_view_bobbing_anim < 0.5) {
174                                 m_view_bobbing_anim += offset;
175                                 if (m_view_bobbing_anim > 0.5)
176                                         m_view_bobbing_anim = 0.5;
177                         } else {
178                                 m_view_bobbing_anim -= offset;
179                                 if (m_view_bobbing_anim < 0.5)
180                                         m_view_bobbing_anim = 0.5;
181                         }
182
183                         if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
184                                         fabs(m_view_bobbing_anim - 0.5) < 0.01) {
185                                 m_view_bobbing_anim = 0;
186                                 m_view_bobbing_state = 0;
187                         }
188                 }
189                 else {
190                         float was = m_view_bobbing_anim;
191                         m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
192                         bool step = (was == 0 ||
193                                         (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
194                                         (was > 0.5f && m_view_bobbing_anim <= 0.5f));
195                         if(step) {
196                                 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
197                         }
198                 }
199         }
200
201         if (m_digging_button != -1) {
202                 f32 offset = dtime * 3.5f;
203                 float m_digging_anim_was = m_digging_anim;
204                 m_digging_anim += offset;
205                 if (m_digging_anim >= 1)
206                 {
207                         m_digging_anim = 0;
208                         m_digging_button = -1;
209                 }
210                 float lim = 0.15;
211                 if(m_digging_anim_was < lim && m_digging_anim >= lim)
212                 {
213                         if (m_digging_button == 0) {
214                                 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
215                         } else if(m_digging_button == 1) {
216                                 m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
217                         }
218                 }
219         }
220 }
221
222 static inline v2f dir(const v2f &pos_dist)
223 {
224         f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
225         f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
226
227         f32 x_abs = std::fabs(x);
228         f32 y_abs = std::fabs(y);
229
230         if (x_abs >= y_abs) {
231                 y *= (1.0f / x_abs);
232                 x /= x_abs;
233         }
234
235         if (y_abs >= x_abs) {
236                 x *= (1.0f / y_abs);
237                 y /= y_abs;
238         }
239
240         return v2f(std::fabs(x), std::fabs(y));
241 }
242
243 void Camera::addArmInertia(f32 player_yaw)
244 {
245         m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
246                 -100.0f, 100.0f) / 0.016f) * 0.01f;
247         m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
248         f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
249         f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
250
251         if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
252                 /*
253                     The arm moves relative to the camera speed,
254                     with an acceleration factor.
255                 */
256
257                 if (m_cam_vel.X > 1.0f) {
258                         if (m_cam_vel.X > m_cam_vel_old.X)
259                                 m_cam_vel_old.X = m_cam_vel.X;
260
261                         f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
262                         m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
263
264                         if (m_last_cam_pos.X != player_yaw)
265                                 m_last_cam_pos.X = player_yaw;
266
267                         m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
268                                 WIELDMESH_OFFSET_X - (WIELDMESH_AMPLITUDE_X * 0.5f),
269                                 WIELDMESH_OFFSET_X + (WIELDMESH_AMPLITUDE_X * 0.5f));
270                 }
271
272                 if (m_cam_vel.Y > 1.0f) {
273                         if (m_cam_vel.Y > m_cam_vel_old.Y)
274                                 m_cam_vel_old.Y = m_cam_vel.Y;
275
276                         f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
277                         m_wieldmesh_offset.Y +=
278                                 m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
279
280                         if (m_last_cam_pos.Y != m_camera_direction.Y)
281                                 m_last_cam_pos.Y = m_camera_direction.Y;
282
283                         m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
284                                 WIELDMESH_OFFSET_Y - (WIELDMESH_AMPLITUDE_Y * 0.5f),
285                                 WIELDMESH_OFFSET_Y + (WIELDMESH_AMPLITUDE_Y * 0.5f));
286                 }
287
288                 m_arm_dir = dir(m_wieldmesh_offset);
289         } else {
290                 /*
291                     Now the arm gets back to its default position when the camera stops,
292                     following a vector, with a smooth deceleration factor.
293                 */
294
295                 f32 dec_X = 0.35f * (std::min(15.0f, m_cam_vel_old.X) * (1.0f +
296                         (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
297
298                 f32 dec_Y = 0.25f * (std::min(15.0f, m_cam_vel_old.Y) * (1.0f +
299                         (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
300
301                 if (gap_X < 0.1f)
302                         m_cam_vel_old.X = 0.0f;
303
304                 m_wieldmesh_offset.X -=
305                         m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
306
307                 if (gap_Y < 0.1f)
308                         m_cam_vel_old.Y = 0.0f;
309
310                 m_wieldmesh_offset.Y -=
311                         m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
312         }
313 }
314
315 void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
316 {
317         // Get player position
318         // Smooth the movement when walking up stairs
319         v3f old_player_position = m_playernode->getPosition();
320         v3f player_position = player->getPosition();
321
322         f32 yaw = player->getYaw();
323         f32 pitch = player->getPitch();
324
325         // This is worse than `LocalPlayer::getPosition()` but
326         // mods expect the player head to be at the parent's position
327         // plus eye height.
328         if (player->getParent())
329                 player_position = player->getParent()->getPosition();
330
331         // Smooth the camera movement after the player instantly moves upward due to stepheight.
332         // The smoothing usually continues until the camera position reaches the player position.
333         float player_stepheight = player->getCAO() ? player->getCAO()->getStepHeight() : HUGE_VALF;
334         float upward_movement = player_position.Y - old_player_position.Y;
335         if (upward_movement < 0.01f || upward_movement > player_stepheight) {
336                 m_stepheight_smooth_active = false;
337         } else if (player->touching_ground) {
338                 m_stepheight_smooth_active = true;
339         }
340         if (m_stepheight_smooth_active) {
341                 f32 oldy = old_player_position.Y;
342                 f32 newy = player_position.Y;
343                 f32 t = std::exp(-23 * frametime);
344                 player_position.Y = oldy * t + newy * (1-t);
345         }
346
347         // Set player node transformation
348         m_playernode->setPosition(player_position);
349         m_playernode->setRotation(v3f(0, -1 * yaw, 0));
350         m_playernode->updateAbsolutePosition();
351
352         // Get camera tilt timer (hurt animation)
353         float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
354
355         // Fall bobbing animation
356         float fall_bobbing = 0;
357         if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
358         {
359                 if(m_view_bobbing_fall == -1) // Effect took place and has finished
360                         player->camera_impact = m_view_bobbing_fall = 0;
361                 else if(m_view_bobbing_fall == 0) // Initialize effect
362                         m_view_bobbing_fall = 1;
363
364                 // Convert 0 -> 1 to 0 -> 1 -> 0
365                 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
366                 // Smoothen and invert the above
367                 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
368                 // Amplify according to the intensity of the impact
369                 if (player->camera_impact > 0.0f)
370                         fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
371
372                 fall_bobbing *= m_cache_fall_bobbing_amount;
373         }
374
375         // Calculate and translate the head SceneNode offsets
376         {
377                 v3f eye_offset = player->getEyeOffset();
378                 if (m_camera_mode == CAMERA_MODE_FIRST)
379                         eye_offset += player->eye_offset_first;
380                 else
381                         eye_offset += player->eye_offset_third;
382
383                 // Set head node transformation
384                 eye_offset.Y += cameratilt * -player->hurt_tilt_strength + fall_bobbing;
385                 m_headnode->setPosition(eye_offset);
386                 m_headnode->setRotation(v3f(pitch, 0,
387                         cameratilt * player->hurt_tilt_strength));
388                 m_headnode->updateAbsolutePosition();
389         }
390
391         // Compute relative camera position and target
392         v3f rel_cam_pos = v3f(0,0,0);
393         v3f rel_cam_target = v3f(0,0,1);
394         v3f rel_cam_up = v3f(0,1,0);
395
396         if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
397                 m_camera_mode < CAMERA_MODE_THIRD) {
398                 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
399                 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
400
401                 f32 bobknob = 1.2;
402                 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
403
404                 v3f bobvec = v3f(
405                         0.3 * bobdir * sin(bobfrac * M_PI),
406                         -0.28 * bobtmp * bobtmp,
407                         0.);
408
409                 rel_cam_pos += bobvec * m_cache_view_bobbing_amount;
410                 rel_cam_target += bobvec * m_cache_view_bobbing_amount;
411                 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * m_cache_view_bobbing_amount);
412         }
413
414         // Compute absolute camera position and target
415         m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
416         m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
417
418         v3f abs_cam_up;
419         m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
420
421         // Separate camera position for calculation
422         v3f my_cp = m_camera_position;
423
424         // Reposition the camera for third person view
425         if (m_camera_mode > CAMERA_MODE_FIRST)
426         {
427                 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
428                         m_camera_direction *= -1;
429
430                 my_cp.Y += 2;
431
432                 // Calculate new position
433                 bool abort = false;
434                 for (int i = BS; i <= BS * 2.75; i++) {
435                         my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
436                         my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
437                         if (i > 12)
438                                 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
439
440                         // Prevent camera positioned inside nodes
441                         const NodeDefManager *nodemgr = m_client->ndef();
442                         MapNode n = m_client->getEnv().getClientMap()
443                                 .getNode(floatToInt(my_cp, BS));
444
445                         const ContentFeatures& features = nodemgr->get(n);
446                         if (features.walkable) {
447                                 my_cp.X += m_camera_direction.X*-1*-BS/2;
448                                 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
449                                 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
450                                 abort = true;
451                                 break;
452                         }
453                 }
454
455                 // If node blocks camera position don't move y to heigh
456                 if (abort && my_cp.Y > player_position.Y+BS*2)
457                         my_cp.Y = player_position.Y+BS*2;
458         }
459
460         // Update offset if too far away from the center of the map
461         m_camera_offset.X += CAMERA_OFFSET_STEP*
462                         (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
463         m_camera_offset.Y += CAMERA_OFFSET_STEP*
464                         (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
465         m_camera_offset.Z += CAMERA_OFFSET_STEP*
466                         (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
467
468         // Set camera node transformation
469         m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
470         m_cameranode->updateAbsolutePosition();
471         m_cameranode->setUpVector(abs_cam_up);
472         // *100.0 helps in large map coordinates
473         m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
474
475         // update the camera position in third-person mode to render blocks behind player
476         // and correctly apply liquid post FX.
477         if (m_camera_mode != CAMERA_MODE_FIRST)
478                 m_camera_position = my_cp;
479
480         /*
481          * Apply server-sent FOV, instantaneous or smooth transition.
482          * If not, check for zoom and set to zoom FOV.
483          * Otherwise, default to m_cache_fov.
484          */
485         if (m_fov_transition_active) {
486                 // Smooth FOV transition
487                 // Dynamically calculate FOV delta based on frametimes
488                 f32 delta = (frametime / m_transition_time) * m_fov_diff;
489                 m_curr_fov_degrees += delta;
490
491                 // Mark transition as complete if target FOV has been reached
492                 if ((m_fov_diff > 0.0f && m_curr_fov_degrees >= m_target_fov_degrees) ||
493                                 (m_fov_diff < 0.0f && m_curr_fov_degrees <= m_target_fov_degrees)) {
494                         m_fov_transition_active = false;
495                         m_curr_fov_degrees = m_target_fov_degrees;
496                 }
497         } else if (m_server_sent_fov) {
498                 // Instantaneous FOV change
499                 m_curr_fov_degrees = m_target_fov_degrees;
500         } else if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
501                 // Player requests zoom, apply zoom FOV
502                 m_curr_fov_degrees = player->getZoomFOV();
503         } else {
504                 // Set to client's selected FOV
505                 m_curr_fov_degrees = m_cache_fov;
506         }
507         m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
508
509         // FOV and aspect ratio
510         const v2u32 &window_size = RenderingEngine::getWindowSize();
511         m_aspect = (f32) window_size.X / (f32) window_size.Y;
512         m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
513         // Increase vertical FOV on lower aspect ratios (<16:10)
514         m_fov_y *= core::clamp(sqrt(16./10. / m_aspect), 1.0, 1.4);
515         m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
516         m_cameranode->setAspectRatio(m_aspect);
517         m_cameranode->setFOV(m_fov_y);
518
519         // Make new matrices and frustum
520         m_cameranode->updateMatrices();
521
522         if (m_arm_inertia)
523                 addArmInertia(yaw);
524
525         // Position the wielded item
526         //v3f wield_position = v3f(45, -35, 65);
527         v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
528         //v3f wield_rotation = v3f(-100, 120, -100);
529         v3f wield_rotation = v3f(-100, 120, -100);
530         wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
531         if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
532         {
533                 f32 frac = 1.0;
534                 if(m_digging_anim > 0.5)
535                         frac = 2.0 * (m_digging_anim - 0.5);
536                 // This value starts from 1 and settles to 0
537                 f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
538                 //f32 ratiothing2 = pow(ratiothing, 0.5f);
539                 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
540                 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
541                 //wield_position.Z += frac * 5.0 * ratiothing2;
542                 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
543                 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
544                 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
545                 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
546         }
547         if (m_digging_button != -1)
548         {
549                 f32 digfrac = m_digging_anim;
550                 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
551                 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
552                 wield_position.Z += 25 * 0.5;
553
554                 // Euler angles are PURE EVIL, so why not use quaternions?
555                 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
556                 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
557                 core::quaternion quat_slerp;
558                 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
559                 quat_slerp.toEuler(wield_rotation);
560                 wield_rotation *= core::RADTODEG;
561         } else {
562                 f32 bobfrac = my_modf(m_view_bobbing_anim);
563                 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
564                 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
565         }
566         m_wieldnode->setPosition(wield_position);
567         m_wieldnode->setRotation(wield_rotation);
568
569         m_player_light_color = player->light_color;
570         m_wieldnode->setNodeLightColor(m_player_light_color);
571
572         // Set render distance
573         updateViewingRange();
574
575         // If the player is walking, swimming, or climbing,
576         // view bobbing is enabled and free_move is off,
577         // start (or continue) the view bobbing animation.
578         const v3f &speed = player->getSpeed();
579         const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
580         const bool movement_Y = fabs(speed.Y) > BS;
581
582         const bool walking = movement_XZ && player->touching_ground;
583         const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
584         const bool climbing = movement_Y && player->is_climbing;
585         const bool flying = g_settings->getBool("free_move")
586                 && m_client->checkLocalPrivilege("fly");
587         if ((walking || swimming || climbing) && !flying) {
588                 // Start animation
589                 m_view_bobbing_state = 1;
590                 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
591         } else if (m_view_bobbing_state == 1) {
592                 // Stop animation
593                 m_view_bobbing_state = 2;
594                 m_view_bobbing_speed = 60;
595         }
596 }
597
598 void Camera::updateViewingRange()
599 {
600         f32 viewing_range = g_settings->getFloat("viewing_range");
601
602         // Ignore near_plane setting on all other platforms to prevent abuse
603 #if ENABLE_GLES
604         m_cameranode->setNearValue(rangelim(
605                 g_settings->getFloat("near_plane"), 0.0f, 0.25f) * BS);
606 #else
607         m_cameranode->setNearValue(0.1f * BS);
608 #endif
609
610         m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
611         if (m_draw_control.range_all) {
612                 m_cameranode->setFarValue(100000.0);
613                 return;
614         }
615         m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
616 }
617
618 void Camera::setDigging(s32 button)
619 {
620         if (m_digging_button == -1)
621                 m_digging_button = button;
622 }
623
624 void Camera::wield(const ItemStack &item)
625 {
626         if (item.name != m_wield_item_next.name ||
627                         item.metadata != m_wield_item_next.metadata) {
628                 m_wield_item_next = item;
629                 if (m_wield_change_timer > 0)
630                         m_wield_change_timer = -m_wield_change_timer;
631                 else if (m_wield_change_timer == 0)
632                         m_wield_change_timer = -0.001;
633         }
634 }
635
636 void Camera::drawWieldedTool(irr::core::matrix4* translation)
637 {
638         // Draw the wielded node (in a separate scene manager)
639         scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
640         cam->setAspectRatio(m_cameranode->getAspectRatio());
641         cam->setFOV(72.0*M_PI/180.0);
642         cam->setNearValue(40); // give wield tool smaller z-depth than the world in most cases.
643         cam->setFarValue(1000);
644         if (translation != NULL)
645         {
646                 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
647                 irr::core::vector3df focusPoint = (cam->getTarget()
648                                 - cam->getAbsolutePosition()).setLength(1)
649                                 + cam->getAbsolutePosition();
650
651                 irr::core::vector3df camera_pos =
652                                 (startMatrix * *translation).getTranslation();
653                 cam->setPosition(camera_pos);
654                 cam->updateAbsolutePosition();
655                 cam->setTarget(focusPoint);
656         }
657         m_wieldmgr->drawAll();
658 }
659
660 void Camera::drawNametags()
661 {
662         core::matrix4 trans = m_cameranode->getProjectionMatrix();
663         trans *= m_cameranode->getViewMatrix();
664
665         gui::IGUIFont *font = g_fontengine->getFont();
666         video::IVideoDriver *driver = RenderingEngine::get_video_driver();
667         v2u32 screensize = driver->getScreenSize();
668
669         for (const Nametag *nametag : m_nametags) {
670                 // Nametags are hidden in GenericCAO::updateNametag()
671
672                 v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
673                 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
674                 trans.multiplyWith1x4Matrix(transformed_pos);
675                 if (transformed_pos[3] > 0) {
676                         std::wstring nametag_colorless =
677                                 unescape_translate(utf8_to_wide(nametag->text));
678                         core::dimension2d<u32> textsize = font->getDimension(
679                                 nametag_colorless.c_str());
680                         f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
681                                 core::reciprocal(transformed_pos[3]);
682                         v2s32 screen_pos;
683                         screen_pos.X = screensize.X *
684                                 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
685                         screen_pos.Y = screensize.Y *
686                                 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
687                         core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
688
689                         auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
690                         if (bgcolor.getAlpha() != 0) {
691                                 core::rect<s32> bg_size(-2, 0, textsize.Width + 2, textsize.Height);
692                                 driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
693                         }
694
695                         font->draw(
696                                 translate_string(utf8_to_wide(nametag->text)).c_str(),
697                                 size + screen_pos, nametag->textcolor);
698                 }
699         }
700 }
701
702 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
703                 const std::string &text, video::SColor textcolor,
704                 Optional<video::SColor> bgcolor, const v3f &pos)
705 {
706         Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
707         m_nametags.push_back(nametag);
708         return nametag;
709 }
710
711 void Camera::removeNametag(Nametag *nametag)
712 {
713         m_nametags.remove(nametag);
714         delete nametag;
715 }
716
717 std::array<core::plane3d<f32>, 4> Camera::getFrustumCullPlanes() const
718 {
719         using irr::scene::SViewFrustum;
720         const auto &frustum_planes = m_cameranode->getViewFrustum()->planes;
721         return {
722                 frustum_planes[SViewFrustum::VF_LEFT_PLANE],
723                 frustum_planes[SViewFrustum::VF_RIGHT_PLANE],
724                 frustum_planes[SViewFrustum::VF_BOTTOM_PLANE],
725                 frustum_planes[SViewFrustum::VF_TOP_PLANE],
726         };
727 }