-
Notifications
You must be signed in to change notification settings - Fork 56
Add barbarian activity levels #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
76ef1fd
Add barbarian activity buttons
ajhalme de718b3
Add BarbarianActivity save state
ajhalme cd513f4
Barbarian AI strategies
ajhalme aa23c99
Apply barb activity to unit spawn
ajhalme 4bdbff5
Make use of GetTilesVisibleToUnit on barb waking
ajhalme f373dc5
Add comments to clarify barb AI implementation isn't true to civ3
ajhalme 4df8a6d
Add DeriveTotalPossibleBarbCamps
ajhalme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| using System.Threading.Tasks; | ||
| using C7Engine.AI.UnitAI; | ||
| using C7GameData; | ||
| using C7GameData.AIData; | ||
|
|
||
| namespace C7Engine; | ||
|
|
||
| internal abstract class BaseStrategy : IBarbarianStrategy { | ||
| // TODO: Determine how barbarian AI is implemented in Civ3 | ||
| // TODO: What are the key parameters influencing barbarian activity levels in Civ3? | ||
|
|
||
| /// <summary> | ||
| /// Observe - Orient - Decide - Act. | ||
| /// | ||
| /// Note: This approach may or may not have anything to do with how Civ3 implements barbarian AI. | ||
| /// </summary> | ||
| public async Task PlayUnitTurn(Player player, MapUnit unit) { | ||
| // "Observe: Collect data and information from the environment through senses and feedback." | ||
|
|
||
| // Wake up the unit if there's a reason to do so | ||
| if (ShouldWake(player, unit)) | ||
| unit.wake(); | ||
|
|
||
| // Skip units that didn't wake up | ||
| if (unit.isFortified) | ||
| return; | ||
|
|
||
| var orientation = await Orient(player, unit); | ||
| var plan = await Decide(player, unit, orientation); | ||
| var result = await Act(player, unit, plan); | ||
|
|
||
| // TODO: store result | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Wake the unit if a foreign unit or the borders of a civ are in sight. | ||
| /// </summary> | ||
| private static bool ShouldWake(Player player, MapUnit unit) { | ||
| var tiles = player.tileKnowledge.GetTilesVisibleToUnit(unit.location); | ||
| foreach (Tile t in tiles) { | ||
| if (t.unitsOnTile.Count > 0 && t.unitsOnTile[0].owner != player) | ||
| return true; | ||
|
|
||
| if (t.OwningPlayer() != null) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// "Orient: Analyze and synthesize data to form a mental perspective, considering experience, | ||
| /// culture, and new information. This is considered the most important phase of the OODA loop." | ||
| /// </summary> | ||
| protected Task<Orientation> Orient(Player player, MapUnit unit) { | ||
| return Task.FromResult(new Orientation { | ||
| IsLastUnitInCamp = unit.location.hasBarbarianCamp && unit.location.unitsOnTile.Count == 1, | ||
| CombatIntel = CombatAI.MakeAiData(unit, player) | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// "Decide: Formulate a plan or course of action based on the orientation." | ||
| /// </summary> | ||
| protected async Task<UnitAI> Decide(Player player, MapUnit unit, Orientation orientation) { | ||
| // Barbarians defend their camp if it is unguarded. | ||
| if (orientation.IsLastUnitInCamp) | ||
| return new DefenderAI(DefenderAI.MakeAiDataForDefendInPlace(unit, player)); | ||
|
|
||
| // Decide whether to engage enemy units | ||
| if (orientation.CanEngage() && DecideToEngage(player, unit, orientation)) | ||
| return new CombatAI(orientation.CombatIntel); | ||
|
|
||
| // Decide whether to explore | ||
| if (DecideToExplore(player, unit, orientation)) { | ||
| var maybeAiData = ExplorerAI.MaybeMakeAiData(unit, player); | ||
| if (maybeAiData != null) | ||
| return new ExplorerAI(maybeAiData); | ||
| } | ||
|
|
||
| // Defend otherwise | ||
| return new DefenderAI(DefenderAI.MakeAiDataForDefendInPlace(unit, player)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// "Act: Implement the decision, which creates new data and feeds back into the observation phase." | ||
| /// </summary> | ||
| protected async Task<UnitAI.Result> Act(Player player, MapUnit unit, UnitAI plan) { | ||
| return await plan.PlayTurn(player, unit); | ||
| } | ||
|
|
||
| internal class Orientation { | ||
| public bool IsLastUnitInCamp { get; set; } | ||
| public CombatAIData CombatIntel { get; set; } | ||
|
|
||
| public bool CanEngage() => CombatIntel != null; | ||
| } | ||
|
|
||
| protected abstract bool DecideToEngage(Player player, MapUnit unit, Orientation orientation); | ||
|
|
||
| protected abstract bool DecideToExplore(Player player, MapUnit unit, Orientation orientation); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System.Threading.Tasks; | ||
| using C7GameData; | ||
|
|
||
| namespace C7Engine; | ||
|
|
||
| internal interface IBarbarianStrategy { | ||
| Task PlayUnitTurn(Player player, MapUnit unit); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using C7GameData; | ||
|
|
||
| namespace C7Engine; | ||
|
|
||
| /// <summary> | ||
| /// Civ3 Manual - Raging: | ||
| /// You asked for it! The world is full of barbarians,and they appear in large numbers. | ||
| /// | ||
| /// Note: Implementation is not based on known Civ3 AI logic. | ||
| /// </summary> | ||
| internal class RagingStrategy : BaseStrategy { | ||
| protected override bool DecideToEngage(Player player, MapUnit unit, Orientation orientation) { | ||
| return true; | ||
| } | ||
|
|
||
| protected override bool DecideToExplore(Player player, MapUnit unit, Orientation orientation) { | ||
| return GameData.rng.Next(100) < 50; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.