: "justentered"
lockplayer
lockscroll

: "l"
set color 0 to 0 0 0
set color 9 to 63 47 47
set color 10 to 47 63 47
set color 11 to 63 47 63
set color 12 to 39 39 63
. "set color 13 63 54 51"
set color 15 to 63 63 63
if "battle" = 0 then "#scroll"
goto "#drawpl"
wait for 1
if "nodraw" = 1 then "nl"
: "contin"
goto "#clearpl"
if "textbox" = 1 then "l"
if "plock" = 1 then "l"
set "local8" to "local4"
if uppressed then "n"
if downpressed then "s"
if rightpressed then "e"
if leftpressed then "w"
set "pushingbox" to 0
set "local4" to -1
set "local7" to 0
set "local17" to 0
goto "l"
: "nl"
wait for 1
if "nodraw" = 1 then "nl"
goto "contin"



: "#scroll"
if "noscroll" = 1 then "#return"
set "local28" to "('py'-12)"
inc "local28" by "('local28'<10*(10-'local28'))"
scrollview position "('px'-40)" "('local28')"
goto "#return"
: "#noanim"
set "local11" to 0
goto "#return"
: "#drawpl"
set "local11" to "('local17'-'MOVE_DELAY'/'WALK_CYCLE'%2)"
if "pushingbox" = 1 then "#noanim"
set "local10" to 0

set "$draw" to "p1"
set "local14" to 0
set "local12" to 9
set "local13" to 20
if "p0" = 1 then "#draw_i"
set "$draw" to "p2"
set "local14" to 1
set "local12" to 10
set "local13" to 24
if "p1" = 1 then "#draw_i"
set "$draw" to "p3"
set "local14" to 2
set "local12" to 11
set "local13" to 28
if "p2" = 1 then "#draw_i"
goto "#return"
: "#clearpl"
set "local11" to 0

set "$draw" to "p1"
set "local14" to 0
if "p0" = 1 then "#clear_i"
set "$draw" to "p2"
set "local14" to 1
if "p1" = 1 then "#clear_i"
set "$draw" to "p3"
set "local14" to 2
if "p2" = 1 then "#clear_i"
goto "#return"
: "rd1"
inc "local10" by 1
goto "rd2"
: "#draw_i"
: "rd2"
set "p&local14&follow" to -1
if "local10" > 19 then "#return"
loop start
set "overlay_x" to "('&$draw&_&fol('local10')d&_&local11&_&loopcount&_x'+'fol&local10&x')"
set "overlay_y" to "('&$draw&_&fol('local10')d&_&local11&_&loopcount&_y'+'fol&local10&y')"
if "overlay_char" != 32 then "rd1"
loop for "('&$draw&_&fol('local10')d&_&local11&'-1)"

loop start
set "overlay_x" to "('&$draw&_&fol('local10')d&_&local11&_&loopcount&_x'+'fol&local10&x')"
set "overlay_y" to "('&$draw&_&fol('local10')d&_&local11&_&loopcount&_y'+'fol&local10&y')"
put "local12" "('local13'+'loopcount')" overlay to "overlay_x" "overlay_y"
set "$t" to "$&$draw&_&fol('local10')d&_&local11&_&loopcount&_c"
char edit "('local13'+'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"
loop for "('&$draw&_&fol('local10')d&_&local11&'-1)"

set "p&local14&follow" to "local10"
set "p&local14&x" to "('fol&local10&x')"
set "p&local14&y" to "('fol&local10&y')"
inc "local10" by "FOLLOW_DIST"
goto "#return"
: "#dch"
goto "#return"
: "#clear_i"
if "p&local14&follow" = -1 then "#return"
loop start
set "overlay_x" to "('&$draw&_&fol('p('local14')follow')d&_&local11&_&loopcount&_x'+'fol('p('local14')follow')x')"
set "overlay_y" to "('&$draw&_&fol('p('local14')follow')d&_&local11&_&loopcount&_y'+'fol('p('local14')follow')y')"
if "('local14'*4+20+'loopcount')" = "overlay_char" then "#clearch"
loop for "('&$draw&_&fol('p('local14')follow')d&_&local11&'-1)"
. "inc \"local10\" by \"FOLLOW_DIST\""
goto "#return"
: "#clearch"
put c07 ' ' overlay to "('&$draw&_&fol('p('local14')follow')d&_&local11&_&loopcount&_x'+'fol('p('local14')follow')x')" "('&$draw&_&fol('p('local14')follow')d&_&local11&_&loopcount&_y'+'fol('p('local14')follow')y')"
goto "#return"

: "n"
set "local2" to 0
set "local3" to -1
set "local4" to 0
goto "mv"
: "s"
set "local2" to 0
set "local3" to 1
set "local4" to 1
goto "mv"
: "e"
set "local2" to 1
set "local3" to 0
set "local4" to 2
goto "mv"
: "w"
set "local2" to -1
set "local3" to 0
set "local4" to 3
goto "mv"
: "#new"
set "local7" to 0
goto "#return"

: "mv"
set "pd" to "local4"
set "fol0d" to "pd"

if "local8" = -1 then "#new"
if "local8" != "local4" then "#new"

loop start
set "local5" to "('loopcount'%2+'px'+'local2')"
set "local6" to "('loopcount'/2+'py'+'local3')"
set "overlay_x" to "local5"
set "overlay_y" to "local6"
if "overlay_char" != 32 then "overlayblock"
if c?? CustomBlock p?? at "local5" "local6" then "block"
if c?? CustomBreak p?? at "local5" "local6" then "block"
loop for 3
set "pushingbox" to 0

set "local17" to "local7"

if "local7" < "MOVE_DELAY" then "wait"
if "('local7'-'MOVE_DELAY'%'MOVE_CYCLE')" > 0 then "wait"

: "moveok"

inc "local7" by 1

inc "px" by "local2"
inc "py" by "local3"
loop start
set "fol(19-'loopcount')x" to "fol(18-'loopcount')x"
set "fol(19-'loopcount')y" to "fol(18-'loopcount')y"
set "fol(19-'loopcount')d" to "fol(18-'loopcount')d"
loop for 18
set "fol0x" to "px"
set "fol0y" to "py"
set "fol0d" to "pd"
goto "l"
: "block"
set "local17" to 0
goto "l"
: "oblock"
copy overlay block at 2 0 for 2 by 2 to "local21" "local22"
copy overlay block at 0 0 for 2 by 2 to 2 0
goto "block"
: "overlayblock"
if "(('overlay_char'>=220)a('overlay_char'<=223))" = 1 then "pushblock"
goto "block"
: "pushblock"
goto "#findblock&overlay_char&"
if spacepressed then "pullblock"

copy overlay block at "local21" "local22" for 2 by 2 to 2 0
copy overlay block at 0 0 for 2 by 2 to "local21" "local22"
loop start
set "local5" to "('loopcount'%2+'local21'+'local2')"
set "local6" to "('loopcount'/2+'local22'+'local3')"
if c?? CustomBlock p?? at "local5" "local6" then "oblock"
if c?? CustomBreak p?? at "local5" "local6" then "oblock"
set "overlay_x" to "local5"
set "overlay_y" to "local6"
if "('overlay_char'!=32)" = 1 then "oblock"
loop for 3
copy overlay block at 2 0 for 2 by 2 to "local21" "local22"
copy overlay block at 0 0 for 2 by 2 to 2 0

set "local17" to "local7"

if "local7" < "MOVE_DELAY" then "wait"
if "('local7'-'MOVE_DELAY'%'MOVE_CYCLE')" > 0 then "wait"

copy overlay block at "local21" "local22" for 2 by 2 to 2 0
copy overlay block at 0 0 for 2 by 2 to "local21" "local22"
copy overlay block at 2 0 for 2 by 2 to "('local21'+'local2')" "('local22'+'local3')"
copy overlay block at 0 0 for 2 by 2 to 2 0

inc "local7" by 1

inc "px" by "local2"
inc "py" by "local3"
loop start
set "fol(19-'loopcount')x" to "fol(18-'loopcount')x"
set "fol(19-'loopcount')y" to "fol(18-'loopcount')y"
set "fol(19-'loopcount')d" to "fol(18-'loopcount')d"
loop for 18
set "fol0x" to "px"
set "fol0y" to "py"
set "fol0d" to "pd"
goto "afterblock"

: "pullblock"
multiply "local2" by -1
multiply "local3" by -1

loop start
set "local5" to "('loopcount'%2+'px'+'local2')"
set "local6" to "('loopcount'/2+'py'+'local3')"
if c?? CustomBlock p?? at "local5" "local6" then "block"
if c?? CustomBreak p?? at "local5" "local6" then "block"
set "overlay_x" to "local5"
set "overlay_y" to "local6"
if "overlay_char" != 32 then "block"
loop for 3
copy overlay block at "local21" "local22" for 2 by 2 to 2 0
copy overlay block at 0 0 for 2 by 2 to "local21" "local22"
loop start
set "local5" to "('loopcount'%2+'local21'+'local2')"
set "local6" to "('loopcount'/2+'local22'+'local3')"
if c?? CustomBlock p?? at "local5" "local6" then "oblock"
if c?? CustomBreak p?? at "local5" "local6" then "oblock"
set "overlay_x" to "local5"
set "overlay_y" to "local6"
if "('overlay_char'!=32)" = 1 then "oblock"
loop for 3
copy overlay block at 2 0 for 2 by 2 to "local21" "local22"
copy overlay block at 0 0 for 2 by 2 to 2 0

set "local17" to "local7"
if "local7" < "MOVE_DELAY" then "wait"
if "('local7'-'MOVE_DELAY'%'MOVE_CYCLE')" > 0 then "wait"

copy overlay block at "local21" "local22" for 2 by 2 to 2 0
copy overlay block at 0 0 for 2 by 2 to "local21" "local22"
copy overlay block at 2 0 for 2 by 2 to "('local21'+'local2')" "('local22'+'local3')"
copy overlay block at 0 0 for 2 by 2 to 2 0

inc "local7" by 1

inc "px" by "local2"
inc "py" by "local3"
loop start
if "loopcount" = "p1follow" then "skov"
if "loopcount" = "p2follow" then "skov"
set "fol&loopcount&x" to "px"
set "fol&loopcount&y" to "py"
set "fol&loopcount&d" to "pd"
: "skov"
loop for 19
set "pushingbox" to 1
goto "#scroll"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "#drawpl"
wait for 1
: "afterblock"
goto "#scroll"
goto "#clearpl"
goto "#drawpl"
wait for 1
goto "#clearpl"
goto "l"



: "#findblock220"
set "local21" to "('overlay_x')"
set "local22" to "('overlay_y')"
goto "#return"
: "#findblock221"
set "local21" to "('overlay_x'-1)"
set "local22" to "('overlay_y')"
goto "#return"
: "#findblock222"
set "local21" to "('overlay_x')"
set "local22" to "('overlay_y'-1)"
goto "#return"
: "#findblock223"
set "local21" to "('overlay_x'-1)"
set "local22" to "('overlay_y'-1)"
goto "#return"

: "wait"
inc "local7" by 1
goto "l"
