// The following animation callback code is used for all
// cranes in anim_cranes.nfo
// -----------------------------------------------------

// frame     : current frame of animation (0 to 225)
// a_frame   : adjusted frame number for direction change (0 to 112, 112 to 0)
// t_frame   : frame number for tile, crane is hidden in first and last positions

// Determine register values for sprite layout
// -------------------------------------------
	// Get animation frame of south tile
	1 * 1	02 04 0A // ID
		81 41 // platform info and relative position
		24 0F // position along platform from south
		\2rst
		7B 66 // animation frame of nearby tile using position from south as parameter
		00 FF
		00 // return frame
		\wx8000

	// Calculate a_frame for reverse movement
	1 * 1	02 04 06 // ID
		81 1A // all bits on
		20 \b225 // max_frame
		\2-
		7D 25 // read register for frame of south tile
		00 FF
		00 // return a_frame = max_frame - frame
		\wx8000

	// Return a_frame for forward movement
	1 * 1	02 04 07 // ID
		81
		7D 25 // read register for frame of south tile
		00 FF
		00 // return a_frame = frame
		\wx8000

	// Select movement direction
	1 * 1	02 04 40 // ID
		81 7E 0A // animation frame - south tile
		20 FF
		\2sto 1A
		00 25 // store in register to prevent multiple calls to frame of nearby tile
		01
		\wx0007 \b0 \b112 // frames 0 to 112
		\wx0006 // frames 113 to max


// Callback 141 - Next animation frame
// --------------------------------
	// Calculate new frame for reverse movement - south tile
	1 * 1	02 04 08 // ID
		81 1A // all bits on
		20 \b225 // max_frame
		\2-
		4A // animation frame
		00 FF
		00 // return new_frame = max_frame - frame
		\wx8000

	// Animation frame control - determine point to reverse direction
	// {max_frame = (plat_length - 1) * 16}
	1 * 1	02 04 09 // ID
		81 41 // platform info and relative position
		30 0F // platform length
		\2< // result = min(val1, val2)
		1A // all bits on
		20 \b8 // result is capped at max 8
		\2-
		1A // all bits on
		20 \b1 // val - 1
		\2<<
		1A // all bits on
		20 \b4 // val * 16 - gives max frame for this platform length
		\2-
		4A // animation frame
		00 FF // subtract animation frame from max frame
		01
		\wx0008 00 00 // if animation frame = max frame, set new frame
		\wx80FE // continue animation

	// Next animation frame
	1 * 1	02 04 41 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0009 00 00 // if most southerly tile
		\wx80FE // continue


	1 * 1	00 04 09 01 CRANE_ON_RAILS
		08 "ISRI"

		// Sprite layout
		1A 02
			// 00 (x)
			44
			\dx000003F4 \wx0000 // ground sprite - track
			\dx8000042D \wx0000 // back platform
			00 00 00 // x/y/z offset
			10 05 03 // x/y/z extent
			\dx8000042E \wx0000 // front platform
			00 0B 00 // x/y/z offset
			10 05 03 // x/y/z extent
			\dx80000431 \wx0013 // crane back
			00 00 03 // x/y/z offset
			0C 05 28 // x/y/z extent
			01 06 11 12 // registers - show, sprite offset, x, y
			\dx8000043C \wx0013 // crane front
			00 0B 03 // x/y/z offset
			0C 05 28 // x/y/z extent
			01 06 11 12 // registers - show, sprite offset, x, y

			// 01 (y)
			44
			\dx000003F3 \wx0000 // ground sprite - track
			\dx8000042F \wx0000 // back platform
			00 00 00 // x/y/z offset
			05 10 03 // x/y/z extent
			\dx80000430 \wx0000 // front platform
			0B 00 00 // x/y/z offset
			05 10 03 // x/y/z extent
			\dx80000447 \wx0013 // crane back
			00 00 03 // x/y/z offset
			05 0C 28 // x/y/z extent
			01 06 12 11 // registers - show, sprite offset, x, y
			\dx80000452 \wx0013 // crane front
			0B 00 03 // x/y/z offset
			05 0C 28 // x/y/z extent
			01 06 12 11 // registers - show, sprite offset, x, y

		0B 06 // use callback to determine layout and animation frame

		0D 01 // disallow platform length 1
		14 FF // wires (none)
		11 00 // pylon placement (none)

		16 \wx01E1 // animation info 226 frames looping
		17 04      // animation speed 2^4 ticks per frame (~400ms)
		18 \wx0018 // animation triggers - train enters (0x08), train leaves (0x10)


