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
if self.AITime < 1:
try:
closestPlayer = findClosestPlayer(self.x, self.y)
weightsx = [-closestPlayer["difference"][0], abs(closestPlayer["difference"][1]), closestPlayer["difference"][0]]
diff = findClosestPlayer(self.x, self.y)["difference"]
weightsx = [-diff[0], abs(diff[1]), diff[0]]
if weightsx[0] < 0:
weightsx[0] = weightsx[2]/6
elif weightsx[2] < 0:
@ -69,7 +69,7 @@ class Enemy:
else:
weightsx[0] = 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:
weightsy[0] = weightsy[2]/6
elif weightsy[2] < 0:
@ -95,22 +95,26 @@ class Smart(Enemy):
self.AITime -= 1
if self.AITime < 1:
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)
if self.trackingCounter < 1:
self.AITime = random.randint(150, 250)
self.trackingCounter = 9
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
if self.trackingCounter < 1:
self.AITime = random.randint(500, 1000)
self.trackedPlayer = False
self.AITime = random.randint(10, 15)
else:
closestPlayer = findClosestPlayer(self.x, self.y)
self.trackedPlayer = closestPlayer["player"]
self.trackingCounter = 8
self.trackedPlayer = closestPlayer
self.AITime = random.randint(10, 15)
except Exception as error:
print(error)
pass

View File

@ -5,30 +5,34 @@ from globals import *
from blocks import *
def buildPathMap(x, y):
pathMap = []
for i in range(tileCountx):
column = []
for j in range(tileCounty):
column.append(False)
pathMap.append(column)
pathMap[x][y] = (x,y,(0,0))
currentTiles = [pathMap[x][y]]
newTiles = set()
while True:
for tile in currentTiles:
for i in range(-1,2):
for j in range(-1,2):
newX = tile[0] + i
newY = tile[1] + j
if (i != 0 or j != 0) and not checkOOBounds((newX,newY)) and not pathMap[newX][newY]:
pathMap[newX][newY] = (newX,newY,(-i,-j))
if not gameMap[newX][newY]:
newTiles.add(pathMap[newX][newY])
if len(currentTiles) < 1:
break
currentTiles = list(newTiles)
try:
pathMap = []
for i in range(tileCountx):
column = []
for j in range(tileCounty):
column.append(False)
pathMap.append(column)
pathMap[x][y] = (x,y,(0,0))
currentTiles = [pathMap[x][y]]
newTiles = set()
return pathMap
while True:
for tile in currentTiles:
for i in range(-1,2):
for j in range(-1,2):
newX = tile[0] + i
newY = tile[1] + j
if (i != 0 or j != 0) and not checkOOBounds((newX,newY)) and not pathMap[newX][newY]:
pathMap[newX][newY] = (newX,newY,(-i,-j))
if not gameMap[newX][newY]:
newTiles.add(pathMap[newX][newY])
if len(currentTiles) < 1:
break
currentTiles = list(newTiles)
newTiles = set()
return pathMap
except Exception as error:
print(error)
raise error
class Player:
def __init__(self, id, x, y, sprite, keys):
@ -70,6 +74,8 @@ class Player:
self.pull = True
else:
self.pull = False
if keys[pygame.K_o]:
print(self.pathMap)
for command in self.commands:
try:
if keys[command[0]]:
@ -100,16 +106,16 @@ class Player:
self.x += x
self.y += y
gameMap[self.x][self.y] = self
for id, player in players.items():
try:
player.buildOwnPathMap()
except Exception as error:
print(error)
try:
if pulling:
gameMap[self.x - 2*x][self.y - 2*y].pulled(x, y)
except:
pass
for id, player in players.items():
try:
player.buildOwnPathMap()
except Exception as error:
print(error)
except Exception as error:
raise error