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