-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
325 lines (276 loc) · 6.46 KB
/
tree.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
package main
import "time"
type align int
const (
center align = iota + 1
left
right
)
type branch int
const (
trunk branch = iota
shootLeft
shootRight
// TODO: find better names for these
dying
dead
)
type counters struct {
branches int
shoots int
shootCounter int
}
// The algorithm for tree generation was ported from the cbonsai codebase and ideally generates identical output.
func drawTree(sc *screen, opts opts) error {
counters := counters{
branches: 0,
shoots: 0,
shootCounter: rand.Int(),
}
life := opts.life
_, maxY := sc.Size()
return drawBranch(sc, opts, counters, life, trunk, sc.x, sc.y, maxY-(maxY-sc.y-1))
}
func drawBranch(sc *screen, opts opts, counters counters, life int, kind branch, x int, y int, maxY int) error {
counters.branches++
dx := 0
dy := 0
age := 0
shootCooldown := opts.multiplier
for life > 0 {
life--
age = opts.life - life
dx, dy = deltas(kind, life, age, opts.multiplier)
// reduce dy if too close to the ground
if dy > 0 && y > (maxY-2) {
dy--
}
// near-dead branch should branch into a lot of leaves
if life < 3 {
err := drawBranch(sc, opts, counters, life, dead, x, y, maxY)
if err != nil {
return err
}
// dying trunk should branch into a lot of leaves
} else if kind == trunk && life < (opts.multiplier+2) {
err := drawBranch(sc, opts, counters, life, dying, x, y, maxY)
if err != nil {
return err
}
// dying shoot should branch into a lot of leaves
} else if (kind == shootLeft || kind == shootRight) && life < (opts.multiplier+2) {
err := drawBranch(sc, opts, counters, life, dying, x, y, maxY)
if err != nil {
return err
}
// trunks should re-branch either randomly, or upon every <multiplier> steps
} else if kind == trunk && (((rand.Int() % 3) == 0) || (life%opts.multiplier == 0)) {
// if trunk is branching and not about to die, create another trunk with random life
if (rand.Int()%8 == 0) && life > 7 {
shootCooldown = opts.multiplier * 2 // reset shoot cooldown
err := drawBranch(sc, opts, counters, life+(rand.Int()%5-2), trunk, x, y, maxY)
if err != nil {
return err
}
// otherwise create a shoot
} else if shootCooldown <= 0 {
shootCooldown = opts.multiplier * 2 // reset shoot cooldown
shootLife := life + opts.multiplier
// first shoot is randomly directed
counters.shoots++
counters.shootCounter++
// create shoot
err := drawBranch(sc, opts, counters, shootLife, branch((counters.shootCounter%2)+1), x, y, maxY)
if err != nil {
return err
}
}
}
shootCooldown--
// move in x and y directions
x += dx
y += dy
color := chooseColor(kind)
// choose string to use for this branch
leaf := chooseLeaf(kind, life, dx, dy, opts)
sc.x = x
sc.y = y
sc.draw(leaf, color)
if opts.live && active {
// draw message box in every live iteration
if opts.msg != "" {
sc.drawMessage(opts.msg, opts.msgX, opts.msgY)
}
// emit drawn event to trigger screen refresh
evDrawn(sc)
// We either await the delay or wait for shutdown.
select {
case <-shutdown:
return nil
case <-time.After(opts.time):
}
}
}
return nil
}
// determine change in X and Y coordinates of a given branch
func deltas(branchType branch, life int, age int, multiplier int) (int, int) {
dx := 0
dy := 0
switch branchType {
case trunk:
if age <= 2 || life < 4 {
// new or dead trunk
dy = 0
dx = (rand.Int() % 3) - 1
} else if age < (multiplier * 3) {
// young trunk should grow wide
// every (multiplier * 0.8) steps, raise tree to next level
if age%(multiplier/2) == 0 {
dy = -1
} else {
dy = 0
}
dice := rand.Intn(10)
if dice >= 0 && dice <= 0 {
dx = -2
} else if dice >= 1 && dice <= 3 {
dx = -1
} else if dice >= 4 && dice <= 5 {
dx = 0
} else if dice >= 6 && dice <= 8 {
dx = 1
} else if dice >= 9 && dice <= 9 {
dx = 2
}
} else {
// middle-aged trunk
dice := rand.Intn(10)
if dice > 2 {
dy = -1
} else {
dy = 0
}
dx = (rand.Int() % 3) - 1
}
case shootLeft: // trend left and little vertical movement
dice := rand.Intn(10)
if dice >= 0 && dice <= 1 {
dy = -1
} else if dice >= 2 && dice <= 7 {
dy = 0
} else if dice >= 8 && dice <= 9 {
dy = 1
}
dice = rand.Intn(10)
if dice >= 0 && dice <= 1 {
dx = -2
} else if dice >= 2 && dice <= 5 {
dx = -1
} else if dice >= 6 && dice <= 8 {
dx = 0
} else if dice >= 9 && dice <= 9 {
dx = 1
}
case shootRight: // trend right and little vertical movement
dice := rand.Intn(10)
if dice >= 0 && dice <= 1 {
dy = -1
} else if dice >= 2 && dice <= 7 {
dy = 0
} else if dice >= 8 && dice <= 9 {
dy = 1
}
dice = rand.Intn(10)
if dice >= 0 && dice <= 1 {
dx = 2
} else if dice >= 2 && dice <= 5 {
dx = 1
} else if dice >= 6 && dice <= 8 {
dx = 0
} else if dice >= 9 && dice <= 9 {
dx = -1
}
case dying: // discourage vertical growth(?) trend left/right (-3,3)
dice := rand.Intn(10)
if dice >= 0 && dice <= 1 {
dy = -1
} else if dice >= 2 && dice <= 8 {
dy = 0
} else if dice >= 9 && dice <= 9 {
dy = 1
}
dice = rand.Intn(15)
if dice >= 0 && dice <= 0 {
dx = -3
} else if dice >= 1 && dice <= 2 {
dx = -2
} else if dice >= 3 && dice <= 5 {
dx = -1
} else if dice >= 6 && dice <= 8 {
dx = 0
} else if dice >= 9 && dice <= 11 {
dx = 1
} else if dice >= 12 && dice <= 13 {
dx = 2
} else if dice >= 14 && dice <= 14 {
dx = 3
}
case dead: // fill in surrounding area
dice := rand.Intn(10)
if dice >= 0 && dice <= 2 {
dy = -1
} else if dice >= 3 && dice <= 6 {
dy = 0
} else if dice >= 7 && dice <= 9 {
dy = 1
}
dx = (rand.Int() % 3) - 1
}
return dx, dy
}
func chooseLeaf(branch branch, life int, dx int, dy int, opts opts) string {
s := "?"
if life < 4 {
branch = dying
}
switch branch {
case trunk:
if dy == 0 {
s = "/~"
} else if dx < 0 {
s = "\\|"
} else if dx == 0 {
s = "/|\\"
} else if dx > 0 {
s = "|/"
}
case shootLeft:
if dy > 0 {
s = "\\"
} else if dy == 0 {
s = "\\_"
} else if dx < 0 {
s = "\\|"
} else if dx == 0 {
s = "/|"
} else if dx > 0 {
s = "/"
}
case shootRight:
if dy > 0 {
s = "/"
} else if dy == 0 {
s = "_/"
} else if dx < 0 {
s = "\\|"
} else if dx == 0 {
s = "/|"
} else if dx > 0 {
s = "/"
}
case dying, dead:
s = opts.leaves[rand.Int()%len(opts.leaves)]
}
return s
}