//===== Hercules Script ======================================
//= MVP Spawn Timer
//===== By: ==================================================
//= jawbreaker
//===== Current Version: =====================================
//= 1.1
//===== Description: =========================================
//= Control MVP Spawn with script. MVP will not respawn on server start or reload using flag.
//===== Additional Comments: =================================
//= 1.0 Remove/Comment all MVP spawn at npc/pre-re/mobs/dungeons/ or /fields.  
//= 1.1 Initialize TimeStamp when not set to all MVP
//       Fixed typo on MVP_Map array.
//= 1.2 Fixed Variables
//============================================================

// @unloadnpc mvpcontroller
// @loadnpc npc/valhalla/mvpcontroller.txt

-	script	mvpcontroller	-1,{
end;

OnReset:
	for( .@i = 0; .@i < getarraysize(.mvpid); .@i++ ) {
		setd("$S"+.mvpva[.@i]+.mvpid[.@i], 0);
		setd("$T"+.mvpva[.@i]+.mvpid[.@i], 0);
		setd("$P"+.mvpva[.@i]+.mvpid[.@i]+"$","");	
	}
end;

OnMvPEvent:
	if(strcharinfo(PC_MAP) == "guild_vs5") end; // BTA
	getitem Green_Voucher,1;
	chanmsg("#system","Player [ "+strcharinfo(PC_NAME)+" ] received 1x "+getitemname(Green_Voucher)+" for slaying an MvP.");
	if(killedrid == 1373){	// LOD
		$P11373$ = strcharinfo(PC_NAME);	
	} else {
		$LMVP$ = strcharinfo(PC_NAME);
	}
	end;

OnTimer60000:
	// Check every 1 minute
	for( .@i = 0; .@i < getarraysize(.mvpid); .@i++ ) {
		// Monster has died and has attached time
		if( getd("$T"+.mvpva[.@i]+.mvpid[.@i]) && getd("$S"+.mvpva[.@i]+.mvpid[.@i]) == 2){
			// Monster time has expired
			if( (getd("$T"+.mvpva[.@i]+.mvpid[.@i]) ) <= gettimetick(2) ) {
				// I'ts alive again
				if(.mvpid[.@i] == 1373){
					donpcevent("NifInvasion::OnSpawn");
				} else {
					setd("$S"+.mvpva[.@i]+.mvpid[.@i], 1);
					setd("$T"+.mvpva[.@i]+.mvpid[.@i], 0);
					monster .mvpmaps$[.@i],0,0,"--ja--",.mvpid[.@i],1;
				}
			}	
		}
	}
	setnpctimer 0;
	end;

OnNPCKillEvent:
	if( getmonsterinfo(killedrid, MOB_MVPEXP) > 0 || killedrid == 1096 || killedrid == 1582 ) {

		// Process Beelzebub (this monster transform from 1873 to 1874 so we manually set this.
		if( killedrid == 1874 && strcharinfo(PC_MAP) == "abbey03" ){
			$S11873 = 2;				// Set ChonChon Beelze as DEAD
			$T11873 = gettimetick(2)+(720*60);	// Set ChonChon Beelze next respawn
			$P11873$ = $LMVP$;			// Set ChonChon Beelze who killed	

		// process other MVP Monsters
		} else {
			for( .@i = 0; .@i < getarraysize(.mvpid); .@i++ ) {
				if( killedrid == .mvpid[.@i] && strcharinfo(PC_MAP) == .mvpmaps$[.@i]) {
						
					// 1 - Alive  2 - Dead
					setd("$S"+.mvpva[.@i]+.mvpid[.@i], 2);

					// current time + respawn time (seconds)
					setd("$T"+.mvpva[.@i]+.mvpid[.@i],gettimetick(2)+(.mvptime[.@i]*60));

					// record also Angeling and Devilling
					if(killedrid == 1096 || killedrid == 1582 ) {
						setd("$P"+.mvpva[.@i]+.mvpid[.@i]+"$", strcharinfo(PC_NAME));
					} else {
						setd("$P"+.mvpva[.@i]+.mvpid[.@i]+"$", $LMVP$);
					}
				}
			}
		}
	}
	end;

OnInit:
	.debug = 0;
	bindatcmd("mvpreset","mvpcontroller::OnReset",99,99);
	// Mvp ids
	setarray .mvpid[0],1038,1039,1046,1059,1086,1087,1087,1112,1115,1147,1150,1157,1159,1190,1251,1252,1272,1312,1389,1418,1492,1511,1583,1623,1630,1658,1685,1688,1719,1734,1751,1768,1785,1785,1785,1832,1871,1873,1885,1917,1373,1096,1096,1096,1582,1582;
	setarray .mvpva[0],1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,1,1,1,1,1,1,1,2,3,1,2;
	setarray .mvpmaps$[0],"moc_pryd04","prt_maze03","gef_dun02","mjolnir_04","prt_sewb4","gef_fild02","gef_fild14","treasure02","pay_fild11","anthell02","pay_dun04","in_sphinx5","moc_fild17","gef_fild10","xmas_dun02","xmas_fild01","gl_chyard","tur_dun04","gef_dun01","gon_dun03","ama_dun03","moc_pryd06","beach_dun","ein_dun02","lou_dun03","lhz_dun02","jupe_core","ayo_dun02","abyss_03","kh_dun02","odin_tem03","ra_san05","ra_fild02","ve_fild01","ve_fild02","thor_v03","abbey02","abbey03","mosk_dun03","moc_fild22","niflheim","pay_fild04","xmas_dun01","yuno_fild03","pay_fild04","yuno_fild03";
	setarray .mvptime[0],60,120,120,120,60,1440,60,120,120,120,60,60,120,120,60,120,60,60,60,120,91,60,300,125,120,120,120,420,180,120,480,300,240,180,360,660,120,720,120,720,133,60,60,60,120,60;

	// Respawn only undead MVP's on server start or @reload
	for( .@i = 0; .@i < getarraysize(.mvpid); .@i++ ) {
		// 1 - Alive  2 - Dead
		if ( getd("$S"+.mvpva[.@i]+.mvpid[.@i]) == 0 || getd("$T"+.mvpva[.@i]+.mvpid[.@i]) == 0  ) {
			if(.mvpid[.@i] == 1373){
				donpcevent("NifInvasion::OnSpawn");
			} else {
				monster .mvpmaps$[.@i],0,0,"--ja--",.mvpid[.@i],1;
			}
			setd("$S"+.mvpva[.@i]+.mvpid[.@i], 1);
			setd("$P"+.mvpva[.@i]+.mvpid[.@i]+"$", "");
		}
	}
	initnpctimer;
	end;
}
