-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.go
325 lines (269 loc) · 7.1 KB
/
functions.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 (
"basicbots/builtin"
"basicbots/object"
"fmt"
"math"
)
// Return the team number and set it is on a team.
func FunctionTeam(env builtin.Environment, args []object.Object) object.Object {
var t float64
if !teams {
return &object.NumberObject{Value: 0}
}
if current == 0 || current == 1 {
t = 1
}
if current == 2 || current == 3 {
t = 2
}
return &object.NumberObject{Value: t}
}
// Basic statement. LOCX returns the current Y location.
func FunctionLocX(env builtin.Environment, args []object.Object) object.Object {
X := Robots[current].X
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionLocX() LOCX:%5.2f\n", current, Robots[current].X)
//}
return &object.NumberObject{Value: X}
}
// Basic statement. LOCY returns the current Y location
func FunctionLocY(env builtin.Environment, args []object.Object) object.Object {
y := Robots[current].Y
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionLocY() LOCY:%5.2f\n", current, Robots[current].Y)
//}
return &object.NumberObject{Value: y}
}
// Basic statement. SPEED returns the current speed of the robot
func FunctionSpeed(env builtin.Environment, args []object.Object) object.Object {
speed := Robots[current].Speed
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionSpeed() Speed:%5.2f\n", current, Robots[current].Speed)
//}
return &object.NumberObject{Value: speed}
}
// Basic statement. DAMAGE returns the current damage of the robot
func FunctionDamage(env builtin.Environment, args []object.Object) object.Object {
damage := float64(Robots[current].Damage)
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionDamage() Damage:%d\n", current, Robots[current].Damage)
//}
return &object.NumberObject{Value: damage}
}
// Basic statement. DRIVE direction,speed sets speed and direction
func FunctionDrive(env builtin.Environment, args []object.Object) object.Object {
var s, d float64
if args[0].Type() == object.NUMBER {
d = args[0].(*object.NumberObject).Value
}
if args[1].Type() == object.NUMBER {
s = args[1].(*object.NumberObject).Value
}
if s < 0.0 {
s = 0.0
}
if s > 100.0 {
s = 100.0
}
d = math.Mod(d, 360.0)
if d < 0.0 {
d += 360.0
}
if d > 360.0 {
d -= 360.0
}
Robots[current].SpeedWanted = s
Robots[current].HeadingWanted = d
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionDrive heading:%5.2f speedwanted:%5.2f\n", current, Robots[current].HeadingWanted, Robots[current].SpeedWanted)
//}
return &object.NumberObject{Value: 0.0}
}
// Basic statement. SCAN direction,width. Scan the battlefield in direction with a width of +/- width
func FunctionScan(env builtin.Environment, args []object.Object) object.Object {
var angle, width float64
if args[0].Type() == object.NUMBER {
angle = args[0].(*object.NumberObject).Value
}
if args[1].Type() == object.NUMBER {
width = args[1].(*object.NumberObject).Value
}
angle = math.Mod(angle, 360.0)
if angle < 0 {
angle += 360.0
}
if angle > 360.0 {
angle -= 360
}
//if debug {
// fmt.Fprintf(os.Stderr, "SCAN angle: %f\n", angle)
//}
if width > 10.0 {
width = 10.0
}
if width < 2.0 {
width = 2.0
}
Robots[current].Scan = angle
Robots[current].Width = width
checkAlive(current)
if Robots[current].Status == DEAD {
return &object.NumberObject{Value: 0.0}
}
x1 := Robots[current].X
y1 := Robots[current].Y
td := 1000.0
for i := 0; i < numberOfRobots; i++ {
// fmt.Println("i=", i, numberOfRobots)
checkAlive(i)
if Robots[i].Status == DEAD {
continue
}
if i == current {
continue
}
if teams {
if current == 0 {
if i == 1 {
continue
}
}
if current == 1 {
if i == 0 {
continue
}
}
if current == 2 {
if i == 3 {
continue
}
}
if current == 3 {
if i == 2 {
continue
}
}
}
x2 := Robots[i].X
y2 := Robots[i].Y
t := Scanner(angle, width, x1, y1, x2, y2)
if t != 0.0 {
if t < td {
td = t
}
}
}
if td == 1000 { // would be fun if the limit of the scan was 100 units longer than missile range. maybe.
td = 0
}
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionScan() angle:%5.2f width:%5.2f result:%5.2f\n", current, angle, width, td)
//}
return &object.NumberObject{Value: td}
}
// Basic statement. CANNON direction, range. Fire the cannon at angle and distance. Do nothing if no missiles available.
func FunctionCannon(env builtin.Environment, args []object.Object) object.Object {
var angle, rang float64
if Robots[current].Reload != 0 {
return &object.NumberObject{Value: 1.0}
}
if args[0].Type() == object.NUMBER {
angle = args[0].(*object.NumberObject).Value
}
if args[1].Type() == object.NUMBER {
rang = args[1].(*object.NumberObject).Value
}
if rang < 1 {
// do nothing
return &object.NumberObject{Value: 0.0}
}
if rang > MISSLERANGE {
rang = MISSLERANGE
}
angle = math.Mod(angle, 360.0)
if angle < 0 {
angle += 360.0
}
if angle > 360.0 {
angle -= 360.0
}
for m := 0; m < MAXMISSILES; m++ {
if Missiles[current][m].Status == AVAILABLE && Missiles[current][m].Reload == 0 {
Missiles[current][m].Status = FLYING
Missiles[current][m].Distance = rang
Missiles[current][m].Heading = angle
Missiles[current][m].XOrigin = Robots[current].X
Missiles[current][m].YOrigin = Robots[current].Y
Missiles[current][m].XO = Robots[current].X
Missiles[current][m].YO = Robots[current].Y
Missiles[current][m].X = Robots[current].X
Missiles[current][m].Y = Robots[current].Y
//if debug {
// fmt.Fprintf(os.Stderr, "Robot:%d FunctionCannon() missile:%d status:%d angle:%5.2f range:%5.2f OX:%5.2f OY:%5.2f\n",
// current, m, Missiles[current][m].Status, angle, rang, Missiles[current][m].XOrigin, Missiles[current][m].YOrigin)
//}
Robots[current].Reload = ROBOTRELOAD
break
}
}
return &object.NumberObject{Value: 1.0}
}
func FunctionOut(env builtin.Environment, args []object.Object) object.Object {
var outmessage string
if args[0].Type() == object.STRING {
outmessage = args[0].(*object.StringObject).Value
}
if current == 0 || current == 1 {
select {
case team1 <- outmessage:
default:
}
} else {
select {
case team2 <- outmessage:
default:
}
}
return &object.NumberObject{Value: 0.0}
}
func FunctionIn(env builtin.Environment, args []object.Object) object.Object {
var msg string
if current == 0 || current == 1 {
select {
case msg = <-team1:
// fmt.Println("received message", msg)
default:
}
} else {
select {
case msg = <-team2:
// fmt.Println("received message", msg)
default:
}
}
return &object.StringObject{Value: msg}
}
// STR converts a number to a string
func FunctionSTRC(env builtin.Environment, args []object.Object) object.Object {
var num, c int
// Error?
if args[0].Type() == object.ERROR {
return args[0]
}
// Already a string?
if args[0].Type() == object.STRING {
return args[0]
}
if args[0].Type() == object.NUMBER {
i := args[0].(*object.NumberObject).Value
num = int(i)
}
if args[1].Type() == object.NUMBER {
i := args[1].(*object.NumberObject).Value
c = int(i)
}
// Get the value
s := fmt.Sprintf("%%%d%d", c, num)
return &object.StringObject{Value: s}
}