init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. -- Pyramid width (must be an odd number)
  2. local PYRA_W = 23
  3. -- Pyramid width minus 1
  4. local PYRA_Wm = PYRA_W - 1
  5. -- Half of (Pyramid width minus 1)
  6. local PYRA_Wh = PYRA_Wm / 2
  7. -- Minimum spawn height
  8. local PYRA_MIN_Y = 3
  9. pyramids = {}
  10. dofile(minetest.get_modpath("pyramids").."/room.lua")
  11. local mg_name = minetest.get_mapgen_setting("mg_name")
  12. local function can_replace(pos)
  13. local n = minetest.get_node_or_nil(pos)
  14. if n and n.name and minetest.registered_nodes[n.name] and not minetest.registered_nodes[n.name].walkable then
  15. return true
  16. elseif not n then
  17. return true
  18. else
  19. return false
  20. end
  21. end
  22. local function make_foundation_part(pos, set_to_stone)
  23. local p2 = pos
  24. local cnt = 0
  25. p2.y = p2.y-1
  26. while can_replace(p2)==true do
  27. cnt = cnt+1
  28. if cnt > 25 then
  29. break
  30. end
  31. table.insert(set_to_stone, table.copy(p2))
  32. p2.y = p2.y-1
  33. end
  34. end
  35. local function make_entrance(pos, rot, brick, sand, flood_sand)
  36. local roffset_arr = {
  37. { x=0, y=0, z=1 }, -- front
  38. { x=-1, y=0, z=0 }, -- left
  39. { x=0, y=0, z=-1 }, -- back
  40. { x=1, y=0, z=0 }, -- right
  41. }
  42. local roffset = roffset_arr[rot + 1]
  43. local way
  44. if rot == 0 then
  45. way = vector.add(pos, {x=PYRA_Wh, y=0, z=0})
  46. elseif rot == 1 then
  47. way = vector.add(pos, {x=PYRA_Wm, y=0, z=PYRA_Wh})
  48. elseif rot == 2 then
  49. way = vector.add(pos, {x=PYRA_Wh, y=0, z=PYRA_Wm})
  50. else
  51. way = vector.add(pos, {x=0, y=0, z=PYRA_Wh})
  52. end
  53. local max_sand_height = math.random(1,3)
  54. for ie=0,6,1 do
  55. local sand_height = math.random(1,max_sand_height)
  56. for iy=2,3,1 do
  57. -- dig hallway
  58. local way_dir = vector.add(vector.add(way, {x=0,y=iy,z=0}), vector.multiply(roffset, ie))
  59. if flood_sand and sand ~= "ignore" and iy <= sand_height and ie >= 3 then
  60. minetest.set_node(way_dir, {name=sand})
  61. else
  62. minetest.remove_node(way_dir)
  63. end
  64. -- build decoration above entrance
  65. if ie == 3 and iy == 3 then
  66. local deco = {x=way_dir.x, y=way_dir.y+1,z=way_dir.z}
  67. minetest.set_node(deco, {name=brick})
  68. if rot == 0 or rot == 2 then
  69. minetest.set_node(vector.add(deco, {x=-1, y=0, z=0}), {name=brick})
  70. minetest.set_node(vector.add(deco, {x=1, y=0, z=0}), {name=brick})
  71. else
  72. minetest.set_node(vector.add(deco, {x=0, y=0, z=-1}), {name=brick})
  73. minetest.set_node(vector.add(deco, {x=0, y=0, z=1}), {name=brick})
  74. end
  75. end
  76. end
  77. end
  78. end
  79. local function make_pyramid(pos, brick, sandstone, stone, sand)
  80. local set_to_brick = {}
  81. local set_to_stone = {}
  82. -- Build pyramid
  83. for iy=0,math.random(10,PYRA_Wh),1 do
  84. for ix=iy,PYRA_W-1-iy,1 do
  85. for iz=iy,PYRA_W-1-iy,1 do
  86. if iy < 1 then
  87. make_foundation_part({x=pos.x+ix,y=pos.y,z=pos.z+iz}, set_to_stone)
  88. end
  89. table.insert(set_to_brick, {x=pos.x+ix,y=pos.y+iy,z=pos.z+iz})
  90. end
  91. end
  92. end
  93. minetest.bulk_set_node(set_to_stone , {name=stone})
  94. minetest.bulk_set_node(set_to_brick, {name=brick})
  95. end
  96. local function make(pos, brick, sandstone, stone, sand, ptype, room_id)
  97. local bpos = table.copy(pos)
  98. -- Build pyramid
  99. make_pyramid(bpos, brick, sandstone, stone, sand)
  100. local rot = math.random(0, 3)
  101. -- Build room
  102. local ok, msg, flood_sand = pyramids.make_room(bpos, ptype, room_id, rot)
  103. -- Build entrance
  104. make_entrance(bpos, rot, brick, sand, flood_sand)
  105. -- Done
  106. minetest.log("action", "[pyramids] Created pyramid at "..minetest.pos_to_string(bpos)..".")
  107. return ok, msg
  108. end
  109. local perl1 = {SEED1 = 9130, OCTA1 = 3, PERS1 = 0.5, SCAL1 = 250} -- Values should match minetest mapgen V6 desert noise.
  110. local perlin1 -- perlin noise buffer
  111. local function hlp_fnct(pos, name)
  112. local n = minetest.get_node_or_nil(pos)
  113. if n and n.name and n.name == name then
  114. return true
  115. else
  116. return false
  117. end
  118. end
  119. local function ground(pos, old)
  120. local p2 = table.copy(pos)
  121. while hlp_fnct(p2, "air") do
  122. p2.y = p2.y -1
  123. end
  124. if p2.y < old.y then
  125. return {x=old.x, y=p2.y, z=old.z}
  126. else
  127. return old
  128. end
  129. end
  130. -- Select the recommended type of pyramid to use, based on the environment.
  131. -- One of sandstone, desert sandstone, desert stone.
  132. local select_pyramid_type = function(minp, maxp)
  133. local mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
  134. local sand
  135. local sands = {"default:sand", "default:desert_sand", "default:desert_stone"}
  136. local p2
  137. local psand = {}
  138. local sand
  139. local cnt = 0
  140. local sand_cnt_max = 0
  141. local sand_cnt_max_id
  142. -- Look for sand or desert stone to place the pyramid on
  143. for s=1, #sands do
  144. cnt = 0
  145. local sand_cnt = 0
  146. sand = sands[s]
  147. psand[s] = minetest.find_node_near(mpos, 25, sand)
  148. while cnt < 5 do
  149. cnt = cnt+1
  150. mpos = {x=math.random(minp.x,maxp.x), y=math.random(minp.y,maxp.y), z=math.random(minp.z,maxp.z)}
  151. local spos = minetest.find_node_near(mpos, 25, sand)
  152. if spos ~= nil then
  153. sand_cnt = sand_cnt + 1
  154. if psand[s] == nil then
  155. psand[s] = spos
  156. end
  157. end
  158. if sand_cnt > sand_cnt_max then
  159. sand_cnt_max = sand_cnt
  160. sand_cnt_max_id = s
  161. p2 = psand[s]
  162. end
  163. end
  164. end
  165. -- Select the material type by the most prominent node type
  166. -- E.g. if desert sand is most prominent, we place a desert sandstone pyramid
  167. if sand_cnt_max_id then
  168. sand = sands[sand_cnt_max_id]
  169. else
  170. sand = nil
  171. p2 = nil
  172. end
  173. return sand, p2
  174. end
  175. -- Attempt to generate a pyramid in the generated area.
  176. -- Up to one pyramid per mapchunk.
  177. minetest.register_on_generated(function(minp, maxp, seed)
  178. if maxp.y < PYRA_MIN_Y then return end
  179. -- TODO: Use Minetests pseudo-random tools
  180. math.randomseed(seed)
  181. if not perlin1 then
  182. perlin1 = minetest.get_perlin(perl1.SEED1, perl1.OCTA1, perl1.PERS1, perl1.SCAL1)
  183. end
  184. --[[ Make sure the pyramid doesn't bleed outside of maxp,
  185. so it doesn't get placed incompletely by the mapgen.
  186. This creates a bias somewhat, as this means there are some coordinates in
  187. which pyramids cannot spawn. But it's still better to have broken pyramids.
  188. ]]
  189. local limit = function(pos, maxp)
  190. pos.x = math.min(pos.x, maxp.x - PYRA_W+1)
  191. pos.y = math.min(pos.y, maxp.y - PYRA_Wh)
  192. pos.z = math.min(pos.z, maxp.z - PYRA_W+1)
  193. return pos
  194. end
  195. local noise1 = perlin1:get_2d({x=minp.x,y=minp.y})
  196. if noise1 > 0.25 or noise1 < -0.26 then
  197. -- Need a bit of luck to place a pyramid
  198. if math.random(0,10) > 7 then
  199. minetest.log("verbose", "[pyramids] Pyramid not placed, bad dice roll. minp="..minetest.pos_to_string(minp))
  200. return
  201. end
  202. local sand, p2
  203. sand, p2 = select_pyramid_type(minp, maxp)
  204. if p2 == nil then
  205. minetest.log("verbose", "[pyramids] Pyramid not placed, no suitable surface. minp="..minetest.pos_to_string(minp))
  206. return
  207. end
  208. -- Select the material type by the most prominent node type
  209. -- E.g. if desert sand is most prominent, we place a desert sandstone pyramid
  210. if sand_cnt_max_id then
  211. sand = sands[sand_cnt_max_id]
  212. end
  213. if p2.y < PYRA_MIN_Y then
  214. minetest.log("info", "[pyramids] Pyramid not placed, too deep. p2="..minetest.pos_to_string(p2))
  215. return
  216. end
  217. -- Now sink the pyramid until each corner of it is no longer floating in mid-air
  218. p2 = limit(p2, maxp)
  219. local oposses = {
  220. {x=p2.x,y=p2.y-1,z=p2.z},
  221. {x=p2.x+PYRA_Wm,y=p2.y-1,z=p2.z+PYRA_Wm},
  222. {x=p2.x+PYRA_Wm,y=p2.y-1,z=p2.z},
  223. {x=p2.x,y=p2.y-1,z=p2.z+PYRA_Wm},
  224. }
  225. for o=1, #oposses do
  226. local opos = oposses[o]
  227. local n = minetest.get_node_or_nil(opos)
  228. if n and n.name and n.name == "air" then
  229. local old = table.copy(p2)
  230. p2 = ground(opos, p2)
  231. end
  232. end
  233. -- Random bonus sinking
  234. p2.y = math.max(p2.y - math.random(0,3), PYRA_MIN_Y)
  235. -- Bad luck, we have hit the chunk border!
  236. if p2.y < minp.y then
  237. minetest.log("info", "[pyramids] Pyramid not placed, sunken too much. p2="..minetest.pos_to_string(p2))
  238. return
  239. end
  240. -- Make sure the pyramid is not near a "killer" node, like water
  241. local middle = vector.add(p2, {x=PYRA_Wh, y=0, z=PYRA_Wh})
  242. if minetest.find_node_near(p2, 5, {"default:water_source"}) ~= nil or
  243. minetest.find_node_near(vector.add(p2, {x=PYRA_W, y=0, z=0}), 5, {"default:water_source"}) ~= nil or
  244. minetest.find_node_near(vector.add(p2, {x=0, y=0, z=PYRA_W}), 5, {"default:water_source"}) ~= nil or
  245. minetest.find_node_near(vector.add(p2, {x=PYRA_W, y=0, z=PYRA_W}), 5, {"default:water_source"}) ~= nil or
  246. minetest.find_node_near(middle, PYRA_W, {"default:dirt_with_grass"}) ~= nil or
  247. minetest.find_node_near(middle, 52, {"default:sandstonebrick", "default:desert_sandstone_brick", "default:desert_stonebrick"}) ~= nil or
  248. minetest.find_node_near(middle, PYRA_Wh + 3, {"default:cactus", "group:leaves", "group:tree"}) ~= nil then
  249. minetest.log("info", "[pyramids] Pyramid not placed, inappropriate node nearby. p2="..minetest.pos_to_string(p2))
  250. return
  251. end
  252. -- Bonus chance to spawn a sandstone pyramid in v6 desert because otherwise they would be too rare in v6
  253. if (mg_name == "v6" and sand == "default:desert_sand" and math.random(1, 2) == 1) then
  254. sand = "default:sand"
  255. end
  256. -- Desert stone pyramids only generate in areas with almost no sand
  257. if sand == "default:desert_stone" then
  258. local nodes = minetest.find_nodes_in_area(vector.add(p2, {x=-1, y=-2, z=-1}), vector.add(p2, {x=PYRA_W+1, y=PYRA_Wh, z=PYRA_W+1}), {"group:sand"})
  259. if #nodes > 5 then
  260. sand = "default:desert_sand"
  261. end
  262. end
  263. -- Generate the pyramid!
  264. if sand == "default:desert_sand" then
  265. -- Desert sandstone pyramid
  266. make(p2, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert_sandstone")
  267. elseif sand == "default:sand" then
  268. -- Sandstone pyramid
  269. make(p2, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone")
  270. else
  271. -- Desert stone pyramid
  272. make(p2, "default:desert_stonebrick", "default:desert_stone_block", "default:desert_stone", "ignore", "desert_stone")
  273. end
  274. end
  275. end)
  276. minetest.register_chatcommand("spawnpyramid", {
  277. description = "Generate a pyramid",
  278. params = "[<room_type>]",
  279. privs = { server = true },
  280. func = function(name, param)
  281. local player = minetest.get_player_by_name(name)
  282. if not player then
  283. return false, "No player."
  284. end
  285. local pos = player:get_pos()
  286. pos = vector.round(pos)
  287. local s = math.random(1,3)
  288. local r = tonumber(param)
  289. local room_id
  290. if r then
  291. room_id = r
  292. end
  293. local ok, msg
  294. pos = vector.add(pos, {x=-PYRA_Wh, y=-1, z=0})
  295. if s == 1 then
  296. -- Sandstone
  297. ok, msg = make(pos, "default:sandstonebrick", "default:sandstone", "default:sandstone", "default:sand", "sandstone", room_id)
  298. elseif s == 2 then
  299. -- Desert sandstone
  300. ok, msg = make(pos, "default:desert_sandstone_brick", "default:desert_sandstone", "default:desert_stone", "default:desert_sand", "desert_sandstone", room_id)
  301. else
  302. -- Desert stone
  303. ok, msg = make(pos, "default:desert_stonebrick", "default:desert_stone_block", "default:desert_stone", "ignore", "desert_stone", room_id)
  304. end
  305. if ok then
  306. return true, "Pyramid generated at @1.", minetest.pos_to_string(pos)
  307. else
  308. return false, msg
  309. end
  310. end,
  311. }
  312. )