102
X-Machines for Agent-Based Modeling: FLAME Perspectives
int Citizen_look_for_sugar()
{
int closest_sugar_id=-1;
double shortest_distance=9999.0;
double current_distance_squared;
double current_distance_squared_citizen;
double closest_x,closest_y;
int richest_citizen_id=-1;
int citizen_sugars=0;
int max_sugar=0;//change to 0
FLAG_SUGAR_FOUND=0;
sugar_location_message=get_first_sugar_location_message();
while(sugar_location_message)
{
//Extracting information from message
current_distance_squared=
(sugar_location_message->x-X)*(sugar_location_message->x-X) +
(sugar_location_message->y-Y)*(sugar_location_message->y-Y);
if(current_distance_squared <=
(citizen_view_length* citizen_view_length))
{
if(current_distance_squared
{
shortest_distance=current_distance_squared;
closest_sugar_id=sugar_location_message->sugar_id;
closest_x=sugar_location_message->x;
closest_y=sugar_location_message->y;
FLAG_SUGAR_FOUND=closest_sugar_id;
printf("Sugar is found");
}
}
}
sugar_location_message=get_next_sugar_location_message
(sugar_location_message);
}
if(FLAG_SUGAR_FOUND!=0)
{
//move randomly in the space
X=X+(citizen_run_length-((double)rand()/(double)(RAND_MAX)*
(citizen_run_length*2.0)));
Y=Y+(citizen_run_length-((double)rand()/(double)(RAND_MAX)*
(citizen_run_length*2.0)));
}
X-Machines for Agent-Based Modeling: FLAME Perspectives
int Citizen_look_for_sugar()
{
int closest_sugar_id=-1;
double shortest_distance=9999.0;
double current_distance_squared;
double current_distance_squared_citizen;
double closest_x,closest_y;
int richest_citizen_id=-1;
int citizen_sugars=0;
int max_sugar=0;//change to 0
FLAG_SUGAR_FOUND=0;
sugar_location_message=get_first_sugar_location_message();
while(sugar_location_message)
{
//Extracting information from message
current_distance_squared=
(sugar_location_message->x-X)*(sugar_location_message->x-X) +
(sugar_location_message->y-Y)*(sugar_location_message->y-Y);
if(current_distance_squared <=
(citizen_view_length* citizen_view_length))
{
if(current_distance_squared
shortest_distance=current_distance_squared;
closest_sugar_id=sugar_location_message->sugar_id;
closest_x=sugar_location_message->x;
closest_y=sugar_location_message->y;
FLAG_SUGAR_FOUND=closest_sugar_id;
printf("Sugar is found");
}
}
}
sugar_location_message=get_next_sugar_location_message
(sugar_location_message);
}
if(FLAG_SUGAR_FOUND!=0)
{
//move randomly in the space
X=X+(citizen_run_length-((double)rand()/(double)(RAND_MAX)*
(citizen_run_length*2.0)));
Y=Y+(citizen_run_length-((double)rand()/(double)(RAND_MAX)*
(citizen_run_length*2.0)));
}
