initial commit
This commit is contained in:
commit
d8e43ff04f
BIN
Courier.ttf
Executable file
BIN
Courier.ttf
Executable file
Binary file not shown.
BIN
SDL_ttf.dll
Executable file
BIN
SDL_ttf.dll
Executable file
Binary file not shown.
254
main.c
Executable file
254
main.c
Executable file
@ -0,0 +1,254 @@
|
||||
#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;
|
||||
}
|
46
main.h
Normal file
46
main.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include "team.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define MAIN WinMain
|
||||
#else
|
||||
#define MAIN main
|
||||
#endif
|
||||
|
||||
#define MAX_FPS 60;
|
||||
#define MAX_DUDES 50;
|
||||
#define WIDTH 400;
|
||||
#define HEIGHT 250;
|
||||
|
||||
// Teams
|
||||
enum{
|
||||
PURPLE, ORANGE, NB_TEAMS;
|
||||
}
|
||||
|
||||
typedef struct{
|
||||
int type;
|
||||
void* data;
|
||||
} t_pixel;
|
||||
|
||||
typedef struct{
|
||||
t_coord pos;
|
||||
int team;
|
||||
int inventory;
|
||||
t_pixel ground;
|
||||
void* custom_data;
|
||||
void* com_data
|
||||
} t_dude
|
||||
|
||||
typedef struct{
|
||||
int team;
|
||||
int dude_size;
|
||||
t_action(void*, void*, int)* update;
|
||||
int nb_dudes;
|
||||
t_dude* dudes;
|
||||
t_coord spawn;
|
||||
int spawn_food;
|
||||
} t_team;
|
||||
|
||||
#endif
|
109
map_generation.c
Normal file
109
map_generation.c
Normal file
@ -0,0 +1,109 @@
|
||||
typedef struct{
|
||||
int x;
|
||||
int y;
|
||||
int type;
|
||||
int power;
|
||||
int radius;
|
||||
} t_epicenter;
|
||||
|
||||
#define MAX_POWER 10
|
||||
|
||||
// create list of epicenter
|
||||
t_epicenter* l_epicenters;
|
||||
int size_epi;
|
||||
|
||||
void map_init(){
|
||||
int i,j;
|
||||
//génération des épicentres
|
||||
l_epicenters = malloc();
|
||||
|
||||
for(i=0;i<NB_TEAMS;i++)
|
||||
create_epicenter(GRASS);
|
||||
|
||||
for(i=2; i<5;i++){
|
||||
int nb_epi = rand()%5;
|
||||
for(i=0;i<nb_epi;i++)
|
||||
create_epicenter(i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//génération de la carte
|
||||
for (i=0;i<WIDTH;i++){
|
||||
for(j=0;j>HEIGHT;j++){
|
||||
if (i == 0 || j == 0){
|
||||
map[i][j]=BEDROCK;
|
||||
else{
|
||||
map[i][j] = generate(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int spawns[NB_TEAMS];
|
||||
memset(spawns, 0, NB_TEAMS);
|
||||
|
||||
for(int k=0;k<NB_TEAMS;k++){
|
||||
x_rand= rand()%WIDTH;
|
||||
y_rand= rand()%HEIGHT;
|
||||
|
||||
int error = 1;
|
||||
while(error != 0){
|
||||
error = 0;
|
||||
for (int l=0;l<k;l++){
|
||||
t_coord sp = teams[l].spawn;
|
||||
if (distance_manhattan(x_rand,y_rand,sp.x,sp.y) < 50)
|
||||
error = 1;
|
||||
}
|
||||
}
|
||||
teams[k].spawn.x = x_rand;
|
||||
teams[k].spawn.y = y_rand;
|
||||
}
|
||||
}
|
||||
|
||||
create_epicenter(int type){
|
||||
t_epicenter epicenter;
|
||||
|
||||
epicenter.x=rand()%WIDTH;
|
||||
epicenter.y=rand()%HEIGHT;
|
||||
epicenter.type = type;
|
||||
epicenter.power = rand()%MAX_POWER;
|
||||
epicenter.radius = rand()%(WIDTH*HEIGHT/4);
|
||||
|
||||
l_epicenters[nb_epicenters++]=epicenter;
|
||||
}
|
||||
|
||||
int generate(int x, int y){
|
||||
int i, type;
|
||||
int proba[5];
|
||||
for(i=0;i<size_epi;i++){
|
||||
epi = l_epicenters[i];
|
||||
dist_to_epi = distance_manhattan(x,y,epi.x, epi.y);
|
||||
if (dist_to_epi < epi.radius){
|
||||
ratio = (int) ((epi.radius - dist_to_epi) * 100) / radius;
|
||||
proba[epi.type-1]=epi.power * ratio;
|
||||
}
|
||||
}
|
||||
|
||||
int sum=0;
|
||||
for (i=0;i<5;i++){
|
||||
sum += proba[i];
|
||||
}
|
||||
val = rand()%sum;
|
||||
|
||||
int seuil = 0;
|
||||
for (i=0;i<5;i++){
|
||||
start += proba[i]
|
||||
if(type < seuil){
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int distance_manhattan(int x1,int y1, int x2, int y2){
|
||||
return absolute(x1-x2) + absolute(y1-y2);
|
||||
}
|
||||
|
||||
int absolute(int val){
|
||||
return val > 0 ? val : -val;
|
||||
}
|
37
team.h
Normal file
37
team.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef TEAM_H
|
||||
#define TEAM_H
|
||||
|
||||
// Directions
|
||||
enum{
|
||||
NORTH, SOUTH, EAST, WEST
|
||||
};
|
||||
|
||||
// Tile types
|
||||
enum{
|
||||
BEDROCK, GRASS, ROCK, IRON_ORE, TREE, BERRIES, // nature
|
||||
FOOD, WOOD, STONE, IRON, // resources
|
||||
CORPSE, DUDE, // humans
|
||||
SPAWN, WALL, ROAD, SWORD, SIGN // buildings
|
||||
};
|
||||
|
||||
// Action types
|
||||
enum{
|
||||
MOVE, ATTACK, PICK, PUT, WORK, WAIT, COMMUNICATE
|
||||
};
|
||||
|
||||
typedef struct{
|
||||
int x;
|
||||
int y;
|
||||
} t_coord;
|
||||
|
||||
typedef struct{
|
||||
int type;
|
||||
int dir;
|
||||
void* data;
|
||||
} t_action;
|
||||
|
||||
int getInventory();
|
||||
|
||||
int getNear(int dir);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user