News:

Simutrans Wiki Manual
The official on-line manual for Simutrans. Read and contribute.

Query for Standard developers - parallel ways

Started by jamespetts, February 08, 2014, 01:21:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jamespetts

I believe that I have asked this question before, and had a simple but useful answer to it which I have not hitherto had time to make use of, but to my embarrassment, and despite some considerable efforts spent searching for the thread, I have not been able to find it, so I hope that I will be forgiven for asking the same question again.

How would I detect whether a given tile has a parallel way of the same type in an adjacent tile? I should like to add a feature to Experimental whereby building a second parallel way (as in a dual carriageway road or double (or quadruple) track railway) is cheaper than building a way not next to another, which I believe is realistic. Merely detecting any adjacent tile with a way of the same type is, of course, no use, as it would always detect the way in the previous tile that has just been built, so the search needs to be confined to parallel tiles. Any suggestions as to doing this would be much appreciated.

prissi

You have the ribis, so look for way 90 degree to them. But this will not easily work on diagonals.

jamespetts

Would not a way 90 degrees to the ribis be perpendicular rather than parallel; or am I misunderstanding something?

kierongreen

If the way is running north-south then you are checking the perpendicular (90 degree) tiles east and west to see if either of those tiles have matching ribis.

And yes, you've asked this before and I gave you an answer then!

jamespetts

Ahh, I see - you suggest checking tiles perpendicular to the current way under construction? That does make sense - thank you. I am currently looking at sint64 wegbauer_t::calc_costs() (I assume that this is the right function to be modifying). What I am having trouble with at present is working out what the direction of the current way is: the way is not built when the costs are calculated, so I cannot get the ribi object from a way tile. I think that I can create a from/to pair using route[i+1], route (or route, route [i-1] for the last tile), but I am not entirely sure how to get a ribi object from a pair of tiles (if that is even the right way of going about it). Apologies for asking what are probably silly questions - I am somewhat unfamiliar with the ribi system.

Apologies again for not being able to find the earlier thread. If any moderator can find it, please do merge it with this. Thank you both for your help so far.

prissi

There is a conversion from coordinates to ribi: ribi_typ(k0,k1) ...

Still it would not work on diagonals, and not on crossings. The only sensible way is to check if there is an exiting route with appropriately shifted waypoints. Duable, but certainly not trivial.

jamespetts

Thank you for that. Initial testing shows that this code in wegbauer_t::get_costs() seems to work:


if(route.get_count() > 1)
{
koord3d earlier;
koord3d later;
if(i < route.get_count() - 1)
{
earlier = route[i];
later = route[i + 1];
}
else
{
earlier = route[i - 1];
later = route[i];
}

const ribi_t::ribi our_direction_bits = ribi_typ(earlier, later);

for(int n = 0; n < 8; n ++)
{
const koord kn = pos.neighbours[n] + pos;
if(kn == earlier || kn == later)
{
continue;
}
const grund_t* gr_neighbour = welt->lookup_kartenboden(kn);
if(gr_neighbour && gr_neighbour->get_weg(besch->get_waytype()))
{
// This is a parallel way of the same type - reduce the forge cost.
forge_cost /= 3; //TODO: Have this customisable in simuconf.tab
}
}
}


although I have not tried it in the code that actually calculates the cost to be applied. Is there a reason, incidentally, that the code in wegbauer_t::get_costs() is duplicated when the actual cost is calculated, rather than re-used?

prissi

This is a preview. Other player or the AI could have been interfered meanwhile.

jamespetts

Ahh, I see, thank you. For reference, I have now implemented the feature on my way-improvements branch, without using ribi at all, but instead checking whether any of the neighbouring tiles are in the route used for laying the ways.