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.

180 lines
8.3KB

  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. local S
  17. if minetest.get_translator ~= nil then
  18. S = minetest.get_translator("nether")
  19. else
  20. -- mock the translator function for MT 0.4
  21. S = function(str, ...)
  22. local args={...}
  23. return str:gsub(
  24. "@%d+",
  25. function(match) return args[tonumber(match:sub(2))] end
  26. )
  27. end
  28. end
  29. -- Global Nether namespace
  30. nether = {}
  31. nether.modname = minetest.get_current_modname()
  32. nether.path = minetest.get_modpath(nether.modname)
  33. nether.get_translator = S
  34. -- nether.useBiomes allows other mods to know whether they can register ores etc. in the Nether.
  35. -- See mapgen.lua for an explanation of why minetest.read_schematic is being checked
  36. nether.useBiomes = minetest.get_mapgen_setting("mg_name") ~= "v6" and minetest.read_schematic ~= nil
  37. -- Settings
  38. nether.DEPTH_CEILING = -25000 -- The y location of the Nether's celing
  39. nether.DEPTH_FLOOR = -31000 -- The y location of the Nether's floor
  40. 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
  41. nether.PORTAL_BOOK_LOOT_WEIGHTING = 0.9 -- Likelyhood of finding the Book of Portals (guide) in dungeon chests. Set to 0 to disable.
  42. nether.NETHER_REALM_ENABLED = true -- Setting to false disables the Nether and Nether portal
  43. -- Override default settings with values from the .conf file, if any are present.
  44. nether.FASTTRAVEL_FACTOR = tonumber(minetest.settings:get("nether_fasttravel_factor") or nether.FASTTRAVEL_FACTOR)
  45. nether.PORTAL_BOOK_LOOT_WEIGHTING = tonumber(minetest.settings:get("nether_portalBook_loot_weighting") or nether.PORTAL_BOOK_LOOT_WEIGHTING)
  46. nether.NETHER_REALM_ENABLED = minetest.settings:get_bool("nether_realm_enabled", nether.NETHER_REALM_ENABLED)
  47. nether.DEPTH_CEILING = tonumber(minetest.settings:get("nether_depth_ymax") or nether.DEPTH_CEILING)
  48. nether.DEPTH_FLOOR = tonumber(minetest.settings:get("nether_depth_ymin") or nether.DEPTH_FLOOR)
  49. if nether.DEPTH_FLOOR + 1000 > nether.DEPTH_CEILING then
  50. 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)
  51. end
  52. nether.DEPTH = nether.DEPTH_CEILING -- Deprecated, use nether.DEPTH_CEILING instead.
  53. -- Load files
  54. dofile(nether.path .. "/portal_api.lua")
  55. dofile(nether.path .. "/nodes.lua")
  56. if nether.NETHER_REALM_ENABLED then
  57. if nether.useBiomes then
  58. dofile(nether.path .. "/mapgen.lua")
  59. else
  60. dofile(nether.path .. "/mapgen_nobiomes.lua")
  61. end
  62. end
  63. dofile(nether.path .. "/portal_examples.lua")
  64. -- Portals are ignited by right-clicking with a mese crystal fragment
  65. nether.register_portal_ignition_item(
  66. "default:mese_crystal_fragment",
  67. {name = "nether_portal_ignition_failure", gain = 0.3}
  68. )
  69. if nether.NETHER_REALM_ENABLED then
  70. -- Use the Portal API to add a portal type which goes to the Nether
  71. -- See portal_api.txt for documentation
  72. nether.register_portal("nether_portal", {
  73. shape = nether.PortalShape_Traditional,
  74. frame_node_name = "default:obsidian",
  75. wormhole_node_color = 0, -- 0 is magenta
  76. title = S("Nether Portal"),
  77. 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.
  78. 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.
  79. 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),
  80. is_within_realm = function(pos) -- return true if pos is inside the Nether
  81. return pos.y < nether.DEPTH_CEILING
  82. end,
  83. find_realm_anchorPos = function(surface_anchorPos)
  84. -- divide x and z by a factor of 8 to implement Nether fast-travel
  85. local destination_pos = vector.divide(surface_anchorPos, nether.FASTTRAVEL_FACTOR)
  86. destination_pos.x = math.floor(0.5 + destination_pos.x) -- round to int
  87. destination_pos.z = math.floor(0.5 + destination_pos.z) -- round to int
  88. destination_pos.y = nether.DEPTH_CEILING - 1000 -- temp value so find_nearest_working_portal() returns nether portals
  89. -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are in the Nether)
  90. local existing_portal_location, existing_portal_orientation =
  91. nether.find_nearest_working_portal("nether_portal", destination_pos, 8, 0)
  92. if existing_portal_location ~= nil then
  93. return existing_portal_location, existing_portal_orientation
  94. else
  95. local start_y = nether.DEPTH_CEILING - math.random(500, 1500) -- Search starting altitude
  96. destination_pos.y = nether.find_nether_ground_y(destination_pos.x, destination_pos.z, start_y)
  97. return destination_pos
  98. end
  99. end,
  100. find_surface_anchorPos = function(realm_anchorPos)
  101. -- A portal definition doesn't normally need to provide a find_surface_anchorPos() function,
  102. -- since find_surface_target_y() will be used by default, but Nether portals also scale position
  103. -- to create fast-travel.
  104. -- Defining a custom function also means we can look for existing nearby portals.
  105. -- Multiply x and z by a factor of 8 to implement Nether fast-travel
  106. local destination_pos = vector.multiply(realm_anchorPos, nether.FASTTRAVEL_FACTOR)
  107. destination_pos.x = math.min(30900, math.max(-30900, destination_pos.x)) -- clip to world boundary
  108. destination_pos.z = math.min(30900, math.max(-30900, destination_pos.z)) -- clip to world boundary
  109. destination_pos.y = 0 -- temp value so find_nearest_working_portal() doesn't return nether portals
  110. -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the Nether)
  111. local existing_portal_location, existing_portal_orientation =
  112. nether.find_nearest_working_portal("nether_portal", destination_pos, 8 * nether.FASTTRAVEL_FACTOR, 0)
  113. if existing_portal_location ~= nil then
  114. return existing_portal_location, existing_portal_orientation
  115. else
  116. destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, "nether_portal")
  117. return destination_pos
  118. end
  119. end,
  120. on_ignite = function(portalDef, anchorPos, orientation)
  121. -- make some sparks fly
  122. local p1, p2 = portalDef.shape:get_p1_and_p2_from_anchorPos(anchorPos, orientation)
  123. local pos = vector.divide(vector.add(p1, p2), 2)
  124. local textureName = portalDef.particle_texture
  125. if type(textureName) == "table" then textureName = textureName.name end
  126. minetest.add_particlespawner({
  127. amount = 110,
  128. time = 0.1,
  129. minpos = {x = pos.x - 0.5, y = pos.y - 1.2, z = pos.z - 0.5},
  130. maxpos = {x = pos.x + 0.5, y = pos.y + 1.2, z = pos.z + 0.5},
  131. minvel = {x = -5, y = -1, z = -5},
  132. maxvel = {x = 5, y = 1, z = 5},
  133. minacc = {x = 0, y = 0, z = 0},
  134. maxacc = {x = 0, y = 0, z = 0},
  135. minexptime = 0.1,
  136. maxexptime = 0.5,
  137. minsize = 0.2 * portalDef.particle_texture_scale,
  138. maxsize = 0.8 * portalDef.particle_texture_scale,
  139. collisiondetection = false,
  140. texture = textureName .. "^[colorize:#F4F:alpha",
  141. animation = portalDef.particle_texture_animation,
  142. glow = 8
  143. })
  144. end
  145. })
  146. end