From f17e21258c5dea7e372ae2d3aed4cbf917419800 Mon Sep 17 00:00:00 2001 From: anselme16 Date: Mon, 19 Jan 2015 08:56:21 +0100 Subject: [PATCH] implemented communicate (not debugged) --- main.c | 33 +++++++++++++++++++++++++++------ main.h | 2 +- orange.c | 2 -- purple.c | 7 ++----- team.h | 4 ++-- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/main.c b/main.c index c26676a..edac239 100755 --- a/main.c +++ b/main.c @@ -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; } } diff --git a/main.h b/main.h index 2893317..ac7429f 100644 --- a/main.h +++ b/main.h @@ -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{ diff --git a/orange.c b/orange.c index 30b54a0..e00b08c 100644 --- a/orange.c +++ b/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){ int i,t; - //default data for action - action->data = NULL; //action choice if (getInventory() == -1){ for(i=0;i<4;i++){ diff --git a/purple.c b/purple.c index be0f063..cd3663d 100644 --- a/purple.c +++ b/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); 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; diff --git a/team.h b/team.h index 347101e..162be99 100644 --- a/team.h +++ b/team.h @@ -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();