//================= Hercules Script =======================================
//=       _   _                     _
//=      | | | |                   | |
//=      | |_| | ___ _ __ ___ _   _| | ___  ___
//=      |  _  |/ _ \ '__/ __| | | | |/ _ \/ __|
//=      | | | |  __/ | | (__| |_| | |  __/\__ \
//=      \_| |_/\___|_|  \___|\__,_|_|\___||___/
//================= License ===============================================
//= This file is part of Hercules.
//= http://herc.ws - http://github.com/HerculesWS/Hercules
//=
//= Copyright (C) 2012-2015  Hercules Dev Team
//= Copyright (C)  Dastgir
//= Copyright (C)  Haru
//= Copyright (C)  rAthena Dev Team
//= Copyright (C)  Euphy
//= Copyright (C)  Joseph
//= Copyright (C)  eAthena Dev Team
//= Copyright (C)  Daegaladh
//= Copyright (C)  brianluau
//= Copyright (C)  Kisuka
//= Copyright (C)  Evera
//= Copyright (C)  erKURITA
//= Copyright (C)  Silentdragon
//= Copyright (C)  Nexon
//= Copyright (C)  L0ne_W0lf
//= Copyright (C)  kobra_k88
//= Copyright (C)  Lupu
//= Copyright (C)  Syrus22
//= Copyright (C)  Darkchild
//= Copyright (C)  Darlskies
//=
//= Hercules is free software: you can redistribute it and/or modify
//= it under the terms of the GNU General Public License as published by
//= the Free Software Foundation, either version 3 of the License, or
//= (at your option) any later version.
//=
//= This program is distributed in the hope that it will be useful,
//= but WITHOUT ANY WARRANTY; without even the implied warranty of
//= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//= GNU General Public License for more details.
//=
//= You should have received a copy of the GNU General Public License
//= along with this program.  If not, see <http://www.gnu.org/licenses/>.
//=========================================================================
//= Kafra Functions
//================= Description ===========================================
//= These functions handle save, storage, cart rental, teleport, and Free
//= Teleport/Warp/Rent Cart options for all Kafra NPCs.
//================= Current Version =======================================
//= 6.9
//=========================================================================

