implemented communicate (not debugged)
This commit is contained in:
parent
daa95448ea
commit
f17e21258c
33
main.c
33
main.c
@ -73,10 +73,10 @@ void updateTeam(t_team team){
|
|||||||
t_coord getPos(t_coord coord, int dir){
|
t_coord getPos(t_coord coord, int dir){
|
||||||
switch(dir){
|
switch(dir){
|
||||||
case NORTH :
|
case NORTH :
|
||||||
coord.y++;
|
coord.y--;
|
||||||
break;
|
break;
|
||||||
case SOUTH :
|
case SOUTH :
|
||||||
coord.y--;
|
coord.y++;
|
||||||
break;
|
break;
|
||||||
case WEST :
|
case WEST :
|
||||||
coord.x++;
|
coord.x++;
|
||||||
@ -147,6 +147,10 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
t_coord target_pos = getPos(dude->pos, action.dir);
|
t_coord target_pos = getPos(dude->pos, action.dir);
|
||||||
t_pixel target = map[target_pos.x][target_pos.y];
|
t_pixel target = map[target_pos.x][target_pos.y];
|
||||||
int* nb_res;
|
int* nb_res;
|
||||||
|
if(dude->com_data != NULL){
|
||||||
|
free(dude->com_data);
|
||||||
|
dude->com_data = NULL;
|
||||||
|
}
|
||||||
switch(action.type){
|
switch(action.type){
|
||||||
case MOVE :
|
case MOVE :
|
||||||
dude->success = 0;
|
dude->success = 0;
|
||||||
@ -192,7 +196,6 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
dude->inventory = -1;
|
dude->inventory = -1;
|
||||||
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
|
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
|
||||||
}else if(target.type == SPAWN && dude->inventory == FOOD){
|
}else if(target.type == SPAWN && dude->inventory == FOOD){
|
||||||
printf("put food in spawn\n");
|
|
||||||
dude->inventory = -1;
|
dude->inventory = -1;
|
||||||
nb_res = (int*)target.data;
|
nb_res = (int*)target.data;
|
||||||
teams[*nb_res].spawn_food++;
|
teams[*nb_res].spawn_food++;
|
||||||
@ -282,9 +285,27 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
dude->success = 1;
|
dude->success = 1;
|
||||||
break;
|
break;
|
||||||
case COMMUNICATE :
|
case COMMUNICATE :
|
||||||
printf("forbidden action\n"); // TODO : implement that
|
switch(target.type){
|
||||||
// if target is dude -> sent message to dude
|
case DUDE :
|
||||||
// if target is library -> sets library
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
main.h
2
main.h
@ -37,7 +37,7 @@ typedef struct{
|
|||||||
int success;
|
int success;
|
||||||
t_pixel ground;
|
t_pixel ground;
|
||||||
void* custom_data;
|
void* custom_data;
|
||||||
void* com_data;
|
t_com* com_data;
|
||||||
} t_dude;
|
} t_dude;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
|
2
orange.c
2
orange.c
@ -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){
|
void gatherFood(t_action* action, t_info_data* data, int success){
|
||||||
int i,t;
|
int i,t;
|
||||||
//default data for action
|
|
||||||
action->data = NULL;
|
|
||||||
//action choice
|
//action choice
|
||||||
if (getInventory() == -1){
|
if (getInventory() == -1){
|
||||||
for(i=0;i<4;i++){
|
for(i=0;i<4;i++){
|
||||||
|
7
purple.c
7
purple.c
@ -63,7 +63,7 @@ t_action purple_update(void* my_info, t_com* com_data, int success){
|
|||||||
int distance = dist(data->pos, 0, 0);
|
int distance = dist(data->pos, 0, 0);
|
||||||
if(distance == 1){
|
if(distance == 1){
|
||||||
action.type = WAIT;
|
action.type = WAIT;
|
||||||
action.data = NULL;
|
|
||||||
action.dir = 0;
|
action.dir = 0;
|
||||||
for(i=0; i<4; i++){
|
for(i=0; i<4; i++){
|
||||||
type = getNear(i);
|
type = getNear(i);
|
||||||
@ -79,7 +79,7 @@ t_action purple_update(void* my_info, t_com* com_data, int success){
|
|||||||
do{
|
do{
|
||||||
action.dir = rand()%4;
|
action.dir = rand()%4;
|
||||||
}while(dist(newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
|
}while(dist(newPos(data->pos, action.dir), 0, 0) > distance && distance != 0);
|
||||||
action.data = NULL;
|
|
||||||
}
|
}
|
||||||
data->last_dir = action.dir;
|
data->last_dir = action.dir;
|
||||||
data->last_action = action.type;
|
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){
|
if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){
|
||||||
action.type = WORK;
|
action.type = WORK;
|
||||||
action.dir = i;
|
action.dir = i;
|
||||||
action.data = NULL;
|
|
||||||
data->last_dir = action.dir;
|
data->last_dir = action.dir;
|
||||||
data->last_action = action.type;
|
data->last_action = action.type;
|
||||||
return action;
|
return action;
|
||||||
}else if(type == FOOD){
|
}else if(type == FOOD){
|
||||||
action.type = PICK;
|
action.type = PICK;
|
||||||
action.dir = i;
|
action.dir = i;
|
||||||
action.data = NULL;
|
|
||||||
data->try = 1;
|
data->try = 1;
|
||||||
data->last_dir = action.dir;
|
data->last_dir = action.dir;
|
||||||
data->last_action = action.type;
|
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 == IRON_ORE
|
||||||
&& type == TREE
|
&& type == TREE
|
||||||
&& type == LIBRARY);
|
&& type == LIBRARY);
|
||||||
action.data = NULL;
|
|
||||||
data->last_dir = action.dir;
|
data->last_dir = action.dir;
|
||||||
data->last_action = action.type;
|
data->last_action = action.type;
|
||||||
return action;
|
return action;
|
||||||
|
4
team.h
4
team.h
@ -12,7 +12,7 @@ typedef struct{
|
|||||||
|
|
||||||
// Directions
|
// Directions
|
||||||
enum{
|
enum{
|
||||||
NORTH, SOUTH, EAST, WEST
|
NORTH, EAST, SOUTH, WEST
|
||||||
};
|
};
|
||||||
|
|
||||||
// communication
|
// communication
|
||||||
@ -52,7 +52,7 @@ enum{
|
|||||||
typedef struct{
|
typedef struct{
|
||||||
int type;
|
int type;
|
||||||
int dir;
|
int dir;
|
||||||
void* data;
|
t_com com_data;
|
||||||
} t_action;
|
} t_action;
|
||||||
|
|
||||||
int getInventory();
|
int getInventory();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user