debugged generator

This commit is contained in:
Lendemor 2015-01-12 20:01:09 +01:00
parent 3052c46e7b
commit a82e9bef82
2 changed files with 48 additions and 24 deletions

View File

@ -1,6 +1,6 @@
CC=gcc CC=gcc
LIB=-lSDL -lm -L./lib LIB=-lSDL -lm -L./lib
FLAGS=-Wall -I./include FLAGS=-Wall
ifdef SystemRoot ifdef SystemRoot
RM=del /Q RM=del /Q

View File

@ -12,7 +12,8 @@ typedef struct{
// variables // variables
t_epicenter* l_epicenters; t_epicenter* l_epicenters;
int size_epi; int size_epicenters;
int cpt_epicenter = 0;
int width, height; int width, height;
// functions // functions
@ -25,41 +26,63 @@ void create_epicenter(int type);
int generate(int x, int y); int generate(int x, int y);
void create_map(t_pixel** map, t_team* teams, int w, int h){ void create_map(t_pixel** map, t_team* teams, int w, int h){
int i,j; int i,j,k,l;
int type;
//epicenter variable
int nb_rock, nb_tree, nb_berries;
//spawn variable
int x_rand, y_rand;
width = w; width = w;
height = h; height = h;
//génération des épicentres //Epicenters generation
l_epicenters = malloc(); // TODO : wtf malloc dans le vide
// random choice for numbers of epicenters
nb_rock = rand()%5;
nb_tree = rand()%5;
nb_berries = rand()%5;
size_epicenters = nb_rock + nb_tree + nb_berries + NB_TEAMS;
l_epicenters = malloc(sizeof(t_epicenter)*size_epicenters);
//plains generation for each player => after spawn
for(i=0;i<NB_TEAMS;i++) for(i=0;i<NB_TEAMS;i++)
create_epicenter(GRASS); create_epicenter(GRASS);
for(i=2; i<5;i++){ for(i=0;i<nb_rock;i++)
int nb_epi = rand()%5; create_epicenter(ROCK);
for(i=0;i<nb_epi;i++)
create_epicenter(i); for(i=0;i<nb_tree;i++)
} create_epicenter(TREE);
for(i=0;i<nb_berries;i++)
create_epicenter(BERRIES);
//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){ if (i == 0 || j == 0){
map[i][j]=BEDROCK; map[i][j].type = BEDROCK;
else{ }else{
map[i][j] = generate(i,j); type=generate(i,j);
map[i][j].type = type;
} }
} }
} }
for(int k=0;k<NB_TEAMS;k++){ //génération spawns
for(k=0;k<NB_TEAMS;k++){
x_rand= rand()%width; x_rand= rand()%width;
y_rand= rand()%height; y_rand= rand()%height;
int error = 1; int error = 1;
while(error != 0){ while(error != 0){
error = 0; error = 0;
for (int l=0;l<k;l++){ for (l=0;l<k;l++){
t_coord sp = teams[l].spawn; t_coord sp = teams[l].spawn;
if (distance_manhattan(x_rand,y_rand,sp.x,sp.y) < 50) if (distance_manhattan(x_rand,y_rand,sp.x,sp.y) < 50)
error = 1; error = 1;
@ -79,22 +102,24 @@ void create_epicenter(int type){
epicenter.power = rand()%MAX_POWER; epicenter.power = rand()%MAX_POWER;
epicenter.radius = rand()%(width*height/4); epicenter.radius = rand()%(width*height/4);
l_epicenters[nb_epicenters++]=epicenter; l_epicenters[cpt_epicenter++]=epicenter;
} }
int generate(int x, int y){ int generate(int x, int y){
int i, type; int i, ratio, dist_to_epi, sum, val, type;
int proba[5]; int proba[5];
for(i=0;i<size_epi;i++){ t_epicenter epi;
for(i=0;i<size_epicenters;i++){
epi = l_epicenters[i]; epi = l_epicenters[i];
dist_to_epi = distance_manhattan(x,y,epi.x, epi.y); dist_to_epi = distance_manhattan(x,y,epi.x, epi.y);
if (dist_to_epi < epi.radius){ if (dist_to_epi < epi.radius){
ratio = (int) ((epi.radius - dist_to_epi) * 100) / radius; ratio = (int) ((epi.radius - dist_to_epi) * 100) / epi.radius;
proba[epi.type-1]=epi.power * ratio; proba[epi.type-1]=epi.power * ratio;
} }
} }
int sum=0; sum=0;
for (i=0;i<5;i++){ for (i=0;i<5;i++){
sum += proba[i]; sum += proba[i];
} }
@ -102,12 +127,11 @@ int generate(int x, int y){
int seuil = 0; int seuil = 0;
for (i=0;i<5;i++){ for (i=0;i<5;i++){
start += proba[i] seuil += proba[i];
if(type < seuil){ if(val < seuil)
return i+1; return i+1;
}
} }
return 1; return GRASS;
} }
int distance_manhattan(int x1,int y1, int x2, int y2){ int distance_manhattan(int x1,int y1, int x2, int y2){