-
Notifications
You must be signed in to change notification settings - Fork 6
/
ResourceModuleHandler.cs
64 lines (48 loc) · 1.69 KB
/
ResourceModuleHandler.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
using UnityEngine;
using System.Collections.Generic;
namespace BackgroundProcessing {
using DebugLevel = AddonConfig.DebugLevel;
abstract class ResourceModuleHandler {
public abstract HashSet<ProtoPartResourceSnapshot> HandleResource(Vessel v, VesselData data, HashSet<ProtoPartResourceSnapshot> modified);
public static HashSet<ProtoPartResourceSnapshot> AddResource(VesselData data, float amount, string name, HashSet<ProtoPartResourceSnapshot> modified) {
BackgroundProcessing.Debug("AddResource called, adding " + amount + " " + name, DebugLevel.ALL);
if (!data.storage.ContainsKey(name)) { return modified; }
bool reduce = amount < 0;
List<ProtoPartResourceSnapshot> relevantStorage = data.storage[name];
for (int i = 0; i < relevantStorage.Count; ++i) {
ProtoPartResourceSnapshot r = relevantStorage[i];
if (amount == 0) { break; }
float n; float m;
n = (float)r.amount;
m = (float)r.maxAmount;
if (
!(float.IsInfinity(n) || float.IsInfinity(m))
) {
n += amount; amount = 0;
if (reduce) { if (n < 0 && i < relevantStorage.Count - 1) { amount = n; n = 0; } }
else { if (n > m && i < relevantStorage.Count - 1) { amount = n - m; n = m; } }
r.amount = n;
}
modified.Add(r);
}
return modified;
}
public static float ClampResource(HashSet<ProtoPartResourceSnapshot> modified) {
float ret = 0;
foreach (ProtoPartResourceSnapshot r in modified) {
float n; float m; float i;
n = (float)r.amount;
m = (float)r.maxAmount;
if (
!(float.IsInfinity(n) || float.IsInfinity(m))
) {
i = Mathf.Clamp(n, 0, m);
ret += i - n;
n = i;
r.amount = n;
}
}
return ret;
}
};
}