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