bug with com_data->flag
This commit is contained in:
parent
9b6cbe6aad
commit
cc9e8538dd
260
orange.c
260
orange.c
@ -7,13 +7,19 @@
|
||||
//Structure definition
|
||||
|
||||
enum{
|
||||
JOBLESS, MASTER, GATHERER, WOODCUTTER, MINER, NB_JOBS
|
||||
O_JOBLESS, O_MASTER, O_GATHERER, O_WOODCUTTER, O_MINER, O_NB_JOBS
|
||||
};
|
||||
|
||||
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{
|
||||
char type; //the type of ressource that was found
|
||||
t_coord pos; // pos where ressource was found
|
||||
@ -22,117 +28,210 @@ typedef struct{
|
||||
typedef struct{
|
||||
char name; // name of the dude
|
||||
t_coord pos; // position relative to the spawn
|
||||
char hatched;
|
||||
char age;
|
||||
char job;
|
||||
char aim;
|
||||
char talking;
|
||||
char listening;
|
||||
t_orange_ressource_spot res_spot;
|
||||
char last_action;
|
||||
char last_dir;
|
||||
} t_orange_info_data;
|
||||
|
||||
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);
|
||||
} t_orange_info;
|
||||
|
||||
//tools functions
|
||||
int orangeNewDir(t_info_data*);
|
||||
int orangeNewDir(t_orange_info*);
|
||||
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
|
||||
void orangeSearchJob(t_action*, t_info_data*, int);
|
||||
void orangeGatherFood(t_action*, t_info_data*, int);
|
||||
void orangeHatched(t_orange_info*);
|
||||
|
||||
t_action orange_update(void* my_info, t_com* communication_data, int success){
|
||||
t_info_data* data = (t_info_data*)my_info;
|
||||
//t_data* data = (t_data*)my_info;
|
||||
void orangeJobless(t_action*, t_orange_info*, t_com*, int);
|
||||
void orangeMaster(t_action*, t_orange_info*, t_com*, int);
|
||||
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;
|
||||
|
||||
if(data->hatched == 0){
|
||||
if(data->age == 0){
|
||||
orangeHatched(data);
|
||||
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){
|
||||
case JOBLESS:
|
||||
action.type = WAIT;
|
||||
action.dir=0;
|
||||
case O_JOBLESS:
|
||||
orangeJobless(&action, data, com_data, success);
|
||||
break;
|
||||
case MASTER:
|
||||
printf("I'm the master!\n");
|
||||
action.type = WAIT;
|
||||
action.dir=0;
|
||||
case O_MASTER:
|
||||
orangeMaster(&action, data, com_data, success);
|
||||
break;
|
||||
case GATHERER:
|
||||
orangeGatherFood(&action,data,success);
|
||||
case O_GATHERER:
|
||||
orangeGatherer(&action,data, com_data, success);
|
||||
break;
|
||||
case WOODCUTTER:
|
||||
case MINER:
|
||||
case O_WOODCUTTER:
|
||||
case O_MINER:
|
||||
break;
|
||||
}
|
||||
data->last_action = action.type;
|
||||
data->last_dir = action.dir;
|
||||
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){
|
||||
case MOVE:
|
||||
//update position
|
||||
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 (orangeDistance(data->pos,data->res_spot.pos) == 0){
|
||||
data->aim = NO_AIM;
|
||||
if (orangeDistance(data->pos,data->res_spot.pos.x, data->res_spot.pos.y) == 0){
|
||||
data->aim = O_NO_AIM;
|
||||
}
|
||||
}else{
|
||||
aim_pos.x = 0;
|
||||
aim_pos.y = 0;
|
||||
if(orangeDistance(data->pos, aim_pos) == 1){
|
||||
data->aim=AIM_SPOT;
|
||||
if(orangeDistance(data->pos, ORANGE_SPAWN_X, ORANGE_SPAWN_Y) == 1){
|
||||
data->aim=O_AIM_SPOT;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ATTACK:
|
||||
case PICK:
|
||||
data->aim=AIM_SPAWN;
|
||||
case O_WOODCUTTER:
|
||||
case O_MINER:
|
||||
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.pos = data->pos;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
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;
|
||||
printf("gatherer\n");
|
||||
//action choice
|
||||
for(i=0;i<4;i++){
|
||||
t = getNear(i);
|
||||
@ -165,26 +264,31 @@ void orangeGatherFood(t_action* action, t_info_data* data, int success){
|
||||
}
|
||||
|
||||
//orange tools
|
||||
int orangeNewDir(t_info_data* data){
|
||||
int orangeNewDir(t_orange_info* data){
|
||||
int dir;
|
||||
int current_dist,new_dist;
|
||||
t_coord aim_pos;
|
||||
int x,y;
|
||||
switch(data->aim){
|
||||
case NO_AIM:
|
||||
case O_NO_AIM:
|
||||
return (data->last_dir + rand()%3 )%4;
|
||||
case AIM_SPAWN:
|
||||
aim_pos.x=0;
|
||||
aim_pos.y=0;
|
||||
case O_AIM_MASTER:
|
||||
x = ORANGE_MASTER_X;
|
||||
y = ORANGE_MASTER_Y;
|
||||
break;
|
||||
case AIM_SPOT:
|
||||
aim_pos=data->res_spot.pos;
|
||||
case O_AIM_SPAWN:
|
||||
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;
|
||||
}
|
||||
current_dist = orangeDistance(data->pos,aim_pos);
|
||||
current_dist = orangeDistance(data->pos,x,y);
|
||||
if (current_dist == 0) return 0;
|
||||
do{
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
int orangeDistance(t_coord p1, t_coord p2){
|
||||
return abs(p2.x-p1.x) + abs(p2.y-p1.y);
|
||||
int orangeDistance(t_coord c, int x, int y){
|
||||
return abs(x-c.x) + abs(y-c.y);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user