-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_object.py
84 lines (64 loc) · 2.16 KB
/
mock_object.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
import math
class PathObject:
"""
Path points attract a point with degrading field upto
a certain cutoff. The field strength increases with
increasing index along the path.
"""
def __init__(self, points):
self.points = points
def poten_at(self, pos, slope=-2, cutoff=4):
"""
Calculate potential at point pos
Args:
pos: point to calculate potential at
slope: slope of degrading field
cutoff: maximum range of affect for field
Returns:
value: minimum potential value possible
"""
def score(path_point, i, pos):
dist = pos.dist(path_point)
if dist == 0:
return -math.inf
else:
return (i + 1) * ((1 / dist - 1 / cutoff) * (slope - i))
scores = [score(path_point, i, pos) for i, path_point in enumerate(self.points)]
return min(scores)
class WallObject:
"""
Wall objects behave as inert tiles, not allowing
any unit to step on them while not affecting any other tile
"""
def __init__(self, points):
self.points = points
def poten_at(self, pos):
"""
Inert function stepping on wall in not possible. No other
point is affected.
Args:
pos: point to calculate potential at
Returns:
value: inf or 0
"""
if pos in self.points:
return math.inf
else:
return 0
class EnemyObject:
"""
Enemy objects exert a attraction/repulsion based on the proximity/probability of enemies
Enemy objects can be initialized so that they can either attract or repel based on multiplier (default:repel)
"""
def __init__(self, pheromone, multiplier=1):
self.pheromone = pheromone
self.multiplier = multiplier
def poten_at(self, pos):
"""
Get enemy proximity
Args:
pos: point to calculate potential at
Returns:
value: potential due to enemies
"""
return (self.pheromone.map[pos.y][pos.x] + self.pheromone.obstacleData[pos.y][pos.x])*self.multiplier