#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Stage 2 - Answers by Happenstance"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let EndStateCalled = false;

let eventhappening = false;

let objmidboss = ObjEnemyBossScene_Create();
let objmidboss2 = ObjEnemyBossScene_Create();
let objmidboss3 = ObjEnemyBossScene_Create();
let objboss = ObjEnemyBossScene_Create();

let scriptEnemy;
let scriptBackground;

#include "../lib/includes/dialogueconsts.dnh"
#include "../lib/stage.eventdialogue.dnh"
#include "../lib/includes/Function_Audio.dnh"
#include "../tricky_system/Event_Const.dnh"
#include "../lib/includes/EffectFunctions.dnh"
#include "../lib/includes/EnemyFunctions.dnh"
#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
	
function LoadAllImagesInFolder(path){
	yield;
	let plist = GetFilePathList(path);
	let dlist = GetDirectoryList(path);
	ascent(i in 0..length(plist)){
		let cFile = plist[i];
		let fExt = cFile[length(cFile)-3..length(cFile)];
		if(fExt == "png" || fExt == "jpg" || fExt == "bmp"){
			LoadTextureInLoadThread(cFile);
		}
	}
	
	ascent(i in 0..length(dlist)){
		LoadAllImagesInFolder(dlist[i]);
	}
}
	
@Loading{
	
	if(GetCommonData("Main Game", false)==false){
		SetPlayerLife(9);
		InstallFont(CSD ~ "../BNKGOTHM.TTF");
		LoadAllImagesInFolder(CSD~"../");
	}
	
	ObjEnemyBossScene_Add(objmidboss, 0, CSD ~ "ShokoraMS1_H.dnh");
	ObjEnemyBossScene_Add(objmidboss2, 0, CSD ~ "ShokoraMS2_H.dnh");
	ObjEnemyBossScene_Add(objmidboss3, 0, CSD ~ "ShokoraMS3_H.dnh");
	
	ObjEnemyBossScene_Add(objboss, 0, CSD ~ "Shokora0.dnh");
	ObjEnemyBossScene_Add(objboss, 0, CSD ~ "ShokoraN1_H.dnh");
	ObjEnemyBossScene_Add(objboss, 0, CSD ~ "ShokoraS1_H.dnh");
	ObjEnemyBossScene_Add(objboss, 1, CSD ~ "ShokoraN2_H.dnh");
	ObjEnemyBossScene_Add(objboss, 1, CSD ~ "ShokoraS2_H.dnh");
	ObjEnemyBossScene_Add(objboss, 2, CSD ~ "ShokoraN3_H.dnh");
	ObjEnemyBossScene_Add(objboss, 2, CSD ~ "ShokoraS3_H.dnh");
	ObjEnemyBossScene_Add(objboss, 3, CSD ~ "ShokoraS4_H.dnh");
	
	ObjEnemyBossScene_LoadInThread(objmidboss);
	ObjEnemyBossScene_LoadInThread(objmidboss2);
	ObjEnemyBossScene_LoadInThread(objmidboss3);
	ObjEnemyBossScene_LoadInThread(objboss);	
}
	
@Initialize{

	scriptEnemy = LoadScript(GetCurrentScriptDirectory ~ "Enemy.dnh");
	StartScript(scriptEnemy);
	
	scriptBackground = LoadScript(GetCurrentScriptDirectory ~ "./NamelessPath.dnh");
	StartScript(scriptBackground);

	TStage();
}

@MainLoop{

	//if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD ||GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH) { CollectItemsInCircle(GetPlayerX, GetPlayerY, 55); }
		if(GetPlayerState() == STATE_END && EndStateCalled==false){ EndStateCalled = true; StartCloseStgScene(); }
	
	yield;	
}

task StartCloseStgScene{
	wait(180);
	CloseStgScene();
}

@Event{
	alternative(GetEventType())	
	case(EV_PAUSE_ENTER){
		NotifyEventAll(EV_MUSIC_PAUSE, true);
	}
	case(EV_PAUSE_LEAVE){
		NotifyEventAll(EV_MUSIC_RESUME, true);
	}
	case(EV_DIALOGUE){
		let eventParam = GetEventArgument(0);
		let type = eventParam;
		
		alternative(type)
		case(0) { Event0; }
		case(1) { Event1; }
	}
}

task FairySwarm(time, amount){
	loop(amount){
		CreateFairy_SmallA1(-10, 50, 0, 1);
		wait(time);
		CreateFairy_SmallA1(384+10, 50, 0, 1);
		wait(time);
	}
}

task FairySwarm2(time, amount){
	let count = 0;
	loop(amount){
		CreateFairy_SmallA1(-10, 50, 0, 1);
		if(count%10==0){ CreateFairy_SmallA3(384+10, 110, 0, 1); }
		wait(time);
		CreateFairy_SmallA1(384+10, 50, 0, 0);
		if(count%10==0){ CreateFairy_SmallA3(-10, 110, 0, 0); }
		wait(time);
		count++;
	}
}

task Spam(time){
	let variable = 0;
	loop(time){
		let obj = CreateShotA1(192+210*sin(variable)+rand(-2, 2), -20, rand(1.9, 2.1)+(0.5*sin(variable*4)), rand(90-2, 90+2), FIRE[RED], 0);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)+1);
		wait(5);
		variable+=21.675;
	}
}
			
task TStage{
	let CSD = -1;
	let switch = 1;
	SetCommonData("BGSpeed", 3);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	wait(90);
	PlayMusic(2.1);
	CreateFairy_MediumA3(200, -10, 775, 0);
	wait(350);
	CreateFairy_MediumA3(-200, -10, 775, 1);
	wait(270);
	SetCommonData("BGSpeed", 4);
	wait(30);
	Title;
	wait(300);

	FairySwarm(10, 26);
	wait(10*26*2);
	FairySwarm2(8, 28);
	wait(8*28*2);
	wait(110);
	SetEnemyLifeToZero;
	wait(30);

	ObjEnemyBossScene_Regist(objmidboss);
	while(!Obj_IsDeleted(objmidboss)){
		yield;
	}
	wait(90);
	ObjEnemyBossScene_Regist(objmidboss2);
	while(!Obj_IsDeleted(objmidboss2)){
		yield;
	}
	wait(120);
	PlaySoundEffect("sagecast", 100, GetStgFrameWidth/2);
	wait(45);
	PlaySoundEffect("ping", 90, GetStgFrameWidth/2);
	wait(15);
	Spam(4*200/4);
	wait(120);
	if(GetPlayerX>GetCenterX){ switch = -1; }
	loop(4){
		CreateFairy_MediumA1(192+165*switch, -55, 0, 2);
		wait(210);
		switch*=-1;
	}
	WaitIfEnemiesOnScreen(999);
	wait(60);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	wait(60);
	Event0;
	while(!Obj_IsDeleted(objmidboss3)){
		yield;
	}
	wait(210);
	CollectAllItems;
	Event1;
	while(!Obj_IsDeleted(objboss)){
		yield;
	}
	CloseScript(scriptEnemy);
	wait(90);
	Event2;
	eventhappening = true;
	while(eventhappening){ yield; }
	if(GetCommonData("Main Game", false)==false){ CloseStgScene(); }
		else{
			CloseScript(scriptBackground);
			CloseScript(GetOwnScriptID());
		}
}

function WaitIfEnemiesOnScreen(w){
	loop(w){ if(length(GetAllEnemyID)>0){ yield; } }
}


