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