//== Main Function =========================================
// Arguments:
// - 0: Used to determine which welcome message to show.
// - 1: Used to determine which menu to display.
// - 2: Used to determine if the info menu is shown in F_KafInfo.
// - 3: Cost to use Storage
// - 4: Cost to Rent a Pushcart
function	script	F_Kafra	{
	callfunc "F_ClearGarbage"; // Clear outdated, unused variables
	// Display Kafra Welcome Message
	switch (getarg(0)) {
		default:
		case 0:
			// Default message (obsolete)
			mes "[Kafra Employee]";
			mes "Welcome to the";
			mes "Kafra Corporation.";
			mes "The Kafra services";
			mes "are always on your side.";
			mes "How may I assist you?";
			break;
		case 1:
			// Niflheim specific message
			mes "[Kafra Employee]";
			mes "^666666W-weeeelc-c-come";
			mes "to th-the K-kaaafrrrra";
			mes "C-coorpoor-r-ratioooonn...^000000";
			break;
		case 2:
			// Guild Castle Kafra message
			@GID = getcharid(CHAR_ID_GUILD);
			mes "[Kafra Employee]";
			mes "Welcome. ^ff0000" + getguildname(@GID) + "^000000 Member.";
			mes "The Kafra Coporation will stay with you wherever you go.";
			break;
		case 3:
			// Amatsu specific message (obsolete)
			mes "[Kafra Employee]";
			mes "So, have you come from a faraway land to study our culture, or are you just sightseeing?";
			mes "In either case, why not stay awhile?";
			mes "The air is eternally heavy with the";
			mes "scent of pleasant wildflowers.";
			break;
		case 4:
			// Louyang and Ayothaya specific message (obsolete)
			mes "[Kafra Employee]";
			mes "With our many Kafra";
			mes "service locations, you're never";
			mes "far from home.";
			break;
		case 5:
			// NPC has it's own welcome message. (Display nothing)
			break;
	}
	next;
	while (true) {
		deletearray @K_Menu0$;
		if (getarg(0) == 2) {
			// Guild Kafra Menu override (free Teleport, free Storage, Free Cart)
			setarray @K_Menu0$[0],"Use Storage","Use Guild Storage","Rent a Pushcart","Use Teleport Service","Cancel";
		} else {
			switch (getarg(1)) {
				// Save and Storage only
				case 1:	setarray @K_Menu0$[0],"Save","Use Storage","Cancel"; break;
				// Storage only
				case 2:	setarray @K_Menu0$[0],"Use Storage","Cancel"; break;
				// No Teleport (Common)
				case 3: setarray @K_Menu0$[0],"Save","Use Storage","Rent a Pushcart","Check Other Information","Cancel"; break;
				// Case 4 is Einbroch no tele message.
				// No save, or teleport. (Common)
				case 5:	setarray @K_Menu0$[0],"Use Storage","Rent a Pushcart","Check Other Information","Cancel"; break;
				// Storage and Check Other Information only.
				case 6:	setarray @K_Menu0$[0],"Use Storage","Check Other Information","Cancel"; break;
				// Save, Storage, and Pushcart only (Kafra Warehouse)
				case 7:	setarray @K_Menu0$[0],"Save","Use Storage","Rent a Pushcart","Cancel"; break;
				// Save, Storage, Other Check information. (Turbo track)
				case 8: setarray @K_Menu0$[0],"Save","Use Storage","Check Other Information","Cancel"; break;
				// No Save (Rune Knight)
				case 9: setarray @K_Menu0$[0],"Use Storage","Rent a Pushcart","Use Teleport Service","Check Other Information","Cancel"; break;
				// Storage, Save, and Pushcart (Dewata, reorder of case 7)
				case 10: setarray @K_Menu0$[0],"Use Storage","Save","Rent a Pushcart","Cancel"; break;
				// Default message (obsolete)
				default: setarray @K_Menu0$[0],"Save","Use Storage","Use Teleport Service","Rent a Pushcart","Check Other Information","Cancel"; break;
			}
		}
		.@menu$ = "";
		for (.@i = 0; .@i < getarraysize(@K_Menu0$); ++.@i)
			.@menu$ += @K_Menu0$[.@i] + ":";
		.@j = select(.@menu$) - 1;
		if (@K_Menu0$[.@j] == "Save") {
			return;
		} else if (@K_Menu0$[.@j] == "Use Storage") {
			// Do not charge for Guild Storage
			if(getarg(0) == 2)
				callfunc "F_KafStor",2,0,0,"[Kafra Employee]";
			else
				callfunc "F_KafStor",0,getarg(3),getarg(0),"[Kafra Employee]";
			next;
		} else if (@K_Menu0$[.@j] == "Use Teleport Service") {
			// Display Einbroch "No Teleport Service" notice.
			if (getarg(1) != 4) {
				callfunc "F_KafTele",getarg(0),"[Kafra Employee]";
			} else {
				mes "[Kafra Employee]";
				mes "Because of the ^FF0000Limited";
				mes "Transport Agreement^000000, the";
				mes "Kafra Corporation cannot";
				mes "provide Teleport Services";
				mes "in the Schwaltzvalt Republic.";
				next;
				mes "[Kafra Employee]";
				mes "We ask that you please";
				mes "use the Airship Service";
				mes "instead. Thank you for your";
				mes "understanding and cooperation.";
			}
			next;
		} else if (@K_Menu0$[.@j] == "Bank") {
			while(1){
			mes "[ ^ff0000Kafra Employee^000000 ]";
			mes "How may I help you today?";
			next;
			switch( select( ("Deposit:Withdraw:Balance Inquiry:" ) + "Close" ) ) {
			case 1:
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "Hi, how much would you like to deposit?";
				mes "Maximum amount per transaction is ";
				mes "^000080"+callfunc("F_InsertComma",10000000)+ "^000000 zeny";
				next;
				input .@amount;
				if ( !.@amount || .@amount > 10000000 ) {
					mes "[ ^ff0000Kafra Employee^000000 ]";
					mes "Invalid amount. Maximum amount per transaction is ";
					mes "^000080"+callfunc("F_InsertComma",10000000)+ "^000000 zeny";
					next;
					break;
				}
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "Are you sure you want to deposit ";
				mes  "^000080"+callfunc("F_InsertComma",.@amount)+ "^000000 zeny";
				mes "to your bank account?";
				next;
				if ( select( "Yes:No" ) - 1 ) break;
				if ( Zeny < .@amount ) {
					mes "[ ^ff0000Kafra Employee^000000 ]";
					mes "Seems like you don't have that";
					mes "kind of amount. I will terminate";
					mes "this transaction then.";
					next;
					break;
				}
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "^000080"+callfunc("F_InsertComma",.@amount)+ "^000000 zeny";
				mes "have been deposited on your account.";
				mes "Thank you for using this BANK System";
				Zeny -= .@amount;
				BankVault += .@amount;
				query_logsql "INSERT INTO `zenylog` ( `time`, `char_id`,`src_id`,`type`,`amount`,`map` ) VALUES ( '"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"','" + getcharid( CHAR_ID_CHAR ) + "','" + getcharid( CHAR_ID_CHAR ) + "','K','"+(.@amount)+"','" +strnpcinfo(NPC_MAP)+ "' )";
				next;	
 				break;
			case 2:
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "Hi, how much would you like to withdraw? Maximum amount per transaction is ";
				mes "^000080"+callfunc("F_InsertComma",10000000)+ "^000000 zeny";
				next;
				input .@amount;
				if ( !.@amount || .@amount > 10000000 ) {
					mes "[ ^ff0000Kafra Employee^000000 ]";
					mes "Invalid amount. Maximum amount per transaction is ";
					mes "^000080"+callfunc("F_InsertComma",10000000)+ "^000000 zeny";
					next;
					break;
				}
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "Are you sure you want to withdraw";
				mes "^000080"+callfunc("F_InsertComma",.@amount)+ "^000000 zeny";
				mes "from your bank account?";
				next;
				if ( select( "Yes:No" ) - 1 ) break;
				if ( BankVault < .@amount ) {
					mes "[ ^ff0000Kafra Employee^000000 ]";
					mes "Seems like you don't have that kind of amount. I will terminate this transaction then.";
					next;
					break;
				}
				if ( ( Zeny + .@amount ) > 2000000000 ) {
					mes "[ ^ff0000Kafra Employee^000000 ]";
					mes "You cannot afford to hold this kind of zeny. The maximum zeny you can hold is";
					mes "^0000802,000,000,000^000000 zeny only.";
					break;
				}
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "^000080"+callfunc("F_InsertComma",.@amount)+ "^000000 zeny";
				mes "have been withdrew on your account. Thank you for using this BANK System";
				BankVault -= .@amount;
				Zeny += .@amount;
				query_logsql "INSERT INTO `zenylog` ( `time`, `char_id`,`src_id`,`type`,`amount`,`map` ) VALUES ( '"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"','" + getcharid( CHAR_ID_CHAR ) + "','" + getcharid( CHAR_ID_CHAR ) + "','K','"+(-.@amount)+"','" +strnpcinfo(NPC_MAP)+ "' )";
				next;	
				break;
			case 3: 
				mes "[ ^ff0000Kafra Employee^000000 ]";
				mes "Your account balance is : ";
				mes "^000080"+callfunc("F_InsertComma",BankVault)+ "^000000 zeny";
				next;					
				break;
			default:
				cutin "", 255;
				close;
			}
			}
		} else if (@K_Menu0$[.@j] == "Rent a Pushcart") {
			if(callfunc("F_KafCart",getarg(0),getarg(4),"[Kafra Employee]") == 1)
				next;
		} else if (@K_Menu0$[.@j] == "Check Other Information") {
			callfunc "F_KafInfo",getarg(2);
			next;
		} else if (@K_Menu0$[.@j] == "Cancel"){
			callfunc "F_KafEnd",getarg(0),0;
			end;
		} else if (@K_Menu0$[.@j] == "Use Guild Storage") {
			callfunc "F_KafStor",1,0,0,"[Kafra Employee]";
			next;
		}
	}
}

