-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic_demo.py
68 lines (64 loc) · 2.56 KB
/
logic_demo.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
from sys import stderr
import time
import numpy as np
from agent.position import Position
from utils import *
from agent.agent import Agent
def logic(agent: Agent):
print(f"Start demo logic", flush=True)
from pathfinding.core.grid import Grid
from pathfinding.finder.best_first import BestFirst
from pathfinding.finder.breadth_first import BreadthFirstFinder
while not agent.is_ready():
time.sleep(0.1)
agent._update()
origin = None
for supply in agent.supplies:
if supply.kind == 'PREMIUM_ARMOR':
origin = (supply.position.x, supply.position.y)
break
if not origin:
for supply in agent.supplies:
if supply.kind == 'FIRST_AID':
origin = (supply.position.x, supply.position.y)
break
if not origin:
for supply in agent.supplies:
if supply.kind == 'PRIMARY_ARMOR':
origin = (supply.position.x, supply.position.y)
break
if origin:
agent.choose_origin(Position[float](*origin))
print(f"Choose origin at {origin}", flush=True)
while not agent.terminated:
agent._update()
time.sleep(0.01)
player = agent.player
enemy = agent.enemy
# print(f"Tick: {agent.ticks}", file=stderr, flush=True)
ppos_f = player.position.x, player.position.y
ppos = int(ppos_f[0]), int(ppos_f[1])
opos_f = enemy.position.x, enemy.position.y
opos = int(opos_f[0]), int(opos_f[1])
dist_op = dist(ppos_f, opos_f)
# print(f"Supply Position: {agent.supplies[0].position}", flush=True)
print(f"Player position: {player.position} health: {player.health}", flush=True)
print(f"Enemy position: {enemy.position} health: {enemy.health}", flush=True)
if any(ppos == (supply.position.x, supply.position.y) for supply in agent.supplies):
agent.pick_up("PREMIUM_ARMOR", 1)
agent.pick_up("PRIMARY_ARMOR", 1)
agent.pick_up("FIRST_AID", 1)
if dist_op <= 2:
agent.attack(Position[float](*opos_f))
if dist_op > 0.5:
matrix = convert_map(agent.map).T.astype(int)
grid = Grid(matrix=matrix)
finder = BestFirst()
start = grid.node(*ppos)
end = grid.node(*opos)
path, runs = finder.find_path(start, end, grid)
if len(path) > 1:
nx, ny = path[1]
agent.move(position=Position[float](nx+0.5, ny+0.5))
else:
print(f"Path: {path}", flush=True)