//===== rAthena Script =======================================
//= HD Refiners
//===== Description: =========================================
//= [Official Conversion]
//= Refiners that use HD ores to refine equipment. Upon
//= failure, the equipment is not destroyed; rather, its
//= refine level decreases by 1. The success rate is identical
//= to that for Enriched ores.
//= - "Blacksmith Mighty Hammer" only refines from +7~9.
//= - "Basta" only refines from +10 and up.
//===== Changelog: ===========================================
//= 1.0 First version. [Euphy]
//= 1.1 Removed re-roll behavior. [Secret]
//============================================================

// Blacksmith Mighty Hammer (+7~9) :: cash_smelting79
//============================================================
-	script	::MightyHammer	-1,{
	disable_items;
	mes "[Blacksmith Mighty Hammer]";
	mes "Unlike others, I am a blacksmith who refines a very limited number of items.";
	mes "I refine only items that are ^CC0000+7 to +9^000000.";
	next;
	mes "[Blacksmith Mighty Hammer]";
	mes "My specialty is that even if my refining fails, the refine level decreases by 1 without losing the gear. Isn't it great?";
	next;
	mes "[Blacksmith Mighty Hammer]";
	mes "So lets kick this into overdrive, what d' ya say? What item do you want to refine?";
	next;
	setarray .@indices[1], EQI_HEAD_TOP, EQI_ARMOR, EQI_HAND_L, EQI_HAND_R, EQI_GARMENT, EQI_SHOES, EQI_ACC_L, EQI_ACC_R, EQI_HEAD_MID, EQI_HEAD_LOW;
	for ( .@i = 1; .@i <= 10; ++.@i )
		.@menu$ = .@menu$ + ( getequipisequiped(.@indices[.@i]) ? getequipname(.@indices[.@i]) : F_getpositionname(.@indices[.@i]) + "-[Not equipped]" ) + ":";
	.@part = .@indices[ select(.@menu$) ];
	if (!getequipisequiped(.@part)) {
		mes "[Blacksmith Mighty Hammer]";
		switch(.@part) {
		case EQI_HEAD_TOP:
			mes "I'm a blacksmith, not a hairstylist.";
			break;
		case EQI_ARMOR:
			mes "With my hammer, I will make you a star of the sky.";
			break;
		case EQI_HAND_L:
		case EQI_HAND_R:
			mes "Making artificial hands is not my specialty.";
			break;
		case EQI_GARMENT:
			mes "Bring out the item so I can refine it!";
			break;
		case EQI_SHOES:
			mes "Where is this foot odor coming from?";
			break;
		case EQI_ACC_L:
		case EQI_ACC_R:
			mes "Where is the accessory?";
			break;
		case EQI_HEAD_MID:
			mes "What do you want me to refine?";
			break;
		case EQI_HEAD_LOW:
			mes "Huh? What do you want me to do?";
			break;
		}
		close;
	}
	if (!getequipisenableref(.@part)) {
		mes "[Blacksmith Mighty Hammer]";
		mes "This item can't be refined.";
		close;
	}
	switch( getequiprefinerycnt(.@part) ) {
	case 7:
		.@blacksmith_blessing_count = 1;
		break;
	case 8:
		.@blacksmith_blessing_count = 2;
		break;
	case 9:
		.@blacksmith_blessing_count = 4;
		break;
	default:
		mes "[Blacksmith Mighty Hammer]";
		mes "I only handle items with refine levels from +7 to +9.";
		close;
	}

	.@refineitemid = getequipid(.@part); // save id of the item
	.@itemtype = getiteminfo( .@refineitemid, ITEMINFO_TYPE );
	.@refinerycnt = getequiprefinerycnt(.@part); //save refinery count
	setarray .@card[0], getequipcardid(.@part,0), getequipcardid(.@part,1), getequipcardid(.@part,2), getequipcardid(.@part,3);
	.@price = getequiprefinecost(.@part, REFINE_COST_HD, REFINE_ZENY_COST);
	.@material = getequiprefinecost(.@part, REFINE_COST_HD, REFINE_MATERIAL_ID);

	mes "[Blacksmith Mighty Hammer]";
	mes "In order to refine the gear you selected you need ^ff9999" + getitemname(.@material) + "^000000 and 20,000 zeny as a fee.";
	mes "Do you have them ready?";
	next;
	if (select("Yes:No") == 2) {
		mes "[Blacksmith Mighty Hammer]";
		mes "I will wait until you are ready.";
		close;
	}
	if (getequippercentrefinery(.@part) < 100) {
		mes "[Blacksmith Mighty Hammer]";
		mes "It looks like this item will likely fail to be refined.";
		mes "Well, even if it fails, it only decreases by 1 refine level.";
		mes "Would you like to continue refining?";
		next;
		if (countitem(6635) < .@blacksmith_blessing_count)
			setarray .@menu$[0], "", "Yes", "Not yet";
		else {
			mes "[Blacksmith Mighty Hammer]";
			mes "Ah! Is it the ^0000ffBlacksmith Blessing^000000?";
			mes "With the Blacksmith Blessing, the equipment won't vanish if the refine fails!";
			next;
			mes "[Blacksmith Mighty Hammer]";
			if ( .@itemtype != IT_WEAPON )
				mes "For +" + getequiprefinerycnt(.@part) + " equipment, refine with^316AC5 " + .@blacksmith_blessing_count + " unit(s) Blacksmith Blessing^000000 can prevent the equipment from vanishing. Do you want use it to refine?";
			else
				mes "For +" + getequiprefinerycnt(.@part) + " weapon, refine with^316AC5 " + .@blacksmith_blessing_count + " unit(s) Blacksmith Blessing^000000 can prevent the equipment from vanishing. Do you want use it to refine?";
			next;
			setarray .@menu$[0], "Use it to refine", "Refine directly without it", "Don't refine yet";
		}
		switch( select(.@menu$[0], .@menu$[1], .@menu$[2]) ) {
		case 1:
			.@bless_who = 1;
			break;
		case 2:
			break;
		case 3:
			mes "[Blacksmith Mighty Hammer]";
			mes "Only those who overcome fear of failure will obtain a masterpiece.";
			close;
		}
	}
	if ((.@bless_who && countitem(6635) < .@blacksmith_blessing_count) || countitem(.@material) == 0 || Zeny < .@price) {
		mes "[Blacksmith Mighty Hammer]";
		mes "Didn't you just say you had everything ready?";
		close;
	}
	if (.@bless_who)
		delitem 6635, .@blacksmith_blessing_count;
	delitem .@material,1;
	Zeny = Zeny - .@price;

	// anti-hack
	if (callfunc("F_IsEquipIDHack", .@part, .@refineitemid) || callfunc("F_IsEquipCardHack", .@part, .@card[0], .@card[1], .@card[2], .@card[3]) ||
		callfunc("F_IsEquipRefineHack", .@part, .@refinerycnt)) {
		mes "[Blacksmith Mighty Hammer]";
		emotion ET_FRET;
		mes "Wait a second...";
		mes "Do you think I'm stupid?!";
		mes "You switched the item while I wasn't looking! Get out of here!";
		close;
	}

	mes "[Blacksmith Mighty Hammer]";
	mes "Tac! Tac! Tac!";
	if (getequippercentrefinery(.@part, true) > rand(100)) {
		successrefitem .@part;
		next;
		emotion ET_BEST;
		mes "[Blacksmith Mighty Hammer]";
		mes "The sound refreshes my mind every time I hear it.";
		mes "Here, have it. Refine succeeded flawlessly!";
		close;
	}
	if (.@bless_who == 1) {
		specialeffect EF_HOLYHIT;
		next;
		emotion ET_HUK;
		mes "[Blacksmith Mighty Hammer]";
		mes "What?!!";
		next;
		mes "[Blacksmith Mighty Hammer]";
		mes "Aiya! I am faceless now. HmHm.";
		close;
	}
	downrefitem .@part;
	next;
	emotion ET_HUK;
	mes "[Blacksmith Mighty Hammer]";
	mes "Oops!!";
	next;
	mes "[Blacksmith Mighty Hammer]";
	mes "I am sure a person like you would never blame me for a decrease in refine level by 1. Hmm.";
	close;
}
prt_in,59,54,3	duplicate(MightyHammer)	Mighty Hammer#prt	4_M_DWARF
morocc_in,65,30,3	duplicate(MightyHammer)	Mighty Hammer#morocc	4_M_DWARF
payon,148,176,3	duplicate(MightyHammer)	Mighty Hammer#pay	4_M_DWARF
alberta_in,16,56,3	duplicate(MightyHammer)	Mighty Hammer#alb	4_M_DWARF
yuno_in01,171,18,3	duplicate(MightyHammer)	Mighty Hammer#yuno	4_M_DWARF
ein_in01,22,82,3	duplicate(MightyHammer)	Mighty Hammer#ein	4_M_DWARF
lhz_in02,280,19,3	duplicate(MightyHammer)	Mighty Hammer#lhz	4_M_DWARF

