Which method suits best to modify the economy of OTTD?

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Which method suits best to modify the economy of OTTD?

Post by Openend »

Hello,

I am considering to work on my own small modifications to openttd. I hope this is the right section of the forums to ask the following questions:
My ultimate target is to change the economy of vanilla openttd, the relations between cities/towns and the production of industries.
I realize I may have to practice and get use to the environment prior to that, however.

This means: Changing parameters and relations, no new vehicles or graphics (yet).

1. What would be the package to apply economy changes to ottd? A patch? NewGRF? Script?
2. Is there a coding primer for ottd? (NML is the language, i think).
3. Is there anything better for coding nml than notepad++ (which I use currently)?
4. What are the reasonable limitations for computing change on the map e.g. City Growth?
5. What type of modification package (see 1) has the power to control the starting parameters of cities? For example: What If I want to start every town with 250 (+- 35) residents?
6. How complicated is it to introduce new types of cargo / supply chains into the game? (logic only, no graphics for now)?

Thanks for your time and answers!

If recommended, I could merge your answers back into this post. What do you think?
Last edited by Openend on 02 Jul 2015 11:57, edited 2 times in total.
User avatar
FLHerne
Tycoon
Tycoon
Posts: 1543
Joined: 12 Jul 2011 12:09
Location: St Ives, Cambs, UK

Re: Some Questions. Thanks!

Post by FLHerne »

OpenTTD itself is written in C++.
Docs are here, but patching the game itself should be a last resort if you can't do what you want sanely otherwise.

NML is used for NewGRFs (it compiles to NFO, which you could write stuff in manually if you really love hex codes).
NML documentation, tutorial; NFO documentation. Some features of NFO aren't supported in NML (yet), but not relevant to the things you've mentioned.

You can use NewGRFs to add industry and cargo types, but there's no way for a NewGRF industry to get much information about towns (you can find the distance to the nearest town and the 'zone' the industry's in, that's about it). Some existing NewGRF industries only allow themselves to be built in/near towns. Notepad++ should be fine, there's a highlighting file for it here.

AIs and GameScripts are written in Squirrel.
Gamescript API docs.
A gamescript can get pretty much any information about towns (and almost everything else), and can create, grow and (indirectly) shrink them. It can also build industries, but not change their production AFAIK. There can only be one gamescript running in a game.
Temporary Permanent signature filling text. Content coming soon delayed indefinitely! Oh, and I have had a screenshot thread.
Linux user (XMonad DWM/KDE, Arch), IRC obsessive and rail enthusiast. No longer building robots; now I ring church bells.
Author of an incredibly boring stickied post about NewGRFs.
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Some Questions. Thanks!

Post by planetmaker »

Please use meaningful titles for your postings. It's not helpful to have a forum full of threads, all with the title "A question".
Openend wrote: 1. What would be the package to apply economy changes to ottd? A patch? NewGRF? Script?
2. Is there a coding primer for ottd? (NML is the language, i think).
3. Is there anything better for coding nml than notepad++ (which I use currently)?
4. What are the reasonable limitations for computing change on the map e.g. City Growth?
5. What type of modification package (see 1) has the power to control the starting parameters of cities? For example: What If I want to start every town with 250 (+- 35) residents?
6. How complicated is it to introduce new types of cargo / supply chains into the game? (logic only, no graphics for now)?
ad 1) Depends on what you mean with 'change economy'. It could be a patch. Or it could be a NewGRF. Or it could be a game script. Or a combination of any three.
ad 2) Depends on the answer to 1). OpenTTD itself is written in C/C++. NewGRFs in NML or NFO. And game scripts and AI in squirrel
ad 3) 'Better' is personal. I heard that notepad++ is quite popular.
ad 4) I don't understand that question
ad 5) That needs a patch to the map creation algorithm. Or just use the scenario editor and place cities manually.
ad 6) Quite easy, nearly every industry NewGRF does so.
Eddi
Tycoon
Tycoon
Posts: 8267
Joined: 17 Jan 2007 00:14

Re: Some Questions. Thanks!

Post by Eddi »

