#include "ships\ships.h"

extern void InitShips();
extern void InitRandomShipsNames();

string	sRndShpSpName[2], sRndShpEnName[2], sRndShpFrName[2], sRndShpPiName[2], sRndShpHoName[2], sRndShpPoName[2];

void ShipsInit()
{
	if (LoadSegment("ships\ships_init.c"))
	{
		InitShips();
		UnloadSegment("ships\ships_init.c");
	}
	if (LoadSegment("ships\ships_name.c"))
	{
		InitRandomShipsNames();
		UnloadSegment("ships\ships_name.c");
	}
}

void SetRandomNameToShip(ref rCharacter)
{
	ref rMassiveOfNames;
	//breakpoint();
	// 04-12-06 add nation >= MAX_NATIONS handling
	int nat = sti(rCharacter.nation);
	if(nat >= MAX_NATIONS)
	{
		if(IsInParty(GetMainCharacterIndex(), sti(rCharacter.index))) nat = RMGetHighestRelation(GetMainCharacter());
		else nat = PIRATE; // default case. Maybe should be PIRATE?
	}
	switch (nat) // 04-12-06 change to var
	{
		case ENGLAND:	makeref(rMassiveOfNames, sRndShpEnName); break;
		case FRANCE:	makeref(rMassiveOfNames, sRndShpFrName); break;
		case SPAIN:		makeref(rMassiveOfNames, sRndShpSpName); break;
		case PIRATE:	makeref(rMassiveOfNames, sRndShpPiName); break;
		case HOLLAND:	makeref(rMassiveOfNames, sRndShpHoName); break;
		case PORTUGAL:	makeref(rMassiveOfNames, sRndShpPoName); break;
		makeref(rMassiveOfNames, sRndShpEnName); //add default case 04-12-06
	}
	int iMassiveOfNamesSize = GetArraySize(rMassiveOfNames);
	rCharacter.Ship.Name = GetRandSubString(rMassiveOfNames[rand(iMassiveOfNamesSize-2)]);

// changed by MAXIMUS [CheckAttribute for fantomtype] -->
	if(nat == ENGLAND)
	{
		if(CheckAttribute(rCharacter,"fantomtype"))
		{
			if(rCharacter.fantomtype == "war") rCharacter.Ship.Name = "HMS " + rCharacter.Ship.Name; // PB & NK: English warships named HMS
		}
	}
// changed by MAXIMUS [CheckAttribute for fantomtype] <--
}

// PS -->
int Force_GetShipType(int maxclass, int minclass, string type, int nat)
{
	//these are JUST IN CASE...
	if(maxclass > minclass) {int tmp = maxclass; maxclass = minclass; minclass = tmp; } // NK to swap min/max if needed 05-04-15
	// 04-09-09 NK-PRS3
	if(type == "pirate")
	{
		if(PIRATES_USE_BOTH_TYPES) { type = ""; }
		else { type = "war"; }
	}
	// NK <--
	switch(type) // add this 05-05-01 for just in case.
	{
		case "trade":
			maxclass = iclamp(MAXMERCHANTCLASS, MIN_CLASS, maxclass);
			minclass = iclamp(MAXMERCHANTCLASS, MIN_CLASS, minclass);
		break;

		case "pirate":
			maxclass = iclamp(MAXPIRATECLASS, MIN_CLASS, maxclass);
			minclass = iclamp(MAXPIRATECLASS, MIN_CLASS, minclass);
		break;
		// default, war, where max class is MAX_CLASS
			maxclass = iclamp(MAX_CLASS, MIN_CLASS, maxclass);
			minclass = iclamp(MAX_CLASS, MIN_CLASS, minclass);
	}

	int iShipType = Fantom_GetShipType(maxclass, minclass, type, nat);
	if(iShipType < SHIP_TYPES_QUANTITY && iShipType >= 0) { return iShipType; }
	trace("oh no, we didn't find a ship the first go!");
	iShipType = Fantom_GetShipType(maxclass, minclass, type, nat); // try again, for randomness
	if(iShipType < SHIP_TYPES_QUANTITY && iShipType >= 0) { return iShipType; }
	iShipType = Fantom_GetShipType(maxclass, minclass, type, -1); // clear nation
	if(iShipType < SHIP_TYPES_QUANTITY && iShipType >= 0) { return iShipType; }
	iShipType = Fantom_GetShipType(maxclass, minclass, "none", -1); // clear type too
	if(iShipType < SHIP_TYPES_QUANTITY && iShipType >= 0) { return iShipType; }
	iShipType = GetCharacterShipType(GetMainCharacter());
	return iShipType;
}

