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