//****************************************************************************************************
// 	Spell card practise menu
//	Author: Helepolis
//	Version: 1.0
// 	Library:
// 		menuPlayerSelectHandler			- Main task flow
// 		makeMenuChoice(c)				- Task to listen to the selected choice and process it. The main task is naturally ending when a selection is made.
//
//****************************************************************************************************

// Global
let hasShownStages = false;
let hasShownSpellCards = false;
let hasShownDifficulty = false;

let hasStage = false;
let hasCard = false; 	
let hasDifficulty = false;

let cardsInPageArray = [];
let cardIndex = 0;

// menu enum
let STAGE_1	= 0;
let STAGE_2 = 1;
let STAGE_3 = 2;
let STAGE_LAST = 3;
let selectedStage = 0;
let availableStageArray = [];

let cardDataBaseNumber = "";
let numberOfCards = 0;
let selectedCard = 0;
let cardcounter = 0;
let pageNumber = 0;
let maxPages = 0;

let DIFF_NORM = 0;
let DIFF_HARD = 1;
let DIFF_RUNA = 2;
let selectedDifficulty = 0;

let TEAM_BORDER = 0;
let TEAM_MAGIC = 1;
let TEAM_REIMU = 2;
let TEAM_YUKARI = 3;
let TEAM_MARISA = 4;
let TEAM_ARISU = 5;
let selectedTeam = 0;

// essential
let difficultyType = "";
let getCardData = [];
let getCardComment = [];
let getCardNum = [];
let hasCountedCards = false;

// Digit showing / info for score, history and capture
let stageCapturedCards = 0;
let stageTotalCards = 0;
let capture = 0;
let history = 0;
let pracCapture = 0;
let pracHistory = 0;
let highScore = 0;

#include "script/thdcs/functions/function_loaddata.txt"
#include "script/thdcs/functions/const_sound.txt"
#include "script/thdcs/stage/spellcarddatabase.txt"

@Initialize {
	SetAutoDeleteObject(true);
	menuSpellSelectHandler;
	SetScriptResult("dummy");
}

@MainLoop { yield; }
@Finalize { } 

