-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai.py
165 lines (130 loc) · 4.86 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
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
#!/usr/bin/env python
#-*- coding: utf8 -*-
import math
import random
import logging
from pathfinding.finder.a_star import AStarFinder
from pathfinding.core.diagonal_movement import DiagonalMovement
from locals import *
class Random (object):
'''
The Random AI would just do some thing at random at predefined
chance of happening. It may fire, move forward or turn at any
direction.
'''
fire_chance = 1
moving_chance = 70
turning_chance = 5
turns_list = [
DIRECTION_UP,
DIRECTION_DOWN,
DIRECTION_LEFT,
DIRECTION_RIGHT,
]
def __init__(self, world):
self.world = world
def do_random_thing(self, enemy):
firing_roll = random.randint(0, 100)
if firing_roll < self.fire_chance:
enemy.fire()
turning_roll = random.randint(0, 100)
if turning_roll <= self.turning_chance:
turn_index = random.randint(0, 3)
turn_direction = self.turns_list[turn_index]
enemy.move(turn_direction)
return
moving_roll = random.randint(0, 100)
if moving_roll <= self.moving_chance:
enemy.move(enemy.facing)
else:
enemy.stop()
def tick(self, deltat, enemies):
for enemy in enemies:
self.do_random_thing(enemy)
class ZombieDriver (Random):
'''
The ZombieDriver AI would follow the player relentlessly around without any
thought or self preservation mechanism!
'''
fire_chance = 3
moving_chance = 80
turning_chance = 5
def __init__(self, world):
self.world = world
self.map = world.map
self.pathfinder = AStarFinder(
diagonal_movement=DiagonalMovement.never,
time_limit=0.006
)
self.path_cache_duration = 1000
def tick(self, deltat, enemies):
interesting_objects = []
for player in self.world.players:
if player.tank is None:
continue
interesting_objects.append((player.tank.rect, 1))
for flag in self.world.un_flags:
interesting_objects.append((flag.rect, 1.5))
for enemy in enemies:
self.process_enemy(enemy, deltat, interesting_objects)
self.fire_at_will(enemy, deltat, interesting_objects)
def process_enemy(self, enemy, deltat, interesting_objects):
enemy.update_path_duration(deltat)
own_coords = enemy.rect
own_grid_pos = self.map.world_to_grid_coords(own_coords.centerx, own_coords.centery)
if enemy.path_duration <= self.path_cache_duration:
self.continue_path(enemy, own_grid_pos)
return
heat_obj = None
heat_weight = 0
for obj, weight in interesting_objects:
dist = math.sqrt(
abs(own_coords.centerx - obj.centerx) ** 2 +
abs(own_coords.centery - obj.centery) ** 2
)
obj_weight = (1 / dist) * weight
if obj_weight > heat_weight:
heat_weight = obj_weight
heat_obj = obj
if heat_obj is None:
logging.error("ZombieDriver AI finds nothing interesting on the map! How can that be!?")
self.do_random_thing(enemy)
return
hobjx, hobjy = heat_obj.centerx, heat_obj.centery
hobj_grid_pos = self.map.world_to_grid_coords(hobjx, hobjy)
if hobj_grid_pos == enemy.current_target:
enemy.reset_path_duration()
self.continue_path(enemy, own_grid_pos)
return
# WTF!? You have to rebiuld the *whole* grid every time find_path is called!
# Definately change to a better library or write A* myself.
self.map.build_grid()
pf_start = self.map.grid.node(*own_grid_pos)
pf_end = self.map.grid.node(*hobj_grid_pos)
path, runs = self.pathfinder.find_path(
pf_start,
pf_end,
self.map.grid
)
enemy.set_current_path(path)
self.continue_path(enemy, own_grid_pos)
def fire_at_will(self, enemy, deltat, interesting_objects):
if len(enemy.bullets) >= enemy.max_bullets:
return
for obj, weight in interesting_objects:
if not enemy.is_facing(self.map, obj):
continue
firing_roll = random.randint(0, 100)
if firing_roll < self.fire_chance:
enemy.fire()
def continue_path(self, enemy, grid_pos):
next_node = enemy.get_path_next(grid_pos)
if next_node is None:
self.do_random_thing(enemy)
return
direction = self.map.grid_direction(grid_pos, next_node)
if direction == DIRECTION_NONE:
logging.error("For some reason the calculate direction returned NONE after pathfinding")
self.do_random_thing(enemy)
else:
enemy.move(direction)