This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgame_test.go
146 lines (137 loc) · 3.45 KB
/
game_test.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
package main
//import "log"
import "testing"
import "github.com/anaseto/gruid"
func init() {
Testing = true
DisableAnimations = true
}
func TestInitLevel(t *testing.T) {
for i := 0; i < 50; i++ {
md := &model{}
g := &game{md: md}
md.g = g
for depth := 0; depth < MaxDepth; depth++ {
g.InitLevel()
if g.Player.P == invalidPos {
t.Errorf("Player starting cell is not valid")
}
if !terrain(g.Dungeon.Cell(g.Player.P)).IsPlayerPassable() {
t.Errorf("Player starting cell is not passable: %+v", g.Dungeon.Cell(g.Player.P).ShortDesc(g, g.Player.P))
}
if terrain(g.Dungeon.Cell(g.Player.P)) != GroundCell {
t.Errorf("Player starting cell is not ground: %+v", g.Dungeon.Cell(g.Player.P).ShortDesc(g, g.Player.P))
}
if g.Depth == WinDepth {
if terrain(g.Dungeon.Cell(g.Places.Shaedra)) != StoryCell {
t.Errorf("Shaedra not there: %+v", g.Places.Shaedra)
}
if g.Objects.Story[g.Places.Shaedra] != StoryShaedra {
t.Errorf("bad Shaedra place: %+v", g.Places.Shaedra)
}
}
if g.Depth == MaxDepth {
if terrain(g.Dungeon.Cell(g.Places.Artifact)) != StoryCell {
t.Errorf("Artifact not there: %+v", g.Places.Artifact)
}
if g.Objects.Story[g.Places.Artifact] != StoryArtifactSealed {
t.Errorf("bad Artifact place: %+v", g.Places.Shaedra)
}
}
if g.Depth == MaxDepth || g.Depth == WinDepth {
if terrain(g.Dungeon.Cell(g.Places.Marevor)) != StoryCell {
t.Errorf("Marevor not there: %+v", g.Places.Artifact)
}
if g.Objects.Story[g.Places.Marevor] != NoStory {
t.Errorf("bad Marevor place: %+v", g.Places.Shaedra)
}
}
if g.Depth == MaxDepth || g.Depth == WinDepth {
if terrain(g.Dungeon.Cell(g.Places.Monolith)) != StoryCell {
t.Errorf("Monolith not there: %+v", g.Places.Artifact)
}
if g.Objects.Story[g.Places.Monolith] != NoStory {
t.Errorf("bad Monolith place: %+v", g.Places.Shaedra)
}
}
if g.Depth != WinDepth {
if len(g.Objects.Magaras) != 1 {
t.Errorf("bad number of magaras: %+v", g.Objects.Magaras)
}
}
for _, m := range g.Monsters {
if !g.Dungeon.Cell(m.P).IsPassable() {
t.Errorf("Not free: %+v", m.P)
}
}
for j := 0; j < 20; j++ {
g.EndTurn()
g.PlayerBump(randomPlayerNeighbor(g.Player.P)) // wait if impossible
if g.Player.HP == 0 {
g.Player.HP = 10
}
}
g.Depth++
}
}
}
func BenchmarkLOS(b *testing.B) {
g := &game{}
g.InitLevel()
for i := 0; i < b.N; i++ {
g.ComputeLOS()
}
}
func BenchmarkLights(b *testing.B) {
g := &game{}
g.InitLevel()
for i := 0; i < b.N; i++ {
g.ComputeLights()
}
}
func BenchmarkInitLevel(b *testing.B) {
for i := 0; i < b.N; i++ {
g := &game{}
for depth := 0; depth < MaxDepth; depth++ {
g.InitLevel()
g.Depth++
}
}
}
func BenchmarkEndTurn(b *testing.B) {
md := &model{}
g := &game{md: md}
md.g = g
g.InitLevel()
for i := 0; i < b.N; i++ {
g.EndTurn()
if g.Player.HP == 0 {
g.Player.HP = 10
}
}
}
func randomPlayerNeighbor(p gruid.Point) gruid.Point {
switch RandInt(4) {
case 0:
return p.Add(gruid.Point{1, 0})
case 1:
return p.Add(gruid.Point{-1, 0})
case 2:
return p.Add(gruid.Point{0, 1})
default:
return p.Add(gruid.Point{0, -1})
}
}
func BenchmarkEndTurnPlayer(b *testing.B) {
md := &model{}
g := &game{md: md}
md.g = g
g.InitLevel()
for i := 0; i < b.N; i++ {
g.EndTurn()
g.PlayerBump(randomPlayerNeighbor(g.Player.P)) // wait if impossible
if g.Player.HP == 0 {
g.Player.HP = 10
}
}
}