From 0bc16047479d36c96c2038a090cd63c656b8311e Mon Sep 17 00:00:00 2001 From: Anselme Date: Wed, 14 Jan 2015 22:57:58 +0100 Subject: [PATCH] implemented pick/put test algorith for purple, but it is bugged --- main.c | 83 +++++++++++++++++++++++++++++++++++++++++--------------- purple.c | 53 ++++++++++++++++++++++++++++++++++-- team.h | 2 +- 3 files changed, 112 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index 80375d0..4db6e48 100755 --- a/main.c +++ b/main.c @@ -20,6 +20,8 @@ t_action orange_update(void* my_info, void* com_data, int my_id, int success); int get_purple_size(); int get_orange_size(); +t_dude* current_dude; + void initWorld(int width, int height){ int i; @@ -66,6 +68,7 @@ void updateTeam(t_team team){ int i; t_action action; for(i=0; ipos, dir); + return map[coord.x][coord.y].type; +} + void spawnDudes(){ int i; t_dude new_dude; @@ -144,12 +152,13 @@ void handleAction(t_action action, t_dude* dude){ 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])); }else dude->success = 0; break; case PUT : if(dude->inventory != -1 && (target.type == GRASS || target.type == dude->inventory)){ - if(target.type == GRASS){ + if(target.type == GRASS || target.type == MARK){ map[target_pos.x][target_pos.y].type = dude->inventory; nb_res = malloc(sizeof(int)); *nb_res = 1; @@ -159,6 +168,7 @@ void handleAction(t_action action, t_dude* dude){ (*nb_res)++; } dude->inventory = -1; + putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y])); }else dude->success = 0; break; @@ -190,20 +200,30 @@ void handleAction(t_action action, t_dude* dude){ 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].type = MARK; map[target_pos.x][target_pos.y].data = NULL; break; - case ROAD : + case MARK : map[target_pos.x][target_pos.y].type = GRASS; map[target_pos.x][target_pos.y].data = NULL; 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; + } + 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].type = ROAD; map[target_pos.x][target_pos.y].data = NULL; } break; @@ -214,12 +234,12 @@ void handleAction(t_action action, t_dude* dude){ else map[target_pos.x][target_pos.y].type = SWORD; break; - // TODO : case wood -> sign - // TODO : case sign -> wood default : dude->success = 0; break; } + if(dude->success) + putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y])); break; case WAIT : dude->success = 1; @@ -265,8 +285,8 @@ void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){ SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); for(i=0; iw; i++){ for(j=0; jh; j++){ - x = i/zoom_level + x_offset; - y = j/zoom_level + y_offset; + x = (i - img->w/2)/zoom_level + x_offset; + y = (j - img->h/2)/zoom_level + y_offset; if(x >= 0 && x < img->w && y >= 0 && y < img->h) putpixel(screen, i, j, getpixel(img, x, y)); } @@ -283,7 +303,10 @@ int MAIN int y_offset = 0; int zoom_level = 1; int over = 0; + int time = 0; int wait_time = 100; + int new_time = 0; + int remaining_time = -1; int fullscreen = 0; int width = DEFAULT_WIDTH; int height = DEFAULT_HEIGHT; @@ -296,9 +319,6 @@ int MAIN case 'f' : fullscreen = 1; break; - case 't' : - wait_time = atoi(argv[0]+2); - break; case 'w' : width = atoi(argv[0]+2); break; @@ -317,8 +337,11 @@ int MAIN initWorld(width, height); SDL_Flip(img); + x_offset = width/2; + y_offset = height/2; printf("Launching simulation...\n"); + time = SDL_GetTicks(); while (!over){ while(SDL_PollEvent(&event)){ switch (event.type){ @@ -327,22 +350,29 @@ int MAIN case SDLK_ESCAPE : over = 1; break; + case SDLK_w : + y_offset -= 5; + break; + case SDLK_s : + y_offset += 5; + break; + case SDLK_a : + x_offset -= 5; + break; + case SDLK_d : + x_offset += 5; + break; case SDLK_UP : - y_offset--; + wait_time *= 2; break; case SDLK_DOWN : - y_offset++; + if(wait_time > 2) + wait_time /= 2; break; case SDLK_LEFT : - x_offset--; - break; - case SDLK_RIGHT : - x_offset++; - break; - case SDLK_PAGEUP : zoom_level++; break; - case SDLK_PAGEDOWN : + case SDLK_RIGHT : zoom_level--; if(zoom_level < 1) zoom_level = 1; @@ -354,6 +384,10 @@ int MAIN break; } break; + case SDL_MOUSEMOTION: + x_offset += event.motion.xrel; + y_offset += event.motion.yrel; + break; case SDL_QUIT: over = 1; break; @@ -362,16 +396,21 @@ int MAIN } } - if(!paused){ + new_time = SDL_GetTicks(); + remaining_time -= new_time - time; + time = new_time; + if(remaining_time < 0 && !paused){ spawnDudes(); for(i=0; itry && 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; + action.type = PUT; + action.data = NULL; + return action; + } + }else + action.dir = data->pos.y > 0 ? SOUTH : NORTH; + } + action.type = MOVE; + action.data = NULL; + data->pos = getPos(data->pos, action.dir); + return action; + } + + for(i=0; i<4; i++){ + type = getNear(i); + if(type == BERRIES || type == TREE || type == IRON_ORE || type == ROCK){ + action.type = WORK; + action.dir = i; + action.data = NULL; + return action; + }else if(type == FOOD){ + action.type = PICK; + action.dir = i; + action.data = NULL; + data->try = 1; + } + } action.type = MOVE; action.dir = rand()%4; action.data = NULL; - + data->pos = getPos(data->pos, action.dir); return action; } \ No newline at end of file diff --git a/team.h b/team.h index 3212783..602ffe7 100644 --- a/team.h +++ b/team.h @@ -11,7 +11,7 @@ enum{ BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature FOOD, WOOD, STONE, IRON, SWORD, // resources DUDE, // humans - SPAWN, WALL, ROAD, SIGN // buildings + SPAWN, WALL, ROAD, MARK, SIGN // buildings }; // Action types