/
shared.go
408 lines (359 loc) · 9.45 KB
/
shared.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"bytes"
"encoding/gob"
"github.com/Rnoadm/wdvn/res"
"image/color"
"io"
"math"
"math/rand"
"sort"
"strings"
)
const (
VelocityClones = 4
TileSize = 16
PixelSize = 64
Gravity = PixelSize * 9 // per tick
MinimumVelocity = PixelSize * 20 // unit stops moving if on ground
TerminalVelocity = 100 * TileSize * PixelSize // unit cannot move faster on x or y than this
Friction = 100 // 1/Friction of the velocity is removed per tick
TicksPerSecond = 100
WhipTimeMin = 0.2 * TicksPerSecond
WhipTimeMax = 1.5 * TicksPerSecond
WhipDamageMin = 10
WhipDamageMax = 5000
WhipSpeedMin = 200 * PixelSize
WhipSpeedMax = 1500 * PixelSize
WhipDistance = 10 * TileSize * PixelSize
ManLives = 10
ManHealth = 10000
DamageFactor = TileSize * PixelSize * 100 // momentum/DamageFactor is damage dealt
RespawnTime = 2 * TicksPerSecond
FloaterFadeStart = 0.5 * TicksPerSecond
FloaterFadeEnd = 1.5 * TicksPerSecond
LemonSpeed = 1000 * PixelSize
LemonTime = 0.3 * TicksPerSecond
VacuumHurt = TicksPerSecond / 5
VacuumSpeed = 100 * PixelSize
VacuumDistance = 1000 * PixelSize
VacuumSuck = 20
)
type Side uint8
const (
SideTop Side = iota
SideBottom
SideLeft
SideRight
)
var (
Type_Ping = res.Type_Ping.Enum()
Type_SelectMan = res.Type_SelectMan.Enum()
Type_Input = res.Type_Input.Enum()
Type_StateDiff = res.Type_StateDiff.Enum()
Type_FullState = res.Type_FullState.Enum()
Type_World = res.Type_World.Enum()
Man_Whip = res.Man_Whip.Enum()
Man_Density = res.Man_Density.Enum()
Man_Vacuum = res.Man_Vacuum.Enum()
Man_Normal = res.Man_Normal.Enum()
Button_released = res.Button_released.Enum()
Button_pressed = res.Button_pressed.Enum()
ReleaseAll = &res.Packet{
Tick: new(uint64),
Mouse1: Button_released,
Mouse2: Button_released,
KeyUp: Button_released,
KeyDown: Button_released,
KeyLeft: Button_released,
KeyRight: Button_released,
}
)
type State struct {
Tick uint64
Mans [res.Man_count]Unit
Floaters []Floater
SpawnPoint Coord
Units map[uint64]*Unit
NextUnit uint64
world *World
}
func NewState(world *World) *State {
var state State
state.world = world
state.Mans[res.Man_Whip].UnitData = &WhipMan{
ManUnitData: ManUnitData{
Man_: res.Man_Whip,
Lives_: ManLives,
Checkpoint_: state.SpawnPoint,
},
}
state.Mans[res.Man_Density].UnitData = &DensityMan{
ManUnitData: ManUnitData{
Man_: res.Man_Density,
Lives_: ManLives,
Checkpoint_: state.SpawnPoint,
},
}
state.Mans[res.Man_Vacuum].UnitData = &VacuumMan{
ManUnitData: ManUnitData{
Man_: res.Man_Vacuum,
Lives_: ManLives,
Checkpoint_: state.SpawnPoint,
},
}
state.Mans[res.Man_Normal].UnitData = &NormalMan{
ManUnitData: ManUnitData{
Man_: res.Man_Normal,
Lives_: ManLives,
Checkpoint_: state.SpawnPoint,
},
}
for i := range state.Mans {
state.Mans[i].Health = state.Mans[i].MaxHealth(&state, &state.Mans[i])
state.FindSpawnPosition(&state.Mans[i])
}
state.Units = make(map[uint64]*Unit)
return &state
}
type Floater struct {
S string
Fg, Bg color.RGBA
X, Y int64
T uint64
}
func (state *State) FindSpawnPosition(u *Unit) {
u.Position = func(hull Coord) Coord {
for i := 0; i < 100; i++ {
pos := state.SpawnPoint
pos.X += rand.Int63n(hull.X*10+1) - hull.X*5
pos.Y += rand.Int63n(hull.Y*10+1) - hull.Y*5
tr := state.Trace(state.SpawnPoint, pos, hull, false)
if len(tr.Units) == 0 {
if tr.End != state.SpawnPoint && tr.HitWorld {
return tr.End
}
pos = tr.End
tr = state.Trace(pos, pos.Add(Coord{0, hull.Y * 10}), hull, false)
tr.Collide(u)
if tr.HitWorld && tr.End != pos {
return tr.End
}
} else if tr.HitWorld {
tr = state.Trace(tr.End, tr.End.Sub(Coord{0, hull.Y * 10}), hull, true)
pos = tr.End
tr = state.Trace(pos, pos.Add(Coord{0, hull.Y * 10}), hull, false)
tr.Collide(u)
if tr.End != pos {
return tr.End
}
}
}
return state.SpawnPoint
}(u.Size(state, u))
}
func (state *State) EachUnit(f func(*Unit)) {
var ignore *Unit
if m, ok := state.Mans[res.Man_Vacuum].UnitData.(*VacuumMan); ok {
ignore = m.Held(state)
}
for i := range state.Mans {
if &state.Mans[i] != ignore {
f(&state.Mans[i])
}
}
for _, u := range state.Units {
if u != ignore {
f(u)
}
}
}
func (state *State) Update(input *[res.Man_count]res.Packet) {
state.Tick++
for i := range state.Mans {
state.Mans[i].UnitData.(Man).Input(&(*input)[i])
}
state.EachUnit(func(u *Unit) {
u.Update(state)
})
for i, l := 0, len(state.Floaters); i < l; i++ {
if state.Floaters[i].T < state.Tick-FloaterFadeEnd {
state.Floaters = append(state.Floaters[:i], state.Floaters[i+1:]...)
i--
l--
}
}
}
type TraceUnit struct {
*Unit
Dist int64 // distance squared
X, Y int64
Side
}
type Trace struct {
End Coord
Units []TraceUnit
HitWorld bool
Special SpecialTile
Side
}
func (t *Trace) Collide(ignore ...*Unit) *Unit {
return t.CollideFunc(func(u *Unit) bool {
for _, i := range ignore {
if u == i {
return false
}
}
return true
})
}
func (t *Trace) CollideWith(state *State, u *Unit) *Unit {
return t.CollideFunc(func(o *Unit) bool {
return u.CollideWith(state, u, o)
})
}
func (t *Trace) CollideFunc(f func(*Unit) bool) *Unit {
for i := range t.Units {
if f(t.Units[i].Unit) {
t.End = Coord{t.Units[i].X, t.Units[i].Y}
t.Side = t.Units[i].Side
return t.Units[i].Unit
}
}
return nil
}
func (t *Trace) Len() int { return len(t.Units) }
func (t *Trace) Swap(i, j int) { t.Units[i], t.Units[j] = t.Units[j], t.Units[i] }
func (t *Trace) Less(i, j int) bool { return t.Units[i].Dist < t.Units[j].Dist }
func (state *State) Trace(start, end, hull Coord, worldOnly bool) *Trace {
min, max := hull.Hull()
min = min.Add(start)
max = max.Add(start)
delta := end.Sub(start)
maxDist := int64(1<<63 - 1)
traceAABB := func(mins, maxs Coord) (dist, x, y int64, side Side) {
if delta.X >= 0 && (min.X >= maxs.X || max.X+delta.X <= mins.X) {
return -1, 0, 0, 0
}
if delta.X <= 0 && (min.X+delta.X >= maxs.X || max.X <= mins.X) {
return -1, 0, 0, 0
}
if delta.Y >= 0 && (min.Y >= maxs.Y || max.Y+delta.Y <= mins.Y) {
return -1, 0, 0, 0
}
if delta.Y <= 0 && (min.Y+delta.Y >= maxs.Y || max.Y <= mins.Y) {
return -1, 0, 0, 0
}
var xEnter, xExit float64
if delta.X > 0 {
xEnter = float64(mins.X-max.X) / float64(delta.X)
xExit = float64(maxs.X-min.X) / float64(delta.X)
} else if delta.X < 0 {
xEnter = float64(maxs.X-min.X) / float64(delta.X)
xExit = float64(mins.X-max.X) / float64(delta.X)
} else {
xEnter = math.Inf(-1)
xExit = math.Inf(1)
}
var yEnter, yExit float64
if delta.Y > 0 {
yEnter = float64(mins.Y-max.Y) / float64(delta.Y)
yExit = float64(maxs.Y-min.Y) / float64(delta.Y)
} else if delta.Y < 0 {
yEnter = float64(maxs.Y-min.Y) / float64(delta.Y)
yExit = float64(mins.Y-max.Y) / float64(delta.Y)
} else {
yEnter = math.Inf(-1)
yExit = math.Inf(1)
}
enter := math.Max(xEnter, yEnter)
exit := math.Min(xExit, yExit)
if exit < 0 || enter > 1 || enter > exit {
return -1, 0, 0, 0
}
if xEnter > yEnter {
if delta.X > 0 {
side = SideLeft
} else {
side = SideRight
}
} else {
if delta.Y > 0 {
side = SideTop
} else {
side = SideBottom
}
}
x = int64(enter * float64(delta.X))
y = int64(enter * float64(delta.Y))
dist = x*x + y*y
if enter < 0 {
dist, x, y = 0, 0, 0
}
return
}
traceUnit := func(u *Unit) (dist, x, y int64, side Side) {
if u.Health <= 0 {
return -1, 0, 0, 0
}
mins, maxs := u.Size(state, u).Hull()
mins = mins.Add(u.Position)
maxs = maxs.Add(u.Position)
dist, x, y, side = traceAABB(mins, maxs)
return
}
tr := &Trace{}
tr.End = end
bounds_min, bounds_max := min, max
if delta.X < 0 {
bounds_min.X += delta.X
} else {
bounds_max.X += delta.X
}
if delta.Y < 0 {
bounds_min.Y += delta.Y
} else {
bounds_max.Y += delta.Y
}
bounds_min = bounds_min.Floor(TileSize * PixelSize)
bounds_max = bounds_max.Floor(TileSize * PixelSize).Add(Coord{TileSize * PixelSize, TileSize * PixelSize})
for x := bounds_min.X; x <= bounds_max.X; x += TileSize * PixelSize {
for y := bounds_min.Y; y <= bounds_max.Y; y += TileSize * PixelSize {
if state.world.Solid(x/TileSize/PixelSize, y/TileSize/PixelSize) {
dist, dx, dy, side := traceAABB(Coord{x, y}, Coord{x + TileSize*PixelSize, y + TileSize*PixelSize})
if dist >= 0 && (dist < maxDist || (dist == maxDist && tr.Special == SpecialTile_None)) {
maxDist = dist
tr.HitWorld = true
tr.End = start.Add(Coord{dx, dy})
tr.Special = state.world.Special(x/TileSize/PixelSize, y/TileSize/PixelSize)
tr.Side = side
}
}
}
}
if !worldOnly {
state.EachUnit(func(u *Unit) {
if dist, x, y, side := traceUnit(u); dist >= 0 && dist <= maxDist {
tr.Units = append(tr.Units, TraceUnit{Unit: u, Dist: dist, X: start.X + x, Y: start.Y + y, Side: side})
}
})
sort.Sort(tr)
}
return tr
}
var FooLevel = LoadWorld(strings.NewReader(res.FooLevel))
func LoadWorld(r io.Reader) (w *World) {
err := gob.NewDecoder(r).Decode(&w)
if err != nil {
panic(err)
}
return
}
func Encode(v interface{}) []byte {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(v)
if err != nil {
panic(err)
}
return buf.Bytes()
}