GSTown.ExpandTown()

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
keoz
Transport Coordinator
Transport Coordinator
Posts: 321
Joined: 16 Jul 2009 10:04

GSTown.ExpandTown()

Post by keoz »

Hi,

I have some questions about GSTown.ExpandTown(). Sometimes, it looks like the function, though called, doesn't actually build anything and I'm trying to figure out why !

1) When using this function, is there a way to have an information about what is actually built and where (on which tile) ?

2) What exactly kind of building action can this function trigger ? I already noticed that it builds buildings, roads, but does it also triggers conversion from normal roads to city roads ? Still other things ?

3) Does the function also triggers renewing of buildings (building new buildings in replacement of old ones) ?

4) Let's say that the function is called in a game where several House NewGRF's are loaded. The choice of the newGRF from which comes the buildings is completely random ?

5) In a gamescript, is there any difference about town growing if using ExpandTown instead of GSTown::SetGrowthRate ? Is there any reason to use one instead of the other ?

Thanks :)
Patch - Let's timetable depot waiting time with the Wait in depot patch.
GameScript - Searching a new way to make your cities growing ? Try the Renewed City Growth GameScript.
My screenshots thread.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: GSTown.ExpandTown()

Post by Zuu »

1) Not other than the mechanisms that players can use when trying to infruence where the town will grow. Eg. by blocking growth in one direction. GS cannot buy land, but there are probably some other construction that you can use to achive the same effect. (although ugly)

2) Not directly. You could record for each town tile if it has a house, road (owned by town) or not before calling the growth method and then compare some time after to figure out the difference.

3) I don't know exactly what it does, but I suspect that it works the same way as the "fund new buildings" option for players.

4) I don't know. Maybe someone else know - or you have to dig into the source code.

5) There is a difference. ExpandTown do not check if the town is allowed to grow or not. SetGrowthRate() set the rate at which the town will grow _if_ all cargo goals are met (see SetCargoGoal). Also, the town GUI offer a translated GUI for SetGrowthRate / SetCargoGoal where you with ExpandTown do not get this for free. On the other hand if you do all condition checks in your GS and then trottle your calls to ExpandTown, then you can do things not supported by SetCargoGoal.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
R2dical
Traffic Manager
Traffic Manager
Posts: 163
Joined: 18 Mar 2013 22:22

Re: GSTown.ExpandTown()

Post by R2dical »

Zuu wrote:2) Not directly. You could record for each town tile if it has a house, road (owned by town) or not before calling the growth method and then compare some time after to figure out the difference.
I have spent quite some time tweaking a function for this purpose (getting house tiles from a town), the tricky bit is doing it without using cargo supply/production in case of newgrf or whatever. Anyway maybe it will be of use to you:

Code: Select all

/**
 *	Get a tile lists of town building tiles in this town.
 * 	@param town_index The town to get tiles from.
 *	@return Returns an GSTileList of tiles.
 */
function MapConstructor::GetTownHouseList(town_index) {

	// Get town tiles.
	local house_tile_factor = 0.90;										///< A float multiplier, larger means a bigger square of town tiles, smaller means smaller raw list.
	local town_radius = (GSTown.GetHouseCount(town_index) * house_tile_factor).tointeger();
	local raw_town_tile_list = SuperLib.Tile.MakeTileRectAroundTile(GSTown.GetLocation(town_index), town_radius);		///< Uses SuperLib.
	local town_tile_list = List(raw_town_tile_list);				///< Note requires the List class from List library for DoValuate function later.
	
	// Filter for houses.
	town_tile_list.Valuate(GSTile.GetTownAuthority);
	town_tile_list.KeepValue(town_index);
	town_tile_list.Valuate(GSTile.GetOwner);
	town_tile_list.KeepValue(-1);
	town_tile_list.Valuate(GSIndustry.GetIndustryID);
	town_tile_list.KeepValue(65535);
	town_tile_list.Valuate(GSTile.IsBuildable);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSTile.IsWaterTile);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSRoad.IsRoadTile);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSRail.IsRailTile);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSMarine.IsCanalTile);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSBridge.IsBridgeTile);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSTunnel.IsTunnelTile);
	town_tile_list.KeepValue(0);
	town_tile_list.Valuate(GSMarine.IsLockTile);
	town_tile_list.KeepValue(0);
	local test_mode = GSTestMode();
	town_tile_list.DoValuate(function(tile)	{
												local demolish = GSTile.DemolishTile(tile);
												if(demolish) return 1;
												else if(GSError.GetLastError() == GSError.ERR_LOCAL_AUTHORITY_REFUSES) return 1;
												else return 0;
											});
	town_tile_list.KeepValue(1);

	return town_tile_list;
}
EDIT: Note the above function requires a GSCompanyMode with valid company in scope. Thus, this cannot be used in the startup tick time of game (no companies yet).

Also is there a way to see if a road is "owned" by a town and not just one from a defunct company or built by GS? Or is it just a matter of proximity...
Last edited by R2dical on 24 Dec 2013 21:03, edited 1 time in total.
User avatar
keoz
Transport Coordinator
Transport Coordinator
Posts: 321
Joined: 16 Jul 2009 10:04

Re: GSTown.ExpandTown()

Post by keoz »

Thank you both.

R2dical, nice for your peace of code. Will be useful.

About questions 3) and 5), I just made a test, and what I can say so far, is there is at least some difference between ExpandTown and SetGrowthRate about the way city growth. In the first case, it appears that the function ExpandTown does not handle building renewing. As a result, in a long term game, looks like the city center stays unchanged for years. With SetGrowthRate, city expands and building are renewed.

I'm gonna explore the other points, watching at the code, as Zuu suggested.
Patch - Let's timetable depot waiting time with the Wait in depot patch.
GameScript - Searching a new way to make your cities growing ? Try the Renewed City Growth GameScript.
My screenshots thread.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 13 guests