if (money == undefined) {
	function tradeValues() {shopValues = vip; shopName = "Clark - The Travelling Merchant";}
	function sortWares() {
		for (i = 0; vip [i] != undefined; i++) for (ii = 0; vip [ii+1] != undefined; ii++) if (vip [i] [8] > vip [ii] [8]) 
			{VIPsave = vip [ii]; vip [ii] = vip [i]; vip [i] = VIPsave;}
	}
	time.sleep(0.05); var VIPsave = 0; var money = shop1Var [0]; var house = shop1Var [1];
	var library = shop1Var [2]; var armory = shop1Var [3]; var kitchen = shop1Var [4]; var shop = shop1Var [5]; 
	var area = shop1Var [6]; var buyMoney = shop1Var [7]; var shopID = 0; var vip = shop1; var timing = 0;
	var pathTo = Vec3(0, 0, 0); var fpathTo = Vec3(0, 0, 0);
} timing += 1; entity.setMoveSpeed(shop_over ? 0 : 1);
shop1 = vip; shop1Var = [money, house, library, armory, kitchen, shop, area, buyMoney]
// ID, [1] damage, [2] value, [3] available amount, [x4] iconX, [x5] iconY, [x6] icon png, [x7] weight, [8] equivalent
if (shopOpen[1] == true && shopOpen[0] == "Clark - Traveling Merchant") {
	shopOpen[1] = false; shop_over = !shop_over; shopActive = 0; traderMoney = money; tradeValues();
	if (!shop_over) {player.setVelocity(0, 0, 0); sortWares();}
}
//EXCHANGE SYSTEM//
if (shopChange != undefined && shopChange != 0 && shopChange [0] != undefined) {
	//[0] moneySpent, [1] slotID/itemID, [2] itemAmount, [3] itemMD
	money += shopChange [0]; traderMoney = Math.floor(money); 
	
	//INVEST
	if (shopChange [0] > 0 && shopChange [1] != 0 && shopChange [2] == 0) {
		k = vip [shopChange [1]] [0]; j = vip [shopChange [1]] [1];
		vip [shopChange [1]] [8] += shopChange [0]/(prices [k] [j] [1]/prices [k] [j] [0]*1.2);
	}
	
	if (shopChange [2] > 0) {/*Sell*/ sortWares();
		for (i = 0; vip [i] != undefined && (vip [i] [0] != -shopChange [1] || vip [i] [1] != shopChange [3]); i++) {}
	
		k = -shopChange [1]; j = shopChange [3];
		if (vip [i] == undefined)
			 {vip [i] = [k, j, 0, shopChange [2], prices [k] [j] [3], prices [k] [j] [4], prices [k] [j] [5], prices [k] [j] [6], 0];}
		else if (vip [i] [0] == -shopChange [1] && vip [i] [1] == shopChange [3]) {vip [i] [3] += shopChange [2];}
			
		for (ii = 0; ii < shopChange [2]; ii++) {vip [i] [8] += 1/Math.sqrt(Math.max(vip [i] [8]*3, 1));}
	} else { /*BUY*/
		shopChange [2] = -shopChange [2]; vip [shopChange [1]] [3] -= shopChange [2]; 
		for (ii = 0; ii < shopChange [2]; ii++) {vip [shopChange [1]] [8] += 2/Math.sqrt(Math.max(vip [shopChange [1]] [8], 1));}
	} 
	
	shopChange = 0;
} else {
	//SORT OUT EMPTY SLOTS
	for (var i = 0; vip [i] != undefined && (vip [i] [3] != 0 || vip [i+1] == undefined || vip [i+1] [3] == 0); i++) {}
	if (vip [i] != undefined) {VIPsave = vip [i]; for (i; vip [i] != undefined; i++) 
		{if (vip [i+1] != undefined) {vip [i] = vip [i+1]; vip [i+1] = [0, 0, 0, 0];}}
	for (var ii = 0; vip [ii] != undefined; ii++) {} 
	if (VIPsave [0] < 2057 || VIPsave [0] > 2080) {vip [ii-1] = VIPsave; VIPsave = 0;}}
}

