/
input.go
591 lines (463 loc) · 11.1 KB
/
input.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package game
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/ghthor/filu/rpg2d/coord"
"github.com/ghthor/filu/rpg2d/entity"
"github.com/ghthor/filu/sim/stime"
)
type updatePhaseLocker struct {
*ActorIndexLocker
}
type updatePhase struct {
index ActorIndex
}
type inputPhaseLocker struct {
*ActorIndexLocker
nextId func() entity.Id
}
type inputPhase struct {
index ActorIndex
nextId func() entity.Id
}
func (phase updatePhaseLocker) Update(e entity.Entity, now stime.Time) entity.Entity {
defer phase.ActorIndexLocker.RUnlock()
return updatePhase{phase.ActorIndexLocker.RLock()}.Update(e, now)
}
func (phase updatePhase) Update(e entity.Entity, now stime.Time) entity.Entity {
switch e := e.(type) {
case actorEntity:
actor := phase.index[e.ActorId()]
if actor.createdAt == 0 {
actor.flags = actor.flags | entity.FlagNew
actor.createdAt = now
} else {
// Clear FlagNew & set the last know Actor State
actor.flags = actor.flags &^ entity.FlagNew
actor.lastState = actor.ToState()
}
// Remove any movement actions that have completed
if actor.pathAction != nil && actor.pathAction.End() <= now {
actor.lastMoveAction = actor.pathAction
actor.cell = actor.pathAction.Dest
actor.pathAction = nil
}
// Reset speed after a charge
if actor.lastStartedCharge+chargeDuration <= now {
actor.speed = baseSpeed
}
return actor.Entity()
case assailEntity:
// Remove all assail entities
return entity.Removed{e, now}
case sayEntity:
// TODO parametize server fps
if e.saidAt+(sayEntityDuration*40) <= now {
// Remove all say entities
return entity.Removed{e, now}
}
e.flags = e.flags &^ entity.FlagNew
return e
case wallEntity:
e.flags = e.flags &^ entity.FlagNew
return e
case entity.Removed:
// 3 Secs * Sim FPS
if e.RemovedAt+(3*40) <= now {
return nil
}
return e
default:
panic(fmt.Sprint("unexpected entity type:", e))
}
}
func (phase inputPhaseLocker) ApplyInputsTo(e entity.Entity, now stime.Time) []entity.Entity {
defer phase.ActorIndexLocker.RUnlock()
return inputPhase{phase.RLock(), phase.nextId}.ApplyInputsTo(e, now)
}
func (phase inputPhase) ApplyInputsTo(e entity.Entity, now stime.Time) []entity.Entity {
switch e := e.(type) {
case actorEntity:
var entities []entity.Entity
actor := phase.index[e.ActorId()]
entities = append(entities,
phase.processUseCmd(actor, now)...,
)
phase.processMoveCmd(actor, now)
entities = append(entities,
phase.processChatCmd(actor, now)...,
)
return append(entities, actor.Entity())
case sayEntity:
return []entity.Entity{e}
case wallEntity:
return []entity.Entity{e}
case entity.Removed:
return []entity.Entity{e}
default:
panic(fmt.Sprint("unexpected entity type:", e))
}
}
type MoveRequestType int
//go:generate stringer -type=MoveRequestType
const (
MR_ERROR MoveRequestType = iota
MR_MOVE
MR_MOVE_CANCEL
MR_SIZE
)
type MoveRequest struct {
MoveRequestType
stime.Time
coord.Direction
}
type moveCmd struct {
stime.Time
coord.Direction
}
type UseRequestType int
//go:generate stringer -type=UseRequestType
const (
UR_ERROR UseRequestType = iota
UR_USE
UR_USE_CANCEL
UR_SIZE
)
type UseRequest struct {
UseRequestType
stime.Time
Skill string
}
type useCmd struct {
stime.Time
skill string
}
type ChatRequestType int
//go:generate stringer -type=ChatRequestType
const (
CR_ERROR ChatRequestType = iota
CR_SAY
CR_SIZE
)
type ChatRequest struct {
ChatRequestType
stime.Time
Msg string
}
type chatCmd struct {
ChatRequestType
stime.Time
msg string
}
func newMoveRequest(t MoveRequestType, timeIssued stime.Time, params string) (MoveRequest, error) {
d, err := coord.NewDirectionWithString(params)
if err != nil {
return MoveRequest{}, err
}
return MoveRequest{
t,
timeIssued,
d,
}, nil
}
func newUseRequest(t UseRequestType, timeIssued stime.Time, params string) (UseRequest, error) {
switch params {
case "assail":
return UseRequest{t, timeIssued, params}, nil
case "charge":
return UseRequest{t, timeIssued, params}, nil
default:
return UseRequest{}, fmt.Errorf("unknown skill: %s", params)
}
}
func newChatRequest(t ChatRequestType, timeIssued stime.Time, params string) (ChatRequest, error) {
if len(params) > 120 {
return ChatRequest{}, errors.New("chat message exceeded 120 char limit")
}
return ChatRequest{
ChatRequestType: t,
Time: timeIssued,
Msg: params,
}, nil
}
// If the cmd and params are a valid combination
// a request will be made and passed into the
// IO muxer. SubmitCmd() will return an error
// if the submitted cmd and params are invalid.
func (c actorConn) SubmitCmd(cmd, params string) error {
parts := strings.Split(cmd, "=")
if len(parts) != 2 {
return errors.New("invalid command syntax")
}
timeIssued, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return err
}
switch parts[0] {
case "move":
r, err := newMoveRequest(MR_MOVE, stime.Time(timeIssued), params)
if err != nil {
return err
}
c.submitMoveRequest <- r
case "moveCancel":
r, err := newMoveRequest(MR_MOVE_CANCEL, stime.Time(timeIssued), params)
if err != nil {
return err
}
c.submitMoveRequest <- r
case "use":
r, err := newUseRequest(UR_USE, stime.Time(timeIssued), params)
if err != nil {
return err
}
c.submitUseRequest <- r
case "useCancel":
r, err := newUseRequest(UR_USE_CANCEL, stime.Time(timeIssued), params)
if err != nil {
return err
}
c.submitUseRequest <- r
case "say":
r, err := newChatRequest(CR_SAY, stime.Time(timeIssued), params)
if err != nil {
return err
}
c.submitChatRequest <- r
default:
return fmt.Errorf("unknown command: %s", parts[0])
}
return nil
}
func (c actorConn) SubmitMoveRequest(r MoveRequest) {
select {
case c.submitMoveRequest <- r:
default:
}
}
func (c actorConn) SubmitUseRequest(r UseRequest) {
select {
case c.submitUseRequest <- r:
default:
}
}
func (c actorConn) SubmitChatRequest(r ChatRequest) {
select {
case c.submitChatRequest <- r:
default:
}
}
func (c actorConn) ReadMoveCmd() *moveCmd {
return <-c.readMoveCmd
}
func (a *actor) applyPathAction(pa *coord.PathAction) {
prevPathAction := a.pathAction
prevFacing := a.facing
prevFlags := a.flags
a.undoLastMoveAction = func() {
a.pathAction = prevPathAction
a.facing = prevFacing
a.flags = prevFlags
a.undoLastMoveAction = nil
}
a.pathAction = pa
a.facing = pa.Direction()
}
func (a *actor) applyTurnAction(ta coord.TurnAction) {
prevAction := a.lastMoveAction
prevFacing := a.facing
prevFlags := a.flags
a.undoLastMoveAction = func() {
a.lastMoveAction = prevAction
a.facing = prevFacing
a.flags = prevFlags
a.undoLastMoveAction = nil
}
a.lastMoveAction = ta
a.facing = ta.To
}
func (a *actor) revertMoveAction() {
if a.undoLastMoveAction != nil {
a.undoLastMoveAction()
}
}
func (phase inputPhase) processMoveCmd(a *actor, now stime.Time) {
cmd := a.ReadMoveCmd()
if cmd == nil {
// The client has canceled all move requests
return
}
// Actor is already moving so the moveRequest won't be
// consumed until the path action has been completed
if a.pathAction != nil {
return
}
// Actor may be able to move
pathAction := &coord.PathAction{
Span: stime.NewSpan(now, now+stime.Time(a.speed)),
Orig: a.Cell(),
Dest: a.Cell().Neighbor(cmd.Direction),
}
if pathAction.CanHappenAfter(a.lastMoveAction) {
a.applyPathAction(pathAction)
return
}
// Actor must change facing
if a.facing != cmd.Direction {
turnAction := coord.TurnAction{
From: a.facing,
To: cmd.Direction,
Time: now,
}
if turnAction.CanHappenAfter(a.lastMoveAction) {
a.applyTurnAction(turnAction)
}
}
}
// 1s
// In frames
const assailCooldown = 40
type assailEntity struct {
id entity.Id
spawnedBy entity.Id
spawnedAt stime.Time
cell coord.Cell
flags entity.Flag
damage int
}
type AssailEntityState struct {
Type string `json:"type"`
Id entity.Id `json:"id"`
SpawnedBy entity.Id `json:"spawnedBy"`
SpawnedAt stime.Time `json:"spawnedAt"`
Cell coord.Cell `json:"cell"`
}
func (e assailEntity) Id() entity.Id { return e.id }
func (e assailEntity) Cell() coord.Cell { return e.cell }
func (e assailEntity) Bounds() coord.Bounds {
return coord.Bounds{e.cell, e.cell}
}
func (e assailEntity) Flags() entity.Flag { return 0 }
func (e assailEntity) ToState() entity.State {
return AssailEntityState{
Type: "assail",
Id: e.id,
SpawnedBy: e.spawnedBy,
SpawnedAt: e.spawnedAt,
Cell: e.cell,
}
}
func (e AssailEntityState) EntityId() entity.Id { return e.Id }
func (e AssailEntityState) Bounds() coord.Bounds {
return coord.Bounds{e.Cell, e.Cell}
}
func (e AssailEntityState) IsDifferentFrom(entity.State) bool {
return true
}
func (c actorConn) ReadUseCmd() *useCmd {
return <-c.readUseCmd
}
func (phase inputPhase) processUseCmd(a *actor, now stime.Time) []entity.Entity {
cmd := a.ReadUseCmd()
if cmd == nil {
return nil
}
// TODO Only allow when stationary
// TODO Trigger a cooldown
switch cmd.skill {
case "assail":
// Implement a cooldown
if a.lastAssail.spawnedAt+assailCooldown > now {
return nil
}
e := assailEntity{
id: phase.nextId(),
spawnedBy: a.actorEntity.Id(),
spawnedAt: now,
cell: a.Cell().Neighbor(a.facing),
flags: entity.FlagNew,
damage: 25,
}
a.lastAssail = e
return []entity.Entity{e}
case "charge":
// Implement a cooldown
if a.lastStartedCharge+chargeCooldown <= now {
a.speed = chargeSpeed
a.lastStartedCharge = now
}
}
return nil
}
// In seconds
const sayEntityDuration = 3
type sayEntity struct {
id entity.Id
saidBy entity.Id
saidAt stime.Time
cell coord.Cell
flags entity.Flag
msg string
}
type SayEntityState struct {
Type string `json:"type"`
Id entity.Id `json:"id"`
SaidBy entity.Id `json:"saidBy"`
SaidAt stime.Time `json:"saidAt"`
Cell coord.Cell `json:"cell"`
Msg string `json:"msg"`
}
func (e sayEntity) Id() entity.Id { return e.id }
func (e sayEntity) Cell() coord.Cell { return e.cell }
func (e sayEntity) Bounds() coord.Bounds {
return coord.Bounds{e.cell, e.cell}
}
func (e sayEntity) Flags() entity.Flag { return e.flags }
func (e sayEntity) ToState() entity.State {
return SayEntityState{
Type: "say",
Id: e.id,
SaidBy: e.saidBy,
SaidAt: e.saidAt,
Cell: e.cell,
Msg: e.msg,
}
}
func (e SayEntityState) EntityId() entity.Id { return e.Id }
func (e SayEntityState) Bounds() coord.Bounds {
return coord.Bounds{e.Cell, e.Cell}
}
func (e SayEntityState) IsDifferentFrom(other entity.State) bool {
switch other := other.(type) {
case SayEntityState:
return e.Id != other.Id ||
e.Msg != other.Msg ||
e.SaidBy != other.SaidBy ||
e.SaidAt != other.SaidAt ||
e.Cell != other.Cell
}
return true
}
func (a *actor) ReadChatCmd() *chatCmd {
return <-a.readChatCmd
}
func (phase inputPhase) processChatCmd(a *actor, now stime.Time) []entity.Entity {
cmd := a.ReadChatCmd()
if cmd == nil {
return nil
}
switch cmd.ChatRequestType {
case CR_SAY:
return []entity.Entity{sayEntity{
id: phase.nextId(),
saidBy: a.actorEntity.Id(),
saidAt: now,
cell: a.Cell(),
flags: entity.FlagNew | entity.FlagNoCollide,
msg: cmd.msg,
}}
}
return nil
}