Skip to content

Commit 709f4a5

Browse files
SmallJokerZeno-
authored andcommittedMar 21, 2015
Connect rails with connect_to_raillike and shorten the codes
1 parent 03e0dd3 commit 709f4a5

File tree

1 file changed

+49
-107
lines changed

1 file changed

+49
-107
lines changed
 

‎src/content_mapblock.cpp

+49-107
Original file line numberDiff line numberDiff line change
@@ -1398,97 +1398,42 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
13981398
break;}
13991399
case NDT_RAILLIKE:
14001400
{
1401-
bool is_rail_x [] = { false, false }; /* x-1, x+1 */
1402-
bool is_rail_z [] = { false, false }; /* z-1, z+1 */
1403-
1404-
bool is_rail_z_minus_y [] = { false, false }; /* z-1, z+1; y-1 */
1405-
bool is_rail_x_minus_y [] = { false, false }; /* x-1, z+1; y-1 */
1406-
bool is_rail_z_plus_y [] = { false, false }; /* z-1, z+1; y+1 */
1407-
bool is_rail_x_plus_y [] = { false, false }; /* x-1, x+1; y+1 */
1408-
1409-
MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
1410-
MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
1411-
MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
1412-
MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
1413-
MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
1414-
MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
1415-
MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
1416-
MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
1417-
MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
1418-
MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
1419-
MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
1420-
MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
1401+
bool is_rail_x[6]; /* (-1,-1,0) X (1,-1,0) (-1,0,0) X (1,0,0) (-1,1,0) X (1,1,0) */
1402+
bool is_rail_z[6];
14211403

14221404
content_t thiscontent = n.getContent();
14231405
std::string groupname = "connect_to_raillike"; // name of the group that enables connecting to raillike nodes of different kind
14241406
int self_group = ((ItemGroupList) nodedef->get(n).groups)[groupname];
14251407

1426-
if ((nodedef->get(n_minus_x).drawtype == NDT_RAILLIKE
1427-
&& ((ItemGroupList) nodedef->get(n_minus_x).groups)[groupname] != self_group)
1428-
|| n_minus_x.getContent() == thiscontent)
1429-
is_rail_x[0] = true;
1430-
1431-
if ((nodedef->get(n_minus_x_minus_y).drawtype == NDT_RAILLIKE
1432-
&& ((ItemGroupList) nodedef->get(n_minus_x_minus_y).groups)[groupname] != self_group)
1433-
|| n_minus_x_minus_y.getContent() == thiscontent)
1434-
is_rail_x_minus_y[0] = true;
1435-
1436-
if ((nodedef->get(n_minus_x_plus_y).drawtype == NDT_RAILLIKE
1437-
&& ((ItemGroupList) nodedef->get(n_minus_x_plus_y).groups)[groupname] != self_group)
1438-
|| n_minus_x_plus_y.getContent() == thiscontent)
1439-
is_rail_x_plus_y[0] = true;
1440-
1441-
if ((nodedef->get(n_plus_x).drawtype == NDT_RAILLIKE
1442-
&& ((ItemGroupList) nodedef->get(n_plus_x).groups)[groupname] != self_group)
1443-
|| n_plus_x.getContent() == thiscontent)
1444-
is_rail_x[1] = true;
1445-
1446-
if ((nodedef->get(n_plus_x_minus_y).drawtype == NDT_RAILLIKE
1447-
&& ((ItemGroupList) nodedef->get(n_plus_x_minus_y).groups)[groupname] != self_group)
1448-
|| n_plus_x_minus_y.getContent() == thiscontent)
1449-
is_rail_x_minus_y[1] = true;
1450-
1451-
if ((nodedef->get(n_plus_x_plus_y).drawtype == NDT_RAILLIKE
1452-
&& ((ItemGroupList) nodedef->get(n_plus_x_plus_y).groups)[groupname] != self_group)
1453-
|| n_plus_x_plus_y.getContent() == thiscontent)
1454-
is_rail_x_plus_y[1] = true;
1455-
1456-
if ((nodedef->get(n_minus_z).drawtype == NDT_RAILLIKE
1457-
&& ((ItemGroupList) nodedef->get(n_minus_z).groups)[groupname] != self_group)
1458-
|| n_minus_z.getContent() == thiscontent)
1459-
is_rail_z[0] = true;
1460-
1461-
if ((nodedef->get(n_minus_z_minus_y).drawtype == NDT_RAILLIKE
1462-
&& ((ItemGroupList) nodedef->get(n_minus_z_minus_y).groups)[groupname] != self_group)
1463-
|| n_minus_z_minus_y.getContent() == thiscontent)
1464-
is_rail_z_minus_y[0] = true;
1465-
1466-
if ((nodedef->get(n_minus_z_plus_y).drawtype == NDT_RAILLIKE
1467-
&& ((ItemGroupList) nodedef->get(n_minus_z_plus_y).groups)[groupname] != self_group)
1468-
|| n_minus_z_plus_y.getContent() == thiscontent)
1469-
is_rail_z_plus_y[0] = true;
1470-
1471-
if ((nodedef->get(n_plus_z).drawtype == NDT_RAILLIKE
1472-
&& ((ItemGroupList) nodedef->get(n_plus_z).groups)[groupname] != self_group)
1473-
|| n_plus_z.getContent() == thiscontent)
1474-
is_rail_z[1] = true;
1475-
1476-
if ((nodedef->get(n_plus_z_minus_y).drawtype == NDT_RAILLIKE
1477-
&& ((ItemGroupList) nodedef->get(n_plus_z_minus_y).groups)[groupname] != self_group)
1478-
|| n_plus_z_minus_y.getContent() == thiscontent)
1479-
is_rail_z_minus_y[1] = true;
1480-
1481-
if ((nodedef->get(n_plus_z_plus_y).drawtype == NDT_RAILLIKE
1482-
&& ((ItemGroupList) nodedef->get(n_plus_z_plus_y).groups)[groupname] != self_group)
1483-
|| n_plus_z_plus_y.getContent() == thiscontent)
1484-
is_rail_z_plus_y[1] = true;
1485-
1486-
bool is_rail_x_all[] = {false, false};
1487-
bool is_rail_z_all[] = {false, false};
1488-
is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
1489-
is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
1490-
is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
1491-
is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
1408+
u8 index = 0;
1409+
for (s8 y0 = -1; y0 <= 1; y0++) {
1410+
// Prevent from indexing never used coordinates
1411+
for (s8 xz = -1; xz <= 1; xz++) {
1412+
if (xz == 0)
1413+
continue;
1414+
MapNode n_xy = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x + xz, y + y0, z));
1415+
MapNode n_zy = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y + y0, z + xz));
1416+
ContentFeatures def_xy = nodedef->get(n_xy);
1417+
ContentFeatures def_zy = nodedef->get(n_zy);
1418+
1419+
// Check if current node would connect with the rail
1420+
is_rail_x[index] = ((def_xy.drawtype == NDT_RAILLIKE
1421+
&& ((ItemGroupList) def_xy.groups)[groupname] == self_group)
1422+
|| n_xy.getContent() == thiscontent);
1423+
1424+
is_rail_z[index] = ((def_zy.drawtype == NDT_RAILLIKE
1425+
&& ((ItemGroupList) def_zy.groups)[groupname] == self_group)
1426+
|| n_zy.getContent() == thiscontent);
1427+
index++;
1428+
}
1429+
}
1430+
1431+
bool is_rail_x_all[2]; // [0] = negative x, [1] = positive x coordinate from the current node position
1432+
bool is_rail_z_all[2];
1433+
is_rail_x_all[0] = is_rail_x[0] || is_rail_x[2] || is_rail_x[4];
1434+
is_rail_x_all[1] = is_rail_x[1] || is_rail_x[3] || is_rail_x[5];
1435+
is_rail_z_all[0] = is_rail_z[0] || is_rail_z[2] || is_rail_z[4];
1436+
is_rail_z_all[1] = is_rail_z[1] || is_rail_z[3] || is_rail_z[5];
14921437

