From 80f0114ee5e0ba0a769f1673565cfa3b0137e44c Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Thu, 16 Jan 2025 16:54:40 -0600 Subject: [PATCH] mission goals hook --- code/mission/missiongoals.cpp | 39 ++++++++++++++++++++++++--------- code/scripting/global_hooks.cpp | 9 ++++++++ code/scripting/global_hooks.h | 2 ++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/code/mission/missiongoals.cpp b/code/mission/missiongoals.cpp index ac96eff64d0..54dca3c6bb8 100644 --- a/code/mission/missiongoals.cpp +++ b/code/mission/missiongoals.cpp @@ -33,6 +33,7 @@ #include "parse/parselo.h" #include "parse/sexp.h" #include "playerman/player.h" +#include "scripting/global_hooks.h" #include "tracing/tracing.h" #include "ui/ui.h" @@ -738,28 +739,46 @@ void mission_goal_status_change( int goal_num, int new_status) send_mission_goal_info_packet( goal_num, new_status, -1 ); } + bool isOverride = false; + + if (scripting::hooks::OnMissionObjectivesChanged->isActive()) { + auto paramList = scripting::hook_param_list(scripting::hook_param("Name", 's', Mission_goals[goal_num].name), + scripting::hook_param("Description", 's', Mission_goals[goal_num].message), + scripting::hook_param("Type", 'i', type + 1), + scripting::hook_param("State", 'b', new_status == GOAL_COMPLETE)); + if (scripting::hooks::OnMissionObjectivesChanged->isOverride(paramList)) { + isOverride = true; // Override here only prevents displaying the goals and playing the music. Everything + // else still runs. + } + scripting::hooks::OnMissionObjectivesChanged->run(paramList); + } + type = Mission_goals[goal_num].type & GOAL_TYPE_MASK; Mission_goals[goal_num].satisfied = new_status; if ( new_status == GOAL_FAILED ) { // don't display bonus goal failure - if ( type != BONUS_GOAL ) { + if (type != BONUS_GOAL ) { - // only do HUD and music is goals are my teams goals. + // only do HUD and music if goals are my teams goals. if ( (Game_mode & GM_NORMAL) || ((Net_player != NULL) && (Net_player->p_info.team == Mission_goals[goal_num].team)) ) { - hud_add_objective_messsage(type, new_status); - if ( !( Mission_goals[goal_num].flags & MGF_NO_MUSIC ) ) { // maybe play event music - event_music_primary_goal_failed(); + if (!isOverride) { + hud_add_objective_messsage(type, new_status); + if (!(Mission_goals[goal_num].flags & MGF_NO_MUSIC)) { // maybe play event music + event_music_primary_goal_failed(); + } } } } mission_log_add_entry( LOG_GOAL_FAILED, Mission_goals[goal_num].name.c_str(), nullptr, goal_num ); } else if ( new_status == GOAL_COMPLETE ) { if ( (Game_mode & GM_NORMAL) || ((Net_player != NULL) && (Net_player->p_info.team == Mission_goals[goal_num].team))) { - hud_add_objective_messsage(type, new_status); - // cue for Event Music - if ( !(Mission_goals[goal_num].flags & MGF_NO_MUSIC) ) { - event_music_primary_goals_met(); - } + if (!isOverride) { + hud_add_objective_messsage(type, new_status); + // cue for Event Music + if (!(Mission_goals[goal_num].flags & MGF_NO_MUSIC)) { + event_music_primary_goals_met(); + } + } mission_log_add_entry( LOG_GOAL_SATISFIED, Mission_goals[goal_num].name.c_str(), nullptr, goal_num ); } diff --git a/code/scripting/global_hooks.cpp b/code/scripting/global_hooks.cpp index 4a5c72bfce7..fd0b8145f03 100644 --- a/code/scripting/global_hooks.cpp +++ b/code/scripting/global_hooks.cpp @@ -437,6 +437,15 @@ const std::shared_ptr> OnCheat = Hook<>::Factory("On Cheat", { "Cheat", "string", "The cheat code the user typed" }, }); +const std::shared_ptr> OnMissionObjectivesChanged = scripting::OverridableHook<>::Factory( + "On Mission Objectives Changed", "Called when a goal is marked as failed or completed.", + { + {"Name", "string", "The name of the objective."}, + {"Description", "string", "The description of the objective."}, + {"Type", "number", "The Objective type. 1 for Primary, 2 for secondary, 3 for bonus."}, + {"State", "boolean", "True if the goal was completed, false if it was failed."}, + }); + const std::shared_ptr> OnMissionAboutToEndHook = Hook<>::Factory("On Mission About To End", "Called when a mission is about to end but has not run any mission-ending logic", {}); diff --git a/code/scripting/global_hooks.h b/code/scripting/global_hooks.h index 1467a91371d..118fe46da7c 100644 --- a/code/scripting/global_hooks.h +++ b/code/scripting/global_hooks.h @@ -81,6 +81,8 @@ extern const std::shared_ptr> OnDialogClose; extern const std::shared_ptr> OnCheat; +extern const std::shared_ptr> OnMissionObjectivesChanged; + extern const std::shared_ptr> OnMissionAboutToEndHook; extern const std::shared_ptr> OnMissionEndHook; extern const std::shared_ptr> OnStateAboutToEndHook;