Skip to content

Commit

Permalink
feat: add scout controller
Browse files Browse the repository at this point in the history
  • Loading branch information
01010100b committed Oct 3, 2021
1 parent 3a1c4d8 commit 6d3577d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
29 changes: 28 additions & 1 deletion Unary/Managers/EconomyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ class EconomyManager : Manager
public int ExtraStonePercentage { get; set; } = 0;
public int MaxTownCenters { get; set; } = 1;
public int ConcurrentVillagers { get; set; } = 3;

public readonly List<Unit> Wood = new();
public readonly List<Unit> Food = new();
public readonly List<Unit> Gold = new();
public readonly List<Unit> Stone = new();
public readonly List<Unit> Farms = new();
public readonly List<Unit> Dropsites = new();

private readonly Dictionary<Unit, List<GatherOperation>> GatherOperations = new Dictionary<Unit, List<GatherOperation>>();
private readonly Dictionary<Unit, List<GatherOperation>> GatherOperations = new();
private int FoodGatherers { get; set; } = 0;
private int WoodGatherers { get; set; } = 0;
private int GoldGatherers { get; set; } = 0;
Expand All @@ -34,6 +41,26 @@ public EconomyManager(Unary unary) : base(unary)

internal override void Update()
{
Wood.Clear();
Food.Clear();
Gold.Clear();
Stone.Clear();
Farms.Clear();
Dropsites.Clear();

foreach (var unit in Unary.GameState.MyPlayer.Units.Where(u => u.Targetable))
{
var type = unit[ObjectData.BASE_TYPE];
if (type == Unary.Mod.TownCenter || type == Unary.Mod.LumberCamp || type == Unary.Mod.MiningCamp || type == Unary.Mod.Mill || type == Unary.Mod.Dock)
{
Dropsites.Add(unit);
}
else if (type == Unary.Mod.Farm)
{
Farms.Add(unit);
}
}

ManagePopulation();
ManageGatherers();
ManageDropsites();
Expand Down
3 changes: 1 addition & 2 deletions Unary/Managers/MilitaryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class ScoutingState
{
public readonly Tile Tile;
public TimeSpan LastAttemptGameTime { get; set; } = TimeSpan.MinValue;
public TimeSpan LastAccessGameTime { get; set; } = TimeSpan.MinValue;

public ScoutingState(Tile tile)
{
Expand Down Expand Up @@ -66,7 +65,7 @@ private void DoScouting()
if (scouts.Count == 0)
{
Unit scout = null;
var idlers = Unary.UnitsManager.GetControllers<IdleController>();
var idlers = Unary.UnitsManager.GetControllers<IdlerController>();
foreach (var idler in idlers)
{
if (idler.Unit[ObjectData.CMDID] == (int)CmdId.MILITARY)
Expand Down
2 changes: 1 addition & 1 deletion Unary/Managers/UnitsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal override void Update()
{
if (!Units.ContainsKey(unit))
{
Units[unit] = new IdleController(unit, Unary);
Units[unit] = new IdlerController(unit, Unary);
Unary.Log.Info($"New unit control {unit.Id}");
}
}; break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Unary.UnitControllers
{
class IdleController : UnitController
class IdlerController : UnitController
{
public IdleController(Unit unit, Unary unary) : base(unit, unary)
public IdlerController(Unit unit, Unary unary) : base(unit, unary)
{

}
Expand Down
13 changes: 5 additions & 8 deletions Unary/UnitControllers/ScoutController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ protected override void Tick()

private void FindState()
{
Debug.WriteLine("Finding state");
var los = 4;

ScoutingState best = null;
Expand All @@ -51,8 +50,7 @@ private void FindState()

foreach (var state in Unary.MilitaryManager.GetScoutingStatesForLos(los))
{

if (state.LastAccessGameTime > TimeSpan.Zero || state.LastAttemptGameTime > TimeSpan.Zero)
if (state.LastAttemptGameTime > TimeSpan.Zero)
{
continue;
}
Expand All @@ -73,7 +71,9 @@ private void FindState()
if (explored < ExploredFraction)
{

var cost = state.Tile.Position.DistanceTo(my_pos) + Math.Abs(AttractorRadius - state.Tile.Position.DistanceTo(AttractorPosition));
var cost = state.Tile.Position.DistanceTo(my_pos)
+ Math.Abs(AttractorRadius - state.Tile.Position.DistanceTo(AttractorPosition));

if (best == null || cost < best_cost)
{
best = state;
Expand All @@ -91,11 +91,10 @@ private void FindState()

private void ExploreState()
{
Debug.WriteLine("Exploring state");
State.LastAttemptGameTime = Unary.GameState.GameTime;

var target_pos = State.Tile.Center;
var distance = target_pos.DistanceTo(Unit.Position);

if (Math.Abs(distance - LastDistance) > 1)
{
LastDistanceChangeGameTime = Unary.GameState.GameTime;
Expand All @@ -110,8 +109,6 @@ private void ExploreState()
}
else
{
Debug.WriteLine("Done exploring state");
State.LastAccessGameTime = Unary.GameState.GameTime;
State = null;
}
}
Expand Down
Binary file modified Unary/sheet.ods
Binary file not shown.

0 comments on commit 6d3577d

Please sign in to comment.