@echo off
SETLOCAL
del Scroller.prg Scroller.map
del bin\Intro.prg bin\Chooser.prg bin\Title.prg
del bin\Scroller1.cmp bin\Scroller2.cmp

SET CHOICE=ScrollEntry_Tusari.a

copy /y "exported assets\Level1Placement.a" "bin\LevelTempPlacement.a"
copy /y "exported assets\Level1Data.a" "bin\LevelTempData.a"
copy /y "exported assets\Level1Activation.a" "bin\LevelTempActivation.a"
copy /y "exported assets\Level1Code.a" "bin\LevelTempCode.a"
copy /y "exported assets\Level1Options.a" "bin\LevelTempOptions.a"
copy /y "exported assets\Level1Splits.a" "bin\LevelTempSplits.a"
copy /y "exported assets\Level1.ctm" "bin\LevelTemp.ctm"
copy /y "bin\Level1.prg" "bin\LevelTemp.prg"
copy /y "exported assets\Level1.spd" "bin\LevelTemp.spd"


echo ;This file is automatically generated by BuildIt.bat >bin/FingerPrint.a
echo !scr "%TIME% %DATE% %COMPUTERNAME% %USERNAME%" >>bin/FingerPrint.a


echo *** Assemble Intro.a
..\acme.exe -v4 --msvc --lib asm/ --lib bin/ --lib ../ --lib ../Scroller/ Intro.a
if not exist bin\Intro.prg goto error
copy /y bin\Intro.prg bin\IntroOrig.prg
..\bin\LZMPi.exe -c64mr bin\IntroOrig.prg bin\Intro.prg $400 >t.txt
if not exist bin\Intro.prg goto error

echo *** Assemble Title.a
..\acme.exe -v4 --msvc --lib asm/ --lib bin/ --lib ../ --lib ../Scroller/ Title.a
if not exist bin\Title.prg goto error
..\bin\LZMPi.exe -cr bin\Title.prg bin\Title.cmp 2 >t.txt
if not exist bin\Intro.prg goto error

echo *** Assemble ScrollEntryAfter.a level 1
..\acme.exe -v4 --msvc --lib asm/ --lib bin/ --lib ../ --lib ../Scroller/ --lib ../stdlib/ ScrollEntry.a %CHOICE% ScrollEntryAfter.a
if not exist Scroller.prg goto error
..\bin\LZMPi.exe -cr Scroller.prg bin\Scroller.cmp 2 >t.txt
if not exist bin\Scroller.cmp goto error


rem No extra Ultimax boot, so use a simple empty address switch as placeholder for the command line
set CART_BOOT_TYPE=-tg
set CART_BOOT_TYPE_HI=-a $8000
rem Using Ultimax boot, so create the high bank with the reset vector
set CART_BOOT_TYPE=-te
set CART_BOOT_TYPE_HI=-a $a000 -b 0 -c $1ffc 2 4 -w

echo CART_BOOT_TYPE = %CART_BOOT_TYPE%

echo Making simple cart

rem First make a simple cart, without speed code
..\bin\MakeCart.exe -i _f_index1.a %CART_BOOT_TYPE% -n -a $8000 -b 0 -r ..\Citadel2\Citadel2Cart_8K.prg -c 0 2 $ffff -w %CART_BOOT_TYPE_HI% -r bin\Intro.prg -a $8000 -b 1 -c 0 $0001 $ffff -w -a $8000 -b 2 -c 0 $2001 $ffff -w -a $8000 -b 3 -c 0 $4001 $ffff -w -a $8000 -b 4 -c 0 $6001 $ffff -w -a $8000 -b 5 -c 0 $8001 $ffff -w -a $8000 -b 6 -c 0 $a001 $ffff -w -o Scroller.crt >>t.txt

rem If there is no speed code enabled then skip the speed code generation
..\ExternalTools\Gnu\bin\sed.exe -n "/Scroller_EnableSpeedCode/q1" Scroller.map
IF NOT ERRORLEVEL 1 goto end

echo ;This file is automatically generated by BuildIt.bat >..\Scroller\CartCode\CartOpts.a

rem Extract all the required variables from this list to a separate file
for %%x in (
	Scroller_Enable
	Scroller_NeedFullColourScroll
	Scroller_Debug
	CharColoursForEveryCharacter
	Scroller_KillCartridgeBank
	Scroller_BankSwitchControl
	Scroller_ScrollBankEntry
	ScrollBankSplit
	Scroller_FullScreen
	Scroller_AdditionalCharLines
	ColourTemp
	CharColours
	topScreenBank
	BankToScreenAddr
	COLOURRAM
	ScrollerDrawStripBottom_GetBlockFromMap
	ScrollerDrawStripRight_GetBlockFromMap
	MapData
	scrollMapLX
	scrollMapHX
	scrollMapLY
	scrollMapHY
	BlockTabL
	BlockTabH
	tempWork
	MapTabL
	MapTabH
) do (
	..\ExternalTools\Gnu\bin\sed.exe -n "/%%x/p" Scroller.map >>..\Scroller\CartCode\CartOpts.a
)

