put the north at the north
This commit is contained in:
parent
b1ed0df54a
commit
efca69674b
95
orange.c
95
orange.c
@ -22,6 +22,7 @@ typedef struct{
|
||||
typedef struct{
|
||||
char name; // name of the dude
|
||||
t_coord pos; // position relative to the spawn
|
||||
char hatched;
|
||||
char job;
|
||||
char aim;
|
||||
t_ressource_spot res_spot;
|
||||
@ -56,12 +57,27 @@ t_action orange_update(void* my_info, t_com* communication_data, int success){
|
||||
//t_data* data = (t_data*)my_info;
|
||||
t_action action;
|
||||
|
||||
if(data->hatched == 0){
|
||||
success = 0;
|
||||
data->hatched = 1;
|
||||
data->job = GATHERER;
|
||||
data->pos.x=0;
|
||||
data->pos.y=0;
|
||||
}
|
||||
|
||||
checkActionSuccess(data,success);
|
||||
|
||||
//searchJob(&action, data, success);
|
||||
switch(data->job){
|
||||
case JOBLESS:
|
||||
action.type = WAIT;
|
||||
action.dir=0;
|
||||
break;
|
||||
case MASTER:
|
||||
printf("I'm the master!\n");
|
||||
action.type = WAIT;
|
||||
action.dir=0;
|
||||
break;
|
||||
case GATHERER:
|
||||
gatherFood(&action,data,success);
|
||||
break;
|
||||
@ -69,19 +85,40 @@ t_action orange_update(void* my_info, t_com* communication_data, int success){
|
||||
case MINER:
|
||||
break;
|
||||
}
|
||||
data->last_action = action.type;
|
||||
data->last_dir = action.dir;
|
||||
return action;
|
||||
}
|
||||
void checkActionSuccess(t_info_data* data, int success){
|
||||
|
||||
if (!success) return;
|
||||
t_coord aim_pos;
|
||||
|
||||
switch(data->last_action){
|
||||
case MOVE:
|
||||
//update position
|
||||
data->pos=orangeNewPos(data->pos, data->last_dir);
|
||||
//update aim
|
||||
if (getInventory() == -1){
|
||||
if (distance(data->pos,data->res_spot.pos) == 0){
|
||||
data->aim = NO_AIM;
|
||||
}
|
||||
}/*else{
|
||||
/*aim_pos.x = 0;
|
||||
aim_pos.y = 0;
|
||||
if(distance(data->pos, aim_pos) == 1){
|
||||
data->aim=AIM_SPOT;
|
||||
}
|
||||
}*/
|
||||
break;
|
||||
case ATTACK:
|
||||
case PICK:
|
||||
data->aim=AIM_SPAWN;
|
||||
data->res_spot.type = getInventory();
|
||||
data->res_spot.pos = data->pos;
|
||||
break;
|
||||
case PUT:
|
||||
data->aim=AIM_SPOT;
|
||||
break;
|
||||
case WORK:
|
||||
case WAIT:
|
||||
case COMMUNICATE:
|
||||
@ -97,21 +134,29 @@ void gatherFood(t_action* action, t_info_data* data, int success){
|
||||
//default data for action
|
||||
action->data = NULL;
|
||||
//action choice
|
||||
if (getInventory() == -1){
|
||||
for(i=0;i<4;i++){
|
||||
t = getNear(i);
|
||||
switch(t){
|
||||
case BERRIES:
|
||||
action->type = WORK;
|
||||
action->dir = i;
|
||||
return;
|
||||
case FOOD:
|
||||
for(i=0;i<4;i++){
|
||||
t = getNear(i);
|
||||
switch(t){
|
||||
case BERRIES:
|
||||
action->type = WORK;
|
||||
action->dir = i;
|
||||
return;
|
||||
case FOOD:
|
||||
if(getInventory() == -1){
|
||||
action->type = PICK;
|
||||
action->dir = i;
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SPAWN:
|
||||
if(getInventory() == FOOD){
|
||||
action->type = PUT;
|
||||
action->dir = i;
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
action->type= MOVE;
|
||||
@ -122,8 +167,11 @@ void gatherFood(t_action* action, t_info_data* data, int success){
|
||||
//orange tools
|
||||
int newDir(t_info_data* data){
|
||||
int dir;
|
||||
int current_dist,new_dist;
|
||||
t_coord aim_pos;
|
||||
switch(data->aim){
|
||||
case NO_AIM:
|
||||
return (data->last_dir + rand()%3 )%4;
|
||||
case AIM_SPAWN:
|
||||
aim_pos.x=0;
|
||||
aim_pos.y=0;
|
||||
@ -131,32 +179,31 @@ int newDir(t_info_data* data){
|
||||
case AIM_SPOT:
|
||||
aim_pos=data->res_spot.pos;
|
||||
break;
|
||||
case NO_AIM:
|
||||
return (data->last_dir + rand()%3)%4;
|
||||
}
|
||||
// printf("[%d : %d] [%d : %d]",data->pos.x,data->pos.y,aim_pos.x,aim_pos.y);
|
||||
current_dist = distance(data->pos,aim_pos);
|
||||
if (current_dist == 0) return 0;
|
||||
do{
|
||||
dir= rand()%4;
|
||||
}while(distance(orangeNewPos(data->pos,dir),aim_pos) < distance(data->pos,aim_pos));
|
||||
new_dist = distance(orangeNewPos(data->pos,dir),aim_pos);
|
||||
}while(current_dist <= new_dist);
|
||||
|
||||
return dir;
|
||||
}
|
||||
t_coord orangeNewPos(t_coord pos, int dir){
|
||||
t_coord new_pos;
|
||||
t_coord new_pos = pos;
|
||||
switch(dir){
|
||||
case NORTH:
|
||||
new_pos.x=pos.x;
|
||||
new_pos.y=pos.y+1;
|
||||
new_pos.y++;
|
||||
break;
|
||||
case SOUTH:
|
||||
new_pos.x=pos.x;
|
||||
new_pos.y=pos.y-1;
|
||||
new_pos.y--;
|
||||
break;
|
||||
case EAST:
|
||||
new_pos.x=pos.x+1;
|
||||
new_pos.y=pos.y;
|
||||
new_pos.x++;
|
||||
break;
|
||||
case WEST:
|
||||
new_pos.x=pos.x-1;
|
||||
new_pos.y=pos.y;
|
||||
new_pos.x--;
|
||||
break;
|
||||
}
|
||||
return new_pos;
|
||||
|
Loading…
x
Reference in New Issue
Block a user