// Callback 140 - Animation control
// --------------------------------
	// Animation start/stop control
	1 * 1	02 04 12 // ID
		81 18 // extra callback info - reason for call
		00 FF
		02
		\wx80FE 03 03 // vehicle enters station - start
		\wx80FF 04 04 // vehicle leaves station - stop
		\wx80FD // do nothing


// Assign register values for sprite layout
// ----------------------------------------

// Frames
// frame:       0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30...
// t_frame t0: 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//         t1:        0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//         t2:                                                        0  1  2  3  4  5  6  7  8  9 10 11 12...

// t1 sprite offset:     0  0  1  1  2  2  3  3  4  4  5  5  5  5  5  5  6  6  7  7  8  8  9  9 10 10
// t1 visible:        0  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  0
// t1 x offset:          5  4  5  4  5  4  5  4  5  4  5  4  3  2  1  0  1  0  1  0  1  0  1  0  1  0


	// Calculate t_frame {t_frame = a_frame + 14 - (p_pos * 16)}
	1 * 1	02 04 10 // ID
		81 7E 40 // get a_frame
		20 FF
		\2+
		1A // all bits on
		20 \b14 // val + 14
		\2-
		41 // platform info, get position along platform (from S)
		20 F0 // shift 0 and mask 4 bits to give p_pos * 16
		\2> // result = max(val1, val2)
		1A // all bits on
		20 \b0 // result is capped at min 0
		\2< // result = min(val1, val2)
		1A // all bits on
		00 \b27 // result is capped at max 27
		00 // return t_frame
		\wx8000

	// Select sprite offset
	1 * 1	02 04 61 // ID
		81 7E 10 // tile frame (0 to 12)
		20 FF
		\2sto 1A
		00 21 // store in register to prevent calculating t_frame multiple times
		0A
		\wx8000 \b1 \b2
		\wx8001 \b3 \b4
		\wx8002 \b5 \b6
		\wx8003 \b7 \b8
		\wx8004 \b9 \b10
		\wx8006 \b17 \b18
		\wx8007 \b19 \b20
		\wx8008 \b21 \b22
		\wx8009 \b23 \b24
		\wx800A \b25 \b26
		\wx8005

	// Check for animated tile
	1 * 1	02 04 60 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0061 00 07 // if valid animation tile
		\wx8000 // else

	// Hide sprite
	1 * 1	02 04 63 // ID
		81
		7D 21 // read register for t_frame
		00 FF
		01
		\wx8001 \b1 \b26 // t_frame 1 to 26 - show
		\wx8000 // hide

	// Check for animated tile
	1 * 1	02 04 62 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0063 00 07 // if valid animation tile
		\wx8000 // hide

	// Select sprite position x offset
	1 * 1	02 04 65 // ID
		81
		7D 21 // read register for t_frame
		00 FF
		\b20
		\wx8005 \b1 \b1
		\wx8004 \b2 \b2
		\wx8005 \b3 \b3
		\wx8004 \b4 \b4
		\wx8005 \b5 \b5
		\wx8004 \b6 \b6
		\wx8005 \b7 \b7
		\wx8004 \b8 \b8
		\wx8005 \b9 \b9
		\wx8004 \b10 \b10
		\wx8005 \b11 \b11
		\wx8004 \b12 \b12
		\wx8003 \b13 \b13
		\wx8002 \b14 \b14
		\wx8001 \b15 \b15
		\wx8001 \b17 \b17
		\wx8001 \b19 \b19
		\wx8001 \b21 \b21
		\wx8001 \b23 \b23
		\wx8001 \b25 \b25
		\wx8000

	// Check for animated tile
	1 * 1	02 04 64 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0065 00 07 // if valid animation tile
		\wx8000 // else

