//@unloadnpc Costume Enhancement
//@loadnpc npc/valhalla/itemoption.txt

office,134,19,4	script	Costume Enhancement	1_M_SMITH,{
	callsub(StartTalking);
	end;
	
OnInit:
	/**
	 * General Configuration
	 */
	/* Chance of the enhancement process to fail. (0 - 99 in percent) */
	.chance_of_failure = 0;
	/* Delete the item on failure? (true/false) */
	.delete_on_failure = true;
	/* Required amount of zeny for a try. */
	.zeny_requirement = 500000;
	/* Minimum amount of the bonus value. 
	 * For negative effects or certain bonuses that require negative values
	 * Maximum possible value is -INT16_MAX)
	 */
	.minimum_bonus_amount = 1; // usually used with delay bonus options, although not provided in the script.
	/* Maximum amount of the bonus value. 
	 * Maximum possible value is INT16_MAX */
	.maximum_bonus_amount = 3;
	/* Disable selection of bonus value (true/false) */
	.enable_random_bonus = true;
	.enable_random_option = true;
	.debug = false;
	/* Allowed Armors */
	set .armors$,
		"31318,30399,30400,30401,19158,19566,19570,19668,19742,19826,19972,20132,20146,20147,20201,20249,20315,20318,20375,20404,20405,20430,20500,20502,20504,20509,20510,20511,20727,20737,20746,20761,20762,20764,20798,30249,30373,30374,30375,30376,30377,30378,30379,30380,30381,30382,31059,31250,31299,31301,31302,31315,31383,31395,31396,31398,31399,31407,31433,31437,31438,31509,31512,31531,31564,31565,31572";
	setarray .eqslot[1],11,12,13,14;
	/* Item Option Constants */
	setarray(.option_constants[1],
		VAR_STRAMOUNT, 
		VAR_AGIAMOUNT, 
		VAR_VITAMOUNT, 
		VAR_INTAMOUNT, 
		VAR_DEXAMOUNT, 
		VAR_LUKAMOUNT,

		VAR_MAXHPPERCENT, 
		VAR_MAXSPPERCENT, 
		VAR_ATKPERCENT, 
		VAR_MAGICATKPERCENT, 
		VAR_PLUSASPDPERCENT,

		RACE_TOLERACE_NOTHING,
		RACE_TOLERACE_UNDEAD,
		RACE_TOLERACE_ANIMAL,
		RACE_TOLERACE_PLANT,
		RACE_TOLERACE_INSECT,
		RACE_TOLERACE_FISHS,
		RACE_TOLERACE_DEVIL,
		RACE_TOLERACE_HUMAN,
		RACE_TOLERACE_ANGEL,
		RACE_TOLERACE_DRAGON
	);
	end;
	
StartTalking:
	.@name$ = _("[ ^990099Costume Enhancement^000000 ]");
	
	disable_items();
	mes(.@name$);
	mesf("Hey there %s", strcharinfo(PC_NAME));
	mes("I am you costume enhancer!  I can enhance costume generated from Gatcha II:");
	mes"- - -";
	mes"Top Costume:";
	mes"- - -";
	mes"+1 ~ 3 Random on ^0000ffStr^000000, ^00cc00Agi^000000, ^ff9900Vit^000000, ^ff0055Int^000000 or ^ff0000Luk^000000.";
	mes"- - -";
	mes"Mid Costume:";
	mes"- - -";	
 	mes"+1% ~ 3% ^0000ffResistance^000000 to random race.";
	mes"- - -";
	mes"Low Costume:";
	mes"- - -";
	mes"+1% ~ 3% on random on ^0000ffAtk^000000 ^ff0000MAtk^000000 ^000000Aspd^000000 ^ff9900MaxHp^000000 ^ff0055MaxSP^000000";
	mes"- - -";
	mes"Garment Costume:";
	mes"- - -";
	mes"+1% ~ 3% on random on ^0000ffAtk^000000 ^ff0000MAtk^000000 ^000000Aspd^000000 ^ff9900MaxHp^000000 ^ff0055MaxSP^000000";

	next();
	if (.enable_random_bonus) {
		mes(.@name$);
		mes("Costume bonuses are added randomly in a slot!");
		mes("So if you wind up with a low amount of bonus, don't hesitate to override the slot.");
		next();
	}
	mes(.@name$);
	mes("Would you like me to add extra stats to your costume?");
	next();
	if (select("Of course!", "Meh, not right now.") == 2) {
		mes(.@name$);
		mes("Alright then, catch you later.");
		close();
	}

	setarray .@pos$[1],"Lower Costume","Mid Costume","Top Costume","Garment Costume";
	.@menu$ = "";
	for(.@i = 11; .@i <= 14; ++.@i) {
		if(getequipisequiped(.@i)) {
			.@menu$ += .@position$[.@i] + "- " + "[ " + getequipname(.@i) + " ]";
			.@equipped = 1;
		}
		.@menu$ += ":";
	}

	if (.@equipped == 0) {
		mes(.@name$);
		mes "I don't think I can enhance any items you have...";
		close;
	}
	.@part = select(.@menu$);

	if(!getequipisequiped(.eqslot[.@part])) { //custom check
		mes(.@name$);
		mes "You're not wearing";
		mes "anything there that";
		mes "I can enhance.";
		emotion e_an;
		close;
	}
	.@refineitemid = getequipid(.eqslot[.@part]); // save id of the item

	// Check if it allows options
	if (!getequipisenableopt(.eqslot[.@part]) || !compare(.armors$,""+.@refineitemid) ) {
		mes(.@name$);
		mes("Brah, this costume can't have enhancement.");
		close();
	}

	// Present a list of currently infused options.
	do {
		.@menu$ = "";
		.@used = false;
		// Option Slot is the actual option slot 0-MAX_ITEM_OPTIONS (@see mmo.h)
		.@option_slot = 1;
		// Check for used slot and request user action if found.
		if (getequipoption(.eqslot[.@part], .@option_slot, IT_OPT_INDEX) > 0) {
			mes(.@name$);
			mes("This costume is already been enhanced!");
			if (select("^990000Override the enhancement?.^000000", "Choose again.") == 2)
				.@used = true;
			next();
		}
	} while (.@used); // loop if the slot is not to be overridden

	if(.eqslot[.@part] == 13) .@option_variable = rand(1,6);	// Top
	if(.eqslot[.@part] == 11) .@option_variable = rand(7,11);	// Low
	if(.eqslot[.@part] == 12) .@option_variable = rand(12,21);	// Mid
	if(.eqslot[.@part] == 14) .@option_variable = rand(7,11);	// Mid

	.@value = rand(.minimum_bonus_amount,.maximum_bonus_amount);
	next();

	if (.debug) {
		mes "Option Variable: "+(.@option_variable);
		mes "Value: "+.@value;
	}



	// If there's a chance of failure, inform the user.
	if (.chance_of_failure) {
		mes(.@name$);
		mes("Alright so,");
		mes("I'll have you know...");
		mesf("There's a ^990000%d%% chance of failure^000000.", .chance_of_failure);
		mes("Because, well... I didn't go to school.");
		next();
		mes(.@name$);
		if (.delete_on_failure) {
			if(getitemslots(getequipid(.eqslot[.@part])) > 0) {
				mes("If I fail, your ^009900"+getitemname(getequipid(.eqslot[.@part]))+" [1]^000000 will break and it ^990000will be destroyed^000000!");
			} else {
				mes("If I fail, your ^009900"+getitemname(getequipid(.eqslot[.@part]))+"^000000 will break and it ^990000will be destroyed^000000!");
			}
		}
		mes("Are you still ready to go forward with this?");
		next();
		if (select("Fo shizzle.", "Hells naw, go back to school.") == 2) {
			mes(.@name$);
			mes("Geez, you don't have to be so harsh about it.");
			close();
		}
		next();
	}
	
	// If there's a Zeny Requirement, perform the usual.
	if (.zeny_requirement > 0) {
		mes(.@name$);
		mesf("You also have to pay ^990000%d Zeny^000000.", .zeny_requirement);
		next();
		if (select("Of course!", "No way!") == 2) {
			mes(.@name$);
			mes("Well, see you around then...");
			close();
		}
		if (Zeny < .zeny_requirement) {
			mes(.@name$);
			mes("You don't have enough ^990000Zeny^000000!");
			close();
		}
		Zeny -= .zeny_requirement;
	}
	
	// Check if there's a chance of failure, set and roll the dice.
	if (.chance_of_failure && rand(100) <= .chance_of_failure) {
		mes(.@name$);
		mes("^990000I failed!^000000");
		mes("Guess I should go back to school.");
		// Delete the item if flagged.
		if (.delete_on_failure)
			failedrefitem(.eqslot[.@part]); // Simulate failed refinement of the item and delete.
	} else {
		// Set the Item Option bonuses in the slot see db/item_options.conf for more info.
		setequipoption(.eqslot[.@part], .@option_slot, .option_constants[.@option_variable], .@value);
		
		mes(.@name$);
		mes("^009900Praise Jesus^000000");
		mes("I have added an option to your item.");
		mes("My skills are flawless!");
	}
	next();
	mes(.@name$);
	mes("See you around!");
	close();
}
