extern void InitNations();
extern void relations_init(); // KK
extern void groups_init(); // KK

#event_handler(GET_RELATION_EVENT,"GetRelationEvent");

void Nations_SetAllFriends()
{
	int i;
	
	for(i=0;i<MAX_NATIONS_SQR;i++)
	{
		NationsRelations[i] = RELATION_FRIEND;
	}

	for(i=0;i<MAX_NATIONS;i++)
	{
		NationsRelations2MainCharacter[i] = RELATION_FRIEND;
	}
}

void NationsInit()
{
	if(LoadSegment("nations\nations_init.c"))
	{
		InitNations();
		UnloadSegment("nations\nations_init.c");
	}
    
	Nations_SetAllFriends();
}

void SetNationRelation2MainCharacter(int iNation, int iRelationType)
{
	if (iNation >= 0 && iNation < MAX_NATIONS) // KK
		NationsRelations2MainCharacter[iNation] = iRelationType;
}

void SetNationRelationBoth(int iNation1, int iNation2, int iRelationType)
{
	if (iNation1 >= 0 && iNation1 < MAX_NATIONS && iNation2 >= 0 && iNation2 < MAX_NATIONS) { // KK
		NationsRelations[iNation1 * MAX_NATIONS + iNation2] = iRelationType;
		NationsRelations[iNation2 * MAX_NATIONS + iNation1] = iRelationType;
	}
}

void SetNationRelation(int iNation1, int iNation2, int iRelationType)
{
	if (iNation1 >= 0 && iNation1 < MAX_NATIONS && iNation2 >= 0 && iNation2 < MAX_NATIONS) // KK
		NationsRelations[iNation1 * MAX_NATIONS + iNation2] = iRelationType;
}

// =================================================================================================
// Character Relation
// =================================================================================================
void SetCharacterRelationAsOtherCharacter(int iCharacterIndex1, int iCharacterIndex2)
{
	Characters[iCharacterIndex1].relation.UseOtherCharacter = iCharacterIndex2;
}

void SetCharacterRelationBoth(int iCharacterIndex1, int iCharacterIndex2, int iRelationType)
{
	SetCharacterRelation(iCharacterIndex1, iCharacterIndex2, iRelationType);
	SetCharacterRelation(iCharacterIndex2, iCharacterIndex1, iRelationType);
}

void SetCharacterRelation(int iCharacterIndex1, int iCharacterIndex2, int iRelationType)
{
	if (iCharacterIndex1 < 0) return; // KK, LDH 14Oct06 removed test for iCharacterIndex2<0
	string sTemp1 = "" + iCharacterIndex2;
	Characters[iCharacterIndex1].relation.(sTemp1) = iRelationType;
}

// =================================================================================================
// Get Relation Methods
// =================================================================================================
int GetNationRelation(int iNation1, int iNation2)
{
	if (iNation1 >= 0 && iNation1 < MAX_NATIONS && iNation2 >= 0 && iNation2 < MAX_NATIONS) // KK
		return NationsRelations[iNation1 * MAX_NATIONS + iNation2];
	if (iNation1 == PERSONAL_NATION && iNation2 >= 0 && ination2 < MAX_NATIONS) return NationsRelations2MainCharacter[iNation2]; // KK
	if (iNation2 == PERSONAL_NATION && iNation1 >= 0 && ination1 < MAX_NATIONS) return NationsRelations2MainCharacter[iNation1]; // KK
	return RELATION_NEUTRAL; // KK
}

int GetNationRelation2MainCharacter(int iNation)
{
	if (iNation == PERSONAL_NATION) return RELATION_FRIEND; // KK
	// added by KAM -->
	if (iNation >= 0 && iNation < MAX_NATIONS)	// LDH fix for iNation < 0
	{
	// <-- added by KAM

	return NationsRelations2MainCharacter[iNation];

	// added by KAM -->
	}
	else
	{
		return RELATION_NEUTRAL;
	}
	// <-- added by KAM
}

int GetNationRelation2Character(int iNation, int iCharacterIndex)
{
	ref rCharacter = GetCharacter(iCharacterIndex);

	if (CheckAttribute(&rCharacter, "relation.UseOtherCharacter"))
	{
		if (sti(rCharacter.relation.UseOtherCharacter) >= 0)
		{
			rCharacter = GetCharacter(sti(rCharacter.relation.UseOtherCharacter));
		}
	}

	int iMCI = GetMainCharacterIndex();
	int iNation2 = sti(rCharacter.nation);
	if (iNation >= MAX_NATIONS || iNation2 >= MAX_NATIONS) return RELATION_NEUTRAL; // NK for KAM extra nations 05-04-20 // KK
	if (iNation < 0 || iNation2 < 0) return RELATION_NEUTRAL;	// LDH fix for iNation < 0
	if (iMCI == sti(rCharacter.index))
	{
		return NationsRelations2MainCharacter[iNation];
	}
	return NationsRelations[iNation * MAX_NATIONS + iNation2];
}

int GetRelation(int iCharacterIndex1, int iCharacterIndex2)
{
	int iMCI = GetMainCharacterIndex();

	ref rCharacter1 = GetCharacter(iCharacterIndex1);
	ref rCharacter2 = GetCharacter(iCharacterIndex2);

	if (CheckAttribute(&rCharacter1, "relation.UseOtherCharacter"))
	{
		if (sti(rCharacter1.relation.UseOtherCharacter) >= 0)
		{
			rCharacter1 = GetCharacter(sti(rCharacter1.relation.UseOtherCharacter));
		}
	}
	if (CheckAttribute(&rCharacter2, "relation.UseOtherCharacter"))
	{
		if (sti(rCharacter2.relation.UseOtherCharacter) >= 0)
		{
			rCharacter2 = GetCharacter(sti(rCharacter2.relation.UseOtherCharacter));
		}
	}

	if (rCharacter1.index == rCharacter2.index) return RELATION_FRIEND;

	// character 2 character relation
	{
		string sTemp = "relation." + rCharacter2.index;
		if (CheckAttribute(&rCharacter1,sTemp))
		{
			return sti(rCharacter1.(sTemp));
		}
	}
	//Trace("iNation1 = " + iNation1 + ", iNation2 = " + iNation2);

	int iNation1 = sti(rCharacter1.nation);
	int iNation2 = sti(rCharacter2.nation);

	if (iNation1 < 0 || iNation2 < 0) return RELATION_NEUTRAL;	// LDH fix for iNation < 0
	// 04-12-06 new section
	bool ismc1 = IsInParty(iMCI, sti(rCharacter1.index));
	bool ismc2 = IsInParty(iMCI, sti(rCharacter2.index));
	if(ismc1 && ismc2) return RELATION_FRIEND;

	if (iNation1 == NEUTRAL_NATION || iNation2 == NEUTRAL_NATION) return RELATION_NEUTRAL; // KK

	if (iNation1 == PERSONAL_NATION && iNation2 < MAX_NATIONS) return NationsRelations2MainCharacter[iNation2]; // KK
	if (iNation2 == PERSONAL_NATION && iNation1 < MAX_NATIONS) return NationsRelations2MainCharacter[iNation1]; // KK

	if(iNation1 >= MAX_NATIONS)
	{
		if(ismc1) //this should never be false...
		{
			iNation1 = RMGetHighestRelation(GetMainCharacter());
		}
		else return RELATION_NEUTRAL; // this case should never occur, since only pchar can set to >=maxnat.
	}
	if(iNation2 >= MAX_NATIONS && iNation2 != PERSONAL_NATION) // KK
	{
		if(ismc2) //this should never be false...
		{
			iNation2 = RMGetHighestRelation(GetMainCharacter());
		}
		else return RELATION_NEUTRAL; // this case should never occur, since only pchar can set to >=maxnat.
	}

	// nation 2 character relation
	if(ismc1) //if (iMCI == sti(rCharacter1.index))
	{
		//Trace("iMainCharacterRelation = " + NationsRelations2MainCharacter[iNation2]);
		if (iNation2 == PERSONAL_NATION) return RELATION_FRIEND; // KK
		return NationsRelations2MainCharacter[iNation2];
	}
	if(ismc2) //if (iMCI == sti(rCharacter2.index))
	{
		//Trace("iMainCharacterRelation = " + NationsRelations2MainCharacter[iNation1]);
		if (iNation1 == PERSONAL_NATION) return RELATION_FRIEND; // KK
		return NationsRelations2MainCharacter[iNation1];
	}

	// nation 2 nation relation
	return NationsRelations[iNation1 * MAX_NATIONS + iNation2];
}

