-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.py
559 lines (448 loc) · 18.3 KB
/
map.py
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env python
"""
map holds a model of the map, where the data of the NXT is finally stored
"""
from mcc import utils
import threading
POINT_FREE = 0
POINT_TARGET = 1
POINT_DODGE_LEFT = 2
POINT_DODGE_CENTER = 3
POINT_DODGE_RIGHT = 4
class MapModel(object):
"""
Class for the MapModel. Contains the reference to the first map section
which contains the position of the NAO.
"""
def __init__(self, name):
"""
Creates a map model
:return: 'MapModel' with an initial map section ('first_map_section')
and a target position ('target_position')
:rtype: MapModel
"""
self.__first_map_section = MapSection()
self.__target_position = None
self.name = name
self._lock = threading.Lock()
self.__max_free_count = 0
self.__max_dodge_count = 0
# self._expand [top,right,bottom,left]
self._expand = [0, 0, 0, 0]
self._need_redraw = False
#PROPERTY --- need_redraw
def fget_need_redraw(self):
"""The need_redraw property getter"""
with self._lock:
return self._need_redraw
def fset_need_redraw(self, value):
"""The need_redraw property setter"""
with self._lock:
self._need_redraw = value
need_redraw = property(fget_need_redraw, fset_need_redraw)
#PROPERTY --- expand
def fget_expand(self):
"""The expand property getter"""
with self._lock:
return self._expand
def fset_expand(self, value):
"""The expand property setter"""
with self._lock:
self._expand = value
expand = property(fget_expand, fset_expand)
#PROPERTY --- max_free_count
def fget__max_free_count(self):
"""The max_free_count property getter"""
with self._lock:
return self.__max_free_count
def fset__max_free_count(self, value):
"""The max_free_count property setter"""
with self._lock:
self.__max_free_count = value
max_free_count = property(fget__max_free_count, fset__max_free_count)
#PROPERTY --- max_dodge_count
def fget_max_dodge_count(self):
"""The max_dodge_count property getter"""
with self._lock:
return self.__max_dodge_count
def fset_max_dodge_count(self, value):
"""The max_dodge_count property setter"""
with self._lock:
self.__max_dodge_count = value
max_dodge_count = property(fget_max_dodge_count, fset_max_dodge_count)
def get_first_map_section(self):
"""
Getter method for first_map_section
:return: first map section in the map model
:rtype: MapSection
"""
with self._lock:
return self.__first_map_section
def set_first_map_section(self, map_section):
"""
Setter method for first_map_section
:param map_section: map section to replace the current
:type map_section: MapSection
:raises TypeError: If the type of the arguments is not MapSection
"""
if type(map_section) != type(MapSection()):
raise TypeError("Type \"MapSection\" excepted,\
but", type(map_section), " given.")
with self._lock:
self.__first_map_section = map_section
first_map_section = property(get_first_map_section, set_first_map_section)
def get_target_position(self):
"""
Getter method for target_position
:return: position of the target in the map model
:rtype: Point
"""
with self._lock:
return self.__target_position
def set_target_position(self, target_position):
"""
Setter method for target_position
:param target_position: point to replace the current target position
:type target_position: Point
:raises TypeError: If the type of the arguments is not Point
"""
if type(target_position) != type(utils.Point(0, 0, 0)):
raise TypeError("Type \"Point\" excepted, but",
type(target_position), " given.")
with self._lock:
self.__target_position = target_position
target_position = property(get_target_position, set_target_position)
def _add_map_section(self, offset_x, offset_y):
"""
Adds a map section at the position stated with the offset.
If there are free spaces (no grids) in the way to the position,
where the new grid will be placed, they are automatically created.
:param offset_x: identifies the steps on the x-axis
:type offset_x: int
:param offset_y: identifies the steps on the y-axis
:type offset_y: int
:raises TypeError: If the type of the arguments is not integer
:raises ValueError: If the offset is (0,0) or already occupied
"""
if (type(offset_x) != type(1)) or (type(offset_y) != type(1)):
raise TypeError("Type \"int\" excepted, but", type(offset_x),
", ", type(offset_y), " given.")
if offset_x == 0 and offset_y == 0:
raise ValueError("Can't add MapSection at position (0,0).")
tmp_map_section = self.__first_map_section
if offset_x > 0:
for _ in range(0, offset_x):
if tmp_map_section.right_grid is None:
self._expand[1] += 1
self._need_redraw = True
tmp_map_section.right_grid = MapSection()
tmp_map_section = tmp_map_section.right_grid
elif offset_x < 0:
for _ in range(0, offset_x, -1):
if tmp_map_section.left_grid is None:
self._expand[3] += 1
self._need_redraw = True
tmp_map_section.left_grid = MapSection()
tmp_map_section = tmp_map_section.left_grid
if offset_y > 0:
for _ in range(0, offset_y):
if tmp_map_section.top_grid is None:
self._expand[0] += 1
self._need_redraw = True
tmp_map_section.top_grid = MapSection()
tmp_map_section = tmp_map_section.top_grid
elif offset_y < 0:
for _ in range(0, offset_y, -1):
if tmp_map_section.bottom_grid is None:
self._expand[2] += 1
self._need_redraw = True
tmp_map_section.bottom_grid = MapSection()
tmp_map_section = tmp_map_section.bottom_grid
if (offset_x > 0 and tmp_map_section.right_grid is not None) or\
(offset_x < 0 and tmp_map_section.left_grid is not None) or\
(offset_y > 0 and tmp_map_section.top_grid is not None) or\
(offset_y < 0 and tmp_map_section.bottom_grid is not None):
raise ValueError("Can't add MapSection with this offset - \
position is occupied.")
def expand_map(self, direction):
"""
0 - top
1 - left
2 - bottom
3 - right
"""
if direction == 0:
with self._lock:
self._add_map_section(0, self.expand[0] + 1)
elif direction == 1:
with self._lock:
self._add_map_section(self.expand[1] + 1, 0)
elif direction == 2:
with self._lock:
self._add_map_section(0, (self.expand[2] + 1) * -1)
elif direction == 3:
with self._lock:
self._add_map_section((self.expand[3] + 1) * -1, 0)
def get_point(self, x_coord, y_coord):
"""
Returns the value of a given point and creates a map section, if
necessary
:param x_coord: the x-coordinate of which the value shall be returned
:type x_coord: int
:param y_coord: the y-coordinate of which the value shall be returned
:type y_coord: int
:return: the value of the point
:rtype: int
"""
if type(x_coord) != type(1) or type(y_coord) != type(1):
raise TypeError("Type \"int\" excepted, but", type(x_coord), ", ",
type(y_coord), " given.")
offset_x = x_coord / MapSection.get_grid_width()
offset_y = y_coord / MapSection.get_grid_height()
x_abs = x_coord % MapSection.get_grid_width()
y_abs = y_coord % MapSection.get_grid_height()
# make sure that the grid exists; pass if it already does
try:
with self._lock:
self._add_map_section(offset_x, offset_y)
except ValueError:
pass
tmp_map_section = self.__first_map_section
if offset_x > 0:
for _ in range(0, offset_x):
tmp_map_section = tmp_map_section.right_grid
elif offset_x < 0:
for _ in range(0, offset_x, -1):
tmp_map_section = tmp_map_section.left_grid
if offset_y > 0:
for _ in range(0, offset_y):
tmp_map_section = tmp_map_section.top_grid
elif offset_y < 0:
for _ in range(0, offset_y, -1):
tmp_map_section = tmp_map_section.bottom_grid
with self._lock:
return tmp_map_section.get_point_value(x_abs, y_abs)
def _set_point(self, x_coord, y_coord, option=0):
"""
Increases the value of a given point or sets a dodge point
and creates a map section, if necessary
:param x_coord: the x-coordinate of which the value shall be increased
:type x_coord: int
:param y_coord: the y-coordinate of which the value shall be increased
:type y_coord: int
:param option: 0: free point, 1: dodge point
:type option: int
"""
if type(x_coord) != type(1) or type(y_coord) != type(1) or \
type(option) != type(1):
raise TypeError("Type \"int\" excepted, but", type(x_coord), ", ",
type(y_coord), ", ", type(option), " given.")
offset_x = x_coord / MapSection.get_grid_width()
offset_y = y_coord / MapSection.get_grid_height()
x_abs = x_coord % MapSection.get_grid_width()
y_abs = y_coord % MapSection.get_grid_height()
# make sure that the grid exists; pass if it already does
try:
self._add_map_section(offset_x, offset_y)
except ValueError:
pass
tmp_map_section = self.__first_map_section
if offset_x > 0:
for _ in range(0, offset_x):
tmp_map_section = tmp_map_section.right_grid
elif offset_x < 0:
for _ in range(0, offset_x, -1):
tmp_map_section = tmp_map_section.left_grid
if offset_y > 0:
for _ in range(0, offset_y):
tmp_map_section = tmp_map_section.top_grid
elif offset_y < 0:
for _ in range(0, offset_y, -1):
tmp_map_section = tmp_map_section.bottom_grid
if option == 0:
count = tmp_map_section.get_point_value(x_abs, y_abs)
if count >= self.__max_free_count:
self.__max_free_count = count + 1
tmp_map_section.update_grid([[x_abs, y_abs]])
elif option == 1 and tmp_map_section.get_point_value(x_abs, y_abs, 1) != 1:
count = tmp_map_section.get_point_value(x_abs, y_abs, 1)
if count >= self.__max_dodge_count:
self.__max_dodge_count = count + 1
tmp_map_section.update_grid([[x_abs, y_abs]], 1)
def increase_points(self, points):
"""
Increases the values of the given points and creates a map section, if
necessary
:param points: a list of points of which the value shall be increased
:type points: list
"""
if type(points) != type([]):
raise TypeError("Type \"list\" excepted, but", type(points), ", ",
" given.")
for p in points:
with self._lock:
self._set_point(p[0], p[1])
class MapSection(object):
"""
Class for the MapSection. Contains the references to the bordering grids
and the grid which is defined by 'grid_height' and 'grid_width'
Grid holds [free, dodge] count
"""
__grid_height = 100
__grid_width = 100
def __init__(self):
"""
Creates a map section
:return: 'MapSection' right -, left-, top- and bottom-grid
which are initially all None
:rtype: MapSection
"""
self.__right_grid = None
self.__left_grid = None
self.__top_grid = None
self.__bottom_grid = None
self.__grid = []
# initialize grid with zeros
for i in range(0, MapSection.__grid_height):
self.__grid.append([])
for _ in range(0, MapSection.__grid_width):
self.__grid[i].append([0, 0])
def get_right_grid(self):
"""
Getter method for right_grid
:return: the grid which is concatenated at the right of this grid
:rtype: MapSection
"""
return self.__right_grid
def set_right_grid(self, right_grid):
"""
Setter method for right_grid
:param right_grid: grid to replace the reference to the current
right grid
:type right_grid: MapSection
:raises TypeError: If the type of the arguments is not MapSection
"""
if type(right_grid) != type(MapSection()):
raise TypeError("Type \"MapSection\" excepted, but",
type(right_grid), " given.")
self.__right_grid = right_grid
right_grid = property(get_right_grid, set_right_grid)
def get_left_grid(self):
"""
Getter method for left_grid
:return: the grid which is concatenated at the left of this grid
:rtype: MapSection
"""
return self.__left_grid
def set_left_grid(self, left_grid):
"""
Setter method for left_grid
:param left_grid: grid to replace the reference to the current
left grid
:type left_grid: MapSection
:raises TypeError: If the type of the arguments is not MapSection
"""
if type(left_grid) != type(MapSection()):
raise TypeError("Type \"MapSection\" excepted, but",
type(left_grid), " given.")
self.__left_grid = left_grid
left_grid = property(get_left_grid, set_left_grid)
def get_top_grid(self):
"""
Getter method for top_grid
:return: the grid which is concatenated at the top of this grid
:rtype: MapSection
"""
return self.__top_grid
def set_top_grid(self, top_grid):
"""
Setter method for top_grid
:param top_grid: grid to replace the reference to the current
top grid
:type top_grid: MapSection
:raises TypeError: If the type of the arguments is not MapSection
"""
if type(top_grid) != type(MapSection()):
raise TypeError("Type \"MapSection\" excepted, but",
type(top_grid), " given.")
self.__top_grid = top_grid
top_grid = property(get_top_grid, set_top_grid)
def get_bottom_grid(self):
"""
Getter method for bottom_grid
:return: the grid which is concatenated at the bottom of this grid
:rtype: MapSection
"""
return self.__bottom_grid
def set_bottom_grid(self, bottom_grid):
"""
Setter method for bottom_grid
:param bottom_grid: grid to replace the reference to the current
bottom grid
:type bottom_grid: MapSection
:raises TypeError: If the type of the arguments is not MapSection
"""
if type(bottom_grid) != type(MapSection()):
raise TypeError("Type \"MapSection\" excepted, but",
type(bottom_grid), " given.")
self.__bottom_grid = bottom_grid
bottom_grid = property(get_bottom_grid, set_bottom_grid)
def get_grid_height():
"""
Getter method for grid_height
:return: the grids height
:rtype: int
"""
return MapSection.__grid_height
get_grid_height = staticmethod(get_grid_height)
def get_grid_width():
"""
Getter method for grid_width
:return: the grids width
:rtype: int
"""
return MapSection.__grid_width
get_grid_width = staticmethod(get_grid_width)
def get_point_value(self, x_coord, y_coord, option=0):
"""
:param x_coord: x_coord where the value shall be retrieved of
:type x_coord: int
:param y_coord: y_coord where the value shall be retrieved of
:type y_coord: int
:param option: 0: Point value; 1: Dodge value
:type option: int
:raises TypeError: If the type of the arguments is not int
"""
if type(x_coord) != type(1) or type(y_coord) != type(1) or type(option) != type(1):
raise TypeError("Type \"int\" excepted, but", type(x_coord),
", ", type(y_coord), ", ", type(option), " given.")
if option == 0:
return self.__grid[x_coord][y_coord][0]
else:
return self.__grid[x_coord][y_coord][1]
def update_grid(self, points, option=0):
"""
Updates the grid by increasing the values of the given points
or marking / deleting a dodge
:param points: points that shall be updated
:type points: list
:param option: 0: Point values; 1: Dodge values
:type option: int
:raises TypeError: If the type of the arguments is not a list of
integer tuple and an int
"""
if type(points) != type([]):
raise TypeError("Type \"list\", \"int\" excepted, but", type(points),
", ", type(option), " given.")
for i in range(0, len(points)):
if len(points[i]) != 2:
raise TypeError("Wrong format!")
if not option:
for i in range(0, len(points)):
self.__grid[points[i][0]][points[i][1]][0] += 1
else:
for i in range(0, len(points)):
if not self.__grid[points[i][0]][points[i][1]][1]:
self.__grid[points[i][0]][points[i][1]][1] = 1
else:
self.__grid[points[i][0]][points[i][1]][1] = 0