//===== rAthena Script ======================================= 
//= Renewal Refining NPCs
//===== By: ==================================================
//= rAthena Dev Team
//===== Current Version: =====================================
//= 1.3
//===== Compatible With: =====================================
//= rAthena SVN
//===== Description: =========================================
//= Renewal-specific refining NPCs and material merchants.
//===== Additional Comments: =================================
//= 1.0 Moved some scripts to Renewal file, optimized "Austry" NPC. [Euphy]
//= 1.0a Added 'disable_items' command. [Euphy]
//= 1.1 Added Malangdo Refiner "Clink". [Euphy]
//= 1.2 Added official success calculation, thanks to Helvetica.
//=     The safe/multiple refine feature is now functional. [Euphy]
//= 1.3 Updated to match the latest official script. [Euphy]
//============================================================

// +11 and above Refiners
//============================================================
prt_in,90,72,5	script	˹#prt	826,{
	callfunc "refinenew","˹",0;
	end;
}
morocc_in,64,41,5	script	˹#moc	826,{
	callfunc "refinenew","˹",0;
	end;
}
payon_in01,18,132,3	script	˹#pay	826,{
	callfunc "refinenew","˹",0;
	end;
}

//============================================================
// +11 and above Refiner Function
//============================================================
//= To allow auto safe refining/multiple refining set the
//= second argument to '1' in the function call.
//= If you enable this function, be sure to edit the value of
//= .@safe to the max safe refine in refine_db.txt as well.
//=
//= On official servers, if an item is unsuccessfully refined
//= it will break at a 20% rate and downgrade at an 80% rate.
//============================================================
function	script	refinenew	{
	disable_items;
	mes "["+ getarg(0) +"]";
	mes "õ!";
	mes "ͨװ, Ҳṩ.";
	mes "ֻԾֵ 9ϵװо.";
	next;
	mes "["+ getarg(0) +"]";
	mes "װװоֵ9 ϵװҪ. ҾͻῼǶ.";
	mes "ĸװ?";
	next;

	setarray .@position$[1],"- ͷ","- ·","- ","- ","- ","- Ь","- װƷ","- װƷ","- ͷ","- ͷ";
	set .@menu$,"";
	for(set .@i,1; .@i<=10; set .@i,.@i+1) {
		if (getequipisequiped(.@i)) {
			set .@menu$, .@menu$ + .@position$[.@i] + "-" + "[" + getequipname(.@i) + "]";
			set .@equipped,1;
		}
		set .@menu$, .@menu$ + ":";
	}
	if (.@equipped == 0) {
		mes "[" + getarg(0) + "]";
		mes "޷ЯƷ...";
		close;
	}
	set .@part, select(.@menu$);
	
	if (!getequipisequiped(.@part)) { //custom check
		mes "[" + getarg(0) + "]";
		mes "ѡλ";
		mes "Ϊʲôû,";
		mes "Ҿʲô.";
		emotion e_an;
		close;
	}
	if (!getequipisenableref(.@part)) {
		mes "[" + getarg(0) + "]";
		mes "Ϊ޷";
		mes "װ...";
		close;
	}
	if (getequiprefinerycnt(.@part) < 10) {
		mes "["+ getarg(0) +"]";
		mes "˵Ҳȥ lv.10 µװ.";
		close;
	}
	if (getequiprefinerycnt(.@part) >= 20) { //custom check
		mes "["+ getarg(0) +"]";
		mes "û";
		mes "ΪѾ˾";
		mes "!";
		close;
	}
	set .@refineitemid, getequipid(.@part); // save id of the item
	set .@refinerycnt, getequiprefinerycnt(.@part); //save refinery count
	if ((getequipweaponlv(.@part) >= 1) && (getequipweaponlv(.@part) <= 4)) {
		set .@type$,"weapon";
		set .@material,6224; //Bradium
		set .@price,100000;
		switch(getequipweaponlv(.@part)) {
			case 1: set .@safe,17; break;
			case 2: set .@safe,16; break;
			case 3: set .@safe,15; break;
			case 4: set .@safe,14; break;
		}
		mes "["+ getarg(0) +"]";
		mes "һ, ǰ?";
		mes "Ҫ,";
		mes "Ҫȡ 1 ^003366^000000  100,000 zeny.";
	} else {
		set .@type$,"armor";
		set .@material,6223; //Carnium
		set .@price,100000;
		set .@safe,14;
		mes "["+ getarg(0) +"]";
		mes "һװ, ǰ?";
		mes "Ҫװ,";
		mes "Ҫȡ 1 ^003366ħ֮ʯ^000000  100,000 zeny.";
	}
	mes "׼ü?";
	next;
	if(select("- :- ") == 2){
		mes "["+ getarg(0) +"]";
		mes "ð, սҲһʽ...";
		close;
	}
	if (getarg(1) != 1) {
		if (getequippercentrefinery(.@part) < 100) {
			mes "["+ getarg(0) +"]";
			mes ""+.@type$+"Ѿ.";
			mes "ٴξܻᱻٵ.";
			mes "Ȼ 100%, һСʵ.";
			next;
			mes "["+ getarg(0) +"]";
			mes "Ľܻ^FF0000;^000000 װ "+.@type$+",";
			mes "ʧĻ, 㽫ͬʱʧȥϵ ^FF0000Ƭ^000000 ĸħЧ.";
			next;
			mes "["+ getarg(0) +"]";
			mes "Ľܻװľȼ,";
			mes "ʧĻ, 㽫ͬʱʧȥװϵ ^FF0000Ƭ^000000 ĸħЧ.";
			next;
			if(select(".:.") == 2) {
				mes "["+ getarg(0) +"]";
				mes "㻹Ҽ?";
				mes "Ѿ㹻.";
				close;
			}
		}
		if (countitem(.@material) < 1 || Zeny < .@price) {
			mes "["+ getarg(0) +"]";
			mes "ܺ, ʱҲһֺõѡ...";
			mes "ûз... ǵ.";
			close;
		}
		set Zeny,Zeny - .@price;
		delitem .@material,1;

		//custom checks
		if (getequipisequiped(.@part) == 0) { // hacker has removed the item (not changed, why?)
			mes "[" + getarg(0) + "]";
			mes "... ǲλǿյ...";
			close;
		}
		if (getequiprefinerycnt(.@part) != .@refinerycnt || getequipid(.@part) != .@refineitemid) { // hacker has changed the item
			mes "[" + getarg(0) + "]";
			emotion e_an;
			mes "Ե...";
			mes "㵱ɵ?!";
			mes "Ƥ¸װ?! ȥƭ!";
			close;
		}

		if (getequippercentrefinery(.@part) > rand(100)) {
			mes "! ! ! !";
			successrefitem .@part;
			next;
			emotion e_no1;
			mes "["+ getarg(0) +"]";
			mes "ܺ! !!!";
			mes "õ.";
			close;
		} else {
			if (rand(100) < 80) {
				mes "["+ getarg(0) +"]";
				mes "! ! ! !";
				downrefitem .@part;
				next;
				emotion (!rand(5))?e_cash:e_omg;
				mes "["+ getarg(0) +"]";
				mes "!!!";
				next;
				mes "["+ getarg(0) +"]";
				mes ", ҵ!";
				mes "ȼ½...";
			} else {
				mes "["+ getarg(0) +"]";
				mes "! ! !";
				failedrefitem .@part;
				next;
				emotion (!rand(5))?e_cash:e_omg;
				mes "["+ getarg(0) +"]";
				mes "!";
				next;
				mes "["+ getarg(0) +"]";
				mes ", ҵ! Ч...";
				mes "Ҳ˼!";
			}
			mes "ʹȫעǷһЩ鷳.";
			mes "ҵ֮.";
			next;
			mes "["+ getarg(0) +"]";
			mes "´һСĵ! õ!";
			close;
		}
	}
// New +11 and above Refining Functions ========================
	if (getequiprefinerycnt(.@part) < .@safe) {
		mes "[" + getarg(0) + "]";
		mes "ҿԽȫֵָķΧ. ôѡ.";
		next;
		set .@menu2,select("- 뾫ȫֵ.","- Լѡ.","- Ҹ...");
	} else
		set .@menu2,2;
	switch(.@menu2){
	case 1: 
		set .@refinecnt,.@safe - getequiprefinerycnt(.@part);
		break;
	case 2:
		mes "[" + getarg(0) + "]";
		mes "ٴ?";
		next;
		input .@refinecnt;
		set .@refinecheck,.@refinecnt + getequiprefinerycnt(.@part);
		if (.@refinecnt < 1 || .@refinecheck > 20) {
			mes "[" + getarg(0) + "]";
			mes "޷پװ.";
			close;
		}
		if (.@refinecheck > .@safe) {
			set .@refinecheck,.@refinecheck - .@safe;
			mes "[" + getarg(0) + "]";
			mes "ѡ " + .@refinecheck + " , ᳬȫֵ. װܻٻ... û?";
			next;
			if(select("- ...:- ...") == 2){
				mes "[" + getarg(0) + "]";
				mes "Ļ... ԼŰ.";
				close;
			}
		}
		break;
	case 3:
		mes "[" + getarg(0) + "]";
		mes "ô˵.......";
		close;
	}
	set .@fullprice,.@price * .@refinecnt;
	mes "[" + getarg(0) + "]";
	mes "ΪҪ֧ " + .@refinecnt + " " + getitemname(.@material) + "  " + .@fullprice + " Zeny. û?";
	next;
	if(select("- ","- ...") == 2){
		mes "[" + getarg(0) + "]";
		mes "Ļ... ԼŰ.";
		close;
	}
	if (countitem(.@material) < .@refinecnt || Zeny < .@fullprice) {
		mes "[" + getarg(0) + "]";
		mes "ȫĶ? ҿɲΪͼ۸ɻ. ԰ҲЬ.";
		mes "´ʱϣ׼ȫ.";
		close;
	}
	set Zeny,Zeny - .@fullprice;
	delitem .@material,.@refinecnt;
	while(.@refinecnt){
		if (getequipisequiped(.@part) == 0) {
			mes "[" + getarg(0) + "]";
			mes "... ǲλǿյ......";
			close;
		}
		if (getequipid(.@part) != .@refineitemid || (.@menu2 == 1 && getequippercentrefinery(.@part) < 100)) {
			mes "[" + getarg(0) + "]";
			mes "Ե...";
			mes "㵱ɵ?!";
			mes "Ƥ¸װ?! ȥƭ!";
			close;
		}
		if (getequippercentrefinery(.@part) > rand(300)) {
			mes "! ! ! !";
			successrefitem .@part;
			set .@refinecnt,.@refinecnt - 1;
			next;
		} else {
			if (rand(100) < 80) {
				mes "["+ getarg(0) +"]";
				mes "! ! ! !";
				downrefitem .@part;
				next;
				emotion (!rand(5))?e_cash:e_omg;
				mes "["+ getarg(0) +"]";
				mes "!!!";
				next;
				mes "["+ getarg(0) +"]";
				mes ", ҵ!";
				mes "ȼ½...";
			} else {
				mes "["+ getarg(0) +"]";
				mes "! ! !";
				failedrefitem .@part;
				next;
				emotion (!rand(5))?e_cash:e_omg;
				mes "["+ getarg(0) +"]";
				mes "!";
				next;
				mes "["+ getarg(0) +"]";
				mes ", ҵ! Ч...";
				mes "Ҳ˼!";
			}
			mes "ʹȫעǷһЩ鷳.";
			mes "ҵ֮.";
			next;
			mes "["+ getarg(0) +"]";
			mes "´һСĵ! õ!";
			close;
		}
	}
	emotion e_no1;
	mes "["+ getarg(0) +"]";
	mes "ǳ! !!!";
	mes "õ.";
	close;
}

