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:
2025-12-27 23:30:22 +08:00
parent b038e5ac29
commit 99ab005021

View File

@@ -3,10 +3,10 @@ from lib.game_engine import GameMap, run_game_server
import threading
import collections
world = GameMap(show_gap_in_msec=10.0)
world = GameMap(show_gap_in_msec=1.0)
lock = threading.Lock()
last_updated_time = -1
update_threshold = 10
update_threshold = 1
player_to_flag_assign = {}
my_side_is_left = None
@@ -22,6 +22,7 @@ class Map:
return y * self.width + x
def update(self,posx,posy):
global my_side_is_left
self.width = world.width
self.height = world.height
self.edge = [[] for _ in range(self.width * self.height)]
@@ -31,7 +32,11 @@ class Map:
x, y = wall
idx = self.convert_pos_to_index(x, y)
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)
ally_players = world.list_players(mine=True, inPrison=False, hasFlag=None)
my_pos = self.convert_pos_to_index(posx, posy)
@@ -42,10 +47,10 @@ class Map:
for enemy in enemy_players:
x, y = enemy["posX"], enemy["posY"]
idx = self.convert_pos_to_index(x, y)
self.grid[idx] = 2
self.grid[idx] = 2
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nx, ny = x + dx, y + dy
self.grid[self.convert_pos_to_index(nx,ny)] = 2
self.grid[self.convert_pos_to_index(nx,ny)] = 2
for y in range(self.height):
for x in range(self.width):
idx = self.convert_pos_to_index(x, y)
@@ -60,10 +65,10 @@ class Map:
if 0 <= nx < self.width and 0 <= ny < self.height:
n_idx = self.convert_pos_to_index(nx, ny)
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)
else:
if self.grid[n_idx] not in (1,):
if self.grid[n_idx] not in (1, 2):
self.edge[idx].append(n_idx)
def guideance(self, posx_start, posy_start, posx_end, posy_end):
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)
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)
my_flags = world.list_flags(mine=True, canPickup=True)
my_targets = list(world.list_targets(mine=True))
active_player_names = {p["name"] for p in my_players if not p["hasFlag"]}
flags_list = []
regard_list = []
pretend_list = []
protect_list = []
for flags in enemy_flags:
flags_list.append((flags["posX"],flags["posY"]))
for p in enemy_players_flags:
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 = {
name: pos for name, pos in player_to_flag_assign.items()
if name in active_player_names
@@ -169,10 +182,8 @@ def plan_next_actions(req):
f = myMap.closest(p["posX"],p["posY"],flags_list)
if f is not None:
player_to_flag_assign[p["name"]] = (f[0],f[1])
print(f[0], f[1])
flags_list.remove((f[0],f[1]))
# 3. Plan moves for each player
player_moves = {}
for p in my_players:
@@ -185,7 +196,15 @@ def plan_next_actions(req):
if f is not None:
dest = (f[0],f[1])
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])
return player_moves