// @unloadnpc invasions
// @loadnpc npc/thor/invasion.txt


-	script	invasions	HIDDEN_NPC,{

OnInit:

	bindatcmd("invasion",strnpcinfo(3) + "::OnSetup",0,99);
	.invasion = 0;
	.GM = 1;
	.server_name$ = "ClassicRO";
	setarray(.maps$[0],"geffen","izlude","aldebaran","morocc","payon","prontera","alberta","comodo","yuno");
	setarray(.mapnames$[0],"Geffen","Izlude","Aldebaran","Morocc","Payon","Prontera","Alberta","Comodo","Juno");
	setarray(.mvps[0],1719,1039,1751,1768,1734,1373,1272,1190,1685,1159,1087,1038,1511,1252,1251,1150,1115,1046,1147,1583,1389,1708,1832,1874,1917);
	setarray(.itemids[0],616,2,617,6,603,20,7179,2,30090,1,7179,12,30090,4);



	
	// set default setting here:
	.Map$ = "geffen";		// Default Map	
	.mapName$ = "Geffen";		// Default Map Name
	.mvp = 1719;			// Default MvPID
	.r_itemid = 603;		// Default Reward ItemID
	.r_qty = 20;			// Default Reward Quantity
	.rpoints = 1;			// points quantity
	end;

OnSetup:
	mes("[^0000ffInvasion Manager^000000]");
	if (.invasion) {
		getmapxy(.@map$, @x, @y, 0);
		if (getmapflag(strcharinfo(3), mf_pvp)) {
			mes("Sorry, you can't join event from this map");
			close();
		}
		if (getmapflag(strcharinfo(3), mf_nowarp)) {
			mes("Sorry, you can't join event from this map");
			close();
		}
		if (spectator) {
			mes("Sorry, you can't join this event while you are at spectator mode.  use @bta and leave.");
			close();
		}
		if (.deadlock && !Hp) {
			mes("You may not use event when you are dead.");
			close();
		}
		mes("Status: ^28bf00Active^000000");
		mes("Map: ^28bf00"+.mapName$+"^000000");
		mes("MvP: ^28bf00"+getmonsterinfo(.mvp,0)+"^000000");
		mes("Kill Reward: ^28bf00"+.r_qty+"x "+getitemname(.r_itemid)+"^000000");
		mes("Normal Mob Reward: ^28bf00"+.rpoints+" Online Points^000000");

	} else {
		mes("Status: ^FF0000Inactive^000000");
	}	
	switch(select(
		 ((getgmlevel() < .GM)?":":" > ^FF0000Setup^000000:")+
		 ((.invasion && !etatus)?" > ^0055FFWarp to Event^000000:":":")+
		 ((etatus)?" > ^0055FFLeave Event^000000:":":")+
		 " > ^777777Close^000000"
	)) {
	case 1:
		while(1){
		switch(select(
			((!.invasion)?"Map [ ^28bf00"+.mapName$+"^000000 ]:":":")+
			((!.invasion)?"MvP [ ^28bf00"+getmonsterinfo(.mvp,0)+"^000000 ]:":":")+
			((!.invasion)?"MvP Reward [ ^28bf00"+.r_qty+"x "+getitemname(.r_itemid)+"^000000 ]:":":")+
	 		((!.invasion)?" > ^28bf00Start^000000 Invasion:":":")+
			((.invasion)?" > ^FF0000Stop^000000 Invasion:":":")+
			"- ^777777Close^000000"
		)) {
		case 1:
			.@menu$ = "";
			for( .@i = 0; .@i < getarraysize(.mapnames$); ++.@i )
				.@menu$ = .@menu$ + "^FF0000>^0000FF ["+.mapnames$[.@i]+"]:";
				.@select = select( .@menu$ ) - 1;
				.Map$ = .maps$[.@select];
				.mapName$ = .mapnames$[.@select];
				break;
		case 2: 
			.@menu$ = "";
			for( .@i = 0; .@i < getarraysize(.mvps); ++.@i )
				.@menu$ = .@menu$ + "^FF0000>^0000FF ["+getmonsterinfo(.mvps[.@i],0)+"]:";
				.@select = select( .@menu$ ) - 1;
				.mvp = .mvps[.@select];
				break;
		case 3:
			.@menu$ = "";
			for( .@i = 0; .@i < getarraysize(.itemids); .@i += 2 )
				.@menu$ = .@menu$ + "^FF0000>^0000FF ["+.itemids[.@i+1]+"x "+getitemname(.itemids[.@i])+"]:";
				.@select = select( .@menu$ ) - 1;
				.r_itemid = .itemids[.@select*2];
				.r_qty = .itemids[.@select*2+1];
				break;
		case 4:
			donpcevent ("invasions::OnStart");
			break;
		case 5:
			donpcevent ("invasions::OnStop");
			break;
		default:
			close();	
		}	
		}
		close();
	case 2:
		/* Save current state and location and warp to event area */
		getmapxy(.@map$, @x, @y, 0);
		elastmap$ = .@map$;
		elastx = @x;
		elasty = @y;
		etatus = 1;
		ehp = Hp;
		esp = Sp;
		percentheal(100, 100);
		warp(.Map$,0,0);
		break;
	case 3:
		etatus = 0;
		Hp = ehp;
		Sp = esp;
		warp(elastmap$,elastx,elasty);
		break;
	default:
		close();
	}
	end;

OnStart:
	if (.invasion) {
		donpcevent (strnpcinfo(3)+"::OnStop");
		end;
	}
	.invasion = 1;
	setbattleflag("item_free_town", 1);
	sleep2 1000;
	if (.mvp == 1719) {		// Detale
		setarray(.@mobs[0],1720,10,1717,20,1714,20,1716,20,1713,20);
	}
	else if (.mvp == 1039) {	// Baphomet
		setarray(.@mobs[0],1294,20,1101,30,1037,20,1120,10,1259,10);
	}
	else if (.mvp == 1751) {	// Valkyrie Randgris
		setarray(.@mobs[0],1753,10,1761,10,1762,10,1693,50);
	}
	else if (.mvp == 1768) {	// Gloom Under Night
		setarray(.@mobs[0],1774,50,1773,50,1772,30,1770,20,1769,20);
	}
	else if (.mvp == 1734) {	// Kiel D-01
		setarray(.@mobs[0],1745,20,1738,60,1737,30,1736,30,1735,40);
	}
	else if (.mvp == 1373) {	// Lord of Death
		setarray(.@mobs[0],1507,20,1505,20,1506,50,1504,50);
	}
	else if (.mvp == 1272) {	// Dark Lord
		setarray(.@mobs[0],1120,5,1291,50,1292,50,1298,50,1302,50);
	}
	else if (.mvp == 1190) {	// Orc Lord
		setarray(.@mobs[0],1023,50,1273,50,1213,50,1096,5,1189,50);
	}
	else if (.mvp == 1685) {	// Vesper
		setarray(.@mobs[0],1678,30,1677,30,1676,30,1673,40,1672,40,1671,40,1670,40,1668,40);
	}
	else if (.mvp == 1159) {	// Phreeoni
		setarray(.@mobs[0],1001,30,1042,30,1119,30,1127,40,1138,40);
	}
	else if (.mvp == 1087) {	// Orc Hero
		setarray(.@mobs[0],1023,30,1189,30,1213,30,1273,40);
	}
	else if (.mvp == 1038) {	// Osiris
		setarray(.@mobs[0],1029,30,1041,30,1191,30,1297,40);
	}
	else if (.mvp == 1511) {	// Amon Ra
		setarray(.@mobs[0],1032,30,1191,30,1194,30,1297,40);
	}
	else if (.mvp == 1252) {	// Garm
		setarray(.@mobs[0],1243,30,1515,30,1037,20,1120,10,1259,10);
	}
	else if (.mvp == 1251) {	// Stormy
		setarray(.@mobs[0],1246,30,1248,30,1249,20,1265,20,1515,20);
	}
	else if (.mvp == 1150) {	// Moonlight
		setarray(.@mobs[0],1016,30,1129,30,1180,20,1290,20,1301,2,1307,5);
	}
	else if (.mvp == 1115) {	// Eddga
		setarray(.@mobs[0],1180,30,1321,30,1180,20,1290,20,1301,2,1307,5);
	}
	else if (.mvp == 1046) {	// Doppel
		setarray(.@mobs[0],1035,30,1036,30,1061,20,1109,20,1143,20,1179,20);
	}
	else if (.mvp == 1147) {	// Maya
		setarray(.@mobs[0],1005,30,1036,30,1061,20,1109,20,1143,20,1179,20);
	}
	else if (.mvp == 1583) {	// Tao Gunka
		setarray(.@mobs[0],1148,30,1255,30,1256,20,1264,20,1143,20,1179,20);
	}
	else if (.mvp == 1389) {	// Dracula
		setarray(.@mobs[0],1035,30,1036,30,1061,20,1109,20,1143,20,1179,20);
	}
	else if (.mvp == 1708) {	// Thana
		setarray(.@mobs[0],1035,30,1036,30,1061,20,1109,20,1143,20,1179,20);
	}
	else if (.mvp == 1832) {	// Ifrit
		setarray(.@mobs[0],1829,30,1830,30,1831,20,1833,20,1839,20);
	}
	else if (.mvp == 1874) {	// Beelz
		setarray(.@mobs[0],1829,30,1830,30,1831,20,1833,20,1839,20);
	}
	else if (.mvp == 1917) {	// Satan
		setarray(.@mobs[0],1918,30,1919,30,1920,30,1921,20);
	}
	 else {

	}

	sleep2 1000;
	announce("[ Rune-Midgard Guard ]: We have trouble here in the town " + .mapName$ + "!", bc_all);

	sleep2 5000;
	announce("[ Rune-Midgard Guard ]: Everyone, we need your help to get rid of these mobs!", bc_all);

	.totalmob = 0; .mobs_left = 0;
	for ( .@i = 0; .@i < getarraysize(.@mobs); .@i += 2 ) {
		monster (.Map$,0,0,getmonsterinfo(.@mobs[.@i], 0 ),.@mobs[.@i], .@mobs[.@i+1],strnpcinfo(3)+"::OnMyMobDead");
		.totalmob = .totalmob +.@mobs[.@i+1];
	}
	.mobs_left = .totalmob;
	end;

/* Announce every 5 minutes */
OnMinute00:
OnMinute05:
OnMinute10:
OnMinute15:
OnMinute20:
OnMinute25:
OnMinute30:
OnMinute35:
OnMinute40:
OnMinute45:
OnMinute50:
OnMinute55:
	if (.invasion) {
		announce("[ Rune-Midgard Guard ]: Invasion event is currently being held at " + .mapName$ + "!  Use @invasion to join.", bc_all);
	}
	end;

/* Kill normal mobs after 30 minutes */
OnTimer1805000:
	killmonster (.Map$,strnpcinfo(3)+"::OnMyMobDead");
	.mobs_left = 0;
	end;

OnStop:
	if (.invasion) {
		if (.mobs_left) {
			killmonster (.Map$,"invasions::OnMyMobDead");
			killmonster (.Map$,"invasions::OnSpecialMobDead");
			//announce("It seems that Game Master has ended the Invasion!.  Please use @invasion and leave to return to your previous location.",bc_all);
			.mobs_left = 0;
			.invasion = 0;
			recovery(.Map$);	
			setbattleflag("item_free_town", 0);
		} else {
			.mobs_left = 0;
			.invasion = 0;
			recovery(.Map$);	
			setbattleflag("item_free_town", 0);		
		}
	}
	end;

OnMyMobDead:
	--.mobs_left;
	if (!.mobs_left) {
		setbattleflag("item_free_town", 0);
		announce("[ Rune-Midgard Guard ]: "+getmonsterinfo(.mvp,0)+" has spawned in " + .Map$ + "!", bc_all);
		monster (.Map$,0,0,getmonsterinfo(.mvp,0),.mvp,1,strnpcinfo(3)+"::OnSpecialMobDead");

	} else {
		specialeffect(EF_ANGEL, AREA, playerattached());
		#PCTIME += .rpoints;
		dispbottom ("Gained "+.rpoints+" Online Points [Total: "+#PCTIME+"]"); 
		announce("["+.mobs_left+"/"+.totalmob+"] Monster left before the main event.",bc_map);
	}
	end;

OnSpecialMobDead:
	killmonster (.Map$,"invasions::OnMyMobDead");
	killmonster (.Map$,"invasions::OnSpecialMobDead");
	recovery(.Map$);
	end;

OnMvPEvent:
	if (.invasion && strcharinfo(3) == .Map$) {
		specialeffect(EF_ANGEL, AREA, playerattached());
		#PCTIME += .rpoints;
		dispbottom ("Gained "+.rpoints+" Online Points [Total: "+#PCTIME+"]"); 
		announce(strcharinfo(0)+" has fought off the "+ getmonsterinfo(.mvp,0) +" Invasion and has been awarded [ "+.r_qty+"x "+getitemname(.r_itemid)+" ] worthy prize!  Use @invasion and leave to return to your previous location.", bc_all);
		getitem (.r_itemid,.r_qty);
		.invasion = 0;
		setbattleflag("item_free_town", 0);
	}
	end;
}
