feat(BuildCalc) Optimized the build calculator

This commit is contained in:
2022-04-13 15:13:06 -04:00
parent 656b914c6e
commit 39db0afbe5
22 changed files with 803 additions and 561 deletions
+10 -14
View File
@@ -244,18 +244,15 @@ public interface IMemoryTesterService {
public void Unsubscribe(MemoryAction memoryAction);
}
public interface IGameLogicService
{
public bool Add(EntityModel entity, int atInterval);
public int MeetsRequirements(EntityModel entity, int interval);
public int MeetsAlloy(EntityModel entity, int interval);
public int MeetsEther(EntityModel entity, int interval);
public int MeetsPyre(EntityModel entity, int interval);
public int MeetsSupply(EntityModel entity, int interval);
public int MeetsTrainingQueue(EntityModel entity, int interval);
}
public interface IBuildOrderService {
public Dictionary<int, List<EntityModel>> StartedOrders { get; }
public Dictionary<int, List<EntityModel>> CompletedOrders { get; }
public Dictionary<string, int> UniqueCompletedTimes { get; }
public Dictionary<int, int> SupplyCountTimes { get; }
public bool Add(EntityModel entity, IEconomyService withEconomy, IToastService toastService);
public void Add(EntityModel entity, int atInterval);
@@ -268,10 +265,9 @@ public interface IBuildOrderService {
public void SetColor(string Color);
public string GetColor();
public bool MeetsRequirements(EntityModel entity, int interval);
public int? WillMeetRequirements(EntityModel entity);
public int? WillMeetSupply(EntityModel entity);
public Dictionary<int, List<EntityModel>> GetOrders();
public List<EntityModel> GetOrdersAt(int interval);
public List<EntityModel> GetCompletedAt(int interval);
public List<EntityModel> GetCompletedBefore(int interval);
public List<EntityModel> GetHarvestersCompletedBefore(int interval);
+180 -170
View File
@@ -3,6 +3,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Model.BuildOrders;
using Model.Entity;
using Model.Entity.Data;
using Model.Feedback;
using Model.Types;
using YamlDotNet.Serialization;
@@ -15,6 +16,11 @@ public class BuildOrderService : IBuildOrderService
private readonly int HumanMicro = 2;
private int lastInterval;
public Dictionary<int, List<EntityModel>> StartedOrders => buildOrder.StartedOrders;
public Dictionary<int, List<EntityModel>> CompletedOrders => buildOrder.CompletedOrders;
public Dictionary<string, int> UniqueCompletedTimes => buildOrder.UniqueCompletedTimes;
public Dictionary<int, int> SupplyCountTimes => buildOrder.SupplyCountTimes;
public int GetLastRequestInterval()
{
return lastInterval;
@@ -38,111 +44,123 @@ public class BuildOrderService : IBuildOrderService
public void Add(EntityModel entity, int atInterval)
{
if (!buildOrder.StartedOrders.ContainsKey(atInterval))
{
buildOrder.StartedOrders.Add(atInterval, new List<EntityModel> { });
}
buildOrder.StartedOrders.Add(atInterval, new List<EntityModel>());
var production = entity.Production();
var completedTime = atInterval;
if (production != null)
{
completedTime += production.BuildTime;
}
if (!buildOrder.CompletedOrders.ContainsKey(atInterval))
{
buildOrder.CompletedOrders.Add(completedTime, new List<EntityModel> { });
}
if (production != null) completedTime += production.BuildTime;
if (!buildOrder.CompletedOrders.ContainsKey(completedTime))
buildOrder.CompletedOrders.Add(completedTime, new List<EntityModel>());
var supply = entity.Supply();
buildOrder.StartedOrders[atInterval].Add(entity.Clone());
buildOrder.CompletedOrders[completedTime].Add(entity.Clone());
if (!buildOrder.UniqueCompletedTimes.ContainsKey(entity.DataType))
buildOrder.UniqueCompletedTimes.Add(entity.DataType, atInterval);
if (supply != null)
{
if (!supply.Takes.Equals(0)) buildOrder.CurrentSupplyUsed += supply.Takes;
if (!supply.Grants.Equals(0))
buildOrder.SupplyCountTimes.Add(buildOrder.SupplyCountTimes.Last().Key + supply.Grants, completedTime);
}
if (atInterval > lastInterval) lastInterval = atInterval;
NotifyDataChanged();
}
public int? WillMeetRequirements(EntityModel entity)
{
var requirements = entity.Requirements();
if (requirements.Count == 0) return 0;
var metTime = 0;
foreach (var requiredEntity in requirements)
{
if (buildOrder.UniqueCompletedTimes.TryGetValue(requiredEntity.Id, out var completedTime))
{
if (completedTime > metTime) metTime = completedTime;
}
else
{
return null;
}
}
return metTime;
}
public int? WillMeetSupply(EntityModel entity)
{
var supply = entity.Supply();
if (supply == null || supply.Takes.Equals(0)) return 0;
foreach (var supplyAtTime in buildOrder.SupplyCountTimes)
if (supply.Takes + buildOrder.CurrentSupplyUsed < supplyAtTime.Key)
return supplyAtTime.Value;
return null;
}
public bool Add(EntityModel entity, IEconomyService withEconomy, IToastService withToasts)
{
var production = entity.Production();
var atInterval = lastInterval;
if (production != null)
{
for (var interval = lastInterval; interval < withEconomy.GetOverTime().Count; interval++)
{
var economyAtSecond = withEconomy.GetOverTime()[interval];
if (economyAtSecond.Alloy >= production.Alloy && economyAtSecond.Ether >= production.Ether &&
economyAtSecond.Pyre >= production.Pyre)
{
if (!MeetsSupply(entity))
{
withToasts.AddToast(new ToastModel
{
Title = "Supply Cap Reached", Message = "Build more supply!",
SeverityType = SeverityType.Error
});
return false;
}
if (!HandleSupply(entity, withToasts, ref atInterval)) return false;
if (!HandleRequirements(entity, withToasts, ref atInterval)) return false;
if (!HandleEconomy(entity, withEconomy, withToasts, ref atInterval)) return false;
if (!MeetsRequirements(entity, interval)) continue;
Add(entity, atInterval);
//Account for human Micro delay
interval += HumanMicro;
if (!buildOrder.StartedOrders.ContainsKey(interval))
buildOrder.StartedOrders.Add(interval, new List<EntityModel> { entity.Clone() });
else
buildOrder.StartedOrders[interval].Add(entity.Clone());
lastInterval = interval;
NotifyDataChanged();
return true;
}
if (interval + 1 == withEconomy.GetOverTime().Count)
{
if (economyAtSecond.Ether < production.Ether)
withToasts.AddToast(new ToastModel
{
Title = "Not Enough Ether", Message = "Build more ether extractors!",
SeverityType = SeverityType.Error
});
}
}
}
else
{
Add(entity, 0);
NotifyDataChanged();
return true;
}
return false;
return true;
}
public void RemoveLast()
{
EntityModel entityRemoved = null!;
if (buildOrder.StartedOrders.Keys.Count > 1)
{
var last = buildOrder.StartedOrders.Keys.Last();
var lastStarted = buildOrder.StartedOrders.Keys.Last();
var lastCompleted = buildOrder.CompletedOrders.Keys.Last();
if (buildOrder.StartedOrders[last].Count > 0)
EntityModel entityRemoved = default!;
if (buildOrder.StartedOrders[lastStarted].Count > 0)
{
entityRemoved = buildOrder.StartedOrders[last].Last();
buildOrder.StartedOrders[last].Remove(buildOrder.StartedOrders[last].Last());
entityRemoved = buildOrder.StartedOrders[lastStarted].Last();
buildOrder.StartedOrders[lastStarted].Remove(buildOrder.StartedOrders[lastStarted].Last());
buildOrder.CompletedOrders[lastCompleted].Remove(buildOrder.CompletedOrders[lastCompleted].Last());
}
if (buildOrder.StartedOrders[last].Count == 0) buildOrder.StartedOrders.Remove(last);
if (buildOrder.StartedOrders[lastStarted].Count == 0) buildOrder.StartedOrders.Remove(lastStarted);
if (buildOrder.CompletedOrders[lastCompleted].Count == 0) buildOrder.CompletedOrders.Remove(lastCompleted);
if (buildOrder.StartedOrders.Keys.Count > 0)
lastInterval = buildOrder.StartedOrders.Keys.Last() + 1;
lastInterval = buildOrder.StartedOrders.Keys.Last();
else
lastInterval = 1;
lastInterval = 0;
if (entityRemoved?.Info()?.Descriptive == DescriptiveType.Worker)
if (entityRemoved.Supply()?.Grants > 0)
SupplyCountTimes.Remove(SupplyCountTimes.Last().Key);
if (entityRemoved.Supply()?.Takes > 0)
buildOrder.CurrentSupplyUsed -= entityRemoved.Supply()!.Takes;
if (UniqueCompletedTimes[entityRemoved!.DataType].Equals(lastInterval + entityRemoved.Production()!.BuildTime))
UniqueCompletedTimes.Remove(entityRemoved.DataType);
if (entityRemoved.Info().Descriptive == DescriptiveType.Worker)
{
RemoveLast();
return;
@@ -172,28 +190,9 @@ public class BuildOrderService : IBuildOrderService
return buildOrderText;
}
public List<EntityModel> GetOrdersAt(int interval)
{
if (!buildOrder.StartedOrders.ContainsKey(interval))
{
return new List<EntityModel>();
}
return buildOrder.StartedOrders[interval].ToList();
}
public List<EntityModel> GetCompletedAt(int interval)
{
if (!buildOrder.CompletedOrders.ContainsKey(interval))
{
return new List<EntityModel>();
}
return buildOrder.CompletedOrders[interval].ToList();
}
public List<EntityModel> GetCompletedBefore(int interval)
{
{
return (from ordersAtTime in buildOrder.StartedOrders
from orders in ordersAtTime.Value
where ordersAtTime.Key + (orders.Production() == null ? 0 : orders.Production().BuildTime) <= interval
@@ -209,52 +208,10 @@ public class BuildOrderService : IBuildOrderService
select orders).ToList();
}
public bool MeetsRequirements(EntityModel entity, int requestedInterval)
public void SetName(string name)
{
var requirements = entity.Requirements();
if (requirements.Count == 0) return true;
foreach (var requirement in requirements)
if (requirement.Requirement == RequirementType.Morph)
{
var entitiesNeeded = from entitiesAtInterval in buildOrder.StartedOrders
from requiredEntity in entitiesAtInterval.Value
where requestedInterval > entitiesAtInterval.Key +
(requiredEntity.Production() == null ? 0 : requiredEntity.Production().BuildTime)
where requiredEntity.DataType == requirement.Id
select requiredEntity;
var entitiesAlreadyMorphed = from entitiesAtInterval in buildOrder.StartedOrders
from existingEntity in entitiesAtInterval.Value
where existingEntity.DataType == entity.DataType
select existingEntity;
if (entitiesAlreadyMorphed.Count() >= entitiesNeeded.Count())
return false;
}
else
{
var entitiesNeeded = from entitiesAtInterval in buildOrder.StartedOrders
from requiredEntity in entitiesAtInterval.Value
where requestedInterval > entitiesAtInterval.Key +
(requiredEntity.Production() == null ? 0 : requiredEntity.Production().BuildTime)
where requiredEntity.DataType == requirement.Id
select requiredEntity;
if (!entitiesNeeded.Any()) return false;
if (entitiesNeeded.Any() == false)
return false;
}
return true;
}
public void SetName(string Name)
{
buildOrder.Name = Name;
buildOrder.Name = name;
NotifyDataChanged();
}
@@ -263,9 +220,9 @@ public class BuildOrderService : IBuildOrderService
return buildOrder.Name;
}
public void SetNotes(string Notes)
public void SetNotes(string notes)
{
buildOrder.Notes = Notes;
buildOrder.Notes = notes;
NotifyDataChanged();
}
@@ -285,37 +242,90 @@ public class BuildOrderService : IBuildOrderService
return buildOrder.Color;
}
private bool HandleEconomy(EntityModel entity, IEconomyService withEconomy, IToastService withToasts,
ref int atInterval)
{
var production = entity.Production();
if (production == null) return true;
for (var interval = atInterval; interval < withEconomy.GetOverTime().Count; interval++)
{
var economyAtSecond = withEconomy.GetOverTime()[interval];
if (economyAtSecond.Alloy >= production.Alloy && economyAtSecond.Ether >= production.Ether &&
economyAtSecond.Pyre >= production.Pyre)
{
atInterval = interval;
if (entity.EntityType != EntityType.Army)
{
atInterval += HumanMicro;
}
return true;
}
}
if (withEconomy.GetOverTime().Last().Ether < production.Ether)
withToasts.AddToast(new ToastModel
{
Title = "Not Enough Ether", Message = "Build more ether extractors!",
SeverityType = SeverityType.Error
});
if (withEconomy.GetOverTime().Last().Alloy < production.Alloy)
withToasts.AddToast(new ToastModel
{
Title = "Not Enough Alloy", Message = "Build more bases!",
SeverityType = SeverityType.Error
});
return false;
}
private bool HandleSupply(EntityModel entity, IToastService withToasts, ref int atInterval)
{
var minSupplyInterval = WillMeetSupply(entity);
if (minSupplyInterval == null)
{
withToasts.AddToast(new ToastModel
{
Title = "Supply Cap Reached", Message = "Build more supply!",
SeverityType = SeverityType.Error
});
return false;
}
if (minSupplyInterval > atInterval) atInterval = (int)minSupplyInterval;
return true;
}
private bool HandleRequirements(EntityModel entity, IToastService withToasts, ref int atInterval)
{
var minRequirementInterval = WillMeetRequirements(entity);
if (minRequirementInterval == null)
{
withToasts.AddToast(new ToastModel
{
Title = "Missing Requirements", Message = "You don't have what's needed for this unit.",
SeverityType = SeverityType.Error
});
return false;
}
if (minRequirementInterval > atInterval) atInterval = (int)minRequirementInterval;
return true;
}
private event Action OnChange = null!;
private void NotifyDataChanged()
{
OnChange?.Invoke();
}
public bool MeetsSupply(EntityModel entity)
{
var supply = entity.Supply();
if (supply == null || supply.Takes == 0) return true;
var supplyTakenTotal = 0;
var supplyTakens = from entitiesAtInterval in buildOrder.StartedOrders
from supplyTakingEntity in entitiesAtInterval.Value
where supplyTakingEntity.Supply()?.Takes > 0
select supplyTakingEntity.Supply().Takes;
foreach (var supplyTaken in supplyTakens) supplyTakenTotal += supplyTaken;
var supplyGrantedTotal = 0;
var supplyGranteds = from entitiesAtInterval in buildOrder.StartedOrders
from supplyGrantingEntity in entitiesAtInterval.Value
where supplyGrantingEntity.Supply()?.Grants > 0
select supplyGrantingEntity.Supply().Grants;
foreach (var supplyGranted in supplyGranteds) supplyGrantedTotal += supplyGranted;
if (supplyGrantedTotal > 160) supplyGrantedTotal = 160;
if (supplyTakenTotal + supply.Takes > supplyGrantedTotal) return false;
return true;
}
}
+46 -31
View File
@@ -7,16 +7,20 @@ namespace Services.Immortal;
public class EconomyService : IEconomyService {
private List<EconomyModel> _economyOverTime = null!;
private event Action OnChange = null!;
public List<EconomyModel> GetOverTime() {
return _economyOverTime;
}
public void Subscribe(Action action) {
onChange += action;
OnChange += action;
}
public void Unsubscribe(Action action) {
onChange -= action;
OnChange -= action;
}
public void Calculate(IBuildOrderService buildOrder, ITimingService timing, int fromInterval) {
@@ -35,9 +39,11 @@ public class EconomyService : IEconomyService {
while (_economyOverTime.Count < timing.GetTiming()) _economyOverTime.Add(new EconomyModel { Interval = _economyOverTime.Count - 1 });
for (var interval = fromInterval; interval < timing.GetTiming(); interval++) {
for (var interval = fromInterval; interval < timing.GetTiming(); interval++)
{
var economyAtSecond = _economyOverTime[interval];
if (interval > 0) {
if (interval > 0)
{
economyAtSecond.Alloy = _economyOverTime[interval - 1].Alloy;
economyAtSecond.Ether = _economyOverTime[interval - 1].Ether;
economyAtSecond.Pyre = _economyOverTime[interval - 1].Pyre;
@@ -62,10 +68,12 @@ public class EconomyService : IEconomyService {
economyAtSecond.Pyre += 1;
// Add funds
foreach (var entity in economyAtSecond.Harvesters) {
foreach (var entity in economyAtSecond.Harvesters)
{
var harvester = entity.Harvest();
if (harvester.RequiresWorker)
if (harvester.Resource == ResourceType.Alloy) {
if (harvester.Resource == ResourceType.Alloy)
{
var usedWorkers = Math.Min(harvester.Slots, freeWorkers);
economyAtSecond.Alloy += harvester.HarvestedPerInterval * usedWorkers;
freeWorkers -= usedWorkers;
@@ -73,7 +81,8 @@ public class EconomyService : IEconomyService {
if (usedWorkers < harvester.Slots) workersNeeded += 1;
}
if (harvester.RequiresWorker == false) {
if (harvester.RequiresWorker == false)
{
if (harvester.Resource == ResourceType.Ether)
economyAtSecond.Ether += harvester.HarvestedPerInterval * harvester.Slots;
@@ -85,46 +94,57 @@ public class EconomyService : IEconomyService {
// Create new worker
if (economyAtSecond.CreatingWorkerCount > 0)
for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++)
if (economyAtSecond.CreatingWorkerDelays[i] > 0) {
if (economyAtSecond.Alloy > 2.5f) {
if (economyAtSecond.CreatingWorkerDelays[i] > 0)
{
if (economyAtSecond.Alloy > 2.5f)
{
economyAtSecond.Alloy -= 2.5f;
economyAtSecond.CreatingWorkerDelays[i]--;
}
}
else {
else
{
economyAtSecond.CreatingWorkerCount -= 1;
economyAtSecond.WorkerCount += 1;
economyAtSecond.CreatingWorkerDelays.Remove(i);
i--;
}
if (workersNeeded > economyAtSecond.CreatingWorkerCount) {
if (workersNeeded > economyAtSecond.CreatingWorkerCount)
{
economyAtSecond.CreatingWorkerCount += 1;
economyAtSecond.CreatingWorkerDelays.Add(50);
}
// Remove Funds from Build Order
var ordersAtTime = buildOrder.GetOrdersAt(interval);
foreach (var order in ordersAtTime) {
var foundEntity = EntityModel.GetDictionary()[order.DataType];
var production = foundEntity.Production();
if (buildOrder.StartedOrders.TryGetValue(interval, out var ordersAtTime))
{
if (production != null) {
economyAtSecond.Alloy -= production.Alloy;
economyAtSecond.Ether -= production.Ether;
economyAtSecond.Pyre -= production.Pyre;
var finishedAt = interval + production.BuildTime;
foreach (var order in ordersAtTime)
{
var foundEntity = EntityModel.GetDictionary()[order.DataType];
var production = foundEntity.Production();
if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1;
if (production != null)
{
economyAtSecond.Alloy -= production.Alloy;
economyAtSecond.Ether -= production.Ether;
economyAtSecond.Pyre -= production.Pyre;
var finishedAt = interval + production.BuildTime;
if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1;
if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1;
if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1;
}
}
}
// Handle new entities
var completedAtInterval = buildOrder.GetCompletedAt(interval);
foreach (var newEntity in completedAtInterval) {
if (buildOrder.CompletedOrders.TryGetValue(interval, out var completedAtInterval))
{
foreach (var newEntity in completedAtInterval)
{
var harvest = newEntity;
if (harvest != null) economyAtSecond.Harvesters.Add(harvest);
@@ -132,6 +152,7 @@ public class EconomyService : IEconomyService {
if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1;
}
}
}
NotifyDataChanged();
}
@@ -141,13 +162,7 @@ public class EconomyService : IEconomyService {
return _economyOverTime[atInterval];
}
private event Action onChange = null!;
private void NotifyDataChanged() {
onChange?.Invoke();
}
public Action OnChange() {
return onChange;
OnChange?.Invoke();
}
}
-58
View File
@@ -1,58 +0,0 @@
using Model.Entity;
using Model.Types;
namespace Services.Immortal;
public class GameLogicService : IGameLogicService
{
private ITimingService timingService;
private IEconomyService economyService;
private IBuildOrderService buildOrderService;
public GameLogicService(ITimingService timingService, IEconomyService economyService, IBuildOrderService buildOrderService)
{
this.timingService = timingService;
this.economyService = economyService;
this.buildOrderService = buildOrderService;
}
public bool Add(EntityModel entity, int atInterval)
{
throw new NotImplementedException();
}
public int MeetsRequirements(EntityModel entity, int interval)
{
var buildOrders = buildOrderService.GetCompletedBefore(interval);
return -1;
}
public int MeetsAlloy(EntityModel entity, int interval)
{
throw new NotImplementedException();
}
public int MeetsEther(EntityModel entity, int interval)
{
throw new NotImplementedException();
}
public int MeetsPyre(EntityModel entity, int interval)
{
throw new NotImplementedException();
}
public int MeetsSupply(EntityModel entity, int interval)
{
throw new NotImplementedException();
}
public int MeetsTrainingQueue(EntityModel entity, int interval)
{
throw new NotImplementedException();
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
namespace Services.Immortal;
public class TimingService : ITimingService {
private int _timing = 360;
private int _timing = 1500;
public void Subscribe(Action? action) {
_onChange += action;