//@@WARNING DISABLE 100
//@@WARNING DISABLE 170

	1 * 1	02 04 15 // ID
		85
		7E 60 // get crane sprite offset
		20 \wx7FFF
		\2sto 1A
		20 \wx0006 // store in register - sprite offset
		\2rst
		7E 62 // get crane show/hide
		20 \wx7FFF
		\2sto 1A
		20 \wx0001 // store in register - show crane flag
		\2rst
		7E 64 // get crane position offset
		20 \wx7FFF
		\2sto 1A
		20 \wx0011 // store in register - move x offset
		\2rst
		1A // all bits on
		20 \w0 // val = 0
		\2sto 1A
		00 \wx0012 // store in register - move y offset
		01
		\wx00CC \w0 \w0
		\wx00CC

	// Purchase
	1 * 1	02 04 16 // ID
		85
		1A // all bits on
		20 \w5 // val = 5
		\2sto 1A
		20 \wx0006 // store in register - sprite offset
		\2rst
		1A // all bits on
		20 \w1 // val = 1
		\2sto 1A
		20 \wx0001 // store in register - show crane flag
		\2rst
		1A // all bits on
		20 \w2 // val = 2
		\2sto 1A
		20 \wx0011 // store in register - move x offset
		\2rst
		1A // all bits on
		20 \w0 // val = 0
		\2sto 1A
		00 \wx0012 // store in register - move y offset
		01
		\wx00CC \w0 \w0
		\wx00CC

//@@WARNING ENABLE 170
//@@WARNING ENABLE 100

	1 * 1	02 04 01 // ID
		85 0C // callback
		00 \wxFFFF
		03
		\wx8000 \wx0014 \wx0014 // sprite layout
		\wx0012 \wx0140 \wx0140 // animation control
		\wx0041 \wx0141 \wx0141 // animation frame
		\wx0015 // real sprites

	// For purchase list
	1 * 1	02 04 02 // ID
		85 0C // callback
		00 \wxFFFF
		01
		\wx8000 \wx0014 \wx0014 // sprite layout
		\wx0016 // real sprites

	1 * 1	03 04
		01 CRANE_ON_RAILS
		01
		FF \wx0002 // purchase
		\wx0001


	1 * 1	00 04 09 01 OVERHEAD_CRANE
		08 "ISRI"

		// Sprite layout
		1A 02
			// 00 (x)
			43
			\dx000003F4 \wx0000 // ground sprite - track
			\dx8000042D \wx0000 // back platform
			00 00 00 // x/y/z offset
			10 05 1A // x/y/z extent
			\dx80000431 \wx0013 // crane
			00 02 19 // x/y/z offset
			09 0B 04 // x/y/z extent
			01 06 11 12 // registers - show, sprite offset, x, y
			\dx8000042E \wx0000 // front platform
			00 0B 00 // x/y/z offset
			10 05 1A // x/y/z extent

			// 01 (y)
			43
			\dx000003F3 \wx0000 // ground sprite - track
			\dx8000042F \wx0000 // back platform
			00 00 00 // x/y/z offset
			05 10 1A // x/y/z extent
			\dx80000438 \wx0013 // crane
			02 00 19 // x/y/z offset
			0B 09 04 // x/y/z extent
			01 06 12 11 // registers - show, sprite offset, x, y
			\dx80000430 \wx0000 // front platform
			0B 00 00 // x/y/z offset
			05 10 1A // x/y/z extent

		// Callback flags
		0B 06 // use callback to determine layout and animation frame

		0D 01 // disallow platform length 1
		14 FF // wires (none)
		11 00 // pylon placement (none)

		16 \wx01E1 // animation info 226 frames looping
		17 03      // animation speed 2^3 ticks per frame (~200ms)
		18 \wx0018 // animation triggers - train enters (0x08), train leaves (0x10)


	1 * 1	01 04 01 12
	1 images/newfixtures2.pcx    18  508  09   44  42  -31  -24  // 042D x, back platform
	1 images/newfixtures2.pcx    71  508  09   44  42  -31  -24  // 042E x, front platform
	1 images/newfixtures2.pcx   161  508  09   44  42   -9  -24  // 042F y, back platform
	1 images/newfixtures2.pcx   209  508  09   44  42   -9  -24  // 0430 y, front platform

	1 images/newfixtures2.pcx   146  508  01   21   6   -1    3  // 0431 x, crane 1
	1 images/newfixtures2.pcx   142  508  01   21  10   -1    1  // 0432 x, crane 2
	1 images/newfixtures2.pcx   138  508  01   21  14   -1   -1  // 0433 x, crane 3
	1 images/newfixtures2.pcx   118  508  01   21  34  -17   -3  // 0434 x, crane 4 - 8 (6 - centre)
	1 images/newfixtures2.pcx   118  508  01   21  28  -13   -5  // 0435 x, crane 9
	1 images/newfixtures2.pcx   118  508  01   21  24   -9   -7  // 0436 x, crane 10
	1 images/newfixtures2.pcx   118  508  01   21  20   -5   -9  // 0437 x, crane 11

	1 images/newfixtures2.pcx   258  508  01   21   6   -3    3  // 0438 y, crane 1
	1 images/newfixtures2.pcx   258  508  01   21  10   -6    1  // 0439 y, crane 2
	1 images/newfixtures2.pcx   258  508  01   21  14  -11   -1  // 043A y, crane 3
	1 images/newfixtures2.pcx   258  508  01   21  34  -15   -3  // 043B y, crane 4 - 8 (6 - centre)
	1 images/newfixtures2.pcx   264  508  01   21  28  -13   -5  // 043C y, crane 9
	1 images/newfixtures2.pcx   268  508  01   21  24  -13   -7  // 043D y, crane 10
	1 images/newfixtures2.pcx   272  508  01   21  20  -13   -9  // 043E y, crane 11

	1 * 1	02 04 00 // ID
		00 01
		\w0


