From 5ee8eb4a2b96b4a9916f85ddec5b43ae24ca93d1 Mon Sep 17 00:00:00 2001 From: salmonik Date: Tue, 3 Nov 2026 23:36:58 +0100 Subject: AI: treat scripted NPCs as neutral; Lamarr neutral Read the server m_NPCState and mark NPC_STATE_SCRIPT actors neutral so cutscene NPCs and scripted pets are not aimbot targets and draw in the neutral colour. Add Lamarr (Kleiner's pet headcrab) to the neutral model list as requested. Also log state changes in the NPC debug line and resolve/log m_NPCState in the server datamap summary. --- CONTEXT.md | 9 +++++++++ src/esp.cpp | 41 ++++++++++++++++++++++++++++------------- src/hl2h.h | 1 + src/server.cpp | 18 ++++++++++++++++-- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/CONTEXT.md b/CONTEXT.md index 1d365e8..741050d 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -174,6 +174,15 @@ vocabulary / decisions. - Bugbait-controlled antlions are also treated as friendly when their server `m_hFollowTarget` is set, covering antlions that spawned before the global `antlion_allied` state changed. +- Stock NPCs have no `m_Relationship` rows at all: their disposition comes from + the global `CHL2GameRules::InitDefaultRelationships()` table indexed by + `Classify()` (e.g. `CLASS_PLAYER_ALLY -> D_LI`, `CLASS_HEADCRAB -> D_HT`). + That table is not stored per entity, so `relation=-1` is normal and the + model heuristic is the fallback for those NPCs (Kleiner, Combine, ...). +- `m_NPCState` (`FIELD_INTEGER`, server datamap) is read as well: + `NPC_STATE_SCRIPT` (4) marks scripted/passive actors as neutral, so they are + not aimbot targets and draw with the neutral colour. Lamarr + (`models/Lamarr.mdl`) is additionally listed as neutral by model. ## Bones / ESP diff --git a/src/esp.cpp b/src/esp.cpp index 405dea7..50d7b5d 100644 --- a/src/esp.cpp +++ b/src/esp.cpp @@ -139,8 +139,10 @@ static bool model_is_friendly(const char *m) { // harmless wildlife that also happens to be CAI_BaseNPC static bool model_is_neutral(const char *m) { + // Lamarr is Kleiner's pet headcrab: scripted and harmless, and the user + // wants her shown neutral rather than hostile. static const char *patterns[] = {"crow", "gull", "pigeon", "seagull", - "bird", nullptr}; + "bird", "Lamarr", nullptr}; if (!m) return false; for (int i = 0; patterns[i]; i++) if (strstr(m, patterns[i])) @@ -155,42 +157,55 @@ static bool model_is_floor_turret(const char *m) { static bool npc_is_friendly(int idx, const char *model, int local_idx, bool &neutral) { int rel = server_npc_relation(idx, local_idx); + int state = server_npc_state(idx); int turret = -1; // Citizen-built and Alyx-hacked turrets never add an m_Relationship row; // their Classify() (CLASS_PLAYER_ALLY) comes from server runtime state. if (model_is_floor_turret(model)) turret = server_npc_turret_flags(idx); - // Debug-only: relation values are the authoritative answer and are the - // first thing to check when a friendly NPC still looks hostile. Logged only - // when an entity's relation changes, so this does not flood the log. + // Debug-only: relation/state changes are the authoritative signals and the + // first thing to check when a friendly NPC still looks hostile. static int rel_cache[2048]; - static bool rel_cache_init = false; - if (!rel_cache_init) { + static int state_cache[2048]; + static bool cache_init = false; + if (!cache_init) { for (int i = 0; i < 2048; i++) - rel_cache[i] = -999; - rel_cache_init = true; + rel_cache[i] = state_cache[i] = -999; + cache_init = true; } - if (idx > 0 && idx < 2048 && rel_cache[idx] != rel) { + if (idx > 0 && idx < 2048 && + (rel_cache[idx] != rel || state_cache[idx] != state)) { rel_cache[idx] = rel; - logd("npc %d (%s): relation=%d followtarget=%d turret=%d local=%d", idx, - model ? model : "?", rel, + state_cache[idx] = state; + logd("npc %d (%s): relation=%d state=%d followtarget=%d turret=%d " + "local=%d", idx, model ? model : "?", rel, state, (int)server_npc_has_follow_target(idx), turret, local_idx); } if (rel == 3) { // D_LI neutral = false; return true; } + if (turret > 0) { // citizen-built / Alyx-hacked + neutral = false; + return true; + } if (rel == 4) { // D_NU neutral = true; return false; } + // NPC_STATE_SCRIPT = 4: cutscene actors and scripted pets are passive even + // when their class relationship says hate (Lamarr, scripted Combine, ...). + if (state == 4) { + neutral = true; + return false; + } if (rel == 1 || rel == 2) { // D_HT / D_FR neutral = false; return false; } - if (turret >= 0) { + if (turret >= 0) { // ordinary floor turret neutral = false; - return turret != 0; + return false; } // Bugbait-controlled antlions follow a target even when their default // relationship was never rewritten. diff --git a/src/hl2h.h b/src/hl2h.h index 6f23221..877afac 100644 --- a/src/hl2h.h +++ b/src/hl2h.h @@ -266,6 +266,7 @@ bool server_set_velocity_z(int idx, float vz); int server_active_weapon(int idx); int server_npc_relation(int idx, int local_idx); bool server_npc_has_follow_target(int idx); +int server_npc_state(int idx); int server_npc_turret_flags(int idx); int server_model_index(void *base); void find_modules(); diff --git a/src/server.cpp b/src/server.cpp index 19be144..c2864c8 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -50,6 +50,7 @@ static int g_srv_relationship = -1; static int g_srv_followtarget = -1; static int g_srv_spawnflags = -1; static int g_srv_hacked = -1; +static int g_srv_npcstate = -1; static bool g_srv_ready = false; static bool g_srv_checked = false; @@ -139,15 +140,16 @@ bool resolve_server_netvars() { // CBaseEntity::m_spawnflags is a DEFINE_KEYFIELD at a low offset. g_srv_spawnflags = datamap_offset("m_spawnflags", FIELD_INTEGER, 0x4); g_srv_hacked = datamap_offset("m_bHackedByAlyx", FIELD_BOOLEAN); + g_srv_npcstate = datamap_offset("m_NPCState", FIELD_INTEGER); g_srv_ready = g_srv_health > 0; if (g_srv_ready) { logf("server datamap (%s): health=%d maxhealth=%d lifeste=%d " "modelindex=%d absvel=%d vel=%d activeweapon=%d relationship=%d " - "followtarget=%d spawnflags=%d hacked=%d", + "followtarget=%d spawnflags=%d hacked=%d npcstate=%d", g_server.path.c_str(), g_srv_health, g_srv_maxhealth, g_srv_lifestate, g_srv_modelindex, g_srv_absvelocity, g_srv_velocity, g_srv_activeweapon, g_srv_relationship, g_srv_followtarget, - g_srv_spawnflags, g_srv_hacked); + g_srv_spawnflags, g_srv_hacked, g_srv_npcstate); fail_logged = false; } else if (!fail_logged) { fail_logged = true; @@ -254,6 +256,18 @@ int server_npc_relation(int idx, int local_idx) { return class_relation; } +// NPC_STATE_SCRIPT = 4 (ai_npcstate.h). Scripted NPCs are passive: cutscene +// actors, Kleiner's Lamarr, scripted soldiers standing down, and so on. +int server_npc_state(int idx) { + if (!g_srv_ready || g_srv_npcstate < 0) + return -1; + void *base = nullptr; + server_entity_health(idx, nullptr, nullptr, &base); + if (!base) + return -1; + return *(int *)((uintptr_t)base + g_srv_npcstate); +} + bool server_npc_has_follow_target(int idx) { if (!g_srv_ready || g_srv_followtarget < 0) return false; -- cgit v1.2.3