Roadpathfinder v3: pathfinder.InitializePath()

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
gligli.ch
Engineer
Engineer
Posts: 7
Joined: 01 Mar 2012 14:30

Roadpathfinder v3: pathfinder.InitializePath()

Post by gligli.ch »

Hello everybody,

I'm new in squirrel Ai coding, and i hope my ask is post in the right place.

I wonder the data-format used by pathfinder.InitializePath().

I tried with two returns of AIMap.TileIndex() without results.
And when i try to construct a map-coordinates for a variable (like tile_a), i can only return the x instead of x,y...in fact don't know how to store such coordinate (x,y) in 'tile_a'

Does pathfinder use TileIndex or coordinates?

Thanks for support.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Roadpathfinder v3: pathfinder.InitializePath()

Post by Zuu »

All API functions that work with tiles take a tile index (with the exception of the function to convert X and Y to a tile index). To get the X and Y you need to use AITile.GetTileX(tile_index) or AITile.GetTileY(tile_index). For simple passing of a coordinate you shouldn't need to get the X and Y values separate. Only for some operations with tile areas etc. you might be interested to get the X and Y separate.

If you want to go X tiles to north, west etc. I would recommend SuperLib.Direction for that. That will save you from most dirty work with X and Y of tile coordinates.

Example: Get the tile X tiles south-east of tile_a:

Code: Select all

local tile_b = Direction.GetTileInDirection(tile_a, Direction.DIR_SE, X);
To import the direction library you need this at the top of your main.nut (or where you keep your imports):

Code: Select all

import("util.superlib", "SuperLib", 19);
Direction <- SuperLib.Direction;
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
gligli.ch
Engineer
Engineer
Posts: 7
Joined: 01 Mar 2012 14:30

Re: Roadpathfinder v3: pathfinder.InitializePath()

Post by gligli.ch »

Ok, lots of thanks.

Is that means that without this lib, we shall resolve the coords by math and then return the TileIndex? for your example (x+1,y) for each tile if i'm right
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Roadpathfinder v3: pathfinder.InitializePath()

Post by Zuu »

When I wrote the direction library I made this image:
Image

So to go south-east, (x, y+1) is the operation that you have to use.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
gligli.ch
Engineer
Engineer
Posts: 7
Joined: 01 Mar 2012 14:30

Re: Roadpathfinder v3: pathfinder.InitializePath()

Post by gligli.ch »

lol, damn, miss...

Yeah, what a work!

But that leads to another question :)

I dont start testing this lib already (but i will, you convinced me ^^), so will the tile_a of your example be treaten as relative for Direction.GetTileInDirection() (i mean 0,0)? so it always will return 0-7 for a tile nextto
Or tile_a will be treaten "as is" and the function return the "real" MapTileIndex?

Furthermore, does "my" pathfinder.initializePath() uses reals or relatives?

It will probably be reals, but i ask to avoid mistakes.

I hope to make these test this week-end.

Again lots of thanks for precisions.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Roadpathfinder v3: pathfinder.InitializePath()

Post by Zuu »

I do generally not work with relative tiles. My lib allows to working with absolute tile coordinates and only express relative moments with a direction and a length in distance.

tile_a in my example could be the result of AIVehicle.GetLocation(vehicle_id) for some vehicle.

If you don't use my library, or something similar, you will have to work with relative tile movements directly. Trying to understand how tiles works first before using a helper like SuperLib.Direction is for sure a good idea that will help you writing better code. In some cases you will be able to make things more efficient by making it yourself than trying to glue together some utility functions that I've written for a different purpose.

Index 0-7 for the directions is something that you generally don't need to know of. There are constants eg. DIR_N for north etc. That image only shows that DIR_N is internally mapped to a zero.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 7 guests