fix compil error in orange.c

This commit is contained in:
Lendemor 2015-01-17 12:03:13 +01:00
parent dd480fe352
commit b1ed0df54a

148
orange.c
View File

@ -11,19 +11,17 @@ enum{
}; };
enum{ enum{
AIM_SPAWN, AIM_SPOT,NO_AIM NO_AIM, AIM_SPAWN, AIM_SPOT,
}; };
typedef struct{ typedef struct{
char type; //the type of ressource that was found char type; //the type of ressource that was found
int x; // x where ressource was found t_coord pos; // pos where ressource was found
int y; // y where ressource was found
} t_ressource_spot; } t_ressource_spot;
typedef struct{ typedef struct{
char name; // name of the dude char name; // name of the dude
int x; // x relative to the spawn t_coord pos; // position relative to the spawn
int y; // y relative to the spawn
char job; char job;
char aim; char aim;
t_ressource_spot res_spot; t_ressource_spot res_spot;
@ -37,58 +35,52 @@ typedef union{
typedef struct{ typedef struct{
char name_sender; char name_sender;
char type_message;
t_message message; t_message message;
} t_com_data; } t_com_custom;
void check_action_success(t_info_data*, int); //check function
void search_job(t_action*, t_info_data*, int); void checkActionSuccess(t_info_data*, int);
void gather_res(t_action*, t_info_data*, int);
//tools functions
int newDir(t_info_data*);
t_coord orangeNewPos(t_coord, int);
int distance(t_coord,t_coord);
//jobs function
void searchJob(t_action*, t_info_data*, int);
void gatherFood(t_action*, t_info_data*, int);
t_action orange_update(void* my_info, t_com* communication_data, int success){ t_action orange_update(void* my_info, t_com* communication_data, int success){
t_info_data* data = (t_info_data*)my_info; t_info_data* data = (t_info_data*)my_info;
//t_data* data = (t_data*)my_info; //t_data* data = (t_data*)my_info;
t_action action; t_action action;
check_action_success(data,success); checkActionSuccess(data,success);
//search_job(action, data, success); //searchJob(&action, data, success);
switch(data.job){ switch(data->job){
case JOBLESS: case JOBLESS:
case MASTER: case MASTER:
case GATHERER: case GATHERER:
gather_food(action,data,success); gatherFood(&action,data,success);
break;
case WOODCUTTER: case WOODCUTTER:
case MINER: case MINER:
break;
} }
return action; return action;
} }
void checkActionSuccess(t_info_data* data, int success){
void check_action_success(t_info_data* data, int success){
if (!success) return; if (!success) return;
switch(data->last_action){ switch(data->last_action){
case MOVE: case MOVE:
switch(data->last_dir){ data->pos=orangeNewPos(data->pos, data->last_dir);
case NORTH:
data->x++;
break;
case SOUTH:
data->x--;
break;
case EAST:
data->y++;
break;
case WEST:
data->y--;
break;
default:
break;
}
break; break;
case ATTACK: case ATTACK:
case PICK: case PICK:
data->searching=0; data->aim=AIM_SPAWN;
case PUT: case PUT:
case WORK: case WORK:
case WAIT: case WAIT:
@ -97,60 +89,78 @@ void check_action_success(t_info_data* data, int success){
break; break;
} }
} }
void searchJob(t_action* action, t_info_data* data, int success){
void search_job(t_action* action, t_info_data* data, int success){
return; return;
} }
void gatherFood(t_action* action, t_info_data* data, int success){
void gather_food(t_action* action, t_info_data* data, int success){
int i,t; int i,t;
//default data for action
action->data = NULL;
//action choice
if (getInventory() == -1){ 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;
action.data = NULL; return;
break;
case FOOD: case FOOD:
action.type = PICK; action->type = PICK;
action.dir = i; action->dir = i;
action.data = NULL; return;
break;
default: default:
action.type = MOVE;
action.dir = rand()%4;
action.data = NULL;
break; break;
} }
} }
}else{
i= newDir(data);
} }
action->type= MOVE;
action->dir = newDir(data);
return; return;
} }
//orange tools
int newDir(t_info_data* data){ int newDir(t_info_data* data){
int dir;
t_coord aim_pos;
switch(data->aim){ switch(data->aim){
case AIM_SPAWN: case AIM_SPAWN:
return dirToAim(O,O); aim_pos.x=0;
aim_pos.y=0;
break;
case AIM_SPOT: case AIM_SPOT:
return dirToAim(data->res_spot->x,data->res_spot->y); aim_pos=data->res_spot.pos;
case NO_TARGET: break;
return rand()%4; case NO_AIM:
return (data->last_dir + rand()%3)%4;
} }
}
int distTo(x1,y1,x2,y2){
return abs(x2-x1) + abs(y2-y1);
}
int dirToAim(int x, int y){
do{ do{
dir= rand()%4;
}while(distTo(x,y) > distTo(x,y)) }while(distance(orangeNewPos(data->pos,dir),aim_pos) < distance(data->pos,aim_pos));
return dir;
}
t_coord orangeNewPos(t_coord pos, int dir){
t_coord new_pos;
switch(dir){
case NORTH:
new_pos.x=pos.x;
new_pos.y=pos.y+1;
break;
case SOUTH:
new_pos.x=pos.x;
new_pos.y=pos.y-1;
break;
case EAST:
new_pos.x=pos.x+1;
new_pos.y=pos.y;
break;
case WEST:
new_pos.x=pos.x-1;
new_pos.y=pos.y;
break;
}
return new_pos;
}
int distance(t_coord p1, t_coord p2){
return abs(p2.x-p1.x) + abs(p2.y-p1.y);
} }