PixelWars/old_code/generator_anselme.c
2016-05-16 00:34:44 +02:00

64 lines
1.3 KiB
C

#include "main.h"
// functions
int distance_manhattan(int x1,int y1, int x2, int y2);
int absolute(int val);
int custom_min(int a, int b){
return a < b ? a : b;
}
int custom_max(int a, int b){
return a > b ? a : b;
}
void create_map(int w, int h){
int i,j, k;
int r = (w/NB_TEAMS < h ? w/NB_TEAMS : h)/2;
for(i=0; i<NB_TEAMS; i++){
teams[i].spawn.x = (w/(NB_TEAMS*2))*(1+i*2);
teams[i].spawn.y = h/2;
}
//génération de la carte
for (i=0;i<w;i++){
for(j=0;j<h;j++){
map[i][j].data = NULL;
if (i == 0 || j == 0 || i == w-1 || j == h-1){
map[i][j].type = BEDROCK;
}else{
int d = custom_max(w, h);
for(k=0; k<NB_TEAMS; k++){
d = custom_min(d, distance_manhattan(teams[k].spawn.x, teams[k].spawn.y, i, j));
if(!d) break;
}
if(d == 0){
map[i][j].type = SPAWN;
map[i][j].data = malloc(sizeof(int));
*((int*)(map[i][j].data)) = k;
}else{
int l = (d-20)+(rand()%40);
if(l > r+15){
map[i][j].type = rand()%8 ? ROCK : IRON_ORE;
}else if(l < r-15){
map[i][j].type = rand()%15 ? GRASS : BERRIES;
}else{
l = rand()%10;
map[i][j].type = l > 5 ? TREE : l ? GRASS : BERRIES;
}
}
}
}
}
}
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;
}