Noise: Prevent unittest crash caused by division by zero
[oweals/minetest.git] / src / minimap.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 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 "minimap.h"
21 #include "threading/mutex_auto_lock.h"
22 #include "threading/semaphore.h"
23 #include "clientmap.h"
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "porting.h"
27 #include "util/numeric.h"
28 #include "util/string.h"
29 #include "mapblock.h"
30 #include <math.h>
31 #include "client/renderingengine.h"
32
33
34 ////
35 //// MinimapUpdateThread
36 ////
37
38 MinimapUpdateThread::~MinimapUpdateThread()
39 {
40         for (std::map<v3s16, MinimapMapblock *>::iterator
41                         it = m_blocks_cache.begin();
42                         it != m_blocks_cache.end(); ++it) {
43                 delete it->second;
44         }
45
46         for (std::deque<QueuedMinimapUpdate>::iterator
47                         it = m_update_queue.begin();
48                         it != m_update_queue.end(); ++it) {
49                 QueuedMinimapUpdate &q = *it;
50                 delete q.data;
51         }
52 }
53
54 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
55 {
56         MutexAutoLock lock(m_queue_mutex);
57
58         // Find if block is already in queue.
59         // If it is, update the data and quit.
60         for (std::deque<QueuedMinimapUpdate>::iterator
61                         it = m_update_queue.begin();
62                         it != m_update_queue.end(); ++it) {
63                 QueuedMinimapUpdate &q = *it;
64                 if (q.pos == pos) {
65                         delete q.data;
66                         q.data = data;
67                         return false;
68                 }
69         }
70
71         // Add the block
72         QueuedMinimapUpdate q;
73         q.pos  = pos;
74         q.data = data;
75         m_update_queue.push_back(q);
76
77         return true;
78 }
79
80 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
81 {
82         MutexAutoLock lock(m_queue_mutex);
83
84         if (m_update_queue.empty())
85                 return false;
86
87         *update = m_update_queue.front();
88         m_update_queue.pop_front();
89
90         return true;
91 }
92
93 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
94 {
95         pushBlockUpdate(pos, data);
96         deferUpdate();
97 }
98
99
100 void MinimapUpdateThread::doUpdate()
101 {
102         QueuedMinimapUpdate update;
103
104         while (popBlockUpdate(&update)) {
105                 if (update.data) {
106                         // Swap two values in the map using single lookup
107                         std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
108                             result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
109                         if (!result.second) {
110                                 delete result.first->second;
111                                 result.first->second = update.data;
112                         }
113                 } else {
114                         std::map<v3s16, MinimapMapblock *>::iterator it;
115                         it = m_blocks_cache.find(update.pos);
116                         if (it != m_blocks_cache.end()) {
117                                 delete it->second;
118                                 m_blocks_cache.erase(it);
119                         }
120                 }
121         }
122
123         if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
124                 getMap(data->pos, data->map_size, data->scan_height);
125                 data->map_invalidated = false;
126         }
127 }
128
129 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
130 {
131         v3s16 region(size, 0, size);
132         v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);
133         v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
134         v3s16 blockpos_min = getNodeBlockPos(pos_min);
135         v3s16 blockpos_max = getNodeBlockPos(pos_max);
136
137 // clear the map
138         for (int z = 0; z < size; z++)
139         for (int x = 0; x < size; x++) {
140                 MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
141                 mmpixel.air_count = 0;
142                 mmpixel.height = 0;
143                 mmpixel.n = MapNode(CONTENT_AIR);
144         }
145
146 // draw the map
147         v3s16 blockpos;
148         for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
149         for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y)
150         for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) {
151                 std::map<v3s16, MinimapMapblock *>::const_iterator pblock =
152                         m_blocks_cache.find(blockpos);
153                 if (pblock == m_blocks_cache.end())
154                         continue;
155                 const MinimapMapblock &block = *pblock->second;
156
157                 v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
158                 v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
159                 // clip
160                 v3s16 range_min = componentwise_max(block_node_min, pos_min);
161                 v3s16 range_max = componentwise_min(block_node_max, pos_max);
162
163                 v3s16 pos;
164                 pos.Y = range_min.Y;
165                 for (pos.Z = range_min.Z; pos.Z <= range_max.Z; ++pos.Z)
166                 for (pos.X = range_min.X; pos.X <= range_max.X; ++pos.X) {
167                         v3s16 inblock_pos = pos - block_node_min;
168                         const MinimapPixel &in_pixel =
169                                 block.data[inblock_pos.Z * MAP_BLOCKSIZE + inblock_pos.X];
170
171                         v3s16 inmap_pos = pos - pos_min;
172                         MinimapPixel &out_pixel =
173                                 data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
174
175                         out_pixel.air_count += in_pixel.air_count;
176                         if (in_pixel.n.param0 != CONTENT_AIR) {
177                                 out_pixel.n = in_pixel.n;
178                                 out_pixel.height = inmap_pos.Y + in_pixel.height;
179                         }
180                 }
181         }
182 }
183
184 ////
185 //// Mapper
186 ////
187
188 Minimap::Minimap(Client *client)
189 {
190         this->client    = client;
191         this->driver    = RenderingEngine::get_video_driver();
192         this->m_tsrc    = client->getTextureSource();
193         this->m_shdrsrc = client->getShaderSource();
194         this->m_ndef    = client->getNodeDefManager();
195
196         m_angle = 0.f;
197
198         // Initialize static settings
199         m_enable_shaders = g_settings->getBool("enable_shaders");
200         m_surface_mode_scan_height =
201                 g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
202
203         // Initialize minimap data
204         data = new MinimapData;
205         data->mode              = MINIMAP_MODE_OFF;
206         data->is_radar          = false;
207         data->map_invalidated   = true;
208         data->texture           = NULL;
209         data->heightmap_texture = NULL;
210         data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
211
212         // Get round minimap textures
213         data->minimap_mask_round = driver->createImage(
214                 m_tsrc->getTexture("minimap_mask_round.png"),
215                 core::position2d<s32>(0, 0),
216                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
217         data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
218
219         // Get square minimap textures
220         data->minimap_mask_square = driver->createImage(
221                 m_tsrc->getTexture("minimap_mask_square.png"),
222                 core::position2d<s32>(0, 0),
223                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
224         data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
225
226         // Create player marker texture
227         data->player_marker = m_tsrc->getTexture("player_marker.png");
228         // Create object marker texture
229         data->object_marker_red = m_tsrc->getTexture("object_marker_red.png");
230
231         // Create mesh buffer for minimap
232         m_meshbuffer = getMinimapMeshBuffer();
233
234         // Initialize and start thread
235         m_minimap_update_thread = new MinimapUpdateThread();
236         m_minimap_update_thread->data = data;
237         m_minimap_update_thread->start();
238 }
239
240 Minimap::~Minimap()
241 {
242         m_minimap_update_thread->stop();
243         m_minimap_update_thread->wait();
244
245         m_meshbuffer->drop();
246
247         data->minimap_mask_round->drop();
248         data->minimap_mask_square->drop();
249
250         driver->removeTexture(data->texture);
251         driver->removeTexture(data->heightmap_texture);
252         driver->removeTexture(data->minimap_overlay_round);
253         driver->removeTexture(data->minimap_overlay_square);
254         driver->removeTexture(data->object_marker_red);
255
256         delete data;
257         delete m_minimap_update_thread;
258 }
259
260 void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
261 {
262         m_minimap_update_thread->enqueueBlock(pos, data);
263 }
264
265 void Minimap::toggleMinimapShape()
266 {
267         MutexAutoLock lock(m_mutex);
268
269         data->minimap_shape_round = !data->minimap_shape_round;
270         g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
271         m_minimap_update_thread->deferUpdate();
272 }
273
274 void Minimap::setMinimapShape(MinimapShape shape)
275 {
276         MutexAutoLock lock(m_mutex);
277
278         if (shape == MINIMAP_SHAPE_SQUARE)
279                 data->minimap_shape_round = false;
280         else if (shape == MINIMAP_SHAPE_ROUND)
281                 data->minimap_shape_round = true;
282
283         g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
284         m_minimap_update_thread->deferUpdate();
285 }
286
287 MinimapShape Minimap::getMinimapShape()
288 {
289         if (data->minimap_shape_round) {
290                 return MINIMAP_SHAPE_ROUND;
291         } else {
292                 return MINIMAP_SHAPE_SQUARE;
293         }
294 }
295
296 void Minimap::setMinimapMode(MinimapMode mode)
297 {
298         static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
299                 {false, 0, 0},
300                 {false, m_surface_mode_scan_height, 256},
301                 {false, m_surface_mode_scan_height, 128},
302                 {false, m_surface_mode_scan_height, 64},
303                 {true, 32, 128},
304                 {true, 32, 64},
305                 {true, 32, 32}
306         };
307
308         if (mode >= MINIMAP_MODE_COUNT)
309                 return;
310
311         MutexAutoLock lock(m_mutex);
312
313         data->is_radar    = modedefs[mode].is_radar;
314         data->scan_height = modedefs[mode].scan_height;
315         data->map_size    = modedefs[mode].map_size;
316         data->mode        = mode;
317
318         m_minimap_update_thread->deferUpdate();
319 }
320
321 void Minimap::setPos(v3s16 pos)
322 {
323         bool do_update = false;
324
325         {
326                 MutexAutoLock lock(m_mutex);
327
328                 if (pos != data->old_pos) {
329                         data->old_pos = data->pos;
330                         data->pos = pos;
331                         do_update = true;
332                 }
333         }
334
335         if (do_update)
336                 m_minimap_update_thread->deferUpdate();
337 }
338
339 void Minimap::setAngle(f32 angle)
340 {
341         m_angle = angle;
342 }
343
344 void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
345 {
346         video::SColor c(240, 0, 0, 0);
347         for (s16 x = 0; x < data->map_size; x++)
348         for (s16 z = 0; z < data->map_size; z++) {
349                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
350
351                 if (mmpixel->air_count > 0)
352                         c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
353                 else
354                         c.setGreen(0);
355
356                 map_image->setPixel(x, data->map_size - z - 1, c);
357         }
358 }
359
360 void Minimap::blitMinimapPixelsToImageSurface(
361         video::IImage *map_image, video::IImage *heightmap_image)
362 {
363         // This variable creation/destruction has a 1% cost on rendering minimap
364         video::SColor tilecolor;
365         for (s16 x = 0; x < data->map_size; x++)
366         for (s16 z = 0; z < data->map_size; z++) {
367                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
368
369                 const ContentFeatures &f = m_ndef->get(mmpixel->n);
370                 const TileDef *tile = &f.tiledef[0];
371
372                 // Color of the 0th tile (mostly this is the topmost)
373                 if(tile->has_color)
374                         tilecolor = tile->color;
375                 else
376                         mmpixel->n.getColor(f, &tilecolor);
377
378                 tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() / 255);
379                 tilecolor.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
380                 tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
381                 tilecolor.setAlpha(240);
382
383                 map_image->setPixel(x, data->map_size - z - 1, tilecolor);
384
385                 u32 h = mmpixel->height;
386                 heightmap_image->setPixel(x,data->map_size - z - 1,
387                         video::SColor(255, h, h, h));
388         }
389 }
390
391 video::ITexture *Minimap::getMinimapTexture()
392 {
393         // update minimap textures when new scan is ready
394         if (data->map_invalidated)
395                 return data->texture;
396
397         // create minimap and heightmap images in memory
398         core::dimension2d<u32> dim(data->map_size, data->map_size);
399         video::IImage *map_image       = driver->createImage(video::ECF_A8R8G8B8, dim);
400         video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
401         video::IImage *minimap_image   = driver->createImage(video::ECF_A8R8G8B8,
402                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
403
404         // Blit MinimapPixels to images
405         if (data->is_radar)
406                 blitMinimapPixelsToImageRadar(map_image);
407         else
408                 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
409
410         map_image->copyToScaling(minimap_image);
411         map_image->drop();
412
413         video::IImage *minimap_mask = data->minimap_shape_round ?
414                 data->minimap_mask_round : data->minimap_mask_square;
415
416         if (minimap_mask) {
417                 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
418                 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
419                         const video::SColor &mask_col = minimap_mask->getPixel(x, y);
420                         if (!mask_col.getAlpha())
421                                 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
422                 }
423         }
424
425         if (data->texture)
426                 driver->removeTexture(data->texture);
427         if (data->heightmap_texture)
428                 driver->removeTexture(data->heightmap_texture);
429
430         data->texture = driver->addTexture("minimap__", minimap_image);
431         data->heightmap_texture =
432                 driver->addTexture("minimap_heightmap__", heightmap_image);
433         minimap_image->drop();
434         heightmap_image->drop();
435
436         data->map_invalidated = true;
437
438         return data->texture;
439 }
440
441 v3f Minimap::getYawVec()
442 {
443         if (data->minimap_shape_round) {
444                 return v3f(
445                         cos(m_angle * core::DEGTORAD),
446                         sin(m_angle * core::DEGTORAD),
447                         1.0);
448         } else {
449                 return v3f(1.0, 0.0, 1.0);
450         }
451 }
452
453 scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
454 {
455         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
456         buf->Vertices.set_used(4);
457         buf->Indices.set_used(6);
458         static const video::SColor c(255, 255, 255, 255);
459
460         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
461         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
462         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
463         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
464
465         buf->Indices[0] = 0;
466         buf->Indices[1] = 1;
467         buf->Indices[2] = 2;
468         buf->Indices[3] = 2;
469         buf->Indices[4] = 3;
470         buf->Indices[5] = 0;
471
472         return buf;
473 }
474
475 void Minimap::drawMinimap()
476 {
477         video::ITexture *minimap_texture = getMinimapTexture();
478         if (!minimap_texture)
479                 return;
480
481         updateActiveMarkers();
482         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
483         const u32 size = 0.25 * screensize.Y;
484
485         core::rect<s32> oldViewPort = driver->getViewPort();
486         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
487         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
488
489         driver->setViewPort(core::rect<s32>(
490                 screensize.X - size - 10, 10,
491                 screensize.X - 10, size + 10));
492         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
493         driver->setTransform(video::ETS_VIEW, core::matrix4());
494
495         core::matrix4 matrix;
496         matrix.makeIdentity();
497
498         video::SMaterial &material = m_meshbuffer->getMaterial();
499         material.setFlag(video::EMF_TRILINEAR_FILTER, true);
500         material.Lighting = false;
501         material.TextureLayer[0].Texture = minimap_texture;
502         material.TextureLayer[1].Texture = data->heightmap_texture;
503
504         if (m_enable_shaders && !data->is_radar) {
505                 u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
506                 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
507         } else {
508                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
509         }
510
511         if (data->minimap_shape_round)
512                 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
513
514         // Draw minimap
515         driver->setTransform(video::ETS_WORLD, matrix);
516         driver->setMaterial(material);
517         driver->drawMeshBuffer(m_meshbuffer);
518
519         // Draw overlay
520         video::ITexture *minimap_overlay = data->minimap_shape_round ?
521                 data->minimap_overlay_round : data->minimap_overlay_square;
522         material.TextureLayer[0].Texture = minimap_overlay;
523         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
524         driver->setMaterial(material);
525         driver->drawMeshBuffer(m_meshbuffer);
526
527         // If round minimap, draw player marker
528         if (!data->minimap_shape_round) {
529                 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
530                 material.TextureLayer[0].Texture = data->player_marker;
531
532                 driver->setTransform(video::ETS_WORLD, matrix);
533                 driver->setMaterial(material);
534                 driver->drawMeshBuffer(m_meshbuffer);
535         }
536
537         // Reset transformations
538         driver->setTransform(video::ETS_VIEW, oldViewMat);
539         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
540         driver->setViewPort(oldViewPort);
541
542         // Draw player markers
543         v2s32 s_pos(screensize.X - size - 10, 10);
544         core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
545         core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
546         static const video::SColor col(255, 255, 255, 255);
547         static const video::SColor c[4] = {col, col, col, col};
548         f32 sin_angle = sin(m_angle * core::DEGTORAD);
549         f32 cos_angle = cos(m_angle * core::DEGTORAD);
550         s32 marker_size2 =  0.025 * (float)size;
551         for (std::list<v2f>::const_iterator
552                         i = m_active_markers.begin();
553                         i != m_active_markers.end(); ++i) {
554                 v2f posf = *i;
555                 if (data->minimap_shape_round) {
556                         f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
557                         f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
558                         posf.X = t1;
559                         posf.Y = t2;
560                 }
561                 posf.X = (posf.X + 0.5) * (float)size;
562                 posf.Y = (posf.Y + 0.5) * (float)size;
563                 core::rect<s32> dest_rect(
564                         s_pos.X + posf.X - marker_size2,
565                         s_pos.Y + posf.Y - marker_size2,
566                         s_pos.X + posf.X + marker_size2,
567                         s_pos.Y + posf.Y + marker_size2);
568                 driver->draw2DImage(data->object_marker_red, dest_rect,
569                         img_rect, &dest_rect, &c[0], true);
570         }
571 }
572
573 void Minimap::updateActiveMarkers()
574 {
575         video::IImage *minimap_mask = data->minimap_shape_round ?
576                 data->minimap_mask_round : data->minimap_mask_square;
577
578         const std::list<Nametag *> &nametags = client->getCamera()->getNametags();
579
580         m_active_markers.clear();
581
582         for (std::list<Nametag *>::const_iterator i = nametags.begin();
583                         i != nametags.end(); ++i) {
584                 v3s16 pos = floatToInt((*i)->parent_node->getPosition() +
585                         intToFloat(client->getCamera()->getOffset(), BS), BS);
586                 pos -= data->pos - v3s16(data->map_size / 2,
587                                 data->scan_height / 2,
588                                 data->map_size / 2);
589                 if (pos.X < 0 || pos.X > data->map_size ||
590                                 pos.Y < 0 || pos.Y > data->scan_height ||
591                                 pos.Z < 0 || pos.Z > data->map_size) {
592                         continue;
593                 }
594                 pos.X = ((float)pos.X / data->map_size) * MINIMAP_MAX_SX;
595                 pos.Z = ((float)pos.Z / data->map_size) * MINIMAP_MAX_SY;
596                 const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
597                 if (!mask_col.getAlpha()) {
598                         continue;
599                 }
600                 m_active_markers.push_back(v2f(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
601                         (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5));
602         }
603 }
604
605 ////
606 //// MinimapMapblock
607 ////
608
609 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
610 {
611
612         for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
613         for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
614                 s16 air_count = 0;
615                 bool surface_found = false;
616                 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
617
618                 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
619                         v3s16 p(x, y, z);
620                         MapNode n = vmanip->getNodeNoEx(pos + p);
621                         if (!surface_found && n.getContent() != CONTENT_AIR) {
622                                 mmpixel->height = y;
623                                 mmpixel->n = n;
624                                 surface_found = true;
625                         } else if (n.getContent() == CONTENT_AIR) {
626                                 air_count++;
627                         }
628                 }
629
630                 if (!surface_found)
631                         mmpixel->n = MapNode(CONTENT_AIR);
632
633                 mmpixel->air_count = air_count;
634         }
635 }