if not exist bin mkdir bin
del bin\*.bin

rem Create the cartridge scroller speed code
echo ScreenA = $c800 >..\Scroller\CartCode\ScreenConfig.a
echo ScreenB = $cc00 >>..\Scroller\CartCode\ScreenConfig.a

..\ExternalTools\Gnu\bin\sed.exe -n "/Scroller_EnableBlockColourSpeedCodeInCart/q1" Scroller.map
IF NOT ERRORLEVEL 1 goto noColourise
echo Adding speed colourise code
..\acme.exe --msvc --lib ../Scroller/ -o bin\ColouriseTop.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\HeaderEntry.a ScrollerStripsMacros.a ScrollerStripsColouriseTop.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\ColouriseBottom.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\HeaderEntry.a ScrollerStripsMacros.a ScrollerStripsColouriseBottom.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\ColouriseLeft.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\HeaderEntry.a ScrollerStripsMacros.a ScrollerStripsColouriseLeft.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\ColouriseRight.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\HeaderEntry.a ScrollerStripsMacros.a ScrollerStripsColouriseRight.a
:noColourise

..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_1.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_1.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_m1.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m1.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_40.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_40.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_m40.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m40.a

..\ExternalTools\Gnu\bin\sed.exe -n "/Scroller_MultiDirectionNoDiagonals/q1" Scroller.map
IF ERRORLEVEL 1 goto noDiagonals1

..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_39.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_39.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_m39.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m39.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_41.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_41.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char0To1_m41.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m41.a

:noDiagonals1

echo ScreenA = $cc00 >..\Scroller\CartCode\ScreenConfig.a
echo ScreenB = $c800 >>..\Scroller\CartCode\ScreenConfig.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_1.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_1.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_m1.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m1.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_40.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_40.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_m40.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m40.a

..\ExternalTools\Gnu\bin\sed.exe -n "/Scroller_MultiDirectionNoDiagonals/q1" Scroller.map
IF ERRORLEVEL 1 goto noDiagonals2

..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_39.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_39.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_m39.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m39.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_41.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_41.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Char1To0_m41.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m41.a

:noDiagonals2

..\ExternalTools\Gnu\bin\sed.exe -n "/ColourTemp/q1" Scroller.map
IF NOT ERRORLEVEL 1 goto noColours

echo ScreenA = $d800 >..\Scroller\CartCode\ScreenConfig.a
echo ScreenB = $d800 >>..\Scroller\CartCode\ScreenConfig.a
echo ScreenBTemp = ColourTemp >>..\Scroller\CartCode\ScreenConfig.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_1.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_1.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_m1.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m1.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_40.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_40.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_m40.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m40.a

..\ExternalTools\Gnu\bin\sed.exe -n "/Scroller_MultiDirectionNoDiagonals/q1" Scroller.map
IF ERRORLEVEL 1 goto noDiagonals3

..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_39.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_39.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_m39.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m39.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_41.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_41.a
..\acme.exe --msvc --lib ../Scroller/ -o bin\Colour_m41.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\Scroll_m41.a

:noDiagonals3

:noColours


..\ExternalTools\Gnu\bin\sed.exe -n "/Scroller_EnableBlockStripsSpeedCodeInCart/q1" Scroller.map
IF NOT ERRORLEVEL 1 goto noStrips1
echo Adding speed strips code

..\acme.exe --msvc --lib ../Scroller/ -o bin\BlockStrips.bin --lib ../Scroller/CartCode/ CartCode\Header.a CartCode\HeaderEntry.a ScrollerStripsMacros.a ScrollerStripsTop.a ScrollerStripsBottom.a ScrollerStripsLeft.a ScrollerStripsRight.a CartCode\StripsOffsets.a

echo ;This file is automatically generated by BuildIt.bat >bin\StripOffsets.a
..\ExternalTools\Gnu\bin\sed.exe -n "/kCartOffset_ScrollerDrawStrip/p" bin\cart.map >>bin\StripOffsets.a


:noStrips1

rem If the files are missing then they are simply not included in the cart
set SCROLLER_SPEED_CODE_FILES_NUM=32
set SCROLLER_SPEED_CODE_FILES=bin\ColouriseTop.bin+! bin\Char0To1_1.bin bin\Char1To0_1.bin bin\ColouriseBottom.bin+! bin\Char0To1_m1.bin bin\Char1To0_m1.bin bin\ColouriseLeft.bin+! bin\Char0To1_40.bin bin\Char1To0_40.bin bin\ColouriseRight.bin+! bin\Char0To1_m40.bin bin\Char1To0_m40.bin bin\Char0To1_41.bin bin\Char1To0_41.bin bin\Char0To1_39.bin bin\Char1To0_39.bin bin\Char0To1_m41.bin bin\Char1To0_m41.bin bin\Char0To1_m39.bin bin\Char1To0_m39.bin bin\Colour_1.bin bin\Colour_m1.bin bin\Colour_40.bin bin\Colour_m40.bin bin\Colour_39.bin bin\Colour_m39.bin bin\Colour_41.bin bin\Colour_m41.bin bin\BlockStrips.bin bin\Scroller1.cmp bin\Scroller2.cmp bin\Title.cmp