//== Storage Function ======================================
// Arguments:
// - 0: Specifies the type of storage that has been requested:
//      0 = Regular storage
//      1 = Guild storage
//      2 = Regular storage, in a castle (free of charge)
// - 1: Cost to use storage.
// - 2: The type of NPC this is being called from. Notable values:
//      0,2-5 = Regular Kafra
//      1 = Niflheim Kafra (special messages)
//      6 = Zonda/Cool Event Staff (no RESRVPTS)
// - 3: NPC title to display.
function	script	F_KafStor	{
	// Unable to access Guild Storage (Busy)
	if (getarg(0) == 1) {
		if (guildopenstorage()) {
			mes getarg(3);
			mes "I'm sorry but another guild member is using the guild storage";
			mes "right now.  Please wait until that person is finished.";
			close2;
			cutin "", 255;
			end;
		}
		cutin "", 255;
		close;
	}
	// Unable to access Normal Storage (Insufficient Basic Skills)
	if (basicskillcheck() && getskilllv(NV_BASIC) < 6 && getskilllv(SU_BASIC_SKILL) == 0) {
		mes getarg(3);
		// Niflheim Specific Message
		if (getarg(2) == 1) {
			mes "^666666S-s-ssoooorry,";
			if (Class == Job_Summoner) {
				mes "y-you n-neeed";
				mes "N-new b-basic sssskill...^000000";
			} else {
				mes "y-you're a-a-aaaa";
				mes "Nooviiice... N-neeeds";
				mes "B-basic sssskill l-level 6...^000000";
			}
			return;
		}
		mes "I'm sorry, but you";
		if (Class == Job_Summoner) {
			mes "need the New Basic Skill to";
		} else {
			mes "need the Novice's";
			mes "Basic Skill Level 6 to";
		}
		mes "use the Storage Service.";
		return;
	}
	// Accessing Normal Storage (Skipped if accessing Storage from Guild castle)
	if (getarg(0) != 2) {
		// Consume "Free Ticket for Kafra Storage" if available.
		if (countitem(Cargo_Free_Ticket)) {
			delitem Cargo_Free_Ticket, 1;
		} else {
			if (Zeny < getarg(1)) {
				mes getarg(3);
				// Niflheim Specific Message
				if (getarg(2) == 1) {
					percentheal -50,-50;
					mes "^666666Zeeeeeny...";
					mes "M-more z-zeny...!";
					mes "N-neeed 150... zeny...";
					mes "Ergh! T-taking bl-blood~!^000000";
					return;
				}
				// Standard Message
				mes "I'm sorry, but you don't";
				mes "have enough zeny to use";
				mes "the Storage Service. Our";
				mes "Storage access fee is "+getarg(1)+" zeny.";
				return;
			}
			Zeny -= getarg(1);
			if (getarg(2) != 6)
				RESRVPTS += getarg(1) / 5;
		}
	}

	// Surpress the "End" message if in guild castle.
	if (getarg(0) != 2) {
		if (getarg(2) == 1) {
			// Niflheim specific Message
			percentheal 0,-10;
			mes getarg(3);
			for (.@i = 0; .@i < 5; ++.@i)
				mes "^666666Thank you.. for... using...^000000";
		} else if (getarg(2) == 6) {
			// Zonda
			mes getarg(3);
			mes "Let me open your personal";
			mes "storage for you right away.";
			mes "Thanks for supporting Cool";
			mes "Event Corp. by using our";
			mes "services. Have a good day~";
		} else {
			// Normal message
			mes getarg(3);
			mes "Here, let me open";
			mes "your Storage for you.";
			mes "Thank you for using";
			mes "the Kafra Service.";
		}
	}
	callfunc("F_CheckKafCode"); //check your storage password, if set
	close2;
	openstorage;
	cutin "", 255;
	end;
}

