Translation to Portuguese of Brazil for Minetest
[oweals/minetest.git] / src / content_mapblock.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 "content_mapblock.h"
21
22 #include "main.h" // For g_settings
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "tile.h"
27 #include "gamedef.h"
28 #include "util/numeric.h"
29 #include "util/directiontables.h"
30
31 // Create a cuboid.
32 //  collector - the MeshCollector for the resulting polygons
33 //  box       - the position and size of the box
34 //  tiles     - the tiles (materials) to use (for all 6 faces)
35 //  tilecount - number of entries in tiles, 1<=tilecount<=6
36 //  c         - vertex colour - used for all
37 //  txc       - texture coordinates - this is a list of texture coordinates
38 //              for the opposite corners of each face - therefore, there
39 //              should be (2+2)*6=24 values in the list. Alternatively, pass
40 //              NULL to use the entire texture for each face. The order of
41 //              the faces in the list is up-down-right-left-back-front
42 //              (compatible with ContentFeatures). If you specified 0,0,1,1
43 //              for each face, that would be the same as passing NULL.
44 void makeCuboid(MeshCollector *collector, const aabb3f &box,
45         TileSpec *tiles, int tilecount,
46         video::SColor &c, const f32* txc)
47 {
48         assert(tilecount >= 1 && tilecount <= 6);
49
50         v3f min = box.MinEdge;
51         v3f max = box.MaxEdge;
52  
53  
54  
55         if(txc == NULL)
56         {
57                 static const f32 txc_default[24] = {
58                         0,0,1,1,
59                         0,0,1,1,
60                         0,0,1,1,
61                         0,0,1,1,
62                         0,0,1,1,
63                         0,0,1,1
64                 };
65                 txc = txc_default;
66         }
67
68         video::S3DVertex vertices[24] =
69         {
70                 // up
71                 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
72                 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
73                 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
74                 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
75                 // down
76                 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
77                 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
78                 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
79                 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
80                 // right
81                 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
82                 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
83                 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
84                 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
85                 // left
86                 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
87                 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
88                 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
89                 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
90                 // back
91                 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
92                 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
93                 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
94                 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
95                 // front
96                 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
97                 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
98                 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
99                 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
100         };
101
102         v2f t;
103         for(int i = 0; i < tilecount; i++)
104                                 {
105                                 switch (tiles[i].rotation)
106                                 {
107                                 case 0:
108                                         break;
109                                 case 1: //R90
110                                         for (int x = 0; x < 4; x++)
111                                                 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
112                                         break;
113                                 case 2: //R180
114                                         for (int x = 0; x < 4; x++)
115                                                 vertices[i*4+x].TCoords.rotateBy(180,irr::core::vector2df(0, 0));
116                                         break;
117                                 case 3: //R270
118                                         for (int x = 0; x < 4; x++)
119                                                 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
120                                         break;
121                                 case 4: //FXR90
122                                         for (int x = 0; x < 4; x++)
123                                                 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
124
125                                         tiles[i].texture.pos.Y += tiles[i].texture.size.Y;
126                                         tiles[i].texture.size.Y *= -1;
127                                         break;
128                                 case 5: //FXR270
129                                         for (int x = 0; x < 4; x++)
130                                                 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
131                                         t=vertices[i*4].TCoords;
132                                         tiles[i].texture.pos.Y += tiles[i].texture.size.Y;
133                                         tiles[i].texture.size.Y *= -1;
134                                         break;
135                                 case 6: //FYR90
136                                         for (int x = 0; x < 4; x++)
137                                                 vertices[i*4+x].TCoords.rotateBy(90,irr::core::vector2df(0, 0));
138                                         tiles[i].texture.pos.X += tiles[i].texture.size.X;
139                                         tiles[i].texture.size.X *= -1;
140                                         break;
141                                 case 7: //FYR270
142                                         for (int x = 0; x < 4; x++)
143                                                 vertices[i*4+x].TCoords.rotateBy(270,irr::core::vector2df(0, 0));
144                                         tiles[i].texture.pos.X += tiles[i].texture.size.X;
145                                         tiles[i].texture.size.X *= -1;
146                                         break;
147                                 case 8: //FX
148                                         tiles[i].texture.pos.Y += tiles[i].texture.size.Y;
149                                         tiles[i].texture.size.Y *= -1;
150                                         break;
151                                 case 9: //FY
152                                         tiles[i].texture.pos.X += tiles[i].texture.size.X;
153                                         tiles[i].texture.size.X *= -1;
154                                         break;
155                                 default:
156                                         break;
157                                 }
158                         }
159                 for(s32 j=0; j<24; j++)
160         {
161                 int tileindex = MYMIN(j/4, tilecount-1);
162                 vertices[j].TCoords *= tiles[tileindex].texture.size;
163                 vertices[j].TCoords += tiles[tileindex].texture.pos;
164         }
165         u16 indices[] = {0,1,2,2,3,0};
166         // Add to mesh collector
167         for(s32 j=0; j<24; j+=4)
168         {
169                 int tileindex = MYMIN(j/4, tilecount-1);
170                 collector->append(tiles[tileindex],
171                                 vertices+j, 4, indices, 6);
172         }
173 }
174
175 void mapblock_mesh_generate_special(MeshMakeData *data,
176                 MeshCollector &collector)
177 {
178         INodeDefManager *nodedef = data->m_gamedef->ndef();
179
180         // 0ms
181         //TimeTaker timer("mapblock_mesh_generate_special()");
182
183         /*
184                 Some settings
185         */
186         bool new_style_water = g_settings->getBool("new_style_water");
187         
188         float node_liquid_level = 1.0;
189         if(new_style_water)
190                 node_liquid_level = 0.85;
191         
192         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
193
194         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
195         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
196         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
197         {
198                 v3s16 p(x,y,z);
199
200                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
201                 const ContentFeatures &f = nodedef->get(n);
202
203                 // Only solidness=0 stuff is drawn here
204                 if(f.solidness != 0)
205                         continue;
206                 
207                 switch(f.drawtype){
208                 default:
209                         infostream<<"Got "<<f.drawtype<<std::endl;
210                         assert(0);
211                         break;
212                 case NDT_AIRLIKE:
213                         break;
214                 case NDT_LIQUID:
215                 {
216                         /*
217                                 Add water sources to mesh if using new style
218                         */
219                         TileSpec tile_liquid = f.special_tiles[0];
220                         TileSpec tile_liquid_bfculled = getNodeTile(n, p, v3s16(0,0,0), data);
221                         AtlasPointer &pa_liquid = tile_liquid.texture;
222
223                         bool top_is_same_liquid = false;
224                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
225                         content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
226                         content_t c_source = nodedef->getId(f.liquid_alternative_source);
227                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
228                                 top_is_same_liquid = true;
229
230                         u16 l = getInteriorLight(n, 0, data);
231                         video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
232
233                         /*
234                                 Generate sides
235                          */
236                         v3s16 side_dirs[4] = {
237                                 v3s16(1,0,0),
238                                 v3s16(-1,0,0),
239                                 v3s16(0,0,1),
240                                 v3s16(0,0,-1),
241                         };
242                         for(u32 i=0; i<4; i++)
243                         {
244                                 v3s16 dir = side_dirs[i];
245
246                                 MapNode neighbor = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir);
247                                 content_t neighbor_content = neighbor.getContent();
248                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
249                                 MapNode n_top = data->m_vmanip.getNodeNoEx(blockpos_nodes + p + dir+ v3s16(0,1,0));
250                                 content_t n_top_c = n_top.getContent();
251
252                                 if(neighbor_content == CONTENT_IGNORE)
253                                         continue;
254
255                                 /*
256                                         If our topside is liquid and neighbor's topside
257                                         is liquid, don't draw side face
258                                 */
259                                 if(top_is_same_liquid && (n_top_c == c_flowing ||
260                                                 n_top_c == c_source || n_top_c == CONTENT_IGNORE))
261                                         continue;
262
263                                 // Don't draw face if neighbor is blocking the view
264                                 if(n_feat.solidness == 2)
265                                         continue;
266
267                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
268                                                 || neighbor_content == c_flowing);
269
270                                 // Don't draw any faces if neighbor same is liquid and top is
271                                 // same liquid
272                                 if(neighbor_is_same_liquid && !top_is_same_liquid)
273                                         continue;
274
275                                 // Use backface culled material if neighbor doesn't have a
276                                 // solidness of 0
277                                 const TileSpec *current_tile = &tile_liquid;
278                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
279                                         current_tile = &tile_liquid_bfculled;
280
281                                 video::S3DVertex vertices[4] =
282                                 {
283                                         video::S3DVertex(-BS/2,0,BS/2,0,0,0, c,
284                                                         pa_liquid.x0(), pa_liquid.y1()),
285                                         video::S3DVertex(BS/2,0,BS/2,0,0,0, c,
286                                                         pa_liquid.x1(), pa_liquid.y1()),
287                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
288                                                         pa_liquid.x1(), pa_liquid.y0()),
289                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
290                                                         pa_liquid.x0(), pa_liquid.y0()),
291                                 };
292
293                                 /*
294                                         If our topside is liquid, set upper border of face
295                                         at upper border of node
296                                 */
297                                 if(top_is_same_liquid)
298                                 {
299                                         vertices[2].Pos.Y = 0.5*BS;
300                                         vertices[3].Pos.Y = 0.5*BS;
301                                 }
302                                 /*
303                                         Otherwise upper position of face is liquid level
304                                 */
305                                 else
306                                 {
307                                         vertices[2].Pos.Y = (node_liquid_level-0.5)*BS;
308                                         vertices[3].Pos.Y = (node_liquid_level-0.5)*BS;
309                                 }
310                                 /*
311                                         If neighbor is liquid, lower border of face is liquid level
312                                 */
313                                 if(neighbor_is_same_liquid)
314                                 {
315                                         vertices[0].Pos.Y = (node_liquid_level-0.5)*BS;
316                                         vertices[1].Pos.Y = (node_liquid_level-0.5)*BS;
317                                 }
318                                 /*
319                                         If neighbor is not liquid, lower border of face is
320                                         lower border of node
321                                 */
322                                 else
323                                 {
324                                         vertices[0].Pos.Y = -0.5*BS;
325                                         vertices[1].Pos.Y = -0.5*BS;
326                                 }
327
328                                 for(s32 j=0; j<4; j++)
329                                 {
330                                         if(dir == v3s16(0,0,1))
331                                                 vertices[j].Pos.rotateXZBy(0);
332                                         if(dir == v3s16(0,0,-1))
333                                                 vertices[j].Pos.rotateXZBy(180);
334                                         if(dir == v3s16(-1,0,0))
335                                                 vertices[j].Pos.rotateXZBy(90);
336                                         if(dir == v3s16(1,0,-0))
337                                                 vertices[j].Pos.rotateXZBy(-90);
338
339                                         // Do this to not cause glitches when two liquids are
340                                         // side-by-side
341                                         /*if(neighbor_is_same_liquid == false){
342                                                 vertices[j].Pos.X *= 0.98;
343                                                 vertices[j].Pos.Z *= 0.98;
344                                         }*/
345
346                                         vertices[j].Pos += intToFloat(p, BS);
347                                 }
348
349                                 u16 indices[] = {0,1,2,2,3,0};
350                                 // Add to mesh collector
351                                 collector.append(*current_tile, vertices, 4, indices, 6);
352                         }
353
354                         /*
355                                 Generate top
356                          */
357                         if(top_is_same_liquid)
358                                 continue;
359                         
360                         video::S3DVertex vertices[4] =
361                         {
362                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
363                                                 pa_liquid.x0(), pa_liquid.y1()),
364                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
365                                                 pa_liquid.x1(), pa_liquid.y1()),
366                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
367                                                 pa_liquid.x1(), pa_liquid.y0()),
368                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
369                                                 pa_liquid.x0(), pa_liquid.y0()),
370                         };
371
372                         v3f offset(p.X*BS, p.Y*BS + (-0.5+node_liquid_level)*BS, p.Z*BS);
373                         for(s32 i=0; i<4; i++)
374                         {
375                                 vertices[i].Pos += offset;
376                         }
377
378                         u16 indices[] = {0,1,2,2,3,0};
379                         // Add to mesh collector
380                         collector.append(tile_liquid, vertices, 4, indices, 6);
381                 break;}
382                 case NDT_FLOWINGLIQUID:
383                 {
384                         /*
385                                 Add flowing liquid to mesh
386                         */
387                         TileSpec tile_liquid = f.special_tiles[0];
388                         TileSpec tile_liquid_bfculled = f.special_tiles[1];
389                         AtlasPointer &pa_liquid = tile_liquid.texture;
390
391                         bool top_is_same_liquid = false;
392                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
393                         content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
394                         content_t c_source = nodedef->getId(f.liquid_alternative_source);
395                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
396                                 top_is_same_liquid = true;
397                         
398                         u16 l = 0;
399                         // If this liquid emits light and doesn't contain light, draw
400                         // it at what it emits, for an increased effect
401                         u8 light_source = nodedef->get(n).light_source;
402                         if(light_source != 0){
403                                 //l = decode_light(undiminish_light(light_source));
404                                 l = decode_light(light_source);
405                                 l = l | (l<<8);
406                         }
407                         // Use the light of the node on top if possible
408                         else if(nodedef->get(ntop).param_type == CPT_LIGHT)
409                                 l = getInteriorLight(ntop, 0, data);
410                         // Otherwise use the light of this node (the liquid)
411                         else
412                                 l = getInteriorLight(n, 0, data);
413                         video::SColor c = MapBlock_LightColor(f.alpha, l, decode_light(f.light_source));
414                         
415                         // Neighbor liquid levels (key = relative position)
416                         // Includes current node
417                         std::map<v3s16, f32> neighbor_levels;
418                         std::map<v3s16, content_t> neighbor_contents;
419                         std::map<v3s16, u8> neighbor_flags;
420                         const u8 neighborflag_top_is_same_liquid = 0x01;
421                         v3s16 neighbor_dirs[9] = {
422                                 v3s16(0,0,0),
423                                 v3s16(0,0,1),
424                                 v3s16(0,0,-1),
425                                 v3s16(1,0,0),
426                                 v3s16(-1,0,0),
427                                 v3s16(1,0,1),
428                                 v3s16(-1,0,-1),
429                                 v3s16(1,0,-1),
430                                 v3s16(-1,0,1),
431                         };
432                         for(u32 i=0; i<9; i++)
433                         {
434                                 content_t content = CONTENT_AIR;
435                                 float level = -0.5 * BS;
436                                 u8 flags = 0;
437                                 // Check neighbor
438                                 v3s16 p2 = p + neighbor_dirs[i];
439                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
440                                 if(n2.getContent() != CONTENT_IGNORE)
441                                 {
442                                         content = n2.getContent();
443
444                                         if(n2.getContent() == c_source)
445                                                 level = (-0.5+node_liquid_level) * BS;
446                                         else if(n2.getContent() == c_flowing)
447                                                 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
448                                                                 + 0.5) / (float)LIQUID_LEVEL_SOURCE * node_liquid_level) * BS;
449
450                                         // Check node above neighbor.
451                                         // NOTE: This doesn't get executed if neighbor
452                                         //       doesn't exist
453                                         p2.Y += 1;
454                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
455                                         if(n2.getContent() == c_source ||
456                                                         n2.getContent() == c_flowing)
457                                                 flags |= neighborflag_top_is_same_liquid;
458                                 }
459                                 
460                                 neighbor_levels[neighbor_dirs[i]] = level;
461                                 neighbor_contents[neighbor_dirs[i]] = content;
462                                 neighbor_flags[neighbor_dirs[i]] = flags;
463                         }
464
465                         // Corner heights (average between four liquids)
466                         f32 corner_levels[4];
467                         
468                         v3s16 halfdirs[4] = {
469                                 v3s16(0,0,0),
470                                 v3s16(1,0,0),
471                                 v3s16(1,0,1),
472                                 v3s16(0,0,1),
473                         };
474                         for(u32 i=0; i<4; i++)
475                         {
476                                 v3s16 cornerdir = halfdirs[i];
477                                 float cornerlevel = 0;
478                                 u32 valid_count = 0;
479                                 u32 air_count = 0;
480                                 for(u32 j=0; j<4; j++)
481                                 {
482                                         v3s16 neighbordir = cornerdir - halfdirs[j];
483                                         content_t content = neighbor_contents[neighbordir];
484                                         // If top is liquid, draw starting from top of node
485                                         if(neighbor_flags[neighbordir] &
486                                                         neighborflag_top_is_same_liquid)
487                                         {
488                                                 cornerlevel = 0.5*BS;
489                                                 valid_count = 1;
490                                                 break;
491                                         }
492                                         // Source is always the same height
493                                         else if(content == c_source)
494                                         {
495                                                 cornerlevel = (-0.5+node_liquid_level)*BS;
496                                                 valid_count = 1;
497                                                 break;
498                                         }
499                                         // Flowing liquid has level information
500                                         else if(content == c_flowing)
501                                         {
502                                                 cornerlevel += neighbor_levels[neighbordir];
503                                                 valid_count++;
504                                         }
505                                         else if(content == CONTENT_AIR)
506                                         {
507                                                 air_count++;
508                                         }
509                                 }
510                                 if(air_count >= 2)
511                                         cornerlevel = -0.5*BS+0.2;
512                                 else if(valid_count > 0)
513                                         cornerlevel /= valid_count;
514                                 corner_levels[i] = cornerlevel;
515                         }
516
517                         /*
518                                 Generate sides
519                         */
520
521                         v3s16 side_dirs[4] = {
522                                 v3s16(1,0,0),
523                                 v3s16(-1,0,0),
524                                 v3s16(0,0,1),
525                                 v3s16(0,0,-1),
526                         };
527                         s16 side_corners[4][2] = {
528                                 {1, 2},
529                                 {3, 0},
530                                 {2, 3},
531                                 {0, 1},
532                         };
533                         for(u32 i=0; i<4; i++)
534                         {
535                                 v3s16 dir = side_dirs[i];
536
537                                 /*
538                                         If our topside is liquid and neighbor's topside
539                                         is liquid, don't draw side face
540                                 */
541                                 if(top_is_same_liquid &&
542                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
543                                         continue;
544
545                                 content_t neighbor_content = neighbor_contents[dir];
546                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
547                                 
548                                 // Don't draw face if neighbor is blocking the view
549                                 if(n_feat.solidness == 2)
550                                         continue;
551                                 
552                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
553                                                 || neighbor_content == c_flowing);
554                                 
555                                 // Don't draw any faces if neighbor same is liquid and top is
556                                 // same liquid
557                                 if(neighbor_is_same_liquid == true
558                                                 && top_is_same_liquid == false)
559                                         continue;
560
561                                 // Use backface culled material if neighbor doesn't have a
562                                 // solidness of 0
563                                 const TileSpec *current_tile = &tile_liquid;
564                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
565                                         current_tile = &tile_liquid_bfculled;
566                                 
567                                 video::S3DVertex vertices[4] =
568                                 {
569                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
570                                                         pa_liquid.x0(), pa_liquid.y1()),
571                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
572                                                         pa_liquid.x1(), pa_liquid.y1()),
573                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
574                                                         pa_liquid.x1(), pa_liquid.y0()),
575                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
576                                                         pa_liquid.x0(), pa_liquid.y0()),
577                                 };
578                                 
579                                 /*
580                                         If our topside is liquid, set upper border of face
581                                         at upper border of node
582                                 */
583                                 if(top_is_same_liquid)
584                                 {
585                                         vertices[2].Pos.Y = 0.5*BS;
586                                         vertices[3].Pos.Y = 0.5*BS;
587                                 }
588                                 /*
589                                         Otherwise upper position of face is corner levels
590                                 */
591                                 else
592                                 {
593                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
594                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
595                                 }
596                                 
597                                 /*
598                                         If neighbor is liquid, lower border of face is corner
599                                         liquid levels
600                                 */
601                                 if(neighbor_is_same_liquid)
602                                 {
603                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
604                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
605                                 }
606                                 /*
607                                         If neighbor is not liquid, lower border of face is
608                                         lower border of node
609                                 */
610                                 else
611                                 {
612                                         vertices[0].Pos.Y = -0.5*BS;
613                                         vertices[1].Pos.Y = -0.5*BS;
614                                 }
615                                 
616                                 for(s32 j=0; j<4; j++)
617                                 {
618                                         if(dir == v3s16(0,0,1))
619                                                 vertices[j].Pos.rotateXZBy(0);
620                                         if(dir == v3s16(0,0,-1))
621                                                 vertices[j].Pos.rotateXZBy(180);
622                                         if(dir == v3s16(-1,0,0))
623                                                 vertices[j].Pos.rotateXZBy(90);
624                                         if(dir == v3s16(1,0,-0))
625                                                 vertices[j].Pos.rotateXZBy(-90);
626                                                 
627                                         // Do this to not cause glitches when two liquids are
628                                         // side-by-side
629                                         /*if(neighbor_is_same_liquid == false){
630                                                 vertices[j].Pos.X *= 0.98;
631                                                 vertices[j].Pos.Z *= 0.98;
632                                         }*/
633
634                                         vertices[j].Pos += intToFloat(p, BS);
635                                 }
636
637                                 u16 indices[] = {0,1,2,2,3,0};
638                                 // Add to mesh collector
639                                 collector.append(*current_tile, vertices, 4, indices, 6);
640                         }
641                         
642                         /*
643                                 Generate top side, if appropriate
644                         */
645                         
646                         if(top_is_same_liquid == false)
647                         {
648                                 video::S3DVertex vertices[4] =
649                                 {
650                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
651                                                         pa_liquid.x0(), pa_liquid.y1()),
652                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
653                                                         pa_liquid.x1(), pa_liquid.y1()),
654                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
655                                                         pa_liquid.x1(), pa_liquid.y0()),
656                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
657                                                         pa_liquid.x0(), pa_liquid.y0()),
658                                 };
659                                 
660                                 // To get backface culling right, the vertices need to go
661                                 // clockwise around the front of the face. And we happened to
662                                 // calculate corner levels in exact reverse order.
663                                 s32 corner_resolve[4] = {3,2,1,0};
664
665                                 for(s32 i=0; i<4; i++)
666                                 {
667                                         //vertices[i].Pos.Y += liquid_level;
668                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
669                                         s32 j = corner_resolve[i];
670                                         vertices[i].Pos.Y += corner_levels[j];
671                                         vertices[i].Pos += intToFloat(p, BS);
672                                 }
673                                 
674                                 // Default downwards-flowing texture animation goes from 
675                                 // -Z towards +Z, thus the direction is +Z.
676                                 // Rotate texture to make animation go in flow direction
677                                 // Positive if liquid moves towards +Z
678                                 int dz = (corner_levels[side_corners[3][0]] +
679                                                 corner_levels[side_corners[3][1]]) -
680                                                 (corner_levels[side_corners[2][0]] +
681                                                 corner_levels[side_corners[2][1]]);
682                                 // Positive if liquid moves towards +X
683                                 int dx = (corner_levels[side_corners[1][0]] +
684                                                 corner_levels[side_corners[1][1]]) -
685                                                 (corner_levels[side_corners[0][0]] +
686                                                 corner_levels[side_corners[0][1]]);
687                                 // -X
688                                 if(-dx >= abs(dz))
689                                 {
690                                         v2f t = vertices[0].TCoords;
691                                         vertices[0].TCoords = vertices[1].TCoords;
692                                         vertices[1].TCoords = vertices[2].TCoords;
693                                         vertices[2].TCoords = vertices[3].TCoords;
694                                         vertices[3].TCoords = t;
695                                 }
696                                 // +X
697                                 if(dx >= abs(dz))
698                                 {
699                                         v2f t = vertices[0].TCoords;
700                                         vertices[0].TCoords = vertices[3].TCoords;
701                                         vertices[3].TCoords = vertices[2].TCoords;
702                                         vertices[2].TCoords = vertices[1].TCoords;
703                                         vertices[1].TCoords = t;
704                                 }
705                                 // -Z
706                                 if(-dz >= abs(dx))
707                                 {
708                                         v2f t = vertices[0].TCoords;
709                                         vertices[0].TCoords = vertices[3].TCoords;
710                                         vertices[3].TCoords = vertices[2].TCoords;
711                                         vertices[2].TCoords = vertices[1].TCoords;
712                                         vertices[1].TCoords = t;
713                                         t = vertices[0].TCoords;
714                                         vertices[0].TCoords = vertices[3].TCoords;
715                                         vertices[3].TCoords = vertices[2].TCoords;
716                                         vertices[2].TCoords = vertices[1].TCoords;
717                                         vertices[1].TCoords = t;
718                                 }
719
720                                 u16 indices[] = {0,1,2,2,3,0};
721                                 // Add to mesh collector
722                                 collector.append(tile_liquid, vertices, 4, indices, 6);
723                         }
724                 break;}
725                 case NDT_GLASSLIKE:
726                 {
727                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
728                         AtlasPointer ap = tile.texture;
729
730                         u16 l = getInteriorLight(n, 1, data);
731                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
732
733                         for(u32 j=0; j<6; j++)
734                         {
735                                 // Check this neighbor
736                                 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
737                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
738                                 // Don't make face if neighbor is of same type
739                                 if(n2.getContent() == n.getContent())
740                                         continue;
741
742                                 // The face at Z+
743                                 video::S3DVertex vertices[4] =
744                                 {
745                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
746                                                 ap.x0(), ap.y1()),
747                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
748                                                 ap.x1(), ap.y1()),
749                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
750                                                 ap.x1(), ap.y0()),
751                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
752                                                 ap.x0(), ap.y0()),
753                                 };
754                                 
755                                 // Rotations in the g_6dirs format
756                                 if(j == 0) // Z+
757                                         for(u16 i=0; i<4; i++)
758                                                 vertices[i].Pos.rotateXZBy(0);
759                                 else if(j == 1) // Y+
760                                         for(u16 i=0; i<4; i++)
761                                                 vertices[i].Pos.rotateYZBy(-90);
762                                 else if(j == 2) // X+
763                                         for(u16 i=0; i<4; i++)
764                                                 vertices[i].Pos.rotateXZBy(-90);
765                                 else if(j == 3) // Z-
766                                         for(u16 i=0; i<4; i++)
767                                                 vertices[i].Pos.rotateXZBy(180);
768                                 else if(j == 4) // Y-
769                                         for(u16 i=0; i<4; i++)
770                                                 vertices[i].Pos.rotateYZBy(90);
771                                 else if(j == 5) // X-
772                                         for(u16 i=0; i<4; i++)
773                                                 vertices[i].Pos.rotateXZBy(90);
774
775                                 for(u16 i=0; i<4; i++){
776                                         vertices[i].Pos += intToFloat(p, BS);
777                                 }
778
779                                 u16 indices[] = {0,1,2,2,3,0};
780                                 // Add to mesh collector
781                                 collector.append(tile, vertices, 4, indices, 6);
782                         }
783                 break;}
784                 case NDT_ALLFACES:
785                 {
786                         TileSpec tile_leaves = getNodeTile(n, p,
787                                         v3s16(0,0,0), data);
788                         AtlasPointer pa_leaves = tile_leaves.texture;
789
790                         u16 l = getInteriorLight(n, 1, data);
791                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
792
793                         v3f pos = intToFloat(p, BS);
794                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
795                         box.MinEdge += pos;
796                         box.MaxEdge += pos;
797                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
798                 break;}
799                 case NDT_ALLFACES_OPTIONAL:
800                         // This is always pre-converted to something else
801                         assert(0);
802                         break;
803                 case NDT_TORCHLIKE:
804                 {
805                         v3s16 dir = n.getWallMountedDir(nodedef);
806                         
807                         u8 tileindex = 0;
808                         if(dir == v3s16(0,-1,0)){
809                                 tileindex = 0; // floor
810                         } else if(dir == v3s16(0,1,0)){
811                                 tileindex = 1; // ceiling
812                         // For backwards compatibility
813                         } else if(dir == v3s16(0,0,0)){
814                                 tileindex = 0; // floor
815                         } else {
816                                 tileindex = 2; // side
817                         }
818
819                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
820                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
821                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
822
823                         AtlasPointer ap = tile.texture;
824
825                         u16 l = getInteriorLight(n, 1, data);
826                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
827
828                         // Wall at X+ of node
829                         video::S3DVertex vertices[4] =
830                         {
831                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
832                                                 ap.x0(), ap.y1()),
833                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
834                                                 ap.x1(), ap.y1()),
835                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
836                                                 ap.x1(), ap.y0()),
837                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
838                                                 ap.x0(), ap.y0()),
839                         };
840
841                         for(s32 i=0; i<4; i++)
842                         {
843                                 if(dir == v3s16(1,0,0))
844                                         vertices[i].Pos.rotateXZBy(0);
845                                 if(dir == v3s16(-1,0,0))
846                                         vertices[i].Pos.rotateXZBy(180);
847                                 if(dir == v3s16(0,0,1))
848                                         vertices[i].Pos.rotateXZBy(90);
849                                 if(dir == v3s16(0,0,-1))
850                                         vertices[i].Pos.rotateXZBy(-90);
851                                 if(dir == v3s16(0,-1,0))
852                                         vertices[i].Pos.rotateXZBy(45);
853                                 if(dir == v3s16(0,1,0))
854                                         vertices[i].Pos.rotateXZBy(-45);
855
856                                 vertices[i].Pos += intToFloat(p, BS);
857                         }
858
859                         u16 indices[] = {0,1,2,2,3,0};
860                         // Add to mesh collector
861                         collector.append(tile, vertices, 4, indices, 6);
862                 break;}
863                 case NDT_SIGNLIKE:
864                 {
865                         TileSpec tile = getNodeTileN(n, p, 0, data);
866                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
867                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
868                         AtlasPointer ap = tile.texture;
869
870                         u16 l = getInteriorLight(n, 0, data);
871                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
872                                 
873                         float d = (float)BS/16;
874                         // Wall at X+ of node
875                         video::S3DVertex vertices[4] =
876                         {
877                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
878                                                 ap.x0(), ap.y0()),
879                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
880                                                 ap.x1(), ap.y0()),
881                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
882                                                 ap.x1(), ap.y1()),
883                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
884                                                 ap.x0(), ap.y1()),
885                         };
886
887                         v3s16 dir = n.getWallMountedDir(nodedef);
888
889                         for(s32 i=0; i<4; i++)
890                         {
891                                 if(dir == v3s16(1,0,0))
892                                         vertices[i].Pos.rotateXZBy(0);
893                                 if(dir == v3s16(-1,0,0))
894                                         vertices[i].Pos.rotateXZBy(180);
895                                 if(dir == v3s16(0,0,1))
896                                         vertices[i].Pos.rotateXZBy(90);
897                                 if(dir == v3s16(0,0,-1))
898                                         vertices[i].Pos.rotateXZBy(-90);
899                                 if(dir == v3s16(0,-1,0))
900                                         vertices[i].Pos.rotateXYBy(-90);
901                                 if(dir == v3s16(0,1,0))
902                                         vertices[i].Pos.rotateXYBy(90);
903
904                                 vertices[i].Pos += intToFloat(p, BS);
905                         }
906
907                         u16 indices[] = {0,1,2,2,3,0};
908                         // Add to mesh collector
909                         collector.append(tile, vertices, 4, indices, 6);
910                 break;}
911                 case NDT_PLANTLIKE:
912                 {
913                         TileSpec tile = getNodeTileN(n, p, 0, data);
914                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
915                         AtlasPointer ap = tile.texture;
916                         
917                         u16 l = getInteriorLight(n, 1, data);
918                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
919
920                         for(u32 j=0; j<2; j++)
921                         {
922                                 video::S3DVertex vertices[4] =
923                                 {
924                                         video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
925                                                 ap.x0(), ap.y1()),
926                                         video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
927                                                 ap.x1(), ap.y1()),
928                                         video::S3DVertex( BS/2*f.visual_scale,
929                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
930                                                 ap.x1(), ap.y0()),
931                                         video::S3DVertex(-BS/2*f.visual_scale,
932                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
933                                                 ap.x0(), ap.y0()),
934                                 };
935
936                                 if(j == 0)
937                                 {
938                                         for(u16 i=0; i<4; i++)
939                                                 vertices[i].Pos.rotateXZBy(45);
940                                 }
941                                 else if(j == 1)
942                                 {
943                                         for(u16 i=0; i<4; i++)
944                                                 vertices[i].Pos.rotateXZBy(-45);
945                                 }
946
947                                 for(u16 i=0; i<4; i++)
948                                 {
949                                         vertices[i].Pos *= f.visual_scale;
950                                         vertices[i].Pos += intToFloat(p, BS);
951                                 }
952
953                                 u16 indices[] = {0,1,2,2,3,0};
954                                 // Add to mesh collector
955                                 collector.append(tile, vertices, 4, indices, 6);
956                         }
957                 break;}
958                 case NDT_FENCELIKE:
959                 {
960                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
961                         TileSpec tile_nocrack = tile;
962                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
963                         
964                         // A hack to put wood the right way around in the posts
965                         ITextureSource *tsrc = data->m_gamedef->tsrc();
966                         TileSpec tile_rot = tile;
967                         tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
968                                         tile.texture.id) + "^[transformR90");
969                                         
970                         u16 l = getInteriorLight(n, 1, data);
971                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
972
973                         const f32 post_rad=(f32)BS/8;
974                         const f32 bar_rad=(f32)BS/16;
975                         const f32 bar_len=(f32)(BS/2)-post_rad;
976
977                         v3f pos = intToFloat(p, BS);
978
979                         // The post - always present
980                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
981                         post.MinEdge += pos;
982                         post.MaxEdge += pos;
983                         f32 postuv[24]={
984                                         6/16.,6/16.,10/16.,10/16.,
985                                         6/16.,6/16.,10/16.,10/16.,
986                                         0/16.,0,4/16.,1,
987                                         4/16.,0,8/16.,1,
988                                         8/16.,0,12/16.,1,
989                                         12/16.,0,16/16.,1};
990                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
991
992                         // Now a section of fence, +X, if there's a post there
993                         v3s16 p2 = p;
994                         p2.X++;
995                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
996                         const ContentFeatures *f2 = &nodedef->get(n2);
997                         if(f2->drawtype == NDT_FENCELIKE)
998                         {
999                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
1000                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
1001                                 bar.MinEdge += pos;
1002                                 bar.MaxEdge += pos;
1003                                 f32 xrailuv[24]={
1004                                         0/16.,2/16.,16/16.,4/16.,
1005                                         0/16.,4/16.,16/16.,6/16.,
1006                                         6/16.,6/16.,8/16.,8/16.,
1007                                         10/16.,10/16.,12/16.,12/16.,
1008                                         0/16.,8/16.,16/16.,10/16.,
1009                                         0/16.,14/16.,16/16.,16/16.};
1010                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1011                                                 c, xrailuv);
1012                                 bar.MinEdge.Y -= BS/2;
1013                                 bar.MaxEdge.Y -= BS/2;
1014                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1015                                                 c, xrailuv);
1016                         }
1017
1018                         // Now a section of fence, +Z, if there's a post there
1019                         p2 = p;
1020                         p2.Z++;
1021                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
1022                         f2 = &nodedef->get(n2);
1023                         if(f2->drawtype == NDT_FENCELIKE)
1024                         {
1025                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
1026                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
1027                                 bar.MinEdge += pos;
1028                                 bar.MaxEdge += pos;
1029                                 f32 zrailuv[24]={
1030                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
1031                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
1032                                         0/16.,9/16.,16/16.,11/16.,
1033                                         0/16.,6/16.,16/16.,8/16.,
1034                                         6/16.,6/16.,8/16.,8/16.,
1035                                         10/16.,10/16.,12/16.,12/16.};
1036                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1037                                                 c, zrailuv);
1038                                 bar.MinEdge.Y -= BS/2;
1039                                 bar.MaxEdge.Y -= BS/2;
1040                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
1041                                                 c, zrailuv);
1042                         }
1043                 break;}
1044                 case NDT_RAILLIKE:
1045                 {
1046                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
1047                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
1048
1049                         bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
1050                         bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
1051                         bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
1052                         bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
1053
1054                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1055                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1056                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1057                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1058                         MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1059                         MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1060                         MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1061                         MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1062                         MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1063                         MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1064                         MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1065                         MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1066                         
1067                         content_t thiscontent = n.getContent();
1068                         if(n_minus_x.getContent() == thiscontent)
1069                                 is_rail_x[0] = true;
1070                         if (n_minus_x_minus_y.getContent() == thiscontent)
1071                                 is_rail_x_minus_y[0] = true;
1072                         if(n_minus_x_plus_y.getContent() == thiscontent)
1073                                 is_rail_x_plus_y[0] = true;
1074
1075                         if(n_plus_x.getContent() == thiscontent)
1076                                 is_rail_x[1] = true;
1077                         if (n_plus_x_minus_y.getContent() == thiscontent)
1078                                 is_rail_x_minus_y[1] = true;
1079                         if(n_plus_x_plus_y.getContent() == thiscontent)
1080                                 is_rail_x_plus_y[1] = true;
1081
1082                         if(n_minus_z.getContent() == thiscontent)
1083                                 is_rail_z[0] = true;
1084                         if (n_minus_z_minus_y.getContent() == thiscontent)
1085                                 is_rail_z_minus_y[0] = true;
1086                         if(n_minus_z_plus_y.getContent() == thiscontent)
1087                                 is_rail_z_plus_y[0] = true;
1088
1089                         if(n_plus_z.getContent() == thiscontent)
1090                                 is_rail_z[1] = true;
1091                         if (n_plus_z_minus_y.getContent() == thiscontent)
1092                                 is_rail_z_minus_y[1] = true;
1093                         if(n_plus_z_plus_y.getContent() == thiscontent)
1094                                 is_rail_z_plus_y[1] = true;
1095
1096                         bool is_rail_x_all[] = {false, false};
1097                         bool is_rail_z_all[] = {false, false};
1098                         is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1099                         is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1100                         is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1101                         is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1102
1103                         // reasonable default, flat straight unrotated rail
1104                         bool is_straight = true;
1105                         int adjacencies = 0;
1106                         int angle = 0;
1107                         u8 tileindex = 0;
1108
1109                         // check for sloped rail
1110                         if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
1111                         {
1112                                 adjacencies = 5; //5 means sloped
1113                                 is_straight = true; // sloped is always straight
1114                         }
1115                         else
1116                         {
1117                                 // is really straight, rails on both sides
1118                                 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
1119                                 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
1120                         }
1121
1122                         switch (adjacencies) {
1123                         case 1:
1124                                 if(is_rail_x_all[0] || is_rail_x_all[1])
1125                                         angle = 90;
1126                                 break;
1127                         case 2:
1128                                 if(!is_straight)
1129                                         tileindex = 1; // curved
1130                                 if(is_rail_x_all[0] && is_rail_x_all[1])
1131                                         angle = 90;
1132                                 if(is_rail_z_all[0] && is_rail_z_all[1]){
1133                                         if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
1134                                 }
1135                                 else if(is_rail_x_all[0] && is_rail_z_all[0])
1136                                         angle = 270;
1137                                 else if(is_rail_x_all[0] && is_rail_z_all[1])
1138                                         angle = 180;
1139                                 else if(is_rail_x_all[1] && is_rail_z_all[1])
1140                                         angle = 90;
1141                                 break;
1142                         case 3:
1143                                 // here is where the potential to 'switch' a junction is, but not implemented at present
1144                                 tileindex = 2; // t-junction
1145                                 if(!is_rail_x_all[1])
1146                                         angle=180;
1147                                 if(!is_rail_z_all[0])
1148                                         angle=90;
1149                                 if(!is_rail_z_all[1])
1150                                         angle=270;
1151                                 break;
1152                         case 4:
1153                                 tileindex = 3; // crossing
1154                                 break;
1155                         case 5: //sloped
1156                                 if(is_rail_z_plus_y[0])
1157                                         angle = 180;
1158                                 if(is_rail_x_plus_y[0])
1159                                         angle = 90;
1160                                 if(is_rail_x_plus_y[1])
1161                                         angle = -90;
1162                                 break;
1163                         default:
1164                                 break;
1165                         }
1166
1167                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
1168                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
1169                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
1170
1171                         AtlasPointer ap = tile.texture;
1172                         
1173                         u16 l = getInteriorLight(n, 0, data);
1174                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1175
1176                         float d = (float)BS/64;
1177                         
1178                         char g=-1;
1179                         if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
1180                                 g=1; //Object is at a slope
1181
1182                         video::S3DVertex vertices[4] =
1183                         {
1184                                         video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1185                                                         ap.x0(), ap.y1()),
1186                                         video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1187                                                         ap.x1(), ap.y1()),
1188                                         video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1189                                                         ap.x1(), ap.y0()),
1190                                         video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1191                                                         ap.x0(), ap.y0()),
1192                         };
1193
1194                         for(s32 i=0; i<4; i++)
1195                         {
1196                                 if(angle != 0)
1197                                         vertices[i].Pos.rotateXZBy(angle);
1198                                 vertices[i].Pos += intToFloat(p, BS);
1199                         }
1200
1201                         u16 indices[] = {0,1,2,2,3,0};
1202                         collector.append(tile, vertices, 4, indices, 6);
1203                 break;}
1204                 case NDT_NODEBOX:
1205                 {
1206                         static const v3s16 tile_dirs[6] = {
1207                                 v3s16(0, 1, 0),
1208                                 v3s16(0, -1, 0),
1209                                 v3s16(1, 0, 0),
1210                                 v3s16(-1, 0, 0),
1211                                 v3s16(0, 0, 1),
1212                                 v3s16(0, 0, -1)
1213                         };
1214                         TileSpec tiles[6];
1215                         
1216                         u16 l = getInteriorLight(n, 0, data);
1217                         video::SColor c = MapBlock_LightColor(255, l, decode_light(f.light_source));
1218
1219                         v3f pos = intToFloat(p, BS);
1220
1221                         std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1222                         for(std::vector<aabb3f>::iterator
1223                                         i = boxes.begin();
1224                                         i != boxes.end(); i++)
1225                         {
1226                         for(int j = 0; j < 6; j++)
1227                                 {
1228                                 // Handles facedir rotation for textures
1229                                 tiles[j] = getNodeTile(n, p, tile_dirs[j], data);
1230                                 }
1231                                 aabb3f box = *i;
1232                                 box.MinEdge += pos;
1233                                 box.MaxEdge += pos;
1234                                 
1235                                 f32 temp;
1236                                 if (box.MinEdge.X > box.MaxEdge.X)
1237                                 {
1238                                         temp=box.MinEdge.X;
1239                                         box.MinEdge.X=box.MaxEdge.X;
1240                                         box.MaxEdge.X=temp;
1241                                 }
1242                                 if (box.MinEdge.Y > box.MaxEdge.Y)
1243                                 {
1244                                         temp=box.MinEdge.Y;
1245                                         box.MinEdge.Y=box.MaxEdge.Y;
1246                                         box.MaxEdge.Y=temp;
1247                                 }
1248                                 if (box.MinEdge.Z > box.MaxEdge.Z)
1249                                 {
1250                                         temp=box.MinEdge.Z;
1251                                         box.MinEdge.Z=box.MaxEdge.Z;
1252                                         box.MaxEdge.Z=temp;
1253                                 }
1254
1255                                 //
1256                                 // Compute texture coords
1257                                 f32 tx1 = (box.MinEdge.X/BS)+0.5;
1258                                 f32 ty1 = (box.MinEdge.Y/BS)+0.5;
1259                                 f32 tz1 = (box.MinEdge.Z/BS)+0.5;
1260                                 f32 tx2 = (box.MaxEdge.X/BS)+0.5;
1261                                 f32 ty2 = (box.MaxEdge.Y/BS)+0.5;
1262                                 f32 tz2 = (box.MaxEdge.Z/BS)+0.5;
1263                                 f32 txc[24] = {
1264                                         // up
1265                                         tx1, 1-tz2, tx2, 1-tz1,
1266                                         // down
1267                                         tx1, tz1, tx2, tz2,
1268                                         // right
1269                                         tz1, 1-ty2, tz2, 1-ty1,
1270                                         // left
1271                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1272                                         // back
1273                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1274                                         // front
1275                                         tx1, 1-ty2, tx2, 1-ty1,
1276                                 };
1277                                 makeCuboid(&collector, box, tiles, 6, c, txc);
1278                         }
1279                 break;}
1280                 }
1281         }
1282 }
1283