Nether mod for Minetest
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
10.0KB

  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.modname = minetest.get_current_modname()
  38. nether.path = minetest.get_modpath(nether.modname)
  39. nether.get_translator = S
  40. -- nether.useBiomes allows other mods to know whether they can register ores etc. in the Nether.
  41. -- See mapgen.lua for an explanation of why minetest.read_schematic is being checked
  42. nether.useBiomes = minetest.get_mapgen_setting("mg_name") ~= "v6" and minetest.read_schematic ~= nil
  43. -- Settings
  44. nether.DEPTH_CEILING = -5000 -- The y location of the Nether's celing
  45. nether.DEPTH_FLOOR = -11000 -- The y location of the Nether's floor
  46. 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
  47. nether.PORTAL_BOOK_LOOT_WEIGHTING = 0.9 -- Likelyhood of finding the Book of Portals (guide) in dungeon chests. Set to 0 to disable.
  48. nether.NETHER_REALM_ENABLED = true -- Setting to false disables the Nether and Nether portal
  49. -- Override default settings with values from the .conf file, if any are present.
  50. nether.FASTTRAVEL_FACTOR = tonumber(minetest.settings:get("nether_fasttravel_factor") or nether.FASTTRAVEL_FACTOR)
  51. nether.PORTAL_BOOK_LOOT_WEIGHTING = tonumber(minetest.settings:get("nether_portalBook_loot_weighting") or nether.PORTAL_BOOK_LOOT_WEIGHTING)
  52. nether.NETHER_REALM_ENABLED = minetest.settings:get_bool("nether_realm_enabled", nether.NETHER_REALM_ENABLED)
  53. nether.DEPTH_CEILING = tonumber(minetest.settings:get("nether_depth_ymax") or nether.DEPTH_CEILING)
  54. nether.DEPTH_FLOOR = tonumber(minetest.settings:get("nether_depth_ymin") or nether.DEPTH_FLOOR)
  55. if nether.DEPTH_FLOOR + 1000 > nether.DEPTH_CEILING then
  56. 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)
  57. end
  58. nether.DEPTH = nether.DEPTH_CEILING -- Deprecated, use nether.DEPTH_CEILING instead.
  59. -- A debug-print function that understands vectors etc. and does not
  60. -- evaluate when debugging is turned off.
  61. -- Works like string.format(), treating the message as a format string.
  62. -- nils, tables, and vectors passed as arguments to nether.debug() are
  63. -- converted to strings and can be included inside the message with %s
  64. function nether.debug(message, ...)
  65. local args = {...}
  66. local argCount = select("#", ...)
  67. for i = 1, argCount do
  68. local arg = args[i]
  69. if arg == nil then
  70. -- convert nils to strings
  71. args[i] = '<nil>'
  72. elseif type(arg) == "table" then
  73. local tableCount = 0
  74. for _,_ in pairs(arg) do tableCount = tableCount + 1 end
  75. if tableCount == 3 and arg.x ~= nil and arg.y ~= nil and arg.z ~= nil then
  76. -- convert vectors to strings
  77. args[i] = minetest.pos_to_string(arg)
  78. else
  79. -- convert tables to strings
  80. -- (calling function can use dump() if a multi-line listing is desired)
  81. args[i] = string.gsub(dump(arg, ""), "\n", " ")
  82. end
  83. end
  84. end
  85. local composed_message = "nether: " .. string.format(message, unpack(args))
  86. if math.floor(DEBUG_FLAGS / 1) % 2 == 1 then print(composed_message) end
  87. if math.floor(DEBUG_FLAGS / 2) % 2 == 1 then minetest.chat_send_all(composed_message) end
  88. if math.floor(DEBUG_FLAGS / 4) % 2 == 1 then minetest.log("info", composed_message) end
  89. end
  90. if DEBUG_FLAGS == 0 then
  91. -- do as little evaluation as possible
  92. nether.debug = function() end
  93. end
  94. -- Load files
  95. dofile(nether.path .. "/portal_api.lua")
  96. dofile(nether.path .. "/nodes.lua")
  97. if nether.NETHER_REALM_ENABLED then
  98. if nether.useBiomes then
  99. dofile(nether.path .. "/mapgen.lua")
  100. else
  101. dofile(nether.path .. "/mapgen_nobiomes.lua")
  102. end
  103. end
  104. dofile(nether.path .. "/portal_examples.lua")
  105. -- Portals are ignited by right-clicking with a mese crystal fragment
  106. nether.register_portal_ignition_item(
  107. "default:mese_crystal_fragment",
  108. {name = "nether_portal_ignition_failure", gain = 0.3}
  109. )
  110. if nether.NETHER_REALM_ENABLED then
  111. -- Use the Portal API to add a portal type which goes to the Nether
  112. -- See portal_api.txt for documentation
  113. nether.register_portal("nether_portal", {
  114. shape = nether.PortalShape_Traditional,
  115. frame_node_name = "default:obsidian",
  116. wormhole_node_color = 0, -- 0 is magenta
  117. title = S("Nether Portal"),
  118. 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.
  119. 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.
  120. 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),
  121. is_within_realm = function(pos) -- return true if pos is inside the Nether
  122. return pos.y < nether.DEPTH_CEILING
  123. end,
  124. find_realm_anchorPos = function(surface_anchorPos, player_name)
  125. -- divide x and z by a factor of 8 to implement Nether fast-travel
  126. local destination_pos = vector.divide(surface_anchorPos, nether.FASTTRAVEL_FACTOR)
  127. destination_pos.x = math.floor(0.5 + destination_pos.x) -- round to int
  128. destination_pos.z = math.floor(0.5 + destination_pos.z) -- round to int
  129. destination_pos.y = nether.DEPTH_CEILING - 1000 -- temp value so find_nearest_working_portal() returns nether portals
  130. -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are in the Nether)
  131. local existing_portal_location, existing_portal_orientation =
  132. nether.find_nearest_working_portal("nether_portal", destination_pos, 8, 0)
  133. if existing_portal_location ~= nil then
  134. return existing_portal_location, existing_portal_orientation
  135. else
  136. local start_y = nether.DEPTH_CEILING - math.random(500, 1500) -- Search starting altitude
  137. destination_pos.y = nether.find_nether_ground_y(destination_pos.x, destination_pos.z, start_y, player_name)
  138. return destination_pos
  139. end
  140. end,
  141. find_surface_anchorPos = function(realm_anchorPos, player_name)
  142. -- A portal definition doesn't normally need to provide a find_surface_anchorPos() function,
  143. -- since find_surface_target_y() will be used by default, but Nether portals also scale position
  144. -- to create fast-travel.
  145. -- Defining a custom function also means we can look for existing nearby portals.
  146. -- Multiply x and z by a factor of 8 to implement Nether fast-travel
  147. local destination_pos = vector.multiply(realm_anchorPos, nether.FASTTRAVEL_FACTOR)
  148. destination_pos.x = math.min(30900, math.max(-30900, destination_pos.x)) -- clip to world boundary
  149. destination_pos.z = math.min(30900, math.max(-30900, destination_pos.z)) -- clip to world boundary
  150. destination_pos.y = 0 -- temp value so find_nearest_working_portal() doesn't return nether portals
  151. -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the Nether)
  152. local existing_portal_location, existing_portal_orientation =
  153. nether.find_nearest_working_portal("nether_portal", destination_pos, 8 * nether.FASTTRAVEL_FACTOR, 0)
  154. if existing_portal_location ~= nil then
  155. return existing_portal_location, existing_portal_orientation
  156. else
  157. destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, "nether_portal", player_name)
  158. return destination_pos
  159. end
  160. end,
  161. on_ignite = function(portalDef, anchorPos, orientation)
  162. -- make some sparks fly
  163. local p1, p2 = portalDef.shape:get_p1_and_p2_from_anchorPos(anchorPos, orientation)
  164. local pos = vector.divide(vector.add(p1, p2), 2)
  165. local textureName = portalDef.particle_texture
  166. if type(textureName) == "table" then textureName = textureName.name end
  167. minetest.add_particlespawner({
  168. amount = 110,
  169. time = 0.1,
  170. minpos = {x = pos.x - 0.5, y = pos.y - 1.2, z = pos.z - 0.5},
  171. maxpos = {x = pos.x + 0.5, y = pos.y + 1.2, z = pos.z + 0.5},
  172. minvel = {x = -5, y = -1, z = -5},
  173. maxvel = {x = 5, y = 1, z = 5},
  174. minacc = {x = 0, y = 0, z = 0},
  175. maxacc = {x = 0, y = 0, z = 0},
  176. minexptime = 0.1,
  177. maxexptime = 0.5,
  178. minsize = 0.2 * portalDef.particle_texture_scale,
  179. maxsize = 0.8 * portalDef.particle_texture_scale,
  180. collisiondetection = false,
  181. texture = textureName .. "^[colorize:#F4F:alpha",
  182. animation = portalDef.particle_texture_animation,
  183. glow = 8
  184. })
  185. end
  186. })
  187. end