def can_place_at(self, system, plan): """ Check if the starlane altering monster fleet ''plan'' can be placed at ''system'' without disjoining the galaxy map. Compute the disjoint set of the galaxy without the starlanes from the proposed system. Return False if there will be more connected regions than the number of placed starlane altering monsters plus one, otherwise True. """ local_lanes = {(min(system, s), max(system, s)) for s in fo.sys_get_starlanes(system)} dsets = DisjointSets() for s in self.systems: dsets.add(s) for lane in self.starlanes: if lane in self.removed_lanes or lane in local_lanes: continue dsets.link(lane[0], lane[1]) num_contiguous_regions = len(dsets.complete_sets()) expected_num_contiguous_regions = (len(self.placed) + 2) if num_contiguous_regions > expected_num_contiguous_regions: return False if num_contiguous_regions < expected_num_contiguous_regions: report_error("Number of contiguous regions %d is below the expected number " "of contiguous regions %d when placing %d monster %s that can " "break starlanes." % (num_contiguous_regions, expected_num_contiguous_regions, len(self.placed) + 1, plan.name())) return False return True