//== Teleport Function =====================================
// Arguments:
// - 1: NPC title to display.
// - 2: The type of NPC this is being called from. Notable values:
//      2 = Guild Castle Kafra (don't consume tickets)
//      6 = Zonda/Cool Event Staff (no RESRVPTS)
function	script	F_KafTele	{
	mes getarg(1);
	mes "Please choose";
	mes "your destination.";
	next;
	.@menu$ = "";
	for (.@i = 0; .@i <= getarraysize(@wrpD$); ++.@i)
		.@menu$ += @wrpC$[.@i] + ":";
	.@j = select(.@menu$) - 1;
//	cutin "", 255;
	if (@wrpC$[.@j] == "Cancel") {
		return;
	// Consume "Free Ticket for Kafra Transportation" if available.
	// Do not consume if in Guild castle
	} else if (@wrpD$[.@j] == "Last Warp") {
		if(lwarpx){
			warp lwarpmap$,lwarpx,lwarpy;
		} else {
			dispbottom "No last warp recorded yet.";
			cutin "", 255;
			close;
		}

	} else if (@wrpD$[.@j] == "Towns") {
		switch(select(
			("Prontera:")+
			("Alberta:")+
			("Aldebaran:")+
			("Amatsu:")+
			("Ayothaya:")+
			("Comodo:")+
			("Einbech:")+
			("Einbroch:")+
			("Geffen:")+
			("Gonryun:")+
			("Hugel:")+
			("Izlude:")+
			("Jawaii:")+
			("Lighthalzen:")+
			("Louyang:")+
			("Lutie (xmas):")+
			("Morroc:")+	
			("Moscovia:")+
			("Nameless Island:")+
			("Niflheim:")+
			("Payon:")+
			("Rachel:")+
			("Umbala:")+
			("Veins:")+
			("Yuno:")+
			"^777777Close^000000"
		)) {
		case 1: lwarpmap$ = "prontera"; 	lwarpx = 155; 	lwarpy = 183; 	warp "prontera",155,183; break;
		case 2: lwarpmap$ = "alberta"; 		lwarpx = 28; 	lwarpy = 234; 	warp "alberta",28,234; break;
		case 3: lwarpmap$ = "aldebaran"; 	lwarpx = 140; 	lwarpy = 131; 	warp "aldebaran",140,131; break;
		case 4: lwarpmap$ = "amatsu"; 		lwarpx = 198; 	lwarpy = 84; 	warp "amatsu",198,84; break;
		case 5: lwarpmap$ = "ayothaya"; 	lwarpx = 208; 	lwarpy = 166; 	warp "ayothaya",208,166; break;
		case 6: lwarpmap$ = "comodo"; 		lwarpx = 209; 	lwarpy = 143; 	warp "comodo",209,143; break;
		case 7: lwarpmap$ = "einbech"; 		lwarpx = 63; 	lwarpy = 35; 	warp "einbech",63,35; break;
		case 8: lwarpmap$ = "einbroch"; 	lwarpx = 64; 	lwarpy = 200; 	warp "einbroch",64,200; break;
		case 9: lwarpmap$ = "geffen"; 		lwarpx = 119; 	lwarpy = 59; 	warp "geffen",119,59; break;
		case 10: lwarpmap$ = "gonryun"; 	lwarpx = 160; 	lwarpy = 120; 	warp "gonryun",160,120; break;
		case 11: lwarpmap$ = "hugel"; 		lwarpx = 96; 	lwarpy = 145; 	warp "hugel",96,145; break;
		case 12: lwarpmap$ = "izlude"; 		lwarpx = 128; 	lwarpy = 114; 	warp "izlude",128,114; break;
		case 13: lwarpmap$ = "jawaii"; 		lwarpx = 251; 	lwarpy = 132; 	warp "jawaii",251,132; break;
		case 14: lwarpmap$ = "lighthalzen"; 	lwarpx = 158; 	lwarpy = 92; 	warp "lighthalzen",158,92; break;
		case 15: lwarpmap$ = "louyang"; 	lwarpx = 217; 	lwarpy = 100; 	warp "louyang",217,100; break;
		case 16: lwarpmap$ = "xmas"; 		lwarpx = 147; 	lwarpy = 134; 	warp "xmas",147,134; break;
		case 17: lwarpmap$ = "morocc"; 		lwarpx = 156; 	lwarpy = 93; 	warp "morocc",156,93; break;
		case 18: lwarpmap$ = "moscovia"; 	lwarpx = 223; 	lwarpy = 184; 	warp "moscovia",223,184; break;
		case 19: lwarpmap$ = "nameless_n"; 	lwarpx = 256; 	lwarpy = 215; 	warp "nameless_n",256,215; break;
		case 20: lwarpmap$ = "niflheim"; 	lwarpx = 202; 	lwarpy = 174; 	warp "niflheim",202,174; break;
		case 21: lwarpmap$ = "payon"; 		lwarpx = 179; 	lwarpy = 100; 	warp "payon",179,100; break;
		case 22: lwarpmap$ = "rachel"; 		lwarpx = 130; 	lwarpy = 110; 	warp "rachel",130,110; break;
		case 23: lwarpmap$ = "umbala"; 		lwarpx = 97; 	lwarpy = 153; 	warp "umbala",97,153; break;
		case 24: lwarpmap$ = "veins"; 		lwarpx = 216; 	lwarpy = 123; 	warp "veins",216,123; break;
		case 25: lwarpmap$ = "yuno"; 		lwarpx = 157; 	lwarpy = 51; 	warp "yuno",157,51; break;
		default:
			cutin "", 255;
			close;
		}
	} else if (@wrpD$[.@j] == "Dungeons") {
		switch(select(
			("Abyss Lakes:")+
			( (event_amatsu == 6 && countitem(Lords_Passable_Ticket) > 0)?"Amatsu Dungeon:":"^777777Amatsu Dungeon (Access Quest)^000000:")+
			("Anthell:")+
			((ayodunquest > 2 && countitem(Thread_Skein) > 0)?"Ayothaya Dungeon:":"^777777Ayothaya Dungeon (Access Quest)^000000:")+
			("Beach Dungeon:")+
			((MISC_QUEST&512)?"Bio Labs:":"^777777Bio Labs (Access Quest)^000000:")+
			("Byalan Dungeon:")+
			("Clock Tower:")+
			("Coal Mines:")+
			((MISC_QUEST & 8)?"Prontera Culvert:":"^777777Prontera Culvert (Access Quest)^000000:")+
			("Einbroch Dungeon:")+
			((countitem(Lucifers_Lament) > 0)?"Gefenia:":"^777777Gefenia (Access Quest)^000000:")+
			("Geffen Dungeon:")+
			("Glast Heim:")+
			("Glast Heim Culvert:")+
			("Glast Heim Prison:")+
			("Glast Heim Chivalry:")+
			("Gonryun Dungeon:")+
			("Hidden Dungeon:")+
			("Ice Dungeon:")+
			("Juperos:")+	
			("Kiel Dungeon:")+
			("Louyang Dungeon:")+
			("Magma Dungeon:")+
			((getequipid(EQI_ACC_L) == GUSLI || getequipid(EQI_ACC_R) == GUSLI)?"Moscovia Dungeon:":"^777777Moscovia Dungeon (Access Quest)^000000:")+
			("Odin Temple:")+
			("Orc Dungeon:")+
			("Payon Dungeon:")+
			("Pyramids:")+
			(((ra_tem_q >= 21) || (MISC_QUEST & 8192))?"Rachel Sanctuary:":"^777777Rachel Sanctuary (Access Quest)^000000:")+
			("Sphinx:")+
			("Sunken Ship:")+
			("Thanatos Tower:")+
			("Thor Volcano:")+
			("Toy Factory:")+
			((TURTLE)?"Turtle Dungeon:":"^777777Turtle Dungeon (Access Quest)^000000:")+
			("Umbala Dungeon:")+
			"^777777Close^000000"
		)) {
		case 1: lwarpmap$ = "hu_fild05"; lwarpx = 167; lwarpy = 308; warp "hu_fild05",167,308; break;
		case 2: 
			if (event_amatsu == 6 && countitem(Lords_Passable_Ticket) > 0){
				 lwarpmap$ = "ama_dun01"; lwarpx = 229; lwarpy = 10; warp "ama_dun01",229,10; break;
			} else {
				dispbottom "Access Quest is required for direct warp.";
				cutin "", 255; close;
			}
		case 3: lwarpmap$ = "anthell01"; lwarpx = 37; lwarpy = 259; warp "anthell01",37,259; break;
		case 4: 
			if (ayodunquest > 2 && countitem(Thread_Skein) > 0) {
				lwarpmap$ = "ayo_fild02"; lwarpx = 277; lwarpy = 150; warp "ayo_fild02",277,150; break;
			} else {
				dispbottom "Access Quest is required for direct warp.";
				cutin "", 255; close;
			}
		case 5: lwarpmap$ = "cmd_fild01"; lwarpx = 34; lwarpy = 319; warp "cmd_fild01",34,319; break;	
		case 6: 
			if (MISC_QUEST&512) {
				lwarpmap$ = "lhz_cube"; lwarpx = 231; lwarpy = 17; warp "lhz_cube",231,17; break;

			} else {
				dispbottom "Access Quest is required for direct warp.";
				cutin "", 255; close;
			}			
		case 7: lwarpmap$ = "iz_dun00"; lwarpx = 168; lwarpy = 168; warp "iz_dun00",168,168; break;
		case 8: lwarpmap$ = "c_tower3"; lwarpx = 48; lwarpy = 42; warp "c_tower3",48,42; break;
		case 9: lwarpmap$ = "mjo_dun01"; lwarpx = 22; lwarpy = 284; warp "mjo_dun01",22,284; break;
		case 10:
			if (MISC_QUEST & 8) {
				 lwarpmap$ = "prt_sewb1"; lwarpx = 131; lwarpy = 247; warp "prt_sewb1",131,247; break;
			} else {
				dispbottom "Access Quest is required for direct warp.";
				cutin "", 255; close;
			}
		case 11: lwarpmap$ = "ein_dun01"; lwarpx = 261; lwarpy = 266; warp "ein_dun01",261,266; break;
		case 12: 
			if (countitem(Lucifers_Lament) > 0) {
				 lwarpmap$ = "gefenia01"; lwarpx = 58; lwarpy = 169; warp "gefenia01",58,169; break;
			} else {
				dispbottom "You must have "+getitemname(Lucifers_Lament)+" for direct warp.";
				cutin "", 255; close;
			}

		case 13: lwarpmap$ = "gef_dun00"; 	lwarpx = 107; 	lwarpy = 162; 	warp "gef_dun00",107,162; 	break;
		case 14: lwarpmap$ = "glast_01"; 	lwarpx = 371; 	lwarpy = 304;	warp "glast_01",371,304; 	break;
		case 15: lwarpmap$ = "gl_sew03"; 	lwarpx = 171; 	lwarpy = 281; 	warp "gl_sew03",171,281; 	break;
		case 16: lwarpmap$ = "gl_prison"; 	lwarpx = 149; 	lwarpy = 177; 	warp "gl_prison",149,177; 	break;
		case 17: lwarpmap$ = "gl_knt02"; 	lwarpx = 157; 	lwarpy = 285; 	warp "gl_knt02",157,285; 	break;
		case 18: lwarpmap$ = "gon_dun01"; 	lwarpx = 148; 	lwarpy = 45; 	warp "gon_dun01",148,45; 	break;
		case 19: lwarpmap$ = "prt_fild01"; 	lwarpx = 136; 	lwarpy = 366; 	warp "prt_fild01",136,366; 	break;
		case 20: lwarpmap$ = "ice_dun01"; 	lwarpx = 157; 	lwarpy = 14;	warp "ice_dun01",157,14; 	break;
		case 21: lwarpmap$ = "yuno_fild07"; 	lwarpx = 213; 	lwarpy = 176; 	warp "yuno_fild07",213,176; 	break;
		case 22: lwarpmap$ = "yuno_fild08"; 	lwarpx = 78; 	lwarpy = 181; 	warp "yuno_fild08",78,181; 	break;
		case 23: lwarpmap$ = "lou_dun01"; 	lwarpx = 181; 	lwarpy = 192; 	warp "lou_dun01",214,192; 	break;
		case 24: lwarpmap$ = "mag_dun01"; 	lwarpx = 125; 	lwarpy = 76; 	warp "mag_dun01",125,76; 	break;
		case 25: if (getequipid(EQI_ACC_L) == GUSLI || getequipid(EQI_ACC_R) == GUSLI){
				lwarpmap$ = "mosk_fild01"; 	lwarpx = 93; 	lwarpy = 94; 	warp "mosk_fild01",93,94; break;
			} else {
				dispbottom "You must equp "+getitemname(GUSLI)+" for direct warp.";
				cutin "", 255; close;
			}
		case 26: lwarpmap$ = "odin_tem01"; 	lwarpx = 99; 	lwarpy = 145; 	warp "odin_tem01",99,145; break;
		case 27: lwarpmap$ = "gef_fild10"; 	lwarpx = 52; 	lwarpy = 326; 	warp "gef_fild10", 52, 326; break;
		case 28: lwarpmap$ = "pay_dun00"; 	lwarpx = 178; 	lwarpy = 34; 	warp "pay_dun00",178,34; break;
		case 29: lwarpmap$ = "moc_pryd01"; 	lwarpx = 10; 	lwarpy = 188; 	warp "moc_pryd01",10,188; break;
		case 30: if ((ra_tem_q > 21) || (MISC_QUEST & 8192)) { 
				lwarpmap$ = "ra_san01"; 	lwarpx = 140; 	lwarpy = 135; 	warp "ra_san01",140,135; break;
			} else {
				dispbottom "Access Quest is required for direct warp.";
				cutin "", 255; close;
			}
		case 31: lwarpmap$ = "in_sphinx1"; 	lwarpx = 79; 	lwarpy = 183; 	warp "in_sphinx1",79,183; break;
		case 32: lwarpmap$ = "alb2trea"; 	lwarpx = 62; 	lwarpy = 69; 	warp "alb2trea",62,69; break;
		case 33: lwarpmap$ = "hu_fild01"; 	lwarpx = 140; 	lwarpy = 158; 	warp "hu_fild01",140,158; break;
		case 34: lwarpmap$ = "ve_fild03"; 	lwarpx = 168; 	lwarpy = 235; 	warp "ve_fild03",168,235; break;
		case 35: lwarpmap$ = "xmas_dun01"; 	lwarpx = 133; 	lwarpy = 131; 	warp "xmas_dun01",133,131; break;
		case 36: 
			if (TURTLE) {
				lwarpmap$ = "tur_dun01"; 	lwarpx = 157; 	lwarpy = 39; 	warp "tur_dun01",157,39; break;
			} else {
				dispbottom "Access Quest is required for direct warp.";
				cutin "", 255; close;
			}
		case 37: lwarpmap$ = "um_dun01"; 	lwarpx = 153; 	lwarpy = 204; 	warp "um_dun01",153,204; break;
		default:
			cutin "", 255;
			close;
		}
	} else if (@wrpD$[.@j] == "Central Office") {
		getmapxy(.@map$, @x, @y, 0);
		set officemap$,.@map$;
		set officex,@x;
		set officey,@y;
		lwarpmap$ = "office"; 	lwarpx = 128; 	lwarpy = 37; 	
		warp "office", 128, 37;
		end;
	} else if (@wrpD$[.@j] == "Main Town") {
		lwarpmap$ = "morocc"; 	lwarpx = 147; 	lwarpy = 86;	
		warp "morocc", 147, 86;
		end;
	} else {
		dispbottom "Invalid warp";
		cutin "", 255; close;
	}
	end;
}


//== Cart Function =========================================
// Arguments:
// - 0: The type of NPC this is being called from. Notable values:
//      0,1,3-5 = Regular Kafra
//      2 = Guild Castle Kafra (don't consume tickets)
//      6 = Zonda/Cool Event Staff (no RESRVPTS)
// - 1: Cost to rent a cart.
// - 2: NPC title to display.
function	script	F_KafCart	{
	// Ensure that the class wanting to rent a pushcart is a merchant
	if (BaseClass != Job_Merchant) {
		mes getarg(2);
		mes "I'm sorry, but the";
		mes "Pushcart rental service";
		mes "is only available to Merchants,";
		mes "Blacksmiths, Master Smiths,";
		mes "Alchemists and Biochemists.";
		return 1;
	}
	if (getskilllv(MC_PUSHCART) == 0) {
		mes getarg(2);
		mes "You can only rent a cart after";
		mes "learning the Pushcart Skill.";
		return 1;
	}
	// Make sure the invoking character does not have a cart already
	if (checkcart() == 1) {
		mes getarg(2);
		mes "You already have";
		mes "a Pushcart equipped.";
		mes "Unfortunately, we can't";
		mes "rent more than one to";
		mes "each customer at a time.";
		return 1;
	}
	// Consume "Free Ticket for the Cart Service" if available.
	// Do not consume if in Guild castle.
	if (countitem(Cart_Free_Ticket) > 0 && getarg(0) != 2) {
		delitem Cart_Free_Ticket, 1;
	} else {
		mes getarg(2);
		mes "The Pushcart rental";
		mes "fee is "+getarg(1)+" zeny. Would";
		mes "you like to rent a Pushcart?";
		next;
		if (select("Rent a Pushcart.", "Cancel") == 2)
			return 0;
		if (Zeny < getarg(1)) {
			mes getarg(2);
			mes "I'm sorry, but you";
			mes "don't have enough";
			mes "zeny to pay the Pushcart";
			mes "rental fee of "+getarg(1)+" zeny.";
			return 1;
		}
		Zeny -= getarg(1);
		if (getarg(0) != 6)
			RESRVPTS += 48;
	}
	setcart;
	return 1;
}

