//===== rAthena Script =======================================
//= Guild Mission - Core Script
//===== Description: =========================================
//= Template for guild mission added in episode 14.3
//===== Changelogs: ==========================================
//= [Official Conversion]
//= 1.0 First version [Secretdataz]
//= 1.1 Fixed potential exploit from original Aegis script. [Secretdataz]
//============================================================

-	script	gld_mission#core	-1,{
function brief;
function mob_brief;
function calcexp;
function calcjobexp;

	if (!checkweight(1201,3)){
		mes "You have too many items to proceed with the quest.";
		mes "Please lighten your load and try again.";
		close;
	}

	.@zone$ = strnpcinfo(2);
	if(.@zone$ == "gqaldeg"){
		.@zoneid = 3;
		.@edition = 1;
	} else if(.@zone$ == "gqaru"){
		.@zoneid = 4;
		.@edition = 2;
	} else if(.@zone$ == "gqgefg"){
		.@zoneid = 2;
		.@edition = 1;
	} else if(.@zone$ == "gqpayg"){
		.@zoneid = 1;
		.@edition = 1;
	} else if(.@zone$ == "gqprtg"){
		.@zoneid = 0;
		.@edition = 1;
	} else if(.@zone$ == "gqsch"){
		.@zoneid = 5;
		.@edition = 2;
	} else if(.@zone$ == "gqtealde"){
		.@zoneid = 6;
		.@edition = 3;
	} else if(.@zone$ == "gqteprt"){
		.@zoneid = 7;
		.@edition = 3;
	} else {
		// Custom error message.
		debugmes "Guild mission script called from an unknown agit region [" + .@zone$ + "].";
		end;
	}
	.@npcname$ = .npcname$[.@zoneid];

	mes .@npcname$;
	mes "We provide missions and daily quests that you can complete in the region of " + .region$[.@zoneid] + ".";
	mes "There are simple daily quests as well as missions that you have to complete within the siege hours only. So, think about it~";
	next;
	switch(select("Conquering " + .region$[.@zoneid], "Guild Dungeon Daily Quests", "About quests", "I don't need it.")){
		case 1: // Conquering.
			.@gid = getcharid(2);
			if(.@gid && getguildmaster(.@gid) == strcharinfo(0)){
				.@time_check1 = checkquest(.conquer_delay_questid[.@zoneid], PLAYTIME);
				if(.@time_check1 == 0){
					mes .@npcname$;
					mes "It's been less than an hour since the last time we've checked!";
					mes "Come back when the waiting time is over.";
					next;
					mes .@npcname$;
					mes "If you have luck on your side, you'll have good results.";
					close;
				} else if(.@time_check1 == 2){
					erasequest .conquer_delay_questid[.@zoneid];
					mes .@npcname$;
					mes "I have confirmed that the waiting time for conquering missions has ended.";
					mes "I'll delete unnecessary records for you.";
					close;
				} else {
					.@siege_check = (.@edition == 1 ? agitcheck() : .@edition == 2 ? agitcheck2() : agitcheck3());
					.@time = atoi(gettimestr("%H%M",4));
					.@after_time = ((.weekday[.@zoneid] == gettime(4)) && ((.@time > .start_time[.@zoneid]) && (.@time < .end_time[.@zoneid])));

					if(.@siege_check && .@after_time){
						mes .@npcname$;
						mes "According to ^4d4dffthe number of fortresses that your guild has conquered at this very moment,^000000 I'll give you a reward.";
						mes "Do you want to check right now? You're aware that ^4d4dffthe number can change even as we speak,^000000 right?";
						next;
						if(select("I'll come back later.","Let's check right now!") == 1){
							mes .@npcname$;
							mes "Right. Life is all about timing.";
							mes "Come back when it's shining the most.";
							close;
						} else {
							.@total = 0;
							for(.@i = 0; .@i < 5; ++.@i){
								.@total += (.@gid == getcastledata(.castle$[.@i+.@zoneid*5],1));
							}

							if(!.@total){
								mes .@npcname$;
								mes "Unfortunately, there is no fortress that your guild has conquered.";
								mes "Try again!";
								close;
							} else {
								mes .@npcname$;
								if(.@total == 5)
									mes "Incredible.";
								else
									mes "Currently, the total number of fortresses that your guild has conquered";
								for(.@i = 0; .@i < 5; ++.@i){
									mes getcastlename(.castle$[.@i+.@zoneid*5]) + (.@gid == getcastledata(.castle$[.@i+.@zoneid*5],1) ? ": ^4d4dffOccupied^000000" : "");
								}
								if(.@total == 5)
									mes "Your guild has conquered " + .region$[.@zoneid] + "!";
								else
									mes "is " + .@total + "!";
								next;
								mes .@npcname$;
								mes "I'll give you a reward accordingly. I hope you make good use of it.";
								mes "You have an hour of waiting time until the next report. Keep that in mind and try again.";
								setquest .conquer_delay_questid[.@zoneid];
								getitem 6489,.@total;
								getitem 6615,.@total;
								getitem 12888,.@total;
								close;
							}
						}
					} else {
						mes .@npcname$;
						mes "I'm sorry but you can complete this mission only during the siege hours. I'm afraid I can't do anything here.";
						close;
					}
				}
			} else {
				mes .@npcname$;
				mes "I'm sorry but conquering missions are for guild masters only.";
				mes "Members should try guild dungeon daily quests instead.";
				close;
			}
			break;
		case 2: // Daily Quests
			mes .@npcname$;
			mes "These are missions that you can complete in guild dungeons of " + .region$[.@zoneid] + " area.";
			mes "You can cancel them if you want to. Feel free to challenge yourself.";
			next;
			.@cancel_index = (.@edition == 1 ? 3 : 2);
			for(.@i = 0; .@i < 3; ++.@i){
				if(.daily_hunting_questid[.@zoneid*3 + .@i])
					.@menu$ = .@menu$ + .region$[.@zoneid] + " Mission " + (.@i+1) + (isbegin_quest(.daily_hunting_questid[.@zoneid*3 + .@i]) ? " ^4d4dffCheck the result^000000" : "") + ":";
			}
			.@sel = select(.@menu$) - 1;
			.@index = .@zoneid*3 + .@sel;
			if(.@sel == .@cancel_index){
				mes .@npcname$;
				mes "You don't need to rush since these missions take place in guild dungeons.";
				mes "Feel free to give it a try whenever you want to.";
				close;
			}
			if(BaseLevel < .level_req[.@index]){
				mes .@npcname$;
				mes "Hmm, unfortunately, this mission is limited to users with ^4d4dfflevel " + .level_req[.@index] + "^000000 or higher only.";
				mes "Your level is not high enough.";
				close;
			}
			.@hunting = checkquest(.daily_hunting_questid[.@index], HUNTING);
			.@playtime = checkquest(.daily_delay_questid[.@index], PLAYTIME);
			if(.@hunting > -1){
				if(.@hunting == 2){
					mes .@npcname$;
					mes "Oh, you've done a good job.";
					mes "It isn't much, but hopefully it will be helpful.";
					setquest .daily_delay_questid[.@index];
					erasequest .daily_hunting_questid[.@index];
					getexp calcexp(.@zoneid,.@index),calcjobexp(.@edition);
					getitem 6615,1;
				} else {
					mes .@npcname$;
					mes "Hmm? It seems like you haven't completed the mission yet…";
					mes "Is there any problem?";
					next;
					if(select("No.","Give up the quest.") == 1){
						mes .@npcname$;
						mes "I'm not sure how long you can use the guild dungeon, but good luck.";
						close;
					} else {
						mes .@npcname$;
						mes "Well, no one's forcing you.";
						mes "Do whatever you like to do.";
						mes "I'll cancel the mission then.";
						erasequest .daily_hunting_questid[.@index];
						close;
					}
				}
			} else {
				if(.@playtime == 0 || .@playtime == 1){
					mes .@npcname$;
					mes "The rule is set here so that you're only allowed to complete one mission each day.";
					mes "I would like you to come back when the waiting time is over.";
					close;
				} else if(.@playtime == 2){
					erasequest .daily_delay_questid[.@index];
					mes .@npcname$;
					mes "I have confirmed that the waiting time for this mission is over.";
					mes "I'll delete the records used for confirmation.";
					mes "Now, give it a try to complete the quest!";
					close;
				} else {
					mes .@npcname$;
					mes "This is the " + .ordinal$[.@sel] + " mission in " + .region$[.@zoneid] + ".";
					brief(.daily_hunting_questid[.@index]);
					next;
					mes .@npcname$;
					if(mob_brief(.daily_hunting_questid[.@index]))
						mes "I'll reward you accordingly upon completing this mission.";
					else
						mes "I'll give you experience points in return.";
					next;
					if(select("Accept it.","Reject it.") == 1){
						mes .@npcname$;
						mes "Entrance to the guild dungeon is restricted. But it's not like you can't find a way to go there, either.";
						mes "I'll pray for your success in fighting those monsters.";
						setquest .daily_hunting_questid[.@index];
						close;
					} else {
						mes .@npcname$;
						mes "Well, do as you wish.";
						close;
					}
				}
			}
			break;
		case 3: // About quests
			mes .@npcname$;
			mes "It's not difficult at all.";
			mes "I give a reward ^4d4dffdepending on how many fortresses you have conquered.^000000";
			mes "^4d4dffThe type and quality of the reward depend on the number of conquered fortresses at the very moment I check.^000000";
			next;
			mes .@npcname$;
			mes "And of course, I check during the siege hours only.";
			mes "^4d4dffReceiving a reward is followed by an hour of waiting time. So you'd better figure out the right timing as well.^000000";
			next;
			mes .@npcname$;
			mes "If you are a master of a guild that is currently participating in siege battles, you should give it a try.";
			next;
			mes .@npcname$;
			mes "Guild dungeon daily quests consist of combat quests that you can complete in guild dungeons.";
			mes "It's simple.";
			close;
		case 4: // I don't need it.
			mes .@npcname$;
			mes "You're really something else~!";
			close;
	}
	end;

