#ifdef WITH_MULTIWORLD
#set // directive to create patch set as APPLY_MULTIWORLD()


// relocates script code at 0x9386b0 to 0xfd8150 (after screen shake script)
// * clear some memory (multiworld-related)
// * set is_in_game=true (original code)
@0x9386b0
07 50 81 15 // call new code at 0xfd8150

@0xfd8150
18 1d 03 b0 // clear multiworld expected index at 7e2575+0..1
18 24 03 b0 // clear multiworld item id at 7e2575+7..8
10 1f 03 b0 // clear multiworld transaction pending at 7e2575+2 (old/current)
//10 26 03 b0 // clear multiworld transaction pending at 7e2575+9 (new/future)
0c 9e 02 b1 // $22ab |= 0x40 (is_in_game = true)
00 // end
@

// we hook into c084a3 after reading controller inputs
// we put our code to schedule script at fd1000, remote effect (death link) at fd1100
// we put our script at fd8000
// death link script at fd8100
// the code checks 7e2577 != 0 to start the script and clears that after
// the script uses 7e257c..d as item id, 7e257a..b as item amount and
// 7e2578..9 as item index (to resync) and clears item id after being done

@0xc084a3
5c 00 10 fd     //JML fd1000
ea ea           //NOP NOP

@0xfd1000
// what we removed above
2d 04 01        //AND $0104
8d 04 01        //STA $0104

// if $7e2577 (8bit) "start transaction" == 0 goto RETURN (JML) below
af 77 25 7e     //LDA (long) $7e2577
29 ff 00        //AND #$00ff
d0 0e           //BNE below jml (skips 14 bytes if not 0)
// if $a07ffe (16bit) "remote effect" != 0 then JML to fd1100
af fe 7f a0     //LDA (long) $a07ffe
f0 04           //BEQ to second jml (skips 4 bytes if effect = 0)
5c 00 11 fd     //JML $fd1100 (to checking remote effects)
5c a9 84 c0     //JML $c084a9 (back to main loop)

// if $86 (next active script list index) > 0 goto RETURN (JML) below
a5 86           //LDA $86
f0 04           //BEQ below jml (skips 4 bytes if 0)
//c9 03 00        //CMP #$0003 (if A-3<0 then $86<2, so <=1 scripts active)
//30 04           //BMI below jml (skips 4 bytes)
// TODO: allow some scripts (like market timer check) in slot 0
5c a9 84 c0     //JML $c084a9

// NOTE: in-animation check below is probably not needed anymore, but testing was done with it enabled
// if in animation ($7e22eb & 0x20) is set, RETURN (JML) below
af eb 22 7e     //LDA (long) $7e22eb
29 20 00        //AND #$0020
f0 04           //BEQ below jml (skips 4 bytes if 0)
5c a9 84 c0     //JML $c084a9

// if screen brightness fading ($7e0106 != 0x000f), RETURN (JML) below
af 06 01 7e     //LDA (long) $7e0106
c9 0f 00        //CMP #$000f
f0 04           //BEQ below jml (skips 4 bytes if A-#$0f==#$00)
5c a9 84 c0     //JML $c084a9

// write script address for fd8000 to $26..$28: 00 80 15
// fd8000 - c00000 + 800000 - 928000 = 2b0000. >>=1: 158000
9c 26 00        //STZ $26
a9 80 15        //LDA #$1580
8d 27 00        //STA $27

// call "add script"
22 18 cf 8c     //JSL $8ccf18

// update active script pointer(s), as done in 8cce87
8a              //TXA: 16bit script list pointer now in A
a6 86           //LDX $86: get next active-script-list index
9f 28 2f 7e     //STA $7e2f28,x: store new script there
e8 e8           //INX INX: increase next active-script-list index
7b              //TDC: clear A
9f 28 2f 7e     //STA $7e2f28,x: store 0 to next active-script-list index
86 86           //STX $86: store back next active-script-list index

// clear our "run once" flag in 7e2577
e2 20           //SEP #$20: 8bit mode
8f 77 25 7e     //STA (long) $7e2577
e2 00           //SEP #$00: 16bit mode

// return
5c a9 84 c0     //JML $c084a9 back to main loop
@

@0xfd1100
// check for the possibility of scheduling effects (death link)
// basically the same code as above but schedules different scripts at the end
// also cleaned up - see above for full commented code