//== Special Reserve Points Function =======================
function	script	F_KafInfo	{
	// Uncomment next line to block Kafra Storage Protection
	//.@block = 1;
	setarray .@m$, "Check Special Reserve Points.","Storage Password Service","Kafra Employee Locations","Cancel";
	if (getarg(0) == 2) deletearray .@m$[1],2; // Port Malaya, Rune Knight: remove Storage Password and Kafra Employee Locations
	else if (.@block) deletearray .@m$[1],1; // Remove Storage Password
	while (true) {
		.@j = select(implode(.@m$,":")) - 1;
		if (.@m$[.@j] == "Check Special Reserve Points.") {
			mes "[Kafra Employee]";
			mes "Let's see...";
			mes strcharinfo(PC_NAME) + "...";
			mes "Ah, you have a total of";
			mes RESRVPTS+ " Special Reserve Points.";
			next;
			mes "[Kafra Employee]";
			mes "You can exchange your";
			mes "Special Reserve Points for";
			mes "rewards at the Kafra Main Office in Al De Baran. Please use our";
			mes "convenient services to see the benefits of our rewards program.";
			next;
			if(getarg(0) == 1)
				return;
		} else if (.@m$[.@j] == "Kafra Employee Locations") {
			for (.@i = 0; .@i <= 3; ++.@i)
				viewpoint 1,@viewpX[.@i],@viewpY[.@i],(.@i+1),0xFF00FF;
			next;
			for (.@i = 0; .@i <= 3; ++.@i)
				viewpoint 2,@viewpX[.@i],@viewpY[.@i],(.@i+1),0xFF00FF;
		} else if (.@m$[.@j] == "Storage Password Service") {
			callfunc("F_SetKafCode","[Kafra Employee]","Kafra Services");
		} else {
			return;
		}
	}
}

