Looking for some advice on station layout callbacks

Discuss, get help with, or post new graphics for TTDPatch and OpenTTD, using the NewGRF system, here. Graphics for plain TTD also acceptable here.

Moderator: Graphics Moderators

Post Reply
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Looking for some advice on station layout callbacks

Post by Maedhros »

I'm currently coding a station which has various different layouts depending on where the platforms are. Outer platforms should have walls, while inner platforms shouldn't. To do this I've got 8 layouts (1 for each direction) - walls on the northern side, no walls, walls on the southern side, and walls on both sides. I'm choosing between them using callback 14, based on variable 49, with the following VarAction 2s:

Code: Select all

	1 * 1	02 04
		01 // ID
		85 // Use the lowest word
		49 // Variable 49
		0C 0F 00
		01
		04 80 00 00 00 00 // Return 04 (callback result) if this is the first platform from the end
		02 80 // Otherwise return 02 (callback result)
	
	1 * 1	02 04
		02 // ID
		85 // Use the lowest word
		49 // Use variable 49
		08 0F 00
		01
		00 80 00 00 00 00 // Return 00 (callback result) if this is the first platform
		01 00 // Otherwise move on
	
	1 * 1	02 04
		03 // ID
		89 // Use the whole dword of the variable
		49 // Use variable 49
		14 0F 00 00 00 // Varadjust (shift right by 20, & with F)
		01
		06 80 01 00 00 00 01 00 00 00 // Return 06 (callback result) if there is 1 platform
		02 00 // Otherwise move onto the next in the chain
This seems like a lot of work though and involves calculating variable 49 3 times, and I was wondering if there was an easier (or at least more elegant) way to do it. I was trying to find a way to do it with an Advanced VarAction2, but couldn't see how, so I ended up with this. If anyone has any ideas, they'd be gratefully received. :)
No-one's more important than the earthworm.
User avatar
OzTrans
Tycoon
Tycoon
Posts: 1714
Joined: 04 Mar 2005 01:07

Post by OzTrans »

I don't use var 49, but here is a simple way to achieve what you want ...

This is for CB 14 only (during construction) , for actual platform display, you do not need this code. For var 40/41 I only every check half bytes using shift num 00, 04, 08, 0C ... with AND-mask 0F.

Code: Select all

* right (south) platform
02 04 DC 81 40 0C 0F 01 X3 80 00 00 X4 80

* left (north) platform
02 04 DD 81 40 08 0F 01 X2 80 00 00 DC 00
						
* single platform
02 04 DE 81 40 14 0F 01 X1 80 01 01 DD 00

* CB 14
02 04 DF 81 0C 00 FF 01 DE 00 14 14 X0 00
X0 = go to action-02 for platform display
X1 = wall/fence on both sides
X2 = ... north only
X3 = ... south only
X4 = middle platform, no walls

Use var 41 instead of 40, if you want to combine your station with other tiles, I don't because its buggy (in TTDPatch) and I only want to know where to place fences in the entire station complex.
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

Thanks OzTrans, although this is pretty much exactly what I'm doing at the moment, especially as callback 14 is the one that's called when the station is drawn (24 is called when the station is being built). :)
No-one's more important than the earthworm.
User avatar
OzTrans
Tycoon
Tycoon
Posts: 1714
Joined: 04 Mar 2005 01:07

Post by OzTrans »

Maedhros wrote:... although this is pretty much exactly what I'm doing at the moment, especially as callback 14 is the one that's called when the station is drawn (24 is called when the station is being built).
I realised that my solution was pretty much the same, but you should not worry to much how many var-act-02s during CB 14 you are using, as they are only processed during construction (and station animation CB 140). BTW, you don't need CB 24 at all, unless you want to have existing stations (built with your set) look respectable if the player deactivates that set.
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

OzTransLtd wrote:I realised that my solution was pretty much the same, but you should not worry to much how many var-act-02s during CB 14 you are using, as they are only processed during construction (and station animation CB 140). BTW, you don't need CB 24 at all, unless you want to have existing stations (built with your set) look respectable if the player deactivates that set.
I don't know about TTDPatch, although I think it's the same, but in OpenTTD callback 14 is called every time the station is drawn. Callback 24 is used when the station is built, so it's also useful for static layouts that won't change based on the surroundings (as long as you don't have more than 4 of them - I once tried to return 0A, which led to both OpenTTD and TTDPatch thinking my station was an airport... :) ).
No-one's more important than the earthworm.
User avatar
OzTrans
Tycoon
Tycoon
Posts: 1714
Joined: 04 Mar 2005 01:07

Post by OzTrans »

... CB 14 vs CB 24 ...
I know station coding is a difficult task to start with ... I learnt it through the school of hard knocks ...

CB 24 can only return 0x00, 02, 04 and 06. It has very limited use. You should always use CB 14 together with station property 09 (station layout table). With CB 14 you can return unlimited (nearly) number of tile IDs or tile definitions. I use up to 850 of them for one single station per ID; just make sure property 09 is less than 64 kBytes.

Tile type, set through CB 24 and Tile ID (my name), set through CB 14, are 2 totally different things.

The Tile ID will give you an index into the station layout table (property 09), wheras the tile type will only allow you to build 'Default' station type layouts within narrow limits.

CB 14 gets only called when the player builds the station, and every time CB 140 (animation) gets triggered. To display the station, you don't need CB 14. The 'current' tile ID (and the tile type) is burnt into every station tile and used to get to the real sprites via the station layout table (property 09). Each entry in that table defines what real sprites are to be displayed. It is that table that gives you all the flexibility you want in station design.

EDIT :
... in OpenTTD callback 14 is called every time the station is drawn ...
That is probably a little bit excessive and burns CPU cycles unnecessarily. CB 14 needs to be called (for every tile in the station complex), only when the player builds/modifies station tiles; plus during CB 140 execution, so that the tile ID can be set in relation to the current animation frame.
User avatar
Maedhros
OpenTTD Developer
OpenTTD Developer
Posts: 603
Joined: 30 Mar 2006 18:24
Location: Durham, UK

Post by Maedhros »

OzTransLtd wrote:Tile type, set through CB 24 and Tile ID (my name), set through CB 14, are 2 totally different things.
No, they're exactly the same thing, but one is stored (callback 24), and one is dynamic (callback 14).
OzTransLtd wrote:The Tile ID will give you an index into the station layout table (property 09), wheras the tile type will only allow you to build 'Default' station type layouts within narrow limits.
Actually, they both give you an index into the station layout table, but the result of callback 24 (or property 0E) is the only one stored in the station structure when the station is built. This is the reason that callback 24 is limited to only 8 layouts, because you can't store more persistently.

Callback 14 is used every time the station is drawn (in TTDPatch as well, by the way - it's called as part of getnewstationsprite in statspri.asm), and this overrides the value stored in the station structure from when it was built. This is why you can use more than 8 layouts with this callback, as the results are never stored.
OzTransLtd wrote:That is probably a little bit excessive and burns CPU cycles unnecessarily. CB 14 needs to be called (for every tile in the station complex), only when the player builds/modifies station tiles; plus during CB 140 execution, so that the tile ID can be set in relation to the current animation frame.
It might be a bit excessive, but it does give you more flexibility - you can use a different sprite layout depending on the time of year, for example. :)
No-one's more important than the earthworm.
Post Reply

Return to “Graphics Development”

Who is online

Users browsing this forum: No registered users and 19 guests