CityBuilder Release Topic

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

Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: CityBuilder Release Topic

Post by Brumi »

Using the latest nightly (r26417), those yellow lines are accumulating under 'global goals' in the goals window. I just started a fresh game, no NewGRFs, and the default configuration of the GS.
Attachments
global goals.png
global goals.png (14.8 KiB) Viewed 8674 times
Zonta
Engineer
Engineer
Posts: 7
Joined: 18 Aug 2010 12:44

Re: CityBuilder Release Topic

Post by Zonta »

Greetings I wanted to try your script which he look very what we want to but the game is pause and won't start. Any hint about this?
Image
Brumi
President
President
Posts: 920
Joined: 18 Jul 2009 17:54

Re: CityBuilder Release Topic

Post by Brumi »

The script pauses the game while it is initializing. Try waiting for a while, it should start eventually.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: CityBuilder Release Topic

Post by Zuu »

If your script uses Pause and then execute DoCommads, I recommend you to alter the advanced setting construction.command_pause_level to the most permissive level and/or make sure you don't call DoComands that are forbidden during pause. If you do call a forbidden DoCommand during pause, your script will hang and be stuck forever due to FS#5238.

Your Start looks like the code paste below. I'm not sure when you build industries during the first 2500 ticks if that counts as action during pause due do happening during world gen or not. Though, if industry construction take long time you will run out of ticks in world gen and the game will start and at that time the above bug/limitation will for sure hit you if constructing industries is not allowed. As the map size etc. is outside of your control I wouldn't recommend to assume that Init_Industry always finish during world gen.

Code: Select all

function CityBuilder::Start()
{
	GSGame.Pause();
	if(!this.on_load)
		{
		this.Init_Industry();
		}
	Sleep(1);
	LoadIntroductionDates();
	DemSizes = Demand_Sizes(intro.weakref());
	// ...
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
User avatar
Hamilton2007
Transport Coordinator
Transport Coordinator
Posts: 289
Joined: 16 Nov 2008 10:57
Location: Belgium

Re: CityBuilder Release Topic

Post by Hamilton2007 »

Is there anywhere a wiki on this?

At company goals i have always: none

The story book window doesn't fit my screen..

Not new to the game, but i am new to the script concept.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: CityBuilder Release Topic

Post by Zuu »

In a quick test in r26620, I can't see that CityBuilder after 1 year has used the Story Book window. I did however only do some simple pax transport in my claimed town.

It does however add a lot of goals to the Goal window. It doesn't seem to update the existing goals. Instead new ones are added. Thus giving you a very long list as time progress.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Aphid
Traffic Manager
Traffic Manager
Posts: 168
Joined: 16 Dec 2011 17:08

Re: CityBuilder Release Topic

Post by Aphid »

I should find me some time to update this.

Apparently some of the goals aren't added to the correct array (which gets 'emptied' each a month). I'm a bit unsure though, maybe some of all of those ways of doing requirements should be cut out, there's basically four ways to do the same thing in there now, that never seems good.

While using the story book would be nice for the sake of finally having a nice requirement list for each town, how would I go about 'linking' it to the towns in a user-friendly manner?
EDIT: Apparently I've found the offending code; in the function that makes news messages it also creates goals instead of extending the news message. So it would only happen if you had news messages on. News messages don't work anyway, I can't use a string that's long enough in a news message, and there is no formatting. I should remove them next version.

For now a little bugfix will do.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: CityBuilder Release Topic

Post by Zuu »

Currently town window cannot interactively link to goals nor story pages. Goals and Story book can link back and forth. There are many ways this could be improved, but none of these have been implemented as of today.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Gigigonzalez
Engineer
Engineer
Posts: 91
Joined: 12 Jun 2014 14:24

Re: CityBuilder Release Topic

Post by Gigigonzalez »

Hello, I would like to thank you for this Wonderful script, it works pretty much without issues. (multiplayer 4k x 4k map)
Im using the absolute latest version :)
So cheers for that!

