Files
WT-Offline/native/wto-net/src/local_mp_kills.c
T
N0VAN0M4LYandduduyagame 6d0abf9caf fix(battle): award Air Arcade mini-base score
Use the native health update identified in PR #10 to report enemy mini-base damage and destruction. Preserve fractional personal score across hits and use the selected mode to gate both the HUD reward and settlement.

Verified server and launcher types, MSVC compilation, Quirrel bytecode, and patch resolution on 2.61.0.37, 2.61.0.58, and 2.61.0.60. In-game acceptance is pending.

Co-authored-by: duduyagame <[email protected]>
2026-12-04 06:13:21 +08:00

217 lines
8.8 KiB
C

/*
* LocalMP authoritative unit-death and kill-attribution hooks across all vehicle branches.
*
* Hooks BOTH `IGroundModel::onBecameDead` (tanks, IFVs, SPAA, boats, ships, infantry)
* and `onUnitDeadOnce` (aircraft & helicopters). All 31 bound MPlayer bots receive 1:1
* PvP kill/death counters and battle score alongside the local player; the scores come from
* the mission settings (local_mp_scores.c). Kills made by the local player are also reported
* as facts through local_mp_report_award_event().
*/
#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 *MPlayerAddScoreFn)(uint8_t *mp, float delta, uint32_t event_id, uint8_t add_team);
typedef void (__fastcall *NetMarkDirtyFn)(void *dirty_field, void *owner_obj, void *payload_ptr);
typedef void (__fastcall *ChooseOffenderVfn)(uint8_t *victim, uint8_t *out_offender, uint8_t clear_flag);
typedef void (__fastcall *IGroundOnDeadFn)(uint8_t *victim, int32_t p2);
typedef void (__fastcall *OnUnitDeadOnceFn)(uint8_t *victim);
static MPlayerAddScoreFn fn_mplayer_add_score;
static NetMarkDirtyFn fn_net_mark_dirty;
static IGroundOnDeadFn orig_iground_on_dead;
static OnUnitDeadOnceFn orig_on_unit_dead_once;
void local_mp_mark_mplayer_dirty(uint8_t *mp)
{
if (mp != NULL && fn_net_mark_dirty != NULL)
fn_net_mark_dirty(mp + LMP_MP_OFF_STAT_DIRTY, mp, mp + LMP_MP_OFF_STAT_PAYLOAD);
}
void local_mp_inc_mplayer_stat(uint8_t *mp, size_t off)
{
int16_t v = *(int16_t *)(mp + off);
if (v < 32767)
*(int16_t *)(mp + off) = (int16_t)(v + 1);
}
static void dec_mplayer_stat_pos(uint8_t *mp, size_t off)
{
int16_t v = *(int16_t *)(mp + off);
if (v > 0)
*(int16_t *)(mp + off) = (int16_t)(v - 1);
}
static void add_mplayer_score(uint8_t *mp, float delta, uint32_t event_id, uint8_t add_team)
{
uint8_t *app = g_local_mp_app ? *g_local_mp_app : NULL;
int32_t total;
if (mp == NULL || delta <= 0.0f || event_id >= LMP_EXP_EVENT_COUNT || !fn_mplayer_add_score)
return;
if (app == NULL || *(void **)(app + LMP_APP_OFF_SCORE_RULES) == NULL)
return;
fn_mplayer_add_score(mp, delta, event_id, add_team);
total = *(int32_t *)(mp + LMP_MP_OFF_SCORE);
*(int16_t *)(mp + LMP_MP_OFF_FALLBACK_SCORE) = (total > 32767) ? (int16_t)32767 : (int16_t)total;
}
void local_mp_add_mplayer_score(uint8_t *mp, float delta, uint32_t event_id)
{
add_mplayer_score(mp, delta, event_id, 1);
}
void local_mp_add_personal_score(uint8_t *mp, float delta, uint32_t event_id)
{
add_mplayer_score(mp, delta, event_id, 0);
}
static int is_same_severe_offender(const uint8_t *victim, const uint8_t *killer_mp)
{
if (victim == NULL || killer_mp == NULL || *(const int16_t *)(victim + LMP_UNIT_OFF_SEVERE_IDX) == -1)
return 0;
return local_mp_find_mplayer(*(const int64_t *)(victim + LMP_UNIT_OFF_SEVERE_UID),
victim[LMP_UNIT_OFF_SEVERE_PID]) == killer_mp;
}
static void record_enemy_kill(uint8_t *killer_mp, uint8_t *victim, int is_pvp)
{
uint16_t utype = *(uint16_t *)(victim + LMP_UNIT_OFF_TYPE);
int is_air = (utype == 0u || utype == 0xdu);
int is_ship = (utype == 5u);
int is_human = (utype == 0xeu);
int own_sev = is_pvp && (is_air || is_ship) && is_same_severe_offender(victim, killer_mp);
uint32_t ev = is_air ? LMP_EXP_EVENT_KILL : (is_human ? LMP_EXP_EVENT_KILL_HUMAN : LMP_EXP_EVENT_KILL_GROUND);
/* Finishing a unit the killer had already severely damaged adds what the kill is worth over
* the severe damage score the killer got then. */
float pts = local_mp_event_score("kill", victim, is_pvp) -
(own_sev ? local_mp_event_score("severeDamage", victim, is_pvp) : 0.0f);
if (is_pvp) {
local_mp_inc_mplayer_stat(killer_mp, is_air ? LMP_MP_OFF_KILLS : (is_ship ? LMP_MP_OFF_NAVAL_KILLS :
(is_human ? LMP_MP_OFF_HUMAN_KILLS : LMP_MP_OFF_GROUND_KILLS)));
if (own_sev)
dec_mplayer_stat_pos(killer_mp, is_air ? LMP_MP_OFF_AIR_SEVERE_DMG : LMP_MP_OFF_SHIP_SEVERE_DMG);
} else {
local_mp_inc_mplayer_stat(killer_mp, is_air ? LMP_MP_OFF_AI_KILLS :
(is_ship ? LMP_MP_OFF_AI_NAVAL_KILLS : LMP_MP_OFF_AI_GROUND_KILLS));
}
local_mp_add_mplayer_score(killer_mp, pts, ev);
local_mp_mark_mplayer_dirty(killer_mp);
if (local_mp_is_local_mplayer(killer_mp)) {
LocalMpAwardEvent award = { ev, victim, is_pvp, 0, own_sev ? "from_severe_damage" : NULL, 0 };
if (!is_ship && !is_human)
local_mp_credit_air_event_kill(killer_mp, is_air);
local_mp_report_award_event(&award);
}
}
static void record_team_kill(uint8_t *killer_mp, uint8_t *victim, int is_pvp)
{
uint16_t utype = *(uint16_t *)(victim + LMP_UNIT_OFF_TYPE);
int is_air = (utype == 0u || utype == 0xdu);
local_mp_inc_mplayer_stat(killer_mp, LMP_MP_OFF_TEAM_KILLS);
local_mp_mark_mplayer_dirty(killer_mp);
if (local_mp_is_local_mplayer(killer_mp)) {
LocalMpAwardEvent award = {
is_air ? LMP_EXP_EVENT_KILL : (utype == 0xeu ? LMP_EXP_EVENT_KILL_HUMAN : LMP_EXP_EVENT_KILL_GROUND),
victim, is_pvp, 1, NULL, 0
};
local_mp_report_award_event(&award);
}
}
static void process_unit_death(uint8_t *victim, uint8_t *killer_mp, int32_t killer_army, int allow_kill_credit)
{
uint8_t *victim_mp = *(uint8_t **)(victim + LMP_UNIT_OFF_MPLAYER);
uint8_t victim_army = victim[LMP_UNIT_OFF_ARMY];
if (victim_mp != NULL) {
local_mp_inc_mplayer_stat(victim_mp, LMP_MP_OFF_DEATHS);
local_mp_mark_mplayer_dirty(victim_mp);
}
if (!allow_kill_credit || killer_mp == NULL || killer_mp == victim_mp)
return;
if (killer_army <= 0)
killer_army = (int32_t)killer_mp[LMP_MP_OFF_TEAM];
if (victim_army != 0 && killer_army == (int32_t)victim_army) {
record_team_kill(killer_mp, victim, victim_mp != NULL);
} else {
record_enemy_kill(killer_mp, victim, victim_mp != NULL);
}
}
static uint8_t *resolve_offender_killer(uint8_t *victim, int32_t *out_army)
{
__declspec(align(16)) uint8_t offender[0x180];
uintptr_t vtbl = victim ? *(uintptr_t *)victim : 0;
ChooseOffenderVfn choose_fn;
if (vtbl == 0)
return NULL;
memset(offender, 0, sizeof(offender));
*(int16_t *)offender = -1;
offender[LMP_OFFENDER_OFF_PID] = 0xffu;
*(int64_t *)(offender + LMP_OFFENDER_OFF_UID) = -1;
choose_fn = *(ChooseOffenderVfn *)(vtbl + LMP_UNIT_VTF_CHOOSE_OFFENDER);
if (choose_fn != NULL)
choose_fn(victim, offender, 0);
if (*(int16_t *)offender == -1)
return NULL;
*out_army = *(int32_t *)(offender + LMP_OFFENDER_OFF_ARMY);
return local_mp_find_mplayer(*(int64_t *)(offender + LMP_OFFENDER_OFF_UID), offender[LMP_OFFENDER_OFF_PID]);
}
static void __fastcall hook_iground_on_became_dead(uint8_t *victim, int32_t p2)
{
int is_host = local_mp_is_host();
if (is_host && victim != NULL &&
victim[LMP_IGM_OFF_ALREADY_DEAD] == 0 &&
victim[LMP_IGM_OFF_DEAD_SILENT] == 0) {
int allow_kill = (victim[LMP_IGM_OFF_BAILOUT_DEAD] == 0);
int32_t killer_army = *(int32_t *)(victim + LMP_IGM_OFF_KILLER_ARMY);
uint8_t *killer_mp = allow_kill ? local_mp_find_mplayer(
*(int64_t *)(victim + LMP_IGM_OFF_KILLER_UID), victim[LMP_IGM_OFF_KILLER_PID]) : NULL;
local_mp_events_begin_death(victim);
if (allow_kill && killer_mp == NULL)
killer_mp = resolve_offender_killer(victim, &killer_army);
if (allow_kill && killer_mp != NULL && victim[LMP_IGM_OFF_KILLER_PID] == 0xffu) {
victim[LMP_IGM_OFF_KILLER_PID] = killer_mp[LMP_MP_OFF_PID];
*(int64_t *)(victim + LMP_IGM_OFF_KILLER_UID) = *(int64_t *)(killer_mp + LMP_MP_OFF_USER_ID);
}
process_unit_death(victim, killer_mp, killer_army, allow_kill);
}
orig_iground_on_dead(victim, p2);
}
static void __fastcall hook_on_unit_dead_once(uint8_t *victim)
{
local_mp_support_unit_dead(victim);
if (local_mp_is_host() && victim != NULL && victim[LMP_UNIT_OFF_SKIP_KILL] == 0) {
int32_t killer_army = 0;
uint8_t *killer_mp = resolve_offender_killer(victim, &killer_army);
local_mp_events_begin_death(victim);
process_unit_death(victim, killer_mp, killer_army, 1);
}
orig_on_unit_dead_once(victim);
}
int install_local_mp_kill_hooks(void)
{
const LocalMpHookSpec hooks[] = {
{ "iground_on_dead", g_lmp.iground_on_dead, (void *)&hook_iground_on_became_dead, (void **)&orig_iground_on_dead },
{ "on_unit_dead_once", g_lmp.on_unit_dead_once, (void *)&hook_on_unit_dead_once, (void **)&orig_on_unit_dead_once },
};
fn_mplayer_add_score = (MPlayerAddScoreFn)g_lmp.mplayer_add_score;
fn_net_mark_dirty = (NetMarkDirtyFn)g_lmp.net_mark_dirty;
return local_mp_queue_hooks("local_mp_kills", hooks, sizeof(hooks) / sizeof(hooks[0]));
}