Page 1 of 1

Basic Road Pathfinder always return null

Posted: 11 Apr 2009 18:07
by deepblue2k8
Hi,

I tried to play around with NoAI in my spare time this weekend but couldn't get very far.

I copied the example source code from the wiki for my new AI to see how that works.

Code: Select all

import("pathfinder.road", "RoadPathFinder", 3);

//.......

local townid_a = townlist.Begin();
		local townid_b = townlist.Next();

		AILog.Info("Town " + AITown.GetName(townid_a) + " Pop " + AITown.GetPopulation(townid_a));
		AILog.Info("Town " + AITown.GetName(townid_b) + " Pop " + AITown.GetPopulation(townid_b));

		local pathfinder = RoadPathFinder();
		AILog.Info("Pos A: " + AITown.GetLocation(townid_a));
		AILog.Info("Pos B: " + AITown.GetLocation(townid_b));
		AILog.Info("IsRoad A: " + AIRoad.IsRoadTile(AITown.GetLocation(townid_a)));
		AILog.Info("IsRoad B: " + AIRoad.IsRoadTile(AITown.GetLocation(townid_b)));

		/* Try to find a path. */
		local path = false;
		pathfinder.InitializePath([AITown.GetLocation(townid_a)], [AITown.GetLocation(townid_b)]);
		while (path == false) {
			path = pathfinder.FindPath(100);
			AIController.Sleep(1);
		}

		if (path == null) {
			/* No path was found. */
			AILog.Error("pathfinder.FindPath return null");
		}
But the problem was that path was alway null. It never finds any path between the two towns. Even if there are only two towns on a flat map.

Can anybody help me? :-)

Thanks in advance

Re: Basic Road Pathfinder always return null

Posted: 11 Apr 2009 19:44
by GeekToo
Town.Getlocation does not neccesarily return a road buildable tile, you will have to check that, not only log it.

Re: Basic Road Pathfinder always return null

Posted: 11 Apr 2009 20:34
by deepblue2k8
Yeah of cause, but in my test cases IsRoadTile returns true for both start and destination tile. In that case the road path finder should find a valid path but for a reason it doesn't :(

Update:

I traced my problem to the line

Code: Select all

AIRoad.BuildRoad(cur_node, next_tile)) 
of the function Road::_Neighbours(path, cur_node, self) in the road pathfinder 3.

BuildRoad seems to return always false, even if both cur_node and next_tile are both empty or one of them is a road in the right direction.
I tested this in OpenTTD 0.7.0, but I still cant make any sense of it, because the downloaded AIs are working fine....


Update 2:

Nevermind that I am an idiot...

It will work if you set: AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);

...