diff --git a/generator.c b/generator.c index 8948368..a672732 100644 --- a/generator.c +++ b/generator.c @@ -26,7 +26,7 @@ void create_epicenter(int x, int y, int type); void create_epicenter_random(int type); int generate(int x, int y); -void create_map(t_pixel** map, t_team* teams, int w, int h){ +void create_map(int w, int h){ int i,j; int type; diff --git a/generator.h b/generator.h index 0f762e4..e84b155 100644 --- a/generator.h +++ b/generator.h @@ -1,6 +1,6 @@ #ifndef GENERATOR_H #define GENERATOR_H -void create_map(t_pixel** map, t_team* teams, int width, int height); +void create_map(int width, int height); #endif \ No newline at end of file diff --git a/generator_anselme.c b/generator_anselme.c index 2ced43f..f59cb83 100644 --- a/generator_anselme.c +++ b/generator_anselme.c @@ -15,7 +15,7 @@ int max(int a, int b) return a > b ? a : b; } -void create_map(t_pixel** map, t_team* teams, int w, int h){ +void create_map(int w, int h){ int i,j, k; int r = (w/NB_TEAMS < h ? w/NB_TEAMS : h)/2; @@ -39,7 +39,7 @@ void create_map(t_pixel** map, t_team* teams, int w, int h){ if(d == 0){ map[i][j].type = SPAWN; map[i][j].data = malloc(sizeof(int)); - *(int*)(map[i][j].data) = k; + *((int*)(map[i][j].data)) = k; }else{ int l = (d-20)+(rand()%40); if(l > r+15){ diff --git a/main.c b/main.c index 2f321a6..bfbf17b 100755 --- a/main.c +++ b/main.c @@ -13,10 +13,6 @@ typedef struct{ int attack; } t_fight; -t_pixel** map; -t_team* teams; -SDL_Surface* img; - // 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); @@ -45,7 +41,7 @@ void initWorld(){ teams[i].dudes = malloc(sizeof(t_dude)*MAX_DUDES); teams[i].spawn.x = 0; teams[i].spawn.y = 0; - teams[i].spawn_food = 0; + teams[i].spawn_food = 10; teams[i].spawn_count = 0; } map = (t_pixel**)malloc(sizeof(t_pixel*)*WIDTH); @@ -54,7 +50,7 @@ void initWorld(){ // generate map printf("Generating map...\n"); - create_map(map, teams, WIDTH, HEIGHT); + create_map(WIDTH, HEIGHT); // create image from map printf("Creating image from map...\n"); @@ -69,26 +65,25 @@ void updateTeam(t_team team){ int i; t_action action; for(i=0; i 10 && map[team.spawn.x][team.spawn.y].type == SPAWN){ - team.spawn_food--; + if(teams[i].spawn_food > 0) + teams[i].spawn_count++; + if(teams[i].spawn_count > 10 && map[teams[i].spawn.x][teams[i].spawn.y].type == SPAWN){ + teams[i].spawn_food--; + teams[i].spawn_count = 0; - new_dude.pos = team.spawn; - new_dude.team = team.team; + new_dude.pos = teams[i].spawn; + new_dude.team = teams[i].team; new_dude.inventory = -1; - new_dude.ground.type = SPAWN; - new_dude.ground.data = NULL; - new_dude.custom_data = malloc(team.dude_size); - memset(new_dude.custom_data, 0, team.dude_size); + new_dude.ground = map[teams[i].spawn.x][teams[i].spawn.y]; + new_dude.custom_data = malloc(teams[i].dude_size); + memset(new_dude.custom_data, 0, teams[i].dude_size); new_dude.com_data = NULL; - team.dudes[team.nb_dudes++] = new_dude; + teams[i].dudes[teams[i].nb_dudes++] = new_dude; } } } -void handleAction(t_action action, t_dude dude){ - t_coord target_pos = getPos(dude.pos, action.dir); +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]; switch(action.type){ case MOVE : @@ -129,18 +123,21 @@ void handleAction(t_action action, t_dude dude){ && target.type != DUDE && target.type != TREE ) - add_move(&dude, target_pos, map); + add_move(dude, target_pos); break; case ATTACK : - if(target.type == DUDE) - addFight(dude, *((t_dude*)(target.data))); + printf("forbidden action\n"); + //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 break; case PUT : + printf("forbidden action\n"); // if target is grass or road or corpse // target = inventory // inventory = NULL @@ -148,6 +145,7 @@ void handleAction(t_action action, t_dude dude){ // spawn.food ++ break; case WORK : + printf("forbidden action\n"); // switch target // case rock -> stone // case berries -> food @@ -162,9 +160,11 @@ void handleAction(t_action action, t_dude dude){ // case corpse -> grass break; case WAIT : + printf("forbidden action\n"); // ... break; case COMMUNICATE : + printf("forbidden action\n"); // if target is sign -> set sign message // if target is dude -> sent message to dude break; @@ -173,41 +173,9 @@ void handleAction(t_action action, t_dude dude){ void generateImg(){ int i, j; - Uint32 color; - t_dude* dudeData; - int* spawnData; for(i=0; iteam == ORANGE ? 0xFF8000 : 0x9900FF; - break; - case SPAWN : - spawnData = (int*)map[i][j].data; - color = *spawnData == ORANGE ? 0xFFC080 : 0xD596FF; - break; - - case WALL : color = 0xE6B2A1; break; - case ROAD : color = 0xEDB287; break; - case SWORD : color = 0xEBEBEB; break; - case SIGN : color = 0xA37A50; break; - default : color = 0x0000FF; break; // bleu absolu = bug - } - putpixel(img, i, j, color); - //img->pixels[j * WIDTH + i] = color; + putpixel(img, i, j, getColor(map[i][j])); } } } @@ -242,7 +210,7 @@ int MAIN( int argc, char** argv ) temps = SDL_GetTicks(); SDL_PumpEvents(); int i; - //spawnDudes(); + spawnDudes(); for(i=0; idude = dude; move->dst = dst; @@ -26,16 +24,19 @@ void add_move(t_dude* dude, t_coord dst, t_pixel** the_map){ void applyMove(t_move* move){ t_pixel target = map[move->dst.x][move->dst.y]; - map[move->dude->pos.x][move->dude->pos.y] = move->dude->ground; - move->dude->ground = target; - target.type = DUDE; - target.data = move->dude; + map[move->dude->pos.x][move->dude->pos.y] = move->dude->ground; // set the ground where the dude was + move->dude->ground = target; // set the ground of the dude to where he goes + map[move->dst.x][move->dst.y].type = DUDE; // set the target tile with the dude + map[move->dst.x][move->dst.y].data = move->dude; + putpixel(img, move->dst.x, move->dst.y, getColor(map[move->dst.x][move->dst.y])); + 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; } void resolve_moves(){ int change = 1; int i; - // clear moves while(nb_clear > 0){ i = rand()%nb_clear; @@ -46,7 +47,6 @@ void resolve_moves(){ } clearMoves[i] = clearMoves[--nb_clear]; } - // occupied moves while(change){ change = 0; @@ -60,6 +60,37 @@ void resolve_moves(){ nb_occupied = 0; } +Uint32 getColor(t_pixel pixel){ + t_dude* dudeData; + int* spawnData; + switch(pixel.type){ + case BEDROCK : return 0x101020; + case GRASS : return 0x719678; + case ROCK : return 0x8C8C8C; + case IRON_ORE : return 0x917B61; + case TREE : return 0x003800; + case BERRIES : return 0x4D6394; + case FOOD : return 0xFF7A7A; + 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; + 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; + default : return 0x0000FF; // bleu absolu = bug + } + +} + Uint32 getpixel(SDL_Surface *surface, int x, int y) { int bpp = surface->format->BytesPerPixel; diff --git a/tools.h b/tools.h index 3d9ba90..f6ad9f6 100644 --- a/tools.h +++ b/tools.h @@ -5,10 +5,12 @@ #include "main.h" #include "team.h" -void add_move(t_dude* dude, t_coord dst, t_pixel** the_map); +void add_move(t_dude* dude, t_coord dst); void resolve_moves(); +Uint32 getColor(t_pixel pixel); + void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); Uint32 getpixel(SDL_Surface *surface, int x, int y);