//===== rAthena Script =======================================
//= Infinite Space
//===== Description: =========================================
//= [Walkthrough Conversion]
//= Infinite Space related merchants and enchanter
//===== Changelogs: ==========================================
//= 1.0 Initial release [crazyarashi]
//= 1.1 Removed unecessary use of functions [Everade]
//= 1.2 Added warp scroll merchant [Everade]
//============================================================

// Food Merchant
-	shop	inf_ration	-1,512:-1,513:-1,515:-1,516:-1

cmd_fild07,63,268,1	script	Emergency Food Merchant#pa0829_01	4_M_BIBI,{
	mes "[Emergency Food Merchant]";
	mes "I have a lot of stuff over here, of course, not for free.";
	close2;
	callshop "inf_ration",1;
	end;
}

// Warp Scroll Seller
cmd_fild07,375,167,1	script	Ruins Black Trader#pa0829_01	4_F_JOB_HUNTER,{
	mes "[Ruins Black Trader]";
	mes "Hey, isn't it hard to walk in and out of here every time? For just ^0000ff20,000^000000 Zeny, you can have a scroll that will take you directly to the entrance of the ruins, what do you think?";
Purchase:
	next;
	switch( select( "Give me one", "I don't think so" )) {
		case 1:
			if (!checkweight(22980,1) || (MaxWeight - Weight) < 1000) {
				mes "You can't proceed with the conversation because you have a large quantity of items.";
				mes "Please organize your items and try again.";
				close;
			}
			else if (Zeny < 20000) {
				mes "[Ruins Black Trader]";
				mes "You don't seem to have any more money. A scroll costs 20,000 Zeny.";
				close;
			}
			Zeny -= 20000;
			getitem 22980,1;
			mes "[Ruins Black Trader]";
			mes "It was a good deal. Need more?";
			goto Purchase;
		case 2:
			mes "[Ruins Black Trader]";
			mes "Come back anytime";
			close;
	}
}

// Equipment Shop
cmd_fild07,57,275,5	script	Artifact Appraiser#pa0829_01	1_F_02,{
	if (!checkweight(1201,1) || (MaxWeight - Weight) < 1000) {
		mes "You can't proceed with the conversation because you have a large quantity of items.";
		mes "Please organize your items and try again.";
		close;
	}
	.@stone_id = 6905;
	mes "[ Artifact Appraiser ]";
	mes "Choose the type of equipment that you want to purchase. You can purchase as many as you want, as long as you have " + getitemname(.@stone_id) + ".";
	next;
	switch (select("Cancel:Weapons:Armors")) {
		case 1:
			mes "[ Artifact Appraiser ]";
			mes "Please come back whenever you want~";
			close;

		case 2:
			setarray .@equip_id,1994,1938,13323,13126,28703,2024,16038,21014,28105,18128;
			.@price = 50;
			break;

		case 3:
			setarray .@equip_id,15141,22075,20779,19033;
			.@price = 50;
			break;
	}
	.@menu$ = "Cancel:";
	for (.@i = 0; .@i < getarraysize(.@equip_id); .@i++)
		.@menu$ += getitemname(.@equip_id[.@i]) + ":";
	.@s = select(.@menu$) - 1;
	switch (.@s) {
		case 0:
			mes "[ Artifact Appraiser ]";
			mes "Please come back whenever you want~";
			close;

		default:
			.@s--;
			mes "[ Artifact Appraiser ]";
			mes "You need ^0000FF" + .@price + "^000000 " + getitemname(.@stone_id) + " to purchase the " + getitemname(.@equip_id[.@s]) + "~";
			next;
			if (select("Cancel:Purchase") == 1) {
				mes "[ Artifact Appraiser ]";
				mes "Please come back whenever you want~";
				close;
			}
			if (countitem(.@stone_id) < .@price) {
				mes "[ Artifact Appraiser ]";
				mes "You don't have enough " + getitemname(.@stone_id) + " to purchase this item.";
				close;
			}
			mes "[ Artifact Appraiser ]";
			mes "Thank you for your trust. Please come back again next time.";
			delitem .@stone_id,.@price;
			getitem .@equip_id[.@s],1;
			close;
	}
}

