feat(ground-ai)!: disable stock tankBot globally when AI script loads

Replace per-bot mid-battle movement handback to stock AI with a clean
binary startup gate:

- Locate Unit::setBehaviorTree (ground_ai_set_beh_tree: .37 0x4e89b0,
  .58 0x4e8f60) in patches/native/local_mp_ground_ai.jsonc and hook it
  in native/wto-net/src/local_mp_ground_ai.c.
- Announce readiness from [init] in wto_ai_runtime.das via
  wtoGroundAiScriptReady once the ground AI scripts finish compiling
  cleanly with custom movement enabled. When announced, hook_set_beh_tree
  suppresses attaching "tankBot" to LocalMP bot tanks and keeps their
  MoveSystem.inactive pinned to 1 for the life of the vehicle, ending
  all tug-of-war (external_inactive_reset) with stock AI.
- If scripts fail to compile, are missing, or mode is "stock" / "fire",
  ownMovement stays 0 and stock tankBot runs untouched.
- Remove per-bot handback, timeout release, and the now-obsolete
  navigation.returnToStockOnFailure config plumbing; on genuine script
  failure a bot halts in place and logs DRIVE_FAIL instead of silently
  switching to stock. Also initialize newly spawned bot ticks to run on
  frame 0 to eliminate spawn-frame invalid_drive_cmd noise.

Verified MSVC build of wto_ws.dll (0 warnings in project C sources), root
and launcher type checks, native module resolution on both 2.61.0.37 and
2.61.0.58 (0 failures), isolated 2.61.0.58 startup confirming 12/12
LocalMP modules installed, wto_ai_runtime.das loaded in 213 ms (0 compile
errors), and "ground AI script ready (ownMovement=1; stock tankBot
disabled)" in wto_ws.log.

BREAKING CHANGE: removes navigation.returnToStockOnFailure from
tuning/ground-ai.jsonc; stock tankBot is now controlled strictly by
whether the custom ground AI script loaded with custom movement enabled.
This commit is contained in:
N0VAN0M4LY committed 2026-12-04 01:06:04 +08:00
1 parent ac35c25851
commit 85532cd930
13 files changed
+105 -124

No files matched your search

