地图随机生成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在给定范围内,生成指定个数不重复随机数组
- 随机生成一个对称矩阵,并输出。(究极版本,可以每一次都改变矩阵的随机数且不同,采用do while退出条件循环)
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(7)习题2.8 随机数组的三种生成算法
- 001【2D Rougelike】随机地图的生成
- 利用深度优先搜索做的随机生成地图的迷宫小游戏
- 星际SC地图制作中生成随机位置,也包括所有需要随机的效果
- 用Unity实现《随机生成 Tile Based 地图之——洞穴》中的算法
- Linux系统下以RPM方式安装mysql-5.7.9【5.7版本以后,启动mysqld服务首次登录需要密码,为root生成的随机密码在错误日志文件/var/log/mysqld.log】
- Unity在地图范围内任意位置随机生成3D模型
- SpriteKit游戏Delve随机生成地牢地图一个Bug的修复
- java版本的身份证验证,随机生成身份证号码
- SpriteKit游戏Delve随机生成地牢地图一个Bug的修复
- Unity2D - 6. 生成随机地图 (2) - 脚本控制Tilemap (1) 自动切换不同的sprite
- 关于“搭桥”游戏生成随机地图的设计思路
- SpriteKit游戏Delve随机生成地牢地图一个Bug的修复
- LunarLander 随机生成地面地图!
- Unity2D - 6. 生成随机游戏地图 (1)
- VC++的随机性问题(连连看随机地图的生成)
- Unity中利用柏林噪音生成随机地图