-
Notifications
You must be signed in to change notification settings - Fork 9
/
ResourceModule.cs
164 lines (144 loc) · 3.77 KB
/
ResourceModule.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using SFSML;
using SFSML.HookSystem.ReWork;
using SFSML.HookSystem.ReWork.BaseHooks.PartHooks;
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using UnityEngine;
public class ResourceModule : Module
{
[Serializable]
public class Grup
{
public bool isValid;
[BoxGroup]
public float resourceAmount;
[BoxGroup]
public float resourceSpace;
[BoxGroup, Range(0f, 1f)]
public float resourcePercent;
[BoxGroup]
public Resource resourceType;
public ResourceModule[] resourceModules;
public bool canTakeResource
{
get
{
return this.resourceAmount > 0f && this.isValid;
}
}
public bool canAddResource
{
get
{
return this.resourcePercent < 1f && this.isValid;
}
}
public Grup(ResourceModule start)
{
this.isValid = true;
start.part.vessel.partsManager.resourceGrups.Add(this);
this.resourceType = start.resourceType;
List<ResourceModule> list = new List<ResourceModule>();
Ref.connectionCheckId++;
start.part.connectionCheckId = Ref.connectionCheckId;
list.Add(start);
List<Part> list2 = new List<Part>(1)
{
start.part
};
while (list2.Count > 0)
{
for (int i = 0; i < list2[0].joints.Count; i++)
{
if (list2[0].joints[i].fuelFlow)
{
Part part = (!(list2[0].joints[i].fromPart == list2[0])) ? list2[0].joints[i].fromPart : list2[0].joints[i].toPart;
if (part.connectionCheckId != Ref.connectionCheckId)
{
ResourceModule resourceModule = part.GetResourceModule();
if (!(resourceModule == null) && !(start.resourceType != resourceModule.resourceType))
{
part.connectionCheckId = Ref.connectionCheckId;
list2.Add(part);
resourceModule.resourceGrup = this;
list.Add(resourceModule);
}
}
}
}
list2.RemoveAt(0);
}
this.resourceModules = list.ToArray();
this.resourceSpace = 0f;
this.resourceAmount = 0f;
for (int j = 0; j < this.resourceModules.Length; j++)
{
this.resourceModules[j].resourceGrup = this;
this.resourceSpace += this.resourceModules[j].resourceSpace;
this.resourceAmount += this.resourceModules[j].resourceAmount.floatValue;
}
this.SetTanks();
}
public void TakeResource(float removeAmount)
{
float newAmount = this.resourceAmount - removeAmount;
MyDrainResourceHook hook = new MyDrainResourceHook(removeAmount, newAmount, this);
hook = MyHookSystem.executeHook<MyDrainResourceHook>(hook);
this.resourceAmount = Mathf.Max(this.resourceAmount - hook.amountToTake, 0f);
this.SetTanks();
}
public void AddResource(float addAmount)
{
this.resourceAmount = Mathf.Min(this.resourceAmount + addAmount, this.resourceSpace);
this.SetTanks();
}
private void SetTanks()
{
this.resourcePercent = this.resourceAmount / this.resourceSpace;
for (int i = 0; i < this.resourceModules.Length; i++)
{
this.resourceModules[i].resourceAmount.floatValue = this.resourceModules[i].resourceSpace * this.resourcePercent;
}
}
public void DestroyGrup()
{
this.isValid = false;
}
}
[BoxGroup]
public FloatValueHolder resourceAmount;
[BoxGroup]
public float resourceSpace;
[BoxGroup]
public Resource resourceType;
[BoxGroup("B", false, false, 0)]
public ResourceModule.Grup resourceGrup;
public bool showParametersDescription = true;
public override List<object> SaveVariables
{
get
{
return new List<object>
{
this.resourceAmount
};
}
}
public override List<string> DescriptionVariables
{
get
{
return (!this.showParametersDescription) ? new List<string>() : new List<string>
{
string.Concat(new object[]
{
this.resourceType.resourceName,
": ",
this.resourceSpace,
this.resourceType.resourceUnit
})
};
}
}
}