PixelWars/generators/anselme.cpp

70 lines
1.5 KiB
C++

#include <map.h>
#include <cmath>
#include <cstdlib>
// g++ -shared anselme.cpp -o anselme.dll -I../src
// functions
int distance_manhattan(int x1,int y1, int x2, int y2)
{
return abs(x1-x2) + abs(y1-y2);
}
inline int custom_min(int a, int b)
{
return a < b ? a : b;
}
inline int custom_max(int a, int b)
{
return a > b ? a : b;
}
extern "C" void generate(Map *mapPtr)
{
Map &map = *mapPtr;
int w = map.getWidth();
int h = map.getHeight();
int n = map.getNbTeams();
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++){
teamCoord[i*2] = (w/(n*2))*(1+i*2);
teamCoord[i*2 +1] = 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 = Pixel::BEDROCK;
}else{
int d = custom_max(w, h);
for(k=0; k<n; k++){
d = custom_min(d, distance_manhattan(teamCoord[k*2], teamCoord[k*2 +1], i, j));
if(!d)
break;
}
if(d == 0){
map[i][j].type = Pixel::SPAWN;
map[i][j].data.nbRes = k;
}else{
int l = (d-20)+(rand()%40);
if(l > r+15) // mountain
map[i][j].type = rand()%8 ? Pixel::ROCK : Pixel::IRON_ORE;
else if(l < r-15) // plains
map[i][j].type = rand()%15 ? Pixel::GRASS : Pixel::BERRIES;
else // forest
{
l = rand()%10;
map[i][j].type = l > 5 ? Pixel::TREE : l ? Pixel::GRASS : Pixel::BERRIES;
}
}
}
}
}
}