// iRO NPC locations:
// payon,174,133,4	duplicate(MightyHammer)	Mighty Hammer#im	4_M_DWARF

// Basta (+10 and up) :: cash_smelting
//============================================================
-	script	::Basta	-1,{
	disable_items;
	mes "[Basta]";
	mes "I'm the best Blacksmith in the whole world, Basta.";
	mes "But I don't provide a normal refine service.";
	mes "I only refine equipment ^CC0000over +10^000000.";
	next;
	mes "[Basta]";
	mes "Which equipment do you want to refine?";
	next;
	setarray .@indices[1], EQI_HEAD_TOP, EQI_ARMOR, EQI_HAND_L, EQI_HAND_R, EQI_GARMENT, EQI_SHOES, EQI_ACC_L, EQI_ACC_R, EQI_HEAD_MID, EQI_HEAD_LOW;
	for ( .@i = 1; .@i <= 10; ++.@i )
		.@menu$ = .@menu$ + ( getequipisequiped(.@indices[.@i]) ? getequipname(.@indices[.@i]) : F_getpositionname(.@indices[.@i]) + "-[Unequipped]" ) + ":";
	.@part = .@indices[ select(.@menu$) ];
	if (!getequipisequiped(.@part)) {
		mes "[Basta]";
		switch(.@part) {
		case EQI_HEAD_TOP:
			mes "Is your head an equipment?";
			break;
		case EQI_ARMOR:
			mes "What do you want me to do?";
			break;
		case EQI_HAND_L:
		case EQI_HAND_R:
			mes "Making artificial hands is not my specialty.";
			break;
		case EQI_GARMENT:
			mes "Do you even know what a robe is?";
			break;
		case EQI_SHOES:
			mes "If you want to refine your feet, don't come to me, try running a marathon.";
			break;
		case EQI_ACC_L:
		case EQI_ACC_R:
			mes "Where is the accessory?";
			break;
		case EQI_HEAD_MID:
			mes "Well... I don't see any equipment worth refining.";
			break;
		case EQI_HEAD_LOW:
			mes "I can't make you smart. Go see a school teacher for that.";
			break;
		}
		close;
	}
	if (!getequipisenableref(.@part)) {
		mes "[Basta]";
		mes "Even I cannot refine this item. There's no way.";
		close;
	}
	.@refine_count = getequiprefinerycnt(.@part);
	if (.@refine_count < 10) {
		mes "[Basta]";
		mes "Haven't I told you? I only refine equipment that are +10 and above.";
		close;
	}
	else if (.@refine_count == 10)
		.@blacksmith_blessing_count = 7;
	else if (.@refine_count == 11)
		.@blacksmith_blessing_count = 11;
	else if (.@refine_count == 20) {
		mes "[Basta]";
		mes "This weapon is perfect, no need to refine it anymore~";
		close;
	}
	.@refineitemid = getequipid(.@part); // save id of the item
	.@itemtype = getiteminfo( .@refineitemid, ITEMINFO_TYPE );
	.@refinerycnt = getequiprefinerycnt(.@part); //save refinery count
	setarray .@card[0], getequipcardid(.@part,0), getequipcardid(.@part,1), getequipcardid(.@part,2), getequipcardid(.@part,3);
	.@price = getequiprefinecost(.@part, REFINE_COST_HD, REFINE_ZENY_COST);
	.@material = getequiprefinecost(.@part, REFINE_COST_HD, REFINE_MATERIAL_ID);

	if ( .@itemtype != IT_WEAPON )
		.@type$ = "armor";
	else
		.@type$ = "weapon";
	mes "[Basta]";
	mes "Hmm... is this the one you want to refine?";
	mes "To refine this equipment, I need 1 ^ff9999" + getitemname(.@material) + "^000000 and " + callfunc("F_InsertComma",.@price) + " zeny as a fee.";
	mes "Do you really want to refine this?";
	next;
	if (select("Yes:No") == 2) {
		mes "[Basta]";
		mes "Okay. If that's what you want...";
		close;
	}
	if (getequippercentrefinery(.@part, true) < 100) {
		mes "[Basta]";
		mes "This " + .@type$ + " has already been refined pretty high.";
		mes "If you try to refine it more, the refine level could decrease.";
		next;
		mes "[Basta]";
		mes "I am different from the blacksmiths in others places.";
		mes "It is impossible that the refine level will drop by, say, 3 or 4... that sounds scary.";
		mes "Here it can only decrease by 1 level.";
		next;
		if (.@blacksmith_blessing_count == 0 || countitem(6635) < .@blacksmith_blessing_count) {
			mes "[Basta]";
			mes "Compared to other blacksmiths, the risk is smaller.";
			mes "I've given all precautions. Do you want to try it?";
			next;
			setarray .@menu$[0], "", "Yes", "No";
		}
		else {
			mes "[Basta]";
			mes "Woah~ Is it the ^316AC5Blacksmith Blessing^000000? It is difficult to get~ But, you have it!";
			next;
			mes "[Basta]";
			mes "For +" + .@refine_count + " " + .@type$ + " need ^316AC5" + .@blacksmith_blessing_count + " unit(s) Blacksmith Blessing^000000 can prevent the equipment from vanishing. Do you want to refine with Blacksmith Blessing?";
			next;
			setarray .@menu$[0], "Refine with Blacksmith Blessing", "Refine without Blacksmith Blessing", "Don't refine yet";
		}
		switch( select(.@menu$[0], .@menu$[1], .@menu$[2]) ) {
		case 1:
			.@bless_who = 1;
			break;
		case 2:
			break;
		case 3:
			mes "[Basta]";
			mes "Well~";
			mes "Not challenging at all could also be a kind of wisdom in life.";
			close;
		}
	}
	if ((.@bless_who && countitem(6635) < .@blacksmith_blessing_count) || countitem(.@material) == 0 || Zeny < .@price) {
		mes "[Basta]";
		mes "Hmm... You didn't bring all the materials needed.";
		mes "Come back when you have them all.";
		close;
	}
	if (.@bless_who)
		delitem 6635, .@blacksmith_blessing_count;
	delitem .@material,1;
	Zeny = Zeny - .@price;

	// anti-hack
	if (callfunc("F_IsEquipIDHack", .@part, .@refineitemid) || callfunc("F_IsEquipCardHack", .@part, .@card[0], .@card[1], .@card[2], .@card[3]) ||
		callfunc("F_IsEquipRefineHack", .@part, .@refinerycnt)) {
		mes "[Basta]";
		emotion ET_FRET;
		mes "Wait a second...";
		mes "Do you think I'm stupid?!";
		mes "You switched the item while I wasn't looking! Get out of here!";
		close;
	}

	mes "Pow! Pow! Pow! Pow!";
	if (getequippercentrefinery(.@part, true) > rand(100)) {
		successrefitem .@part;
		next;
		emotion ET_BEST;
		mes "[Basta]";
		mes "Great! Nicely done!!";
		mes "I really am the best blacksmith in the whole wide world!";
		close;
	}
	if (.@bless_who == 1) {
		specialeffect EF_HOLYHIT;
		next;
		emotion (!rand(5))?ET_MONEY:ET_HUK;
		mes "[Basta]";
		mes "Aaaaaaaaaaak!!!";
		next;
		mes "[Basta]";
		mes "Refining has failed!";
		mes "The best blacksmith in the world like me...";
		mes "doesn't guarantee 100% success~";
	}
	else {
		downrefitem .@part;
		next;
		emotion (!rand(5))?ET_MONEY:ET_HUK;
		mes "[Basta]";
		mes "Aaaaaaaaaaak!!!";
		next;
		mes "[Basta]";
		mes "Damn it!";
		mes "Refining has failed and refine level has decreased!";
		mes "Even the best blacksmith in the world doesn't guarantee 100% success!";
	}
	mes "Too bad.";
	next;
	mes "[Basta]";
	mes "I'll do better next time! Don't worry!";
	close;
}

prt_in,57,54,3	duplicate(Basta)	Basta#prt	4_M_DWARF
morocc_in,68,30,3	duplicate(Basta)	Basta#morocc	4_M_DWARF
payon,148,174,3	duplicate(Basta)	Basta#payon	4_M_DWARF
alberta_in,18,56,3	duplicate(Basta)	Basta#alberta	4_M_DWARF
yuno_in01,173,18,3	duplicate(Basta)	Basta#yuno	4_M_DWARF
ein_in01,24,82,3	duplicate(Basta)	Basta#einbroch	4_M_DWARF
lhz_in02,280,17,3	duplicate(Basta)	Basta#lighthalzen	4_M_DWARF

// Refine UI makes these NPCs useless
-	script	RefineUI_Init	-1,{
	end;
OnInit:
	if (getbattleflag("feature.refineui")) {
		unloadnpc "Basta";
		unloadnpc "MightyHammer";
	}
	end;
}