void SetUpShip(ref chr, string ftype, bool isgen)
{
	if(!CheckAttribute(chr,"ship.type")) return;
	if(chr.ship.type == SHIP_NOTUSED_TYPE_NAME) return;
	int iShipType = GetCharacterShipType(&chr);
	if(iShipType == -1) return;
	if(iShipType == SHIP_NOTUSED) return;
	SetBaseShipData(&chr);
	ref Shiptype = GetShipByType(iShipType);
	// PRS3 -->
	aref arship; makearef(arship, chr.ship);
	// PRS3 <--
	if(sti(chr.nation) == PIRATE) ftype= "pirate";
	chr.FantomType = ftype;
	float mult = 1.0;
	switch(ftype)
	{
		case "trade":
			mult *= 1.0;
			if(isgen) chr.quest.officertype = OFFIC_TYPE_CAPMERCHANT;
			break;
			
		case "war":
			mult *= 1.25;
			if(isgen) chr.quest.officertype = OFFIC_TYPE_CAPNAVY;
			break;
			
		case "pirate":
			mult *= 1.0;
			if(isgen) chr.quest.officertype = OFFIC_TYPE_CAPPIRATE;
			break;

		case "error":
			mult *= 1.0;
			if(isgen) chr.quest.officertype = OFFIC_TYPE_CAPPIRATE;
			break;
	}
	int iMCShipClass = makeint(GetCharacterShipClass(GetMainCharacter()));
	//Log_SetStringToLog("Mult 1 = " + mult);
	if(makeint(Shiptype.Class) < iMCShipClass) mult *= 1.5;
	if(makeint(Shiptype.Class) > iMCShipClass) mult *= 0.75;
	chr.Points = mult * stf(GetLocalShipAttrib(arship, Shiptype, "Weight")) / 5000; // PRS3
	//Log_SetStringToLog("NewM " + mult);
	//end hack.

	//set various stuff using AIFantom calls.
	if(isgen) LAi_Create_Officer(0, chr);
	if(isgen) Fantom_SetRandomMoney(chr, ftype);
	Fantom_SetCannons(chr, ftype);
	Fantom_SetSails(chr, ftype);
	if(isgen) Ship_SetFantomData(chr);
	Fantom_ClearCargo(chr); // TIH clear out prior slot cargo
	Fantom_SetBalls(chr, ftype);
	Fantom_SetGoods(chr, ftype);
}
// PS <--

void arSetRandomStatsToShip(aref chrship, int idx, int nat)
{
	int a;
	int cal;
	bool isint = true;
	float fStatMod = 1.0; //the base modifier. See below.
	bool NatStatMod = true;
	string atrname, atrstr; //temp attribute strings
	ref refShip = GetShipByType(idx); //this is our base
	if(nat == PIRATE && frnd() < CHANCE_OVERRIDE_PRS3NAT_IF_PIRATE) nat = rand(MAX_NATIONS-1); //override nation if pirate and chance ok
	if(CheckAttribute(refShip,"nation") && frnd() > CHANCE_OVERRIDE_SHIPNATION) nat = sti(refShip.nation); //if ship has nation, use that.
	//trace("setting random stats for ship " + refShip.id + " for nation " + Nations[nat].name);

	DeleteAttribute(chrship, "stats"); //delete old tree
	if(CheckAttribute(refShip,"noNatStatMod")) { if(sti(refShip.noNatStatMod)) NatStatMod = false; } //i.e. do random variation but do not vary by nation
	
	chrship.stats = ""; //redeclare attribute
	chrship.stats.nation = nat;
	aref arShip; makearef(arShip, chrship.stats); //this is where we'll put stuff
	if(!CheckAttribute(shipstatsmult,"n"+nat)) return; // 04-09-10 if no mult for nation, return. Whoops!
	aref natstats; atrstr = "n" + nat;makearef(natstats, shipstatsmult.(atrstr)); //these are our multipliers
	// added 04-09-08 to support class-specific mults -->
	if(CheckAttribute(natstats,"class"+sti(refShip.class))) { atrname = "class"+sti(refShip.class); makearef(natstats, shipstatsmult.(atrstr).(atrname)); }

	//loop through attributes in natstats
	for(a = 0; a < GetAttributesNum(natstats); a++)
	{
		fStatMod = 1.0; //reset mod
		isint = true;
		atrname = GetAttributeName(GetAttributeN(natstats,a));
		if(HasSubStr(atrname, "class")) continue; // added 04-09-08 skip attribute if it has class in it (so we can support class-specific mults)
		if(HasSubStr(atrname, "skillmult")) continue; // 04-09-10 added skip if skill mult
		if(atrname == "MaxCaliber")
		{
			if(NatStatMod)
			{
				//special manual handling for caliber
				if(stf(natstats.MaxCaliber) == 1.0) continue; //caliber doesn't get random variation
				cal = GetCannonCaliberIndex(sti(refShip.MaxCaliber));
				if(stf(natstats.MaxCaliber) > 1.0)
				{
					if(cal != -1 && cal+1 < CANNON_CALIBERS_MAX) { cal++; arShip.MaxCaliber = Cannon_Calibers[cal]; }
				}
				else
				{
					if(cal != -1 && cal-1 >= 0) {cal--; arShip.MaxCaliber = Cannon_Calibers[cal]; }
				}
			}
		}
		else
		{
			if(atrname == "speedrate" || atrname == "turnrate") isint = false;
			//new local stat will be base stat * (natstat mult +/- rand(SHIPSTAT_VARIANCE))
			if(NatStatMod) fStatMod = makefloat(natstats.(atrname)); // so we can still do random variation even if not doing nation variation.
			arShip.(atrname) = makefloat(refShip.(atrname)) * (fStatMod * RandMult(SHIPSTAT_VARIANCE)); // 05-05-11 mult. If you prefer additive: - SHIPSTAT_VARIANCE + frnd()*SHIPSTAT_VARIANCE*2);
			if(isint) arShip.(atrname) = round(stf(arShip.(atrname)));
		}
	}
}

