// NK added 05-07-03 as centralized HP setting thing.
// PB: Simplified and uniformized for all characters
float ResetHP(ref chr)
{
	float HP = CHAR_START_HITPOINTS;
	float level = makefloat(GetLevel(&chr));
	if(CheckAttribute(chr, "HPBonus")) HP += stf(chr.HPBonus);
	HP += (level - 1) * CHAR_HITPOINTS_PER_LEVEL;
// LDH --> Added toughness perk from elrapido verson of ResetMaxHP
	if(IsCharacterPerkOn(chr, "Toughness"))
	{
		HP = HP * 1.1;			// 10% health bonus for the tough guys
		chr.chr_ai.hp_dlt = 0.2;	// higher regeneration rate, default is 0.1
	}
	chr.chr_ai.hp_max = HP;
// LDH <--
	LAi_SetHP(&chr, HP, HP);
	return HP;
}

int ChangeHPBonus(ref chr, int offset)
{
	if(CheckAttribute(chr, "HPBonus")) offset += sti(chr.HPBonus);
	chr.HPBonus = offset;
	return sti(offset);
}

//----------------------------------------------------------------------------------------------------------------
//--------------------Create Officer---------------------------------------------------------------------------------

// with additions by NK to use this for all char creation, and to differentiate between friends and enemies, and for generating captains
// and to include difficulty level.
void LAi_Create_Officer(int rankch, ref Npchar)
{
	ref PChar;
	PChar = GetMainCharacter();
	//check to see if enemy or friend.
	int pLuck = GetSummonSkillFromName(PChar,SKILL_SNEAK);
	int pLead = GetSummonSkillFromName(PChar,SKILL_LEADERSHIP);
	int pRank = sti(PChar.rank);
	if(rankch != 0) pRank += rankch;
	if(pRank < 1) pRank = 1;
	int nRank = 1;
	float caprankmult = 1.0; //for use if captain, and based on shipclass
	//two new attributes for NPCs, friend and officer (bool); captain type uses officertype.
	bool isfriend = CheckAttribute(Npchar, "friend") && sti(Npchar.friend); // NK finally implement this 05-05-10
	bool officer = CheckAttribute(Npchar, "officer"); // && isfriend; //implement friend later;
	if(officer) isfriend = true; //tempfix until I implement friendchecks.
	bool captain = false;
	int cShipClass = 0;
	//Log_SetStringToLog(Npchar.quest.officertype + ", officer " + officer);

	if(!CheckAttribute(Npchar,"quest.officertype")) Npchar.quest.officertype = OFFIC_TYPE_RANDCHAR;
	
	switch(Npchar.quest.officertype)
	{
		case OFFIC_TYPE_CAPMERCHANT:
			caprankmult = 0.5 * (2.0/3.0 + (makefloat(GetDifficulty())-1.0) / 1.5);
			captain = true;
			break;

		case OFFIC_TYPE_CAPNAVY:
			caprankmult = 0.8 * (2.0/3.0 + (makefloat(GetDifficulty())-1.0) / 1.5);
			captain = true;
			break;

		case OFFIC_TYPE_CAPPIRATE:
			caprankmult = 1.0 * (2.0/3.0 + (makefloat(GetDifficulty())-1.0) / 1.5);
			captain = true;
			break;
	}
	
	if(isfriend)
	{
		nRank = makeint((pRank/2.0 + Rand(pRank) + (pLead / 3.0) + (pLuck / 3.0)) * (2.0/3.0 + (4.0-makefloat(GetDifficulty()))/5.0) + 0.5);
	}
	else
	{
		nRank = makeint((pRank/2.0 + Rand(pRank) - (pLuck / 3.0)) * (2.0/3.0 + ((makefloat(GetDifficulty())-1.0)/5.0)) + 0.5);
	}
	nRank = makeint(makefloat(nRank) * caprankmult);
	//Log_SetStringToLog("Preclamp, " + /*Npchar.id + " (" + Npchar.name + " " + Npchar.lastname + "), " + Npchar.quest.officertype + ", has rank " + */nRank + "; mult " + caprankmult);
	
	if(captain)
	{
		//Log_SetStringToLog("Cap");
		//note: here caprankmult is re-assigned, to account for pchar's officers; this has been disabled, as it's now done in AI.
		//caprankmult = 1 + makefloat(GetOfficersQuantity(PChar)*5 + rand(GetOfficersQuantity(PChar)*5))/10.0;
		cShipClass = 8 - GetCharacterShipClass(Npchar);
		if(cShipClass > 8) cShipClass = 1;
		if(GetCharacterShipType(Npchar) == SHIP_FORT) cShipClass = 8;
		if(nRank < makeint(pow((cShipClass),1.25))) nRank = makeint(pow((cShipClass),1.25));
		if(GetDifficulty() < 4 && Npchar.quest.officertype != OFFIC_TYPE_CAPPIRATE && nRank > makeint(pow((cShipClass),1.5)*2.5)) nRank = makeint(pow((cShipClass),1.5)*2.5);
		//Npchar.oldrank = makeint(nRank);
		//nRank = makeint(nRank * caprankmult);
	}
	//Log_SetStringToLog("Clamped, " + /*Npchar.id + " (" + Npchar.name + " " + Npchar.lastname + "), " + Npchar.quest.officertype + ", has rank " + */nRank + "; mult " + caprankmult);
	If (isfriend && pLead == 10) nRank++;
	If (nRank < 1) nRank = 1;
	Npchar.rank = makeint(nRank);

	Npchar.perks.FreePoints = nRank;
	Npchar.skill.Freeskill = nRank * ADD_SKILLPOINTS_PERLEVEL; // TIH changed to user configurable setting Nov14'06
	//Log_SetStringToLog(" " + Npchar.perks.FreePoints + " " + Npchar.skill.Freeskill);

	if(officer) Npchar.quest.OfficerPrice = makeint((nRank * 2 + 10)*100);

	Int Skillvariation;
	Skillvariation = intRet(isfriend, nRank - makeint(pLead / 2), nRank + makeint(pLuck/3));
	LAi_Officer_SetSkillmodifiers_and_Perks(Npchar,Skillvariation);
	LAi_SetSkill_on_Modifier(Npchar);

	//Npchar.Experience = 1000;

//---------------------------------Set Officer Variation based on MainChar Luck-------------------------------------------------------------------------------------------------
	If (rand(9) < pLuck) {Npchar.perks.FreePoints = makeint(Npchar.perks.Freepoints) +1;}
	Npchar.skill.Freeskill = makeint(Npchar.skill.Freeskill) + rand(makeint(pLuck / 3));
//MAXIMUS 20.09.2006 [so, freeskill and freepoints will not exceed maximal possible amount] -->	
	aref arPerksRoot;
	int n,j;
	makearef(arPerksRoot,Npchar.perks.list);
	n = GetAttributesNum(arPerksRoot);
	int freePerks = 0;
	int usedPerks = 0;
	for(j=0; j<n; j++)
	{
		string perkName = GetAttributeName(GetAttributeN(arPerksRoot,j));//MAXIMUS: correcting free perks -[17.02.2007]->
		if(!CheckCharacterPerk(Npchar,perkName)) { freePerks = freePerks + 1; }
		else { usedPerks = usedPerks + 1; }
//		arPerk = GetAttributeN(arPerksRoot,j);
//		if(IsCharacterPerkOn(PChar,GetAttributeName(arPerk))) numActivePerks = numActivePerks+1;
//		if(Npchar.perks.FreePoints > makeint(n-numActivePerks)) Npchar.perks.FreePoints = makeint(n-numActivePerks);
	}
	if(freePerks==0) { Npchar.perks.FreePoints = 0; }
	else
	{
		if(CheckAttribute(Npchar,"perks.FreePoints") && sti(Npchar.perks.FreePoints)>freePerks) { Npchar.perks.FreePoints = freePerks; }
	}//MAXIMUS: correcting free perks <-[17.02.2007]-
//MAXIMUS 20.09.2006 [so, freeskill and freepoints will not exceed maximal possible amount] <--
	/*if(captain) // for use with above officerqty scale
	{
		nRank = makeint(Npchar.oldrank);
		DeleteAttribute(Npchar, "oldrank");
		Npchar.rank = makeint(nRank);
	}*/
	float mhp = LAI_DEFAULT_HP;
	//mhp += intRet(isfriend, intRet(officer, nRank * (14 - GetDifficulty()*2), nRank * (8-GetDifficulty())), nRank * (3+GetDifficulty()));
	mhp += intRet(officer, nRank * makeint(11-GetDifficulty()), (nRank - 1) * CHAR_HITPOINTS_PER_LEVEL);    // LDH 05Dec06
	//Log_SetStringToLog(mhp + ", mult " + (GetDifficulty()*3))
	LAi_SetHP(Npchar,mhp,mhp);
	Npchar.experience = CalculateExperienceFromRank(nRank);
}

//--------------------------------------------------------------------------------
//----------SelectPerksForOfficers------------------------------------------------
//--------------------------------------------------------------------------------

void SelectPerksForOfficers(ref Npchar, int LeadershipValue, int CombatValue, int CannonValue, int NavigatorValue, int GrapplingValue, int DefenseValue, int QMasterValue)
{
	Int FreePerks = makeint(Npchar.Perks.Freepoints);
	Npchar.Perks.Freepoints = 0;
	int LeadershipSpent = 0;
	Int CombatSpent = 0;
	Int CannonSpent = 0;
	Int NavigatorSpent = 0;
	Int GrapplingSpent = 0;
	Int DefenseSpent = 0;
	Int QMasterSpent = 0;
	Int PerksSelect = 0;

	while (FreePerks > 0)
	{
		if(LeadershipSpent >= 2) {LeadershipValue = 0;}
		If (CombatSpent >= 8) {CombatValue = 0;}
		If (CannonSpent >= 8) {CannonValue = 0;}
		If (NavigatorSpent >= 6) {NavigatorValue = 0;}
		If (GrapplingSpent >= 2) {GrapplingValue = 0;}
		If (DefenseSpent >= 3) {DefenseValue = 0;}
		If (QMasterSpent >= 5) {QMasterValue = 0;}
		
		If (LeadershipValue + CombatValue + CannonValue + NavigatorValue + GrapplingValue + DefenseValue + QMasterValue == 0) {Npchar.Perks.FreePoints = makeint(Npchar.Perks.FreePoints) + FreePerks; FreePerks = 0; continue;}

		PerksSelect = Rand(LeadershipValue + CombatValue + CannonValue + NavigatorValue + GrapplingValue + DefenseValue + QMasterValue -1) + 1;
	// NK 04-09-20 rewrite the check attr lines
        if (CheckAttribute(Npchar,"Perks.List.SwordplayProfessional")  && Npchar.Perks.List.SwordplayProfessional == "1" &&
            CheckAttribute(Npchar,"Perks.List.GrapplingProfessional")  && Npchar.Perks.List.GrapplingProfessional == "1" &&
            CheckAttribute(Npchar,"Perks.List.SailingProfessional")    && Npchar.Perks.List.SailingProfessional   == "1") { if(!CheckAttribute(Npchar,"Perks.List.InstantBoarding") || Npchar.Perks.List.InstantBoarding       != "1")
        {
            FreePerks = FreePerks - 1; Npchar.Perks.List.InstantBoarding = "1"; continue;
        } }
		If (PerksSelect <= LeadershipValue) {FreePerks = FreePerks - SelectPerks_Leadership(Npchar); LeadershipSpent++; continue;}
		PerksSelect = PerksSelect - LeadershipValue;
		If (PerksSelect <= CombatValue) {FreePerks = FreePerks - SelectPerks_Combat(Npchar); CombatSpent++; continue;}
		PerksSelect = PerksSelect - CombatValue;
		If (PerksSelect <= CannonValue) {FreePerks = FreePerks - SelectPerks_Cannon(Npchar); CannonSpent++; continue;}
		PerksSelect = PerksSelect - CannonValue;
		If (PerksSelect <= NavigatorValue) {FreePerks = FreePerks - SelectPerks_Navigator(Npchar); NavigatorSpent++; continue;}
		PerksSelect = PerksSelect - NavigatorValue;
		If (PerksSelect <= GrapplingValue) {FreePerks = FreePerks - SelectPerks_Grappling(Npchar); GrapplingSpent++; continue;}
		PerksSelect = PerksSelect - GrapplingValue;
		If (PerksSelect <= DefenseValue) {FreePerks = FreePerks - SelectPerks_Defense(Npchar); DefenseSpent++; continue;}
		PerksSelect = PerksSelect - DefenseValue;
		If (PerksSelect <= QMasterValue) {FreePerks = FreePerks - SelectPerks_QMaster(Npchar); QMasterSpent++; continue;}

		Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;
		//Npchar.Experience = makeint(Npchar.Experience) +1;
		FreePerks = FreePerks -1;
	}
}

int SelectPerks_Leadership(ref Npchar)
{
	Int P_Shared = 0;
	If (!CheckAttribute(Npchar,"Perks.List.SharedExperience"))  {P_Shared = 10;}
	else { if(Npchar.Perks.List.SharedExperience != "1") {P_Shared = 10;} }
	Int P_Iron = 0;
	If (!CheckAttribute(Npchar,"Perks.List.IronWill")) {P_Iron = 5;}
	else { if( Npchar.Perks.List.IronWill != "1")  {P_Iron = 5;} }
	
	If (P_Shared + P_Iron == 0) {Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1; return 1;}
	Int Perkselect = 0;
	Perkselect = Rand(P_Shared + P_Iron - 1) + 1;

	If (Perkselect <= P_Shared) {Npchar.Perks.List.SharedExperience = "1"; return 1;}
	Perkselect = Perkselect - P_Shared;
	If (Perkselect <= P_Shared) {Npchar.Perks.List.IronWill = "1"; return 1;}
	Perkselect = Perkselect - P_Iron;
	
	//Npchar.Experience = makeint(Npchar.Experience) + 10;
	Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;
	return 1;
}

