-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbee.go
126 lines (116 loc) · 2.28 KB
/
bee.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
package main
import (
"fmt"
"os"
"time"
)
type Bee struct {
Id string
Nectar int
TripDuration int
Life int
location Location
lastMove int
}
func NewBee(l Location) *Bee {
b := &Bee{
Id: NewId(),
Nectar: 0,
TripDuration: 0,
Life: BeeLife,
location: l,
}
return b
}
func (b *Bee) Spawn(n *NomadAPI, r *Redis) {
err := n.CreateBeeJob(b)
if err != nil {
fmt.Println(err)
}
err = r.SaveBee(*b)
if err != nil {
fmt.Println(err)
}
}
func (b *Bee) Die(r *Redis) {
r.DeleteBee(b)
//Goodbye
fmt.Println("Time to die...")
os.Exit(0)
}
func (b *Bee) Lifecycle() {
r := NewRedis()
for {
see, err := r.See(b.location, BeeSight, b.Id)
if err != nil {
fmt.Println(err)
b.Die(r)
}
loc, landed := b.decide(see)
b.location = loc
switch landed {
case FlowerCode:
f, err := r.GetFlowerAt(loc)
if err != nil {
fmt.Println(err)
}
f.Pollinate(r)
b.Nectar++
break
case HiveCode:
h, err := r.GetHiveAt(loc)
if err != nil {
fmt.Println(err)
b.Die(r)
}
h.Visit(b.Nectar, r)
b.Nectar = 0
b.TripDuration = 0
break
}
b.TripDuration++
b.Life--
if b.Life <= 0 || b.TripDuration > BeeTravelLimit {
b.Die(r)
} else {
r.SaveBee(*b)
}
time.Sleep(Tick * time.Millisecond)
}
}
// Heres the fun part
func (b *Bee) decide(vision [][]byte) (Location, byte) {
target := Location{X: BeeSight * 2, Y: BeeSight * 2}
relativeLoc := Location{X: BeeSight, Y: BeeSight} // Hmm
for i := 0; i < len(vision); i++ {
for j := 0; j < len(vision[i]); j++ {
switch vision[i][j] {
case BeeCode:
break
case FlowerCode:
if b.Nectar < BeeNectarCapacity {
loc := Location{X: i, Y: j}
if relativeLoc.distance(loc) < relativeLoc.distance(target) {
target = loc
}
}
break
case HiveCode:
if b.Nectar == BeeNectarCapacity {
loc := Location{X: i, Y: j}
if relativeLoc.distance(loc) < relativeLoc.distance(target) {
target = loc
}
}
break
}
}
}
bearing := relativeLoc.bearing(target)
b.lastMove = bearing
move := relativeLoc.moveTo(target)
landing := vision[move.X][move.Y]
newX := b.location.X + (move.X - BeeSight)
newY := b.location.Y + (move.Y - BeeSight)
return Location{X: newX, Y: newY}, landing
}