#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Stage 1 - Nippy Nameless Path"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let switch = 1;

let EndStateCalled = false;

let eventhappening = false;

let objmidboss = 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"
	
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 ~ "KosukeMN1_H.dnh");
	ObjEnemyBossScene_Add(objmidboss, 0, CSD ~ "KosukeMS1_H.dnh");
	
	ObjEnemyBossScene_Add(objboss, 0, CSD ~ "Kosuke0.dnh");
	ObjEnemyBossScene_Add(objboss, 0, CSD ~ "KosukeN1_H.dnh");
	ObjEnemyBossScene_Add(objboss, 0, CSD ~ "KosukeS1_H.dnh");
	ObjEnemyBossScene_Add(objboss, 1, CSD ~ "KosukeN2_H.dnh");
	ObjEnemyBossScene_Add(objboss, 1, CSD ~ "KosukeS2_H.dnh");
	ObjEnemyBossScene_Add(objboss, 2, CSD ~ "KosukeS3_H.dnh");
	
	ObjEnemyBossScene_LoadInThread(objmidboss);
	ObjEnemyBossScene_LoadInThread(objboss);
	
}
	
@Initialize{

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

@MainLoop{
	
	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 UselessFairies(time, amount){
	loop(amount){
		CreateFairy_SmallA1(rand(192-160, 192), rand(-10, -50), 0, randInt(0, 2));
		wait(time);
		CreateFairy_SmallA1(rand(192, 192+160), rand(-10, -50), 0, randInt(0, 2));
		wait(time);
	}
}

task LessUselessFairies(time, amount){
	loop(amount){
		CreateFairy_SmallA2(rand(192-160, 192), rand(-10, -50), 0, randInt(0, 2));
		wait(time);
		CreateFairy_SmallA2(rand(192, 192+160), rand(-10, -50), 0, randInt(0, 2));
		wait(time);
	}
}

task TStage{
	let color = 0;
	SetCommonData("BGSpeed", 1);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	wait(120);
	PlayMusic(1.1);

	UselessFairies(3, 21);
	wait(120);
	if(GetPlayerX>=GetCenterX){
		CreateFairy_SmallA3(192-96, -15, -1, 2);
		wait(45);
		CreateFairy_SmallA3(192+96, -15, -1, 2);
		wait(45);
	}
	else{
		CreateFairy_SmallA3(192+96, -15, -1, 2);
		wait(45);
		CreateFairy_SmallA3(192-96, -15, -1, 2);
		wait(45);
	}
	CreateFairy_SmallA3(192, -15, -1, 1);
	wait(120);
	LessUselessFairies(3, 21);
	wait(150);
	if(GetPlayerX>=GetCenterX){
		loop(2){
			CreateFairy_SmallA3(192-96, -15, -1, 2);
			wait(44);
			CreateFairy_SmallA3(192+96, -15, -1, 2);
			wait(44);
		}
	}
	else{
		loop(2){
			CreateFairy_SmallA3(192+96, -15, -1, 2);
			wait(44);
			CreateFairy_SmallA3(192-96, -15, -1, 2);
			wait(44);
		}
	}
	CreateFairy_SmallA3(192, -15, -1, 1);
	wait(120);
	LessUselessFairies(8, 16);
	wait(210);
	Title;
	wait(435);
	if(GetPlayerX>=GetCenterX){
		ascent(i in 0..7){ 
			CreateFairy_MediumA1(-20+i, 120+(i*10), -0.25, 2); wait(90); CreateFairy_MediumA1(404-i, 120+(i*10), 0.25, 2); wait(90);
			if(i%2==0){ CreateFairy_MediumA2(192+96*switch, -10, 400, 1); switch*=-1; }
		}
		ascent(i in 7..10){ 
			CreateFairy_MediumA1(-25+i, 120+(i*10), -0.25, 2); CreateFairy_MediumA1(-20+i, 120+(i*10)-80, -0.25, 2); wait(90); CreateFairy_MediumA1(404-i, 120+(i*10), 0.25, 2);
			CreateFairy_MediumA1(409-i, 120+(i*10)-80, 0.25, 2); wait(90);
		}
	}
	else{
		ascent(i in 0..7){ 
			CreateFairy_MediumA1(404-i, 120+(i*10), 0.25, 2); wait(90); CreateFairy_MediumA1(-20+i, 120+(i*10), -0.25, 2); wait(90); 
			if(i%2==0){ CreateFairy_MediumA2(192+96*switch*-1, -10, 400, 1); switch*=-1; }
		}
		ascent(i in 7..10){ 
			CreateFairy_MediumA1(409-i, 120+(i*10), 0.25, 2); CreateFairy_MediumA1(404-i, 120+(i*10)-80, 0.25, 2); wait(90); CreateFairy_MediumA1(-25+i, 120+(i*10), -0.25, 2); CreateFairy_MediumA1(-20+i, 120+(i*10)-80, -0.25, 2); wait(90); 
		}
	}
	CreateFairy_MediumA2(192-96, -10, 450, 2);
	CreateFairy_MediumA2(192+96, -10, 450, 2);
	wait(300);
	CreateFairy_MediumA2(192, -10, 700, 2);
	wait(240);
	WaitIfEnemiesOnScreen(120);
	wait(15);
	SetCommonData("BGSpeed", 4);
	SetEnemyLifeToZero;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	wait(30);
	CollectAllItems;
	ObjEnemyBossScene_Regist(objmidboss);
	BossShadow;
	
	while(!Obj_IsDeleted(objmidboss)){
		yield;
	}

	wait(90);
	
	LessUselessFairies(3, 21);
	wait(300);
	if(GetPlayerX>=GetCenterX){
		ascent(i in 0..5){ 
			CreateFairy_MediumA1(-20+i, 120+(i*10), -0.25, 2); wait(45); CreateFairy_SmallA3(192+96, -15, -1, 0); wait(45);
			CreateFairy_MediumA1(404-i, 120+(i*10), 0.25, 2); wait(45); CreateFairy_SmallA3(192-96, -15, -1, 0); wait(45);
		}
		wait(90);
		ascent(i in 5..10){ 
			CreateFairy_MediumA3(-20+i, 120+(i*10), -0.25, 2); wait(45); CreateFairy_SmallA4(192+96, -10, -1, [0,1][color%2]); wait(45); color++;
			CreateFairy_MediumA3(404-i, 120+(i*10), 0.25, 2); wait(45); CreateFairy_SmallA4(192-96, -10, -1, [0,1][color%2]); wait(45); color++;
		}
	}
	else{
		ascent(i in 0..5){ 
			CreateFairy_MediumA1(404+i, 120+(i*10), 0.25, 2); wait(45); CreateFairy_SmallA3(192-96, -15, -1, [0,1][color%2]); wait(45); color++;
			CreateFairy_MediumA1(-20-i, 120+(i*10), -0.25, 2); wait(45); CreateFairy_SmallA3(192+96, -15, -1, [0,1][color%2]); wait(45); color++;
		}
		wait(90);
		ascent(i in 5..10){ 
			CreateFairy_MediumA3(404-i, 120+(i*10), 0.25, 2); wait(45); CreateFairy_SmallA4(192-96, -10, -1, [0,1][color%2]); wait(45); color++;
			CreateFairy_MediumA3(-20+i, 120+(i*10), -0.25, 2); wait(45); CreateFairy_SmallA4(192+96, -10, -1, [0,1][color%2]); wait(45); color++;
		}
	}
	wait(300);
	SetEnemyLifeToZero;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	wait(15);
	CollectAllItems;
	wait(60);
	
	Event0;
	while(!Obj_IsDeleted(objboss)){
		yield;
	}
	CloseScript(scriptEnemy);
	wait(90);
	Event1;
	eventhappening = true;
	while(eventhappening){ yield; }
	if(GetCommonData("Main Game", false)==false){ CloseStgScene(); }
	else{
	CloseScript(scriptBackground);
	CloseScript(GetOwnScriptID());
	}
}

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);
	SetCutinGraphic(1, 0, kosuke, -40, 100, true);
	Talking(0);
	SetEventText(0, 6, "No leads so far. This winter's[r]not even my fault this time.", 0);
	Talking(1);
	SetEventText(1, 4, "Hey, are you investigating too?[r]Wait up! I've got some leads.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);	
	SetEventText(0, 2, "Hmm? I'll slow down...", 0);
	SetCommonData("BGSpeed", 1);
	Talking(1);
	ObjEnemyBossScene_Regist(objboss); 
	wait(120);
	BossName("Kosuke Suzukaze");
	SetCharStatusPresent(1, 0, true);
	SetEventText(1, 4, "Thanks for waiting warmly.[r]I'm Kosuke, by the way.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinasmug, -70, 45, false);	
	SetEventText(0, 4, "I'd introduce myself, but[r]I don't need introduction.", 0);
	Talking(1);
	SetEventText(1, 0, "...Nope, you don't look familiar.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);	
	SetEventText(0, 4, "Okina? Okina Matara? The[r]Ultimate, Absolute Secret God?", 0);
	Talking(1);
	SetEventText(1, 4, "Stupidly long title...wait![r]Didn't I beat you a while back?", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);
	SetEventText(0, 4, "You and three others.[r]It wasn't a fair fight.", 0);
	Talking(1);
	SetEventText(1, 0, "But the new turn-based format-", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinasmug, -70, 45, false);	
	SetEventText(0, 4, "Regardless, I have an incident to solve.[r]There's no time to talk to small fry.", 0);
	Talking(1);
	SetEventText(1, 5, "Small fry?! I'm not small fry![r]I know what I'm doing!", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinasmug, -70, 45, false);	
	SetEventText(0, 4, "Then you should know[r]to get out of my way.", 0);
	Talking(1);
	SetEventText(1, 0, "Uh, wow, not even Reimu's this mean.", 0);
	PlayMusic(1.2);
	BossCircle;
	BossShadow;
	SetCommonData("BGSpeed", 4);
	SetEventText(1, 4, "If you want me to move,[r]you'll have to make me!", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);
	SetEventText(0, 0, "Oh, what a bother...", 0);
	Talking(1);
	SetEventText(1, 1, "I'll sear some kindness into you!", 0);
	SetCharStatusPresent(0, 0, false); SetCharStatusPresent(1, 0, false);
	NotifyEventAll(EV_KILLBOSS, 1);
	FinalizeEvent;
}
	
task Event1{
	SetCommonData("BGSpeed", 1);
	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, kosukedefeated, -40, 100, true);
	SetCharStatusPresent(1, 0, true);
	Talking(1);
	SetEventText(1, 3, "I'm all burnt out...", 0);
	Talking(0);
	SetEventText(0, 4, "As expected from small fry.[r]Now, tell me where to go!", 0);
	Talking(1);
	SetEventText(1, 3, "...Uh, what?", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinasmug, -70, 45, false);
	SetEventText(0, 0, "You said you had information.", 0);
	Talking(1);
	SetEventText(1, 3, "I said that before I was on fire.", 0);
	Talking(0);
	SetCutinGraphic(0, 0, okinahappy, -70, 45, false);
	SetEventText(0, 4, "Just tell me and you[r]can go put yourself out!", 0);
	Talking(1);
	SetEventText(1, 7, "Since you were so smug,[r]you can help yourself.", 0);
	SetEventText(1, 3, "I'm gonna go take a nap...", 0);
	SetCharStatusPresent(1, 0, false);
	Talking(0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);
	wait(30);
	SetEventText(0, 2, "...He just left?", 0);
	SetCutinGraphic(0, 0, okinaclosed, -70, 45, false);	
	SetEventText(0, 2, "I didn't expect that.", 0);
	SetCutinGraphic(0, 0, okinawhat, -70, 45, false);	
	SetEventText(0, 4, "I mean...it doesn't matter.[r]I can investigate alone!", 0);
	SetCutinGraphic(0, 0, okinahappy, -70, 45, false);	
	SetEventText(0, 1, "Onward!", 0);
	SetCommonData("BGSpeed", 4);
	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_SmallA4(x,y,CSD,color) {
	NotifyEventAll(EV_SPAWN_ENEMY,[1.4,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);
	}
}

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

task Title {
    let objStar = ObjPrim_Create(OBJ_SPRITE_2D);
	let logotexture = CSD ~ "sprites/Logo_Tricky.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,100,0,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);
}