Int SelectPerks_Combat(ref Npchar)
{
	Int P_BasicDefense = 0;
	If(!CheckAttribute(Npchar,"Perks.List.BasicDefense")) {P_BasicDefense = 15;}
	else { if(Npchar.Perks.List.BasicDefense != "1") {P_BasicDefense = 15;} }
	Int P_Advanced = 0;
	If (CheckAttribute(Npchar,"Perks.List.BasicDefense") && Npchar.Perks.List.BasicDefense == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.AdvancedDefense")) {P_Advanced = 10;}
		else { if(Npchar.Perks.List.AdvancedDefense != "1") {P_Advanced = 10;} }
	}
	//MAXIMUS 20.09.2006 -->
	int P_Toughness = 0;
	if(CheckAttribute(Npchar,"Perks.List.AdvancedDefense") && Npchar.Perks.List.AdvancedDefense == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.Toughness")) {P_Toughness = 10;}
		else { if(Npchar.Perks.List.Toughness != "1") {P_Toughness = 10;} }
	}
	//MAXIMUS 20.09.2006 <--
	Int P_Swordplay = 0;
	If (CheckAttribute(Npchar,"Perks.List.AdvancedDefense") && Npchar.Perks.List.AdvancedDefense == "1" && 
        CheckAttribute(Npchar,"Perks.List.CriticalHit") && Npchar.Perks.List.CriticalHit == "1")
        {
		if(!CheckAttribute(Npchar,"Perks.List.SwordplayProfessional")) {P_Swordplay = 10;}
		else { if(Npchar.Perks.List.SwordplayProfessional != "1") {P_Swordplay = 10;} }
	}
	Int P_Critical = 0;
	If (!CheckAttribute(Npchar,"Perks.List.CriticalHit")) {P_Critical = 8;}
	else { if(Npchar.Perks.List.CriticalHit != "1") {P_Critical = 8;} }
	Int P_Gunman = 0;
	If (!CheckAttribute(Npchar,"Perks.List.Gunman")) {P_Gunman = 5;}
	else { if(Npchar.Perks.List.Gunman != "1") {P_Gunman = 5;} }
	Int P_Professional = 0;
	If (CheckAttribute(Npchar,"Perks.List.Gunman") && Npchar.Perks.List.Gunman == "1" )
	{
		if(!CheckAttribute(Npchar,"Perks.List.GunProfessional")) {P_Professional = 5;}
		else { if(Npchar.Perks.List.GunProfessional != "1") {P_Professional = 5;} }
	}
	Int P_Rush = 0;
	If (!CheckAttribute(Npchar,"Perks.List.Rush")) {P_Rush = 1;}
	else { if(Npchar.Perks.List.Rush != "1") {P_Rush = 1;} }
	//MAXIMUS 20.09.2006 -->
	If (P_BasicDefense + P_Advanced + P_Toughness + P_Swordplay + P_Critical + P_Gunman + P_Professional + P_Rush == 0) {Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1; return 1;}
	Int Perkselect = 0;
	Perkselect = Rand(P_BasicDefense + P_Advanced + P_Toughness + P_Swordplay + P_Critical + P_Gunman + P_Professional + P_Rush - 1) + 1;
	//MAXIMUS 20.09.2006 <--
	If (Perkselect <= P_BasicDefense) {Npchar.Perks.List.BasicDefense = "1"; return 1;}
	Perkselect = Perkselect - P_BasicDefense; 
	If (Perkselect <= P_Advanced) {Npchar.Perks.List.AdvancedDefense = "1"; return 1;}
	Perkselect = Perkselect - P_Advanced;
	//MAXIMUS 20.09.2006 -->
	if(Perkselect <= P_Toughness) {Npchar.Perks.List.Toughness = "1"; return 1;}
	Perkselect = Perkselect - P_Toughness;
	//MAXIMUS 20.09.2006 <--
	If (Perkselect <= P_Swordplay) {Npchar.Perks.List.SwordplayProfessional = "1"; return 1;}
	Perkselect = Perkselect - P_Swordplay;
	If (Perkselect <= P_Critical) {Npchar.Perks.List.CriticalHit = "1"; return 1;}
	Perkselect = Perkselect - P_Critical;
	If (Perkselect <= P_Gunman) {Npchar.Perks.List.Gunman = "1"; return 1;}
	Perkselect = Perkselect - P_Gunman;
	If (Perkselect <= P_Professional) {Npchar.Perks.List.GunProfessional = "1"; return 1;}
	Perkselect = Perkselect - P_Professional;
	If (Perkselect <= P_Rush) {Npchar.Perks.List.Rush = "1"; return 1;}
	
	//Npchar.Experience = makeint(Npchar.Experience) + 10;
	Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;
	return 1;

}

