diff --git a/main.c b/main.c index fb3ecd8..f77d3d8 100755 --- a/main.c +++ b/main.c @@ -15,10 +15,8 @@ typedef struct{ } t_fight; // temp code -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(); +t_action purple_update(void* my_info, void* com_data, int success); +t_action orange_update(void* my_info, void* com_data, int success); t_dude* current_dude; @@ -32,11 +30,9 @@ void initWorld(int width, int height){ teams[i].team = i; switch(i){ case ORANGE : - teams[i].dude_size = get_orange_size(); teams[i].update = orange_update; break; case PURPLE : - teams[i].dude_size = get_purple_size(); teams[i].update = purple_update; break; } @@ -44,7 +40,7 @@ void initWorld(int width, int height){ teams[i].dudes = malloc(sizeof(t_dude)*MAX_DUDES); teams[i].spawn.x = 0; teams[i].spawn.y = 0; - teams[i].spawn_food = 10; + teams[i].spawn_food = NB_STARTING_FOOD; teams[i].spawn_count = 0; } map = (t_pixel**)malloc(sizeof(t_pixel*)*width); @@ -69,7 +65,7 @@ void updateTeam(t_team team){ t_action action; for(i=0; ipos, action.dir); t_pixel target = map[target_pos.x][target_pos.y]; int* nb_res; + t_team* team; switch(action.type){ case MOVE : dude->success = 0; @@ -134,6 +131,7 @@ void handleAction(t_action action, t_dude* dude){ && target.type != IRON_ORE && target.type != DUDE && target.type != TREE + && target.type != LIBRARY ) add_move(dude, target_pos); break; @@ -151,13 +149,13 @@ void handleAction(t_action action, t_dude* dude){ free(nb_res); map[target_pos.x][target_pos.y].type = GRASS; map[target_pos.x][target_pos.y].data = NULL; + 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 dude->success = 0; break; case PUT : - if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){ + if(dude->inventory != -1 && (target.type == GRASS || target.type == MARK || target.type == dude->inventory)){ if(target.type == GRASS || target.type == MARK){ map[target_pos.x][target_pos.y].type = dude->inventory; nb_res = malloc(sizeof(int)); @@ -169,8 +167,15 @@ 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 + }else if(target.type == SPAWN && dude->inventory == FOOD){ + printf("put food in spawn\n"); + dude->inventory = -1; + team = (t_team*)target.data; + team->spawn_food++; + }else{ + printf("put failed : trying to put %d in %d\n", dude->inventory, target.type); dude->success = 0; + } break; case WORK : dude->success = 1; @@ -209,12 +214,20 @@ void handleAction(t_action action, t_dude* dude){ break; case WOOD : 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; + switch(*nb_res){ + case 1 : + free(target.data); + map[target_pos.x][target_pos.y].type = WALL; + map[target_pos.x][target_pos.y].data = NULL; + break; + case 2 : + free(target.data); + map[target_pos.x][target_pos.y].type = LIBRARY; + map[target_pos.x][target_pos.y].data = malloc(128); + memset(map[target_pos.x][target_pos.y].data, 0, 128); + default : + dude->success = 0; + break; } break; case STONE : @@ -246,8 +259,8 @@ void handleAction(t_action action, t_dude* dude){ break; case COMMUNICATE : printf("forbidden action\n"); // TODO : implement that - // if target is sign -> set sign message // if target is dude -> sent message to dude + // if target is library -> sets library break; } } @@ -352,13 +365,13 @@ int MAIN case SDLK_ESCAPE : over = 1; break; - case SDLK_w : + case SDLK_z : y_offset -= 5; break; case SDLK_s : y_offset += 5; break; - case SDLK_a : + case SDLK_q : x_offset -= 5; break; case SDLK_d : diff --git a/main.h b/main.h index ce283e0..92019f1 100644 --- a/main.h +++ b/main.h @@ -15,6 +15,7 @@ #define STACK_SIZE 5 #define DEFAULT_WIDTH 400 #define DEFAULT_HEIGHT 250 +#define NB_STARTING_FOOD 5 // Teams enum{ @@ -38,8 +39,7 @@ typedef struct{ typedef struct{ int team; - int dude_size; - t_action (*update)(void*, void*, int, int); + t_action (*update)(void*, void*, int); int nb_dudes; t_dude* dudes; t_coord spawn; diff --git a/orange.c b/orange.c index 42c65e4..0660416 100644 --- a/orange.c +++ b/orange.c @@ -7,11 +7,7 @@ typedef struct{ int plop; // custom info } t_data; -int get_orange_size(){ - return sizeof(t_data); -} - -t_action orange_update(void* my_info, void* com_data, int my_id, int success){ +t_action orange_update(void* my_info, void* com_data, int success){ t_data* data = (t_data*)my_info; t_action action; diff --git a/purple.c b/purple.c index 1541f04..6d92f3c 100644 --- a/purple.c +++ b/purple.c @@ -1,3 +1,4 @@ +#include #include "team.h" #include "stdlib.h" @@ -7,44 +8,60 @@ typedef struct{ t_coord pos; int try; int brings_food; + int last_dir; + int last_action; } t_data; -int get_purple_size(){ - return sizeof(t_data); -} - t_coord getPos(t_coord coord, int dir); -t_action purple_update(void* my_info, void* com_data, int my_id, int success){ +int abs(int val){ + return val > 0 ? val : -val; +} + +int dist(t_coord coord, int x, int y){ + return abs(coord.x-x) + abs(coord.y-y); +} + +t_action purple_update(void* my_info, void* com_data, int success){ t_data* data = (t_data*)my_info; t_action action; int i, type; - if(data->try && success) + if(data->last_action == MOVE && success) + data->pos = getPos(data->pos, data->last_dir); + + if(data->try && success){ data->brings_food = 1; + } data->try = 0; if(data->brings_food){ - if(data->pos.x == 0) - action.dir = WEST; - else{ - if(data->pos.y == 0){ - if(data->pos.x > 0) - action.dir = EAST; - else - action.dir = WEST; - if(data->pos.x == -1 || data->pos.x == 1){ - data->brings_food = 0; + int distance = dist(data->pos, 0, 0); + if(distance == 1){ + action.type = WAIT; + action.data = NULL; + action.dir = 0; + data->last_action = action.type; + for(i=0; i<4; i++){ + type = getNear(i); + if(type == SPAWN){ + action.dir = i; action.type = PUT; - action.data = NULL; - return action; + data->brings_food = 0; + data->last_action = action.type; + break; } - }else - action.dir = data->pos.y > 0 ? SOUTH : NORTH; + } + }else{ + action.type = MOVE; + do{ + action.dir = rand()%4; + }while(dist(getPos(data->pos, action.dir), 0, 0) > distance && distance != 0); + action.data = NULL; + data->pos = getPos(data->pos, action.dir); + data->last_dir = action.dir; + data->last_action = action.type; } - action.type = MOVE; - action.data = NULL; - data->pos = getPos(data->pos, action.dir); return action; } @@ -54,18 +71,25 @@ t_action purple_update(void* my_info, void* com_data, int my_id, int success){ 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_action = action.type; + return action; } } action.type = MOVE; - action.dir = rand()%4; + do{ + action.dir = rand()%4; + }while(action.dir == data->last_dir); action.data = NULL; - data->pos = getPos(data->pos, action.dir); + data->last_dir = action.dir; + data->last_action = action.type; return action; } \ No newline at end of file diff --git a/team.h b/team.h index 602ffe7..b9ef836 100644 --- a/team.h +++ b/team.h @@ -1,6 +1,9 @@ #ifndef TEAM_H #define TEAM_H +#define LIBRARY_SIZE 128 +#define DUDE_MEMORY 128 + // Directions enum{ NORTH, SOUTH, EAST, WEST @@ -11,7 +14,7 @@ enum{ BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature FOOD, WOOD, STONE, IRON, SWORD, // resources DUDE, // humans - SPAWN, WALL, ROAD, MARK, SIGN // buildings + SPAWN, WALL, ROAD, MARK, LIBRARY // buildings }; // Action types diff --git a/tools.c b/tools.c index a88035b..b04eb70 100644 --- a/tools.c +++ b/tools.c @@ -68,6 +68,7 @@ Uint32 getColor(t_pixel pixel){ switch(pixel.type){ case BEDROCK : return 0x101020; case GRASS : return 0x719678; + case MARK : return 0x5D7B62; case ROCK : return 0x8C8C8C; case IRON_ORE : return 0x917B61; case TREE : return 0x003800; @@ -81,12 +82,11 @@ Uint32 getColor(t_pixel pixel){ return dudeData->team == ORANGE ? 0x7A4100 : 0x9900FF; case SPAWN : spawnData = (int*)(pixel.data); - if(spawnData == NULL) printf("WTF\n"); return *spawnData == ORANGE ? 0xFFC080 : 0xD596FF; case WALL : return 0xE6B2A1; case ROAD : return 0xEDB287; case SWORD : return 0xEBEBEB; - case SIGN : return 0xA37A50; + case LIBRARY : return 0xA37A50; default : return 0x0000FF; // bleu absolu = bug }