//KB Tuning ships, changed parameters - removed aref chrship and changed by int _idx 
//// NK PRS3 -->
void SetRandomStatsToShip(int _idx, int idx, int nat)
{
//KB - Tuning ships - changed calling parameters
	if (_idx < 0) return;
	aref chrship;
	ref character;
	if (_idx == GetMainCharacterIndex())
		character = GetMainCharacter();
	else
		character = GetCharacter(_idx);
	makearef(chrship, character.ship);
	ref refShip = GetShipByType(idx); //this is our base

	if (CheckAttribute(refShip, "unique")) {
		if (sti(refShip.unique)) return; //if ship is unique, do not modify stats
	}
	if (CheckAttribute(chrship, "nomodstats")) {
		if (sti(chrship.nomodstats)) return; //if flag set, do not mod stats
	}

	arSetRandomStatsToShip(chrship, idx, nat);

	if (CheckAttribute(chrship, "standard")) {
		if (sti(chrship.standard)) return; //if ship is "standard", do not tune her
	}

//KB - Tuning ships
//KB Tuning ships <--
//KB - Tuning Ships - Adding random tuned chars to ships
	InitTun(_idx);
	int cost=0;
	int time=0;
	if (rand(99) <= sti(CPDISP) ) {ApplyTun(_idx,"cplates",cost,time) ;}
	if (rand(99) <= sti(BCDISP) ) {ApplyTun(_idx,"bcannons",cost,time);}
	if (rand(99) <= sti(GCDISP) ) { ApplyTun(_idx,"gchasers",cost,time);}
	if (rand(99) <= sti(NSDISP) ) { ApplyTun(_idx,"nsails",cost,time);}
	if (rand(99) <= sti(TMDISP) ) { ApplyTun(_idx,"ltopmasts",cost,time);}
	if (rand(99) <= sti(SSDISP) ) {ApplyTun(_idx,"stays",cost,time);}
	if (rand(99) <= sti(RHDISP) ) { ApplyTun(_idx,"rhull",cost,time);}
	if (nat == PIRATE || character.nation == PIRATE ) { 
		if (rand(99) <= sti(FLUSHEDPROB_IFPIRATE)) { ApplyTun(_idx,"flushed",cost,time); }
		}
	else {	
		if (rand(99) <= sti(FLUSHEDPROB) )  ApplyTun(_idx,"flushed",cost,time); 
	}
}

string GetLocalShipAttrib(aref chrship, ref ship, string attrib) //pass this the chr and the shipstypes[] entry and the attribute name. Will return local version if there.
{
	//originally written to use a string; should work ok without it.
	//string tmpstr;
	//tmpstr = ship.(attrib);
	// NK special handling for cannon qty 05-04-18 -->
	if(attrib == "CurCanQty")
	{
		// TIH --> empty cannons bug fix 7-7-06 // KK -->
		bool bCannons = false;
		if (CheckAttribute(chrship,"Cannons.Borts.cannonf.qty") && CheckAttribute(chrship,"Cannons.Borts.cannonb.qty") && CheckAttribute(chrship,"Cannons.Borts.cannonl.qty") && CheckAttribute(chrship,"Cannons.Borts.cannonr.qty"))
				bCannons = true;
		if (bCannons)
			return "" + (sti(chrship.Cannons.Borts.cannonf.qty) + sti(chrship.Cannons.Borts.cannonb.qty) + sti(chrship.Cannons.Borts.cannonl.qty) + sti(chrship.Cannons.Borts.cannonr.qty));// TIH what kind of kludge is ""+int ? // KK ""+<int> is conversion to string
		else
			return GetLocalShipAttrib(&chrship, &ship, "CannonsQuantity");
		// TIH <-- empty cannons bug fix // <-- KK
	}
	if(attrib == "MaxCanQty")
	{
		int fullqty = sti(GetLocalShipAttrib(&chrship, &ship, "Cannons.Borts.cannonf.qty"));
		fullqty += sti(GetLocalShipAttrib(&chrship, &ship, "Cannons.Borts.cannonb.qty"));
		fullqty += sti(GetLocalShipAttrib(&chrship, &ship, "Cannons.Borts.cannonl.qty"));
		fullqty += sti(GetLocalShipAttrib(&chrship, &ship, "Cannons.Borts.cannonr.qty"));
		return ""+fullqty;
	}
	// NK <--
	if(CheckAttribute(chrship,"stats."+attrib)) return chrship.stats.(attrib);
	if (CheckAttribute(ship, attrib)) return ship.(attrib); // KK
	return ""; // KK
}

bool CheckShipAttribute(aref chrship, ref ship, string attrib)
{
	if(CheckAttribute(chrship,"stats."+attrib)) return true;
	if(CheckAttribute(ship,attrib)) return true;
	return false;
}

// 04-09-10 added skill mult for nations.
float GetSkillMultByNation(string skill, int nat)
{
	if(nat == -1) return 1.0;
	if(nat > MAX_NATIONS) return 1.0; // 05-04-20 check
	string atr = "n"+nat + ".skill." + skill;
	if(!Checkattribute(shipstatsmult,atr)) return 1.0;
	return stf(shipstatsmult.(atr));
	return 1.0;
}
// NK PRS3 <--

string GetShipLocationID(ref chref)
{
	if(!CheckAttribute(chref,"ship.type")) return "";
	int st = GetCharacterShipType(&chref); // PS
	if(st==SHIP_NOTUSED) return "";
	if(st==SHIP_FORT)
	{
		aref ar_FindLoc = FindIslandReloadLocator(chref.location,chref.location.locator);
		if( CheckAttribute(ar_FindLoc,"go") ) return ar_FindLoc.go;
		return "";
	}
	return GetCharacterShipQDeck(chref); // KK
}

