- Scoreboard scores come from the local service: BattleEconomy missionScores() writes the official mission scores into the stock mission block customScoreParams, which the client copies into MissionSettings+0x6d8 without reading it; local_mp_scores.c reads them with DataBlock::getReal. The hardcoded scores and the death score go. - countActivePlayers already counts players with a living vehicle; the only LocalMP gap is its filter skipping bots (MPlayer+0x1d8 bit 7). A one-byte exe patch drops that filter bit instead of a rewrite hook. - ucavLaunch returns early on a host; an exe patch skips that return instead of clearing the global host flag around the call. - onBecameDead no longer runs with isLocalMP cleared: that only queued an EventOnPlayerDeath stock LocalMP skips on purpose, and broke the local player id remap inside the function. - The host chat object keeps its two-field layout, now documented from the host chat handler that reads it.
213 lines
8.9 KiB
C
213 lines
8.9 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);
|
|
}
|
|
|
|
void local_mp_add_mplayer_score(uint8_t *mp, float delta, uint32_t event_id)
|
|
{
|
|
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, 1);
|
|
total = *(int32_t *)(mp + LMP_MP_OFF_SCORE);
|
|
*(int16_t *)(mp + LMP_MP_OFF_FALLBACK_SCORE) = (total > 32767) ? (int16_t)32767 : (int16_t)total;
|
|
}
|
|
|
|
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 };
|
|
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)
|
|
{
|
|
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(const uint8_t *text, size_t text_size, uint8_t *p_ground_dead)
|
|
{
|
|
uint8_t *p_add = local_mp_scan_unique(text, text_size, "MPlayer::addScore", LMP_SIG_MPLAYER_ADD_SCORE);
|
|
uint8_t *p_dirty = local_mp_scan_unique(text, text_size, "net_mark_dirty", LMP_SIG_NET_MARK_DIRTY);
|
|
uint8_t *p_dead = local_mp_scan_unique(text, text_size, "onUnitDeadOnce", LMP_SIG_ON_UNIT_DEAD_ONCE);
|
|
|
|
if (!p_ground_dead || !p_add || !p_dirty || !p_dead)
|
|
return 0;
|
|
|
|
fn_mplayer_add_score = (MPlayerAddScoreFn)p_add;
|
|
fn_net_mark_dirty = (NetMarkDirtyFn)p_dirty;
|
|
|
|
if (MH_CreateHook(p_ground_dead, (void *)&hook_iground_on_became_dead, (void **)&orig_iground_on_dead) != MH_OK ||
|
|
MH_EnableHook(p_ground_dead) != MH_OK ||
|
|
MH_CreateHook(p_dead, (void *)&hook_on_unit_dead_once, (void **)&orig_on_unit_dead_once) != MH_OK ||
|
|
MH_EnableHook(p_dead) != MH_OK) {
|
|
log_line("local_mp: failed to install unit-death scoring hooks");
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|