Int SelectPerks_Cannon(ref Npchar)
{
	Int P_FastReload = 0;
	If (!CheckAttribute(Npchar,"Perks.List.FastReload")) {P_FastReload = 1;}
	else { if(Npchar.Perks.List.FastReload != "1") {P_FastReload = 1;} }
	Int P_Immediate = 0;
	If (CheckAttribute(Npchar,"Perks.List.FastReload") && Npchar.Perks.List.FastReload == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.ImmediateReload")) {P_Immediate = 1;}
		else { if(Npchar.Perks.List.ImmediateReload != "1") {P_Immediate = 1;} }
	}
	Int P_HullDamageUp = 0;
	If (!CheckAttribute(Npchar,"Perks.List.HullDamageUp")) {P_HullDamageUp = 2;}
	else { if(Npchar.Perks.List.HullDamageUp != "1") {P_HullDamageUp = 2;} }
	Int P_SailsDamageUp = 0;
	If (!CheckAttribute(Npchar,"Perks.List.SailsDamageUp")) {P_SailsDamageUp = 1;}
	else { if(Npchar.Perks.List.SailsDamageUp != "1") {P_SailsDamageUp = 1;} }
	Int P_CrewDamageUp = 0;
	If (!CheckAttribute(Npchar,"Perks.List.CrewDamageUp")) {P_CrewDamageUp =1;}
	else { if(Npchar.Perks.List.CrewDamageUp != "1") {P_CrewDamageUp =1;} }
	Int P_CriticalShoot = 0;
	If (CheckAttribute(Npchar,"Perks.List.HullDamageUp") && Npchar.Perks.List.HullDamageUp == "1" && 
        CheckAttribute(Npchar,"Perks.List.SailsDamageUp") && Npchar.Perks.List.SailsDamageUp == "1" && 
        CheckAttribute(Npchar,"Perks.List.CrewDamageUp") && Npchar.Perks.List.CrewDamageUp == "1")
        {
		if(!CheckAttribute(Npchar,"Perks.List.CriticalShoot")) {P_CriticalShoot = 5;}
		else { if(Npchar.Perks.List.CriticalShoot != "1") {P_CriticalShoot = 5;} }
	}
	Int P_LongRangeShoot = 0;
	If (!CheckAttribute(Npchar,"Perks.List.LongRangeShoot")) {P_LongRangeShoot = 2;}
	else { if(Npchar.Perks.List.LongRangeShoot != "1") {P_LongRangeShoot = 2;} }
	Int P_CannonProfessional = 0;
	If(CheckAttribute(Npchar,"Perks.List.CriticalShoot") && Npchar.Perks.List.CriticalShoot == "1" && 
        CheckAttribute(Npchar,"Perks.List.LongRangeShoot") && Npchar.Perks.List.LongRangeShoot == "1")
        {
		if(!CheckAttribute(Npchar,"Perks.List.CannonProfessional")) {P_CannonProfessional = 10;}
		else { if(Npchar.Perks.List.CannonProfessional != "1") {P_CannonProfessional = 10;} }
	}

	If (P_FastReload + P_Immediate + P_HullDamageUp + P_SailsDamageUp + P_CrewDamageUp + P_CriticalShoot + P_LongRangeShoot + P_CannonProfessional	== 0) {Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1; return 1;}

	Int Perkselect = 0;
	Perkselect = Rand(P_FastReload + P_Immediate + P_HullDamageUp + P_SailsDamageUp + P_CrewDamageUp + P_CriticalShoot + P_LongRangeShoot + P_CannonProfessional - 1) +1;
	
	If (Perkselect <= P_FastReload) {Npchar.Perks.List.FastReload = "1"; return 1;}
	Perkselect = Perkselect - P_FastReload;
	If (Perkselect <= P_Immediate) {Npchar.Perks.List.ImmediateReload = "1"; return 1;}
	Perkselect = Perkselect - P_Immediate;
	If (Perkselect <= P_HullDamageUp) {Npchar.Perks.List.HullDamageUp = "1"; return 1;}
	Perkselect = Perkselect - P_HullDamageUp;
	If (Perkselect <= P_SailsDamageUp) {Npchar.Perks.List.SailsDamageUp = "1"; return 1;}
	Perkselect = Perkselect - P_SailsDamageUp;
	If (Perkselect <= P_CrewDamageUp) {Npchar.Perks.List.CrewDamageUp = "1"; return 1;}
	Perkselect = Perkselect - P_CrewDamageUp;
	If (Perkselect <= P_CriticalShoot) {Npchar.Perks.List.CriticalShoot = "1"; return 1;}
	Perkselect = Perkselect - P_CriticalShoot;
	If (Perkselect <= P_LongRangeShoot) {Npchar.Perks.List.LongRangeShoot = "1"; return 1;}
	Perkselect = Perkselect - P_LongRangeShoot;
	If (Perkselect <= P_CannonProfessional) {Npchar.Perks.List.CannonProfessional = "1"; return 1;}

	Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;

	return 1;
}

Int SelectPerks_Navigator(ref Npchar)
{
	Int P_ShipSpeedUp = 0;
	If (!CheckAttribute(Npchar,"Perks.List.ShipSpeedUp")) {P_ShipSpeedUp = 3;}
	else { if(Npchar.Perks.List.ShipSpeedUp != "1") {P_ShipSpeedUp = 3;} }
	Int P_ShipTurnRateUp = 0;
	If(!CheckAttribute(Npchar,"Perks.List.ShipTurnRateUp")) {P_ShipTurnRateUp = 3;}
	else { if(Npchar.Perks.List.ShipTurnRateUp != "1") {P_ShipTurnRateUp = 3;} }
	Int P_StormProfessional = 0;
	If (!CheckAttribute(Npchar,"Perks.List.StormProfessional")) {P_StormProfessional = 2;}
	else { if(Npchar.Perks.List.StormProfessional != "1") {P_StormProfessional = 2;} }
	Int P_Turn180 = 0;
	If (CheckAttribute(Npchar,"Perks.List.ShipSpeedUp") && Npchar.Perks.List.ShipSpeedUp == "1" && 
        CheckAttribute(Npchar,"Perks.List.ShipTurnRateUp") && Npchar.Perks.List.ShipTurnRateUp == "1")
        {
		if(!CheckAttribute(Npchar,"Perks.List.Turn180")) {P_Turn180 = 2;}
		else { if(Npchar.Perks.List.Turn180 != "1") {P_Turn180 = 2;} }
	}
	Int P_SandbankManeuver = 0;
	If (CheckAttribute(Npchar,"Perks.List.Turn180") && Npchar.Perks.List.Turn180 == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.SandbankManeuver")) {P_SandbankManeuver = 2;}
		else { if(Npchar.Perks.List.SandbankManeuver != "1") {P_SandbankManeuver = 2;} }
	}
	Int P_SailingProfessional = 0;
	If (CheckAttribute(Npchar,"Perks.List.SandbankManeuver") && Npchar.Perks.List.SandbankManeuver == "1" && 
        CheckAttribute(Npchar,"Perks.List.StormProfessional") && Npchar.Perks.List.StormProfessional == "1")
        {
		if(!CheckAttribute(Npchar,"Perks.List.SailingProfessional")) {P_SailingProfessional = 10;}
		else { if(Npchar.Perks.List.SailingProfessional != "1") {P_SailingProfessional = 10;} }
	}

	If ( P_ShipSpeedUp + P_ShipTurnRateUp + P_StormProfessional + P_Turn180 + P_SandbankManeuver + P_SailingProfessional  == 0) {Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1; return 1;}

	Int Perkselect = 0;
	Perkselect = Rand(P_ShipSpeedUp + P_ShipTurnRateUp + P_StormProfessional + P_Turn180 + P_SandbankManeuver + P_SailingProfessional - 1) + 1;

	If (Perkselect <= P_ShipSpeedUp) {Npchar.Perks.List.ShipSpeedUp = "1"; return 1;}
	Perkselect = Perkselect - P_ShipSpeedUp;
	If (Perkselect <= P_ShipTurnRateUp) {Npchar.Perks.List.ShipTurnRateUp = "1"; return 1;}
	Perkselect = Perkselect - P_ShipTurnRateUp;
	If (Perkselect <= P_StormProfessional) {Npchar.Perks.List.StormProfessional = "1"; return 1;}
	Perkselect = Perkselect - P_StormProfessional;
	If (Perkselect <= P_Turn180) {Npchar.Perks.List.Turn180 = "1"; return 1;}
	Perkselect = Perkselect - P_Turn180;
	If (Perkselect <= P_SandbankManeuver) {Npchar.Perks.List.SandbankManeuver = "1"; return 1;}
	Perkselect = Perkselect - P_SandbankManeuver;
	If (Perkselect <= P_SailingProfessional) {Npchar.Perks.List.SailingProfessional = "1"; return 1;}

	//Npchar.Experience = makeint(Npchar.Experience) + 10;

	Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;

	return 1;
}

