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