forked from kivutar/permadev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
139 lines (117 loc) · 3.11 KB
/
ai.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
from random import randint
import glo
dirs = [
(-1,-1),
(-1, 0),
(-1, 1),
( 0,-1),
( 0, 0),
( 0, 1),
( 1,-1),
( 1, 0),
( 1, 1),
]
# picks a random direction around the entity
def random_dir():
return dirs[randint(0, 8)]
# check if an item is on the same tile as the entity
def simple_sensor(self):
self.busy += 5
self.energy -= 1
if self.energy <= 0:
self.log.append("SENSOR FAILED: OUT OF ENERGY")
return None
for item in glo.items:
if item.x == self.x and item.y == self.y:
return item
return None
def sonar(self):
self.busy += 5
self.energy -= 2
if self.energy <= 0:
self.log.append("SONAR FAILED: OUT OF ENERGY")
return None
for d1 in dirs:
glo.game_map.tiles[self.x+d1[0]][self.y+d1[1]].explored = True
if not glo.game_map.tiles[self.x+d1[0]][self.y+d1[1]].block_sight:
for d2 in dirs:
glo.game_map.tiles[self.x+d1[0]+d2[0]][self.y+d1[1]+d2[0]].explored = True
if not glo.game_map.tiles[self.x+d1[0]+d2[0]][self.y+d1[1]+d2[0]].block_sight:
for d3 in dirs:
glo.game_map.tiles[self.x+d1[0]+d2[0]+d3[0]][self.y+d1[1]+d2[0]+d3[0]].explored = True
return None
def wall_sensor(self):
self.busy += 5
self.energy -= 1
if self.energy <= 0:
self.log.append("SENSOR FAILED: OUT OF ENERGY")
return None
if self.x == 1 or self.y == 1 or self.x == glo.game_map.width-1 or self.y == glo.game_map.height-1:
self.log.append("SENSOR FAILED: OUT OF MAP")
return None
for d in dirs:
if glo.game_map.tiles[self.x+d[0]][self.y+d[1]].block_sight:
return (self.x+d[0], self.y+d[1])
return None
def rand_move(self):
if self.char == 'W':
self.busy += 5
else:
self.busy += 15
self.energy -= 2
if self.energy <= 0:
self.log.append("MOVE FAILED: OUT OF ENERGY")
return None
d = random_dir()
if not glo.game_map.is_blocked(self.x + d[0], self.y + d[1]):
self.move(d[0], d[1])
self.log.append("SIMPLE MOVE TO %s %s" % d)
else:
self.log.append("FAILED MOVE TO %s %s" % d)
def simple_pick(self, item):
self.busy += 10
self.energy -= 4
if self.energy <= 0:
self.log.append("PICK FAILED: OUT OF ENERGY")
return None
if item.x == self.x and item.y == self.y:
if len(self.items) < self.item_capacity:
glo.items.remove(item)
self.items.append(item)
self.log.append("SIMPLE PICK")
return True
else:
self.log.append("FAILED PICK: CAPACITY REACHED")
else:
self.log.append("FAILED PICK: OBJECT NOT FOUND")
return False
def dig(self, x, y):
self.busy += 20
self.energy -= 50
if self.energy <= 0:
self.log.append("DIG FAILED: OUT OF ENERGY")
return None
if glo.game_map.tiles[x][y].blocked:
glo.game_map.tiles[x][y].blocked = False
glo.game_map.tiles[x][y].block_sight = False
glo.game_map.tiles[x][y].explored = True
self.log.append("SIMPLE DIG")
return True
else:
self.log.append("FAILED DIG: WALL NOT FOUND")
return False
def memorize_location(self, x, y):
self.locations.append((x, y))
wanderer_text = """rand_move(self)
sonar(self)
"""
gatherer_text = """item = simple_sensor(self)
if item:
simple_pick(self, item)
rand_move(self)
"""
miner_text = """wall = wall_sensor(self)
if wall != None:
dig(self, wall[0], wall[1])
rand_move(self)
"""