Int SelectPerks_Grappling(ref Npchar)
{
	bool tmp = false;
	If (!CheckAttribute(Npchar,"Perks.List.LongRangeGrappling")) tmp = true;
	else { if(Npchar.Perks.List.LongRangeGrappling != "1") tmp = true;}
	if(tmp)
    {
        Npchar.Perks.List.LongRangeGrappling = "1";
    }
	else
	{
		tmp = false;
		If(!CheckAttribute(Npchar,"Perks.List.GrapplingProfessional")) tmp = true;
		else { if(Npchar.Perks.List.GrapplingProfessional != "1") tmp = true;}
		if(tmp)
        {
            Npchar.Perks.List.GrapplingProfessional = "1";
        }
		else 
        {
            Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;
        }
	}
	return 1;
}

Int SelectPerks_Defense(ref Npchar)
{
	bool tmp = false;
	If (!CheckAttribute(Npchar,"Perks.List.BasicBattleState")) tmp = true;
	else { if(Npchar.Perks.List.BasicBattleState != "1") tmp = true;}
	if(tmp) {Npchar.Perks.List.BasicBattleState = "1";}
	else
	{
		tmp = false;
		If(!CheckAttribute(Npchar,"Perks.List.AdvancedBattleState")) tmp = true;
		else { if(Npchar.Perks.List.AdvancedBattleState != "1") tmp = true;}
		if(tmp) {Npchar.Perks.List.AdvancedBattleState = "1";}
		else 
		{
			tmp = false;
			If(!CheckAttribute(Npchar,"Perks.List.ShipDefenseProfessional")) tmp = true;
			else { if(Npchar.Perks.List.ShipDefenseProfessional != "1") tmp = true;}
			if(tmp) {Npchar.Perks.List.ShipDefenseProfessional = "1";}
			else {Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;}
		}
	}
	return 1;
}

Int SelectPerks_QMaster(ref Npchar)
{
	Int P_LightRepair = 0;
	If (!CheckAttribute(Npchar,"Perks.List.LightRepair")) {P_LightRepair = 2;}
	else { if(Npchar.Perks.List.LightRepair != "1") {P_LightRepair = 2;} }
	Int P_InstantRepair = 0;
	If (CheckAttribute(Npchar,"Perks.List.LightRepair") && Npchar.Perks.List.LightRepair == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.InstantRepair")) {P_InstantRepair = 1;}
		else { if(Npchar.Perks.List.InstantRepair != "1") {P_InstantRepair = 1;} }
	}
	Int P_Trustworthy = 0;
	If (!CheckAttribute(Npchar,"Perks.List.Trustworthy")) {P_Trustworthy = 2;}
	else { if(Npchar.Perks.List.Trustworthy != "1") {P_Trustworthy = 2;} }
	Int P_BasicCommerce = 0;
	If (!CheckAttribute(Npchar,"Perks.List.BasicCommerce")) {P_BasicCommerce = 3;}
	else { if(Npchar.Perks.List.BasicCommerce != "1") {P_BasicCommerce = 3;} }
	// GZ -->
	int P_BasicLandOwner = 0;
	If (CheckAttribute(Npchar,"Perks.List.BasicCommerce") && Npchar.Perks.List.BasicCommerce == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.BasicLandOwner")) {P_BasicLandOwner = 3;}
		else { if(Npchar.Perks.List.BasicLandOwner != "1") {P_BasicLandOwner = 3;} }
	}
	// GZ <--
	Int P_AdvancedCommerce = 0;
	If (CheckAttribute(Npchar,"Perks.List.BasicCommerce") && Npchar.Perks.List.BasicCommerce == "1")
	{
		if(!CheckAttribute(Npchar,"Perks.List.AdvancedCommerce")) {P_AdvancedCommerce = 5;}
		else { if(Npchar.Perks.List.AdvancedCommerce != "1") {P_AdvancedCommerce = 5;} }
	}

	If ( P_LightRepair + P_InstantRepair + P_Trustworthy + P_BasicCommerce + P_BasicLandOwner + P_AdvancedCommerce  == 0) {Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1; return 1;} // GZ

	Int Perkselect = 0;
	Perkselect = Rand(P_LightRepair + P_InstantRepair + P_Trustworthy + P_BasicCommerce + P_BasicLandOwner + P_AdvancedCommerce - 1) +1; // GZ

	If (Perkselect <= P_LightRepair) {Npchar.Perks.List.LightRepair = "1"; return 1;}
	Perkselect = Perkselect - P_LightRepair;
	If (Perkselect <= P_InstantRepair) {Npchar.Perks.List.InstantRepair = "1"; return 1;}
	Perkselect = Perkselect - P_InstantRepair;
	If (Perkselect <= P_Trustworthy) {Npchar.Perks.List.Trustworthy = "1"; return 1;}
	Perkselect = Perkselect - P_Trustworthy;
	If (Perkselect <= P_BasicCommerce) {Npchar.Perks.List.BasicCommerce = "1"; return 1;}
	Perkselect = Perkselect - P_BasicCommerce;
	// GZ -->
	If (Perkselect <= P_BasicLandOwner) {Npchar.Perks.List.BasicLandOwner = "1"; return 1;}
	Perkselect = Perkselect - P_BasicLandOwner;
	// GZ <--
	If (Perkselect <= P_AdvancedCommerce) {Npchar.Perks.List.AdvancedCommerce = "1"; return 1;}

	//Npchar.Experience = makeint(Npchar.Experience) + 1000;

	Npchar.Perks.FreePoints = Makeint(Npchar.Perks.Freepoints) + 1;

	return 1;
}


