diff --git a/main.c b/main.c index 2c143b3..d27de08 100755 --- a/main.c +++ b/main.c @@ -52,7 +52,6 @@ void initWorld(int width, int height){ void updateTeam(t_team team){ int i; - t_action action; for(i=0; i<team.nb_dudes; i++){ team.dudes[i].action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), team.dudes[i].success); @@ -127,6 +126,7 @@ void spawnDudes(){ new_dude.team = teams[i].team; new_dude.inventory = -1; new_dude.success = 1; + new_dude.dead = 0; new_dude.ground = map[teams[i].spawn.x][teams[i].spawn.y]; new_dude.custom_data = malloc(DUDE_MEMORY); memset(new_dude.custom_data, 0, DUDE_MEMORY); @@ -138,9 +138,9 @@ void spawnDudes(){ } void handleAction(t_dude* dude){ + t_action action = dude->action; t_coord target_pos = getPos(dude->pos, action.dir); t_pixel target = map[target_pos.x][target_pos.y]; - t_action action = dude->action; int* nb_res; if(dude->com_data != NULL){ free(dude->com_data); @@ -159,9 +159,28 @@ void handleAction(t_dude* dude){ add_move(dude, target_pos); break; case ATTACK : - printf("forbidden action\n"); // TODO : implement that - //if(target.type == DUDE) - // addFight(dude, *((t_dude*)(target.data))); + dude->success = 0; + if( target.type == DUDE + || target.type == SPAWN + || target.type == WALL + || target.type == LIBRARY + || target.type == ROAD + ){ + dude->success = 1; + if(target.type == DUDE) + add_fight(dude, target_pos); + else{ + if(target.type == SPAWN){ + nb_res = target.data; + teams[*nb_res].spawn_food = 0; + teams[*nb_res].spawn_count = 0; + } + if(target.data != NULL) + free(target.data); + map[target_pos.x][target_pos.y].type = GRASS; + map[target_pos.x][target_pos.y].data = NULL; + } + } break; case PICK : nb_res = target.data; @@ -281,19 +300,27 @@ void handleAction(t_dude* dude){ break; case COMMUNICATE : switch(target.type){ - // TODO : conflict possible if 2 dudes talk to the same dude + // TODO : conflicts are not handled in a fair way 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; + if(dude->com_data == NULL){ + dude->com_data = malloc(sizeof(t_com)); + *(dude->com_data) = action.com_data; + dude->success = 1; + }else + dude->success = 0; 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); + if(dude->com_data == NULL){ + 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{ + dude->success = 0; + break; + } }else{ memcpy(target.data + action.com_data.flag, action.com_data.data, 32); } @@ -477,7 +504,7 @@ int MAIN spawnDudes(); for(i=0; i<NB_TEAMS; i++) updateTeam(teams[i]); - resolve_fights(); + // resolve_fights(); // TODO : debugging resolve_moves(); remaining_time = wait_time; diff --git a/main.h b/main.h index 974f0b8..063983f 100644 --- a/main.h +++ b/main.h @@ -35,6 +35,7 @@ typedef struct{ int team; int inventory; int success; + int dead; t_pixel ground; t_action action; void* custom_data; @@ -53,7 +54,7 @@ typedef struct{ void generateImg(); -void handleAction(t_action action, t_dude* dude); +void handleAction(t_dude* dude); // variables globales t_pixel** map; diff --git a/team.h b/team.h index f725dab..8c4752a 100644 --- a/team.h +++ b/team.h @@ -34,7 +34,7 @@ typedef struct{ enum{ BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature FOOD, WOOD, STONE, IRON, SWORD, // resources - DUDE, DEAD_DUDE // humans + DUDE, DEAD_DUDE, // humans SPAWN, WALL, ROAD, MARK, LIBRARY // buildings }; diff --git a/tools.c b/tools.c index c4d04ec..df366d4 100644 --- a/tools.c +++ b/tools.c @@ -61,7 +61,8 @@ void add_fight(t_dude* dude, t_coord dst){ void applyMove(t_move* move){ t_pixel target = map[move->dst.x][move->dst.y]; - if( target.type == WALL + if( move->dude->dead + || target.type == WALL || target.type == ROCK || target.type == BEDROCK || target.type == IRON_ORE @@ -83,13 +84,21 @@ void applyMove(t_move* move){ } void resolve_fights(){ - int i; + int i, j; int defense = 0; for(i=0; i<nb_fight; i++){ int type = fights[i]->dude->action.type; defense = (type == MOVE || type == ATTACK)*(1 + 3*(fights[i]->dude->inventory == SWORD)); if((defense*100)/(defense + fights[i]->dmg) < fights[i]->proba){ - // perdu + t_dude* target = fights[i]->target; + t_team team = teams[target->team]; + for(j=0; j<team.nb_dudes; j++){ + if(team.dudes + j == target){ + team.dudes[j] = team.dudes[--team.nb_dudes]; + map[target->pos.x][target->pos.y].type = DEAD_DUDE; + break; + } + } } free(fights[i]); }