# Combat and safety

When bots fight, how they fight, and how to stay on their good side.

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

Bots fight with the same weapons and rules you do: swords, axes, bows and shields, real attack cooldowns and real
reach. Whether they fight *you* depends on your [factions](/ai-players/guide/factions/), on what you've done to
them and on how well you're equipped.

## When does a bot fight?

A bot always:

- defends itself against anyone who hurt it in the last few seconds, whatever their faction;
- helps its allies when it sees something hurting them (and helps villagers against monsters);
- fights monsters that come close or come for it.

A bot sometimes:

- **attacks enemies on sight.** Members of an enemy faction, and anyone it holds a grudge against, are fair game.
  How eagerly depends on its play style. A Rusher spots you from far away and charges, while a Neutral bot never
  does.
- **fights enemies inside its base**, whatever its play style.
- **hunts enemies down.** Bots remember where they last saw their enemies, share it with their faction, and go
  looking for them when they feel ready.
- **raids their bases.** Styles built for raiding scout an enemy faction's base, sneak in past the wall, empty a
  chest and sprint home as soon as they're spotted. They don't pick fights on the way. See
  [Raiding enemy bases](/ai-players/guide/raiding/).
- **hunts whoever robbed its base.** If its base noticed a thief, every bot of that base fights the thief on sight,
  whatever their style, and their faction raids the thief's faction back
  ([the counter-raid](/ai-players/guide/raiding/#the-counter-raid)).

A bot never attacks a member of its own faction or an ally unprovoked. It also leaves alone any neutral player who
hasn't hurt it or robbed it in front of it.

:::danger[Enemy bots fight to kill]
A bot in a faction at war with yours, or one that holds a grudge against you, may attack you on sight and fights
until one of you dies. Before walking up to one, make your factions neutral or allied. Don't count on outrunning
it either, since a bot chases a target that turns and runs.
:::

## Grudges

Hitting a bot that isn't your enemy earns you a **grudge**. For a while (10 to 30 minutes for the shipped
styles), that bot and its faction treat you as an enemy. Joining an allied faction clears the grudge. A sword's
sweep that catches a bot standing beside the mob you swung at is an accident, and earns none. A kill settles the
score: a bot that killed you forgets its grudge, and a bot you killed forgets its own against you when it respawns
(its faction keeps theirs), so a fight between neighbors ends with the first death.

## Offenses in the base

A bot that sees you rob its base holds the same grudge as if you had hit it. A neutral player offends when, in
front of a base bot, they:

- open one of the base's chests (**theft**);
- break a block of one of its houses, its storehouse, its enchanting corner or its wall (**vandalism**);
- trample the farmland of one of its plots (**trampling**).

The bot and its faction then hold a grudge against that player for the style's `grudge_minutes`, and each new
offense refreshes it. The first bot to hold it says so in chat, once:

```txt title="Chat"
<Ann> Alice, I saw you open our chest. You are our enemy now.
```

From then on you're its enemy. Near its own house it fights you whatever its aggression. Elsewhere it engages you
on sight if the gear comparison allows it, and keeps away when you outmatch it.

Only what a bot actually perceives counts. The offense has to happen within its view and in its line of sight, and
it registers after the bot's reaction delay. A bot that didn't see you (at night behind a wall, or looking away)
knows nothing. Allies, faction mates, other bots and players a bot can't hurt (creative, spectator, PvP off) never
offend. A chest the base doesn't record, like a mine chest out in the wild, doesn't belong to the base.

The fight is the bot's only revenge, and it doesn't block your access. What you're allowed to break is up to
vanilla (or a protection mod).

:::warning[Bots without a faction are neutral to you too]
A bot that's in no faction treats *everyone*, you included, as a neutral stranger, so taking from its chest in front
of it counts as theft. To hand things to such a bot through its chest, or to take from it, put yourself and the bot
in one [faction](/ai-players/guide/factions/) first, or give its play style `"offenses": []`.
:::

## Bots size you up

Before attacking an enemy on sight, a bot compares its gear with yours: armor, sword or axe, enchantments, whether
it has a bow with arrows, and whether either of you has a Strength potion active. If you're clearly better
equipped, it keeps its distance (about 12 blocks) instead of charging, and comes back once its own gear catches
up. Bolder play styles take bigger risks.

Bots use the same comparison before picking a fight with dangerous [Nether mobs](/ai-players/guide/nether/) (blazes,
hoglins, wither skeletons and piglin brutes). The comparison only decides whether a bot *starts* a fight. If you
hit it, it fights back whatever your gear. A hurt bot also won't run from an attacker it outweighs twice over, so
a bot in full iron with an iron sword keeps fighting a player with a wooden sword to the end, however low its
health.

### Strength in numbers

Bots also count their friends. Faction mates, allied players and iron golems built by players nearby add to a
bot's courage, so a group of bots will take on an enemy none of them would face alone. If its friends walk away
mid-fight, a bot may back off. A bot in a faction that meets an enemy it can't take alone
[calls its faction for help in team chat](/ai-players/guide/factions/#calling-for-help) and attacks once enough
have come.

## How bots fight

- **Melee:** they walk up, keep a short distance, strafe, time their swings to the attack cooldown and jump for
  critical hits.
- **Shields:** every bot that fights with a sword or axe crafts a shield along with its iron tools (right after its
  iron pickaxe, from the very next ingot, where it stands) and wears it in its off-hand. In a fight it raises the
  shield between swings against close melee attackers. It also raises it against archers within 16 blocks while
  holding its ground, and against a swelling creeper within 4 blocks when it isn't stepping back. Out of a fight, a
  bot with a shield doesn't turn its back on a creeper swelling within five blocks: it faces it and takes the blast
  on the shield. Against *your* raised shield, it swings an axe to disable it.
- **Bows:** every style with `bow` in its weapons makes a bow and keeps 16 arrows on hand, shooting from 8 to 16
  blocks away and leading moving targets.
- **Rushing archers and taking cover:** against a skeleton or bow/crossbow user within 16 blocks, a melee bot
  sprints in while side-stepping left and right to throw off the shooter's aim. If an archer shoots it and the bot
  won't fight it (because it's hurt or outmatched), it ducks behind the nearest block within 8 blocks that breaks
  line of sight, leaving a spot where the arrows still land for another, or walls the archer off with the blocks it
  holds when no cover is in reach. A bot with no weapon at all never walks up to an archer, since fists against a
  bow lose. It takes cover instead, and fights any monster only once it's within two blocks.
- **Creepers:** on Normal, Hard and Expert difficulties, a healthy bot holding a sword or axe fights creepers the
  way an experienced player does. It sprints in, lands one hit and steps back until the hiss dies down, then
  repeats until the creeper is dead without having exploded. It can also shoot it from range with a bow. An
  unarmed or hurt bot, a bot on Easy difficulty, or a bot that meets a creeper already about to blow runs away
  instead. A creeper it has run from isn't fought again for ten seconds, so the fight and the run never take turns
  until the blast. A bot hears a creeper too: one hissing within seven blocks behind it, or walking up behind it
  within six, is run from (or braced for with a shield) as if seen.
- **Potions:** bots whose play style uses `potions` drink a Strength potion at the start of a tough fight against
  an evenly matched or stronger player, bot or Nether mob.
- **Hit and run:** some styles (like the Rusher and Flanker) back off after every hit until their next swing is
  ready.
- **Chasing runners:** if you turn and run while a bot can see you, it stops keeping its distance and runs you
  down. It sprints straight after you, jumps blocks in the way and swings at your back each time its cooldown is
  ready. It lets you go when you gain 24 blocks on it, when it hasn't seen you for three seconds, when its own
  health gets too low, or when the chase has taken it farther than its roaming radius from where it began. After
  that it leaves you alone for 30 seconds. Someone standing still behind a fence or on a ledge isn't chased.
- **Healing mid-fight:** a bot at its retreat line holding a golden apple (any style) or a healing potion (styles
  with `potions`) doesn't flee a fight it isn't outmatched in. It sprints a step back, eats or drinks, and comes
  right back with its weapon out. Heals are saved for fights, so a hungry bot never eats its golden apples.
  `gear_up` crafts one when gold is plentiful.
- **Packs: the tower and the doorway.** Sometimes a fit bot gets caught in the open by three or more melee monsters
  (zombies and their kind; spiders climb, so they don't count) with no archer in sight. If it has two blocks to
  spare, it pillars two blocks up where it stands and fights from the top, out of a zombie's reach, shooting the
  rest with a bow if it has one. It digs the pillar back down once the pack is gone or an archer shows up. Within a
  dozen blocks of its own house, it backs into the doorway instead and fights from the cell just inside the door,
  where only one monster at a time can reach it. While it holds a tower or a doorway it doesn't flee for its
  health, though it still runs from a creeper.
- **In its own tunnel it stands its ground.** A bot holding a stone sword or better in a one-wide tunnel it dug
  itself (its strip, its stair, its burrow's shaft) fights whatever comes down the tunnel at it. If an archer
  shoots it down the tunnel, it closes in rather than running or taking cover. Only one monster can reach it at a
  time there, and running down a dead end with a monster on its heels is worse than the fight. Its health still
  wins out. Below its retreat line, or when shot a second time from beyond its sword's reach while still hurt, it
  backs down its own tunnel away from the foe and fights again once healed. It still runs from a creeper, back
  along the tunnel.
- **A hurt bot fights a lone monster down.** A skeleton's arrows outrun a sprint and a zombie at its heels hits a
  turned back, so a bot with a sword or axe doesn't run from a lone archer within 16 blocks or a lone melee monster
  within 4 when nothing else is near. It fights on whatever its health. Under half its health it won't rush an
  archer it can't shoot back at, and takes cover instead. Below three health (a heart and a half) a bot fights
  nothing at all: it walls itself in or runs.
- **A bot doesn't fight a pack.** Three monsters within ten blocks, two of them with bows, are left whatever its
  health. It fights only one that comes within reach and otherwise runs, or walls itself in underground. Its own
  one-wide tunnel is the exception, where monsters come one at a time.
- **Walling in:** underground, a bot that would run closes every open cell around it with blocks instead and
  waits inside until nothing has been near for five seconds and it has healed, eating if it holds food and striking
  whatever pokes in. A fight that turns badly (two monsters or more under 40% health, or one with no weapon in
  hand) walls the bot in wherever it stands, even in the open, unless a creeper is near or a monster is already
  within two blocks ([survival](/ai-players/guide/how-bots-live/#getting-out-of-trouble)).
- **Retreating:** when its health drops too low (and it has no heal, or is outmatched), a bot sprints away along a
  path that avoids every nearby threat, eats cooked food to heal, and comes back to finish the job.

### Looting defeated players

If a bot deals the killing blow to you (or to another bot it was fighting), it loots what you dropped. It walks
over the death spot and picks up your items within 8 blocks. Any armor piece better than its own goes on right
away, and the rest it keeps or stores in its base's chests. It never touches the drops of a player it didn't kill
itself (after a fall or a mob kill, say), or of someone killed inside another faction's protected base.

## Hunting monsters

Bots hunt zombies, skeletons, spiders and creepers for what they drop (string for a bow, bones, gunpowder), once
they have at least a stone sword or axe and are in good health:

- **Dusk hunts at the doorstep:** a bot that wants a bow or arrows hunts at dusk instead of waiting to stumble
  across a spider by day. Between sunset and midnight, once its house stands, it waits outside its front door and
  walks out up to 16 blocks to kill the spiders and skeletons that spawn nearby, picking up their string, bones and
  arrows. While its farm is short of crops it also stays out for the skeletons' bones, for
  [bone meal](/ai-players/guide/farming/#bone-meal).
- **Endermen:** styles with the `ender_pearl` tactic hunt endermen for the two pearls their
  [raids](/ai-players/guide/raiding/) carry. Styles that go to [the End](/ai-players/guide/the-end/#eyes-of-ender)
  hunt them for the pearls their eyes of ender need, whatever their tactics. They go after any enderman in sight at
  any hour, and once their End gear is done they also take night walks round the base or a trip to a warped forest
  in the Nether. That hunt requires an iron or better sword and more than three quarters health. The bot also
  stays after an enderman that teleports away much longer than after any other monster.
- **Silverfish:** fought like any other monster (handy in a stronghold).
- **Nether monsters:** in [the Nether](/ai-players/guide/nether/), bots hunt blazes, magma cubes and wither
  skeletons for blaze rods, magma cream and coal once their gear is strong enough.

## The dragon

The End's dragon gets its own fight. A party takes it on as part of its End trip
([the End](/ai-players/guide/the-end/#the-fight)). It uses the bow while the dragon flies and the sword when it
perches, stays out of its breath and off the line of its dives, and retreats behind a pillar to heal, never off the
island. Any other bot weighs the dragon at the difficulty's `ender_dragon` (6 by default) in the gear comparison.
As with a blaze or a brute, it keeps away when its side is lighter, and it never chases the dragon.

## Shulkers

Shulkers in an end city are left to the [city raid](/ai-players/guide/the-end/#the-second-trip-the-outer-islands),
and a bot raiding a city never takes one as an ordinary target. It closes in with its shield raised, strikes with
its sword while the shell is open and holds the shield up while it's closed. It only shoots a shulker that no
standing spot within reach can see (one on a wall over the void, for example), and only while the shell is open,
since a closed shell turns arrows. A bullet within 4 blocks is met with the shield, or struck down from within
reach if the bot has no shield. While a shulker in sight is alive, the bot never bridges, pillars or stands at the
void's edge to reach it. In the gear comparison a shulker weighs the difficulty's `shulker` (2 by default). A bot
fighting on a block at the void's edge crouches like a player would, so it can't step off mid-fight.

## Staying safe

- **Turn off PvP:** with `/gamerule pvp false`, no bot fights a player or another bot.
- **Creative and spectator** players are never attacked, and neither are players protected by their team's
  friendly-fire setting.
- **Make peace:** set your faction's relation with theirs to `neutral` or `ally`.
- **Stop a bot completely:** an operator can switch off a bot's brain with `/aiplayer debug <bot> auto off` (and
  back on with `auto on`).

## Technical details

<Accordions>
  <AccordionItem title="Engagement rules in detail">
    - **On sight:** within `aggression × perception_radius` blocks (the play style's aggression, the difficulty's
      radius), gated by the gear comparison below. Aggression 0 never engages on sight.
    - **In the base:** an enemy inside the base's bounds plus 8 blocks is fought whatever the aggression.
    - **Self-defense:** whoever hurt the bot in the last 10 seconds. Hits from allies are ignored.
    - **Assist:** whatever hurts an ally in sight (a villager only against mobs).
    - **Monsters:** within 3 × `engage_range` (9 blocks by default) or when they target the bot. Blazes, wither
      skeletons, hoglins and piglin brutes are also gated by the gear comparison on sight.
    - **Creepers:** fought with hit-and-step-back when `creeper_tactic` is `fight` (default on Normal, Hard and
      Expert), the bot's health is at or above `retreat_below_health`, and it holds a sword or axe. Otherwise
      they're fled within 6 blocks (or immediately when already swelling within 3 blocks).
    - **Archers:** closed on by sprinting with a side-step strafe when in sight within 16 blocks. If the bot won't
      fight the archer that shot it, it takes cover behind the nearest block within 8 blocks that breaks line of
      sight.
    - **Unreachable targets** (in a pen, on a ledge) are given up after three failed paths or three two-second
      attempts that don't close in, and left alone for 30 seconds, unless they hit the bot meanwhile.
    - **Runners:** a target whose own movement takes it away from the bot (at least 0.05 blocks a tick along the
      line between them; knockback from the bot's own hit doesn't count) puts the controller in `pursue`. That
      means held keys, sprinting, a swing on each ready cooldown and no "unreachable" count. It switches to a path
      for a moment when sight is lost for half a second or the ground ahead breaks (lava, water deeper than a
      block, a drop over three). The chase ends at 24 blocks gained on its closest point, three seconds unseen, the
      retreat line or the roaming radius, and a runner let go is benched for 30 seconds. While chased, the target
      counts like the bot's last attacker, so only someone who hits the bot takes over.
    - **Heals:** at `retreat_below_health`, with a heal held and the target not outmatching the bot (the gear
      comparison, aggression included), the controller enters `heal`. It turns away and sprints back for at most
      half a second or until 4 blocks off, then uses the heal while still backing off (the one case where a bot
      drinks with an enemy within 4 blocks), and fights on. The potion (instant) comes first, otherwise the golden
      apple, whose regeneration keeps the fight on too. A heal that fails isn't retried in that fight. `stow` keeps
      two golden apples and the style's `nether.potions` count of healing potions (at least two). `gear_up` crafts
      a golden apple from an apple and eight gold ingots when it holds fewer than two, an apple is at hand and 16
      or more gold ingots are in hand and in the chests.
    - **The tower:** three or more melee monsters (no bow, no creeper, no spider, nothing that flies; players and
      bots never count) within 6 blocks, no shooter within 16 blocks with a line of sight, the bot fit (at or above
      `retreat_below_health`), a solid floor, two placeable blocks held, and no water, hazard, low ceiling or another
      base's protected ground around the column. The bot holds the top while a melee threat stays within 8 blocks.
      It digs down after 40 ticks with none, right away for a shooter, or after 30 seconds without a hit. A bot that
      comes back to its own pillar fights from the top again rather than building another.
    - **The doorway:** two or more monsters other than creepers within 12 blocks of the house's door, and the bot
      within 12 blocks of it and fit. It stands on the inside cell one in from the door with the door open, steps
      back in when knocked out, and leaves after 40 ticks with no threat near the door or 30 seconds without a hit.
      A bot already shut inside stays behind its door.
    - **Standing odds:** below `retreat_below_health` a bot still fights a player or bot that hit it in the last ten
      seconds when its side (its kit and its allies' share, no aggression boost) weighs at least twice the attacker
      as it sees them. Monsters are never weighed this way: a hurt bot still runs from a zombie beyond four blocks,
      and always from a creeper, but it fights a lone monster down (an archer within 16 blocks, anything else but
      a creeper within 4, with no other monster or enemy within 12). Under three health it fights nothing.
    - **The pack rule:** three monsters within 10 blocks, two with a bow or crossbow, are never closed on outside
      the bot's own tunnel, and for five seconds after it has seen a pack it closes on none of its members.
    - **The wall-in:** a bot that would flee from a monster within 12 blocks where the sky doesn't light its feet
      closes every open cell round it (at most four open, a block held for each, the nearest monster's side first)
      and waits until nothing has been within 12 blocks for five seconds and it has healed to its retreat line and
      60% of its health, two minutes at most. A creeper near, or a monster within two blocks, makes it run instead.
      Under six health with no heal it walls in against a single monster anywhere with at most four open cells.
    - **The tunnel stand:** needs a stone or better sword and the bot standing in a one-wide tunnel of its own (its
      strip, stair or burrow shaft, or cells it cut in the last two minutes). It's kept for three seconds after the
      bot steps out into a notch, a junction or a cave it broke into. It yields below `retreat_below_health`, or
      when the bot is shot a second time within ten seconds from beyond its sword's reach while not yet a quarter
      of its health above that line. The bot then backs down its own tunnel, out of the arrows' line, and fights
      again once healed a quarter past the retreat line.
  </AccordionItem>
  <AccordionItem title="The gear comparison">
    A weight starts at 1.0, plus 0.1 per armor point, plus 0.1 to 0.8 for the best sword or axe by tier, plus 0.3
    for a bow, plus 1.0 per active level of Strength, plus enchantment bonuses (weapon damage enchantments and
    armor protection enchantments). A bot weighs itself by its whole kit (its best sword or axe anywhere in its
    inventory, and its bow while it has arrows), and an enemy by what it can see (worn armor, the weapon in the
    enemy's hands, and active effects). The bot's weight is boosted by its aggression:
    `weight × (1 + aggression × 0.5)`. It engages on sight only if that reaches the enemy's weight (a tie engages).

    For example, a Rusher with a stone sword (1.2 × 1.4 = 1.68) takes on someone with an iron sword (1.4). An
    unarmored bot won't take on iron armor plus an iron sword (2.9), not even with a netherite sword
    (1.8 × 1.5 = 2.7 at aggression 1). And an iron fighter who drank Strength (3.9) outmatches the same kit
    without it (2.9).

    In the Nether, blazes (2.5), hoglins (2.5), wither skeletons (3.0) and piglin brutes (3.5) are checked against
    the same gate using the difficulty's `threats` weights, and so are the dragon (6.0, outside a party's fight) and
    the shulkers a city raid weighs against its party (2.0 each). Self-defense, assisting allies, the base rule, regular
    Overworld monsters, `defend` posts and `attack` orders ignore the gear check. A bot inside its own house
    doesn't keep its distance from an enemy outside; it stays in and fights if the enemy comes in.
  </AccordionItem>
  <AccordionItem title="Group odds">
    Allies within 16 blocks that the bot can see add half their weight to its side: faction mates and allied
    players (weighed by what the bot sees of them) and player-built iron golems (weight 2.0, about iron armor plus
    an iron sword). Villagers add nothing. There's no cap, so three allies in iron make a naked bot charge a
    diamond-clad player. The comparison is re-checked during the fight, so the bot disengages if its allies leave.
  </AccordionItem>
  <AccordionItem title="Combat settings in the play style">
    ```json title="playstyle.json (excerpt)"
    "combat": {
      "weapons": ["sword", "bow"],
      "engage_range": 3.0,
      "retreat_below_health": 0.35,
      "shield_break_with_axe": true,
      "hit_and_run": true
    }
    ```

    `weapons` lists `sword`, `axe` and `bow` in order of preference (`crossbow` and `trident` are accepted but not
    used yet). `engage_range` is the standoff distance (2.0 to 3.5 blocks). Below `retreat_below_health` the bot
    flees, and it re-engages once its health is 0.25 above that. The difficulty's `combat_skill`,
    `reaction_ticks` and `aim_error_degrees` set swing timing, crit attempts, shield reaction and arrow spread.
    See [Play style files](/ai-players/reference/play-style-files/#combat).
  </AccordionItem>
  <AccordionItem title="Hunting remembered enemies">
    Once a second, every bot records the enemies it can see and shares the sightings with its faction (16 per
    bot, 32 per faction, newest per enemy). A sighting is forgotten after the style's `enemy_memory_minutes`, or
    right away when the enemy is offline, dead or no longer an enemy. A bot with aggression above 0 hunts the
    freshest sighting when it has at least a stone pickaxe, is healthy enough, would win the gear comparison and
    the sighting is within twice its roaming radius. It walks there, searches around four times, and fights as
    soon as the enemy shows up. A thief its base noticed is hunted before any other sighting, whatever the style's
    aggression and `hunt` weight. `/aiplayer info <bot>` lists sightings on its `enemies:` line.
  </AccordionItem>
  <AccordionItem title="Offenses in detail">
    Offenses are read from vanilla's own events (a container opened, a block broken, farmland trampled), never by
    scanning. Each event is matched against the base's records and against what each base bot had in view at that
    tick (sampled twice a second, after `reaction_ticks`). "A block of a building" is a cell inside a house, a
    storehouse or the enchanting corner, or on the wall ring at a post's height or above. A pen, a plot's torch or
    the portal frame don't count. Looking into a chest without taking anything is still theft. The play style's
    `offenses` key lists which of `theft`, `vandalism` and `trampling` the style answers (all three by default), and
    `grudge_minutes: 0` holds no grudge for an offense either. The grudge records its cause and `info` shows it:
    `grudges: Alice (9 min, theft)`. A faction already at war earns nothing new (it's an enemy already), and a bot
    that tramples a neighbor's plot earns nothing, since only players offend.
  </AccordionItem>
</Accordions>