//---------------------------------SetSkillmodifiers------------------------------------------------------------------------------------------- 
void LAi_Officer_SetSkillmodifiers_and_Perks(ref Npchar, int Skillvariation)
{
	int LeadershipValue = 0;
	int CombatValue = 0;
	int CannonValue = 0;
	int NavigatorValue = 0;
	int GrapplingValue = 0;
	int DefenseValue = 0;
	int QMasterValue = 0;
	int nRank = sti(Npchar.rank);
// added by MAXIMUS [new skill-change] -->
	if(AUTO_SKILL_SYSTEM)
	{
		for(int c = 0; c < 10; c++)
		{
			string curSkill = GetSkillName(c);
			if(CheckAttribute(Npchar,"skill."+curSkill))
			{
				if(CheckAttribute(Npchar,"skill."+curSkill+".bonus")) { DeleteAttribute(Npchar,"skill."+curSkill+".bonus"); }
			}
		}
		if(SKILLS_UP_BONUS<10) SKILLS_UP_BONUS = 10;
		if(SKILLS_UP_BONUS>99) SKILLS_UP_BONUS = 99;
		float curSpeed = makefloat(stf(SKILLS_UP_BONUS+100)/100);
		string bonusSkillName = GetRandSkillName(Npchar);
	}
// added by MAXIMUS [new skill-change] <--
	
	switch(Npchar.quest.officertype)	  
	{
		case OFFIC_TYPE_RANDCHAR:

			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = Skillvariation*8;
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = Skillvariation;

			LeadershipValue = Skillvariation;
			CombatValue = Skillvariation*6;
			CannonValue = Skillvariation;
			NavigatorValue = Skillvariation;
			GrapplingValue = Skillvariation;
			DefenseValue = Skillvariation;
			QMasterValue = Skillvariation;
			Npchar.money = GetRandCharMoney(Npchar, 50);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.(bonusSkillName).bonus = curSpeed;
				bonusSkillName = GetRandSkillName(Npchar);
				Npchar.skill.(bonusSkillName).bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_CIVILIAN:

			Npchar.quest.LeadershipModify = rand(makeint(Skillvariation*1.0));
			Npchar.quest.FencingModify = rand(makeint(Skillvariation*1.25));
			Npchar.quest.SailingModify = rand(makeint(Skillvariation*1.33));
			Npchar.quest.AccuracyModify = rand(makeint(Skillvariation*0.5));
			Npchar.quest.CannonsModify = rand(makeint(Skillvariation*0.5));
			Npchar.quest.GrapplingModify = rand(makeint(Skillvariation*0.75));
			Npchar.quest.RepairModify = rand(makeint(Skillvariation*2.0));
			Npchar.quest.DefenseModify = rand(makeint(Skillvariation*0.75));
			Npchar.quest.CommerceModify = rand(makeint(Skillvariation*2.0));
			Npchar.quest.SneakModify = rand(makeint(Skillvariation*2.0));

			LeadershipValue = rand(makeint(Skillvariation*1.0));
			CombatValue = rand(makeint(Skillvariation*1.25));
			CannonValue = rand(makeint(Skillvariation*0.5));
			NavigatorValue = rand(makeint(Skillvariation*1.33));
			GrapplingValue = rand(makeint(Skillvariation*0.75));
			DefenseValue = rand(makeint(Skillvariation*0.75));
			QMasterValue = rand(makeint(Skillvariation*2.0));
			Npchar.money = GetRandCharMoney(Npchar, rand(25)*2 + 25);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.(bonusSkillName).bonus = curSpeed;
				bonusSkillName = GetRandSkillName(Npchar);
				Npchar.skill.(bonusSkillName).bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_CAPMERCHANT:

			Npchar.quest.LeadershipModify = 75;
			Npchar.quest.FencingModify = 50;
			Npchar.quest.SailingModify = 120;
			Npchar.quest.AccuracyModify = 50;
			Npchar.quest.CannonsModify = 40;
			Npchar.quest.GrapplingModify = 67;
			Npchar.quest.RepairModify = 120;
			Npchar.quest.DefenseModify = 67;
			Npchar.quest.CommerceModify = 150;
			Npchar.quest.SneakModify = 100;

			LeadershipValue = 150;
			CombatValue = 50;
			CannonValue = 50;
			NavigatorValue = 100;
			GrapplingValue = 67;
			DefenseValue = 67;
			QMasterValue = 200;
			Npchar.money = GetRandCharMoney(Npchar, 500);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Sailing.bonus = curSpeed;
				Npchar.skill.Commerce.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_CAPNAVY:

			Npchar.quest.LeadershipModify = 100;
			Npchar.quest.FencingModify = 70;
			Npchar.quest.SailingModify = 80;
			Npchar.quest.AccuracyModify = 100;
			Npchar.quest.CannonsModify = 100;
			Npchar.quest.GrapplingModify = 67;
			Npchar.quest.RepairModify = 50;
			Npchar.quest.DefenseModify = 80;
			Npchar.quest.CommerceModify = 25;
			Npchar.quest.SneakModify = 25;

			LeadershipValue = 100;
			CombatValue = 70;
			CannonValue = 150;
			NavigatorValue = 85;
			GrapplingValue = 67;
			DefenseValue = 100;
			QMasterValue = 30;
			Npchar.money = GetRandCharMoney(Npchar, 120);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Accuracy.bonus = curSpeed;
				Npchar.skill.Defense.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_CAPPIRATE:

			Npchar.quest.LeadershipModify = 100;
			Npchar.quest.FencingModify = 90;
			Npchar.quest.SailingModify = 110;
			Npchar.quest.AccuracyModify = 80;
			Npchar.quest.CannonsModify = 80;
			Npchar.quest.GrapplingModify = 100;
			Npchar.quest.RepairModify = 50;
			Npchar.quest.DefenseModify = 75;
			Npchar.quest.CommerceModify = 50;
			Npchar.quest.SneakModify = 75;

			LeadershipValue = 100;
			CombatValue = 90;
			CannonValue = 80;
			NavigatorValue = 110;
			GrapplingValue = 100;
			DefenseValue = 75;
			QMasterValue = 50;
			Npchar.money = makeint(pow((Rand(3) + 1),2) * GetRandCharMoney(Npchar, 50));
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Cannons.bonus = curSpeed;
				Npchar.skill.Grappling.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_BOATSWAIN:

			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = 120;
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = 100;
			Npchar.quest.RepairModify = 30;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = Skillvariation;
			If (nRank >= 10) {Npchar.quest.RepairModify = 70;}

			CombatValue = 60;
			GrapplingValue = 60;
			DefenseValue = 25;
			If (nRank >= 10) 
			{
				LeadershipValue = 25;
				QMasterValue = 25;
				CannonValue = 0;
				NavigatorValue = 0;
			}
			If (nRank >=15) {NavigatorValue = 10;}
			Npchar.money = GetRandCharMoney(Npchar, 50);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Leadership.bonus = curSpeed;
				Npchar.skill.Fencing.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_CANNONEER:

			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = 50;
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = 100;
			Npchar.quest.CannonsModify = 100;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = Skillvariation;
			If(nRank >= 10) {Npchar.quest.GrapplingModify = 40;}

			CombatValue = 40;
			CannonValue = 70;
			If(nRank >=12)
			{
				GrapplingValue = 15;
				DefenseValue = 15;
				QMasterValue = 0;
				NavigatorValue = 0;
			}
			If (nRank >= 18) {NavigatorValue = 10;}
			Npchar.money = GetRandCharMoney(Npchar, 50);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Cannons.bonus = curSpeed;
				Npchar.skill.Accuracy.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_QMASTER:

			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = 50;
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = 100;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = 100;
			Npchar.quest.SneakModify = Skillvariation;
			If(nRank >= 10) {Npchar.quest.DefenseModify = 40;}

			CombatValue = 20;
			QMasterValue = 70;
			If(nRank >= 10)
			{
				CannonValue = 0;
				GrapplingValue = 15;
				NavigatorValue = 0;
				DefenseValue = 15;
			}
			If (nRank >=14) {NavigatorValue = 10;}
			Npchar.money = GetRandCharMoney(Npchar, 75);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Commerce.bonus = curSpeed;
				Npchar.skill.Sneak.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_NAVIGATOR:
					
			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = 50;
			Npchar.quest.SailingModify = 100;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = 100;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = Skillvariation;
			If(nRank >= 10) {Npchar.quest.SneakModify = 40;}

			CombatValue = 30;
			NavigatorValue = 70;
			If(nRank >= 11){DefenseValue = 25; LeadershipValue = 20;}
			If (nRank >= 15) {QMasterValue = 10;}
			Npchar.money = GetRandCharMoney(Npchar, 67);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Sailing.bonus = curSpeed;
				Npchar.skill.Grappling.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_FIRSTMATE:

			Npchar.quest.LeadershipModify = 100;
			Npchar.quest.FencingModify = 50; 
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = 100;
			If(nRank >= 10) {Npchar.quest.CommerceModify = 40;}

			LeadershipValue = 60;
			CombatValue = 50;
			DefenseValue = 50;
			If (nRank >= 9) {QMasterValue = 30;}
			If (nRank >= 14) {NavigatorValue = 15;}
			Npchar.money = GetRandCharMoney(Npchar, 90);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Leadership.bonus = curSpeed;
				Npchar.skill.Defense.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;
// MAXIMUS officers -->

		case OFFIC_TYPE_CARPENTER:

			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = Skillvariation; 
			Npchar.quest.SailingModify = 100;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = 100;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = 50;
			If(nRank >= 10) {Npchar.quest.CommerceModify = 40;}

			CombatValue = 50;
			If (nRank >= 10) 
			{
				LeadershipValue = 25;
			}
			Npchar.money = GetRandCharMoney(Npchar, 75);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Defense.bonus = curSpeed;
				Npchar.skill.Repair.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_DOCTOR:

			Npchar.quest.LeadershipModify = 100;
			Npchar.quest.FencingModify = Skillvariation; 
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = 100;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = 100;
			If(nRank >= 10) {Npchar.quest.CommerceModify = 40;}

			CombatValue = 50;
			DefenseValue = 50;
			If (nRank >= 10) 
			{
				LeadershipValue = 25;
			}
			Npchar.money = GetRandCharMoney(Npchar, 67);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Defense.bonus = curSpeed;
				Npchar.skill.Sneak.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;

		case OFFIC_TYPE_ABORDAGE:

			Npchar.quest.LeadershipModify = 100;
			Npchar.quest.FencingModify = 100;
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = 100;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = 75;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = 75;

			If (nRank >= 10) 
			{
				LeadershipValue = 25;
			}
			CombatValue = 100;
			Npchar.money = pow((Rand(3) + 1),2) * GetRandCharMoney(Npchar, 50);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.Grappling.bonus = curSpeed;
				Npchar.skill.Fencing.bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		break;
// MAXIMUS officers <--

		//default: // scheffnow - bfs
			Npchar.quest.LeadershipModify = Skillvariation;
			Npchar.quest.FencingModify = 45; 
			Npchar.quest.SailingModify = Skillvariation;
			Npchar.quest.AccuracyModify = Skillvariation;
			Npchar.quest.CannonsModify = Skillvariation;
			Npchar.quest.GrapplingModify = Skillvariation;
			Npchar.quest.RepairModify = Skillvariation;
			Npchar.quest.DefenseModify = Skillvariation;
			Npchar.quest.CommerceModify = Skillvariation;
			Npchar.quest.SneakModify = Skillvariation;
			//If(nRank >= 10) {Npchar.quest.CommerceModify = 40;}

			LeadershipValue = 15;
			CombatValue = 20;
			CannonValue = 10;
			NavigatorValue = 10;
			GrapplingValue = 10;
			DefenseValue = 10;
			QMasterValue = 20;
			Npchar.money = GetRandCharMoney(Npchar, 50);
// added by MAXIMUS [new skill-change] -->
			if(AUTO_SKILL_SYSTEM)
			{
				Npchar.skill.(bonusSkillName).bonus = curSpeed;
				bonusSkillName = GetRandSkillName(Npchar);
				Npchar.skill.(bonusSkillName).bonus = curSpeed;
			}
// added by MAXIMUS [new skill-change] <--
		
			//If (nRank >= 9) {QMasterValue = 30;}
			//If (nRank >= 14) {NavigatorValue = 15;}
	}
//----------------------------------Set Perks for Officers--------------------------------------------------------------------------------------------------------------------------------

	DeleteAttribute(Npchar,"perks.List.BasicDefense");
	DeleteAttribute(Npchar,"perks.List.AdvancedDefense");
	DeleteAttribute(Npchar,"perks.List.Toughness"); //MAXIMUS 20.09.2006
	DeleteAttribute(Npchar,"perks.List.SwordplayProfessional");
	DeleteAttribute(Npchar,"perks.List.CriticalHit");
	DeleteAttribute(Npchar,"perks.List.Gunman");
	DeleteAttribute(Npchar,"perks.List.GunProfessional");
	DeleteAttribute(Npchar,"perks.list.FastReload");
	DeleteAttribute(Npchar,"perks.list.ImmediateReload");
	DeleteAttribute(Npchar,"perks.list.HullDamageUp");
	DeleteAttribute(Npchar,"perks.list.SailsDamageUp");
	DeleteAttribute(Npchar,"perks.list.CrewDamageUp");
	DeleteAttribute(Npchar,"perks.list.LongRangeShoot");
	DeleteAttribute(Npchar,"perks.list.CriticalShoot");
	DeleteAttribute(Npchar,"perks.list.CannonProfessional");
	DeleteAttribute(Npchar,"perks.list.ShipSpeedUp");
	DeleteAttribute(Npchar,"perks.list.ShipTurnRateUp");
	DeleteAttribute(Npchar,"perks.list.StormProfessional");
	DeleteAttribute(Npchar,"perks.list.Turn180");
	DeleteAttribute(Npchar,"perks.list.SandbankManeuver");
	DeleteAttribute(Npchar,"perks.list.SailingProfessional");
	DeleteAttribute(Npchar,"perks.list.LongRangeGrappling");
	DeleteAttribute(Npchar,"perks.list.GrapplingProfessional");
	DeleteAttribute(Npchar,"perks.list.InstantBoarding");
	DeleteAttribute(Npchar,"perks.list.LightRepair");
	DeleteAttribute(Npchar,"perks.list.InstantRepair");
	DeleteAttribute(Npchar,"perks.list.BasicBattleState");
	DeleteAttribute(Npchar,"perks.list.AdvancedBattleState");
	DeleteAttribute(Npchar,"perks.list.ShipDefenseProfessional");
	DeleteAttribute(Npchar,"perks.list.IronWill");
	DeleteAttribute(Npchar,"perks.list.SharedExperience");
	DeleteAttribute(Npchar,"perks.list.Trustworthy");
	DeleteAttribute(Npchar,"perks.list.BasicCommerce");
	DeleteAttribute(Npchar,"perks.list.AdvancedCommerce");
	DeleteAttribute(Npchar,"perks.list.BasicLandOwner"); // GZ

	//Npchar.perks.List.BasicDefense = "1"; 
	//Npchar.perks.Freepoints = makeint(Npchar.perks.Freepoints) -1;

	SelectPerksForOfficers(Npchar, LeadershipValue, CombatValue, CannonValue, NavigatorValue, GrapplingValue, DefenseValue, QMasterValue);
	
}


