272 lines
5.7 KiB
C
Executable File
272 lines
5.7 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>
|
|
#include <string.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 width, int height){
|
|
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(width, height);
|
|
}
|
|
|
|
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 width, int height){
|
|
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(int width, int height, int fullscreen)
|
|
{
|
|
srand(time(NULL));
|
|
if(fullscreen)
|
|
img = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
|
|
else
|
|
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
|
|
{
|
|
SDL_Event event;
|
|
int i;
|
|
int over = 0;
|
|
int wait_time = 100;
|
|
int fullscreen = 0;
|
|
int width = DEFAULT_WIDTH;
|
|
int height = DEFAULT_HEIGHT;
|
|
|
|
#ifndef _WIN32
|
|
argv++;
|
|
while(argc > 1){ // pas initialisé sur windows
|
|
printf("%s\n", argv[0]);
|
|
switch(argv[0][0]){
|
|
case 'f' :
|
|
fullscreen = 1;
|
|
break;
|
|
case 't' :
|
|
wait_time = atoi(argv[0]+2);
|
|
break;
|
|
case 'w' :
|
|
width = atoi(argv[0]+2);
|
|
break;
|
|
case 'h' :
|
|
height = atoi(argv[0]+2);
|
|
break;
|
|
default :
|
|
break;
|
|
}
|
|
argv++;
|
|
}
|
|
#endif
|
|
|
|
initSDL(width, height, fullscreen);
|
|
initWorld(width, height);
|
|
SDL_Flip(img);
|
|
|
|
printf("Launching simulation...\n");
|
|
|
|
while (!over){
|
|
while(SDL_PollEvent(&event)){
|
|
switch (event.type){
|
|
case SDL_KEYDOWN:
|
|
switch(event.key.keysym.sym) {
|
|
case SDLK_ESCAPE :
|
|
over = 1;
|
|
break;
|
|
default :
|
|
break;
|
|
}
|
|
break;
|
|
case SDL_QUIT:
|
|
over = 1;
|
|
break;
|
|
default :
|
|
break;
|
|
}
|
|
}
|
|
|
|
spawnDudes();
|
|
for(i=0; i<NB_TEAMS; i++)
|
|
updateTeam(teams[i]);
|
|
resolve_moves();
|
|
|
|
SDL_Delay(wait_time);
|
|
SDL_Flip(img);
|
|
}
|
|
SDL_FreeSurface(img);
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|