]> git.lizzy.rs Git - dragonfireclient.git/blob - src/clientmap.cpp
Try to make background/fog/sky color selection work better
[dragonfireclient.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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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(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                         v3s16 cpn = block->getPos() * MAP_BLOCKSIZE;
328                         cpn += v3s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2);
329                         float step = BS*1;
330                         float stepfac = 1.1;
331                         float startoff = BS*1;
332                         float endoff = -BS*MAP_BLOCKSIZE*1.42*1.42;
333                         v3s16 spn = cam_pos_nodes + v3s16(0,0,0);
334                         s16 bs2 = MAP_BLOCKSIZE/2 + 1;
335                         u32 needed_count = 1;
336                         if(
337                                 isOccluded(this, spn, cpn + v3s16(0,0,0),
338                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
339                                 isOccluded(this, spn, cpn + v3s16(bs2,bs2,bs2),
340                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
341                                 isOccluded(this, spn, cpn + v3s16(bs2,bs2,-bs2),
342                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
343                                 isOccluded(this, spn, cpn + v3s16(bs2,-bs2,bs2),
344                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
345                                 isOccluded(this, spn, cpn + v3s16(bs2,-bs2,-bs2),
346                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
347                                 isOccluded(this, spn, cpn + v3s16(-bs2,bs2,bs2),
348                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
349                                 isOccluded(this, spn, cpn + v3s16(-bs2,bs2,-bs2),
350                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
351                                 isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,bs2),
352                                         step, stepfac, startoff, endoff, needed_count, nodemgr) &&
353                                 isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,-bs2),
354                                         step, stepfac, startoff, endoff, needed_count, nodemgr)
355                         )
356                         {
357                                 blocks_occlusion_culled++;
358                                 continue;
359                         }
360                         
361                         // This block is in range. Reset usage timer.
362                         block->resetUsageTimer();
363
364                         // Limit block count in case of a sudden increase
365                         blocks_would_have_drawn++;
366                         if(blocks_drawn >= m_control.wanted_max_blocks
367                                         && m_control.range_all == false
368                                         && d > m_control.wanted_min_range * BS)
369                                 continue;
370
371                         // Mesh animation
372                         {
373                                 //JMutexAutoLock lock(block->mesh_mutex);
374                                 MapBlockMesh *mapBlockMesh = block->mesh;
375                                 // Pretty random but this should work somewhat nicely
376                                 bool faraway = d >= BS*50;
377                                 //bool faraway = d >= m_control.wanted_range * BS;
378                                 if(mapBlockMesh->isAnimationForced() ||
379                                                 !faraway ||
380                                                 mesh_animate_count_far < (m_control.range_all ? 200 : 50))
381                                 {
382                                         bool animated = mapBlockMesh->animate(
383                                                         faraway,
384                                                         animation_time,
385                                                         crack,
386                                                         daynight_ratio);
387                                         if(animated)
388                                                 mesh_animate_count++;
389                                         if(animated && faraway)
390                                                 mesh_animate_count_far++;
391                                 }
392                                 else
393                                 {
394                                         mapBlockMesh->decreaseAnimationForceTimer();
395                                 }
396                         }
397
398                         // Add to set
399                         drawset[block->getPos()] = block;
400                         
401                         sector_blocks_drawn++;
402                         blocks_drawn++;
403
404                 } // foreach sectorblocks
405
406                 if(sector_blocks_drawn != 0)
407                         m_last_drawn_sectors[sp] = true;
408         }
409         } // ScopeProfiler
410         
411         /*
412                 Draw the selected MapBlocks
413         */
414
415         {
416         ScopeProfiler sp(g_profiler, prefix+"drawing blocks", SPT_AVG);
417
418         int timecheck_counter = 0;
419         for(core::map<v3s16, MapBlock*>::Iterator
420                         i = drawset.getIterator();
421                         i.atEnd() == false; i++)
422         {
423                 {
424                         timecheck_counter++;
425                         if(timecheck_counter > 50)
426                         {
427                                 timecheck_counter = 0;
428                                 int time2 = time(0);
429                                 if(time2 > time1 + 4)
430                                 {
431                                         infostream<<"ClientMap::renderMap(): "
432                                                 "Rendering takes ages, returning."
433                                                 <<std::endl;
434                                         return;
435                                 }
436                         }
437                 }
438                 
439                 MapBlock *block = i.getNode()->getValue();
440
441                 /*
442                         Draw the faces of the block
443                 */
444                 {
445                         //JMutexAutoLock lock(block->mesh_mutex);
446
447                         MapBlockMesh *mapBlockMesh = block->mesh;
448                         assert(mapBlockMesh);
449
450                         scene::SMesh *mesh = mapBlockMesh->getMesh();
451                         assert(mesh);
452
453                         u32 c = mesh->getMeshBufferCount();
454                         bool stuff_actually_drawn = false;
455                         for(u32 i=0; i<c; i++)
456                         {
457                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
458                                 const video::SMaterial& material = buf->getMaterial();
459                                 video::IMaterialRenderer* rnd =
460                                                 driver->getMaterialRenderer(material.MaterialType);
461                                 bool transparent = (rnd && rnd->isTransparent());
462                                 // Render transparent on transparent pass and likewise.
463                                 if(transparent == is_transparent_pass)
464                                 {
465                                         if(buf->getVertexCount() == 0)
466                                                 errorstream<<"Block ["<<analyze_block(block)
467                                                                 <<"] contains an empty meshbuf"<<std::endl;
468                                         /*
469                                                 This *shouldn't* hurt too much because Irrlicht
470                                                 doesn't change opengl textures if the old
471                                                 material has the same texture.
472                                         */
473                                         driver->setMaterial(buf->getMaterial());
474                                         driver->drawMeshBuffer(buf);
475                                         vertex_count += buf->getVertexCount();
476                                         meshbuffer_count++;
477                                         stuff_actually_drawn = true;
478                                 }
479                         }
480                         if(stuff_actually_drawn)
481                                 blocks_had_pass_meshbuf++;
482                         else
483                                 blocks_without_stuff++;
484                 }
485         }
486         } // ScopeProfiler
487         
488         // Log only on solid pass because values are the same
489         if(pass == scene::ESNRP_SOLID){
490                 g_profiler->avg("CM: blocks in range", blocks_in_range);
491                 g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled);
492                 if(blocks_in_range != 0)
493                         g_profiler->avg("CM: blocks in range without mesh (frac)",
494                                         (float)blocks_in_range_without_mesh/blocks_in_range);
495                 g_profiler->avg("CM: blocks drawn", blocks_drawn);
496                 g_profiler->avg("CM: animated meshes", mesh_animate_count);
497                 g_profiler->avg("CM: animated meshes (far)", mesh_animate_count_far);
498         }
499         
500         g_profiler->avg(prefix+"vertices drawn", vertex_count);
501         if(blocks_had_pass_meshbuf != 0)
502                 g_profiler->avg(prefix+"meshbuffers per block",
503                                 (float)meshbuffer_count / (float)blocks_had_pass_meshbuf);
504         if(blocks_drawn != 0)
505                 g_profiler->avg(prefix+"empty blocks (frac)",
506                                 (float)blocks_without_stuff / blocks_drawn);
507
508         m_control.blocks_drawn = blocks_drawn;
509         m_control.blocks_would_have_drawn = blocks_would_have_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         INodeDefManager *ndef = m_gamedef->ndef();
592         static v3f z_directions[50] = {
593                 v3f(-100, 0, 0)
594         };
595         static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
596                 -1000,
597         };
598         if(z_directions[0].X < -99){
599                 for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
600                         z_directions[i] = v3f(
601                                 0.01 * myrand_range(-100, 100),
602                                 1.0,
603                                 0.01 * myrand_range(-100, 100)
604                         );
605                         z_offsets[i] = 0.01 * myrand_range(0,100);
606                 }
607         }
608         if(debugprint)
609                 std::cerr<<"In goes "<<PP(m_camera_direction)<<", out comes ";
610         int sunlight_seen_count = 0;
611         float sunlight_min_d = max_d*0.8;
612         if(sunlight_min_d > 35*BS)
613                 sunlight_min_d = 35*BS;
614         core::array<int> values;
615         for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
616                 v3f z_dir = z_directions[i];
617                 z_dir.normalize();
618                 core::CMatrix4<f32> a;
619                 a.buildRotateFromTo(v3f(0,1,0), z_dir);
620                 v3f dir = m_camera_direction;
621                 a.rotateVect(dir);
622                 int br = 0;
623                 float step = BS*1.5;
624                 if(max_d > 35*BS)
625                         step = max_d / 35 * 1.5;
626                 float off = step * z_offsets[i];
627                 bool sunlight_seen_now = false;
628                 bool ok = getVisibleBrightness(this, m_camera_position, dir,
629                                 step, 1.0, max_d*0.6+off, max_d, ndef, daylight_factor,
630                                 sunlight_min_d,
631                                 &br, &sunlight_seen_now);
632                 if(sunlight_seen_now)
633                         sunlight_seen_count++;
634                 if(!ok)
635                         continue;
636                 values.push_back(br);
637                 // Don't try too much if being in the sun is clear
638                 if(sunlight_seen_count >= 20)
639                         break;
640         }
641         int brightness_sum = 0;
642         int brightness_count = 0;
643         values.sort();
644         u32 num_values_to_use = values.size();
645         if(num_values_to_use >= 10)
646                 num_values_to_use -= num_values_to_use/2;
647         else if(num_values_to_use >= 7)
648                 num_values_to_use -= num_values_to_use/3;
649         u32 first_value_i = (values.size() - num_values_to_use) / 2;
650         if(debugprint){
651                 for(u32 i=0; i < first_value_i; i++)
652                         std::cerr<<values[i]<<" ";
653                 std::cerr<<"[";
654         }
655         for(u32 i=first_value_i; i < first_value_i+num_values_to_use; i++){
656                 if(debugprint)
657                         std::cerr<<values[i]<<" ";
658                 brightness_sum += values[i];
659                 brightness_count++;
660         }
661         if(debugprint){
662                 std::cerr<<"]";
663                 for(u32 i=first_value_i+num_values_to_use; i < values.size(); i++)
664                         std::cerr<<values[i]<<" ";
665         }
666         int ret = 0;
667         if(brightness_count == 0){
668                 MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
669                 if(ndef->get(n).param_type == CPT_LIGHT){
670                         ret = decode_light(n.getLightBlend(daylight_factor, ndef));
671                 } else {
672                         ret = oldvalue;
673                         //ret = blend_light(255, 0, daylight_factor);
674                 }
675         } else {
676                 /*float pre = (float)brightness_sum / (float)brightness_count;
677                 float tmp = pre;
678                 const float d = 0.2;
679                 pre *= 1.0 + d*2;
680                 pre -= tmp * d;
681                 int preint = pre;
682                 ret = MYMAX(0, MYMIN(255, preint));*/
683                 ret = brightness_sum / brightness_count;
684         }
685         if(debugprint)
686                 std::cerr<<"Result: "<<ret<<" sunlight_seen_count="
687                                 <<sunlight_seen_count<<std::endl;
688         *sunlight_seen_result = (sunlight_seen_count > 0);
689         return ret;
690 }
691
692 void ClientMap::renderPostFx()
693 {
694         INodeDefManager *nodemgr = m_gamedef->ndef();
695
696         // Sadly ISceneManager has no "post effects" render pass, in that case we
697         // could just register for that and handle it in renderMap().
698
699         m_camera_mutex.Lock();
700         v3f camera_position = m_camera_position;
701         m_camera_mutex.Unlock();
702
703         MapNode n = getNodeNoEx(floatToInt(camera_position, BS));
704
705         // - If the player is in a solid node, make everything black.
706         // - If the player is in liquid, draw a semi-transparent overlay.
707         const ContentFeatures& features = nodemgr->get(n);
708         video::SColor post_effect_color = features.post_effect_color;
709         if(features.solidness == 2 && g_settings->getBool("free_move") == false)
710         {
711                 post_effect_color = video::SColor(255, 0, 0, 0);
712         }
713         if (post_effect_color.getAlpha() != 0)
714         {
715                 // Draw a full-screen rectangle
716                 video::IVideoDriver* driver = SceneManager->getVideoDriver();
717                 v2u32 ss = driver->getScreenSize();
718                 core::rect<s32> rect(0,0, ss.X, ss.Y);
719                 driver->draw2DRectangle(post_effect_color, rect);
720         }
721 }
722
723 void ClientMap::PrintInfo(std::ostream &out)
724 {
725         out<<"ClientMap: ";
726 }
727
728