//===== eAthena Script =======================================
//= Kafra Express - Refining Module
//===== By: ==================================================
//= Skotlex
//===== Current Version: =====================================
//= 2.7
//===== Compatible With: =====================================
//= eAthena SVN R3424+
//===== Description: =========================================
//= Part of the Kafra Express Script Package.
//= Does item forging, ore purifying and repairing.
//= Can also sell phracon/emveretarcon
//===== Additional Comments: =================================
//= See config.txt for configuration.
//============================================================

-	script	keInit_refine	-1,{
OnInit:	//Load Config
	donpcevent "keConfig::OnLoadRefine";
	end;
}

function	script	F_keRefine	{

function SF_refine;
function SF_repair;
function SF_purify;
function SF_sellOres;

	do {
		set @kmenu, select(
			"- Return",
			"- Refine Equipment",
			"- Repair Equipment",
			"- Buy Ores",
			"- Purify Ores"
		);
		switch(@kmenu) {
		case 2: //Refine
			SF_refine();
			break;
		case 3: //Repair
			SF_repair();
			break;
		case 4: //Buy Ore
			SF_sellOres();
			break;
		case 5: //Purify Ore
			SF_purify();
			break;
		}
	} while (@kmenu > 1);
	return;

//Subfunction SF_refine, presents the refining menu.
function	SF_refine {
	do {
		set @part, select(
			"- Cancel upgrading",
			"- "+getequipname(1),
			"- "+getequipname(2),
			"- "+getequipname(3),
			"- "+getequipname(4),
			"- "+getequipname(5),
			"- "+getequipname(6),
			"- "+getequipname(7),
			"- "+getequipname(8),
			"- "+getequipname(9),
			"- "+getequipname(10)
		);
		set @part, @part-1;
		if (@part == 0)
			break;
		if (getequipisequiped(@part) == 0) {
			callfunc "F_keIntro", e_swt, "There is nothing to refine there...";
		} else
		if(getequipisenableref(@part) == 0 && $@kerf_refineAll == 0) {
			callfunc "F_keIntro", -1, "Sorry, this item cannot be refined.";
		} else
		if(getequiprefinerycnt(@part) >= $@kerf_maxLv) {
			callfunc "F_keIntro", -1, "This item cannot be refined further.";
			break;
		} else {
			switch (getequipweaponlv(@part))
			{
			case 0: //Armor
				set @mat,985;
				set @cost,$@kerf_armorCost;
				break;
			case 1:
				set @mat,1010;
				set @cost,$@kerf_weaponLv1Cost;
				break;
			case 2:
				set @mat,1011;
				set @cost,$@kerf_weaponLv2Cost;
				break;
			case 3:
				set @mat,984;
				set @cost,$@kerf_weaponLv3Cost;
				break;
			default:
				set @mat,984;
				set @cost,$@kerf_weaponLv4Cost;
				break;
			}
			set @cost, callfunc("F_keCost",@cost,$@kerf_discount);
			do {
				if ($@kerf_safe==0 && getequippercentrefinery(@part) < 100) {
					if ($@kerf_showChance) {
						if (select(
							"- Return ("+(100-getequippercentrefinery(@part))+"% chance refine will fail)",
							"- Refine (Need "+getitemname(@mat)+" and "+@cost+"z)") != 2)
							break;
					} else {
						if (select(
							"- Return (Upgrade not safe, item might break!)",
							"- Refine (Need "+getitemname(@mat)+" and "+@cost+"z)") != 2)
							break;
					}
				} else {
					if (select(
						"- Refine (Need "+getitemname(@mat)+" and "+@cost+"z)",
						"- Cancel") != 1)
						break;
				}
				if (getequiprefinerycnt(@part) >= $@kerf_maxLv) {
					callfunc "F_keIntro", e_ok, "This item has reached it's maximum level.";
					break;
				}
				if (countitem(@mat) < 1) {
					callfunc "F_keIntro", e_ag, "Sorry, you don't have the materials I need.";
					break;
				}
				if (!(callfunc("F_keCharge",@cost,$@kerf_discount,1))) {
					callfunc "F_keIntro", e_ag, "Sorry, you don't have enough zeny.";
					break;
				}
				delitem @mat,1;
				if ($@kerf_safe || getequippercentrefinery(@part)>rand(100)) {
					successrefitem @part;
					emotion e_rock;
				} else {
					failedrefitem @part;
					callfunc "F_keIntro", e_swt, "...sorry.";
					break;
				}
			} while (1);
		}
	} while (@part > 0);
	return;
}

//Subfunction: SF_repair()
function	SF_repair {
	set @cost,callfunc("F_keCost",$@kerf_repairCost,$@kerf_repairDiscount);
	do {
		set @broken1,getbrokenid(1);
		if(@broken1==NULL) {
			callfunc "F_keIntro", -1, "You don't need anything repaired for now.";
			return;
		}
		set @broken2,getbrokenid(2);
		set @broken3,getbrokenid(3);
		set @broken4,getbrokenid(4);
		set @broken5,getbrokenid(5);
		set @broken6,getbrokenid(6);
		set @broken7,getbrokenid(7);
		set @broken8,getbrokenid(8);
		set @broken9,getbrokenid(9);
		set @broken10,getbrokenid(10);

		if ($@kerf_repairSteel)
			set @str$,"- Cancel (Repairing costs "+@cost+"z and 1 "+getitemname(999);
		else
			set @str$,"- Cancel (Repairing costs "+@cost+"z)";

		set @idRepair, select(
			@str$,
			getitemname(@broken1),
			getitemname(@broken2),
			getitemname(@broken3),
			getitemname(@broken4),
			getitemname(@broken5),
			getitemname(@broken6),
			getitemname(@broken7),
			getitemname(@broken8),
			getitemname(@broken9),
			getitemname(@broken10)
		);
		set @idRepair, @idRepair-1;
		if (@idRepair == 0) break;
		if ($@kerf_repairSteel && countitem(999) < 1) {
			callfunc "F_keIntro", e_ag, "Sorry, you don't have the materials I need.";
			return;
		}
		if (!(callfunc("F_keCharge", $@kerf_repairCost, $@kerf_repairDiscount,1))) {
			callfunc "F_keIntro", e_ag, "Sorry, you don't have enough zeny.";
			return;
		}
		if ($@kerf_repairSteel) delitem 999,1;
		repair(@idRepair);
		emotion e_lv2;
	} while (@idRepair > 0);
}

//Subfunction: SF_purify()
function SF_purify {
	if ($@kerf_purifyAll) {
		set @submenu, select (
			"- Return",
			"- Purify "+getitemname(984)+" (need 5 "+getitemname(756)+")",
			"- Purify "+getitemname(985)+" (need 5 "+getitemname(757)+")",
			"- Purify "+getitemname(998)+" (need 2 "+getitemname(1002)+")",
			"- Temper "+getitemname(999)+" (need 5 "+getitemname(998)+", 1 "+getitemname(1003)+")",
			"- Temper "+getitemname(994)+" (need 10 "+getitemname(990)+")",
			"- Temper "+getitemname(995)+" (need 10 "+getitemname(991)+")",
			"- Temper "+getitemname(996)+" (need 10 "+getitemname(992)+")",
			"- Temper "+getitemname(997)+" (need 10 "+getitemname(993)+")",
			"- Temper "+getitemname(1000)+" (need 10 "+getitemname(1001)+")"
		);
	} else {
		set @submenu, select(
			"- Return",
			"- Purify "+getitemname(984)+" (need 5 "+getitemname(756)+")",
			"- Purify "+getitemname(985)+" (need 5 "+getitemname(757)+")"
		);
	}
	switch (@submenu) {
	case 2: //Oridecon
		set @item, 984;
		set @rough, 756;
		set @qty, 5;
		break;
	case 3: //Elunium
		set @item, 985;
		set @rough, 757;
		set @qty, 5;
		break;
	case 4: //Iron
		set @item, 998;
		set @rough, 1002;
		set @qty, 2;
		break;
	case 5: //Steel
		set @item,999;
		set @rough,998;
		set @qty,5;
		set @rough2,1003;
		set @qty2,1; 

		set @count,countitem(@rough1);
		set @count2,countitem(@rough2);
		if (@count < @qty || @count2 < @qty2) {
			callfunc "F_keIntro", e_ag, "Sorry, you don't have the materials I need.";
			return;
		}
		set @count, @count/@qty;
		set @count2, @count2/@qty2;
		if (@count2 < @count)
			set @count,@count2;
		delitem @rough2,@qty2*@count;
		break;
	case 6: //Fire
		set @item, 994;
		set @rough, 990;
		set @qty, 10;
		break;
	case 7: //Ice	
		set @item, 995;
		set @rough, 991;
		set @qty, 10;
		break;
	case 8: //Wind
		set @item, 996;
		set @rough, 992;
		set @qty, 10;
		break;
	case 9: //Earth	
		set @item, 997;
		set @rough, 993;
		set @qty, 10;
		break;
	case 10: //Star
		set @item, 1000;
		set @rough, 1001;
		set @qty, 10;
		break;
	default:
		return;
	}
	set @count,countitem(@rough);
	if (@count < @qty) {
		callfunc "F_keIntro", e_ag, "Sorry, you don't have the materials I need.";
		return;
	}
	set @count, @count/@qty;
	delitem @rough,@qty*@count;
	getitem @item,@count;
	emotion e_paper;
}

//Subfunction: SF_sellOres()
function SF_sellOres {
	do {
		set @submenu, select(
			"- Return",
			"- Buy "+getitemname(1010)+" (200z each)",
			"- Buy "+getitemname(1011)+" (1000z each)"
		);
		switch (@submenu) {
		case 2: //Phracon
			set @mat, 1010;
			set @cost, 200;
			break;
		case 3: //Emveratarcon
			set @mat, 1011;
			set @cost, 1000;
			break;
		default:
			return;
		}
		input @qty;
		set @cost,@cost*@qty;
		if (@qty < 1) {
			callfunc "F_keIntro", e_pif, "That is NOT a valid quantity...";
		} else if (@cost > Zeny) {
			callfunc "F_keIntro", e_ag, "Sorry, you don't have enough zeny.";
		} else if (@cost < 0) {
			callfunc "F_keIntro", e_swt2, "Sorry, that's too much! Try buying less...";
		} else {
			set Zeny,Zeny-@cost;
			getitem @mat,@qty;
			emotion e_paper;
		}
	} while (@submenu > 1);
}

}