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