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