game days to travel distance x at speed y?

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
User avatar
Kev!
Engineer
Engineer
Posts: 37
Joined: 21 Jun 2011 16:30
Location: In Da House!
Contact:

game days to travel distance x at speed y?

Post by Kev! »

Howdy,

Anyone have this coded already? or point me to some reference?

of course time = distance/speed

time being ticks, distance being 664.(216) per unit of distance 10 for example, speed let's say 20mph 1950's ferry boat being 32 kph-ish...

6642.(163) / 32 * 2048(it a boat) = .1 or so ticks 74 ticks in a day 7.4 days for a ferry to go 10 squares...

hover craft is faster 112 kph-ish...

6642.(163) / 112 * 2048(it a boat) = .03 ish ticks 74 ticks in a day 2.2 days for a hovercraft to go 10 squares...


Am I in the ball park?

Please advise,
I DO like a nice caboose...
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: game days to travel distance x at speed y?

Post by Zuu »

In SuperLib I have a function Engine.GetFullSpeedTravelTime. You might want to take a look at this and subtract full speed with your desired speed:

Full source code

Code: Select all

function _SuperLib_Engine::GetFullSpeedTraveltime(engine, distance)
{
	// X km/h => X/27 tiles/day

	/* Multiply by this from start and then remove it at the end in order to 
	 * not lose to much significance in the calculation process. 
	 *
	 * On 100 km/h the tile/day speed becomes 3.7, which would become just
	 * 3 in the integer conversion which would be loosing to much detail.
	 */ 
	local MULTI = 50; 

	local km_h_speed = AIEngine.GetMaxSpeed(engine) * MULTI;
	local tile_speed = km_h_speed / 27;

	local travel_time_days = distance * MULTI / tile_speed;

	return travel_time_days;
}
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Jtm
Engineer
Engineer
Posts: 2
Joined: 29 Jul 2008 19:39

Re: game days to travel distance x at speed y?

Post by Jtm »

Alternatively you could create your own helper function by removing the variable named MULTI and moving divisor 27 to act as multiplier, thus improving quality, accuracy and optimizing the function performance. If possible, one should always prefer multiplying over dividing. This way there is no need for additional multiplier to preserve the accuracy. This is the result:

Code: Select all

function GetFullSpeedTravelTime(engine, distance)
{
	// X km/h => X/27 tiles/day
	local travel_time_days = 27 * distance / AIEngine.GetMaxSpeed(engine);
	return travel_time_days;
}
Or simply:

Code: Select all

local travel_time_days = 27 * distance / AIEngine.GetMaxSpeed(engine);
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests