//===== eAthena Script ======================================= 
//= Kafra Express - Uncarder Module
//===== By: ================================================== 
//= Skotlex 
//===== Current Version: ===================================== 
//= 1.6
//===== Compatible With: ===================================== 
//= eAthena SVN R3424+
//===== Description: ========================================= 
//= Part of the Kafra Express Script Package.
//= Offers uncarding services.
//===== Additional Comments: ================================= 
//= See config.txt for configuration.
//============================================================ 

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

function	script	F_keUncard	{

function SF_uncard;

	do {
		set @kmenu, select(
			"- Return",
			"- See Price Specifics",
			"- "+getequipname(1),
			"- "+getequipname(2),
			"- "+getequipname(3),
			"- "+getequipname(4),
			"- "+getequipname(5),
			"- "+getequipname(6),
			"- "+getequipname(7),
			"- "+getequipname(8),
			"- "+getequipname(9),
			"- "+getequipname(10)
		);
		if (@kmenu == 1)
			return;
		if (@kmenu == 2) {	//Details
			mes "Okay, listen up...";
			next;
			mes "["+@name$+"]";
			if ($@keuc_BaseCost)
				mes "The base cost is of "+$@keuc_BaseCost+"z.";
			mes "Each card to be removed incurs a cost of +"+$@keuc_CardCost+"z.";
			mes "The refine level of the compounded equipment can incur an additional cost:";
			mes "Armors: "+$@keuc_UpgradeCostA+"z per level.";
			mes "Lv1 Weapons: "+$@keuc_UpgradeCostW1+"z per level.";
			mes "Lv2 Weapons: "+$@keuc_UpgradeCostW2+"z per level.";
			mes "Lv3 Weapons: "+$@keuc_UpgradeCostW3+"z per level.";
			mes "Lv4 Weapons: "+$@keuc_UpgradeCostW4+"z per level.";

			if ($@keuc_Mat1 && $@keuc_Qty1) {
				mes "The materials needed are:";
				mes "- "+$@keuc_Qty1+" "+getitemname($@keuc_Mat1)+".";
			}
			if ($@keuc_Mat2 && $@keuc_Qty2)
				mes "- "+$@keuc_Qty2+" "+getitemname($@keuc_Mat2)+".";
			
			if ($@keuc_Fail1Chance || $@keuc_Fail2Chance)
				mes "There is a chance the process might fail and you'll lose your items.";

			next;
			callfunc "F_keIntro", -1, "";
		} else {
			if (SF_uncard(@kmenu-2))
				return;
		}
	} while (@kmenu >1);
	return;

//Subfunction SF_uncard (equipment-position)
//Attempts to uncard the equipment.
function SF_uncard {
	set @part, getarg(0);
	if(getequipcardcnt(@part) == 0) {
		callfunc "F_keIntro", e_ag, "There are no cards to remove there!";
		return 0;
	} 
	set @cost, $@keuc_BaseCost;
	set @cost, @cost + $@keuc_CardCost*getequipcardcnt(@part);

	switch (getequipweaponlv(@part)) {
	case 0: //Armor
		set @upgradeCost, $@keuc_UpgradeCostA;
		break;
	case 1: //Lv1 Weapons
		set @upgradeCost, $@keuc_UpgradeCostW1;
		break;
	case 2: //Lv2 Weapons
		set @upgradeCost, $@keuc_UpgradeCostW2;
		break;
	case 3: //Lv3 Weapons
		set @upgradeCost, $@keuc_UpgradeCostW3;
		break;
	default: //Lv4 Weapons
		set @upgradeCost, $@keuc_UpgradeCostW4;
	}
	
	set @cost, @cost + @upgradeCost*getequiprefinerycnt(@part);

	set @price, callfunc("F_keCost",@cost,$@keuc_discount);
	
	if ($@keuc_Fail1Chance) {
		set @failsave, select(
			"- Cancel",
			"- Remove cards ("+@price+"z, item gets priority)",
			"- Remove cards ("+@price+"z, cards get priority)"
		);
	} else {
		set @failsave, select(
			"- Cancel",
			"- Remove cards ("+@price+"z)"
		);
	}
	if (@failsave == 1)
		return 0;
	set @failsave, @failsave-1;	//1: Save item, 2: Save Card

	if (($@keuc_Mat1 && countitem($@keuc_Mat1) < $@keuc_Qty1)
		|| ($@keuc_Mat2 && countitem($@keuc_Mat2) < $@keuc_Qty2)) {
		callfunc "F_keIntro", e_pif, "You do not have all the materials I need...";
		return 0;
	}
	
	if (!(callfunc("F_keCharge",@cost,$@keuc_discount,1))) {
		callfunc "F_keIntro", e_ag, "Sorry, you don't have enough zeny.";
		return 0;
	}
	
	if ($@keuc_Mat1 && $@keuc_Qty1)
		delitem $@keuc_Mat1, $@keuc_Qty1;
	if ($@keuc_Mat2 && $@keuc_Qty2)
		delitem $@keuc_Mat2, $@keuc_Qty2;
	
	set @failtype,-1;
	//Recycling cost...
	set @cost, rand(1000);
	if (@cost < $@keuc_Fail2Chance)	//Total Failure
		set @failtype, 0;
	else if (@cost < $@keuc_Fail1Chance)	//Partial Failure
		set @failtype, @failsave;
	else if (@cost < $@keuc_Fail0Chance)	//Harmless Failure
		set @failtype, 3;
	else {	//Success
		successremovecards @part;
		emotion e_ho;
		return 1;
	}
	failedremovecards @part,@failtype;
	if (@failtype == 1)
		mes "The cards were lost...";
	if (@failtype == 2)
		mes "The item was lost...";
	if (@failtype == 0)
		mes "Lost the cards and item...";
	callfunc "F_keIntro", e_swt, "...oops.";
	return 1;
}

}