// NK moved shipyard fill code here
void FillShipsList(ref NPChar)
{
	DeleteAttribute(NPChar, "shipyard");

	//npchar.shipyard.ship1 = SHIP_TARTANE;
	int NPCNumShips = 0;
	string attr;
	int i, iClass;
	int shipnation = -1;
	int iNation = sti(NPChar.nation);
	ref rShip;
	aref curship; // PRS3

	int iClassMin = 7; //no minimum class
	int iClassMax = 3;

	//now we can do this via townsack.
	string sTownName = "";
	if(CheckAttribute(Locations[FindLoadedLocation()],"townsack"))
	{
		sTownName = Locations[FindLoadedLocation()].townsack;
	}
	else { sTownName = GetTownIDFromGroup(Stores[GetCharacterCurrentStore(GetMainCharacter())].group); }
	if(SHIPYARD_SELL_ALL_CLASSES) iClassMax = 1; // NK 05-03-19 add toggle to class limiting
	else
	{
		if(sTownName != "")
		{
			if(GetTownSize(sTownName) >= (STORE_MINSIZE/2)) iClassMax = 3;
			if(GetTownSize(sTownName) >= STORE_MINSIZE) iClassMax = 2;
		}
	}
	if(iNation == PIRATE) iClassMax = 1;
	for (i=0; i<SHIP_TYPES_QUANTITY; i++)
	{
		shipnation = -1;
		rShip = GetShipByType(i);
		iClass = MakeInt(rShip.Class);
		if (CheckAttribute(NPChar,"id") && NPChar.id == "Hendrick Vanderdecken") // PB: Enable the buying of unavailable quest ships
		{
			if(sti(rShip.CanBuy) == true || sti(rShip.CanEncounter) == true) { continue; }
		}
		else
		{
			if (iClass > iClassMin) { continue; }
			if (iClass < iClassMax) { continue; }
			if(sti(rShip.CanBuy) != true) { continue; }
			// PB: Periods -->
// KK -->
		if (iNation >= 0 && iNation < MAX_NATIONS) {
			string sNation = GetNationNameByType(iNation);
			if (CheckAttribute(rShip, sNation) == true && sti(rShip.(sNation)) > FRAND(1.0)) continue;
		}
		string sPeriod = GetCurrentPeriod();
		if (CheckAttribute(rShip, "period."+sPeriod) && sti(rShip.period.(sPeriod)) > FRAND(1.0)) continue;
// <-- KK
			/*switch(iNation)
			{
				case ENGLAND:	if(CheckAttribute(rShip,"england") && sti(rShip.england) > rand(100))	continue; break;
				case HOLLAND:	if(CheckAttribute(rShip,"holland") && sti(rShip.holland) > rand(100))	continue; break;
				case FRANCE:	if(CheckAttribute(rShip,"france") && sti(rShip.france) > rand(100))		continue; break;
				case SPAIN:		if(CheckAttribute(rShip,"spain") && sti(rShip.spain) > rand(100))		continue; break;
				case PORTUGAL:	if(CheckAttribute(rShip,"portugal") && sti(rShip.portugal) > rand(100))	continue; break;
				case PIRATE:	if(CheckAttribute(rShip,"pirate") && sti(rShip.pirate) > rand(100))		continue; break;
			}
			switch(GetCurrentPeriod())
			{
				case PERIOD_EARLY_EXPLORERS:		if(CheckAttribute(rShip,"period0") && sti(rShip.period0) > rand(100)) continue; break;
				case PERIOD_THE_SPANISH_MAIN:		if(CheckAttribute(rShip,"period1") && sti(rShip.period1) > rand(100)) continue; break;
				case PERIOD_GOLDEN_AGE_OF_PIRACY:	if(CheckAttribute(rShip,"period2") && sti(rShip.period2) > rand(100)) continue; break;
				case PERIOD_COLONIAL_POWERS:		if(CheckAttribute(rShip,"period3") && sti(rShip.period3) > rand(100)) continue; break;
				case PERIOD_REVOLUTIONS:			if(CheckAttribute(rShip,"period4") && sti(rShip.period4) > rand(100)) continue; break;
				case PERIOD_THE_CORSICAN:			if(CheckAttribute(rShip,"period5") && sti(rShip.period5) > rand(100)) continue; break;
			}*/
			// PB: Periods <--
			if(CheckAttribute(rShip,"Nation")) shipnation = sti(rShip.Nation);
			if(Rand(makeint(retmax(0,SHIPYARD_CHANCE_CLASS-1-iClass)))) { continue; } // NK 05-03-31 change this from 7 to define.
		}
		shipnation = iNation;
		if(frnd() < SHIPYARD_CHANCE_OTHERNATION)
		{
			// loop until find available other nation. Note, to avoid infinite loop checking capped at 50.
			shipnation = rand(MAX_NATIONS - 1);
			string tmp = "skipnat"+shipnation;
			for(int n = 0; n < 50; n++)
			{
				if(!CheckAttribute(rShip,tmp)) break;
				if(sti(rShip.(tmp)) == 0) break;
				shipnation = rand(MAX_NATIONS - 1);
				tmp = "skipnat"+shipnation;
			}
		}
		NPCNumShips++;
		attr = "ship"+NPCNumShips;
		NPChar.shipyard.(attr) = i;
		// PRS3 -->
		makearef(curship, NPChar.shipyard.(attr));
		arSetRandomStatsToShip(curship, i, shipnation);
		// PRS3 <--
	}

	// TIH --> rpg_style Auto-sort ships by class and price Jul27'06
	// yes this is an ugly mess... but the engine was giving me beef... so this is how it ended up
	// this DOES work, works real good... just ugly :)
	// by all means if you see a cleaner way to do it, then PLEASE do it... no one else has!
	if ( SY_RPG_STYLE )
	{
		aref 	syShipList;
		makearef(syShipList, NPChar.shipyard);
		int 	st;
		int 	j,x,y;
		int 	numShips = GetAttributesNum(syShipList);
		int 	SortClass[600];
		int 	SortPrice[600];
		int 	holder, idxholder;
		aref 	tmparef, tmpShip;
		aref 	tmparefC, tmparefN;
		aref 	swapShipT, swapShipN, swapShipC;
		string 	slotN, slotC;

		syShipList.swaptmp = true;
		makearef(swapShipT, syShipList.swaptmp);

		// god I wish I had a real database engine ...
		for(j = 0; j < numShips; j++)
		{
			tmparef = GetAttributeN(syShipList,j); makearef(tmpShip, tmparef);
			st 		= sti(GetAttributeValue(tmparef));
			SortClass[j] = GetLocalShipAttrib(tmpShip, ShipsTypes[st],"Class");
			SortPrice[j] = GetLocalShipAttrib(tmpShip, ShipsTypes[st],"Price");
		}

		//trace("----------------------------------------------");
		//trace("---> before sort:");
		//DumpAttributes(syShipList);
		//trace("----------------------------------------------");

		// sort by class of ship
		for(x = 0; x < numShips; x++)
		{
			for(y = 0; y < numShips-1; y++)
			{
				if(SortClass[y] < SortClass[y+1])
				{
					tmparefC = GetAttributeN(syShipList,y);
					tmparefN = GetAttributeN(syShipList,(y+1));
					slotC 	= GetAttributeName(tmparefC);
					slotN 	= GetAttributeName(tmparefN);

					makearef(swapShipC,syShipList.(slotC));
					makearef(swapShipN,syShipList.(slotN));

					//trace("-> swapShipC  Class: "+SortClass[y]+"  Slot: "+slotC+"");
					//trace("-> swapShipN  Class: "+SortClass[y+1]+"  Slot: "+slotN+"");

					CopyAttributes(swapShipT,swapShipN);
					CopyAttributes(swapShipN,swapShipC);
					CopyAttributes(swapShipC,swapShipT);

					idxholder			= sti(GetAttributeValue(tmparefN));
					syShipList.(slotN) 	= sti(GetAttributeValue(tmparefC));
					syShipList.(slotC) 	= idxholder;

					holder 			= SortClass[y+1];
					SortClass[y+1] 	= SortClass[y];
					SortClass[y] 	= holder;
					holder 			= SortPrice[y+1];
					SortPrice[y+1] 	= SortPrice[y];
					SortPrice[y] 	= holder;
				}
			}
		}
		//trace("----------------------------------------------");
		//trace("---> after sort class:");
		//DumpAttributes(syShipList);
		//trace("----------------------------------------------");

		// do it all over again, but for price per class
		for(x = 0; x < numShips; x++)
		{
			for(y = 0; y < numShips-1; y++)
			{
				if(SortPrice[y] > SortPrice[y+1] && SortClass[y] == SortClass[y+1])
				{
					tmparefC = GetAttributeN(syShipList,y);
					tmparefN = GetAttributeN(syShipList,(y+1));
					slotC 	= GetAttributeName(tmparefC);
					slotN 	= GetAttributeName(tmparefN);

					makearef(swapShipC,syShipList.(slotC));
					makearef(swapShipN,syShipList.(slotN));

					//trace("-> swapShipC  Class: "+SortClass[y]+"  Slot: "+slotC+"");
					//trace("-> swapShipN  Class: "+SortClass[y+1]+"  Slot: "+slotN+"");

					CopyAttributes(swapShipT,swapShipN);
					CopyAttributes(swapShipN,swapShipC);
					CopyAttributes(swapShipC,swapShipT);

					idxholder			= sti(GetAttributeValue(tmparefN));
					syShipList.(slotN) 	= sti(GetAttributeValue(tmparefC));
					syShipList.(slotC) 	= idxholder;

					holder 			= SortClass[y+1];
					SortClass[y+1] 	= SortClass[y];
					SortClass[y] 	= holder;
					holder 			= SortPrice[y+1];
					SortPrice[y+1] 	= SortPrice[y];
					SortPrice[y] 	= holder;
				}
			}
		}
		//trace("----------------------------------------------");
		//trace("---> after sort class & price:");
		//DumpAttributes(syShipList);
		//trace("----------------------------------------------");
		DeleteAttribute(syShipList,"swaptmp");
	}
	// TIH <--

}
// NK <--
