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