public static Location getRandomObjectLocationInPerceptionRange ( ShanksSimulation simulation , PercipientShanksAgent agent ) { Location agentLocation = agent . getCurrentLocation ( ) ; try { if ( agentLocation . is3DLocation ( ) ) { ContinuousPortrayal3D devicesPortrayal = ( ContinuousPortrayal3D ) simulation . getScenarioPortrayal ( ) . getPortrayals ( ) . get ( Scenario3DPortrayal . MAIN_DISPLAY_ID ) . get ( ScenarioPortrayal . DEVICES_PORTRAYAL ) ; Continuous3D devicesField = ( Continuous3D ) devicesPortrayal . getField ( ) ; Bag objects = devicesField . getObjectsWithinDistance ( agentLocation . getLocation3D ( ) , agent . getPerceptionRange ( ) , false ) ; if ( objects . size ( ) == 1 && objects . get ( 0 ) . equals ( agent ) ) { return null ; } Object o ; Double3D newTarget = null ; do { int size = objects . size ( ) ; o = objects . get ( simulation . random . nextInt ( size ) ) ; newTarget = devicesField . getObjectLocation ( o ) ; } while ( o . equals ( agent ) ) ; return new Location ( newTarget ) ; } else if ( agentLocation . is2DLocation ( ) ) { ContinuousPortrayal2D devicesPortrayal = ( ContinuousPortrayal2D ) simulation . getScenarioPortrayal ( ) . getPortrayals ( ) . get ( Scenario2DPortrayal . MAIN_DISPLAY_ID ) . get ( ScenarioPortrayal . DEVICES_PORTRAYAL ) ; Continuous2D devicesField = ( Continuous2D ) devicesPortrayal . getField ( ) ; Bag objects = devicesField . getObjectsWithinDistance ( agentLocation . getLocation2D ( ) , agent . getPerceptionRange ( ) , false ) ; Object o ; Double2D newTarget = null ; do { int size = objects . size ( ) ; o = objects . get ( simulation . random . nextInt ( size ) ) ; newTarget = devicesField . getObjectLocation ( o ) ; } while ( o . equals ( agent ) ) ; return new Location ( newTarget ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } return null ; }
Get a random object location in the perception range of the agent .