架空地図ジェネレータ自体はvectorにあるのですが、架空地図の地図データをゲームに扱えたら素敵だと思ったんです。
まぁ、vectorにあるものは白黒のみにすることができるので、画像解析を使えばできないことはないと思いますがね。
とりあえずの処理の流れは
1、大陸の中心となる点を決定する
2、中心点から四方に点をランダムで伸ばす
という手順のみ。池や川はとりあえず保留です。
というわけで実験的に作ってみました。
ソースは以下のような感じです。マジックナンバーだらけなのは、まぁ、ね?(何
#include "DxLib.h"
#include
#include
#include
#define SCR_X (640)
#define SCR_Y (480)
#define LAND (1)
#define SEA (0)
#define LIMIT (50)
int map[SCR_Y][SCR_X] = {0};
void makeMap(int x, int y, int num){
if(map[y][x] == SEA) return;
if(x 639) return;
if(y 479) return;
if(num >= LIMIT) return;
srand((unsigned int)time(NULL));
if(map[y-1][x] == SEA){
if(rand()%100+1 >= 50){
map[y-1][x] = LAND;
makeMap(x, y-1, num+1);
}
}
if(map[y][x+1] == SEA){
if(rand()%100+1 >= 50){
map[y][x+1] = LAND;
makeMap(x+1, y, num+1);
}
}
if(map[y+1][x] == SEA){
if(rand()%100+1 >= 50){
map[y+1][x] = LAND;
makeMap(x, y+1, num+1);
}
}
if(map[y][x-1] == SEA){
if(rand()%100+1 >= 50){
map[y][x-1] = LAND;
makeMap(x-1, y, num+1);
}
}
return;
}
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR lp, int ns){
ChangeWindowMode(TRUE);
if(DxLib_Init() == -1) return -1;
map[240][320] = LAND;
makeMap(320, 240, 0);
for(int y=0; y<SCR_Y; y++){
for(int x=0; x<SCR_X; x++){
if(map[y][x] != SEA){
DrawPixel(x, y, 0xffff);
}
}
}
WaitKey();
DxLib_End();
return 0;
}
ランダム周りも微妙に変な感じですし、もうちょっと改良していく必要がありますねえ。