//----------------------------------Set Skills for Officers----------------------------------------------------------------------------------			

void LAi_SetSkill_on_Modifier(ref Npchar)
{
	Npchar.skill.Leadership = 1; DeleteAttribute(&Npchar,"skill.Leadership.mod"); // NK/GZ items mod
	Npchar.skill.Fencing = 1; DeleteAttribute(&Npchar,"skill.Fencing.mod"); // NK/GZ items mod
	Npchar.skill.Sailing = 1; DeleteAttribute(&Npchar,"skill.Sailing.mod"); // NK/GZ items mod
	Npchar.skill.Accuracy = 1; DeleteAttribute(&Npchar,"skill.Accuracy.mod"); // NK/GZ items mod
	Npchar.skill.Cannons = 1; DeleteAttribute(&Npchar,"skill.Cannons.mod"); // NK/GZ items mod
	Npchar.skill.Grappling = 1; DeleteAttribute(&Npchar,"skill.Grappling.mod"); // NK/GZ items mod
	Npchar.skill.Repair = 1; DeleteAttribute(&Npchar,"skill.Repair.mod"); // NK/GZ items mod
	Npchar.skill.Defence = 1; DeleteAttribute(&Npchar,"skill.Defence.mod"); // NK/GZ items mod
	Npchar.skill.Commerce = 1; DeleteAttribute(&Npchar,"skill.Commerce.mod"); // NK/GZ items mod
	Npchar.skill.Sneak = 1; DeleteAttribute(&Npchar,"skill.Sneak.mod"); // NK/GZ items mod

	int Skillpoints = makeint(Npchar.skill.freeskill);
	int PerkRest = 0;
	int SumValue,Placeskill;
	SumValue = makeint(Npchar.quest.LeadershipModify) + makeint(Npchar.quest.FencingModify) + makeint(Npchar.quest.SailingModify) + makeint(Npchar.quest.AccuracyModify) + makeint(Npchar.quest.CannonsModify) + makeint(Npchar.quest.GrapplingModify) + makeint(Npchar.quest.RepairModify) + makeint(Npchar.quest.DefenseModify) + makeint(Npchar.quest.CommerceModify) + makeint(Npchar.quest.SneakModify);

	for (int i = 0; i < Skillpoints; i++)
	{
		Placeskill = Rand(SumValue - 1) + 1;

		If (Placeskill <= makeint(Npchar.quest.LeadershipModify)) {Npchar.skill.Leadership = makeint(Npchar.skill.Leadership) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.LeadershipModify);
		If (Placeskill <= makeint(Npchar.quest.FencingModify))	  {Npchar.skill.Fencing = makeint(Npchar.skill.Fencing) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.FencingModify);
		If (Placeskill <= makeint(Npchar.quest.SailingModify))	  {Npchar.skill.Sailing = makeint(Npchar.skill.Sailing) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.SailingModify);
		If (Placeskill <= makeint(Npchar.quest.AccuracyModify))   {Npchar.skill.Accuracy = makeint(Npchar.skill.Accuracy) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.AccuracyModify);
		If (Placeskill <= makeint(Npchar.quest.CannonsModify))	  {Npchar.skill.Cannons = makeint(Npchar.skill.Cannons) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.CannonsModify);
		If (Placeskill <= makeint(Npchar.quest.GrapplingModify))  {Npchar.skill.Grappling = makeint(Npchar.skill.Grappling) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.GrapplingModify);
		If (Placeskill <= makeint(Npchar.quest.RepairModify))	  {Npchar.skill.Repair = makeint(Npchar.skill.Repair) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.RepairModify);
		If (Placeskill <= makeint(Npchar.quest.DefenseModify))	  {Npchar.skill.Defence = makeint(Npchar.skill.Defence) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.DefenseModify);
		If (Placeskill <= makeint(Npchar.quest.CommerceModify))   {Npchar.skill.Commerce = makeint(Npchar.skill.Commerce) + 1; continue;}
		Placeskill = Placeskill - makeint(Npchar.quest.CommerceModify);
		If (Placeskill <= makeint(Npchar.quest.SneakModify))	  {Npchar.skill.Sneak = makeint(Npchar.skill.Sneak) + 1; continue;}
		PerkRest = PerkRest + 1;
	}  

	if(makeint(Npchar.skill.Leadership) >= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Leadership) - 10; Npchar.skill.Leadership = 10;} 
	if(makeint(Npchar.skill.Fencing)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Fencing) - 10; Npchar.skill.Fencing = 10;}
	if(makeint(Npchar.skill.Sailing)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Sailing) - 10; Npchar.skill.Sailing = 10;}
	if(makeint(Npchar.skill.Accuracy)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Accuracy) - 10; Npchar.skill.Accuracy = 10;}
	if(makeint(Npchar.skill.Cannons)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Cannons) - 10; Npchar.skill.Cannons = 10;}
	if(makeint(Npchar.skill.Grappling)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Grappling) - 10; Npchar.skill.Grappling = 10;}
	if(makeint(Npchar.skill.Repair) >= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Repair) - 10; Npchar.skill.Repair = 10;}
	if(makeint(Npchar.skill.Defence)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Defence) - 10; Npchar.skill.Defence = 10;}
	if(makeint(Npchar.skill.Commerce)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Commerce) - 10; Npchar.skill.Commerce = 10;}
	if(makeint(Npchar.skill.Sneak)	>= 10) {PerkRest = PerkRest + makeint(Npchar.skill.Sneak) - 10; Npchar.skill.Sneak = 10;}

	Npchar.skill.Freeskill = PerkRest;
}
