# Base defenses

Fences and fortress walls, lights, thorns, lava moats, decoy chests and iron golems, and how bots use them to keep their base safe.

import { AccordionItem, Accordions } from '@prosefly/astro-components';

A base full of chests is worth protecting, especially with enemy factions around. Their raiders open chests and
gates like any player ([Raiding](/ai-players/guide/raiding/)). Bots defend their bases in six ways, from cheapest
to most expensive: lights, walls (a fence ring or a cobblestone fortress), thorns, a lava moat, a decoy chest and
golems. Every play style lights its grounds, and the [Turtler](/ai-players/guide/play-styles/#turtler) goes all
the way. A robbed base also fights back with [the counter-raid](/ai-players/guide/raiding/#the-counter-raid).

## Lit grounds

Every shipped play style keeps its base lit. Besides the torches inside its buildings, a bot places up to eight
torches a round on any dark walkable ground inside its wall (or around its buildings if it has no wall), so
monsters can't spawn at its front door. A spot counts as dark at block light 7 or below, whatever the time of
day. The bot never torches a building's inside, the wall's own line, or the ground straight inward of a gate.

## The wall

A bot with walls builds a fence all around its base, about a dozen blocks out from the buildings:

- **A gate in the middle of each side**, four in all. The ground on either side of a gate is filled level
  with it, then steps down to the ground beyond one block at a time (a stair of up to six each way), so a
  gate on a slope can be walked through both ways.
- **It follows the ground.** Each post sits one block above the highest ground beside it, so nothing can jump
  over it from either side. Where the ground dips, the post stands on a column of dirt filled in underneath (a
  post over water gets its fill at the surface first). A post or gate stands on a dirt path, farmland or slab
  as it would on a full block.
- **One side at a time.** The bot builds each side as it gathers the wood, out of the plank it holds most, so
  even a half-built wall starts protecting the base. A side it can't finish waits five minutes while it builds
  the next.
- **It grows with the base.** When a new building goes up near or past the wall, the bot takes down the posts
  in the way, collects them and builds a bigger ring around everything. New buildings are sited inside the
  yard while room remains.
- **Tree trunks count.** A tree growing right where the wall goes becomes part of it, and a tree beside the
  ring doesn't push the neighboring posts up.

The bot clears only plants, leaves, logs, snow, fences, gates and torches from the wall's line. A sign, a rail,
a chest or any other block of a player's build stays, and that side is left short rather than broken into. Bots
never take blocks from a wall, and they walk through its gates rather than over or under it. There are two
exceptions: an enemy raider with the `block_theft` tactic breaks a single post to get in, or tunnels under the
ring ([Raiding](/ai-players/guide/raiding/#getting-in)). A ring torn down to the last post is forgotten, though,
and the wall starts over.

### The fortress wall

A bot of the `fortress` base style (the Turtler) builds the same ring in cobblestone, whatever `walls` says:

- **Two blocks high** above the highest ground beside each cell, with one more block on top jutting one block
  outward. No spider climbs past that overhang (and a zombie doesn't get over two blocks either).
- **A tower at each corner**, two blocks above the wall, with a torch on top.
- **A wooden door in the middle of each side**, facing out under a cobblestone lintel, where a fence ring has
  its gate. The bot walks through the doors and shuts them behind it.

The wall goes up one side at a time like the fence, at about a hundred cobblestone a side around a lone house.
The bot uses what its base's chests hold before it quarries more. The fortress grows with the base the same way.
A base that had a fence ring keeps it until it outgrows it, and the next ring is a fortress. A fortress the base
outgrows is taken down block by block. The bot cares more about a fortress's last side than a fence's last
posts, so it finishes the ring before another round of mining.

A raider treats the doors as gates. It opens one it knows, breaks one door with `block_theft` (never the stone),
or tunnels underneath. When a fortress still has 48 or more blocks to go, the bot
[calls its faction for help](/ai-players/guide/factions/#calling-for-help).

### Gates get shut

Bots close the gates and doors they open, once they're through. That keeps the base closed, and it also keeps the
cows in the [animal pen](/ai-players/guide/farming/#the-animal-pen). A gate stays open only while something is
standing in the way, or while the bot is leading an animal or escorting a player through it. Bots on a
[raid](/ai-players/guide/raiding/) leave gates open too, since a raider never shuts a gate behind it (nor touches
its own faction's gates on the way through).

## The thorn ring

A bot with the **berry bush trap** plants a ring of sweet berry bushes just outside its fence, once the fence is
complete. The bushes hurt and slow down anything that walks through them, and both mobs and bots path around
them. Monsters and raiders either take the thorns or go around to a gate, where the way in is kept clear and the
gate is shut.

The bot plants from inside the wall, so it never has to walk through its own thorns. It collects the berries from
wild bushes, which grow in taiga forests, so the ring has to wait until it finds one. Once a lava moat is started
around a fortress, the band outside the wall belongs to the moat and no more bushes go there.

## The lava moat

A bot with the **lava trap** and a complete fortress wall digs a moat along the outside foot of the wall. It digs
out the block of ground beside the wall and the one under it, one side at a time, standing outside the trench so
it never steps into its own moat. The cell straight out from each door is left alone, so the way in and out stays
open. Every fourth cell of a run gets a lava source poured from a bucket, and the lava runs along the trench from
there.

The bot carries the lava from the nearest pool within its roaming radius, a bucket at a time (it crafts a bucket
when it has none), so a Turtler with no lava nearby keeps its bush ring instead. A cell is skipped where the
ground is water or lava, where the block under the trench isn't solid, or where a neighbor lies so low the lava
would run out. The moat is protected like the wall, and the bot's own paths treat it like any lava. A fence ring
never gets a moat, since the lava would set the fence on fire.

## The decoy chest

A bot with the **TNT trap** and a storehouse lays a decoy at the far end of the storehouse's aisle. It's a trapped
chest facing the door, with a few blocks of little worth inside (the smallest stack of cobblestone, dirt or the
like it holds) and a block of TNT set in the floor in front of it. The decoy isn't one of the base's chests. Its
owners never count it, store into it or open it, and they walk around the floor in front of it. A raider working
through the storehouse opens it last of the full chests, which lights the TNT under the raider's feet.

The trapped chest is crafted from a chest and a tripwire hook, and the TNT from sand and gunpowder (from the
creepers it hunts at dusk). A decoy that went off is noticed as a
[theft](/ai-players/guide/raiding/#the-counter-raid) and laid again.

## Iron golems

A bot that wants golems builds iron golems inside its walls, on the free spot nearest the inside of a gate. It
builds them the way you would: two iron blocks stacked, two more for the arms, and a pumpkin on top, carved with
shears. A carved pumpkin is placed as it is. If the carving spawns nothing, the pumpkin is broken and placed again
(once). Since a player built the golem, it's on the bot's side.

It keeps building until it has the number of golems its play style asks for (two for the Turtler). If a golem
dies or wanders more than 32 blocks from the base, the bot builds a new one. It never leads one back. Iron comes
from mining (the Turtler keeps a stock of 72 iron ingots for this, 36 a golem) and pumpkins come from wild
pumpkin patches.

:::tip
Iron golems you build yourself count as friends too. Bots never attack them, and they defend them against
monsters. A golem that's angry at a bot, though (for example because the bot hit a villager), is treated as a
threat.
:::

:::warning[Golems don't guard against raiders]
Vanilla never lets a player-built iron golem attack a player, and bots are players. Your golems fight the
monsters that spawn at night, and they make the bots beside them braver, but an enemy
[raider](/ai-players/guide/raiding/) walks right past them.
:::

## Safety in numbers

Bots are braver in company. Faction mates, allied players and player-built golems nearby make a bot more willing
to take on a strong enemy. See [Combat and safety](/ai-players/guide/combat/#strength-in-numbers).

## Who builds what

| Defense          | Neutral | Rusher | Boomer | Turtler | Flanker |
| :--------------- | :-----: | :----: | :----: | :-----: | :-----: |
| Lit grounds      | ✓       | ✓      | ✓      | ✓       | (hidden base) |
| Fence wall       |         |        |        |         |         |
| Fortress wall    |         |        |        | ✓       |         |
| Berry bush ring  |         |        |        | ✓       |         |
| Lava moat        |         |        |        | ✓       |         |
| Decoy chest      |         |        |        | ✓       |         |
| Iron golems      |         |        |        | 2       |         |

The Flanker builds none of these. Its [hidden base](/ai-players/guide/bases/#hidden-bases) has nothing standing in
the open to defend, and staying hidden is its defense. Any defense can be switched on or off for a custom play
style (see [Play style files](/ai-players/reference/play-style-files/#base)).

## Technical details

<Accordions>
  <AccordionItem title="Defense settings in the play style">
    ```json title="playstyle.json (excerpt)"
    "base": {
      "style": "fortress",
      "lighting": true,
      "traps": ["berry_bush", "lava", "tnt"],
      "golems": 2
    },
    "weights": { "fortify": 1.5 }
    ```

    - `style`: `fortress` walls the base in cobblestone whatever `walls` says; `hut` with `walls: true` builds the
      fence ring; `hidden` builds no defense at all.
    - `walls` (default `false`): the fence ring (ignored by `fortress`).
    - `lighting` (default `true`): torches inside buildings and in the yard.
    - `traps`: `berry_bush` builds the bush ring, `lava` digs the moat (fortress only), `tnt` lays the storehouse
      decoy. The raider's TNT trap is a different thing, a
      [tactic](/ai-players/reference/play-style-files/#tactics).
    - `golems` (0 to 8, default 0): how many player-built golems to keep near the base.
    - The `fortify` weight sets how much the bot cares about all of this.
  </AccordionItem>
  <AccordionItem title="Fortress geometry">
    The fortress uses the fence ring's cells and post heights. Each cell is two cobblestone above the post height
    plus one overhang block jutting outward (a corner gets two orthogonal overhangs and the diagonal). Each corner
    is a 1×1 tower two above the wall, with a torch placed once the bot holds one. Each of the four gate cells is a
    door facing out under a lintel two above the post, its jambs rising to the lintel. The overhangs are placed from
    outside through the open doorway, and the door is hung last from inside. The palette is cobblestone only (the
    door of the plank held most, oak otherwise); the style's `structures` aren't read. A wall record carries its
    kind (`fence` or `fortress`, old worlds loading as `fence`), so an old Turtler's fence ring stands until its base
    outgrows it. Cobblestone a player sets on the ring's line counts as ground under the wall.
  </AccordionItem>
  <AccordionItem title="Wall geometry">
    The ring is the base's bounds (its buildings plus 4 blocks) grown by 8 blocks on each side, so the nearest
    post is 12 blocks from the nearest building. Each post stands one block above the highest ground among its own
    cell and its neighbors that aren't ring cells, on a fill capped at 6 blocks above the lowest of those
    neighbors. A post over water stands on a fill at the surface. The gate is the middle cell of each side,
    facing out, level with its two filled approaches, which step down beyond in a stair of fills of up to six
    cells each way. A ring is capped at 64 by 64 blocks around the base's anchor. A ring is "stale" when a building
    comes within 4 blocks of it or beyond it. The bot then breaks and collects the fences and gates that aren't
    on the new ring and builds the new sides, keeping the posts that are. A side is built in one go from the
    plank the bot holds most (oak if it has none). A side that fails for anything but a shortage of materials
    cools down 6000 ticks while the next is built.
  </AccordionItem>
  <AccordionItem title="How the bot prioritizes defenses">
    For a miner (a style with a mining strategy and a gear goal of iron or above) the whole objective counts half
    until the bot holds an iron pickaxe, so the first day's light goes to the mine once the house stands.
    The fortify objective scores six needs and works on the most pressing:

    - a missing or broken wall: up to 0.6 (a fortress's last side never falls below 0.5, a fence's below 0.25);
    - dark spots in the yard: up to 0.5, at 16 or more dark spots;
    - missing golems: up to 0.6;
    - missing bushes: up to 0.3, only once the wall is complete;
    - the moat: 0.4 times the share of cells still to dig or pour, at least 0.1;
    - the decoy: 0.35 while none stands.

    A yard spot is "dark" at block light 7 or below. It places up to 8 torches per round, builds walls and moats
    one side per round, and waits while a faction mate fortifies. It only runs when the bot has a house in the
    base and is within its roaming radius of it.
  </AccordionItem>
  <AccordionItem title="Inspecting defenses">
    `/aiplayer info <bot>` shows a `fortify:` line for styles that use any defense, for example
    `fortify: wall 112/116 (building), gates 4, dark 3, golems 1/2, bushes 40/108`. Operators can trigger each part
    by hand with `/aiplayer debug <bot> task build wall`, `task light yard`, `task golem` and `task bushes`, or a
    whole round with `/aiplayer debug <bot> objective fortify`.
  </AccordionItem>
  <AccordionItem title="Known limits">
    - Where the wall crosses water, it's open below the surface. A cliff more than 6 blocks above the wall is a
      way in.
    - After the wall is moved outward, the old ring's dirt fills stay behind as small bumps. A moat is never moved
      with an outgrown ring, and the old trench stays where it was dug.
    - Buildings beyond the 64 by 64 cap stay outside the wall, and the wall only rings the base's own buildings,
      so a claimed village is never fenced. A side crossing a player's or a village's build is left short
      there, retried every five minutes.
    - A gate on a slope of one block a cell or steeper across its side is walked one way only, and a gate whose
      approaches are both too deep leads nowhere.
    - The bush ring has gaps on stone, sand and water, where berry bushes can't grow, and deep snow in a bush
      cell fails that side of the ring.
    - Golems are counted by distance, so a golem a player built near the base counts toward the bot's number.
      Player-built golems never attack players or bots (vanilla's rule), raiders included. Two faction
      mates may each build a golem for the same gap.
    - The yard's dark count skips places the bot can't walk to, so a plateau or a pit inside the yard, and the
      six-block line inward of each gate, are never lit.
    - A fortress is built only of cobblestone, and taking an outgrown one down is slow. The moat doesn't spare a
      farm plot or a pen beside the wall, and its lava may in time set fire to a wooden door beside an approach.
      A storehouse has one decoy.
    - None of this stops a raider that tunnels under the wall. Walled villages and snow or copper golems aren't
      available yet.
  </AccordionItem>
</Accordions>