task Event0{
	let dispX_Player = 0;
	let dispX_Enem = 0;
	InitializeEvent(1, 1);
	HandleEventLogic();
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);
	SetCharStatusPresent(0, 0, true);
	Talking(0);
	SetEventText(0, 2, "Long-ranged danmaku? Creative.", 80);
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);
	SetEventText(0, 6, "The fire's a nice, warm touch.[r]It's a shame it's over.", 150);
	wait(30);
	ObjEnemyBossScene_Regist(objmidboss3); 
	wait(60);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 2, "...or not. Nevermind.", 120);
	SetCharStatusPresent(0, 0, false); SetCharStatusPresent(1, 0, false);
	FinalizeEvent;
}
	
task Event1{
	InitializeEvent(1, 1);
	HandleEventLogic();
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);
	SetCharStatusPresent(0, 0, true);
	SetCutinGraphic(1, 0, shokora, 10, 100, false);
	SetCharStatusPresent(1, 0, false);
	Talking(0);
	SetEventText(0, 2, "Even I shouldn't tempt fate by saying that.", 0);
	Talking(1);
	SetEventText(1, 1, "She's still coming? Damn it!", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 2, "...Is that yelling?", 0);
	SetCommonData("BGSpeed", 1);
	Talking(1);
	SetEventText(1, 5, "I should've brought more[r]of my long-ranged spells!", 0);
	Talking(0);
	SetEventText(0, 0, "My, isn't this a loud one.", 0);
	SetCommonData("BGSpeed", 1);
	Talking(1);
	ObjEnemyBossScene_Regist(objboss);
	BossShadow;	
	wait(120);
	SetCharStatusPresent(1, 0, true);
	SetEventText(1, 1, "Who're you calling loud, chairy?", 0);
	PlaySoundEffect("whack", 100, 384/2);
	SetEventText(1, 1, "*whack*", 00);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 0, "Ow! You do realize who you're dealing with?", 0);
	Talking(1);
	SetEventText(1, 4, "*page flip*[r]*page flip*", 0);
	SetEventText(1, 0, "Okina Matara?", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinasmug, -70, 45, false);
	SetEventText(0, 0, "Yes. The Ultimate, Absolute-", 0);
	Talking(1);
	SetEventText(1, 0, "You look hella stupid in person.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 0, "...", 0);
	SetEventText(0, 5, "Wait a minute! Just who[r]do you think you are?!", 0);
	Talking(1);
	BossName("Shokora Milphew");
	SetEventText(1, 4, "Shokora Milphew. I run a print shop.[r]I do graphics and advertising too.", 0);
	SetEventText(1, 0, "You should stop by any time!", 0);
	SetEventText(1, 0, "Except today. I'm super busy.", 0);
	Talking(0);
	SetEventText(0, 0, "Busy with what?", 0);
	Talking(1);
	SetEventText(1, 4, "One of my friends needs[r]help with a project.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinahappy, -70, 45, false);
	SetEventText(0, 2, "A project? That sounds like a lead.", 0);
	SetEventText(0, 0, "A project? That sounds like a lea-", 0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 0, "A...thing...that I should help with!", 0);
	Talking(1);
	SetEventText(1, 4, "Girl, you sure you're a secret goddess?[r]You're really damn bad at secrets.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 2, "...keep calm, Okina...", 0);
	SetCutinGraphic(0, 0, okinahappy, -70, 45, false);
	SetEventText(0, 0, "Fine. I'll just be on my merry way.", 0);
	Talking(1);
	PlaySoundEffect("whack", 100, 384/2);
	SetEventText(1, 1, "*whack*", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 0, "Ow! Is that thing magic?", 0);
	Talking(1);
	SetEventText(1, 4, "Today my job's to stop threatening intruders.[r]Your weird ass counts since you got past my stuff.", 0);
	SetEventText(1, 4, "And no, it's not magic.[r]All the magic is INSIDE.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);
	SetEventText(0, 4, "She's just an insolent human.[r]She doesn't even look good at magic.", 0);
	Talking(1);
	PlaySoundEffect("whack", 100, 384/2);
	SetEventText(1, 1, "*whack*", 00);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(1, 4, "Don't need to be to beat you.[r]I can make a headline out of that.", 0);
	PlayMusic(2.2);
	SetCommonData("BGSpeed", 8);
	BossCircle;
	SetCommonData("vsshokora", true);
	SetEventText(1, 1, "'Village's #1 Editor Beats Backdoor Goddess!'", 0);
	SetCharStatusPresent(0, 0, false); SetCharStatusPresent(1, 0, false);
	NotifyEventAll(EV_KILLBOSS, 1);
	FinalizeEvent;
}

task Event2{
	SetCommonData("BGSpeed", 8);
	let dispX_Player = 0;
	let dispX_Enem = 0;
	InitializeEvent(1, 1);
	HandleEventLogic();
	SetCutinGraphic(0, 0, okinasmug, -70, 45, false);
	SetCharStatusPresent(0, 0, true);
	SetCutinGraphic(1, 0, shokoradefeated, 10, 100, false);
	SetCharStatusPresent(1, 0, true);
	Talking(1);
	SetEventText(1, 0, "Ouchie ouch.", 0);
	Talking(0);
	SetEventText(0, 0, "How's that for a backdoor goddess?", 0);
	Talking(1);
	SetEventText(1, 4, "Geez, you're stronger than I thought![r]I guess he was right to warn me about you.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	SetEventText(0, 0, "He, you say?", 0);
	Talking(1);
	SetEventText(1, 0, "Yeah, he.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinahappy, -70, 45, false);
	SetEventText(0, 0, "Information! Tell me, who is this 'he'?", 0);
	Talking(1);
	SetEventText(1, 4, "We ain't too far from the Forest of Magic. Just[r]keep going down the path and you'll get there.", 0);
	SetEventText(1, 0, "'He' is there.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinahappy, -70, 45, false);
	SetEventText(0, 0, "Great! Thanks, bye!", 0);
	SetCharStatusPresent(0, 0, false);
	Talking(1);
	SetEventText(1, 4, "I wonder how she'll do against[r]him when he's angry.", 0);
	SetEventText(1, 4, "Oh well, not my problem.[r]Back to work for me.", 0);
	wait(45);
	SetEventText(1, 0, "...is something burning?", 0);
	SetCharStatusPresent(1, 0, false);
	SetCommonData("BGSpeed", 8);
	SetCharStatusPresent(0, 0, false); 
	FadeOutMusic(240);
	wait(240);
	eventhappening = false;
	FinalizeEvent;
}

function CreateFairy_SmallA1(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[1.1,x,y,CSD,color]);
}

function CreateFairy_SmallA2(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[1.2,x,y,CSD,color]);
}

function CreateFairy_SmallA3(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[1.3,x,y,CSD,color]);
}

function CreateFairy_MediumA1(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[2.1,x,y,CSD,color]);
}

function CreateFairy_MediumA2(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[2.2,x,y,CSD,color]);
}

function CreateFairy_MediumA3(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[2.3,x,y,CSD,color]);
}

