forked from xmnovotny/VoxelTycoon-ScheduleStopwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemandHelper.cs
114 lines (104 loc) · 4.71 KB
/
DemandHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using VoxelTycoon;
using VoxelTycoon.Buildings;
using VoxelTycoon.Cities;
using VoxelTycoon.Researches;
using VoxelTycoon.Tracks;
namespace ScheduleStopwatch
{
[HarmonyPatch]
public class DemandHelper
{
public static bool IsInBasicDemand(VehicleStation station, IStorageNetworkNode node)
{
return GetBasicStationDemand(station).Contains(node);
}
public static List<IStorageNetworkNode> GetBasicStationDemand(VehicleStation station)
{
List<IStorageNetworkNode> targetNodes = new List<IStorageNetworkNode>();
List<IStorageNetworkNode> sourceNodes = new List<IStorageNetworkNode>();
station.GetConnectedNodes(targetNodes, sourceNodes);
return targetNodes;
}
public static IEnumerable<IStorageNetworkNode> GetStationDemandNodes(VehicleStation station, bool additionalDemands = true, bool includeDisabled = false)
{
List<IStorageNetworkNode> targetNodes = new List<IStorageNetworkNode>();
List<IStorageNetworkNode> sourceNodes = new List<IStorageNetworkNode>();
station.GetConnectedNodes(targetNodes, sourceNodes);
StorageNetworkManager manager = LazyManager<StorageNetworkManager>.Current;
foreach (IStorageNetworkNode node in targetNodes)
{
if (includeDisabled || manager.GetIsEnabled(station, node) && (node is Store || node is Lab))
{
yield return node;
}
}
if (additionalDemands)
{
foreach (IStorageNetworkNode node in LazyManager<StationDemandManager>.Current.GetAdditionalDemandsEnum(station))
{
yield return node;
}
}
yield break;
}
public static HashSet<IStorageNetworkNode> GetStationDemandNodesHashSet(VehicleStation station, bool additionalDemands = true, bool includeDisabled = false)
{
return new HashSet<IStorageNetworkNode>(GetStationDemandNodes(station, additionalDemands, includeDisabled));
}
public static void GetStationDemandNodesHashSet(HashSet<IStorageNetworkNode> demandNodes, VehicleStation station, bool additionalDemands = true, bool includeDisabled = false)
{
foreach (IStorageNetworkNode node in GetStationDemandNodes(station, additionalDemands, includeDisabled))
{
demandNodes.Add(node);
}
}
public static Dictionary<Item, int> GetStationDemands(VehicleStation station, Dictionary<Item, int> demandsList = null, Dictionary<Item, int> unservicedDemands = null, bool additionalDemands = true)
{
Dictionary<Item, int> result = demandsList ?? new Dictionary<Item, int>();
foreach (IStorageNetworkNode node in GetStationDemandNodes(station, additionalDemands))
{
GetNodeDemands(node, result, unservicedDemands);
}
return result;
}
public static void GetNodesDemands(IEnumerable<IStorageNetworkNode> nodes, Dictionary<Item, int> demands, Dictionary<Item, int> unservicedDemands = null)
{
foreach (IStorageNetworkNode node in nodes)
{
GetNodeDemands(node, demands, unservicedDemands);
}
}
public static void GetNodeDemands(IStorageNetworkNode node, Dictionary<Item, int> demands, Dictionary<Item, int> unservicedDemands = null)
{
if (node.Building is Store store)
{
AddCityDemand(store.Demand, demands);
if (unservicedDemands != null && store.Demand.DeliveredCounter.Lifetime == 0)
{
//unserviced demand
AddCityDemand(store.Demand, unservicedDemands);
}
}
if (node.Building is Lab lab && lab.IsEnabled && lab.Research != null && !LazyManager<ResearchManager>.Current.IsCompleted(lab.Research))
{
AddLab(lab, demands);
}
}
private static void AddCityDemand(CityDemand demand, Dictionary<Item, int> demandsList)
{
RecipeHelper.AddItem(demandsList, demand.Item, demand.Demand);
}
private static void AddLab(Lab lab, Dictionary<Item, int> demandsList)
{
foreach (Research.RequiredItem reqItem in lab.Research.GetItemsPerDay().ToList())
{
RecipeHelper.AddItem(demandsList, reqItem.Item, Mathf.RoundToInt(reqItem.Count * lab.SharedData.Efficiency * 30));
}
}
}
}