merge(ground-ai): merge feat/tank-ai phase 1 into main
This commit is contained in:
commit
0af4183cb1
58 files changed
+6900
-84
No files matched your search
@@ -23,7 +23,7 @@ New-Item -ItemType Directory -Force $obj | Out-Null
|
||||
$sources = @(
|
||||
"$src\dll_main.c", "$src\hook_config.c", "$src\redirect_rules.c", "$src\resolver_hooks.c", "$src\connection_hooks.c",
|
||||
"$src\local_mp_core.c", "$src\local_mp_values.c", "$src\local_mp_players.c", "$src\local_mp_loadout.c", "$src\local_mp_actions.c", "$src\local_mp_awards.c", "$src\local_mp_kills.c", "$src\local_mp_events.c", "$src\local_mp_scores.c", "$src\local_mp_nuke.c", "$src\local_mp_support.c",
|
||||
"$src\local_mp_damage_notifications.c", "$src\local_mp_air_events.c",
|
||||
"$src\local_mp_damage_notifications.c", "$src\local_mp_air_events.c", "$src\local_mp_ground_ai.c",
|
||||
"$mh\src\hook.c", "$mh\src\buffer.c", "$mh\src\trampoline.c", "$mh\src\hde\hde64.c"
|
||||
) | ForEach-Object { '"' + (Resolve-Path $_) + '"' }
|
||||
|
||||
|
||||
@@ -143,6 +143,7 @@ static const LocalMpModule MODULES[] = {
|
||||
{ "local_mp_support", install_local_mp_support_hooks },
|
||||
{ "local_mp_damage_notifications", install_local_mp_damage_notification_hooks },
|
||||
{ "local_mp_air_events", install_local_mp_air_event_hooks },
|
||||
{ "local_mp_ground_ai", install_local_mp_ground_ai_hooks },
|
||||
};
|
||||
|
||||
static void use_core_values(void)
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Global stock-tankBot replacement gate for LocalMP battles.
|
||||
*
|
||||
* When WTOffline's ground AI daScript compiles and loads cleanly with custom movement enabled,
|
||||
* its [init] hook announces "wtoGroundAiScriptReady". Once announced, stock "tankBot" behavior
|
||||
* trees are never attached to LocalMP bot tanks and their MoveSystem stays paused for the
|
||||
* lifetime of the vehicle (no mid-battle fallback or tug-of-war with stock AI).
|
||||
*
|
||||
* If the script fails to compile, is missing, or the player chooses "stock" mode, the ready
|
||||
* signal never arrives and stock tankBot runs untouched.
|
||||
*/
|
||||
#include "hook_config.h"
|
||||
#include "local_mp_internal.h"
|
||||
#include "local_mp_offsets.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SCRIPT_READY_ACTION "wtoGroundAiScriptReady"
|
||||
#define DRIVE_CONTROL_ACTION "wtoGroundAiDriveControl"
|
||||
|
||||
typedef void (__fastcall *MissionActionFn)(const char *name, const void *blk);
|
||||
typedef uint8_t *(__fastcall *AsGroundModelFn)(uint8_t *unit);
|
||||
typedef uint8_t (__fastcall *DriveToPosUpdateFn)(void *node, void *agent, float dt);
|
||||
|
||||
typedef struct GroundAiDriveControl {
|
||||
uint8_t *unit;
|
||||
uint8_t *move_system;
|
||||
uint32_t eid;
|
||||
} GroundAiDriveControl;
|
||||
|
||||
static MissionActionFn orig_mission_action;
|
||||
static DriveToPosUpdateFn orig_drive_to_pos_update;
|
||||
static AsGroundModelFn as_ground_model;
|
||||
|
||||
static volatile LONG g_script_owns_movement;
|
||||
static GroundAiDriveControl controls[UINT8_MAX + 1];
|
||||
static SRWLOCK control_lock = SRWLOCK_INIT;
|
||||
|
||||
static uint8_t *get_move_system(uint8_t *unit)
|
||||
{
|
||||
uint8_t *ground_model = unit ? as_ground_model(unit) : NULL;
|
||||
return ground_model ? *(uint8_t **)(ground_model + g_lmp.ground_ai_move_system) : NULL;
|
||||
}
|
||||
|
||||
static uint8_t *get_bot_owner(uint8_t *unit, uint32_t eid)
|
||||
{
|
||||
uint8_t *owner = *(uint8_t **)(unit + LMP_UNIT_OFF_MPLAYER);
|
||||
if (owner == NULL || local_mp_is_local_mplayer(owner) ||
|
||||
owner != local_mp_find_mplayer(-1, owner[LMP_MP_OFF_PID]) ||
|
||||
*(int64_t *)(owner + LMP_MP_OFF_USER_ID) >= -1 ||
|
||||
*(uint32_t *)(owner + LMP_MP_OFF_UNIT_EID) != eid)
|
||||
return NULL;
|
||||
return owner;
|
||||
}
|
||||
|
||||
static void clear_slot(GroundAiDriveControl *control, const char *reason)
|
||||
{
|
||||
log_line("local_mp: ground AI cleared drive slot eid=%#lx (%s)", (unsigned long)control->eid, reason);
|
||||
memset(control, 0, sizeof(*control));
|
||||
}
|
||||
|
||||
static void clear_by_eid(uint32_t eid, const char *reason)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(controls) / sizeof(controls[0]); i++) {
|
||||
if (controls[i].unit == NULL)
|
||||
continue;
|
||||
if (eid == 0 || controls[i].eid == eid)
|
||||
clear_slot(&controls[i], reason);
|
||||
}
|
||||
}
|
||||
|
||||
static void acquire_drive_control(uint32_t eid)
|
||||
{
|
||||
uint8_t *unit;
|
||||
uint8_t *move_system;
|
||||
uint8_t *owner;
|
||||
GroundAiDriveControl *control;
|
||||
|
||||
if (eid == 0 || !local_mp_is_host() || !InterlockedCompareExchange(&g_script_owns_movement, 0, 0))
|
||||
return;
|
||||
unit = (uint8_t *)g_lmp_get_unit_by_eid(eid);
|
||||
move_system = get_move_system(unit);
|
||||
owner = move_system ? get_bot_owner(unit, eid) : NULL;
|
||||
if (owner == NULL)
|
||||
return;
|
||||
control = &controls[owner[LMP_MP_OFF_PID]];
|
||||
if (control->unit != NULL &&
|
||||
(control->eid != eid || control->unit != unit || control->move_system != move_system))
|
||||
clear_slot(control, "vehicle changed");
|
||||
|
||||
move_system[g_lmp.ground_ai_move_inactive] = 1;
|
||||
if (control->unit == NULL) {
|
||||
control->unit = unit;
|
||||
control->move_system = move_system;
|
||||
control->eid = eid;
|
||||
log_line("local_mp: ground AI owns drive eid=%#lx", (unsigned long)eid);
|
||||
}
|
||||
}
|
||||
|
||||
static int read_uint(const void *blk, const char *key, uint32_t *out)
|
||||
{
|
||||
const char *value = g_lmp_datablock_get_str(blk, key, "");
|
||||
char *end;
|
||||
unsigned long parsed;
|
||||
|
||||
if (value == NULL || *value < '0' || *value > '9')
|
||||
return 0;
|
||||
errno = 0;
|
||||
parsed = strtoul(value, &end, 0);
|
||||
if (errno != 0 || *end != 0)
|
||||
return 0;
|
||||
*out = (uint32_t)parsed;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint8_t __fastcall hook_drive_to_pos_update(void *node, void *agent, float dt)
|
||||
{
|
||||
/* Keep the BehTree object initialized on the unit so vehicle physics steps normally,
|
||||
* but short-circuit stock driveToPos to ER_SUCCESS (1) whenever our AI script is active. */
|
||||
if (local_mp_is_host() && InterlockedCompareExchange(&g_script_owns_movement, 0, 0))
|
||||
return 1;
|
||||
return orig_drive_to_pos_update(node, agent, dt);
|
||||
}
|
||||
|
||||
static void __fastcall hook_mission_action(const char *name, const void *blk)
|
||||
{
|
||||
uint32_t eid = 0, hold_ms = 0, own = 0;
|
||||
|
||||
if (name != NULL && strcmp(name, SCRIPT_READY_ACTION) == 0) {
|
||||
if (blk != NULL && read_uint(blk, "ownMovement", &own)) {
|
||||
LONG prev = InterlockedExchange(&g_script_owns_movement, own ? 1 : 0);
|
||||
if (prev != (own ? 1 : 0)) {
|
||||
log_line("local_mp: ground AI script ready (ownMovement=%u; stock tankBot %s)",
|
||||
(unsigned)(own ? 1 : 0), own ? "disabled" : "kept");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (name == NULL || strcmp(name, DRIVE_CONTROL_ACTION) != 0) {
|
||||
orig_mission_action(name, blk);
|
||||
return;
|
||||
}
|
||||
if (blk == NULL || !read_uint(blk, "eid", &eid) || !read_uint(blk, "holdMs", &hold_ms))
|
||||
return;
|
||||
AcquireSRWLockExclusive(&control_lock);
|
||||
if (hold_ms == 0)
|
||||
clear_by_eid(eid, eid == 0 ? "mission reset" : "unit ended");
|
||||
else
|
||||
acquire_drive_control(eid);
|
||||
ReleaseSRWLockExclusive(&control_lock);
|
||||
}
|
||||
|
||||
void local_mp_ground_ai_update(float dt)
|
||||
{
|
||||
size_t i;
|
||||
int hosting;
|
||||
(void)dt;
|
||||
|
||||
if (orig_mission_action == NULL)
|
||||
return;
|
||||
hosting = local_mp_is_host();
|
||||
AcquireSRWLockExclusive(&control_lock);
|
||||
for (i = 0; i < sizeof(controls) / sizeof(controls[0]); i++) {
|
||||
GroundAiDriveControl *control = &controls[i];
|
||||
uint8_t *unit, *owner;
|
||||
if (control->unit == NULL)
|
||||
continue;
|
||||
if (!hosting) {
|
||||
clear_slot(control, "session ended");
|
||||
continue;
|
||||
}
|
||||
unit = (uint8_t *)g_lmp_get_unit_by_eid(control->eid);
|
||||
owner = unit == control->unit ? get_bot_owner(unit, control->eid) : NULL;
|
||||
if (owner == NULL || owner[LMP_MP_OFF_PID] != i || get_move_system(unit) != control->move_system) {
|
||||
clear_slot(control, "vehicle changed");
|
||||
continue;
|
||||
}
|
||||
/* Keep stock MoveSystem permanently paused while this bot tank is alive. */
|
||||
control->move_system[g_lmp.ground_ai_move_inactive] = 1;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&control_lock);
|
||||
}
|
||||
|
||||
int install_local_mp_ground_ai_hooks(void)
|
||||
{
|
||||
LocalMpHookSpec hooks[] = {
|
||||
{ "das_call_mission_action", g_lmp.ground_ai_mission_action, (void *)&hook_mission_action, (void **)&orig_mission_action },
|
||||
{ "drive_to_pos_update", g_lmp.ground_ai_drive_to_pos_update, (void *)&hook_drive_to_pos_update, (void **)&orig_drive_to_pos_update },
|
||||
};
|
||||
|
||||
as_ground_model = (AsGroundModelFn)g_lmp.ground_ai_as_ground_model;
|
||||
return local_mp_queue_hooks("local_mp_ground_ai", hooks, sizeof(hooks) / sizeof(hooks[0]));
|
||||
}
|
||||
@@ -106,3 +106,5 @@ int install_local_mp_nuke_hooks(void);
|
||||
int install_local_mp_support_hooks(void);
|
||||
int install_local_mp_damage_notification_hooks(void);
|
||||
int install_local_mp_air_event_hooks(void);
|
||||
int install_local_mp_ground_ai_hooks(void);
|
||||
void local_mp_ground_ai_update(float dt);
|
||||
@@ -13,8 +13,19 @@
|
||||
|
||||
#include "MinHook.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct ReflBulletEntry {
|
||||
const char *weapon_name;
|
||||
const char *bullet_mod;
|
||||
uint16_t count;
|
||||
uint8_t trigger_group;
|
||||
uint8_t pad[5];
|
||||
} ReflBulletEntry;
|
||||
|
||||
C_ASSERT(sizeof(ReflBulletEntry) == 24);
|
||||
|
||||
typedef void (__fastcall *SemChangeAircraftSendFn)(
|
||||
void *self, const char *unit_name, const char *weapon_name, const void *modifications_blk,
|
||||
const char *skin_name, const void *custom_preset_blk, uint8_t flags_byte,
|
||||
@@ -39,9 +50,9 @@ typedef uint64_t (__fastcall *StrHashFnv1Fn)(const char *str, size_t len);
|
||||
typedef int32_t (__fastcall *NameMapAddNameIdFn)(void *map, const char *str, size_t len, uint64_t hash);
|
||||
|
||||
static SemChangeAircraftSendFn orig_sem_send;
|
||||
static UnitChangeAircraftFn orig_unit_change_aircraft;
|
||||
static ReflCtorFn fn_refl_ctor;
|
||||
static ReflDtorFn fn_refl_dtor;
|
||||
static UnitChangeAircraftFn fn_unit_change_aircraft;
|
||||
static DataBlockSetFromFn fn_datablock_set_from;
|
||||
static DataBlockDtorFn fn_datablock_dtor;
|
||||
static BulletSetVecAssignFn fn_bullet_vec_assign;
|
||||
@@ -120,6 +131,93 @@ static void apply_mplayer_spawn_base(uint8_t *mplayer, int32_t req_base_id)
|
||||
*(float *)(mplayer + LMP_MP_OFF_SPAWN_POS_Z) = 0.0f;
|
||||
}
|
||||
|
||||
static int is_bot_unit(const uint8_t *unit)
|
||||
{
|
||||
const uint8_t *owner = *(const uint8_t *const *)(unit + LMP_UNIT_OFF_MPLAYER);
|
||||
return owner != NULL && !local_mp_is_local_mplayer(owner) &&
|
||||
*(const int64_t *)(owner + LMP_MP_OFF_USER_ID) < -1;
|
||||
}
|
||||
|
||||
static int parse_bullet_pair(const char *token, char *mod_buf, size_t mod_size, uint16_t *out_count)
|
||||
{
|
||||
const char *colon = strchr(token, ':');
|
||||
size_t mod_len;
|
||||
unsigned long parsed;
|
||||
char *end;
|
||||
|
||||
if (colon == NULL)
|
||||
return 0;
|
||||
mod_len = (size_t)(colon - token);
|
||||
if (mod_len >= mod_size)
|
||||
return 0;
|
||||
memcpy(mod_buf, token, mod_len);
|
||||
mod_buf[mod_len] = '\0';
|
||||
parsed = strtoul(colon + 1, &end, 10);
|
||||
if (end == colon + 1 || (*end != '\0' && *end != '|') || parsed == 0 || parsed > 0xfffeu)
|
||||
return 0;
|
||||
*out_count = (uint16_t)parsed;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void apply_bot_tank_ammo(const uint8_t *unit, uint8_t *refl)
|
||||
{
|
||||
const char *raw_name, *unit_name, *spec, *p1, *p2;
|
||||
char key[128], weapon[96], b0[96], b1[96];
|
||||
uint16_t c0 = 0, c1 = 0;
|
||||
size_t wlen;
|
||||
ReflBulletEntry slots[2];
|
||||
size_t count = 0;
|
||||
|
||||
if (g_local_mp_mission_settings == NULL || g_lmp_datablock_get_str == NULL || !is_bot_unit(unit))
|
||||
return;
|
||||
raw_name = *(const char *const *)(refl + LMP_REFL_OFF_UNIT_NAME);
|
||||
if (raw_name == NULL || *raw_name == '\0')
|
||||
return;
|
||||
unit_name = strncmp(raw_name, "tankModels/", 11) == 0 ? raw_name + 11 : raw_name;
|
||||
if (strlen(unit_name) + 9 >= sizeof(key))
|
||||
return;
|
||||
memcpy(key, "wtoAmmo_", 8);
|
||||
strcpy_s(key + 8, sizeof(key) - 8, unit_name);
|
||||
spec = g_lmp_datablock_get_str(g_local_mp_mission_settings + LMP_MIS_OFF_CUSTOM_BOTS, key, "");
|
||||
if (spec == NULL || *spec == '\0')
|
||||
return;
|
||||
p1 = strchr(spec, '|');
|
||||
if (p1 == NULL)
|
||||
return;
|
||||
wlen = (size_t)(p1 - spec);
|
||||
if (wlen == 0 || wlen >= sizeof(weapon))
|
||||
return;
|
||||
memcpy(weapon, spec, wlen);
|
||||
weapon[wlen] = '\0';
|
||||
if (!parse_bullet_pair(p1 + 1, b0, sizeof(b0), &c0))
|
||||
return;
|
||||
|
||||
memset(slots, 0, sizeof(slots));
|
||||
slots[0].weapon_name = weapon;
|
||||
slots[0].bullet_mod = b0;
|
||||
slots[0].count = c0;
|
||||
slots[0].trigger_group = 0xffu;
|
||||
count = 1;
|
||||
p2 = strchr(p1 + 1, '|');
|
||||
if (p2 != NULL && parse_bullet_pair(p2 + 1, b1, sizeof(b1), &c1)) {
|
||||
slots[1].weapon_name = weapon;
|
||||
slots[1].bullet_mod = b1;
|
||||
slots[1].count = c1;
|
||||
slots[1].trigger_group = 0xffu;
|
||||
count = 2;
|
||||
}
|
||||
fn_bullet_vec_assign(refl + LMP_REFL_OFF_BULLETS_VEC, &slots[0], &slots[count]);
|
||||
log_line("local_mp: bot tank ammo unit='%s' weapon='%s' b0='%s':%u b1='%s':%u",
|
||||
unit_name, weapon, b0, (unsigned)c0, count > 1 ? b1 : "", (unsigned)c1);
|
||||
}
|
||||
|
||||
static uint8_t __fastcall hook_unit_change_aircraft(void *unit, void *refl, uint8_t async)
|
||||
{
|
||||
if (local_mp_is_host() && unit != NULL && refl != NULL && async == 0)
|
||||
apply_bot_tank_ammo((const uint8_t *)unit, (uint8_t *)refl);
|
||||
return orig_unit_change_aircraft(unit, refl, async);
|
||||
}
|
||||
|
||||
static void __fastcall hook_sem_change_aircraft_send(
|
||||
void *self, const char *unit_name, const char *weapon_name, const void *modifications_blk,
|
||||
const char *skin_name, const void *custom_preset_blk, uint8_t flags_byte,
|
||||
@@ -182,7 +280,7 @@ static void __fastcall hook_sem_change_aircraft_send(
|
||||
refl[LMP_REFL_OFF_VERT_TARGETING] = vertical_targeting ? 1u : 0u;
|
||||
*(float *)(refl + LMP_REFL_OFF_BOMB_FUSE) = bomb_activation_time;
|
||||
|
||||
fn_unit_change_aircraft(hero, refl, 1);
|
||||
orig_unit_change_aircraft(hero, refl, 1);
|
||||
fn_refl_dtor(refl);
|
||||
|
||||
if (mplayer[LMP_MP_OFF_GUI_STATE] != LMP_GUI_STATE_PILOT_CONTROL &&
|
||||
@@ -198,6 +296,7 @@ int install_local_mp_loadout_hooks(void)
|
||||
{
|
||||
const LocalMpHookSpec hooks[] = {
|
||||
{ "sem_change_aircraft_send", g_lmp.sem_change_aircraft_send, (void *)&hook_sem_change_aircraft_send, (void **)&orig_sem_send },
|
||||
{ "unit_change_aircraft", g_lmp.unit_change_aircraft, (void *)&hook_unit_change_aircraft, (void **)&orig_unit_change_aircraft },
|
||||
};
|
||||
|
||||
/* The reflection is built in a fixed stack buffer; its bullet sets vector is its last member. */
|
||||
@@ -208,7 +307,6 @@ int install_local_mp_loadout_hooks(void)
|
||||
}
|
||||
fn_refl_ctor = (ReflCtorFn)g_lmp.refl_ctor;
|
||||
fn_refl_dtor = (ReflDtorFn)g_lmp.refl_dtor;
|
||||
fn_unit_change_aircraft = (UnitChangeAircraftFn)g_lmp.unit_change_aircraft;
|
||||
fn_datablock_set_from = (DataBlockSetFromFn)g_lmp.datablock_set_from;
|
||||
fn_datablock_dtor = (DataBlockDtorFn)g_lmp.datablock_dtor;
|
||||
fn_bullet_vec_assign = (BulletSetVecAssignFn)g_lmp.bullet_vec_assign;
|
||||
|
||||
@@ -166,6 +166,7 @@ static void __fastcall hook_gui_state_update(uint8_t *gui_state_obj, float dt)
|
||||
uint8_t *mplayer;
|
||||
|
||||
orig_gui_state_update(gui_state_obj, dt);
|
||||
local_mp_ground_ai_update(dt);
|
||||
|
||||
if (!local_mp_is_host()) {
|
||||
g_accept_pending = 0;
|
||||
|
||||
@@ -41,6 +41,7 @@ typedef struct LmpValueSpec {
|
||||
#define SUPPORT "local_mp_support"
|
||||
#define DAMAGE_NOTICES "local_mp_damage_notifications"
|
||||
#define AIR_EVENTS "local_mp_air_events"
|
||||
#define GROUND_AI "local_mp_ground_ai"
|
||||
|
||||
static const LmpValueSpec SPECS[] = {
|
||||
A(CORE, "do_respawn_player", do_respawn_player), O(CORE, "do_respawn_player.app_score_rules", app_score_rules),
|
||||
@@ -187,6 +188,12 @@ static const LmpValueSpec SPECS[] = {
|
||||
A(AIR_EVENTS, "air_event_orientation.call", air_event_orientation),
|
||||
A(AIR_EVENTS, "send_unit_respawn", send_unit_respawn),
|
||||
A(AIR_EVENTS, "send_unit_respawn_broadcast.mpi_sendto", mpi_sendto),
|
||||
|
||||
A(GROUND_AI, "ground_ai_mission_binding.action", ground_ai_mission_action),
|
||||
A(GROUND_AI, "ground_ai_as_ground_model", ground_ai_as_ground_model),
|
||||
A(GROUND_AI, "ground_ai_drive_to_pos_update", ground_ai_drive_to_pos_update),
|
||||
O(GROUND_AI, "ground_ai_move_properties.move_system", ground_ai_move_system),
|
||||
O(GROUND_AI, "ground_ai_move_annotation.inactive", ground_ai_move_inactive),
|
||||
};
|
||||
|
||||
static void store(const LmpValueSpec *spec, int64_t value, uint8_t *base)
|
||||
|
||||
@@ -88,6 +88,9 @@ typedef struct LmpValues {
|
||||
uint8_t *air_event_orientation, *send_unit_respawn, *mpi_sendto;
|
||||
size_t mis_mode_name, gp_ks_rules, gp_ks_rule_count, ks_rule_tag, ks_rule_air_ai_mult, ks_rule_ground_ai_mult;
|
||||
size_t ks_rule_spawn_pos, ks_rule_spawn_pos_count, ks_spawn_pos_stride, mp_ks_progress, app_terrain;
|
||||
/* local_mp_ground_ai */
|
||||
uint8_t *ground_ai_mission_action, *ground_ai_as_ground_model, *ground_ai_drive_to_pos_update;
|
||||
size_t ground_ai_move_system, ground_ai_move_inactive;
|
||||
} LmpValues;
|
||||
|
||||
extern LmpValues g_lmp;
|
||||
|
||||
Reference in new issue
Block a user