Improve ai

This commit is contained in:
PAlexanderFranklin 2023-05-18 02:40:51 -07:00
parent 0add18a31d
commit de08303bb0
2 changed files with 54 additions and 44 deletions

View File

@ -60,8 +60,8 @@ class Enemy:
self.AITime -= 1 self.AITime -= 1
if self.AITime < 1: if self.AITime < 1:
try: try:
closestPlayer = findClosestPlayer(self.x, self.y) diff = findClosestPlayer(self.x, self.y)["difference"]
weightsx = [-closestPlayer["difference"][0], abs(closestPlayer["difference"][1]), closestPlayer["difference"][0]] weightsx = [-diff[0], abs(diff[1]), diff[0]]
if weightsx[0] < 0: if weightsx[0] < 0:
weightsx[0] = weightsx[2]/6 weightsx[0] = weightsx[2]/6
elif weightsx[2] < 0: elif weightsx[2] < 0:
@ -69,7 +69,7 @@ class Enemy:
else: else:
weightsx[0] = weightsx[1]/8 weightsx[0] = weightsx[1]/8
weightsx[2] = weightsx[1]/8 weightsx[2] = weightsx[1]/8
weightsy = [-closestPlayer["difference"][1], abs(closestPlayer["difference"][0]), closestPlayer["difference"][1]] weightsy = [-diff[1], abs(diff[0]), diff[1]]
if weightsy[0] < 0: if weightsy[0] < 0:
weightsy[0] = weightsy[2]/6 weightsy[0] = weightsy[2]/6
elif weightsy[2] < 0: elif weightsy[2] < 0:
@ -95,22 +95,26 @@ class Smart(Enemy):
self.AITime -= 1 self.AITime -= 1
if self.AITime < 1: if self.AITime < 1:
try: try:
if self.trackedPlayer and self.trackedPlayer.alive:
fastDir = self.trackedPlayer.pathMap[self.x][self.y][2]
# movementx = random.choices([-1, 0, 1], weightsx)[0]
# movementy = random.choices([-1, 0, 1], weightsy)[0]
movementx = fastDir[0]
movementy = fastDir[1]
# self.move(movementx, movementy)
self.trackingCounter -= 1
if self.trackingCounter < 1: if self.trackingCounter < 1:
self.AITime = random.randint(500, 1000) self.AITime = random.randint(150, 250)
self.trackingCounter = 9
self.trackedPlayer = False self.trackedPlayer = False
elif self.trackedPlayer and self.trackedPlayer["player"].alive:
pathMap = self.trackedPlayer["player"].pathMap
weightsx = [1,1,1]
weightsy = [1,1,1]
if pathMap[self.x][self.y]:
fastDir = pathMap[self.x][self.y][2]
weightsx[1 + fastDir[0]] = 30
weightsy[1 + fastDir[1]] = 30
movementx = random.choices([-1, 0, 1], weightsx)[0]
movementy = random.choices([-1, 0, 1], weightsy)[0]
self.move(movementx, movementy)
self.trackingCounter -= 1
self.AITime = random.randint(10, 15) self.AITime = random.randint(10, 15)
else: else:
closestPlayer = findClosestPlayer(self.x, self.y) closestPlayer = findClosestPlayer(self.x, self.y)
self.trackedPlayer = closestPlayer["player"] self.trackedPlayer = closestPlayer
self.trackingCounter = 8
self.AITime = random.randint(10, 15) self.AITime = random.randint(10, 15)
except Exception as error: except Exception as error:
print(error) pass

View File

@ -5,6 +5,7 @@ from globals import *
from blocks import * from blocks import *
def buildPathMap(x, y): def buildPathMap(x, y):
try:
pathMap = [] pathMap = []
for i in range(tileCountx): for i in range(tileCountx):
column = [] column = []
@ -29,6 +30,9 @@ def buildPathMap(x, y):
currentTiles = list(newTiles) currentTiles = list(newTiles)
newTiles = set() newTiles = set()
return pathMap return pathMap
except Exception as error:
print(error)
raise error
class Player: class Player:
def __init__(self, id, x, y, sprite, keys): def __init__(self, id, x, y, sprite, keys):
@ -70,6 +74,8 @@ class Player:
self.pull = True self.pull = True
else: else:
self.pull = False self.pull = False
if keys[pygame.K_o]:
print(self.pathMap)
for command in self.commands: for command in self.commands:
try: try:
if keys[command[0]]: if keys[command[0]]:
@ -100,16 +106,16 @@ class Player:
self.x += x self.x += x
self.y += y self.y += y
gameMap[self.x][self.y] = self gameMap[self.x][self.y] = self
for id, player in players.items():
try:
player.buildOwnPathMap()
except Exception as error:
print(error)
try: try:
if pulling: if pulling:
gameMap[self.x - 2*x][self.y - 2*y].pulled(x, y) gameMap[self.x - 2*x][self.y - 2*y].pulled(x, y)
except: except:
pass pass
for id, player in players.items():
try:
player.buildOwnPathMap()
except Exception as error:
print(error)
except Exception as error: except Exception as error:
raise error raise error