implemented communicate (not debugged)

This commit is contained in:
anselme16 2015-01-19 08:56:21 +01:00
parent daa95448ea
commit f17e21258c
5 changed files with 32 additions and 16 deletions

33
main.c
View File

@ -73,10 +73,10 @@ void updateTeam(t_team team){
t_coord getPos(t_coord coord, int dir){
switch(dir){
case NORTH :
coord.y++;
coord.y--;
break;
case SOUTH :
coord.y--;
coord.y++;
break;
case WEST :
coord.x++;
@ -147,6 +147,10 @@ void handleAction(t_action action, t_dude* dude){
t_coord target_pos = getPos(dude->pos, action.dir);
t_pixel target = map[target_pos.x][target_pos.y];
int* nb_res;
if(dude->com_data != NULL){
free(dude->com_data);
dude->com_data = NULL;
}
switch(action.type){
case MOVE :
dude->success = 0;
@ -192,7 +196,6 @@ void handleAction(t_action action, t_dude* dude){
dude->inventory = -1;
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
}else if(target.type == SPAWN && dude->inventory == FOOD){
printf("put food in spawn\n");
dude->inventory = -1;
nb_res = (int*)target.data;
teams[*nb_res].spawn_food++;
@ -282,9 +285,27 @@ void handleAction(t_action action, t_dude* dude){
dude->success = 1;
break;
case COMMUNICATE :
printf("forbidden action\n"); // TODO : implement that
// if target is dude -> sent message to dude
// if target is library -> sets library
switch(target.type){
case DUDE :
action.com_data.flag = (action.dir+2)%4;
dude->com_data = malloc(sizeof(t_com));
*(dude->com_data) = action.com_data;
dude->success = 1;
break;
case LIBRARY :
if(action.com_data.flag & READ){
action.com_data.flag = action.dir;
dude->com_data = malloc(sizeof(t_com));
*(dude->com_data) = action.com_data;
memcpy(dude->com_data + sizeof(int), target.data + (action.com_data.flag | 3), 32);
}else{
memcpy(target.data + action.com_data.flag, action.com_data.data, 32);
}
dude->success = 1;
break;
default :
break;
}
break;
}
}

2
main.h
View File

@ -37,7 +37,7 @@ typedef struct{
int success;
t_pixel ground;
void* custom_data;
void* com_data;
t_com* com_data;
} t_dude;
typedef struct{

View File

@ -94,8 +94,6 @@ void searchJob(t_action* action, t_info_data* data, int success){
}
void gatherFood(t_action* action, t_info_data* data, int success){
int i,t;
//default data for action
action->data = NULL;
//action choice
if (getInventory() == -1){
for(i=0;i<4;i++){

View File

@ -63,7 +63,7 @@ t_action purple_update(void* my_info, t_com* com_data, int success){
int distance = dist(data->pos, 0, 0);
if(distance == 1){
action.type = WAIT;
action.data = NULL;
action.dir = 0;
for(i=0; i<4; i++){
type = getNear(i);
@ -79,7 +79,7 @@ t_action purple_update(void* my_info, t_com* com_data, int success){
do{
action.dir = rand()%4;
}while(dist(newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
action.data = NULL;
}
data->last_dir = action.dir;
data->last_action = action.type;
@ -91,14 +91,12 @@ t_action purple_update(void* my_info, t_com* com_data, int success){
if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){
action.type = WORK;
action.dir = i;
action.data = NULL;
data->last_dir = action.dir;
data->last_action = action.type;
return action;
}else if(type == FOOD){
action.type = PICK;
action.dir = i;
action.data = NULL;
data->try = 1;
data->last_dir = action.dir;
data->last_action = action.type;
@ -116,7 +114,6 @@ t_action purple_update(void* my_info, t_com* com_data, int success){
&& type == IRON_ORE
&& type == TREE
&& type == LIBRARY);
action.data = NULL;
data->last_dir = action.dir;
data->last_action = action.type;
return action;

4
team.h
View File

@ -12,7 +12,7 @@ typedef struct{
// Directions
enum{
NORTH, SOUTH, EAST, WEST
NORTH, EAST, SOUTH, WEST
};
// communication
@ -52,7 +52,7 @@ enum{
typedef struct{
int type;
int dir;
void* data;
t_com com_data;
} t_action;
int getInventory();