I do have a few questions though, here they come!

1. Im running a dedicated server with a large map, and i was wondering if the amount of Opcodes before script is suspended has an effect on cpu usage on both dedicated server and client side. At this point its set on 50,000. Could i put this back to default of 10,000 without implications for the dedicated server and huge map?

2. The maximum HQ distance is 16 as default, increasing this would be fun but its pretty much the same question, does increasing this setting affect the cpu usage on dedicated server/clients? I could imagine it would, unless the script only calculates it once when someone claims a town.

3. Im having a slight issue with buildings that are auto replaced, or actually the lack thereof. Im using total town replacement set, and usually start game quite early. By the time its 1920 and up only the new built houses at the edge of the city takes the new buildings, but in the center of city no buildings get replaced. Is there an option to change this? or is it mandatory to destroy buildinge yourself?

TYVM :)
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: CityBuilder Release Topic

Post by Zuu »

Execution of game scripts happen only on server side. Some things it create are saved in the save game sent to clients, but internal script variables are only stored in the server memory an saved only when you save the game on the server but not sent to clients.

So script performance only matter for the server. Other than displaying a huge goal list or an insanely tall story page may have some affect for clients. Or if the script construct complex road for road PF or otherwise create stuff in the map that affect clients. But the script itself only run on the server.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Gigigonzalez
Engineer
Engineer
Posts: 91
Joined: 12 Jun 2014 14:24

Re: CityBuilder Release Topic

Post by Gigigonzalez »

Thanks for the insight, much appreciated, but unfortunately my questions are still open.

ps. Opcode before suspend option thus affects server cpu usage? Question still remains if lowering this to default (10,000) will impair the script negatively.
I read somewhere that it makes AI harder, but what would it affect for a script like CB?
Aphid
Traffic Manager
Traffic Manager
Posts: 168
Joined: 16 Dec 2011 17:08

Re: CityBuilder Release Topic

Post by Aphid »

As for this particular script, you can with some coding perhaps test your particular situation.

You see, I wrote a special 'atomic' function that tries to reserve some X opcodes. This is important for things like
detecting new companies and making objects for them (which can be used by the rest of the script to give them goals,
and the new story book pages, etc.).

Code: Select all

function Atomic(n){
if(n > GSController.GetOpsTillSuspend () + 1){GSController.Sleep(1);}
// Uncomment this to test if it works (spammy!)
//Log.Info("OPS: "+GSController.GetOpsTillSuspend (), Log.LVL_INFO);
}
Now by far and wide the two most important things that will slow down cityBuilder are the amount of towns, and the amount of cargoes you keep track of for each town.

Armed with these ideas you could try to 'measure' the amount of operations codes needed for a 'small' map, in the range of 10-20 towns. Make sure the towns are large enough to have a full requirement set, because (very) small towns are processed considerably faster. Set the maximum opcodes to a high value. Use the GetOpsTillSuspend function twice, at the beginning and end of the main loop, and print the results to console. The result is the amount of opcodes needed for your map. Divide it by the number of towns and we have (approximately) the amount of opcodes per town. There's some amount of overhead included, which makes this somewhat of a conservative estimate.

Note that I won't do the work for you, there's many settings combinations you can have for this script, and they do affect the code paths that are taken. If you put on a lot of logging/news settings and a lot of the special requirements, the script is slower than when you turn all this stuff off.

How can I calculate how large my map can be?

We know there are 28 days in the shortest month, and thus 2072 ticks in this month. If you have your opcode limit at N, we can thus at most use 2072 * N opcodes in the main loop each month.
So, if you want to use citybuilder with a very large map, you can use this to calculate how many towns you can have. Let's do an example:

Suppose that citybuilder uses 50,000 opcodes per town. If we have our limit at 10,000, you can have 414 towns before you will run into problems. Because we would like things to go smoothly in any scenario, and having more users can pose some extra overhead, I recommend keeping town count below 75% of this value, or below 300 in this case. The reality is actually pretty simple, the opcode count scales pretty much linearly with the amount of towns you can have before problems occur.

