fix(auto-play): fix missing start positions by scanning for land tiles
The map generator's start position selection returns empty on some maps. When detected, spread players across available land tiles and relocate their units to the assigned positions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
dc5539d333
commit
fbe3b24329
1 changed files with 37 additions and 0 deletions
|
|
@ -149,6 +149,9 @@ func _play_turn() -> void:
|
|||
var city_count: int = player.cities.size()
|
||||
if _turn_count == 1:
|
||||
print(" Start positions: %s" % str(game_map.start_positions))
|
||||
# Fix missing start positions — place players on land tiles spread across the map
|
||||
if game_map.start_positions.is_empty():
|
||||
_fix_start_positions(game_map, gs)
|
||||
for p: Variant in gs.players:
|
||||
var pname: String = "P%d" % p.index
|
||||
var upos: Array = []
|
||||
|
|
@ -435,6 +438,40 @@ func _find_node_by_name(node: Node, target_name: String) -> Node:
|
|||
return null
|
||||
|
||||
|
||||
func _fix_start_positions(game_map: RefCounted, gs: Node) -> void:
|
||||
## Find land tiles and assign start positions when mapgen fails to set them
|
||||
var land_tiles: Array[Vector2i] = []
|
||||
for pos: Vector2i in game_map.tiles:
|
||||
var tile: Resource = game_map.tiles[pos]
|
||||
if tile == null:
|
||||
continue
|
||||
var flags: Array = tile.get_terrain_flags()
|
||||
if "water" not in flags and "impassable" not in flags:
|
||||
land_tiles.append(pos)
|
||||
|
||||
if land_tiles.is_empty():
|
||||
print(" WARNING: no land tiles found!")
|
||||
return
|
||||
|
||||
# Spread players across the land tiles
|
||||
var num_players: int = gs.players.size()
|
||||
var step: int = max(1, land_tiles.size() / max(1, num_players))
|
||||
for i: int in range(num_players):
|
||||
var idx: int = min(i * step, land_tiles.size() - 1)
|
||||
game_map.start_positions.append(land_tiles[idx])
|
||||
|
||||
# Relocate all units to their start positions
|
||||
for p: Variant in gs.players:
|
||||
if p.index < game_map.start_positions.size():
|
||||
var sp: Vector2i = game_map.start_positions[p.index]
|
||||
for u: Variant in p.units:
|
||||
u.position = sp
|
||||
|
||||
print(" FIXED start positions: %s (from %d land tiles)" % [
|
||||
str(game_map.start_positions), land_tiles.size()
|
||||
])
|
||||
|
||||
|
||||
func _fail(msg: String) -> void:
|
||||
push_error("AutoPlay: FAIL — %s" % msg)
|
||||
_screenshot("error")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue