@@ -18,12 +18,20 @@ const Debug = false
18
18
var BitmapScale int
19
19
var scanBuffer []RobotDriverProtocol.ScanResponse
20
20
var finishedMapping = false
21
+ var CurrentlyMapping = true
21
22
22
23
// RobotMap is the current environment being mapped
23
24
var firstScan = true
24
25
var checkLocation = false
25
26
var followingPath = false
26
27
28
+ var compassOffset = - 1
29
+ var compassHeading = 0
30
+ var waitingForHeading = false
31
+
32
+ var distanceSinceLastScan = 0
33
+ var minimumScanDistance = 50
34
+
27
35
var pathIndex = 0
28
36
29
37
// RobotMap is a map of the current room being mapped
@@ -57,10 +65,6 @@ func MapInit(bitmapScale int) {
57
65
fmt .Println ("[RDP Link Ready]" )
58
66
59
67
scanBuffer = make ([]RobotDriverProtocol.ScanResponse , 0 )
60
-
61
- defer RobotMap .Print (nil )
62
-
63
- RobotDriverProtocol .Scan ()
64
68
}
65
69
66
70
// GetSeenMap returns the seen map.
@@ -78,11 +82,23 @@ func (this *Map) GetRobot() *Robot {
78
82
return & this .robot
79
83
}
80
84
85
+ func UpdateCompassHeading (angle int ) {
86
+ if compassOffset == - 1 {
87
+ compassOffset = angle
88
+ }
89
+ compassHeading = (angle - compassOffset + 360 ) % 360
90
+ waitingForHeading = false
91
+ }
92
+
81
93
// FollowingPath Returns the followingPath boolean
82
94
func FollowingPath () bool {
83
95
return followingPath
84
96
}
85
97
98
+ func StopFollowingPath () {
99
+ followingPath = false
100
+ }
101
+
86
102
// CreateMap creates an empty 1x1 map (this single point will be the robots starting position.)
87
103
func CreateMap () (createdMap Map ) {
88
104
createdMap = Map {width : 1 , height : 1 , robot : Robot {0 , 0 , 0 }}
@@ -161,16 +177,14 @@ func (this *Map) addBufferToMap() {
161
177
}
162
178
163
179
// FindLocation finds and returns the location of the robot based on the current scan information and the previous map information
164
- func (this * Map ) FindLocation () (int , int , float64 ) {
165
- rotationAmmount := 30
166
- rotationJump := 1
180
+ func (this * Map ) FindLocation (areaToSearch , rotationAmmount , rotationJump int ) (int , int , float64 ) {
167
181
x , y := int (this .GetRobot ().GetX ()), int (this .GetRobot ().GetY ())
168
182
var count = - 999
169
183
var angle = int (this .GetRobot ().GetRotation ())
170
184
var currentRotation = int (this .GetRobot ().GetRotation ())
171
185
for i := - rotationAmmount ; i <= rotationAmmount ; i += rotationJump {
172
186
currentRotation = i + int (this .GetRobot ().GetRotation ())
173
- tempX , tempY , tempCount := this .findLocation (createMapFragment (scanBuffer , currentRotation ))
187
+ tempX , tempY , tempCount := this .findLocation (areaToSearch , createMapFragment (scanBuffer , currentRotation ))
174
188
if tempCount > count {
175
189
x = tempX
176
190
y = tempY
@@ -182,14 +196,22 @@ func (this *Map) FindLocation() (int, int, float64) {
182
196
return x , y , float64 (angle )
183
197
}
184
198
185
- func (this * Map ) findLocation (fragment Map ) (int , int , int ) {
199
+ func (this * Map ) findLocation (areaToSearch int , fragment Map ) (int , int , int ) {
186
200
fmt .Println ("Attempting to find location..." )
187
201
mX , mY , mCount := int (this .GetRobot ().GetX ()), int (this .GetRobot ().GetY ()), 0
188
202
189
203
fmt .Println ("Robots Assumed Location: (" , mX , "," , mY , ")" )
190
204
191
- for i := 0 ; i < this .width ; i ++ {
192
- for j := 0 ; j < this .height ; j ++ {
205
+ xSearch := int (this .GetRobot ().GetX ())
206
+ ySearch := int (this .GetRobot ().GetY ())
207
+
208
+ if areaToSearch == 0 {
209
+ xSearch = this .width
210
+ ySearch = this .height
211
+ }
212
+
213
+ for i := xSearch - areaToSearch ; i < xSearch + areaToSearch ; i ++ {
214
+ for j := ySearch - areaToSearch ; j < ySearch + areaToSearch ; j ++ {
193
215
if i >= 0 && j >= 0 && i < this .width && j < this .height {
194
216
count , _ , _ := this .probabilityAtLocation (fragment , int (i ), int (j ))
195
217
if count != 0 {
@@ -230,9 +252,20 @@ func (this *Map) probabilityAtLocation(fragment Map, x int, y int) (int, int, in
230
252
}
231
253
232
254
func (this * Map ) TakeNextStep (lastX int , lastY int ) {
255
+ // If we haven't scanned in a while: do a scan and return.
256
+ if distanceSinceLastScan > minimumScanDistance {
257
+ distanceSinceLastScan = 0
258
+ RobotDriverProtocol .Scan ()
259
+ return
260
+ }
261
+
262
+ // Otherwise just continue moving
233
263
if len (path ) != 0 {
264
+ followingPath = true
234
265
robotPoint := Point {int (this .GetRobot ().GetX ()), int (this .GetRobot ().GetY ())}
235
266
line , movesLeft := this .getNextMove (int (this .GetRobot ().GetX ()), int (this .GetRobot ().GetY ()), lastX , lastY , path )
267
+ distanceSinceLastScan += line .getLength ()
268
+ fmt .Println ("Current length of line: " , line .getLength ())
236
269
237
270
fmt .Println ("currentLocation: " , robotPoint )
238
271
// If you are 1 move away from end point
@@ -251,7 +284,8 @@ func (this *Map) TakeNextStep(lastX int, lastY int) {
251
284
252
285
degree := line .getAngleFrom (robotPoint )
253
286
magnitude := line .getLength ()
254
-
287
+ offset := RobotMap .GetRobot ().GetRotation ()
288
+ degree = (degree + int (offset )) % 360
255
289
fmt .Println ("[TakeNextStep]: Required Move: " , degree , " -> " , magnitude )
256
290
257
291
RobotDriverProtocol .Move (uint16 (degree ), uint32 (magnitude ))
@@ -269,6 +303,18 @@ func (this *Map) TakeNextStep(lastX int, lastY int) {
269
303
// Used when following a path into unseen areas to prevent crashes.
270
304
func (this * Map ) MoveRobotAlongPath (newPath [][]bool , stopBeforePoint bool ) {
271
305
path = newPath
306
+ lines := BitmapToVector (path )
307
+
308
+ for i := 0 ; i < len (lines ); i ++ {
309
+ if lines [i ].getLength () > minimumScanDistance {
310
+ line1 , line2 := lines [i ].split ()
311
+ lines [i ] = line1
312
+ lines = append (lines , line2 )
313
+ i --
314
+ }
315
+ }
316
+
317
+ lineMap = lines
272
318
followingPath = true
273
319
this .TakeNextStep (- 1 , - 1 )
274
320
fmt .Println ("Queued a path for movement..." )
@@ -286,18 +332,18 @@ func (this *Map) MoveRobotToPoint(x, y int) {
286
332
287
333
// Returns the next move in the path.
288
334
func (this * Map ) getNextMove (x , y , prevX , prevY int , path [][]bool ) (line Line , possibleMove bool ) {
289
- lines := BitmapToVector (path )
290
- fmt .Println ("Line Map: " , lines )
291
- fmt .Println ("Index: " , pathIndex , " | Size: " , len (lines ))
292
-
293
335
robotPoint := Point {x , y }
294
336
prevRobotPoint := Point {prevX , prevY }
295
337
296
- for _ , line := range lines {
297
- fmt .Println ("Did " , line , " get past here?" )
298
- fmt .Println ("Previous Loc: " , prevRobotPoint )
338
+ for _ , line := range lineMap {
339
+ if Debug {
340
+ fmt .Println ("Did " , line , " get past here?" )
341
+ fmt .Println ("Previous Loc: " , prevRobotPoint )
342
+ }
299
343
if ! line .pointOnLine (prevRobotPoint ) {
300
- fmt .Println ("\t " , line , " contains " , robotPoint , " => " , line .pointOnLine (robotPoint ))
344
+ if Debug {
345
+ fmt .Println ("\t " , line , " contains " , robotPoint , " => " , line .pointOnLine (robotPoint ))
346
+ }
301
347
if line .pointOnLine (robotPoint ) {
302
348
return line , true
303
349
}
@@ -477,11 +523,11 @@ func (this *Map) AddWallByLine(degree, distance float64) {
477
523
fmt .Println ("Adding Wall @" , x , y )
478
524
}
479
525
this .AddWall (x , y , true )
480
- this .MarkLineAsSeen (degree , distance )
526
+ this .MarkLineAsSeen (degree , distance , x , y )
481
527
}
482
528
483
529
// MarkLineAsSeen marks anything the line passes through as "seen".
484
- func (this * Map ) MarkLineAsSeen (degree , distance float64 ) {
530
+ func (this * Map ) MarkLineAsSeen (degree , distance float64 , wallX , wallY int ) {
485
531
if Debug {
486
532
fmt .Println (RobotMap )
487
533
RobotMap .Print (nil )
@@ -499,31 +545,25 @@ func (this *Map) MarkLineAsSeen(degree, distance float64) {
499
545
if this.seen [y ][x ] == 0 {
500
546
this.seen [y ][x ] = 1
501
547
}
548
+ if y != wallY && x != wallX {
549
+ this.floor [y ][x ] = false
550
+ }
502
551
}
503
552
}
504
553
}
505
554
506
555
// Counts the ammount of seen tiles (that are just empty space) surrounding the current tile (horizontaly and vertically)
507
556
func (this * Map ) getAdjacentSeenTilesCount (x , y int ) (count int ) {
508
557
count = 0
509
- if y + 1 < len (this .floor ) {
510
- if this .seen [y + 1 ][x ] == 1 {
511
- count ++
512
- }
513
- }
514
- if x + 1 < len (this .floor [y ]) {
515
- if this .seen [y ][x + 1 ] == 1 {
516
- count ++
517
- }
518
- }
519
- if y - 1 >= 0 {
520
- if this .seen [y - 1 ][x ] == 1 {
521
- count ++
522
- }
523
- }
524
- if x - 1 >= 0 {
525
- if this .seen [y ][x - 1 ] == 1 {
526
- count ++
558
+ for yOffset := - 1 ; yOffset <= 1 ; yOffset ++ {
559
+ for xOffset := - 1 ; xOffset <= 1 ; xOffset ++ {
560
+ if ! (yOffset == 0 && xOffset == 0 ) {
561
+ if y + yOffset >= 0 && x + xOffset >= 0 && y + yOffset < len (this .floor ) && x + xOffset < len (this .floor [0 ]) {
562
+ if this .seen [y + yOffset ][x + xOffset ] == 1 {
563
+ count ++
564
+ }
565
+ }
566
+ }
527
567
}
528
568
}
529
569
return
@@ -570,7 +610,6 @@ func (this *Map) ContinueToNextArea() {
570
610
this .MoveRobotAlongPath (path , true )
571
611
return
572
612
}
573
- print ("No valid path to node" , i , "checking next node." )
574
613
}
575
614
if ! possible {
576
615
finishedMapping = true
0 commit comments