// 'Global' procedures
// - BF: Get offset for desert/snow non-track ground sprites
// - BE: Get offset for desert/snow track ground sprites
// - BD: Get rail/monorail/maglev ground sprites

// - BC: Open/close train entry doors
// - BB: Show/hide snow on roofs
// - BA: Get offset for road crossing overlay
// - B9: Get offset for road crossing overlay - purchase list
// - B8: Get offset for tower crane animation

// - B7: Show/hide NW fence
// - B6: Show/hide NE fence
// - B5: Show/hide SE fence
// - B4: Show/hide SW fence

// - B3: Check if there is a parking tile to the south


// Procedure to get offset for desert/snow non-track ground sprites
	1 * 1	02 04 BF // ID
		81 42 // terrain and track type
		00 FF
		02
		\wx8239 01 01 // if desert: 11C6 - 0F8D = 239
		\wx8239 04 04 // if snow
		\wx8000 // else

// Procedure to get offset for desert/snow track ground sprites
	1 * 1	02 04 BE // ID
		81 42 // terrain and track type
		00 FF
		02
		\wx801A 01 01 // if desert: 040E - 03F4 = 1A
		\wx801A 04 04 // if snow
		\wx8000 // else

// Procedure to get offset for rail/monorail/maglev ground sprites
	1 * 1	02 04 01 // ID
		81 7F 19 // read parameter 19
		00 FF
		01
		\wx8000 01 01 // monorail has been replaced
		\wx8052 // monorail offset 82

	1 * 1	02 04 02 // ID
		81 7F 1A // read parameter 1A
		00 FF
		01
		\wx8000 01 01 // maglev has been replaced
		\wx80A4 // maglev offset 164

	1 * 1	02 04 BD // ID
		81 42 // terrain and track type
		08 FF
		02
		\wx0001 02 02 // if monorail
		\wx0002 03 03 // if maglev
		\wx8000 // else


// Procedure to open/close train entry doors
// Used by train sheds, goods station A and valuables sheds/station
	// Test tile to south for buffer
	1 * 1	02 04 02 // ID
		85 68 01 // station info: check tile to S
		00 \wx0FFF // mask 12 bits for tile ID, type and direction
		02
		\wx8000 BUFFER_LOW 05 BUFFER_LOW 05 // tile is buffer - door open
		\wx8000 BUFFER_HIGH 05 BUFFER_HIGH 05 // tile is buffer - door open
		\wx8001 // else - door closed

	1 * 1	02 04 01 // ID
		81 45 // rail continuation info
		00 01 // use bit 0 - rail to south
		01
		\wx8000 01 01 // there is a connecting rail - door open
		\wx0002 // else

	1 * 1	02 04 BC // ID
		81 44 // PBS status
		01 01 // use bit 1
		01
		\wx0001 01 01 // station is reserved or PBS is off
		\wx8001 // else - door closed


	// Procedure to show/hide snow on roofs
	1 * 1	02 04 BB // ID
		81
		42 // terrain and track type
		00 FF
		01
		\wx8001 04 04 // show snow
		\wx8000 // no snow


// Procedure to get offset for road crossing overlay
	// For monorail check if sprites have been replaced by old type rail grf
	1 * 1	02 04 06 // ID
		81 7F 14 // read parameter 14
		00 FF
		01
		\wx8002 02 02 // monorail
		\wx8000 // normal rail

	// For maglev check if sprites have been replaced by old type rail grf
	1 * 1	02 04 07 // ID
		81 7F 15 // read parameter 15
		00 FF
		01
		\wx8004 03 03 // maglev
		\wx8000 // normal rail

	// Test for track type
	1 * 1	02 04 05 // ID
		81 42 // terrain and track type
		08 FF
		02
		\wx0006 02 02 // monorail
		\wx0007 03 03 // maglev
		\wx8000 // normal rail

	// Show monorail overlay for fine scale railtypes grf (pb_trax, xUSSR)
	1 * 1	02 04 BA // ID
		81 7F 16 // read parameter 16
		00 FF
		01
		\wx8002 01 01 // fine scale rail set loaded - monorail
		\wx0005


// Procedure to get offset for road crossing overlay - purchase list
	// We can't test for rail type in the purchase window so use the
	// maglev overlay as it is the only one that looks reasonable with all rail types.
	// Note, OpenTTD feature request FS#2091.
	// Show monorail overlay for fine scale railtypes grf (pb_trax, xUSSR)
	1 * 1	02 04 B9 // ID
		81 7F 16 // read parameter 16
		00 FF
		01
		\wx8002 01 01 // fine scale rail set loaded - monorail
		\wx8004 // maglev


// Procedure to get sprite offset for tower crane animation
	// Calculate frame for reverse movement
	1 * 1	02 04 02 // ID
		81 1A // all bits on
		20 \b16 // last frame (19) - 3 static frames
		\2-
		4A // animation frame
		00 FF
		00 // return {16 - frame}
		\wx8000

	// Set frame for forward movement
	1 * 1	02 04 01 // ID
		81 4A // animation frame
		00 FF
		00 // return frame
		\wx8000

	// Select movement direction
	1 * 1	02 04 B8 // ID
		81 4A // animation frame
		00 FF
		03
		\wx0001 \b0 \b6 // frames 0 to 6
		\wx8006 \b7 \b9 // frames 7 to 9 (last position)
		\wx0002 \b10 \b16 // frames 10 to 16
		\wx8000 // frames 17 to 19 (first position)


// Procedures to show/hide fences
	// Check tile to NW (for x orientation)
	1 * 1	02 04 B7 // ID
		85 68 F0 // station info: check tile on adjacent platform to N
		08 \wx0007 // mask 3 bits for defined by GRF and current station
		01
		\wx8000 \wx0004 \wx0006 // tile is part of this station
		\wx8001 // not this station - show fence

	// Check tile to NE (for x orientation)
	1 * 1	02 04 B6 // ID
		85 68 0F // station info: check tile along platform to N
		08 \wx0007 // mask 3 bits for defined by GRF and current station
		01
		\wx8000 \wx0004 \wx0006 // tile is part of this station
		\wx8001 // not this station - show fence

	// Check tile to SE (for x orientation)
	1 * 1	02 04 B5 // ID
		85 68 10 // station info: check tile on adjacent platform to S
		08 \wx0007 // mask 3 bits for defined by GRF and current station
		01
		\wx8000 \wx0004 \wx0006 // tile is part of this station
		\wx8001 // not this station - show fence

	// Check tile to SW (for x orientation)
	1 * 1	02 04 B4 // ID
		85 68 01 // station info: check tile along platform to S
		08 \wx0007 // mask 3 bits for defined by GRF and current station
		01
		\wx8000 \wx0004 \wx0006 // tile is part of this station
		\wx8001 // not this station - show fence


// Procedure to check if there is a parking tile to the south
// Used to hide windows and doors on freight station buildings
	1 * 1	02 04 B3 // ID
		85 68 10 // station info: check tile on adjacent platform to S
		00 \wx0FFF // mask 12 bits for tile ID, type and direction
		01
		\wx8000 VALUABLES_PARKING 05 VALUABLES_PARKING 05 // hide windows and doors
		\wx8001 // show windows and doors
