PixelWars/map_generation.c
2015-01-12 16:01:58 +01:00

110 lines
1.9 KiB
C

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;
}