//===== eAthena Script ======================================= 
//= Kafra Express - Warping Town Module
//===== By: ================================================== 
//= Skotlex 
//===== Current Version: ===================================== 
//= 2.1
//===== Compatible With: ===================================== 
//= eAthena SVN R3424+, RO Episode 8+ (Hugel)
//===== Description: ========================================= 
//= Part of the Kafra Express Script Package.
//= Offers warp services to towns.
//===== Additional Comments: ================================= 
//= See config.txt for configuration.
//= Flag values used for towns:
//= 0x00000001 Alberta
//= 0x00000002 AlDeBaran
//= 0x00000004 Amatsu
//= 0x00000008 Ayothaya
//= 0x00000010 Comodo
//= 0x00000020 Einbech
//= 0x00000040 Einbroch
//= 0x00000080 Geffen
//= 0x00000100 Gon Ryun
//= 0x00000200 Hugel
//= 0x00000400 Izlude
//= 0x00000800 Jawaii
//= 0x00001000 LightHalzen
//= 0x00002000 Lou Yang
//= 0x00004000 Lutie
//= 0x00008000 Morocc
//= 0x00010000 Niflheim
//= 0x00020000 Payon
//= 0x00040000 Prontera
//= 0x00080000 Umbala
//= 0x00100000 Yuno
//============================================================ 

-	script	keInit_warpTown	{
OnInit:	//Load Config
	donpcevent "keConfig::OnLoadWarpTown";
	end;
}

function	script	F_keWarpTown	{

	function SF_check;
	function SF_warp;

	set @cost,callfunc("F_keCost",$@kewt_cost,$@kewt_discount);
	set @niflcost,callfunc("F_keCost",$@kewt_niflCost,$@kewd_discount);
	if (@cost > Zeny && $@kewt_free)
		set @cost, Zeny;
	do {
		if ($@kewt_showOnline) {
			set @kmenu, select(
				"- Cancel",
				"- Alberta ("+@cost+"z/"+(getmapusers("alberta.gat")+getmapusers("alberta_in.gat"))+" "+$@ked_users$+")",
				"- Al De Baran ("+@cost+"z/"+(getmapusers("aldebaran.gat")+getmapusers("aldeba_in.gat"))+" "+$@ked_users$+")",
				"- Amatsu ("+@cost+"z/"+(getmapusers("amatsu.gat")+getmapusers("ama_in01.gat")+getmapusers("ama_in02.gat"))
					+" "+$@ked_users$+")",
				"- Ayothaya ("+@cost+"z/"+(getmapusers("ayothaya.gat")+getmapusers("ayo_in01.gat")+getmapusers("ayo_in02.gat"))
					+" "+$@ked_users$+")",
				"- Comodo ("+@cost+"z/"+(getmapusers("comodo.gat")+getmapusers("cmd_in01.gat")+getmapusers("cmd_in02.gat"))
					+" "+$@ked_users$+")",
				"- Einbech ("+@cost+"z/"+(getmapusers("einbech.gat"))+" "+$@ked_users$+")",
				"- Einbroch ("+@cost+"z/"+(getmapusers("einbroch.gat")+getmapusers("ein_in01.gat"))+" "+$@ked_users$+")",
				"- Geffen ("+@cost+"z/"+(getmapusers("geffen.gat")+getmapusers("geffen_in.gat")+getmapusers("gef_tower.gat"))
					+" "+$@ked_users$+")",
				"- Gon Ryun ("+@cost+"z/"+(getmapusers("gonryun.gat")+getmapusers("gon_in.gat"))+" "+$@ked_users$+")",
				"- Hugel ("+@cost+"z/"+(getmapusers("hugel.gat")+getmapusers("hu_in01.gat"))+" "+$@ked_users$+")",
				"- Izlude ("+@cost+"z/"+(getmapusers("izlude.gat")+getmapusers("izlude_in.gat"))+" "+$@ked_users$+")",
				"- Jawaii ("+@cost+"z/"+(getmapusers("jawaii.gat")+getmapusers("jawaii_in.gat"))+" "+$@ked_users$+")",
				"- LightHalzen ("+@cost+"z/"+(getmapusers("lighthalzen.gat")+getmapusers("lhz_in01.gat")
					+getmapusers("lhz_in02.gat")+getmapusers("lhz_in03.gat"))+" "+$@ked_users$+")",
				"- Lou Yang ("+@cost+"z/"+(getmapusers("louyang.gat")+getmapusers("lou_in01.gat")+getmapusers("lou_in02.gat"))
					+" "+$@ked_users$+")",
				"- Lutie ("+@cost+"z/"+(getmapusers("xmas.gat")+getmapusers("xmas_in.gat"))+" "+$@ked_users$+")",
				"- Morocc ("+@cost+"z/"+(getmapusers("morocc.gat")+getmapusers("morocc_in.gat")+getmapusers("moc_castle.gat"))
					+" "+$@ked_users$+")",
				"- Niflheim ("+@niflcost+"z/"+(getmapusers("niflheim.gat")+getmapusers("nif_in.gat"))+" "+$@ked_users$+")",
				"- Payon ("+@cost+"z/"+(getmapusers("payon.gat")+getmapusers("payon_in01.gat")+getmapusers("payon_in02.gat"))
					+" "+$@ked_users$+")",
				"- Prontera ("+@cost+"z/"+(getmapusers("prontera.gat")+getmapusers("prt_castle.gat")
					+getmapusers("prt_church.gat")+getmapusers("prt_in.gat"))+" "+$@ked_users$+")",
				"- Umbala ("+@cost+"z/"+(getmapusers("umbala.gat")+getmapusers("um_in.gat"))+" "+$@ked_users$+")",
				"- Yuno ("+@cost+"z/"+(getmapusers("yuno.gat")+getmapusers("yuno_in01.gat")+getmapusers("yuno_in02.gat")
					+getmapusers("yuno_in03.gat")+getmapusers("yuno_in04.gat")+getmapusers("yuno_in05.gat"))+" "+$@ked_users$+")"
			);
		} else {
			set @kmenu, select(
				"- Cancel",
				"- Alberta ("+@cost+"z)",
				"- Al De Baran ("+@cost+"z)",
				"- Amatsu ("+@cost+"z)",
				"- Ayothaya ("+@cost+"z)",
				"- Comodo ("+@cost+"z)",
				"- Einbech ("+@cost+"z)",
				"- Einbroch ("+@cost+"z)",
				"- Geffen ("+@cost+"z)",
				"- Gon Ryun ("+@cost+"z)",
				"- Hugel ("+@cost+"z)",
				"- Izlude ("+@cost+"z)",
				"- Jawaii ("+@cost+"z)",
				"- LightHalzen ("+@cost+"z)",
				"- Lou Yang ("+@cost+"z)",
				"- Lutie ("+@cost+"z)",
				"- Morocc ("+@cost+"z)",
				"- Niflheim ("+@niflcost+"z)",
				"- Payon ("+@cost+"z)",
				"- Prontera ("+@cost+"z)",
				"- Umbala ("+@cost+"z)",
				"- Yuno ("+@cost+"z)"
			);
		}
		switch (@kmenu) {
		case 2: //Alberta
			if (SF_check(0x1))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"alberta.gat",117,56;
			break;
		case 3: //AlDeBaran
			if (SF_check(0x2))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"aldebaran.gat",139,124;
			break;
		case 4: //Amatsu
			if (SF_check(0x4))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"amatsu.gat",197,88;
			break;
		case 5: //Ayathoya
			if (SF_check(0x8))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"ayothaya.gat",202,174;
			break;
		case 6: //Comodo
			if (SF_check(0x10))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"comodo.gat",189,150;
			break;
		case 7: //Einbech
			if (SF_check(0x20))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"einbech.gat",137,220;
			break;
		case 8: //Einbroch
			if (SF_check(0x40))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"einbroch.gat",64,199;
			break;
		case 9: //Geffen
			if (SF_check(0x80))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"geffen.gat",120,66;
			break;
		case 10: //GonRyun
			if (SF_check(0x100))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"gonryun.gat",160,180;
			break;
		case 11: //Hugel
			if (SF_check(0x200))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"hugel.gat",96,105;
			break;
		case 12: //Izlude
			if (SF_check(0x400))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"izlude.gat",127,97;
			break;
		case 13: //Jawaii
			if (SF_check(0x800))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"jawaii.gat",214,223;
			break;
		case 14: //LightHalzen
			if (SF_check(0x1000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"lighthalzen.gat",158,92;
			break;
		case 15: //LouYang
			if (SF_check(0x2000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"louyang.gat",218,118;
			break;
		case 16: //Lutie
			if (SF_check(0x4000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"xmas.gat",148,133;
			break;
		case 17: //Morocc
			if (SF_check(0x8000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"morocc.gat",162,91;
			break;
		case 18: //Niflheim
			if (SF_check(0x10000)) {
				if (nif_q_done != 1) {
					mes "...?";
					callfunc "F_keIntro", e_no, "For some reason I can't warp you there!";
				} else
					callfunc "F_keWarp",$@kewt_niflCost,$@kewd_discount,e_yawn,"niflheim.gat",194,185;
			}
			break;
		case 19: //Payon
			if (SF_check(0x20000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"payon.gat",159,181;
			break;
		case 20: //Prontera
			if (SF_check(0x40000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"prontera.gat",156,175;
			break;
		case 21: //Umbala
			if (SF_check(0x80000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"umbala.gat",88,153;
			break;
		case 22: //Yuno
			if (SF_check(0x100000))
				SF_warp $@kewt_cost,$@kewt_discount,e_yawn,"yuno.gat",158,77;
			break;
		}
	} while (@kmenu > 1);
	return;
	
//SubFunction SF_check(char-flag, account_flag)
//Checks if the character can warp to that town taking into consideration
//traveller's mode.
function SF_check {
	if ($@kewt_travel && !((kewt_travel|#kewt_travel)&getarg(0))) {
		callfunc "F_keIntro", e_sry, "Sorry, but we can only warp you to towns you have saved in at least once.";
		return 0;
	}
	return 1;
}

//SubFunction SF_warp (int cost, int discount, int emotion, String map, int x, int y)
//Modded from F_keWarp to allow free warping to towns when not enough zeny.
function SF_warp {
	set @cost, getarg(0);
	if (@cost > Zeny && $@kewt_free)
		set @cost, Zeny;
	if (!(callfunc("F_keCharge",@cost,getarg(1),1))) {
		callfunc "F_keIntro", e_an, "You don't have enough Zeny...";
		return;
	}
	emotion getarg(2);
	warp getarg(3),getarg(4),getarg(5);
	end;
}

}

//Invoked when we want to add a map to the list of travelled-to towns
function	script	F_keAddTravelTown	{
	set @map$, getarg(0);
// Temporary code to update variables from old format to new.
	if(ketw_alberta) {
		set kewt_travel,kewt_travel|0x1;
		set ketw_alberta,0;
	}
	if(ketw_aldebaran) {
		set kewt_travel,kewt_travel|0x2;
		set ketw_aldebaran,0;
	}
	if(ketw_amatsu) {
		set kewt_travel,kewt_travel|0x4;
		set ketw_amatsu,0;
	}
	if(ketw_ayothaya) {
		set kewt_travel,kewt_travel|0x8;
		set ketw_ayothaya,0;
	}
	if(ketw_comodo) {
		set kewt_travel,kewt_travel|0x10;
		set ketw_comodo,0;
	}
	if(ketw_einbech) {
		set kewt_travel,kewt_travel|0x20;
		set ketw_einbech,0;
	}
	if(ketw_einbroch) {
		set kewt_travel,kewt_travel|0x40;
		set ketw_einbroch,0;
	}
	if(ketw_geffen) {
		set kewt_travel,kewt_travel|0x80;
		set ketw_geffen,0;
	}
	if(ketw_gonryun) {
		set kewt_travel,kewt_travel|0x100;
		set ketw_gonryun,0;
	}
	if(ketw_hugel) {
		set kewt_travel,kewt_travel|0x200;
		set ketw_hugel,0;
	}
	if(ketw_izlude) {
		set kewt_travel,kewt_travel|0x400;
		set ketw_izlude,0;
	}
	if(ketw_jawaii) {
		set kewt_travel,kewt_travel|0x800;
		set ketw_jawaii,0;
	}
	if(ketw_lighthalzen) {
		set kewt_travel,kewt_travel|0x1000;
		set ketw_lighthalzen,0;
	}
	if(ketw_louyang) {
		set kewt_travel,kewt_travel|0x2000;
		set ketw_louyang,0;
	}
	if(ketw_lutie) {
		set kewt_travel,kewt_travel|0x4000;
		set ketw_lutie,0;
	}
	if(ketw_morroc) {
		set kewt_travel,kewt_travel|0x8000;
		set ketw_morroc,0;
	}
	if(ketw_niflheim) {
		set kewt_travel,kewt_travel|0x10000;
		set ketw_niflheim,0;
	}
	if(ketw_payon) {
		set kewt_travel,kewt_travel|0x20000;
		set ketw_payon,0;
	}
	if(ketw_prontera) {
		set kewt_travel,kewt_travel|0x40000;
		set ketw_prontera,0;
	}
	if(ketw_umbala) {
		set kewt_travel,kewt_travel|0x80000;
		set ketw_umbala,0;
	}
	if(ketw_yuno) {
		set kewt_travel,kewt_travel|0x100000;
		set ketw_yuno,0;
	}

	if(#ketw_alberta) {
		set #kewt_travel,#kewt_travel|0x1;
		set #ketw_alberta,0;
	}
	if(#ketw_aldebaran) {
		set #kewt_travel,#kewt_travel|0x2;
		set #ketw_aldebaran,0;
	}
	if(#ketw_amatsu) {
		set #kewt_travel,#kewt_travel|0x4;
		set #ketw_amatsu,0;
	}
	if(#ketw_ayothaya) {
		set #kewt_travel,#kewt_travel|0x8;
		set #ketw_ayothaya,0;
	}
	if(#ketw_comodo) {
		set #kewt_travel,#kewt_travel|0x10;
		set #ketw_comodo,0;
	}
	if(#ketw_einbech) {
		set #kewt_travel,#kewt_travel|0x20;
		set #ketw_einbech,0;
	}
	if(#ketw_einbroch) {
		set #kewt_travel,#kewt_travel|0x40;
		set #ketw_einbroch,0;
	}
	if(#ketw_geffen) {
		set #kewt_travel,#kewt_travel|0x80;
		set #ketw_geffen,0;
	}
	if(#ketw_gonryun) {
		set #kewt_travel,#kewt_travel|0x100;
		set #ketw_gonryun,0;
	}
	if(#ketw_hugel) {
		set #kewt_travel,#kewt_travel|0x200;
		set #ketw_hugel,0;
	}
	if(#ketw_izlude) {
		set #kewt_travel,#kewt_travel|0x400;
		set #ketw_izlude,0;
	}
	if(#ketw_jawaii) {
		set #kewt_travel,#kewt_travel|0x800;
		set #ketw_jawaii,0;
	}
	if(#ketw_lighthalzen) {
		set #kewt_travel,#kewt_travel|0x1000;
		set #ketw_lighthalzen,0;
	}
	if(#ketw_louyang) {
		set #kewt_travel,#kewt_travel|0x2000;
		set #ketw_louyang,0;
	}
	if(#ketw_lutie) {
		set #kewt_travel,#kewt_travel|0x4000;
		set #ketw_lutie,0;
	}
	if(#ketw_morroc) {
		set #kewt_travel,#kewt_travel|0x8000;
		set #ketw_morroc,0;
	}
	if(#ketw_niflheim) {
		set #kewt_travel,#kewt_travel|0x10000;
		set #ketw_niflheim,0;
	}
	if(#ketw_payon) {
		set #kewt_travel,#kewt_travel|0x20000;
		set #ketw_payon,0;
	}
	if(#ketw_prontera) {
		set #kewt_travel,#kewt_travel|0x40000;
		set #ketw_prontera,0;
	}
	if(#ketw_umbala) {
		set #kewt_travel,#kewt_travel|0x80000;
		set #ketw_umbala,0;
	}
	if(#ketw_yuno) {
		set #kewt_travel,#kewt_travel|0x100000;
		set #ketw_yuno,0;
	}
// End update code.

	if ($@kewt_travel > 1) {
		if (@map$ == "alberta.gat" || kewt_travel&0x1)
			set #kewt_travel,#kewt_travel|0x1;
		if (@map$ == "aldebaran.gat" || kewt_travel&0x2)
			set #kewt_travel,#kewt_travel|0x2;
		if (@map$ == "amatsu.gat" || kewt_travel&0x4)
			set #kewt_travel,#kewt_travel|0x4;
		if (@map$ == "ayothaya.gat" || kewt_travel&0x8)
			set #kewt_travel,#kewt_travel|0x8;
		if (@map$ == "comodo.gat" || kewt_travel&0x10)
			set #kewt_travel,#kewt_travel|0x10;
		if (@map$ == "einbech.gat" || kewt_travel&0x20)
			set #kewt_travel,#kewt_travel|0x20;
		if (@map$ == "einbroch.gat" || kewt_travel&0x40)
			set #kewt_travel,#kewt_travel|0x40;
		if (@map$ == "geffen.gat" || kewt_travel&0x80)
			set #kewt_travel,#kewt_travel|0x80;
		if (@map$ == "gonryun.gat" || kewt_travel&0x100)
			set #kewt_travel,#kewt_travel|0x100;
		if (@map$ == "hugel.gat" || kewt_travel&0x200)
			set #kewt_travel,#kewt_travel|0x200;
		if (@map$ == "izlude.gat" || kewt_travel&0x400)
			set #kewt_travel,#kewt_travel|0x400;
		if (@map$ == "jawaii.gat" || kewt_travel&0x800)
			set #kewt_travel,#kewt_travel|0x800;
		if (@map$ == "lighthalzen.gat" || kewt_travel&1000)
			set #kewt_travel,#kewt_travel|0x1000;
		if (@map$ == "louyang.gat" || kewt_travel&0x2000)
			set #kewt_travel,#kewt_travel|0x2000;
		if (@map$ == "xmas.gat" || kewt_travel&0x4000)
			set #kewt_travel,#kewt_travel|0x4000;
		if (@map$ == "morocc.gat" || kewt_travel&0x8000)
			set #kewt_travel,#kewt_travel|0x8000;
		if (@map$ == "niflheim.gat" || kewt_travel&0x10000)
			set #kewt_travel,#kewt_travel|0x10000;
		if (@map$ == "payon.gat" || kewt_travel&0x20000)
			set #kewt_travel,#kewt_travel|0x20000;
		if (@map$ == "prontera.gat" || kewt_travel&0x40000)
			set #kewt_travel,#kewt_travel|0x40000;
		if (@map$ == "umbala.gat" || kewt_travel&0x80000)
			set #kewt_travel,#kewt_travel|0x80000;
		if (@map$ == "yuno.gat" || kewt_travel&0x100000)
			set #kewt_travel,#kewt_travel|0x100000;
	} else {
		if (@map$ == "alberta.gat")
			set kewt_travel,kewt_travel|0x1;
		if (@map$ == "aldebaran.gat")
			set kewt_travel,kewt_travel|0x2;
		if (@map$ == "amatsu.gat")
			set kewt_travel,kewt_travel|0x4;
		if (@map$ == "ayothaya.gat")
			set kewt_travel,kewt_travel|0x8;
		if (@map$ == "comodo.gat")
			set kewt_travel,kewt_travel|0x10;
		if (@map$ == "einbech.gat")
			set kewt_travel,kewt_travel|0x20;
		if (@map$ == "einbroch.gat")
			set kewt_travel,kewt_travel|0x40;
		if (@map$ == "geffen.gat")
			set kewt_travel,kewt_travel|0x80;
		if (@map$ == "gonryun.gat")
			set kewt_travel,kewt_travel|0x100;
		if (@map$ == "hugel.gat")
			set kewt_travel,kewt_travel|0x200;
		if (@map$ == "izlude.gat")
			set kewt_travel,kewt_travel|0x400;
		if (@map$ == "jawaii.gat")
			set kewt_travel,kewt_travel|0x800;
		if (@map$ == "lighthalzen.gat")
			set kewt_travel,kewt_travel|0x1000;
		if (@map$ == "louyang.gat")
			set kewt_travel,kewt_travel|0x2000;
		if (@map$ == "xmas.gat")
			set kewt_travel,kewt_travel|0x4000;
		if (@map$ == "morocc.gat")
			set kewt_travel,kewt_travel|0x8000;
		if (@map$ == "niflheim.gat")
			set kewt_travel,kewt_travel|0x10000;
		if (@map$ == "payon.gat")
			set kewt_travel,kewt_travel|0x20000;
		if (@map$ == "prontera.gat")
			set kewt_travel,kewt_travel|0x40000;
		if (@map$ == "umbala.gat")
			set kewt_travel,kewt_travel|0x80000;
		if (@map$ == "yuno.gat")
			set kewt_travel,kewt_travel|0x100000;
	}
	return;
}