// Callback 140 - Animation control
// --------------------------------
	// Animation start/stop control
	1 * 1	02 04 12 // ID
		81 18 // extra callback info - reason for call
		00 FF
		02
		\wx80FE 03 03 // vehicle enters station - start
		\wx80FF 04 04 // vehicle leaves station - stop
		\wx80FD // do nothing


// Assign register values for sprite layout
// ----------------------------------------

// Frames
// frame:       0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// t_frame t0: 12 13 14 15 16 17 18 19 20 21 22 23
//         t1:              0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
//         t2:                                                              0  1  2  3  4  5  6  7  8  9 10 ...

// t1 sprite offset:           0  0  1  1  2  2  3  3  3  3  3  3  3  3  3  3  4  4  5  5  6  6
// t1 visible:              0  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  0
// t1 x offset:                9  8  9  8  9  8  9  8  7  6  5  4  3  2  1  0  1  0  1  0  1  0


	// Calculate t_frame {t_frame = a_frame + 12 - (p_pos * 16)}
	1 * 1	02 04 10 // ID
		81 7E 40 // get a_frame
		20 FF
		\2+
		1A // all bits on
		20 \b12 // val + 12
		\2-
		41 // platform info, get position along platform (from S)
		20 F0 // shift 0 and mask 4 bits to give p_pos * 16
		\2> // result = max(val1, val2)
		1A // all bits on
		20 \b0 // result is capped at min 0
		\2< // result = min(val1, val2)
		1A // all bits on
		00 \b23 // result is capped at max 23
		00 // return t_frame
		\wx8000

	// Select sprite offset
	1 * 1	02 04 61 // ID
		81 7E 10 // tile frame (0 to 12)
		20 FF
		\2sto 1A
		00 21 // store in register to prevent calculating t_frame multiple times
		06
		\wx8000 \b1 \b2
		\wx8001 \b3 \b4
		\wx8002 \b5 \b6
		\wx8004 \b17 \b18
		\wx8005 \b19 \b20
		\wx8006 \b21 \b22
		\wx8003

	// Check for animated tile
	1 * 1	02 04 60 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0061 00 07 // if valid animation tile
		\wx8000 // else

	// Hide sprite
	1 * 1	02 04 63 // ID
		81
		7D 21 // read register for t_frame
		00 FF
		01
		\wx8001 \b1 \b22 // t_frame 1 to 22 - show
		\wx8000 // hide

	// Check for animated tile
	1 * 1	02 04 62 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0063 00 07 // if valid animation tile
		\wx8000 // hide

	// Select sprite position x offset
	1 * 1	02 04 65 // ID
		81
		7D 21 // read register for t_frame
		00 FF
		\b18
		\wx8009 \b1 \b1
		\wx8008 \b2 \b2
		\wx8009 \b3 \b3
		\wx8008 \b4 \b4
		\wx8009 \b5 \b5
		\wx8008 \b6 \b6
		\wx8009 \b7 \b7
		\wx8008 \b8 \b8
		\wx8007 \b9 \b9
		\wx8006 \b10 \b10
		\wx8005 \b11 \b11
		\wx8004 \b12 \b12
		\wx8003 \b13 \b13
		\wx8002 \b14 \b14
		\wx8001 \b15 \b15
		\wx8001 \b17 \b17
		\wx8001 \b19 \b19
		\wx8001 \b21 \b21
		\wx8000

	// Check for animated tile
	1 * 1	02 04 64 // ID
		81 41 // platform info and relative position
		04 0F // position along platform from south
		01
		\wx0065 00 07 // if valid animation tile
		\wx8000 // else