ad 1: as a rule of thumb, NewGRFs are better for things that have a local effect (e.g. which house to place during town growth, or how much cargo to process in an industry), while Scripts are better for a global scope (e.g. how much cargo to gather before town growth is initiated, general size of the economy, etc.). a patch is needed when it falls in neither of these categories cleanly. (note that a script cannot rely on the presence of a NewGRF or vice versa)
ad 2: i don't know what a primer is
ad 3: no. if you dig a little, you might find a syntax highlighter for your favourite editor, but that's about it.
ad 4: a town records the amount of cargo delivered for each of the "town effects" (passengers, mail, goods, food, water) [multiple cargos can have the same town effect, they are mushed together and are indistinguishable at this point], and a script can freely trigger growth whenever it likes to do so based on this or other data. also, a script can terraform and mess with players' infrastructure.
ad 5: you could try starting an empty map in the scenario editor, and have a script place the towns. depending on your skills, a patch might be simpler
ad 6: medium. you're looking at a NewGRF for this.
User avatar
PikkaBird
Graphics Moderator
Graphics Moderator
Posts: 5602
Joined: 13 Sep 2004 13:21
Location: The Moon

Re: Some Questions. Thanks!

Post by PikkaBird »

FLHerne wrote:You can use NewGRFs to add industry and cargo types, but there's no way for a NewGRF industry to get much information about towns (you can find the distance to the nearest town and the 'zone' the industry's in, that's about it).
Actually, the parent object for industries is the town, so industries can access all their town's variables (whether it's a 'city', the index number, the population, the physical size, and the number of buildings).
planetmaker wrote:
Openend wrote: 5. What type of modification package (see 1) has the power to control the starting parameters of cities? For example: What If I want to start every town with 250 (+- 35) residents?
ad 5) That needs a patch to the map creation algorithm. Or just use the scenario editor and place cities manually.
Actually actually, limiting town size can be done easily in NewGRF; just disallow all buildings from placing* if the town size is above your target number.

*actually...
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: How would I start to modify OpenTTD game logic?

Post by Openend »

Thanks for the answers, I've modified the thread title.

Let me rephrase one of the questions:
4. What are the reasonable limitations for computing change on the map e.g. City Growth?
Is it possible to have a complex gamescript (like Renewed City Growth for example), without noticeable delay and without pausing the game for calculations on average hardware? It this simply a matter of testing?

The information given allows me to come up with a set of (hopefully) refined questions:

From what I read so far, I am now aiming for a gamescript. Are the following operations within the scope of a gamescript?
UPDATED
-> Define average city starting population and setting limits for deviation.
-> Set cargo acceptance for cities / industries and keep track of the delievered amounts to trigger changes
-> Change name / monthly production of towns/ industries when certain conditions (e.g. amount of specific cargo delievered) are met.

Digging into squirrel right now: From what I read so far there a many functions to check for values, but I have so far not found any function to influence values. Can you help me out here? Or is this a limitation of Gamescripts?

UPDATE:
Is there a way to initiate a period of rapid growth for cities? Like go from 250 to 1000 pop within one month? (given enough space).
Eddi
Tycoon
Tycoon
Posts: 8267
Joined: 17 Jan 2007 00:14

Re: How would I start to modify OpenTTD game logic?

Post by Eddi »

Openend wrote:Thanks for the answers, I've modified the thread title.

Let me rephrase one of the questions:
4. What are the reasonable limitations for computing change on the map e.g. City Growth?
Is it possible to have a complex gamescript (like Renewed City Growth for example), without noticeable delay and without pausing the game for calculations on average hardware? It this simply a matter of testing?
GameScripts are not particularly efficient. if you experience lags, you can reduce the number of "opcodes" that a script can use in any given tick. this will slow down the script, but improve the overall performance of the game.
-> Define average city starting population and setting limits for deviation.
sort of. it won't be pretty. aside from the already mentioned "start empty and generate the towns in the script", you can also go over the map on creation and cut down existing towns to a size you desire. note that this will probably leave towns with buildings that are larger than their final size would allow. a source code patch instead could directly tap into the "desired size" value that the create town function uses.
-> Set cargo acceptance for cities / industries and keep track of the delievered amounts to trigger changes
no-ish, setting acceptance is in the scope of a NewGRF. you can keep track of amounts delivered, but not affect the individual houses/industries
-> Change name / monthly production of towns/ industries when certain conditions (e.g. amount of specific cargo delievered) are met.
no, that is NewGRF territory.
Is there a way to initiate a period of rapid growth for cities? Like go from 250 to 1000 pop within one month? (given enough space).
yes, you can run the "grow town" routine as often as you like (up to once per tick). this basically works like the "grow town" button in the scenario editor.

note that newly created buildings will have 0 population until their construction is finished, so you won't see the effects immediately.
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

Ok, thanks!

Would a NEWGRF be suitable to create this modification package, or has NEWGRF some limitation in other areas, making a patch required?
Eddi
Tycoon
Tycoon
Posts: 8267
Joined: 17 Jan 2007 00:14

Re: Which method suits best to modify the economy of OTTD?

Post by Eddi »

