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