Improve mines avoidance
This commit is contained in:
parent
47fb9df56c
commit
2f00def791
1 changed files with 33 additions and 25 deletions
|
@ -93,7 +93,7 @@ class GameMap:
|
||||||
self.map[entity.coord] = entity
|
self.map[entity.coord] = entity
|
||||||
|
|
||||||
def get_at(self, tile: 'Tile'):
|
def get_at(self, tile: 'Tile'):
|
||||||
return self.map[tile.coord]
|
return self.map.get(tile.coord, Tile(tile.x, tile.y))
|
||||||
|
|
||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
|
@ -101,6 +101,9 @@ class Tile:
|
||||||
self.y = y
|
self.y = y
|
||||||
self.x = x
|
self.x = x
|
||||||
|
|
||||||
|
def distance(self, other: 'Tile') -> float:
|
||||||
|
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def coord(self):
|
def coord(self):
|
||||||
return '{};{}'.format(self.x, self.y)
|
return '{};{}'.format(self.x, self.y)
|
||||||
|
@ -149,15 +152,15 @@ class Entity(Tile):
|
||||||
self.game_map = game_map
|
self.game_map = game_map
|
||||||
self.id = entity_id
|
self.id = entity_id
|
||||||
|
|
||||||
def distance(self, other: 'Entity') -> float:
|
@property
|
||||||
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
|
|
||||||
|
|
||||||
def nearest_barrel(self) -> Optional['Barrel']:
|
def nearest_barrel(self) -> Optional['Barrel']:
|
||||||
return self.nearest(self.game_map.barrels)
|
return self.nearest(self.game_map.barrels)
|
||||||
|
|
||||||
|
@property
|
||||||
def nearest_opponent(self) -> Optional['Ship']:
|
def nearest_opponent(self) -> Optional['Ship']:
|
||||||
return self.nearest(self.game_map.opponent_ships)
|
return self.nearest(self.game_map.opponent_ships)
|
||||||
|
|
||||||
|
@property
|
||||||
def nearest_mine(self) -> Optional['Mine']:
|
def nearest_mine(self) -> Optional['Mine']:
|
||||||
return self.nearest(self.game_map.mines)
|
return self.nearest(self.game_map.mines)
|
||||||
|
|
||||||
|
@ -206,25 +209,26 @@ class Ship(Entity):
|
||||||
perr('pos', self.tile)
|
perr('pos', self.tile)
|
||||||
perr('mine_wait', self.mine_wait)
|
perr('mine_wait', self.mine_wait)
|
||||||
perr('shoot_wait', self.shoot_wait)
|
perr('shoot_wait', self.shoot_wait)
|
||||||
|
perr('on path', self.tiles_on_path)
|
||||||
|
perr('distance oppo', self.distance(self.nearest_opponent))
|
||||||
|
|
||||||
perr('on path', self.tiles_on_path())
|
for tile in self.tiles_on_path:
|
||||||
for tile in self.tiles_on_path():
|
|
||||||
if isinstance(tile, Mine) or isinstance(tile, Cannonball):
|
if isinstance(tile, Mine) or isinstance(tile, Cannonball):
|
||||||
action, target = self.avoid(tile)
|
action, target = self.avoid(tile)
|
||||||
break
|
break
|
||||||
|
|
||||||
if not action:
|
if not action:
|
||||||
if (self.mine_wait > 0 and self.shoot_wait > 0) or rand > 0.4:
|
if 0.2 < rand <= 0.4 and self.shoot_wait <= 0 and self.distance(self.nearest_opponent.tiles_on_path[-1]) < 10:
|
||||||
|
action = Action.FIRE
|
||||||
|
target = self.nearest_opponent.tiles_on_path[-1]
|
||||||
|
elif rand <= 0.2 and self.mine_wait <= 0:
|
||||||
|
action = Action.MINE
|
||||||
|
else:
|
||||||
target = Tile(random.randint(0, 23), random.randint(0, 21))
|
target = Tile(random.randint(0, 23), random.randint(0, 21))
|
||||||
if self.game_map.barrels:
|
if self.game_map.barrels:
|
||||||
target = self.nearest_barrel()
|
target = self.nearest_barrel
|
||||||
|
|
||||||
action = Action.MOVE
|
action = Action.MOVE
|
||||||
elif rand > 0.2:
|
|
||||||
action = Action.FIRE
|
|
||||||
target = self.nearest_opponent()
|
|
||||||
else:
|
|
||||||
action = Action.MINE
|
|
||||||
|
|
||||||
if action is Action.MINE:
|
if action is Action.MINE:
|
||||||
self.mine_wait = DEFAULT_MINE_WAIT
|
self.mine_wait = DEFAULT_MINE_WAIT
|
||||||
|
@ -243,28 +247,31 @@ class Ship(Entity):
|
||||||
action = Action.MOVE
|
action = Action.MOVE
|
||||||
target = None
|
target = None
|
||||||
if self.orientation is Orientation.RIGHT:
|
if self.orientation is Orientation.RIGHT:
|
||||||
target = tile.top_right()
|
|
||||||
elif self.orientation is Orientation.TOP_RIGHT:
|
|
||||||
target = tile.right()
|
|
||||||
elif self.orientation is Orientation.TOP_LEFT:
|
|
||||||
target = tile.left()
|
|
||||||
elif self.orientation is Orientation.LEFT:
|
|
||||||
target = tile.top_left()
|
target = tile.top_left()
|
||||||
elif self.orientation is Orientation.BOTTOM_LEFT:
|
elif self.orientation is Orientation.TOP_RIGHT:
|
||||||
target = tile.left()
|
target = tile.left()
|
||||||
elif self.orientation is Orientation.BOTTOM_RIGHT:
|
elif self.orientation is Orientation.TOP_LEFT:
|
||||||
|
target = tile.bottom_left()
|
||||||
|
elif self.orientation is Orientation.LEFT:
|
||||||
|
target = tile.bottom_right()
|
||||||
|
elif self.orientation is Orientation.BOTTOM_LEFT:
|
||||||
target = tile.right()
|
target = tile.right()
|
||||||
|
elif self.orientation is Orientation.BOTTOM_RIGHT:
|
||||||
|
target = tile.top_right()
|
||||||
return action, target
|
return action, target
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tile(self):
|
def tile(self):
|
||||||
return Tile(self.x, self.y)
|
return Tile(self.x, self.y)
|
||||||
|
|
||||||
|
@property
|
||||||
def tiles_on_path(self):
|
def tiles_on_path(self):
|
||||||
tile = self.tile
|
tile = self.tile
|
||||||
perr('tile', tile)
|
tiles = [tile]
|
||||||
tiles = []
|
if self.speed == 0:
|
||||||
for _ in range(self.speed + 1):
|
return tiles
|
||||||
|
|
||||||
|
for _ in range(self.speed + 2):
|
||||||
if self.orientation is Orientation.RIGHT:
|
if self.orientation is Orientation.RIGHT:
|
||||||
tile = tile.right()
|
tile = tile.right()
|
||||||
elif self.orientation is Orientation.TOP_RIGHT:
|
elif self.orientation is Orientation.TOP_RIGHT:
|
||||||
|
@ -277,7 +284,7 @@ class Ship(Entity):
|
||||||
tile = tile.bottom_left()
|
tile = tile.bottom_left()
|
||||||
elif self.orientation is Orientation.BOTTOM_RIGHT:
|
elif self.orientation is Orientation.BOTTOM_RIGHT:
|
||||||
tile = tile.bottom_right()
|
tile = tile.bottom_right()
|
||||||
tiles.append(tile)
|
tiles.append(self.game_map.get_at(tile))
|
||||||
|
|
||||||
return tiles
|
return tiles
|
||||||
|
|
||||||
|
@ -345,6 +352,7 @@ def main():
|
||||||
shooter_id = int(arg_1)
|
shooter_id = int(arg_1)
|
||||||
turns_before_impact = int(arg_2)
|
turns_before_impact = int(arg_2)
|
||||||
game_map.add_cannonball(Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact))
|
game_map.add_cannonball(Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact))
|
||||||
|
perr('detected cannonball', Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact))
|
||||||
|
|
||||||
perr(game_map.map)
|
perr(game_map.map)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue