def save_world(create=False): gfx.title('Saving...') logging.debug('Offloading world...') if create: maps.cache_all_clusters() _config_directory, _worlds_directory = profiles.has_reactor3() _version_file = os.path.join(_worlds_directory, WORLD_INFO['id'], 'version.txt') with open(_version_file, 'w') as version_file: version_file.write(VERSION) logging.debug('Saving items...') items.save_all_items() maps.save_map('map', base_dir=profiles.get_world_directory(WORLD_INFO['id'])) logging.debug('Saving life...') _life = life.save_all_life() with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'life.dat'), 'w') as e: e.write(_life) with open(os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items.dat'), 'w') as e: e.write(json.dumps(ITEMS)) #cache.commit_cache('items') #cache.save_cache('items') items.reload_all_items() logging.info('World saved.')
def commands(): global MAP if controls.key_pressed('s'): maps.save_map('test', MAP) elif controls.key_pressed('l'): MAP = maps.load_map('test')
def save_world(create=False): gfx.title('Saving...') logging.debug('Offloading world...') if create: maps.cache_all_clusters() _config_directory, _worlds_directory = profiles.has_reactor3() _version_file = os.path.join(_worlds_directory, WORLD_INFO['id'], 'version.txt') with open(_version_file, 'w') as version_file: version_file.write(VERSION) logging.debug('Saving items...') items.save_all_items() maps.save_map('map', base_dir=profiles.get_world_directory(WORLD_INFO['id'])) logging.debug('Saving life...') _life = life.save_all_life() with open( os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'life.dat'), 'w') as e: e.write(_life) with open( os.path.join(profiles.get_world_directory(WORLD_INFO['id']), 'items.dat'), 'w') as e: e.write(json.dumps(ITEMS)) #cache.commit_cache('items') #cache.save_cache('items') items.reload_all_items() logging.info('World saved.')
if _old_size[2]>size[2]: for x1 in range(_old_size[0]): for y1 in range(_old_size[1]): for z in range(abs(_old_size[2]-size[2])): _new_map[x1][y1].pop() elif _old_size[2]<size[2]: for x1 in range(_old_size[0]): _y1 = [] for y1 in range(abs(_old_size[1])): _z1 = [] for z1 in range(abs(_old_size[2]-size[2])): _z1.append(None) _new_map[x1][y1].extend(_z1) return _new_map if __name__ == '__main__': if '--create' in sys.argv: create_all_tiles() WORLD_INFO['map'] = maps.create_map() maps.create_position_maps() maps.update_chunk_map() zones.create_zone_map() zones.connect_ramps() maps.save_map('temp_map') print 'Created map: temp_map.dat'
def run(Display, map_name, size): import logging Map_Data = maps.load_map(map_name) if Map_Data == False: Map_Data = maps.new_maps(size) Pos_Ecran_Actuelle = [0, 0] Size = (Map_Data["size"][0]*32, Map_Data["size"][1]*32) Default_Sprite = entity.Sprite(pygame.Surface((32, 32))) ScreenSize = Display.get_rect() Maps_Surface = pygame.Surface(Size) Menu_Width = int(config.CFG["mapeditor.menu_width"]) Menu_Surface = pygame.Surface((Menu_Width*32, ScreenSize.height)) Pos_Menu_Surface = Menu_Surface.get_rect() Pos_Menu_Surface.x = ScreenSize.width - Menu_Width*32 pos_curs = (50, 50) Scheduler = pygame.time.Clock() SpriteGroup = entity.EntityGroup() ListeSprite = [] Position = 0 for Sp_i in sorted(ressource.SPRITE): if Sp_i < 1000: continue Sp = ressource.SPRITE[Sp_i] Pos_x = (Position%Menu_Width) * 32 Pos_y = (Position//Menu_Width) * 32 Position += 1 Menu_Surface.blit(Sp.image, (Pos_x, Pos_y)) ListeSprite.append({"id":Sp.data["ID"]}) Texture_Séléctionné = 0 GameRun = True while GameRun: for event in pygame.event.get(): #Attente des événements if event.type == MOUSEMOTION: pos_curs = event.pos if event.type == MOUSEBUTTONDOWN: if event.button == 1: clic_x = event.pos[0] clic_y = event.pos[1] if clic_x > Pos_Menu_Surface.x: # Si clique dans le menu de gauche bloc_x = (clic_x - Pos_Menu_Surface.x) // 32 bloc_y = (clic_y) // 32 Texture_Séléctionné = bloc_y*Menu_Width + bloc_x if clic_x < Pos_Menu_Surface.x: bloc_x = (Pos_Ecran_Actuelle[0] + clic_x) // 32 bloc_y = (Pos_Ecran_Actuelle[1] + clic_y) // 32 Id = int(ListeSprite[Texture_Séléctionné]["id"]) if bloc_y>=0 and bloc_y<len(Map_Data["data"]): if bloc_x>=0 and bloc_x<len(Map_Data["data"][bloc_y]): Map_Data["data"][bloc_y][bloc_x] = Id logging.debug("Bloc cliqué: %s %s %s"%(bloc_x, bloc_y, Id)) if event.button == 2: clic_x = event.pos[0] clic_y = event.pos[1] if clic_x < Pos_Menu_Surface.x: bloc_x = (Pos_Ecran_Actuelle[0] + clic_x) // 32 bloc_y = (Pos_Ecran_Actuelle[1] + clic_y) // 32 Id = Map_Data["data"][bloc_y][bloc_x] for i in range(0, len(ListeSprite)): if Id == ListeSprite[i]["id"]: Texture_Séléctionné = i logging.debug("Bloc pipette: %s %s %s %s"%(bloc_x, bloc_y, Id, Texture_Séléctionné)) if event.type == QUIT: maps.save_map(map_name, Map_Data) GameRun = False if event.type == KEYDOWN: if event.key == K_LEFT: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (-15, 0)) if event.key == K_RIGHT: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (15, 0)) if event.key == K_UP: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (0, -15)) if event.key == K_DOWN: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (0, 15)) if event.key == K_s: maps.save_map(map_name, Map_Data) if pos_curs[0] < 10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (-5, 0)) if pos_curs[0] > config.CFG["screen.size"][0]-10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (5, 0)) if pos_curs[1] < 10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (0, -5)) if pos_curs[1] > config.CFG["screen.size"][1]-10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width*32, (0, 5)) y=0 for y in range(0, Map_Data["size"][1]): for x in range(0, Map_Data["size"][0]): try: sprite = ressource.SPRITE[ Map_Data["data"][y][x] ] except Exception: sprite = Default_Sprite Maps_Surface.blit(sprite.image, (x*32, y*32)) SpriteGroup.draw(Maps_Surface) Pos_inversé = (-Pos_Ecran_Actuelle[0], -Pos_Ecran_Actuelle[1]) Display.blit(Maps_Surface, Pos_inversé) Display.blit(Menu_Surface, Pos_Menu_Surface) Pos_x = ScreenSize.width - Menu_Width*32 + ((Texture_Séléctionné%Menu_Width) * 32) Pos_y = (Texture_Séléctionné//Menu_Width) * 32 Display.blit(ressource.SPRITE[ 200 ].image, (Pos_x, Pos_y)) Scheduler.tick_busy_loop(int(config.CFG["screen.fps"])) pygame.display.flip() Maps_Surface.fill(0) Display.fill(0)
if _old_size[2] > size[2]: for x1 in range(_old_size[0]): for y1 in range(_old_size[1]): for z in range(abs(_old_size[2] - size[2])): _new_map[x1][y1].pop() elif _old_size[2] < size[2]: for x1 in range(_old_size[0]): _y1 = [] for y1 in range(abs(_old_size[1])): _z1 = [] for z1 in range(abs(_old_size[2] - size[2])): _z1.append(None) _new_map[x1][y1].extend(_z1) return _new_map if __name__ == '__main__': if '--create' in sys.argv: create_all_tiles() WORLD_INFO['map'] = maps.create_map() maps.create_position_maps() maps.update_chunk_map() zones.create_zone_map() zones.connect_ramps() maps.save_map('temp_map') print 'Created map: temp_map.dat'
def run(Display, network, map_name): Map_Data = maps.load_map(map_name) Pos_Ecran_Actuelle = [0, 0] Size = (Map_Data["size"][0] * 32, Map_Data["size"][1] * 32) Maps_Surface = pygame.Surface(Size) ScreenSize = Display.get_rect() Menu_Width = 0 pos_curs = (50, 50) Default_Sprite = entity.Sprite(pygame.Surface((32, 32))) Scheduler = pygame.time.Clock() logging.info("Tentative de connection...") while True: if network.loginok: break time.sleep(0.1) logging.info("Connection ok...") network.join_room("serdtfyugiohjpk") while True: if network.room_joined: break time.sleep(0.1) network.create_entity(1, network.player_uuid, UUID.uuid4(), (random.randint(0, 10), random.randint(0, 10))) network.create_entity(1, network.player_uuid, UUID.uuid4(), (random.randint(0, 10), random.randint(0, 10))) logging.info("Lancement de la bouble principale...") Sprite_cliqué = False GameRun = True while GameRun: for event in pygame.event.get(): #Attente des événements if event.type == MOUSEMOTION: pos_curs = event.pos if event.type == MOUSEBUTTONDOWN: if event.button == 1: clic_x = event.pos[0] clic_y = event.pos[1] bloc_x = (Pos_Ecran_Actuelle[0] + clic_x) // 32 bloc_y = (Pos_Ecran_Actuelle[1] + clic_y) // 32 Sprite_cliqué = False for Player in network.Player_list.values(): for sprite in Player.Entity: sprite_x = sprite.rect.x // 32 sprite_y = sprite.rect.y // 32 if sprite_x == bloc_x and sprite_y == bloc_y: Sprite_cliqué = sprite logging.debug("Entitée cliqué: %s %s %s" % (bloc_x, bloc_y, Sprite_cliqué)) break if event.button == 3: clic_x = event.pos[0] clic_y = event.pos[1] bloc_x = (Pos_Ecran_Actuelle[0] + clic_x) // 32 bloc_y = (Pos_Ecran_Actuelle[1] + clic_y) // 32 if Sprite_cliqué: network.move_entity(Sprite_cliqué, (bloc_x, bloc_y)) logging.debug("Entitée déplacé: %s %s %s" % (bloc_x, bloc_y, Sprite_cliqué)) if event.type == QUIT: logging.info("Demande de fermeture...") GameRun = False if event.type == KEYDOWN: if event.key == K_LEFT: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (-15, 0)) if event.key == K_RIGHT: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (15, 0)) if event.key == K_UP: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (0, -15)) if event.key == K_DOWN: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (0, 15)) if event.key == K_s: maps.save_map(map_name, Map_Data) if pos_curs[0] < 10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (-5, 0)) if pos_curs[0] > config.CFG["screen.size"][0] - 10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (5, 0)) if pos_curs[1] < 10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (0, -5)) if pos_curs[1] > config.CFG["screen.size"][1] - 10: divers.move_screen(Pos_Ecran_Actuelle, Size, ScreenSize, Menu_Width * 32, (0, 5)) y = 0 for y in range(0, Map_Data["size"][1]): for x in range(0, Map_Data["size"][0]): try: sprite = ressource.SPRITE[Map_Data["data"][y][x]] except Exception: sprite = Default_Sprite Maps_Surface.blit(sprite.image, (x * 32, y * 32)) for Player in network.Player_list.values(): Player.Entity.draw(Maps_Surface) Pos_inversé = (-Pos_Ecran_Actuelle[0], -Pos_Ecran_Actuelle[1]) Display.blit(Maps_Surface, Pos_inversé) Scheduler.tick_busy_loop(int(config.CFG["screen.fps"])) pygame.display.flip() Maps_Surface.fill(0) Display.fill(0)
def handle_input(): global PLACING_TILE,RUNNING,SETTINGS,KEYBOARD_STRING if gfx.window_is_closed(): SETTINGS['running'] = False return True elif INPUT['\x1b'] or INPUT['q']: if not ACTIVE_MENU['menu'] == -1 and not SETTINGS['running'] == 1: ACTIVE_MENU['menu'] = -1 return True SETTINGS['running'] = False return True elif INPUT['-']: if SETTINGS['draw console']: SETTINGS['draw console'] = False else: SETTINGS['draw console'] = True elif SETTINGS['draw console']: return elif INPUT['up']: if not ACTIVE_MENU['menu'] == -1: menus.move_up(MENUS[ACTIVE_MENU['menu']], MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[1] -= SETTINGS['cursor speed'] elif INPUT['down']: if not ACTIVE_MENU['menu'] == -1: menus.move_down(MENUS[ACTIVE_MENU['menu']], MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[1] += SETTINGS['cursor speed'] elif INPUT['right']: if not ACTIVE_MENU['menu'] == -1: menus.next_item(MENUS[ACTIVE_MENU['menu']],MENUS[ACTIVE_MENU['menu']]['index']) menus.item_changed(ACTIVE_MENU['menu'],MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[0] += SETTINGS['cursor speed'] elif INPUT['left']: if not ACTIVE_MENU['menu'] == -1: menus.previous_item(MENUS[ACTIVE_MENU['menu']],MENUS[ACTIVE_MENU['menu']]['index']) menus.item_changed(ACTIVE_MENU['menu'],MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[0] -= SETTINGS['cursor speed'] elif INPUT[' ']: menus.activate_menu_by_name('Buildings') elif INPUT['n']: for alife in LIFE: life.memory(alife, 'focus_on_chunk', chunk_key=life.get_current_chunk_id(LIFE[SETTINGS['controlling']])) elif INPUT['\r']: if ACTIVE_MENU['menu'] == -1: return False menus.item_selected(ACTIVE_MENU['menu'],MENUS[ACTIVE_MENU['menu']]['index']) ACTIVE_MENU['menu'] = -1 elif INPUT['\t']: pass elif INPUT['c']: CURSOR_POS[0] = MAP_SIZE[0]/2 CURSOR_POS[1] = MAP_SIZE[1]/2 elif INPUT['m']: if SETTINGS['running'] == 2: gfx.add_view_to_scene_by_name('chunk_map') gfx.set_active_view('chunk_map') SETTINGS['running'] = 3 elif SETTINGS['running'] == 3: gfx.remove_view_from_scene_by_name('chunk_map') gfx.set_active_view('map') SETTINGS['running'] = 2 elif INPUT['r']: if regenerate_world(): SETTINGS['running'] = 2 elif INPUT['s']: items.save_all_items() gfx.title('Generating Chunk Map') maps.update_chunk_map() gfx.title('Zoning...') zones.create_zone_map() gfx.title('Saving...') maps.save_map(str(time.time()), only_cached=False) items.reload_all_items() elif INPUT['l']: SUN_BRIGHTNESS[0] += 4 elif INPUT['k']: SUN_BRIGHTNESS[0] -= 4 elif INPUT['1']: CAMERA_POS[2] = 1 gfx.refresh_view('map') elif INPUT['2']: CAMERA_POS[2] = 2 gfx.refresh_view('map') elif INPUT['3']: CAMERA_POS[2] = 3 gfx.refresh_view('map') elif INPUT['4']: CAMERA_POS[2] = 4 gfx.refresh_view('map') elif INPUT['5']: CAMERA_POS[2] = 5 gfx.refresh_view('map') elif INPUT['6']: CAMERA_POS[2] = 6 gfx.refresh_view('map') elif INPUT['7']: CAMERA_POS[2] = 7 gfx.refresh_view('map') elif INPUT['8']: CAMERA_POS[2] = 8 gfx.refresh_view('map') elif INPUT['9']: CAMERA_POS[2] = 9 gfx.refresh_view('map') elif INPUT['0']: CAMERA_POS[2] = 0 gfx.refresh_view('map')
def handle_input(): global PLACING_TILE, RUNNING, SETTINGS, KEYBOARD_STRING if gfx.window_is_closed(): SETTINGS['running'] = False return True elif INPUT['\x1b'] or INPUT['q']: if not ACTIVE_MENU['menu'] == -1 and not SETTINGS['running'] == 1: ACTIVE_MENU['menu'] = -1 return True SETTINGS['running'] = False return True elif INPUT['-']: if SETTINGS['draw console']: SETTINGS['draw console'] = False else: SETTINGS['draw console'] = True elif SETTINGS['draw console']: return elif INPUT['up']: if not ACTIVE_MENU['menu'] == -1: menus.move_up(MENUS[ACTIVE_MENU['menu']], MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[1] -= SETTINGS['cursor speed'] elif INPUT['down']: if not ACTIVE_MENU['menu'] == -1: menus.move_down(MENUS[ACTIVE_MENU['menu']], MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[1] += SETTINGS['cursor speed'] elif INPUT['right']: if not ACTIVE_MENU['menu'] == -1: menus.next_item(MENUS[ACTIVE_MENU['menu']], MENUS[ACTIVE_MENU['menu']]['index']) menus.item_changed(ACTIVE_MENU['menu'], MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[0] += SETTINGS['cursor speed'] elif INPUT['left']: if not ACTIVE_MENU['menu'] == -1: menus.previous_item(MENUS[ACTIVE_MENU['menu']], MENUS[ACTIVE_MENU['menu']]['index']) menus.item_changed(ACTIVE_MENU['menu'], MENUS[ACTIVE_MENU['menu']]['index']) else: CURSOR_POS[0] -= SETTINGS['cursor speed'] elif INPUT[' ']: menus.activate_menu_by_name('Buildings') elif INPUT['n']: for alife in LIFE: life.memory(alife, 'focus_on_chunk', chunk_key=life.get_current_chunk_id( LIFE[SETTINGS['controlling']])) elif INPUT['\r']: if ACTIVE_MENU['menu'] == -1: return False menus.item_selected(ACTIVE_MENU['menu'], MENUS[ACTIVE_MENU['menu']]['index']) ACTIVE_MENU['menu'] = -1 elif INPUT['\t']: pass elif INPUT['c']: CURSOR_POS[0] = MAP_SIZE[0] / 2 CURSOR_POS[1] = MAP_SIZE[1] / 2 elif INPUT['m']: if SETTINGS['running'] == 2: gfx.add_view_to_scene_by_name('chunk_map') gfx.set_active_view('chunk_map') SETTINGS['running'] = 3 elif SETTINGS['running'] == 3: gfx.remove_view_from_scene_by_name('chunk_map') gfx.set_active_view('map') SETTINGS['running'] = 2 elif INPUT['r']: if regenerate_world(): SETTINGS['running'] = 2 elif INPUT['s']: items.save_all_items() gfx.title('Generating Chunk Map') maps.update_chunk_map() gfx.title('Zoning...') zones.create_zone_map() gfx.title('Saving...') maps.save_map(str(time.time()), only_cached=False) items.reload_all_items() elif INPUT['l']: SUN_BRIGHTNESS[0] += 4 elif INPUT['k']: SUN_BRIGHTNESS[0] -= 4 elif INPUT['1']: CAMERA_POS[2] = 1 gfx.refresh_view('map') elif INPUT['2']: CAMERA_POS[2] = 2 gfx.refresh_view('map') elif INPUT['3']: CAMERA_POS[2] = 3 gfx.refresh_view('map') elif INPUT['4']: CAMERA_POS[2] = 4 gfx.refresh_view('map') elif INPUT['5']: CAMERA_POS[2] = 5 gfx.refresh_view('map') elif INPUT['6']: CAMERA_POS[2] = 6 gfx.refresh_view('map') elif INPUT['7']: CAMERA_POS[2] = 7 gfx.refresh_view('map') elif INPUT['8']: CAMERA_POS[2] = 8 gfx.refresh_view('map') elif INPUT['9']: CAMERA_POS[2] = 9 gfx.refresh_view('map') elif INPUT['0']: CAMERA_POS[2] = 0 gfx.refresh_view('map')
if _old_size[2]>size[2]: for x1 in range(_old_size[0]): for y1 in range(_old_size[1]): for z in range(abs(_old_size[2]-size[2])): _new_map[x1][y1].pop() elif _old_size[2]<size[2]: for x1 in range(_old_size[0]): _y1 = [] for y1 in range(abs(_old_size[1])): _z1 = [] for z1 in range(abs(_old_size[2]-size[2])): _z1.append(None) _new_map[x1][y1].extend(_z1) return _new_map if __name__ == '__main__': if '--create' in sys.argv: create_all_tiles() WORLD_INFO['map'] = maps.create_map() maps.create_position_maps() maps.update_chunk_map() zones.create_zone_map() zones.connect_ramps() maps.save_map('temp_map.dat') print 'Created map: temp_map.dat'