<s> import pyrecast <EOL> from pandac . PandaModules import VBase4 , Point3 , Vec3 <EOL> import time <EOL> def panda2detour ( pos ) : <EOL> """<STR_LIT>""" <EOL> return pyrecast . dtPoint3 ( pos [ <NUM_LIT:0> ] , pos [ <NUM_LIT:2> ] , - pos [ <NUM_LIT:1> ] ) <EOL> def detour2panda ( pos , y = None , z = None ) : <EOL> """<STR_LIT>""" <EOL> if y is None and z is None : <EOL> get = pyrecast . floatp_getitem <EOL> return Point3 ( get ( pos , <NUM_LIT:0> ) , - get ( pos , <NUM_LIT:2> ) , get ( pos , <NUM_LIT:1> ) ) <EOL> else : <EOL> return Point3 ( pos , - z , y ) <EOL> class NavMesh : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , navmesh , maxnodes = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> self . mesh = pyrecast . dtLoadMesh ( navmesh ) <EOL> self . filter = pyrecast . dtQueryFilter ( ) <EOL> self . query = pyrecast . dtNavMeshQuery ( ) <EOL> status = self . query . init ( self . mesh , maxnodes ) <EOL> def nearest_point ( self , <EOL> pos = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> radius = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> radius = pyrecast . dtPoint3 ( radius , radius , radius ) <EOL> tmp_polyref = pyrecast . new_uintp ( <NUM_LIT:1> ) <EOL> tmp_point = pyrecast . dtPoint3 ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . query . findNearestPoly ( panda2detour ( pos ) , radius , self . filter , tmp_polyref , tmp_point ) <EOL> return [ tmp_polyref , tmp_point ] <EOL> def nearest_edge_point ( self , <EOL> loc , <EOL> radius = <NUM_LIT:5> <EOL> ) : <EOL> """<STR_LIT>""" <EOL> tmp_point = pyrecast . dtPoint3 ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . query . closestPointOnPolyBoundary ( pyrecast . uintp_getitem ( loc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , loc [ <NUM_LIT:1> ] , tmp_point ) <EOL> return [ loc [ <NUM_LIT:0> ] , tmp_point ] <EOL> def is_reachable ( self , <EOL> a , <EOL> b , <EOL> tolerance = <NUM_LIT:0.5> , <EOL> max_path = <NUM_LIT:1000> <EOL> ) : <EOL> """<STR_LIT>""" <EOL> a = self . nearest_point ( a , radius = tolerance ) <EOL> b = self . nearest_point ( b , radius = tolerance ) <EOL> tmp_path = pyrecast . new_uintp ( max_path ) <EOL> tmp_pathcount = pyrecast . new_intp ( <NUM_LIT:1> ) <EOL> status = self . query . findPath ( pyrecast . uintp_getitem ( a [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , pyrecast . uintp_getitem ( b [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , a [ <NUM_LIT:1> ] , b [ <NUM_LIT:1> ] , self . filter , tmp_path , tmp_pathcount , int ( max_path ) ) <EOL> result = pyrecast . dtStatusSucceed ( status ) <EOL> return result <EOL> class NavCrowd : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> nav , <EOL> maxagents = <NUM_LIT:10> , <EOL> maxagentradius = <NUM_LIT> , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> self . nav = nav <EOL> self . crowd = pyrecast . dtCrowd ( ) <EOL> self . crowd . init ( maxagents , maxagentradius , self . nav . mesh ) <EOL> self . debuginfo = pyrecast . dtCrowdAgentDebugInfo ( ) <EOL> self . last_time = time . time ( ) <EOL> self . _active_indices = [ ] <EOL> taskMgr . add ( self . update , '<STR_LIT>' ) <EOL> def destroy ( self ) : <EOL> taskMgr . remove ( '<STR_LIT>' ) <EOL> def active_indices ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _active_indices <EOL> def add_agent ( self , <EOL> loc = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> radius = <NUM_LIT> , <EOL> height = <NUM_LIT:2> , <EOL> maxaccel = <NUM_LIT:3> , <EOL> maxspeed = <NUM_LIT> , <EOL> collisionquery_range = <NUM_LIT> , <EOL> pathoptimization_range = <NUM_LIT> , <EOL> obstacleavoidance_type = <NUM_LIT:3> , <EOL> separation_weight = <NUM_LIT:1> , <EOL> anticipate_turns = True , <EOL> avoid_obstacles = False , <EOL> crowd_separation = False , <EOL> optimize_vis = True , <EOL> optimize_topo = True , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> params = pyrecast . dtCrowdAgentParams ( ) <EOL> params . radius = radius <EOL> params . height = height <EOL> params . maxAcceleration = maxaccel <EOL> params . maxSpeed = maxspeed <EOL> params . collisionQueryRange = collisionquery_range * radius <EOL> params . pathOptimizationRange = pathoptimization_range * radius <EOL> params . obstacleAvoidanceType = obstacleavoidance_type <EOL> params . separationWeight = separation_weight <EOL> params . updateFlags = <NUM_LIT:0> <EOL> if anticipate_turns : <EOL> params . updateFlags |= pyrecast . DT_CROWD_ANTICIPATE_TURNS <EOL> if optimize_vis : <EOL> params . updateFlags |= pyrecast . DT_CROWD_OPTIMIZE_VIS <EOL> if optimize_topo : <EOL> params . updateFlags |= pyrecast . DT_CROWD_OPTIMIZE_TOPO <EOL> if avoid_obstacles : <EOL> params . updateFlags |= pyrecast . DT_CROWD_OBSTACLE_AVOIDANCE <EOL> if crowd_separation : <EOL> params . updateFlags |= pyrecast . DT_CROWD_SEPARATION <EOL> if hasattr ( loc , '<STR_LIT>' ) : <EOL> if len ( loc ) == <NUM_LIT:2> : <EOL> loc = loc [ <NUM_LIT:1> ] <EOL> else : <EOL> loc = panda2detour ( loc ) <EOL> elif isinstance ( loc , Point3 ) : <EOL> loc = panda2detour ( loc [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> idx = self . crowd . addAgent ( loc , params ) <EOL> self . _active_indices . append ( idx ) <EOL> return idx <EOL> def remove_agent ( self , idx ) : <EOL> """<STR_LIT>""" <EOL> self . _active_indices . remove ( idx ) <EOL> self . crowd . removeAgent ( idx ) <EOL> def request_move_target ( self , idx , loc ) : <EOL> """<STR_LIT>""" <EOL> self . crowd . requestMoveTarget ( idx , pyrecast . uintp_getitem ( loc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , loc [ <NUM_LIT:1> ] ) <EOL> def replan_move_target ( self , idx , loc ) : <EOL> """<STR_LIT>""" <EOL> self . crowd . requestMoveTargetReplan ( idx , pyrecast . uintp_getitem ( loc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , loc [ <NUM_LIT:1> ] ) <EOL> def adjust_move_target ( self , idx , loc ) : <EOL> """<STR_LIT>""" <EOL> self . crowd . adjustMoveTarget ( idx , pyrecast . uintp_getitem ( loc [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , loc [ <NUM_LIT:1> ] ) <EOL> def agent_status ( self , idx ) : <EOL> """<STR_LIT>""" <EOL> agent = self . crowd . getAgent ( idx ) <EOL> class status : <EOL> pass <EOL> result = status ( ) <EOL> result . npos = detour2panda ( agent . npos ) <EOL> tmp = detour2panda ( agent . vel ) <EOL> result . vel = Vec3 ( tmp . getX ( ) , tmp . getY ( ) , tmp . getZ ( ) ) <EOL> return result <EOL> def update ( self , task ) : <EOL> """<STR_LIT>""" <EOL> cur_time = time . time ( ) <EOL> self . crowd . update ( cur_time - self . last_time , self . debuginfo ) <EOL> self . last_time = cur_time <EOL> return task . cont </s>
