This commit is contained in:
Lendemor 2015-01-15 00:31:15 +01:00
commit 79bc7066b0
6 changed files with 253 additions and 54 deletions

238
main.c
View File

@ -15,11 +15,13 @@ typedef struct{
} t_fight;
// 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);
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_dude* current_dude;
void initWorld(int width, int height){
int i;
@ -66,7 +68,8 @@ void updateTeam(t_team team){
int i;
t_action action;
for(i=0; i<team.nb_dudes; i++){
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i);
current_dude = team.dudes + i;
action = team.update((void*)(team.dudes[i].custom_data), (void*)(team.dudes[i].com_data), i, team.dudes[i].success);
handleAction(action, team.dudes+i);
}
}
@ -89,6 +92,11 @@ t_coord getPos(t_coord coord, int dir){
return coord;
}
int getNear(int dir){
t_coord coord = getPos(current_dude->pos, dir);
return map[coord.x][coord.y].type;
}
void spawnDudes(){
int i;
t_dude new_dude;
@ -102,6 +110,7 @@ void spawnDudes(){
new_dude.pos = teams[i].spawn;
new_dude.team = teams[i].team;
new_dude.inventory = -1;
new_dude.success = 1;
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);
@ -115,8 +124,10 @@ void spawnDudes(){
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];
int* nb_res;
switch(action.type){
case MOVE :
dude->success = 0;
if( target.type != WALL
&& target.type != ROCK
&& target.type != BEDROCK
@ -127,45 +138,114 @@ void handleAction(t_action action, t_dude* dude){
add_move(dude, target_pos);
break;
case ATTACK :
printf("forbidden action\n");
printf("forbidden action\n"); // TODO : implement that
//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
nb_res = target.data;
if(target.type >= FOOD && target.type <= SWORD && dude->inventory == -1){
dude->inventory = target.type;
(*nb_res)--;
if(*nb_res < 1){
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]));
}else
dude->success = 0;
break;
case PUT :
printf("forbidden action\n");
// if target is grass or road or corpse
// target = inventory
// inventory = NULL
// else if target is spawn and inventory is food
// spawn.food ++
if(dude->inventory != -1 && (target.type == GRASS || 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));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
}else{
nb_res = target.data;
(*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;
case WORK :
printf("forbidden action\n");
// switch target
// case rock -> stone
// case berries -> food
// case tree -> wood
// case grass -> road
// case road -> grass
// case stone -> wall
// case wood -> sign
// case sign -> wood
// case iron_ore -> iron
// case iron -> sword
// case corpse -> grass
dude->success = 1;
switch(target.type){
case ROCK :
map[target_pos.x][target_pos.y].type = STONE;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case BERRIES :
map[target_pos.x][target_pos.y].type = FOOD;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case TREE :
map[target_pos.x][target_pos.y].type = WOOD;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case IRON_ORE :
map[target_pos.x][target_pos.y].type = IRON;
nb_res = malloc(sizeof(int));
*nb_res = 1;
map[target_pos.x][target_pos.y].data = nb_res;
break;
case GRASS :
map[target_pos.x][target_pos.y].type = MARK;
map[target_pos.x][target_pos.y].data = NULL;
break;
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 = ROAD;
map[target_pos.x][target_pos.y].data = NULL;
}
break;
case IRON :
nb_res = target.data;
if(*nb_res != 1)
dude->success = 0;
else
map[target_pos.x][target_pos.y].type = SWORD;
break;
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 :
printf("forbidden action\n");
// ...
dude->success = 1;
break;
case COMMUNICATE :
printf("forbidden action\n");
printf("forbidden action\n"); // TODO : implement that
// if target is sign -> set sign message
// if target is dude -> sent message to dude
break;
@ -173,6 +253,7 @@ void handleAction(t_action action, t_dude* dude){
}
void generateImg(int width, int height){
img = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
int i, j;
for(i=0; i<width; i++){
for(j=0; j<height; j++){
@ -181,28 +262,51 @@ void generateImg(int width, int height){
}
}
void initSDL(int width, int height, int fullscreen)
SDL_Surface* initSDL(int width, int height, int fullscreen)
{
SDL_Surface* screen;
srand(time(NULL));
if(fullscreen)
img = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
else
img = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Pixel Wars", NULL);
SDL_FillRect(img, NULL, SDL_MapRGB(img->format, 255, 255, 255));
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur d'initialisation de la SDL");// and you don't want my french comment :p
exit(EXIT_FAILURE);
}
return screen;
}
void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){
int i, j, x, y;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
for(i=0; i<screen->w; i++){
for(j=0; j<screen->h; j++){
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));
}
}
}
int MAIN
{
SDL_Surface* screen;
SDL_Event event;
int i;
int paused = 0;
int x_offset = 0;
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;
@ -215,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;
@ -228,15 +329,19 @@ int MAIN
break;
}
argv++;
argc--;
}
#endif
initSDL(width, height, fullscreen);
screen = initSDL(width, height, fullscreen);
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){
@ -245,10 +350,44 @@ 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 :
wait_time *= 2;
break;
case SDLK_DOWN :
if(wait_time > 2)
wait_time /= 2;
break;
case SDLK_LEFT :
zoom_level++;
break;
case SDLK_RIGHT :
zoom_level--;
if(zoom_level < 1)
zoom_level = 1;
break;
case SDLK_SPACE :
paused = !paused;
break;
default :
break;
}
break;
case SDL_MOUSEMOTION:
x_offset += event.motion.xrel;
y_offset += event.motion.yrel;
break;
case SDL_QUIT:
over = 1;
break;
@ -257,15 +396,24 @@ int MAIN
}
}
spawnDudes();
for(i=0; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_moves();
new_time = SDL_GetTicks();
remaining_time -= new_time - time;
time = new_time;
if(remaining_time < 0 && !paused){
spawnDudes();
for(i=0; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_moves();
remaining_time = wait_time;
}
SDL_Delay(wait_time);
SDL_Flip(img);
render(screen, x_offset, y_offset, zoom_level);
SDL_Delay(30);
SDL_Flip(screen);
}
SDL_FreeSurface(img);
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}

