-
Notifications
You must be signed in to change notification settings - Fork 3
/
techlevel.go
188 lines (171 loc) · 4.91 KB
/
techlevel.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cs
import "math"
// Represents a TechLevel the player has or a tech requires, or the amount of research spent on each tech level
type TechLevel struct {
Energy int `json:"energy,omitempty"`
Weapons int `json:"weapons,omitempty"`
Propulsion int `json:"propulsion,omitempty"`
Construction int `json:"construction,omitempty"`
Electronics int `json:"electronics,omitempty"`
Biotechnology int `json:"biotechnology,omitempty"`
}
// return true if this techlevel has the required techlevels for a requirements
func (tl TechLevel) HasRequiredLevels(required TechLevel) bool {
return tl.Energy >= required.Energy &&
tl.Weapons >= required.Weapons &&
tl.Propulsion >= required.Propulsion &&
tl.Construction >= required.Construction &&
tl.Electronics >= required.Electronics &&
tl.Biotechnology >= required.Biotechnology
}
// return the minimum tech level
func (tl TechLevel) Min() int {
return MinInt(
tl.Energy,
tl.Weapons,
tl.Propulsion,
tl.Construction,
tl.Electronics,
tl.Biotechnology)
}
// return the sum of all tech levels
func (tl TechLevel) Sum() int {
return tl.Energy +
tl.Weapons +
tl.Propulsion +
tl.Construction +
tl.Electronics +
tl.Biotechnology
}
func (tl TechLevel) Lowest() TechField {
lowestField := Energy
lowest := math.MaxInt
for _, field := range TechFields {
level := tl.Get(field)
if level < lowest {
lowestField = field
lowest = level
}
}
return lowestField
}
func (tl TechLevel) Get(field TechField) int {
switch field {
case Energy:
return tl.Energy
case Weapons:
return tl.Weapons
case Propulsion:
return tl.Propulsion
case Construction:
return tl.Construction
case Electronics:
return tl.Electronics
case Biotechnology:
return tl.Biotechnology
}
return None
}
func (tl *TechLevel) Set(field TechField, level int) {
switch field {
case Energy:
tl.Energy = level
case Weapons:
tl.Weapons = level
case Propulsion:
tl.Propulsion = level
case Construction:
tl.Construction = level
case Electronics:
tl.Electronics = level
case Biotechnology:
tl.Biotechnology = level
}
}
// add a tech level to this one
func (tl TechLevel) Add(other TechLevel) TechLevel {
return TechLevel{
tl.Energy + other.Energy,
tl.Weapons + other.Weapons,
tl.Propulsion + other.Propulsion,
tl.Construction + other.Construction,
tl.Electronics + other.Electronics,
tl.Biotechnology + other.Biotechnology,
}
}
func (tl TechLevel) Minus(tl2 TechLevel) TechLevel {
return TechLevel{
tl.Energy - tl2.Energy,
tl.Weapons - tl2.Weapons,
tl.Propulsion - tl2.Propulsion,
tl.Construction - tl2.Construction,
tl.Electronics - tl2.Electronics,
tl.Biotechnology - tl2.Biotechnology,
}
}
func (tl TechLevel) Max(tl2 TechLevel) TechLevel {
return TechLevel{
Energy: MaxInt(tl.Energy, tl2.Energy),
Weapons: MaxInt(tl.Weapons, tl2.Weapons),
Propulsion: MaxInt(tl.Propulsion, tl2.Propulsion),
Construction: MaxInt(tl.Construction, tl2.Construction),
Electronics: MaxInt(tl.Electronics, tl2.Electronics),
Biotechnology: MaxInt(tl.Biotechnology, tl2.Biotechnology),
}
}
// return this TechLevel with a minimum of zero for each value
func (tl TechLevel) MinZero() TechLevel {
return TechLevel{
Energy: MaxInt(tl.Energy, 0),
Weapons: MaxInt(tl.Weapons, 0),
Propulsion: MaxInt(tl.Propulsion, 0),
Construction: MaxInt(tl.Construction, 0),
Electronics: MaxInt(tl.Electronics, 0),
Biotechnology: MaxInt(tl.Biotechnology, 0),
}
}
// Get the mininum levels above this tech
// i.e. if we just are a starter humanoid and just gained prop 5
// level ({ 3, 3, 6, 3, 3}) we are 0 levels above the Radiating Hyrdo-Ram Scoop
// returns maxInt if the tech is all 0
func (tl TechLevel) LevelsAbove(other TechLevel) int {
levelsAbove := math.MaxInt
if tl.Energy != 0 {
levelsAbove = MinInt(levelsAbove, other.Energy-tl.Energy)
}
if tl.Weapons != 0 {
levelsAbove = MinInt(levelsAbove, other.Weapons-tl.Weapons)
}
if tl.Propulsion != 0 {
levelsAbove = MinInt(levelsAbove, other.Propulsion-tl.Propulsion)
}
if tl.Construction != 0 {
levelsAbove = MinInt(levelsAbove, other.Construction-tl.Construction)
}
if tl.Electronics != 0 {
levelsAbove = MinInt(levelsAbove, other.Electronics-tl.Electronics)
}
if tl.Biotechnology != 0 {
levelsAbove = MinInt(levelsAbove, other.Biotechnology-tl.Biotechnology)
}
return levelsAbove
}
// LevelsAboveField returns the levels we are above a tech in a given field, or MaxInt if the field requirement is 0
func (tl TechLevel) LevelsAboveField(other TechLevel, field TechField) int {
switch field {
case Energy:
return other.Energy - tl.Energy
case Weapons:
return other.Weapons - tl.Weapons
case Propulsion:
return other.Propulsion - tl.Propulsion
case Construction:
return other.Construction - tl.Construction
case Electronics:
return other.Electronics - tl.Electronics
case Biotechnology:
return other.Biotechnology - tl.Biotechnology
default:
return 0
}
}