//== End Function ==========================================
// Arguments:
// - 0: used to determine what message to display.
// - 1: used to determine if save message is displayed.
// - 2: used to display the name of the area you're saving in.
function	script	F_KafEnd	{
	mes "[Kafra Employee]";
	if (getarg(1) == 1) {
		// Save
		mes "Your Respawn Point";
		mes "has been saved here";
		mes getarg(2)+".";
		mes "Thank you for using";
		mes "the Kafra Services.";
	} else if (getarg(0) == 0 || getarg(0) == 5) {
		// Generic End
		mes "We, here at Kafra Corporation,";
		mes "are always endeavoring to provide you with the best services. We hope that we meet your adventuring needs and standards of excellence.";
	} else if (getarg(0) == 1) {
		// Niflheim End
		percentheal 0,-25;
		mes "^666666Kaffffra n-never";
		mes "diiiiiiiiiiiiiies. On...";
		mes "On y-yooour siiiiide~^000000";
	}
	close2;
	cutin "", 255;
	end;
}

//== Check Storage Password Function =======================
function	script	F_CheckKafCode	{
	if (!#kafra_code)
		return;
	mes "Enter your storage password:";
	@code_ = 0;
	input(@code_, 0);
	if (@code_ != #kafra_code) {
		dispbottom "Wrong storage password.";
		close2;
		cutin "",255;
		end;
	}
	@kafcode_try = 0;
	@code_ = 0;
	return;
}

//== Set / Change / Clear Storage Password Function ========
// Arguments:
// - 0: NPC Name
// - 1: Company Name
function	script	F_SetKafCode	{
	mes getarg(0);
	if (#kafra_code) {
		mes "Your storage is protected with a password. What would you do now?";
		next;
		switch (select("Change old password -> 5000z", "Remove storage password -> 1000z", "Cancel")) {
			case 1:
				mes getarg(0);
				mes "At first, please enter your ^0000FFold password^000000.";
				@code = callfunc("F_EntKafCode");
				if (!@code || @code != #kafra_code) {
					mes "Wrong password. You can't set a new password.";
					emotion e_hmm;
					break;
				}
				next;
				callsub S_SET,getarg(0),getarg(1);
				break;
			case 2:
				mes getarg(0);
				mes "Please, enter your password before its removal.";
				@code = callfunc("F_EntKafCode");
				if (!@code) {
					mes "The password hasn't been removed.";
					emotion e_hmm;
					break;
				}
				next;
				mes getarg(0);
				if (Zeny < 1000) {
					mes "You don't have enough zeny.";
					emotion e_cash;
					break;
				}
				Zeny -= 1000;
				if (@code == #kafra_code) {
					#kafra_code = 0;
					mes "You've successfully cleared your storage password.";
					mes "Thank you for using "+getarg(1)+".";
					emotion e_thx;
				} else {
					mes "Wrong password. We won't return your 1000z.";
					mes "Please, next time enter correct password.";
					emotion e_sry;
				}
				break;
			case 3:
				break;
		}
	} else {
		mes getarg(1)+" proudly presents you a new service:";
		mes "Additional storage protection with a password.";
		next;
		switch (select("Set new password -> 5000z", "Cancel")) {
			case 1: callsub S_SET,getarg(0),getarg(1); break;
			case 2: break;
		}
	}
	close2;
	cutin "",255;
	end;

S_SET:
	mes getarg(0);
	mes "Now enter your ^FF0000new password^000000 to protect your storage from thieves.";
	@code = callfunc("F_EntKafCode");
	if (!@code) {
		mes "The password hasn't been changed.";
		emotion e_hmm;
		return;
	}
	next;
	mes getarg(0);
	if (Zeny < 5000) {
		mes "You don't have enough zeny.";
		emotion e_cash;
		return;
	}
	Zeny -= 5000;
	#kafra_code = @code;
	mes "You've protected your storage with a secret password.";
	mes "Thank you for using "+getarg(1)+".";
	emotion e_thx;
	return;
}

//== Basic Password Validation Function ====================
function	script	F_EntKafCode	{
	mes "Enter a number 1000~10000000:";
	@code_ = 0;
	++@kafcode_try;
	if (@kafcode_try > 10) {
		@kafcode_try = 0;
		logmes "Hack: Tried to fit storage password.";
	}
	if (input(@code_, 0) == 1) {
		mes "You can't use such big password.";
		return 0;
	}
	if (@code_ < 1000) {
		mes "You shouldn't use such short password.";
		return 0;
	}
	return @code_;
}

//== Warp Points / View Points Function ====================
function	script	F_KafSet	{
	deletearray @wrpC$;
	deletearray @wrpD$;
	deletearray @wrpP$;
	deletearray @viewpX;
	deletearray @viewpY;

	setarray @wrpD$, "Last Warp","Central Office","Main Town","Towns","Dungeons";
	setarray @wrpP, 0, 0, 0, 0;

	for (.@i = 0; .@i < getarraysize(@wrpD$); ++.@i)
		if( @wrpD$[.@i] == "Central Office" ) {
			@wrpC$[.@i] = "[ ^ff0066"+@wrpD$[.@i]+"^000000 ]";
		} else if( @wrpD$[.@i] == "Main Town") {
			@wrpC$[.@i] = "[ ^d6721f"+@wrpD$[.@i]+"^000000 ]";
		} else {
			@wrpC$[.@i] = @wrpD$[.@i];
		}
	@wrpC$[getarraysize(@wrpC$)] = "Cancel";
	return;
}
