|
|
|
@ -54,121 +54,162 @@ public class EconomyService : IEconomyService |
|
|
|
buildEconomyOverTime[interval] = new EconomyModel(); |
|
|
|
buildEconomyOverTime[interval] = new EconomyModel(); |
|
|
|
|
|
|
|
|
|
|
|
var economyAtSecond = buildEconomyOverTime[interval]; |
|
|
|
var economyAtSecond = buildEconomyOverTime[interval]; |
|
|
|
if (interval > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
CarryOverEconomyFromPreviousInterval(interval, economyAtSecond); |
|
|
|
economyAtSecond.Alloy = buildEconomyOverTime[interval - 1].Alloy; |
|
|
|
|
|
|
|
economyAtSecond.Ether = buildEconomyOverTime[interval - 1].Ether; |
|
|
|
SetupCurrentInverval(buildOrder, economyAtSecond, interval); |
|
|
|
economyAtSecond.Pyre = buildEconomyOverTime[interval - 1].Pyre; |
|
|
|
AddPassivePyreGain(interval, economyAtSecond); |
|
|
|
economyAtSecond.WorkerCount = buildEconomyOverTime[interval - 1].WorkerCount; |
|
|
|
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount; |
|
|
|
economyAtSecond.BusyWorkerCount = buildEconomyOverTime[interval - 1].BusyWorkerCount; |
|
|
|
var workersNeeded = AddFundsFromHarvestPoints(economyAtSecond, freeWorkers); |
|
|
|
economyAtSecond.CreatingWorkerCount = buildEconomyOverTime[interval - 1].CreatingWorkerCount; |
|
|
|
workersNeeded -= CalculateCreatingWorkerCosts(economyAtSecond); |
|
|
|
economyAtSecond.Harvesters = buildEconomyOverTime[interval - 1].Harvesters.ToList(); |
|
|
|
MakeNeededNewWorkersRequests(workersNeeded, economyAtSecond); |
|
|
|
economyAtSecond.CreatingWorkerDelays = buildEconomyOverTime[interval - 1].CreatingWorkerDelays.ToList(); |
|
|
|
SubtractFundsOnRequestedOrders(buildOrder, interval, economyAtSecond); |
|
|
|
} |
|
|
|
HandledAddingNewHarvestPointsAndWorkersToEconomy(buildOrder, interval, economyAtSecond); |
|
|
|
|
|
|
|
} |
|
|
|
economyAtSecond.Interval = interval; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add funds |
|
|
|
NotifyDataChanged(); |
|
|
|
float freeWorkers = economyAtSecond.WorkerCount - economyAtSecond.BusyWorkerCount; |
|
|
|
} |
|
|
|
var workersNeeded = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
economyAtSecond.Harvesters = |
|
|
|
private static void SetupCurrentInverval(IBuildOrderService buildOrder, EconomyModel economyAtSecond, int interval) |
|
|
|
(from harvester in buildOrder.GetHarvestersCompletedBefore(interval) |
|
|
|
{ |
|
|
|
select harvester).ToList(); |
|
|
|
economyAtSecond.Interval = interval; |
|
|
|
|
|
|
|
economyAtSecond.HarvestPoints = |
|
|
|
|
|
|
|
(from harvester in buildOrder.GetHarvestPointsCompletedBefore(interval) |
|
|
|
|
|
|
|
select harvester).ToList(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Add funds |
|
|
|
private static void HandledAddingNewHarvestPointsAndWorkersToEconomy(IBuildOrderService buildOrder, int interval, |
|
|
|
if (interval % 3 == 0) |
|
|
|
EconomyModel economyAtSecond) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (buildOrder.CompletedOrders.TryGetValue(interval, out var completedAtInterval)) |
|
|
|
|
|
|
|
foreach (var newEntity in completedAtInterval) |
|
|
|
{ |
|
|
|
{ |
|
|
|
economyAtSecond.Pyre += 1; |
|
|
|
var harvest = newEntity; |
|
|
|
|
|
|
|
if (harvest != null) economyAtSecond.HarvestPoints.Add(harvest); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var production = newEntity.Production(); |
|
|
|
|
|
|
|
if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1; |
|
|
|
} |
|
|
|
} |
|
|
|
// Add funds |
|
|
|
} |
|
|
|
foreach (var entity in economyAtSecond.Harvesters) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var harvester = entity.Harvest(); |
|
|
|
|
|
|
|
if (harvester.RequiresWorker) |
|
|
|
|
|
|
|
if (harvester.Resource == ResourceType.Alloy) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var usedWorkers = Math.Min(harvester.Slots, freeWorkers); |
|
|
|
|
|
|
|
economyAtSecond.Alloy += harvester.HarvestedPerInterval * usedWorkers; |
|
|
|
|
|
|
|
freeWorkers -= usedWorkers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (usedWorkers < harvester.Slots) workersNeeded += 1; |
|
|
|
private static void SubtractFundsOnRequestedOrders(IBuildOrderService buildOrder, int interval, |
|
|
|
} |
|
|
|
EconomyModel economyAtSecond) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (buildOrder.StartedOrders.TryGetValue(interval, out var ordersAtTime)) |
|
|
|
|
|
|
|
foreach (var order in ordersAtTime) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var foundEntity = EntityModel.GetDictionary()[order.DataType]; |
|
|
|
|
|
|
|
var production = foundEntity.Production(); |
|
|
|
|
|
|
|
|
|
|
|
if (harvester.RequiresWorker == false) |
|
|
|
if (production != null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (harvester.Resource == ResourceType.Ether) |
|
|
|
economyAtSecond.Alloy -= production.Alloy; |
|
|
|
economyAtSecond.Ether += harvester.HarvestedPerInterval * harvester.Slots; |
|
|
|
economyAtSecond.Ether -= production.Ether; |
|
|
|
|
|
|
|
economyAtSecond.Pyre -= production.Pyre; |
|
|
|
|
|
|
|
var finishedAt = interval + production.BuildTime; |
|
|
|
|
|
|
|
|
|
|
|
if (harvester.Resource == ResourceType.Alloy) |
|
|
|
if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1; |
|
|
|
economyAtSecond.Alloy += harvester.HarvestedPerInterval * harvester.Slots; |
|
|
|
|
|
|
|
|
|
|
|
if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Create new worker |
|
|
|
private static void MakeNeededNewWorkersRequests(int workersNeeded, EconomyModel economyAtSecond) |
|
|
|
if (economyAtSecond.CreatingWorkerCount > 0) |
|
|
|
{ |
|
|
|
for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++) |
|
|
|
if (workersNeeded > economyAtSecond.CreatingWorkerCount) |
|
|
|
if (economyAtSecond.CreatingWorkerDelays[i] > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
economyAtSecond.CreatingWorkerCount += 1; |
|
|
|
if (economyAtSecond.Alloy > 2.5f) |
|
|
|
economyAtSecond.CreatingWorkerDelays.Add(20); |
|
|
|
{ |
|
|
|
} |
|
|
|
economyAtSecond.Alloy -= 2.5f; |
|
|
|
} |
|
|
|
economyAtSecond.CreatingWorkerDelays[i]--; |
|
|
|
|
|
|
|
} |
|
|
|
/** |
|
|
|
} |
|
|
|
* Returns amount of workers created |
|
|
|
else |
|
|
|
*/ |
|
|
|
|
|
|
|
private static int CalculateCreatingWorkerCosts(EconomyModel economyAtSecond) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int createdWorkers = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (economyAtSecond.CreatingWorkerCount > 0) |
|
|
|
|
|
|
|
for (var i = 0; i < economyAtSecond.CreatingWorkerDelays.Count; i++) |
|
|
|
|
|
|
|
if (economyAtSecond.CreatingWorkerDelays[i] > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (economyAtSecond.Alloy > 2.5f) |
|
|
|
{ |
|
|
|
{ |
|
|
|
economyAtSecond.CreatingWorkerCount -= 1; |
|
|
|
economyAtSecond.Alloy -= 2.5f; |
|
|
|
economyAtSecond.WorkerCount += 1; |
|
|
|
economyAtSecond.CreatingWorkerDelays[i]--; |
|
|
|
economyAtSecond.CreatingWorkerDelays.Remove(i); |
|
|
|
|
|
|
|
i--; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
economyAtSecond.CreatingWorkerCount -= 1; |
|
|
|
|
|
|
|
economyAtSecond.WorkerCount += 1; |
|
|
|
|
|
|
|
createdWorkers++; |
|
|
|
|
|
|
|
economyAtSecond.CreatingWorkerDelays.Remove(i); |
|
|
|
|
|
|
|
i--; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (workersNeeded > economyAtSecond.CreatingWorkerCount) |
|
|
|
return createdWorkers; |
|
|
|
{ |
|
|
|
} |
|
|
|
economyAtSecond.CreatingWorkerCount += 1; |
|
|
|
|
|
|
|
economyAtSecond.CreatingWorkerDelays.Add(50); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove Funds from Build Order |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buildOrder.StartedOrders.TryGetValue(interval, out var ordersAtTime)) |
|
|
|
/** |
|
|
|
foreach (var order in ordersAtTime) |
|
|
|
* Returns needed workers to maximize harvest points |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static int AddFundsFromHarvestPoints(EconomyModel economyAtSecond, float freeWorkers) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
int workersNeeded = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var entity in economyAtSecond.HarvestPoints) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var harvester = entity.Harvest(); |
|
|
|
|
|
|
|
if (harvester.RequiresWorker) |
|
|
|
|
|
|
|
if (harvester.Resource == ResourceType.Alloy) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var foundEntity = EntityModel.GetDictionary()[order.DataType]; |
|
|
|
var usedWorkers = Math.Min(harvester.Slots, freeWorkers); |
|
|
|
var production = foundEntity.Production(); |
|
|
|
economyAtSecond.Alloy += harvester.HarvestedPerInterval * usedWorkers; |
|
|
|
|
|
|
|
freeWorkers -= usedWorkers; |
|
|
|
|
|
|
|
|
|
|
|
if (production != null) |
|
|
|
if (usedWorkers < harvester.Slots) workersNeeded += 1; |
|
|
|
{ |
|
|
|
} |
|
|
|
economyAtSecond.Alloy -= production.Alloy; |
|
|
|
|
|
|
|
economyAtSecond.Ether -= production.Ether; |
|
|
|
|
|
|
|
economyAtSecond.Pyre -= production.Pyre; |
|
|
|
|
|
|
|
var finishedAt = interval + production.BuildTime; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (production.RequiresWorker) economyAtSecond.BusyWorkerCount += 1; |
|
|
|
if (harvester.RequiresWorker == false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (harvester.Resource == ResourceType.Ether) |
|
|
|
|
|
|
|
economyAtSecond.Ether += harvester.HarvestedPerInterval * harvester.Slots; |
|
|
|
|
|
|
|
|
|
|
|
if (production.ConsumesWorker) economyAtSecond.WorkerCount -= 1; |
|
|
|
if (harvester.Resource == ResourceType.Alloy) |
|
|
|
} |
|
|
|
economyAtSecond.Alloy += harvester.HarvestedPerInterval * harvester.Slots; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Handle new entities |
|
|
|
return workersNeeded; |
|
|
|
if (buildOrder.CompletedOrders.TryGetValue(interval, out var completedAtInterval)) |
|
|
|
} |
|
|
|
foreach (var newEntity in completedAtInterval) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var harvest = newEntity; |
|
|
|
|
|
|
|
if (harvest != null) economyAtSecond.Harvesters.Add(harvest); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var production = newEntity.Production(); |
|
|
|
private static void AddPassivePyreGain(int interval, EconomyModel economyAtSecond) |
|
|
|
if (production != null && production.RequiresWorker) economyAtSecond.BusyWorkerCount -= 1; |
|
|
|
{ |
|
|
|
} |
|
|
|
if (interval % 3 == 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
economyAtSecond.Pyre += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NotifyDataChanged(); |
|
|
|
private void CarryOverEconomyFromPreviousInterval(int interval, EconomyModel economyAtSecond) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (interval > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
economyAtSecond.Alloy = buildEconomyOverTime[interval - 1].Alloy; |
|
|
|
|
|
|
|
economyAtSecond.Ether = buildEconomyOverTime[interval - 1].Ether; |
|
|
|
|
|
|
|
economyAtSecond.Pyre = buildEconomyOverTime[interval - 1].Pyre; |
|
|
|
|
|
|
|
economyAtSecond.WorkerCount = buildEconomyOverTime[interval - 1].WorkerCount; |
|
|
|
|
|
|
|
economyAtSecond.BusyWorkerCount = buildEconomyOverTime[interval - 1].BusyWorkerCount; |
|
|
|
|
|
|
|
economyAtSecond.CreatingWorkerCount = buildEconomyOverTime[interval - 1].CreatingWorkerCount; |
|
|
|
|
|
|
|
economyAtSecond.HarvestPoints = buildEconomyOverTime[interval - 1].HarvestPoints.ToList(); |
|
|
|
|
|
|
|
economyAtSecond.CreatingWorkerDelays = buildEconomyOverTime[interval - 1].CreatingWorkerDelays.ToList(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EconomyModel GetEconomy(int atInterval) |
|
|
|
public EconomyModel GetEconomy(int atInterval) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (atInterval >= buildEconomyOverTime.Count) return buildEconomyOverTime.Last(); |
|
|
|
if (atInterval >= buildEconomyOverTime.Count) return buildEconomyOverTime.Last(); |
|
|
|
|