- bool self_connect_to_raillike = ((ItemGroupList) nodedef->get(n).groups)[groupname] != 0;
-
- if ((nodedef->get(n_minus_x).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_minus_x).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_minus_x.getContent() == thiscontent)
- is_rail_x[0] = true;
-
- if ((nodedef->get(n_minus_x_minus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_minus_x_minus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_minus_x_minus_y.getContent() == thiscontent)
- is_rail_x_minus_y[0] = true;
-
- if ((nodedef->get(n_minus_x_plus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_minus_x_plus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_minus_x_plus_y.getContent() == thiscontent)
- is_rail_x_plus_y[0] = true;
-
- if ((nodedef->get(n_plus_x).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_plus_x).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_plus_x.getContent() == thiscontent)
- is_rail_x[1] = true;
-
- if ((nodedef->get(n_plus_x_minus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_plus_x_minus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_plus_x_minus_y.getContent() == thiscontent)
- is_rail_x_minus_y[1] = true;
-
- if ((nodedef->get(n_plus_x_plus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_plus_x_plus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_plus_x_plus_y.getContent() == thiscontent)
- is_rail_x_plus_y[1] = true;
-
- if ((nodedef->get(n_minus_z).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_minus_z).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_minus_z.getContent() == thiscontent)
- is_rail_z[0] = true;
-
- if ((nodedef->get(n_minus_z_minus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_minus_z_minus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_minus_z_minus_y.getContent() == thiscontent)
- is_rail_z_minus_y[0] = true;
-
- if ((nodedef->get(n_minus_z_plus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_minus_z_plus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_minus_z_plus_y.getContent() == thiscontent)
- is_rail_z_plus_y[0] = true;
-
- if ((nodedef->get(n_plus_z).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_plus_z).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_plus_z.getContent() == thiscontent)
- is_rail_z[1] = true;
-
- if ((nodedef->get(n_plus_z_minus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_plus_z_minus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || n_plus_z_minus_y.getContent() == thiscontent)
- is_rail_z_minus_y[1] = true;
-
- if ((nodedef->get(n_plus_z_plus_y).drawtype == NDT_RAILLIKE
- && ((ItemGroupList) nodedef->get(n_plus_z_plus_y).groups)[groupname] != 0
- && self_connect_to_raillike)
- || 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];
+ int self_group = ((ItemGroupList) nodedef->get(n).groups)[groupname];
+
+ u8 index = 0;
+ for (s8 y0 = -1; y0 <= 1; y0++) {
+ // Prevent from indexing never used coordinates
+ for (s8 xz = -1; xz <= 1; xz++) {
+ if (xz == 0)
+ continue;
+ MapNode n_xy = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x + xz, y + y0, z));
+ MapNode n_zy = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y + y0, z + xz));
+ ContentFeatures def_xy = nodedef->get(n_xy);
+ ContentFeatures def_zy = nodedef->get(n_zy);
+
+ // Check if current node would connect with the rail
+ is_rail_x[index] = ((def_xy.drawtype == NDT_RAILLIKE
+ && ((ItemGroupList) def_xy.groups)[groupname] == self_group)
+ || n_xy.getContent() == thiscontent);
+
+ is_rail_z[index] = ((def_zy.drawtype == NDT_RAILLIKE
+ && ((ItemGroupList) def_zy.groups)[groupname] == self_group)
+ || n_zy.getContent() == thiscontent);
+ index++;
+ }
+ }
+
+ bool is_rail_x_all[2]; // [0] = negative x, [1] = positive x coordinate from the current node position
+ bool is_rail_z_all[2];
+ is_rail_x_all[0] = is_rail_x[0] || is_rail_x[2] || is_rail_x[4];
+ is_rail_x_all[1] = is_rail_x[1] || is_rail_x[3] || is_rail_x[5];
+ is_rail_z_all[0] = is_rail_z[0] || is_rail_z[2] || is_rail_z[4];
+ is_rail_z_all[1] = is_rail_z[1] || is_rail_z[3] || is_rail_z[5];