224 lines
4.9 KiB
C
Executable File
224 lines
4.9 KiB
C
Executable File
#include "main.h"
|
|
#include "team.h"
|
|
#include "tools.h"
|
|
#include "generator.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <math.h>
|
|
|
|
typedef struct{
|
|
t_dude dude1;
|
|
t_dude dude2;
|
|
int attack;
|
|
} 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);
|
|
int get_purple_size();
|
|
int get_orange_size();
|
|
|
|
void initWorld(){
|
|
int i;
|
|
|
|
// allocations
|
|
printf("Allocating memory...\n");
|
|
teams = malloc(sizeof(t_team)*NB_TEAMS);
|
|
for(i=0; i<NB_TEAMS; i++){
|
|
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;
|
|
}
|
|
teams[i].nb_dudes = 0;
|
|
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_count = 0;
|
|
}
|
|
map = (t_pixel**)malloc(sizeof(t_pixel*)*WIDTH);
|
|
for(i=0; i<WIDTH; i++)
|
|
map[i] = (t_pixel*)malloc(sizeof(t_pixel)*HEIGHT);
|
|
|
|
// generate map
|
|
printf("Generating map...\n");
|
|
create_map(WIDTH, HEIGHT);
|
|
|
|
// create image from map
|
|
printf("Creating image from map...\n");
|
|
generateImg();
|
|
}
|
|
|
|
void addFight(t_dude dude, t_dude other_dude){
|
|
|
|
}
|
|
|
|
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);
|
|
handleAction(action, team.dudes+i);
|
|
}
|
|
}
|
|
|
|
t_coord getPos(t_coord coord, int dir){
|
|
switch(dir){
|
|
case NORTH :
|
|
coord.y++;
|
|
break;
|
|
case SOUTH :
|
|
coord.y--;
|
|
break;
|
|
case WEST :
|
|
coord.x++;
|
|
break;
|
|
case EAST :
|
|
coord.x--;
|
|
break;
|
|
}
|
|
return coord;
|
|
}
|
|
|
|
void spawnDudes(){
|
|
int i;
|
|
t_dude new_dude;
|
|
for(i=0; i<NB_TEAMS; i++){
|
|
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 = teams[i].spawn;
|
|
new_dude.team = teams[i].team;
|
|
new_dude.inventory = -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);
|
|
new_dude.com_data = NULL;
|
|
|
|
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);
|
|
t_pixel target = map[target_pos.x][target_pos.y];
|
|
switch(action.type){
|
|
case MOVE :
|
|
if( target.type != WALL
|
|
&& target.type != ROCK
|
|
&& target.type != BEDROCK
|
|
&& target.type != IRON_ORE
|
|
&& target.type != DUDE
|
|
&& target.type != TREE
|
|
)
|
|
add_move(dude, target_pos);
|
|
break;
|
|
case ATTACK :
|
|
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
|
|
// else if target is spawn and inventory is food
|
|
// spawn.food ++
|
|
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
|
|
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;
|
|
}
|
|
}
|
|
|
|
void generateImg(){
|
|
int i, j;
|
|
for(i=0; i<WIDTH; i++){
|
|
for(j=0; j<HEIGHT; j++){
|
|
putpixel(img, i, j, getColor(map[i][j]));
|
|
}
|
|
}
|
|
}
|
|
|
|
void initSDL()
|
|
{
|
|
srand(time(NULL));
|
|
img = 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));
|
|
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);
|
|
}
|
|
}
|
|
|
|
int MAIN( int argc, char** argv )
|
|
{
|
|
Uint8 *keystate = SDL_GetKeyState(NULL);
|
|
int over = 0;
|
|
Uint32 temps;
|
|
|
|
printf("Starting Pixel Wars on %s\n", OS);
|
|
initSDL();
|
|
initWorld();
|
|
SDL_Flip(img);
|
|
|
|
printf("Launching simulation... press ESCAPE to stop.\n");
|
|
|
|
while (!keystate[SDLK_ESCAPE] && !over){
|
|
temps = SDL_GetTicks();
|
|
SDL_PumpEvents();
|
|
int i;
|
|
spawnDudes();
|
|
for(i=0; i<NB_TEAMS; i++)
|
|
updateTeam(teams[i]);
|
|
resolve_moves();
|
|
if(SDL_GetTicks()-temps < MAX_FPS) SDL_Delay(MAX_FPS+temps-SDL_GetTicks());
|
|
SDL_Flip(img);
|
|
}
|
|
SDL_FreeSurface(img);
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|