echo Adding speed code

..\bin\MakeCart.exe -i _f_index1.a -tg -n -a $8000 -b 7 -f $2000 %SCROLLER_SPEED_CODE_FILES_NUM% %SCROLLER_SPEED_CODE_FILES% -o Scroller.crt >>t.txt

rem Build the final code again to get the offsets

rem Level1
echo *** Assemble ScrollEntryAfter.a final level 1
del Scroller.prg
..\acme.exe -v4 --msvc --lib asm/ --lib bin/ --lib ../ --lib ../Scroller/ --lib ../stdlib/ ScrollEntry.a %CHOICE% ScrollEntryAfter.a >tf.txt
if not exist Scroller.prg goto error
..\bin\LZMPi.exe -cr Scroller.prg bin\Scroller1.cmp 2 >t.txt
if not exist bin\Scroller1.cmp goto error
copy /y "Scroller.pdb" "Scroller1.pdb"
copy /y "Scroller.lbl" "Scroller1.lbl"


rem Level2
echo *** Assemble ScrollEntryAfter.a final level 2
copy /y "exported assets\Level2Placement.a" "bin\LevelTempPlacement.a"
copy /y "exported assets\Level2Data.a" "bin\LevelTempData.a"
copy /y "exported assets\Level2Activation.a" "bin\LevelTempActivation.a"
copy /y "exported assets\Level2Code.a" "bin\LevelTempCode.a"
copy /y "exported assets\Level2Options.a" "bin\LevelTempOptions.a"
copy /y "exported assets\Level2Splits.a" "bin\LevelTempSplits.a"
copy /y "exported assets\Level2.ctm" "bin\LevelTemp.ctm"
copy /y "bin\Level2.prg" "bin\LevelTemp.prg"
copy /y "exported assets\Level2.spd" "bin\LevelTemp.spd"
del Scroller.prg
..\acme.exe -v4 --msvc --lib asm/ --lib bin/ --lib ../ --lib ../Scroller/ --lib ../stdlib/ ScrollEntry.a %CHOICE% ScrollEntryAfter.a >tf.txt
if not exist Scroller.prg goto error
..\bin\LZMPi.exe -cr Scroller.prg bin\Scroller2.cmp 2 >t.txt
if not exist bin\Scroller2.cmp goto error
copy /y "Scroller.pdb" "Scroller2.pdb"
copy /y "Scroller.lbl" "Scroller2.lbl"


..\bin\MakeCart.exe -i _f_index1.a %CART_BOOT_TYPE% -n -a $8000 -b 0 -r ..\Citadel2\Citadel2Cart_8K.prg -c 0 2 $ffff -w %CART_BOOT_TYPE_HI% -r bin\Intro.prg -a $8000 -b 1 -c 0 $0001 $ffff -w -a $8000 -b 2 -c 0 $2001 $ffff -w -a $8000 -b 3 -c 0 $4001 $ffff -w -a $8000 -b 4 -c 0 $6001 $ffff -w -a $8000 -b 5 -c 0 $8001 $ffff -w -a $8000 -b 6 -c 0 $a001 $ffff -w -a $8000 -s $2000 -b 7 -f $2000 %SCROLLER_SPEED_CODE_FILES_NUM% %SCROLLER_SPEED_CODE_FILES% -o Scroller.crt >>tf.txt


echo *** Assemble Chooser.a
..\acme.exe -v4 --msvc --lib asm/ --lib bin/ --lib ../ --lib ../Scroller/ Chooser.a
if not exist bin\Chooser.prg goto error

..\bin\MakeCart.exe -i _f_index1.a %CART_BOOT_TYPE% -n -a $8000 -b 0 -r ..\Citadel2\Citadel2Cart_8K.prg -c 0 2 $ffff -w %CART_BOOT_TYPE_HI% -r bin\Intro.prg -a $8000 -b 1 -c 0 $0001 $ffff -w -a $8000 -b 2 -c 0 $2001 $ffff -w -a $8000 -b 3 -c 0 $4001 $ffff -w -a $8000 -b 4 -c 0 $6001 $ffff -w -a $8000 -b 5 -c 0 $8001 $ffff -w -a $8000 -b 6 -c 0 $a001 $ffff -w -a $8000 -b 6 -r bin\Chooser.prg -c 0 2 $ffff -w -s $2000 -b 7 -f $2000 %SCROLLER_SPEED_CODE_FILES_NUM% %SCROLLER_SPEED_CODE_FILES% -o Scroller.crt >>tf.txt

goto end
:error
echo Scroller.crt not created!
exit /B -1
:end