function SetEnemyLifeToZero{
	let enm = GetAllEnemyID;
	ascent(i in 0..length(enm)) {
		Obj_SetValue(enm[i],"Item",false);
		ObjEnemy_SetLife(enm[i],0);
	}
}

task Title {
    let objStar = ObjPrim_Create(OBJ_SPRITE_2D);
	let logotexture = CSD ~ "sprites/Logo_Tricky2.png";
	
    ObjRender_SetBlendType(objStar,BLEND_ALPHA);
    Obj_SetRenderPriorityI(objStar,90);
    ObjPrim_SetTexture(objStar, logotexture);
    ObjSprite2D_SetSourceRect(objStar,0,0,192,192);
    ObjSprite2D_SetDestCenter(objStar);
   
    let objStage = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(objStage,BLEND_ALPHA);
    Obj_SetRenderPriorityI(objStage,90);
    ObjPrim_SetTexture(objStage, logotexture);
    ObjSprite2D_SetSourceRect(objStage,192,0,512,128);
    ObjSprite2D_SetDestCenter(objStage);
   
    let objTitle = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(objTitle,BLEND_ALPHA);
    Obj_SetRenderPriorityI(objTitle,90);
    ObjPrim_SetTexture(objTitle, logotexture);
    ObjSprite2D_SetSourceRect(objTitle,0,192,512,256);
    ObjSprite2D_SetDestCenter(objTitle);
   
    let objText = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(objText,BLEND_ALPHA);
    Obj_SetRenderPriorityI(objText,90);
    ObjPrim_SetTexture(objText, logotexture);
    ObjSprite2D_SetSourceRect(objText,192,128,512,192);
    ObjSprite2D_SetDestCenter(objText);
   
    let x = GetCenterX-15;
    let y = 140;
    let alphaStar = 0;
    let alphaText = 0;
    let spin = 0;
   
    ObjRender_SetScaleXYZ(objStar,0.6,0.6,0.6);
    ObjRender_SetScaleXYZ(objStage,0.5,0.5,0.5);
    ObjRender_SetScaleXYZ(objTitle,0.7,0.7,0.7);
    ObjRender_SetScaleXYZ(objText,0.8,0.8,0.8);
   
    loop(270) {
        if(x>GetCenterX-95) { x-=1; spin-=180/80; alphaStar+=255/80; }
        else if(alphaText<255){ alphaText+=255/90; }
        ObjRender_SetAlpha(objStar,alphaStar);
        ObjRender_SetAlpha(objStage,alphaText);
        ObjRender_SetAlpha(objTitle,alphaText);
        ObjRender_SetAlpha(objText,alphaText);
        ObjRender_SetPosition(objStar,x,y,0);
        ObjRender_SetPosition(objStage,x,y-3,0);
        ObjRender_SetPosition(objTitle,GetCenterX+90,120,0);
        ObjRender_SetPosition(objText,GetCenterX+90,155,0);
        ObjRender_SetAngleZ(objStar,spin);
        yield;
    }
    loop(90) {
        ObjRender_SetAlpha(objStar,alphaStar);
        ObjRender_SetAlpha(objStage,alphaText);
        ObjRender_SetAlpha(objTitle,alphaText);
        ObjRender_SetAlpha(objText,alphaText);
        alphaStar-=255/90;
        alphaText-=255/90;
        yield;
    }
    Obj_Delete(objStar);
    Obj_Delete(objStage);
    Obj_Delete(objTitle);
    Obj_Delete(objText);
}

