Improve ai
This commit is contained in:
parent
0add18a31d
commit
de08303bb0
36
enemies.py
36
enemies.py
@ -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:
|
if self.trackingCounter < 1:
|
||||||
fastDir = self.trackedPlayer.pathMap[self.x][self.y][2]
|
self.AITime = random.randint(150, 250)
|
||||||
# movementx = random.choices([-1, 0, 1], weightsx)[0]
|
self.trackingCounter = 9
|
||||||
# movementy = random.choices([-1, 0, 1], weightsy)[0]
|
self.trackedPlayer = False
|
||||||
movementx = fastDir[0]
|
elif self.trackedPlayer and self.trackedPlayer["player"].alive:
|
||||||
movementy = fastDir[1]
|
pathMap = self.trackedPlayer["player"].pathMap
|
||||||
# self.move(movementx, movementy)
|
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.trackingCounter -= 1
|
||||||
if self.trackingCounter < 1:
|
|
||||||
self.AITime = random.randint(500, 1000)
|
|
||||||
self.trackedPlayer = False
|
|
||||||
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
|
62
player.py
62
player.py
@ -5,30 +5,34 @@ from globals import *
|
|||||||
from blocks import *
|
from blocks import *
|
||||||
|
|
||||||
def buildPathMap(x, y):
|
def buildPathMap(x, y):
|
||||||
pathMap = []
|
try:
|
||||||
for i in range(tileCountx):
|
pathMap = []
|
||||||
column = []
|
for i in range(tileCountx):
|
||||||
for j in range(tileCounty):
|
column = []
|
||||||
column.append(False)
|
for j in range(tileCounty):
|
||||||
pathMap.append(column)
|
column.append(False)
|
||||||
pathMap[x][y] = (x,y,(0,0))
|
pathMap.append(column)
|
||||||
currentTiles = [pathMap[x][y]]
|
pathMap[x][y] = (x,y,(0,0))
|
||||||
newTiles = set()
|
currentTiles = [pathMap[x][y]]
|
||||||
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()
|
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:
|
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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user