//@unloadnpc dyn_shop1
//@unloadnpc Tailoring Loom
//@loadnpc npc/thor/crafting/tailoring/tailoring_loom.txt


-	shop	dyn_shop1	FAKE_NPC,501:50
office,118,25,0	script	Tailoring Loom	4_LOOM,{
	callshop "dyn_shop1",2;
	npcshopattach "dyn_shop1";
	end;

OnSellItem:
	disable_items();
	if (getarraysize(@sold_nameid) == 1 || getarraysize(@sold_nameid) == 3 || getarraysize(@sold_nameid) == 5 || getarraysize(@sold_nameid) > 6 ) {
		dispbottom "I can not process that item combination. ";
	}

	if (getarraysize(@sold_nameid) == 2) {
		switch( handicrafting_skill ) {
		case 0: case 1: case 2: case 3: case 4:
			.@2items = 30135;
			setarray .@2need[ 0 ], 930, 1, 504, 1;
			break;
		case 5: case 6: case 7: case 8: case 9:
			.@2items = 30136;
			setarray .@2need[ 0 ], 7071, 1, 504, 1; 
			break;
		case 10: case 11: case 12: case 13: case 14: 
			.@2items = 30137;
			setarray .@2need[ 0 ], 1099, 1, 504, 1; 
			break;
		case 15: case 16: case 17: case 18: case 19:
			.@2items = 30138;
			setarray .@2need[ 0 ], 7069, 1, 504, 1; 
			break;
		case 20: case 21: case 22: case 23: case 24: 
			.@2items = 30139;
			setarray .@2need[ 0 ], 7153, 1, 504, 1; 
			break;
		case 25: case 26: case 27: case 28: case 29:
			.@2items = 30140;
			setarray .@2need[ 0 ], 7065, 1, 504, 1; 
			break;
		case 30: case 31: case 32: case 33: case 34:
			.@2items = 30141;
			setarray .@2need[ 0 ], 1020, 1, 504, 1; 
			break;
		case 35: case 36: case 37: case 38: case 39: case 40:
			.@2items = 30142;
			setarray .@2need[ 0 ], 1029, 1, 504, 1; 
			break;
		default: 
			break;
		}
		set .@count,0;
		for (.@i = 0; .@i < getarraysize(@sold_nameid); ++.@i) {
			for ( .@ix = 0; .@ix < getarraysize( .@2need ); .@ix += 2 )
				if (.@2need[ .@ix ] == @sold_nameid[ .@i ] && .@2need[ .@ix + 1 ] == @sold_quantity[ .@i ]) {
					.@count ++;
				}
		}
		if (.@count == 2) {
			progressbar "ffffff",2; 	
			.@s = rand( 1, 2 );
			getitem .@2items,.@s;
			specialeffect(305, AREA, playerattached());	
			for (.@i = 0; .@i < getarraysize(@sold_nameid); ++.@i) {	
				delitem @sold_nameid[ .@i ], @sold_quantity[.@i];
			}
		}
		end;
	}

	if (getarraysize(@sold_nameid) == 4) {

		setarray .@book1[ 0 ], 30100, 1, 30130, 1, 7126, 1, 905, 2, 30120, 1;
		setarray .@book2[ 0 ], 30101, 1, 30130, 1, 7217, 1, 30100, 2, 30121, 1;
		setarray .@book3[ 0 ], 30102, 1, 30130, 1, 921, 1, 30101, 2, 30129, 1;
		setarray .@book4[ 0 ], 30113, 1, 30130, 1, 7220, 1, 919, 3, 30122, 1;
		setarray .@book5[ 0 ], 30112, 1, 30130, 1, 7139, 1, 30113, 2, 30123, 1;

		setarray .@book6[ 0 ], 30103, 1, 30131, 1, 7126, 1, 914, 2, 30120, 1;
		setarray .@book7[ 0 ], 30104, 1, 30131, 1, 7217, 1, 30103, 2, 30121, 1;
		setarray .@book8[ 0 ], 30105, 1, 30131, 1, 921, 1, 30104, 2, 30129, 1;
		setarray .@book9[ 0 ], 30115, 1, 30131, 1, 7220, 1, 7003, 3, 30122, 1;
		setarray .@book10[ 0 ], 30114,1, 30131, 1, 7139, 1, 30115, 2, 30123, 1;

		setarray .@book11[ 0 ], 30106, 1, 30132, 1, 7126, 1, 914, 2, 30120, 1;
		setarray .@book12[ 0 ], 30107, 1, 30132, 1, 7217, 1, 30106, 2, 30121, 1;
		setarray .@book13[ 0 ], 30108, 1, 30132, 1, 921, 1, 30107, 2, 30129, 1;
		setarray .@book14[ 0 ], 30117, 1, 30132, 1, 7220, 1, 7123, 3, 30122, 1;
		setarray .@book15[ 0 ], 30116, 1, 30132, 1, 7139, 1, 30117, 2, 30123, 1;

		setarray .@book16[ 0 ], 30109, 1, 30133, 1, 7126, 1, 914, 2, 30120, 1;
		setarray .@book17[ 0 ], 30110, 1, 30133, 1, 7217, 1, 30109, 2, 30121, 1;
		setarray .@book18[ 0 ], 30111, 1, 30133, 1, 921, 1, 30110, 2, 30129, 1;
		setarray .@book19[ 0 ], 30119, 1, 30133, 1, 7220, 1, 7107, 3, 30122, 1;
		setarray .@book20[ 0 ], 30118, 1, 30133, 1, 7139, 1, 30119, 2, 30123, 1;

		for (.@j = 1; .@j < 21; ++.@j) {
			set .@count,0;
			for (.@i = 0; .@i < getarraysize(@sold_nameid); ++.@i) {
				for ( .@ix = 2; .@ix < getarraysize( getd(".@book"+.@j) ); .@ix += 2 )
					if (getd(".@book"+.@j+"["+.@ix+"]") == @sold_nameid[ .@i ] && getd(".@book"+.@j+"["+(.@ix+1)+"]") == @sold_quantity[ .@i ]) {
						.@count ++;
					}
			}
			if (.@count == 4) {
				if (.@j == 5 || .@j == 10 || .@j == 15 ||  .@j == 20) {
					.@s = rand( 5, 10 );
				} else {
					.@s = rand( 1, 2 );
				}	
				progressbar "ffffff",2; 

				.@total_chance = 10 + ( handicrafting_skill / 2 ) + ( countitem( .artisan_charm_id ) ? 10 : 0 );
				dispbottom "You have a total chance of " + .@total_chance + "% on crafting this item.";	
				if ( rand( 100 ) < .@total_chance ) {
					specialeffect(305, AREA, playerattached());
					getitem getd(".@book"+.@j+"[ 0 ]"),.@s;
					tailoring_exp += .@s;
					/* Earn handicrafting skill if experience is still greather than 100 */
					while ( 1 ) {
						if ( tailoring_exp >= 100 ) {
							tailoring_exp -= 100; // deduct 100 experience points since we will add one handicrafting skill level
							tailoring_skill++; // every 100 tailoring experience is equal to 1 tailoring skill level
							dispbottom "Your tailoring skill is now "+tailoring_skill;
						} else {
							dispbottom "You gained "+.@s+" tailoring experience. Your tailoring skill is now "+tailoring_skill;
							break;
						}
					}
				} else {
					specialeffect(306, AREA, playerattached());
					dispbottom "Too bad " + strcharinfo( 0 ) + ", You failed to craft this item. Better luck next time";	
				}

				for (.@i = 0; .@i < getarraysize(@sold_nameid); ++.@i) {	
					delitem @sold_nameid[ .@i ], @sold_quantity[.@i];
				}
				end;
			}
		}
	}		

	if (getarraysize(@sold_nameid) == 6) {

		setarray .@produce1[ 0 ], 30151, 1, 30101, 1, 30112, 10, 30113, 4, 30124, 1, 30125, 1, 30128, 1;
		setarray .@produce2[ 0 ], 30143, 1, 30101, 1, 30112, 5, 30113, 7, 30124, 1, 30125, 1, 30128, 1;
		setarray .@produce3[ 0 ], 30147, 1, 30101, 1, 30112, 5, 30113, 5, 30124, 1, 30125, 1, 30128, 1;
		setarray .@produce4[ 0 ], 30155, 1, 30101, 1, 30112, 15, 30113, 6, 30124, 1, 30125, 1, 30128, 1;

		setarray .@produce5[ 0 ], 30152, 1, 30104, 1, 30114, 10, 30105, 4, 30124, 1, 30125, 1, 30126, 1;
		setarray .@produce6[ 0 ], 30144, 1, 30104, 1, 30114, 5, 30105, 7, 30124, 1, 30125, 1, 30127, 1;
		setarray .@produce7[ 0 ], 30148, 1, 30104, 1, 30114, 5, 30105, 5, 30124, 1, 30125, 1, 30127, 1;
		setarray .@produce8[ 0 ], 30156, 1, 30104, 1, 30114, 15, 30105, 6, 30124, 1, 30125, 1, 30126, 1;


		for (.@j = 1; .@j < 9; ++.@j) {
			set .@count,0;
			for (.@i = 0; .@i < getarraysize(@sold_nameid); ++.@i) {
				for ( .@ix = 2; .@ix < getarraysize( getd(".@produce"+.@j) ); .@ix += 2 )
					if (getd(".@produce"+.@j+"["+.@ix+"]") == @sold_nameid[ .@i ] && getd(".@produce"+.@j+"["+(.@ix+1)+"]") == @sold_quantity[ .@i ]) {
						.@count ++;
				}
				if (.@count == 6) {
					progressbar "ffffff",5; 
					.@total_chance = 10 + ( handicrafting_skill / 2 ) + ( countitem( .artisan_charm_id ) ? 10 : 0 );
					dispbottom "You have a total chance of " + .@total_chance + "% on crafting this item.";	
					if ( rand( 100 ) < .@total_chance ) {
						specialeffect(154, AREA, playerattached());
						atcommand "@produce "+getd(".@produce"+.@j+"[ 0 ]")+" 0";
					} else {
						specialeffect(155, AREA, playerattached());
						dispbottom "Too bad " + strcharinfo( 0 ) + ", You failed to craft this item. Better luck next time";	
					}
					
					for (.@i = 0; .@i < getarraysize(@sold_nameid); ++.@i) {	
						delitem @sold_nameid[ .@i ], @sold_quantity[.@i];
					}
					end;
				}
			}
		}
	}		

	deletearray @sold_quantity, getarraysize(@sold_quantity);
	deletearray @sold_nameid, getarraysize(@sold_nameid);
	close;
OnInit:
	.artisan_charm_id = 30159;
	end;
}