// Ori/Elu to Carnium/Bradium Refiners
//============================================================
-	script	Austry#ref	-1,{
	if (checkweight(1201,1) == 0) {
		mes "- ȵ !! -";
		mes "- ̫Ķ -";
		mes "- ⽫Ӱ콻. -";
		mes "-  -";
		mes "- . -";
		close;
	}
	mes "[˹]";
	mes "ܸ3֮";
	mes "3, ͿԽ";
	mes "1ħ֮ʯ1.";
	mes "Ҫȡ";
	mes "50,000z .";
	next;
	switch(select("- ֮ħ֮ʯ:- :- þħ֮ʯ:- ȡ")) {
	case 1:
		setarray .@i[0],984,3,6224;  //Oridecon -> Bradium
		break;
	case 2:
		setarray .@i[0],985,3,6223;  //Elunium -> Carnium
		break;
	case 3:
		setarray .@i[0],6090,1,6223; //Purified_Bradium -> Carnium
		break;
	case 4:
		mes "[˹]";
		mes "...";
		close;
	}
	if (countitem(.@i[0]) >= .@i[1] && Zeny >= 50000) {
		delitem .@i[0],.@i[1];
		set Zeny, Zeny - 50000;
		getitem .@i[2],1;
		mes "[˹]";
		if (.@i[0] == 6090) {
			mes "ħ֮ʯҪ΢һ㡭";
			mes "ֻ1";
			mes "㽻.";
		} else
			mes "!  "+getitemname(.@i[2])+".";
		mes "ϣܺú.";
		close;
	}
	mes "[˹]";
	mes "ñƭ";
	mes "ϸ";
	mes "ûд㹻 zeny";
	mes " "+getitemname(.@i[0])+".";
	close;
}
prt_in,85,71,5	duplicate(Austry#ref)	˹#prt	826
payon_in01,14,125,5	duplicate(Austry#ref)	˹#pay	826
morocc_in,60,38,5	duplicate(Austry#ref)	˹#moc	826

// Malangdo Refiner
//============================================================
malangdo,224,172,6	script	Clink#mal_normal	544,{
	disable_items;
	mes "[Clink]";
	mes "My cool dad Holink said I have the world's greatest refine hammer!!";
	mes "Meow Meow~";
	mes "Who do you think I am?";
	mes "Yes!!! You!! You want to refine?";
	next;
	setarray .@position$[1],"ͷ","·","","","","Ь","װƷ","װƷ","ͷ","ͷ";
	set .@menu$,"";
	for(set .@i,1; .@i<=10; set .@i,.@i+1)
		set .@menu$, .@menu$+((getequipisequiped(.@i))?getequipname(.@i):.@position$[.@i]+"-[δװ]")+":";
	set .@part, select(.@menu$);
	if (!getequipisequiped(.@part)) {
		mes "[Clink]";
		switch(.@part) {
		case 1:
			mes "Dad said. There's no cure for stupidity...";
			break;
		case 2:
			mes "There's nothing to see here!!";
			break;
		case 3:
			mes "What an arrogant left hand this is!";
			break;
		case 4:
			mes "What an arrogant right hand this is!";
			break;
		case 5:
			mes "Get that dirty thing off my face!!";
			break;
		case 6:
			mes "Kyaong~! Do not provoke me.";
			break;
		case 7:
		case 8:
			mes "Where is the accessory?";
			break;
		case 9:
		case 10:
			mes "Are you talking about the other head part?";
			break;
		}
		close;
	}
	if (!getequipisenableref(.@part)) {
		mes "[Clink]";
		mes "This can't be refined!!";
		close;
	}
	if (getequiprefinerycnt(.@part) >= 10) {
		mes "[Clink]";
		mes "Perfect refining. Did I do this for you?";
		close;
	}
	mes "[Clink]";
	switch(getequipweaponlv(.@part)) {
	default:
	case 0: // Armor
		set .@price,2000;
		set .@material,985; //Elunium
		set .@type$,"armor";
		mes "Hmm, an armor refine? Someone like you?";
		break;
	case 1: // Level 1 Weapon
		set .@price,50;
		set .@material,1010; //Phracon
		set .@type$,"weapon";
		mes "A level 1 weapon?";
		mes "Urr... Annoying... Okay, let's try...";
		break;
	case 2: // Level 2 Weapon
		set .@price,200;
		set .@material,1011; //Emveretarcon
		set .@type$,"weapon";
		mes "A level 2 weapon?";
		break;
	case 3: // Level 3 Weapon
		set .@price,20000;
		set .@material,984; //Oridecon
		set .@type$,"weapon";
		mes "Woot!! A level 3 weapon? Impressive~";
		break;
	case 4: // Level 4 Weapon
		set .@price,50000;
		set .@material,984; //Oridecon
		set .@type$,"weapon";
		mes "Wow!... A level 4 weapon~!!";
		break;
	}
	mes "You need ^ff9999"+getitemname(.@material)+"^000000 and ^ff9999"+.@price+"^000000 Zeny. Do you have them?";
	next;
	if(select("Yes, I do!!:Forget about it!!") == 2) {
		mes "[Clink]";
		mes "I knew it!!";
		mes "I knew you were not worth trying my magical refining hammer for.";
		close;
	}
	if (getequippercentrefinery(.@part) < 100) {
		mes "[Clink]";
		mes "Wow!!";
		mes "This "+.@type$+" has been refined quite a bit, huh?";
		mes "You do know that this might break, right?";
		next;
		mes "[Clink]";
		mes "If you break the "+.@type$+", you can never use it again.";
		mes "Cards and enchant effects...";
		mes "the ^ff0000whole thing will disappear^000000.";
		mes "You still up for this~?";
		next;
		if(select("Yes, I am!!:Forget about it!!") == 2) {
			mes "[Clink]";
			mes "I knew it!!";
			mes "You can't even take this big step. Don't think about refining...";
			close;
		}
	}
	if (countitem(.@material) == 0 || Zeny < .@price) {
		mes "[Clink]";
		mes "Hey you!! Didn't I tell you";
		mes "that you need ^ff9999"+getitemname(.@material)+"^000000 and ^ff9999"+.@price+"^000000 Zeny??!!";
		close;
	}
	delitem .@material,1;
	set Zeny, Zeny-.@price;
	if (getequippercentrefinery(.@part) <= rand(100)) {
		failedrefitem .@part;
		mes "[Clink]";
		mes "Cry Hammer!! Cry!!!";
		next;
		switch(rand(1,5)) {
			case 1: emotion e_sob; break;
			case 2: emotion e_swt2; break;
			case 3: emotion e_wah; break;
			case 4: emotion e_hmm; break;
			case 5: emotion e_lv2; break;
		}
		mes "[Clink]";
		mes "Huh?! I failed?!";
		next;
		mes "[Clink]";
		mes "Arrgg~ It's all~ Broken...? What a pity~";
		next;
		mes "[Clink]";
		mes "Hey...!! Get me another one.";
		mes "This is not possible.";
		mes "How can my hammer fail from refining?";
		close;
	}
	successrefitem .@part;
	mes "[Clink]";
	mes "Cry Hammer!! Cry!!!";
	next;
	emotion e_kis;
	mes "[Clink]";
	mes "Ok!! Perfect!!";
	mes "There's nothing I can't refine";
	mes "with this special hammer.";
	mes "You can praise me!!";
	mes "What a day!!";
	close;
}