4
main.h
View File

@ -12,6 +12,7 @@
#endif
#define MAX_DUDES 50
#define STACK_SIZE 5
#define DEFAULT_WIDTH 400
#define DEFAULT_HEIGHT 250
@ -29,6 +30,7 @@ typedef struct{
t_coord pos;
int team;
int inventory;
int success;
t_pixel ground;
void* custom_data;
void* com_data;
@ -37,7 +39,7 @@ typedef struct{
typedef struct{
int team;
int dude_size;
t_action (*update)(void*, void*, int);
t_action (*update)(void*, void*, int, int);
int nb_dudes;
t_dude* dudes;
t_coord spawn;

View File

@ -11,7 +11,7 @@ int get_orange_size(){
return sizeof(t_data);
}
t_action orange_update(void* my_info, void* com_data, int my_id){
t_action orange_update(void* my_info, void* com_data, int my_id, int success){
t_data* data = (t_data*)my_info;
t_action action;

View File

@ -4,20 +4,68 @@
// Hello World
typedef struct{
int plop; // custom info
t_coord pos;
int try;
int brings_food;
} t_data;
int get_purple_size(){
return sizeof(t_data);
}
t_action purple_update(void* my_info, void* com_data, int my_id){
t_coord getPos(t_coord coord, int dir);
t_action purple_update(void* my_info, void* com_data, int my_id, int success){
t_data* data = (t_data*)my_info;
t_action action;
int i, type;
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;
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;
}

6
team.h
View File

@ -9,9 +9,9 @@ enum{
// Tile types
enum{
BEDROCK, GRASS, TREE, BERRIES, ROCK, IRON_ORE, // nature
FOOD, WOOD, STONE, IRON, // resources
CORPSE, DUDE, // humans
SPAWN, WALL, ROAD, SWORD, SIGN // buildings
FOOD, WOOD, STONE, IRON, SWORD, // resources
DUDE, // humans
SPAWN, WALL, ROAD, MARK, SIGN // buildings
};
// Action types

View File

@ -32,6 +32,8 @@ void applyMove(t_move* move){
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;
move->dude->success = 1;
free(move);
}
void resolve_moves(){
@ -74,7 +76,6 @@ Uint32 getColor(t_pixel pixel){
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;