//===== Hercules Script ======================================
//= Hunting Missions
//===== By: ==================================================
//= Euphy
//===== Current Version: =====================================
//= 1.4b
//===== Description: =========================================
//= Random hunting missions.
//= Rewards are based on quest difficulty.
//===== Additional Comments: =================================
//= 1.0 Initial script.
//= 1.1 Small improvements and fixes.
//= 1.2 Added party support and replaced blacklists with an
//=     SQL query, both thanks to AnnieRuru.
//= 1.3 Re-added a blacklist adapted for the SQL query.
//= 1.3a Added mission reset options.
//= 1.4 Fix ranking table using new SQL database [ossi0110]
//= 1.4a Fix a bug when a character has deleted, that account
//=     can't do mission. [AnnieRuru]
//= 1.4b Update to follow author's method on fixing
//=     character deletion bug [AnnieRuru]
//============================================================
//@unloadnpc Hunting Missions
//@loadnpc npc/classic/mission.txt

office,122,38,4	script	Hunting Missions	8W_SOLDIER,{
function Chk; function Cm;
	mes "[Hunting Missions]";
	mes "Hello, "+strcharinfo(PC_NAME)+"!";
	if (!#Mission_Delay) {
		next;
		mes "[Hunting Missions]";
		mes "I can't find any records...";
		mes "You must be new here!";
		emotion e_omg;
		next;
		callsub Mission_Info;
		emotion e_go;
		set #Mission_Delay,1;
		close;
	}
	mes rand(2)?"Working hard, as always...":"Not slacking, I hope...";
	mes "Is there anything I can help you with?.";
	mes "Btw, you can access this menu using ^0055FF@mission^000000 anywhere.";
	mes " ";
	mes "^777777~ You've completed ^0055FF"+Mission_Total+"^777777 mission"+((Mission_Total == 1)?"":"s")+". ~^000000";
	next;
	switch(select(((!Mission0)?" ~ New Mission::":": ~ Mission Status: ~ Abandon Mission")+": ~ Information: ~ Mission Shop: ~ View Top Hunters: ~ ^777777Cancel^000000")) {
	case 1:
		mes "[Hunting Missions]";
		if ( #Mission_Count ) {
			mes "You've started a mission";
			mes "on another character.";
			if ( !@hm_char_del_check ) {
				if ( !query_sql( "SELECT 1 FROM `char_reg_num_db` WHERE `key` = 'Mission0' AND `char_id` IN ( SELECT `char_id` FROM `char` WHERE `account_id` = "+ getcharid(3) +" )", .@dummy ) ) {
					next;
					mes "[Hunting Missions]";
					mes "I can't seem to find any records";
					mes "for that character, though...";
					mes "One moment, please.";
					emotion e_hmm;
					set #Mission_Count, 0;
				}
				set @hm_char_del_check, 1;
			}
			close;
		}
		if (#Mission_Delay > gettimetick(2) && .delay) {
			set .@i, #Mission_Delay-gettimetick(2);
			if (.@i > 3600) set .@j$, (.@i/3600)+" hour"+(((.@i/3600) == 1)?"":"s");
			else if (.@i > 60) set .@j$, (.@i/60)+" minute"+(((.@i/60) == 1)?"":"s");
			else set .@j$, (.@i)+" second"+((.@i == 1)?"":"s");
			mes "I'm afraid you'll have to wait "+.@j$+" before taking another mission.";
			close;
		}
		mes "You must hunt:";
		set .@blvl,BaseLevel;
		if (.@blvl >=1 && .@blvl <=10 ) {
			query_sql("SELECT ID FROM `mob_db` WHERE LV >= 1 AND LV <= 9 AND left(Sprite, 4) != 'meta' AND left(Sprite, 2) != 'E_' AND ~Mode & 32 AND EXP > 0 AND MVP1id = 0 AND DropCardid > 4000 AND DropCardid < 5000 AND ID < 2000 AND instr('"+.Blacklist$+"',ID) = 0 ORDER BY rand() LIMIT "+.quests, .@mob);
		} else if (.@blvl >=11 && .@blvl <=20) {
			query_sql("SELECT ID FROM `mob_db` WHERE LV >= 10 AND LV <= 17 AND left(Sprite, 4) != 'meta' AND left(Sprite, 2) != 'E_' AND ~Mode & 32 AND EXP > 0 AND MVP1id = 0 AND DropCardid > 4000 AND DropCardid < 5000 AND ID < 2000 AND instr('"+.Blacklist$+"',ID) = 0 ORDER BY rand() LIMIT "+.quests, .@mob);
		} else if (.@blvl >=21 && .@blvl <=60) {
			query_sql("SELECT ID FROM `mob_db` WHERE LV >= 18 AND LV <= 23 AND left(Sprite, 4) != 'meta' AND left(Sprite, 2) != 'E_' AND ~Mode & 32 AND EXP > 0 AND MVP1id = 0 AND DropCardid > 4000 AND DropCardid < 5000 AND ID < 2000 AND instr('"+.Blacklist$+"',ID) = 0 ORDER BY rand() LIMIT "+.quests, .@mob);
		} else if (.@blvl >=61 && .@blvl <=70) {
			query_sql("SELECT ID FROM `mob_db` WHERE LV >= 24 AND LV <= 30 AND left(Sprite, 4) != 'meta' AND left(Sprite, 2) != 'E_' AND ~Mode & 32 AND EXP > 0 AND MVP1id = 0 AND DropCardid > 4000 AND DropCardid < 5000 AND ID < 2000 AND instr('"+.Blacklist$+"',ID) = 0 ORDER BY rand() LIMIT "+.quests, .@mob);
		} else if (.@blvl >=71 && .@blvl <=80) {
			query_sql("SELECT ID FROM `mob_db` WHERE LV >= 31 AND LV <= 39 AND left(Sprite, 4) != 'meta' AND left(Sprite, 2) != 'E_' AND ~Mode & 32 AND EXP > 0 AND MVP1id = 0 AND DropCardid > 4000 AND DropCardid < 5000 AND ID < 2000 AND instr('"+.Blacklist$+"',ID) = 0 ORDER BY rand() LIMIT "+.quests, .@mob);
		} else if (.@blvl >=81 && .@blvl <=99) {
			query_sql("SELECT ID FROM `mob_db` WHERE LV >= 40 AND LV <= 80 AND left(Sprite, 4) != 'meta' AND left(Sprite, 2) != 'E_' AND ~Mode & 32 AND EXP > 0 AND MVP1id = 0 AND DropCardid > 4000 AND DropCardid < 5000 AND ID < 2000 AND instr('"+.Blacklist$+"',ID) = 0 ORDER BY rand() LIMIT "+.quests, .@mob);
		}
					
		for (set .@i,0; .@i<.quests; set .@i,.@i+1) {
			setd "Mission"+.@i, .@mob[.@i];
			setd "Mission"+.@i +"_",0;
		}
		set #Mission_Count, rand(.Count[0],.Count[1]);
		callsub Mission_Status;
		next;
		mes "[Hunting Missions]";
		mes "Report back when";
		mes "you've finished.";
		mes "Good luck!";
		close;
	case 2:
		mes "[Hunting Missions]";
		mes "Mission status:";
		callsub Mission_Status;
		close;
	case 3:
		mes "[Hunting Missions]";
		mes "Do you really want to";
		mes "abandon your mission?";
		if (.reset < 0 && .delay)
			mes "Your delay time will not be reset.";
		else if (.reset > 0)
			mes "It will cost "+Cm(.reset)+" Zeny.";
		next;
		switch(select(" ~ Abandon...", " ~ ^777777Cancel^000000")) {
		case 1:
			if (.reset > 0) {
				if (Zeny < .reset) {
					mes "[Hunting Missions]";
					mes "You don't have enough";
					mes "Zeny to drop this mission.";
					emotion e_sry;
					close;
				}
				Zeny -= .reset;
				emotion e_cash;
			}
			mes "[Hunting Missions]";
			mes "Alright, I've dropped";
			mes "your current mission.";
			specialeffect(EF_STORMKICK4, AREA, playerattached());
			for(set .@i,0; .@i<.quests; set .@i,.@i+1) {
				setd "Mission"+.@i,0;
				setd "Mission"+.@i+"_",0;
			}
			set #Mission_Count,0;
			if (.reset < 0 && .delay) set #Mission_Delay, gettimetick(2)+(.delay*3600);
			close;
		case 2:
			mes "[Hunting Missions]";
			mes "I knew you were kidding!";
			mes "Keep up the good work.";
			emotion e_heh;
			close;
		}
	case 4:
		callsub Mission_Info;
		close;
	case 5:
		mes "[Hunting Missions]";
		mes "You have ^0055FF"+#Mission_Points+"^000000 Mission Points.";
		mes "Use them well!";
		callshop "mission_shop",1;
		npcshopattach "mission_shop";
		end;
	case 6:
		mes "[Hunting Missions]";
		mes "The top hunters are:";
		query_sql("SELECT char_id AS id, (SELECT `name` FROM `char` WHERE char_id = id),`value` FROM `char_reg_num_db` WHERE `key` = 'Mission_Total' ORDER BY `value` DESC LIMIT 5",.@id,.@name$,.@val);
		for(set .@i,0; .@i<5; set .@i,.@i+1)
			mes "  [Rank "+(.@i+1)+"]  "+((.@name$[.@i] == "")?"^777777none":"^0055FF"+.@name$[.@i]+"^000000 : ^FF0000"+.@val[.@i]+" pt.")+"^000000";
		close;
	case 7:
		mes "[Hunting Missions]";
		mes "Nothing? Okay...";
		emotion e_hmm;
		close;
	}

Mission_Status:
	set @f,0;
	deletearray .@j[0], getarraysize(.@j);
	for(set .@i,0; .@i<.quests; set .@i,.@i+1) {
		set .@j[.@i], getd("Mission"+.@i);
		set .@j[.quests], .@j[.quests]+strmobinfo(3,.@j[.@i]);
		set .@j[.quests+1], .@j[.quests+1]+(strmobinfo(6,.@j[.@i])/(getbattleflag("base_exp_rate")/100)*.Modifier[0]);
		set .@j[.quests+2], .@j[.quests+2]+(strmobinfo(7,.@j[.@i])/(getbattleflag("job_exp_rate")/100)*.Modifier[1]);
		mes " > "+Chk(getd("Mission"+.@i+"_"),#Mission_Count)+strmobinfo(1,.@j[.@i])+" ("+getd("Mission"+.@i+"_")+"/"+#Mission_Count+")^000000";
	}

	// Reward formulas:
	set .@Mission_Points, 3+(.@j[.quests]/.quests/6);
	set .@Base_Exp, #Mission_Count*.@j[.quests+1]/5;
	set .@Job_Exp, #Mission_Count*.@j[.quests+2]/5;
	set .@zeny, #Mission_Count*.quests*.@j[.@i]*1;

	next;
	mes "[Hunting Missions]";
	mes "Mission rewards:";
	mes " > Mission Points: ^0055FF"+.@Mission_Points+"^000000";
	mes " > Base Experience: ^0055FF"+Cm(.@Base_Exp)+"^000000";
	mes " > Job Experience: ^0055FF"+Cm(.@Job_Exp)+"^000000";
	mes " > Zeny: ^0055FF"+Cm(.@zeny)+"^000000";
	if (@f) { set @f,0; return; }
	next;
	mes "[Hunting Missions]";
	mes "Oh, you're done!";
	mes "Good work.";
	mes "Here's your reward.";
	emotion e_no1;
	specialeffect(EF_ANGEL, AREA, playerattached());
	specialeffect(EF_TRUESIGHT, AREA, playerattached());
	set #Mission_Points, #Mission_Points+.@Mission_Points;
	set BaseExp, BaseExp+.@Base_Exp;
	set JobExp, JobExp+.@Job_Exp;
	Zeny += .@zeny;
	for(set .@i,0; .@i<.quests; set .@i,.@i+1) {
		setd "Mission"+.@i,0;
		setd "Mission"+.@i+"_",0;
	}
	set #Mission_Count,0;
	if (.delay) set #Mission_Delay, gettimetick(2)+(.delay*3600);
	set Mission_Total, Mission_Total+1;
	if (Mission_Total == 1) query_sql("INSERT INTO `char_reg_num_db` (`char_id`,`key`,`index`,`value`) VALUES ("+getcharid(0)+",'Mission_Total','0',1)");
	else query_sql("UPDATE `char_reg_num_db` SET `value` = "+Mission_Total+" WHERE `char_id` = "+getcharid(0)+" AND `key` = 'Mission_Total'");
	close;

OnStatus:
	mes "[Hunting Missions]";
	if(!Mission0){
		mes "You have not applied for any mission. Visit the Hunting Mission at the office to join.";	
		close;
	}

	mes "You must hunt:";
	set @f,0;
	deletearray .@j[0], getarraysize(.@j);
	for(set .@i,0; .@i<.quests; set .@i,.@i+1) {
		set .@j[.@i], getd("Mission"+.@i);
		set .@j[.quests], .@j[.quests]+strmobinfo(3,.@j[.@i]);
		set .@j[.quests+1], .@j[.quests+1]+(strmobinfo(6,.@j[.@i])/(getbattleflag("base_exp_rate")/100)*.Modifier[0]);
		set .@j[.quests+2], .@j[.quests+2]+(strmobinfo(7,.@j[.@i])/(getbattleflag("job_exp_rate")/100)*.Modifier[1]);
		mes " > "+Chk(getd("Mission"+.@i+"_"),#Mission_Count)+strmobinfo(1,.@j[.@i])+" ("+getd("Mission"+.@i+"_")+"/"+#Mission_Count+")^000000";
	}

	// Reward formulas:
	set .@Mission_Points, 3+(.@j[.quests]/.quests/6);
	set .@Base_Exp, #Mission_Count*.@j[.quests+1]/5;
	set .@Job_Exp, #Mission_Count*.@j[.quests+2]/5;
	set .@zeny, #Mission_Count*.quests*.@j[.@i]*1;

	next;
	mes "[Hunting Missions]";
	mes "Mission rewards:";
	mes " > Mission Points: ^0055FF"+.@Mission_Points+"^000000";
	mes " > Base Experience: ^0055FF"+Cm(.@Base_Exp)+"^000000";
	mes " > Job Experience: ^0055FF"+Cm(.@Job_Exp)+"^000000";
	mes " > Zeny: ^0055FF"+Cm(.@zeny)+"^000000";
	if (@f) { set @f,0; close; }
	close;

Mission_Info:
	mes "[Hunting Missions]";
	mes "If you so choose, I can assign";
	mes "you a random hunting quest.";
	mes "Some are easier than others, but";
	mes "the rewards increase with difficulty.";
	next;
	mes "[Hunting Missions]";
	mes "Missions points are shared";
	mes "amongst all your characters.";
	if (.delay) mes "Delay time is, too.";
	mes "You can't take missions on";
	mes "multiple characters at once.";
	next;
	mes "[Hunting Missions]";
	mes "You can start a quest";
	if (.delay) mes "every "+((.delay == 1)?"hour.":.delay+" hours.");
	else mes "whenever you want.";
	mes "That's everything~";
	return;

function Chk {
	if (getarg(0) < getarg(1)) { set @f,1; return "^FF0000"; }
	else return "^00FF00"; }

function Cm {
	set .@str$, getarg(0);
	for(set .@i,getstrlen(.@str$)-3; .@i>0; set .@i,.@i-3)
		set .@str$, insertchar(.@str$,",",.@i);
	return .@str$; }

OnBuyItem:
	set @cost,0;
	for(set .@i,0; .@i<getarraysize(@bought_nameid); set .@i,.@i+1)
		for(set .@j,0; .@j<getarraysize(.Shop); set .@j,.@j+2)
			if (@bought_nameid[.@i] == .Shop[.@j]) {
				set @cost, @cost+(.Shop[.@j+1]*@bought_quantity[.@i]);
				break;
			}
	mes "[Hunting Missions]";
	if (@cost > #Mission_Points) mes "You don't have enough Mission Points.";
	else {
		for(set .@i,0; .@i<getarraysize(@bought_nameid); set .@i,.@i+1) {
			getitem @bought_nameid[.@i], @bought_quantity[.@i];
			dispbottom "Purchased "+@bought_quantity[.@i]+"x "+getitemname(@bought_nameid[.@i])+".";
		}
		set #Mission_Points, #Mission_Points-@cost;
		mes "Deal completed.";
		emotion e_cash;
	}
	set @cost,0;
	deletearray @bought_nameid[0], getarraysize(@bought_nameid);
	deletearray @bought_quantity[0], getarraysize(@bought_quantity);
	close;

OnNPCKillEvent:
	if (!getcharid(1) || !.party) {
		if (!#Mission_Count || !Mission0) end;
		for (set .@i, 0; .@i<.quests; set .@i,.@i+1) {
			if (strmobinfo(1,killedrid) == strmobinfo(1,getd("Mission"+.@i))) {
				if (getd("Mission"+.@i+"_") < #Mission_Count) {
					dispbottom "[Hunting Mission] Killed "+(set(getd("Mission"+.@i+"_"),getd("Mission"+.@i+"_")+1))+" of "+#Mission_Count+" "+strmobinfo(1,killedrid)+".";
					end;
				}
			}
		}
	} else if (.party) {
		set .@mob, killedrid;
		getmapxy(.@map1$, .@x1, .@y1, UNITTYPE_PC);
		getpartymember getcharid(1),1;
		getpartymember getcharid(1),2;
		for(set .@i,0; .@i<$@partymembercount; set .@i,.@i+1) {
			if (isloggedin($@partymemberaid[.@i], $@partymembercid[.@i])) {
				attachrid $@partymemberaid[.@i];
				if (#Mission_Count && Mission0 && Hp > 0) {
					getmapxy(.@map2$, .@x2, .@y2, UNITTYPE_PC);
					if ((.@map1$ == .@map2$ || .party == 1) && (distance(.@x1,.@y1,.@x2,.@y2) <= 30 || .party < 3)) {
						for(set .@j,0; .@j<.quests; set .@j,.@j+1) {
							if (strmobinfo(1,.@mob) == strmobinfo(1,getd("Mission"+.@j))) {
								if (getd("Mission"+.@j+"_") < #Mission_Count) {
									dispbottom "[Hunting Mission] Killed "+(set(getd("Mission"+.@j+"_"),getd("Mission"+.@j+"_")+1))+" of "+#Mission_Count+" "+strmobinfo(1,.@mob)+".";
									break;
								}
							}
						}
					}
				}
			}
		}
	}
	end;

OnInit:
	bindatcmd("mission","Hunting Missions::OnStatus",0,99);
	set .delay,12;		// Quest delay, in hours (0 to disable).
	set .quests,4;		// Number of subquests per mission (increases rewards).
	set .party,3;		// Party options: 0 (exclude party kills), 1 (include party kills), 2 (same map only), 3 (screen area only)
	set .reset,-1;		// Reset options: -1 (abandoning mission sets delay time), 0 (no delay time), [Zeny] (cost to abandon mission, no delay time)
	setarray .Count[0],	// Min and max monsters per subquest (increases rewards).
		40,70;

	setarray .Modifier[0],	// Multipliers for Base Exp, Job Exp, and Zeny rewards.
		getbattleflag("base_exp_rate")/100,getbattleflag("job_exp_rate")/100,60;

	setarray .Shop[0],	// Reward items: <ID>,<point cost> (about 10~20 points per hunt).
		512,1,513,1,514,1,538,5,539,5,558,10,561,10;
	set .Blacklist$,	// Blacklisted mob IDs.

"1088,1581,1119,1186,1214,1268,1219,1108,"+
"1003,1006,1017,1021,1022,1027,1043,1062,1065,1072,"+
"1075,1076,1098,1136,1137,1148,1154,1157,1162,1168,"+
"1171,1172,1173,1180,1181,1182,1183,1184,1187,1198,"+
"1201,1202,1210,1212,1220,1221,1222,1223,1224,1225,"+
"1226,1227,1228,1229,1230,1231,1233,1234,1235,1236,"+
"1237,1238,1239,1240,1241,1244,1245,1247,1284,1285,"+
"1286,1287,1288,1324,1325,1326,1327,1328,1329,1330,"+
"1331,1332,1333,1334,1335,1336,1337,1338,1339,1340,"+
"1341,1342,1343,1344,1345,1346,1347,1348,1349,1350,"+
"1351,1352,1353,1354,1355,1356,1357,1358,1359,1360,"+
"1361,1362,1363,1364,1365,1373,1377,1389,1391,1392,"+
"1393,1394,1395,1396,1397,1398,1399,1400,1401,1402,"+
"1403,1404,1405,1406,1407,1408,1409,1410,1412,1413,"+
"1415,1416,1417,1418,1419,1420,1421,1422,1423,1424,"+
"1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,"+
"1435,1436,1437,1438,1439,1440,1441,1442,1443,1444,"+
"1445,1446,1447,1448,1449,1450,1451,1452,1453,1454,"+
"1455,1456,1457,1458,1459,1460,1461,1462,1463,1464,"+
"1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,"+
"1475,1476,1477,1478,1479,1480,1481,1482,1483,1484,"+
"1485,1486,1487,1488,1489,1490,1491,1492,1493,1494,"+
"1495,1497,1498,1499,1500,1502,1503,1504,1505,1506,"+
"1507,1508,1509,1510,1511,1512,1513,1514,1515,1516,"+
"1517,1518,1519,1520,1588,1618,1676,1677,1678,1679,"+
"1796,1797,1900,1901,1920,1921,1970,1971,1972,1973,"+
"1974,1975,1976,1977,1978,1979,1374,1370,1371,1390";

	npcshopdelitem "mission_shop",512;
	for(set .@i,0; .@i<getarraysize(.Shop); set .@i,.@i+2)
		npcshopadditem "mission_shop", .Shop[.@i], .Shop[.@i+1];
	end;
}
-	shop	mission_shop	FAKE_NPC,512:-1

office,230,47,4	duplicate(Hunting Missions)	Hunting Missions#vip	8W_SOLDIER