files-game/engine/map.gd
2026-10-01 17:22:18 -04:00

142 lines
5.1 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name GameMap
## The playmats (map.ts). Location ids follow the diagram in rules.md: S1 is South's start, S2 is
## North's. A line is {id, a, b, gateway}; lines with a gateway can hold a Firewall.
##
## The current board lives in static vars. RulesEngine.create_game switches it with use_board, so a
## process plays one board at a time, as in TS.
const START_LOC: Array[String] = ["S1", "S2"]
## Boards by name: {grid, lines: [[a, b, gateway], ...], win}. The grid is row by row from South's
## side; the row is the letter (A is South's first row). `win` is the grid locations needed to win.
static var BOARDS: Dictionary = {}
static var GRID: Array = []
static var LOCATIONS: Array = []
static var LINES: Array = []
static var LINE_BY_ID: Dictionary = {}
static var LINES_AT: Dictionary = {}
static var _row_count := 0
static func _static_init() -> void:
BOARDS = {
"classic": _classic(),
# 4×4, each start joins the middle two squares of its first row.
"grid4": _squares([2, 3]),
# 4×4, each start joins all four squares of its first row.
"grid4-wide": _squares([1, 2, 3, 4]),
# 4×4, each start joins one square: A2 for South, D3 for North.
"grid4-one": _squares([2]),
# Three lanes, linked only across the middle row; every line between locations has a gateway.
"lanes": _lanes(true),
# Three lanes, but only the two middle cross-links can be firewalled.
"lanes-open": _lanes(false),
}
use_board("classic")
## The playmat in rules.md: rows of 3, 2, 3, 2, 3, entered from the middle of each end row.
static func _classic() -> Dictionary:
return {
"grid": ["A1", "A2", "A3", "B1", "B2", "C1", "C2", "C3", "D1", "D2", "E1", "E2", "E3"],
"lines": [
["S1", "A2", false],
["A1", "A2", true], ["A2", "A3", true],
["A1", "B1", true], ["A2", "B1", true], ["A2", "B2", true], ["A3", "B2", true],
["B1", "C1", true], ["B1", "C2", true], ["B2", "C2", true], ["B2", "C3", true],
["C1", "D1", true], ["C2", "D1", true], ["C2", "D2", true], ["C3", "D2", true],
["D1", "E1", true], ["D1", "E2", true], ["D2", "E2", true], ["D2", "E3", true],
["E1", "E2", true], ["E2", "E3", true],
["S2", "E2", false],
],
"win": 10,
}
## A 4×4 grid of squares, rows A–D, columns 1–4, joined side to side (not diagonally), with a
## gateway on every line between squares. Each start joins the listed squares of its first row;
## North's are South's rotated 180°. Win with 12 of the 16.
static func _squares(south_entries: Array) -> Dictionary:
var rows := ["A", "B", "C", "D"]
var grid := []
for r in rows:
for c in [1, 2, 3, 4]:
grid.append("%s%d" % [r, c])
var lines := []
for c in south_entries:
lines.append(["S1", "A%d" % c, false])
for i in rows.size():
var r: String = rows[i]
for c in range(1, 5):
if c < 4:
lines.append(["%s%d" % [r, c], "%s%d" % [r, c + 1], true])
if i < 3:
lines.append(["%s%d" % [r, c], "%s%d" % [rows[i + 1], c], true])
for c in south_entries:
lines.append(["S2", "D%d" % (5 - c), false])
return {"grid": grid, "lines": lines, "win": 12}
## Three lanes of five rows, A–E, joined to each other only across the middle row (C1–C2–C3). Each
## start joins the first location of every lane. With `lane_gateways` false the lane lines can't be
## firewalled, so only the two middle cross-links can. Win with 11 of the 15.
static func _lanes(lane_gateways: bool) -> Dictionary:
var rows := ["A", "B", "C", "D", "E"]
var grid := []
for r in rows:
for c in [1, 2, 3]:
grid.append("%s%d" % [r, c])
var lines := []
for c in [1, 2, 3]:
lines.append(["S1", "A%d" % c, false])
for i in rows.size() - 1:
for c in range(1, 4):
lines.append(["%s%d" % [rows[i], c], "%s%d" % [rows[i + 1], c], lane_gateways])
lines.append(["C1", "C2", true])
lines.append(["C2", "C3", true])
for c in range(1, 4):
lines.append(["S2", "E%d" % c, false])
return {"grid": grid, "lines": lines, "win": 11}
static func use_board(board_name: String) -> void:
if not BOARDS.has(board_name):
Util.fail("Unknown board \"%s\". Boards: %s" % [board_name, ", ".join(BOARDS.keys())])
return
var board: Dictionary = BOARDS[board_name]
GRID = board.grid
LOCATIONS = [START_LOC[0]] + GRID + [START_LOC[1]]
LINES = []
for l in board.lines:
LINES.append({"id": "%s-%s" % [l[0], l[1]], "a": l[0], "b": l[1], "gateway": l[2]})
LINE_BY_ID = {}
for l in LINES:
LINE_BY_ID[l.id] = l
LINES_AT = {}
for loc in LOCATIONS:
LINES_AT[loc] = LINES.filter(func(l): return l.a == loc or l.b == loc)
var rows := {}
for loc in GRID:
rows[loc[0]] = true
_row_count = rows.size()
static func other_end(line: Dictionary, loc: String) -> String:
return line.b if line.a == loc else line.a
static func is_start(loc: String) -> bool:
return loc == START_LOC[0] or loc == START_LOC[1]
## A location's level for the player capturing it: your first row is Lv 1, counting up to the far
## row (Lv 5 on the classic board, Lv 4 on the 4×4 ones). Your own start is Lv 1.
static func level_for(p: int, loc: String) -> int:
if loc == START_LOC[p]:
return 1
var row := "ABCDE".find(loc[0])
if row < 0 or row >= _row_count:
Util.fail("No level for %s from player %d" % [loc, p])
return 1
return row + 1 if p == 0 else _row_count - row