task BossName(name) {
	let objtext = ObjText_Create;
	
	ObjText_SetFontSize(objtext,12);
	ObjText_SetFontType(objtext, "Bank Gothic MD");
	// ObjText_SetFontBold(objtext, true);
	ObjText_SetFontColorTop(objtext,255,255,255);
	ObjText_SetFontColorBottom(objtext,0,100,0);
	ObjText_SetFontBorderType(objtext,BORDER_FULL);
	ObjText_SetFontBorderWidth(objtext,1);
	ObjText_SetFontBorderColor(objtext,0,0,0);
	ObjText_SetHorizontalAlignment(objtext,ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(objtext,400);
	Obj_SetRenderPriorityI(objtext,65);
	
	let objEnemy = ID_INVALID;
	let objScene = GetEnemyBossSceneObjectID;
	let alpha = 0;
	
	while(!Obj_IsDeleted(objScene)) {
		if(length(GetEnemyBossObjectID)>0) { objEnemy = GetEnemyBossObjectID[0]; }
		ObjRender_SetAlpha(objtext,alpha);
		ObjText_SetText(objtext,name);
		ObjRender_SetPosition(objtext,5,18,0);
		if(alpha<255 && GetPlayerY>=115) { alpha+=255/30; }
		if(alpha>150 && GetPlayerY<115) { alpha-=255/30; }
		yield;
	}
	Obj_Delete(objtext);
}