-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
381 lines (324 loc) · 8.04 KB
/
main.go
File metadata and controls
381 lines (324 loc) · 8.04 KB
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
package main
import (
"fmt"
"image/color"
"math"
"math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
WindowWidth = 800
WindowHeight = 600
CircleRadius = 10
GridCellSize = 20
NumCircles = 50
MaxAge = 1000
ReproductionDistance = GridCellSize * 3
ReproductionChance = 0.01
ReproductionCooldown = 300
InitialEnergy = 100
EnergyLossPerTick = 0.1
FoodEnergy = 50
NumFoodParticles = 20
ReproductionEnergyCost = 0.33
)
type Brain struct {
targetMate *Circle
rng *rand.Rand
}
func NewBrain(rng *rand.Rand) *Brain {
return &Brain{
rng: rng,
}
}
func (b *Brain) think(self *Circle, circles []*Circle) {
if b.rng.Float64() < 0.01 {
self.dx = b.rng.Float64()*2 - 1
self.dy = b.rng.Float64()*2 - 1
return
}
if self.reproductionTime > 0 || !self.alive {
b.targetMate = nil
return
}
if b.targetMate != nil && (!b.targetMate.alive || b.targetMate.reproductionTime > 0) {
b.targetMate = nil
}
if b.targetMate == nil {
b.findNewMate(self, circles)
}
if b.targetMate != nil {
b.moveTowardsMate(self)
}
}
func (b *Brain) findNewMate(self *Circle, circles []*Circle) {
var closestMate *Circle
closestDist := math.MaxFloat64
for _, other := range circles {
if other == self || !other.alive || other.reproductionTime > 0 {
continue
}
dx := other.x - self.x
dy := other.y - self.y
dist := math.Sqrt(dx*dx + dy*dy)
if dist < closestDist {
closestDist = dist
closestMate = other
}
}
b.targetMate = closestMate
}
func (b *Brain) moveTowardsMate(self *Circle) {
dx := b.targetMate.x - self.x
dy := b.targetMate.y - self.y
dist := math.Sqrt(dx*dx + dy*dy)
if dist > 0 {
self.dx = (dx / dist) * 2
self.dy = (dy / dist) * 2
}
}
type Food struct {
x, y float64
}
type Circle struct {
x float64
y float64
dx float64
dy float64
alive bool
color color.RGBA
age int
reproductionTime int
brain *Brain
energy float64
rng *rand.Rand
}
func NewCircle(x, y float64, rng *rand.Rand) *Circle {
return &Circle{
x: x,
y: y,
dx: rng.Float64()*2 - 1,
dy: rng.Float64()*2 - 1,
alive: true,
color: color.RGBA{R: uint8(rng.Intn(256)), G: uint8(rng.Intn(256)), B: uint8(rng.Intn(256)), A: 255},
age: 0,
reproductionTime: 0,
brain: NewBrain(rng),
energy: InitialEnergy,
rng: rng,
}
}
func (c *Circle) update(circles []*Circle) {
c.brain.think(c, circles)
// Calculate potential new position
newX := c.x + c.dx
newY := c.y + c.dy
// Check collisions with other circles
for _, other := range circles {
if other != c && other.alive {
dx := newX - other.x
dy := newY - other.y
distSquared := dx*dx + dy*dy
minDist := float64(CircleRadius * 2)
if distSquared < minDist*minDist {
// Calculate collision response
dist := math.Sqrt(distSquared)
if dist == 0 {
// If exactly overlapping, nudge slightly
newX += c.rng.Float64() - 0.5
newY += c.rng.Float64() - 0.5
continue
}
// Normal vector of collision
nx := dx / dist
ny := dy / dist
// Separate the circles
overlap := minDist - dist
newX += nx * overlap * 0.5
newY += ny * overlap * 0.5
// Bounce velocity along normal
dotProduct := c.dx*nx + c.dy*ny
c.dx -= 2 * dotProduct * nx
c.dy -= 2 * dotProduct * ny
// Add small random variation to prevent getting stuck
c.dx += (c.rng.Float64()*0.2 - 0.1)
c.dy += (c.rng.Float64()*0.2 - 0.1)
}
}
}
// Boundary checking
if newX < CircleRadius {
newX = CircleRadius
c.dx *= -1
} else if newX > WindowWidth-CircleRadius {
newX = WindowWidth - CircleRadius
c.dx *= -1
}
if newY < CircleRadius {
newY = CircleRadius
c.dy *= -1
} else if newY > WindowHeight-CircleRadius {
newY = WindowHeight - CircleRadius
c.dy *= -1
}
c.x = newX
c.y = newY
}
type Game struct {
circles []*Circle
food []*Food
rng *rand.Rand
ticks int
startTime time.Time
}
func (g *Game) respawnFood() {
for i, f := range g.food {
if f == nil && g.rng.Float64() < 0.1 {
g.food[i] = &Food{
x: g.rng.Float64() * WindowWidth,
y: g.rng.Float64() * WindowHeight,
}
}
}
}
func (g *Game) Update() error {
g.ticks++
// Update positions and age
for _, circle := range g.circles {
if circle.alive {
circle.update(g.circles)
circle.age++
circle.energy -= EnergyLossPerTick
// Die if no energy
if circle.energy <= 0 {
circle.alive = false
}
// Check for food collision
for i, f := range g.food {
if f != nil {
dx := circle.x - f.x
dy := circle.y - f.y
if dx*dx+dy*dy < CircleRadius*CircleRadius {
circle.energy += FoodEnergy
g.food[i] = nil
}
}
}
if circle.reproductionTime > 0 {
circle.reproductionTime--
}
if circle.age > MaxAge {
circle.alive = false
}
}
}
// Respawn food
g.respawnFood()
// Handle reproduction
newCircles := []*Circle{}
for i, c1 := range g.circles {
if !c1.alive || c1.reproductionTime > 0 || c1.energy < InitialEnergy*ReproductionEnergyCost {
continue
}
for j, c2 := range g.circles {
if i != j && c2.alive && c2.reproductionTime == 0 && c2.energy >= InitialEnergy*ReproductionEnergyCost {
dx := c1.x - c2.x
dy := c1.y - c2.y
dist := dx*dx + dy*dy
if dist < ReproductionDistance*ReproductionDistance && g.rng.Float64() < ReproductionChance {
newX := (c1.x + c2.x) / 2
newY := (c1.y + c2.y) / 2
newCircle := NewCircle(newX, newY, g.rng)
// Transfer energy from parents to child
energyContribution := InitialEnergy * ReproductionEnergyCost
c1.energy -= energyContribution
c2.energy -= energyContribution
newCircle.energy = energyContribution * 2
newCircles = append(newCircles, newCircle)
c1.reproductionTime = ReproductionCooldown
c2.reproductionTime = ReproductionCooldown
}
}
}
}
g.circles = append(g.circles, newCircles...)
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.Black)
// Draw food
for _, f := range g.food {
if f != nil {
drawCircle(screen, int(f.x), int(f.y), 3, color.RGBA{0, 255, 0, 255})
}
}
for _, circle := range g.circles {
if circle.alive {
drawCircle(screen, int(circle.x), int(circle.y), CircleRadius, circle.color)
}
}
// Draw stats overlay
aliveCount := 0
for _, c := range g.circles {
if c.alive {
aliveCount++
}
}
foodCount := 0
for _, f := range g.food {
if f != nil {
foodCount++
}
}
elapsed := time.Since(g.startTime).Milliseconds()
stats := []string{
fmt.Sprintf("Ticks: %d", g.ticks),
fmt.Sprintf("Time: %dms", elapsed),
fmt.Sprintf("Alive: %d", aliveCount),
fmt.Sprintf("Food: %d", foodCount),
}
for i, text := range stats {
ebitenutil.DebugPrintAt(screen, text, 10, 20*i+10)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return WindowWidth, WindowHeight
}
func drawCircle(screen *ebiten.Image, x, y, radius int, c color.Color) {
for py := -radius; py <= radius; py++ {
for px := -radius; px <= radius; px++ {
if px*px+py*py <= radius*radius {
screen.Set(x+px, y+py, c)
}
}
}
}
func main() {
rng := rand.New(rand.NewSource(1234))
game := &Game{
circles: make([]*Circle, NumCircles),
food: make([]*Food, NumFoodParticles),
rng: rng,
startTime: time.Now(),
}
// Initialize circles
for i := range game.circles {
x := rng.Float64() * WindowWidth
y := rng.Float64() * WindowHeight
game.circles[i] = NewCircle(x, y, rng)
}
// Initialize food
for i := range game.food {
game.food[i] = &Food{
x: rng.Float64() * WindowWidth,
y: rng.Float64() * WindowHeight,
}
}
ebiten.SetWindowSize(WindowWidth, WindowHeight)
ebiten.SetWindowTitle("Moving Circles Life Simulation")
if err := ebiten.RunGame(game); err != nil {
panic(err)
}
}