OnInit:
	setarray .castle$[0],
		"prtg_cas01","prtg_cas02","prtg_cas03","prtg_cas04","prtg_cas05",
		"payg_cas01","payg_cas02","payg_cas03","payg_cas04","payg_cas05",
		"gefg_cas01","gefg_cas02","gefg_cas03","gefg_cas04","gefg_cas05",
		"aldeg_cas01","aldeg_cas02","aldeg_cas03","aldeg_cas04","aldeg_cas05",
		"arug_cas01","arug_cas02","arug_cas03","arug_cas04","arug_cas05",
		"schg_cas01","schg_cas02","schg_cas03","schg_cas04","schg_cas05",
		"te_aldecas1","te_aldecas2","te_aldecas3","te_aldecas4","te_aldecas5",
		"te_prtcas01","te_prtcas02","te_prtcas03","te_prtcas04","te_prtcas05";
	setarray .npcname$[0],
		"[Altir]","[Alshine]","[Denev]","[Tarazed]","[Mirah]","[Almark]","[Becrux]","[Acrux]";
	setarray .region$[0],
		"Valkyrie Realm","Greenwood Lake","Britoria","Luina","Valfreyja","Nidhoggur","Kafragarten","Gloria";
	setarray .weekday[0], // Day of the week. 0 = Sunday, 6 = Saturday.
		0,0,0,0,0,0,6,6;
	setarray .start_time[0],
		2200,2200,2200,2200,2200,2200,2200,2200;
	setarray .end_time[0],
		2230,2230,2230,2230,2230,2230,2230,2230;
	setarray .conquer_delay_questid[0], // Quest ID for cooldowns
		7517,7520,7519,7518,7522,7521,7524,7523;
	setarray .level_req[0], // Level requirement for quests. There are 2-3 quests per region
		100,120,120,	//prt
		100,120,120,	//pay
		100,120,120,	//gef
		100,120,120,	//alde
		90,90,0,		//aru
		90,90,0,		//sch
		70,70,0,		//tealde
		70,70,0;		//teprt
	setarray .max_level[0], // Exp increase cap for quests. 0 for unlimited/not exist
		130,140,0,	//prt
		130,140,0,	//pay
		130,140,0,	//gef
		130,140,0,	//alde
		120,120,0,	//aru
		120,120,0,	//sch
		99,99,0,	//tealde
		99,99,0;	//teprt
	setarray .daily_hunting_questid[0], //Quest ID for daily quests.
		7525,7526,7527,	//prt
		7534,7535,7536,	//pay
		7531,7532,7533,	//gef
		7528,7529,7530,	//alde
		7539,7540,0,	//aru
		7537,7538,0,	//sch
		7561,7562,0,	//tealde
		7541,7542,0;	//teprt
	setarray .daily_delay_questid[0],	//Quest ID for mob hunting check.
		7543,7544,7545,	//prt
		7552,7553,7554,	//pay
		7549,7550,7551,	//gef
		7546,7547,7548,	//alde
		7557,7558,0,	//aru
		7555,7556,0,	//sch
		7563,7564,0,	//tealde
		7559,7560,0;	//teprt
	setarray .ordinal$[0],"first","second","third";
	end;

	function brief {
		switch(getarg(0)){
			case 7527:
			case 7530:
			case 7533:
			case 7536:
				mes "It is to deal with the powerful creatures that appear in the Corridor of the Abyss.";
				break;
			default:
				mes "It is to eliminate the monsters that appear in the guild dungeon.";
				break;
		}
		return;
	}

	function mob_brief {
		switch(getarg(0)){
			case 7525:
				mes "You should hunt 50 or more Caterpillars and Creamy Fear monsters..";
				break;
			case 7526:
				mes "Hunt down 30 or more of all 3 types of Abyss Kobolds.";
				break;
			case 7527:
				mes "^4d4dffYou should kill furious 'Pyuriel' and warrior 'Lora'.^000000";
				return 1;
			case 7528:
				mes "^4d4dffHunt 50 or more of Giant Hornets and Ancient Worms, respectively.^000000";
				break;
			case 7529:
				mes "^4d4dffHunt 40 or more of Killer Mantises and 50 or more of Angra Mantises.^000000";
				break;
			case 7530:
				mes "They are ^4d4dff'Elvira' and 'Gioia,' which is a strange mechanical creature of heroes' tears.^000000";
				return 1;
			case 7531:
				mes "^4d4dffHunt 50 or more Zombie Masters and Wraith Deads, respectively.^000000";
				break;
			case 7532:
				mes "^4d4dffHunt 50 GLD_DARK_SHADOW, 20 GLD_DARK_FRAME, and 20 GLD_DARK_PRIEST monsters.^000000";
				break;
			case 7533:
				mes "They are ^4d4dff'Kades' and 'Rudo' who wander around the dead's hill.^000000";
				return 1;
			case 7534:
				mes "^4d4dffHunt 50 or more Gullinbursti and Leib Olmai, respectively.^000000";
				break;
			case 7535:
				mes "^4d4dffHunt 45 or more Skeleton General, 20 or more Am Mut and 25 or more Gajomart, respectively.^000000";
				break;
			case 7536:
				mes "^4d4dffThey are Old Empire's General 'Daehyon' and Guard 'Soheon'.^000000";
				return 1;
			case 7537:
				mes "^4d4dffHunt 50 or more Hell Apocalypse and Zakudam, respectively.^000000";
				break;
			case 7538:
				mes "^4d4dffHunt 35 or more Heavy Metaling and Cobalt Mineral, respectively.^000000";
				break;
			case 7539:
				mes "^4d4dffHunt more than 50 Banshee Masters and 30 Beholder Masters.^000000";
				break;
			case 7540:
				mes "^4d4dffHunt 50 Aunoes, and 20 Fanats.^000000";
				break;
			case 7541:
				mes "^4d4dffHunt 30 or more Knight, Blacksmith, Assassin, respectively.^000000";
				break;
			case 7542:
				mes "^4d4dffHunt 30 or more Wizard, Priest, Hunter respectively.^000000";
				break;
			case 7561:
				mes "^4d4dffHunt 30 or more, Crusader, Rogue, Alchemist, respectively.^000000";
				break;
			case 7562:
				mes "^4d4dffHunt 30 or more Sage, Monk, Bard, respectively.^000000";
				break;
		}
		return 0;
	}
	
	function calcexp {
		.@zoneid = getarg(0);
		.@index = getarg(1);
		.@maxlevel = .max_level[.@index];
		.@isboss = (.@zoneid < 4 && !((.@index+1)%3));
		
		if(!.@isboss && BaseLevel > .@maxlevel){
			return 1000 * (100 + ((.@maxlevel - 100) * 3));
		} else {
			return 1000 * (100 + ((BaseLevel - 100) * 3)) * (1 + .@isboss);
		}
	}
	
	function calcjobexp {
		.@edition = getarg(0);
		
		if(.@edition == 3){
			if(jobcanentermap("te_prtcas01")){
				if (JobLevel > 50)
					return 1000 * (50 + ((JobLevel - 50) * 3));
				else
					return 5000;
			} else {
				debugmes "Unexpected case for Job: " + Class + "(" + jobname(Class) + ") for gld_mission#core::calcjobexp";
				return 0;
			}
		} else if(.@edition == 2 ) {
			if(JobLevel > 50)
				return 1000 * (50 + ((JobLevel - 50) * 3));
			else
				return 3000;
		} else {
			return 5000 * JobLevel;
		}
		return 0;
	}
}
