Skip to content

Commit

Permalink
Merge pull request #6467 from Goober5000/ai_goal_refactor
Browse files Browse the repository at this point in the history
Ai-goal refactor
  • Loading branch information
Goober5000 authored Jan 11, 2025
2 parents 49de71d + 8baee04 commit a214186
Show file tree
Hide file tree
Showing 28 changed files with 423 additions and 240 deletions.
1 change: 1 addition & 0 deletions code/ai/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extern const int Num_ai_flag_names;
#define MAX_ENEMY_DISTANCE 2500.0f // Maximum distance from which a ship will pursue an enemy.

#define MAX_AI_GOALS 5
#define AI_ACTIVE_GOAL_NONE -1
#define AI_ACTIVE_GOAL_DYNAMIC 999

typedef struct ai_class {
Expand Down
2 changes: 1 addition & 1 deletion code/ai/ai_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace AI {
Dockee_index_valid, // when set, index field for dockee is valid
Goal_on_hold, // when set, this goal cannot currently be satisfied, although it could be in the future
Subsys_needs_fixup, // when set, the subsystem index (for a destroy subsystem goal) is invalid and must be gotten from the subsys name stored in docker.name field!!
Goal_override, // paired with AIG_TYPE_DYNAMIC to mean this goal overrides any other goal
Goal_override, // paired with ai_goal_type::DYNAMIC to mean this goal overrides any other goal
Purge, // purge this goal next time we process
Goals_purged, // this goal has already caused other goals to get purged
Depart_sound_played,// Goober5000 - replacement for AL's hack ;)
Expand Down
4 changes: 2 additions & 2 deletions code/ai/aibig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,13 +913,13 @@ static void ai_big_maybe_fire_weapons(float dist_to_enemy, float dot_to_enemy)
}

if (tswp->num_secondary_banks > 0) {
if (!(En_objp->flags[Object::Object_Flags::Protected]) || (aip->goals[0].ai_mode & (AI_GOAL_DISABLE_SHIP | AI_GOAL_DISABLE_SHIP_TACTICAL | AI_GOAL_DISARM_SHIP | AI_GOAL_DISARM_SHIP_TACTICAL))) {
if ( !(En_objp->flags[Object::Object_Flags::Protected]) || ai_goal_is_disable_or_disarm(aip->goals[0].ai_mode) ) {
bool valid_secondary = ai_choose_secondary_weapon(Pl_objp, aip, En_objp);
int current_bank = tswp->current_secondary_bank;
if (current_bank > -1 && valid_secondary) {
weapon_info *swip = &Weapon_info[tswp->secondary_bank_weapons[current_bank]];

if(!(En_objp->flags[Object::Object_Flags::Protected]) || ((aip->goals[0].ai_mode & (AI_GOAL_DISABLE_SHIP | AI_GOAL_DISABLE_SHIP_TACTICAL | AI_GOAL_DISARM_SHIP | AI_GOAL_DISARM_SHIP_TACTICAL)) && swip->wi_flags[Weapon::Info_Flags::Puncture] )) { //override lockdown on protected ships when using anti subsystem weapons - Valathil
if ( !(En_objp->flags[Object::Object_Flags::Protected]) || (ai_goal_is_disable_or_disarm(aip->goals[0].ai_mode) && swip->wi_flags[Weapon::Info_Flags::Puncture]) ) { //override lockdown on protected ships when using anti subsystem weapons - Valathil
// If ship is protected and very low on hits, don't fire missiles.
if (!(En_objp->flags[Object::Object_Flags::Protected]) || (En_objp->hull_strength > 10*swip->damage)) {
if (aip->ai_flags[AI::AI_Flags::Unload_secondaries]) {
Expand Down
30 changes: 14 additions & 16 deletions code/ai/aicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4605,7 +4605,7 @@ void ai_fly_to_target_position(const vec3d* target_pos, bool* pl_done_p=NULL, bo

/* I shouldn't be flying to position for what ever called me any more.
Set mode to none so that default dynamic behaviour gets started up again. */
if ( (aip->active_goal == AI_GOAL_NONE) || (aip->active_goal == AI_ACTIVE_GOAL_DYNAMIC) ) {
if ( (aip->active_goal == AI_ACTIVE_GOAL_NONE) || (aip->active_goal == AI_ACTIVE_GOAL_DYNAMIC) ) {
aip->mode = AIM_NONE;
}

Expand Down Expand Up @@ -4871,12 +4871,10 @@ void ai_fly_to_target_position(const vec3d* target_pos, bool* pl_done_p=NULL, bo
// for itself and in a wing, treat the completion as we would a ship
treat_as_ship = 1;
if ( shipp->wingnum != -1 ) {
int type;

// protect array access from invalid indexes
if ((aip->active_goal >= 0) && (aip->active_goal < MAX_AI_GOALS)) {
type = aip->goals[aip->active_goal].type;
if ( (type == AIG_TYPE_EVENT_WING) || (type == AIG_TYPE_PLAYER_WING) ) {
auto type = aip->goals[aip->active_goal].type;
if ( (type == ai_goal_type::EVENT_WING) || (type == ai_goal_type::PLAYER_WING) ) {
treat_as_ship = 0;
} else {
treat_as_ship = 1;
Expand Down Expand Up @@ -5058,7 +5056,7 @@ int maybe_resume_previous_mode(object *objp, ai_info *aip)
aip->mode = aip->previous_mode;
aip->submode = aip->previous_submode;
aip->submode_start_time = Missiontime;
aip->active_goal = AI_GOAL_NONE;
aip->active_goal = AI_ACTIVE_GOAL_NONE;
aip->mode_time = -1; // Means do forever.
return 1;
}
Expand All @@ -5079,7 +5077,7 @@ int maybe_resume_previous_mode(object *objp, ai_info *aip)
aip->mode = aip->previous_mode;
aip->submode = AIS_GUARD_PATROL;
aip->submode_start_time = Missiontime;
aip->active_goal = AI_GOAL_NONE;
aip->active_goal = AI_ACTIVE_GOAL_NONE;
return 1;
}
}
Expand Down Expand Up @@ -5739,7 +5737,7 @@ int ai_select_primary_weapon(object *objp, object *other_objp, Weapon::Info_Flag
Assert( shipp->ship_info_index >= 0 && shipp->ship_info_index < ship_info_size());

//made it so it only selects puncture weapons if the active goal is to disable something -Bobboau
if ((flags == Weapon::Info_Flags::Puncture) && (Ai_info[shipp->ai_index].goals[0].ai_mode & (AI_GOAL_DISARM_SHIP | AI_GOAL_DISARM_SHIP_TACTICAL | AI_GOAL_DISABLE_SHIP | AI_GOAL_DISABLE_SHIP_TACTICAL)))
if ((flags == Weapon::Info_Flags::Puncture) && ai_goal_is_disable_or_disarm(Ai_info[shipp->ai_index].goals[0].ai_mode))
{
if (swp->current_primary_bank >= 0)
{
Expand Down Expand Up @@ -5966,9 +5964,9 @@ void set_primary_weapon_linkage(object *objp)

// AL 2-11-98: If ship has a disarm or disable goal, don't link unless both weapons are
// puncture weapons
if ( (aip->active_goal != AI_GOAL_NONE) && (aip->active_goal != AI_ACTIVE_GOAL_DYNAMIC) )
if ( (aip->active_goal != AI_ACTIVE_GOAL_NONE) && (aip->active_goal != AI_ACTIVE_GOAL_DYNAMIC) )
{
if ( aip->goals[aip->active_goal].ai_mode & (AI_GOAL_DISABLE_SHIP|AI_GOAL_DISABLE_SHIP_TACTICAL|AI_GOAL_DISARM_SHIP|AI_GOAL_DISARM_SHIP_TACTICAL) )
if ( ai_goal_is_disable_or_disarm(aip->goals[aip->active_goal].ai_mode) )
{
// only continue if both primaries are puncture weapons
if ( swp->num_primary_banks == 2 ) {
Expand Down Expand Up @@ -14974,7 +14972,7 @@ int ai_need_new_target(object *pl_objp, int target_objnum)

// Goober5000 - targeting the same team is allowed if pl_objp is going bonkers
ai_info *pl_aip = &Ai_info[Ships[pl_objp->instance].ai_index];
if (pl_aip->active_goal != AI_GOAL_NONE && pl_aip->active_goal != AI_ACTIVE_GOAL_DYNAMIC) {
if (pl_aip->active_goal != AI_ACTIVE_GOAL_NONE && pl_aip->active_goal != AI_ACTIVE_GOAL_DYNAMIC) {
if (pl_aip->goals[pl_aip->active_goal].flags[AI::Goal_Flags::Target_own_team]) {
return 0;
}
Expand Down Expand Up @@ -15176,7 +15174,7 @@ void ai_frame(int objnum)
if ( ai_need_new_target(Pl_objp, target_objnum) ) {
if ((aip->mode != AIM_EVADE_WEAPON) && (aip->active_goal == AI_ACTIVE_GOAL_DYNAMIC)) {
aip->resume_goal_time = -1;
aip->active_goal = AI_GOAL_NONE;
aip->active_goal = AI_ACTIVE_GOAL_NONE;
} else if (aip->resume_goal_time == -1) {
// AL 12-9-97: Don't allow cargo and navbuoys to set their aip->target_objnum
if ( Ship_info[shipp->ship_info_index].class_type > -1 && (Ship_types[Ship_info[shipp->ship_info_index].class_type].flags[Ship::Type_Info_Flags::AI_auto_attacks]) ) {
Expand Down Expand Up @@ -15241,7 +15239,7 @@ void ai_frame(int objnum)

// If there is a goal to resume and enough time has elapsed, resume the goal.
if ((aip->resume_goal_time > 0) && (aip->resume_goal_time < Missiontime)) {
aip->active_goal = AI_GOAL_NONE;
aip->active_goal = AI_ACTIVE_GOAL_NONE;
aip->resume_goal_time = -1;
target_objnum = find_enemy(objnum, 2000.0f, The_mission.ai_profile->max_attackers[Game_skill_level]);
if (target_objnum != -1) {
Expand Down Expand Up @@ -15733,7 +15731,7 @@ void ai_do_default_behavior(object *obj)
// default behavior in most cases (especially if we're docked) is to just stay put
aip->mode = AIM_NONE;
aip->submode_start_time = Missiontime;
aip->active_goal = AI_GOAL_NONE;
aip->active_goal = AI_ACTIVE_GOAL_NONE;

// if we're not docked, we may modify the behavior a bit
if (!object_is_docked(obj))
Expand Down Expand Up @@ -16443,7 +16441,7 @@ void ai_ship_destroy(int shipnum)
set_target_objnum(other_aip, -1);
// If this ship had a dynamic goal of chasing the dead ship, clear the dynamic goal.
if (other_aip->resume_goal_time != -1)
other_aip->active_goal = AI_GOAL_NONE;
other_aip->active_goal = AI_ACTIVE_GOAL_NONE;
}

if (other_aip->goal_objnum == dead_shipp->objnum) {
Expand Down Expand Up @@ -16610,7 +16608,7 @@ void ai_add_rearm_goal( object *requester_objp, object *support_objp )
// ensures that the player get a higher priority!
requester_aip->ai_flags.set(AI::AI_Flags::Awaiting_repair); // Tell that I'm awaiting repair.
if ( requester_objp->flags[Object::Object_Flags::Player_ship] )
ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_REARM_REPAIR, -1, requester_shipp->ship_name, support_aip );
ai_add_ship_goal_player( ai_goal_type::PLAYER_SHIP, AI_GOAL_REARM_REPAIR, -1, requester_shipp->ship_name, support_aip );
else
ai_add_goal_ship_internal( support_aip, AI_GOAL_REARM_REPAIR, requester_shipp->ship_name, -1, -1 );

Expand Down
Loading

0 comments on commit a214186

Please sign in to comment.