#include "../functions/functions_enemy.txt"
#include "./eventlist.txt"
#include "./patternlist.txt"
#include "./chapterlist.txt"

let del = 0;
let eve = [];
let army;
let divis;
let rank;

function FStage
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "events/Frog.png");
	ObjSprite2D_SetSourceRect(obj,0,0,1,1);
	ObjSprite2D_SetDestRect(obj,-8,-8,GetStgFrameWidth+8,GetStgFrameHeight+8);
	Obj_SetRenderPriority(obj,0.21);
	MoveEnable = true;
	loop(stgHeight-1)
		{
		FSelect;
		}
	MoveEnable = false;
	loop(60){yield;}
	SetStageDir(0);
	MoveSound = 0;
	Obj_Delete(obj);
}

function FSelect
{
	let scale = 0.33;

	let vary = [1,1];
	army = -1;	
	divis = max(1,min(10,floor(rand(vary[0],vary[1]))));
	rank = min(10,floor(rand(1,4)*(Dif+1)));

	FArmy(army,divis,rank);
	if(army == -1)
		{
		alternative(divis)
			case(1)
				{
				CreateChapterA1;
				}
		}
	if(army == 1)
		{
		alternative(divis)
			case(1)
				{
				CreateChapterB1;
				}
		}
	loop(720){yield;}
	ClearStage;
	loop(30){yield;}
	del = 2;
	PlaySound("se_Shift",130);
	MoveEnable = false;
	FShiftStage;
	loop(45){yield;}
	SetStageDir(0);
	MoveEnable = true;
	del = 0;		
	eve = [];
	army = 0;
	divis = 0;
	rank = 0;

	function FArmy(army,divis,rank)
		{
		let objA = ObjPrim_Create(OBJ_SPRITE_2D);
		if(army == -1)
			{
			ObjPrim_SetTexture(objA,GetCurrentScriptDirectory ~ "events/Snake.png");
			}
		else
			{
			ObjPrim_SetTexture(objA,GetCurrentScriptDirectory ~ "events/Frog.png");
			}
		ObjSprite2D_SetSourceRect(objA,0,0,280,73);
		Obj_SetRenderPriority(objA,0.75);
		ObjSprite2D_SetDestRect(objA,-140,-36,140,37);
	
		let objD = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objD,GetCurrentScriptDirectory ~ "events/division" ~ IntToString(divis) ~ ".png");
		ObjSprite2D_SetSourceRect(objD,-10,0,280-10,73);
		ObjSprite2D_SetDestRect(objD,-140,-36,140,37);
		Obj_SetRenderPriority(objD,0.75);
	
		let objR = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objR,GetCurrentScriptDirectory ~ "events/rank" ~ IntToString(rank) ~ ".png");
		ObjSprite2D_SetSourceRect(objR,0,0,73,280);
		ObjSprite2D_SetDestRect(objR,-36,-140,37,140);
		Obj_SetRenderPriority(objR,0.75);

		let numb = min(4,floor(rand(0,(Dif+1)*4)));
		numb = 0;
		ascent(i in 0..numb)
			{
			TEventStart(i);
			}

		let speed = 0;
		let dist = -160;
		ascent(i in 0..30)
			{
			speed+=20/30;
			dist+=speed;
			ObjRender_SetPosition(objA,dist,100,0);
			ObjRender_SetPosition(objD,dist,100,0);
			ObjRender_SetPosition(objR,GetStgFrameWidth-60,dist,0);
			yield;
			}
		loop(numb*30+60){yield;}
		del = 1;
		descent(t in 0..30)
			{
			ObjRender_SetAlpha(objA,t*255/30);
			ObjRender_SetAlpha(objD,t*255/30);
			ObjRender_SetAlpha(objR,t*255/30);
			yield;
			}
		loop(15){yield;}
		TDel1;
		task TDel1
			{
			ObjSprite2D_SetDestRect(objA,0,-75*scale,280*scale,0);
			ObjSprite2D_SetDestRect(objD,0,-75*scale,280*scale,0);
			Obj_SetRenderPriority(objA,0.19);
			Obj_SetRenderPriority(objD,0.19);
			Obj_SetRenderPriority(objR,0.19);
			ObjRender_SetPosition(objA,12,GetStgFrameTop+GetStgFrameHeight-8,0);
			ObjRender_SetPosition(objD,12,GetStgFrameTop+GetStgFrameHeight-8,0);
			Obj_Delete(objR);
			ascent(t in 0..30)
				{
				ObjRender_SetAlpha(objA,t*255/30);
				ObjRender_SetAlpha(objD,t*255/30);
				yield;
				}
			while(del == 1){yield;}
			descent(t in 0..20)
				{
				ObjRender_SetAlpha(objA,t*255/20);
				ObjRender_SetAlpha(objD,t*255/20);
				yield;
				}
			Obj_Delete(objA);
			Obj_Delete(objD);
			}
		task TEventStart(numb)
			{	
			let name = floor(rand(0,23));
			let switch = false;
			while(switch == false)
				{
				switch = true;
				ascent(i in 0..length(eve))
					{
					if(switch == true)
						{
						if((name == eve[i]) || ((name == 2 || name == 3 || name == 4) && (eve[i] == 2 || eve[i] == 3 || eve[i] == 4)) || ((name == 8 || name == 9 || name == 10) && (eve[i] == 8 || eve[i] == 9 || eve[i] == 10)) || ((name == 14 || name == 15 || name == 16) && (eve[i] == 14 || eve[i] == 15 || eve[i] ==16)) || ((name == 7 || name == 19) && (eve[i] == 7 || eve[i] == 19)))
							{
							name = floor(rand(0,23));
							switch = false;
							}
						}
					}
				}
			alternative(name)
				case(0)
					{
					EBombSeal;
					}
				case(1)
					{
					EBeer;
					}
				case(2)
					{
					EHardEnemy(2);
					}
				case(3)
					{
					EHardEnemy(0.5);
					}
				case(4)
					{
					EHardEnemy(0);
					}
				case(5)
					{
					ELaser;
					}
				case(6)
					{
					EGravity;
					}
				case(7)
					{
					EPacifist;
					}
				case(8,9,10)
					{
					EPower(name-7);
					}
				case(11)
					{
					EItem;
					}
				case(12)
					{
					EBreak;
					}
				case(13)
					{
					EDrain;
					}
				case(14,15,16)
					{
					EBlind((name-14));
					}
				case(17)
					{
					EDrunk;
					}
				case(18)
					{
					EPierce;
					}
				case(19)
					{
					EPoison;
					}
				case(20)
					{
					EReverse;
					}
				case(21)
					{
					ESlow;
					}
				others
					{
					ELife;
					name = 100;
					}
			eve = eve ~ [name];
			loop(numb*30+30){yield;}
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "events/event_" ~ IntToString(name) ~ ".png");
			ObjSprite2D_SetSourceRect(obj,0,0,30,136);
			ObjSprite2D_SetDestRect(obj,-15,-68,15,68);
			Obj_SetRenderPriority(obj,0.75);
			let speed = 0;
			let dist = -150;
			ascent(i in 0..30)
				{
				speed+=23.5/30;
				dist+=speed;
				ObjRender_SetPosition(obj,30+numb*45,dist,0);
				yield;
				}
			TDel2;
			task TDel2
				{
				while(del == 0){yield;}
				descent(t in 0..30)
					{
					ObjRender_SetAlpha(obj,t*255/30);
					yield;
					}
				ObjSprite2D_SetDestRect(obj,0,0,30*2.5*0.275,136*2.5*0.275);
				Obj_SetRenderPriority(obj,0.91);
				ObjRender_SetPosition(obj,2.5+(15+numb*45)*2.25*0.25,5+GetScreenHeight/2+50,0);
				ascent(t in 0..30)
					{
					ObjRender_SetAlpha(obj,t*255/30);
					yield;
					}
				while(del == 1){yield;}
				descent(t in 0..20)
					{
					ObjRender_SetAlpha(obj,t*255/20);
					yield;
					}
				Obj_Delete(obj);
				}	
			}
		}
}