//===== Hercules Script ======================================
//= Euphy's Quest Shop
//===== By: ==================================================
//= Euphy
//===== Current Version: =====================================
//= 1.6a
//===== Description: =========================================
//= A dynamic quest shop based on Lunar's, with easier config.
//= Includes support for multiple shops & cashpoints.
//= Item Preview script by ToastOfDoom.
//===== Additional Comments: =================================
//= 1.0 Initial script.
//= 1.2 Added category support.
//= 1.3 More options and fixes.
//= 1.4 Added debug settings.
//= 1.5 Replaced categories with shop IDs.
//= 1.6 Added support for purchasing stackables.
//= 1.6a Added support for previewing costumes and robes.
//============================================================

// Shop NPCs -- supplying no argument displays entire menu.
//	callfunc "qshop"{,<shop ID>{,<shop ID>{,...}}};
//============================================================
prontera,164,203,6	script	Quest Shop#1	4_M_MOCASS2,{ callfunc "qshop"; }


// Script Core
//============================================================
-	script	quest_shop	FAKE_NPC,{
function Add; function Chk; function Slot; function A_An;
OnInit:
	freeloop(1);

// -----------------------------------------------------------
//  Basic shop settings.
// -----------------------------------------------------------

	set .Announce,1;	// Announce quest completion? (1: yes / 0: no)
	set .ShowSlot,1;	// Show item slots? (2: all equipment / 1: if slots > 0 / 0: never)
	set .ShowID,0;  	// Show item IDs? (1: yes / 0: no)
	set .ShowZeny,0;	// Show Zeny cost, if any? (1: yes / 0: no)
	set .MaxStack,100;	// Max number of quest items purchased at one time.

// -----------------------------------------------------------
//  Points variable -- optional quest requirement.
//	setarray .Points$[0],"<variable name>","<display name>";
// -----------------------------------------------------------

	setarray .Points$[0],"#CASHPOINTS","Cash Points";

// -----------------------------------------------------------
//  Shop IDs -- to add shops, copy dummy data at bottom of file.
//	setarray .Shops$[1],"<Shop 1>","<Shop 2>"{,...};
// -----------------------------------------------------------

	setarray .Shops$[1],"Headgears","Weapons","Other";

// -----------------------------------------------------------
//  Quest items -- do NOT use a reward item more than once!
//	Add(<shop ID>,<reward ID>,<reward amount>,
//	    <Zeny cost>,<point cost>,
//	    <required item ID>,<required item amount>{,...});
// -----------------------------------------------------------

	Add(1,5022,1,0,0,7086,1,969,10,999,40,1003,50,984,2);
	Add(1,5032,1,0,0,1059,250,2221,1,2227,1,7063,600);
	Add(1,5027,1,0,0,2252,1,1036,400,7001,50,4052,1);
	Add(1,5045,1,0,0,2252,1,1054,450,943,1200);

	Add(2,1224,1,0,0,7297,30,969,10,999,50,714,10);
	Add(2,1225,1,0,0,7292,30,969,10,999,50,714,10);

	Add(3,531,1,3,0,512,1,713,1);
	Add(3,532,1,3,0,513,1,713,1);
	Add(3,533,1,3,0,514,1,713,1);
	Add(3,534,1,3,0,515,1,713,1);

// -----------------------------------------------------------

	freeloop(0);
	set .menu$,"";
	for(set .@i,1; .@i<=getarraysize(.Shops$); set .@i,.@i+1) {
		set .menu$, .menu$+.Shops$[.@i]+":";
		npcshopdelitem "qshop"+.@i,909;
	}
	end;

OnMenu:
	set .@size, getarraysize(@i);
	if (!.@size) set .@i, select(.menu$);
	else if (.@size == 1) set .@i, @i[0];
	else {
		for(set .@j,0; .@j<.@size; set .@j,.@j+1)
			set .@menu$, .@menu$+.Shops$[@i[.@j]]+":";
		set .@i, @i[select(.@menu$)-1];
	}
	deletearray @i[0],getarraysize(@i);
	if (.Shops$[.@i] == "") {
		message strcharinfo(PC_NAME),"An error has occurred.";
		end;
	}
	dispbottom "Select one item at a time.";
	callshop "qshop"+.@i,1;
	npcshopattach "qshop"+.@i;
	end;

OnBuyItem:
	// .@q[] : RewardID, BoughtAmt, RewardAmt, BaseAmt, ReqZeny, ReqPts, { ReqItem, ReqAmt, ... }
	setarray .@q[0],@bought_nameid[0],((@bought_quantity[0] > .MaxStack)?.MaxStack:@bought_quantity[0]);
	copyarray .@q[3],getd(".q_"+.@q[0]+"[0]"),getarraysize(getd(".q_"+.@q[0]));
	set .@q[2],.@q[1]*.@q[3];
	if (!.@q[2] || .@q[2] > 30000) {
		message strcharinfo(PC_NAME),"You can't purchase that many "+getitemname(.@q[0])+".";
		end;
	}
	mes "[Quest Shop]";
	mes "Reward: ^0055FF"+((.@q[2] > 1)?.@q[2]+"x ":"")+Slot(.@q[0])+"^000000";
	mes "Requirements:";
	if (.@q[4]) mes " > "+Chk(Zeny,.@q[4]*.@q[1])+(.@q[4]*.@q[1])+" Zeny^000000";
	if (.@q[5]) mes " > "+Chk(getd(.Points$[0]),.@q[5]*.@q[1])+(.@q[5]*.@q[1])+" "+.Points$[1]+" ("+getd(.Points$[0])+"/"+(.@q[5]*.@q[1])+")^000000";
	if (.@q[6]) for(set .@i,6; .@i<getarraysize(.@q); set .@i,.@i+2)
		mes " > "+Chk(countitem(.@q[.@i]),.@q[.@i+1]*.@q[1])+((.ShowID)?"{"+.@q[.@i]+"} ":"")+Slot(.@q[.@i])+" ("+countitem(.@q[.@i])+"/"+(.@q[.@i+1]*.@q[1])+")^000000";
	next;
	setarray @qe[1], getiteminfo(.@q[0], ITEMINFO_LOC), getiteminfo(.@q[0], ITEMINFO_VIEWSPRITE);
	if (@qe[2] > 0 && ((@qe[1] & EQP_HEAD_LOW) || (@qe[1] & EQP_HEAD_TOP) || (@qe[1] & EQP_HEAD_MID) || (@qe[1] & EQP_COSTUME_HEAD_TOP) || (@qe[1] & EQP_COSTUME_HEAD_MID) || (@qe[1] & EQP_COSTUME_HEAD_LOW) || (@qe[1] & EQP_GARMENT) || (@qe[1] & EQP_COSTUME_GARMENT)))
		set .@preview,1;
	addtimer 1000, strnpcinfo(NPC_NAME)+"::OnEnd";
	while(1) {
		switch(select(" ~ Purchase ^0055FF"+ getitemname(.@q[0]) +"^000000", ((.@preview && !@qe[7])?" ~ Preview...": ""), " ~ ^777777Cancel^000000")) {
		case 1:
			if (@qe[0]) {
				mes "[Quest Shop]";
				mes "You're missing one or more quest requirements.";
				close;
			}
			if (!checkweight(.@q[0],.@q[2])) {
				mes "[Quest Shop]";
				mes "^FF0000You need "+(((.@q[2] * getiteminfo(.@q[0], ITEMINFO_WEIGHT)) + Weight - MaxWeight) / 10)+" additional weight capacity to complete this trade.^000000";
				close;
			}
			if (.@q[4]) Zeny -= (.@q[4]*.@q[1]);
			if (.@q[5]) setd .Points$[0], getd(.Points$[0])-(.@q[5]*.@q[1]);
			if (.@q[6]) for(set .@i,6; .@i<getarraysize(.@q); set .@i,.@i+2)
				delitem .@q[.@i],.@q[.@i+1]*.@q[1];
			getitem .@q[0],.@q[2];
			if (.Announce) announce strcharinfo(PC_NAME)+" has created "+((.@q[2] > 1)?.@q[2]+"x "+getitemname(.@q[0]):A_An(getitemname(.@q[0])))+"!",0;
			specialeffect(EF_FLOWERLEAF, AREA, playerattached());
			close;
		case 2:
			setarray @qe[3], getlook(LOOK_HEAD_BOTTOM), getlook(LOOK_HEAD_TOP), getlook(LOOK_HEAD_MID), getlook(LOOK_ROBE), 1;
			if ((@qe[1] & 1) || (@qe[1] & 4096)) changelook LOOK_HEAD_BOTTOM, @qe[2];
			else if ((@qe[1] & 256) || (@qe[1] & 1024)) changelook LOOK_HEAD_TOP, @qe[2];
			else if ((@qe[1] & 512) || (@qe[1] & 2048)) changelook LOOK_HEAD_MID, @qe[2];
			else if ((@qe[1] & 4) || (@qe[1] & 8192)) changelook LOOK_ROBE, @qe[2];
			break;
		case 3:
			close;
		}
	}

OnEnd:
	if (@qe[7]) {
		changelook LOOK_HEAD_BOTTOM, @qe[3];
		changelook LOOK_HEAD_TOP, @qe[4];
		changelook LOOK_HEAD_MID, @qe[5];
		changelook LOOK_ROBE, @qe[6];
	}
	deletearray @qe[0],8;
	end;

function Add {
	if (getitemname(getarg(1)) == "null") {
		debugmes "Quest reward #"+getarg(1)+" invalid (skipped).";
		return;
	}
	setarray .@j[0],getarg(2),getarg(3),getarg(4);
	for(set .@i,5; .@i<getargcount(); set .@i,.@i+2) {
		if (getitemname(getarg(.@i)) == "null") {
			debugmes "Quest requirement #"+getarg(.@i)+" invalid (skipped).";
			return;
		} else
			setarray .@j[.@i-2],getarg(.@i),getarg(.@i+1);
	}
	copyarray getd(".q_"+getarg(1)+"[0]"),.@j[0],getarraysize(.@j);
	npcshopadditem "qshop"+getarg(0),getarg(1),((.ShowZeny)?getarg(3):0);
	return;
}

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

function Slot {
	set .@s$,getitemname(getarg(0));
	switch(.ShowSlot) {
		case 1: if (!getitemslots(getarg(0))) return .@s$;
		case 2: if (getiteminfo(getarg(0), ITEMINFO_TYPE) == IT_WEAPON || getiteminfo(getarg(0), ITEMINFO_TYPE) == IT_ARMOR) return .@s$+" ["+getitemslots(getarg(0))+"]";
		default: return .@s$;
	}
}

function A_An {
	setarray .@A$[0],"a","e","i","o","u";
	set .@B$, "_"+getarg(0);
	for(set .@i,0; .@i<5; set .@i,.@i+1)
		if (compare(.@B$,"_"+.@A$[.@i])) return "an "+getarg(0);
	return "a "+getarg(0);
}
}

function	script	qshop	{
	deletearray @i[0],getarraysize(@i);
	for(set .@i,0; .@i<getargcount(); set .@i,.@i+1)
		set @i[.@i],getarg(.@i);
	doevent "quest_shop::OnMenu";
	end;
}


// Dummy shop data -- copy as needed.
//============================================================
-	shop	qshop1	FAKE_NPC,909:-1
-	shop	qshop2	FAKE_NPC,909:-1
-	shop	qshop3	FAKE_NPC,909:-1
-	shop	qshop4	FAKE_NPC,909:-1
-	shop	qshop5	FAKE_NPC,909:-1