int GetRelationEvent()
{
	int iCharacterIndex1 = GetEventData();
	int iCharacterIndex2 = GetEventData();
	return GetRelation(iCharacterIndex1, iCharacterIndex2);
}

int FindEnemyNation2Nation(int iNation)
{
	int iNations[MAX_NATIONS];
	int iNumNations = 0;

	for (int i=0;i<MAX_NATIONS;i++)
	{
		if (GetNationRelation(i,iNation) == RELATION_ENEMY)
		{
			iNations[iNumNations] = i;
			iNumNations++;
		}
	}
	if (iNumNations == 0) return PIRATE;		// LDH fix
	return iNations[rand(iNumNations - 1)];
}

// NK add friendly/neutral case here too 04-09-09
int FindFriendlyNation2Nation(int iNation)
{
	int iNations[MAX_NATIONS];
	int iNumNations = 0;

	for (int i=0;i<MAX_NATIONS;i++)
	{
		if (GetNationRelation(i,iNation) != RELATION_ENEMY)
		{
			iNations[iNumNations] = i;
			iNumNations++;
		}
	}
	if (iNumNations == 0) return PIRATE;		// LDH fix
	return iNations[rand(iNumNations - 1)];
}
// NK <--

int FindEnemyNation2Character(int iCharacterIndex)
{
	int iNations[MAX_NATIONS];
	int iNumNations = 0;

	for (int i=0; i<MAX_NATIONS; i++)
	{
		if (GetNationRelation2Character(i, iCharacterIndex) == RELATION_ENEMY)
		{
			iNations[iNumNations] = i;
			iNumNations++;
		}
	}
	if (iNumNations == 0) return PIRATE;		// LDH fix
	return iNations[rand(iNumNations - 1)];
}

int GetNationTypeByName(string nationName)
{
	for(int i=0; i<MAX_NATIONS; i++)
	{
		if(Nations[i].Name==nationName) return i;
	}
	return -1;
}

string GetNationNameByType(int iNationType)
{
	if (iNationType >= 0 && iNationType < MAX_NATIONS)		// LDH fix for iNationType < 0
	{
		return Nations[iNationType].Name;
	}

	// added by KAM -->
	if (iNationType == CATALONIA) { return "Catalonia"; }
	if (iNationType == NEUTRAL_NATION) { return "no nation"; }
	if (iNationType == UNKNOWN_NATION) { return "unknown nation"; }
	// <-- added by KAM

	return "unknown"; // NK
}

ref GetNationByType(int iNationType)
{
	if (iNationType < 0) iNationType = PIRATE;		// LDH fix
	return &Nations[iNationType];
}

// RM Put these in Nations.c -->

//NEW PROPERTY: nation[n].Desc = adjective, i.e. French
//this returns it.
string GetNationDescByType(int iNationType)
{
	if (iNationType >= 0 && iNationType < MAX_NATIONS)  // LDH fix for iNationType < 0
	{
		return Nations[iNationType].Desc;
	}

	// added by KAM -->
	if (iNationType == CATALONIA) { return "Catalan"; }
	if (iNationType == NEUTRAL_NATION) { return "neutral"; }
	if (iNationType == UNKNOWN_NATION) { return "unknown"; }
	// <-- added by KAM

	return "unknown";
}


//NEW PROPERTY: nation[n].Royal = title, i.e. His Most Christian Majesty
//this returns it.
string GetNationRoyalByType(int iNationType)
{
	if (iNationType >= 0 && iNationType < MAX_NATIONS)	// LDH fix for iNationType < 0
	{
		return Nations[iNationType].Royal;
	}
	return "Unknown";
}

//new property: nation[n].soldiergroup = first part of name for soldiers. For Hol + Por, it's name of island, not nation
string GetSoldiersName(int nation)
{
	if (nation < 0) return "";		// LDH fix
	if (nation == PERSONAL_NATION) return "PERSONAL"; // KK
	if(CheckAttribute(&Nations[nation],"soldiergroup")) return Nations[nation].soldiergroup;
	switch(nation)
	{
		case ENGLAND: return "ENGLAND"; break;
		case FRANCE: return "FRANCE"; break;
		case SPAIN: return "SPAIN"; break;
		case HOLLAND: return "DOUWESEN"; break;
		case PORTUGAL: return "CONCEICAO"; break;
	}
	return "";
}


//START RM CODE
//gets RMRelation between char and iNation
float GetRMRelation(ref char, int iNation)
{
	if (iNation == PERSONAL_NATION) return REL_NEUTRAL;
	string sNation = "" + iNation;
	float rel = REL_AMNESTY; // default to amnesty
	//if(sti(char.nation) == iNation) rel = REL_NEUTRAL;
	if(CheckAttribute(char, "nations."+sNation+".Relation"))
		{ rel = stf(char.nations.(sNation).Relation); }
	return rel;
}


//gets stock relation type based on RMRelation
int GetRMRelationType(float rel)
{
	if(rel <= REL_WAR) return RELATION_ENEMY;
	if(rel < REL_NEUTRAL) return RELATION_NEUTRAL;
	return RELATION_FRIEND;
}


