Files
WT-Offline/native/wto-net/src/local_mp_nuke.c
T
N0VAN0M4LY 6ab941a086 Award the nuclear strike in LocalMP battles
Online the battle server gives killStreak_bomber_nuclear_success (mode
killStreakNuclearSuccess, 10000 SL) when a player's nuclear bomb
explodes; the LocalMP host has no battle server, so the award was never
given.

The native nuke module now remembers whether the local player launched
the instant-win bomb and, when it explodes, raises the HUD script event
wtoLocalMpNuclearStrike. The battle record keeps it as a strike, sends
it live (wto_battle.fact kind "strike") and reports it after the battle
(strikes{}); the local service awards killStreakNuclearSuccess once per
strike, live as the award banner and in the settlement.

Checked with typecheck, check-gui, check-offsets and the native build;
not yet verified in game.
2026-11-28 19:03:24 +08:00

149 lines
6.5 KiB
C

/*
* Instant win of the nuclear bomb in a LocalMP battle.
*
* Online the battle server ends the battle when a bomb with isInstantWinOnExplode explodes
* (gamedata/weapons/bombguns/us_b61.blk and the other killstreak nuclear bombs); the 2.61.0.37
* client only marks the bomb and plays the explosion. The LocalMP host has no battle server, so
* the battle went on after the explosion. Here the host takes the same decision with the stock
* end of battle:
*
* 1. Bomb launch 0x1431c10: at its end the client sets the launch record's instant-win flag
* (record +0x70, written at 0x143330d / 0x143337d) from the bomb property
* isInstantWinOnExplode (+0x6a6, read at 0x13cb5a5). After it returns, a flagged record of a
* unit with an MPlayer remembers that player's team.
* 2. Projectile explosion 0xbe7c00: the host's shell update passes the launch record's flag as
* its 8th argument (movzx eax, byte [r15+0x70] at 0x2fd778, call at 0x2fd7c3); with the flag
* set it plays the nuclear explosion (flag 0x7bf4958+0x2be2, camera type 0x17 at 0xbe97e6) and
* nothing else. The remembered team becomes the winner; a bomb of the local player is also
* reported to the scripts (LMP_NUCLEAR_STRIKE_EVENT_NAME), the fact of the award
* killStreak_bomber_nuclear_success that online the battle server gives.
* 3. Domination logic cta_logic_action (0x11bc400, the mp_update_trig action registered at
* 0x11ba558): before it runs, the loser's mission variable mpTi1 / mpTi2 (tickets of team 1 / 2,
* looked up at 0x11bd1c7) is set to 0. The stock logic then ends the battle as on tickets:
* mp_game_over with the result for the local player's team (0x11bf661 / 0x11bf9c1).
*/
#include "hooks.h"
#include "hook_config.h"
#include "local_mp_internal.h"
#include "local_mp_offsets.h"
#include "MinHook.h"
#include <string.h>
typedef void (__fastcall *ShellLaunchFn)(void **launch, void *a2, uint8_t *owner, void *a4, uint64_t a5, uint64_t a6);
typedef void (__fastcall *ShellExplodeFn)(void *a1, uint64_t a2, void *a3, void *a4, void *a5, void *a6,
uint64_t a7, uint64_t instant_win, void *a9);
typedef uint64_t (__fastcall *LogicActionFn)(uint8_t *ctx, void *a2, void *a3, void *a4);
static ShellLaunchFn orig_shell_launch;
static ShellExplodeFn orig_shell_explode;
static LogicActionFn orig_cta_logic;
/* Team of the last launched instant-win bomb and whether the local player launched it, and the
* team that won with it (0 = none). */
static volatile uint8_t g_bomb_team;
static volatile uint8_t g_bomb_is_local;
static volatile uint8_t g_winner_team;
static uint8_t *g_logic_ctx;
static const uint8_t *owner_mplayer(const uint8_t *unit)
{
return unit != NULL ? *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER) : NULL;
}
static void __fastcall hook_shell_launch(void **launch, void *a2, uint8_t *owner, void *a4, uint64_t a5, uint64_t a6)
{
const uint8_t *record;
const uint8_t *mp;
uint8_t team;
orig_shell_launch(launch, a2, owner, a4, a5, a6);
if (launch == NULL || !local_mp_is_host())
return;
record = (const uint8_t *)launch[LMP_SHELL_LAUNCH_IDX_RECORD];
if (record == NULL || record[LMP_SHELL_REC_OFF_INSTANT_WIN] != 1)
return;
mp = owner_mplayer(owner);
team = mp != NULL ? mp[LMP_MP_OFF_TEAM] : 0;
if (team != 1 && team != 2)
return;
if (g_bomb_team != team)
log_line("local_mp: instant-win bomb launched by team %u", (unsigned)team);
g_bomb_team = team;
g_bomb_is_local = local_mp_is_local_mplayer(mp) ? 1 : 0;
}
static void __fastcall hook_shell_explode(void *a1, uint64_t a2, void *a3, void *a4, void *a5, void *a6,
uint64_t a7, uint64_t instant_win, void *a9)
{
orig_shell_explode(a1, a2, a3, a4, a5, a6, a7, instant_win, a9);
if ((instant_win & 1u) == 0 || !local_mp_is_host() || g_bomb_team == 0 || g_winner_team != 0)
return;
g_winner_team = g_bomb_team;
g_bomb_team = 0;
log_line("local_mp: nuclear explosion, team %u wins%s", (unsigned)g_winner_team,
g_bomb_is_local ? " (local player's bomb)" : "");
if (g_bomb_is_local)
local_mp_report_nuclear_strike();
g_bomb_is_local = 0;
}
static int *mission_int_var(uint8_t *ctx, const char *name)
{
uint8_t *vars = *(uint8_t **)(ctx + LMP_MISSION_VARS_OFF_DATA);
uint32_t count = *(uint32_t *)(ctx + LMP_MISSION_VARS_OFF_COUNT);
uint32_t i;
if (vars == NULL)
return NULL;
for (i = 0; i < count; i++) {
uint8_t *var = vars + (size_t)i * LMP_MISSION_VAR_STRIDE;
const char *var_name = *(const char **)(var + LMP_MISSION_VAR_OFF_NAME);
if (var_name != NULL && strcmp(var_name, name) == 0)
return (int *)(var + LMP_MISSION_VAR_OFF_VALUE);
}
return NULL;
}
static uint64_t __fastcall hook_cta_logic(uint8_t *ctx, void *a2, void *a3, void *a4)
{
if (ctx != g_logic_ctx) {
/* A new mission: nothing launched or won in the previous one carries over. */
g_logic_ctx = ctx;
g_bomb_team = 0;
g_bomb_is_local = 0;
g_winner_team = 0;
}
if (ctx != NULL && g_winner_team != 0 && local_mp_is_host()) {
int *loser_tickets = mission_int_var(ctx, g_winner_team == 1 ? "mpTi2" : "mpTi1");
if (loser_tickets != NULL && *loser_tickets > 0) {
log_line("local_mp: nuclear win, tickets of team %u set to 0 (were %d)",
(unsigned)(3u - g_winner_team), *loser_tickets);
*loser_tickets = 0;
}
g_winner_team = 0;
}
return orig_cta_logic(ctx, a2, a3, a4);
}
int install_local_mp_nuke_hooks(const uint8_t *text, size_t text_size)
{
uint8_t *p_launch = local_mp_scan_unique(text, text_size, "shell launch (instant-win flag)", LMP_SIG_SHELL_LAUNCH);
uint8_t *p_explode = local_mp_scan_unique(text, text_size, "shell explosion", LMP_SIG_SHELL_EXPLODE);
uint8_t *p_cta = local_mp_scan_unique(text, text_size, "cta_logic_action", LMP_SIG_CTA_LOGIC_ACTION);
if (!p_launch || !p_explode || !p_cta)
return 0;
if (MH_CreateHook(p_launch, (void *)&hook_shell_launch, (void **)&orig_shell_launch) != MH_OK ||
MH_CreateHook(p_explode, (void *)&hook_shell_explode, (void **)&orig_shell_explode) != MH_OK ||
MH_CreateHook(p_cta, (void *)&hook_cta_logic, (void **)&orig_cta_logic) != MH_OK ||
MH_EnableHook(p_launch) != MH_OK ||
MH_EnableHook(p_explode) != MH_OK ||
MH_EnableHook(p_cta) != MH_OK) {
log_line("local_mp: failed to install the nuclear instant-win hooks");
return 0;
}
return 1;
}