-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulator.go
407 lines (365 loc) · 9.18 KB
/
simulator.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
package main
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
_ "image/png"
"log"
"math"
"math/rand"
"github.com/dmarkham/goNEAT/neat/genetics"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
resources "github.com/hajimehoshi/ebiten/examples/resources/images/flappy"
"golang.org/x/image/font"
)
func init() {
//rand.Seed(time.Now().UnixNano())
//rand.Seed(43)
}
var (
gopherImage *ebiten.Image
tilesImage *ebiten.Image
arcadeFont font.Face
smallArcadeFont font.Face
)
func init() {
img, _, err := image.Decode(bytes.NewReader(resources.Gopher_png))
if err != nil {
log.Fatal(err)
}
gopherImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
img, _, err = image.Decode(bytes.NewReader(resources.Tiles_png))
if err != nil {
log.Fatal(err)
}
tilesImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
func floorDiv(x, y int) int {
d := x / y
if d*y == x || x >= 0 {
return d
}
return d - 1
}
func floorMod(x, y int) int {
return x - floorDiv(x, y)*y
}
const (
screenWidth = 640
screenHeight = 480
//screenWidth = 800
//screenHeight = 600
tileSize = 32
fontSize = 32
smallFontSize = fontSize / 2
pipeWidth = tileSize * 2
pipeStartOffsetX = 8
pipeIntervalX = 16
pipeGapY = 6
maxScore = 50
)
type Mode int
const (
ModeTitle Mode = iota
ModeGame
ModeGameOver
)
type Player struct {
// The gopher's position
x16 int
y16 int
vy16 int
dead bool
}
type Game struct {
mode Mode
players []*Player
// Camera
cameraX int
cameraY int
Population *genetics.Population
// Pipes
pipeTileYs []int
gameoverCount int
steps uint64
}
func NewGame(pop *genetics.Population) *Game {
g := &Game{Population: pop}
g.init()
return g
}
func (g *Game) init() {
g.mode = ModeGame
g.players = make([]*Player, len(g.Population.Organisms))
for i := 0; i < len(g.players); i++ {
p := &Player{}
p.x16 = 0
p.y16 = 100 * 16
g.players[i] = p
}
g.cameraX = -240
g.cameraY = 0
if g.pipeTileYs == nil {
g.pipeTileYs = make([]int, 256)
for i := range g.pipeTileYs {
g.pipeTileYs[i] = rand.Intn(6) + 2
}
} else {
panic("in else")
g.pipeTileYs[0] = rand.Intn(6) + 2
g.pipeTileYs[1] = rand.Intn(6) + 2
g.pipeTileYs[3] = rand.Intn(6) + 2
g.pipeTileYs[10] = rand.Intn(6) + 2
g.pipeTileYs[20] = rand.Intn(6) + 2
}
}
func (g *Game) Update() error {
deadCount := 0
switch g.mode {
case ModeGame:
g.steps++
g.cameraX += 2
nextPipeHight, nextPipeDistance := g.nextPipe()
for i, p := range g.players {
if p.dead {
deadCount++
continue
//return errors.New("Dead")
}
p.x16 += 32
org := g.Population.Organisms[i]
netDepth, err := org.Phenotype.MaxDepth()
if err != nil {
panic(fmt.Sprintf("Err 1: %+v", err))
}
d := float64((p.x16 + (tileSize * nextPipeDistance)) - p.x16)
//fmt.Println(i, p.x16/tileSize, g.cameraX, p.x16, p.vy16, nextPipeHight, nextPipeDistance, d)
//org.Phenotype.LoadSensors([]float64{float64(g.cameraX), float64(p.vy16), float64(p.y16), float64(nextPipeHight * tileSize), d})
org.Phenotype.LoadSensors([]float64{float64(p.vy16), float64(p.y16), float64(nextPipeHight * tileSize), d})
// Relax net and get output
success, err := org.Phenotype.Activate()
if err != nil {
org.Fitness = 0
p.dead = true
fmt.Println(err)
continue
//panic(fmt.Sprintf("Err 3: %+v", err))
}
if !success {
// use depth to ensure relaxation
for relax := 0; relax <= netDepth; relax++ {
success, err = org.Phenotype.Activate()
if err != nil {
org.Fitness = 0
p.dead = true
fmt.Println(err)
continue
//panic(fmt.Sprintf("Err 2: %+v", err))
}
}
}
if !success {
panic("we didn't have success")
}
j := org.Phenotype.Outputs[0].Activation
org.Phenotype.Flush()
//fmt.Println("HERE: Jump?", j)
DoJump := false
if j >= .5 {
DoJump = true
}
if DoJump {
p.vy16 = -96
}
p.y16 += p.vy16
// Gravity
p.vy16 += 4
if p.vy16 > 96 {
p.vy16 = 96
}
//if p.score() > 0 {
// fmt.Println("Winning: ", p.score(), p.y16/16, p.x16, nextPipeHight, nextPipeDistance)
//}
if g.hit(i) {
fitness := float64(p.score()) / maxScore
//fmt.Println("HIT: ", deadCount, fitness, p.y16/16, p.x16, nextPipeHight, nextPipeDistance)
org.Fitness = fitness
org.Error = 1 - org.Fitness
p.dead = true
} else if p.score() >= maxScore {
fitness := 1.0
//fmt.Println(fitness, p.y16/16, p.x16, nextPipeHight, nextPipeDistance)
org.Fitness = fitness
org.Error = 0
org.IsWinner = true
p.dead = true
}
}
if deadCount == len(g.players) {
return errors.New("done")
}
}
return nil
}
func (g *Game) nextPipe() (int, int) {
const (
nx = screenWidth / tileSize
ny = screenHeight / tileSize
pipeTileSrcX = 128
pipeTileSrcY = 192
)
for i := -2; i < nx+1; i++ {
// pipe
if tileY, ok := g.pipeAt(floorDiv(g.cameraX, tileSize) + i); ok {
return int(tileY), i
}
}
return g.pipeTileYs[0], 20
}
func (g *Game) pipeAt(tileX int) (tileY int, ok bool) {
if (tileX - pipeStartOffsetX) <= 0 {
return 0, false
}
if floorMod(tileX-pipeStartOffsetX, pipeIntervalX) != 0 {
return 0, false
}
idx := floorDiv(tileX-pipeStartOffsetX, pipeIntervalX)
return g.pipeTileYs[idx%len(g.pipeTileYs)], true
}
func (g *Player) score() int {
//return int(0 - math.Abs(float64(g.y16)))
//return g.x16
x := floorDiv(g.x16, 16) / tileSize
if (x - pipeStartOffsetX) <= 0 {
return 0
}
s := floorDiv(x-pipeStartOffsetX, pipeIntervalX)
if s > 0 {
return s
}
return s
//return int(0 - math.Abs(float64(g.y16)))
}
func (g *Game) hit(playerID int) bool {
if g.mode != ModeGame {
return false
}
const (
gopherWidth = 30
gopherHeight = 60
)
//if g.players[playerID].y16 < 0 {
// return true
//}
w, h := gopherImage.Size()
x0 := floorDiv(g.players[playerID].x16, 16) + (w-gopherWidth)/2
y0 := floorDiv(g.players[playerID].y16, 16) + (h-gopherHeight)/2
x1 := x0 + gopherWidth
y1 := y0 + gopherHeight
if y0 < -tileSize*4 {
return true
}
if y1 >= screenHeight-tileSize {
return true
}
xMin := floorDiv(x0-pipeWidth, tileSize)
xMax := floorDiv(x0+gopherWidth, tileSize)
for x := xMin; x <= xMax; x++ {
y, ok := g.pipeAt(x)
if !ok {
continue
}
if x0 >= x*tileSize+pipeWidth {
continue
}
if x1 < x*tileSize {
continue
}
if y0 < y*tileSize {
return true
}
if y1 >= (y+pipeGapY)*tileSize {
return true
}
}
return false
}
func (g *Game) drawGopher(screen *ebiten.Image) {
w, h := gopherImage.Size()
count := 0
for _, p := range g.players {
if p.dead {
//continue
}
op := &ebiten.DrawImageOptions{}
count++
if count > 5 {
//break
}
op.GeoM.Translate(-float64(w)/2.0, -float64(h)/2.0)
op.GeoM.Rotate(float64(p.vy16) / 96.0 * math.Pi / 6)
op.GeoM.Translate(float64(w)/2.0, float64(h)/2.0)
op.GeoM.Translate(float64(p.x16/16.0)-float64(g.cameraX), float64(p.y16/16.0)-float64(g.cameraY))
op.Filter = ebiten.FilterLinear
screen.DrawImage(gopherImage, op)
}
}
func (g *Game) drawTiles(screen *ebiten.Image) {
const (
nx = screenWidth / tileSize
ny = screenHeight / tileSize
pipeTileSrcX = 128
pipeTileSrcY = 192
)
op := &ebiten.DrawImageOptions{}
for i := -2; i < nx+1; i++ {
// ground
op.GeoM.Reset()
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
screen.DrawImage(tilesImage.SubImage(image.Rect(0, 0, tileSize, tileSize)).(*ebiten.Image), op)
// pipe
if tileY, ok := g.pipeAt(floorDiv(g.cameraX, tileSize) + i); ok {
for j := 0; j < tileY; j++ {
op.GeoM.Reset()
op.GeoM.Scale(1, -1)
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
float64(j*tileSize-floorMod(g.cameraY, tileSize)))
op.GeoM.Translate(0, tileSize)
var r image.Rectangle
if j == tileY-1 {
r = image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize)
} else {
r = image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize*2)
}
screen.DrawImage(tilesImage.SubImage(r).(*ebiten.Image), op)
}
for j := tileY + pipeGapY; j < screenHeight/tileSize-1; j++ {
op.GeoM.Reset()
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
float64(j*tileSize-floorMod(g.cameraY, tileSize)))
var r image.Rectangle
if j == tileY+pipeGapY {
r = image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize)
} else {
r = image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize+tileSize)
}
screen.DrawImage(tilesImage.SubImage(r).(*ebiten.Image), op)
}
}
}
}
func (g *Game) Draw(screen *ebiten.Image) error {
//screen.Clear()
screen.Fill(color.RGBA{0x80, 0xa0, 0xc0, 0xff})
g.drawTiles(screen)
g.drawGopher(screen)
//scoreStr := fmt.Sprintf("%04d", g.player.score())
//scoreStr := "Yo"
//text.Draw(screen, scoreStr, arcadeFont, screenWidth-len(scoreStr)*fontSize, fontSize, color.White)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
return nil
}