//return name of relation, enemy->neutral->LoM->rank.
string GetRMRelationName(ref char, int iNation)
{
	float rel = GetRMRelation(char, iNation);
	if(rel <= REL_WAR) return "Hostile";
	if(rel <= REL_AFTERATTACK) return "Wary"; //right now only this gets <=
	if(rel < REL_NEUTRAL) return "Neutral";
	if(!GetRank(char, iNation)) return "Letter of Marque";
	return GetRankName(char, iNation);
}


//sets RMRelation between char and iNation to rel, sets
//stockrelation based on that, and logs if LogsToggle on. Requires NK - Toggles.
float SetRMRelation(ref char, int iNation, float rel)
{
	string sNation = "" + iNation;
	if(rel < REL_MIN) rel = REL_MIN;
	char.nations.(sNation).Relation = rel;

//find new stockrelation and set.
	if(sti(char.index) == GetMainCharacterIndex()) SetNationRelation2MainCharacter(iNation, GetRMRelationType(rel));

//log new relation
	if(LogsToggle) Log_SetStringToLog(XI_ConvertString(GetNationNameByType(iNation))+ " - " + XI_ConvertString(GetRMRelationName(char, iNation)) +"."); // PB: Fixes a dispay error
	return rel;
}


//changes RMRelation between char and iNation, calling setRMR.
float ChangeRMRelation(ref char, int iNation, float relch)
{
	string sNation = "" + iNation;
// KK -->
	if (ENABLE_FLAGS == 0) {
		relch += GetRMRelation(char, iNation);
	} else {
		relch += GetActualRMRelation(iNation);
	}
// <-- KK
	if(relch < REL_MIN) relch = REL_MIN;
	char.nations.(sNation).Relation = relch;
// KK -->
	if (ENABLE_FLAGS == 1) {
		SetActualRMRelation(iNation, relch);

		if (relch < 0)
			LooseLetterOfMarque(iNation);
	}
// <-- KK

//find new stockrelation and set.
	if(sti(char.index) == GetMainCharacterIndex()) SetNationRelation2MainCharacter(iNation, GetRMRelationType(relch));

//log new relation
	//int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	if(LogsToggle) Log_SetStringToLog(XI_ConvertString(GetNationNameByType(iNation))+ " - " + TranslateString("",GetRMRelationName(char, iNation)) + ".");
	//LanguageCloseFile(tmpLangFileID);
	return relch;	
}

// 04-12-06 added
int RMGetHighestRelation(ref char)
{
	int bestnat = PIRATE;
	float tpoints = REL_MIN;
// KK -->
	int i;
	if (ENABLE_FLAGS == 0) {
		for(i = 0; i < MAX_NATIONS; i++) { if(GetRMRelation(&char, i) > tpoints) { tpoints = GetRMRelation(&char, i); bestnat = i; } }
	} else {
		for(i = 0; i < MAX_NATIONS; i++) { if(GetActualRMRelation(i) > tpoints) { tpoints = GetActualRMRelation(i); bestnat = i; } }
	}
// <-- KK
	if(tpoints < REL_AMNESTY) bestnat = PIRATE;
	return bestnat;
}


//gets cost for amnesty
int RMAmnestyCost(ref PChar, int iNation)
{
// KK -->
	if (ENABLE_FLAGS == 0) {
		return makeint(sqrt(stf(PChar.rank)) * makefloat(GetRMRelation(PChar, iNation)) / stf(PChar.reputation) * 15000.0 / 100.0) * 100;
	} else {
		return makeint(sqrt(stf(PChar.rank)) * makefloat(GetActualRMRelation(iNation)) / stf(PChar.reputation) * 15000.0 / 100.0) * 100;
	}
// <-- KK
}


