From 6697095a8791ec1c53307ae442d16951070ce956 Mon Sep 17 00:00:00 2001 From: anselme16 Date: Wed, 14 Jan 2015 11:57:27 +0100 Subject: [PATCH] implemented work, pick, put (not debugged yet) --- main.c | 121 +++++++++++++++++++++++++++++++++++++++++-------------- main.h | 4 +- orange.c | 2 +- purple.c | 3 +- team.h | 6 +-- tools.c | 3 +- 6 files changed, 102 insertions(+), 37 deletions(-) diff --git a/main.c b/main.c index 14d9644..c6c4af3 100755 --- a/main.c +++ b/main.c @@ -15,8 +15,8 @@ typedef struct{ } t_fight; // temp code -t_action purple_update(void* my_info, void* com_data, int my_id); -t_action orange_update(void* my_info, void* com_data, int my_id); +t_action purple_update(void* my_info, void* com_data, int my_id, int success); +t_action orange_update(void* my_info, void* com_data, int my_id, int success); int get_purple_size(); int get_orange_size(); @@ -66,7 +66,7 @@ void updateTeam(t_team team){ int i; t_action action; for(i=0; ipos, action.dir); t_pixel target = map[target_pos.x][target_pos.y]; + int* nb_res; switch(action.type){ case MOVE : + dude->success = 0; if( target.type != WALL && target.type != ROCK && target.type != BEDROCK @@ -127,45 +130,102 @@ void handleAction(t_action action, t_dude* dude){ add_move(dude, target_pos); break; case ATTACK : - printf("forbidden action\n"); + printf("forbidden action\n"); // TODO : implement that //if(target.type == DUDE) // addFight(dude, *((t_dude*)(target.data))); break; case PICK : - printf("forbidden action\n"); - // if target is resource : - // put target in inventory - // put grass on target + nb_res = target.data; + if(target.type >= FOOD && target.type <= SWORD && dude->inventory == -1){ + dude->inventory = target.type; + (*nb_res)--; + if(*nb_res < 1){ + free(nb_res); + map[target_pos.x][target_pos.y].type = GRASS; + map[target_pos.x][target_pos.y].data = NULL; + } + }else + dude->success = 0; break; case PUT : - printf("forbidden action\n"); - // if target is grass or road or corpse - // target = inventory - // inventory = NULL - // else if target is spawn and inventory is food - // spawn.food ++ + if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){ + if(target.type == GRASS){ + map[target_pos.x][target_pos.y].type = dude->inventory; + nb_res = malloc(sizeof(int)); + *nb_res = 1; + map[target_pos.x][target_pos.y].data = nb_res; + }else{ + nb_res = target.data; + (*nb_res)++; + } + dude->inventory = -1; + }else + dude->success = 0; break; case WORK : - printf("forbidden action\n"); - // switch target - // case rock -> stone - // case berries -> food - // case tree -> wood - // case grass -> road - // case road -> grass - // case stone -> wall - // case wood -> sign - // case sign -> wood - // case iron_ore -> iron - // case iron -> sword - // case corpse -> grass + dude->success = 1; + switch(target.type){ + case ROCK : + map[target_pos.x][target_pos.y].type = STONE; + nb_res = malloc(sizeof(int)); + *nb_res = 1; + map[target_pos.x][target_pos.y].data = nb_res; + break; + case BERRIES : + map[target_pos.x][target_pos.y].type = FOOD; + nb_res = malloc(sizeof(int)); + *nb_res = 1; + map[target_pos.x][target_pos.y].data = nb_res; + break; + case TREE : + map[target_pos.x][target_pos.y].type = WOOD; + nb_res = malloc(sizeof(int)); + *nb_res = 1; + map[target_pos.x][target_pos.y].data = nb_res; + break; + case IRON_ORE : + map[target_pos.x][target_pos.y].type = IRON; + nb_res = malloc(sizeof(int)); + *nb_res = 1; + map[target_pos.x][target_pos.y].data = nb_res; + break; + case GRASS : + map[target_pos.x][target_pos.y].type = ROAD; + map[target_pos.x][target_pos.y].data = NULL; + break; + case ROAD : + map[target_pos.x][target_pos.y].type = GRASS; + map[target_pos.x][target_pos.y].data = NULL; + break; + case STONE : + nb_res = target.data; + if(*nb_res != 1) + dude->success = 0; + else{ + free(target.data); + map[target_pos.x][target_pos.y].type = WALL; + map[target_pos.x][target_pos.y].data = NULL; + } + break; + case IRON : + nb_res = target.data; + if(*nb_res != 1) + dude->success = 0; + else + map[target_pos.x][target_pos.y].type = SWORD; + break; + // TODO : case wood -> sign + // TODO : case sign -> wood + default : + dude->success = 0; + break; + } break; case WAIT : - printf("forbidden action\n"); - // ... + dude->success = 1; break; case COMMUNICATE : - printf("forbidden action\n"); + printf("forbidden action\n"); // TODO : implement that // if target is sign -> set sign message // if target is dude -> sent message to dude break; @@ -228,6 +288,7 @@ int MAIN break; } argv++; + argc--; } #endif diff --git a/main.h b/main.h index 53a4cde..ce283e0 100644 --- a/main.h +++ b/main.h @@ -12,6 +12,7 @@ #endif #define MAX_DUDES 50 +#define STACK_SIZE 5 #define DEFAULT_WIDTH 400 #define DEFAULT_HEIGHT 250 @@ -29,6 +30,7 @@ typedef struct{ t_coord pos; int team; int inventory; + int success; t_pixel ground; void* custom_data; void* com_data; @@ -37,7 +39,7 @@ typedef struct{ typedef struct{ int team; int dude_size; - t_action (*update)(void*, void*, int); + t_action (*update)(void*, void*, int, int); int nb_dudes; t_dude* dudes; t_coord spawn; diff --git a/orange.c b/orange.c index a2ff514..42c65e4 100644 --- a/orange.c +++ b/orange.c @@ -11,7 +11,7 @@ int get_orange_size(){ return sizeof(t_data); } -t_action orange_update(void* my_info, void* com_data, int my_id){ +t_action orange_update(void* my_info, void* com_data, int my_id, int success){ t_data* data = (t_data*)my_info; t_action action; diff --git a/purple.c b/purple.c index 8461c15..7b2a15c 100644 --- a/purple.c +++ b/purple.c @@ -11,9 +11,10 @@ int get_purple_size(){ return sizeof(t_data); } -t_action purple_update(void* my_info, void* com_data, int my_id){ +t_action purple_update(void* my_info, void* com_data, int my_id, int success){ t_data* data = (t_data*)my_info; t_action action; + int i; action.type = MOVE; action.dir = rand()%4; diff --git a/team.h b/team.h index 98e19f9..3212783 100644 --- a/team.h +++ b/team.h @@ -9,9 +9,9 @@ enum{ // Tile types enum{ BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature - FOOD, WOOD, STONE, IRON, // resources - CORPSE, DUDE, // humans - SPAWN, WALL, ROAD, SWORD, SIGN // buildings + FOOD, WOOD, STONE, IRON, SWORD, // resources + DUDE, // humans + SPAWN, WALL, ROAD, SIGN // buildings }; // Action types diff --git a/tools.c b/tools.c index f8282be..a88035b 100644 --- a/tools.c +++ b/tools.c @@ -32,6 +32,8 @@ void applyMove(t_move* move){ putpixel(img, move->dude->pos.x, move->dude->pos.y, getColor(map[move->dude->pos.x][move->dude->pos.y])); move->dude->pos.x = move->dst.x; move->dude->pos.y = move->dst.y; + move->dude->success = 1; + free(move); } void resolve_moves(){ @@ -74,7 +76,6 @@ Uint32 getColor(t_pixel pixel){ case WOOD : return 0x634A22; case STONE : return 0x454545; case IRON : return 0x4A4036; - case CORPSE : return 0xFF0000; case DUDE : dudeData = pixel.data; return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF;