From f21291211caaaa22ee5673adf46615dd46a36285 Mon Sep 17 00:00:00 2001
From: Jeija <norrepli@gmail.com>
Date: Sat, 3 Mar 2012 19:16:48 +0100
Subject: [PATCH] Improve raillike drawtype

---
 src/content_mapblock.cpp | 113 +++++++++++++++++++++++++++++++--------
 1 file changed, 91 insertions(+), 22 deletions(-)

diff --git a/src/content_mapblock.cpp b/src/content_mapblock.cpp
index dc1e1daed..402c4e8b4 100644
--- a/src/content_mapblock.cpp
+++ b/src/content_mapblock.cpp
@@ -883,22 +883,69 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
 			bool is_rail_x [] = { false, false };  /* x-1, x+1 */
 			bool is_rail_z [] = { false, false };  /* z-1, z+1 */
 
+			bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
+			bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
+			bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
+			bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
+
 			MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
 			MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
 			MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
 			MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
+			MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
+			MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
+			MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
+			MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
+			MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
+			MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
+			MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
+			MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
 			
 			content_t thiscontent = n.getContent();
 			if(n_minus_x.getContent() == thiscontent)
 				is_rail_x[0] = true;
+			if (n_minus_x_minus_y.getContent() == thiscontent)
+				is_rail_x_minus_y[0] = true;
+			if(n_minus_x_plus_y.getContent() == thiscontent)
+				is_rail_x_plus_y[0] = true;
+
 			if(n_plus_x.getContent() == thiscontent)
 				is_rail_x[1] = true;
+			if (n_plus_x_minus_y.getContent() == thiscontent)
+				is_rail_x_minus_y[1] = true;
+			if(n_plus_x_plus_y.getContent() == thiscontent)
+				is_rail_x_plus_y[1] = true;
+
 			if(n_minus_z.getContent() == thiscontent)
 				is_rail_z[0] = true;
+			if (n_minus_z_minus_y.getContent() == thiscontent)
+				is_rail_z_minus_y[0] = true;
+			if(n_minus_z_plus_y.getContent() == thiscontent)
+				is_rail_z_plus_y[0] = true;
+
 			if(n_plus_z.getContent() == thiscontent)
 				is_rail_z[1] = true;
+			if (n_plus_z_minus_y.getContent() == thiscontent)
+				is_rail_z_minus_y[1] = true;
+			if(n_plus_z_plus_y.getContent() == thiscontent)
+				is_rail_z_plus_y[1] = true;
+
+
+			bool is_rail_x_all[] = {false, false};
+			bool is_rail_z_all[] = {false, false};
+			is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
+			is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
+			is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
+			is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
+
+			bool is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);//is really straight, rails on both sides
+			int adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
 
-			int adjacencies = is_rail_x[0] + is_rail_x[1] + is_rail_z[0] + is_rail_z[1];
+			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]) //is straight because sloped
+			{
+				adjacencies = 5; //5 means sloped
+				is_straight = true;
+			}
 
 			// Assign textures
 			AtlasPointer ap = f.tiles[0].texture; // straight
@@ -906,7 +953,7 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
 				ap = f.tiles[0].texture; // straight
 			else if(adjacencies == 2)
 			{
-				if((is_rail_x[0] && is_rail_x[1]) || (is_rail_z[0] && is_rail_z[1]))
+				if(is_straight)
 					ap = f.tiles[0].texture; // straight
 				else
 					ap = f.tiles[1].texture; // curved
@@ -928,49 +975,71 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
 			u8 l = decode_light(n.getLightBlend(data->m_daynight_ratio, nodedef));
 			video::SColor c = MapBlock_LightColor(255, l);
 
-			float d = (float)BS/16;
+			float d = (float)BS/64;
+			
+			char g=-1;
+			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])
+				g=1; //Object is at a slope
+
 			video::S3DVertex vertices[4] =
 			{
-				video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
-						ap.x0(), ap.y1()),
-				video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
-						ap.x1(), ap.y1()),
-				video::S3DVertex(BS/2,-BS/2+d,BS/2, 0,0,0, c,
-						ap.x1(), ap.y0()),
-				video::S3DVertex(-BS/2,-BS/2+d,BS/2, 0,0,0, c,
-						ap.x0(), ap.y0()),
+					video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
+							ap.x0(), ap.y1()),
+					video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
+							ap.x1(), ap.y1()),
+					video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
+							ap.x1(), ap.y0()),
+					video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
+							ap.x0(), ap.y0()),
 			};
 
+
 			// Rotate textures
 			int angle = 0;
 
 			if(adjacencies == 1)
 			{
-				if(is_rail_x[0] || is_rail_x[1])
+				if(is_rail_x_all[0] or is_rail_x_all[1])
 					angle = 90;
 			}
-			else if(adjacencies == 2)
+			if(adjacencies == 2)
 			{
-				if(is_rail_x[0] && is_rail_x[1])
+				if(is_rail_x_all[0] && is_rail_x_all[1])
+				{
 					angle = 90;
-				else if(is_rail_x[0] && is_rail_z[0])
+				}
+				if(is_rail_z_all[0] && is_rail_z_all[1])
+				{
+					if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
+				}
+				else if(is_rail_x_all[0] && is_rail_z_all[0])
 					angle = 270;
-				else if(is_rail_x[0] && is_rail_z[1])
+				else if(is_rail_x_all[0] && is_rail_z_all[1])
 					angle = 180;
-				else if(is_rail_x[1] && is_rail_z[1])
+				else if(is_rail_x_all[1] && is_rail_z_all[1])
 					angle = 90;
 			}
-			else if(adjacencies == 3)
+			if(adjacencies == 3)
 			{
-				if(!is_rail_x[0])
+				if(!is_rail_x_all[0])
 					angle=0;
-				if(!is_rail_x[1])
+				if(!is_rail_x_all[1])
 					angle=180;
-				if(!is_rail_z[0])
+				if(!is_rail_z_all[0])
 					angle=90;
-				if(!is_rail_z[1])
+				if(!is_rail_z_all[1])
 					angle=270;
 			}
+			//adjacencies 4: Crossing
+			if(adjacencies == 5) //sloped
+			{
+				if(is_rail_z_plus_y[0])
+					angle = 180;
+				if(is_rail_x_plus_y[0])
+					angle = 90;
+				if(is_rail_x_plus_y[1])
+					angle = -90;
+			}
 
 			if(angle != 0) {
 				for(u16 i=0; i<4; i++)
-- 
GitLab