14931438
// reasonable default, flat straight unrotated rail
14941439
bool is_straight = true;
@@ -1497,58 +1442,55 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
14971442
u8 tileindex = 0;
14981443

14991444
// check for sloped rail
1500-
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])
1501-
{
1502-
adjacencies = 5; //5 means sloped
1445+
if (is_rail_x[4] || is_rail_x[5] || is_rail_z[4] || is_rail_z[5]) {
1446+
adjacencies = 5; // 5 means sloped
15031447
is_straight = true; // sloped is always straight
1504-
}
1505-
else
1506-
{
1448+
} else {
15071449
// is really straight, rails on both sides
15081450
is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
15091451
adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
15101452
}
15111453

15121454
switch (adjacencies) {
15131455
case 1:
1514-
if(is_rail_x_all[0] || is_rail_x_all[1])
1456+
if (is_rail_x_all[0] || is_rail_x_all[1])
15151457
angle = 90;
15161458
break;
15171459
case 2:
1518-
if(!is_straight)
1460+
if (!is_straight)
15191461
tileindex = 1; // curved
1520-
if(is_rail_x_all[0] && is_rail_x_all[1])
1462+
if (is_rail_x_all[0] && is_rail_x_all[1])
15211463
angle = 90;
1522-
if(is_rail_z_all[0] && is_rail_z_all[1]){
1523-
if (is_rail_z_plus_y[0])
1464+
if (is_rail_z_all[0] && is_rail_z_all[1]) {
1465+
if (is_rail_z[4])
15241466
angle = 180;
15251467
}
1526-
else if(is_rail_x_all[0] && is_rail_z_all[0])
1468+
else if (is_rail_x_all[0] && is_rail_z_all[0])
15271469
angle = 270;
1528-
else if(is_rail_x_all[0] && is_rail_z_all[1])
1470+
else if (is_rail_x_all[0] && is_rail_z_all[1])
15291471
angle = 180;
1530-
else if(is_rail_x_all[1] && is_rail_z_all[1])
1472+
else if (is_rail_x_all[1] && is_rail_z_all[1])
15311473
angle = 90;
15321474
break;
15331475
case 3:
15341476
// here is where the potential to 'switch' a junction is, but not implemented at present
15351477
tileindex = 2; // t-junction
15361478
if(!is_rail_x_all[1])
1537-
angle=180;
1479+
angle = 180;
15381480
if(!is_rail_z_all[0])
1539-
angle=90;
1481+
angle = 90;
15401482
if(!is_rail_z_all[1])
1541-
angle=270;
1483+
angle = 270;
15421484
break;
15431485
case 4:
15441486
tileindex = 3; // crossing
15451487
break;
15461488
case 5: //sloped
1547-
if(is_rail_z_plus_y[0])
1489+
if (is_rail_z[4])
15481490
angle = 180;
1549-
if(is_rail_x_plus_y[0])
1491+
if (is_rail_x[4])
15501492
angle = 90;
1551-
if(is_rail_x_plus_y[1])
1493+
if (is_rail_x[5])
15521494
angle = -90;
15531495
break;
15541496
default:
@@ -1566,7 +1508,7 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
15661508
float s = BS/2;
15671509

15681510
short g = -1;
1569-
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])
1511+
if (is_rail_x[4] || is_rail_x[5] || is_rail_z[4] || is_rail_z[5])
15701512
g = 1; //Object is at a slope
15711513

15721514
video::S3DVertex vertices[4] =

0 commit comments

Comments
 (0)
Please sign in to comment.