diff --git a/src/battle/ai.c b/src/battle/ai.c index 7307366..95db6d1 100644 --- a/src/battle/ai.c +++ b/src/battle/ai.c @@ -53,6 +53,8 @@ static void wander(void); static void doWander(void); static int selectWeaponForTarget(Entity *e); static void deployMine(void); +static int isSurrendering(void); +static void doSurrender(void); void doAI(void) { @@ -103,6 +105,11 @@ void doAI(void) return; } + if ((self->aiFlags & AIF_SURRENDERS) && (battle.stats[STAT_TIME] % 6 == 0) && isSurrendering()) + { + return; + } + if (!(self->aiFlags & AIF_AVOIDS_COMBAT)) { if (self->speed) @@ -583,6 +590,54 @@ static int isRetreating(void) return self->flags & EF_RETREATING; } +static int isSurrendering(void) +{ + float chance; + + if (!(self->flags & EF_SURRENDERED)) + { + if (self->health < self->maxHealth) + { + chance = self->health; + chance /= self->maxHealth; + chance *= 100; + + if (rand() % 100 > chance) + { + self->aiActionTime = FPS * 3; + + self->aiFlags |= AIF_AVOIDS_COMBAT; + self->aiFlags &= ~AIF_SURRENDERS; + + self->flags |= EF_MUST_DISABLE; + self->flags |= EF_MISSION_TARGET; + + self->action = doSurrender; + + battle.stats[STAT_ENEMIES_SURRENDERED]++; + + runScriptFunction("ENEMY_SURRENDERED %d", battle.stats[STAT_ENEMIES_SURRENDERED]); + + addHudMessage(colors.white, _("%s has surrendered"), self->name); + + return 1; + } + } + } + + return 0; +} + +static void doSurrender(void) +{ + if (--self->aiActionTime <= 0) + { + self->flags |= EF_SURRENDERED; + + nextAction(); + } +} + static int nearEnemies(void) { int i, numEnemies, x, y; diff --git a/src/battle/fighters.c b/src/battle/fighters.c index 89dc7fb..07a6063 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -345,6 +345,11 @@ void doFighter(void) updateCondition(self->name, TT_DESTROY); updateCondition(self->groupName, TT_DESTROY); + + if (self->flags & EF_SURRENDERED) + { + updateCondition("SURRENDERED", TT_DESTROY); + } } } } diff --git a/src/defs.h b/src/defs.h index e4b1020..85010a0 100644 --- a/src/defs.h +++ b/src/defs.h @@ -109,6 +109,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define EF_NO_THREAT (2 << 23) #define EF_DROPS_ITEMS (2 << 24) #define EF_COMMON_FIGHTER (2 << 25) +#define EF_SURRENDERED (2 << 26) #define AIF_NONE 0 #define AIF_FOLLOWS_PLAYER (2 << 0) @@ -132,6 +133,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define AIF_ASSASSIN (2 << 18) #define AIF_SUSPICIOUS (2 << 19) #define AIF_ZAK_SUSPICIOUS (2 << 20) +#define AIF_SURRENDERS (2 << 21) /* player abilities */ #define BOOST_RECHARGE_TIME (FPS * 7) @@ -376,6 +378,7 @@ enum STAT_CAPITAL_SHIPS_DESTROYED, STAT_CAPITAL_SHIPS_LOST, STAT_MINES_DESTROYED, + STAT_ENEMIES_SURRENDERED, /* add stats before here, so as not to mess up the stats screen */ STAT_TIME, STAT_MAX diff --git a/src/game/stats.c b/src/game/stats.c index 671162d..6a64d74 100644 --- a/src/game/stats.c +++ b/src/game/stats.c @@ -70,6 +70,7 @@ void initStats(void) statDescription[STAT_CAPITAL_SHIPS_DESTROYED] = _("Capital Ships Destroyed"); statDescription[STAT_CAPITAL_SHIPS_LOST] = _("Capital Ships Lost"); statDescription[STAT_MINES_DESTROYED] = _("Mines Destroyed"); + statDescription[STAT_ENEMIES_SURRENDERED] = _("Enemies Surrendered"); statDescription[STAT_TIME] = _("Time Played"); } diff --git a/src/system/lookup.c b/src/system/lookup.c index 6b21b95..7274ef2 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -93,6 +93,7 @@ void initLookups(void) addLookup("AIF_ASSASSIN", AIF_ASSASSIN); addLookup("AIF_SUSPICIOUS", AIF_SUSPICIOUS); addLookup("AIF_ZAK_SUSPICIOUS", AIF_ZAK_SUSPICIOUS); + addLookup("AIF_SURRENDERS", AIF_SURRENDERS); addLookup("DT_ANY", DT_ANY); addLookup("DT_NO_SPIN", DT_NO_SPIN);