init.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. --[[
  2. Nether mod for minetest
  3. Copyright (C) 2013 PilzAdam
  4. Permission to use, copy, modify, and/or distribute this software for
  5. any purpose with or without fee is hereby granted, provided that the
  6. above copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
  10. BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
  11. OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  12. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  13. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14. SOFTWARE.
  15. ]]--
  16. -- Set DEBUG_FLAGS to determine the behavior of nether.debug():
  17. -- 0 = off
  18. -- 1 = print(...)
  19. -- 2 = minetest.chat_send_all(...)
  20. -- 4 = minetest.log("info", ...)
  21. local DEBUG_FLAGS = 0
  22. local S
  23. if minetest.get_translator ~= nil then
  24. S = minetest.get_translator("nether")
  25. else
  26. -- mock the translator function for MT 0.4
  27. S = function(str, ...)
  28. local args={...}
  29. return str:gsub(
  30. "@%d+",
  31. function(match) return args[tonumber(match:sub(2))] end
  32. )
  33. end
  34. end
  35. -- Global Nether namespace
  36. nether = {}
  37. nether.mapgen = {} -- Shared Nether mapgen namespace, for mapgen files to expose functions and constants
  38. nether.modname = minetest.get_current_modname()
  39. nether.path = minetest.get_modpath(nether.modname)
  40. nether.get_translator = S
  41. -- nether.useBiomes allows other mods to know whether they can register ores etc. in the Nether.
  42. -- See mapgen.lua for an explanation of why minetest.read_schematic is being checked
  43. nether.useBiomes = minetest.get_mapgen_setting("mg_name") ~= "v6" and minetest.read_schematic ~= nil
  44. nether.fogColor = { -- only used if climate_api is installed
  45. netherCaverns = "#1D0504", -- Distance-fog colour for classic nether
  46. mantle = "#070916", -- Distance-fog colour for the Mantle region
  47. geodes = "#300530" -- Distance-fog colour for secondary region
  48. }
  49. -- Settings
  50. nether.DEPTH_CEILING = -22000 -- The y location of the Nether's celing
  51. nether.DEPTH_FLOOR = -32000 -- The y location of the Nether's floor
  52. nether.FASTTRAVEL_FACTOR = 8 -- 10 could be better value for Minetest, since there's no sprint, but ex-Minecraft players will be mathing for 8
  53. nether.PORTAL_BOOK_LOOT_WEIGHTING = 0.9 -- Likelyhood of finding the Book of Portals (guide) in dungeon chests. Set to 0 to disable.
  54. nether.NETHER_REALM_ENABLED = true -- Setting to false disables the Nether and Nether portal
  55. -- Override default settings with values from the .conf file, if any are present.
  56. nether.FASTTRAVEL_FACTOR = tonumber(minetest.settings:get("nether_fasttravel_factor") or nether.FASTTRAVEL_FACTOR)
  57. nether.PORTAL_BOOK_LOOT_WEIGHTING = tonumber(minetest.settings:get("nether_portalBook_loot_weighting") or nether.PORTAL_BOOK_LOOT_WEIGHTING)
  58. nether.NETHER_REALM_ENABLED = minetest.settings:get_bool("nether_realm_enabled", nether.NETHER_REALM_ENABLED)
  59. nether.DEPTH_CEILING = tonumber(minetest.settings:get("nether_depth_ymax") or nether.DEPTH_CEILING)
  60. nether.DEPTH_FLOOR = tonumber(minetest.settings:get("nether_depth_ymin") or nether.DEPTH_FLOOR)
  61. if nether.DEPTH_FLOOR + 1000 > nether.DEPTH_CEILING then
  62. error("The lower limit of the Nether must be set at least 1000 lower than the upper limit, and more than 3000 is recommended. Set settingtypes.txt, or 'All Settings' -> 'Mods' -> 'nether' -> 'Nether depth'", 0)
  63. end
  64. nether.DEPTH = nether.DEPTH_CEILING -- Deprecated, use nether.DEPTH_CEILING instead.
  65. -- DEPTH_FLOOR_LAYERS gives the bottom Y of all locations that wish to be
  66. -- considered part of the Nether.
  67. -- DEPTH_FLOOR_LAYERS Allows mods to insert extra layers below the
  68. -- Nether, by knowing where their layer ceiling should start, and letting
  69. -- the layers be included in effects which only happen in the Nether.
  70. -- If a mod wishes to add a layer below the Nether it should read
  71. -- nether.DEPTH_FLOOR_LAYERS to find the bottom Y of the Nether and any
  72. -- other layers already under the Nether. The mod should leave a small gap
  73. -- between DEPTH_FLOOR_LAYERS and its ceiling (e.g. use DEPTH_FLOOR_LAYERS - 6
  74. -- for its ceiling Y, so there is room to shift edge-case biomes), then set
  75. -- nether.DEPTH_FLOOR_LAYERS to reflect the mod's floor Y value, and call
  76. -- shift_existing_biomes() with DEPTH_FLOOR_LAYERS as the floor_y argument.
  77. nether.DEPTH_FLOOR_LAYERS = nether.DEPTH_FLOOR
  78. -- A debug-print function that understands vectors etc. and does not
  79. -- evaluate when debugging is turned off.
  80. -- Works like string.format(), treating the message as a format string.
  81. -- nils, tables, and vectors passed as arguments to nether.debug() are
  82. -- converted to strings and can be included inside the message with %s
  83. function nether.debug(message, ...)
  84. local args = {...}
  85. local argCount = select("#", ...)
  86. for i = 1, argCount do
  87. local arg = args[i]
  88. if arg == nil then
  89. -- convert nils to strings
  90. args[i] = '<nil>'
  91. elseif type(arg) == "table" then
  92. local tableCount = 0
  93. for _,_ in pairs(arg) do tableCount = tableCount + 1 end
  94. if tableCount == 3 and arg.x ~= nil and arg.y ~= nil and arg.z ~= nil then
  95. -- convert vectors to strings
  96. args[i] = minetest.pos_to_string(arg)
  97. else
  98. -- convert tables to strings
  99. -- (calling function can use dump() if a multi-line listing is desired)
  100. args[i] = string.gsub(dump(arg, ""), "\n", " ")
  101. end
  102. end
  103. end
  104. local composed_message = "nether: " .. string.format(message, unpack(args))
  105. if math.floor(DEBUG_FLAGS / 1) % 2 == 1 then print(composed_message) end
  106. if math.floor(DEBUG_FLAGS / 2) % 2 == 1 then minetest.chat_send_all(composed_message) end
  107. if math.floor(DEBUG_FLAGS / 4) % 2 == 1 then minetest.log("info", composed_message) end
  108. end
  109. if DEBUG_FLAGS == 0 then
  110. -- do as little evaluation as possible
  111. nether.debug = function() end
  112. end
  113. -- Load files
  114. dofile(nether.path .. "/portal_api.lua")
  115. dofile(nether.path .. "/nodes.lua")
  116. if nether.NETHER_REALM_ENABLED then
  117. if nether.useBiomes then
  118. dofile(nether.path .. "/mapgen.lua")
  119. else
  120. dofile(nether.path .. "/mapgen_nobiomes.lua")
  121. end
  122. end
  123. dofile(nether.path .. "/portal_examples.lua")
  124. dofile(nether.path..'/custom.lua')
  125. -- Portals are ignited by right-clicking with a mese crystal fragment
  126. nether.register_portal_ignition_item(
  127. "default:mese_crystal_fragment",
  128. {name = "nether_portal_ignition_failure", gain = 0.3}
  129. )
  130. if nether.NETHER_REALM_ENABLED then
  131. -- Use the Portal API to add a portal type which goes to the Nether
  132. -- See portal_api.txt for documentation
  133. nether.register_portal("nether_portal", {
  134. shape = nether.PortalShape_Traditional,
  135. frame_node_name = "default:obsidian",
  136. wormhole_node_color = 0, -- 0 is magenta
  137. title = S("Nether Portal"),
  138. book_of_portals_pagetext = S([[Construction requires 14 blocks of obsidian, which we found deep underground where water had solidified molten rock. The finished frame is four blocks wide, five blocks high, and stands vertically, like a doorway.
  139. This opens to a truly hellish place, though for small mercies the air there is still breathable. There is an intriguing dimensional mismatch happening between this realm and ours, as after opening the second portal into it we observed that 10 strides taken in the Nether appear to be an equivalent of @1 in the natural world.
  140. The expedition parties have found no diamonds or gold, and after an experienced search party failed to return from the trail of a missing expedition party, I must conclude this is a dangerous place.]], 10 * nether.FASTTRAVEL_FACTOR),
  141. is_within_realm = function(pos) -- return true if pos is inside the Nether
  142. return pos.y < nether.DEPTH_CEILING and pos.y > nether.DEPTH_FLOOR
  143. end,
  144. find_realm_anchorPos = function(surface_anchorPos, player_name)
  145. -- divide x and z by a factor of 8 to implement Nether fast-travel
  146. local destination_pos = vector.divide(surface_anchorPos, nether.FASTTRAVEL_FACTOR)
  147. destination_pos.x = math.floor(0.5 + destination_pos.x) -- round to int
  148. destination_pos.z = math.floor(0.5 + destination_pos.z) -- round to int
  149. destination_pos.y = nether.DEPTH_CEILING - 1 -- temp value so find_nearest_working_portal() returns nether portals
  150. -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are in the Nether)
  151. local existing_portal_location, existing_portal_orientation =
  152. nether.find_nearest_working_portal("nether_portal", destination_pos, 8, 0)
  153. if existing_portal_location ~= nil then
  154. return existing_portal_location, existing_portal_orientation
  155. else
  156. local start_y = nether.DEPTH_CEILING - math.random(500, 1500) -- Search starting altitude
  157. destination_pos.y = nether.find_nether_ground_y(destination_pos.x, destination_pos.z, start_y, player_name)
  158. return destination_pos
  159. end
  160. end,
  161. find_surface_anchorPos = function(realm_anchorPos, player_name)
  162. -- A portal definition doesn't normally need to provide a find_surface_anchorPos() function,
  163. -- since find_surface_target_y() will be used by default, but Nether portals also scale position
  164. -- to create fast-travel.
  165. -- Defining a custom function also means we can look for existing nearby portals.
  166. -- Multiply x and z by a factor of 8 to implement Nether fast-travel
  167. local destination_pos = vector.multiply(realm_anchorPos, nether.FASTTRAVEL_FACTOR)
  168. destination_pos.x = math.min(30900, math.max(-30900, destination_pos.x)) -- clip to world boundary
  169. destination_pos.z = math.min(30900, math.max(-30900, destination_pos.z)) -- clip to world boundary
  170. destination_pos.y = nether.DEPTH_CEILING + 1 -- temp value so find_nearest_working_portal() doesn't return nether portals
  171. -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the Nether)
  172. local existing_portal_location, existing_portal_orientation =
  173. nether.find_nearest_working_portal("nether_portal", destination_pos, 8 * nether.FASTTRAVEL_FACTOR, 0)
  174. if existing_portal_location ~= nil then
  175. return existing_portal_location, existing_portal_orientation
  176. else
  177. destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, "nether_portal", player_name)
  178. return destination_pos
  179. end
  180. end,
  181. on_ignite = function(portalDef, anchorPos, orientation)
  182. -- make some sparks fly
  183. local p1, p2 = portalDef.shape:get_p1_and_p2_from_anchorPos(anchorPos, orientation)
  184. local pos = vector.divide(vector.add(p1, p2), 2)
  185. local textureName = portalDef.particle_texture
  186. if type(textureName) == "table" then textureName = textureName.name end
  187. minetest.add_particlespawner({
  188. amount = 110,
  189. time = 0.1,
  190. minpos = {x = pos.x - 0.5, y = pos.y - 1.2, z = pos.z - 0.5},
  191. maxpos = {x = pos.x + 0.5, y = pos.y + 1.2, z = pos.z + 0.5},
  192. minvel = {x = -5, y = -1, z = -5},
  193. maxvel = {x = 5, y = 1, z = 5},
  194. minacc = {x = 0, y = 0, z = 0},
  195. maxacc = {x = 0, y = 0, z = 0},
  196. minexptime = 0.1,
  197. maxexptime = 0.5,
  198. minsize = 0.2 * portalDef.particle_texture_scale,
  199. maxsize = 0.8 * portalDef.particle_texture_scale,
  200. collisiondetection = false,
  201. texture = textureName .. "^[colorize:#F4F:alpha",
  202. animation = portalDef.particle_texture_animation,
  203. glow = 8
  204. })
  205. end
  206. })
  207. -- Set appropriate nether distance-fog if climate_api is available
  208. --
  209. -- Delegating to a mod like climate_api means nether won't unexpectedly stomp on the sky of
  210. -- any other mod.
  211. -- Skylayer is another mod which can perform this role, and skylayer support could be added
  212. -- here as well. However skylayer doesn't provide a position-based method of specifying sky
  213. -- colours out-of-the-box, so the nether mod will have to monitor when players enter and
  214. -- leave the nether.
  215. if minetest.get_modpath("climate_api") and minetest.global_exists("climate_api") and climate_api.register_weather ~= nil then
  216. climate_api.register_influence(
  217. "nether_biome",
  218. function(pos)
  219. local result = "surface"
  220. if pos.y <= nether.DEPTH_CEILING and pos.y >= nether.DEPTH_FLOOR then
  221. result = "nether"
  222. -- since mapgen_nobiomes.lua has no regions it doesn't implement get_region(),
  223. -- so only use get_region() if it exists
  224. if nether.mapgen.get_region ~= nil then
  225. -- the biomes-based mapgen supports 2 extra regions
  226. local regions = nether.mapgen.RegionEnum
  227. local region = nether.mapgen.get_region(pos)
  228. if region == regions.CENTER or region == regions.CENTERSHELL then
  229. result = "mantle"
  230. elseif region == regions.NEGATIVE or region == regions.NEGATIVESHELL then
  231. result = "geode"
  232. end
  233. end
  234. end
  235. return result
  236. end
  237. )
  238. -- using sky type "plain" unfortunately means we don't get smooth fading transitions when
  239. -- the color of the sky changes, but it seems to be the only way to obtain a sky colour
  240. -- which doesn't brighten during the daytime.
  241. local undergroundSky = {
  242. sky_data = {
  243. base_color = nil,
  244. type = "plain",
  245. textures = nil,
  246. clouds = false,
  247. },
  248. sun_data = {
  249. visible = false,
  250. sunrise_visible = false
  251. },
  252. moon_data = {
  253. visible = false
  254. },
  255. star_data = {
  256. visible = false
  257. }
  258. }
  259. local netherSky, mantleSky, geodeSky = table.copy(undergroundSky), table.copy(undergroundSky), table.copy(undergroundSky)
  260. netherSky.sky_data.base_color = nether.fogColor.netherCaverns
  261. mantleSky.sky_data.base_color = nether.fogColor.mantle
  262. geodeSky.sky_data.base_color = nether.fogColor.geodes
  263. climate_api.register_weather(
  264. "nether:caverns",
  265. { nether_biome = "nether" },
  266. { ["climate_api:skybox"] = netherSky }
  267. )
  268. climate_api.register_weather(
  269. "nether:mantle",
  270. { nether_biome = "mantle" },
  271. { ["climate_api:skybox"] = mantleSky }
  272. )
  273. climate_api.register_weather(
  274. "nether:geode",
  275. { nether_biome = "geode" },
  276. { ["climate_api:skybox"] = geodeSky }
  277. )
  278. end
  279. end -- end of "if nether.NETHER_REALM_ENABLED..."
  280. -- Play bubbling lava sounds if player killed by lava
  281. minetest.register_on_dieplayer(
  282. function(player, reason)
  283. if reason.node ~= nil and minetest.get_node_group(reason.node, "lava") > 0 or reason.node == "nether:lava_crust" then
  284. minetest.sound_play(
  285. "nether_lava_bubble",
  286. -- this sample was encoded at 3x speed to reduce .ogg file size
  287. -- at the expense of higher frequencies, so pitch it down ~3x
  288. {to_player = player:get_player_name(), pitch = 0.3, gain = 0.8}
  289. )
  290. end
  291. end
  292. )