def check_build_line(cls, session, point1, point2, rotation=45, ship=None): # Pathfinding currently only supports buildingsize 1x1, so don't use it in this case if cls.size != (1, 1): return [ cls.check_build_fuzzy(session, point2, rotation=rotation, ship=ship) ] # use pathfinding to get a path, then try to build along it island = session.world.get_island(point1) if island is None: return [] if cls.id == BUILDINGS.TRAIL: nodes = island.path_nodes.nodes elif cls.id == BUILDINGS.BARRIER: # Allow nodes that can be walked upon and existing barriers when finding a # build path nodes = ChainMap(island.path_nodes.nodes, island.barrier_nodes.nodes) else: raise Exception( 'BuildableLine does not support building id {0}'.format( cls.id)) path = a_star_find_path(point1.to_tuple(), point2.to_tuple(), nodes, rotation in (45, 225)) if path is None: # can't find a path between these points return [] # TODO: maybe implement alternative strategy possible_builds = [] #TODO duplicates recalculation code in world.building.path for x, y in path: action = '' for action_char, (xoff, yoff) in \ sorted(BUILDINGS.ACTION.action_offset_dict.items()): # order is important here if action_char in 'abcd' and (xoff + x, yoff + y) in path: action += action_char if action == '': action = 'single' # single trail piece with no neighbors build = cls.check_build(session, Point(x, y)) build.action = action possible_builds.append(build) return possible_builds
def check_build_line(cls, session, point1, point2, rotation=45, ship=None): # Pathfinding currently only supports buildingsize 1x1, so don't use it in this case if cls.size != (1, 1): return [cls.check_build_fuzzy(session, point2, rotation=rotation, ship=ship)] # use pathfinding to get a path, then try to build along it island = session.world.get_island(point1) if island is None: return [] if cls.id == BUILDINGS.TRAIL: nodes = island.path_nodes.nodes elif cls.id == BUILDINGS.BARRIER: # Allow nodes that can be walked upon and existing barriers when finding a # build path nodes = ChainMap(island.path_nodes.nodes, island.barrier_nodes.nodes) else: raise Exception('BuildableLine does not support building id {0}'.format(cls.id)) path = a_star_find_path(point1.to_tuple(), point2.to_tuple(), nodes, rotation in (45, 225)) if path is None: # can't find a path between these points return [] # TODO: maybe implement alternative strategy possible_builds = [] #TODO duplicates recalculation code in world.building.path for x, y in path: action = '' for action_char, (xoff, yoff) in \ sorted(BUILDINGS.ACTION.action_offset_dict.items()): # order is important here if action_char in 'abcd' and (xoff + x, yoff + y) in path: action += action_char if action == '': action = 'single' # single trail piece with no neighbors build = cls.check_build(session, Point(x, y)) build.action = action possible_builds.append(build) return possible_builds
def remove(s, p): """ Place a couple of buildings and tear down one randomly, run a while afterwards. Called by test_removal with different parameters. """ settlement, island = settle(s) settlement.warehouse.get_component( StorageComponent).inventory.adjust_limit(sys.maxsize) # Plant trees for (x, y) in product(range(23, 38), repeat=2): if s.random.randint(0, 1) == 1: tree = Build(BUILDINGS.TREE, x, y, island, settlement=settlement)(p) assert tree tree.get_component(Producer).finish_production_now() jack = Build(BUILDINGS.LUMBERJACK, 25, 30, island, settlement=settlement)(p) assert jack jack = Build(BUILDINGS.LUMBERJACK, 35, 30, island, settlement=settlement)(p) assert jack # Throw some fish into the water for x in (25, 30, 35): school = Build(BUILDINGS.FISH_DEPOSIT, x, 18, s.world, ownerless=True)(None) assert school school.get_component(Producer).finish_production_now() fisherman = Build(BUILDINGS.FISHER, 25, 20, island, settlement=settlement)(p) assert fisherman fisherman = Build(BUILDINGS.FISHER, 35, 20, island, settlement=settlement)(p) assert fisherman # Some wild animals in the forest for (x_off, y_off) in product([-5, -4, 4, 5], repeat=2): x = 30 + x_off y = 30 + y_off animal = CreateUnit(island.worldid, UNITS.WILD_ANIMAL, x, y)(None) assert animal animal.get_component(Producer).finish_production_now() hunter = Build(BUILDINGS.HUNTER, 30, 35, island, settlement=settlement)(p) assert hunter # Build a farm assert Build(BUILDINGS.FARM, 26, 33, island, settlement=settlement)(p) assert Build(BUILDINGS.PASTURE, 22, 33, island, settlement=settlement)(p) assert Build(BUILDINGS.PASTURE, 26, 37, island, settlement=settlement)(p) # Build roads for (start, dest) in [(Point(27, 30), Point(30, 23)), (Point(32, 23), Point(35, 29)), (Point(25, 22), Point(30, 23)), (Point(32, 23), Point(35, 22)), (Point(30, 34), Point(32, 25)), (Point(26, 32), Point(27, 30))]: path = a_star_find_path(start.to_tuple(), dest.to_tuple(), island.path_nodes.nodes) assert path for (x, y) in path: Build(BUILDINGS.TRAIL, x, y, island, settlement=settlement)(p) s.run(seconds=before_ticks) # Tear down a random building that is not a trail or tree. target = [ b for b in settlement.buildings if b.id not in (BUILDINGS.TRAIL, BUILDINGS.TREE) ][tear_index] Tear(target)(p) s.run(seconds=after_ticks)
def remove(s, p): """ Place a couple of buildings and tear down one randomly, run a while afterwards. Called by test_removal with different parameters. """ settlement, island = settle(s) settlement.warehouse.get_component(StorageComponent).inventory.adjust_limit(sys.maxsize) # Plant trees for (x, y) in product(range(23, 38), repeat=2): if s.random.randint(0, 1) == 1: tree = Build(BUILDINGS.TREE, x, y, island, settlement=settlement)(p) assert tree tree.get_component(Producer).finish_production_now() jack = Build(BUILDINGS.LUMBERJACK, 25, 30, island, settlement=settlement)(p) assert jack jack = Build(BUILDINGS.LUMBERJACK, 35, 30, island, settlement=settlement)(p) assert jack # Throw some fish into the water for x in (25, 30, 35): school = Build(BUILDINGS.FISH_DEPOSIT, x, 18, s.world, ownerless=True)(None) assert school school.get_component(Producer).finish_production_now() fisherman = Build(BUILDINGS.FISHER, 25, 20, island, settlement=settlement)(p) assert fisherman fisherman = Build(BUILDINGS.FISHER, 35, 20, island, settlement=settlement)(p) assert fisherman # Some wild animals in the forest for (x_off, y_off) in product([-5, -4, 4, 5], repeat=2): x = 30 + x_off y = 30 + y_off animal = CreateUnit(island.worldid, UNITS.WILD_ANIMAL, x, y)(None) assert animal animal.get_component(Producer).finish_production_now() hunter = Build(BUILDINGS.HUNTER, 30, 35, island, settlement=settlement)(p) assert hunter # Build a farm assert Build(BUILDINGS.FARM, 26, 33, island, settlement=settlement)(p) assert Build(BUILDINGS.PASTURE, 22, 33, island, settlement=settlement)(p) assert Build(BUILDINGS.PASTURE, 26, 37, island, settlement=settlement)(p) # Build roads for (start, dest) in [(Point(27, 30), Point(30, 23)), (Point(32, 23), Point(35, 29)), (Point(25, 22), Point(30, 23)), (Point(32, 23), Point(35, 22)), (Point(30, 34), Point(32, 25)), (Point(26, 32), Point(27, 30))]: path = a_star_find_path(start.to_tuple(), dest.to_tuple(), island.path_nodes.nodes) assert path for (x, y) in path: Build(BUILDINGS.TRAIL, x, y, island, settlement=settlement)(p) s.run(seconds=before_ticks) # Tear down a random building that is not a trail or tree. target = [b for b in settlement.buildings if b.id not in (BUILDINGS.TRAIL, BUILDINGS.TREE)][tear_index] Tear(target)(p) s.run(seconds=after_ticks)