// Main task 
task menuSpellSelectHandler {

    musicRestartEnable(GetAreaCommonData("sound", "song", 0), 0);

    InstallFont("script/thdcs/font/nametoho.ttf");

    // BG and overlay
	let pSelScreen = drawPlayerScreen(imgPSelBg, BLEND_ALPHA);
	let pSelScreen2 = drawPlayerScreen(imgPSelBg, BLEND_ADD_ARGB);
	let pSelLay = drawPlayerScreen2(GetScreenWidth/2+160, GetScreenHeight/2, imgPSelLay);
	let pLogoBg = drawTexture(GetScreenWidth/2, 384, minitex, BLEND_ALPHA, 32, 0, 0, 640, 160);
	let topLogoBg = drawTexture2(128, 32, menulogos, BLEND_ALPHA, 155, 0, 0, 512, 128);
	let topLogo = drawTexture2(128, 64, menulogos, BLEND_ALPHA, 255, 0, 896, 512, 1024);
	let pGenArray = [pSelScreen, pSelScreen2, pSelLay, pLogoBg, topLogoBg, topLogo];

	// Player portraits
	let pSelParts = drawTexture(GetScreenWidth-144, 220, imgPYukaris, BLEND_ALPHA, 0, 0, 0, 512, 512);
	let pSelMains = drawTexture(GetScreenWidth-234, 290, imgPReimus, BLEND_ALPHA, 0, 0, 0, 512, 512);
	let pSelPart = drawTexture(GetScreenWidth-144, 220, imgPYukari, BLEND_ALPHA, 0, 0, 0, 512, 512);
	let pSelMain = drawTexture(GetScreenWidth-234, 290, imgPReimu, BLEND_ALPHA, 0, 0, 0, 512, 512);
	let pSelArray = [pSelParts, pSelMains, pSelPart, pSelMain];

	// Text and other
	let pTotalHistory = drawTexture(364, 364, menuitems, BLEND_ALPHA, 255, 896, 704, 1024, 734);
	let pCapture = drawTexture(160, 364, menuitems, BLEND_ALPHA, 255, 896, 672, 1024, 704);
	let pHistory = drawTexture(256, 364, menuitems, BLEND_ALPHA, 255, 768, 672, 896, 704);
	let pStoryMode = drawTexture(54, 384, menuitems, BLEND_ALPHA, 255, 512, 672, 640, 704);
	let pPracMode = drawTexture(63, 404, menuitems, BLEND_ALPHA, 255, 640, 672, 768, 704);
	let pHighScore = drawTexture(50, 424, menuitems, BLEND_ALPHA, 255, 640, 704, 768, 734);
	let menuSpritesArray = [pTotalHistory, pCapture, pHistory, pStoryMode, pPracMode, pHighScore];

	// summon the menu items
	let menuTextStage1 = writeGraphicalText(32, 100, 255, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Stage 1");
	let menuTextStage2 = writeGraphicalText(32, 125, 255, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Stage 2");
	let menuTextStage3 = writeGraphicalText(32, 150, 255, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Stage 3");
	let menuTextLastWord = writeGraphicalText(32, 175, 0, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Last Word");
	let menuTextExtra = writeGraphicalText(32, 200, 0, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Extra");
	let stageTextArray = [menuTextStage1, menuTextStage2, menuTextStage3];

	let menuTextEasy = writeGraphicalText(112, 150, 0, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Easy");
	let menuTextNormal = writeGraphicalText(112, 150, 0, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Normal");
	let menuTextHard = writeGraphicalText(112, 175, 0, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Hard");
	let menuTextLunatic = writeGraphicalText(112, 200, 0, 17, [255,255,255], [255,255,255], [42,99,122], 512, "Lunatic");
    let difficultyTextArray = [menuTextNormal, menuTextHard, menuTextLunatic];

	// anim vars
	let alphaValue = 0;
	let layerSpinValue = 0;
	let rgb = 254;	let rgbf = 0;
	let r = 255; let g = 64; let b = 64;
	ObjRender_SetColor(pLogoBg, r, g, b);
	
	// If by any chance returning from the Game Over scene during practise, pre-enter the values.
	cardDataBaseNumber = ToString(GetCommonData("cardDatabaseNumber", 0));
	selectedStage = GetCommonData("tryStage", 0);
	selectedCard = GetCommonData("tryCard", 0);
	selectedDifficulty = GetCommonData("tryDifficulty", 0);
	selectedTeam = GetAreaCommonData("game", "team", 0);
    cardIndex = GetCommonData("tryCard", 0);
    pageNumber = GetCommonData("SPPageNum", 0);
    maxPages = GetCommonData("SPMaxPages", 0);

    // Stage current and total cards digits + slash
	let maxDigitStgCards = 1;
	let objDigitStg1Cards = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objDigitStg1Cards, menuitems);
	ObjRender_SetBlendType(objDigitStg1Cards, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objDigitStg1Cards, 91);
	ObjRender_SetScaleXYZ(objDigitStg1Cards, 1, 1, 0);
	ObjRender_SetY(objDigitStg1Cards, 368);

    let slash1 = drawGraphicalSlash(364, 385);

	let maxDigitTotalCards = 1;
	let objDigitTotalCards = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objDigitTotalCards, menuitems);
	ObjRender_SetBlendType(objDigitTotalCards, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objDigitTotalCards, 91);
	ObjRender_SetScaleXYZ(objDigitTotalCards, 1, 1, 0);
	ObjRender_SetY(objDigitTotalCards, 368);

	// Capture Stg
	let maxDigit = 1;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);	// captures
	ObjPrim_SetTexture(obj, menuitems);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj, 91);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetY(obj, 368);

	// History Stg
	let maxDigit2 = 1;
	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);	// history
	ObjPrim_SetTexture(obj2, menuitems);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj2, 91);
	ObjRender_SetScaleXYZ(obj2, 1, 1, 0);
	ObjRender_SetY(obj2, 368);

	// Capture Prac
	let maxDigit3 = 1;
	let obj3 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);	// captures
	ObjPrim_SetTexture(obj3, menuitems);
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj3, 91);
	ObjRender_SetScaleXYZ(obj3, 1, 1, 0);
	ObjRender_SetY(obj3, 388);

	// History Prac
	let maxDigit4 = 1;
	let obj4 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);	// history
	ObjPrim_SetTexture(obj4, menuitems);
	ObjRender_SetBlendType(obj4, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj4, 91);
	ObjRender_SetScaleXYZ(obj4, 1, 1, 0);
	ObjRender_SetY(obj4, 388);

    // High Score
	let maxDigit5 = 1;
	let obj5 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);	// high score
	ObjPrim_SetTexture(obj5, menuitems);
	ObjRender_SetBlendType(obj5, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj5, 91);
	ObjRender_SetScaleXYZ(obj5, 1, 1, 0);
	ObjRender_SetY(obj5, 408);

	// Spell card number
	let objSpellNum = ObjText_Create;
	Obj_SetRenderPriorityI(objSpellNum, 90);
	ObjRender_SetAlpha(objSpellNum, 255);
	ObjText_SetFontType(objSpellNum, "MeiryoKe_PGothic");
	ObjText_SetFontSize(objSpellNum, 18);
	ObjText_SetFontColorTop(objSpellNum, 255, 160, 128);
	ObjText_SetFontColorBottom(objSpellNum, 255, 160, 128);
	ObjText_SetFontBorderWidth(objSpellNum, 2);
	ObjText_SetSidePitch(objSpellNum, -2);
	ObjText_SetFontBorderType(objSpellNum, BORDER_SHADOW);
	ObjText_SetFontBorderColor(objSpellNum, 0, 0, 0);
	ObjText_SetMaxWidth(objSpellNum, 512);
	ObjText_SetHorizontalAlignment(objSpellNum, ALIGNMENT_LEFT);
	ObjRender_SetPosition(objSpellNum, 16, 304, 0);
	ObjText_SetText(objSpellNum, "");

	// Spell card name
	let objSpellName = ObjText_Create;
	Obj_SetRenderPriorityI(objSpellName, 90);
	ObjRender_SetAlpha(objSpellName, 255);
	ObjText_SetFontType(objSpellName, "MeiryoKe_PGothic");
	ObjText_SetFontSize(objSpellName, 24);
	ObjText_SetFontColorTop(objSpellName, 255, 255, 255);
	ObjText_SetFontColorBottom(objSpellName, 255, 255, 255);
	ObjText_SetFontBorderWidth(objSpellName, 2);
	ObjText_SetSidePitch(objSpellName, -2);
	ObjText_SetFontBorderType(objSpellName, BORDER_SHADOW);
	ObjText_SetFontBorderColor(objSpellName, 0, 0, 0);
	ObjText_SetMaxWidth(objSpellName, 512);
	ObjText_SetHorizontalAlignment(objSpellName, ALIGNMENT_LEFT);
	ObjRender_SetPosition(objSpellName, 16, 324, 0);
	ObjText_SetText(objSpellName, "");

	// Spell card comment
	let objSpellComment = ObjText_Create;
	Obj_SetRenderPriorityI(objSpellComment, 90);
	ObjRender_SetAlpha(objSpellComment, 255);
	ObjText_SetFontType(objSpellComment, "MeiryoKe_PGothic");
	ObjText_SetFontSize(objSpellComment, 15);
	ObjText_SetFontColorTop(objSpellComment, 255, 255, 255);
	ObjText_SetFontColorBottom(objSpellComment, 255, 255, 255);
	ObjText_SetFontBorderWidth(objSpellComment, 2);
	ObjText_SetSidePitch(objSpellComment, -2);
	ObjText_SetFontBorderType(objSpellComment, BORDER_SHADOW);
	ObjText_SetFontBorderColor(objSpellComment, 0, 0, 0);
	ObjText_SetMaxWidth(objSpellComment, 512);
	ObjText_SetHorizontalAlignment(objSpellComment, ALIGNMENT_LEFT);
	ObjRender_SetPosition(objSpellComment, 14, 438, 0);
	ObjText_SetText(objSpellComment, "");

    function countAndGetCapturedCards() {
        let counter = 0;

        let getCommonSpellCardData = [];
        let bossName = ["pierre", "tenji", "dj", "last"];
        let bossDifficultyArray = ["norm", "hard", "runa"];

        let bossCardOPreNumber = "0";
        let forgedString = "";

        let currentDifficulty = 0;
        let numberOfBossCards = 0;
        let numberOfMidCards = 0;

        if(selectedStage == 0) { numberOfMidCards = 2; numberOfBossCards = 5; }
        if(selectedStage == 1) { numberOfMidCards = 2; numberOfBossCards = 8; }
        if(selectedStage == 2) { numberOfMidCards = 2; numberOfBossCards = 8; }
        if(selectedStage == 3) { numberOfMidCards = 0; numberOfBossCards = 20; }

        ascent(difficulty in 0..length(bossDifficultyArray)) {
            if(selectedStage != 2) {
                ascent(i in 1..numberOfMidCards) {
                    if(selectedStage == 0) { forgedString = ToString("fifi") ~ "0" ~ IntToString(i) ~ ToString(bossDifficultyArray[currentDifficulty]); }
                    if(selectedStage == 1) { forgedString = ToString("nue") ~ "0" ~ IntToString(i) ~ ToString(bossDifficultyArray[currentDifficulty]); }
                    getCommonSpellCardData = GetAreaCommonData("core", forgedString, [0,0,0,0,0,0,0,0,0,0,0]);
                    if(getCommonSpellCardData[3] > 0) { counter++; }
                }
            }
            else {
                ascent(i in 0..numberOfMidCards) {
                    forgedString = ToString("border") ~ "0" ~ IntToString(i) ~ ToString(bossDifficultyArray[currentDifficulty]);
                    getCommonSpellCardData = GetAreaCommonData("core", forgedString, [0,0,0,0,0,0,0,0,0,0,0]);
                    if(getCommonSpellCardData[3] > 0) { counter++; }
                }
                ascent(i in 0..numberOfMidCards) {
                    forgedString = ToString("magic") ~ "0" ~ IntToString(i) ~ ToString(bossDifficultyArray[currentDifficulty]);
                    getCommonSpellCardData = GetAreaCommonData("core", forgedString, [0,0,0,0,0,0,0,0,0,0,0]);
                    if(getCommonSpellCardData[3] > 0) { counter++; }
                }
            }
            ascent(i in 1..numberOfBossCards) {
                forgedString = ToString(bossName[selectedStage]) ~ "0" ~ IntToString(i) ~ ToString(bossDifficultyArray[currentDifficulty]);
                getCommonSpellCardData = GetAreaCommonData("core", forgedString, [0,0,0,0,0,0,0,0,0,0,0]);
                if(getCommonSpellCardData[3] > 0) { counter++; }
            }
            if(currentDifficulty < 3) { currentDifficulty++; }
        }
        return counter;
    }

    function resetCardsInPageArray() {
        ascent(i in 0..length(cardsInPageArray)) {
            Obj_Delete(cardsInPageArray[i]);
        }
        hasShownSpellCards = false;
    }

    function drawSpellCards() {
        // Draw the spell card text on the screen
        if(!hasShownSpellCards) {
            cardsInPageArray = [];
            let yPosCounter = 0;
            cardcounter = 0;

            if(numberOfCards > 10) { cardcounter = 10; }
            else { cardcounter = numberOfCards; }

            ascent(i in (pageNumber*10)+0..(pageNumber*10)+cardcounter) {
                let setText = "Spell Card " ~ IntToString(i + 1);
                let textObject = writeGraphicalText(64, 130 + (yPosCounter*25), 255, 17, [255,255,255], [255,255,255], [42,99,122], 512, setText);
                cardsInPageArray = cardsInPageArray ~ [textObject];
                yPosCounter++;
            }
            hasShownSpellCards = true;
        }
    }

	loop(5) { yield; }

	loop {
		ObjRender_SetAngleZ(pSelLay, layerSpinValue);
		ObjRender_SetColor(pSelScreen2, rgb, rgb, rgb);
		rgb -= sin(rgbf) * 8;
		rgbf += 5;
		layerSpinValue += 0.05;
		ObjRender_SetColor(pLogoBg, r, g, b);

		//-------------------------------------------------------------------------------------------------
		// HISTORY AND CAPTURE RATE HANDLING

        // digits for stage 1 capture/total
        if(stageCapturedCards < 9) { maxDigitStgCards = 1; stageCapturedCards = min(stageCapturedCards,9); }
        if(stageCapturedCards > 9 && stageCapturedCards < 99) { maxDigitStgCards = 2; stageCapturedCards = min(stageCapturedCards, 99); }

        if(stageTotalCards < 9) { maxDigitTotalCards = 1; stageTotalCards = min(stageTotalCards,9); }
        if(stageTotalCards > 9 && stageTotalCards < 99) { maxDigitTotalCards = 2; stageTotalCards = min(stageTotalCards, 99); }

		// digits for captures stg
		if(capture < 9) { maxDigit = 1; capture = min(capture,9); }
		if(capture > 9 && capture < 99) { maxDigit = 2; capture = min(capture, 99); }
		if(capture > 99 && capture < 999) { maxDigit = 3; capture = min(capture, 999); }
		if(capture > 999 && capture < 9999) { maxDigit = 4; capture = min(capture, 9999); }
		if(capture > 9999 && capture < 99999) { maxDigit = 5; capture = min(capture, 99999); }

		// digits for attempts stg
		if(history < 9) { maxDigit2 = 1; history = min(history,9); }
		if(history > 9 && history < 99) { maxDigit2 = 2; history = min(history, 99); }
		if(history > 99 && history < 999) { maxDigit2 = 3; history = min(history, 999); }
		if(history > 999 && history < 9999) { maxDigit2 = 4; history = min(history, 9999); }
		if(history > 9999 && history < 99999) { maxDigit2 = 5; history = min(history, 99999); }

		// digits for captures stg
		if(pracCapture < 9) { maxDigit3 = 1; pracCapture = min(pracCapture,9); }
		if(pracCapture > 9 && pracCapture < 99) { maxDigit3 = 2; pracCapture = min(pracCapture, 99); }
		if(pracCapture > 99 && pracCapture < 999) { maxDigit3 = 3; pracCapture = min(pracCapture, 999); }
		if(pracCapture > 999 && pracCapture < 9999) { maxDigit3 = 4; pracCapture = min(pracCapture, 9999); }
		if(pracCapture > 9999 && pracCapture < 99999) { maxDigit3 = 5; pracCapture = min(pracCapture, 99999); }

		// digits for attempts stg
		if(pracHistory < 9) { maxDigit4 = 1; pracHistory = min(pracHistory,9); }
		if(pracHistory > 9 && pracHistory < 99) { maxDigit4 = 2; pracHistory = min(pracHistory, 99); }
		if(pracHistory > 99 && pracHistory < 999) { maxDigit4 = 3; pracHistory = min(pracHistory, 999); }
		if(pracHistory > 999 && pracHistory < 9999) { maxDigit4 = 4; pracHistory = min(pracHistory, 9999); }
		if(pracHistory > 9999 && pracHistory < 99999) { maxDigit4 = 5; pracHistory = min(pracHistory, 99999); }

		// digits for high score
		if(highScore < 9) { maxDigit5 = 1; highScore = min(highScore,9); }
		if(highScore > 9 && highScore < 99) { maxDigit5 = 2; highScore = min(highScore,99); }
		if(highScore > 99 && highScore < 999) { maxDigit5 = 3; highScore = min(highScore,999); }
		if(highScore > 999 && highScore < 9999) { maxDigit5 = 4; highScore = min(highScore,9999); }
		if(highScore > 9999 && highScore < 99999) { maxDigit5 = 5; highScore = min(highScore,99999); }
		if(highScore > 99999 && highScore < 999999) { maxDigit5 = 6; highScore = min(highScore,999999); }
		if(highScore > 999999 && highScore < 9999999) { maxDigit5 = 7; highScore = min(highScore,9999999); }
		if(highScore > 9999999 && highScore < 99999999) { maxDigit5 = 8; highScore = min(highScore,99999999); }
		if(highScore > 99999999 && highScore < 999999999) { maxDigit5 = 9; highScore = min(highScore,999999999); }
		if(highScore > 999999999 && highScore < 9999999999) { maxDigit5 = 10; highScore = min(highScore,9999999999); }
		if(highScore > 9999999999 && highScore < 99999999999) { maxDigit5 = 11; highScore = min(highScore,99999999999); }
		if(highScore > 99999999999 && highScore < 999999999999) { maxDigit5 = 12; highScore = min(highScore,999999999999); }
		if(highScore > 999999999999 && highScore < 9999999999999) { maxDigit5 = 13; highScore = min(highScore,9999999999999); }
		if(highScore > 9999999999999 && highScore < 99999999999999) { maxDigit5 = 14; highScore = min(highScore,99999999999999); }

		let listNumStgCards = DigitToArray(stageCapturedCards, maxDigitStgCards);
		let listNumStg1Total = DigitToArray(stageTotalCards, maxDigitTotalCards);

		let listNum = DigitToArray(capture, maxDigit);
		let listNum2 = DigitToArray(history, maxDigit2);
		let listNum3 = DigitToArray(pracCapture, maxDigit3);
		let listNum4 = DigitToArray(pracHistory, maxDigit4);
		let listNum5 = DigitToArray(highScore, maxDigit5);

		ObjSpriteList2D_ClearVertexCount(objDigitStg1Cards);
		ObjSpriteList2D_ClearVertexCount(objDigitTotalCards);

		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(obj2);
		ObjSpriteList2D_ClearVertexCount(obj3);
		ObjSpriteList2D_ClearVertexCount(obj4);
		ObjSpriteList2D_ClearVertexCount(obj5);

		ascent(i in 0..maxDigitStgCards) {
			let num = listNumStgCards[i];
			let xPos = 330;

			ObjRender_SetX(objDigitStg1Cards, xPos+i*12);
			ObjSpriteList2D_SetSourceRect(objDigitStg1Cards, num*20, 321, (num + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(objDigitStg1Cards, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(objDigitStg1Cards);
		}
		ascent(i in 0..maxDigitTotalCards) {
			let num = listNumStg1Total[i];
			let xPos = 366;

			ObjRender_SetX(objDigitTotalCards, xPos+i*12);
			ObjSpriteList2D_SetSourceRect(objDigitTotalCards, num*20, 321, (num + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(objDigitTotalCards, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(objDigitTotalCards);
		}

		//-------------------------------------------------------------------------------------------------
		// captures stg
		ascent(i in 0..maxDigit) {
			let num = listNum[i];
			ObjRender_SetX(obj, 160+i*12);
			ObjSpriteList2D_SetSourceRect(obj, num*20, 321, (num + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(obj);
		}
		// history stg
		ascent(i in 0..maxDigit2) {
			let num2 = listNum2[i];
			ObjRender_SetX(obj2, 256+i*12);
			ObjSpriteList2D_SetSourceRect(obj2, num2*20, 321, (num2 + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(obj2, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(obj2);
		}
		// captures prac
		ascent(i in 0..maxDigit3) {
			let num3 = listNum3[i];
			ObjRender_SetX(obj3, 160+i*12);
			ObjSpriteList2D_SetSourceRect(obj3, num3*20, 321, (num3 + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(obj3, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(obj3);
		}
		// history prac
		ascent(i in 0..maxDigit4) {
			let num4 = listNum4[i];
			ObjRender_SetX(obj4, 256+i*12);
			ObjSpriteList2D_SetSourceRect(obj4, num4*20, 321, (num4 + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(obj4, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(obj4);
		}

		// high score prac
		ascent(i in 0..maxDigit5) {
			let num5 = listNum5[i];
			ObjRender_SetX(obj5, 160+i*12);
			ObjSpriteList2D_SetSourceRect(obj5, num5*20, 321, (num5 + 1)*20, 352);
			ObjSpriteList2D_SetDestRect(obj5, 0, 0, 20, 32);
			ObjSpriteList2D_AddVertex(obj5);
		}

		//-------------------------------------------------------------------------------------------------
		// PLAYER SELECT ANIMATION HANDLING
		if(ObjRender_GetX(pSelParts) < GetScreenWidth-132) {
			ObjRender_SetX(pSelParts, ObjRender_GetX(pSelParts)+0.5);
			ObjRender_SetX(pSelMains, ObjRender_GetX(pSelMains)+0.5);
		}

		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH) {
			if(selectedTeam > -1) {
				selectedTeam--;
				PlaySFX(SFX_MENPICK);
				ObjRender_SetX(pSelMain, GetScreenWidth-224); ObjRender_SetX(pSelMains, GetScreenWidth-224);
				ObjRender_SetX(pSelPart, GetScreenWidth-144); ObjRender_SetX(pSelParts, GetScreenWidth-144);
			}
			alphaValue = 0;
			if(selectedTeam == -1) { selectedTeam = 1; }
		}
		if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH) {
			if(selectedTeam < 2) {
				selectedTeam++;
				PlaySFX(SFX_MENPICK);
				// anim reset
				ObjRender_SetX(pSelMain, GetScreenWidth-224); ObjRender_SetX(pSelMains, GetScreenWidth-224);
				ObjRender_SetX(pSelPart, GetScreenWidth-144); ObjRender_SetX(pSelParts, GetScreenWidth-144);

			}
			alphaValue = 0;
			if(selectedTeam == 2) { selectedTeam = 0; }
		}

		alternative(selectedTeam)
		case(TEAM_BORDER) {
			selectedTeam = TEAM_BORDER;
			ObjPrim_SetTexture(pSelMain, imgPReimu); ObjPrim_SetTexture(pSelMains, imgPReimus);
			ObjPrim_SetTexture(pSelPart, imgPYukari); ObjPrim_SetTexture(pSelParts, imgPYukaris);
			ascent(i in 0..length(pSelArray)) { ObjRender_SetAlpha(pSelArray[i], alphaValue); }
			if(alphaValue < 250) { alphaValue += 20; }
		}
		case(TEAM_MAGIC) {
		    selectedTeam = TEAM_MAGIC;
			SetAreaCommonData("game", "team", 1);
			ObjPrim_SetTexture(pSelMain, imgPMarisa); ObjPrim_SetTexture(pSelMains, imgPMarisas);
			ObjPrim_SetTexture(pSelPart, imgPArisu); ObjPrim_SetTexture(pSelParts, imgPArisus);
			ascent(i in 0..length(pSelArray)) { ObjRender_SetAlpha(pSelArray[i], alphaValue); }
			if(alphaValue < 250) { alphaValue += 20; }
		}

		//-------------------------------------------------------------------------------------------------
		// SPELL PRACTICE ANIMATION HANDLING

        // Update the number of current cards and total cards when highlighting a stage
        if(selectedStage == STAGE_1) {
            stageTotalCards = 15;
            numberOfCards = length(stage1SpellCards[2]);
        }
        if(selectedStage == STAGE_2) {
            stageTotalCards = 24;
            numberOfCards = length(stage2SpellCards[2]);
        }
        if(selectedStage == STAGE_3) {
            stageTotalCards = 27;
            numberOfCards = length(stage3SpellCards[2]);
        }
        if(selectedStage == STAGE_LAST) {
            stageTotalCards = 20;
            numberOfCards = length(lastSpellCards[0]);
        }
		if(!hasCountedCards) {
		    stageCapturedCards = countAndGetCapturedCards();
		    maxPages = 0;
            ascent(i in 0..numberOfCards) {
                if(i%10 == 0) { maxPages++; }
            }
            maxPages--;
		    hasCountedCards = true;
        }

		// If no stage has been selected, run this block first
		if(!hasStage) {
			ObjText_SetText(objSpellName, "");
			ObjText_SetText(objSpellComment, "");
			ObjText_SetText(objSpellNum, "");

            // Darken all stage text except the highlighted one (selectedStage)
            ascent(i in 0..length(stageTextArray)) {
                ObjRender_SetAlpha(stageTextArray[i], 255);
                changeTextColour(stageTextArray[i], 128, 128, 128, [1,1,1]);
                ObjRender_SetY(stageTextArray[i], 100 + i*25);
            }
            changeTextColour(stageTextArray[selectedStage], 255, 255, 255, [42,99,122]);

			if(GetVirtualKeyState(VK_UP) == KEY_PUSH) { 
				PlaySFX(SFX_MENPICK);
				if(selectedStage > -1) { selectedStage--; }
				if(selectedStage == -1) { selectedStage = 2; }
				hasCountedCards = false;
			}
			if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH) { 
				PlaySFX(SFX_MENPICK); 			
				if(selectedStage < 3) { selectedStage++; }
				if(selectedStage == 3) { selectedStage = 0; }
                hasCountedCards = false;
			}
		} else {
		    // animate the selected item to the top
            ascent(i in 0..length(stageTextArray)) {
                ObjRender_SetAlpha(stageTextArray[i], 0);
            }
            let animateObject = stageTextArray[selectedStage];
            ObjRender_SetAlpha(animateObject, 255);
            changeTextColour(animateObject, 255, 255, 255, [42,99,122]);

            while(ObjRender_GetY(animateObject) > 100) {
                ObjRender_SetY(animateObject, ObjRender_GetY(animateObject) - 5);
                yield;
            }

		    // If no card has been selected, run this block first
			if(!hasCard) {
				ObjText_SetText(objSpellName, "- - -");
				ObjText_SetText(objSpellComment, "- - -");
				ObjText_SetText(objSpellNum, "Spell No. ");
				capture = 0;
				history = 0;	
				pracCapture = 0;
				pracHistory = 0;
				highScore = 0;

				drawSpellCards();

                // Darken all spell card text except the highlighted one (selectedCard)
                ascent(i in 0..length(cardsInPageArray)) {
                    ObjRender_SetAlpha(cardsInPageArray[i], 255);
                    changeTextColour(cardsInPageArray[i], 128, 128, 128, [1,1,1]);
                    ObjRender_SetY(cardsInPageArray[i], 125 + i*25);
                }
                changeTextColour(cardsInPageArray[selectedCard], 255, 255, 255, [42,99,122]);

                if(selectedStage == STAGE_LAST) {
					let forgedName = ToString(allNameSpellCards[selectedStage][selectedCard]) ~ ToString(difficultyType);
					getCardData = GetAreaCommonData("core",forgedName,[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
					capture = getCardData[1];
					history = getCardData[2];
					pracCapture = getCardData[3];
					pracHistory = getCardData[4];

             		if(selectedTeam == TEAM_BORDER) { highScore = getCardData[5]; }
             		if(selectedTeam == TEAM_MAGIC) { highScore = getCardData[6]; }
             		if(selectedTeam == TEAM_REIMU) { highScore = getCardData[7]; }
             		if(selectedTeam == TEAM_YUKARI) { highScore = getCardData[8]; }
             		if(selectedTeam == TEAM_MARISA) { highScore = getCardData[9]; }
             		if(selectedTeam == TEAM_ARISU) { highScore = getCardData[10]; }

					if(selectedStage == 0 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInf01JP; }
					if(selectedStage == 0 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInf01EN; }
					if(selectedStage == 1 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInf02JP; }
					if(selectedStage == 1 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInf02EN; }
					if(selectedStage == 2 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInf03JP; }
					if(selectedStage == 2 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInf03EN; }
					if(selectedStage == 3 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInfLastJP; }
					if(selectedStage == 3 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInfLastEN; }

					getCardNum = lastSpellCards;

					ObjText_SetText(objSpellNum, "Spell No. " ~ ToString(getCardNum[0][cardIndex][0]));

					if(getCardData[0] > 0) {
						ObjText_SetText(objSpellName, getCardComment[cardIndex][0]);
						ObjText_SetText(objSpellComment, getCardComment[cardIndex][1]);
					} else {
						ObjText_SetText(objSpellName, "H H H H H");
						ObjText_SetText(objSpellComment, "H H H H H");
					}
                }

				if(GetVirtualKeyState(VK_UP) == KEY_PUSH) { 
					PlaySFX(SFX_MENPICK);
                    selectedCard--;
                    cardIndex--;

                    if(cardIndex < 0 && maxPages == 0) {
                        selectedCard = numberOfCards - 1;
                        cardIndex = numberOfCards - 1;

                    }
                    else if(cardIndex < 0 && maxPages > 0) {
                        selectedCard = 9;
                        cardIndex = numberOfCards - 1;
                        pageNumber = maxPages;
                        resetCardsInPageArray();
                    }
                    else if(selectedCard < 0 && pageNumber > 0) {
                        pageNumber--;
                        selectedCard = 9;
                        resetCardsInPageArray();
                    }
				}
				if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH) { 
					PlaySFX(SFX_MENPICK);
                    selectedCard++;
                    cardIndex++;

                    if(cardIndex > numberOfCards - 1 && maxPages == 0) {
                        selectedCard = 0;
                        cardIndex = 0;
                    }
                    else if(cardIndex == cardcounter && pageNumber < maxPages) {
                        selectedCard = 0;
                        pageNumber++;
                        resetCardsInPageArray();
                    }
                    else if(cardIndex == numberOfCards && pageNumber == maxPages) {
                        selectedCard = 0;
                        cardIndex = 0;
                        pageNumber = 0;
                        resetCardsInPageArray();
                    }
				}			
			} else {
                // animate the selected item to the top
                ascent(i in 0..length(cardsInPageArray)) {
                    ObjRender_SetAlpha(cardsInPageArray[i], 0);
                }
                let animateObject = cardsInPageArray[cardIndex];
                ObjRender_SetAlpha(animateObject, 255);
                changeTextColour(animateObject, 255, 255, 255, [42,99,122]);

                while(ObjRender_GetY(animateObject) > 130) {
                    ObjRender_SetY(animateObject, ObjRender_GetY(animateObject) - 10);
                    yield;
                }
                drawSpellCards();

				// If no difficulty has been selected, run this block first
				// But only if the selected card is not a last word
				if(!hasDifficulty) {
                    // Darken all difficulty text except the highlighted one (selectedDifficulty)
                    ascent(i in 0..length(difficultyTextArray)) {
                        ObjRender_SetAlpha(difficultyTextArray[i], 255);
                        changeTextColour(difficultyTextArray[i], 128, 128, 128, [1,1,1]);
                    }
                    changeTextColour(difficultyTextArray[selectedDifficulty], 255, 255, 255, [42,99,122]);

                    if(GetVirtualKeyState(VK_UP) == KEY_PUSH) {
                        PlaySFX(SFX_MENPICK);
                        if(selectedDifficulty > -1) { selectedDifficulty--; }
                        if(selectedDifficulty == -1) { selectedDifficulty = 2; }
                    }
                    if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH) {
                        PlaySFX(SFX_MENPICK);
                        if(selectedDifficulty < 3) { selectedDifficulty++; }
                        if(selectedDifficulty == 3) { selectedDifficulty = 0; }
                    }

                    if(selectedDifficulty == DIFF_NORM) { difficultyType = "norm"; }
                    if(selectedDifficulty == DIFF_HARD) { difficultyType = "hard"; }
                    if(selectedDifficulty == DIFF_RUNA) { difficultyType = "runa"; }

					let forgedName = ToString(allNameSpellCards[selectedStage][selectedCard]) ~ ToString(difficultyType);
					getCardData = GetAreaCommonData("core",forgedName,[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
					capture = getCardData[1];
					history = getCardData[2];
					pracCapture = getCardData[3];
					pracHistory = getCardData[4];

             		if(selectedTeam == TEAM_BORDER) { highScore = getCardData[5]; }
             		if(selectedTeam == TEAM_MAGIC) { highScore = getCardData[6]; }
             		if(selectedTeam == TEAM_REIMU) { highScore = getCardData[7]; }
             		if(selectedTeam == TEAM_YUKARI) { highScore = getCardData[8]; }
             		if(selectedTeam == TEAM_MARISA) { highScore = getCardData[9]; }
             		if(selectedTeam == TEAM_ARISU) { highScore = getCardData[10]; }

					if(selectedStage == 0 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInf01JP; }
					if(selectedStage == 0 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInf01EN; }
					if(selectedStage == 1 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInf02JP; }
					if(selectedStage == 1 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInf02EN; }
					if(selectedStage == 2 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInf03JP; }
					if(selectedStage == 2 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInf03EN; }
					if(selectedStage == 3 && GetAreaCommonData("system","gameLang",0) == 0) { getCardComment = allSpellCardInfLastJP; }
					if(selectedStage == 3 && GetAreaCommonData("system","gameLang",0) == 1) { getCardComment = allSpellCardInfLastEN; }

					if(selectedStage == STAGE_1) { getCardNum = stage1SpellCards; }
					if(selectedStage == STAGE_2) { getCardNum = stage2SpellCards; }
					if(selectedStage == STAGE_3) { getCardNum = stage3SpellCards; }
					if(selectedStage == STAGE_LAST) { getCardNum = lastSpellCards; }
					
					ObjText_SetText(objSpellNum, "Spell No. " ~ ToString(getCardNum[selectedDifficulty][cardIndex][0]));
					cardDataBaseNumber = ToString(getCardNum[selectedDifficulty][cardIndex][0]);
					SetCommonData("cardDatabaseNumber", cardDataBaseNumber);

					if(getCardData[0] > 0) { 		
						ObjText_SetText(objSpellName, getCardComment[cardIndex][0]);
						ObjText_SetText(objSpellComment, getCardComment[cardIndex][1]);
					} else {
						ObjText_SetText(objSpellName, "H H H H H");
						ObjText_SetText(objSpellComment, "H H H H H");
					}
				}
			}
		}

        // Confirming (Default: Z button)
		if(GetVirtualKeyState(VK_OK) == KEY_PULL) {
			if(!hasStage) {
			    PlaySFX(SFX_OK00);
			    hasStage = true;
            } else if(hasStage && !hasCard) {
			    PlaySFX(SFX_OK00);
			    hasCard = true;
                if(selectedStage == STAGE_LAST) {
					PlaySFX(SFX_OK00);
                    hasDifficulty = true;
	                SetCommonData("SPPageNum", pageNumber);
	                SetCommonData("SPMaxPages", maxPages);
					makeMenuChoice(selectedStage, cardIndex, selectedDifficulty, selectedTeam);
                    break;
                }
            } else if(hasStage && hasCard && !hasDifficulty) {
				if(getCardData[0] > 0) {
					PlaySFX(SFX_OK00);
				    hasDifficulty = true;
					makeMenuChoice(selectedStage, cardIndex, selectedDifficulty, selectedTeam);
					break; 
				} else {
					PlaySFX(SFX_DAME);
				}
			}
		}
		// Canceling (Default: X button)
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL) { 
			if(hasStage && !hasCard) {
			    PlaySFX(SFX_MENCANCEL);
			    hasStage = false;
                resetCardsInPageArray();
                cardIndex = 0;
                selectedCard = 0;
                pageNumber = 0;
			    hasShownSpellCards = false;
            } else if(hasStage && hasCard) {
			    PlaySFX(SFX_MENCANCEL);
			    hasCard = false;
			    selectedDifficulty = 0;
                ascent(i in 0..length(difficultyTextArray)) {
                    ObjRender_SetAlpha(difficultyTextArray[i], 0);
                }
            } else {
				PlaySFX(SFX_MENCANCEL);

	            SetCommonData("tryStage", 0);
	            SetCommonData("tryCard", 0);
	            SetCommonData("tryDifficulty", 0);

				SetScriptResult("CANCEL");
				CloseScript(GetOwnScriptID);
			}
		} 
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(obj2);
	Obj_Delete(obj3);
	Obj_Delete(obj4);
	Obj_Delete(obj5);

    Obj_Delete(objDigitStg1Cards);
    Obj_Delete(objDigitTotalCards);
    Obj_Delete(slash1);

	Obj_Delete(objSpellComment);
	Obj_Delete(objSpellName);
	Obj_Delete(objSpellNum);

	ascent(i in 0..length(pGenArray)) { Obj_Delete(pGenArray[i]); } 
	ascent(i in 0..length(pSelArray)) { Obj_Delete(pSelArray[i]); }
	ascent(i in 0..length(menuSpritesArray)) { Obj_Delete(menuSpritesArray[i]); }
	ascent(i in 0..length(stageTextArray)) { Obj_Delete(stageTextArray[i]); }
	ascent(i in 0..length(cardsInPageArray)) { Obj_Delete(cardsInPageArray[i]); }
	ascent(i in 0..length(difficultyTextArray)) { Obj_Delete(difficultyTextArray[i]); }
}

// Menu choice made handle
task makeMenuChoice(stage, card, difficulty, team) {
	// Get the parameters and pass them to the commondata
	SetCommonData("tryStage", stage);
	SetCommonData("tryCard", card);
	SetCommonData("tryDifficulty", difficulty);
	SetAreaCommonData("game", "stageNum", 0);
	SetAreaCommonData("game", "team", team);
	SaveCommonDataAreaA1("game");

    let path = "script/thdcs/menu/pack_stage.txt";
	let id = LoadScript(path);
	StartScript(id);	

	while(!IsCloseScript(id)) {
		yield;
	}		
		
	let result = GetScriptResult(id);	
	if(result == "ENDSPELLPRACTISE") {
		if(GetCommonData("tryStage", stage) == STAGE_LAST) {
    	    hasStage = true;
    	    hasCard = false;
    	    hasDifficulty = false;
        } else {
	        hasStage = true;
	        hasCard = true;
	        hasDifficulty = false;
        }
        hasCountedCards = false;
	    hasShownStages = false;
	    hasShownSpellCards = false;
	    hasShownDifficulty = false;
	    menuSpellSelectHandler;
    }
}

// Confirm animation
function menSelectAnim(obj) { 
	loop(2) {	
		ObjRender_SetColor(obj, 64, 64, 64);
		loop(2) { yield; }	
		ObjRender_SetColor(obj, 255, 255, 255);
		loop(2) { yield; }
	}
}

// Draw the '/' character at desired location
function drawGraphicalSlash(x, y) {
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI(obj, 90);
    ObjPrim_SetTexture(obj,menuitems);
    ObjRender_SetBlendType(obj, BLEND_ALPHA);
    ObjRender_SetAlpha(obj, 255);
    ObjSprite2D_SetSourceRect(obj, 100, 288, 110, 304);
    ObjRender_SetColor(obj, 255, 255, 255);
    ObjRender_SetScaleXYZ(obj, 2.0, 2.0, 0);
    ObjSprite2D_SetDestCenter(obj);
    ObjRender_SetPosition(obj, x, y, 0);

    return obj;
}

// ORIGINAL Ph3 default_system code utility.
function DigitToArray(let digit, let count) {
	let res = [];
	digit = truncate(digit);
	loop {
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0) { break; }
	}
	loop(max(0, count - length(res))) {
		res = [0] ~ res;
	}
	return res;
}

// Menu item piece
function menuItem(x,y,scale,alpha,left,top,right,bottom) { 
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,menuitems);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,alpha);
	Obj_SetRenderPriorityI(obj,90);
	ObjRender_SetScaleXYZ(obj,scale,scale,0);
	ObjSprite2D_SetSourceRect(obj,left,top,right,bottom);
	ObjSprite2D_SetDestCenter(obj);	
	ObjRender_SetPosition(obj,x,y,0);
	return obj;
}

// Draws a texture (flexible)
function drawTexture(x,y,tex,btype,alphaValue,left,top,right,bottom) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(obj,tex);
	ObjRender_SetBlendType(obj,btype);
	ObjRender_SetAlpha(obj,alphaValue);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite2D_SetSourceRect(obj,left,top,right,bottom);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	
	return obj;	
}

function drawTexture2(x,y,tex,btype,alphaValue,left,top,right,bottom) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(obj,tex);
	ObjRender_SetBlendType(obj,btype);
	ObjRender_SetAlpha(obj,alphaValue);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite2D_SetSourceRect(obj,left,top,right,bottom);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	
	return obj;	
}

// Draws the title screen
function drawPlayerScreen(tex,btype) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(obj,tex);
	ObjRender_SetBlendType(obj,btype);
	ObjRender_SetAlpha(obj,255);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite2D_SetSourceRect(obj,0,0,640,480);
	ObjSprite2D_SetDestRect(obj,0,0,640,480);
	
	return obj;	
}

// Draws the title screen
function drawPlayerScreen2(x,y,tex) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(obj,tex);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,155);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite2D_SetSourceRect(obj,0,0,1080,1080);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	return obj;	
}

function writeGraphicalText(x, y, alpha, fontSize, topRGB, bottomRGB, borderRGB, maxWidth, text) {
    let obj = ObjText_Create;
    let topColourRed = topRGB[0];
    let topColourGreen = topRGB[1];
    let topColourBlue = topRGB[2];

    let bottomColourRed = bottomRGB[0];
    let bottomColourGreen = bottomRGB[1];
    let bottomColourBlue = bottomRGB[2];

    let shadowColourRed = borderRGB[0];
    let shadowColourGreen = borderRGB[1];
    let shadowColourBlue = borderRGB[2];

    Obj_SetRenderPriorityI(obj, 90);
    ObjRender_SetAlpha(obj, alpha);
    ObjText_SetFontType(obj, "Ename Regular");
    ObjText_SetFontSize(obj, fontSize);
    ObjText_SetFontColorTop(obj, topColourRed, topColourGreen, topColourBlue);
    ObjText_SetFontColorBottom(obj, bottomColourRed, bottomColourGreen, bottomColourBlue);
    ObjText_SetFontBorderWidth(obj, 3);
    ObjText_SetSidePitch(obj, -2);
    ObjText_SetFontBorderType(obj, BORDER_SHADOW);
    ObjText_SetFontBorderColor(obj, shadowColourRed, shadowColourGreen, shadowColourBlue);
    ObjText_SetMaxWidth(obj, maxWidth);
    ObjText_SetHorizontalAlignment(obj, ALIGNMENT_LEFT);
    ObjRender_SetPosition(obj, x, y, 0);
    ObjText_SetText(obj, text);

    return obj;
}

function changeTextColour(obj, red, green, blue, shadowRGB) {
    ObjText_SetFontColorTop(obj, red, green, blue);
    ObjText_SetFontColorBottom(obj, red, green, blue);
    ObjText_SetFontBorderColor(obj, shadowRGB[0], shadowRGB[1], shadowRGB[2]);
}

// TODO: remove when finished. Temp code to quick enable all last words
task enableAllLastWords() {
    SetAreaCommonData("core","last1",[1,0,0,0,0,0,0,0,0,0,0]);          // Reimu
    SetAreaCommonData("core","last2",[1,0,0,0,0,0,0,0,0,0,0]);          // Yukari
    SetAreaCommonData("core","last3",[1,0,0,0,0,0,0,0,0,0,0]);          // Marisa
    SetAreaCommonData("core","last4",[1,0,0,0,0,0,0,0,0,0,0]);          // Alice
    SetAreaCommonData("core","last5",[1,0,0,0,0,0,0,0,0,0,0]);          // Fifi
    SetAreaCommonData("core","last6",[1,0,0,0,0,0,0,0,0,0,0]);          // Pierre
    SetAreaCommonData("core","last7",[1,0,0,0,0,0,0,0,0,0,0]);          // Hina
    SetAreaCommonData("core","last8",[1,0,0,0,0,0,0,0,0,0,0]);          // Ufo
    SetAreaCommonData("core","last9",[1,0,0,0,0,0,0,0,0,0,0]);          // Kogasa
    SetAreaCommonData("core","last10",[1,0,0,0,0,0,0,0,0,0,0]);         // Nue
    SetAreaCommonData("core","last11",[1,0,0,0,0,0,0,0,0,0,0]);         // Tenji
    SetAreaCommonData("core","last12",[1,0,0,0,0,0,0,0,0,0,0]);         // Iku
    SetAreaCommonData("core","last13",[1,0,0,0,0,0,0,0,0,0,0]);         // Dj
    SetAreaCommonData("core","last14",[1,0,0,0,0,0,0,0,0,0,0]);         // Tewi
    SetAreaCommonData("core","last15",[1,0,0,0,0,0,0,0,0,0,0]);         // Border
    SetAreaCommonData("core","last16",[1,0,0,0,0,0,0,0,0,0,0]);         // Magic
    SetAreaCommonData("core","last17",[1,0,0,0,0,0,0,0,0,0,0]);         // Kogasa & Nue
    SetAreaCommonData("core","last18",[1,0,0,0,0,0,0,0,0,0,0]);         // Pierre + Hina
    SetAreaCommonData("core","last19",[1,0,0,0,0,0,0,0,0,0,0]);         // Tenji + Iku
    SetAreaCommonData("core","last20",[1,0,0,0,0,0,0,0,0,0,0]);         // Dj + Tewi
    SaveCommonDataAreaA1("core");
}