78
X-Machines for Agent-Based Modeling: FLAME Perspectives
/* Send fox location message */
add_foxInformation_message(FOXX, FOXY);
return 0;
}
/** \fn Chase_rabbits()
* \brief Read rabbit locations and chase
*/
int chaseRabbits()
{
/* Closest rabbit id, default for no rabbits is -1 */
int closest_rabbit_id = -1;
/* Shortest distance, used to find closest rabbit */
double shortest_distance = 9999.0;
/* Current distance squared, holds
distance from current agent to message sending agent */
double current_distance_squared;
/* Holds position of closest rabbit */
double closest_x, closest_y;
/* Angle to closest rabbit */
double theta;
/* Look for nearest rabbit */
/* Get first rabbit location message from list */
rabbitInformation_message = get_first_rabbitInformation_message();
/* Loop through all messages on the list */
while(rabbitInformation_message)
{
/* Calculate distance */
current_distance_squared =
(rabbitInformation_message->rabbitX - FOXX)*
(rabbitInformation_message->rabbitX - FOXX) +
(rabbitInformation_message->rabbitY - FOXY)*
(rabbitInformation_message->rabbitY - FOXY);
/* If distance within view distance of the fox */
if(current_distance_squared <= (fox_view_length*fox_view_length))
{
/* If shortest distance then save values */
if(current_distance_squared < shortest_distance)
{
shortest_distance = current_distance_squared;
closest_rabbit_id = rabbitInformation_message->rabbitID;
closest_x = rabbitInformation_message->rabbitX;
closest_y = rabbitInformation_message->rabbitY;
}
}
/* Get next message */
rabbitInformation_message = get_next_rabbitInformation_message
X-Machines for Agent-Based Modeling: FLAME Perspectives
/* Send fox location message */
add_foxInformation_message(FOXX, FOXY);
return 0;
}
/** \fn Chase_rabbits()
* \brief Read rabbit locations and chase
*/
int chaseRabbits()
{
/* Closest rabbit id, default for no rabbits is -1 */
int closest_rabbit_id = -1;
/* Shortest distance, used to find closest rabbit */
double shortest_distance = 9999.0;
/* Current distance squared, holds
distance from current agent to message sending agent */
double current_distance_squared;
/* Holds position of closest rabbit */
double closest_x, closest_y;
/* Angle to closest rabbit */
double theta;
/* Look for nearest rabbit */
/* Get first rabbit location message from list */
rabbitInformation_message = get_first_rabbitInformation_message();
/* Loop through all messages on the list */
while(rabbitInformation_message)
{
/* Calculate distance */
current_distance_squared =
(rabbitInformation_message->rabbitX - FOXX)*
(rabbitInformation_message->rabbitX - FOXX) +
(rabbitInformation_message->rabbitY - FOXY)*
(rabbitInformation_message->rabbitY - FOXY);
/* If distance within view distance of the fox */
if(current_distance_squared <= (fox_view_length*fox_view_length))
{
/* If shortest distance then save values */
if(current_distance_squared < shortest_distance)
{
shortest_distance = current_distance_squared;
closest_rabbit_id = rabbitInformation_message->rabbitID;
closest_x = rabbitInformation_message->rabbitX;
closest_y = rabbitInformation_message->rabbitY;
}
}
/* Get next message */
rabbitInformation_message = get_next_rabbitInformation_message
