317 lines
6.9 KiB
C
Executable File
317 lines
6.9 KiB
C
Executable File
#include "main.h"
|
|
#include "team.h"
|
|
#include "generator.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <math.h>
|
|
|
|
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);
|
|
int get_purple_size();
|
|
int get_orange_size();
|
|
|
|
void initWorld(){
|
|
int i, j;
|
|
|
|
// 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 = 0;
|
|
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(map, teams, 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++){
|
|
t_dude dude = team.dudes[i];
|
|
action = team.update((void*)(dude.custom_data), (void*)(dude.com_data), i);
|
|
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.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.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, *((t_dude*)(target.data)));
|
|
break;
|
|
case PICK :
|
|
// if target is resource :
|
|
// put target in inventory
|
|
// put grass on target
|
|
break;
|
|
case PUT :
|
|
// 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 :
|
|
// 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 :
|
|
// ...
|
|
break;
|
|
case COMMUNICATE :
|
|
// if target is sign -> set sign message
|
|
// if target is dude -> sent message to dude
|
|
break;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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(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);
|
|
|
|
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);
|
|
}
|
|
SDL_FreeSurface(img);
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|