-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
253 lines (223 loc) · 4.61 KB
/
util.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"context"
"errors"
"fmt"
direct "github.com/buger/goterm"
"math"
"math/rand"
"sync/atomic"
"time"
)
var (
monotonicId int64
noDescription = struct {
Name string
Description string
}{Name: "N/A", Description: "N/A"}
)
type MinMaxCurr struct {
MinMax
Current float64
}
func newThrottle(every time.Duration, done bool) *throttle {
var left time.Duration = 0
if !done {
left = every
}
return &throttle{
left: left,
duration: every,
}
}
type throttle struct {
left, duration time.Duration
}
func (t *throttle) Reach(timeLeft time.Duration) bool {
if t.left < t.duration {
t.left += timeLeft
return false
}
t.left = 0
return true
}
func (t *throttle) Reset() {
t.left = 0
}
func (t *throttle) Copy() *throttle {
copy := *t
return ©
}
func newRandomCoordinate() Point {
w := direct.Width()
h := direct.Height()
if w <= 0 {
w = 100
}
if h <= 0 {
h = 100
}
return Point{
X: float64(rand.Intn(w)),
Y: float64(rand.Intn(h)),
}
}
func genId() int64 {
old := atomic.LoadInt64(&monotonicId)
swapped := atomic.CompareAndSwapInt64(&monotonicId, old, old+1)
for !swapped {
old = atomic.LoadInt64(&monotonicId)
swapped = atomic.CompareAndSwapInt64(&monotonicId, old, old+1)
}
return old + 1
}
func absMax(arguments ...float64) float64 {
var cur float64
var curi int
for i := 0; i < len(arguments); i++ {
if math.Abs(arguments[i]) > cur {
cur = arguments[i]
curi = i
}
}
return arguments[curi]
}
func absMin(arguments ...float64) float64 {
var cur float64
var curi int
for i := 0; i < len(arguments); i++ {
if math.Abs(arguments[i]) < cur {
cur = arguments[i]
curi = i
}
}
return arguments[curi]
}
func getDistance(x1, y1, x2, y2 float64) float64 {
distX, distY := x1-x2, y1-y2
return distX*distX + distY*distY
}
func divRemF(numenator, denumenator float64) float64 {
return numenator - float64(int(math.Round(numenator/denumenator)))*denumenator
}
func triang(x int64) int64 {
return int64(float64(x) / float64(2) * float64(x+1))
}
//rand betwen [1, max] with probability of n
func triangRand(max int64) int64 {
rand := int64(rand.Intn(int(triang(max)))) + 1
for i := int64(1); i <= max; i++ {
tri := triang(i)
if rand <= tri {
return i
}
}
return 0
}
func absInt64(n int64) int64 {
y := n >> 63
return (n ^ y) - y
}
//no special cases check :(
func absInt(n int) int {
if n > 0 {
return n
} else {
return n * -1
}
}
// no special cases
func maxInt64(x, y int64) int64 {
if x > y {
return x
}
return y
}
func maxInt(x, y int) int {
if x > y {
return x
}
return y
}
func minInt(x, y int) int {
if x < y {
return x
}
return y
}
func isInf(f int64, sign int) bool {
// Test for infinity by comparing against maximum float.
// To avoid the floating-point hardware, could use:
// x := Float64bits(f);
// return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
return sign >= 0 && f > math.MaxInt64 || sign <= 0 && f < -math.MaxInt64
}
func every(duration time.Duration, ctx context.Context) <-chan time.Time {
output := make(chan time.Time)
go func(timer chan time.Time, ctx context.Context) {
innerTimer := time.NewTimer(duration)
for {
select {
case timeLeft := <-innerTimer.C:
timer <- timeLeft
innerTimer.Reset(duration)
case <-ctx.Done():
return
}
}
}(output, ctx)
return output
}
func everyFunc(duration time.Duration, callback func(), ctx context.Context) {
output := make(chan time.Time)
go func(timer chan time.Time, ctx context.Context) {
innerTimer := time.NewTimer(duration)
for {
select {
case <-innerTimer.C:
go callback()
innerTimer.Reset(duration)
case <-ctx.Done():
return
}
}
}(output, ctx)
}
func GetTags(object ObjectInterface) (*Tags, error) {
switch object.(type) {
case *Unit:
return object.(*Unit).Tags, nil
case *Wall:
return object.(*Wall).Tags, nil
case *Projectile:
return object.(*Projectile).Tags, nil
case *Explosion:
return object.(*Explosion).Tags, nil
case *Collectable:
return object.(*Collectable).Tags, nil
case *Object:
return object.(*Object).Tags, nil
case *MotionObject:
return object.(*MotionObject).Tags, nil
case *SpawnPoint:
return object.(*SpawnPoint).Tags, nil
default:
return nil, errors.New(fmt.Sprintf("GetTags unknown object type %t", object))
}
}
func getProjectilePlDescription(blueprint string) struct {
Name string
Description string
} {
info, err := Info(blueprint)
if err != nil {
return noDescription
}
if info.Type != "projectile" {
return noDescription
}
return struct {
Name,
Description string
}{Name: info.Name, Description: info.Description}
}