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