// if $86 (next active script list index) > 0 goto RETURN (JML) below
a5 86           //LDA $86
f0 04           //BEQ below jml (skips 4 bytes if 0)
// TODO: allow some scripts (like market timer check) in slot 0
5c a9 84 c0     //JML $c084a9

// NOTE: in-animation check below is probably not needed anymore, but testing was done with it enabled
// if in animation ($7e22eb & 0x20) is set, RETURN (JML) below
af eb 22 7e     //LDA (long) $7e22eb
29 20 00        //AND #$0020
f0 04           //BEQ below jml (skips 4 bytes if 0)
5c a9 84 c0     //JML $c084a9

// if screen brightness fading ($7e0106 != 0x000f), RETURN (JML) below
af 06 01 7e     //LDA (long) $7e0106
c9 0f 00        //CMP #$000f
f0 04           //BEQ below jml (skips 4 bytes if A-#$0f==#$00)
5c a9 84 c0     //JML $c084a9

// if $a07ffe (16bit) "remote effect" == 1 then schedule deathlink, otherwise next
af fe 7f a0     //LDA (long) $a07ffe
c9 01 00        //CMP #1
d0 0b           //BNE over setting script (skips 11 bytes if != 1)

// write script address for fd8100 to $26..$28: 00 81 15
// fd8100 - c00000 + 800000 - 928000 = 2b0100. >>=1: 158100
9c 26 00        //STZ $26
a9 81 15        //LDA #$1581
8d 27 00        //STA $27
80 0b           //BRA to call below (always skips 11 bytes)

// if we get here, the effect was unknown (not deathlink)
// clear effect (STZ would be nice but needs bank switching)
a9 00 00        //LDA 0000
8f fe 7f a0     //STA (long) $a07ffe
// return
5c a9 84 c0     //JML $c084a9 back to main loop

// call "add script"
22 18 cf 8c     //JSL $8ccf18

// update active script pointer(s), as done in 8cce87
8a              //TXA: 16bit script list pointer now in A
a6 86           //LDX $86: get next active-script-list index
9f 28 2f 7e     //STA $7e2f28,x: store new script there
e8 e8           //INX INX: increase next active-script-list index
7b              //TDC: clear A
9f 28 2f 7e     //STA $7e2f28,x: store 0 to next active-script-list index
86 86           //STX $86: store back next active-script-list index

// clear effect (STZ would be nice but needs bank switching)
a9 00 00        //LDA 0000
8f fe 7f a0     //STA (long) $a07ffe
// return
5c a9 84 c0     //JML $c084a9 back to main loop
@

@0xfd8000
// bd8000-920000 = 2b0000, erase 8000 bit -> 158000 as script address

08 09 1d 03 29 09 20 03 a3 5e 00 // if new index (2578) != expected index (2575), jump to clear lock (+94)

1e 00 89 39 01      // save prize to script arg0
1e 02 89 3b 01      // save amount to script arg2
1e 04 89 09 02      // save "next add" to script arg4

18 39 01 89 24 03   // copy prize to 2391 from 257c

08 08 39 01 29 04 00 ff a4 09 00 // if prize & 0xff00 skip 9 (use "next add")
18 3b 01 89 22 03                // copy amount from 257a to 2393
04 0e 00                         // skip 14
17 3b 01 01 00                   // amount ($2393) = 1
18 09 02 09 22 03 29 31 9b       // copy amount - 1 from 257a to "next add" 2461

a3 3e               // call loot part 1
09 85 90 04 05 00   // if not loot successful skip 5
a3 3f               // call loot part 2 (looted message)
04 02 00            // skip 2
a3 41               // call loot part 3 (failed message)

18 39 01 93 00      // restore prize from script arg0
18 3b 01 93 02      // restore amount from script arg2
18 09 02 93 04      // restore "next add" from script arg4

18 1d 03 09 20 03 29 31 9a // expected index (2575) = new index (2578) + 1 // (or expected index + 1)
18 24 03 b0         // clear the item being received ("lock") at 257c
00                  // end
@

@0xfd8100
// death link script
// TODO: show text
92 d0 84 e7 03 // damage boy for 999 with animation
00 // END
@

#endif // WITH_MULTIWORLD
