PixelWars/generator.c

151 lines
2.7 KiB
C

#include "main.h"
#define MAX_POWER 10
typedef struct{
int x;
int y;
int type;
int power;
int radius;
} t_epicenter;
// variables
t_epicenter* l_epicenters;
int size_epicenters;
int cpt_epicenter = 0;
int width, height;
// functions
int distance_manhattan(int x1,int y1, int x2, int y2);
int absolute(int val);
void create_epicenter(int type);
int generate(int x, int y);
void create_map(t_pixel** map, t_team* teams, int w, int h){
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;
height = h;
//Epicenters generation
// 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++)
create_epicenter(GRASS);
for(i=0;i<nb_rock;i++)
create_epicenter(ROCK);
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
for (i=0;i<width;i++){
for(j=0;j<height;j++){
if (i == 0 || j == 0){
map[i][j].type = BEDROCK;
}else{
type=generate(i,j);
map[i][j].type = type;
}
}
}
//génération spawns
for(k=0;k<NB_TEAMS;k++){
x_rand= rand()%width;
y_rand= rand()%height;
int error = 1;
while(error != 0){
error = 0;
for (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;
}
}
map[x_rand][y_rand].type=SPAWN;
teams[k].spawn.x = x_rand;
teams[k].spawn.y = y_rand;
}
}
void 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[cpt_epicenter++]=epicenter;
}
int generate(int x, int y){
int i, ratio, dist_to_epi, sum, val;
int proba[5];
t_epicenter epi;
for(i=0;i<size_epicenters;i++){
epi = l_epicenters[i];
dist_to_epi = distance_manhattan(x,y,epi.x, epi.y);
if (dist_to_epi < epi.radius){
ratio = (int) (dist_to_epi * 100) / epi.radius;
proba[epi.type-1]=epi.power * ratio;
}else{
proba[epi.type-1]=0;
}
}
sum=0;
for (i=0;i<5;i++){
sum += proba[i];
}
val = rand()%sum;
int seuil = 0;
for (i=0;i<5;i++){
seuil += proba[i];
if(val < seuil)
return i+2;
}
return GRASS;
}
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;
}