// Equipment Enchanter
cmd_fild07,60,275,3	script	Artifact Enhancer#pa0829_01	4_F_JOB_BLACKSMITH,{
	if (!checkweight(1201,1) || (MaxWeight - Weight) < 1000) {
		mes "You can't proceed with the conversation because you have a large quantity of items.";
		mes "Please organize your items and try again.";
		close;
	}
	disable_items;
	.@stone_id = 6905;
	function equip_check;
	mes "[ Artifact Enhancer ]";
	mes "If you're looking to strengthen the equipment that you've obtained in the Infinite Space, you have come to the right place.";
	mes "Do you have any " + getitemname(.@stone_id) + "?";
	next;
	switch (select("How do I enchant my equipment?:Enchant equipment.:Initialize equipment's enchant.")) {
		case 1:
			mes "[ Artifact Enhancer ]";
			mes "You'll be able to obtain " + getitemname(.@stone_id) + ", if you explore the space under the Paros Lighthouse.";
			next;
			mes "[ Artifact Enhancer ]";
			mes "For that material, I can reinforce the equipment that you got from the Infinite Space.";
			next;
			mes "[ Artifact Enhancer ]";
			mes "I can only enchant the 3rd and the 4th slot.";
			next;
			mes "[ Artifact Enhancer ]";
			mes "There is no chance of the equipment being destroyed during the enchanting process, but there's a high probability that it will be destroyed when initializing the enchants.";
			next;
			mes "[ Artifact Enhancer ]";
			mes "Then come visit me when you want to enchant the equipment.";
			break;

		case 2:
			.@stone_id = 6905;
			.@fee = 20;
			mes "[ Artifact Enhancer ]";
			mes "Please select the equipment that you want to enchant.";
			next;
			switch (select("Cancel:Weapon:Armor:Shoes:Garment:Helm")) {
				case 1:
					mes "[ Artifact Enhancer ]";
					mes "Then come visit me when you want to enchant the equipment.";
					close;

				case 2:
					.@part = EQI_HAND_R;
					break;

				case 3:
					.@part = EQI_ARMOR;
					break;

				case 4:
					.@part = EQI_SHOES;
					break;

				case 5:
					.@part = EQI_GARMENT;
					break;

				case 6:
					.@part = EQI_HEAD_TOP;
					break;
			}
			.@equip_id = getequipid(.@part);
			.@refine = getequiprefinerycnt(.@part);
			equip_check(.@equip_id);
			for (.@i = 0;.@i < 4;.@i++) {
				.@card[.@i] = getequipcardid(.@part,.@i);
				.@check[.@i] = getequipcardid(.@part,.@i);
			}
			if (.@card[2] > 0) {
				mes "[ Artifact Enhancer ]";
				mes "I can only enchant up to the third slot. Your equipment can't be further enchanted.";
				close;
			}
			switch (.@part) {
				case EQI_HAND_R:
					setarray .@enchant$,
					"4700,4701:4710,4711:4720,4721",
					"4811,4810,4809,4808,4820,4821,4822,4823:4815,4814,4813,4812,4826,4827,4828,4829:4832,4833,4834,4835,4836,4837,4838,4839";
					break;

				case EQI_ARMOR:
				case EQI_SHOES:
					setarray .@enchant$,
					"4700,4701,4702,4703:4710,4711,4712,4713:4720,4721,4722,4723",
					"4795,4796,4797:4870,4871,4800:4870,4871,4800";
					break;

				case EQI_GARMENT:
				case EQI_HEAD_TOP:
					setarray .@enchant$,
					"4700,4701,4702,4703:4710,4711,4712,4713:4720,4721,4722,4723",
					"4861,4862,4867,4868,4900:4861,4862,4867,4868,4900:4861,4862,4867,4868,4900";
					break;
			}
			.@slot = (.@card[3] > 0 ? 2 : 3);
			.@index = (.@slot == 3 ? 0 : 1);
			mes "[ Artifact Enhancer ]";
			mes "You can choose 3 types of enchant, and the enchant fee is " + .@fee + " " + getitemname(.@stone_id) + ".";
			mes "I'll make sure it gets enchanted without a problem.";
			next;
			.@type = select("Quit:Physical:Magical:Range") - 2;
			mes "[ Artifact Enhancer ]";
			if (.@slot == 3)
				mes "Okay, let's begin the first enchantment.";
			else
				mes "Okay, let's proceed with the second enchantment.";
			next;
			if (select("I'll return later.:Please proceed.") == 1) {
				mes "[ Artifact Enhancer ]";
				mes "If you change your mind, just return to me.";
				close;
			}
			if (countitem(.@stone_id) < .@fee) {
				mes "[ Artifact Enhancer ]";
				mes "Hm. By the way, I don't think you were listening, you don't have enough " + getitemname(.@stone_id) + ".";
				close;
			}
			explode(.@T$,.@enchant$[.@index],":");
			explode(.@TT$,.@T$[.@type],",");
			.@enchant = atoi(.@TT$[rand(getarraysize(.@TT$))]);
			.@card[.@slot] = .@enchant;
			delitem .@stone_id,.@fee;
			if (callfunc("F_IsEquipIDHack", .@part, .@equip_id) || callfunc("F_IsEquipRefineHack", .@part, .@refine) || callfunc("F_IsEquipCardHack", .@part, .@check[0], .@check[1], .@check[2], .@check[3]))
				close;
			delequip .@part;
			getitem2 .@equip_id,1,1,.@refine,0,.@card[0],.@card[1],.@card[2],.@card[3];
			specialeffect2 EF_REPAIRWEAPON;
			mes "[ Artifact Enhancer ]";
			mes "Hm. It was well done. Check your equipment at once.";
			break;
		
		case 3:
			.@stone_id = 6905;
			.@fee = 30;
			.@break_chance = 30;
			mes "[ Artifact Enhancer ]";
			mes "Please select the equipment that you want to enchant.";
			next;
			switch (select("Cancel:Weapon:Armor:Shoes:Garment:Helm")) {
				case 1:
					mes "[ Artifact Enhancer ]";
					mes "Then come visit me when you want to enchant the equipment.";
					close;

				case 2:
					.@part = EQI_HAND_R;
					break;

				case 3:
					.@part = EQI_ARMOR;
					break;

				case 4:
					.@part = EQI_SHOES;
					break;

				case 5:
					.@part = EQI_GARMENT;
					break;

				case 6:
					.@part = EQI_HEAD_TOP;
					break;
			}
			.@equip_id = getequipid(.@part);
			.@refine = getequiprefinerycnt(.@part);
			equip_check(.@equip_id);
			for (.@i = 0; .@i < 4; .@i++) {
				.@card[.@i] = getequipcardid(.@part,.@i);
				.@check[.@i] = .@card[.@i];
			}
			if (.@card[3] == 0) {
				mes "[ Artifact Enhancer ]";
				mes "Your equipment doesn't have any enchant on it.";
				close;
			}
			mes "[ Artifact Enhancer ]";
			mes "There is a chance that your infinite space equipment will be destroyed on the initializion process. Are you still willing to proceed?";
			next;
			if (select("Quit:Proceed") == 1) {
				mes "[ Artifact Enhancer ]";
				mes "If you change your mind, just return to me.";
				close;
			}
			if (countitem(.@stone_id) < .@fee) {
				mes "[ Artifact Enhancer ]";
				mes "Hm. By the way, I don't think you were listening, you don't have enough " + getitemname(.@stone_id) + ".";
				close;
			}
			delitem .@stone_id,.@fee;
			if (callfunc("F_IsEquipIDHack", .@part, .@equip_id) || callfunc("F_IsEquipRefineHack", .@part, .@refine) || callfunc("F_IsEquipCardHack", .@part, .@check[0], .@check[1], .@check[2], .@check[3]))
				close;
			delequip .@part;
			if (rand(1,100) > .@chance) {
				getitem2 .@equip_id,1,1,.@refine,0,.@card[0],.@card[1],0,0;
				specialeffect2 EF_REPAIRWEAPON;
				mes "[ Artifact Enhancer ]";
				mes "Hm. It was well done. Check your equipment at once.";
			} else {
				specialeffect2 EF_REFINEFAIL;
				mes "[ Artifact Enhancer ]";
				mes "Well, I did warn you. You aren't lucky, eh?";
			}
			break;
	}
	close;

	function	equip_check	{
		setarray .@equip_id,1994,1938,13323,13126,28703,2024,16038,21014,28105,18128,15141,22075,20779,19033;
		if (!getarg(0)) {
			mes "[ Artifact Enhancer ]";
			mes "Did you take off your equipment?";
			close;
		}
		if (inarray(.@equip_id,getarg(0)) == -1) {
			mes "[ Artifact Enhancer ]";
			mes "This equipment is not suitable for enchanting. Don't forget that only the infinite equipment series can be enchanted.";
			close;
		}
		return;
	}
}
