//=====================================================
// Adding XP functions
//=====================================================
//This function is always called. it determins who should get the XP etc
bool AddXP(ref chref, string expName, int _exp, string group)
{
ref chr, capt;
int cn,i;
bool LevelUp = false;
//First catch some weird cases which shouldn't happen in the first place. let log if they do happen.
if(!CheckAttribute(chref,"index")) { if(DEBUG_EXPERIENCE>0) Trace("XP ERROR: Character "+GetMySimpleName(chref)+" has no index"); return false;}
if(sti(chref.index)<0) { if(DEBUG_EXPERIENCE>0){ Trace("XP ERROR: Character "+GetMySimpleName(chref)+" has index smaller than 0")}; return false; }
if(_exp < 0)
{
_exp = -(_exp);
if(DEBUG_EXPERIENCE>0) Trace("XP ERROR: NEGATIVE XP CALL");
}
if(_exp == 0)
{
if(DEBUG_EXPERIENCE>0) Trace("XP ERROR: ZERO XP CALL");
return false;
}
if(DEBUG_EXPERIENCE>0) Trace("XP LOG: Called AddXP for "+GetMySimpleName(chref)+" with skill: "+expName+" and xp: "+_exp+" and group: "+group);
//If we are only doing the player we can return here (for performance reasons)
if(group == XP_GROUP_PLAYER)
{
//Add a difficulty multiplier
float diffmult = 1.0-((GetDifficulty()-1.0) / 6.0); //Let's make it a bit less dependent on difficulty
int xp = makeint(makefloat(_exp)*GetXPmult(expName));
if(AddXPtoChar(chref, expName, makeint(diffmult*xp))) LevelUp = true;
return LevelUp;
}
//Check for shared experience
bool SharedXP = GetOfficersPerkUsing(chref,"SharedExperience");
if(DEBUG_EXPERIENCE>1) { if(SharedXP) Trace("XP LOG: SharedXP is active"); }
//Now handle the officers
if(group == XP_GROUP_OFFIC)
{
float skillmult, tempmult;
capt = GetCharacter(FindCaptainIndex(chref)); // Get the captain of the ship
for(i=-1; i < GetPassengersQuantity(capt); i++) // Start at -1 to include captain
{
if (i == -1) cn = sti(capt.index); // Captain of the group
else cn = GetPassenger(capt, i); // Passenger of this character
if (cn < 0) continue; // Skip invalid characters
chr = GetCharacter(cn); // Reference to the character
if (CheckAttribute(chr,"prisoned") && sti(chr.prisoned)) continue; // Filter prisoned characters
if(DEBUG_EXPERIENCE>1) Trace("XP LOG: Loop "+i+", Checking Officer "+GetMySimpleName(chr)+" with id "+chr.id);
// Determine Skill Multiplier for this character
skillmult = 0.0;
if (cn == sti(chref.index)) skillmult = 1.0; // The character who gained the XP gets 100%
if (expName == "") skillmult = 1.0; // All characters join in "general XP"
if (expName == SKILL_FENCING) // Fencing is a personal skill and should be handled differently
{
if (skillmult < 0.5 && bAllies(chref) && IsOfficer(chr)) skillmult = 0.5; // Character is in the player shore party
}
else // For any non-personal skills
{
tempmult = GetOfficerSkillFactor(chr, expName); // This means some officers can get 200% XP!
if (skillmult < tempmult) skillMult = tempmult; // The officer who gains the XP will ALWAYS get it
}
if (IsMainCharacter(chr)) // Special case for the player, because you cannot focus on all skills at the same time
{
skillmult = 0.5; // Only 50% of all skills
if (expName == SKILL_LEADERSHIP) skillmult = 1.0; // But a captain MUST know how to lead a crew
if (expName == SKILL_SAILING) skillmult = 1.0; // And navigate a ship
}
if (skillmult < 0.5 && SharedXP) skillmult = 0.5; // Sharing XP, so everybody gets at least 50%
if(DEBUG_EXPERIENCE>1) Trace("XP LOG: skillmult = "+skillmult);
// Add Experience
if (skillmult > 0.0) // If any XP is to be added to this character
{
if(AddXP(chr, expName, makeint(_exp*skillmult), XP_GROUP_PLAYER)) LevelUp = true; // Add that XP and see if it resulted in a Level-Up
}
}
}
//Now handle the companions
if(group == XP_GROUP_PARTY)
{
capt = GetCharacter(FindCommanderIndex(chref)); // Get the commander of the fleet
for (i = 0; i <= GetCompanionQuantity(capt); i++) {
cn = GetCompanionIndex(capt, i);
if (cn < 0) continue;
chr = GetCharacter(cn); // Reference to the character
if(DEBUG_EXPERIENCE>1) Trace("XP LOG: Loop "+i+", Checking Companion "+GetMySimpleName(chr)+" with id "+chr.id);
if(AddXP(chr, expName, _exp, XP_GROUP_OFFIC)) LevelUp = true; // Add the XP to the companion and maybe his officers
}
}
//returns true if there is a levelup somewhere
return LevelUp;
}