-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplant.go
More file actions
172 lines (156 loc) · 3.36 KB
/
plant.go
File metadata and controls
172 lines (156 loc) · 3.36 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
package main
import (
"math/rand"
"github.com/gotk3/gotk3/cairo"
)
var directionModifiers = [][]int{
[]int{-1, 1},
[]int{0, 1},
[]int{1, 1},
[]int{-1, 0},
[]int{1, 0},
[]int{-1, -1},
[]int{0, -1},
[]int{1, -1},
}
// Plant represents a single plant for our purposes
type Plant struct {
x, y, energy int
color RGB
altruistic bool
}
func (p *Plant) act(g *Grid) bool {
// Check for the chance to spawn.
if threshhold := rand.Intn(100); threshhold < 5 {
// We're spawning, choose a random direction to try and spread.
direction := rand.Intn(8)
cX := p.x - directionModifiers[direction][0]
cY := p.y - directionModifiers[direction][1]
if a, inbounds := g.getCell(cX, cY); inbounds {
if a == nil {
// We have enough energy to spawn, huzzah.
if p.energy > 100 {
newPlant := p.split(cX, cY)
g.append(&newPlant, newPlant.x, newPlant.y)
}
} else {
if p.altruistic {
// Altruistic plants will share their energy as long as they won't
// steal energy from their target. Sharing averages out their
// respective energies.
if p.energy > a.getEnergy() {
split := (p.energy + a.getEnergy()) / 2
p.energy = split
a.setEnergy(split)
}
} else {
// Nonaltruistic plants are theives. They steal energy, though some
// is wasted.
p.energy += 6
a.setEnergy(a.getEnergy() - 8)
}
// Stagnation penelty.
p.energy -= 3
}
}
return true
} else if threshhold < 60 && p.energy < 121 {
// Not spawning, we rolled the get energy result.
p.energy++
} else if p.energy == 0 {
// check for starvation
g.remove(p.x, p.y)
} else {
// if nothing else lose an energy.
p.energy--
}
return false
}
func (p *Plant) split(x, y int) Plant {
// Check for mutation
r, g, b, a := p.color.r, p.color.g, p.color.b, p.altruistic
if threshhold := rand.Intn(200); threshhold < 1 {
flip := rand.Intn(7)
switch flip {
case 0:
x := r
r = b
b = x
case 1:
x := r
r = g
g = x
case 2:
x := b
b = g
g = x
case 3:
x := b
b = r
r = x
case 4:
x := g
g = r
r = x
case 5:
x := g
g = b
b = x
case 6:
a = !a
}
}
newPlant := Plant{x, y, (p.energy / 2) - 5, RGB{r, g, b}, a}
p.energy = p.energy/2 - 5
return newPlant
}
func (p *Plant) getX() int {
return p.x
}
func (p *Plant) getY() int {
return p.y
}
func (p *Plant) setX(cX int) {
p.x = cX
}
func (p *Plant) setY(cY int) {
p.y = cY
}
func (p *Plant) setEnergy(newEnergy int) {
p.energy = newEnergy
}
func (p *Plant) getEnergy() int {
return p.energy
}
func (p *Plant) create() {
p.x, p.y = rand.Intn(63), rand.Intn(47)
p.energy = 50
p.color = RGB{rand.Float64(), rand.Float64(), rand.Float64()}
p.altruistic = rand.Intn(2) == 1
}
func (p *Plant) draw(unitsize float64, cr *cairo.Context) {
cr.SetSourceRGBA(p.color.r, p.color.g, p.color.b, p.getBrightness())
x, y := float64(p.x)*unitsize, float64(p.y)*unitsize
cr.Rectangle(x, y, unitsize, unitsize)
cr.Fill()
// Need some way of seeing what plants are altruistic.
if p.altruistic {
cr.SetSourceRGBA(0, 0, 0, 1)
cr.Rectangle(x+3, y+3, unitsize-6, unitsize-6)
cr.Fill()
}
}
func (p *Plant) getBrightness() float64 {
var retValue float64
switch {
case p.energy <= 25:
retValue = .25
case p.energy <= 50:
retValue = .50
case p.energy <= 75:
retValue = .75
default:
retValue = 1.0
}
return retValue
}