一、本期成果
完成地形设计
保存地图设计
显示地图设计
二、实现
随机生成算法
地图肯定是随机生成的,但问题是完全随机就变成散点了,所以我们要增强它的规律性和连续性,就需要一种算法。
我选择了柏林算法,具体就移步[Nature of Code] 柏林噪声。
要把噪音叠起来:
int ans=0, numer=25;
for (int k = 0; k < stage; k++) {
ans += floor(perlin(float(i) / numer, float(j) / numer, seed,numer) * numer);
numer *= 2;
}
return ans;
但写完后就会发现边角全粘在一起了:
这可不是地图,我们要对边角进行一个修正:
// P点的插值
float d1 = y - p0y;
float t1 = 6.0 * pow(d1, 5.0) - 15.0 * pow(d1, 4.0) + 10.0 * pow(d1, 3.0);
if (sqrt(pow(abs(x * loud - map_size / 2), 2.0) + pow(abs(y * loud - map_size / 2), 2.0)) > map_size / 2) {
return (0 - sqrt(pow(abs(x * loud - map_size / 2), 2.0) + pow(abs(y * loud - map_size / 2), 2.0)) + map_size / 2) / 300+n1 * (1.0 - t1) + n0 * t1;
}
float n2 = n1 * (1.0 - t1) + n0 * t1;
return n2;
保存地图设计
用个fstream.h就可以
srand(seed);
ofstream savefile;
string s1 = "./res/save" + to_string(seed) +"large=" + to_string(map_size) + "topography" + ".txt";
const char* s2 = s1.c_str();
savefile.open(s2);
for (int i = 0; i < map_size; i++) {
for (int j = 0; j < map_size; j++) {
if (rand()%10000==0 && perlins(4, i, j)>32)river(i, j);
savefile << perlins(4,i,j) << ' ';
}
savefile << endl;
}
savefile.close();
显示地图设计
用了easyx库
ifstream loadfile;
loadfile.open(name.c_str(), ios::in);
int highnum;
for (int i = 0; i < map_size; i++) {
for (int j = 0; j < map_size; j++) {
loadfile >> highnum;
if (highnum >= 16 * 7) { putpixel(i, j, RGB(186, 113, 70)); goto drawmap_flag; }
if (highnum >= 16 * 6) { putpixel(i, j, RGB(237, 133, 78)); goto drawmap_flag; }
if (highnum >= 16 * 5) { putpixel(i, j, RGB(241, 170, 116)); goto drawmap_flag; }
if (highnum >= 16 * 4) { putpixel(i, j, RGB(227, 216, 196)); goto drawmap_flag; }
if (highnum >= 16 * 3) { putpixel(i, j, RGB(228, 240, 236)); goto drawmap_flag; }
if (highnum >= 16 * 2) { putpixel(i, j, RGB(168, 221, 195)); goto drawmap_flag; }
if (highnum >= 16 * 1) { putpixel(i, j, RGB(106, 190, 156)); goto drawmap_flag; }
if (highnum >= 16 * -2) { putpixel(i, j, RGB(50, 130, 246)); goto drawmap_flag; }
if (highnum >= 16 * -4) { putpixel(i, j, RGB(0, 35, 245)); goto drawmap_flag; }
if (highnum >= 16 * -6) { putpixel(i, j, RGB(0, 18, 154)); goto drawmap_flag; }
putpixel(i, j, RGB(0, 12, 123));
drawmap_flag:;
}
}
loadfile.close();
三、跳转连接
第一篇,当然没有啦。