#include "characters\init\Other.c"
#include "characters\init\CommonQuest.c"
#include "characters\init\PostInit.c" // CHANGE THIS WHEN TOTAL_CHARACTERS IS UPPED!

// Set characters initial data

void CreateCharacters()
{
	//int tmpNameFileID = LanguageOpenFile("characters_names.txt");
 	ref ch;
	int n;

		for(n=0; n<TOTAL_CHARACTERS; n++)
		{
			makeref(ch,Characters[n]);
			//DeleteAttribute(ch,"act");
			DeleteAttribute(ch,"");
			//Logic data
			ch.id = "0";
			ch.index = n;
			InitCharacter(ch); // KK
			// added by MAXIMUS [new skill-change] -->
//			if(AUTO_SKILL_SYSTEM)
//			{
//				ch.autoskills = 0;
//				InitAutoSkillsSystem(ch, false);
//			}
			// added by MAXIMUS [new skill-change] <--
		}

	ReloadProgressUpdate();

	makeref(ch,Characters[0]);		//Blaze Devlin

// MAXIMUS [choose character mod] -->
	ch.old.name = CharOldFirstName;
	if (CharOldMiddleName != "") ch.old.middlename = CharOldMiddleName; // KK
	ch.old.lastname = CharOldLastName;
	ch.name 	= CharFirstName;
	if (CharMiddleName != "") ch.middlename = CharMiddleName; // KK
	ch.lastname	= CharLastName;
	ch.nation	= CharNation;
	ch.sex		= CharSex;
	ch.model	= CharModel;
	ch.model.animation	= CharAni;
	ch.difficulty = CharDifficulty;
// KK -->
	if (ch.sex == "woman")
		ch.model.height = WOMAN_HEIGHT;
	else
		ch.model.height = MAN_HEIGHT;
// <-- KK
// MAXIMUS [choose character mod] <--

	ch.id		= "Blaze";

	ch.activity_begin = "0";
	ch.activity_end = "24";
	ch.luck = 4;
//	ch.spyglass.itemID = COMMON_SPYGLASS;
//	ch.gun.itemID = GUN_COMMON;
//	ch.gun = "gun";
//	ch.blade = "dagger";
//	ch.blade.itemID = BLADE_DAGGER;
//	ch.blade.time = 0.1;
//	ch.blade.colorstart = argb(64, 64, 64, 64);
//	ch.blade.colorend = argb(0, 32, 32, 32);
	ch.location	= "";
	ch.location.group = "";
	ch.location.locator = "";
	ch.Dialog.Filename = "blaze_dialog.c";
	ch.rank 	= START_LEVEL; // NK
	ch.reputation = "50";
	ch.experience = intRet(sti(ch.rank) > 1, CalculateExperienceFromRank(sti(ch.rank)), 0); // NK
	ch.skill.freeskill = sti(ch.rank) * ADD_SKILLPOINTS_PERLEVEL; // TIH changed to user configurable setting Nov14'06 // NK
	if (CHOOSABLE_NEWGAMESKILLS) {// TIH --> mod toggle Jul15'06
		switch ( CharDifficulty ) {
			case "3":
				ch.skill.freeskill = sti(ch.skill.freeskill) + ADD_SKILLPOINTS_PERLEVEL - 1;break;// TIH changed to user configurable setting Nov14'06
			case "2":
				ch.skill.freeskill = sti(ch.skill.freeskill) + ADD_SKILLPOINTS_PERLEVEL;break;// TIH changed to user configurable setting Nov14'06
			case "1":
				ch.skill.freeskill = sti(ch.skill.freeskill) + ADD_SKILLPOINTS_PERLEVEL;break;// TIH changed to user configurable setting Nov14'06
		}
		if ( ch.sex == "woman" ) {ch.skill.freeskill = sti(ch.skill.freeskill) - 1;}// TIH remove a free skill point from women characters because they start wtih TWO free skills already... only fair!
	}// TIH <-- mod toggle
	if (sti(ch.skill.freeskill) < 0) { ch.skill.freeskill = 0; } // TIH safety check Nov14'06
	if(!AUTO_SKILL_SYSTEM) ch.perks.freepoints = sti(ch.rank); // NK
	else ch.perks.freepoints = 1;
	ch.money = START_MONEY; // NK

// MAXIMUS [choose character mod] -->
	ch.skill.Leadership = CharLeadership;
	ch.skill.Fencing = CharFencing;
	ch.skill.Sailing = CharSailing;
	ch.skill.Accuracy = CharAccuracy;
	ch.skill.Cannons = CharCannons;
	ch.skill.Grappling = CharGrappling;
	ch.skill.Repair = CharRepair;
	ch.skill.Defence = CharDefence;
	ch.skill.Commerce = CharCommerce;
	ch.skill.Sneak = CharSneak;

	ch.skill.Leadership.bonus = CharBonusLeadership;
	ch.skill.Fencing.bonus = CharBonusFencing;
	ch.skill.Sailing.bonus = CharBonusSailing;
	ch.skill.Accuracy.bonus = CharBonusAccuracy;
	ch.skill.Cannons.bonus = CharBonusCannons;
	ch.skill.Grappling.bonus = CharBonusGrappling;
	ch.skill.Repair.bonus = CharBonusRepair;
	ch.skill.Defence.bonus = CharBonusDefence;
	ch.skill.Commerce.bonus = CharBonusCommerce;
	ch.skill.Sneak.bonus = CharBonusSneak;

	ch.Ship.Type = CharShipType;
	if (CharShipType != SHIP_NOTUSED_TYPE_NAME) {
		ch.Ship.Name = CharShipName;
		// PRS3 -->
		GetCharacterShipType(&ch);
		//aref arship; makearef(arship, ch.ship);
//KB - Tuning ships - changed call to SetRandomStatsToShip
		ch.ship.standard = true; // KK
		SetRandomStatsToShip(sti(ch.index), sti(ch.ship.idx), START_SHIP_NATION); // NK 04-09-06
//KB - orig SetRandomStatsToShip(arship, sti(ch.ship.idx), START_SHIP_NATION); // NK 04-09-06
//KB 
		ch.Ship.Cannons.Type = CANNON_TYPE_LONG_LBS4; // KK
		ch.Ship.Cannons.Charge.Type = DEFAULT_CHARGE; // KK
		// PRS3 <--
		ch.Ship.Stopped = true;
	} else {
		ch.Ship.Name = "";
	}
// MAXIMUS [choose character mod] <--

// KK -->
	ch.Soldier.m0 = "Offic_per";
	ch.Soldier.m1 = "Soldier_Per";
	ch.Soldier.m2 = "Soldier_Per2";
	ch.Soldier.m3 = "Soldier_Per3";
	ch.Soldier.m4 = "Soldier_Per4";
	ch.Soldier.m5 = "Soldier_Per5";
	ch.Soldier.m6 = "Soldier_Per6";
	ch.Soldier.m7 = "Soldier_Per" + (rand(4) + 2);

	ch.Boarder.Count = 12;
	ch.Boarder.m0 = NATHANIEL_BOARDER_0;
	ch.Boarder.m1 = NATHANIEL_BOARDER_1;
	ch.Boarder.m2 = NATHANIEL_BOARDER_2;
	ch.Boarder.m3 = NATHANIEL_BOARDER_3;
	ch.Boarder.m4 = NATHANIEL_BOARDER_4;
	ch.Boarder.m5 = NATHANIEL_BOARDER_5;
	ch.Boarder.m6 = NATHANIEL_BOARDER_6;
	ch.Boarder.m7 = NATHANIEL_BOARDER_7;
	ch.Boarder.m8 = NATHANIEL_BOARDER_8;
	ch.Boarder.m9 = NATHANIEL_BOARDER_9;
	ch.Boarder.m10 = NATHANIEL_BOARDER_10;
	ch.Boarder.m11 = NATHANIEL_BOARDER_11;
	ch.Boarder.Model = BOARDERMODEL;
	ch.Boarder.Index = BOARDER_INDEX;

	LAi_RemoveLoginTime(ch);
	GiveItem2Character(ch, "map");
// <-- KK

	//ch.Ship.Cannons.Type = CANNON_TYPE_TEST;
	ch.FaceId = 31;
	ch.ShipSlot1.Type=SHIP_NOTUSED;
	ch.ShipSlot1.Name="NoName";
	ch.ShipSlot2.Type=SHIP_NOTUSED;
	ch.ShipSlot2.Name="NoName";
	ch.ShipSlot3.Type=SHIP_NOTUSED;
	ch.ShipSlot3.Name="NoName";

	ch.quest.Contraband.relation = 50;
	ch.quest.RelationAgentMet = "0";
	ch.quest.gambling = "0";
	ch.quest.friend_in_tavern = "";
	ch.quest.main_line = "0";
	ch.quest.killed.pirates = "0";

/*	string stmp;
	stmp = GUN_COMMON;	Ch.items.(stmp) = 1;
	stmp = POTION;	Ch.items.(stmp) = 2;
	stmp = BLADE_SABER;	Ch.items.(stmp) = 1;
	stmp = CHEAP_SPYGLASS;	Ch.items.(stmp) = 1;*/

	// RM -->
	ch.land = 0;
	ch.wealth = START_WEALTH;

	string sNat;
	for(int nat = 0; nat < MAX_NATIONS; nat++) // KK
	{
		sNat = nat;
		ch.nations.(sNat).Rank = 0;
		ch.nations.(sNat).OldRank = 0;
	}
	ch.married = false;
	// RM <--
	ch.buildversion = BUILDVERSION; // 04-11-01 add build version, 05-03-25 set 12.2, 13; set back to 12.2 on 05-07-15
	ch.knbcannons = USE_REAL_CANNONS; // NK to handle KNB cannon switching 05-04-10

	ReloadProgressUpdate();
	if(AUTO_SKILL_SYSTEM) { InitAutoSkillsSystem(ch, false); }// added by MAXIMUS [new skill-change]
	trace("Gauging: start NPC init");
	n = 1;
// KK -->
	n = CreateGameCharacters(n, "FalaiseDeFleur"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Redmond"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "IslaMuelle"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Oxbay"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Conceicao"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Douwesen"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "QuebradasCostillas"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Turks"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Hispaniola"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Cayman"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Guadeloupe"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Cuba"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Antigua"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "SaintMartin"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Story"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "TempQuest"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "TempQuestEnemy"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Officers"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "FortCommandants"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Usurers"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "StreetMerchants"); ReloadProgressUpdate();
	ch.firstStore = n;//MAXIMUS
	n = CreateGameCharacters(n, "StoreOwners"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "TActors"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "NK"); ReloadProgressUpdate();
	n = CreateGameCharacters(n, "Ship"); ReloadProgressUpdate();

	n = CreateCommonQuestCharacters(n);
	ReloadProgressUpdate();
// <-- KK
	trace("Gauging: done NPC init");
	// NK <--
// added by MAXIMUS -->
	/*n = CreateShipCharacters(n);
	ReloadProgressUpdate();*/
	CHARACTERS_QUANTITY = n; // NK 05-04-05 use int. // KK
	trace("::Completing characters initialisation - CHARACTERS_QUANTITY = "+CHARACTERS_QUANTITY+"::");
	ReloadProgressUpdate();
// added by MAXIMUS <--
	//Trace("NChar: " + n);

	CreateOtherCharacters();

	ReloadProgressUpdate();

	if(GetMainCharacterIndex()>=0)
	{
		SetMainCharacterIndex(GetMainCharacterIndex());
	}

		//SetAllShipData();
		//SetAllFellows();

		//Post init
	// do in PostInit()
	DoCharactersPostInit();
	/*for(n=0; n<TOTAL_CHARACTERS; n++)
		{
			// move to bottom as Character_PostInit(int n);
		}*/
	trace("Gauging: done second pass through char array");

	ReloadProgressUpdate();
	//LanguageCloseFile(tmpNameFileID);
}

void SetAllShipData()
{
	for(int i=0;i<CHARACTERS_QUANTITY;i++) // NK 05-04-05 use CHARACTERS_QUANTITY.
	{
		SetBaseShipData(GetCharacter(i));
	}
}

void SetAllFellows()
{
	ref rcc;
	for(int i=0;i<CHARACTERS_QUANTITY;i++) // NK 05-04-05 use CHARACTERS_QUANTITY
	{
		rcc = GetCharacter(i);
		if(CheckAttribute(rcc,"fellows"))
		{
			SetBaseFellows(rcc);
		}
	}
}

// KK -->
void InitCharacter(ref ch)
{
	if (!CheckAttribute(ch, "sex")) ch.sex = "man";
	//Model data
	if (!CheckAttribute(ch, "model")) ch.model = "none";
	if (!CheckAttribute(ch, "model.entity")) ch.model.entity = "NPCharacter";
	if (!CheckAttribute(ch, "model.animation")) ch.model.animation = "";
	//Address
	if (!CheckAttribute(ch, "location")) ch.location = "none";
	if (!CheckAttribute(ch, "location.group")) ch.location.group = "";
	if (!CheckAttribute(ch, "location.locator")) ch.location.locator = "";
	if (!CheckAttribute(ch, "location.from_sea")) ch.location.from_sea = "";
	//Ship data
	if (!CheckAttribute(ch, "Ship.Type")) ch.Ship.Type = SHIP_NOTUSED_TYPE_NAME;//SHIP_LUGGER; // PS
	//Quest data
	if (!CheckAttribute(ch, "Dialog.CurrentNode")) ch.Dialog.CurrentNode = "First time";
	if (!CheckAttribute(ch, "Dialog.TempNode")) ch.Dialog.TempNode = ch.Dialog.CurrentNode;
	if (!CheckAttribute(ch, "quest")) ch.quest = "True";
	if (!CheckAttribute(ch, "quest.meeting")) ch.quest.meeting = "0";
	//Misc data
	if (!CheckAttribute(ch, "nation")) ch.nation = ENGLAND;
	if (!CheckAttribute(ch, "freeskill")) ch.skill.freeskill = 0;
	if (!CheckAttribute(ch, "rank")) ch.rank = 1;
	if (!CheckAttribute(ch, "reputation")) ch.reputation = REPUTATION_NEUTRAL;
	// battle hp
	if (!CheckAttribute(ch, "headModel")) ch.headModel = "capitan";
	if (!CheckAttribute(ch, "location.stime") || !CheckAttribute(ch, "location.etime")) {
		float stime = 6.0;
		float etime = 21.98333;
		if (CheckAttribute(ch, "location.stime")) stime = stf(ch.location.stime);
		if (CheckAttribute(ch, "location.etime")) etime = stf(ch.location.etime);
		LAi_SetLoginTime(ch, stime, etime);
	}
	if (!CheckAttribute(ch, "chr_ai.hp") || !CheckAttribute(ch, "chr_ai.hp_max")) {
		float hp = LAI_DEFAULT_HP;
		float hp_max = LAI_DEFAULT_HP_MAX;
		if (CheckAttribute(ch, "chr_ai.hp")) hp = stf(ch.chr_ai.hp);
		if (CheckAttribute(ch, "chr_ai.hp_max")) hp_max = stf(ch.chr_ai.hp_max);
		LAi_SetHP(ch, hp, hp_max);
	}
	// boal 05.09.03 offecer need to go to abordage -->
	if (!CheckAttribute(ch, "AbordageMode")) ch.AbordageMode = makeint(1);
	// boal 05.09.03 offecer need to go to abordage <--
}

int CreateGameCharacters(int n, string name)
{
	string sl = GetStorylineDir(FindCurrentStoryline());
	string fname = name + ".c";
	string func = "Create" + name + "Characters";
	if (FindFile("PROGRAM\Characters\init\", "*.c", fname) != "") {
		if (LoadSegment("Characters\init\" + fname)) {
			call func(&n);
			UnloadSegment("Characters\init\" + fname);
		}
	}
	if (FindFile("PROGRAM\" + sl + "\Characters\init\", "*.c", fname) != "") {
		if (LoadSegment(sl + "\Characters\init\" + fname)) {
			call func(&n);
			UnloadSegment(sl + "\Characters\init\" + fname);
		}
	}
	return n;
}

void AddGameCharacter(ref n, ref ch)
{
	if (!CheckAttribute(ch, "id")) return;
	int chridx = GetCharacterIndex(ch.id);
	ref chr;
	if (chridx >= 0) {
		makeref(chr, Characters[chridx]);
	} else {
		makeref(chr, Characters[n]);
		chridx = n;
		n++;
	}
	DeleteAttribute(chr, "");
	CopyAttributes(chr, ch);
	chr.index = chridx;
	InitCharacter(chr);
	DeleteAttribute(ch, "");
	if (isSoldier(chr) > -1) {
		int iNation = sti(chr.nation);
		string model = "m" + sti(chr.model);
		if (iNation >= 0 && iNation < MAX_NATIONS) model = Nations[iNation].fantomModel.(model);
		if (iNation == PERSONAL_NATION) model = characters[GetMainCharacterIndex()].Soldier.(model);
		SetModelFromID(chr, model);
	}
}
// <-- KK
