Verify HudSetParams input when hotbar textures are set (#6013)
[oweals/minetest.git] / src / clientmap.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 "clientmap.h"
21 #include "client.h"
22 #include "mapblock_mesh.h"
23 #include <IMaterialRenderer.h>
24 #include <matrix4.h>
25 #include "log.h"
26 #include "mapsector.h"
27 #include "nodedef.h"
28 #include "mapblock.h"
29 #include "profiler.h"
30 #include "settings.h"
31 #include "camera.h"               // CameraModes
32 #include "util/basic_macros.h"
33 #include <algorithm>
34
35 ClientMap::ClientMap(
36                 Client *client,
37                 MapDrawControl &control,
38                 scene::ISceneNode* parent,
39                 scene::ISceneManager* mgr,
40                 s32 id
41 ):
42         Map(dout_client, client),
43         scene::ISceneNode(parent, mgr, id),
44         m_client(client),
45         m_control(control),
46         m_camera_position(0,0,0),
47         m_camera_direction(0,0,1),
48         m_camera_fov(M_PI)
49 {
50         m_box = aabb3f(-BS*1000000,-BS*1000000,-BS*1000000,
51                         BS*1000000,BS*1000000,BS*1000000);
52
53         /* TODO: Add a callback function so these can be updated when a setting
54          *       changes.  At this point in time it doesn't matter (e.g. /set
55          *       is documented to change server settings only)
56          *
57          * TODO: Local caching of settings is not optimal and should at some stage
58          *       be updated to use a global settings object for getting thse values
59          *       (as opposed to the this local caching). This can be addressed in
60          *       a later release.
61          */
62         m_cache_trilinear_filter  = g_settings->getBool("trilinear_filter");
63         m_cache_bilinear_filter   = g_settings->getBool("bilinear_filter");
64         m_cache_anistropic_filter = g_settings->getBool("anisotropic_filter");
65
66 }
67
68 ClientMap::~ClientMap()
69 {
70 }
71
72 MapSector * ClientMap::emergeSector(v2s16 p2d)
73 {
74         DSTACK(FUNCTION_NAME);
75         // Check that it doesn't exist already
76         try{
77                 return getSectorNoGenerate(p2d);
78         }
79         catch(InvalidPositionException &e)
80         {
81         }
82
83         // Create a sector
84         ClientMapSector *sector = new ClientMapSector(this, p2d, m_gamedef);
85
86         {
87                 //MutexAutoLock lock(m_sector_mutex); // Bulk comment-out
88                 m_sectors[p2d] = sector;
89         }
90
91         return sector;
92 }
93
94 void ClientMap::OnRegisterSceneNode()
95 {
96         if(IsVisible)
97         {
98                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
99                 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
100         }
101
102         ISceneNode::OnRegisterSceneNode();
103 }
104
105 void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
106                 v3s16 *p_blocks_min, v3s16 *p_blocks_max)
107 {
108         v3s16 box_nodes_d = m_control.wanted_range * v3s16(1, 1, 1);
109         // Define p_nodes_min/max as v3s32 because 'cam_pos_nodes -/+ box_nodes_d'
110         // can exceed the range of v3s16 when a large view range is used near the
111         // world edges.
112         v3s32 p_nodes_min(
113                 cam_pos_nodes.X - box_nodes_d.X,
114                 cam_pos_nodes.Y - box_nodes_d.Y,
115                 cam_pos_nodes.Z - box_nodes_d.Z);
116         v3s32 p_nodes_max(
117                 cam_pos_nodes.X + box_nodes_d.X,
118                 cam_pos_nodes.Y + box_nodes_d.Y,
119                 cam_pos_nodes.Z + box_nodes_d.Z);
120         // Take a fair amount as we will be dropping more out later
121         // Umm... these additions are a bit strange but they are needed.
122         *p_blocks_min = v3s16(
123                         p_nodes_min.X / MAP_BLOCKSIZE - 3,
124                         p_nodes_min.Y / MAP_BLOCKSIZE - 3,
125                         p_nodes_min.Z / MAP_BLOCKSIZE - 3);
126         *p_blocks_max = v3s16(
127                         p_nodes_max.X / MAP_BLOCKSIZE + 1,
128                         p_nodes_max.Y / MAP_BLOCKSIZE + 1,
129                         p_nodes_max.Z / MAP_BLOCKSIZE + 1);
130 }
131
132 void ClientMap::updateDrawList(video::IVideoDriver* driver)
133 {
134         ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
135         g_profiler->add("CM::updateDrawList() count", 1);
136
137         for (std::map<v3s16, MapBlock*>::iterator i = m_drawlist.begin();
138                         i != m_drawlist.end(); ++i) {
139                 MapBlock *block = i->second;
140                 block->refDrop();
141         }
142         m_drawlist.clear();
143
144         v3f camera_position = m_camera_position;
145         v3f camera_direction = m_camera_direction;
146         f32 camera_fov = m_camera_fov;
147
148         // Use a higher fov to accomodate faster camera movements.
149         // Blocks are cropped better when they are drawn.
150         // Or maybe they aren't? Well whatever.
151         camera_fov *= 1.2;
152
153         v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
154         v3s16 p_blocks_min;
155         v3s16 p_blocks_max;
156         getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
157
158         // Number of blocks in rendering range
159         u32 blocks_in_range = 0;
160         // Number of blocks occlusion culled
161         u32 blocks_occlusion_culled = 0;
162         // Number of blocks in rendering range but don't have a mesh
163         u32 blocks_in_range_without_mesh = 0;
164         // Blocks that had mesh that would have been drawn according to
165         // rendering range (if max blocks limit didn't kick in)
166         u32 blocks_would_have_drawn = 0;
167         // Blocks that were drawn and had a mesh
168         u32 blocks_drawn = 0;
169         // Blocks which had a corresponding meshbuffer for this pass
170         //u32 blocks_had_pass_meshbuf = 0;
171         // Blocks from which stuff was actually drawn
172         //u32 blocks_without_stuff = 0;
173         // Distance to farthest drawn block
174         float farthest_drawn = 0;
175
176         // No occlusion culling when free_move is on and camera is
177         // inside ground
178         bool occlusion_culling_enabled = true;
179         if (g_settings->getBool("free_move")) {
180                 MapNode n = getNodeNoEx(cam_pos_nodes);
181                 if (n.getContent() == CONTENT_IGNORE ||
182                                 m_nodedef->get(n).solidness == 2)
183                         occlusion_culling_enabled = false;
184         }
185
186         for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
187                         si != m_sectors.end(); ++si) {
188                 MapSector *sector = si->second;
189                 v2s16 sp = sector->getPos();
190
191                 if (m_control.range_all == false) {
192                         if (sp.X < p_blocks_min.X || sp.X > p_blocks_max.X ||
193                                         sp.Y < p_blocks_min.Z || sp.Y > p_blocks_max.Z)
194                                 continue;
195                 }
196
197                 MapBlockVect sectorblocks;
198                 sector->getBlocks(sectorblocks);
199
200                 /*
201                         Loop through blocks in sector
202                 */
203
204                 u32 sector_blocks_drawn = 0;
205
206                 for (MapBlockVect::iterator i = sectorblocks.begin();
207                                 i != sectorblocks.end(); ++i) {
208                         MapBlock *block = *i;
209
210                         /*
211                                 Compare block position to camera position, skip
212                                 if not seen on display
213                         */
214
215                         if (block->mesh)
216                                 block->mesh->updateCameraOffset(m_camera_offset);
217
218                         float range = 100000 * BS;
219                         if (!m_control.range_all)
220                                 range = m_control.wanted_range * BS;
221
222                         float d = 0.0;
223                         if (!isBlockInSight(block->getPos(), camera_position,
224                                         camera_direction, camera_fov, range, &d))
225                                 continue;
226
227                         blocks_in_range++;
228
229                         /*
230                                 Ignore if mesh doesn't exist
231                         */
232                         if (!block->mesh) {
233                                 blocks_in_range_without_mesh++;
234                                 continue;
235                         }
236
237                         /*
238                                 Occlusion culling
239                         */
240                         if (occlusion_culling_enabled && isBlockOccluded(block, cam_pos_nodes)) {
241                                 blocks_occlusion_culled++;
242                                 continue;
243                         }
244
245                         // This block is in range. Reset usage timer.
246                         block->resetUsageTimer();
247
248                         // Limit block count in case of a sudden increase
249                         blocks_would_have_drawn++;
250                         if (blocks_drawn >= m_control.wanted_max_blocks &&
251                                         !m_control.range_all &&
252                                         d > m_control.wanted_range * BS)
253                                 continue;
254
255                         // Add to set
256                         block->refGrab();
257                         m_drawlist[block->getPos()] = block;
258
259                         sector_blocks_drawn++;
260                         blocks_drawn++;
261                         if (d / BS > farthest_drawn)
262                                 farthest_drawn = d / BS;
263
264                 } // foreach sectorblocks
265
266                 if (sector_blocks_drawn != 0)
267                         m_last_drawn_sectors.insert(sp);
268         }
269
270         m_control.blocks_would_have_drawn = blocks_would_have_drawn;
271         m_control.blocks_drawn = blocks_drawn;
272         m_control.farthest_drawn = farthest_drawn;
273
274         g_profiler->avg("CM: blocks in range", blocks_in_range);
275         g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled);
276         if (blocks_in_range != 0)
277                 g_profiler->avg("CM: blocks in range without mesh (frac)",
278                                 (float)blocks_in_range_without_mesh / blocks_in_range);
279         g_profiler->avg("CM: blocks drawn", blocks_drawn);
280         g_profiler->avg("CM: farthest drawn", farthest_drawn);
281         g_profiler->avg("CM: wanted max blocks", m_control.wanted_max_blocks);
282 }
283
284 struct MeshBufList
285 {
286         /*!
287          * Specifies in which layer the list is.
288          * All lists which are in a lower layer are rendered before this list.
289          */
290         u8 layer;
291         video::SMaterial m;
292         std::vector<scene::IMeshBuffer*> bufs;
293 };
294
295 struct MeshBufListList
296 {
297         std::vector<MeshBufList> lists;
298
299         void clear()
300         {
301                 lists.clear();
302         }
303
304         void add(scene::IMeshBuffer *buf, u8 layer)
305         {
306                 const video::SMaterial &m = buf->getMaterial();
307                 for(std::vector<MeshBufList>::iterator i = lists.begin();
308                                 i != lists.end(); ++i){
309                         MeshBufList &l = *i;
310
311                         // comparing a full material is quite expensive so we don't do it if
312                         // not even first texture is equal
313                         if (l.m.TextureLayer[0].Texture != m.TextureLayer[0].Texture)
314                                 continue;
315
316                         if(l.layer != layer)
317                                 continue;
318
319                         if (l.m == m) {
320                                 l.bufs.push_back(buf);
321                                 return;
322                         }
323                 }
324                 MeshBufList l;
325                 l.layer = layer;
326                 l.m = m;
327                 l.bufs.push_back(buf);
328                 lists.push_back(l);
329         }
330 };
331
332 void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
333 {
334         DSTACK(FUNCTION_NAME);
335
336         bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
337
338         std::string prefix;
339         if (pass == scene::ESNRP_SOLID)
340                 prefix = "CM: solid: ";
341         else
342                 prefix = "CM: transparent: ";
343
344         /*
345                 This is called two times per frame, reset on the non-transparent one
346         */
347         if (pass == scene::ESNRP_SOLID)
348                 m_last_drawn_sectors.clear();
349
350         /*
351                 Get time for measuring timeout.
352
353                 Measuring time is very useful for long delays when the
354                 machine is swapping a lot.
355         */
356         int time1 = time(0);
357
358         /*
359                 Get animation parameters
360         */
361         float animation_time = m_client->getAnimationTime();
362         int crack = m_client->getCrackLevel();
363         u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
364
365         v3f camera_position = m_camera_position;
366         v3f camera_direction = m_camera_direction;
367         f32 camera_fov = m_camera_fov;
368
369         /*
370                 Get all blocks and draw all visible ones
371         */
372
373         u32 vertex_count = 0;
374         u32 meshbuffer_count = 0;
375
376         // For limiting number of mesh animations per frame
377         u32 mesh_animate_count = 0;
378         u32 mesh_animate_count_far = 0;
379
380         // Blocks that were drawn and had a mesh
381         u32 blocks_drawn = 0;
382         // Blocks which had a corresponding meshbuffer for this pass
383         u32 blocks_had_pass_meshbuf = 0;
384         // Blocks from which stuff was actually drawn
385         u32 blocks_without_stuff = 0;
386
387         /*
388                 Draw the selected MapBlocks
389         */
390
391         {
392         ScopeProfiler sp(g_profiler, prefix + "drawing blocks", SPT_AVG);
393
394         MeshBufListList drawbufs;
395
396         for (std::map<v3s16, MapBlock*>::iterator i = m_drawlist.begin();
397                         i != m_drawlist.end(); ++i) {
398                 MapBlock *block = i->second;
399
400                 // If the mesh of the block happened to get deleted, ignore it
401                 if (!block->mesh)
402                         continue;
403
404                 float d = 0.0;
405                 if (!isBlockInSight(block->getPos(), camera_position,
406                                 camera_direction, camera_fov, 100000 * BS, &d))
407                         continue;
408
409                 // Mesh animation
410                 if (pass == scene::ESNRP_SOLID) {
411                         //MutexAutoLock lock(block->mesh_mutex);
412                         MapBlockMesh *mapBlockMesh = block->mesh;
413                         assert(mapBlockMesh);
414                         // Pretty random but this should work somewhat nicely
415                         bool faraway = d >= BS * 50;
416                         //bool faraway = d >= m_control.wanted_range * BS;
417                         if (mapBlockMesh->isAnimationForced() || !faraway ||
418                                         mesh_animate_count_far < (m_control.range_all ? 200 : 50)) {
419                                 bool animated = mapBlockMesh->animate(faraway, animation_time,
420                                         crack, daynight_ratio);
421                                 if (animated)
422                                         mesh_animate_count++;
423                                 if (animated && faraway)
424                                         mesh_animate_count_far++;
425                         } else {
426                                 mapBlockMesh->decreaseAnimationForceTimer();
427                         }
428                 }
429
430                 /*
431                         Get the meshbuffers of the block
432                 */
433                 {
434                         //MutexAutoLock lock(block->mesh_mutex);
435
436                         MapBlockMesh *mapBlockMesh = block->mesh;
437                         assert(mapBlockMesh);
438
439                         for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
440                                 scene::IMesh *mesh = mapBlockMesh->getMesh(layer);
441                                 assert(mesh);
442
443                                 u32 c = mesh->getMeshBufferCount();
444                                 for (u32 i = 0; i < c; i++) {
445                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
446
447                                         video::SMaterial& material = buf->getMaterial();
448                                         video::IMaterialRenderer* rnd =
449                                                 driver->getMaterialRenderer(material.MaterialType);
450                                         bool transparent = (rnd && rnd->isTransparent());
451                                         if (transparent == is_transparent_pass) {
452                                                 if (buf->getVertexCount() == 0)
453                                                         errorstream << "Block [" << analyze_block(block)
454                                                                 << "] contains an empty meshbuf" << std::endl;
455
456                                                 material.setFlag(video::EMF_TRILINEAR_FILTER,
457                                                         m_cache_trilinear_filter);
458                                                 material.setFlag(video::EMF_BILINEAR_FILTER,
459                                                         m_cache_bilinear_filter);
460                                                 material.setFlag(video::EMF_ANISOTROPIC_FILTER,
461                                                         m_cache_anistropic_filter);
462                                                 material.setFlag(video::EMF_WIREFRAME,
463                                                         m_control.show_wireframe);
464
465                                                 drawbufs.add(buf, layer);
466                                         }
467                                 }
468                         }
469                 }
470         }
471
472         std::vector<MeshBufList> &lists = drawbufs.lists;
473
474         int timecheck_counter = 0;
475         for (std::vector<MeshBufList>::iterator i = lists.begin();
476                         i != lists.end(); ++i) {
477                 timecheck_counter++;
478                 if (timecheck_counter > 50) {
479                         timecheck_counter = 0;
480                         int time2 = time(0);
481                         if (time2 > time1 + 4) {
482                                 infostream << "ClientMap::renderMap(): "
483                                         "Rendering takes ages, returning."
484                                         << std::endl;
485                                 return;
486                         }
487                 }
488
489                 MeshBufList &list = *i;
490
491                 driver->setMaterial(list.m);
492
493                 for (std::vector<scene::IMeshBuffer*>::iterator j = list.bufs.begin();
494                                 j != list.bufs.end(); ++j) {
495                         scene::IMeshBuffer *buf = *j;
496                         driver->drawMeshBuffer(buf);
497                         vertex_count += buf->getVertexCount();
498                         meshbuffer_count++;
499                 }
500
501         }
502         } // ScopeProfiler
503
504         // Log only on solid pass because values are the same
505         if (pass == scene::ESNRP_SOLID) {
506                 g_profiler->avg("CM: animated meshes", mesh_animate_count);
507                 g_profiler->avg("CM: animated meshes (far)", mesh_animate_count_far);
508         }
509
510         g_profiler->avg(prefix + "vertices drawn", vertex_count);
511         if (blocks_had_pass_meshbuf != 0)
512                 g_profiler->avg(prefix + "meshbuffers per block",
513                         (float)meshbuffer_count / (float)blocks_had_pass_meshbuf);
514         if (blocks_drawn != 0)
515                 g_profiler->avg(prefix + "empty blocks (frac)",
516                         (float)blocks_without_stuff / blocks_drawn);
517
518         /*infostream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
519                         <<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
520 }
521
522 static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
523                 float step_multiplier, float start_distance, float end_distance,
524                 INodeDefManager *ndef, u32 daylight_factor, float sunlight_min_d,
525                 int *result, bool *sunlight_seen)
526 {
527         int brightness_sum = 0;
528         int brightness_count = 0;
529         float distance = start_distance;
530         dir.normalize();
531         v3f pf = p0;
532         pf += dir * distance;
533         int noncount = 0;
534         bool nonlight_seen = false;
535         bool allow_allowing_non_sunlight_propagates = false;
536         bool allow_non_sunlight_propagates = false;
537         // Check content nearly at camera position
538         {
539                 v3s16 p = floatToInt(p0 /*+ dir * 3*BS*/, BS);
540                 MapNode n = map->getNodeNoEx(p);
541                 if(ndef->get(n).param_type == CPT_LIGHT &&
542                                 !ndef->get(n).sunlight_propagates)
543                         allow_allowing_non_sunlight_propagates = true;
544         }
545         // If would start at CONTENT_IGNORE, start closer
546         {
547                 v3s16 p = floatToInt(pf, BS);
548                 MapNode n = map->getNodeNoEx(p);
549                 if(n.getContent() == CONTENT_IGNORE){
550                         float newd = 2*BS;
551                         pf = p0 + dir * 2*newd;
552                         distance = newd;
553                         sunlight_min_d = 0;
554                 }
555         }
556         for(int i=0; distance < end_distance; i++){
557                 pf += dir * step;
558                 distance += step;
559                 step *= step_multiplier;
560
561                 v3s16 p = floatToInt(pf, BS);
562                 MapNode n = map->getNodeNoEx(p);
563                 if(allow_allowing_non_sunlight_propagates && i == 0 &&
564                                 ndef->get(n).param_type == CPT_LIGHT &&
565                                 !ndef->get(n).sunlight_propagates){
566                         allow_non_sunlight_propagates = true;
567                 }
568                 if(ndef->get(n).param_type != CPT_LIGHT ||
569                                 (!ndef->get(n).sunlight_propagates &&
570                                         !allow_non_sunlight_propagates)){
571                         nonlight_seen = true;
572                         noncount++;
573                         if(noncount >= 4)
574                                 break;
575                         continue;
576                 }
577                 if(distance >= sunlight_min_d && *sunlight_seen == false
578                                 && nonlight_seen == false)
579                         if(n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
580                                 *sunlight_seen = true;
581                 noncount = 0;
582                 brightness_sum += decode_light(n.getLightBlend(daylight_factor, ndef));
583                 brightness_count++;
584         }
585         *result = 0;
586         if(brightness_count == 0)
587                 return false;
588         *result = brightness_sum / brightness_count;
589         /*std::cerr<<"Sampled "<<brightness_count<<" points; result="
590                         <<(*result)<<std::endl;*/
591         return true;
592 }
593
594 int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
595                 int oldvalue, bool *sunlight_seen_result)
596 {
597         const bool debugprint = false;
598         static v3f z_directions[50] = {
599                 v3f(-100, 0, 0)
600         };
601         static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
602                 -1000,
603         };
604         if(z_directions[0].X < -99){
605                 for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
606                         z_directions[i] = v3f(
607                                 0.01 * myrand_range(-100, 100),
608                                 1.0,
609                                 0.01 * myrand_range(-100, 100)
610                         );
611                         z_offsets[i] = 0.01 * myrand_range(0,100);
612                 }
613         }
614         if(debugprint)
615                 std::cerr<<"In goes "<<PP(m_camera_direction)<<", out comes ";
616         int sunlight_seen_count = 0;
617         float sunlight_min_d = max_d*0.8;
618         if(sunlight_min_d > 35*BS)
619                 sunlight_min_d = 35*BS;
620         std::vector<int> values;
621         for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
622                 v3f z_dir = z_directions[i];
623                 z_dir.normalize();
624                 core::CMatrix4<f32> a;
625                 a.buildRotateFromTo(v3f(0,1,0), z_dir);
626                 v3f dir = m_camera_direction;
627                 a.rotateVect(dir);
628                 int br = 0;
629                 float step = BS*1.5;
630                 if(max_d > 35*BS)
631                         step = max_d / 35 * 1.5;
632                 float off = step * z_offsets[i];
633                 bool sunlight_seen_now = false;
634                 bool ok = getVisibleBrightness(this, m_camera_position, dir,
635                                 step, 1.0, max_d*0.6+off, max_d, m_nodedef, daylight_factor,
636                                 sunlight_min_d,
637                                 &br, &sunlight_seen_now);
638                 if(sunlight_seen_now)
639                         sunlight_seen_count++;
640                 if(!ok)
641                         continue;
642                 values.push_back(br);
643                 // Don't try too much if being in the sun is clear
644                 if(sunlight_seen_count >= 20)
645                         break;
646         }
647         int brightness_sum = 0;
648         int brightness_count = 0;
649         std::sort(values.begin(), values.end());
650         u32 num_values_to_use = values.size();
651         if(num_values_to_use >= 10)
652                 num_values_to_use -= num_values_to_use/2;
653         else if(num_values_to_use >= 7)
654                 num_values_to_use -= num_values_to_use/3;
655         u32 first_value_i = (values.size() - num_values_to_use) / 2;
656         if(debugprint){
657                 for(u32 i=0; i < first_value_i; i++)
658                         std::cerr<<values[i]<<" ";
659                 std::cerr<<"[";
660         }
661         for(u32 i=first_value_i; i < first_value_i+num_values_to_use; i++){
662                 if(debugprint)
663                         std::cerr<<values[i]<<" ";
664                 brightness_sum += values[i];
665                 brightness_count++;
666         }
667         if(debugprint){
668                 std::cerr<<"]";
669                 for(u32 i=first_value_i+num_values_to_use; i < values.size(); i++)
670                         std::cerr<<values[i]<<" ";
671         }
672         int ret = 0;
673         if(brightness_count == 0){
674                 MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
675                 if(m_nodedef->get(n).param_type == CPT_LIGHT){
676                         ret = decode_light(n.getLightBlend(daylight_factor, m_nodedef));
677                 } else {
678                         ret = oldvalue;
679                 }
680         } else {
681                 /*float pre = (float)brightness_sum / (float)brightness_count;
682                 float tmp = pre;
683                 const float d = 0.2;
684                 pre *= 1.0 + d*2;
685                 pre -= tmp * d;
686                 int preint = pre;
687                 ret = MYMAX(0, MYMIN(255, preint));*/
688                 ret = brightness_sum / brightness_count;
689         }
690         if(debugprint)
691                 std::cerr<<"Result: "<<ret<<" sunlight_seen_count="
692                                 <<sunlight_seen_count<<std::endl;
693         *sunlight_seen_result = (sunlight_seen_count > 0);
694         return ret;
695 }
696
697 void ClientMap::renderPostFx(CameraMode cam_mode)
698 {
699         // Sadly ISceneManager has no "post effects" render pass, in that case we
700         // could just register for that and handle it in renderMap().
701
702         MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
703
704         // - If the player is in a solid node, make everything black.
705         // - If the player is in liquid, draw a semi-transparent overlay.
706         // - Do not if player is in third person mode
707         const ContentFeatures& features = m_nodedef->get(n);
708         video::SColor post_effect_color = features.post_effect_color;
709         if(features.solidness == 2 && !(g_settings->getBool("noclip") &&
710                         m_client->checkLocalPrivilege("noclip")) &&
711                         cam_mode == CAMERA_MODE_FIRST)
712         {
713                 post_effect_color = video::SColor(255, 0, 0, 0);
714         }
715         if (post_effect_color.getAlpha() != 0)
716         {
717                 // Draw a full-screen rectangle
718                 video::IVideoDriver* driver = SceneManager->getVideoDriver();
719                 v2u32 ss = driver->getScreenSize();
720                 core::rect<s32> rect(0,0, ss.X, ss.Y);
721                 driver->draw2DRectangle(post_effect_color, rect);
722         }
723 }
724
725 void ClientMap::PrintInfo(std::ostream &out)
726 {
727         out<<"ClientMap: ";
728 }
729
730