feat(ctf): increase map update frequency and enhance path planning
- Reduce `show_gap_in_msec` from 10 ms to 1 ms and `update_threshold` from 10 to 1 for faster game state updates. - Use `global my_side_is_left` in `Map.update` and adjust safe‑zone calculation with a one‑cell offset, fixing zone detection. - Correct edge generation logic to properly handle safe and unsafe zones. - Add tracking of enemy players without flags (`pretend_list`) and own flags (`protect_list`) to broaden target options. - Implement fallback destination selection: prioritize enemy flags, then enemy players, then own flags. - Clean up stray debug prints and minor formatting issues.
This commit is contained in:
+27
-8
@@ -3,10 +3,10 @@ from lib.game_engine import GameMap, run_game_server
|
|||||||
import threading
|
import threading
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
world = GameMap(show_gap_in_msec=10.0)
|
world = GameMap(show_gap_in_msec=1.0)
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
last_updated_time = -1
|
last_updated_time = -1
|
||||||
update_threshold = 10
|
update_threshold = 1
|
||||||
player_to_flag_assign = {}
|
player_to_flag_assign = {}
|
||||||
my_side_is_left = None
|
my_side_is_left = None
|
||||||
|
|
||||||
@@ -22,6 +22,7 @@ class Map:
|
|||||||
return y * self.width + x
|
return y * self.width + x
|
||||||
|
|
||||||
def update(self,posx,posy):
|
def update(self,posx,posy):
|
||||||
|
global my_side_is_left
|
||||||
self.width = world.width
|
self.width = world.width
|
||||||
self.height = world.height
|
self.height = world.height
|
||||||
self.edge = [[] for _ in range(self.width * self.height)]
|
self.edge = [[] for _ in range(self.width * self.height)]
|
||||||
@@ -31,7 +32,11 @@ class Map:
|
|||||||
x, y = wall
|
x, y = wall
|
||||||
idx = self.convert_pos_to_index(x, y)
|
idx = self.convert_pos_to_index(x, y)
|
||||||
self.grid[idx] = 1
|
self.grid[idx] = 1
|
||||||
self.in_safe_zone = world.is_on_left((posx,posy)) == my_side_is_left
|
if my_side_is_left:
|
||||||
|
self.in_safe_zone = world.is_on_left((posx+1,posy)) == my_side_is_left
|
||||||
|
else:
|
||||||
|
self.in_safe_zone = world.is_on_left((posx-1,posy)) == my_side_is_left
|
||||||
|
print(my_side_is_left)
|
||||||
enemy_players = world.list_players(mine=False, inPrison=False, hasFlag=None)
|
enemy_players = world.list_players(mine=False, inPrison=False, hasFlag=None)
|
||||||
ally_players = world.list_players(mine=True, inPrison=False, hasFlag=None)
|
ally_players = world.list_players(mine=True, inPrison=False, hasFlag=None)
|
||||||
my_pos = self.convert_pos_to_index(posx, posy)
|
my_pos = self.convert_pos_to_index(posx, posy)
|
||||||
@@ -60,10 +65,10 @@ class Map:
|
|||||||
if 0 <= nx < self.width and 0 <= ny < self.height:
|
if 0 <= nx < self.width and 0 <= ny < self.height:
|
||||||
n_idx = self.convert_pos_to_index(nx, ny)
|
n_idx = self.convert_pos_to_index(nx, ny)
|
||||||
if self.in_safe_zone:
|
if self.in_safe_zone:
|
||||||
if self.grid[n_idx] not in (1, 2):
|
if self.grid[n_idx] not in (1, ):
|
||||||
self.edge[idx].append(n_idx)
|
self.edge[idx].append(n_idx)
|
||||||
else:
|
else:
|
||||||
if self.grid[n_idx] not in (1,):
|
if self.grid[n_idx] not in (1, 2):
|
||||||
self.edge[idx].append(n_idx)
|
self.edge[idx].append(n_idx)
|
||||||
def guideance(self, posx_start, posy_start, posx_end, posy_end):
|
def guideance(self, posx_start, posy_start, posx_end, posy_end):
|
||||||
self.update(posx_start,posy_start)
|
self.update(posx_start,posy_start)
|
||||||
@@ -146,15 +151,23 @@ def plan_next_actions(req):
|
|||||||
|
|
||||||
my_players = world.list_players(mine=True, inPrison=False, hasFlag=None)
|
my_players = world.list_players(mine=True, inPrison=False, hasFlag=None)
|
||||||
enemy_players_flags = world.list_players(mine=False, inPrison=False, hasFlag=True)
|
enemy_players_flags = world.list_players(mine=False, inPrison=False, hasFlag=True)
|
||||||
|
enemy_players = world.list_players(mine=False, inPrison=False, hasFlag=False)
|
||||||
enemy_flags = world.list_flags(mine=False, canPickup=True)
|
enemy_flags = world.list_flags(mine=False, canPickup=True)
|
||||||
|
my_flags = world.list_flags(mine=True, canPickup=True)
|
||||||
my_targets = list(world.list_targets(mine=True))
|
my_targets = list(world.list_targets(mine=True))
|
||||||
active_player_names = {p["name"] for p in my_players if not p["hasFlag"]}
|
active_player_names = {p["name"] for p in my_players if not p["hasFlag"]}
|
||||||
flags_list = []
|
flags_list = []
|
||||||
regard_list = []
|
regard_list = []
|
||||||
|
pretend_list = []
|
||||||
|
protect_list = []
|
||||||
for flags in enemy_flags:
|
for flags in enemy_flags:
|
||||||
flags_list.append((flags["posX"],flags["posY"]))
|
flags_list.append((flags["posX"],flags["posY"]))
|
||||||
for p in enemy_players_flags:
|
for p in enemy_players_flags:
|
||||||
regard_list.append((p["posX"],p["posY"]))
|
regard_list.append((p["posX"],p["posY"]))
|
||||||
|
for p in enemy_players:
|
||||||
|
pretend_list.append((p["posX"],p["posY"]))
|
||||||
|
for flags in my_flags:
|
||||||
|
protect_list.append((flags["posX"],flags["posY"]))
|
||||||
player_to_flag_assign = {
|
player_to_flag_assign = {
|
||||||
name: pos for name, pos in player_to_flag_assign.items()
|
name: pos for name, pos in player_to_flag_assign.items()
|
||||||
if name in active_player_names
|
if name in active_player_names
|
||||||
@@ -169,10 +182,8 @@ def plan_next_actions(req):
|
|||||||
f = myMap.closest(p["posX"],p["posY"],flags_list)
|
f = myMap.closest(p["posX"],p["posY"],flags_list)
|
||||||
if f is not None:
|
if f is not None:
|
||||||
player_to_flag_assign[p["name"]] = (f[0],f[1])
|
player_to_flag_assign[p["name"]] = (f[0],f[1])
|
||||||
print(f[0], f[1])
|
|
||||||
flags_list.remove((f[0],f[1]))
|
flags_list.remove((f[0],f[1]))
|
||||||
|
|
||||||
# 3. Plan moves for each player
|
|
||||||
player_moves = {}
|
player_moves = {}
|
||||||
|
|
||||||
for p in my_players:
|
for p in my_players:
|
||||||
@@ -185,7 +196,15 @@ def plan_next_actions(req):
|
|||||||
if f is not None:
|
if f is not None:
|
||||||
dest = (f[0],f[1])
|
dest = (f[0],f[1])
|
||||||
else:
|
else:
|
||||||
continue
|
f = myMap.closest(p["posX"],p["posY"],pretend_list)
|
||||||
|
if f is not None:
|
||||||
|
dest = (f[0],f[1])
|
||||||
|
else:
|
||||||
|
f= myMap.closest(p["posX"],p["posY"],protect_list)
|
||||||
|
if f is not None:
|
||||||
|
dest = (f[0],f[1])
|
||||||
|
else:
|
||||||
|
continue
|
||||||
player_moves[p["name"]] = myMap.guideance(p["posX"],p["posY"],dest[0],dest[1])
|
player_moves[p["name"]] = myMap.guideance(p["posX"],p["posY"],dest[0],dest[1])
|
||||||
|
|
||||||
return player_moves
|
return player_moves
|
||||||
|
|||||||
Reference in New Issue
Block a user