What problems will occur?

Well, the game loop will sort of fail. Instead of running each month, a month transition occurs half-way. This means the game will effectively 'skip' a month.
It may be an idea to build in some sort of mechanism to detect occurrences and resolve this. My question would be what the best mechanism is in this case. Do we want to force the game to pause, do we want to throw an error or do we want to throw a warning? The error guarantees the script to be well-behaved but of course can ruin your day when your server effectively crashes (well, the game continues, but the script stops).

How does opcode affect the server?

Now what is meant by 'opcode' is actually one instance of a squirrel function or one API call to openTTD, e.g. a script opcode. At the very technical level, an opcode is an instruction for the CPU from the x86 set. And at an even lower level there are differences still per opcode how many instruction cycles per opcode are needed (or opcodes per instruction cycle). Those CPU opcodes result from compiled C code, which in turn is invoked by the squirrel code. Thus 10,000 squirrel calls may be somewhere in the range of about 250,000 to many millions of actual CPU instructions. Some operations like

Code: Select all

function foo(i, j){return i + j; }
are really simple (we assume that the result of compute is actually used). Other operations may be more complicated:

Code: Select all

function bar(i){return GSCompany.ResolveCompanyID(i);}
Generally speaking, the opcode limit as defined by default is a very conservative limit so that the openTTD game script engine will give you minimal overhead when it is added on top of the already existing other game functions, even in worst case scenarios where a script writer writes a script that tries to call mostly resource-intensive operations. Now if you set the limit too high for your machine to handle, you may observe noticeable lag in your game (the clients will have to wait for the server to synchronize). If openTTD uses up high amounts of CPU power to run then you can set this number lower to use less.

Be careful though, premature optimization is never good. First identify if indeed this setting is relevant to system usage. There may be other issues with large maps (pathfinding a large amount of vehicles on a complex network) that can be the cause of your problem.

Some extra info

By the way, a map size of 4kx4k is considered insanely huge by openttd standards. While your users will rarely build as dense a network as is commonly used at openttdcoop it probably won't work when you have high town/industry density. Tile count isn't really the biggest issue, it's vehicle/track count. Pathfinding is going to cost you around O(N * ln(N)), and we run that for some V vehicles, it becomes O(V * N * ln(N)). (N would be the amount of 'vertices' in the pathfinding graph, which initially starts at two. Each network has its own graph. Each railway intersection possibility in a net adds one vertex for a dead-end, and half a vertex for a connected piece. A signal 'block' also adds a vertex).

Because pathfinding generally tends to be the most consuming resource, you might want to look into putting ships back to the default TTD pathfinder. This means the ships become 'stupid' and people have to steer them using a lot of buoys, but has the advantage that the pathfinding is much faster. The reason for the ships specifically having this problem is because the 'network' the ships run on has a lot of vertices, ships can go in a lot of possible routes and it isn't easy finding the best possible route from A to B like with planes (who just go straight all the time). Alternatively, try forbidding any use of ships at all.

On the other hand, if you only include very little towns and industries, then you just have a very sparse map, where networks will not need a lot of intersections.
Gigigonzalez
Engineer
Engineer
Posts: 91
Joined: 12 Jun 2014 14:24

Re: CityBuilder Release Topic

Post by Gigigonzalez »

Wow long reply :) ill try to use that code you posted.

Well we've been running a dedicated server with a 4K x 4K map for quite some time now with daylenght patch.
So far ive dropped opcodes all the way to 10,000 and no problems so far.

Got about 100-125 towns/cities and some of them are 50K to 200K population. So far i havent seem to run into any CB problems. I suspect daylenght compensates for the (perhaps too low) opcode setting because time goes slower.

