<s> from globals import * <EOL> from math import * <EOL> import pathfinding <EOL> import render_los <EOL> import logging <EOL> import random <EOL> import numpy <EOL> import tiles <EOL> import time <EOL> import maps <EOL> def clip ( number , start , end ) : <EOL> """<STR_LIT>""" <EOL> return max ( start , min ( number , end ) ) <EOL> def roll ( dice , sides ) : <EOL> return sum ( [ random . choice ( range ( sides ) ) + <NUM_LIT:1> for d in range ( dice ) ] ) <EOL> def lerp ( n1 , n2 , t ) : <EOL> return n1 + ( n2 - n1 ) * t <EOL> def distance ( pos1 , pos2 , old = False ) : <EOL> if old : <EOL> return abs ( pos1 [ <NUM_LIT:0> ] - pos2 [ <NUM_LIT:0> ] ) + abs ( pos1 [ <NUM_LIT:1> ] - pos2 [ <NUM_LIT:1> ] ) <EOL> x_dist = abs ( pos1 [ <NUM_LIT:0> ] - pos2 [ <NUM_LIT:0> ] ) <EOL> y_dist = abs ( pos1 [ <NUM_LIT:1> ] - pos2 [ <NUM_LIT:1> ] ) <EOL> if x_dist > y_dist : <EOL> return y_dist + ( x_dist - y_dist ) <EOL> else : <EOL> return x_dist + ( y_dist - x_dist ) <EOL> def velocity ( direction , speed ) : <EOL> rad = direction * ( pi / <NUM_LIT> ) <EOL> velocity = numpy . multiply ( numpy . array ( [ cos ( rad ) , sin ( rad ) ] ) , speed ) <EOL> return [ velocity [ <NUM_LIT:0> ] , - velocity [ <NUM_LIT:1> ] , <NUM_LIT:0> ] <EOL> def lerp_velocity ( velocity1 , velocity2 , interp ) : <EOL> return [ lerp ( velocity1 [ <NUM_LIT:0> ] , velocity2 [ <NUM_LIT:0> ] , interp ) , <EOL> lerp ( velocity1 [ <NUM_LIT:1> ] , velocity2 [ <NUM_LIT:1> ] , interp ) , <EOL> lerp ( velocity1 [ <NUM_LIT:2> ] , velocity2 [ <NUM_LIT:2> ] , interp ) ] <EOL> def get_surface_area ( structure ) : <EOL> if '<STR_LIT>' in structure : <EOL> return structure [ '<STR_LIT:size>' ] * len ( structure [ '<STR_LIT>' ] ) <EOL> return structure [ '<STR_LIT:size>' ] <EOL> def direction_to ( pos1 , pos2 ) : <EOL> theta = atan2 ( ( pos1 [ <NUM_LIT:1> ] - pos2 [ <NUM_LIT:1> ] ) , - ( pos1 [ <NUM_LIT:0> ] - pos2 [ <NUM_LIT:0> ] ) ) <EOL> if theta < <NUM_LIT:0> : <EOL> theta += <NUM_LIT:2> * pi <EOL> return theta * ( <NUM_LIT> / pi ) <EOL> def create_flee_map ( dijkstra ) : <EOL> for _x in range ( dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) : <EOL> for _y in range ( dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) : <EOL> if dijkstra [ '<STR_LIT>' ] [ _y - dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , _x - dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ] == <NUM_LIT> : <EOL> continue <EOL> dijkstra [ '<STR_LIT>' ] [ _y - dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , _x - dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ] *= - <NUM_LIT> <EOL> def calculate_dijkstra_map ( dijkstra ) : <EOL> _map = dijkstra [ '<STR_LIT>' ] <EOL> _min_x = dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> _max_x = dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> _min_y = dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> _max_y = dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> _target_positions = [ tuple ( target [ '<STR_LIT>' ] ) for target in dijkstra [ '<STR_LIT>' ] ] <EOL> _i = <NUM_LIT:0> <EOL> while <NUM_LIT:1> == <NUM_LIT:1> : <EOL> _i += <NUM_LIT:1> <EOL> _orig_map = _map . copy ( ) <EOL> for _x in range ( _min_x , _max_x ) : <EOL> for _y in range ( _min_y , _max_y ) : <EOL> if ( _x , _y ) in _target_positions or _orig_map [ _y - _min_y , _x - _min_x ] == - <NUM_LIT:1> : <EOL> continue <EOL> _lowest_score = <NUM_LIT> <EOL> for x1 in range ( - <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> x = _x + x1 <EOL> if <NUM_LIT:0> > x or x >= _max_x : <EOL> continue <EOL> for y1 in range ( - <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> y = _y + y1 <EOL> if <NUM_LIT:0> > y or y >= _max_y or ( x1 , y1 ) == ( <NUM_LIT:0> , <NUM_LIT:0> ) or _orig_map [ y - _min_y , x - _min_x ] == - <NUM_LIT:1> : <EOL> continue <EOL> if _orig_map [ y - _min_y , x - _min_x ] < _lowest_score : <EOL> _lowest_score = _orig_map [ y - _min_y , x - _min_x ] <EOL> if _lowest_score >= <NUM_LIT:0> : <EOL> if _orig_map [ _y - _min_y , _x - _min_x ] - _lowest_score >= <NUM_LIT:2> : <EOL> _map [ _y - _min_y , _x - _min_x ] = _lowest_score + <NUM_LIT:1> <EOL> if numpy . array_equal ( _map , _orig_map ) : <EOL> break <EOL> def _create_dijkstra_map ( center , source_map , targets , size = ( <NUM_LIT:50> , <NUM_LIT:50> ) , flee = False , ** kvargs ) : <EOL> if not targets : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> _target_positions = [ tuple ( target [ '<STR_LIT>' ] ) for target in targets ] <EOL> _min_x = clip ( center [ <NUM_LIT:0> ] - ( size [ <NUM_LIT:0> ] ) , <NUM_LIT:0> , MAP_SIZE [ <NUM_LIT:0> ] ) <EOL> _max_x = clip ( center [ <NUM_LIT:0> ] + ( size [ <NUM_LIT:0> ] ) , <NUM_LIT:0> , MAP_SIZE [ <NUM_LIT:0> ] ) <EOL> _min_y = clip ( center [ <NUM_LIT:1> ] - ( size [ <NUM_LIT:1> ] ) , <NUM_LIT:0> , MAP_SIZE [ <NUM_LIT:1> ] ) <EOL> _max_y = clip ( center [ <NUM_LIT:1> ] + ( size [ <NUM_LIT:1> ] ) , <NUM_LIT:0> , MAP_SIZE [ <NUM_LIT:1> ] ) <EOL> _stime = time . time ( ) <EOL> _map = numpy . ones ( ( _max_y , _max_x ) ) <EOL> _orig_map = None <EOL> for target in targets : <EOL> _map [ target [ '<STR_LIT>' ] [ <NUM_LIT:1> ] - _min_y , target [ '<STR_LIT>' ] [ <NUM_LIT:0> ] - _min_x ] = <NUM_LIT:0> <EOL> _map *= <NUM_LIT:30> <EOL> for x in range ( _min_x , _max_x ) : <EOL> for y in range ( _min_y , _max_y ) : <EOL> if source_map [ x ] [ y ] [ center [ <NUM_LIT:2> ] + <NUM_LIT:1> ] : <EOL> if flee : <EOL> _map [ y - _min_y , x - _min_x ] = <NUM_LIT:1> <EOL> else : <EOL> _map [ y - _min_y , x - _min_x ] = - <NUM_LIT:1> <EOL> continue <EOL> _dijkstra = { '<STR_LIT>' : _map , <EOL> '<STR_LIT>' : ( _min_x , _max_x ) , <EOL> '<STR_LIT>' : ( _min_y , _max_y ) , <EOL> '<STR_LIT>' : targets } <EOL> calculate_dijkstra_map ( _dijkstra ) <EOL> if flee : <EOL> create_flee_map ( _dijkstra ) <EOL> calculate_dijkstra_map ( _dijkstra ) <EOL> logging . info ( '<STR_LIT>' % ( str ( time . time ( ) - _stime ) , ( _max_x - _min_x ) , ( _max_y - _min_y ) ) ) <EOL> print '<STR_LIT>' % ( str ( time . time ( ) - _stime ) , ( _max_x - _min_x ) , ( _max_y - _min_y ) , <NUM_LIT:0> ) <EOL> return _dijkstra <EOL> def draw_dijkstra ( dijkstra , path ) : <EOL> for _y in range ( dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) : <EOL> y = _y - dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> for _x in range ( dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) : <EOL> x = _x - dijkstra [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> _score = clip ( int ( abs ( dijkstra [ '<STR_LIT>' ] [ y , x ] ) ) , <NUM_LIT:0> , <NUM_LIT:9> ) <EOL> if ( _x , _y , <NUM_LIT:0> ) in path : <EOL> _score = '<STR_LIT>' <EOL> elif _score == - <NUM_LIT:1> : <EOL> _score = '<STR_LIT>' <EOL> else : <EOL> _score = '<STR_LIT>' <EOL> print '<STR_LIT:%s>' % _score , <EOL> print <EOL> def create_dijkstra_map ( center , source_map , targets , flee = False ) : <EOL> _farthest_distance = <NUM_LIT:0> <EOL> for target in targets : <EOL> _dist = distance ( center , target [ '<STR_LIT>' ] ) <EOL> if _dist > _farthest_distance : <EOL> _farthest_distance = _dist + <NUM_LIT:1> <EOL> return _create_dijkstra_map ( center , source_map , targets , size = ( _farthest_distance , _farthest_distance ) , flee = flee ) </s>
