def test_settler_save_load(): """Save/loading """ session, player = new_session() settlement, island = settle(session) # setup: # 1) build settler # 2) save/load # 3) build main square # -> settler won't load properly and not use the resources and die settler = Build(BUILDINGS.RESIDENTIAL, 25, 22, island, settlement=settlement)(player) assert settler main_square = Build(BUILDINGS.MAIN_SQUARE, 23, 24, island, settlement=settlement)(player) assert main_square main_square.get_component(StorageComponent).inventory.alter(RES.FOOD, 100) session = saveload(session) session.run(seconds=500) tile = session.world.get_tile(Point(25, 22)) # tile will contain ruin in case of failure assert tile.object.id == BUILDINGS.RESIDENTIAL session.end()
def test_combat_save_load(): """ create a savegame with combat units and actual combat, then save/load it """ session, player = new_session() (p0, s0), (p1, s1) = setup_combat(session, UNITS.FRIGATE) s0_worldid, s1_worldid = s0.worldid, s1.worldid session.run(seconds=1) # saveload session = saveload(session) s0 = WorldObject.get_object_by_id(s0_worldid) s1 = WorldObject.get_object_by_id(s1_worldid) # fight AddEnemyPair(p0, p1).execute(session) Attack(s0, s1).execute(session) Attack(s1, s0).execute(session) session.run(seconds=20) # saveload session = saveload(session) assert one_dead(s0_worldid, s1_worldid) session.end()
def test_load_inactive_production(): """ create a savegame with a inactive production, load it """ session, player = new_session() settlement, island = settle(session) lj = Build(BUILDINGS.LUMBERJACK, 30, 30, island, settlement=settlement)(player) # Set lumberjack to inactive lj.get_component(Producer).set_active(active=False) worldid = lj.worldid session.run(seconds=1) # Save and reload game session = saveload(session) loadedlj = WorldObject.get_object_by_id(worldid) # Make sure it really is not active producer = loadedlj.get_component(Producer) assert not producer.is_active() # Trigger bug #1359 ToggleActive(producer).execute(session) session.end()
def test_settler_save_load(): """Save/loading """ session, player = new_session() settlement, island = settle(session) # setup: # 1) build settler # 2) save/load # 3) build main square # -> settler won't load properly and not use the resources and die settler = Build(BUILDINGS.RESIDENTIAL, 25, 22, island, settlement=settlement)(player) assert settler main_square = Build(BUILDINGS.MAIN_SQUARE, 23, 24, island, settlement=settlement)(player) assert main_square main_square.get_component(StorageComponent).inventory.alter(RES.FOOD, 100) session = saveload(session) session.run(seconds=500) tile = session.world.get_tile(Point(25, 22)) # tile will contain ruin in case of failure assert tile.object.id == BUILDINGS.RESIDENTIAL
def create_lumberjack_production_session(): """Create a saved game with a producing production and then load it.""" session, player = new_session() settlement, island = settle(session) for x in [29, 30, 31, 32]: Build( BUILDINGS.TREE, x, 29, island, settlement=settlement, )(player) building = Build(BUILDINGS.LUMBERJACK, 30, 30, island, settlement=settlement)(player) production = building.get_component(Producer).get_productions()[0] # wait for the lumberjack to start producing while True: if production.get_state() is PRODUCTION.STATES.producing: break session.run(ticks=1) # Save and reload game session = saveload(session) return session
def test_load_inactive_production(): """ create a savegame with a inactive production, load it """ session, player = new_session() settlement, island = settle(session) lj = Build(BUILDINGS.LUMBERJACK, 30, 30, island, settlement=settlement)(player) # Set lumberjack to inactive lj.get_component(Producer).set_active(active = False) worldid = lj.worldid session.run(seconds=1) fd, filename = tempfile.mkstemp() os.close(fd) assert session.save(savegamename=filename) session.end(keep_map=True) # Load game session = load_session(filename) loadedlj = WorldObject.get_object_by_id(worldid) # Make sure it really is not active producer = loadedlj.get_component(Producer) assert not producer.is_active() # Trigger bug #1359 ToggleActive(producer).execute(session) session.end()
def create_lumberjack_production_session(): """Create a saved game with a producing production and then load it.""" session, player = new_session() settlement, island = settle(session) for x in [29, 30, 31, 32]: Build( BUILDINGS.TREE, x, 29, island, settlement=settlement, )(player) building = Build(BUILDINGS.LUMBERJACK, 30, 30, island, settlement=settlement)(player) production = building.get_component(Producer).get_productions()[0] # wait for the lumberjack to start producing while True: if production.get_state() is PRODUCTION.STATES.producing: break session.run(ticks=1) fd1, filename1 = tempfile.mkstemp() os.close(fd1) assert session.save(savegamename=filename1) session.end(keep_map=True) # load the game session = load_session(filename1) return session
def test_hunter_save_load(): """Save/loading hunter in different states""" session, player = new_session() settlement, island = settle(session) # setup hunter, trees (to keep animals close) and animals hunter = Build(BUILDINGS.HUNTER_CLASS, 30, 30, island, settlement=settlement)(player) hunter_worldid = hunter.worldid del hunter # invalid after save/load for x in xrange(27, 29): for y in xrange(25, 28): assert Build(BUILDINGS.TREE_CLASS, x, y, island, settlement=settlement)(player) CreateUnit(island.worldid, UNITS.WILD_ANIMAL_CLASS, 27, 27)(issuer=None) CreateUnit(island.worldid, UNITS.WILD_ANIMAL_CLASS, 28, 27)(issuer=None) CreateUnit(island.worldid, UNITS.WILD_ANIMAL_CLASS, 29, 27)(issuer=None) # utility def saveload(session): fd, filename = tempfile.mkstemp() os.close(fd) assert session.save(savegamename=filename) session.end(keep_map=True) session = load_session(filename) Scheduler().before_ticking() # late init finish (not ticking already) return session def get_hunter_collector(session): hunter = WorldObject.get_object_by_id(hunter_worldid) return hunter.get_component(CollectingComponent)._CollectingComponent__collectors[0] def await_transition(session, collector, old_state, new_state): assert collector.state == old_state, "expected old state %s, got %s" % (old_state, collector.state) while collector.state == old_state: session.run(seconds=1) assert collector.state == new_state, "expected new state %s, got %s" % (old_state, collector.state) sequence = [ Collector.states.idle, Collector.states.waiting_for_animal_to_stop, Collector.states.moving_to_target, Collector.states.working, Collector.states.moving_home, Collector.states.idle ] # do full run without saveload collector = get_hunter_collector(session) for i in xrange(len(sequence)-1): await_transition(session, collector, sequence[i], sequence[i+1]) # do full run with saveload for i in xrange(len(sequence)-1): collector = get_hunter_collector(session) await_transition(session, collector, sequence[i], sequence[i+1]) session = saveload(session)
def test_ticket_1427(): """Boatbuilder production progress should be saved properly""" session, player = new_session() settlement, island = settle(session) boat_builder = Build(BUILDINGS.BOATBUILDER_CLASS, 35, 20, island, settlement=settlement)(player) worldid = boat_builder.worldid # Make sure no boards are available settlement.get_component(StorageComponent).inventory.alter(RES.BOARDS_ID, -1000) bb_storage = boat_builder.get_component(StorageComponent) # Add production to use resources bb_producer = boat_builder.get_component(Producer) bb_producer.add_production_by_id(PRODUCTIONLINES.HUKER) production = bb_producer._productions[PRODUCTIONLINES.HUKER] assert production.progress == 0.0 bb_storage.inventory.alter(RES.TEXTILE_ID, 10) bb_storage.inventory.alter(RES.BOARDS_ID, 6) production_line = production._prod_line # Make sure the boatbuilder consumes everything in his inventory session.run(seconds=10) # Check if correctly consumed wood assert production_line.consumed_res[RES.BOARDS_ID] == -2 # Save all production process for later expected_consumed_res = production_line.consumed_res expected_produced_res = production_line.produced_res expected_production = production_line.production expected_progress = production.progress # Make sure the producer used the boards assert bb_storage.inventory[RES.BOARDS_ID] == 0 fd, filename = tempfile.mkstemp() os.close(fd) assert session.save(savegamename=filename) session.end(keep_map=True) # Load game session = load_session(filename) loadedbb = WorldObject.get_object_by_id(worldid) production_loaded = loadedbb.get_component(Producer)._productions[PRODUCTIONLINES.HUKER] production_line_loaded = production_loaded._prod_line # Make sure everything is loaded correctly assert expected_consumed_res == production_line_loaded.consumed_res assert expected_produced_res == production_line_loaded.produced_res assert expected_production == production_line_loaded.production assert expected_progress == production_loaded.progress
def test_ticket_1427(): """Boatbuilder production progress should be saved properly""" session, player = new_session() settlement, island = settle(session) boat_builder = Build(BUILDINGS.BOAT_BUILDER, 35, 20, island, settlement=settlement)(player) worldid = boat_builder.worldid # Make sure no boards are available settlement.get_component(StorageComponent).inventory.alter(RES.BOARDS, -1000) bb_storage = boat_builder.get_component(StorageComponent) # Add production to use resources bb_producer = boat_builder.get_component(Producer) bb_producer.add_production_by_id(PRODUCTIONLINES.HUKER) production = bb_producer._productions[PRODUCTIONLINES.HUKER] assert production.progress == 0.0 bb_storage.inventory.alter(RES.TEXTILE, 10) bb_storage.inventory.alter(RES.BOARDS, 6) production_line = production._prod_line # Make sure the boatbuilder consumes everything in its inventory session.run(seconds=10) # Check if correctly consumed wood assert production_line.consumed_res[RES.BOARDS] == -2 # Save all production process for later expected_consumed_res = production_line.consumed_res expected_produced_res = production_line.produced_res expected_production = production_line.production expected_progress = production.progress # Make sure the producer used the boards assert bb_storage.inventory[RES.BOARDS] == 0 # Save and reload game session = saveload(session) loadedbb = WorldObject.get_object_by_id(worldid) production_loaded = loadedbb.get_component(Producer)._productions[PRODUCTIONLINES.HUKER] production_line_loaded = production_loaded._prod_line # Make sure everything is loaded correctly assert expected_consumed_res == production_line_loaded.consumed_res assert expected_produced_res == production_line_loaded.produced_res assert expected_production == production_line_loaded.production assert expected_progress == production_loaded.progress # if you don't let the session run for a bit then collectors won't be fully initialized and can't be killed => another test will fail in session.end() session.run(seconds=1) session.end()
def test_hunter_save_load(): """Save/loading hunter in different states""" session, player = new_session() settlement, island = settle(session) # setup hunter, trees (to keep animals close) and animals hunter = Build(BUILDINGS.HUNTER, 30, 30, island, settlement=settlement)(player) hunter_worldid = hunter.worldid del hunter # invalid after save/load for x in xrange(27, 29): for y in xrange(25, 28): assert Build(BUILDINGS.TREE, x, y, island, settlement=settlement)(player) CreateUnit(island.worldid, UNITS.WILD_ANIMAL, 27, 27)(issuer=None) CreateUnit(island.worldid, UNITS.WILD_ANIMAL, 28, 27)(issuer=None) CreateUnit(island.worldid, UNITS.WILD_ANIMAL, 29, 27)(issuer=None) def get_hunter_collector(session): hunter = WorldObject.get_object_by_id(hunter_worldid) return hunter.get_component( CollectingComponent)._CollectingComponent__collectors[0] def await_transition(session, collector, old_state, new_state): assert collector.state == old_state, "expected old state %s, got %s" % ( old_state, collector.state) while collector.state == old_state: session.run(seconds=1) assert collector.state == new_state, "expected new state %s, got %s" % ( old_state, collector.state) sequence = [ Collector.states.idle, Collector.states.waiting_for_animal_to_stop, Collector.states.moving_to_target, Collector.states.working, Collector.states.moving_home, Collector.states.idle ] # do full run without saveload collector = get_hunter_collector(session) for i in xrange(len(sequence) - 1): await_transition(session, collector, sequence[i], sequence[i + 1]) # do full run with saveload for i in xrange(len(sequence) - 1): collector = get_hunter_collector(session) await_transition(session, collector, sequence[i], sequence[i + 1]) session = saveload(session) # last state reached successfully 2 times -> finished session.end()
def test_hunter_save_load(): """Save/loading hunter in different states""" session, player = new_session() settlement, island = settle(session) # setup hunter, trees (to keep animals close) and animals hunter = Build(BUILDINGS.HUNTER, 30, 30, island, settlement=settlement)(player) hunter_worldid = hunter.worldid del hunter # invalid after save/load for x in xrange(27, 29): for y in xrange(25, 28): assert Build(BUILDINGS.TREE, x, y, island, settlement=settlement)(player) CreateUnit(island.worldid, UNITS.WILD_ANIMAL, 27, 27)(issuer=None) CreateUnit(island.worldid, UNITS.WILD_ANIMAL, 28, 27)(issuer=None) CreateUnit(island.worldid, UNITS.WILD_ANIMAL, 29, 27)(issuer=None) def get_hunter_collector(session): hunter = WorldObject.get_object_by_id(hunter_worldid) return hunter.get_component(CollectingComponent)._CollectingComponent__collectors[0] def await_transition(session, collector, old_state, new_state): assert collector.state == old_state, "expected old state %s, got %s" % (old_state, collector.state) while collector.state == old_state: session.run(seconds=1) assert collector.state == new_state, "expected new state %s, got %s" % (old_state, collector.state) sequence = [ Collector.states.idle, Collector.states.waiting_for_animal_to_stop, Collector.states.moving_to_target, Collector.states.working, Collector.states.moving_home, Collector.states.idle ] # do full run without saveload collector = get_hunter_collector(session) for i in xrange(len(sequence)-1): await_transition(session, collector, sequence[i], sequence[i+1]) # do full run with saveload for i in xrange(len(sequence)-1): collector = get_hunter_collector(session) await_transition(session, collector, sequence[i], sequence[i+1]) session = saveload(session) # last state reached successfully 2 times -> finished session.end()
def test_settler_level_save_load(s, p): """ Verify that settler level up with save/load works """ # test all available upgrades: 0->1, 1->2, 2->3... for test_level in range(TIER.CURRENT_MAX): session, player = new_session() settlement, island = settle(s) settler = Build(BUILDINGS.RESIDENTIAL, 22, 22, island, settlement=settlement)(p) settler.level += test_level settler._update_level_data(True, True) settler_worldid = settler.worldid settlement.tax_settings[settler.level] = -0.5 # make it happy inv = settler.get_component(StorageComponent).inventory to_give = inv.get_free_space_for(RES.HAPPINESS) inv.alter(RES.HAPPINESS, to_give) settler.inhabitants = settler.inhabitants_max level = settler.level # wait for it to realize it's supposed to upgrade s.run(seconds=GAME.INGAME_TICK_INTERVAL) assert settler.level == level session = saveload(session) settler = WorldObject.get_object_by_id(settler_worldid) inv = settler.get_component(StorageComponent).inventory assert settler.level == level # continue s.run(seconds=GAME.INGAME_TICK_INTERVAL) assert inv[RES.BOARDS] == 0 assert inv[RES.BRICKS] == 0 assert settler.level == level # give upgrade res inv.alter(RES.BOARDS, 100) inv.alter(RES.BRICKS, 100) # give it max population settler.inhabitants = settler.inhabitants_max s.run(seconds=GAME.INGAME_TICK_INTERVAL) # should have leveled up assert settler.level == level + 1
def test_settler_level_save_load(s, p): """ Verify that settler level up with save/load works """ # test all available upgrades: 0->1, 1->2, 2->3... for test_level in xrange(TIER.CURRENT_MAX): session, player = new_session() settlement, island = settle(s) settler = Build(BUILDINGS.RESIDENTIAL, 22, 22, island, settlement=settlement)(p) settler.level += test_level settler_worldid = settler.worldid # make it happy inv = settler.get_component(StorageComponent).inventory to_give = inv.get_free_space_for(RES.HAPPINESS) inv.alter(RES.HAPPINESS, to_give) level = settler.level # wait for it to realize it's supposed to upgrade s.run(seconds=GAME.INGAME_TICK_INTERVAL) session = saveload(session) settler = WorldObject.get_object_by_id(settler_worldid) inv = settler.get_component(StorageComponent).inventory # continue s.run(seconds=GAME.INGAME_TICK_INTERVAL) assert settler.level == level # give upgrade res inv.alter(RES.BOARDS, 100) inv.alter(RES.BRICKS, 100) # give it max population settler.inhabitants = settler.inhabitants_max s.run(seconds=GAME.INGAME_TICK_INTERVAL) # should have leveled up assert settler.level == level + 1
def create_lumberjack_production_session(): """Create a saved game with a producing production and then load it.""" session, player = new_session() settlement, island = settle(session) for x in [29, 30, 31, 32]: Build(BUILDINGS.TREE, x, 29, island, settlement=settlement,)(player) building = Build(BUILDINGS.LUMBERJACK, 30, 30, island, settlement=settlement)(player) production = building.get_component(Producer).get_productions()[0] # wait for the lumberjack to start producing while True: if production.get_state() is PRODUCTION.STATES.producing: break session.run(ticks=1) # Save and reload game session = saveload(session) return session
def test_combat_save_load(): """ create a savegame with combat units and actual combat, then save/load it """ session, player = new_session() (p0, s0), (p1, s1) = setup_combat(session, UNITS.FRIGATE) s0_worldid, s1_worldid = s0.worldid, s1.worldid session.run(seconds=1) # saveload fd, filename = tempfile.mkstemp() os.close(fd) assert session.save(savegamename=filename) session.end(keep_map=True) session = load_session(filename) s0 = WorldObject.get_object_by_id(s0_worldid) s1 = WorldObject.get_object_by_id(s1_worldid) # fight AddEnemyPair(p0, p1).execute(session) Attack(s0, s1).execute(session) Attack(s1, s0).execute(session) session.run(seconds=20) # saveload fd, filename = tempfile.mkstemp() os.close(fd) assert session.save(savegamename=filename) session.end(keep_map=True) session = load_session(filename) assert one_dead(s0_worldid, s1_worldid) session.end()
def test_settler_level_save_load(s, p): """ Verify that settler level up with save/load works """ for test_level in xrange(3): # test upgrade 0->1, 1->2 and 2->3 session, player = new_session() settlement, island = settle(s) settler = Build(BUILDINGS.RESIDENTIAL, 22, 22, island, settlement=settlement)(p) settler.level += test_level settler_worldid = settler.worldid # make it happy inv = settler.get_component(StorageComponent).inventory to_give = inv.get_free_space_for(RES.HAPPINESS) inv.alter(RES.HAPPINESS, to_give) level = settler.level # wait for it to realize it's supposed to upgrade s.run(seconds=GAME.INGAME_TICK_INTERVAL) session = saveload(session) settler = WorldObject.get_object_by_id(settler_worldid) inv = settler.get_component(StorageComponent).inventory # continue s.run(seconds=GAME.INGAME_TICK_INTERVAL) assert settler.level == level # give upgrade res inv.alter(RES.BOARDS, 100) inv.alter(RES.BRICKS, 100) # give it max population settler.inhabitants = settler.inhabitants_max s.run(seconds=GAME.INGAME_TICK_INTERVAL) # should have leveled up assert settler.level == level + 1
def create_lumberjack_production_session(): """Create a saved game with a producing production and then load it.""" session, player = new_session() settlement, island = settle(session) for x in [29, 30, 31, 32]: Build(BUILDINGS.TREE, x, 29, island, settlement=settlement,)(player) building = Build(BUILDINGS.LUMBERJACK, 30, 30, island, settlement=settlement)(player) production = building.get_component(Producer).get_productions()[0] # wait for the lumberjack to start producing while True: if production.get_state() is PRODUCTION.STATES.producing: break session.run(ticks=1) fd1, filename1 = tempfile.mkstemp() os.close(fd1) assert session.save(savegamename=filename1) session.end(keep_map=True) # load the game session = load_session(filename1) return session