Yes the server is quite resource demanding, and unless you have a decent rig (C2D/i5/7) the game is probsbly going to be laggy. On the newer processors its still smooth. Estimated about 15-25% cpu load on openttd.exe based on one core. Unfortunately A LOT of people i meet related to openttd often still run a fairly old rig, ol pentium 4's are no exception :/ (no disrespect intended)

I did however recompile the build we use with the Intel C++ compiler, SSE4/AVX code as well when the CPU supports it, and 64bit variant. I have so far observed a wild performance increase compared to the standard GCC compiled build, depending on workload (graphic intensive or more math based) up to 50% decrease in cpu usage. Ofcourse thats on my i7.


One more question, did you ever implement some way for cities to auto demolish buildings? because nothing gets replaced as years go by, or did i miss some setting/function? Shrink size i already tried changing i believe.
Aphid
Traffic Manager
Traffic Manager
Posts: 168
Joined: 16 Dec 2011 17:08

Re: CityBuilder Release Topic

Post by Aphid »

It's a side effect of the way I stop city growth. When a town isn't growing, it literally 'freezes' in time. Cities should replace buildings when they grow though. This has the following reason:

The problem is doing the opposite (allowing building replacement) nets you a lot of free growth a lot of the time. You see, the town zones in openttd are quite large. A dense town (3x3 grid or 2x13 grid or something similar) will actually easily become fully tier 4/5 buildings at quite a small size. Which means that (fairly large) towns get converted to only office buildings by allowing building replacement. Some clever person could abuse this to make their towns larger than what cargo requirements would allow.

If I could figure out a way to make towns demolish buildings (to keep town population the same by bulldozing some random older stuff when modernizing), it wouldn't be such a problem to have buildings get replaced.
Gigigonzalez
Engineer
Engineer
Posts: 91
Joined: 12 Jun 2014 14:24

Re: CityBuilder Release Topic

Post by Gigigonzalez »

The problem is that its literally not happening. Lets imagine a scenario, we start at 1830

We have TTRS and japanese building set.

What happens is, when you look at the same map in 2000 or something, and weve played A LOT, the cities literally only apply new buildings on the outer rings of thE cities with new buildings from,newer eras. so the town center pretry much still have the old buildings from when we started in 1830.

Look at it like when you,chop a tree, and see the inner rings where you can recognize its age.


Its not a super critical problem as players can replace their buildings manually but..
Gigigonzalez
Engineer
Engineer
Posts: 91
Joined: 12 Jun 2014 14:24

Re: CityBuilder Release Topic

Post by Gigigonzalez »

Would,be nice to have some regular changelog,updates, i noticed you also pulled them out of the readme file.
Gigigonzalez
Engineer
Engineer
Posts: 91
Joined: 12 Jun 2014 14:24

Re: CityBuilder Release Topic

Post by Gigigonzalez »

v109 crashes on r26694
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: CityBuilder Release Topic

Post by planetmaker »

Gigigonzalez wrote:v109 crashes on r26694
The stack trace of that would be quite helpful. You can make a screenshot in the scripts/AI settings of that.
Hobbyte
Engineer
Engineer
Posts: 4
Joined: 10 Sep 2014 22:17

Re: CityBuilder Release Topic

Post by Hobbyte »

planetmaker wrote:
Gigigonzalez wrote:v109 crashes on r26694
The stack trace of that would be quite helpful. You can make a screenshot in the scripts/AI settings of that.

I'm having the same issue on r26694-reddit.

I've attached the trace from the debug window, as well as the save where the error is occurring.
Attachments
error.png
(183.47 KiB) Downloaded 5 times
fixcitybuilder.sav
(2.03 MiB) Downloaded 168 times
Hobbyte
Engineer
Engineer
Posts: 4
Joined: 10 Sep 2014 22:17

Re: CityBuilder Release Topic

Post by Hobbyte »

Ok, I have an update to the situation.

It appears to be a bug in the CityBuilder script.

On line 87 of Citygrowth.nut, it is referencing the value "this.tsize" which does not exist in the City class. I believe it should be changed to reference "this.min_pop"
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 15 guests