<s> from globals import * <EOL> import life as lfe <EOL> import references <EOL> import judgement <EOL> import language <EOL> import survival <EOL> import movement <EOL> import numbers <EOL> import speech <EOL> import chunks <EOL> import groups <EOL> import brain <EOL> import stats <EOL> import jobs <EOL> import logging <EOL> import random <EOL> def get_camp ( camp_id ) : <EOL> if not camp_id in WORLD_INFO [ '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' % camp_id ) <EOL> return WORLD_INFO [ '<STR_LIT>' ] [ camp_id ] <EOL> def get_flag ( life , camp_id , flag ) : <EOL> if flag in life [ '<STR_LIT>' ] [ camp_id ] [ '<STR_LIT>' ] : <EOL> return life [ '<STR_LIT>' ] [ camp_id ] [ '<STR_LIT>' ] [ flag ] <EOL> return None <EOL> def flag ( life , camp_id , flag , value ) : <EOL> if not camp_id in life [ '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( life [ '<STR_LIT:name>' ] ) ) , value ) <EOL> life [ '<STR_LIT>' ] [ camp_id ] [ '<STR_LIT>' ] [ flag ] = value <EOL> logging . debug ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( life [ '<STR_LIT:name>' ] ) , camp_id , flag ) ) <EOL> def create_all_camps ( ) : <EOL> for camp_key in WORLD_INFO [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> _camp_id = str ( WORLD_INFO [ '<STR_LIT>' ] ) <EOL> _camp_reference = WORLD_INFO [ '<STR_LIT>' ] [ camp_key ] <EOL> for chunk_key in _camp_reference : <EOL> chunks . flag_global ( chunk_key , '<STR_LIT>' , _camp_id ) <EOL> WORLD_INFO [ '<STR_LIT>' ] [ _camp_id ] = { '<STR_LIT:id>' : _camp_id , <EOL> '<STR_LIT>' : _camp_reference , <EOL> '<STR_LIT>' : { } } <EOL> WORLD_INFO [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> def discover_camp ( life , camp_id ) : <EOL> _camp = { '<STR_LIT:id>' : camp_id , <EOL> '<STR_LIT>' : { '<STR_LIT:time>' : - <NUM_LIT:1> , '<STR_LIT>' : [ ] } } <EOL> life [ '<STR_LIT>' ] [ camp_id ] = _camp <EOL> def _get_nearest_known_camp ( life ) : <EOL> _nearest_camp = { '<STR_LIT>' : None , '<STR_LIT>' : - <NUM_LIT:1> } <EOL> for camp in [ life [ '<STR_LIT>' ] [ i ] for i in life [ '<STR_LIT>' ] ] : <EOL> _key = references . find_nearest_key_in_reference ( life , get_camp ( camp [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] ) <EOL> _center = [ int ( val ) + ( WORLD_INFO [ '<STR_LIT>' ] / <NUM_LIT:2> ) for val in _key . split ( '<STR_LIT:U+002C>' ) ] <EOL> _distance = numbers . distance ( life [ '<STR_LIT>' ] , _center ) <EOL> if not _nearest_camp [ '<STR_LIT>' ] or _distance > _nearest_camp [ '<STR_LIT>' ] : <EOL> _nearest_camp [ '<STR_LIT>' ] = camp <EOL> _nearest_camp [ '<STR_LIT>' ] = _distance <EOL> return _nearest_camp <EOL> def get_nearest_known_camp ( life ) : <EOL> return _get_nearest_known_camp ( life ) [ '<STR_LIT>' ] <EOL> def get_distance_to_nearest_known_camp ( life ) : <EOL> return _get_nearest_known_camp ( life ) [ '<STR_LIT>' ] <EOL> def get_camp_via_reference ( reference ) : <EOL> for camp in CAMPS . values ( ) : <EOL> if camp [ '<STR_LIT>' ] == reference : <EOL> return camp [ '<STR_LIT:id>' ] <EOL> return None <EOL> def get_controlling_groups ( camp_id ) : <EOL> _groups = { } <EOL> for life in [ LIFE [ l ] for l in get_all_alife_in_camp ( camp_id ) ] : <EOL> if not life [ '<STR_LIT>' ] : <EOL> continue <EOL> if life [ '<STR_LIT>' ] in _groups : <EOL> _groups [ life [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> else : <EOL> _groups [ life [ '<STR_LIT>' ] ] = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:id>' : life [ '<STR_LIT>' ] } <EOL> return _groups <EOL> def get_controlling_group_global ( camp_id ) : <EOL> _groups = get_controlling_groups ( camp_id ) <EOL> _groups_controlling = [ _grp [ '<STR_LIT:id>' ] for _grp in _groups . values ( ) if _grp [ '<STR_LIT>' ] == max ( [ _grp [ '<STR_LIT>' ] for _grp in _groups . values ( ) ] ) ] <EOL> if _groups_controlling : <EOL> return _groups_controlling [ <NUM_LIT:0> ] <EOL> return None <EOL> def get_controlling_group_according_to ( life , camp_id ) : <EOL> _best_group = { '<STR_LIT>' : None , '<STR_LIT>' : <NUM_LIT:0> } <EOL> _camp = life [ '<STR_LIT>' ] [ camp_id ] <EOL> for group in _camp [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if not _best_group [ '<STR_LIT>' ] or _camp [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ group ] > _best_group [ '<STR_LIT>' ] : <EOL> _best_group [ '<STR_LIT>' ] = group <EOL> _best_group [ '<STR_LIT>' ] = _best_group [ '<STR_LIT>' ] <EOL> return _best_group [ '<STR_LIT>' ] <EOL> def get_all_alife_in_camp ( camp_id ) : <EOL> return [ life [ '<STR_LIT:id>' ] for life in LIFE . values ( ) if is_in_camp ( life , WORLD_INFO [ '<STR_LIT>' ] [ camp_id ] ) ] <EOL> def is_in_camp ( life , camp ) : <EOL> return references . life_is_in_reference ( life , camp [ '<STR_LIT>' ] ) <EOL> def is_in_any_camp ( position ) : <EOL> for camp in WORLD_INFO [ '<STR_LIT>' ] . values ( ) : <EOL> if references . is_in_reference ( position , camp [ '<STR_LIT>' ] ) : <EOL> return camp [ '<STR_LIT:id>' ] <EOL> return False <EOL> def position_is_in_camp ( position , camp_id ) : <EOL> return references . is_in_reference ( position , get_camp ( camp_id ) [ '<STR_LIT>' ] ) <EOL> def get_nearest_position_in_camp ( life , camp ) : <EOL> _camp = WORLD_INFO [ '<STR_LIT>' ] [ camp ] <EOL> _key = references . find_nearest_key_in_reference_exact ( life [ '<STR_LIT>' ] , _camp [ '<STR_LIT>' ] ) <EOL> return chunks . get_nearest_position_in_chunk ( life [ '<STR_LIT>' ] , _key ) <EOL> def guard_camp ( life ) : <EOL> _delay = random . randint ( <NUM_LIT> , jobs . get_job_detail ( life [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> if not life [ '<STR_LIT:path>' ] and not lfe . find_action ( life , matches = [ { '<STR_LIT:action>' : '<STR_LIT>' } ] ) : <EOL> _chunk = WORLD_INFO [ '<STR_LIT>' ] [ references . find_least_populated_key_in_reference ( life , CAMPS [ life [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] ) ] <EOL> lfe . add_action ( life , { '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT:to>' : random . choice ( _chunk [ '<STR_LIT>' ] ) } , <EOL> <NUM_LIT:200> , <EOL> delay = _delay ) <EOL> return False <EOL> def debug_camps ( ) : <EOL> for camp_id in WORLD_INFO [ '<STR_LIT>' ] : <EOL> _group = get_controlling_group_global ( camp_id ) <EOL> if not _group : <EOL> continue <EOL> print camp_id , '<STR_LIT>' , _group <EOL> _actual_group = groups . get_group ( life , _group ) <EOL> print '<STR_LIT>' % len ( _actual_group [ '<STR_LIT>' ] ) </s>