-10
View File
@@ -5,8 +5,6 @@
* port=<local server port> without a valid port the hooks still answer every host name
* locally but refuse every connection that would leave the machine
* log=0|1 optional, defaults to 1; when 0, wto_ws.log is not opened
* ground_ai_return_to_stock=0|1 optional, defaults to 1; when 0, timed-out ground AI control
* stays paused instead of returning to stock tankBot (testing)
* exe.time_date_stamp=0x... the executable the native values were resolved for
* exe.size_of_image=0x...
* native.<name>=<value> what the LocalMP modules need from the executable: RVAs of
@@ -37,7 +35,6 @@ static wchar_t g_folder[MAX_PATH];
static CRITICAL_SECTION g_log_lock;
static FILE *g_log;
static int g_log_enabled = 1;
static int g_ground_ai_return_to_stock = 1;
static NativeValue g_native[NATIVE_VALUES_MAX];
static size_t g_native_count;
static int64_t g_exe_time_date_stamp = -1;
@@ -105,8 +102,6 @@ static void read_settings(void)
read_port(line + 5);
else if (strncmp(line, "log=", 4) == 0)
g_log_enabled = atoi(line + 4) != 0;
else if (strncmp(line, "ground_ai_return_to_stock=", 26) == 0)
g_ground_ai_return_to_stock = atoi(line + 26) != 0;
else if (strncmp(line, "exe.time_date_stamp=", 20) == 0)
parse_value(line + 20, &g_exe_time_date_stamp);
else if (strncmp(line, "exe.size_of_image=", 18) == 0)
@@ -143,11 +138,6 @@ int config_native_matches_executable(void)
(int64_t)nt->OptionalHeader.SizeOfImage == g_exe_size_of_image;
}
int config_ground_ai_return_to_stock(void)
{
return g_ground_ai_return_to_stock;
}
void config_load(HMODULE self)
{
wchar_t path[MAX_PATH];
-2
View File
@@ -13,5 +13,3 @@ void describe_address(const struct sockaddr *name, int length, char *text, size_
int config_native_value(const char *name, int64_t *value);
/* Whether the settings were resolved for the executable this process runs (PE time stamp and size). */
int config_native_matches_executable(void);
/* Whether timed-out ground AI control returns to stock tankBot (wto_ws.cfg "ground_ai_return_to_stock="); 1 by default. */
int config_ground_ai_return_to_stock(void);
+57 -48
View File
@@ -1,7 +1,13 @@
/*
* Temporarily pause stock tank movement for a LocalMP bot receiving custom physics inputs.
* Only the native side changes the MoveSystem flag, so it can give control back if the script
* stops renewing it. All other mission actions still go through the original daScript binding.
* 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"
@@ -11,29 +17,30 @@
#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 void (__fastcall *SetBehTreeFn)(uint8_t *unit, const char *tree_name, uint8_t force);
typedef struct GroundAiDriveControl {
uint8_t *unit;
uint8_t *move_system;
uint32_t eid;
float remaining_seconds;
int stalled_logged;
} GroundAiDriveControl;
static MissionActionFn orig_mission_action;
static SetBehTreeFn orig_set_beh_tree;
static AsGroundModelFn as_ground_model;
/* Player IDs are one byte in the engine. */
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 = as_ground_model(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;
}
@@ -48,41 +55,31 @@ static uint8_t *get_bot_owner(uint8_t *unit, uint32_t eid)
return owner;
}
static void give_back_drive(GroundAiDriveControl *control, const char *reason)
static void clear_slot(GroundAiDriveControl *control, const char *reason)
{
uint8_t *unit = (uint8_t *)g_lmp_get_unit_by_eid(control->eid);
/* The old unit or its movement component may already have been destroyed. */
if (unit == control->unit && get_move_system(unit) == control->move_system)
control->move_system[g_lmp.ground_ai_move_inactive] = 0;
log_line("local_mp: ground AI returned drive eid=%#lx (%s)", (unsigned long)control->eid, 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 release_by_eid(uint32_t eid, const char *reason)
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)
give_back_drive(&controls[i], reason);
clear_slot(&controls[i], reason);
}
}
static void update_drive_control(uint32_t eid, uint32_t hold_ms)
static void acquire_drive_control(uint32_t eid)
{
uint8_t *unit;
uint8_t *move_system;
uint8_t *owner;
GroundAiDriveControl *control;
uint8_t *inactive;
if (hold_ms == 0) {
release_by_eid(eid, eid == 0 ? "mission reset" : "script released");
return;
}
if (eid == 0 || !local_mp_is_host())
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);
@@ -92,20 +89,15 @@ static void update_drive_control(uint32_t eid, uint32_t hold_ms)
control = &controls[owner[LMP_MP_OFF_PID]];
if (control->unit != NULL &&
(control->eid != eid || control->unit != unit || control->move_system != move_system))
give_back_drive(control, "vehicle changed");
clear_slot(control, "vehicle changed");
inactive = move_system + g_lmp.ground_ai_move_inactive;
move_system[g_lmp.ground_ai_move_inactive] = 1;
if (control->unit == NULL) {
if (*inactive)
return; /* Leave mission-disabled vehicles under their existing owner. */
control->unit = unit;
control->move_system = move_system;
control->eid = eid;
*inactive = 1;
log_line("local_mp: ground AI acquired drive eid=%#lx", (unsigned long)eid);
log_line("local_mp: ground AI owns drive eid=%#lx", (unsigned long)eid);
}
control->remaining_seconds = (float)hold_ms / 1000.0f;
control->stalled_logged = 0;
}
static int read_uint(const void *blk, const char *key, uint32_t *out)
@@ -124,20 +116,44 @@ static int read_uint(const void *blk, const char *key, uint32_t *out)
return 1;
}
static void __fastcall hook_set_beh_tree(uint8_t *unit, const char *tree_name, uint8_t force)
{
if (local_mp_is_host() && InterlockedCompareExchange(&g_script_owns_movement, 0, 0) &&
tree_name != NULL && strcmp(tree_name, "tankBot") == 0 && unit != NULL) {
uint8_t *move_system = get_move_system(unit);
if (move_system != NULL)
move_system[g_lmp.ground_ai_move_inactive] = 1;
log_line("local_mp: suppressed stock tankBot for unit=%p (custom ground AI loaded)", (void *)unit);
return;
}
orig_set_beh_tree(unit, tree_name, force);
}
static void __fastcall hook_mission_action(const char *name, const void *blk)
{
uint32_t eid, hold_ms;
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;
if (hold_ms > 0 && (eid == 0 || !local_mp_is_host()))
return;
AcquireSRWLockExclusive(&control_lock);
update_drive_control(eid, hold_ms);
if (hold_ms == 0)
clear_by_eid(eid, eid == 0 ? "mission reset" : "unit ended");
else
acquire_drive_control(eid);
ReleaseSRWLockExclusive(&control_lock);
}
@@ -145,6 +161,7 @@ void local_mp_ground_ai_update(float dt)
{
size_t i;
int hosting;
(void)dt;
if (orig_mission_action == NULL)
return;
@@ -156,26 +173,17 @@ void local_mp_ground_ai_update(float dt)
if (control->unit == NULL)
continue;
if (!hosting) {
give_back_drive(control, "session ended");
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) {
give_back_drive(control, "vehicle changed");
clear_slot(control, "vehicle changed");
continue;
}
if (dt > 0.0f)
control->remaining_seconds -= dt;
if (control->remaining_seconds <= 0.0f) {
if (config_ground_ai_return_to_stock()) {
give_back_drive(control, "script stopped renewing control");
} else if (!control->stalled_logged) {
control->stalled_logged = 1;
log_line("local_mp: ground AI control stalled eid=%#lx (auto-return disabled; stock movement kept paused)",
(unsigned long)control->eid);
}
}
/* Keep stock MoveSystem permanently paused while this bot tank is alive. */
control->move_system[g_lmp.ground_ai_move_inactive] = 1;
}
ReleaseSRWLockExclusive(&control_lock);
}
@@ -184,6 +192,7 @@ 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 },
{ "unit_set_beh_tree", g_lmp.ground_ai_set_beh_tree, (void *)&hook_set_beh_tree, (void **)&orig_set_beh_tree },
};
as_ground_model = (AsGroundModelFn)g_lmp.ground_ai_as_ground_model;
+1
View File
@@ -190,6 +190,7 @@ static const LmpValueSpec SPECS[] = {
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_set_beh_tree", ground_ai_set_beh_tree),
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),
};
+1 -1
View File
@@ -88,7 +88,7 @@ typedef struct LmpValues {
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;
uint8_t *ground_ai_mission_action, *ground_ai_as_ground_model, *ground_ai_set_beh_tree;
size_t ground_ai_move_system, ground_ai_move_inactive;
} LmpValues;