debugged move action, and some other render problems

This commit is contained in:
Anselme 2015-01-13 23:35:48 +01:00
parent ade7d5d38b
commit af2321fe71
9 changed files with 91 additions and 85 deletions

View File

@ -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;

View File

@ -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

View File

@ -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){

102
main.c
View File

@ -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<team.nb_dudes; i++){
t_dude dude = team.dudes[i];
action = team.update((void*)(dude.custom_data), (void*)(dude.com_data), i);
handleAction(action, dude);
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i);
handleAction(action, team.dudes+i);
}
}
t_coord getPos(t_coord coord, int dir){
switch(dir){
case NORTH :
coord.x++;
break;
case SOUTH :
coord.x--;
break;
case WEST :
coord.y++;
break;
case EAST :
case SOUTH :
coord.y--;
break;
case WEST :
coord.x++;
break;
case EAST :
coord.x--;
break;
}
return coord;
}
@ -97,28 +92,27 @@ void spawnDudes(){
int i;
t_dude new_dude;
for(i=0; i<NB_TEAMS; i++){
t_team team = teams[i];
if(team.spawn_food)
team.spawn_count++;
if(team.spawn_count > 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; i<WIDTH; i++){
for(j=0; j<HEIGHT; j++){
switch(map[i][j].type){
case BEDROCK : color = 0x101020; break;
case GRASS : color = 0x004400; break;
case ROCK : color = 0x8C8C8C; break;
case IRON_ORE : color = 0x917B61; break;
case TREE : color = 0x000F0F; break;
case BERRIES : color = 0x6B87C7; break;
case FOOD : color = 0xFF7A7A; break;
case WOOD : color = 0x634A22; break;
case STONE : color = 0x999999; break;
case IRON : color = 0x555555; break;
case CORPSE : color = 0xFF0000; break;
case DUDE :
dudeData = map[i][j].data;
color = dudeData->team == 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; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_moves();

9
main.h
View File

@ -12,7 +12,7 @@
#define OS "Linux"
#endif
#define MAX_FPS 60
#define MAX_FPS 4
#define MAX_DUDES 50
#define WIDTH 400
#define HEIGHT 250
@ -49,6 +49,11 @@ typedef struct{
void generateImg();
void handleAction(t_action action, t_dude dude);
void handleAction(t_action action, t_dude* dude);
// variables globales
t_pixel** map;
t_team* teams;
SDL_Surface* img;
#endif

View File

@ -16,7 +16,7 @@ t_action orange_update(void* my_info, void* com_data, int my_id){
t_action action;
action.type = MOVE;
action.dir = SOUTH;
action.dir = rand()%4;
action.data = NULL;
return action;

View File

@ -16,7 +16,7 @@ t_action purple_update(void* my_info, void* com_data, int my_id){
t_action action;
action.type = MOVE;
action.dir = NORTH;
action.dir = rand()%4;
action.data = NULL;
return action;

49
tools.c
View File

@ -10,10 +10,8 @@ t_move* clearMoves[MAX_DUDES*NB_TEAMS];
int nb_clear = 0;
t_move* occupiedMoves[MAX_DUDES*NB_TEAMS];
int nb_occupied = 0;
t_pixel** map;
void add_move(t_dude* dude, t_coord dst, t_pixel** the_map){
map = the_map;
void add_move(t_dude* dude, t_coord dst){
t_move* move = malloc(sizeof(t_move));
move->dude = 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;

View File

@ -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);