NewGRFs, of course, have different kind of limitations. e.g. for industries, you can react to cargo delivered to this industry, but you cannot gather statistics about other industries, and only very limited statistics about the nearby town.

for houses, you can in a limited way prevent town growth, by denying every house to be built under certain conditions, but you cannot accelerate growth in any way. also, excessively preventing growth this way can cause the town to build extensive road networks with no houses nearby, especially if the town is a "city"
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

Are the properties in NML-Industries (prod_multiplier for example) static, e.g. can only be set for the game and not changed dynamically via NML while the game is running?
Eddi
Tycoon
Tycoon
Posts: 8267
Joined: 17 Jan 2007 00:14

Re: Which method suits best to modify the economy of OTTD?

Post by Eddi »

almost all properties have a matching callback that can be used to dynamically change them.

this may in some cases have serious performance impact
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

That what I thought while reading the specs. But now I am confused. Doens't that mean, that I can change the production value of an industy via NML while the game is running?

If yes, where would I define the conditions to trigger this? Or doesn't it work this way?
Update: Ok I've found the callback tutorial. I still dont know where the callbacks are triggered. Do they work light Event/Listener from other programming languages?
User avatar
andythenorth
Tycoon
Tycoon
Posts: 5658
Joined: 31 Mar 2007 14:23
Location: Lost in Music

Re: Which method suits best to modify the economy of OTTD?

Post by andythenorth »

Callbacks are triggered by events, for example:
- the game loop on some frequency (n ticks, monthly etc)
- game event (e.g. cargo is delivered to the industry)

You handle the cb and return result(s). So yes, it's basically an event/listener system. You can't have arbitrary functions subscribing to arbitrary events though, you need a single chain handling each callback ;)

w.r.t what Eddi said...
For vehicles it's true that many of the static properties have callback equivalents.
For industries (and industry tiles) the static properties mostly *don't* have a direct cb equivalent that can be changed at any time. Some properties however can be modified when the industry is constructed.
The production can be modified using the production cb, and/or by modifying the production multiplier on the monthly or random production change cb. There is spaghetti there for historical reasons. ;)
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

Ive made some progress now (my custom NEWGRF is accepted and works [just disabled all industries]) and now I am stuck again.
Looking throught almost the whole docs and also http://pythonhosted.org/nml did not help


What is the equivalent of IF in an nml file? (The docs say, that IF is not like IF in other programming languages)
How would I execute a simple expression?

I just want to check for a condition and react to it, I've been programming for 4+ years know, but I dont know how to do it in NML :(
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Which method suits best to modify the economy of OTTD?

Post by Alberth »

May I recommend reading some prior art?

http://dev.openttdcoop.org/projects/ogf ... y/show/src
It's the source of opengfx+industries, a relatively simple industry newgrf.

Other options are FIRS and YETI.
Being a retired OpenTTD developer does not mean I know what I am doing.
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

Thanks, I was looking throught the list provided (open source grfs), and Ive been digging through FIRS, but somehow they make use of python and transform the script later?
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Which method suits best to modify the economy of OTTD?

Post by Alberth »

Yeah, pretty much all NewGRF projects use a pre-processor of some kind to generate an nml source first, to reduce the amount of programming you need to do.
andythenorth (the author of FIRS) prefers Python, most other projects use CPP (C pre-processor). Some use m4, but not many, I think.

A general strategy to get around that is to checkout the project, and build it at your system. Usually somewhere in the process a NML file is written with everything in it.
Being a retired OpenTTD developer does not mean I know what I am doing.
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

How would I send a debug message to the console or display a string?

I don't like asking for code, but lets look at this simple case:
For practice, I wanted to shutdown a coalmine, if the city nearby has less than 1500 inhabitants.

Code: Select all

 switch (FEAT_INDUSTRIES,PARENT,msg_switch,population)
 {
	0..1500: return CB_RESULT_IND_PROD_CLOSE;
 }
 
 
 item(FEAT_INDUSTRIES, INDUSTRYTYPE_COAL_MINE) 
{
 graphics {
  monthly_prod_change: msg_switch;
 }
}
Nothing happens.
What am I doing wrong? Do I need to fetch a specific instance of a coal mine?
User avatar
Sylf
President
President
Posts: 957
Joined: 23 Nov 2010 21:25
Location: ::1

Re: Which method suits best to modify the economy of OTTD?

Post by Sylf »

For some pure NML example, you can take a look at my Smaller Mines. You can see how to use code to close industries, conditional statements, etc.
Openend
Engineer
Engineer
Posts: 36
Joined: 18 Jun 2015 09:09

Re: Which method suits best to modify the economy of OTTD?

Post by Openend »

Thanks, looks like gold to me :)
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 44 guests