]> git.lizzy.rs Git - minetest.git/blob - src/content_mapblock.cpp
When player times out, log the action as a timeout rather than regular quitting
[minetest.git] / src / content_mapblock.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 "content_mapblock.h"
21 #include "content_mapnode.h"
22 #include "main.h" // For g_settings and g_texturesource
23 #include "mineral.h"
24 #include "mapblock_mesh.h" // For MapBlock_LightColor()
25 #include "settings.h"
26
27 #ifndef SERVER
28 // Create a cuboid.
29 //  material  - the material to use (for all 6 faces)
30 //  collector - the MeshCollector for the resulting polygons
31 //  pa        - texture atlas pointer for the material
32 //  c         - vertex colour - used for all
33 //  pos       - the position of the centre of the cuboid
34 //  rz,ry,rz  - the radius of the cuboid in each dimension
35 //  txc       - texture coordinates - this is a list of texture coordinates
36 //              for the opposite corners of each face - therefore, there
37 //              should be (2+2)*6=24 values in the list. Alternatively, pass
38 //              NULL to use the entire texture for each face. The order of
39 //              the faces in the list is top-backi-right-front-left-bottom
40 //              If you specified 0,0,1,1 for each face, that would be the
41 //              same as passing NULL.
42 void makeCuboid(video::SMaterial &material, MeshCollector *collector,
43         AtlasPointer* pa, video::SColor &c,
44         v3f &pos, f32 rx, f32 ry, f32 rz, f32* txc)
45 {
46         f32 tu0=pa->x0();
47         f32 tu1=pa->x1();
48         f32 tv0=pa->y0();
49         f32 tv1=pa->y1();
50         f32 txus=tu1-tu0;
51         f32 txvs=tv1-tv0;
52
53         video::S3DVertex v[4] =
54         {
55                 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv1),
56                 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv1),
57                 video::S3DVertex(0,0,0, 0,0,0, c, tu1, tv0),
58                 video::S3DVertex(0,0,0, 0,0,0, c, tu0, tv0)
59         };
60
61         for(int i=0;i<6;i++)
62         {
63                 switch(i)
64                 {
65                         case 0: // top
66                                 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
67                                 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
68                                 v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
69                                 v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
70                                 break;
71                         case 1: // back
72                                 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
73                                 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
74                                 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
75                                 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
76                                 break;
77                         case 2: //right
78                                 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
79                                 v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
80                                 v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
81                                 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
82                                 break;
83                         case 3: // front
84                                 v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
85                                 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
86                                 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
87                                 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
88                                 break;
89                         case 4: // left
90                                 v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
91                                 v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
92                                 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
93                                 v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
94                                 break;
95                         case 5: // bottom
96                                 v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
97                                 v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
98                                 v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
99                                 v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
100                                 break;
101                 }
102
103                 if(txc!=NULL)
104                 {
105                         v[0].TCoords.X=tu0+txus*txc[0]; v[0].TCoords.Y=tv0+txvs*txc[3];
106                         v[1].TCoords.X=tu0+txus*txc[2]; v[1].TCoords.Y=tv0+txvs*txc[3];
107                         v[2].TCoords.X=tu0+txus*txc[2]; v[2].TCoords.Y=tv0+txvs*txc[1];
108                         v[3].TCoords.X=tu0+txus*txc[0]; v[3].TCoords.Y=tv0+txvs*txc[1];
109                         txc+=4;
110                 }
111
112                 for(u16 i=0; i<4; i++)
113                         v[i].Pos += pos;
114                 u16 indices[] = {0,1,2,2,3,0};
115                 collector->append(material, v, 4, indices, 6);
116
117         }
118
119 }
120 #endif
121
122 #ifndef SERVER
123 void mapblock_mesh_generate_special(MeshMakeData *data,
124                 MeshCollector &collector)
125 {
126         // 0ms
127         //TimeTaker timer("mapblock_mesh_generate_special()");
128
129         /*
130                 Some settings
131         */
132         bool new_style_water = g_settings->getBool("new_style_water");
133         bool new_style_leaves = g_settings->getBool("new_style_leaves");
134         //bool smooth_lighting = g_settings->getBool("smooth_lighting");
135         bool invisible_stone = g_settings->getBool("invisible_stone");
136         
137         float node_liquid_level = 1.0;
138         if(new_style_water)
139                 node_liquid_level = 0.85;
140         
141         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
142
143         // New-style leaves material
144         video::SMaterial material_leaves1;
145         material_leaves1.setFlag(video::EMF_LIGHTING, false);
146         //material_leaves1.setFlag(video::EMF_BACK_FACE_CULLING, false);
147         material_leaves1.setFlag(video::EMF_BILINEAR_FILTER, false);
148         material_leaves1.setFlag(video::EMF_FOG_ENABLE, true);
149         material_leaves1.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
150         AtlasPointer pa_leaves1 = g_texturesource->getTexture(
151                         g_texturesource->getTextureId("leaves.png"));
152         material_leaves1.setTexture(0, pa_leaves1.atlas);
153
154         // Glass material
155         video::SMaterial material_glass;
156         material_glass.setFlag(video::EMF_LIGHTING, false);
157         material_glass.setFlag(video::EMF_BILINEAR_FILTER, false);
158         material_glass.setFlag(video::EMF_FOG_ENABLE, true);
159         material_glass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
160         AtlasPointer pa_glass = g_texturesource->getTexture(
161                         g_texturesource->getTextureId("glass.png"));
162         material_glass.setTexture(0, pa_glass.atlas);
163
164         // Wood material
165         video::SMaterial material_wood;
166         material_wood.setFlag(video::EMF_LIGHTING, false);
167         material_wood.setFlag(video::EMF_BILINEAR_FILTER, false);
168         material_wood.setFlag(video::EMF_FOG_ENABLE, true);
169         material_wood.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
170         AtlasPointer pa_wood = g_texturesource->getTexture(
171                         g_texturesource->getTextureId("wood.png"));
172         material_wood.setTexture(0, pa_wood.atlas);
173
174         // General ground material for special output
175         // Texture is modified just before usage
176         video::SMaterial material_general;
177         material_general.setFlag(video::EMF_LIGHTING, false);
178         material_general.setFlag(video::EMF_BILINEAR_FILTER, false);
179         material_general.setFlag(video::EMF_FOG_ENABLE, true);
180         material_general.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
181
182
183         // Papyrus material
184         video::SMaterial material_papyrus;
185         material_papyrus.setFlag(video::EMF_LIGHTING, false);
186         material_papyrus.setFlag(video::EMF_BILINEAR_FILTER, false);
187         material_papyrus.setFlag(video::EMF_FOG_ENABLE, true);
188         material_papyrus.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
189         AtlasPointer pa_papyrus = g_texturesource->getTexture(
190                         g_texturesource->getTextureId("papyrus.png"));
191         material_papyrus.setTexture(0, pa_papyrus.atlas);
192         
193         // Apple material
194         video::SMaterial material_apple;
195         material_apple.setFlag(video::EMF_LIGHTING, false);
196         material_apple.setFlag(video::EMF_BILINEAR_FILTER, false);
197         material_apple.setFlag(video::EMF_FOG_ENABLE, true);
198         material_apple.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
199         AtlasPointer pa_apple = g_texturesource->getTexture(
200                         g_texturesource->getTextureId("apple.png"));
201         material_apple.setTexture(0, pa_apple.atlas);
202
203
204         // Sapling material
205         video::SMaterial material_sapling;
206         material_sapling.setFlag(video::EMF_LIGHTING, false);
207         material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
208         material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
209         material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
210         AtlasPointer pa_sapling = g_texturesource->getTexture(
211                         g_texturesource->getTextureId("sapling.png"));
212         material_sapling.setTexture(0, pa_sapling.atlas);
213
214
215         // junglegrass material
216         video::SMaterial material_junglegrass;
217         material_junglegrass.setFlag(video::EMF_LIGHTING, false);
218         material_junglegrass.setFlag(video::EMF_BILINEAR_FILTER, false);
219         material_junglegrass.setFlag(video::EMF_FOG_ENABLE, true);
220         material_junglegrass.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
221         AtlasPointer pa_junglegrass = g_texturesource->getTexture(
222                         g_texturesource->getTextureId("junglegrass.png"));
223         material_junglegrass.setTexture(0, pa_junglegrass.atlas);
224
225         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
226         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
227         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
228         {
229                 v3s16 p(x,y,z);
230
231                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
232                 
233                 /*
234                         Add torches to mesh
235                 */
236                 if(n.getContent() == CONTENT_TORCH)
237                 {
238                         video::SColor c(255,255,255,255);
239
240                         // Wall at X+ of node
241                         video::S3DVertex vertices[4] =
242                         {
243                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
244                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
245                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
246                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
247                         };
248
249                         v3s16 dir = unpackDir(n.param2);
250
251                         for(s32 i=0; i<4; i++)
252                         {
253                                 if(dir == v3s16(1,0,0))
254                                         vertices[i].Pos.rotateXZBy(0);
255                                 if(dir == v3s16(-1,0,0))
256                                         vertices[i].Pos.rotateXZBy(180);
257                                 if(dir == v3s16(0,0,1))
258                                         vertices[i].Pos.rotateXZBy(90);
259                                 if(dir == v3s16(0,0,-1))
260                                         vertices[i].Pos.rotateXZBy(-90);
261                                 if(dir == v3s16(0,-1,0))
262                                         vertices[i].Pos.rotateXZBy(45);
263                                 if(dir == v3s16(0,1,0))
264                                         vertices[i].Pos.rotateXZBy(-45);
265
266                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
267                         }
268
269                         // Set material
270                         video::SMaterial material;
271                         material.setFlag(video::EMF_LIGHTING, false);
272                         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
273                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
274                         //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
275                         material.MaterialType
276                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
277
278                         if(dir == v3s16(0,-1,0))
279                                 material.setTexture(0,
280                                                 g_texturesource->getTextureRaw("torch_on_floor.png"));
281                         else if(dir == v3s16(0,1,0))
282                                 material.setTexture(0,
283                                                 g_texturesource->getTextureRaw("torch_on_ceiling.png"));
284                         // For backwards compatibility
285                         else if(dir == v3s16(0,0,0))
286                                 material.setTexture(0,
287                                                 g_texturesource->getTextureRaw("torch_on_floor.png"));
288                         else
289                                 material.setTexture(0, 
290                                                 g_texturesource->getTextureRaw("torch.png"));
291
292                         u16 indices[] = {0,1,2,2,3,0};
293                         // Add to mesh collector
294                         collector.append(material, vertices, 4, indices, 6);
295                 }
296                 /*
297                         Signs on walls
298                 */
299                 else if(n.getContent() == CONTENT_SIGN_WALL)
300                 {
301                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
302                         video::SColor c = MapBlock_LightColor(255, l);
303                                 
304                         float d = (float)BS/16;
305                         // Wall at X+ of node
306                         video::S3DVertex vertices[4] =
307                         {
308                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
309                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
310                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
311                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
312                         };
313
314                         v3s16 dir = unpackDir(n.param2);
315
316                         for(s32 i=0; i<4; i++)
317                         {
318                                 if(dir == v3s16(1,0,0))
319                                         vertices[i].Pos.rotateXZBy(0);
320                                 if(dir == v3s16(-1,0,0))
321                                         vertices[i].Pos.rotateXZBy(180);
322                                 if(dir == v3s16(0,0,1))
323                                         vertices[i].Pos.rotateXZBy(90);
324                                 if(dir == v3s16(0,0,-1))
325                                         vertices[i].Pos.rotateXZBy(-90);
326                                 if(dir == v3s16(0,-1,0))
327                                         vertices[i].Pos.rotateXYBy(-90);
328                                 if(dir == v3s16(0,1,0))
329                                         vertices[i].Pos.rotateXYBy(90);
330
331                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
332                         }
333
334                         // Set material
335                         video::SMaterial material;
336                         material.setFlag(video::EMF_LIGHTING, false);
337                         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
338                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
339                         material.setFlag(video::EMF_FOG_ENABLE, true);
340                         //material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
341                         material.MaterialType
342                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
343
344                         material.setTexture(0, 
345                                         g_texturesource->getTextureRaw("sign_wall.png"));
346
347                         u16 indices[] = {0,1,2,2,3,0};
348                         // Add to mesh collector
349                         collector.append(material, vertices, 4, indices, 6);
350                 }
351                 /*
352                         Add flowing liquid to mesh
353                 */
354                 else if(content_features(n).liquid_type == LIQUID_FLOWING)
355                 {
356                         assert(content_features(n).special_material);
357                         video::SMaterial &liquid_material =
358                                         *content_features(n).special_material;
359                         assert(content_features(n).special_atlas);
360                         AtlasPointer &pa_liquid1 =
361                                         *content_features(n).special_atlas;
362
363                         bool top_is_same_liquid = false;
364                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
365                         content_t c_flowing = content_features(n).liquid_alternative_flowing;
366                         content_t c_source = content_features(n).liquid_alternative_source;
367                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
368                                 top_is_same_liquid = true;
369                         
370                         u8 l = 0;
371                         // Use the light of the node on top if possible
372                         if(content_features(ntop).param_type == CPT_LIGHT)
373                                 l = decode_light(ntop.getLightBlend(data->m_daynight_ratio));
374                         // Otherwise use the light of this node (the liquid)
375                         else
376                                 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
377                         video::SColor c = MapBlock_LightColor(
378                                         content_features(n).vertex_alpha, l);
379                         
380                         // Neighbor liquid levels (key = relative position)
381                         // Includes current node
382                         core::map<v3s16, f32> neighbor_levels;
383                         core::map<v3s16, content_t> neighbor_contents;
384                         core::map<v3s16, u8> neighbor_flags;
385                         const u8 neighborflag_top_is_same_liquid = 0x01;
386                         v3s16 neighbor_dirs[9] = {
387                                 v3s16(0,0,0),
388                                 v3s16(0,0,1),
389                                 v3s16(0,0,-1),
390                                 v3s16(1,0,0),
391                                 v3s16(-1,0,0),
392                                 v3s16(1,0,1),
393                                 v3s16(-1,0,-1),
394                                 v3s16(1,0,-1),
395                                 v3s16(-1,0,1),
396                         };
397                         for(u32 i=0; i<9; i++)
398                         {
399                                 content_t content = CONTENT_AIR;
400                                 float level = -0.5 * BS;
401                                 u8 flags = 0;
402                                 // Check neighbor
403                                 v3s16 p2 = p + neighbor_dirs[i];
404                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
405                                 if(n2.getContent() != CONTENT_IGNORE)
406                                 {
407                                         content = n2.getContent();
408
409                                         if(n2.getContent() == c_source)
410                                                 level = (-0.5+node_liquid_level) * BS;
411                                         else if(n2.getContent() == c_flowing)
412                                                 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
413                                                                 + 0.5) / 8.0 * node_liquid_level) * BS;
414
415                                         // Check node above neighbor.
416                                         // NOTE: This doesn't get executed if neighbor
417                                         //       doesn't exist
418                                         p2.Y += 1;
419                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
420                                         if(n2.getContent() == c_source ||
421                                                         n2.getContent() == c_flowing)
422                                                 flags |= neighborflag_top_is_same_liquid;
423                                 }
424                                 
425                                 neighbor_levels.insert(neighbor_dirs[i], level);
426                                 neighbor_contents.insert(neighbor_dirs[i], content);
427                                 neighbor_flags.insert(neighbor_dirs[i], flags);
428                         }
429
430                         // Corner heights (average between four liquids)
431                         f32 corner_levels[4];
432                         
433                         v3s16 halfdirs[4] = {
434                                 v3s16(0,0,0),
435                                 v3s16(1,0,0),
436                                 v3s16(1,0,1),
437                                 v3s16(0,0,1),
438                         };
439                         for(u32 i=0; i<4; i++)
440                         {
441                                 v3s16 cornerdir = halfdirs[i];
442                                 float cornerlevel = 0;
443                                 u32 valid_count = 0;
444                                 u32 air_count = 0;
445                                 for(u32 j=0; j<4; j++)
446                                 {
447                                         v3s16 neighbordir = cornerdir - halfdirs[j];
448                                         content_t content = neighbor_contents[neighbordir];
449                                         // If top is liquid, draw starting from top of node
450                                         if(neighbor_flags[neighbordir] &
451                                                         neighborflag_top_is_same_liquid)
452                                         {
453                                                 cornerlevel = 0.5*BS;
454                                                 valid_count = 1;
455                                                 break;
456                                         }
457                                         // Source is always the same height
458                                         else if(content == c_source)
459                                         {
460                                                 cornerlevel = (-0.5+node_liquid_level)*BS;
461                                                 valid_count = 1;
462                                                 break;
463                                         }
464                                         // Flowing liquid has level information
465                                         else if(content == c_flowing)
466                                         {
467                                                 cornerlevel += neighbor_levels[neighbordir];
468                                                 valid_count++;
469                                         }
470                                         else if(content == CONTENT_AIR)
471                                         {
472                                                 air_count++;
473                                         }
474                                 }
475                                 if(air_count >= 2)
476                                         cornerlevel = -0.5*BS;
477                                 else if(valid_count > 0)
478                                         cornerlevel /= valid_count;
479                                 corner_levels[i] = cornerlevel;
480                         }
481
482                         /*
483                                 Generate sides
484                         */
485
486                         v3s16 side_dirs[4] = {
487                                 v3s16(1,0,0),
488                                 v3s16(-1,0,0),
489                                 v3s16(0,0,1),
490                                 v3s16(0,0,-1),
491                         };
492                         s16 side_corners[4][2] = {
493                                 {1, 2},
494                                 {3, 0},
495                                 {2, 3},
496                                 {0, 1},
497                         };
498                         for(u32 i=0; i<4; i++)
499                         {
500                                 v3s16 dir = side_dirs[i];
501
502                                 /*
503                                         If our topside is liquid and neighbor's topside
504                                         is liquid, don't draw side face
505                                 */
506                                 if(top_is_same_liquid &&
507                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
508                                         continue;
509
510                                 content_t neighbor_content = neighbor_contents[dir];
511                                 
512                                 // Don't draw face if neighbor is not air or liquid
513                                 if(neighbor_content != CONTENT_AIR
514                                                 && content_liquid(neighbor_content) == false)
515                                         continue;
516                                 
517                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
518                                                 || neighbor_content == c_flowing);
519                                 
520                                 // Don't draw any faces if neighbor same is liquid and top is
521                                 // same liquid
522                                 if(neighbor_is_same_liquid == true
523                                                 && top_is_same_liquid == false)
524                                         continue;
525                                 
526                                 video::S3DVertex vertices[4] =
527                                 {
528                                         /*video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,1),
529                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,1),
530                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
531                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
532                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
533                                                         pa_liquid1.x0(), pa_liquid1.y1()),
534                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
535                                                         pa_liquid1.x1(), pa_liquid1.y1()),
536                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
537                                                         pa_liquid1.x1(), pa_liquid1.y0()),
538                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
539                                                         pa_liquid1.x0(), pa_liquid1.y0()),
540                                 };
541                                 
542                                 /*
543                                         If our topside is liquid, set upper border of face
544                                         at upper border of node
545                                 */
546                                 if(top_is_same_liquid)
547                                 {
548                                         vertices[2].Pos.Y = 0.5*BS;
549                                         vertices[3].Pos.Y = 0.5*BS;
550                                 }
551                                 /*
552                                         Otherwise upper position of face is corner levels
553                                 */
554                                 else
555                                 {
556                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
557                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
558                                 }
559                                 
560                                 /*
561                                         If neighbor is liquid, lower border of face is corner
562                                         liquid levels
563                                 */
564                                 if(neighbor_is_same_liquid)
565                                 {
566                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
567                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
568                                 }
569                                 /*
570                                         If neighbor is not liquid, lower border of face is
571                                         lower border of node
572                                 */
573                                 else
574                                 {
575                                         vertices[0].Pos.Y = -0.5*BS;
576                                         vertices[1].Pos.Y = -0.5*BS;
577                                 }
578                                 
579                                 for(s32 j=0; j<4; j++)
580                                 {
581                                         if(dir == v3s16(0,0,1))
582                                                 vertices[j].Pos.rotateXZBy(0);
583                                         if(dir == v3s16(0,0,-1))
584                                                 vertices[j].Pos.rotateXZBy(180);
585                                         if(dir == v3s16(-1,0,0))
586                                                 vertices[j].Pos.rotateXZBy(90);
587                                         if(dir == v3s16(1,0,-0))
588                                                 vertices[j].Pos.rotateXZBy(-90);
589                                                 
590                                         // Do this to not cause glitches when two liquids are
591                                         // side-by-side
592                                         if(neighbor_is_same_liquid == false){
593                                                 vertices[j].Pos.X *= 0.98;
594                                                 vertices[j].Pos.Z *= 0.98;
595                                         }
596
597                                         vertices[j].Pos += intToFloat(p + blockpos_nodes, BS);
598                                 }
599
600                                 u16 indices[] = {0,1,2,2,3,0};
601                                 // Add to mesh collector
602                                 collector.append(liquid_material, vertices, 4, indices, 6);
603                         }
604                         
605                         /*
606                                 Generate top side, if appropriate
607                         */
608                         
609                         if(top_is_same_liquid == false)
610                         {
611                                 video::S3DVertex vertices[4] =
612                                 {
613                                         /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
614                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
615                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
616                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
617                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
618                                                         pa_liquid1.x0(), pa_liquid1.y1()),
619                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
620                                                         pa_liquid1.x1(), pa_liquid1.y1()),
621                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
622                                                         pa_liquid1.x1(), pa_liquid1.y0()),
623                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
624                                                         pa_liquid1.x0(), pa_liquid1.y0()),
625                                 };
626                                 
627                                 // This fixes a strange bug
628                                 s32 corner_resolve[4] = {3,2,1,0};
629
630                                 for(s32 i=0; i<4; i++)
631                                 {
632                                         //vertices[i].Pos.Y += liquid_level;
633                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
634                                         s32 j = corner_resolve[i];
635                                         vertices[i].Pos.Y += corner_levels[j];
636                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
637                                 }
638
639                                 u16 indices[] = {0,1,2,2,3,0};
640                                 // Add to mesh collector
641                                 collector.append(liquid_material, vertices, 4, indices, 6);
642                         }
643                 }
644                 /*
645                         Add water sources to mesh if using new style
646                 */
647                 else if(content_features(n).liquid_type == LIQUID_SOURCE
648                                 && new_style_water)
649                 {
650                         assert(content_features(n).special_material);
651                         video::SMaterial &liquid_material =
652                                         *content_features(n).special_material;
653                         assert(content_features(n).special_atlas);
654                         AtlasPointer &pa_liquid1 =
655                                         *content_features(n).special_atlas;
656
657                         bool top_is_air = false;
658                         MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
659                         if(n.getContent() == CONTENT_AIR)
660                                 top_is_air = true;
661                         
662                         if(top_is_air == false)
663                                 continue;
664
665                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
666                         video::SColor c = MapBlock_LightColor(
667                                         content_features(n).vertex_alpha, l);
668                         
669                         video::S3DVertex vertices[4] =
670                         {
671                                 /*video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c, 0,1),
672                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c, 1,1),
673                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c, 1,0),
674                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c, 0,0),*/
675                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
676                                                 pa_liquid1.x0(), pa_liquid1.y1()),
677                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
678                                                 pa_liquid1.x1(), pa_liquid1.y1()),
679                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
680                                                 pa_liquid1.x1(), pa_liquid1.y0()),
681                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
682                                                 pa_liquid1.x0(), pa_liquid1.y0()),
683                         };
684
685                         for(s32 i=0; i<4; i++)
686                         {
687                                 vertices[i].Pos.Y += (-0.5+node_liquid_level)*BS;
688                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
689                         }
690
691                         u16 indices[] = {0,1,2,2,3,0};
692                         // Add to mesh collector
693                         collector.append(liquid_material, vertices, 4, indices, 6);
694                 }
695                 /*
696                         Add leaves if using new style
697                 */
698                 else if(n.getContent() == CONTENT_LEAVES && new_style_leaves)
699                 {
700                         /*u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));*/
701                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
702                         video::SColor c = MapBlock_LightColor(255, l);
703
704                         for(u32 j=0; j<6; j++)
705                         {
706                                 video::S3DVertex vertices[4] =
707                                 {
708                                         /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
709                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
710                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
711                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
712                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
713                                                 pa_leaves1.x0(), pa_leaves1.y1()),
714                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
715                                                 pa_leaves1.x1(), pa_leaves1.y1()),
716                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
717                                                 pa_leaves1.x1(), pa_leaves1.y0()),
718                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
719                                                 pa_leaves1.x0(), pa_leaves1.y0()),
720                                 };
721
722                                 if(j == 0)
723                                 {
724                                         for(u16 i=0; i<4; i++)
725                                                 vertices[i].Pos.rotateXZBy(0);
726                                 }
727                                 else if(j == 1)
728                                 {
729                                         for(u16 i=0; i<4; i++)
730                                                 vertices[i].Pos.rotateXZBy(180);
731                                 }
732                                 else if(j == 2)
733                                 {
734                                         for(u16 i=0; i<4; i++)
735                                                 vertices[i].Pos.rotateXZBy(-90);
736                                 }
737                                 else if(j == 3)
738                                 {
739                                         for(u16 i=0; i<4; i++)
740                                                 vertices[i].Pos.rotateXZBy(90);
741                                 }
742                                 else if(j == 4)
743                                 {
744                                         for(u16 i=0; i<4; i++)
745                                                 vertices[i].Pos.rotateYZBy(-90);
746                                 }
747                                 else if(j == 5)
748                                 {
749                                         for(u16 i=0; i<4; i++)
750                                                 vertices[i].Pos.rotateYZBy(90);
751                                 }
752
753                                 for(u16 i=0; i<4; i++)
754                                 {
755                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
756                                 }
757
758                                 u16 indices[] = {0,1,2,2,3,0};
759                                 // Add to mesh collector
760                                 collector.append(material_leaves1, vertices, 4, indices, 6);
761                         }
762                 }
763                 /*
764                         Add glass
765                 */
766                 else if(n.getContent() == CONTENT_GLASS)
767                 {
768                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
769                         video::SColor c = MapBlock_LightColor(255, l);
770
771                         for(u32 j=0; j<6; j++)
772                         {
773                                 video::S3DVertex vertices[4] =
774                                 {
775                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
776                                                 pa_glass.x0(), pa_glass.y1()),
777                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
778                                                 pa_glass.x1(), pa_glass.y1()),
779                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
780                                                 pa_glass.x1(), pa_glass.y0()),
781                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
782                                                 pa_glass.x0(), pa_glass.y0()),
783                                 };
784
785                                 if(j == 0)
786                                 {
787                                         for(u16 i=0; i<4; i++)
788                                                 vertices[i].Pos.rotateXZBy(0);
789                                 }
790                                 else if(j == 1)
791                                 {
792                                         for(u16 i=0; i<4; i++)
793                                                 vertices[i].Pos.rotateXZBy(180);
794                                 }
795                                 else if(j == 2)
796                                 {
797                                         for(u16 i=0; i<4; i++)
798                                                 vertices[i].Pos.rotateXZBy(-90);
799                                 }
800                                 else if(j == 3)
801                                 {
802                                         for(u16 i=0; i<4; i++)
803                                                 vertices[i].Pos.rotateXZBy(90);
804                                 }
805                                 else if(j == 4)
806                                 {
807                                         for(u16 i=0; i<4; i++)
808                                                 vertices[i].Pos.rotateYZBy(-90);
809                                 }
810                                 else if(j == 5)
811                                 {
812                                         for(u16 i=0; i<4; i++)
813                                                 vertices[i].Pos.rotateYZBy(90);
814                                 }
815
816                                 for(u16 i=0; i<4; i++)
817                                 {
818                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
819                                 }
820
821                                 u16 indices[] = {0,1,2,2,3,0};
822                                 // Add to mesh collector
823                                 collector.append(material_glass, vertices, 4, indices, 6);
824                         }
825                 }
826                 /*
827                         Add fence
828                 */
829                 else if(n.getContent() == CONTENT_FENCE)
830                 {
831                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
832                         video::SColor c = MapBlock_LightColor(255, l);
833
834                         const f32 post_rad=(f32)BS/10;
835                         const f32 bar_rad=(f32)BS/20;
836                         const f32 bar_len=(f32)(BS/2)-post_rad;
837
838                         // The post - always present
839                         v3f pos = intToFloat(p+blockpos_nodes, BS);
840                         f32 postuv[24]={
841                                         0.4,0.4,0.6,0.6,
842                                         0.35,0,0.65,1,
843                                         0.35,0,0.65,1,
844                                         0.35,0,0.65,1,
845                                         0.35,0,0.65,1,
846                                         0.4,0.4,0.6,0.6};
847                         makeCuboid(material_wood, &collector,
848                                 &pa_wood, c, pos,
849                                 post_rad,BS/2,post_rad, postuv);
850
851                         // Now a section of fence, +X, if there's a post there
852                         v3s16 p2 = p;
853                         p2.X++;
854                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
855                         if(n2.getContent() == CONTENT_FENCE)
856                         {
857                                 pos = intToFloat(p+blockpos_nodes, BS);
858                                 pos.X += BS/2;
859                                 pos.Y += BS/4;
860                                 f32 xrailuv[24]={
861                                         0,0.4,1,0.6,
862                                         0,0.4,1,0.6,
863                                         0,0.4,1,0.6,
864                                         0,0.4,1,0.6,
865                                         0,0.4,1,0.6,
866                                         0,0.4,1,0.6};
867                                 makeCuboid(material_wood, &collector,
868                                         &pa_wood, c, pos,
869                                         bar_len,bar_rad,bar_rad, xrailuv);
870
871                                 pos.Y -= BS/2;
872                                 makeCuboid(material_wood, &collector,
873                                         &pa_wood, c, pos,
874                                         bar_len,bar_rad,bar_rad, xrailuv);
875                         }
876
877                         // Now a section of fence, +Z, if there's a post there
878                         p2 = p;
879                         p2.Z++;
880                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
881                         if(n2.getContent() == CONTENT_FENCE)
882                         {
883                                 pos = intToFloat(p+blockpos_nodes, BS);
884                                 pos.Z += BS/2;
885                                 pos.Y += BS/4;
886                                 f32 zrailuv[24]={
887                                         0,0.4,1,0.6,
888                                         0,0.4,1,0.6,
889                                         0,0.4,1,0.6,
890                                         0,0.4,1,0.6,
891                                         0,0.4,1,0.6,
892                                         0,0.4,1,0.6};
893                                 makeCuboid(material_wood, &collector,
894                                         &pa_wood, c, pos,
895                                         bar_rad,bar_rad,bar_len, zrailuv);
896                                 pos.Y -= BS/2;
897                                 makeCuboid(material_wood, &collector,
898                                         &pa_wood, c, pos,
899                                         bar_rad,bar_rad,bar_len, zrailuv);
900
901                         }
902
903                 }
904 #if 1
905                 /*
906                         Add stones with minerals if stone is invisible
907                 */
908                 else if(n.getContent() == CONTENT_STONE && invisible_stone && n.getMineral() != MINERAL_NONE)
909                 {
910                         for(u32 j=0; j<6; j++)
911                         {
912                                 // NOTE: Hopefully g_6dirs[j] is the right direction...
913                                 v3s16 dir = g_6dirs[j];
914                                 /*u8 l = 0;
915                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + dir);
916                                 if(content_features(n2).param_type == CPT_LIGHT)
917                                         l = decode_light(n2.getLightBlend(data->m_daynight_ratio));
918                                 else
919                                         l = 255;*/
920                                 u8 l = 255;
921                                 video::SColor c = MapBlock_LightColor(255, l);
922                                 
923                                 // Get the right texture
924                                 TileSpec ts = n.getTile(dir);
925                                 AtlasPointer ap = ts.texture;
926                                 material_general.setTexture(0, ap.atlas);
927
928                                 video::S3DVertex vertices[4] =
929                                 {
930                                         /*video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c, 0,1),
931                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c, 1,1),
932                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c, 1,0),
933                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c, 0,0),*/
934                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
935                                                 ap.x0(), ap.y1()),
936                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
937                                                 ap.x1(), ap.y1()),
938                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
939                                                 ap.x1(), ap.y0()),
940                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
941                                                 ap.x0(), ap.y0()),
942                                 };
943
944                                 if(j == 0)
945                                 {
946                                         for(u16 i=0; i<4; i++)
947                                                 vertices[i].Pos.rotateXZBy(0);
948                                 }
949                                 else if(j == 1)
950                                 {
951                                         for(u16 i=0; i<4; i++)
952                                                 vertices[i].Pos.rotateXZBy(180);
953                                 }
954                                 else if(j == 2)
955                                 {
956                                         for(u16 i=0; i<4; i++)
957                                                 vertices[i].Pos.rotateXZBy(-90);
958                                 }
959                                 else if(j == 3)
960                                 {
961                                         for(u16 i=0; i<4; i++)
962                                                 vertices[i].Pos.rotateXZBy(90);
963                                 }
964                                 else if(j == 4)
965
966                                 for(u16 i=0; i<4; i++)
967                                 {
968                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
969                                 }
970
971                                 u16 indices[] = {0,1,2,2,3,0};
972                                 // Add to mesh collector
973                                 collector.append(material_general, vertices, 4, indices, 6);
974                         }
975                 }
976 #endif
977                 else if(n.getContent() == CONTENT_PAPYRUS)
978                 {
979                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
980                         video::SColor c = MapBlock_LightColor(255, l);
981
982                         for(u32 j=0; j<4; j++)
983                         {
984                                 video::S3DVertex vertices[4] =
985                                 {
986                                         video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
987                                                 pa_papyrus.x0(), pa_papyrus.y1()),
988                                         video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
989                                                 pa_papyrus.x1(), pa_papyrus.y1()),
990                                         video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
991                                                 pa_papyrus.x1(), pa_papyrus.y0()),
992                                         video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
993                                                 pa_papyrus.x0(), pa_papyrus.y0()),
994                                 };
995
996                                 if(j == 0)
997                                 {
998                                         for(u16 i=0; i<4; i++)
999                                                 vertices[i].Pos.rotateXZBy(45);
1000                                 }
1001                                 else if(j == 1)
1002                                 {
1003                                         for(u16 i=0; i<4; i++)
1004                                                 vertices[i].Pos.rotateXZBy(-45);
1005                                 }
1006                                 else if(j == 2)
1007                                 {
1008                                         for(u16 i=0; i<4; i++)
1009                                                 vertices[i].Pos.rotateXZBy(135);
1010                                 }
1011                                 else if(j == 3)
1012                                 {
1013                                         for(u16 i=0; i<4; i++)
1014                                                 vertices[i].Pos.rotateXZBy(-135);
1015                                 }
1016
1017                                 for(u16 i=0; i<4; i++)
1018                                 {
1019                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1020                                 }
1021
1022                                 u16 indices[] = {0,1,2,2,3,0};
1023                                 // Add to mesh collector
1024                                 collector.append(material_papyrus, vertices, 4, indices, 6);
1025                         }
1026                 }
1027                 else if(n.getContent() == CONTENT_JUNGLEGRASS)
1028                 {
1029                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1030                         video::SColor c = MapBlock_LightColor(255, l);
1031
1032                         for(u32 j=0; j<4; j++)
1033                         {
1034                                 video::S3DVertex vertices[4] =
1035                                 {
1036                                         video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1037                                                 pa_papyrus.x0(), pa_papyrus.y1()),
1038                                         video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1039                                                 pa_papyrus.x1(), pa_papyrus.y1()),
1040                                         video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1041                                                 pa_papyrus.x1(), pa_papyrus.y0()),
1042                                         video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1043                                                 pa_papyrus.x0(), pa_papyrus.y0()),
1044                                 };
1045
1046                                 if(j == 0)
1047                                 {
1048                                         for(u16 i=0; i<4; i++)
1049                                                 vertices[i].Pos.rotateXZBy(45);
1050                                 }
1051                                 else if(j == 1)
1052                                 {
1053                                         for(u16 i=0; i<4; i++)
1054                                                 vertices[i].Pos.rotateXZBy(-45);
1055                                 }
1056                                 else if(j == 2)
1057                                 {
1058                                         for(u16 i=0; i<4; i++)
1059                                                 vertices[i].Pos.rotateXZBy(135);
1060                                 }
1061                                 else if(j == 3)
1062                                 {
1063                                         for(u16 i=0; i<4; i++)
1064                                                 vertices[i].Pos.rotateXZBy(-135);
1065                                 }
1066
1067                                 for(u16 i=0; i<4; i++)
1068                                 {
1069                                         vertices[i].Pos *= 1.3;
1070                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1071                                 }
1072
1073                                 u16 indices[] = {0,1,2,2,3,0};
1074                                 // Add to mesh collector
1075                                 collector.append(material_junglegrass, vertices, 4, indices, 6);
1076                         }
1077                 }
1078                 else if(n.getContent() == CONTENT_RAIL)
1079                 {
1080                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1081                         video::SColor c = MapBlock_LightColor(255, l);
1082
1083                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
1084                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
1085
1086                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1087                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1088                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1089                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1090
1091                         if(n_minus_x.getContent() == CONTENT_RAIL)
1092                                 is_rail_x[0] = true;
1093                         if(n_plus_x.getContent() == CONTENT_RAIL)
1094                                 is_rail_x[1] = true;
1095                         if(n_minus_z.getContent() == CONTENT_RAIL)
1096                                 is_rail_z[0] = true;
1097                         if(n_plus_z.getContent() == CONTENT_RAIL)
1098                                 is_rail_z[1] = true;
1099
1100                         float d = (float)BS/16;
1101                         video::S3DVertex vertices[4] =
1102                         {
1103                                 video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1104                                         0, 1),
1105                                 video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1106                                         1, 1),
1107                                 video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
1108                                         1, 0),
1109                                 video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
1110                                         0, 0),
1111                         };
1112
1113                         video::SMaterial material_rail;
1114                         material_rail.setFlag(video::EMF_LIGHTING, false);
1115                         material_rail.setFlag(video::EMF_BACK_FACE_CULLING, false);
1116                         material_rail.setFlag(video::EMF_BILINEAR_FILTER, false);
1117                         material_rail.setFlag(video::EMF_FOG_ENABLE, true);
1118                         material_rail.MaterialType
1119                                         = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1120
1121                         int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
1122
1123                         // Assign textures
1124                         if(adjacencies < 2)
1125                                 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1126                         else if(adjacencies == 2)
1127                         {
1128                                 if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
1129                                         material_rail.setTexture(0, g_texturesource->getTextureRaw("rail.png"));
1130                                 else
1131                                         material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_curved.png"));
1132                         }
1133                         else if(adjacencies == 3)
1134                                 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_t_junction.png"));
1135                         else if(adjacencies == 4)
1136                                 material_rail.setTexture(0, g_texturesource->getTextureRaw("rail_crossing.png"));
1137
1138                         // Rotate textures
1139                         int angle = 0;
1140
1141                         if(adjacencies == 1)
1142                         {
1143                                 if(is_rail_x[0] || is_rail_x[1])
1144                                         angle = 90;
1145                         }
1146                         else if(adjacencies == 2)
1147                         {
1148                                 if(is_rail_x[0] && is_rail_x[1])
1149                                         angle = 90;
1150                                 else if(is_rail_x[0] && is_rail_z[0])
1151                                         angle = 270;
1152                                 else if(is_rail_x[0] && is_rail_z[1])
1153                                         angle = 180;
1154                                 else if(is_rail_x[1] && is_rail_z[1])
1155                                         angle = 90;
1156                         }
1157                         else if(adjacencies == 3)
1158                         {
1159                                 if(!is_rail_x[0])
1160                                         angle=0;
1161                                 if(!is_rail_x[1])
1162                                         angle=180;
1163                                 if(!is_rail_z[0])
1164                                         angle=90;
1165                                 if(!is_rail_z[1])
1166                                         angle=270;
1167                         }
1168
1169                         if(angle != 0) {
1170                                 for(u16 i=0; i<4; i++)
1171                                         vertices[i].Pos.rotateXZBy(angle);
1172                         }
1173
1174                         for(s32 i=0; i<4; i++)
1175                         {
1176                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1177                         }
1178
1179                         u16 indices[] = {0,1,2,2,3,0};
1180                         collector.append(material_rail, vertices, 4, indices, 6);
1181                 }
1182                 else if (n.getContent() == CONTENT_LADDER) {
1183                         u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio));
1184                         video::SColor c(255,l,l,l);
1185
1186                         float d = (float)BS/16;
1187
1188                         // Assume wall is at X+
1189                         video::S3DVertex vertices[4] =
1190                         {
1191                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c, 0,1),
1192                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c, 1,1),
1193                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c, 1,0),
1194                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c, 0,0),
1195                         };
1196
1197                         v3s16 dir = unpackDir(n.param2);
1198
1199                         for(s32 i=0; i<4; i++)
1200                         {
1201                                 if(dir == v3s16(1,0,0))
1202                                         vertices[i].Pos.rotateXZBy(0);
1203                                 if(dir == v3s16(-1,0,0))
1204                                         vertices[i].Pos.rotateXZBy(180);
1205                                 if(dir == v3s16(0,0,1))
1206                                         vertices[i].Pos.rotateXZBy(90);
1207                                 if(dir == v3s16(0,0,-1))
1208                                         vertices[i].Pos.rotateXZBy(-90);
1209                                 if(dir == v3s16(0,-1,0))
1210                                         vertices[i].Pos.rotateXYBy(-90);
1211                                 if(dir == v3s16(0,1,0))
1212                                         vertices[i].Pos.rotateXYBy(90);
1213
1214                                 vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1215                         }
1216
1217                         video::SMaterial material_ladder;
1218                         material_ladder.setFlag(video::EMF_LIGHTING, false);
1219                         material_ladder.setFlag(video::EMF_BACK_FACE_CULLING, false);
1220                         material_ladder.setFlag(video::EMF_BILINEAR_FILTER, false);
1221                         material_ladder.setFlag(video::EMF_FOG_ENABLE, true);
1222                         material_ladder.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1223                         material_ladder.setTexture(0, g_texturesource->getTextureRaw("ladder.png"));
1224
1225                         u16 indices[] = {0,1,2,2,3,0};
1226                         // Add to mesh collector
1227                         collector.append(material_ladder, vertices, 4, indices, 6);
1228                 }
1229                 else if(n.getContent() == CONTENT_APPLE)
1230                 {
1231                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1232                         video::SColor c = MapBlock_LightColor(255, l);
1233
1234                         for(u32 j=0; j<4; j++)
1235                         {
1236                                 video::S3DVertex vertices[4] =
1237                                 {
1238                                         video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1239                                                 pa_apple.x0(), pa_apple.y1()),
1240                                         video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1241                                                 pa_apple.x1(), pa_apple.y1()),
1242                                         video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
1243                                                 pa_apple.x1(), pa_apple.y0()),
1244                                         video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
1245                                                 pa_apple.x0(), pa_apple.y0()),
1246                                 };
1247
1248                                 if(j == 0)
1249                                 {
1250                                         for(u16 i=0; i<4; i++)
1251                                                 vertices[i].Pos.rotateXZBy(45);
1252                                 }
1253                                 else if(j == 1)
1254                                 {
1255                                         for(u16 i=0; i<4; i++)
1256                                                 vertices[i].Pos.rotateXZBy(-45);
1257                                 }
1258                                 else if(j == 2)
1259                                 {
1260                                         for(u16 i=0; i<4; i++)
1261                                                 vertices[i].Pos.rotateXZBy(135);
1262                                 }
1263                                 else if(j == 3)
1264                                 {
1265                                         for(u16 i=0; i<4; i++)
1266                                                 vertices[i].Pos.rotateXZBy(-135);
1267                                 }
1268
1269                                 for(u16 i=0; i<4; i++)
1270                                 {
1271                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1272                                 }
1273
1274                                 u16 indices[] = {0,1,2,2,3,0};
1275                                 // Add to mesh collector
1276                                 collector.append(material_apple, vertices, 4, indices, 6);
1277                         }
1278                 }
1279                 else if(n.getContent() == CONTENT_SAPLING) {
1280                         u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
1281                         video::SColor c = MapBlock_LightColor(255, l);
1282
1283                         for(u32 j=0; j<4; j++)
1284                         {
1285                                 video::S3DVertex vertices[4] =
1286                                 {
1287                                         video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
1288                                                 pa_sapling.x0(), pa_sapling.y1()),
1289                                         video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
1290                                                 pa_sapling.x1(), pa_sapling.y1()),
1291                                         video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
1292                                                 pa_sapling.x1(), pa_sapling.y0()),
1293                                         video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
1294                                                 pa_sapling.x0(), pa_sapling.y0()),
1295                                 };
1296
1297                                 if(j == 0)
1298                                 {
1299                                         for(u16 i=0; i<4; i++)
1300                                                 vertices[i].Pos.rotateXZBy(45);
1301                                 }
1302                                 else if(j == 1)
1303                                 {
1304                                         for(u16 i=0; i<4; i++)
1305                                                 vertices[i].Pos.rotateXZBy(-45);
1306                                 }
1307                                 else if(j == 2)
1308                                 {
1309                                         for(u16 i=0; i<4; i++)
1310                                                 vertices[i].Pos.rotateXZBy(135);
1311                                 }
1312                                 else if(j == 3)
1313                                 {
1314                                         for(u16 i=0; i<4; i++)
1315                                                 vertices[i].Pos.rotateXZBy(-135);
1316                                 }
1317
1318                                 for(u16 i=0; i<4; i++)
1319                                 {
1320                                         vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
1321                                 }
1322
1323                                 u16 indices[] = {0,1,2,2,3,0};
1324                                 // Add to mesh collector
1325                                 collector.append(material_sapling, vertices, 4, indices, 6);
1326                         }
1327                 }
1328         }
1329 }
1330 #endif
1331