Skip to content

Commit

Permalink
feat: gather farms
Browse files Browse the repository at this point in the history
  • Loading branch information
01010100b committed Sep 12, 2021
1 parent b17089a commit 80f25c6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
25 changes: 23 additions & 2 deletions Unary/Managers/BuildingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Unary.Managers
{
class BuildingManager : Manager
{
private static readonly Point[] TC_FARM_DELTAS = new[] { new Point(2, 3), new Point(-1, 3), new Point(3, 0), new Point(3, -3), new Point(-4, 2), new Point(-4, -1), new Point(0, -4), new Point(-3, -4) };
private static readonly Point[] MILL_FARM_DELTAS = new[] { new Point(-1, 2), new Point(2, -1), new Point(2, 2), new Point(-3, -1), new Point(-1, -3) };
internal static readonly Point[] TC_FARM_DELTAS = new[] { new Point(2, 3), new Point(-1, 3), new Point(3, 0), new Point(3, -3), new Point(-4, 2), new Point(-4, -1), new Point(0, -4), new Point(-3, -4) };
internal static readonly Point[] MILL_FARM_DELTAS = new[] { new Point(-1, 2), new Point(2, -1), new Point(2, 2), new Point(-3, -1), new Point(-1, -3) };

private readonly HashSet<Unit> BuildingFoundations = new HashSet<Unit>();
private readonly List<BuildOperation> BuildOperations = new List<BuildOperation>();
Expand Down Expand Up @@ -70,6 +70,26 @@ public IEnumerable<Tile> GetBuildingPlacements(UnitType building)
throw new NotImplementedException();
}

public IEnumerable<Tile> GetFarmPlacements(Unit dropsite)
{
var deltas = TC_FARM_DELTAS;
if (dropsite[ObjectData.BASE_TYPE] == 68)
{
deltas = MILL_FARM_DELTAS;
}

foreach (var delta in deltas)
{
var x = dropsite.Position.PointX + delta.X;
var y = dropsite.Position.PointY + delta.Y;

if (Unary.GameState.Map.IsOnMap(x, y))
{
yield return Unary.GameState.Map.GetTile(x, y);
}
}
}

internal override void Update()
{
Unary.GameState.SetStrategicNumber(StrategicNumber.DISABLE_BUILDER_ASSISTANCE, 1);
Expand Down Expand Up @@ -182,6 +202,7 @@ private void DoBuildOperations()
if (builders.Count == 0)
{
Unary.Log.Info($"Could not find enough builders");

break;
}

Expand Down
4 changes: 2 additions & 2 deletions Unary/Managers/EconomyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void ManageGatherers()
Unary.GameState.SetStrategicNumber(StrategicNumber.MAXIMUM_STONE_DROP_DISTANCE, -2);

Unary.GameState.SetStrategicNumber(StrategicNumber.MAXIMUM_FOOD_DROP_DISTANCE, -2);
Unary.GameState.SetStrategicNumber(StrategicNumber.MAXIMUM_HUNT_DROP_DISTANCE, 8);
Unary.GameState.SetStrategicNumber(StrategicNumber.MAXIMUM_HUNT_DROP_DISTANCE, -2);
Unary.GameState.SetStrategicNumber(StrategicNumber.ENABLE_BOAR_HUNTING, 0);
Unary.GameState.SetStrategicNumber(StrategicNumber.LIVESTOCK_TO_TOWN_CENTER, 1);

Expand Down Expand Up @@ -295,7 +295,7 @@ private void ManageGatherOperations()
var free = op.Units.FirstOrDefault(u => u[ObjectData.CARRY] == 0);
if (free != null && Unary.Rng.NextDouble() < 0.1)
{
free.Target(op.Dropsite.Position);
free.Target(op.Dropsite);
op.RemoveUnit(free);
}
}
Expand Down
42 changes: 41 additions & 1 deletion Unary/Operations/GatherOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void DoFood()
UnitCapacity = 0;

var units = Units;
units.Sort((a, b) => a[ObjectData.ID].CompareTo(b[ObjectData.ID]));

var meat = GetMeat();
if (meat.Count > 0)
Expand Down Expand Up @@ -182,7 +183,31 @@ private void DoFood()

var berries = GetBerries();
UnitCapacity += Math.Min(4, berries.Count * 2);
DoGathering(units, berries);
if (berries.Count > 0)
{
var gatherers = new List<Unit>();
for (int i = 0; i < 4; i++)
{
if (units.Count == 0)
{
break;
}

gatherers.Add(units[0]);
units.RemoveAt(0);
}
DoGathering(gatherers, berries);
}

var farms = GetFarms();
UnitCapacity += farms.Count;
for (int i = 0; i < Math.Min(farms.Count, units.Count); i++)
{
if (units[i][ObjectData.TARGET_ID] != farms[i].Id)
{
units[i].Target(farms[i]);
}
}
}

private List<Unit> GetMeat()
Expand Down Expand Up @@ -235,6 +260,21 @@ private List<Unit> GetBerries()
return berries;
}

private List<Unit> GetFarms()
{
var farms = new List<Unit>();
foreach (var tile in Unary.BuildingManager.GetFarmPlacements(Dropsite))
{
var farm = tile.Units.FirstOrDefault(u => u.Targetable && u[ObjectData.CLASS] == (int)UnitClass.Farm);
if (farm != null)
{
farms.Add(farm);
}
}

return farms;
}

private void DoGathering(List<Unit> units, List<Unit> resources)
{
if (units.Count == 0 || resources.Count == 0)
Expand Down
Binary file modified Unary/sheet.ods
Binary file not shown.

0 comments on commit 80f25c6

Please sign in to comment.