Skip to content

AI leaves garrison when moving away from captured village#25

Merged
dmccoystephenson merged 6 commits intomainfrom
copilot/add-enemy-garrison-mechanic
Feb 14, 2026
Merged

AI leaves garrison when moving away from captured village#25
dmccoystephenson merged 6 commits intomainfrom
copilot/add-enemy-garrison-mechanic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 11, 2026

When the AI captures a village, it now splits its army and leaves a 1-soldier garrison behind to build up forces, rather than moving on immediately with the full army and gaining no benefit from the village.

Changes Made

  • Garrison creation: Modified executeAI() to split off a 1-soldier garrison when an AI army moves away from an owned village, with safeguards against duplicate garrisons at the same village (checks both existing armies and garrisons created earlier in the same tick).
  • Minimum soldier threshold: AI armies garrisoning an owned village now stay put until they accumulate at least 5 soldiers (AI_MIN_SOLDIERS_TO_MOVE), preventing small garrison armies from immediately wandering off and creating trails of 1-soldier armies.
  • Army cap: Added a maximum of 5 total AI armies (AI_MAX_ARMIES). Garrison creation is skipped if the cap would be exceeded.
  • Tests: Added deterministic tests using getInternalStateForTest() to validate both garrison creation on departure and garrison staying behavior below the soldier threshold.

Testing

  • ✅ All 132 unit tests pass
  • ✅ CodeQL security scan: 0 alerts

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 11, 2026 15:06
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Copilot AI changed the title [WIP] Add garrison mechanic for enemy after village takeover AI leaves garrison when moving away from captured village Feb 11, 2026
@dmccoystephenson dmccoystephenson marked this pull request as ready for review February 12, 2026 00:02
# Conflicts:
#	backend/src/test/java/com/barony/backend/service/GameServiceTest.java
@dmccoystephenson
Copy link
Copy Markdown
Member

@copilot Unit tests for the backend are failing and the AI is splitting far too often:

image

Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 12, 2026

@copilot Unit tests for the backend are failing and the AI is splitting far too often:

<img width="1282" height="932" alt="image" src="https://github.com/user-attachments/assets/716e7214-0a14-4e7a-...

Fixed in ef4e0f1. Two issues addressed:

  1. Over-splitting: The garrison army would gain soldiers from village generation, then the AI would assign it a destination on the next tick, triggering another garrison split — repeating every tick. Fixed by checking for an existing friendly army already staying at the village before creating a new garrison.

  2. Test failure: The test used hardcoded village at (6,6) which broke after the random map generation merge. Updated to use getInternalStateForTest() for deterministic setup.

All 131 tests pass.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the AI turn logic so that when an AI army leaves an AI-owned village, it splits off a 1-soldier garrison (while avoiding repeated/duplicate garrison creation at the same village), and adds a deterministic regression test to validate the behavior.

Changes:

  • Update executeAI() to create a 1-soldier garrison when an AI army starts moving away from an AI-owned village.
  • Prevent garrison over-splitting by skipping garrison creation when a friendly non-moving army is already stationed at that village (including garrisons created earlier in the same tick).
  • Add a new deterministic test using getInternalStateForTest() to avoid randomness-related coordinate brittleness.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
backend/src/main/java/com/barony/backend/service/GameService.java Adds AI garrison-splitting logic when moving away from AI-owned villages, with safeguards against duplicate garrisons.
backend/src/test/java/com/barony/backend/service/GameServiceTest.java Adds a deterministic regression test for AI garrison behavior (and minor whitespace-only adjustments).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dmccoystephenson
Copy link
Copy Markdown
Member

@copilot The enemy is still splitting very often, causing trails of 1 man armies to pop up. The AI should decide to garrison at a village until it reaches a particular number of soldiers. Maybe a limit should be placed on how many armies can exist at one time too.

Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 12, 2026

@copilot The enemy is still splitting very often, causing trails of 1 man armies to pop up. The AI should decide to garrison at a village until it reaches a particular number of soldiers. Maybe a limi...

Fixed in dacfe1f. Two changes:

  1. Minimum soldier threshold (AI_MIN_SOLDIERS_TO_MOVE = 5): AI armies sitting on owned villages now stay put until they've accumulated at least 5 soldiers. This prevents the pattern where a 1-soldier garrison gains a soldier from village generation, immediately gets sent somewhere, and spawns another garrison.

  2. Army cap (AI_MAX_ARMIES = 5): No new garrisons are created if the AI already has 5 armies total.

All 132 tests pass including a new test verifying that small garrisons stay at their village.

@dmccoystephenson dmccoystephenson merged commit 2c75ea9 into main Feb 14, 2026
@dmccoystephenson dmccoystephenson deleted the copilot/add-enemy-garrison-mechanic branch February 14, 2026 06:20
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.

3 participants