Skip to content

Added capturing of non-combat units with related bugfixes - #1137

Open
zack-the-coding-actuary wants to merge 5 commits into
C7-Game:Developmentfrom
zack-the-coding-actuary:kidnapping
Open

zack-the-coding-actuary wants to merge 5 commits into
C7-Game:Developmentfrom
zack-the-coding-actuary:kidnapping

Conversation

@zack-the-coding-actuary

@zack-the-coding-actuary zack-the-coding-actuary commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Added all the mechanics I think we should need for proper non-combat unit capture, including addressing the bug in #395. Total changes include:

Capturing

  • All units play the "surrender" animation, if present for the unit when captured. Barbarians still make noncombat units play the "death" animation.
  • Captured settlers now generate X workers of the defender's nationality for the attacker, where X equal to the population cost of the settler and X must be at least 1.
  • Captured workers convert into a worker for the attacker, preserving the defender's nationality (reduced worker speed working as intended)
  • If the unit is available for the attacker to build and they know the tech to build it, they can capture it
  • Otherwise, the unit is destroyed (i.e. non-Expansionist civs can't capture scouts, a player in the Middle Ages cannot capture an Artillery, etc.)
  • Barbarians unconditionally destroy non-combat units, they no longer go through the combat loops in the Fight method
  • Attacking units now properly capture non-combat units and move into the target tile after winning combat against the last combat unit on the target tile.

Retreat logic

  • Non-combat units can no longer potentially retreat from battle
  • Combat units may never retreat if they're the last combat unit defending non-combat units

Future TODOs

  • These mechanics do not consider sea or air units. Attempting to capture a city with a land unit that has a sea or air unit defending has not been tested.

Threw in a lot of tests too since there were a lot of edge cases to go over, lmk if you guys have questions or changes, thanks!

- Await each CAPTURE animation before removing the unit, so the animations play one after another instead of the unit disappearing immediately
- Iterate over a copy of the tile's unit list in Fight(), since RemoveFromPlay() removes units from location.unitsOnTile and the modified collection threw an InvalidOperationException that stopped Move() before the attacker took the defeated unit's tile
- Play each CAPTURE animation in sequence (awaited) before the unit is removed or transferred, so the animation is visible instead of the unit vanishing immediately
- Iterate over a copy of the tile's unit list in Fight(), since RemoveFromPlay() modifies location.unitsOnTile and the modified collection threw an InvalidOperationException that stopped Move() before the attacker advanced
- Captured settlers are removed and replaced with workers for the attacker, one per population cost (minimum 1), using the cheapest worker prototype the attacker can build and keeping the settler's original nationality
- Other non-combat units, including workers, are transferred to the attacker if the attacker has the required tech: they are removed from the previous owner's unit list and AI, added to the attacker's list, and both players' active tiles are recomputed. Units the attacker can't build are destroyed
- Barbarians still destroy non-combat units rather than capturing them
- In Move(), only hostile defenders now block the attacker from advancing after a win, so captured units and spawned workers left on the tile no longer stop it from taking the tile
- GameData.SpawnUnit now returns the spawned MapUnit; its parameters are unchanged
…ng, and add capture tests

- A non-combat unit is now only captured if the attacker knows its required tech and the attacker's civ can produce it (producibleBy); otherwise it is destroyed, e.g. an Egyptian attacker cannot capture a Portuguese scout
- Non-combat units can no longer retreat from battle, so units with more than one movement point such as scouts are always captured or killed instead of sometimes escaping barbarians
- Add NonCombatUnitCaptureTest covering captured workers (ownership, nationality, AI detachment, slave work speed and recapture by the original civ), settlers becoming one worker per population cost (minimum 1) using the cheapest buildable worker, and capture requiring both the tech and producibility regardless of missing resources
- The tests also cover barbarians destroying non-combat units instead of capturing them, mixed stacks, combat units protecting non-combat units, and the attacker advancing onto the tile after every outcome without being blocked by friendly units
…defender retreating

- Move the capture/destroy logic into a private CaptureOrDestroyNonCombatUnits method, which only affects non-combat units hostile to the attacker
- Call it when an attacker moves onto a tile holding only non-combat units, and also after an attacker wins a battle and no hostile combat unit able to defend the tile remains, so surviving non-combat units surrender and the attacker takes the tile
- Barbarians now use the same path but always destroy non-combat units (DEATH animation) instead of capturing them, replacing the special case that sent them through a regular battle
- A combat defender can no longer retreat if it is the last land combat unit on its tile, so it cannot leave non-combat units undefended
- Add tests for capture after the last combat unit dies (workers captured, settlers converted, uncapturable units destroyed, barbarians destroying everything), friendly units being left alone, nothing happening when the attacker loses or another combat unit still defends, and the retreat rule
- Replace the "more than one land combat unit on the tile" requirement for retreating, which stopped a lone defender from ever retreating, with a check that only blocks the retreat when it would leave non-combat units on the tile without a land combat unit protecting them
- Add a test that a lone combat defender with nothing to protect still retreats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant