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