end
: "start"
mod "*"
set "battle" to 1
set "local2" to 1000
set "local3" to 1000
set "local4" to -1
set "local5" to -1
loop start
if "p&loopcount&" = 1 then "#mt_p"
loop for 2
loop start
goto "#mt_e"
loop for "('enemies'-1)"
if "battle_manualscroll" = 1 then "manualscroll"
set "battle_scrollx" to "('local2'-(80-('local4'-'local2')/2))"
set "battle_scrolly" to "('local3'-(25-('local5'-'local3')/2))"
if "battle_scrollx" < 0 then "#clip_x_0"
if "battle_scrolly" < 10 then "#clip_y_10"
if "battle_scrollx" > "('board_w'-80)" then "#clip_x_m"
if "battle_scrolly" > "('board_h'-25)" then "#clip_y_m"
: "manualscroll"
set "battle_manualscroll" to 0
set "local2" to 0
set "local3" to 20
set "nodraw" to 1
set "local10" to "scrolledx"
set "local11" to "scrolledy"
loop start
set "local5" to "('p&loopcount&x')"
set "local6" to "('p&loopcount&y')"
if "p&loopcount&" = 1 then "#draw_pl"
loop for 2
wait for 1
loop start
set "local5" to "('p&loopcount&x')"
set "local6" to "('p&loopcount&y')"
if "p&loopcount&" = 1 then "#draw_pl"
loop for 2
wait for 1

: "dr"
loop start
set "local5" to "('p&loopcount&x'+('battle_p&loopcount&x'-'p&loopcount&x'*'local2'+('local3'/2)/'local3'))"
set "local6" to "('p&loopcount&y'+('battle_p&loopcount&y'-'p&loopcount&y'*'local2'+('local3'/2)/'local3'))"
if "p&loopcount&" = 1 then "#draw_pl"
loop for 2
set "local5" to "('local10'+('battle_scrollx'-'local10'*'local2'+('local3'/2)/'local3'))"
set "local6" to "('local11'+('battle_scrolly'-'local11'*'local2'+('local3'/2)/'local3'))"
scrollview position "local5" "local6"
wait for 1
loop start
set "local5" to "('p&loopcount&x'+('battle_p&loopcount&x'-'p&loopcount&x'*'local2'+('local3'/2)/'local3'))"
set "local6" to "('p&loopcount&y'+('battle_p&loopcount&y'-'p&loopcount&y'*'local2'+('local3'/2)/'local3'))"
if "p&loopcount&" = 1 then "#clear_pl"
loop for 2
inc "local2" by 1
if "local2" <= "local3" then "dr"
set "local2" to "local3"
loop start
set "local5" to "('battle_p&loopcount&x')"
set "local6" to "('battle_p&loopcount&y')"
if "p&loopcount&" = 1 then "#draw_pl"
loop for 2

scrollview position "battle_scrollx" "battle_scrolly"
: "colfading"
wait for 1
loop start
set "local5" to "('battle_p&loopcount&x')"
set "local6" to "('battle_p&loopcount&y')"
if "p&loopcount&" = 1 then "#draw_pl"
loop for 2
if "colfading" = 1 "colfading"
set "$from" to "board_name"
loop start
set "local5" to "('battle_p&loopcount&x')"
set "local6" to "('battle_p&loopcount&y')"
if "p&loopcount&" = 1 then "#clear_pl"
loop for 2
loop start
goto "#clear_enemy"
loop for "('enemies'-1)"
loop start
set "board_x" to "('loopcount'%80+'scrolledx')"
set "overlay_x" to "('loopcount'%80+'scrolledx')"
set "board_y" to "('loopcount'/80+'scrolledy')"
set "overlay_y" to "('loopcount'/80+'scrolledy')"
set "$bg_id.&loopcount&" to 7
set "$bg_param.&loopcount&" to "overlay_char"
set "$bg_color.&loopcount&" to "overlay_color"
if "$bg_color.&loopcount&" < 16 then "#overlaycolour"
if "$bg_param.&loopcount&" = 32 then "#overlayspace"
loop for 1999
teleport player to "battle" at 0 0
set "nodraw" to 0
set "noscroll" to 1
loop start
if "p&loopcount&" = 1 then "leader"
loop for 2
: "leader_done"

