def create_effects(item, pos, real_z_pos, z_min): for _z in range(0, 2): _z_level = numbers.clip(z_min-_z, 0, maputils.get_map_size(WORLD_INFO['map'])[2]-1) if WORLD_INFO['map'][pos[0]][pos[1]][_z_level]: if int(round(real_z_pos))-_z_level<=2: if 'BLOODY' in item['flags']: if random.randint(0,50)<=35: effects.create_splatter('blood', [pos[0]+random.randint(-2, 2), pos[1]+random.randint(-2, 2), _z_level]) break
def create_effects(item, pos, real_z_pos, z_min): for _z in range(0, 2): _z_level = bad_numbers.clip( z_min - _z, 0, maputils.get_map_size(WORLD_INFO['map'])[2] - 1) if WORLD_INFO['map'][pos[0]][pos[1]][_z_level]: if int(round(real_z_pos)) - _z_level <= 2: if 'BLOODY' in item['flags']: if random.randint(0, 50) <= 35: effects.create_splatter('blood', [ pos[0] + random.randint(-2, 2), pos[1] + random.randint(-2, 2), _z_level ]) if 'SMOKING' in item['flags']: if random.randint(0, 50) <= 25: effects.create_smoke_streamer( [ pos[0] + random.randint(-item['size'], item['size']), pos[1] + random.randint(-item['size'], item['size']), _z_level ], item['size'] / 2, random.randint(item['size'] * 2, (item['size'] * 2) + 5)) if 'BURNING' in item['flags']: if random.randint(0, 50) <= 25: effects.create_smoke_cloud([ pos[0] + random.randint(-item['size'], item['size']), pos[1] + random.randint(-item['size'], item['size']), _z_level ], random.randint( item['size'], (item['size']) + 3), color=tcod.light_crimson) if 'max_speed' in item and is_moving(item): effects.create_vapor( item['pos'], 5, bad_numbers.clip(item['speed'] / 20, 0, 1))
def create_effects(item, pos, real_z_pos, z_min): for _z in range(0, 2): _z_level = bad_numbers.clip(z_min-_z, 0, maputils.get_map_size(WORLD_INFO['map'])[2]-1) if WORLD_INFO['map'][pos[0]][pos[1]][_z_level]: if int(round(real_z_pos))-_z_level<=2: if 'BLOODY' in item['flags']: if random.randint(0,50)<=35: effects.create_splatter('blood', [pos[0]+random.randint(-2, 2), pos[1]+random.randint(-2, 2), _z_level]) if 'SMOKING' in item['flags']: if random.randint(0, 50)<=25: effects.create_smoke_streamer([pos[0]+random.randint(-item['size'], item['size']), pos[1]+random.randint(-item['size'], item['size']), _z_level], item['size']/2, random.randint(item['size']*2, (item['size']*2)+5)) if 'BURNING' in item['flags']: if random.randint(0, 50)<=25: effects.create_smoke_cloud([pos[0]+random.randint(-item['size'], item['size']), pos[1]+random.randint(-item['size'], item['size']), _z_level], random.randint(item['size'], (item['size'])+3), color=tcod.light_crimson) if 'max_speed' in item and is_moving(item): effects.create_vapor(item['pos'], 5, bad_numbers.clip(item['speed']/20, 0, 1))
def tick_item(item): if 'CAN_BURN' in item['flags'] and item['burning'] and item['owner']: life.burn(LIFE[item['owner']], item['burning']) if 'stored_in' in item or is_item_owned(item['uid']): return False _z_max = numbers.clip(item['pos'][2], 0, maputils.get_map_size(WORLD_INFO['map'])[2]-1) if item['velocity'][:2] == [0.0, 0.0] and WORLD_INFO['map'][item['pos'][0]][item['pos'][1]][_z_max]: return False _x = item['pos'][0] _y = item['pos'][1] #_view = gfx.get_view_by_name('map') #if 0<=_x<_view['draw_size'][0] and 0<=_y<_view['draw_size'][1]: if gfx.position_is_in_frame((_x, _y)): gfx.refresh_view_position(_x-CAMERA_POS[0], _y-CAMERA_POS[1], 'map') item['realpos'][0] += item['velocity'][0] item['realpos'][1] += item['velocity'][1] _break = False _line = drawing.diag_line(item['pos'],(int(round(item['realpos'][0])),int(round(item['realpos'][1])))) if not _line: item['velocity'][2] -= item['gravity'] item['realpos'][2] = item['realpos'][2]+item['velocity'][2] item['pos'][2] = int(round(item['realpos'][2])) if item['pos'][0]<0 or item['pos'][0]>=MAP_SIZE[0] or item['pos'][1]<0 or item['pos'][1]>=MAP_SIZE[1]: delete_item(item) return False _z_min = numbers.clip(int(round(item['realpos'][2])), 0, maputils.get_map_size(WORLD_INFO['map'])[2]-1) if collision_with_solid(item, [item['pos'][0], item['pos'][1], _z_min]): pos = item['pos'][:] _break = True create_effects(item, item['pos'], item['realpos'][2], _z_min) for pos in _line: item['realpos'][2] += item['velocity'][2] item['velocity'][2] -= item['velocity'][2]*item['gravity'] if 'drag' in item: _drag = item['drag'] else: _drag = item['gravity'] logging.warning('Improper use of gravity.') _min_x_vel, _min_y_vel, _max_x_vel, _max_y_vel = get_min_max_velocity(item) if 0<item['velocity'][0]<0.1 or -.1<item['velocity'][0]<0: item['velocity'][0] = 0 if 0<item['velocity'][1]<0.1 or -.1<item['velocity'][1]<0: item['velocity'][1] = 0 item['velocity'][0] -= numbers.clip(item['velocity'][0]*_drag, _min_x_vel, _max_x_vel) item['velocity'][1] -= numbers.clip(item['velocity'][1]*_drag, _min_y_vel, _max_y_vel) item['speed'] -= numbers.clip(item['speed']*_drag, 0, 100) if 0>pos[0] or pos[0]>=MAP_SIZE[0] or 0>pos[1] or pos[1]>=MAP_SIZE[1] or item['realpos'][2]<0 or item['realpos'][2]>=MAP_SIZE[2]-1: logging.warning('Item OOM: %s', item['uid']) delete_item(item) return False if collision_with_solid(item, [pos[0], pos[1], int(round(item['realpos'][2]))]): if item['type'] == 'bullet': effects.create_light(item['pos'], (255, 0, 0), 9, 0, fade=0.1) logging.debug('Item #%s hit a wall.' % item['uid']) return False if item['type'] == 'bullet': for _life in [LIFE[i] for i in LIFE]: if _life['id'] == item['shot_by'] or _life['dead']: continue if _life['pos'][0] == pos[0] and _life['pos'][1] == pos[1] and _life['pos'][2] == int(round(item['realpos'][2])): remove_from_chunk(item) item['pos'] = [pos[0],pos[1],_life['pos'][2]] add_to_chunk(item) life.damage_from_item(_life,item,60) if item['uid'] in ITEMS: delete_item(item) return False if _break: break #_z_max = numbers.clip(int(round(item['realpos'][2]))+1, 0, maputils.get_map_size(WORLD_INFO['map'])[2]-1) #if MAP[pos[0]][pos[1]][_z_max]: # item['velocity'][0] = 0 # item['velocity'][1] = 0 # item['velocity'][2] = 0 # item['pos'] = [pos[0],pos[1],item['pos'][2]-1] # # print 'LANDED',item['pos'] # _break = True # break _z_min = numbers.clip(int(round(item['realpos'][2])), 0, maputils.get_map_size(WORLD_INFO['map'])[2]-1) if collision_with_solid(item, [pos[0], pos[1], _z_min]): _break = True break create_effects(item, pos, item['realpos'][2], _z_min) remove_from_chunk(item) if _break: item['pos'][0] = int(pos[0]) item['pos'][1] = int(pos[1]) item['pos'][2] = int(round(item['realpos'][2])) else: item['pos'][0] = int(round(item['realpos'][0])) item['pos'][1] = int(round(item['realpos'][1])) item['pos'][2] = int(round(item['realpos'][2])) add_to_chunk(item) _x = item['pos'][0] _y = item['pos'][1] if gfx.position_is_in_frame((_x, _y)): gfx.refresh_view_position(_x-CAMERA_POS[0], _y-CAMERA_POS[1], 'map') if item['pos'][0] < 0 or item['pos'][0] > MAP_SIZE[0] \ or item['pos'][1] < 0 or item['pos'][1] > MAP_SIZE[1]: delete_item(item) return False #elif _break: # maps.refresh_chunk(life.get_current_chunk_id(item)) _min_x_vel, _min_y_vel, _max_x_vel, _max_y_vel = get_min_max_velocity(item) if 0<item['velocity'][0]<0.1 or -.1<item['velocity'][0]<0: item['velocity'][0] = 0 if 0<item['velocity'][1]<0.1 or -.1<item['velocity'][1]<0: item['velocity'][1] = 0 #TODO: This isn't gravity... if 'drag' in item: _drag = item['drag'] else: _drag = item['gravity'] logging.warning('Improper use of gravity.') item['velocity'][0] -= numbers.clip(item['velocity'][0]*_drag, _min_x_vel, _max_x_vel) item['velocity'][1] -= numbers.clip(item['velocity'][1]*_drag, _min_y_vel, _max_y_vel) item['speed'] -= numbers.clip(item['speed']*_drag, 0, 100)
def save_map(map_name, base_dir=MAP_DIR, only_cached=True): _map_dir = os.path.join(base_dir, map_name) #if base_dir == DATA_DIR: # _map_dir = os.path.join(_map_dir, map_name) try: os.makedirs(_map_dir) except: pass for light in WORLD_INFO['lights']: if 'los' in light: del light['los'] if 'old_pos' in light: del light['old_pos'] with open(os.path.join(_map_dir, 'world.meta'), 'w') as _map_file: try: _slices = WORLD_INFO['slices'] _references = WORLD_INFO['references'] _chunk_map = WORLD_INFO['chunk_map'] _map = WORLD_INFO['map'] _weather_light_map = None del WORLD_INFO['slices'] del WORLD_INFO['chunk_map'] del WORLD_INFO['references'] del WORLD_INFO['map'] WORLD_INFO['map_size'] = maputils.get_map_size(_map) if 'light_map' in WORLD_INFO['weather']: _weather_light_map = WORLD_INFO['weather']['light_map'] del WORLD_INFO['weather']['light_map'] logging.debug('Writing map metadata to disk...') _map_file.write('world_info:%s\n' % json.dumps(WORLD_INFO)) for _slice in _slices.keys(): if '_map' in _slices[_slice]: del _slices[_slice]['_map'] _map_file.write('slice:%s:%s\n' % (_slice, json.dumps(_slices[_slice]))) for _chunk_key in _chunk_map: _map_file.write( 'chunk:%s:%s\n' % (_chunk_key, json.dumps(_chunk_map[_chunk_key]))) #_map_file.write('slice_map:%s' % json.dumps(_slice_map)) WORLD_INFO['slices'] = _slices WORLD_INFO['chunk_map'] = _chunk_map WORLD_INFO['references'] = _references WORLD_INFO['map'] = _map #WORLD_INFO['slice_map'] = _slice_map if _weather_light_map: WORLD_INFO['weather']['light_map'] = _weather_light_map #logging.debug('Reloading slices...') #reload_slices() #logging.debug('Done!') except TypeError as e: logging.critical('FATAL: Map not JSON serializable.') gfx.log( 'TypeError: Failed to save map (Map not JSON serializable).') raise e _chunk_cluster_size = WORLD_INFO['chunk_size'] * 10 _map = WORLD_INFO['map'] del WORLD_INFO['map'] if only_cached: _cluster_keys = LOADED_CHUNKS else: _cluster_keys = [] for y1 in range(0, MAP_SIZE[1], _chunk_cluster_size): for x1 in range(0, MAP_SIZE[0], _chunk_cluster_size): _cluster_keys.append('%s,%s' % (x1, y1)) for cluster_key in _cluster_keys: _x1 = int(cluster_key.split(',')[0]) _y1 = int(cluster_key.split(',')[1]) with open( os.path.join( _map_dir, 'world_%s.cluster' % cluster_key.replace(',', '_')), 'w') as _cluster_file: for y2 in range(_y1, _y1 + _chunk_cluster_size): for x2 in range(_x1, _x1 + _chunk_cluster_size): _cluster_file.write(json.dumps(_map[x2][y2]) + '\n') WORLD_INFO['map'] = _map SETTINGS['base_dir'] = _map_dir
def save_map(map_name, base_dir=MAP_DIR, only_cached=True): _map_dir = os.path.join(base_dir, map_name) #if base_dir == DATA_DIR: # _map_dir = os.path.join(_map_dir, map_name) try: os.makedirs(_map_dir) except: pass for light in WORLD_INFO['lights']: if 'los' in light: del light['los'] if 'old_pos' in light: del light['old_pos'] with open(os.path.join(_map_dir, 'world.meta'), 'w') as _map_file: try: _slices = WORLD_INFO['slices'] _references = WORLD_INFO['references'] _chunk_map = WORLD_INFO['chunk_map'] _map = WORLD_INFO['map'] _weather_light_map = None del WORLD_INFO['slices'] del WORLD_INFO['chunk_map'] del WORLD_INFO['references'] del WORLD_INFO['map'] WORLD_INFO['map_size'] = maputils.get_map_size(_map) if 'light_map' in WORLD_INFO['weather']: _weather_light_map = WORLD_INFO['weather']['light_map'] del WORLD_INFO['weather']['light_map'] logging.debug('Writing map metadata to disk...') _map_file.write('world_info:%s\n' % json.dumps(WORLD_INFO)) for _slice in _slices.keys(): if '_map' in _slices[_slice]: del _slices[_slice]['_map'] _map_file.write('slice:%s:%s\n' % (_slice, json.dumps(_slices[_slice]))) for _chunk_key in _chunk_map: _map_file.write('chunk:%s:%s\n' % (_chunk_key, json.dumps(_chunk_map[_chunk_key]))) #_map_file.write('slice_map:%s' % json.dumps(_slice_map)) WORLD_INFO['slices'] = _slices WORLD_INFO['chunk_map'] = _chunk_map WORLD_INFO['references'] = _references WORLD_INFO['map'] = _map #WORLD_INFO['slice_map'] = _slice_map if _weather_light_map: WORLD_INFO['weather']['light_map'] = _weather_light_map #logging.debug('Reloading slices...') #reload_slices() #logging.debug('Done!') except TypeError as e: logging.critical('FATAL: Map not JSON serializable.') gfx.log('TypeError: Failed to save map (Map not JSON serializable).') raise e _chunk_cluster_size = WORLD_INFO['chunk_size']*10 _map = WORLD_INFO['map'] del WORLD_INFO['map'] if only_cached: _cluster_keys = LOADED_CHUNKS else: _cluster_keys = [] for y1 in range(0, MAP_SIZE[1], _chunk_cluster_size): for x1 in range(0, MAP_SIZE[0], _chunk_cluster_size): _cluster_keys.append('%s,%s' % (x1, y1)) for cluster_key in _cluster_keys: _x1 = int(cluster_key.split(',')[0]) _y1 = int(cluster_key.split(',')[1]) with open(os.path.join(_map_dir, 'world_%s.cluster' % cluster_key.replace(',', '_')), 'w') as _cluster_file: for y2 in range(_y1, _y1+_chunk_cluster_size): for x2 in range(_x1, _x1+_chunk_cluster_size): _cluster_file.write(json.dumps(_map[x2][y2])+'\n') WORLD_INFO['map'] = _map SETTINGS['base_dir'] = _map_dir
def load_map(map_name, base_dir=DATA_DIR): _map_dir = os.path.join(base_dir,'maps') if not map_name.count('.dat'): map_name+='.dat' WORLD_INFO['map'] = [] with open(os.path.join(_map_dir,map_name),'r') as _map_file: #try: #WORLD_INFO.update(json.loads(' '.join(_map_file.readlines()))) for line in _map_file.readlines(): line = line.rstrip() value = line.split(':') if line.startswith('chunk'): WORLD_INFO['chunk_map'][value[1]] = json.loads(':'.join(value[2:])) elif line.startswith('map'): WORLD_INFO['map'].append(json.loads(':'.join(value[2:]))) elif line.startswith('slice'): WORLD_INFO['slices'][value[1]] = json.loads(':'.join(value[2:])) elif line.startswith('world_info'): WORLD_INFO.update(json.loads(':'.join(value[1:]))) if 'items' in WORLD_INFO: ITEMS.update(WORLD_INFO['items']) #if not (x, y) in zone['map']: #for slice _map_size = maputils.get_map_size(WORLD_INFO['map']) MAP_SIZE[0] = _map_size[0] MAP_SIZE[1] = _map_size[1] MAP_SIZE[2] = _map_size[2] reload_slices() WORLD_INFO['chunk_map'].update(WORLD_INFO['chunk_map']) alife.chunks.generate_cache() if WORLD_INFO['weather']: weather.create_light_map(WORLD_INFO['weather']) _map_size = maputils.get_map_size(WORLD_INFO['map']) for x in range(MAP_SIZE[0]): for y in range(MAP_SIZE[1]): for z in range(MAP_SIZE[2]): if not WORLD_INFO['map'][x][y][z]: continue for key in TILE_STRUCT_DEP: if key in WORLD_INFO['map'][x][y][z]: del WORLD_INFO['map'][x][y][z][key] for key in TILE_STRUCT: if not key in WORLD_INFO['map'][x][y][z]: WORLD_INFO['map'][x][y][z][key] = copy.copy(TILE_STRUCT[key]) zones.cache_zones() create_position_maps() logging.info('Map \'%s\' loaded.' % map_name) gfx.log('Map \'%s\' loaded.' % map_name) return True