added biome generator

This commit is contained in:
Anselme FRANÇOIS 2016-05-17 19:23:14 +02:00
parent b40adcc3db
commit 5ed2fd2bde
5 changed files with 62 additions and 63 deletions

View File

@ -11,20 +11,24 @@ extern "C" void generate(Map *mapPtr)
int w = map.getWidth(); int w = map.getWidth();
int h = map.getHeight(); int h = map.getHeight();
int n = map.getNbTeams(); int n = map.getNbTeams();
int i,j, k; int i, j, k;
int r = (w/n < h ? w/n : h)/2;
int *teamCoord = new int[n*2];
for(i=0; i<n; i++) for(i=0; i<n; i++)
map.team(i) = Coord((w/(n*2))*(1+i*2), h/2); map.team(i) = Coord((w/(n*2))*(1+i*2), h/2);
if(n == 0)
n = 1;
int r = (w/n < h ? w/n : h)/2;
//génération de la carte //génération de la carte
for (i=0;i<w;i++){ for (i=0;i<w;i++){
for(j=0;j<h;j++){ for(j=0;j<h;j++){
if (i == 0 || j == 0 || i == w-1 || j == h-1 || n == 0){ if (i == 0 || j == 0 || i == w-1 || j == h-1){
map[i][j].type = Pixel::BEDROCK; map[i][j].type = Pixel::BEDROCK;
}else{ }else{
int d = std::max(w, h); int d = std::max(w, h);
if(n == 0)
d = 30;
for(k=0; k<n; k++){ for(k=0; k<n; k++){
d = std::min(d, map.team(k).dist(i, j)); d = std::min(d, map.team(k).dist(i, j));
if(!d) if(!d)

BIN
generators/anselme.dll Normal file

Binary file not shown.

View File

@ -1,4 +1,8 @@
#include "main.h" #include <map.h>
#include <cstdlib> // rand
#include <cstring> // memset
// g++ -shared biome.cpp -o biome.dll -I../src
#define MAX_POWER 10 #define MAX_POWER 10
#define MAX_EPICENTER_BY_TYPE 7 #define MAX_EPICENTER_BY_TYPE 7
@ -8,7 +12,7 @@
//map type ----> not used for now //map type ----> not used for now
enum{ enum{
FLAT, VALLEY, FLAT, VALLEY
}; };
//biome type //biome type
@ -29,33 +33,33 @@ typedef struct{
} t_biome; } t_biome;
// variables // variables
int sp_x[NB_TEAMS], sp_y[NB_TEAMS];
t_biome* l_biomes; t_biome* l_biomes;
int size_biomes; int size_biomes;
int nb_plains, nb_forests, nb_mountains; int nb_plains, nb_forests, nb_mountains;
int cpt_biome = 0; int cpt_biome;
int width, height; int width, height, n;
// functions // functions
int distance_manhattan(int x1,int y1, int x2, int y2); int distance_manhattan(int x1,int y1, int x2, int y2);
int absolute(int val); void set_spawns(Map &map);
void set_spawns(t_pixel** map, t_team* teams);
void create_biome(int x, int y, int type); void create_biome(int x, int y, int type);
void create_biome_random(int type); void create_biome_random(Map &map, int type);
int check_nears_biomes(int x, int y); int check_nears_biomes(int x, int y);
int check_nears_spawn(int x, int y); int check_nears_spawn(Map &map, int x, int y);
int in_radius(int x,int y,t_biome e); int in_radius(int x,int y,t_biome e);
int generate(int x, int y); int generate(int x, int y);
void init_generator(); void init_generator();
void create_map(int w, int h){ extern "C" void generate(Map *mapPtr)
{
Map &map = *mapPtr;
width = map.getWidth();
height = map.getHeight();
n = map.getNbTeams();
int i,j; int i,j;
//biome variable //biome variable
width = w;
height = h;
init_generator(); init_generator();
//Epicenters generation //Epicenters generation
@ -64,70 +68,66 @@ void create_map(int w, int h){
nb_forests = (rand()%MAX_EPICENTER_BY_TYPE)+3; nb_forests = (rand()%MAX_EPICENTER_BY_TYPE)+3;
nb_mountains = (rand()%MAX_EPICENTER_BY_TYPE)+3; nb_mountains = (rand()%MAX_EPICENTER_BY_TYPE)+3;
size_biomes = nb_plains+ nb_forests + nb_mountains + NB_TEAMS; size_biomes = nb_plains+ nb_forests + nb_mountains + n;
l_biomes = malloc(sizeof(t_biome)*size_biomes);
l_biomes = (t_biome*)malloc(sizeof(t_biome)*size_biomes);
cpt_biome = 0;
// Spawn generations // Spawn generations
set_spawns(map,teams); set_spawns(map);
for(i=0;i<nb_plains;i++) for(i=0;i<nb_plains;i++)
create_biome_random(PLAINS); create_biome_random(map, PLAINS);
for(i=0;i<nb_forests;i++) for(i=0;i<nb_forests;i++)
create_biome_random(FOREST); create_biome_random(map, FOREST);
for(i=0;i<nb_mountains;i++) for(i=0;i<nb_mountains;i++)
create_biome_random(MOUNTAINS); create_biome_random(map, MOUNTAINS);
// */ // */
//génération de la carte //génération de la carte
for (i=0;i<width;i++){ for (i=0;i<width;i++){
for(j=0;j<height;j++){ for(j=0;j<height;j++){
if (i == 0 || j == 0 || i == w-1 || j == h-1){ if (i == 0 || j == 0 || i == width-1 || j == height-1)
map[i][j].type = BEDROCK; map[i][j].type = Pixel::BEDROCK;
}else if((i == sp_x[PURPLE] && j == sp_y[PURPLE])){ else
map[i][j].type = SPAWN;
map[i][j].data=malloc(sizeof(int));
*((int*)(map[i][j].data)) = PURPLE;
}else if(i == sp_x[ORANGE] && j == sp_y[ORANGE]){
map[i][j].type = SPAWN;
map[i][j].data=malloc(sizeof(int));
*((int*)(map[i][j].data)) = ORANGE;
}else{
map[i][j].type = generate(i,j); map[i][j].type = generate(i,j);
}
} }
} }
for(i=0; i<n; ++i)
{
map[map.team(i)].type = Pixel::SPAWN;
map[map.team(i)].data.nbRes = 0;
}
free(l_biomes);
} }
void init_generator(){ void init_generator(){
} }
void set_spawns(t_pixel** map, t_team* teams){ void set_spawns(Map &map){
int i; int i;
int tier_w, tier_h; int tier_w, tier_h;
tier_w = width/6; tier_w = width/6;
tier_h = height/3; tier_h = height/3;
sp_x[0] = (rand()%tier_w)+tier_w; for(i=0; i<n; ++i) // TODO : update spawn location initialisation
sp_y[0] = (rand()%tier_h)+tier_h; {
map.team(i) = Coord((rand()%tier_w)+tier_w, (rand()%tier_h)+tier_h);
sp_x[1] = width-sp_x[0]; create_biome(map.team(i).x, map.team(i).y, VILLAGE);
sp_y[1] = height-sp_y[0];
for(i=0;i<2;i++){
teams[i].spawn.x = sp_x[i];
teams[i].spawn.y = sp_y[i];
create_biome(sp_x[i],sp_y[i],VILLAGE);
} }
// sp_x[1] = width-sp_x[0];
// sp_y[1] = height-sp_y[0];
} }
void create_biome(int x, int y, int type){ void create_biome(int x, int y, int type){
t_biome *biome = malloc(sizeof(t_biome)); t_biome *biome = l_biomes + cpt_biome++;
biome->x=x; biome->x=x;
biome->y=y; biome->y=y;
@ -146,16 +146,14 @@ void create_biome(int x, int y, int type){
biome->radius = (rand()%(OFFSET_RADIUS))+MIN_RADIUS; biome->radius = (rand()%(OFFSET_RADIUS))+MIN_RADIUS;
break; break;
} }
l_biomes[cpt_biome++]=*biome;
} }
void create_biome_random(int type){ void create_biome_random(Map &map, int type){
int x,y; int x,y;
do { do {
x=rand()%width; x=rand()%width;
y=rand()%height; y=rand()%height;
} while ((check_nears_biomes(x,y) != 0) || (check_nears_spawn(x,y) != 0)); //prevent biome superposition } while ((check_nears_biomes(x,y) != 0) || (check_nears_spawn(map, x,y) != 0)); //prevent biome superposition
create_biome(x,y,type); create_biome(x,y,type);
} }
@ -168,10 +166,11 @@ int check_nears_biomes(int x, int y){
return c; return c;
} }
int check_nears_spawn(int x, int y){ int check_nears_spawn(Map &map, int x, int y){
int i,c = 0; int i,c = 0;
for(i=0;i<2;i++) for(i=0;i<n;i++)
if (distance_manhattan(x,y,sp_x[i],sp_y[i]) < 75) c++; if (map.team(i).dist(x,y) < 75)
++c;
return c; return c;
} }
@ -217,18 +216,14 @@ int generate(int x, int y){
}else{ }else{
val = rand()%100; val = rand()%100;
if (val <95){ if (val <95){
return GRASS; return Pixel::GRASS;
}else{ }else{
return TREE; return Pixel::TREE;
} }
} }
return GRASS; return Pixel::GRASS;
} }
int distance_manhattan(int x1,int y1, int x2, int y2){ int distance_manhattan(int x1,int y1, int x2, int y2){
return absolute(x1-x2) + absolute(y1-y2); return Coord::dist(Coord(x1, y1), Coord(x2, y2));
}
int absolute(int val){
return val > 0 ? val : -val;
} }

BIN
generators/biome.dll Normal file

Binary file not shown.

Binary file not shown.