Isolate irrlicht references and use a singleton (#6041)
[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
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         m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f;
201         m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y);
202
203         if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
204                 /*
205                    the arm moves when the camera moves fast enough.
206                 */
207
208                 if (m_cam_vel.X > 1.0f) {
209                         m_timer.X = 0.0f;
210                         m_timer.X += frametime;
211
212                         if (m_cam_vel.X > m_cam_vel_old.X)
213                                 m_cam_vel_old.X = m_cam_vel.X;
214
215                         if (m_last_cam_pos.X > player_yaw)
216                                 // right
217                                 m_wieldmesh_offset.X -= (0.1f + frametime) * m_cam_vel.X;
218                         else
219                                 // left
220                                 m_wieldmesh_offset.X += (0.1f + frametime) * m_cam_vel.X;
221
222                         if (m_last_cam_pos.X != player_yaw)
223                                 m_last_cam_pos.X = player_yaw;
224
225                         m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X, 48.0f, 62.0f);
226                 }
227
228                 if (m_cam_vel.Y > 1.0f) {
229                         m_timer.Y = 0.0f;
230                         m_timer.Y += frametime;
231
232                         if (m_cam_vel.Y > m_cam_vel_old.Y)
233                                 m_cam_vel_old.Y = m_cam_vel.Y;
234
235                         if (m_last_cam_pos.Y > m_camera_direction.Y)
236                                 // down
237                                 m_wieldmesh_offset.Y += (0.1f + frametime) * m_cam_vel.Y;
238                         else
239                                 // up
240                                 m_wieldmesh_offset.Y -= (0.1f + frametime) * m_cam_vel.Y;
241
242                         if (m_last_cam_pos.Y != m_camera_direction.Y)
243                                 m_last_cam_pos.Y = m_camera_direction.Y;
244
245                         m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y, -45.0f, -30.0f);
246                 }
247         } else {
248                 /*
249                    the arm now gets back to its default position when the camera stops.
250                 */
251
252                 if (floor(m_wieldmesh_offset.X) == 55.0f) {
253                         m_cam_vel_old.X = 0.0f;
254                         m_wieldmesh_offset.X = 55.0f;
255                 }
256
257                 if (m_wieldmesh_offset.X > 55.0f)
258                         m_wieldmesh_offset.X -= (0.05f + frametime) * m_cam_vel_old.X;
259                 if (m_wieldmesh_offset.X < 55.0f)
260                         m_wieldmesh_offset.X += (0.05f + frametime) * m_cam_vel_old.X;
261
262                 if (floor(m_wieldmesh_offset.Y) == -35.0f) {
263                         m_cam_vel_old.Y = 0.0f;
264                         m_wieldmesh_offset.Y = -35.0f;
265                 }
266
267                 if (m_wieldmesh_offset.Y > -35.0f)
268                         m_wieldmesh_offset.Y -= (0.05f + frametime) * m_cam_vel_old.Y;
269                 if (m_wieldmesh_offset.Y < -35.0f)
270                         m_wieldmesh_offset.Y += (0.05f + frametime) * m_cam_vel_old.Y;
271         }
272 }
273
274 void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
275                 f32 tool_reload_ratio, ClientEnvironment &c_env)
276 {
277         // Get player position
278         // Smooth the movement when walking up stairs
279         v3f old_player_position = m_playernode->getPosition();
280         v3f player_position = player->getPosition();
281         if (player->isAttached && player->parent)
282                 player_position = player->parent->getPosition();
283         //if(player->touching_ground && player_position.Y > old_player_position.Y)
284         if(player->touching_ground &&
285                         player_position.Y > old_player_position.Y)
286         {
287                 f32 oldy = old_player_position.Y;
288                 f32 newy = player_position.Y;
289                 f32 t = exp(-23*frametime);
290                 player_position.Y = oldy * t + newy * (1-t);
291         }
292
293         // Set player node transformation
294         m_playernode->setPosition(player_position);
295         m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
296         m_playernode->updateAbsolutePosition();
297
298         // Get camera tilt timer (hurt animation)
299         float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
300
301         // Fall bobbing animation
302         float fall_bobbing = 0;
303         if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
304         {
305                 if(m_view_bobbing_fall == -1) // Effect took place and has finished
306                         player->camera_impact = m_view_bobbing_fall = 0;
307                 else if(m_view_bobbing_fall == 0) // Initialize effect
308                         m_view_bobbing_fall = 1;
309
310                 // Convert 0 -> 1 to 0 -> 1 -> 0
311                 fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
312                 // Smoothen and invert the above
313                 fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
314                 // Amplify according to the intensity of the impact
315                 fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
316
317                 fall_bobbing *= m_cache_fall_bobbing_amount;
318         }
319
320         // Calculate players eye offset for different camera modes
321         v3f PlayerEyeOffset = player->getEyeOffset();
322         if (m_camera_mode == CAMERA_MODE_FIRST)
323                 PlayerEyeOffset += player->eye_offset_first;
324         else
325                 PlayerEyeOffset += player->eye_offset_third;
326
327         // Set head node transformation
328         m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
329         m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
330         m_headnode->updateAbsolutePosition();
331
332         // Compute relative camera position and target
333         v3f rel_cam_pos = v3f(0,0,0);
334         v3f rel_cam_target = v3f(0,0,1);
335         v3f rel_cam_up = v3f(0,1,0);
336
337         if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
338                 m_camera_mode < CAMERA_MODE_THIRD) {
339                 f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
340                 f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
341
342                 #if 1
343                 f32 bobknob = 1.2;
344                 f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
345                 //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
346
347                 v3f bobvec = v3f(
348                         0.3 * bobdir * sin(bobfrac * M_PI),
349                         -0.28 * bobtmp * bobtmp,
350                         0.);
351
352                 //rel_cam_pos += 0.2 * bobvec;
353                 //rel_cam_target += 0.03 * bobvec;
354                 //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
355                 float f = 1.0;
356                 f *= m_cache_view_bobbing_amount;
357                 rel_cam_pos += bobvec * f;
358                 //rel_cam_target += 0.995 * bobvec * f;
359                 rel_cam_target += bobvec * f;
360                 rel_cam_target.Z -= 0.005 * bobvec.Z * f;
361                 //rel_cam_target.X -= 0.005 * bobvec.X * f;
362                 //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
363                 rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
364                 #else
365                 f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
366                 f32 angle_rad = angle_deg * M_PI / 180;
367                 f32 r = 0.05;
368                 v3f off = v3f(
369                         r * sin(angle_rad),
370                         r * (cos(angle_rad) - 1),
371                         0);
372                 rel_cam_pos += off;
373                 //rel_cam_target += off;
374                 rel_cam_up.rotateXYBy(angle_deg);
375                 #endif
376
377         }
378
379         // Compute absolute camera position and target
380         m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
381         m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
382
383         v3f abs_cam_up;
384         m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
385
386         // Seperate camera position for calculation
387         v3f my_cp = m_camera_position;
388
389         // Reposition the camera for third person view
390         if (m_camera_mode > CAMERA_MODE_FIRST)
391         {
392                 if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
393                         m_camera_direction *= -1;
394
395                 my_cp.Y += 2;
396
397                 // Calculate new position
398                 bool abort = false;
399                 for (int i = BS; i <= BS*2.75; i++)
400                 {
401                         my_cp.X = m_camera_position.X + m_camera_direction.X*-i;
402                         my_cp.Z = m_camera_position.Z + m_camera_direction.Z*-i;
403                         if (i > 12)
404                                 my_cp.Y = m_camera_position.Y + (m_camera_direction.Y*-i);
405
406                         // Prevent camera positioned inside nodes
407                         INodeDefManager *nodemgr = m_client->ndef();
408                         MapNode n = c_env.getClientMap().getNodeNoEx(floatToInt(my_cp, BS));
409                         const ContentFeatures& features = nodemgr->get(n);
410                         if(features.walkable)
411                         {
412                                 my_cp.X += m_camera_direction.X*-1*-BS/2;
413                                 my_cp.Z += m_camera_direction.Z*-1*-BS/2;
414                                 my_cp.Y += m_camera_direction.Y*-1*-BS/2;
415                                 abort = true;
416                                 break;
417                         }
418                 }
419
420                 // If node blocks camera position don't move y to heigh
421                 if (abort && my_cp.Y > player_position.Y+BS*2)
422                         my_cp.Y = player_position.Y+BS*2;
423         }
424
425         // Update offset if too far away from the center of the map
426         m_camera_offset.X += CAMERA_OFFSET_STEP*
427                         (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
428         m_camera_offset.Y += CAMERA_OFFSET_STEP*
429                         (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
430         m_camera_offset.Z += CAMERA_OFFSET_STEP*
431                         (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
432
433         // Set camera node transformation
434         m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
435         m_cameranode->setUpVector(abs_cam_up);
436         // *100.0 helps in large map coordinates
437         m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
438
439         // update the camera position in front-view mode to render blocks behind player
440         if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
441                 m_camera_position = my_cp;
442
443         // Get FOV
444         f32 fov_degrees;
445         if (player->getPlayerControl().zoom && m_client->checkLocalPrivilege("zoom")) {
446                 fov_degrees = m_cache_zoom_fov;
447         } else {
448                 fov_degrees = m_cache_fov;
449         }
450         fov_degrees = rangelim(fov_degrees, 7.0, 160.0);
451
452         // FOV and aspect ratio
453         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
454         m_aspect = (f32) window_size.X / (f32) window_size.Y;
455         m_fov_y = fov_degrees * M_PI / 180.0;
456         // Increase vertical FOV on lower aspect ratios (<16:10)
457         m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
458         m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
459         m_cameranode->setAspectRatio(m_aspect);
460         m_cameranode->setFOV(m_fov_y);
461
462         if (m_arm_inertia)
463                 addArmInertia(player->getYaw(), frametime);
464
465         // Position the wielded item
466         //v3f wield_position = v3f(45, -35, 65);
467         v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
468         //v3f wield_rotation = v3f(-100, 120, -100);
469         v3f wield_rotation = v3f(-100, 120, -100);
470         wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
471         if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
472         {
473                 f32 frac = 1.0;
474                 if(m_digging_anim > 0.5)
475                         frac = 2.0 * (m_digging_anim - 0.5);
476                 // This value starts from 1 and settles to 0
477                 f32 ratiothing = pow((1.0f - tool_reload_ratio), 0.5f);
478                 //f32 ratiothing2 = pow(ratiothing, 0.5f);
479                 f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
480                 wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
481                 //wield_position.Z += frac * 5.0 * ratiothing2;
482                 wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
483                 wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
484                 //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
485                 //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
486         }
487         if (m_digging_button != -1)
488         {
489                 f32 digfrac = m_digging_anim;
490                 wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
491                 wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
492                 wield_position.Z += 25 * 0.5;
493
494                 // Euler angles are PURE EVIL, so why not use quaternions?
495                 core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
496                 core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
497                 core::quaternion quat_slerp;
498                 quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
499                 quat_slerp.toEuler(wield_rotation);
500                 wield_rotation *= core::RADTODEG;
501         } else {
502                 f32 bobfrac = my_modf(m_view_bobbing_anim);
503                 wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
504                 wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
505         }
506         m_wieldnode->setPosition(wield_position);
507         m_wieldnode->setRotation(wield_rotation);
508
509         m_wieldnode->setColor(player->light_color);
510
511         // Set render distance
512         updateViewingRange();
513
514         // If the player is walking, swimming, or climbing,
515         // view bobbing is enabled and free_move is off,
516         // start (or continue) the view bobbing animation.
517         v3f speed = player->getSpeed();
518         const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
519         const bool movement_Y = fabs(speed.Y) > BS;
520
521         const bool walking = movement_XZ && player->touching_ground;
522         const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
523         const bool climbing = movement_Y && player->is_climbing;
524         if ((walking || swimming || climbing) &&
525                         (!g_settings->getBool("free_move") || !m_client->checkLocalPrivilege("fly"))) {
526                 // Start animation
527                 m_view_bobbing_state = 1;
528                 m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
529         }
530         else if (m_view_bobbing_state == 1)
531         {
532                 // Stop animation
533                 m_view_bobbing_state = 2;
534                 m_view_bobbing_speed = 60;
535         }
536 }
537
538 void Camera::updateViewingRange()
539 {
540         f32 viewing_range = g_settings->getFloat("viewing_range");
541         m_draw_control.wanted_range = viewing_range;
542         if (m_draw_control.range_all) {
543                 m_cameranode->setFarValue(100000.0);
544                 return;
545         }
546         m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
547 }
548
549 void Camera::setDigging(s32 button)
550 {
551         if (m_digging_button == -1)
552                 m_digging_button = button;
553 }
554
555 void Camera::wield(const ItemStack &item)
556 {
557         if (item.name != m_wield_item_next.name ||
558                         item.metadata != m_wield_item_next.metadata) {
559                 m_wield_item_next = item;
560                 if (m_wield_change_timer > 0)
561                         m_wield_change_timer = -m_wield_change_timer;
562                 else if (m_wield_change_timer == 0)
563                         m_wield_change_timer = -0.001;
564         }
565 }
566
567 void Camera::drawWieldedTool(irr::core::matrix4* translation)
568 {
569         // Clear Z buffer so that the wielded tool stay in front of world geometry
570         m_wieldmgr->getVideoDriver()->clearZBuffer();
571
572         // Draw the wielded node (in a separate scene manager)
573         scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
574         cam->setAspectRatio(m_cameranode->getAspectRatio());
575         cam->setFOV(72.0*M_PI/180.0);
576         cam->setNearValue(10);
577         cam->setFarValue(1000);
578         if (translation != NULL)
579         {
580                 irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
581                 irr::core::vector3df focusPoint = (cam->getTarget()
582                                 - cam->getAbsolutePosition()).setLength(1)
583                                 + cam->getAbsolutePosition();
584
585                 irr::core::vector3df camera_pos =
586                                 (startMatrix * *translation).getTranslation();
587                 cam->setPosition(camera_pos);
588                 cam->setTarget(focusPoint);
589         }
590         m_wieldmgr->drawAll();
591 }
592
593 void Camera::drawNametags()
594 {
595         core::matrix4 trans = m_cameranode->getProjectionMatrix();
596         trans *= m_cameranode->getViewMatrix();
597
598         for (std::list<Nametag *>::const_iterator
599                         i = m_nametags.begin();
600                         i != m_nametags.end(); ++i) {
601                 Nametag *nametag = *i;
602                 if (nametag->nametag_color.getAlpha() == 0) {
603                         // Enforce hiding nametag,
604                         // because if freetype is enabled, a grey
605                         // shadow can remain.
606                         continue;
607                 }
608                 v3f pos = nametag->parent_node->getAbsolutePosition() + v3f(0.0, 1.1 * BS, 0.0);
609                 f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
610                 trans.multiplyWith1x4Matrix(transformed_pos);
611                 if (transformed_pos[3] > 0) {
612                         std::string nametag_colorless = unescape_enriched(nametag->nametag_text);
613                         core::dimension2d<u32> textsize =
614                                 g_fontengine->getFont()->getDimension(
615                                 utf8_to_wide(nametag_colorless).c_str());
616                         f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
617                                 core::reciprocal(transformed_pos[3]);
618                         v2u32 screensize = m_driver->getScreenSize();
619                         v2s32 screen_pos;
620                         screen_pos.X = screensize.X *
621                                 (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
622                         screen_pos.Y = screensize.Y *
623                                 (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
624                         core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
625                         g_fontengine->getFont()->draw(utf8_to_wide(nametag->nametag_text).c_str(),
626                                         size + screen_pos, nametag->nametag_color);
627                 }
628         }
629 }
630
631 Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
632                 std::string nametag_text, video::SColor nametag_color)
633 {
634         Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color);
635         m_nametags.push_back(nametag);
636         return nametag;
637 }
638
639 void Camera::removeNametag(Nametag *nametag)
640 {
641         m_nametags.remove(nametag);
642         delete nametag;
643 }