255 lines
5.1 KiB
C
Executable File
255 lines
5.1 KiB
C
Executable File
#include "main.h"
|
|
#include "team.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <math.h>
|
|
#include <SDL/SDL.h>
|
|
#include <SDL/SDL_ttf.h>
|
|
|
|
t_pixel** map;
|
|
t_teams* teams;
|
|
SDL_Surface* img;
|
|
|
|
void initWorld(){
|
|
int i, j;
|
|
// allocations
|
|
for(i=0; i<NB_TEAMS; i++)
|
|
dudes = malloc(sizeof(t_dude)*MAX_DUDES);
|
|
map = malloc(sizeof(t_pixel)*WIDTH*HEIGHT);
|
|
|
|
// generate map
|
|
// here
|
|
|
|
// create image from map
|
|
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++){
|
|
t_dude dude = team.dudes[i];
|
|
action = team.update((void*)(dude.custom_data), (void*)(dude.com_data));
|
|
handleAction(action, dude);
|
|
}
|
|
}
|
|
|
|
t_pixel getTile(t_coord coord, int dir){
|
|
switch(dir){
|
|
case NORTH :
|
|
coord.x++;
|
|
break;
|
|
case SOUTH :
|
|
coord.x--;
|
|
break;
|
|
case WEST :
|
|
coord.y++;
|
|
break;
|
|
case EAST :
|
|
coord.y--;
|
|
break;
|
|
}
|
|
return map[coord.x][coord.y];
|
|
}
|
|
|
|
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--;
|
|
|
|
new_dude.pos = team.spawn;
|
|
new_dude.team = team.team;
|
|
new_dude.inventory = -1;
|
|
new_dude.ground = SPAWN;
|
|
new_dude.custom_data = malloc(team.dude_size);
|
|
memset(new_dude.custom_data, 0, dude_size);
|
|
new_dude.com_data = NULL;
|
|
|
|
team.dudes[team.nb_dudes++] = new_dude;
|
|
}
|
|
}
|
|
}
|
|
|
|
void handleAction(t_action action, t_dude dude){
|
|
t_pixel target = getTile(dude.pos, action.dir);
|
|
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
|
|
){
|
|
map[dude.pos.x][dude.pos.y] = dude.ground;
|
|
dude.ground = target;
|
|
target.type = DUDE;
|
|
target.data = &dude;
|
|
}
|
|
break;
|
|
case ATTACK :
|
|
if(target.type == DUDE)
|
|
addFight(dude, *((dude*)(target.data)));
|
|
break;
|
|
case PICK :
|
|
|
|
// TODO
|
|
break;
|
|
case PUT :
|
|
break;
|
|
case WORK :
|
|
break;
|
|
case WAIT :
|
|
break;
|
|
case COMMUNICATE :
|
|
break;
|
|
}
|
|
}
|
|
|
|
void generateImg(){
|
|
Uint32 color;
|
|
for(i=0; i<WIDTH; i++){
|
|
for(j=0; j<HEIGHT; j++){
|
|
switch(map[i][j].type){
|
|
BEDROCK : color = 0xFF00FFFF; break;
|
|
GRASS : color = 0x00FF00FF; break;
|
|
ROCK : color = 0x000000FF; break;
|
|
IRON_ORE : color = 0x000000FF; break;
|
|
TREE : color = 0x000000FF; break;
|
|
BERRIES : color = 0x000000FF; break;
|
|
FOOD : color = 0x000000FF; break;
|
|
WOOD : color = 0x000000FF; break;
|
|
STONE : color = 0x000000FF; break;
|
|
IRON : color = 0x000000FF; break;
|
|
CORPSE : color = 0x000000FF; break;
|
|
DUDE : color = 0x000000FF; break;
|
|
SPAWN : color = 0x000000FF; break;
|
|
WALL : color = 0x000000FF; break;
|
|
ROAD : color = 0x000000FF; break;
|
|
SWORD : color = 0x000000FF; break;
|
|
SIGN : color = 0x000000FF; break;
|
|
}
|
|
putpixel(img, i, j, color);
|
|
//img->pixels[j * WIDTH + i] = color;
|
|
}
|
|
}
|
|
}
|
|
|
|
int min(int a, int b)
|
|
{
|
|
if (a<b) return a;
|
|
else return b;
|
|
}
|
|
|
|
int max(int a, int b)
|
|
{
|
|
if (a>b) return a;
|
|
else return b;
|
|
}
|
|
|
|
Uint32 getpixel(SDL_Surface *surface, int x, int y)
|
|
{
|
|
int bpp = surface->format->BytesPerPixel;
|
|
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
|
|
switch (bpp) {
|
|
case 1:
|
|
return *p;
|
|
|
|
case 2:
|
|
return *(Uint16 *)p;
|
|
case 3:
|
|
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
return p[0] << 16 | p[1] << 8 | p[2];
|
|
else
|
|
return p[0] | p[1] << 8 | p[2] << 16;
|
|
case 4:
|
|
return *(Uint32 *)p;
|
|
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
|
|
{
|
|
int bpp = surface->format->BytesPerPixel;
|
|
/* Here p is the address to the pixel we want to set */
|
|
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
|
|
|
|
switch(bpp) {
|
|
case 1:
|
|
*p = pixel;
|
|
break;
|
|
|
|
case 2:
|
|
*(Uint16 *)p = pixel;
|
|
break;
|
|
|
|
case 3:
|
|
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
|
|
p[0] = (pixel >> 16) & 0xff;
|
|
p[1] = (pixel >> 8) & 0xff;
|
|
p[2] = pixel & 0xff;
|
|
} else {
|
|
p[0] = pixel & 0xff;
|
|
p[1] = (pixel >> 8) & 0xff;
|
|
p[2] = (pixel >> 16) & 0xff;
|
|
}
|
|
break;
|
|
|
|
case 4:
|
|
*(Uint32 *)p = pixel;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void initSDL()
|
|
{
|
|
srand(time(NULL));
|
|
img = SDL_SetVideoMode(RESX, RESY, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
|
SDL_WM_SetCaption("Pixel Wars", NULL);
|
|
SDL_FillRect(img, NULL, SDL_MapRGB(img->format, 255, 255, 255));
|
|
if(TTF_Init() == -1)
|
|
{
|
|
fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (SDL_Init(SDL_INIT_VIDEO) == -1)
|
|
{
|
|
fprintf(stderr, "Erreur d'initialisation de la SDL");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
int MAIN( int argc, char** argv )
|
|
{
|
|
initSDL();
|
|
initWorld();
|
|
|
|
while (!keystate[SDLK_ESCAPE] && !over){
|
|
temps = SDL_GetTicks();
|
|
SDL_PumpEvents();
|
|
int i;
|
|
spawnDudes();
|
|
for(i=0; i<NB_TEAMS; i++)
|
|
updateTeam(teams[i]);
|
|
if(SDL_GetTicks()-temps < MAX_FPS) SDL_Delay(MAX_FPS+temps-SDL_GetTicks());
|
|
SDL_Flip(img);
|
|
}
|
|
TTF_CloseFont(police);
|
|
TTF_Quit();
|
|
SDL_FreeSurface(img);
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|