loop start
set "fol&loopcount&x" to "px"
set "fol&loopcount&y" to "py"
set "fol&loopcount&d" to "pd"
loop for 19
set "local10" to "battle_scrollx"
set "local11" to "battle_scrolly"
set "local28" to "('py'-12)"
inc "local28" by "('local28'<10*(10-'local28'))"
set "battle_scrollx" to "('px'-40)"
set "battle_scrolly" to "('local28')"
loop start
set "local5" to "('local10'+('battle_scrollx'-'local10'*'loopcount'+(20/2)/20))"
set "local6" to "('local11'+('battle_scrolly'-'local11'*'loopcount'+(20/2)/20))"
scrollview position "local5" "local6"
wait for 1
loop for 20
set "noscroll" to 0
set "battle" to 0
end
: "#clear_enemy"
write overlay c07 "  " at "('e&loopcount&x')" "('e&loopcount&y'+0)"
write overlay c07 "  " at "('e&loopcount&x')" "('e&loopcount&y'+1)"
goto "#return"
: "leader"
set "px" to "battle_p&loopcount&x"
set "py" to "battle_p&loopcount&y"
set "pd" to "battle_p&loopcount&d"
goto "leader_done"
end

: "#draw_pl"
set "local30" to "loopcount"

set "$draw" to "p('local30'+1)"
loop start
set "overlay_x" to "('&$draw&_&battle_p('local30')d&_0_&loopcount&_x'+'local5')"
set "overlay_y" to "('&$draw&_&battle_p('local30')d&_0_&loopcount&_y'+'local6')"
if "overlay_char" = 32 then "#drc"
loop for "('&$draw&_&battle_p('local30')d&_0'-1)"

set "loopcount" to "local30"
goto "#return"
: "#drc"
put "(9+'local30')" "('local30'*4+20+'loopcount')" overlay to "overlay_x" "overlay_y"
set "$t" to "$&$draw&_&battle_p('local30')d&_0_&loopcount&_c"
char edit "('local30'*4+20+'loopcount')" to "$t.0" "$t.1" "$t.2" "$t.3" "$t.4" "$t.5" "$t.6" "$t.7" "$t.8" "$t.9" "$t.10" "$t.11" "$t.12" "$t.13"
goto "#return"

: "#clear_pl"
set "local30" to "loopcount"
set "$draw" to "p('local30'+1)"
change overlay "('local30'+9)" "('local30'*4+20)" to c07 ' '
change overlay "('local30'+9)" "('local30'*4+21)" to c07 ' '
change overlay "('local30'+9)" "('local30'*4+22)" to c07 ' '
change overlay "('local30'+9)" "('local30'*4+23)" to c07 ' '
. "loop start"
. "put c07 ' ' overlay to \"('&$draw&_&battle_p('local30')d&_0_&loopcount&_x'+'local5')\" \"('&$draw&_&battle_p('local30')d&_0_&loopcount&_y'+'local6')\""
. "loop for \"('&$draw&_&battle_p('local30')d&_0'-1)\""

set "loopcount" to "local30"
goto "#return"



: "#mt_p"
set "local6" to "battle_p&loopcount&x"
if "local6" < "local2" then "#minx"
if "local6" > "local4" then "#maxx"
set "local6" to "battle_p&loopcount&y"
if "local6" < "local3" then "#miny"
if "local6" > "local5" then "#maxy"
goto "#return"
: "#mt_e"
set "local6" to "e&loopcount&x"
if "local6" < "local2" then "#minx"
if "local6" > "local4" then "#maxx"
set "local6" to "e&loopcount&y"
if "local6" < "local3" then "#miny"
if "local6" > "local5" then "#maxy"
goto "#return"
: "#minx"
set "local2" to "local6"
goto "#return"
: "#miny"
set "local3" to "local6"
goto "#return"
: "#maxx"
set "local4" to "local6"
goto "#return"
: "#maxy"
set "local5" to "local6"
goto "#return"

: "#clip_x_0"
set "battle_scrollx" to 0
goto "#return"
: "#clip_y_10"
set "battle_scrolly" to 10
goto "#return"
: "#clip_x_m"
set "battle_scrollx" to "('board_w'-80)"
goto "#return"
: "#clip_y_m"
set "battle_scrolly" to "('board_h'-25)"
goto "#return"
: "#overlaycolour"
inc "$bg_color.&loopcount&" by "('board_color'a240)"
goto "#return"
: "#overlayspace"
set "$bg_param.&loopcount&" to "board_param"
set "$bg_color.&loopcount&" to "board_color"
set "$bg_id.&loopcount&" to "board_id"
goto "#return"
