您的位置:首页 > 编程语言 > Lua

地图随机生成lua版本

2017-10-25 22:02 901 查看
-- random 2d map
--[[
从这里获取的 http://www.roguebasin.com/index.php?title=Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels 
--]]
--参照的是c的部分

local TILE_FLOOR = 0;
local TILE_WALL = 1;

local grid = {};
local grid2 = {};

local fillprob = 40;
local r1_cutoff = 5;
local r2_cutoff = 2;
local size_x = 64
local size_y = 20;
local generations;

local params = nil;
local params_set = {};

local function randpick()
if ( math.random() * 100 < fillprob) then
return TILE_WALL;
else
return TILE_FLOOR;
end
end

local function initmap()
-- 初始化
for yi=1, size_y do
if (grid[yi] == nil) then
grid[yi] = {};
end
for xi=1,size_x do
grid[yi][xi] = randpick();
end
end

for yi=1, size_y do
if (grid2[yi] == nil) then
grid2[yi] = {};
end
for xi=1, size_x do
grid2[yi][xi] = TILE_WALL;
end
end

for yi=1,size_y do
grid[yi][1] = TILE_WALL;
grid[yi][size_x] = TILE_WALL;
end
for xi=1,size_x do
grid[1][xi] = TILE_WALL;
grid[size_y][xi] = TILE_WALL;
end

end

local function generation()
-- 生成
for yi = 2,size_y-1 do
for xi = 2, size_x-1 do
local adjcount_r1 = 0;
local adjcount_r2 = 0;

for ii=-1,1 do
for jj=-1,1 do
if(grid[yi+ii][xi+jj] ~= TILE_FLOOR) then
adjcount_r1 = adjcount_r1 + 1;
end
end
end
for ii=yi-2,yi+2 do
for jj=xi-2,xi+2 do
if(not (math.abs(ii-yi)==2 and math.abs(jj-xi)==2))
and (not (ii<1 or jj<1 or ii>=size_y or jj>=size_x)) then
if(grid[ii][jj] ~= TILE_FLOOR) then
adjcount_r2 = adjcount_r2 + 1;
end
end
end
end
if(adjcount_r1 >= params.r1_cutoff or adjcount_r2 <= params.r2_cutoff) then
if (grid2[yi][xi] == TILE_FLOOR) then
grid2[yi][xi] = TILE_WALL;
end
else
grid2[yi][xi] = TILE_FLOOR;
end
end
end
for yi=2,size_y do
for xi=2,size_x do
--if (grid[yi][xi] == TILE_FLOOR and grid2[yi][xi] ~= TILE_FLOOR) then
grid[yi][xi] = grid2[yi][xi];
--end
end
end
return grid;
end

local function printfunc()
print("W[0](p) = rand[0,100) < "..fillprob.."\n");

for ii = 1,generations do
print("Repeat "..params_set[ii].reps..": W'(p) = R[1](p) >= "..params_set[ii].r1_cutoff)

if (params_set[ii].r2_cutoff >= 0) then
print(" || R[2](p) <= "..params_set[ii].r2_cutoff);
else
print('\n');
end
end
end

function printmap()
local rs = {"A","B","C","E","F","G","H","I","J","K","L","N","M","O","P","Q","R","S","T"};
local n = #rs;
for yi = 1,size_y do
local str = "";
for xi = 1,size_x do
local tr = grid[yi][xi];
if (tr > n) then
tr = n;
end
if (tr ~= TILE_FLOOR) then
str = str .. rs;
--if (grid[yi][xi] == TILE_WALL) then
--str = str .. "#";
else
str = str ..".";
end
end
print(str);
end
end

-- 從一個點出發,找到所有可以連接的點,比較兩邊是否,還有沒有在列表裡面的,如果是,選擇這個點找尋所有可以到達的點
-- 1. 找出所有孤立的區域,之後再他們之間進行連接

function tmain()
-- 60 20 40 5 2 40
-- 60 40 40 5 2 40
local argv = {60,20,40,5,2,10};
local argc = table.maxn(argv) + 1;
size_x = argv[1];
size_y = argv[2];
fillprob = argv[3];

generations = (argc - 4) / 3 ;

params = {};
params_set = {};
for i = 1,generations do
params_set[i] = {
r1_cutoff = 0;
r2_cutoff = 0;
reps = 0;
};
end

local idx = 1;
params = params_set[idx];
ii = 4;
while(true) do
if (ii + 2 >= argc) then
break;
end

params.r1_cutoff = (argv[ii]);
params.r2_cutoff = (argv[ii + 1]);
params.reps = (argv[ii + 2]);

idx = idx + 1;
params = params_set[idx];
ii = ii + 3;
end

--srand(time(NULL));
math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )

initmap();

for ii = 1, generations do
params = params_set[ii];
for jj = 1,params.reps do
generation();
TILE_WALL = TILE_WALL + 1;
end
end
printfunc();
printmap();
return 0;
end

tmain();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua 地图 random
相关文章推荐