// NK DailyCrewUpdate functions

// NK moved defines to BuildSettings.h 05-03-19


//start main function
void DailyCrewUpdate()
{
	//int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	//trace("Gauging: NEXTDAY_DCU start");

//declare vars -->
	ref pchar = GetMainCharacter();
	int i,cn;
	ref chref;
// variables that are needed always
	int skillComm = GetSummonSkillFromName(pchar, SKILL_COMMERCE);
	int PerkComm=GetOfficersPerkUsing(pchar,"BasicCommerce")+GetOfficersPerkUsing(pchar,"AdvancedCommerce");
	int PerkBasicLand = GetOfficersPerkUsing(pchar,"BasicLandOwner");
	int skillLuck = GetSummonSkillFromName(pchar,SKILL_SNEAK);
	if(!CheckAttribute(pchar,"disableDCU")) // 05-04-17 add disableDCU check
	{
//variables that are needed by DCU
		int crewQ = GetSquadronCrewQuantity(pchar);
		bool CheckFood = FOOD_ON && CheckAttribute(pchar, "ship");
//		if(CheckFood) CheckFood = pchar.ship.type != "Tartane1" && pchar.ship.type != "Tartane2" && !CheckAttribute(pchar,"skipfood"); // both NK 04-11 to skip foodcheck // KK The crew doesn't eat when on tartane?

	//morale
	// for now, all companion ships take pchar's ship's morale.
		int morale = 45;
		if(CheckAttribute(pchar,"Ship.Crew.Morale")) morale = sti(pchar.Ship.Crew.Morale);
		int PerkIron = GetOfficersPerkUsing(pchar,"IronWill");
		int skillLead = GetSummonSkillFromName(pchar,SKILL_LEADERSHIP);

		float moralech = 0.0;
		float moralemod = ( (skillLead*2.0 + skillLuck + PerkIron*5.0)/30.0 / 4.0 * (1.0+(PerkIron/4.0)));
		//max: x1.36; min: x0.64
		float moralescale = 1.0;
		bool articles = false;
		if (CheckAttribute(pchar, "articles") == true && sti(pchar.articles) == true) articles = true;


	//JA 1Dec06 reworked Food & Rum system -->		
	//food
		int foodQ = GetSquadronGoods(pchar, GOOD_WHEAT);
		float foodRatio[4];
		int ft[4];
		ft[0]=0; ft[1] = 0; ft[2]=0; ft[3]=0;
		float foodused = 0;
		if(CheckFood) foodused = makefloat(crewQ) * FOOD_PER_CREW;
		float foodusedtemp = 0.0;
		if(CheckAttribute(pchar,"Crewstatus.foodused"))
		{ //Add leftover food requirement from yesterday.
			if(CheckFood) { foodusedtemp += stf(pchar.Crewstatus.foodused); }
			pchar.Crewstatus.foodused = 0.0;
		}
		int foodrations = 1; //this is a divisor
		if(CheckAttribute(pchar,"Crewstatus.foodrations")) foodrations = sti(pchar.Crewstatus.foodrations);
		int food_daysleft = 999;

		float rationedFoodConsumption;
		if(foodused==0) rationedFoodConsumption = 1; // avoid divide by zero
		else rationedFoodConsumption = foodused / makefloat(foodrations);
		if(CheckFood) food_daysleft = makeint((makefloat(foodQ) - foodusedtemp) / rationedFoodConsumption);

		if(CheckFood) { if((makefloat(foodQ) - foodusedtemp) / rationedFoodConsumption < 1) { food_daysleft = 0; } }

		int daysnofood = 0;
		if(CheckFood) { if(CheckAttribute(pchar, "Crewstatus.daysnofood")) { daysnofood = sti(pchar.Crewstatus.daysnofood); } }
		
		if(food_daysleft < 8 && food_daysleft > 0 && !LogsToggle) Log_SetStringToLog(TranslateString("","Captain, we have only") + " " + food_daysleft + " " + TranslateString("","days worth of food left!"));
		if(food_daysleft == 0 && !LogsToggle) Log_SetStringToLog(TranslateString("","Captain, we're out of food!"));

	//fill foodratio so we can track what ship had what food later.
		int iMostFoodShip = 0; //JA find the ship with the most food on it for handling special low food case later
		if(CheckFood && foodQ) // NK 04-09-09 divbyzero error fix
		{
			float iHighestFoodRatio = 0; //JA Need this to handle special case.
			for(i=0; i<4; i++)
			{
				cn = GetCompanionIndex(&pchar,i);
				if(cn!=-1 && GetRemovable(GetCharacter(cn))) { // KK -->
					foodRatio[i] = makefloat(GetCargoGoods(&Characters[cn],GOOD_WHEAT)) / makefloat(foodQ);
					if(foodRatio[i] > iHighestFoodRatio)
					{
						iHighestFoodRatio = foodRatio[i];
						iMostFoodShip = i;
					}
				}// <-- KK
			}
		}

	//rum
		int rumQ = GetSquadronGoods(pchar, GOOD_RUM);
		float rumRatio[4];
		int rt[4];
		rt[0]=0; rt[1] = 0; rt[2]=0; rt[3]=0;
		float rumused = 0;
		if(CheckFood) rumused = makefloat(crewQ) * FOOD_PER_CREW;
		float rumusedtemp = 0.0;
		if(CheckAttribute(pchar,"Crewstatus.rumused")) //Handle left over rum fraction used from yesterday.
		{
			if(CheckFood) { rumusedtemp += stf(pchar.Crewstatus.rumused); }
			pchar.Crewstatus.rumused = 0.0;
		}
		int rumrations = 2; //this is a divisor
		if(CheckAttribute(pchar,"Crewstatus.rumrations")) rumrations = sti(pchar.Crewstatus.rumrations);
		int rum_daysleft = 999;

		float rationedRumConsumption;
		if(rumused==0) rationedRumConsumption = 1;
		else rationedRumConsumption = rumused / makefloat(rumrations);
		if(CheckFood) rum_daysleft = makeint((makefloat(rumQ) - rumusedtemp) / rationedRumConsumption);
		
		// --> Swindler 05-10-10: fix for divide by zero error
        if (rumused > 0 && (makefloat(rumQ) - rumusedtemp) / rationedRumConsumption < 1) { rum_daysleft = 0; }

        // Swindler <--

		if(rum_daysleft < 8 && rum_daysleft > 0 && !LogsToggle) Log_SetStringToLog(TranslateString("","Captain, we have only") + " " + rum_daysleft + " " + TranslateString("","days worth of rum left!"));
		if(rum_daysleft == 0 && !LogsToggle) Log_SetStringToLog(TranslateString("","Captain, we're out of rum!"));

		int rum2chance = RUM_BASECH + ((skillLead/2 + skillLuck/2) * (PerkIron + 0.5));

	//fill rumratio so we can track what ship had what rum later.
		int iMostRumShip = 0; //JA find the ship with the most rum on it for handling special low rum case later
		if(CheckFood && rumQ) // NK 04-09-09 divbyzero error fix
		{
			float iHighestRumRatio = 0; //JA Need this to handle special case.
			for(i=0; i<4; i++)
			{
				cn = GetCompanionIndex(&pchar,i);
				if(cn!=-1 && GetRemovable(GetCharacter(cn))) { // KK -->
					rumRatio[i] = makefloat(GetCargoGoods(&Characters[cn],GOOD_RUM)) / makefloat(rumQ);
					if(rumRatio[i] > iHighestRumRatio)
					{
						iHighestRumRatio = rumRatio[i];
						iMostRumShip = i;
					}
				} // <-- KK
			}
		}


	//check food and deal with it.
		if(CheckFood)
		{
			foodusedtemp += rationedFoodConsumption; //Get total food to be eaten today
			
			//Handle fractional food requiements.
			//ignore small leftover food fractions
			if(round(foodusedtemp) - foodusedtemp <= 0.05 && round(foodusedtemp) - foodusedtemp >= -0.05) foodusedtemp = round(foodusedtemp);
			else //add extra food fraction to next day's food, and round down todays food.
			{
				pchar.Crewstatus.foodused = foodusedtemp - makefloat(makeint(foodusedtemp));
				foodusedtemp = makeint(foodusedtemp);
			}
			//Remove eaten food from total quantity


			foodQ -= foodusedtemp;

			if(foodQ <= 0)
			{
				foodQ = 0;
				pchar.Crewstatus.foodused = 0.0;
			}

			//divide remaining food back into ships
			for(i=0; i<4; i++)
			{
				cn = GetCompanionIndex(&pchar,i);
				if(cn!=-1) ft[i] = makeint(makefloat(foodQ)*foodRatio[i]);
			}
			if(ft[0]+ft[1]+ft[2]+ft[3] == 0 && foodQ > 0) //handle special case of trying to divide 1 or two food between a few ships
			{
				ft[iMostFoodShip] = 1; //put last remaning food on this ship;
			}

			//if food doesn't divide evenly:
			while(ft[0]+ft[1]+ft[2]+ft[3] != foodQ)
			{
				for(i=0; i<4; i++)
				{
					if(ft[i] > 0) //Ship had food
					{
						if(ft[0]+ft[1]+ft[2]+ft[3] > foodQ) //too much food alocated, so remove some
						{
							ft[i]--;
						}
						if(ft[0]+ft[1]+ft[2]+ft[3] < foodQ) //not enough food alocated, so add some
						{
							ft[i]++;
						}
					}
				}
			}
	//JA <-- 1Dec06 reworked Food & Rum system 

			for(i=0; i<4; i++)
			{
				cn = GetCompanionIndex(&pchar,i);
				if(cn!=-1) SetCharacterGoods(&Characters[cn],GOOD_WHEAT, ft[i]);
			}
			if(LogsToggle)
			{
				if(foodQ <= 0) Log_SetStringToLog(TranslateString("","Captain, we're out of food!"));
				else Log_SetStringToLog(TranslateString("","Food Qty") + ": " + makeint(foodQ) + ", " + TranslateString("","good for") + " " + food_daysleft + " " + XI_ConvertString("days."));
			}
			if(foodQ <= 0)
			{
				daysnofood++;
				moralescale *= (FOOD0_SCALE/(pow(daysnofood,1.25)/2));
				float tempdie = 1 - ((pow(daysnofood,1.5) + frand(pow(daysnofood,1.5)))/4.0 * FOOD_CREWDIEOFF * (1.0-(FRAND(skillLuck)/30.0)));
				if(tempdie < 0) tempdie = 0.0;
				if(GetCrewQuantity(pchar)<=makeint(GetMaxCrewQuantity(pchar)/4)) { KAM_Mutiny(); }//MAXIMUS: new "Mutiny"
				//Log_SetStringToLog("Dieratio: " + tempdie);
				//Log_SetStringToLog("No food! Scale down to " + moralescale);
				string tempded = GetSquadronCrewQuantity(pchar) - SetSquadronCrewQuantityRatio(pchar, tempdie);				
				if(LogsToggle)
				{
				  if(tempded < 1) Log_SetStringToLog(TranslateString("","Captain, we're out of crew!"));
				  else Log_SetStringToLog("" + tempded + " " + TranslateString("","crew have died from starvation."));
				}
			}
			else
			{
				daysnofood = 0;
				if(foodrations == 2)
				{
					moralescale *= FOOD2_SCALE;
					//Log_SetStringToLog("Scale down to " + moralescale);
				}
			}
		}
		pchar.Crewstatus.foodrations = foodrations;
		pchar.Crewstatus.daysnofood = daysnofood;

	//JA 1Dec06 reworked Food & Rum system -->		
		//check rum and deal with it.
		if(CheckFood)
		{
			rumusedtemp += rationedRumConsumption; //JA: total rum used today and rest of yesterday

			//Handle fractional rum requiements.
			//ignore small leftover rum fractions
			if(round(rumusedtemp) - rumusedtemp <= 0.05 && round(rumusedtemp) - rumusedtemp >= -0.05) rumusedtemp = round(rumusedtemp);
			else //add extra rum fraction to next day's rum, and round down todays rum.
			{
				pchar.Crewstatus.rumused = rumusedtemp - makefloat(makeint(rumusedtemp));
				rumusedtemp = makeint(rumusedtemp);
			}
			//Remove eaten rum from total quantity
			rumQ -= rumusedtemp;
			
			if(rumQ <= 0)
			{
				rumQ = 0;
				pchar.Crewstatus.rumused = 0.0
			}

			//divide remaining rum back into ships
			for(i=0; i<4; i++)
			{
				cn = GetCompanionIndex(&pchar,i);
				if(cn!=-1) rt[i] = makeint(makefloat(rumQ)*rumRatio[i]);
			}

			if(rt[0]+rt[1]+rt[2]+rt[3] == 0 && rumQ > 0) //handle special case of trying to divide 1 or two rum between a few ships
			{
				rt[iMostRumShip] = 1; //put last remaning rum on this ship;
			}

			//if rum doesn't divide evenly:
			while(rt[0]+rt[1]+rt[2]+rt[3] != rumQ)
			{
				for(i=0; i<4; i++)
				{
					if(rt[i] > 0) //ship had rum
					{
						if(rt[0]+rt[1]+rt[2]+rt[3] > rumQ) //too much rum so remove one
						{
							rt[i]--;
						}
						if(rt[0]+rt[1]+rt[2]+rt[3] < rumQ) //not enough rum so add one
						{
							rt[i]++;
						}
					}
				}
			}
			SetCharacterGoods(&pchar, GOOD_RUM, rt[0]);
			for(i=1; i<4; i++)
			{
				cn = GetCompanionIndex(&pchar,i);
				if(cn!=-1) SetCharacterGoods(&Characters[cn],GOOD_RUM, rt[i]);
			}

	//JA <-- 1Dec06 reworked Food & Rum system 
			
			if(LogsToggle)
			{				
			    if(rumQ <= 0) Log_SetStringToLog(TranslateString("","Captain, we're out of rum!"));
				else Log_SetStringToLog(TranslateString("","Rum Qty") + ": " + makeint(rumQ) + ", " + TranslateString("","good for") + " " + rum_daysleft + " " + XI_ConvertString("days."));
			}

			if(rumQ <= 0)
			{
				moralescale *= RUM0_SCALE;
				//Log_SetStringToLog("No rum! Scale down to " + moralescale);
			}
			else
			{
				if(rumrations == 1)
				{
					if(!rand(rum2chance))
					{
						moralech += RUMDEC * (1.0-moralemod);
						Log_SetStringToLog(TranslateString("","Drunken brawl breaks out, morale plummets!")); // NK 05-03-19 added detail
					}
					else
					{
						moralescale *= RUM_SCALE;
					}
				}
				else
				{
					moralescale *= RUM2_SCALE;
				}
			}
		}
		pchar.Crewstatus.rumrations = rumrations;


	//loot
		float lastcsr = 1.0;
		if(CheckAttribute(pchar,"Crewstatus.lastcsr")) lastcsr = stf(pchar.CrewStatus.lastcsr);
		else pchar.CrewStatus.lastcsr = lastcsr;
		//unclamp lastcsr
		if(lastcsr > 1.0) lastcsr = 1/(2-lastcsr);

	//expedition length, i.e. days since divide plunder
		int explength = 0;
		if(CheckAttribute(pchar,"CrewStatus.explength")) explength = sti(pchar.CrewStatus.explength);
		else pchar.CrewStatus.explength = 0;
		int norm_morale = 100;
		float xl_csr_scale = 1.0;
		if(explength <= (NORMAL_EXP_LENGTH * EXP_LENGTH_START_FRAC)) { xl_csr_scale += (((NORMAL_EXP_LENGTH * EXP_LENGTH_START_FRAC) - explength) / (NORMAL_EXP_LENGTH * 1/2)) * 3.0; }

	//check loot and explength and deal with it
		float  csr = ((5.0-xl_csr_scale)*GetCrewShareRatio(pchar) + xl_csr_scale * lastcsr)/5.0;
	//temp
		csr = 0.25 + explength/NORMAL_EXP_LENGTH;
		//adjust for explength
		csr *= (10*pow(2.71828182845905,-3.256 * explength / NORMAL_EXP_LENGTH)) * (1.0+(moralemod)/2);
		if(articles && LogsToggle) Log_SetStringToLog(TranslateString("","Each crewman's share is now") + " " + csr + TranslateString("","x average for this time."));

		//clamp csr
		if(csr > 1) csr = 2-(1/csr);
		if(articles) { norm_morale = (100 - (CSR_SCALE*2))/2 + csr * CSR_SCALE; }
		else { norm_morale *= SALARY_SCALE; }
		moralescale *= (1.0+moralemod);
		norm_morale *= moralescale; //apply rum and food
		if(norm_morale > MORALE_MAX) norm_morale = MORALE_MAX;
		if(norm_morale < MORALE_MIN) norm_morale = MORALE_MIN;
		//Log_SetStringToLog("Mult: " + 10*pow(2.71828182845905,-3.256 * explength / NORMAL_EXP_LENGTH) + ", NM: " + norm_morale);
		
		if((norm_morale-morale) * CHANGE_MULT < 0)
		{
			moralech += (norm_morale-morale) * CHANGE_MULT;
			//Log_SetStringToLog("neg");
		}
		else
		{
			moralech += (norm_morale-morale) * CHANGE_MULT;
			//Log_SetStringToLog("pos");
		}
		//Log_SetStringToLog("CSR: chto " + (10 + csr * CSR_SCALE) + ", ch: " + (((10 + csr * CSR_SCALE)-morale) * CHANGE_MULT));

		
	//set values for next time and play sounds.
		explength++;
		pchar.CrewStatus.explength = explength;
		//apply personal explengths
		for(i=1; i<4; i++)
		{
			cn = GetCompanionIndex(&pchar,i);
			if(cn!=-1)
			{
				chref = GetCharacter(cn);
				chref.Ship.Crew.Morale = morale;
				if(CheckAttribute(chref,"cexplength")) {chref.cexplength = sti(chref.cexplength)+1;}
				else {chref.cexplength = 1;}
			}
		}
		for(i=1; i<4; i++)
		{
			cn = GetOfficersIndex(&pchar,i);
			if(cn!=-1)
			{
				chref = GetCharacter(cn);
				if(CheckAttribute(chref,"oexplength")) {chref.oexplength = sti(chref.oexplength)+1};
				else {chref.oexplength = 1;}
			}
		}
		//Log_SetStringToLog("XL: " + explength + ", NM: " + norm_morale);
		if(LogsToggle) Log_SetStringToLog(TranslateString("","We've been out") + " " + explength + " " + TranslateString("","days."));
		morale = morale + makeint(moralech);

		if(morale > 99) morale = 99;
		if(morale < 0) morale = 0;
		pchar.Ship.Crew.Morale = morale;

		if(morale<=10) { KAM_Mutiny(); }//MAXIMUS: new "Mutiny"
		if(LogsToggle) Log_SetStringToLog(TranslateString("","The crew now has") + " " + XI_ConvertString(GetMoraleName(morale)) + " " + XI_ConvertString("morale") + "(" + morale + ").");
		//if(LogsToggle) Log_SetStringToLog(" ");
		// tracking on/off toggle
		if(!CheckFood) { pchar.foodoff = true; }
	}
	// GreatZen -->
	int land = GetCharacterLand(pchar);
	int profit = makeint(LAND_PROFIT_SCALAR * pow(land/100,LAND_PROFIT_POW) * (0.25 + frand(0.5) + makefloat(PerkComm)/4.0) * (0.5 + makefloat(skillComm)/5.0 + makefloat(rand(roundup(makefloat(skillComm)/5.0)))/2.0) * (1.0 + 0.5 * makefloat(PerkBasicLand)));
	AddWealthtoCharacter(&pchar, profit);
	if(AUTO_SKILL_SYSTEM) { AddCharacterExpChar(&pchar, "Commerce", makeint(profit/10)); }// MAXIMUS - [skillsup system]
	if(LogsToggle && profit) { Log_SetStringToLog(TranslateString("","You made") + " " + MakeMoneyShow(profit, MONEY_SIGN, MONEY_DELIVER) + " " + TranslateString("","gold from your estates.")); }
	// GreatZen <--
// KK -->
	profit = 0;
	for (i = 0; i < TOWNS_QUANTITY; i++) {
		if (GetTownNation(Towns[i].id) == PERSONAL_NATION && CheckAttribute(Towns[i], "tax") == true) {
			profit += sti(makefloat(Towns[i].tax) * makefloat(GetTownSize(Towns[i].id)) / 5.0);
			if (rand(11) > 10) {
				moralech = TAX_NORMAL - sti(Towns[i].tax);
				SetTownCrewMorale(Towns[i].id, GetTownCrewMorale(Towns[i].id) + moralech);
			}
		}
	}
	AddMoneyToCharacter(&pchar, profit);
	if (LogsToggle && profit) Log_SetStringToLog(TranslateString("", "You made") + " " + MakeMoneyShow(profit, MONEY_SIGN, MONEY_DELIVER) + " " + TranslateString("", "gold as taxes from controlled towns."));
// <-- KK

	//ransom section
	for(i = 0; i < GetPassengersQuantity(pchar); i++)
	{
		cn = GetPassenger(pchar, i);
		if(cn != -1)
		{
			chref = GetCharacter(cn);
			//MAXIMUS: [if player wants to release captive in location of his nation, he doesn't want to escape anymore]
			if(CheckAttribute(chref,"prisoned") && chref.prisoned==true && !CheckAttribute(chref,"released"))
			{
				int tmpransom = GetPrisonRansomCost(chref);// NK 04-10-25 change so escape only near shore, make sure ransom increases
				chref.ransom = makeint(stf(chref.ransom) * RANSOM_INC) +1;
				//if(sti(chref.ransom) == tmpransom) chref.ransom = tmpransom + 1;
				bool rtmp = false;
				if(IsEntity(&worldMap) != 0) { if(wdmCurrentIsland != WDM_NONE_ISLAND) { rtmp = true; } }// on wdmap and near island
				else { if(!bSeaActive) { rtmp = true; } } // on land, we presume
				if(rtmp)
				{
					if(frnd() < ESCAPE_CHANCE * (1.0 + makefloat(GetCharacterSkill(chref, SKILL_SNEAK))) / (1.0 + makefloat(skillLuck))) // was GetCharacterSkill(pchar, SKILL_SNEAK)
					{
						DeleteAttribute(chref,"prisoned");
						RemovePassenger(pchar,chref);
						DeleteAttribute(chref,"ransom"); // NK
						ChangeCharacterAddressGroup(chref, "None", "", "");
						chref.location = "none";
						Log_SetStringToLog(TranslateString("","Your prisoner") + " " + GetMySimpleName(chref) + " " + TranslateString("","jumped overboard and escaped!"));
					}
				}// NK <--
			}
		}
	}

	if(!articles) DoDailyCrewPay(); //JA 26Nov06 Calc Pay owed daily.//MAXIMUS

	// PB: First Contact Quest -->
	if(CheckQuestAttribute("Firstcontact", "prepare_davyencounter"))
	{
		PChar.quest.Firstcontact = "start_davyencounter";
		SetNextWeather("Day Storm");
	}
	// PB: First Contact Quest <--

//Fudge Dragon
// Changing Face of the Caribbean
	if (CHANGING_RELATIONS != 0 && RANDOM_RELATIONS == 1) {
		int chance;
		int random;
		if (GetTownNation("Oxbay") == ENGLAND) {
 			if (GetNationRelation(ENGLAND, FRANCE) == RELATION_ENEMY) {
				chance = rand(365);
				if (chance == 365) {
 					Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("France") + " " + TranslateString("","have declared a ceasefire!"));
					SetNationRelationBoth(ENGLAND, FRANCE, RELATION_NEUTRAL);
 				}
			} else {
				if (GetNationRelation(ENGLAND, FRANCE) == RELATION_NEUTRAL) {
 					chance = rand(365);
 					random = rand(2);
 					if (chance == 365) {
 						if (random == 0) {
 							Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("France") + " " + TranslateString("","have formed an alliance!"));
 							SetNationRelationBoth(ENGLAND, FRANCE, RELATION_FRIEND);
 						} else {
 							Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("France") + " " + TranslateString("","have declared war on each other!"));
 							SetNationRelationBoth(ENGLAND, FRANCE, RELATION_ENEMY);
 						}
 					}
 				} else {
 					chance = rand(365);
 					random = rand(2);
 					if (chance == 365) {
 						if (random == 0) {
 							Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("France") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
 							SetNationRelationBoth(ENGLAND, FRANCE, RELATION_NEUTRAL);
 						} else {
							Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("France") + " " + TranslateString("","have broken their alliance and are now at war!"));
							SetNationRelationBoth(ENGLAND, FRANCE, RELATION_ENEMY);
 						}
 					}
 				}
 			}
 		}
 		if (GetNationRelation(ENGLAND, SPAIN) == RELATION_ENEMY) {
			chance = rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(ENGLAND, SPAIN, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(ENGLAND, SPAIN) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(ENGLAND, SPAIN, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(ENGLAND, SPAIN, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(ENGLAND, SPAIN, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(ENGLAND, SPAIN, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(ENGLAND, HOLLAND) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(ENGLAND, HOLLAND, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(ENGLAND, HOLLAND) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(ENGLAND, HOLLAND, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(ENGLAND, HOLLAND, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(ENGLAND, HOLLAND, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(ENGLAND, HOLLAND, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(ENGLAND, PORTUGAL) == RELATION_ENEMY) {
			chance = rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(ENGLAND, PORTUGAL, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(ENGLAND, PORTUGAL) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(ENGLAND, PORTUGAL, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(ENGLAND, PORTUGAL, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(ENGLAND, PORTUGAL, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("England") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(ENGLAND, PORTUGAL, RELATION_ENEMY);
					}
    		}
			}
		}
		if (GetNationRelation(FRANCE, SPAIN) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(FRANCE, SPAIN, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(FRANCE, SPAIN) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(FRANCE, SPAIN, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(FRANCE, SPAIN, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(FRANCE, SPAIN, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Spain") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(FRANCE, SPAIN, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(FRANCE, HOLLAND) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(FRANCE, HOLLAND, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(FRANCE, HOLLAND) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(FRANCE, HOLLAND, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(FRANCE, HOLLAND, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(FRANCE, HOLLAND, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(FRANCE, HOLLAND, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(FRANCE, PORTUGAL) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(FRANCE, PORTUGAL, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(FRANCE, PORTUGAL) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(FRANCE, PORTUGAL, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(FRANCE, PORTUGAL, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(FRANCE, PORTUGAL, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("France") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(FRANCE, PORTUGAL, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(SPAIN, HOLLAND) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(SPAIN, HOLLAND, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(SPAIN, HOLLAND) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(SPAIN, HOLLAND, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(SPAIN, HOLLAND, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(SPAIN, HOLLAND, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Holland") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(SPAIN, HOLLAND, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(SPAIN, PORTUGAL) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(SPAIN, PORTUGAL, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(SPAIN, PORTUGAL) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(SPAIN, PORTUGAL, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(SPAIN, PORTUGAL, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(SPAIN, PORTUGAL, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("Spain") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(SPAIN, PORTUGAL, RELATION_ENEMY);
					}
				}
			}
		}
		if (GetNationRelation(HOLLAND, PORTUGAL) == RELATION_ENEMY) {
			chance =  rand(365);
			if (chance == 365) {
				Log_SetStringToLog(XI_ConvertString("Holland") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared a ceasefire!"));
				SetNationRelationBoth(HOLLAND, PORTUGAL, RELATION_NEUTRAL);
			}
		} else {
			if (GetNationRelation(HOLLAND, PORTUGAL) == RELATION_NEUTRAL) {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random == 0) {
						Log_SetStringToLog(XI_ConvertString("Holland") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have formed an alliance!"));
						SetNationRelationBoth(HOLLAND, PORTUGAL, RELATION_FRIEND);
					} else {
						Log_SetStringToLog(XI_ConvertString("Holland") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have declared war on each other!"));
						SetNationRelationBoth(HOLLAND, PORTUGAL, RELATION_ENEMY);
					}
				}
			} else {
				chance = rand(365);
				random = rand(2);
				if (chance == 365) {
					if (random != 0) {
						Log_SetStringToLog(XI_ConvertString("Holland") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance but are currently peaceful!"));
						SetNationRelationBoth(HOLLAND, PORTUGAL, RELATION_NEUTRAL);
					} else {
						Log_SetStringToLog(XI_ConvertString("Holland") + " " + XI_ConvertString("and") + " " + XI_ConvertString("Portugal") + " " + TranslateString("","have broken their alliance and are now at war!"));
						SetNationRelationBoth(HOLLAND, PORTUGAL, RELATION_ENEMY);
					}
				}
			}
		}
 	}
// Changing Face of the Caribbean
//Fudge Dragon

	//trace("Gauging: NEXTDAY_DCU end");	
}
