/** * @brief Checks whether two factions are allies or not. * * @param a Faction A. * @param b Faction B. * @return 1 if A and B are allies, 0 otherwise. */ int areAllies( int a, int b ) { Faction *fa, *fb; int i; /* If they are the same they must be allies. */ if (a==b) return 1; /* we assume player becomes allies with high rating */ if (a==FACTION_PLAYER) { if (faction_isFaction(b)) { if (faction_stack[b].player > PLAYER_ALLY) return 1; else return 0; } else { WARN("%d is an invalid faction", b); return 0; } } if (b==FACTION_PLAYER) { if (faction_isFaction(a)) { if (faction_stack[a].player > PLAYER_ALLY) return 1; else return 0; } else { WARN("%d is an invalid faction", a); return 0; } } if ((a==FACTION_PLAYER) || (b==FACTION_PLAYER)) /* player has no allies */ return 0; /* handle a */ if (faction_isFaction(a)) fa = &faction_stack[a]; else { /* a isn't valid */ WARN("%d is an invalid faction", a); return 0; } /* handle b */ if (faction_isFaction(b)) fb = &faction_stack[b]; else { /* b is invalid */ WARN("%d is an invalid faction", b); return 0; } /* both are factions */ if (fa && fb) { for (i=0;i<fa->nallies;i++) if (fa->allies[i] == b) return 1; for (i=0;i<fb->nallies;i++) if(fb->allies[i] == a) return 1; } return 0; }
/** * @brief Checks whether two factions are enemies. * * @param a Faction A. * @param b Faction B. * @return 1 if A and B are enemies, 0 otherwise. */ int areEnemies( int a, int b) { Faction *fa, *fb; int i; if (a==b) return 0; /* luckily our factions aren't masochistic */ /* player handled seperately */ if (a==FACTION_PLAYER) { if (faction_isFaction(b)) { if (faction_stack[b].player < PLAYER_ENEMY) return 1; else return 0; } else { WARN("areEnemies: %d is an invalid faction", b); return 0; } } if (b==FACTION_PLAYER) { if (faction_isFaction(a)) { if (faction_stack[a].player < PLAYER_ENEMY) return 1; else return 0; } else { WARN("areEnemies: %d is an invalid faction", a); return 0; } } /* handle a */ if (faction_isFaction(a)) fa = &faction_stack[a]; else { /* a isn't valid */ WARN("areEnemies: %d is an invalid faction", a); return 0; } /* handle b */ if (faction_isFaction(b)) fb = &faction_stack[b]; else { /* b is invalid */ WARN("areEnemies: %d is an invalid faction", b); return 0; } /* both are factions */ if (fa && fb) { for (i=0;i<fa->nenemies;i++) if (fa->enemies[i] == b) return 1; for (i=0;i<fb->nenemies;i++) if(fb->enemies[i] == a) return 1; } return 0; }
/** * @brief Grabs a (for now) random fleet out of the stack for the faction. * * @param faction Which faction to get a fleet for. * @return a pointer to a fleet, or NULL if not found. */ Fleet* fleet_grab( const int faction ) { Fleet* fleet; int inf = 0; int rnd; /* Check for a legal faction. */ if(!faction_isFaction(faction)) { WARN("%i is not a faction.", faction); return NULL; } /* Try to find a fleet of the faction. */ while(1) { /* Check for infinite loop. */ if(inf > 100 * nfleets) { WARN("Could not find a fleet for faction %s.", faction_name(faction)); return NULL; } inf++; /* Get a fleet and check its faction. */ rnd = RNGF() * (nfleets - 0.01); fleet = &fleet_stack[rnd]; } return fleet; }
/** * @brief Modifies the player's standing without affecting others. * * Does not affect allies nor enemies and does not run through the Lua script. * * @param f Faction whose standing to modify. * @param mod Amount to modify standing by. * * @sa faction_modPlayer */ void faction_modPlayerRaw( int f, double mod ) { Faction *faction; HookParam hparam[3]; if (!faction_isFaction(f)) { WARN("%d is an invalid faction", f); return; } faction = &faction_stack[f]; faction->player += mod; /* Run hook if necessary. */ hparam[0].type = HOOK_PARAM_FACTION; hparam[0].u.lf.f = f; hparam[1].type = HOOK_PARAM_NUMBER; hparam[1].u.num = mod; hparam[2].type = HOOK_PARAM_SENTINEL; hooks_runParam( "standing", hparam ); /* Sanitize just in case. */ faction_sanitizePlayer( faction ); /* Tell space the faction changed. */ space_factionChange(); }
/** * @brief Gets the list of enemies of a faction. * * @param f Faction to get enemies of. * @param[out] Number of enemies. * @return The enemies of the faction. */ int* faction_getEnemies( int f, int *n ) { int i, nenemies; int *enemies; if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } /* Player's faction ratings can change, so regenerate each call. */ if (f == FACTION_PLAYER) { nenemies = 0; enemies = malloc(sizeof(int)*faction_nstack); for (i=0; i<faction_nstack; i++) if (faction_isPlayerEnemy(i)) enemies[nenemies++] = i; enemies = realloc(enemies, sizeof(int)*nenemies); free(faction_stack[f].enemies); faction_stack[f].enemies = enemies; faction_stack[f].nenemies = nenemies; } *n = faction_stack[f].nenemies; return faction_stack[f].enemies; }
/** * @brief Gets the list of allies of a faction. * * @param f Faction to get allies of. * @param[out] Number of allies. * @return The allies of the faction. */ int* faction_getAllies( int f, int *n ) { int i, nallies; int *allies; if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } /* Player's faction ratings can change, so regenerate each call. */ if (f == FACTION_PLAYER) { nallies = 0; allies = malloc(sizeof(int)*faction_nstack); for (i=0; i<faction_nstack; i++) if (faction_isPlayerFriend(i)) allies[nallies++] = i; allies = realloc(allies, sizeof(int)*nallies); free(faction_stack[f].allies); faction_stack[f].allies = allies; faction_stack[f].nallies = nallies; } *n = faction_stack[f].nallies; return faction_stack[f].allies; }
/** * @brief Modifies the player's standing with a faction. * * Affects enemies and allies too. * * @param f Faction to modify player's standing. * @param mod Modifier to modify by. */ void faction_modPlayer( int f, double mod, const char *source ) { int i; Faction *faction; if (!faction_isFaction(f)) { WARN("%d is an invalid faction", f); return; } faction = &faction_stack[f]; /* Modify faction standing with parent faction. */ faction_modPlayerLua( f, mod, source, 0 ); /* Now mod allies to a lesser degree */ for (i=0; i<faction->nallies; i++) { /* Modify faction standing */ faction_modPlayerLua( faction->allies[i], mod, source, 1 ); } /* Now mod enemies */ for (i=0; i<faction->nenemies; i++) { /* Modify faction standing. */ faction_modPlayerLua( faction->enemies[i], -mod, source, 1 ); } }
/** * @brief Gets the state assosciated to the faction scheduler. */ lua_State *faction_getState( int f ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } return faction_stack[f].state; }
/** * @brief Gets the colour of the faction * * @param f Faction to get the colour of. * @return The faction's colour */ glColour* faction_colour( int f ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } return faction_stack[f].colour; }
/** * @brief Gets the faction's tiny logo (24x24 or smaller). * * @param f Faction to get the logo of. * @return The faction's tiny logo image. */ glTexture* faction_logoTiny( int f ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } return faction_stack[f].logo_tiny; }
/** * @brief Gets the player's default standing with a faction. * * @param f Faction to get player's default standing from. * @return The default standing the player has with the faction. */ double faction_getPlayerDef( int f ) { if (faction_isFaction(f)) return faction_stack[f].player_def; else { WARN("%d is an invalid faction", f); return -1000; } }
/** * @brief Modifies the player's standing without affecting others. * * Does not affect allies nor enemies. * * @param f Faction whose standing to modify. * @param mod Amount to modify standing by. * * @sa faction_modPlayer */ void faction_modPlayerSingle( int f, double mod, const char *source ) { if (!faction_isFaction(f)) { WARN("%d is an invalid faction", f); return; } faction_modPlayerLua( f, mod, source, 0 ); }
/** * @brief Gets the list of allies of a faction. * * @param f Faction to get allies of. * @param[out] Number of allies. * @return The allies of the faction. */ int* faction_getAllies( int f, int *n ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } *n = faction_stack[f].nallies; return faction_stack[f].allies; }
/** * @brief Gets the faction's long name (formal). * * @param f Faction to get the name of. * @return The faction's long name. */ char* faction_longname( int f ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } if (faction_stack[f].longname != NULL) return faction_stack[f].longname; return faction_stack[f].name; }
/** * @brief Get's a factions "real" name. * * @param f Faction to get the name of. * @return Name of the faction. */ char* faction_name( int f ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } /* Don't want player to see his escorts as "Player" faction. */ if (f == FACTION_PLAYER) return "Escort"; return faction_stack[f].name; }
/** * @brief Gets a factions short name. * * @param f Faction to get the name of. * @return Name of the faction. */ char* faction_shortname( int f ) { if (!faction_isFaction(f)) { WARN("Faction id '%d' is invalid.",f); return NULL; } /* Don't want player to see their escorts as "Player" faction. */ if (f == FACTION_PLAYER) return "Escort"; /* Possibly get display name. */ if (faction_stack[f].displayname != NULL) return faction_stack[f].displayname; return faction_stack[f].name; }
/** * @brief Modifies the player's standing without affecting others. * * Does not affect allies nor enemies. * * @param f Faction whose standing to modiy. * @param mod Amount to modiy standing by. * * @sa faction_modPlayer */ void faction_modPlayerRaw( int f, double mod ) { Faction *faction; if (!faction_isFaction(f)) { WARN("%d is an invalid faction", f); return; } faction = &faction_stack[f]; /* Make sure it's not static. */ if (faction_isFlag(faction, FACTION_STATIC)) return; faction->player += mod; faction_sanitizePlayer(faction); }
/** * @brief Modifies the player's standing with a faction. * * Affects enemies and allies too. * * @param f Faction to modify player's standing. * @param mod Modifier to modify by. * * @sa faction_modPlayerRaw */ void faction_modPlayer( int f, double mod ) { int i; Faction *faction; double m; if (!faction_isFaction(f)) { WARN("%d is an invalid faction", f); return; } /* Modify faction standing with parent faction. */ faction_modPlayerRaw( f, mod ); /* Now mod allies to a lesser degree */ faction = &faction_stack[f]; for (i=0; i<faction->nallies; i++) { /* Enemies are made faster. */ m = RNG_2SIGMA()/4. + 0.5; if (mod > 0.) m *= 0.75; /* Modify faction standing */ faction_modPlayerRaw( faction->allies[i], m*mod ); } /* Now mod enemies */ for (i=0; i<faction->nenemies; i++) { /* Enemies are made faster. */ m = RNG_2SIGMA()/4. + 0.5; if (mod < 0.) m *= 0.75; /* Modify faction standing. */ faction_modPlayerRaw( faction->enemies[i], -m*mod ); } }