def initialize_world(SCREEN_SIZE): ''' returns a world object that has completed basic init ''' world = World(SCREEN_SIZE) load_images(world) return world
def worker(self, args: WorkerArgs): logging.info(f'World {args.world_name} has been started') if args.world_constants: world = World(args.world_constants) else: world = serializer.load(args.world_save) analyzer = MammothAnalyzer(world) world_start_time = world.time start_time = prev_save_time = time.perf_counter() while True: if world.time % self.save_world_each == 0: now = time.perf_counter() total_elapsed = now - start_time total_performance = total_elapsed / ((world.time - world_start_time) or 1) elapsed = now - prev_save_time performance = elapsed / self.save_world_each logging.info( f'{args.world_name}: ' f'{world.time} wtime, ' f'{elapsed:.3f}s elapsed, ' f'{performance:.6f} performance, ' f'{total_elapsed:.3f}s total elapsed, ' f'{total_performance:.6f} total performance.' ) self._save_world(world, args.snapshot_dir) prev_save_time = now world.update() analyzer.update() if analyzer.amount_of_killings > 0.01: logging.info(f'World {args.world_name} got reaction at {world.time}') break if self.max_cycle and world.time >= self.max_cycle: break total_performance = (time.perf_counter() - start_time) / ((world.time - world_start_time) or 1) self._save_world(world, args.snapshot_dir) logging.info(f'World {args.world_name} has finished with average performance: {total_performance}')
def death(self, owner): return owner.hp <= 0 def addEnemy(): E = EnemyD(world, 3) E.target = E.cen() world.entityList.add(E) #Initialize variables txt = None with open('engine/testMap.txt', 'r') as file: txt = file.readlines() world = World(txt) world.player.speed = 4 world.player.acts = [attackPlayer, death] world.player['beard'] = 'mustache' world.player['right hand'] = 'spear' world.player['left hand'] = 'arrow' for i in range(12): addEnemy() camera = Camera(background, world, 1) # profile = cProfile.Profile() # profile.enable()
def do_work(connection, channel, delivery_tag, job): global SIGTERM global SIGUSR1 _LOGGER.info( f'Worker begin. Delivery tag: {delivery_tag}. Raw job: {job!r}') # Parse job job = json.loads(job) snapshot_dir = job['snapshot_dir'] latest_tick = job.get('latest_tick') cycle_amount = job.get('cycle_amount', 1000) max_cycle = job.get('max_cycle', 10_000) world_constants_override = job.get('constants') # Load or Create world if latest_tick: save_path = os.path.join(snapshot_dir, f'{latest_tick}.wrld') _LOGGER.info(f'Loading from {save_path}') world = serializer.load(save_path) else: _LOGGER.info(f'Creating new world {snapshot_dir}') world_constants = WorldConstants() if world_constants_override: new_dict = { **world_constants.to_dict(False), **world_constants_override } world_constants = WorldConstants.from_dict(new_dict) world = World(world_constants) save_path = os.path.join(snapshot_dir, '0.wrld') serializer.save(world, save_path) world_start_time = world.time stop_world = False _LOGGER.info( f'World {save_path} calculating for {cycle_amount}. max_cycle {max_cycle}' ) start_time = time.time() # Calculate world for _ in range(cycle_amount): world.update() if world.time >= max_cycle or len(world.animals) == 0: stop_world = True break if SIGTERM is True: _LOGGER.warning("SIGTERM received in worker. Finishing it.") break if SIGUSR1 is True: _LOGGER.info(f"Current world {save_path} time is {world.time}") SIGUSR1 = False # Analyzing performance elapsed = time.time() - start_time performance = elapsed / ((world.time - world_start_time) or 1) _LOGGER.info( f'World: {save_path}, calculated: {world.time - world_start_time} ticks, ' f'world.time: {world.time} ticks, elapsed: {elapsed:.3f}s, performance: {performance:.6f} s/tick' ) # Saving world save_path = os.path.join(snapshot_dir, f'{world.time}.wrld') _LOGGER.info(f'Saving {save_path}') serializer.save(world, save_path) # Preparing new job if not stop_world: job['latest_tick'] = world.time new_job = json.dumps(job) scale_down = False else: _LOGGER.info(f'World {save_path} is finished') new_job = None scale_down = True _LOGGER.info( f'Worker done. Delivery tag: {delivery_tag}. new_message: {new_job}') cb = functools.partial(ack_message, channel, delivery_tag, new_message=new_job, scale_down=scale_down) connection.add_callback_threadsafe(cb)
from engine.world import World import pygame from engine.constants import STATS_WIDTH import cProfile profiler = cProfile.Profile() profiler.enable() w = World(map_name='map_duel') w.load() DISPLAY = (w.width, w.height) pygame.init() pygame.display.set_caption(w.name) myfont = pygame.font.SysFont("timesnewroman", 15) flags = pygame.DOUBLEBUF | pygame.HWSURFACE screen = pygame.display.set_mode(DISPLAY, flags) # This has to be done somewhere inside world loading # It`s applying keyboard mode to the only agent w.agents[0].name = 'JGbot' #w.agents[1].name = '1100' #w.agents[2].name = '1600' w.agents[1].name = 'Random' w.agents[0].load('shared_models/DQN52794.h5') w.agents[0].to_learn = False w.agents[0].epsilon = 1 w.agents[0].delta = 1
class Builder(Actor): def __init__(self): self.has_started = False def replicate(self) -> 'Builder': return Builder() def act(self, sight: List[View]) -> Action: action = Action.WORK if self.has_started else Action.START_REPLICA self.has_started = True return action if __name__ == "__main__": world = World(Vector(12, 12)) position = Vector(4, 4) world.insert(Builder(), position, Direction.BACKWARD) position = Direction.BACKWARD.translate_vector(position) world.insert(Resource(), position, Direction.BACKWARD) position = Direction.BACKWARD.translate_vector(position) world.insert(Resource(), position, Direction.BACKWARD) renderer = Renderer(world) interactor = Interactor(world) renderer.render() for _ in range(12): interactor.tick() renderer.render()
from engine.world import World import pygame from engine.constants import STATS_WIDTH import cProfile profiler = cProfile.Profile() profiler.enable() w = World(map_name='map2') w.load() DISPLAY = (w.width, w.height) pygame.init() pygame.display.set_caption(w.name) myfont = pygame.font.SysFont("timesnewroman", 15) flags = pygame.DOUBLEBUF | pygame.HWSURFACE screen = pygame.display.set_mode(DISPLAY, flags) # This has to be done somewhere inside world loading w.agents[0].name = 'SSbot' w.agents[1].name = 'Target1' w.agents[2].name = 'Target2' w.agents[3].name = 'Target3' w.agents[4].name = 'Target4' w.spawns = [50, 490, 50, 320] w.episode_duration = 1000
return owner.hp <= 0 def addEnemy(): E = EnemyD(world, 3) E.target = E.cen() world.entityList.add(E) #Initialize variables txt = None with open('engine/testMap.txt', 'r') as file: txt = file.readlines() world = World(txt) world.player.speed = 4 world.player.acts = [attackPlayer, death] world.player['beard'] = 'mustache' world.player['right hand'] = 'spear' world.player['left hand'] = 'arrow' for i in range(12): addEnemy() camera = Camera(background, world, 1) # profile = cProfile.Profile() # profile.enable()
import resource import sys from PyQt5.QtWidgets import QApplication from engine.world import World from engine.world_constants import WorldConstants from ui.main_window import MainWindow if __name__ == "__main__": # Increasing size of stack to be able to pickle print(resource.getrlimit(resource.RLIMIT_STACK)) print(sys.getrecursionlimit()) max_rec = 0x100000 # May segfault without this line. 0x100 is a guess at the size of each stack frame. # resource.setrlimit(resource.RLIMIT_STACK, [0x100 * max_rec, resource.RLIM_INFINITY]) sys.setrecursionlimit(max_rec) world_constants = WorldConstants() save_genealogy = False world = World(constants=world_constants, save_genealogy=save_genealogy) app = QApplication(sys.argv) mySW = MainWindow(world) mySW.show() sys.exit(app.exec_())