#include "islands\islands_loader.c"

extern void InitIslands();

void IslandsInit()
{
	if(LoadSegment("islands\islands_init.c"))
	{
		InitIslands();
		UnloadSegment("islands\islands_init.c");
	}
}

ref GetIslandByIndex(int iIslandIndex) 
{ 
	return &Islands[iIslandIndex]; 
}

ref GetIslandByID(string sIslandID) 
{
	// NK use native 05-05-03 -->
	int idx = FindIsland(sIslandID);
	if(idx >= 0 && idx <ISLANDS_QUANTITY) return &Islands[idx];
	return &Islands[0];
	// NK <--
	for (int i=0;i<ISLANDS_QUANTITY;i++) // NK 05-03-30 use int for max, and oversized array
	{
		if (Islands[i].id == sIslandID) return &Islands[i];
	}
	//Trace("Invalid island ID: " + sIslandID);
	return &Islands[0];
}

aref FindIslandReloadLocator(string sIslandID, string sLocatorName)
{
	aref	arReload, arLocator;

	int		iIsland = FindIsland(sIslandID);
	if (iIsland < 0)
	{
		Trace("Can't find island sIslandID = " + sIslandID );
		// KK return arLocator;
		aref tmpref; makearef(tmpref, NullCharacter); // KK
		return tmpref; // KK
	}
	ref		rIsland = GetIslandByIndex(iIsland);
	makearef(arReload, rIsland.reload);
	int		iNumReload = GetAttributesNum(arReload);
	for (int i=0;i<iNumReload;i++)
	{
		arLocator = GetAttributeN(arReload, i);
		if (arLocator.Name == sLocatorName)
		{
			return arLocator;
		}
	}
	Trace("Can't find locator = " + sLocatorName + " on island sIslandID = " + sIslandID );
	return arLocator;
}

int FindIsland(string id)
{
	if (id=="") return -1;
	// NK use native 05-05-03 -->
	int idx = NativeFindCharacter(&Islands, id);
	if(idx >= 0 && idx <ISLANDS_QUANTITY) return idx;
	return -1;
	// NK <--
	for(int n = 0; n<ISLANDS_QUANTITY; n++) // NK 05-03-30 use int for max, and oversized array
	{
		if(Islands[n].id == id) return n;
	}
	return -1;
}

void Island_SetReloadEnableGlobal(string sIslandID, bool bEnable)
{
	ref rIsland = GetIslandByID(sIslandID);
	rIsland.reload_enable = bEnable;
}

void Island_SetReloadEnableLocal(string sIslandID, string sLocatorID, bool bEnable)
{
	aref arIslandReloadLocator = FindIslandReloadLocator(sIslandID, sLocatorID);
	arIslandReloadLocator.enable = bEnable;
}

bool Island_isReloadEnableGlobal(string sIslandID)
{
	ref rIsland = GetIslandByID(sIslandID);
	return sti(rIsland.reload_enable);
}

bool Island_isReloadFort(string sIslandID, string sLocatorID)
{
	aref arIslandReloadLocator = FindIslandReloadLocator(sIslandID, sLocatorID);
	if (CheckAttribute(arIslandReloadLocator,"fort")) return true;
	return false;
}

bool Island_isReloadEnableLocal(string sIslandID, string sLocatorID)
{
	aref arIslandReloadLocator = FindIslandReloadLocator(sIslandID, sLocatorID);
	if (!CheckAttribute(arIslandReloadLocator,"enable")) return true;
	return sti(arIslandReloadLocator.enable);
}

void Island_SetGotoEnableLocal(string sIslandID, string sLocatorID, bool bEnable)
{
	aref arIslandReloadLocator = FindIslandReloadLocator(sIslandID, sLocatorID);
	arIslandReloadLocator.goto_enable = bEnable;
}

bool Island_isGotoEnableLocal(string sIslandID, string sLocatorID)
{
	aref arIslandReloadLocator = FindIslandReloadLocator(sIslandID, sLocatorID);
	if (!CheckAttribute(arIslandReloadLocator,"goto_enable")) return true;
	return sti(arIslandReloadLocator.goto_enable);
}

void Island_SetEncountersEnable(string sIslandID, bool bEnable)
{
	ref rIsland = GetIslandByID(sIslandID);
	rIsland.Enc_enable = bEnable;
}

bool Island_IsEncountersEnable(string sIslandID)
{
	ref rIsland = GetIslandByID(sIslandID);
	if (!CheckAttribute(rIsland, "Enc_enable")) return true;
	return sti(rIsland.Enc_enable);
}

int FindIslandBySeaLocation(string locID)
{
	if(FindIsland(locID)!=-1) return FindIsland(locID);//MAXIMUS
	int n,m;
	aref arroot,arreload;
	for(n=0; n<ISLANDS_QUANTITY; n++) // NK 05-03-30 use int for max, and oversized array
	{
		makearef(arroot,Islands[n].reload);
		for(m=0; m<GetAttributesNum(arroot); m++)
		{
			arreload = GetAttributeN(arroot,m);
			if(arreload.go == locID) return n;
		}
	}
	return -1;
}

// KK -->
string FindIslandName(string islandid)
{
	string ret;
	if (REAL_CARIBBEAN == 1) {
		switch (islandid) {
			case "Falaise de Fleur":
				ret = "Martinique";
			break;
			case "FalaiseDeFleur":
				ret = "Martinique";
			break;
			case "Redmond":
				ret = "Jamaica";
			break;
			case "Oxbay":
				ret = "Barbados";
			break;
			case "Douwesen":
				ret = "Bonaire";
			break;
			case "Conceicao":
				ret = "Granada";
			break;
			case "Isla Muelle":
				ret = "Puerto Rico";
			break;
			case "IslaMuelle":
				ret = "Puerto Rico";
			break;
			case "Quebradas Costillas":
				ret = "Nevis";
			break;
			case "QuebradasCostillas":
				ret = "Nevis";
			break;
			case "Khael Roa":
				ret = "Cozumel";
			break;
			case "KhaelRoa":
				ret = "Cozumel";
			break;
			case "Sent":
				ret = "Saint Martin";
			break;
			case "Terks":
				ret = "Turks";
			break;
			// default:
				ret = islandid;
		}
	} else {
		ret = islandid;
	}
	return ret;
}
// <-- KK

string FindIslandByLocation(string locID)//MAXIMUS
{
	if(locID=="" || FindLocation(locID)==-1)
	{
		if(CheckAttribute(&characters[GetMainCharacterIndex()],"location.from_sea")) return Islands[FindIslandBySeaLocation(characters[GetMainCharacterIndex()].location.from_sea)].id;
		return "";
	}
	if(CheckAttribute(&locations[FindLocation(locID)],"townsack")) return GetIslandIDFromTown(GetCurrentTownID());
	if(CheckAttribute(&locations[FindLocation(locID)],"island")) return locations[FindLocation(locID)].island;
	if(FindIslandBySeaLocation(FindSeaShoreForLocation(locID))!=-1) return Islands[FindIslandBySeaLocation(FindSeaShoreForLocation(locID))].id;
	return "";
}
