bug with com_data->flag
This commit is contained in:
parent
9b6cbe6aad
commit
cc9e8538dd
258
orange.c
258
orange.c
@ -7,13 +7,19 @@
|
|||||||
//Structure definition
|
//Structure definition
|
||||||
|
|
||||||
enum{
|
enum{
|
||||||
JOBLESS, MASTER, GATHERER, WOODCUTTER, MINER, NB_JOBS
|
O_JOBLESS, O_MASTER, O_GATHERER, O_WOODCUTTER, O_MINER, O_NB_JOBS
|
||||||
};
|
};
|
||||||
|
|
||||||
enum{
|
enum{
|
||||||
NO_AIM, AIM_SPAWN, AIM_SPOT,
|
O_NO_AIM, O_AIM_MASTER, O_AIM_SPAWN, O_AIM_SPOT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define ORANGE_SPAWN_X 0
|
||||||
|
#define ORANGE_SPAWN_Y 0
|
||||||
|
|
||||||
|
#define ORANGE_MASTER_X 3
|
||||||
|
#define ORANGE_MASTER_Y 3
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
char type; //the type of ressource that was found
|
char type; //the type of ressource that was found
|
||||||
t_coord pos; // pos where ressource was found
|
t_coord pos; // pos where ressource was found
|
||||||
@ -22,117 +28,210 @@ 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 age;
|
||||||
char job;
|
char job;
|
||||||
char aim;
|
char aim;
|
||||||
|
char talking;
|
||||||
|
char listening;
|
||||||
t_orange_ressource_spot res_spot;
|
t_orange_ressource_spot res_spot;
|
||||||
char last_action;
|
char last_action;
|
||||||
char last_dir;
|
char last_dir;
|
||||||
} t_orange_info_data;
|
} t_orange_info;
|
||||||
|
|
||||||
typedef union{
|
|
||||||
t_orange_ressource_spot res_spot;
|
|
||||||
}t_orange_message;
|
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
char name_sender;
|
|
||||||
char type_message;
|
|
||||||
t_orange_message message;
|
|
||||||
} t_orange_com;
|
|
||||||
|
|
||||||
//check function
|
|
||||||
void orangeCheckActionSuccess(t_info_data*, int);
|
|
||||||
|
|
||||||
//tools functions
|
//tools functions
|
||||||
int orangeNewDir(t_info_data*);
|
int orangeNewDir(t_orange_info*);
|
||||||
t_coord orangeNewPos(t_coord, int);
|
t_coord orangeNewPos(t_coord, int);
|
||||||
int orangeDistance(t_coord,t_coord);
|
int orangeDistance(t_coord, int, int);
|
||||||
|
//action function
|
||||||
|
void orangeApplyAction(t_orange_info*,t_com*);
|
||||||
|
void orangeMoveEffect(t_orange_info*);
|
||||||
|
void orangePickEffect(t_orange_info*);
|
||||||
|
void orangePutEffect(t_orange_info*);
|
||||||
|
void orangeCommunicateEffect(t_orange_info*);
|
||||||
|
void orangeWaitEffect(t_orange_info*, t_com*);
|
||||||
//jobs function
|
//jobs function
|
||||||
void orangeSearchJob(t_action*, t_info_data*, int);
|
void orangeHatched(t_orange_info*);
|
||||||
void orangeGatherFood(t_action*, t_info_data*, int);
|
|
||||||
|
|
||||||
t_action orange_update(void* my_info, t_com* communication_data, int success){
|
void orangeJobless(t_action*, t_orange_info*, t_com*, int);
|
||||||
t_info_data* data = (t_info_data*)my_info;
|
void orangeMaster(t_action*, t_orange_info*, t_com*, int);
|
||||||
//t_data* data = (t_data*)my_info;
|
void orangeGatherer(t_action*, t_orange_info*, t_com*, int);
|
||||||
|
|
||||||
|
t_action orange_update(void* my_info, t_com* com_data, int success){
|
||||||
|
t_orange_info* data = (t_orange_info*)my_info;
|
||||||
t_action action;
|
t_action action;
|
||||||
|
|
||||||
if(data->hatched == 0){
|
if(data->age == 0){
|
||||||
|
orangeHatched(data);
|
||||||
success = 0;
|
success = 0;
|
||||||
data->hatched = 1;
|
|
||||||
data->job = GATHERER;
|
|
||||||
data->pos.x=0;
|
|
||||||
data->pos.y=0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
orangeCheckActionSuccess(data,success);
|
if (!success){
|
||||||
|
action.type = data->last_action;
|
||||||
|
action.dir = data->last_dir;
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
orangeApplyAction(data,com_data);
|
||||||
|
|
||||||
//orangeSearchJob(&action, data, success);
|
|
||||||
switch(data->job){
|
switch(data->job){
|
||||||
case JOBLESS:
|
case O_JOBLESS:
|
||||||
action.type = WAIT;
|
orangeJobless(&action, data, com_data, success);
|
||||||
action.dir=0;
|
|
||||||
break;
|
break;
|
||||||
case MASTER:
|
case O_MASTER:
|
||||||
printf("I'm the master!\n");
|
orangeMaster(&action, data, com_data, success);
|
||||||
action.type = WAIT;
|
|
||||||
action.dir=0;
|
|
||||||
break;
|
break;
|
||||||
case GATHERER:
|
case O_GATHERER:
|
||||||
orangeGatherFood(&action,data,success);
|
orangeGatherer(&action,data, com_data, success);
|
||||||
break;
|
break;
|
||||||
case WOODCUTTER:
|
case O_WOODCUTTER:
|
||||||
case MINER:
|
case O_MINER:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
data->last_action = action.type;
|
data->last_action = action.type;
|
||||||
data->last_dir = action.dir;
|
data->last_dir = action.dir;
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
void orangeCheckActionSuccess(t_info_data* data, int success){
|
|
||||||
if (!success) return;
|
|
||||||
t_coord aim_pos;
|
|
||||||
|
|
||||||
|
void orangeHatched(t_orange_info* data){
|
||||||
|
data->age++;
|
||||||
|
data->job = O_JOBLESS;
|
||||||
|
data->aim = O_AIM_MASTER;
|
||||||
|
data->pos.x=0;
|
||||||
|
data->pos.y=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void orangeApplyAction(t_orange_info* data,t_com* com_data){
|
||||||
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
|
orangeMoveEffect(data);
|
||||||
|
break;
|
||||||
|
case PICK:
|
||||||
|
orangePickEffect(data);
|
||||||
|
break;
|
||||||
|
case PUT:
|
||||||
|
orangePutEffect(data);
|
||||||
|
break;
|
||||||
|
case COMMUNICATE:
|
||||||
|
orangeCommunicateEffect(data);
|
||||||
|
break;
|
||||||
|
case WAIT:
|
||||||
|
orangeWaitEffect(data,com_data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void orangeMoveEffect(t_orange_info* data){
|
||||||
|
int dist_to_master;
|
||||||
|
switch(data->job){
|
||||||
|
case O_JOBLESS:
|
||||||
|
dist_to_master = orangeDistance(data->pos,ORANGE_MASTER_X,ORANGE_MASTER_Y);
|
||||||
|
if(dist_to_master == 0){
|
||||||
|
data->job=O_MASTER;
|
||||||
|
data->listening = 1;
|
||||||
|
} else if(dist_to_master == 1)
|
||||||
|
data->talking = 1;
|
||||||
|
break;
|
||||||
|
case O_GATHERER:
|
||||||
if (getInventory() == -1){
|
if (getInventory() == -1){
|
||||||
if (orangeDistance(data->pos,data->res_spot.pos) == 0){
|
if (orangeDistance(data->pos,data->res_spot.pos.x, data->res_spot.pos.y) == 0){
|
||||||
data->aim = NO_AIM;
|
data->aim = O_NO_AIM;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
aim_pos.x = 0;
|
if(orangeDistance(data->pos, ORANGE_SPAWN_X, ORANGE_SPAWN_Y) == 1){
|
||||||
aim_pos.y = 0;
|
data->aim=O_AIM_SPOT;
|
||||||
if(orangeDistance(data->pos, aim_pos) == 1){
|
|
||||||
data->aim=AIM_SPOT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ATTACK:
|
case O_WOODCUTTER:
|
||||||
case PICK:
|
case O_MINER:
|
||||||
data->aim=AIM_SPAWN;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void orangePickEffect(t_orange_info* data){
|
||||||
|
switch(data->job){
|
||||||
|
case O_GATHERER:
|
||||||
|
data->aim=O_AIM_SPAWN;
|
||||||
data->res_spot.type = getInventory();
|
data->res_spot.type = getInventory();
|
||||||
data->res_spot.pos = data->pos;
|
data->res_spot.pos = data->pos;
|
||||||
break;
|
break;
|
||||||
case PUT:
|
}
|
||||||
data->aim=AIM_SPOT;
|
}
|
||||||
|
|
||||||
|
void orangePutEffect(t_orange_info* data){
|
||||||
|
switch(data->job){
|
||||||
|
case O_GATHERER:
|
||||||
|
data->aim=O_AIM_SPOT;
|
||||||
break;
|
break;
|
||||||
case WORK:
|
}
|
||||||
case WAIT:
|
}
|
||||||
case COMMUNICATE:
|
|
||||||
default:
|
void orangeCommunicateEffect(t_orange_info* data){
|
||||||
|
data->listening = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void orangeWaitEffect(t_orange_info* data, t_com* com_data){
|
||||||
|
switch(data->job){
|
||||||
|
case O_JOBLESS:
|
||||||
|
if ((data->listening == 1) && (com_data != NULL)){
|
||||||
|
if (com_data->data[0] == 1){
|
||||||
|
data->job = com_data->data[1];
|
||||||
|
data->listening = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case O_MASTER:
|
||||||
|
if ((data->listening == 1) && (com_data != NULL)){
|
||||||
|
if(com_data->data[0] == 1)
|
||||||
|
data->talking = 1;
|
||||||
|
}else{
|
||||||
|
data->talking = 0;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// job functions
|
// job functions
|
||||||
void orangeSearchJob(t_action* action, t_info_data* data, int success){
|
void orangeJobless(t_action* action, t_orange_info* data, t_com* com_data, int success){
|
||||||
|
int dir_to_master;
|
||||||
|
|
||||||
|
dir_to_master = orangeNewDir(data);
|
||||||
|
|
||||||
|
if(data->listening){
|
||||||
|
action->type=WAIT;
|
||||||
|
action->dir=0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (data->talking){
|
||||||
|
if (getNear(dir_to_master) == DUDE){
|
||||||
|
action->type = COMMUNICATE;
|
||||||
|
action->dir = dir_to_master;
|
||||||
|
action->com_data.data[0]=1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
action->type = MOVE;
|
||||||
|
action->dir = dir_to_master;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void orangeGatherFood(t_action* action, t_info_data* data, int success){
|
|
||||||
|
void orangeMaster(t_action* action, t_orange_info* data, t_com* com_data, int success){
|
||||||
|
if (data->talking){
|
||||||
|
action->type = COMMUNICATE;
|
||||||
|
action->dir = com_data->flag;
|
||||||
|
action->com_data.data[0] = 1;
|
||||||
|
action->com_data.data[1] = O_GATHERER;
|
||||||
|
}else{
|
||||||
|
action->type = WAIT;
|
||||||
|
action->dir=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void orangeGatherer(t_action* action, t_orange_info* data, t_com* com_data, int success){
|
||||||
int i,t;
|
int i,t;
|
||||||
|
printf("gatherer\n");
|
||||||
//action choice
|
//action choice
|
||||||
for(i=0;i<4;i++){
|
for(i=0;i<4;i++){
|
||||||
t = getNear(i);
|
t = getNear(i);
|
||||||
@ -165,26 +264,31 @@ void orangeGatherFood(t_action* action, t_info_data* data, int success){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//orange tools
|
//orange tools
|
||||||
int orangeNewDir(t_info_data* data){
|
int orangeNewDir(t_orange_info* data){
|
||||||
int dir;
|
int dir;
|
||||||
int current_dist,new_dist;
|
int current_dist,new_dist;
|
||||||
t_coord aim_pos;
|
int x,y;
|
||||||
switch(data->aim){
|
switch(data->aim){
|
||||||
case NO_AIM:
|
case O_NO_AIM:
|
||||||
return (data->last_dir + rand()%3 )%4;
|
return (data->last_dir + rand()%3 )%4;
|
||||||
case AIM_SPAWN:
|
case O_AIM_MASTER:
|
||||||
aim_pos.x=0;
|
x = ORANGE_MASTER_X;
|
||||||
aim_pos.y=0;
|
y = ORANGE_MASTER_Y;
|
||||||
break;
|
break;
|
||||||
case AIM_SPOT:
|
case O_AIM_SPAWN:
|
||||||
aim_pos=data->res_spot.pos;
|
x = ORANGE_SPAWN_X;
|
||||||
|
y = ORANGE_SPAWN_Y;
|
||||||
|
break;
|
||||||
|
case O_AIM_SPOT:
|
||||||
|
x = data->res_spot.pos.x;
|
||||||
|
y = data->res_spot.pos.y;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
current_dist = orangeDistance(data->pos,aim_pos);
|
current_dist = orangeDistance(data->pos,x,y);
|
||||||
if (current_dist == 0) return 0;
|
if (current_dist == 0) return 0;
|
||||||
do{
|
do{
|
||||||
dir= rand()%4;
|
dir= rand()%4;
|
||||||
new_dist = orangeDistance(orangeNewPos(data->pos,dir),aim_pos);
|
new_dist = orangeDistance(orangeNewPos(data->pos,dir),x,y);
|
||||||
}while(current_dist <= new_dist);
|
}while(current_dist <= new_dist);
|
||||||
|
|
||||||
if (getNear(dir) == DUDE || getNear(dir) == TREE || getNear(dir) == ROCK || getNear(dir) == IRON_ORE){
|
if (getNear(dir) == DUDE || getNear(dir) == TREE || getNear(dir) == ROCK || getNear(dir) == IRON_ORE){
|
||||||
@ -212,6 +316,6 @@ t_coord orangeNewPos(t_coord pos, int dir){
|
|||||||
}
|
}
|
||||||
return new_pos;
|
return new_pos;
|
||||||
}
|
}
|
||||||
int orangeDistance(t_coord p1, t_coord p2){
|
int orangeDistance(t_coord c, int x, int y){
|
||||||
return abs(p2.x-p1.x) + abs(p2.y-p1.y);
|
return abs(x-c.x) + abs(y-c.y);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user