Files
SirRoboGarage/ModularBot_garage/src/module_switches.nim
T

107 lines
4.9 KiB
Nim

## Module on/off switches (job j136).
##
## The owner drives the bot from a `.env` file and wants to flip whole modules
## on and off to test combinations. Modules that ALREADY had an off switch keep
## it — no duplicate knobs are introduced here:
##
## adaptive melee radar -> TR_RADAR_FORCE_SPIN (on = force the old full spin)
## power policy -> TR_POWER_POLICY
## fire fix -> TR_FIRE_FIX
## time-indexed heat -> TR_TFIL_HEAT_TIME
## virtual-bullet overlay -> TR_VBULLET_DEBUG
## geometry overlay -> TR_GEO_DEBUG
## global debug draw -> TR_DEBUG_DRAW
## strafe heat grid -> TR_STRAFE_HEAT_GRID
## every gun -> TR_RACK_<GUN> (membership `off` = disabled)
##
## Everything NEW below uses one uniform, greppable prefix: `TR_MODULE_<NAME>`.
## Every switch defaults to ON, so an unset environment is byte-for-byte the
## shipped bot. "OFF" always means the simpler/older fallback, never a broken
## bot:
##
## TR_MODULE_VBULLETS=0 no gun predicts or spawns virtual bullets; the
## selector falls back to its floor gun.
## TR_MODULE_RAM=0 no ramming at all; the movement engine alone
## drives (the finisher/desperation/plan/opportunity
## triggers all stop firing).
## TR_MODULE_MOVE_<ENGINE>=0 that engine is no longer selectable. The
## EFFECTIVE engine falls back to the first enabled
## engine, with `tfil` (the long-shipped mover) as
## the designated fallback. There is no "no
## movement": if every engine is switched off the
## bot still runs `tfil`.
##
## Read once at module init, like every other knob, so one compiled binary can
## A/B every arm by environment alone.
import std/[os, strutils]
# ── the new switches ─────────────────────────────────────────────────────────
const
ModuleVBulletsEnvVar* = "TR_MODULE_VBULLETS"
ModuleRamEnvVar* = "TR_MODULE_RAM"
ModuleMoveTfilEnvVar* = "TR_MODULE_MOVE_TFIL"
ModuleMoveRingEnvVar* = "TR_MODULE_MOVE_TFIL_RING"
ModuleMoveStrafeEnvVar* = "TR_MODULE_MOVE_STRAFE"
ModuleMoveSurfEnvVar* = "TR_MODULE_MOVE_SURF"
ModuleMoveLearnedEnvVar* = "TR_MODULE_MOVE_LEARNED"
proc moduleFlagOn*(name: string, default = true): bool =
## Value-based, exactly like `TR_POWER_POLICY`: unset/empty -> `default`;
## explicit 0/false/no/off -> off; any other value -> on.
let v = getEnv(name, "").strip().toLowerAscii()
if v.len == 0: return default
v notin ["0", "false", "no", "off"]
let ModuleVBullets* = moduleFlagOn(ModuleVBulletsEnvVar)
## OFF: the virtual-bullet tracker stops. The selector's fitness stays empty
## and `chooseFromFit` takes its floor path (the first admitted gun), so the
## bot keeps aiming and firing — just without the fitness signal.
let ModuleRam* = moduleFlagOn(ModuleRamEnvVar)
## OFF: `triggerRam` is never true, so `isRamming` stays false and the
## movement engine drives every tick (the pre-rammer fallback).
type
MoveEngine* = object
name*: string ## the `TR_MOVEMENT` value
envVar*: string ## its `TR_MODULE_MOVE_*` switch
enabled*: bool
let MoveEngines*: array[5, MoveEngine] = [
MoveEngine(name: "tfil", envVar: ModuleMoveTfilEnvVar,
enabled: moduleFlagOn(ModuleMoveTfilEnvVar)),
MoveEngine(name: "tfil_ring", envVar: ModuleMoveRingEnvVar,
enabled: moduleFlagOn(ModuleMoveRingEnvVar)),
MoveEngine(name: "strafe", envVar: ModuleMoveStrafeEnvVar,
enabled: moduleFlagOn(ModuleMoveStrafeEnvVar)),
MoveEngine(name: "surf", envVar: ModuleMoveSurfEnvVar,
enabled: moduleFlagOn(ModuleMoveSurfEnvVar)),
MoveEngine(name: "learned", envVar: ModuleMoveLearnedEnvVar,
enabled: moduleFlagOn(ModuleMoveLearnedEnvVar)),
]
proc moveEngineEnvVar*(name: string): string =
## The switch name for `name`, or "" when it is not a known engine.
for e in MoveEngines:
if e.name == name: return e.envVar
""
proc moveEngineEnabled*(name: string): bool =
for e in MoveEngines:
if e.name == name: return e.enabled
false
proc resolveMoveEngine*(requested: string): string =
## The EFFECTIVE engine the live tick will run.
## * an unknown/empty `requested` means `tfil` (the shipped fallback);
## * the requested engine is used when its switch is on;
## * otherwise the first enabled engine in `MoveEngines` (tfil first);
## * if every engine is off, `tfil` is forced — there is no no-movement.
var req = requested.strip().toLowerAscii()
if moveEngineEnvVar(req).len == 0: req = "tfil"
if moveEngineEnabled(req): return req
for e in MoveEngines:
if e.enabled: return e.name
"tfil"