This is a King Arthur's Gold mod. Files inside this mod directory override matching files in the base game. Do not edit King Arthur's Gold/Base directly; add or change files under this mod's Base, Rules, Scripts, Sprites, or other mod folders.
Some functions are legacy and KAG/3D documentation is sparse. When in doubt, inspect working mods installed under King Arthur's Gold/Mods, especially:
Hunter4DEasy3DEasy3DExampleModEasyUI
When launching KAG for testing or gameplay debugging, start it in a visible window and leave it running. Do not use -WindowStyle Hidden or short auto-kill launches unless the user explicitly asks for a compile-only check.
Base/Entities/Characters/AIBuilder/AIBuilder.cfgwires the AI builder blob and scripts.Base/Entities/Characters/AIBuilder/AIBuilder.ashandles the player-facing "Harvest wood" button. AI builders spawn idle; this button sets"ai builder state"tofind_tree.Base/Entities/Characters/AIBuilder/AIBuilderBrain.asis the server-only behavior state machine.Base/Entities/Industry/CTFShops/AIBuilderShop/AIBuilderShop.asdeploys AI builders and sets their team to the caller's team.Scripts/CustomRenderer.asowns the current overseer tree-selection UI and selected-tree marker rendering.
The AI builder state enum is:
idlefind_treechop_treefind_logchop_logfind_woodreturn_wood
Normal flow:
- A same-team player uses the AI builder's "Harvest wood" button.
AIBuilder.assets state tofind_tree.find_treepicks a valid tree and switches tochop_tree.chop_treepaths to the hit position and uses builder hit logic until the tree is felled or gone.find_logwaits up toAIB_LOG_WAITticks for logs, then either chops logs, collects loose wood, returns wood, or finds another tree.chop_logchopslogblobs and tracks pending generated wood.find_woodpicks up loosemat_wood.return_woodgoes to the same-teamtent, withhallfallback, and drops carried/inventorymat_*resources there.
The AI does not intentionally drop resources in place. If no same-team tent or hall exists, it logs the missing-home condition when debug is enabled, switches to idle, and keeps the resources.
Full inventory only forces return_wood when the inventory contains a mat_* resource. A carried mat_* resource also forces return_wood.
Tree, log, and loose wood targets pass through accessibility checks before the AI commits to them.
Barrier rule:
AIBuilderBrain.asincludesRedBarrierCommon.as.- While
shouldBarrier(rules)is true, resources must be on the same side ofbarrier_x1/barrier_x2as the AI builder. - Resources inside the barrier strip or across the barrier are ignored.
Safety rule:
- Resources are unsafe if an enemy
knightorarcheris within 10 tiles and there is a solid-free ray from the resource to that enemy. - If terrain blocks the ray, such as a tall wall between enemy and resource, the resource remains valid.
Tree priority:
- Tree selection prefers trees close to the AI builder's team home (
tent, thenhall). - Builder distance is included as a small tie-breaker, currently
homeDistance + builderDistance * 0.20.
Overseer selection:
- Every player is currently treated as an overseer.
- Pressing
Xopens the existing blueprint menu with a custom-renderedSelect treescontrol. - The control is not implemented with KAG button helpers. It uses Inventory/EasyUI-style tick-side hover/press/release state and simple
GUI::DrawRectangle/GUI::DrawTextCenteredrendering. - In tree selection mode, click-dragging a rectangle toggles trees in that tile area.
- Selected trees store synced bool
"aibuilder selected tree"and a local tag"aibuilder selected tree". - Selected trees get a marker from
RenderSelectedTreeMarkers. - If at least one tree is selected, AI builders only consider selected trees. If no trees are selected, normal tree selection is used.
Confirm trees selectionexits selection mode. Right-click/cancel also exits.
The builder uses a hybrid of base KAG pathing and direct key movement:
- Long or obstructed movement uses
CBrain.SetPathTo,SetSuggestedKeys, and path states. - Close visible movement can switch to direct key presses through
AIB_PathTo. AIB_DetectObstructionswatches low movement and alternates between repathing and direct movement if stuck.AIB_ScaleObstaclespresses up on ladders, in water, on walls, or when blocked horizontally.
Enemy knights interrupt work:
- If an enemy
knightis within 10 tiles and has a solid-free ray to the builder, the AI clears its target/path and runs away for that tick. - Terrain between the knight and builder suppresses this fear response.
AIBuilderBrain.as has:
const bool AIB_DEBUG = false;
const u32 AIB_DEBUG_SNAPSHOT_RATE = 150;Set AIB_DEBUG to true only while testing. It uses print(), so leave it false for deployed builds.
When enabled, search KAG logs for [AIBuilder]. Messages include:
- state transitions and reasons
- target netid and target name
- pending wood
- wood/resource possession
- direct movement mode
- periodic brain/path snapshots
- ignored resources due to barrier or unsafe enemy zone
- knight flee events
- missing team home during resource delivery
Useful signs:
state find_log -> return_wood: logs are gone and harvested wood exists.state return_wood -> find_tree reason=wood delivered: resource delivery completed.brain=stuckor highobstruction: navigation is likely the issue.outside current barrier zone: resource was across or inside the red barrier while active.unsafe enemy zone: enemy knight/archer had direct access to the resource.fleeing enemy knight: harvesting was interrupted by nearby reachable knight threat.no team home found; cannot drop resources: no same-team tent or hall was found.
[AIBEVT] event logging is controlled at runtime by:
rules.get_bool("aib event log enabled")Scripts/AIBTestEventLog.as enables it for the AIBTest gamemode and resets aib event log seq. Normal deployed gameplay should leave it disabled unless explicitly debugging. Event records are compact, machine-readable lines for test actions, player-equivalent commands, UI selection rectangles, AI state/target changes, resource rejection, no-home handling, and cleanup.
Use Tools/run_aib_tests.ps1 for automated coverage. The current suite has 13 scenarios and should end with:
AIB tests passed: 13 passed, 0 failed
The live headless KAG server currently stops advancing around game tick 51, so long real tree-chopping scenarios are not reliable as automated tests. The suite covers tree selection, resource generation as simulated pipeline events, and real return/drop-at-tent behavior; keep full visual tree chopping as a manual check.
Zombies_Reborn was used as a reference for how KAG mods build practical AI.
Main pattern:
- Zombies use
generic_brain, but most ground movement is direct key pressing rather than engine pathfinding. - Brains decide intent: target, destination, aim, and pressed keys.
- Movement physics are separated into movement scripts with per-creature constants.
- Attack effects are separated into blob scripts.
- Expensive decisions are throttled with randomized brain delays, and old keys are reused between decision ticks.
- Obstruction recovery is explicit: jump, climb, attack obstructions, or reset target/destination.
Contrast with base KAG:
- Base KAG character bots lean on
BrainCommon.as,CBrain.SetPathTo,SetSuggestedKeys, and class-specific strategy states. - Base KAG is better for player-like precision. Zombies_Reborn's direct movement is better for hordes and simple destructive enemies.
Practical guidance for future AI work in this mod:
- Use a shared rules-level target cache only when many entities need the same target set.
- Keep AI intent in the brain, but keep physics/attack side effects outside the brain when behavior grows.
- Use direct movement for close, visible, simple goals; use
SetPathTo/SetSuggestedKeyswhen navigation precision matters. - Always include stuck recovery and debug logs that can be disabled with one constant.
- Avoid KAG UI button helpers for custom overlay controls in this mod; prefer Inventory/EasyUI-style manual input state and simple GUI rendering.