<s> from globals import WORLD_INFO <EOL> import random <EOL> def walker ( chunk_key , steps , building_chunks , add_first = False , chunk_keys = True , avoid_chunk_keys = [ ] ) : <EOL> _pos = [ int ( i ) for i in chunk_key . split ( '<STR_LIT:U+002C>' ) ] <EOL> _path = [ ] <EOL> if add_first : <EOL> _path . append ( _pos [ : ] ) <EOL> for i in range ( steps - add_first ) : <EOL> _directions = [ ( - <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , - <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:1> ) ] <EOL> _valid_directions = [ ] <EOL> for mod in _directions : <EOL> _n_pos = ( _pos [ <NUM_LIT:0> ] + ( mod [ <NUM_LIT:0> ] * WORLD_INFO [ '<STR_LIT>' ] ) , _pos [ <NUM_LIT:1> ] + ( mod [ <NUM_LIT:1> ] * WORLD_INFO [ '<STR_LIT>' ] ) ) <EOL> _n_chunk_key = '<STR_LIT>' % ( _n_pos [ <NUM_LIT:0> ] , _n_pos [ <NUM_LIT:1> ] ) <EOL> if not _n_chunk_key in building_chunks : <EOL> continue <EOL> if _n_chunk_key in avoid_chunk_keys or _n_chunk_key in [ '<STR_LIT>' % ( p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] ) for p in _path ] : <EOL> continue <EOL> _valid_directions . append ( mod [ : ] ) <EOL> if not _valid_directions : <EOL> break <EOL> _mod = random . choice ( _valid_directions ) <EOL> _pos [ <NUM_LIT:0> ] += _mod [ <NUM_LIT:0> ] * WORLD_INFO [ '<STR_LIT>' ] <EOL> _pos [ <NUM_LIT:1> ] += _mod [ <NUM_LIT:1> ] * WORLD_INFO [ '<STR_LIT>' ] <EOL> _path . append ( _pos [ : ] ) <EOL> if chunk_keys : <EOL> return [ '<STR_LIT>' % ( p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] ) for p in _path ] <EOL> else : <EOL> return _path <EOL> def get_neighbors ( chunk_key , only_chunk_keys = [ ] , avoid_chunk_keys = [ ] ) : <EOL> _neighbors = [ ] <EOL> _pos = [ int ( i ) for i in chunk_key . split ( '<STR_LIT:U+002C>' ) ] <EOL> for pos in [ ( - <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , - <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:1> ) ] : <EOL> _chunk_key = '<STR_LIT>' % ( _pos [ <NUM_LIT:0> ] + ( pos [ <NUM_LIT:0> ] * WORLD_INFO [ '<STR_LIT>' ] ) , _pos [ <NUM_LIT:1> ] + ( pos [ <NUM_LIT:1> ] * WORLD_INFO [ '<STR_LIT>' ] ) ) <EOL> if only_chunk_keys and not _chunk_key in only_chunk_keys : <EOL> continue <EOL> if _chunk_key in avoid_chunk_keys : <EOL> continue <EOL> _neighbors . append ( _chunk_key ) <EOL> return _neighbors <EOL> def connect_to_chunks ( connect_to , existing_connections , steps , building_chunks ) : <EOL> _connect_layers = { } <EOL> _all_chunk_keys = [ ] <EOL> for room_name in existing_connections : <EOL> for chunk_key in existing_connections [ room_name ] [ '<STR_LIT>' ] : <EOL> _all_chunk_keys . append ( chunk_key ) <EOL> _allowed = False <EOL> for room_name in connect_to : <EOL> if not room_name in existing_connections : <EOL> continue <EOL> _neighbors = [ ] <EOL> for chunk_key in existing_connections [ room_name ] [ '<STR_LIT>' ] : <EOL> _temp_neighbors = get_neighbors ( chunk_key , only_chunk_keys = building_chunks , avoid_chunk_keys = _all_chunk_keys ) <EOL> if not _temp_neighbors : <EOL> return - <NUM_LIT:1> <EOL> _neighbors . extend ( _temp_neighbors ) <EOL> _connect_layers [ room_name ] = { '<STR_LIT>' : existing_connections [ room_name ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _neighbors } <EOL> if _neighbors : <EOL> _allowed = True <EOL> if not _allowed : <EOL> return - <NUM_LIT:1> <EOL> _common_neighbors = { } <EOL> _highest = <NUM_LIT:0> <EOL> for layer in _connect_layers . values ( ) : <EOL> for chunk_key in layer [ '<STR_LIT>' ] : <EOL> if chunk_key in _common_neighbors : <EOL> _common_neighbors [ chunk_key ] += <NUM_LIT:1> <EOL> else : <EOL> _common_neighbors [ chunk_key ] = <NUM_LIT:1> <EOL> if _common_neighbors [ chunk_key ] > _highest : <EOL> _highest = _common_neighbors [ chunk_key ] <EOL> for chunk_key in _common_neighbors . keys ( ) : <EOL> if _common_neighbors [ chunk_key ] < _highest : <EOL> del _common_neighbors [ chunk_key ] <EOL> if not _common_neighbors : <EOL> return False <EOL> if _highest < len ( _connect_layers ) : <EOL> return - <NUM_LIT:1> <EOL> return random . choice ( _common_neighbors . keys ( ) ) <EOL> def _create_building ( chunk_key , design , building_chunks ) : <EOL> _pos = chunk_key <EOL> _rooms = { } <EOL> _building = { } <EOL> _building_chunk_keys = [ ] <EOL> _build_queue = [ design [ '<STR_LIT>' ] ] <EOL> _to_build = design [ '<STR_LIT>' ] . keys ( ) <EOL> while _build_queue : <EOL> _room_name = _build_queue . pop ( <NUM_LIT:0> ) <EOL> _to_build . remove ( _room_name ) <EOL> _building [ _room_name ] = { '<STR_LIT>' : [ ] } <EOL> _room = design [ '<STR_LIT>' ] [ _room_name ] <EOL> if '<STR_LIT>' in _room : <EOL> _flags = _room [ '<STR_LIT>' ] <EOL> else : <EOL> _flags = { } <EOL> if _rooms : <EOL> _start_chunk = connect_to_chunks ( _room [ '<STR_LIT>' ] , _building , _room [ '<STR_LIT>' ] , building_chunks ) <EOL> if not _start_chunk : <EOL> return False <EOL> if _start_chunk == - <NUM_LIT:1> : <EOL> return - <NUM_LIT:1> <EOL> _path = walker ( _start_chunk , _room [ '<STR_LIT>' ] , building_chunks , avoid_chunk_keys = _building_chunk_keys , add_first = True ) <EOL> _rooms [ _room_name ] = _path [ : ] <EOL> _building [ _room_name ] = { '<STR_LIT>' : _rooms [ _room_name ] , <EOL> '<STR_LIT:type>' : _room [ '<STR_LIT:type>' ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _flags , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] } } <EOL> _building_chunk_keys . extend ( _rooms [ _room_name ] ) <EOL> else : <EOL> _rooms [ _room_name ] = walker ( _pos , _room [ '<STR_LIT>' ] , building_chunks , avoid_chunk_keys = _building_chunk_keys , add_first = True ) <EOL> _building_chunk_keys . extend ( _rooms [ _room_name ] ) <EOL> _building [ _room_name ] = { '<STR_LIT>' : _rooms [ _room_name ] , <EOL> '<STR_LIT:type>' : _room [ '<STR_LIT:type>' ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : _room [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _flags , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] } } <EOL> if _room [ '<STR_LIT>' ] : <EOL> _build_queue . extend ( [ d for d in _room [ '<STR_LIT>' ] if d in _to_build and not d in _build_queue ] ) <EOL> return _building <EOL> def create_building ( chunk_key , design , building_chunks ) : <EOL> _building = None <EOL> while not _building : <EOL> _building = _create_building ( chunk_key , design , building_chunks ) <EOL> if _building == - <NUM_LIT:1> : <EOL> return False <EOL> return _building </s>