if (!shop_over) {
//TRAVEL/TRADE SYSTEM//
if (time.time >= 5400 && time.time <= 5500) {
	for (i = 0; vip [i] != undefined; i++) if (vip [i] [1] < 2057 || vip [i] [1] > 2080) {
		if (vip [i] [8] == NaN) {vip [i] [8] = 1;} k = vip [i] [0]; j = vip [i] [1];
		vip [i] [2] = prices [k] [j] [1]/prices [k] [j] [0];
		while (Math.ceil(vip [i] [3] - vip [i] [8]) < 0 && money > Math.round(vip [i] [2]/1.2)) 
			{money -= Math.round(vip [i] [2]/2); vip [i] [3] += 1;}
		while (Math.floor(vip [i] [3] - vip [i] [8]) > 0) {money += Math.round(vip [i] [2]/1.5); vip [i] [3] -= 1;}
	}
	if (library < libraryUP) {library += 1;
	if (library == 1) {shopChange = [0, -340, 4, 0];} 	if (library == 2) {shopChange = [0, -2041, 1, 0];}
	if (library == 3) {shopChange = [0, -339, 6, 0];} 	if (library == 4) {shopChange = [0, -2045, 1, 0];}
	if (library == 5) {shopChange = [0, -338, 5, 0];} 	if (library == 6) {shopChange = [0, -2043, 3, 0];}
	if (library == 7) {shopChange = [0, -321, 4, 0];}} 
	else if (house < houseUP) {house += 1;
	if (house == 1) {shopChange = [0, -17, 5, 0];} 		if (house == 2) {shopChange = [0, -20, 4, 0];}
	if (house == 3) {shopChange = [0, -271, 1, 0];} 	if (house == 4) {shopChange = [0, -211, 5, 0];}
	if (house == 5) {shopChange = [0, -2012, 1, 0];} 	if (house == 6) {shopChange = [0, -2008, 3, 0];}
	if (house == 7) {shopChange = [0, -2056, 1, 0];}} 
	else if (armory < armoryUP) {armory += 0.5;
	if (armory == 0.5) {shopChange = [0, -268, 2, 0];}	if (armory == 1) {shopChange = [0, -269, 2, 0];}
	if (armory == 1.5) {shopChange = [0, -334, 5, 0];} 	if (armory == 2) {shopChange = [0, -16, 4, 0];}
	if (armory == 2.5) {shopChange = [0, -348, 5, 0];} 	if (armory == 3) {shopChange = [0, -274, 1, 0];}
	if (armory == 3.5) {shopChange = [0, -300, 1, 0];} 	if (armory == 4) {shopChange = [0, -2100, 1, 0];}
	if (armory == 4.5) {shopChange = [0, -262, 10, 0];} if (armory == 5) {shopChange = [0, -15, 3, 0];}
	if (armory == 5.5) {shopChange = [0, -304, 1, 0];} 	if (armory == 6) {shopChange = [0, -272, 1, 0];}
	if (armory == 6.5) {shopChange = [0, -273, 1, 0];} 	if (armory == 7) {shopChange = [0, -265, 2, 0];}
	if (armory == 7.5) {shopChange = [0, -14, 3, 0];} 	if (armory == 8) {shopChange = [0, -266, 3, 0];}
	if (armory == 8.5) {shopChange = [0, -308, 1, 0];} 	if (armory == 9) {shopChange = [0, -56, 1, 0];}} 
	else if (kitchen < kitchenUP) {kitchen += 1;
	if (kitchen == 1) {shopChange = [0, -2086, 1, 0];} 	if (kitchen == 2) {shopChange = [0, -2021, 1, 0];}
	if (kitchen == 3) {shopChange = [0, -2020, 6, 0];} 	if (kitchen == 4) {shopChange = [0, -2088, 4, 0];}
	if (kitchen == 5) {shopChange = [0, -2046, 6, 0];} 	if (kitchen == 6) {shopChange = [0, -325, 2, 0];}
	if (kitchen == 7) {shopChange = [0, -296, 4, 0];} 	if (kitchen == 8) {shopChange = [0, -351, 3, 3];}
	if (kitchen == 9) {shopChange = [0, -353, 5, 0];} 	if (kitchen == 10) {shopChange = [0, -295, 6, 0];}
	if (kitchen == 11) {shopChange = [0, -2047, 3, 0];}}
	else if (shop < shopUP) {shop += 1;
	if (shop == 1) {shopChange = [0, -359, 1, 0];} 		if (shop == 2) {shopChange = [0, -352, 4, 0];}
	if (shop == 3) {shopChange = [0, -287, 5, 0];} 		if (shop == 4) {shopChange = [0, -332, 4, 0];}
	if (shop == 5) {shopChange = [0, -344, 4, 0];} 		if (shop == 6) {shopChange = [0, -341, 5, 0];}
	if (shop == 7) {shopChange = [0, -351, 3, 0];}}
	else if (area < areaUP && areaUP < 25) {area = areaUP; shopChange = [1, - 2056 - areaUP, 1, 0];}
	else if (buyMoney < money) {buyMoney += 5;
	if (buyMoney == 50) {shopChange = [0, -2083, 1, 0];} if (buyMoney == 60) {shopChange = [0, -270, 1, 0];} 
	if (buyMoney == 70) {shopChange = [0, -2092, 1, 0];} if (buyMoney == 70) {shopChange = [0, -2087, 1, 0];}
	if (buyMoney == 80) {shopChange = [0, -2024, 1, 0];} if (buyMoney == 90) {shopChange = [0, -2098, 5, 0];}}
}
//PATH SYSTEM//
var ppos = player.getPosition(); var pos = entity.getPosition(); 
if (time.time%5 == 0 && (ppos.distance(pos) > 3 || ppos.distance(pos) < 40)) {
	entity.setMoveSpeed(1); 
	if (time.time >= 22500 && time.time <= 24000) {var pathTo = Vec3(146, 62, 10);}
	if (time.time >= 6000 && time.time <= 7500) {var pathTo = Vec3(81, 69, -111);}
	if (time.time >= 7550 && time.time <= 13000 && time.time%50 == 0)
		if (Math.random()*6 > 5) {var pathTo = Vec3(pos.x - 15 + Math.random()*30, pos.y - 3 + Math.random()*6, pos.z - 15 + Math.random()*30);}
		else {var pathTo = Vec3(66 + Math.random()*30, 66 + Math.random()*6, -126 + Math.random()*30);}
	if (time.time >= 13000 && time.time <= 22500) {var pathTo = Vec3(76, 70, -86);}
} else {pathTo = Vec3(0, 0, 0); fpathTo = Vec3(0, 0, 0); entity.setMoveSpeed(0);}

if (time.time >= 0 && time.time <= 5000) {entity.setMoveSpeed(0); var fpathTo = Vec3(146, 52, 10);}
if (time.time >= 5000 && time.time <= 6000) {entity.setMoveSpeed(0); var fpathTo = Vec3(146, 62, 10);}
if (time.time >= 7500 && time.time <= 7550) {entity.setMoveSpeed(0); var fpathTo = Vec3(81, 69, -111);}

if (fpathTo.x == 0 && fpathTo.y == 0 && fpathTo.z == 0) {
	if (pos.distance(pathTo) > 3 && (pathTo.x != 0 || pathTo.y != 0 || pathTo.z != 0) && !entity.isPathing())
		{entity.pathToBlock(pathTo.x, pathTo.y, pathTo.z);}
	if (pos.distance(pathTo) < 3 && entity.isPathing()) {entity.clearPath(); }
} else if (pos.distance(fpathTo) > 3)
	{entity.setPosition(fpathTo.x, fpathTo.y, fpathTo.z); fpathTo = Vec3(0, 0, 0);}
}