Skip to main content

scan

Function scan 

Source
pub fn scan() -> Result<RMsgScan, MessageError>
Expand description

Initiates a 360-degree scan to detect nearby objects around the robot within a specified range.

This function performs a full 360-degree scan around the robot to detect all nearby objects within the scanning range. The scan results include detailed information about each detected object, such as its tag, kind, position (x, y), and potential buffs.

§Returns

A Result containing msg::RMsgScan representing the scan results, including a list of all detected objects and their associated information, or an error of type MessageError if the scan operation fails.

§Examples

let scan_results = rbot::modules::scan()?;

Below is an example function to use the scanner to find the average enemy position (if the enemy is in range of the scanner):

pub fn scan_for_average_bot_component() -> Option<[f32;2]> {
   let scan_msg = rbot::modules::scan().ok()?;
   let components: Vec<_> = scan_msg
       .objects
       .into_iter()
       .filter(|o| o.tag == rbot::constants::tag::COMPONENT)
       .collect();

   if components.len() == 0 {
       return None;
   }

   // Find the average position of the components.
   let x: f32 = components.iter().map(|c| c.x).sum::<f32>() / components.len() as f32;
   let y: f32 = components.iter().map(|c| c.y).sum::<f32>() / components.len() as f32;

   Some([x, y])
}