/
sususer
/
ColonyGEN
Обзор
Документация
Войти
/
sususer
/
ColonyGEN
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
core/simulation/system.py
173 строки
4 KB
Chekr
f11
03 июн 2026, 13:53
03 июн 2026, 13:53
d260831
Код
Авторство
О чём код?
from collections import deque from core.simulation.rules import RulesSystem from core.simulation.energy import EnergySystem class SimulationSystem: def __init__(self, game_map): self.map = game_map self.turn = 0 self.max_ap = 10 self.ap = self.max_ap self.resources = 0 self.buildings = [] self.energy = EnergySystem() self.rules = RulesSystem(game_map, self) self.connected_mines = set() self._dirty_connectivity = True def tick(self): self.reset_ap() self.process_buildings() self.energy.rebuild(self) if self._dirty_connectivity: self.rebuild_connectivity() self._dirty_connectivity = False self.turn += 1 def reset_ap(self): self.ap = self.max_ap def spend_ap(self, amount: int) -> bool: if self.ap < amount: return False self.ap -= amount return True def _apply_mine_penalty(self): if self.max_ap > 1: self.max_ap -= 1 self.ap = min(self.ap, self.max_ap) def build(self, building_class, x, y, cost_ap=1, cost_resources=0): if not self.spend_ap(cost_ap): return False if self.resources < cost_resources: return False if not self.rules.can_place(building_class, x, y): return False building = building_class(x, y) if not self.rules.place_building(building, x, y): return False self.add_building(building) if building.__class__.__name__ == "Mine": self._apply_mine_penalty() self.resources -= cost_resources self._dirty_connectivity = True return True def build_by_name(self, building_class, x, y, cost_ap=1, cost_resources=0): return self.build(building_class, x, y, cost_ap, cost_resources) def build_class(self, building_class, x, y, cost_ap=1, cost_resources=0): return self.build(building_class, x, y, cost_ap, cost_resources) def add_building(self, building): self.buildings.append(building) self._dirty_connectivity = True def process_buildings(self): for b in list(self.buildings): b.tick(self) def rebuild_connectivity(self): grid = self.map.grid visited = set() q = deque() self.connected_mines = set() base_pos = None for b in self.buildings: if b.__class__.__name__ == "Base" or getattr(b, "is_base", False): base_pos = (b.x, b.y) break if base_pos is None: return q.append(base_pos) visited.add(base_pos) while q: x, y = q.popleft() tile = grid[y][x] if tile.building and tile.building.__class__.__name__ == "Mine": self.connected_mines.add(tile.building) for dx, dy in [(1,0), (-1,0), (0,1), (0,-1)]: nx, ny = x + dx, y + dy if not (0 <= nx < self.map.width and 0 <= ny < self.map.height): continue if (nx, ny) in visited: continue neighbor = grid[ny][nx] if neighbor.type in ["road", "bridge"]: visited.add((nx, ny)) q.append((nx, ny)) elif neighbor.building and neighbor.building.__class__.__name__ in [ "Mine", "ResearchStation" ]: visited.add((nx, ny)) q.append((nx, ny)) def is_mine_connected(self, mine): return mine in self.connected_mines def snapshot(self): return { "turn": self.turn, "ap": self.ap, "max_ap": self.max_ap, "resources": self.resources, "buildings": [ { "type": b.__class__.__name__, "x": b.x, "y": b.y } for b in self.buildings ], "connected_mines": len(self.connected_mines) }