set "!DEBUG" 0

if "!DEBUG" = 0 "noDebug"
set "pl_x" 80
set "pl_y" 100
set "COMMANDS" 10000
: "noDebug"

set "ox" 0
set "oy" 0

load char set "@160lv1/trees160.chr"
set "$!map" "lv1/l1-0"
set "$!pal" ""
set "!curPal" 0
set "!destPal" 1
set "!destPalX" 0
set "!destPalY" 0
set "!destPalD" 0
goto "#LOAD"

set "!p" 3
set "$!dir" "lv1"
goto "#loadPals"
set "" "FREAD_OPEN"

: "cy"
restore "LOAD" 1
goto "#&$!map&"
goto "#pal"
if "!DEBUG" = 1 "#fakePlayer"
cycle 1
goto "cy"

: "#fakePlayer"
inc "pl_x" "('key77'-'key75')"
inc "pl_y" "('key80'-'key72')"
scrollview position "('pl_x'+'ox'-40)" "('pl_y'+'oy'-12)"
goto "#RETURN"

: "#loadPals"
loop start
set "&$!dir&/&LOOPCOUNT&.pal" "FREAD_OPEN"
set "FREAD_POS" -1
if "FREAD_POS" < 24 "#RETURN"
set "FREAD_POS" 0
set "$!pal+&$!pal.length&" "FREAD24"
loop "('!p'-1)"
goto "#RETURN"

: "#pal"
set "LOCAL" "max0,&min('!destPalD'),('SQRT('pl_x'+'ox'-'!destPalX'^2+('pl_y'+'oy'-'!destPalY'^2))')&"
loop start
set "LOCAL2" "('!curPal'*8+'LOOPCOUNT'*3)"
set "LOCAL3" "('!destPal'*8+'LOOPCOUNT'*3)"
set "SMZX_R&LOOPCOUNT&" "('$!pal.('LOCAL2'+0)'-'$!pal.('LOCAL3'+0)'*'LOCAL'/'!destPalD'+'$!pal.('LOCAL3'+0)')"
set "SMZX_G&LOOPCOUNT&" "('$!pal.('LOCAL2'+1)'-'$!pal.('LOCAL3'+1)'*'LOCAL'/'!destPalD'+'$!pal.('LOCAL3'+1)')"
set "SMZX_B&LOOPCOUNT&" "('$!pal.('LOCAL2'+2)'-'$!pal.('LOCAL3'+2)'*'LOCAL'/'!destPalD'+'$!pal.('LOCAL3'+2)')"
loop 7
goto "#RETURN"

: "#LOAD"
: "LOAD"
zap "LOAD" 1
put "@&$!map&b.mzm" image_file p00 0 2
put "@&$!map&o.mzm" image_file p01 0 2
goto "#&$!map&"
goto "#RETURN"



: "#lv1/l1-0"
set "!curPal" 0
set "!destPal" 1
set "!destPalX" 188
set "!destPalY" 16
set "!destPalD" 80
if "('pl_y'+'oy')" <= 14 "l1-0_top"
goto "#RETURN"
: "l1-0_top"
inc "ox" -124
inc "oy" 70
set "$!map" "lv1/l1-1"
goto "LOAD"

: "#lv1/l1-1"
set "!curPal" 1
set "!destPal" 2
set "!destPalX" 380
set "!destPalY" 16
set "!destPalD" 300
if "('pl_y'+'oy')" >= 87 "l1-1_bottom"
goto "#RETURN"
: "l1-1_bottom"
dec "ox" -124
dec "oy" 70
set "$!map" "lv1/l1-0"
goto "LOAD"