FS#4702 - AIStation::IsWithinTownInfluence doesn't report a station put north a town

Attached to Project: OpenTTD
Opened by pagnon stephane (krinn) - Saturday, 30 July 2011, 09:14 GMT
Last edited by frosch (frosch) - Friday, 19 August 2011, 21:20 GMT
Type Bug
Category Script → NoAI
Status Closed
Assigned To No-one
Operating System All
Severity Low
Priority Normal
Reported Version trunk
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No


description in the api state :

"Find out if this station is within the rating influence of a town. "
It's conflicting with just checking one tile of a station, as the ai dev expect a real answer as if any tiles of the station are within the town influence, that station will be useable with that town.

the function itself (from ai_station.cpp) isn't bug, and do what it was made for, but the logic show weakness and finally end-up returning a bad result, as a station could be in the town influence but the function will says false
/* static */ bool AIStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
if (!IsValidStation(station_id)) return false;

return AITown::IsWithinTownInfluence(town_id, GetLocation(station_id));

It could be easy solve in squirrel with :
function IsWithinTownInfluence(stationid, townid)
stationtiles= (well, all tiles to check, should be all tiles of the station)
foreach (tile, dummy in stationtile) { if (AITile.IsWithinTownInfluence(tile, townid)) return true; }
return false;

That bug/weakness/mistake/misinterpretation... of conception isn't really seen by most ai devs because we need, not a big race condition to see it, but a special condition :
1 station tile must be within town influence + top station tile must be outside town influence.

And because of how all structure building functions works in noai (give the stration toptile position to build it) it's generally impossible to see that effect. As common use makes you filter tiles that are within a town influence, then you pick the best... any tile, and just try to build your station from that tile, and as the build station take top tile to build the station, you endup with 1/ station is too close from town and cannot find a position to put your station or 2/ you indeed have space for the station, and toptile == a tile within town influence.
So "common use" prevent building big structure north of town, and prevent that "bug" seen with their top tile outside town influence.

Now if i made an airport 10 tiles deep, 5 tiles outside station influence + 5 tiles within (of course north of the town): then toptile is outside town influence and the AIStation.IsWithinTownInfluence will return false

- the function should be change to return true if any station tiles is within the town influence

or the doc:
- "Find out if this station is within the rating influence of a town." -> Find out if this station toptile is within the rating influence of a town
-"True if the tile is within the rating influence of the town."->"True if the station top tile is ...".
- And maybe an additional warning to ai dev to point out/remind them this special case
This task depends upon

Closed by  frosch (frosch)
Friday, 19 August 2011, 21:20 GMT
Reason for closing:  Fixed
Additional comments about closing:  in r22763, r22764
Comment by frosch (frosch) - Saturday, 30 July 2011, 09:28 GMT
You are misunderstanding the meaning of "town influence" in the game mechanics.
A station is within town influence, if the service quality at it affects the town rating. This is a property of the whole station, not of a single tile.
A tile is within town influcence, if building something on it affects the town rating. This is a property of a single tile.

Edit: I.e. what you are experiencing is the weirdness of some aspects of the game mechanics, and not a bug in the NoAI API. If the station sign is far away from the town, the station will not affect the town rating, even if some parts of the station are close to the town.
Comment by pagnon stephane (krinn) - Saturday, 30 July 2011, 12:02 GMT
that's indeed kinda weird, what you are saying is that such airport would :
1/ take town rating damage by its constrution because lower tiles are own & influence the town
2/ won't take benefits to town rating because the station sign is too far

Just hope at least town rating doesn't get mad seeing the few tiles in its influence and never then see it's working (poor town rating for having a "not working" station in its influence") or the station is totally ignore ?
Also good to know how contrary works too: airport south of town with toptile within town influence area, but lower tiles outside, how this is handle from town point of view. Because it could mean a small airport with all its tiles within the boundaries of the town influence gave better results for a town vs a bigger airport that have some tiles outside the town limits.

Anyway never really think it's a bug from the api or the function itself, more the doc that need a better clarification imo.
Don't you think it might be better of such special mechanic expose to the aidev or at least a more clear "using top tile of station" than just "station" or "tile"?
Comment by frosch (frosch) - Saturday, 30 July 2011, 13:11 GMT
Attached diff slightly changes the wording of AITile.IsWithinTownInfluence and AITown.IsWithinTownInfluence.
While these functions are duplicate (and thus should be removed according to the API policy), they may be required both for Valuators.

The diff also adds a function AITile::GetTownAuthority which reports the town affected by tree removal/planting and station construction.