-
Notifications
You must be signed in to change notification settings - Fork 0
/
ant.go
151 lines (126 loc) · 3.2 KB
/
ant.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
package main
type Ant struct {
Id uint `json:"id"`
Event string `json:"event"`
Errors uint `json:"errors"`
Age uint `json:"age"`
Health uint `json:"health"`
Cargo uint `json:"cargo"`
Point Point `json:"point"`
}
func (a *Ant) tryUnload() (bool, *Order) {
if a.Cargo > 0 && a.Point.Y > 0 &&
canvas.Cells[a.Point.Y-1][a.Point.X].Hive == id &&
canvas.Cells[a.Point.Y-1][a.Point.X].Ant == "" {
return true, &Order{
Action: ActionPut,
Direction: DirectionUp}
}
if a.Cargo > 0 && a.Point.X < canvas.Width-1 &&
canvas.Cells[a.Point.Y][a.Point.X+1].Hive == id &&
canvas.Cells[a.Point.Y][a.Point.X+1].Ant == "" {
return true, &Order{
Action: ActionPut,
Direction: DirectionRight}
}
if a.Cargo > 0 && a.Point.Y < canvas.Height-1 &&
canvas.Cells[a.Point.Y+1][a.Point.X].Hive == id &&
canvas.Cells[a.Point.Y+1][a.Point.X].Ant == "" {
return true, &Order{
Action: ActionPut,
Direction: DirectionDown}
}
if a.Cargo > 0 && a.Point.X > 0 &&
canvas.Cells[a.Point.Y][a.Point.X-1].Hive == id &&
canvas.Cells[a.Point.Y][a.Point.X-1].Ant == "" {
return true, &Order{
Action: ActionPut,
Direction: DirectionLeft}
}
return false, nil
}
func (a *Ant) tryConsume() (bool, *Order) {
order := &Order{}
if a.Health < 9 {
order.Action = ActionEat
} else if a.Cargo < 9 {
order.Action = ActionTake
} else {
return false, nil
}
if isFood(a.Point.Y-1, a.Point.X, order) {
order.Direction = DirectionUp
return true, order
}
if isFood(a.Point.Y, a.Point.X+1, order) {
order.Direction = DirectionRight
return true, order
}
if isFood(a.Point.Y+1, a.Point.X, order) {
order.Direction = DirectionDown
return true, order
}
if isFood(a.Point.Y, a.Point.X-1, order) {
order.Direction = DirectionLeft
return true, order
}
return false, nil
}
func tryMove(a Ant) (bool, *Order) {
objects := getTargets()
var shortest uint = 9999999
var firstTarget *Target
var secondTarget *Target
for _, object := range objects {
if a.Cargo == 9 && !object.hive { // move home
continue
}
if a.Cargo < 5 && object.hive { // search for food
continue
}
s := object.distance(a.Point.Y, a.Point.X)
if s == 0 {
continue
}
if s < shortest {
shortest = s
if !object.used {
secondTarget = firstTarget
firstTarget = object
} else {
secondTarget = object
}
}
}
if firstTarget != nil {
return chooseDirection(a, firstTarget.y, firstTarget.x)
}
if secondTarget != nil {
return chooseDirection(a, secondTarget.y, secondTarget.x)
}
return false, nil
}
//TODO: check for future occupied cells by my ants
func chooseDirection(a Ant, dy, dx uint) (bool, *Order) {
if a.Point.X < dx && isEmpty(a.Point.Y, a.Point.X+1) {
return true, &Order{
Action: ActionMove,
Direction: DirectionRight}
}
if a.Point.Y < dy && isEmpty(a.Point.Y+1, a.Point.X) {
return true, &Order{
Action: ActionMove,
Direction: DirectionDown}
}
if a.Point.X > dx && isEmpty(a.Point.Y, a.Point.X-1) {
return true, &Order{
Action: ActionMove,
Direction: DirectionLeft}
}
if a.Point.Y > dy && isEmpty(a.Point.Y-1, a.Point.X) {
return true, &Order{
Action: ActionMove,
Direction: DirectionUp}
}
return false, nil
}