NML: Example for expressions and calculations in switch blocks

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

Post Reply
GojkoG
Engineer
Engineer
Posts: 14
Joined: 06 Dec 2017 18:45

NML: Example for expressions and calculations in switch blocks

Post by GojkoG »

I couldn’t find examples of how to make calculations in switch blocks so I want to show a working example of what is possible.
(I could only figure this out because I programmed in NFO first and learnt that expressions can be chained. The NML documentation could need some better examples.)

I also want to raise the question if there’s an easier way to do that. :-)

Code: Select all

switch( FEAT_OBJECTS, SELF, switch_AnhalterSW_sections, [	// the array bracket contains a chain of expressions
	// First I check if the 6 neighbor tiles in Y directions have the same object (id 0x09).
	// Every tile gets one bit in the result value (by using the bitmask)
	// (For example: "001011" if the next two and the fourth neighbor are the same object.)
	STORE_TEMP(
		((nearby_tile_object_type(0, 1) == 0x09)? bitmask(0):0)		// summing the results of
		+ ((nearby_tile_object_type(0, 2) == 0x09)? bitmask(1):0)	// multiple "?: if expressions"
		+ ((nearby_tile_object_type(0, 3) == 0x09)? bitmask(2):0)	// to one value
		+ ((nearby_tile_object_type(0, 4) == 0x09)? bitmask(3):0)
		+ ((nearby_tile_object_type(0, 5) == 0x09)? bitmask(4):0)
		+ ((nearby_tile_object_type(0, 6) == 0x09)? bitmask(5):0), 0 ),	//and saving it to temp var 0
		
	// Taking temp var 0 and checking:
	// if exact the next 1 or 3 or 5 neighbors are the same object (id 0x09)
	// then save "2" (= section 2) to var 1, else 0
	STORE_TEMP(
		(getbits(LOAD_TEMP(0), 0, 2) == bitmask( 0 )		// the 2 first bits of temp var 0: check, if only the first one is set
		|| getbits(LOAD_TEMP(0), 0, 4) == bitmask( 0,1,2 )	// OR the 4 first bits ... OR the 6 first bits ...
		|| getbits(LOAD_TEMP(0), 0, 6) == bitmask( 0,1,2,3,4 ))? 2 : 0,  1 ),
		
	// if exact the next 2 or 4 or 6 neighbors are the same object (id 0x09)
	// then save "3" (= section 3) to var 1, else take var 1’s former value by loading it
	STORE_TEMP(
		(getbits(LOAD_TEMP(0), 0, 3) == bitmask( 0,1 )
		|| getbits(LOAD_TEMP(0), 0, 5) == bitmask( 0,1,2,3 )
		|| getbits(LOAD_TEMP(0), 0, 7) == bitmask( 0,1,2,3,4,5 ))? 3 : LOAD_TEMP(1),  1 ),
		
	// if the next neighbor is, but the other side isn’t the same object (id 0x09)
	// then save "4" to var 1, else keep var 1’s value by loading it
	STORE_TEMP(
		(nearby_tile_object_type(0, -1) != 0x09
		&& nearby_tile_object_type(0, 1) == 0x09)? 4 : LOAD_TEMP(1),  1 ),
		
	// load the now calculated value of temp var 1 and finish the expression block [].
	// (The last value will be evaluated by the switch, like if you use the result of a function call)
	LOAD_TEMP(1)
] ){
	4: sprtLyt_Anhalter_facingSW_sec4;
	3: sprtLyt_Anhalter_facingSW_sec3;
	2: sprtLyt_Anhalter_facingSW_sec2;
	sprtLyt_Anhalter_facingSW_sec1;		// default: section 1
}
What does it do? It dynamically pics the right spritelayout for three combinations of my Anhalter Bahnhof graphics (4, 6 or 8 tiles wide) depending on the neighbor tiles in Y direction, without having to put it together out of four separat objects.
Anhalter-Bahnhof--1obj-vs-4obj.png
Anhalter-Bahnhof--1obj-vs-4obj.png (153.94 KiB) Viewed 2876 times
But again: Is there an easier way to check multiple tiles?
temporal8
Route Supervisor
Route Supervisor
Posts: 429
Joined: 17 May 2019 14:15

Re: NML: Example for expressions and calculations in switch blocks

Post by temporal8 »

HI, im not a coder but take a look to improved_town_layouts by 2TallTyler, there are some switches that may be useful or guide you.

https://github.com/2TallTyler/improved_ ... master/src
Real Projects 32bpp releases:

Real Semi Trucks 32bpp: viewtopic.php?t=90996
Real Houses Eyecandy Objects 32bp: viewtopic.php?t=90767
Real Ships 32bpp: viewtopic.php?t=90733
Real Industries 32bpp: viewtopic.php?t=90183
Real Houses Townset 32bp: viewtopic.php?p=1254605
Real Stations 32bpp: viewtopic.php?p=1255635
Real Cars 32bpp: viewtopic.php?p=1249244
Real Vehicles 32bpp: viewtopic.php?p=1253581
Real Bus 32bpp: viewtopic.php?p=1249245
Real Trucks 32bpp: viewtopic.php?p=1254468
Real Parks 32bpp: viewtopic.php?p=1250255
Argentina World Cup 2022 Champions Bus 32bpp viewtopic.php?p=1257026
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: No registered users and 3 guests