//sets amnesty with iNation
void RMAmnesty(ref Pchar, int iNation)
{
	//use pchar at the moment
	Pchar = GetMainCharacter();
	SetRMRelation(&Pchar, iNation, REL_AMNESTY);
// KK -->
	if (ENABLE_FLAGS == 1)
		SetActualRMRelation(iNation, REL_AMNESTY);
// <-- KK
	if(GetSoldiersName(iNation) != "")
	{
		string PeaceGroup = GetSoldiersName(iNation) + "_SOLDIERS";
		LAi_group_SetRelation(PeaceGroup, LAI_GROUP_PLAYER, LAI_GROUP_FRIEND);
		//SetRMKills(&Pchar, iNation, 0);
	}
	switch(iNation)
	{
		case ENGLAND:
			SetCharacterRelationBoth(GetCharacterIndex("Greenford Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
			SetCharacterRelationBoth(GetCharacterIndex("Redmond Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
			if(sti(Characters[GetCharacterIndex("Oxbay Commander")].nation) == ENGLAND)
				SetCharacterRelationBoth(GetCharacterIndex("Oxbay Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
		break;

		case FRANCE:
			SetCharacterRelationBoth(GetCharacterIndex("FalaiseDeFleur Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
			if(sti(Characters[GetCharacterIndex("Oxbay Commander")].nation) == FRANCE)
				SetCharacterRelationBoth(GetCharacterIndex("Oxbay Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
		break;

		case SPAIN:
			SetCharacterRelationBoth(GetCharacterIndex("IslaMuelle Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
		break;

		case HOLLAND:
			SetCharacterRelationBoth(GetCharacterIndex("Douwesen Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
		break;

		case PORTUGAL:
			SetCharacterRelationBoth(GetCharacterIndex("Conceicao Commander"),GetMainCharacterIndex(),RELATION_FRIEND);
		break;
	}
	UpdateRelations();
}


//gets RMRelation between char and gov.
float GetGovRelation(ref char, ref gov)
{
	float rel = 0.0;
	string id = gov.id;
	if(CheckAttribute(char,"GovRelations."+id))
		{ rel = stf(char.GovRelations.(id)); }
	return rel;
}


//sets RMRelation bgetween char and gov to rel.
float SetGovRelation(ref char, ref gov, float rel)
{
	string id = gov.id;
	if(rel < REL_MIN) rel = REL_MIN;
	char.GovRelations.(id) = rel;
	return rel;
}


//changes RMRelation between char and gov by relch.
float ChangeGovRelation(ref char, ref gov, float relch)
{
	string id = gov.id;
	relch += GetGovRelation(char, gov);
	if(relch < REL_MIN) relch = REL_MIN;
	char.GovRelations.(id) = relch;
	return relch;
}


//gets sum RMRelation between char, and gov /and gov's nation/,
//used to find if forts fire.
float GetRMRelationWithGov(ref char, ref gov)
{
// KK -->
	if (ENABLE_FLAGS == 0) {
		return GetRMRelation(char, sti(gov.nation)) + GetGovRelation(char, gov);
	} else {
		return GetActualRMRelation(sti(gov.nation)) + GetGovRelation(char, gov);
	}
// <-- KK
}


//find if fort should be hostile, based on char, and fortchar's governor.
// KK Never used anyway
int GovHostileCheck(ref char, ref fortchar)
{
// KK -->
	if (CheckAttribute(fortchar, "town")) {
		int govidx = GetTownGovernorIndex(fortchar.town);
		if (govidx > -1) return GetRMRelationType(GetRMRelationWithGov(char, GetCharacter(govidx)));
//	if(CheckAttribute(fortchar,"gov")) { return GetRMRelationType(GetRMRelationWithGov(char, CharacterFromID(fortchar.gov))); }
	}
	if (ENABLE_FLAGS == 0)
		return GetRMRelationType(GetRMRelation(char, sti(fortchar.nation)));
	else
		return GetRMRelationType(GetActualRMRelation(sti(fortchar.nation)));
// <-- KK
}


/*NEW PROPERTY: in object Islands, for each Islands[n], where n is an inhabited island,
add property towns
add property towns.quantity = num towns on island
add property towns.i (where i is the number of the town) = town id.*/

//run this whenever friendly or neutral thing of iNation is /attacked/ by char.
float AttackRMRelation(ref char, int iNation)
{
	//init vars
	ref island;
	int govid[1];
	string townid[1];
	string tn;
	int tqty = 0;
	int govqty = 0;
	//ref gov;
	int i = 0;

	//find if near island and if so, get all govs on island.
	/*if(bIslandLoaded)
	{
		island = GetIslandByID(char.location);
		if(CheckAttribute(island,"towns.quantity")) tqty = sti(island.towns.quantity);
		if(tqty)
		{
			SetArraySize(&townid, tqty);
			string gstr;
			for(i = 1; i <= tqty; i++)
			{
				tn = i;
				townid[i] = island.towns.(tn);
				gstr = GetTownGovernorID(townid[i]);
				govqty++;
				SetArraySize(&govid, govqty);
				govid[govqty-1] = gstr;
			}
		}
		if(govqty)
		{
			// nothing yet
		}
	}*/

// KK -->
	//get current rel
	float rel;
	if (ENABLE_FLAGS == 0) {
		rel = GetRMRelation(char, iNation);
	} else {
		rel = GetActualRMRelation(iNation);
	}
// <-- KK
	bool bad = false;
	
	//set rmrel for iNation to afterattack.
	//note that REPCH_FRIENDLY is in /addition/ to RCH_neut.
	if(rel > REL_NEUTRAL)
	{
		ChangeCharacterReputation(&char, REPCH_FRIENDLY);
		if(sti(char.index) == GetMainCharacterIndex())
		{
			bad = true;
		}
	}
	if(rel > REL_AFTERATTACK)
	{
		SetRMRelation(&char, iNation, REL_AFTERATTACK);
// KK -->
		if (ENABLE_FLAGS == 1) 
			SetActualRMRelation(iNation, REL_AFTERATTACK);
// <-- KK
		ChangeCharacterReputation(&char, REPCH_NEUTRAL);
	}
	LeaveService(&char, iNation, bad)
	//reset points for each gov if each gov is of iNation.
	/*if(govqty)
	{
		for(i = 0; i < govqty; i++)
		{
			ref gov = characterFromID(govid[i]);
			if(sti(gov.nation) == iNation && GetGovRelation(char, gov) > REL_NEUTRAL) SetGovRelation(&char, &gov, REL_NEUTRAL);
		}
	}*/
	return GetRMRelation(&char, iNation); // NK 04-09-20 func must return something, it's a float. Oops!
}

/* Explanation:
this changes relations based on an event (sink/take ship, sack/take town, etc.).
It adds points to all nations hostile to iNation (extra points if you have LoM)
and to all governors of those hostile nations from island if near island.*/
float UpdateRMRelation(ref char, int iNation, float fPoints)
{
//init vars
	//ref island;
	//string govid[1];
	int govqty = 0;
	//ref gov;
	int i = 0;

//find if near island and if so, get all govs on island.
	/*if(bIslandLoaded)
	{
		island = GetIslandByID(char.location);
		if(CheckAttribute(island,"towns.gov.quantity")) govqty = sti(island.towns.gov.quantity);
		if(govqty)
		{
			SetArraySize(govid, govqty);
			for(i = 0; i < govqty; i++) govid[i] = island.towns.gov.("gov"+i);
		}
	}*/

// KK -->
	//get current rel
	float rel;
	if (ENABLE_FLAGS == 0) {
		rel = GetRMRelation(char, iNation);
	} else {
		rel = GetActualRMRelation(iNation);
	}
// <-- KK

	//for govs
	float pirmult = 1.0;
	if(iNation == PIRATE) pirmult = 1.5;

	//Add points to char's nationrelations for each nation hostile to iNation;
	//if no LoM add only 1/4. If only neutral to that nation add only 1/8.
	//if near governors, add points in same way as above; with bonus if points were from attacking pirate.
	for(i = 0; i < MAX_NATIONS; i++)
	{
		if(GetNationRelation(i, iNation) == RELATION_ENEMY)
		{
// KK -->
			if (ENABLE_FLAGS == 0) {
				if(GetRMRelation(char, i) > REL_WAR && GetRMRelation(char, i) + (fPoints * NEUTRAL_POINTS) < REL_NEUTRAL)
				{
					ChangeRMRelation(&char, i, (fPoints * NEUTRAL_POINTS));
					/*if(govqty)
					{
						for(i = 0; i < govqty; i++)
						{
							ref gov = characterFromID(govid[i]);
							if(sti(gov.nation) == i)
							{
								ChangeGovRelation(&char, &gov, (fPoints * NEUTRAL_POINTS*pirmult));
							}
						}
					}*/
				}
				else
				{
					if(GetRMRelation(char, i) >= REL_NEUTRAL)
					{
						ChangeRMRelation(&char, i, (fPoints * FULL_POINTS));
						/*if(govqty)
						{
							for(i = 0; i < govqty; i++)
							{
								ref gov = characterFromID(govid[i]);
								if(sti(gov.nation) == i)
								{
									ChangeGovRelation(&char, &gov, (fPoints * FULL_POINTS *pirmult));
								}
							}
						}*/
					}

				}
			} else {
				if(GetActualRMRelation(i) > REL_WAR && GetActualRMRelation(i) + (fPoints * NEUTRAL_POINTS) < REL_NEUTRAL)
				{
					ChangeRMRelation(&char, i, (fPoints * NEUTRAL_POINTS));
					/*if(govqty)
					{
						for(i = 0; i < govqty; i++)
						{
							ref gov = characterFromID(govid[i]);
							if(sti(gov.nation) == i)
							{
								ChangeGovRelation(&char, &gov, (fPoints * NEUTRAL_POINTS*pirmult));
							}
						}
					}*/
				}
				else
				{
					if(GetActualRMRelation(i) >= REL_NEUTRAL)
					{
						ChangeRMRelation(&char, i, (fPoints * FULL_POINTS));
						/*if(govqty)
						{
							for(i = 0; i < govqty; i++)
							{
								ref gov = characterFromID(govid[i]);
								if(sti(gov.nation) == i)
								{
									ChangeGovRelation(&char, &gov, (fPoints * FULL_POINTS *pirmult));
								}
							}
						}*/
					}

				}
			}
// <-- KK
		}
	}

	//subtract points from attacked nation
	if(rel > REL_WAR) ChangeRMRelation(&char, iNation, -(sqrt(fPoints) * NEUTRAL_MULT));
	else ChangeRMRelation(&char, iNation, -fPoints);

	//subtract points from each gov if each gov is of iNation.
	/*if(govqty)
	{
		for(i = 0; i < govqty; i++)
		{
			ref gov = characterFromID(govid[i]);
			if(sti(gov.nation) == iNation)
			{
				ChangeGovRelation(&char, &gov, -sqrt(fPoints));
			}
		}
	}*/

	//return new RMRelation
	return GetRMRelation(char, iNation);
}

// soldiers killed section
// used to see if soldiers actually should be hostile or not.
int GetRMKills(ref chr, int nat)
{
	int num = -1;
	string snat = nat;
	if(CheckAttribute(chr,"nation."+snat+".kills")) { num = sti(chr.nations.(snat).kills); }
	//else { chr.nations.(snat).kills = 0; }
	return num;
}

int SetRMKills(ref chr, int nat, int kills)
{
	int old = GetRMKills(&chr, nat);
	string snat = nat;
	chr.nations.(snat).kills = kills;
	DebugLog((kills) + " " + GetNationDescByType(nat) + " " + XI_ConvertString("soldiers killed") + ".");
	return old;
}

int ChangeRMKills(ref chr, int nat, int kills)
{
	string snat = nat;
	int old = GetRMKills(&chr, nat);
	if(old == -1) old = 0;
	old += kills;
	chr.nations.(snat).kills = kills;
	return kills;
}

string GetRMKillRelation(ref chr, int nat)
{
	if(GetRMKills(&chr, nat) >= RM_GROUP_MINKILLS) return LAI_GROUP_ENEMY;
	return LAI_GROUP_NEITRAL;
}

void SetAllSoldierGroups(ref chr)
{
	string groupname;
	for(int i = 0; i < MAX_NATIONS; i++)
	{
		groupname = GetSoldiersName(i);
		if(groupname == "") continue;
		LAi_group_SetRelation(groupname + "_SOLDIERS", LAI_GROUP_PLAYER, GetRMKillRelation(&chr, i));
		DebugLog("setting " + groupname + " to " + GetRMKillRelation(&chr, i));
	}
}

// KK -->
int isSoldier(ref ch)
{
	if (!TestRef(ch)) return -1;
	if (CheckAttribute(ch, "isSoldier") == true && sti(ch.isSoldier) == true) {
		if (CheckAttribute(ch, "nation")) return sti(ch.nation);
		if (CheckAttribute(ch, "location")) return GetLocationNationFromID(ch.location);
		return GetCurrentLocationNation();
	}
	if (!CheckAttribute(ch, "chr_ai.group")) return -1;
	string group = ch.chr_ai.group;
	if (group == LAI_GROUP_GUARDS || group == LAI_GROUP_PATROL) {
		if (CheckAttribute(ch, "nation")) return sti(ch.nation);
		if (CheckAttribute(ch, "location")) return GetLocationNationFromID(ch.location);
		return GetCurrentLocationNation();
	}
	if (!HasSubStr(group, "_SOLDIERS")) return -1;
	if (group == "PERSONAL_SOLDIERS") return PERSONAL_NATION;
	for(int i = 0; i < MAX_NATIONS; i++)
	{
		if(i == PIRATE) continue;
		if(GetSoldiersName(i) + "_SOLDIERS" == group) {
			if (CheckAttribute(ch, "nation")) return sti(ch.nation);
			if (CheckAttribute(ch, "location")) return GetLocationNationFromID(ch.location);
			return GetCurrentLocationNation();
		}
	}
	return -1;
}
// <-- KK

bool AddOneSoldier(string group)
{
	if(group == "error" || group == "") return false;
	int nat = isSoldier(group);
	if(nat == -1) return false;
	ChangeRMKills(GetMainCharacter(), nat, 1);
}
// RM <--

// KK -->
int ServedNation;
float ActualRMRelations[MAX_NATIONS];
float FlagRMRelations[MAX_NATIONS];
int iForceDetectionFalseFlag = 0;

void SetServedNation(int iNation)
{
	ref PChar = GetMainCharacter();

	ServedNation = iNation;

	ActualRMRelations[ENGLAND] = GetRMRelation(PChar, ENGLAND);
	ActualRMRelations[FRANCE] = GetRMRelation(PChar, FRANCE);
	ActualRMRelations[SPAIN] = GetRMRelation(PChar, SPAIN);
	ActualRMRelations[PIRATE] = GetRMRelation(PChar, PIRATE);
	ActualRMRelations[HOLLAND] = GetRMRelation(PChar, HOLLAND);
	ActualRMRelations[PORTUGAL] = GetRMRelation(PChar, PORTUGAL);

	FlagRMRelations[ENGLAND] = ActualRMRelations[ENGLAND];
	FlagRMRelations[FRANCE] = ActualRMRelations[FRANCE];
	FlagRMRelations[SPAIN] = ActualRMRelations[SPAIN];
	FlagRMRelations[PIRATE] = ActualRMRelations[PIRATE];
	FlagRMRelations[HOLLAND] = ActualRMRelations[HOLLAND];
	FlagRMRelations[PORTUGAL] = ActualRMRelations[PORTUGAL];
}

int GetServedNation()
{
	return ServedNation;
}

float GetActualRMRelation(int iNation)
{
	if ((iNation >= 0) && (iNation < MAX_NATIONS))
		return ActualRMRelations[iNation];
	return REL_NEUTRAL;
}

string GetActualRMRelationName(ref char, int iNation)
{
	float rel = GetActualRMRelation(iNation);
	if(rel <= REL_WAR) return "Hostile";
	if(rel <= REL_AFTERATTACK) return "Wary"; //right now only this gets <=
	if(rel < REL_NEUTRAL) return "Neutral";
	if(!GetRank(char, iNation) && !HaveLetterOfMarque(iNation)) return "Friendly";
	if(!GetRank(char, iNation) && HaveLetterOfMarque(iNation)) return "Letter of Marque";
	return GetRankName(char, iNation);
}

void SetActualRMRelation(int iNation, float rel)
{
	if ((iNation >= 0) && (iNation < MAX_NATIONS))
		ActualRMRelations[iNation] = rel;
}

int GetActualNationRelation2MainCharacter(int iNation)
{
	float rmrel;
	int rel;

	rmrel = GetActualRMRelation(iNation);
	rel = GetRMRelationType(rmrel);
	return rel;
}

float GetFlagRMRelation(int iNation)
{
	if (iNation >= 0 && iNation < MAX_NATIONS)
		return FlagRMRelations[iNation];
	return REL_NEUTRAL;
}

void SetFlagRMRelation(int iNation, float rel)
{
	if ((iNation >= 0) && (iNation < MAX_NATIONS))
		FlagRMRelations[iNation] = rel;
}

float RelationToRMRelation(int rel)
{
	switch (rel) {
		case RELATION_ENEMY:
			return REL_WAR;
		break;
		case RELATION_NEUTRAL:
			return REL_AMNESTY;
		break;
		case RELATION_FRIEND:
			return REL_NEUTRAL-1;
		break;
	}
}

void ReceiveLetterOfMarque(int iNation)
{
	ref PChar = GetMainCharacter();
	string sNation = ""+iNation;
	aref arTmp;
	if ((iNation >= 0) && (iNation < MAX_NATIONS)) {
		makearef(arTmp, PChar.nations.(sNation));
		arTmp.LoM = "1";
	}
}

void LooseLetterOfMarque(int iNation)
{
	ref PChar = GetMainCharacter();
	string sNation = ""+iNation;
	if ((iNation >= 0) && (iNation < MAX_NATIONS))
		DeleteAttribute(PChar, "nations."+sNation+".LoM");
}

bool HaveLetterOfMarque(int iNation)
{
	ref PChar = GetMainCharacter();
	string sNation = ""+iNation;
	if ((iNation >= 0) && (iNation < MAX_NATIONS))
		return CheckAttribute(PChar, "nations."+sNation+".LoM");
	return false;
}

int GetCurrentFlag()
{
  return sti(characters[GetMainCharacterIndex()].nation);
}

void HoistFlag(int iNation)
{
	int rel;

	switch (iNation) {
		case ENGLAND:
			SetFlagRMRelation(ENGLAND, REL_NEUTRAL-1.);
			rel = GetNationRelation(ENGLAND, FRANCE);
			SetFlagRMRelation(FRANCE, RelationToRMRelation(rel));
			rel = GetNationRelation(ENGLAND, SPAIN);
			SetFlagRMRelation(SPAIN, RelationToRMRelation(rel));
			rel = GetNationRelation(ENGLAND, PIRATE);
			SetFlagRMRelation(PIRATE, RelationToRMRelation(rel));
			rel = GetNationRelation(ENGLAND, HOLLAND);
			SetFlagRMRelation(HOLLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(ENGLAND, PORTUGAL);
			SetFlagRMRelation(PORTUGAL, RelationToRMRelation(rel));
		break;
		case FRANCE:
			rel = GetNationRelation(FRANCE, ENGLAND);
			SetFlagRMRelation(ENGLAND, RelationToRMRelation(rel));
			SetFlagRMRelation(FRANCE, REL_NEUTRAL-1.);
			rel = GetNationRelation(FRANCE, SPAIN);
			SetFlagRMRelation(SPAIN, RelationToRMRelation(rel));
			rel = GetNationRelation(FRANCE, PIRATE);
			SetFlagRMRelation(PIRATE, RelationToRMRelation(rel));
			rel = GetNationRelation(FRANCE, HOLLAND);
			SetFlagRMRelation(HOLLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(FRANCE, PORTUGAL);
			SetFlagRMRelation(PORTUGAL, RelationToRMRelation(rel));
		break;
		case SPAIN:
			rel = GetNationRelation(SPAIN, ENGLAND);
			SetFlagRMRelation(ENGLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(SPAIN, FRANCE);
			SetFlagRMRelation(FRANCE, RelationToRMRelation(rel));
			SetFlagRMRelation(SPAIN, REL_NEUTRAL-1.);
			rel = GetNationRelation(SPAIN, PIRATE);
			SetFlagRMRelation(PIRATE, RelationToRMRelation(rel));
			rel = GetNationRelation(SPAIN, HOLLAND);
			SetFlagRMRelation(HOLLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(SPAIN, PORTUGAL);
			SetFlagRMRelation(PORTUGAL, RelationToRMRelation(rel));
		break;
		case PIRATE:
			rel = GetNationRelation(PIRATE, ENGLAND);
			SetFlagRMRelation(ENGLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(PIRATE, FRANCE);
			SetFlagRMRelation(FRANCE, RelationToRMRelation(rel));
			rel = GetNationRelation(PIRATE, SPAIN);
			SetFlagRMRelation(SPAIN, RelationToRMRelation(rel));
			SetFlagRMRelation(PIRATE, REL_NEUTRAL-1.);
			rel = GetNationRelation(PIRATE, HOLLAND);
			SetFlagRMRelation(HOLLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(PIRATE, PORTUGAL);
			SetFlagRMRelation(PORTUGAL, RelationToRMRelation(rel));
		break;
		case HOLLAND:
			rel = GetNationRelation(HOLLAND, ENGLAND);
			SetFlagRMRelation(ENGLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(HOLLAND, FRANCE);
			SetFlagRMRelation(FRANCE, RelationToRMRelation(rel));
			rel = GetNationRelation(HOLLAND, SPAIN);
			SetFlagRMRelation(SPAIN, RelationToRMRelation(rel));
			rel = GetNationRelation(HOLLAND, PIRATE);
			SetFlagRMRelation(PIRATE, RelationToRMRelation(rel));
			SetFlagRMRelation(HOLLAND, REL_NEUTRAL-1.);
			rel = GetNationRelation(HOLLAND, PORTUGAL);
			SetFlagRMRelation(PORTUGAL, RelationToRMRelation(rel));
		break;
		case PORTUGAL:
			rel = GetNationRelation(PORTUGAL, ENGLAND);
			SetFlagRMRelation(ENGLAND, RelationToRMRelation(rel));
			rel = GetNationRelation(PORTUGAL, FRANCE);
			SetFlagRMRelation(FRANCE, RelationToRMRelation(rel));
			rel = GetNationRelation(PORTUGAL, SPAIN);
			SetFlagRMRelation(SPAIN, RelationToRMRelation(rel));
			rel = GetNationRelation(PORTUGAL, PIRATE);
			SetFlagRMRelation(PIRATE, RelationToRMRelation(rel));
			rel = GetNationRelation(PORTUGAL, HOLLAND);
			SetFlagRMRelation(HOLLAND, RelationToRMRelation(rel));
			SetFlagRMRelation(PORTUGAL, REL_NEUTRAL-1.);
		break;
		case PERSONAL_NATION:
			SetFlagRMRelation(ENGLAND, GetActualRMRelation(ENGLAND));
			SetFlagRMRelation(FRANCE, GetActualRMRelation(FRANCE));
			SetFlagRMRelation(SPAIN, GetActualRMRelation(SPAIN));
			SetFlagRMRelation(PIRATE, GetActualRMRelation(PIRATE));
			SetFlagRMRelation(HOLLAND, GetActualRMRelation(HOLLAND));
			SetFlagRMRelation(PORTUGAL, GetActualRMRelation(PORTUGAL));
		break;
	}
}

void SetRelationsForFlag(int iNation)
{
	ref PChar = GetMainCharacter();
	float rel;

	rel = GetFlagRMRelation(ENGLAND);
	SetRMRelation(PChar, ENGLAND, rel);
	SetNationRelation2MainCharacter(ENGLAND, GetRMRelationType(rel));
	rel = GetFlagRMRelation(FRANCE);
	SetRMRelation(PChar, FRANCE, rel);
	SetNationRelation2MainCharacter(FRANCE, GetRMRelationType(rel));
	rel = GetFlagRMRelation(SPAIN);
	SetRMRelation(PChar, SPAIN, rel);
	SetNationRelation2MainCharacter(SPAIN, GetRMRelationType(rel));
	rel = GetFlagRMRelation(PIRATE);
	SetRMRelation(PChar, PIRATE, rel);
	SetNationRelation2MainCharacter(PIRATE, GetRMRelationType(rel));
	rel = GetFlagRMRelation(HOLLAND);
	SetRMRelation(PChar, HOLLAND, rel);
	SetNationRelation2MainCharacter(HOLLAND, GetRMRelationType(rel));
	rel = GetFlagRMRelation(PORTUGAL);
	SetRMRelation(PChar, PORTUGAL, rel);
	SetNationRelation2MainCharacter(PORTUGAL, GetRMRelationType(rel));

	if (iNation != PERSONAL_NATION) {
		if (HaveLetterOfMarque(iNation)) {
			SetRMRelation(PChar, iNation, GetActualRMRelation(iNation));
			SetNationRelation2MainCharacter(iNation, RELATION_FRIEND);
		}
	} else {
		if (HaveLetterOfMarque(ENGLAND)) {
			SetRMRelation(PChar, ENGLAND, GetActualRMRelation(ENGLAND));
			SetNationRelation2MainCharacter(ENGLAND, RELATION_FRIEND);
		}
		if (HaveLetterOfMarque(FRANCE)) {
			SetRMRelation(PChar, FRANCE, GetActualRMRelation(FRANCE));
			SetNationRelation2MainCharacter(FRANCE, RELATION_FRIEND);
		}
		if (HaveLetterOfMarque(SPAIN)) {
			SetRMRelation(PChar, SPAIN, GetActualRMRelation(SPAIN));
			SetNationRelation2MainCharacter(SPAIN, RELATION_FRIEND);
		}
		if (HaveLetterOfMarque(HOLLAND)) {
			SetRMRelation(PChar, HOLLAND, GetActualRMRelation(HOLLAND));
			SetNationRelation2MainCharacter(HOLLAND, RELATION_FRIEND);
		}
		if (HaveLetterOfMarque(PORTUGAL)) {
			SetRMRelation(PChar, PORTUGAL, GetActualRMRelation(PORTUGAL));
			SetNationRelation2MainCharacter(PORTUGAL, RELATION_FRIEND);
		}
	}

	UpdateRelations();
}

float GetChanceDetectFalseFlag()
{
// original code -->
//	float rank = GetRankFromPoints(GetScore(GetMainCharacter()));
//	return CHANCE_DETECT_FALSE_FLAG_BASE+(CHANCE_DETECT_FALSE_FLAG_MAX-CHANCE_DETECT_FALSE_FLAG_BASE)*rank/MAX_RANK;
// original code <--

// LDH -->
	ref mchr = GetMainCharacter(); // KK
	float score  = GetScore(mchr); // KK
	float rank   = GetRankFromPoints(score);
	int   sneak  = GetSummonSkillFromName(mchr, SKILL_SNEAK);
	int difficulty = GetDifficulty();
	float chance = rank/MAX_RANK * (11.0-sneak)/10.0;
	chance = chance * difficulty/2.0;		// 0.5, 1.0, 1.5, 2.0
//	chance = chance * (difficulty+1)/3.0;		// alternate difficulty calculation 0.67, 1,0, 1.33, 1.67
	chance = retmin(chance,1.0);	// so chance doesn't go over 100%
//	LogIt("False Flag Detection - Score: " + score + ", rank: " + rank + ", Sneak: " + sneak + ", Chance: " + chance*100.0 + "%"); // for testing
//	LogIt("The chance of your false flag being detected is " + chance*100.0 + "%");	// Tell the player, it might get him used to looking for it.
	return chance;
// LDH <--
}

void SetNationRelation2Blaze(int iNation, int rel)
{
	if (iNation >= 0 && iNation < MAX_NATIONS) {
		SetNationRelation2MainCharacter(iNation, rel);
		SetActualRMRelation(iNation, RelationToRMRelation(rel));
		UpdateRelations();
	}
}

string GetSoldiersDialog(int nation, string town)
{
	string dialogfile = "";
	switch(nation) {
		case ENGLAND:
			switch (town) {
				case "Redmond":
					dialogfile = "Redmond";
				break;
				case "Oxbay":
					dialogfile = "Oxbay";
				break;
				case "Greenford":
					dialogfile = "Greenford";
				break;

				case "Charlestown":
					dialogfile = "Oxbay";
				break;
			}
		break;
		case FRANCE:
			dialogfile = "Falaise De Fleur";
		break;
		case SPAIN:
			dialogfile = "Isla Muelle";
		break;
		case HOLLAND:
			dialogfile = "Douwesen";
		break;
		case PORTUGAL:
			dialogfile = "Conceicao";
		break;
		case PERSONAL_NATION:
			dialogfile = "Personal";
		break;
		// default:
			dialogfile = GetNationNameByType(nation);
	}
	return dialogfile + " Soldier_dialog.c";
}

string GetSoldiersGreeting(int nation, string town)
{
	string greeting = "";
	switch(nation) {
		case ENGLAND:
			switch (town) {
				case "Redmond":
					greeting = "Redmond";
				break;
				case "Oxbay":
					greeting = "Oxbay";
				break;
				case "Greenford":
					greeting = "Greenford";
				break;
				case "Charlestown":
					greeting = "Charlestown";
				break;
			}
		break;
		case FRANCE:
			greeting = "Falaise De Fleur";
		break;
		case SPAIN:
			greeting = "Isla Muelle";
		break;
		case HOLLAND:
			greeting = "Douwesen";
		break;
		case PORTUGAL:
			greeting = "Conceicao";
		break;
		case PERSONAL_NATION:
			greeting = "Redmond";
		break;
		// default:
			greeting = GetNationNameByType(nation);
	}
	return "Gr_" + greeting + " Soldier";
}

string GetCitizensName(int nation)
{
	switch(nation)
	{
		case ENGLAND: return "ENGLAND"; break;
		case FRANCE: return "FRANCE"; break;
		case SPAIN: return "SPAIN"; break;
		case HOLLAND: return "DOUWESEN"; break;
		case PORTUGAL: return "CONCEICAO"; break;
		case PERSONAL_NATION: return "PERSONAL"; break;
	}
	return "";
}

void InitRelations()
{
	string initfile = "";
	string slpath = GetStorylineDir(FindCurrentStoryline());
	if (FindFile("PROGRAM\" + slpath + "Nations", "*.c", "relations_init.c") != "") {
		initfile = slpath + "Nations\relations_init.c";
		if (LoadSegment(initfile)) {
			relations_init();
			UnloadSegment(initfile);
		}
	} else {
		initfile = "Nations\relations_init.c";
		if (LoadSegment(initfile)) {
			relations_init();
			UnloadSegment(initfile);
		}
	}
}

void InitGroups()
{
	LAi_group_SetRelation("CONCEICAO_SOLDIERS", "CONCEICAO_CITIZENS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("CONCEICAO_SOLDIERS", "CONCEICAO_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("CONCEICAO_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetAlarmReaction("CONCEICAO_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetAlarmReaction("CONCEICAO_SOLDIERS", "CONCEICAO_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("CONCEICAO_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("CONCEICAO_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("CONCEICAO_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetPriority("CONCEICAO_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("CONCEICAO_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("CONCEICAO_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("CONCEICAO_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("CONCEICAO_CITIZENS", LAI_GROUP_DEF_SAY);

	LAi_group_SetAlarmReaction("SMUGGLERS_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("SMUGGLERS_SOLDIERS", "SMUGGLERS_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("SMUGGLERS_CITIZENS", "SMUGGLERS_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("SMUGGLERS_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("SMUGGLERS_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("SMUGGLERS_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("SMUGGLERS_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("SMUGGLERS_SOLDIERS", "SMUGGLERS_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("SMUGGLERS_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("SMUGGLERS_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("SMUGGLERS_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("SMUGGLERS_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("SMUGGLERS_CITIZENS", LAI_GROUP_DEF_SAY);
	
	LAi_group_SetAlarmReaction("DOUWESEN_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("DOUWESEN_CITIZENS", "DOUWESEN_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("DOUWESEN_SOLDIERS", "DOUWESEN_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("DOUWESEN_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("DOUWESEN_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("DOUWESEN_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("DOUWESEN_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("DOUWESEN_SOLDIERS", "DOUWESEN_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("DOUWESEN_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("DOUWESEN_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("DOUWESEN_SOLDIERS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("DOUWESEN_SOLDIERS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("DOUWESEN_SOLDIERS", LAI_GROUP_DEF_SAY);

	LAi_group_SetAlarmReaction("DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("DOUWESEN_PIRATE_SOLDIERS", "DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("DOUWESEN_PIRATE_CITIZENS", "DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("DOUWESEN_PIRATE_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("DOUWESEN_PIRATE_SOLDIERS", "DOUWESEN_PIRATE_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("DOUWESEN_PIRATE_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("DOUWESEN_PIRATE_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("DOUWESEN_PIRATE_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("DOUWESEN_PIRATE_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("DOUWESEN_PIRATE_CITIZENS", LAI_GROUP_DEF_SAY);

	LAi_group_SetAlarmReaction("FRANCE_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("FRANCE_CITIZENS", "FRANCE_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("FRANCE_SOLDIERS", "FRANCE_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("FRANCE_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("FRANCE_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("FRANCE_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("FRANCE_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("FRANCE_SOLDIERS", "FRANCE_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("FRANCE_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("FRANCE_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("FRANCE_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("FRANCE_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("FRANCE_CITIZENS", LAI_GROUP_DEF_SAY);

	LAi_group_SetAlarmReaction("SPAIN_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("SPAIN_CITIZENS", "SPAIN_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("SPAIN_SOLDIERS", "SPAIN_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("SPAIN_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("SPAIN_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("SPAIN_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("SPAIN_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("SPAIN_SOLDIERS", "SPAIN_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("SPAIN_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("SPAIN_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("SPAIN_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("SPAIN_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("SPAIN_CITIZENS", LAI_GROUP_DEF_SAY);

	LAi_group_SetAlarmReaction("ENGLAND_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("ENGLAND_CITIZENS", "ENGLAND_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("ENGLAND_SOLDIERS", "ENGLAND_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("ENGLAND_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("ENGLAND_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("ENGLAND_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("ENGLAND_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("ENGLAND_SOLDIERS", "ENGLAND_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("ENGLAND_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("ENGLAND_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("ENGLAND_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("ENGLAND_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("ENGLAND_CITIZENS", LAI_GROUP_DEF_SAY);

	LAi_group_SetAlarmReaction("QC_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY, LAI_GROUP_FRIEND);
	LAi_group_SetRelation("QC_CITIZENS", "QC_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("QC_SOLDIERS", "QC_SOLDIERS", LAI_GROUP_FRIEND);
	LAi_group_SetRelation("QC_SOLDIERS", LAI_DEFAULT_GROUP, LAI_GROUP_FRIEND);
	LAi_group_SetLookRadius("QC_SOLDIERS", LAI_GROUP_GRD_LOOK);
	LAi_group_SetHearRadius("QC_SOLDIERS", LAI_GROUP_GRD_HEAR);
	LAi_group_SetSayRadius("QC_SOLDIERS", LAI_GROUP_GRD_SAY);
	LAi_group_SetAlarmReaction("QC_SOLDIERS", "QC_CITIZENS", LAI_GROUP_FRIEND, LAI_GROUP_FRIEND);
	LAi_group_SetPriority("QC_CITIZENS", LAI_GROUP_PCITIZENS);
	LAi_group_SetPriority("QC_SOLDIERS", LAI_GROUP_PGUARDS);

	LAi_group_SetLookRadius("QC_CITIZENS", LAI_GROUP_DEF_LOOK);
	LAi_group_SetHearRadius("QC_CITIZENS", LAI_GROUP_DEF_HEAR);
	LAi_group_SetSayRadius("QC_CITIZENS", LAI_GROUP_DEF_SAY);

	string slpath = GetStorylineDir(FindCurrentStoryline());
	if (FindFile("PROGRAM\\" + slpath + "NATIONS", "*.c", "groups_init.c") != "") {
		if (LoadSegment(slpath + "NATIONS\groups_init.c")) {
			groups_init();
			UnloadSegment(slpath + "NATIONS\groups_init.c");
		}
	}
}
// <-- KK