//@@WARNING DISABLE 100
//@@WARNING DISABLE 170

	1 * 1	02 04 15 // ID
		85
		7E 60 // get crane sprite offset
		20 \wx7FFF
		\2sto 1A
		20 \wx0006 // store in register - sprite offset
		\2rst
		7E 62 // get crane show/hide
		20 \wx7FFF
		\2sto 1A
		20 \wx0001 // store in register - show crane flag
		\2rst
		7E 64 // get crane position offset
		20 \wx7FFF
		\2sto 1A
		20 \wx0011 // store in register - move x offset
		\2rst
		1A // all bits on
		20 \w0 // val = 0
		\2sto 1A
		00 \wx0012 // store in register - move y offset
		01
		\wx0000 \w0 \w0
		\wx0000

	// Purchase
	1 * 1	02 04 16 // ID
		85
		1A // all bits on
		20 \w3 // val = 3
		\2sto 1A
		20 \wx0006 // store in register - sprite offset
		\2rst
		1A // all bits on
		20 \w1 // val = 1
		\2sto 1A
		20 \wx0001 // store in register - show crane flag
		\2rst
		1A // all bits on
		20 \w4 // val = 4
		\2sto 1A
		20 \wx0011 // store in register - move x offset
		\2rst
		1A // all bits on
		20 \w0 // val = 0
		\2sto 1A
		00 \wx0012 // store in register - move y offset
		01
		\wx0000 \w0 \w0
		\wx0000

//@@WARNING ENABLE 170
//@@WARNING ENABLE 100

	1 * 1	02 04 01 // ID
		85 0C // callback
		00 \wxFFFF
		03
		\wx8000 \wx0014 \wx0014 // sprite layout
		\wx0012 \wx0140 \wx0140 // animation control
		\wx0041 \wx0141 \wx0141 // animation frame
		\wx0015 // real sprites

	// For purchase list
	1 * 1	02 04 02 // ID
		85 0C // callback
		00 \wxFFFF
		01
		\wx8000 \wx0014 \wx0014 // sprite layout
		\wx0016 // real sprites

	1 * 1	03 04
		01 OVERHEAD_CRANE
		01
		FF \wx0002 // purchase
		\wx0001
