forked from MobRulesGames/haunts
/
entity.go
697 lines (603 loc) · 16.7 KB
/
entity.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package game
import (
"encoding/gob"
gl "github.com/MobRulesGames/gogl/gl21"
"github.com/MobRulesGames/glop/sprite"
"github.com/MobRulesGames/glop/util/algorithm"
"github.com/MobRulesGames/haunts/base"
"github.com/MobRulesGames/haunts/game/status"
"github.com/MobRulesGames/haunts/house"
"github.com/MobRulesGames/haunts/sound"
"github.com/MobRulesGames/haunts/texture"
"github.com/MobRulesGames/mathgl"
"image"
"path/filepath"
"regexp"
)
type Ai interface {
// Kills any goroutines associated with this Ai
Terminate()
// Informs the Ai that a new turn has started
Activate()
// Returns true if the Ai still has things to do this turn
Active() bool
ActionExecs() <-chan ActionExec
}
// A dummy ai that always claims to be inactive, this is just a convenience so
// that we don't have to keep checking if an Ai is nil or not.
type inactiveAi struct{}
func (a inactiveAi) Terminate() {}
func (a inactiveAi) Activate() {}
func (a inactiveAi) Active() bool { return false }
func (a inactiveAi) ActionExecs() <-chan ActionExec { return nil }
func init() {
gob.Register(inactiveAi{})
}
type AiKind int
const (
EntityAi AiKind = iota
MinionsAi
DenizensAi
IntrudersAi
)
var ai_maker func(path string, g *Game, ent *Entity, dst *Ai, kind AiKind)
func SetAiMaker(f func(path string, g *Game, ent *Entity, dst *Ai, kind AiKind)) {
ai_maker = f
}
func LoadAllEntities() {
base.RemoveRegistry("entities")
base.RegisterRegistry("entities", make(map[string]*entityDef))
basedir := base.GetDataDir()
base.RegisterAllObjectsInDir("entities", filepath.Join(basedir, "entities"), ".json", "json")
base.RegisterAllObjectsInDir("entities", filepath.Join(basedir, "objects"), ".json", "json")
}
// Tries to place new_ent in the game at its current position. Returns true
// on success, false otherwise.
// pattern is a regexp that matches only the names of all valid spawn points.
func (g *Game) placeEntity(pattern string) bool {
if g.new_ent == nil {
base.Log().Printf("No new ent")
return false
}
re, err := regexp.Compile(pattern)
if err != nil {
base.Error().Printf("Failed to compile regexp: '%s': %v", pattern, err)
return false
}
g.new_ent.Info.RoomsExplored[g.new_ent.CurrentRoom()] = true
ix, iy := int(g.new_ent.X), int(g.new_ent.Y)
idx, idy := g.new_ent.Dims()
r, f, _ := g.House.Floors[0].RoomFurnSpawnAtPos(ix, iy)
if r == nil || f != nil {
return false
}
for _, e := range g.Ents {
x, y := e.Pos()
dx, dy := e.Dims()
r1 := image.Rect(x, y, x+dx, y+dy)
r2 := image.Rect(ix, iy, ix+idx, iy+idy)
if r1.Overlaps(r2) {
return false
}
}
// Check for spawn points
for _, spawn := range g.House.Floors[0].Spawns {
if !re.MatchString(spawn.Name) {
continue
}
x, y := spawn.Pos()
dx, dy := spawn.Dims()
if ix < x || ix+idx > x+dx {
continue
}
if iy < y || iy+idy > y+dy {
continue
}
g.Ents = append(g.Ents, g.new_ent)
g.new_ent = nil
return true
}
return false
}
func (e *Entity) LoadAi() {
filename := e.Ai_path.String()
if e.Ai_file_override != "" {
filename = e.Ai_file_override.String()
}
if filename == "" {
base.Log().Printf("No ai for %s", e.Name)
e.Ai = inactiveAi{}
return
}
ai_maker(filename, e.Game(), e, &e.Ai, EntityAi)
if e.Ai == nil {
e.Ai = inactiveAi{}
base.Log().Printf("Failed to make Ai for '%s' with %s", e.Name, filename)
} else {
base.Log().Printf("Made Ai for '%s' with %s", e.Name, filename)
}
}
// Does some basic setup that is common to both creating a new entity and to
// loading one from a saved game.
func (e *Entity) Load(g *Game) {
e.sprite.Load(e.Sprite_path.String())
e.Sprite().SetTriggerFunc(func(s *sprite.Sprite, name string) {
x, y := e.Pos()
dx, dy := e.Dims()
volume := 1.0
if e.Side() == SideExplorers || e.Side() == SideHaunt {
volume = e.Game().ViewFrac(x, y, dx, dy)
}
if e.current_action != nil {
if sound_name, ok := e.current_action.SoundMap()[name]; ok {
sound.PlaySound(sound_name, volume)
return
}
}
if e.Sounds != nil {
if sound_name, ok := e.Sounds[name]; ok {
sound.PlaySound(sound_name, volume)
}
}
})
if e.Side() == SideHaunt || e.Side() == SideExplorers {
e.los = &losData{}
full_los := make([]bool, house.LosTextureSizeSquared)
e.los.grid = make([][]bool, house.LosTextureSize)
for i := range e.los.grid {
e.los.grid[i] = full_los[i*house.LosTextureSize : (i+1)*house.LosTextureSize]
}
}
g.all_ents_in_memory[e] = true
g.viewer.RemoveDrawable(e)
g.viewer.AddDrawable(e)
e.game = g
e.LoadAi()
}
func (e *Entity) Release() {
e.Ai.Terminate()
}
func MakeEntity(name string, g *Game) *Entity {
ent := Entity{Defname: name}
base.GetObject("entities", &ent)
for _, action_name := range ent.Action_names {
ent.Actions = append(ent.Actions, MakeAction(action_name))
}
if ent.Side() == SideHaunt || ent.Side() == SideExplorers {
stats := status.MakeInst(ent.Base)
stats.OnBegin()
ent.Stats = &stats
}
ent.Info = makeInfo()
ent.Id = g.Entity_id
g.Entity_id++
ent.Load(g)
g.all_ents_in_memory[&ent] = true
return &ent
}
type spriteContainer struct {
sp *sprite.Sprite
// If there is an error when loading the sprite it will be stored here
err error
}
func (sc *spriteContainer) Sprite() *sprite.Sprite {
return sc.sp
}
func (sc *spriteContainer) Load(path string) {
sc.sp, sc.err = sprite.LoadSprite(path)
if sc.err != nil {
base.Error().Printf("Unable to load sprite: %s:%v", path, sc.err)
}
}
// Allows the Ai system to signal to us under certain circumstance
type AiEvalSignal int
const (
AiEvalCont AiEvalSignal = iota
AiEvalTerm
AiEvalPause
)
type entityDef struct {
Name string
Dx, Dy int
Sprite_path base.Path
Walking_speed float64
// Still frame of the sprite - not necessarily one of the individual frames,
// but still usable for identifying it. Should be the same dimensions as
// any of the frames.
Still texture.Object `registry:"autoload"`
// Headshot of this character. Should be square.
Headshot texture.Object `registry:"autoload"`
// List of actions that this entity defaults to having
Action_names []string
// Mapping from trigger name to sound name.
Sounds map[string]string
// Path to the Ai that this entity should use if not player-controlled
Ai_path base.Path
// If true, grants los to the opposing side as well as its own.
Enemy_los bool
Base status.Base
ExplorerEnt *ExplorerEnt
HauntEnt *HauntEnt
ObjectEnt *ObjectEnt
}
func (ei *entityDef) Side() Side {
types := 0
if ei.ExplorerEnt != nil {
types++
}
if ei.HauntEnt != nil {
types++
}
if ei.ObjectEnt != nil {
types++
}
if types > 1 {
base.Error().Printf("Entity '%s' must specify exactly zero or one ent type.", ei.Name)
return SideNone
}
switch {
case ei.ExplorerEnt != nil:
return SideExplorers
case ei.HauntEnt != nil:
switch ei.HauntEnt.Level {
case LevelMinion:
case LevelMaster:
case LevelServitor:
default:
base.Error().Printf("Entity '%s' speciied unknown level '%s'.", ei.Name, ei.HauntEnt.Level)
}
return SideHaunt
case ei.ObjectEnt != nil:
return SideObject
default:
return SideNpc
}
return SideNone
}
func (ei *entityDef) Dims() (int, int) {
if ei.Dx <= 0 || ei.Dy <= 0 {
base.Error().Printf("Entity '%s' didn't have its Dims set properly", ei.Name)
ei.Dx = 1
ei.Dy = 1
}
return ei.Dx, ei.Dy
}
type HauntEnt struct {
// If this entity is a Master, Cost indicates how many points it can spend
// on Servitors, otherwise it indicates how many points a Master must pay to
// include this entity in its army.
Cost int
// If this entity is a Master this indicates how many points worth of
// minions it begins the game with. Not used for non-Masters.
Minions int
Level EntLevel
}
type ExplorerEnt struct {
Gear_names []string
// If the explorer has picked a piece of gear it will be listed here.
Gear *Gear
}
type ObjectEnt struct {
Goal ObjectGoal
}
type ObjectGoal string
const (
GoalRelic ObjectGoal = "Relic"
GoalCleanse ObjectGoal = "Cleanse"
GoalMystery ObjectGoal = "Mystery"
)
type EntLevel string
const (
LevelMinion EntLevel = "Minion"
LevelServitor EntLevel = "Servitor"
LevelMaster EntLevel = "Master"
)
type Side int
const (
SideNone Side = iota
SideExplorers
SideHaunt
SideNpc
SideObject
)
type losData struct {
// All positions that can be seen by this entity are stored here.
grid [][]bool
// Floor coordinates of the last position los was determined from, so that
// we don't need to recalculate it more than we need to as an ent is moving.
x, y int
// Range of vision - all true values in grid are contained within these
// bounds.
minx, miny, maxx, maxy int
}
type EntityId int
type EntityInst struct {
// Used to keep track of entities across a save/load
Id EntityId
X, Y float64
sprite spriteContainer
los *losData
// so we know if we should draw a reticle around it
hovered bool
selected bool
controlled bool
// The width that this entity's sprite was rendered at the last time it was
// drawn. User to determine what entity the cursor is over.
last_render_width float32
// Some methods may require being able to access other entities, so each
// entity has a pointer to the game itself.
game *Game
// Actions that this entity currently has available to it for use. This
// may not be a bijection of Actions mentioned in entityDef.Action_names.
Actions []Action
// If this entity is currently executing an Action it will be stored here
// until the Action is complete.
current_action Action
Stats *status.Inst
// Ai stuff - the channels cannot be gobbed, so they need to be remade when
// loading an ent from a file
Ai Ai
Ai_file_override base.Path
Ai_data map[string]string
// Info that may be of use to the Ai
Info Info
// For inanimate objects - some of them need to be activated so we know when
// the players can interact with them.
Active bool
}
type aiStatus int
const (
aiNone aiStatus = iota
aiReady
aiRunning
aiDone
)
// This stores things that a stateless ai can't figure out
type Info struct {
// Basic or Aoe attacks suffice
LastEntThatAttackedMe EntityId
// Basic or Aoe attacks suffice, but since you can hit multiple enemies
// (including allies) with an aoe it will only be set if you hit an enemy,
// and it will only remember one of them.
LastEntThatIAttacked EntityId
// Set of all rooms that this entity has actually stood in. The values are
// indices into the array of rooms in the floor.
RoomsExplored map[int]bool
}
func makeInfo() Info {
var i Info
i.RoomsExplored = make(map[int]bool)
return i
}
func (e *Entity) Game() *Game {
return e.game
}
func (e *Entity) Sprite() *sprite.Sprite {
return e.sprite.sp
}
func (e *Entity) HasLos(x, y, dx, dy int) bool {
if e.los == nil {
return false
}
for i := x; i < x+dx; i++ {
for j := y; j < y+dy; j++ {
if i < 0 || j < 0 || i >= len(e.los.grid) || j >= len(e.los.grid[0]) {
continue
}
if e.los.grid[i][j] {
return true
}
}
}
return false
}
func (e *Entity) HasTeamLos(x, y, dx, dy int) bool {
return e.game.TeamLos(e.Side(), x, y, dx, dy)
}
func DiscretizePoint32(x, y float32) (int, int) {
return DiscretizePoint64(float64(x), float64(y))
}
func DiscretizePoint64(x, y float64) (int, int) {
x += 0.5
y += 0.5
if x < 0 {
x -= 1
}
if y < 0 {
y -= 1
}
return int(x), int(y)
}
func (ei *EntityInst) Pos() (int, int) {
return DiscretizePoint64(ei.X, ei.Y)
}
func (ei *EntityInst) FPos() (float64, float64) {
return ei.X, ei.Y
}
func (ei *EntityInst) CurrentRoom() int {
x, y := ei.Pos()
room := roomAt(ei.game.House.Floors[0], x, y)
for i := range ei.game.House.Floors[0].Rooms {
if ei.game.House.Floors[0].Rooms[i] == room {
return i
}
}
return -1
}
type Entity struct {
Defname string
*entityDef
EntityInst
}
func (e *Entity) drawReticle(pos mathgl.Vec2, rgba [4]float64) {
if !e.hovered && !e.selected && !e.controlled {
return
}
gl.PushAttrib(gl.CURRENT_BIT)
r := gl.Ubyte(rgba[0] * 255)
g := gl.Ubyte(rgba[1] * 255)
b := gl.Ubyte(rgba[2] * 255)
a := gl.Ubyte(rgba[3] * 255)
switch {
case e.controlled:
gl.Color4ub(0, 0, r, a)
case e.selected:
gl.Color4ub(r, g, b, a)
default:
gl.Color4ub(r, g, b, gl.Ubyte((int(a)*200)>>8))
}
glow := texture.LoadFromPath(filepath.Join(base.GetDataDir(), "ui", "glow.png"))
dx := float64(e.last_render_width + 0.5)
dy := float64(e.last_render_width * 150 / 100)
glow.Render(float64(pos.X), float64(pos.Y), dx, dy)
gl.PopAttrib()
}
func (e *Entity) Color() (r, g, b, a byte) {
return 255, 255, 255, 255
}
func (e *Entity) Render(pos mathgl.Vec2, width float32) {
var rgba [4]float64
gl.GetDoublev(gl.CURRENT_COLOR, (*gl.Double)(&rgba[0]))
e.last_render_width = width
gl.Enable(gl.TEXTURE_2D)
e.drawReticle(pos, rgba)
if e.sprite.sp != nil {
dxi, dyi := e.sprite.sp.Dims()
dx := float32(dxi)
dy := float32(dyi)
tx, ty, tx2, ty2 := e.sprite.sp.Bind()
gl.Begin(gl.QUADS)
gl.TexCoord2d(gl.Double(tx), gl.Double(-ty))
gl.Vertex2f(gl.Float(pos.X), gl.Float(pos.Y))
gl.TexCoord2d(gl.Double(tx), gl.Double(-ty2))
gl.Vertex2f(gl.Float(pos.X), gl.Float(pos.Y+dy*width/dx))
gl.TexCoord2d(gl.Double(tx2), gl.Double(-ty2))
gl.Vertex2f(gl.Float(pos.X+width), gl.Float(pos.Y+dy*width/dx))
gl.TexCoord2d(gl.Double(tx2), gl.Double(-ty))
gl.Vertex2f(gl.Float(pos.X+width), gl.Float(pos.Y))
gl.End()
}
}
func facing(v mathgl.Vec2) int {
fs := []mathgl.Vec2{
mathgl.Vec2{-1, -1},
mathgl.Vec2{-4, 1},
mathgl.Vec2{0, 1},
mathgl.Vec2{1, 1},
mathgl.Vec2{1, 0},
mathgl.Vec2{1, -4},
}
var max float32
ret := 0
for i := range fs {
fs[i].Normalize()
dot := fs[i].Dot(&v)
if dot > max {
max = dot
ret = i
}
}
return ret
}
func (e *Entity) TurnToFace(x, y int) {
target := mathgl.Vec2{float32(x), float32(y)}
source := mathgl.Vec2{float32(e.X), float32(e.Y)}
var seg mathgl.Vec2
seg.Assign(&target)
seg.Subtract(&source)
target_facing := facing(seg)
f_diff := target_facing - e.sprite.sp.StateFacing()
if f_diff != 0 {
f_diff = (f_diff + 6) % 6
if f_diff > 3 {
f_diff -= 6
}
for f_diff < 0 {
e.sprite.sp.Command("turn_left")
f_diff++
}
for f_diff > 0 {
e.sprite.sp.Command("turn_right")
f_diff--
}
}
}
// Advances ent up to dist towards the target cell. Returns the distance
// traveled.
func (e *Entity) DoAdvance(dist float32, x, y int) float32 {
if dist <= 0 {
e.sprite.sp.Command("stop")
return 0
}
e.sprite.sp.Command("move")
source := mathgl.Vec2{float32(e.X), float32(e.Y)}
target := mathgl.Vec2{float32(x), float32(y)}
var seg mathgl.Vec2
seg.Assign(&target)
seg.Subtract(&source)
e.TurnToFace(x, y)
var traveled float32
if seg.Length() > dist {
seg.Scale(dist / seg.Length())
traveled = dist
} else {
traveled = seg.Length()
}
seg.Add(&source)
e.X = float64(seg.X)
e.Y = float64(seg.Y)
return dist - traveled
}
func (e *Entity) Think(dt int64) {
if e.sprite.sp != nil {
e.sprite.sp.Think(dt)
}
}
func (e *Entity) SetGear(gear_name string) bool {
if e.ExplorerEnt == nil {
base.Error().Printf("Tried to set gear on a non-explorer entity.")
return false
}
if e.ExplorerEnt.Gear != nil && gear_name != "" {
base.Error().Printf("Tried to set gear on an explorer that already had gear.")
return false
}
if e.ExplorerEnt.Gear == nil && gear_name == "" {
base.Error().Printf("Tried to remove gear from an explorer with no gear.")
return false
}
if gear_name == "" {
algorithm.Choose(&e.Actions, func(a Action) bool {
return a.String() != e.ExplorerEnt.Gear.Action
})
if e.ExplorerEnt.Gear.Condition != "" {
e.Stats.RemoveCondition(e.ExplorerEnt.Gear.Condition)
}
e.ExplorerEnt.Gear = nil
return true
}
var g Gear
g.Defname = gear_name
base.GetObject("gear", &g)
if g.Name == "" {
base.Error().Printf("Tried to load gear '%s' that doesn't exist.", gear_name)
return false
}
e.ExplorerEnt.Gear = &g
if g.Action != "" {
e.Actions = append(e.Actions, MakeAction(g.Action))
}
if g.Condition != "" {
e.Stats.ApplyCondition(status.MakeCondition(g.Condition))
}
return true
}
func (e *Entity) OnRound() {
if e.Stats != nil {
e.Stats.OnRound()
if e.Stats.HpCur() <= 0 {
e.sprite.Sprite().Command("defend")
e.sprite.Sprite().Command("killed")
}
}
}