portal_api.lua 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. --[[
  2. Portal API for Minetest
  3. See portal_api.txt for documentation
  4. --
  5. Copyright (C) 2020 Treer
  6. Permission to use, copy, modify, and/or distribute this software for
  7. any purpose with or without fee is hereby granted, provided that the
  8. above copyright notice and this permission notice appear in all copies.
  9. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
  12. BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
  13. OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  14. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  15. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  16. SOFTWARE.
  17. ]]--
  18. -- setting DEBUG_IGNORE_MODSTORAGE true prevents portals from knowing where other
  19. -- portals are, forcing find_realm_anchorpos() etc. to be executed every time.
  20. local DEBUG_IGNORE_MODSTORAGE = false
  21. nether.registered_portals = {}
  22. nether.registered_portals_count = 0
  23. -- Exposes a list of node names that are used as frame nodes by registered portals
  24. nether.is_frame_node = {}
  25. -- gives the colour values in nether_portals_palette.png that are used by the wormhole colorfacedir
  26. -- hardware colouring.
  27. nether.portals_palette = {
  28. [0] = {r = 128, g = 0, b = 128, asString = "#800080"}, -- traditional/magenta
  29. [1] = {r = 0, g = 0, b = 0, asString = "#000000"}, -- black
  30. [2] = {r = 19, g = 19, b = 255, asString = "#1313FF"}, -- blue
  31. [3] = {r = 55, g = 168, b = 0, asString = "#37A800"}, -- green
  32. [4] = {r = 141, g = 237, b = 255, asString = "#8DEDFF"}, -- cyan
  33. [5] = {r = 221, g = 0, b = 0, asString = "#DD0000"}, -- red
  34. [6] = {r = 255, g = 240, b = 0, asString = "#FFF000"}, -- yellow
  35. [7] = {r = 255, g = 255, b = 255, asString = "#FFFFFF"} -- white
  36. }
  37. if minetest.get_mod_storage == nil then
  38. error(nether.modname .. " does not support Minetest versions earlier than 0.4.16", 0)
  39. end
  40. local S = nether.get_translator
  41. nether.portal_destination_not_found_message =
  42. S("Mysterious forces prevented you from opening that portal. Please try another location")
  43. --[[
  44. Positions
  45. =========
  46. p1 & p2 p1 and p2 is the system used by earlier versions of the nether mod, which the portal_api
  47. is forwards and backwards compatible with.
  48. p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together
  49. they define the bounding volume for the portal.
  50. The value of p1 and p2 is kept in the metadata of every node in the portal
  51. WormholePos The location of the node that a portal's target is set to, and a player is teleported
  52. to. It can also be used to test whether a portal is active.
  53. AnchorPos Introduced by the portal_api. Coordinates for portals are normally given in terms of
  54. the AnchorPos. The AnchorPos does not change with portal orientation - portals rotate
  55. around the AnchorPos. Ideally an AnchorPos would be near the bottom center of a portal
  56. shape, but this is not the case with PortalShape_Traditional to keep comptaibility with
  57. earlier versions of the nether mod.
  58. Usually an orientation is required with an AnchorPos.
  59. Orientation is yaw, either 0 or 90, 0 meaning a portal that faces north/south - i.e. obsidian
  60. running east/west.
  61. TimerPos The portal_api replaces ABMs with a single node timer per portal, and the TimerPos is the
  62. node in which that timer is located. Extra metadata is also kept in the TimerPos node.
  63. Portal shapes
  64. =============
  65. For the PortalShape_Traditional implementation, p1, p2, anchorPos, wormholdPos and TimerPos are defined
  66. as follows:
  67. .
  68. +--------+--------+--------+--------+
  69. | | Frame | |
  70. | | | | p2 |
  71. +--------+--------+--------+--------+
  72. | | | |
  73. | | | |
  74. +--------+ + +--------+
  75. | | Wormhole | |
  76. | | | |
  77. +--------+ + +--------+
  78. | |Wormhole | |
  79. | | Pos | |
  80. +--------+--------+--------+--------+
  81. AnchorPos|TimerPos| | |
  82. | p1 | | | |
  83. +--------+--------+--------+--------+
  84. +X/East or +Z/North ----->
  85. A better location for AnchorPos would be directly under WormholePos, as it's more centered
  86. and you don't need to know the portal's orientation to find AnchorPos from the WormholePos
  87. or vice-versa, however AnchorPos is in the bottom/south/west-corner to keep compatibility
  88. with earlier versions of nether mod (which only records portal corners p1 & p2 in the node
  89. metadata).
  90. ]]
  91. local facedir_up, facedir_north, facedir_south, facedir_east, facedir_west, facedir_down = 0, 4, 8, 12, 16, 20
  92. local __ = {name = "air", prob = 0}
  93. local AA = {name = "air", prob = 255, force_place = true}
  94. local ON = {name = "default:obsidian", facedir = facedir_north + 0, prob = 255, force_place = true}
  95. local ON2 = {name = "default:obsidian", facedir = facedir_north + 1, prob = 255, force_place = true}
  96. local ON3 = {name = "default:obsidian", facedir = facedir_north + 2, prob = 255, force_place = true}
  97. local ON4 = {name = "default:obsidian", facedir = facedir_north + 3, prob = 255, force_place = true}
  98. local OS = {name = "default:obsidian", facedir = facedir_south, prob = 255, force_place = true}
  99. local OE = {name = "default:obsidian", facedir = facedir_east, prob = 255, force_place = true}
  100. local OW = {name = "default:obsidian", facedir = facedir_west, prob = 255, force_place = true}
  101. local OU = {name = "default:obsidian", facedir = facedir_up + 0, prob = 255, force_place = true}
  102. local OU2 = {name = "default:obsidian", facedir = facedir_up + 1, prob = 255, force_place = true}
  103. local OU3 = {name = "default:obsidian", facedir = facedir_up + 2, prob = 255, force_place = true}
  104. local OU4 = {name = "default:obsidian", facedir = facedir_up + 3, prob = 255, force_place = true}
  105. local OD = {name = "default:obsidian", facedir = facedir_down, prob = 255, force_place = true}
  106. -- facedirNodeList is a list of node references which should have their facedir value copied into
  107. -- param2 before placing a schematic. The facedir values will only be copied when the portal's frame
  108. -- node has a paramtype2 of "facedir" or "colorfacedir".
  109. -- Having schematics provide this list avoids needing to check every node in the schematic volume.
  110. local facedirNodeList = {ON, ON2, ON3, ON4, OS, OE, OW, OU, OU2, OU3, OU4, OD}
  111. -- This object defines a portal's shape, segregating the shape logic code from portal behaviour code.
  112. -- You can create a new "PortalShape" definition object which implements the same
  113. -- functions if you wish to register a custom shaped portal in register_portal(). Examples of other
  114. -- shapes follow after PortalShape_Traditional.
  115. -- Since it's symmetric, this PortalShape definition has only implemented orientations of 0 and 90
  116. nether.PortalShape_Traditional = {
  117. name = "Traditional",
  118. size = vector.new(4, 5, 1), -- size of the portal, and not necessarily the size of the schematic,
  119. -- which may clear area around the portal.
  120. is_horizontal = false, -- whether the wormhole is a vertical or horizontal surface
  121. diagram_image = {
  122. image = "nether_book_diagram_traditional.png", -- The diagram to be shown in the Book of Portals
  123. width = 142,
  124. height = 305
  125. },
  126. -- returns the coords for minetest.place_schematic() that will place the schematic on the anchorPos
  127. get_schematicPos_from_anchorPos = function(anchorPos, orientation)
  128. assert(orientation, "no orientation passed")
  129. if orientation == 0 then
  130. return {x = anchorPos.x, y = anchorPos.y, z = anchorPos.z - 2}
  131. else
  132. return {x = anchorPos.x - 2, y = anchorPos.y, z = anchorPos.z }
  133. end
  134. end,
  135. get_wormholePos_from_anchorPos = function(anchorPos, orientation)
  136. assert(orientation, "no orientation passed")
  137. if orientation == 0 then
  138. return {x = anchorPos.x + 1, y = anchorPos.y + 1, z = anchorPos.z }
  139. else
  140. return {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 1}
  141. end
  142. end,
  143. get_anchorPos_from_wormholePos = function(wormholePos, orientation)
  144. assert(orientation, "no orientation passed")
  145. if orientation == 0 then
  146. return {x = wormholePos.x - 1, y = wormholePos.y - 1, z = wormholePos.z }
  147. else
  148. return {x = wormholePos.x, y = wormholePos.y - 1, z = wormholePos.z - 1}
  149. end
  150. end,
  151. -- p1 and p2 are used to keep maps compatible with earlier versions of this mod.
  152. -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together
  153. -- they define the bounding volume for the portal.
  154. get_p1_and_p2_from_anchorPos = function(self, anchorPos, orientation)
  155. assert(orientation, "no orientation passed")
  156. assert(self ~= nil and self.name == nether.PortalShape_Traditional.name, "Must pass self as first argument, or use shape:func() instead of shape.func()")
  157. local p1 = anchorPos -- PortalShape_Traditional puts the anchorPos at p1 for backwards&forwards compatibility
  158. local p2
  159. if orientation == 0 then
  160. p2 = {x = p1.x + self.size.x - 1, y = p1.y + self.size.y - 1, z = p1.z }
  161. else
  162. p2 = {x = p1.x, y = p1.y + self.size.y - 1, z = p1.z + self.size.x - 1}
  163. end
  164. return p1, p2
  165. end,
  166. get_anchorPos_and_orientation_from_p1_and_p2 = function(p1, p2)
  167. if p1.z == p2.z then
  168. return p1, 0
  169. elseif p1.x == p2.x then
  170. return p1, 90
  171. else
  172. -- this KISS implementation will break you've made a 3D PortalShape definition
  173. minetest.log("error", "get_anchorPos_and_orientation_from_p1_and_p2 failed on p1=" .. minetest.pos_to_string(p1) .. " p2=" .. minetest.pos_to_string(p2))
  174. end
  175. end,
  176. -- returns true if function was applied to all frame nodes
  177. apply_func_to_frame_nodes = function(anchorPos, orientation, func)
  178. -- a 4x5 portal is small enough that hardcoded positions is simpler that procedural code
  179. local shortCircuited
  180. if orientation == 0 then
  181. -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true
  182. shortCircuited =
  183. func({x = anchorPos.x + 0, y = anchorPos.y, z = anchorPos.z}) or
  184. func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z}) or
  185. func({x = anchorPos.x + 2, y = anchorPos.y, z = anchorPos.z}) or
  186. func({x = anchorPos.x + 3, y = anchorPos.y, z = anchorPos.z}) or
  187. func({x = anchorPos.x + 0, y = anchorPos.y + 4, z = anchorPos.z}) or
  188. func({x = anchorPos.x + 1, y = anchorPos.y + 4, z = anchorPos.z}) or
  189. func({x = anchorPos.x + 2, y = anchorPos.y + 4, z = anchorPos.z}) or
  190. func({x = anchorPos.x + 3, y = anchorPos.y + 4, z = anchorPos.z}) or
  191. func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z}) or
  192. func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z}) or
  193. func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z}) or
  194. func({x = anchorPos.x + 3, y = anchorPos.y + 1, z = anchorPos.z}) or
  195. func({x = anchorPos.x + 3, y = anchorPos.y + 2, z = anchorPos.z}) or
  196. func({x = anchorPos.x + 3, y = anchorPos.y + 3, z = anchorPos.z})
  197. else
  198. shortCircuited =
  199. func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 0}) or
  200. func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 1}) or
  201. func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 2}) or
  202. func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 3}) or
  203. func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 0}) or
  204. func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 1}) or
  205. func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 2}) or
  206. func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 3}) or
  207. func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z }) or
  208. func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z }) or
  209. func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z }) or
  210. func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 3}) or
  211. func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z + 3}) or
  212. func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z + 3})
  213. end
  214. return not shortCircuited
  215. end,
  216. -- returns true if function was applied to all wormhole nodes
  217. apply_func_to_wormhole_nodes = function(anchorPos, orientation, func)
  218. local shortCircuited
  219. if orientation == 0 then
  220. local wormholePos = {x = anchorPos.x + 1, y = anchorPos.y + 1, z = anchorPos.z}
  221. -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true
  222. shortCircuited =
  223. func({x = wormholePos.x + 0, y = wormholePos.y + 0, z = wormholePos.z}) or
  224. func({x = wormholePos.x + 1, y = wormholePos.y + 0, z = wormholePos.z}) or
  225. func({x = wormholePos.x + 0, y = wormholePos.y + 1, z = wormholePos.z}) or
  226. func({x = wormholePos.x + 1, y = wormholePos.y + 1, z = wormholePos.z}) or
  227. func({x = wormholePos.x + 0, y = wormholePos.y + 2, z = wormholePos.z}) or
  228. func({x = wormholePos.x + 1, y = wormholePos.y + 2, z = wormholePos.z})
  229. else
  230. local wormholePos = {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 1}
  231. shortCircuited =
  232. func({x = wormholePos.x, y = wormholePos.y + 0, z = wormholePos.z + 0}) or
  233. func({x = wormholePos.x, y = wormholePos.y + 0, z = wormholePos.z + 1}) or
  234. func({x = wormholePos.x, y = wormholePos.y + 1, z = wormholePos.z + 0}) or
  235. func({x = wormholePos.x, y = wormholePos.y + 1, z = wormholePos.z + 1}) or
  236. func({x = wormholePos.x, y = wormholePos.y + 2, z = wormholePos.z + 0}) or
  237. func({x = wormholePos.x, y = wormholePos.y + 2, z = wormholePos.z + 1})
  238. end
  239. return not shortCircuited
  240. end,
  241. -- Check for whether the portal is blocked in, and if so then provide a safe way
  242. -- on one side for the player to step out of the portal. Suggest including a roof
  243. -- incase the portal was blocked with lava flowing from above.
  244. -- If portal can appear in mid-air then can also check for that and add a platform.
  245. disable_portal_trap = function(anchorPos, orientation)
  246. assert(orientation, "no orientation passed")
  247. -- Not implemented yet. It may not need to be implemented because if you
  248. -- wait in a portal long enough you teleport again. So a trap portal would have to link
  249. -- to one of two blocked-in portals which link to each other - which is possible, but
  250. -- quite extreme.
  251. end,
  252. schematic = {
  253. size = {x = 4, y = 5, z = 5},
  254. data = { -- note that data is upside down
  255. __,__,__,__,
  256. AA,AA,AA,AA,
  257. AA,AA,AA,AA,
  258. AA,AA,AA,AA,
  259. AA,AA,AA,AA,
  260. __,__,__,__,
  261. AA,AA,AA,AA,
  262. AA,AA,AA,AA,
  263. AA,AA,AA,AA,
  264. AA,AA,AA,AA,
  265. ON,OW,OE,ON2,
  266. OU,AA,AA,OU,
  267. OU,AA,AA,OU,
  268. OU,AA,AA,OU,
  269. ON4,OE,OW,ON3,
  270. __,__,__,__,
  271. AA,AA,AA,AA,
  272. AA,AA,AA,AA,
  273. AA,AA,AA,AA,
  274. AA,AA,AA,AA,
  275. __,__,__,__,
  276. AA,AA,AA,AA,
  277. AA,AA,AA,AA,
  278. AA,AA,AA,AA,
  279. AA,AA,AA,AA,
  280. },
  281. facedirNodes = facedirNodeList
  282. }
  283. } -- End of PortalShape_Traditional class
  284. -- Example alternative PortalShape
  285. nether.PortalShape_Circular = {
  286. name = "Circular",
  287. size = vector.new(7, 7, 1), -- size of the portal, and not necessarily the size of the schematic,
  288. -- which may clear area around the portal.
  289. is_horizontal = false, -- whether the wormhole is a vertical or horizontal surface
  290. diagram_image = {
  291. image = "nether_book_diagram_circular.png", -- The diagram to be shown in the Book of Portals
  292. width = 149,
  293. height = 243
  294. },
  295. -- returns the coords for minetest.place_schematic() that will place the schematic on the anchorPos
  296. get_schematicPos_from_anchorPos = function(anchorPos, orientation)
  297. assert(orientation, "no orientation passed")
  298. if orientation == 0 then
  299. return {x = anchorPos.x - 3, y = anchorPos.y, z = anchorPos.z - 3}
  300. else
  301. return {x = anchorPos.x - 3, y = anchorPos.y, z = anchorPos.z - 3 }
  302. end
  303. end,
  304. get_wormholePos_from_anchorPos = function(anchorPos, orientation)
  305. -- wormholePos is the node above anchorPos
  306. return {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z}
  307. end,
  308. get_anchorPos_from_wormholePos = function(wormholePos, orientation)
  309. -- wormholePos is the node above anchorPos
  310. return {x = wormholePos.x, y = wormholePos.y - 1, z = wormholePos.z}
  311. end,
  312. -- p1 and p2 are used to keep maps compatible with earlier versions of this mod.
  313. -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together
  314. -- they define the bounding volume for the portal.
  315. get_p1_and_p2_from_anchorPos = function(self, anchorPos, orientation)
  316. assert(orientation, "no orientation passed")
  317. assert(self ~= nil and self.name == nether.PortalShape_Circular.name, "Must pass self as first argument, or use shape:func() instead of shape.func()")
  318. local p1
  319. local p2
  320. if orientation == 0 then
  321. p1 = {x = anchorPos.x - 3, y = anchorPos.y, z = anchorPos.z }
  322. p2 = {x = p1.x + self.size.x - 1, y = p1.y + self.size.y - 1, z = p1.z }
  323. else
  324. p1 = {x = anchorPos.x, y = anchorPos.y, z = anchorPos.z - 3 }
  325. p2 = {x = p1.x, y = p1.y + self.size.y - 1, z = p1.z + self.size.x - 1}
  326. end
  327. return p1, p2
  328. end,
  329. get_anchorPos_and_orientation_from_p1_and_p2 = function(p1, p2)
  330. if p1.z == p2.z then
  331. return {x= p1.x + 3, y = p1.y, z = p1.z }, 0
  332. elseif p1.x == p2.x then
  333. return {x= p1.x, y = p1.y, z = p1.z + 3}, 90
  334. end
  335. end,
  336. apply_func_to_frame_nodes = function(anchorPos, orientation, func)
  337. local shortCircuited
  338. if orientation == 0 then
  339. -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true
  340. shortCircuited =
  341. func({x = anchorPos.x + 0, y = anchorPos.y + 0, z = anchorPos.z}) or
  342. func({x = anchorPos.x + 1, y = anchorPos.y + 0, z = anchorPos.z}) or func({x = anchorPos.x - 1, y = anchorPos.y + 0, z = anchorPos.z}) or
  343. func({x = anchorPos.x + 2, y = anchorPos.y + 1, z = anchorPos.z}) or func({x = anchorPos.x - 2, y = anchorPos.y + 1, z = anchorPos.z}) or
  344. func({x = anchorPos.x + 3, y = anchorPos.y + 2, z = anchorPos.z}) or func({x = anchorPos.x - 3, y = anchorPos.y + 2, z = anchorPos.z}) or
  345. func({x = anchorPos.x + 3, y = anchorPos.y + 3, z = anchorPos.z}) or func({x = anchorPos.x - 3, y = anchorPos.y + 3, z = anchorPos.z}) or
  346. func({x = anchorPos.x + 3, y = anchorPos.y + 4, z = anchorPos.z}) or func({x = anchorPos.x - 3, y = anchorPos.y + 4, z = anchorPos.z}) or
  347. func({x = anchorPos.x + 2, y = anchorPos.y + 5, z = anchorPos.z}) or func({x = anchorPos.x - 2, y = anchorPos.y + 5, z = anchorPos.z}) or
  348. func({x = anchorPos.x + 1, y = anchorPos.y + 6, z = anchorPos.z}) or func({x = anchorPos.x - 1, y = anchorPos.y + 6, z = anchorPos.z}) or
  349. func({x = anchorPos.x + 0, y = anchorPos.y + 6, z = anchorPos.z})
  350. else
  351. shortCircuited =
  352. func({x = anchorPos.x, y = anchorPos.y + 0, z = anchorPos.z + 0}) or
  353. func({x = anchorPos.x, y = anchorPos.y + 0, z = anchorPos.z + 1}) or func({x = anchorPos.x, y = anchorPos.y + 0, z = anchorPos.z - 1}) or
  354. func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 2}) or func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z - 2}) or
  355. func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z + 3}) or func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z - 3}) or
  356. func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z + 3}) or func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z - 3}) or
  357. func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 3}) or func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z - 3}) or
  358. func({x = anchorPos.x, y = anchorPos.y + 5, z = anchorPos.z + 2}) or func({x = anchorPos.x, y = anchorPos.y + 5, z = anchorPos.z - 2}) or
  359. func({x = anchorPos.x, y = anchorPos.y + 6, z = anchorPos.z + 1}) or func({x = anchorPos.x, y = anchorPos.y + 6, z = anchorPos.z - 1}) or
  360. func({x = anchorPos.x, y = anchorPos.y + 6, z = anchorPos.z + 0})
  361. end
  362. return not shortCircuited
  363. end,
  364. -- returns true if function was applied to all wormhole nodes
  365. apply_func_to_wormhole_nodes = function(anchorPos, orientation, func)
  366. local xRange = 2
  367. local zRange = 0
  368. if orientation ~= 0 then
  369. xRange = 0
  370. zRange = 2
  371. end
  372. local xEdge, yEdge, zEdge
  373. local pos = {}
  374. for x = -xRange, xRange do
  375. pos.x = anchorPos.x + x
  376. xEdge = x == -xRange or x == xRange
  377. for z = -zRange, zRange do
  378. zEdge = z == -zRange or z == zRange
  379. pos.z = anchorPos.z + z
  380. for y = 1, 5 do
  381. yEdge = y == 1 or y == 5
  382. if not (yEdge and xEdge and zEdge) then
  383. pos.y = anchorPos.y + y
  384. if func(pos) then
  385. -- func() caused an abort by returning true
  386. return false
  387. end
  388. end
  389. end
  390. end
  391. end
  392. return true
  393. end,
  394. -- Check for whether the portal is blocked in, and if so then provide a safe way
  395. -- on one side for the player to step out of the portal. Suggest including a roof
  396. -- incase the portal was blocked with lava flowing from above.
  397. -- If portal can appear in mid-air then can also check for that and add a platform.
  398. disable_portal_trap = function(anchorPos, orientation)
  399. assert(orientation, "no orientation passed")
  400. -- Not implemented.
  401. end,
  402. schematic = {
  403. size = {x = 7, y = 7, z = 7},
  404. data = { -- note that data is upside down
  405. __,__,__,__,__,__,__,
  406. __,__,__,__,__,__,__,
  407. __,__,AA,AA,AA,__,__,
  408. __,__,AA,AA,AA,__,__,
  409. __,__,AA,AA,AA,__,__,
  410. __,__,__,__,__,__,__,
  411. __,__,__,__,__,__,__,
  412. __,__,__,__,__,__,__,
  413. __,AA,AA,AA,AA,AA,__,
  414. __,AA,AA,AA,AA,AA,__,
  415. __,AA,AA,AA,AA,AA,__,
  416. __,AA,AA,AA,AA,AA,__,
  417. __,AA,AA,AA,AA,AA,__,
  418. __,__,__,__,__,__,__,
  419. __,__,__,__,__,__,__,
  420. __,AA,AA,AA,AA,AA,__,
  421. AA,AA,AA,AA,AA,AA,AA,
  422. AA,AA,AA,AA,AA,AA,AA,
  423. AA,AA,AA,AA,AA,AA,AA,
  424. __,AA,AA,AA,AA,AA,__,
  425. __,__,AA,AA,AA,__,__,
  426. __,__,OW,OW,OW,__,__,
  427. __,ON,AA,AA,AA,ON2,__,
  428. OU,AA,AA,AA,AA,AA,OD,
  429. OU,AA,AA,AA,AA,AA,OD,
  430. OU,AA,AA,AA,AA,AA,OD,
  431. __,ON4,AA,AA,AA,ON3,__,
  432. __,__,OE,OE,OE,__,__,
  433. __,__,__,__,__,__,__,
  434. __,AA,AA,AA,AA,AA,__,
  435. AA,AA,AA,AA,AA,AA,AA,
  436. AA,AA,AA,AA,AA,AA,AA,
  437. AA,AA,AA,AA,AA,AA,AA,
  438. __,AA,AA,AA,AA,AA,__,
  439. __,__,AA,AA,AA,__,__,
  440. __,__,__,__,__,__,__,
  441. __,AA,AA,AA,AA,AA,__,
  442. __,AA,AA,AA,AA,AA,__,
  443. __,AA,AA,AA,AA,AA,__,
  444. __,AA,AA,AA,AA,AA,__,
  445. __,AA,AA,AA,AA,AA,__,
  446. __,__,__,__,__,__,__,
  447. __,__,__,__,__,__,__,
  448. __,__,__,__,__,__,__,
  449. __,__,AA,AA,AA,__,__,
  450. __,__,AA,AA,AA,__,__,
  451. __,__,AA,AA,AA,__,__,
  452. __,__,__,__,__,__,__,
  453. __,__,__,__,__,__,__,
  454. },
  455. facedirNodes = facedirNodeList
  456. }
  457. } -- End of PortalShape_Circular class
  458. -- Example alternative PortalShape
  459. -- This platform shape is symmetrical around the y-axis, so the orientation value never matters.
  460. nether.PortalShape_Platform = {
  461. name = "Platform",
  462. size = vector.new(5, 2, 5), -- size of the portal, and not necessarily the size of the schematic,
  463. -- which may clear area around the portal.
  464. is_horizontal = true, -- whether the wormhole is a vertical or horizontal surface
  465. diagram_image = {
  466. image = "nether_book_diagram_platform.png", -- The diagram to be shown in the Book of Portals
  467. width = 200,
  468. height = 130
  469. },
  470. -- returns the coords for minetest.place_schematic() that will place the schematic on the anchorPos
  471. get_schematicPos_from_anchorPos = function(anchorPos, orientation)
  472. return {x = anchorPos.x - 2, y = anchorPos.y, z = anchorPos.z - 2}
  473. end,
  474. get_wormholePos_from_anchorPos = function(anchorPos, orientation)
  475. -- wormholePos is the node above anchorPos
  476. return {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z}
  477. end,
  478. get_anchorPos_from_wormholePos = function(wormholePos, orientation)
  479. -- wormholePos is the node above anchorPos
  480. return {x = wormholePos.x, y = wormholePos.y - 1, z = wormholePos.z}
  481. end,
  482. -- p1 and p2 are used to keep maps compatible with earlier versions of this mod.
  483. -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together
  484. -- they define the bounding volume for the portal.
  485. get_p1_and_p2_from_anchorPos = function(self, anchorPos, orientation)
  486. assert(self ~= nil and self.name == nether.PortalShape_Platform.name, "Must pass self as first argument, or use shape:func() instead of shape.func()")
  487. local p1 = {x = anchorPos.x - 2, y = anchorPos.y, z = anchorPos.z - 2}
  488. local p2 = {x = anchorPos.x + 2, y = anchorPos.y + 1, z = anchorPos.z + 2}
  489. return p1, p2
  490. end,
  491. get_anchorPos_and_orientation_from_p1_and_p2 = function(p1, p2)
  492. return {x= p1.x + 2, y = p1.y, z = p1.z + 2}, 0
  493. end,
  494. apply_func_to_frame_nodes = function(anchorPos, orientation, func)
  495. local shortCircuited
  496. local yPlus1 = anchorPos.y + 1
  497. -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true
  498. shortCircuited =
  499. func({x = anchorPos.x - 2, y = yPlus1, z = anchorPos.z - 1}) or func({x = anchorPos.x + 2, y = yPlus1, z = anchorPos.z - 1}) or
  500. func({x = anchorPos.x - 2, y = yPlus1, z = anchorPos.z }) or func({x = anchorPos.x + 2, y = yPlus1, z = anchorPos.z }) or
  501. func({x = anchorPos.x - 2, y = yPlus1, z = anchorPos.z + 1}) or func({x = anchorPos.x + 2, y = yPlus1, z = anchorPos.z + 1}) or
  502. func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z - 2}) or func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z + 2}) or
  503. func({x = anchorPos.x , y = yPlus1, z = anchorPos.z - 2}) or func({x = anchorPos.x , y = yPlus1, z = anchorPos.z + 2}) or
  504. func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z - 2}) or func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z + 2}) or
  505. func({x = anchorPos.x - 1, y = anchorPos.y, z = anchorPos.z - 1}) or
  506. func({x = anchorPos.x - 1, y = anchorPos.y, z = anchorPos.z }) or
  507. func({x = anchorPos.x - 1, y = anchorPos.y, z = anchorPos.z + 1}) or
  508. func({x = anchorPos.x , y = anchorPos.y, z = anchorPos.z - 1}) or
  509. func({x = anchorPos.x , y = anchorPos.y, z = anchorPos.z }) or
  510. func({x = anchorPos.x , y = anchorPos.y, z = anchorPos.z + 1}) or
  511. func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z - 1}) or
  512. func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z }) or
  513. func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z + 1})
  514. return not shortCircuited
  515. end,
  516. -- returns true if function was applied to all wormhole nodes
  517. apply_func_to_wormhole_nodes = function(anchorPos, orientation, func)
  518. local shortCircuited
  519. local yPlus1 = anchorPos.y + 1
  520. -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true
  521. shortCircuited =
  522. func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z - 1}) or
  523. func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z }) or
  524. func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z + 1}) or
  525. func({x = anchorPos.x , y = yPlus1, z = anchorPos.z - 1}) or
  526. func({x = anchorPos.x , y = yPlus1, z = anchorPos.z }) or
  527. func({x = anchorPos.x , y = yPlus1, z = anchorPos.z + 1}) or
  528. func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z - 1}) or
  529. func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z }) or
  530. func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z + 1})
  531. return not shortCircuited
  532. end,
  533. -- Check for suffocation
  534. disable_portal_trap = function(anchorPos, orientation)
  535. -- Not implemented.
  536. end,
  537. schematic = {
  538. size = {x = 5, y = 5, z = 5},
  539. data = { -- note that data is upside down
  540. __,__,__,__,__,
  541. OU4,OW,OW,OW,OU3,
  542. __,AA,AA,AA,__,
  543. __,AA,AA,AA,__,
  544. __,__,__,__,__,
  545. __,OU4,OW,OU3,__,
  546. ON,AA,AA,AA,OS,
  547. AA,AA,AA,AA,AA,
  548. AA,AA,AA,AA,AA,
  549. __,AA,AA,AA,__,
  550. __,ON,OD,OS,__,
  551. ON,AA,AA,AA,OS,
  552. AA,AA,AA,AA,AA,
  553. AA,AA,AA,AA,AA,
  554. __,AA,AA,AA,__,
  555. __,OU,OE,OU2,__,
  556. ON,AA,AA,AA,OS,
  557. AA,AA,AA,AA,AA,
  558. AA,AA,AA,AA,AA,
  559. __,AA,AA,AA,__,
  560. __,__,__,__,__,
  561. OU,OE,OE,OE,OU2,
  562. __,AA,AA,AA,__,
  563. __,AA,AA,AA,__,
  564. __,__,__,__,__,
  565. },
  566. facedirNodes = facedirNodeList
  567. }
  568. } -- End of PortalShape_Platform class
  569. --====================================================--
  570. --======== End of PortalShape implementations ========--
  571. --====================================================--
  572. -- Portal implementation functions --
  573. -- =============================== --
  574. local debugf = nether.debug
  575. local ignition_item_name
  576. local mod_storage = minetest.get_mod_storage()
  577. local meseconsAvailable = minetest.get_modpath("mesecon") ~= nil and minetest.global_exists("mesecon")
  578. local book_added_as_treasure = false
  579. local function get_timerPos_from_p1_and_p2(p1, p2)
  580. -- Pick a frame node for the portal's timer.
  581. --
  582. -- The timer event will need to know the portal definition, which can be determined by
  583. -- what the portal frame is made from, so the timer node should be on the frame.
  584. -- The timer event will also need to know its portal orientation, but unless someone
  585. -- makes a cubic portal shape, orientation can be determined from p1 and p2 in the node's
  586. -- metadata (frame nodes don't have orientation set in param2 like wormhole nodes do).
  587. --
  588. -- We shouldn't pick p1 or p2 as it's possible for two orthogonal portals to share
  589. -- the same p1, etc. - or at least it was - there's code to try to stop that now.
  590. --
  591. -- I'll pick the bottom center node of the portal, since that works for rectangular portals
  592. -- and if someone want to make a circular portal then that positon will still likely be part
  593. -- of the frame.
  594. return {
  595. x = math.floor((p1.x + p2.x) / 2),
  596. y = p1.y,
  597. z = math.floor((p1.z + p2.z) / 2),
  598. }
  599. end
  600. -- orientation is the yaw rotation degrees passed to place_schematic: 0, 90, 180, or 270
  601. -- color is a value from 0 to 7 corresponding to the color of pixels in nether_portals_palette.png
  602. -- portal_is_horizontal is a bool indicating whether the portal lies flat or stands vertically
  603. local function get_colorfacedir_from_color_and_orientation(color, orientation, portal_is_horizontal)
  604. assert(orientation, "no orientation passed")
  605. local axis_direction, rotation
  606. local dir = math.floor((orientation % 360) / 90 + 0.5)
  607. -- if the portal is vertical then node axis direction will be +Y (up) and portal orientation
  608. -- will set the node's rotation.
  609. -- if the portal is horizontal then the node axis direction reflects the yaw orientation and
  610. -- the node's rotation will be whatever's needed to keep the texture horizontal (either 0 or 1)
  611. if portal_is_horizontal then
  612. if dir == 0 then axis_direction = 1 end -- North
  613. if dir == 1 then axis_direction = 3 end -- East
  614. if dir == 2 then axis_direction = 2 end -- South
  615. if dir == 3 then axis_direction = 4 end -- West
  616. rotation = math.floor(axis_direction / 2); -- a rotation is only needed if axis_direction is east or west
  617. else
  618. axis_direction = 0 -- 0 is up, or +Y
  619. rotation = dir
  620. end
  621. -- wormhole nodes have a paramtype2 of colorfacedir, which means the
  622. -- high 3 bits are palette, followed by 3 direction bits and 2 rotation bits.
  623. -- We set the palette bits and rotation
  624. return rotation + axis_direction * 4 + color * 32
  625. end
  626. local function get_orientation_from_colorfacedir(param2)
  627. local axis_direction = 0
  628. -- Strip off the top 6 bits to leave the 2 rotation bits, unfortunately MT lua has no bitwise '&'
  629. -- (high 3 bits are palette, followed by 3 direction bits then 2 rotation bits)
  630. if param2 >= 128 then param2 = param2 - 128 end
  631. if param2 >= 64 then param2 = param2 - 64 end
  632. if param2 >= 32 then param2 = param2 - 32 end
  633. if param2 >= 16 then param2 = param2 - 16; axis_direction = axis_direction + 4 end
  634. if param2 >= 8 then param2 = param2 - 8; axis_direction = axis_direction + 2 end
  635. if param2 >= 4 then param2 = param2 - 4; axis_direction = axis_direction + 1 end
  636. -- if the portal is vertical then node axis direction will be +Y (up) and portal orientation
  637. -- will set the node's rotation.
  638. -- if the portal is horizontal then the node axis direction reflects the yaw orientation and
  639. -- the node's rotation will be whatever's needed to keep the texture horizontal (either 0 or 1)
  640. if axis_direction == 0 or axis_direction == 5 then
  641. -- portal is vertical
  642. return param2 * 90
  643. else
  644. if axis_direction == 1 then return 0 end
  645. if axis_direction == 3 then return 90 end
  646. if axis_direction == 2 then return 180 end
  647. if axis_direction == 4 then return 270 end
  648. end
  649. end
  650. -- We want wormhole nodes to only emit mesecon energy orthogonally to the
  651. -- wormhole surface so that the wormhole will not send power to the frame,
  652. -- this allows the portal frame to listen for mesecon energy from external switches/wires etc.
  653. function get_mesecon_emission_rules_from_colorfacedir(param2)
  654. local axis_direction = 0
  655. -- Strip off the top 6 bits to leave the 2 rotation bits, unfortunately MT lua has no bitwise '&'
  656. -- (high 3 bits are palette, followed by 3 direction bits then 2 rotation bits)
  657. if param2 >= 128 then param2 = param2 - 128 end
  658. if param2 >= 64 then param2 = param2 - 64 end
  659. if param2 >= 32 then param2 = param2 - 32 end
  660. if param2 >= 16 then param2 = param2 - 16; axis_direction = axis_direction + 4 end
  661. if param2 >= 8 then param2 = param2 - 8; axis_direction = axis_direction + 2 end
  662. if param2 >= 4 then param2 = param2 - 4; axis_direction = axis_direction + 1 end
  663. -- if the portal is vertical then node axis_direction will be +Y (up) and node rotation
  664. -- will reflect the portal's yaw orientation.
  665. -- If the portal is horizontal then the node axis direction reflects the yaw orientation and
  666. -- the node's rotation will be whatever's needed to keep the texture horizontal (either 0 or 1)
  667. local rules
  668. if axis_direction == 0 or axis_direction == 5 then
  669. -- portal is vertical
  670. rules = {{x = 0, y = 0, z = 1}, {x = 0, y = 0, z = -1}}
  671. if param2 % 2 ~= 0 then
  672. rules = mesecon.rotate_rules_right(rules)
  673. end
  674. else
  675. -- portal is horizontal, only emit up
  676. rules = {{x = 0, y = 1, z = 0}}
  677. end
  678. return rules
  679. end
  680. nether.get_mesecon_emission_rules_from_colorfacedir = get_mesecon_emission_rules_from_colorfacedir -- make the function available to nodes.lua
  681. -- Combining frame_node_name, p1, and p2 will always be enough to uniquely identify a portal_definition
  682. -- WITHOUT needing to inspect the world. register_portal() will enforce this.
  683. -- This function does not require the portal to be in a loaded chunk.
  684. -- Returns nil if no portal_definition matches the arguments
  685. local function get_portal_definition(frame_node_name, p1, p2)
  686. local size = vector.add(vector.subtract(p2, p1), 1)
  687. local rotated_size = {x = size.z, y = size.y, z = size.x}
  688. for _, portal_def in pairs(nether.registered_portals) do
  689. if portal_def.frame_node_name == frame_node_name then
  690. if vector.equals(size, portal_def.shape.size) or vector.equals(rotated_size, portal_def.shape.size) then
  691. return portal_def
  692. end
  693. end
  694. end
  695. return nil
  696. end
  697. -- Returns a list of all portal_definitions with a frame made of frame_node_name.
  698. -- Ideally no two portal types will be built from the same frame material so this call might be enough
  699. -- to uniquely identify a portal_definition without needing to inspect the world, HOWEVER we shouldn't
  700. -- cramp anyone's style and prohibit non-nether use of obsidian to make portals, so it returns a list.
  701. -- If the list contains more than one item then routines like ignite_portal() will have to search twice
  702. -- for a portal and take twice the CPU.
  703. local function list_portal_definitions_for_frame_node(frame_node_name)
  704. local result = {}
  705. for _, portal_def in pairs(nether.registered_portals) do
  706. if portal_def.frame_node_name == frame_node_name then table.insert(result, portal_def) end
  707. end
  708. return result
  709. end
  710. -- Add portal information to mod storage, so new portals may find existing portals near the target location.
  711. -- Do this whenever a portal is created or changes its ignition state
  712. local function store_portal_location_info(portal_name, anchorPos, orientation, ignited)
  713. if not DEBUG_IGNORE_MODSTORAGE then
  714. local key = minetest.pos_to_string(anchorPos) .. " is " .. portal_name
  715. debugf("Adding/updating portal in mod_storage: " .. key)
  716. mod_storage:set_string(
  717. key,
  718. minetest.serialize({orientation = orientation, active = ignited})
  719. )
  720. end
  721. end
  722. -- Remove portal information from mod storage.
  723. -- Do this if a portal frame is destroyed such that it cannot be ignited anymore.
  724. local function remove_portal_location_info(portal_name, anchorPos)
  725. if not DEBUG_IGNORE_MODSTORAGE then
  726. local key = minetest.pos_to_string(anchorPos) .. " is " .. portal_name
  727. debugf("Removing portal from mod_storage: " .. key)
  728. mod_storage:set_string(key, "")
  729. end
  730. end
  731. -- Returns a table of the nearest portals to anchorPos indexed by distance, based on mod_storage
  732. -- data.
  733. -- Only portals in the same realm as the anchorPos will be returned, even if y_factor is 0.
  734. -- WARNING: Portals are not checked, and inactive portals especially may have been damaged without
  735. -- being removed from the mod_storage data. Check these portals still exist before using them, and
  736. -- invoke remove_portal_location_info() on any found to no longer exist.
  737. --
  738. -- A y_factor of 0 means y does not affect the distance_limit, a y_factor of 1 means y is included,
  739. -- and a y_factor of 2 would squash the search-sphere by a factor of 2 on the y-axis, etc.
  740. -- Pass a nil or negative distance_limit to indicate no distance limit
  741. local function list_closest_portals(portal_definition, anchorPos, distance_limit, y_factor)
  742. local result = {}
  743. if not DEBUG_IGNORE_MODSTORAGE then
  744. local isRealm = portal_definition.is_within_realm(anchorPos)
  745. if distance_limit == nil then distance_limit = -1 end
  746. if y_factor == nil then y_factor = 1 end
  747. for key, value in pairs(mod_storage:to_table().fields) do
  748. local closingBrace = key:find(")", 6, true)
  749. if closingBrace ~= nil then
  750. local found_anchorPos = minetest.string_to_pos(key:sub(0, closingBrace))
  751. if found_anchorPos ~= nil and portal_definition.is_within_realm(found_anchorPos) == isRealm then
  752. local found_name = key:sub(closingBrace + 5)
  753. if found_name == portal_definition.name then
  754. local x = anchorPos.x - found_anchorPos.x
  755. local y = anchorPos.y - found_anchorPos.y
  756. local z = anchorPos.z - found_anchorPos.z
  757. local distance = math.hypot(y * y_factor, math.hypot(x, z))
  758. if distance <= distance_limit or distance_limit < 0 then
  759. local info = minetest.deserialize(value) or {}
  760. debugf("found %s listed at distance %.2f (within %.2f) from dest %s, found: %s orientation %s", found_name, distance, distance_limit, anchorPos, found_anchorPos, info.orientation)
  761. info.anchorPos = found_anchorPos
  762. info.distance = distance
  763. result[distance] = info
  764. end
  765. end
  766. end
  767. end
  768. end
  769. end
  770. return result
  771. end
  772. -- the timerNode is used to keep the metadata as that node already needs to be known any time a portal is stopped or run
  773. -- see also ambient_sound_stop()
  774. function ambient_sound_play(portal_definition, soundPos, timerNodeMeta)
  775. if portal_definition.sounds.ambient ~= nil then
  776. local soundLength = portal_definition.sounds.ambient.length
  777. if soundLength == nil then soundLength = 3 end
  778. local lastPlayed = timerNodeMeta:get_int("ambient_sound_last_played")
  779. -- Using "os.time() % soundLength == 0" is lightweight but means delayed starts, so trying a stored lastPlayed
  780. if os.time() >= lastPlayed + soundLength then
  781. local soundHandle = minetest.sound_play(portal_definition.sounds.ambient, {pos = soundPos, max_hear_distance = 8})
  782. if timerNodeMeta ~= nil then
  783. timerNodeMeta:set_int("ambient_sound_handle", soundHandle)
  784. timerNodeMeta:set_int("ambient_sound_last_played", os.time())
  785. end
  786. end
  787. end
  788. end
  789. -- the timerNode is used to keep the metadata as that node already needs to be known any time a portal is stopped or run
  790. -- see also ambient_sound_play()
  791. function ambient_sound_stop(timerNodeMeta)
  792. if timerNodeMeta ~= nil then
  793. local soundHandle = timerNodeMeta:get_int("ambient_sound_handle")
  794. minetest.sound_fade(soundHandle, -3, 0)
  795. -- clear the metadata
  796. timerNodeMeta:set_string("ambient_sound_handle", "")
  797. timerNodeMeta:set_string("ambient_sound_last_played", "")
  798. end
  799. end
  800. -- WARNING - this is invoked by on_destruct, so you can't assume there's an accesible node at pos
  801. -- Returns true if a portal was found to extinguish
  802. function extinguish_portal(pos, node_name, frame_was_destroyed)
  803. -- mesecons seems to invoke action_off() 6 times every time you place a block?
  804. debugf("extinguish_portal %s %s", pos, node_name)
  805. local meta = minetest.get_meta(pos)
  806. local p1 = minetest.string_to_pos(meta:get_string("p1"))
  807. local p2 = minetest.string_to_pos(meta:get_string("p2"))
  808. local target = minetest.string_to_pos(meta:get_string("target"))
  809. if p1 == nil or p2 == nil then
  810. debugf(" no active portal found to extinguish")
  811. return false
  812. end
  813. local portal_definition = get_portal_definition(node_name, p1, p2)
  814. if portal_definition == nil then
  815. minetest.log("error", "extinguish_portal() invoked on " .. node_name .. " but no registered portal is constructed from " .. node_name)
  816. return false -- no portal frames are made from this type of node
  817. end
  818. if portal_definition.sounds.extinguish ~= nil then
  819. minetest.sound_play(portal_definition.sounds.extinguish, {pos = p1})
  820. end
  821. -- stop timer and ambient sound
  822. local timerPos = get_timerPos_from_p1_and_p2(p1, p2)
  823. minetest.get_node_timer(timerPos):stop()
  824. ambient_sound_stop(minetest.get_meta(timerPos))
  825. -- update the ignition state in the portal location info
  826. local anchorPos, orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(p1, p2)
  827. if frame_was_destroyed then
  828. remove_portal_location_info(portal_definition.name, anchorPos)
  829. else
  830. store_portal_location_info(portal_definition.name, anchorPos, orientation, false)
  831. end
  832. local frame_node_name = portal_definition.frame_node_name
  833. local wormhole_node_name = portal_definition.wormhole_node_name
  834. for x = p1.x, p2.x do
  835. for y = p1.y, p2.y do
  836. for z = p1.z, p2.z do
  837. local clearPos = {x = x, y = y, z = z}
  838. local nn = minetest.get_node(clearPos).name
  839. if nn == frame_node_name or nn == wormhole_node_name then
  840. if nn == wormhole_node_name then
  841. minetest.remove_node(clearPos)
  842. if meseconsAvailable then mesecon.receptor_off(clearPos) end
  843. end
  844. local m = minetest.get_meta(clearPos)
  845. m:set_string("p1", "")
  846. m:set_string("p2", "")
  847. m:set_string("target", "")
  848. m:set_string("portal_type", "")
  849. end
  850. end
  851. end
  852. end
  853. if target ~= nil then
  854. debugf(" attempting to also extinguish target with wormholePos %s", target)
  855. extinguish_portal(target, node_name)
  856. end
  857. if portal_definition.on_extinguish ~= nil then
  858. portal_definition.on_extinguish(portal_definition, anchorPos, orientation)
  859. end
  860. return true
  861. end
  862. -- Note: will extinguish any portal using the same nodes that are being set
  863. local function set_portal_metadata(portal_definition, anchorPos, orientation, destination_wormholePos, ignite)
  864. ignite = ignite or false;
  865. debugf("set_portal_metadata(ignite=%s) at %s orient %s, setting to target %s", ignite, anchorPos, orientation, destination_wormholePos)
  866. -- Portal position is stored in metadata as p1 and p2 to keep maps compatible with earlier versions of this mod.
  867. -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together
  868. -- they define the bounding volume for the portal.
  869. local p1, p2 = portal_definition.shape:get_p1_and_p2_from_anchorPos(anchorPos, orientation)
  870. local p1_string, p2_string = minetest.pos_to_string(p1), minetest.pos_to_string(p2)
  871. local param2 = get_colorfacedir_from_color_and_orientation(portal_definition.wormhole_node_color, orientation, portal_definition.shape.is_horizontal)
  872. local mesecon_rules
  873. if ignite and meseconsAvailable then mesecon_rules = get_mesecon_emission_rules_from_colorfacedir(param2) end
  874. local update_aborted-- using closures to allow the updateFunc to return extra information - by setting this variable
  875. local updateFunc = function(pos)
  876. local meta = minetest.get_meta(pos)
  877. if ignite then
  878. local node_name = minetest.get_node(pos).name
  879. if node_name == "air" then
  880. minetest.set_node(pos, {name = portal_definition.wormhole_node_name, param2 = param2})
  881. if meseconsAvailable then mesecon.receptor_on(pos, mesecon_rules) end
  882. end
  883. local existing_p1 = meta:get_string("p1")
  884. if existing_p1 ~= "" then
  885. local existing_p2 = meta:get_string("p2")
  886. if existing_p1 ~= p1_string or existing_p2 ~= p2_string then
  887. debugf("set_portal_metadata() found existing metadata from another portal: existing_p1 %s, existing_p2 %s, p1 %s, p2 %s, will extinguish existing portal...", existing_p1, existing_p2, p1_string, p2_string)
  888. -- this node is already part of another portal, so extinguish that, because nodes only
  889. -- contain a link in the metadata to one portal, and being part of two allows a slew of bugs
  890. extinguish_portal(pos, node_name, false)
  891. -- clear the metadata to avoid causing a loop if extinguish_portal() fails on this node (e.g. it only works on frame nodes)
  892. meta:set_string("p1", nil)
  893. meta:set_string("p2", nil)
  894. meta:set_string("target", nil)
  895. meta:set_string("portal_type", nil)
  896. update_aborted = true
  897. return true -- short-circuit the update
  898. end
  899. end
  900. end
  901. meta:set_string("p1", minetest.pos_to_string(p1))
  902. meta:set_string("p2", minetest.pos_to_string(p2))
  903. meta:set_string("target", minetest.pos_to_string(destination_wormholePos))
  904. if portal_definition.name ~= "nether_portal" then
  905. -- Legacy portals won't have this extra metadata, so don't rely on it.
  906. -- It's not strictly necessary for PortalShape_Traditional as we know that p1 is part of
  907. -- the frame and we can look up the portal type from p1, p2, and frame node name.
  908. -- Being able to read this from the metadata means other portal shapes needn't have their
  909. -- frame at the timerPos, it may handle unloaded nodes better, and it saves an extra call
  910. -- to minetest.getnode().
  911. meta:set_string("portal_type", portal_definition.name)
  912. end
  913. end
  914. repeat
  915. update_aborted = false
  916. portal_definition.shape.apply_func_to_frame_nodes(anchorPos, orientation, updateFunc)
  917. portal_definition.shape.apply_func_to_wormhole_nodes(anchorPos, orientation, updateFunc)
  918. until not update_aborted
  919. local timerPos = get_timerPos_from_p1_and_p2(p1, p2)
  920. minetest.get_node_timer(timerPos):start(1)
  921. store_portal_location_info(portal_definition.name, anchorPos, orientation, true)
  922. end
  923. local function set_portal_metadata_and_ignite(portal_definition, anchorPos, orientation, destination_wormholePos)
  924. set_portal_metadata(portal_definition, anchorPos, orientation, destination_wormholePos, true)
  925. end
  926. -- this function returns two bools: portal found, portal is lit
  927. local function is_portal_at_anchorPos(portal_definition, anchorPos, orientation, force_chunk_load)
  928. local nodes_are_valid -- using closures to allow the check functions to return extra information - by setting this variable
  929. local portal_is_ignited -- using closures to allow the check functions to return extra information - by setting this variable
  930. local frame_node_name = portal_definition.frame_node_name
  931. local check_frame_Func = function(check_pos)
  932. local foundName = minetest.get_node(check_pos).name
  933. if foundName ~= frame_node_name then
  934. if force_chunk_load and foundName == "ignore" then
  935. -- area isn't loaded, force loading/emerge of check area
  936. minetest.get_voxel_manip():read_from_map(check_pos, check_pos)
  937. foundName = minetest.get_node(check_pos).name
  938. debugf("Forced loading of 'ignore' node at %s, got %s", check_pos, foundName)
  939. if foundName ~= frame_node_name then
  940. nodes_are_valid = false
  941. return true -- short-circuit the search
  942. end
  943. else
  944. nodes_are_valid = false
  945. return true -- short-circuit the search
  946. end
  947. end
  948. end
  949. local wormhole_node_name = portal_definition.wormhole_node_name
  950. local check_wormhole_Func = function(check_pos)
  951. local node_name = minetest.get_node(check_pos).name
  952. if node_name ~= wormhole_node_name then
  953. portal_is_ignited = false;
  954. if node_name ~= "air" then
  955. nodes_are_valid = false
  956. return true -- short-circuit the search
  957. end
  958. end
  959. end
  960. nodes_are_valid = true
  961. portal_is_ignited = true
  962. portal_definition.shape.apply_func_to_frame_nodes(anchorPos, orientation, check_frame_Func) -- check_frame_Func affects nodes_are_valid, portal_is_ignited
  963. if nodes_are_valid then
  964. -- a valid frame exists at anchorPos, check the wormhole is either ignited or unobstructed
  965. portal_definition.shape.apply_func_to_wormhole_nodes(anchorPos, orientation, check_wormhole_Func) -- check_wormhole_Func affects nodes_are_valid, portal_is_ignited
  966. end
  967. return nodes_are_valid, portal_is_ignited and nodes_are_valid -- returns two bools: portal was found, portal is lit
  968. end
  969. -- Checks pos, and if it's part of a portal or portal frame then three values are returned: anchorPos, orientation, is_ignited
  970. -- where orientation is 0 or 90 (0 meaning a portal that faces north/south - i.e. obsidian running east/west)
  971. local function is_within_portal_frame(portal_definition, pos)
  972. local width_minus_1 = portal_definition.shape.size.x - 1
  973. local height_minus_1 = portal_definition.shape.size.y - 1
  974. local depth_minus_1 = portal_definition.shape.size.z - 1
  975. for d = -depth_minus_1, depth_minus_1 do
  976. for w = -width_minus_1, width_minus_1 do
  977. for y = -height_minus_1, height_minus_1 do
  978. local testAnchorPos_x = {x = pos.x + w, y = pos.y + y, z = pos.z + d}
  979. local portal_found, portal_lit = is_portal_at_anchorPos(portal_definition, testAnchorPos_x, 0, true)
  980. if portal_found then
  981. return testAnchorPos_x, 0, portal_lit
  982. else
  983. -- try orthogonal orientation
  984. local testForAnchorPos_z = {x = pos.x + d, y = pos.y + y, z = pos.z + w}
  985. portal_found, portal_lit = is_portal_at_anchorPos(portal_definition, testForAnchorPos_z, 90, true)
  986. if portal_found then return testForAnchorPos_z, 90, portal_lit end
  987. end
  988. end
  989. end
  990. end
  991. end
  992. -- sets param2 values in the schematic to match facedir values, or 0 if the portalframe-nodedef doesn't use facedir
  993. local function set_schematic_param2(schematic_table, frame_node_name, frame_node_color)
  994. local paramtype2 = minetest.registered_nodes[frame_node_name].paramtype2
  995. local isFacedir = paramtype2 == "facedir" or paramtype2 == "colorfacedir"
  996. if schematic_table.facedirNodes ~= nil then
  997. for _, node in ipairs(schematic_table.facedirNodes) do
  998. if isFacedir and node.facedir ~= nil then
  999. -- frame_node_color can be nil
  1000. local colorBits = (frame_node_color or math.floor((node.param2 or 0) / 32)) * 32
  1001. node.param2 = node.facedir + colorBits
  1002. else
  1003. node.param2 = 0
  1004. end
  1005. end
  1006. end
  1007. end
  1008. local function build_portal(portal_definition, anchorPos, orientation, destination_wormholePos)
  1009. set_schematic_param2(portal_definition.shape.schematic, portal_definition.frame_node_name, portal_definition.frame_node_color)
  1010. minetest.place_schematic(
  1011. portal_definition.shape.get_schematicPos_from_anchorPos(anchorPos, orientation),
  1012. portal_definition.shape.schematic,
  1013. orientation,
  1014. { -- node replacements
  1015. ["default:obsidian"] = portal_definition.frame_node_name,
  1016. },
  1017. true
  1018. )
  1019. -- set the param2 on wormhole nodes to ensure they are the right color
  1020. local wormholeNode = {
  1021. name = portal_definition.wormhole_node_name,
  1022. param2 = get_colorfacedir_from_color_and_orientation(portal_definition.wormhole_node_color, orientation, portal_definition.shape.is_horizontal)
  1023. }
  1024. portal_definition.shape.apply_func_to_wormhole_nodes(
  1025. anchorPos,
  1026. orientation,
  1027. function(pos) minetest.swap_node(pos, wormholeNode) end
  1028. )
  1029. debugf("Placed %s portal schematic at %s, orientation %s", portal_definition.name, portal_definition.shape.get_schematicPos_from_anchorPos(anchorPos, orientation), orientation)
  1030. set_portal_metadata(portal_definition, anchorPos, orientation, destination_wormholePos)
  1031. if portal_definition.on_created ~= nil then
  1032. portal_definition.on_created(portal_definition, anchorPos, orientation)
  1033. end
  1034. end
  1035. -- Sometimes after a portal is placed, concurrent mapgen routines overwrite it.
  1036. -- Make portals immortal for ~20 seconds after creation
  1037. local function remote_portal_checkup(elapsed, portal_definition, anchorPos, orientation, destination_wormholePos)
  1038. debugf("portal checkup at %d seconds", elapsed)
  1039. local wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(anchorPos, orientation)
  1040. local wormhole_node = minetest.get_node_or_nil(wormholePos)
  1041. local portalFound, portalLit = false, false
  1042. if wormhole_node ~= nil and wormhole_node.name == portal_definition.wormhole_node_name then
  1043. -- a wormhole node was there, but check the whole frame is intact
  1044. portalFound, portalLit = is_portal_at_anchorPos(portal_definition, anchorPos, orientation, false)
  1045. end
  1046. if not portalFound or not portalLit then
  1047. -- ruh roh
  1048. local message = "Newly created portal at " .. minetest.pos_to_string(anchorPos) .. " was overwritten. Attempting to recreate. Issue spotted after " .. elapsed .. " seconds"
  1049. minetest.log("warning", message)
  1050. debugf("!!! " .. message)
  1051. -- A pre-existing portal frame wouldn't have been immediately overwritten, so no need to check for one, just place the portal.
  1052. build_portal(portal_definition, anchorPos, orientation, destination_wormholePos)
  1053. end
  1054. if elapsed < 10 then -- stop checking after ~20 seconds
  1055. local delay = elapsed * 2
  1056. minetest.after(delay, remote_portal_checkup, elapsed + delay, portal_definition, anchorPos, orientation, destination_wormholePos)
  1057. end
  1058. end
  1059. -- Used to find or build the remote twin after a portal is opened.
  1060. -- If a portal is found that is already lit then it will be extinguished first and its destination_wormholePos updated,
  1061. -- this is to enforce that portals only link together in mutual pairs. It would be better for gameplay if I didn't apply
  1062. -- that restriction, but it would require maintaining an accurate list of every portal that links to a portal so they
  1063. -- could be updated if the portal is destroyed. To keep the code simple I'm going to limit portals to only being the
  1064. -- destination of one lit portal at a time.
  1065. -- * suggested_wormholePos indicates where the portal should be built - note this not an anchorPos!
  1066. -- * suggested_orientation is the suggested schematic rotation to use if no useable portal is found at suggested_wormholePos:
  1067. -- 0, 90, 180, 270 (0 meaning a portal that faces north/south - i.e. obsidian running east/west)
  1068. -- * destination_wormholePos is the wormholePos of the destination portal this one will be linked to.
  1069. --
  1070. -- Returns the final (anchorPos, orientation), as they may differ from the anchorPos and orientation that was
  1071. -- specified if an existing portal was already found there.
  1072. local function locate_or_build_portal(portal_definition, suggested_wormholePos, suggested_orientation, destination_wormholePos)
  1073. debugf("locate_or_build_portal() called at wormholePos%s with suggested orient %s, targeted to %s", suggested_wormholePos, suggested_orientation, destination_wormholePos)
  1074. local result_anchorPos;
  1075. local result_orientation;
  1076. -- Searching for an existing portal at wormholePos seems better than at anchorPos, though isn't important
  1077. local found_anchorPos, found_orientation, is_ignited = is_within_portal_frame(portal_definition, suggested_wormholePos) -- can be optimized - check for portal at exactly suggested_wormholePos first
  1078. if found_anchorPos ~= nil then
  1079. -- A portal is already here, we don't have to build one, though we may need to ignite it
  1080. result_anchorPos = found_anchorPos
  1081. result_orientation = found_orientation
  1082. if is_ignited then
  1083. -- We're about to link to this portal, so if it's already linked to a different portal then
  1084. -- extinguish it, to update the state of the about-to-be-orphaned portal.
  1085. local result_target_str = minetest.get_meta(result_anchorPos):get_string("target")
  1086. local result_target = minetest.string_to_pos(result_target_str)
  1087. if result_target ~= nil and vector.equals(result_target, destination_wormholePos) then
  1088. -- It already links back to the portal the player is teleporting from, so don't
  1089. -- extinguish it or the player's portal will also extinguish.
  1090. debugf(" Build unnecessary: already a lit portal that links back here at %s, orientation %s", found_anchorPos, result_orientation)
  1091. else
  1092. debugf(" Build unnecessary: already a lit portal at %s, orientation %s, linking to %s. Extinguishing...", found_anchorPos, result_orientation, result_target_str)
  1093. extinguish_portal(found_anchorPos, portal_definition.frame_node_name, false)
  1094. end
  1095. else
  1096. debugf(" Build unnecessary: already an unlit portal at %s, orientation %s", found_anchorPos, result_orientation)
  1097. end
  1098. -- ignite the portal
  1099. set_portal_metadata_and_ignite(portal_definition, result_anchorPos, result_orientation, destination_wormholePos)
  1100. else
  1101. result_orientation = suggested_orientation
  1102. result_anchorPos = portal_definition.shape.get_anchorPos_from_wormholePos(suggested_wormholePos, result_orientation)
  1103. build_portal(portal_definition, result_anchorPos, result_orientation, destination_wormholePos)
  1104. -- make sure portal isn't overwritten by ongoing generation/emerge
  1105. minetest.after(2, remote_portal_checkup, 2, portal_definition, result_anchorPos, result_orientation, destination_wormholePos)
  1106. end
  1107. return result_anchorPos, result_orientation
  1108. end
  1109. -- invoked when a player attempts to turn obsidian nodes into an open portal
  1110. -- player_name is optional, allowing a player to spawn a remote portal in their own protected area
  1111. -- ignition_node_name is optional
  1112. local function ignite_portal(ignition_pos, player_name, ignition_node_name)
  1113. if ignition_node_name == nil then ignition_node_name = minetest.get_node(ignition_pos).name end
  1114. debugf("IGNITE the %s at %s", ignition_node_name, ignition_pos)
  1115. -- find which sort of portals are made from the node that was clicked on
  1116. local portal_definition_list = list_portal_definitions_for_frame_node(ignition_node_name)
  1117. for _, portal_definition in ipairs(portal_definition_list) do
  1118. local continue = false -- WRT the for loop, since lua has no continue keyword
  1119. -- check it was a portal frame that the player is trying to ignite
  1120. local anchorPos, orientation, is_ignited = is_within_portal_frame(portal_definition, ignition_pos)
  1121. if anchorPos == nil then
  1122. debugf("No %s portal frame found at ", portal_definition.name, ignition_pos)
  1123. continue = true -- no portal is here, but perhaps there's more than one portal type we need to search for
  1124. elseif is_ignited then
  1125. -- Found a portal, check its metadata and timer is healthy.
  1126. local repair = false
  1127. local meta = minetest.get_meta(ignition_pos)
  1128. if meta ~= nil then
  1129. local p1, p2, target = meta:get_string("p1"), meta:get_string("p2"), meta:get_string("target")
  1130. if p1 == "" or p2 == "" or target == "" then
  1131. -- metadata is missing, the portal frame node must have been removed without calling
  1132. -- on_destruct - perhaps by an ABM, then replaced - presumably by a player.
  1133. -- allowing reigniting will repair the portal
  1134. debugf("Broken portal detected, allowing reignition/repair")
  1135. repair = true
  1136. else
  1137. debugf("This portal links to %s. p1=%s p2=%s", meta:get_string("target"), meta:get_string("p1"), meta:get_string("p2"))
  1138. -- Check the portal's timer is running, and fix if it's not.
  1139. -- A portal's timer can stop running if the game is played without that portal type being
  1140. -- registered, e.g. enabling one of the example portals then later disabling it, then enabling it again.
  1141. -- (if this is a frequent problem, then change the value of "run_at_every_load" in the lbm)
  1142. local timer = minetest.get_node_timer(get_timerPos_from_p1_and_p2(minetest.string_to_pos(p1), minetest.string_to_pos(p2)))
  1143. if timer ~= nil and timer:get_timeout() == 0 then
  1144. debugf("Portal timer was not running: restarting the timer.")
  1145. timer:start(1)
  1146. end
  1147. end
  1148. end
  1149. if not repair then return false end -- portal is already ignited (or timer has been fixed)
  1150. end
  1151. if continue == false then
  1152. debugf("Found portal frame. Looked at %s, found at %s orientation %s", ignition_pos, anchorPos, orientation)
  1153. local destination_anchorPos, destination_orientation
  1154. if portal_definition.is_within_realm(ignition_pos) then
  1155. destination_anchorPos, destination_orientation = portal_definition.find_surface_anchorPos(anchorPos, player_name or "")
  1156. else
  1157. destination_anchorPos, destination_orientation = portal_definition.find_realm_anchorPos(anchorPos, player_name or "")
  1158. end
  1159. if destination_orientation == nil then
  1160. debugf("No destination_orientation given")
  1161. destination_orientation = orientation
  1162. end
  1163. if destination_anchorPos == nil or destination_anchorPos.y == nil then
  1164. -- destination_anchorPos.y was also checked for nil in case portal_definition.find_surface_anchorPos()
  1165. -- had used nether.find_surface_target_y() and that had returned nil.
  1166. debugf("No portal destination available here!")
  1167. if (player_name or "") ~= "" then
  1168. minetest.chat_send_player(player_name, nether.portal_destination_not_found_message)
  1169. end
  1170. return false
  1171. else
  1172. local destination_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(destination_anchorPos, destination_orientation)
  1173. debugf("Destination set to %s", destination_anchorPos)
  1174. -- ignition/BURN_BABY_BURN
  1175. set_portal_metadata_and_ignite(portal_definition, anchorPos, orientation, destination_wormholePos)
  1176. if portal_definition.sounds.ignite ~= nil then
  1177. local local_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(anchorPos, orientation)
  1178. minetest.sound_play(portal_definition.sounds.ignite, {pos = local_wormholePos, max_hear_distance = 20})
  1179. end
  1180. if portal_definition.on_ignite ~= nil then
  1181. portal_definition.on_ignite(portal_definition, anchorPos, orientation)
  1182. end
  1183. return true
  1184. end
  1185. end
  1186. end
  1187. end
  1188. -- invoked when a player is standing in a portal
  1189. local function ensure_remote_portal_then_teleport(playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos)
  1190. local player = minetest.get_player_by_name(playerName)
  1191. if player == nil then return end -- player quit the game while teleporting
  1192. local playerPos = player:get_pos()
  1193. if playerPos == nil then return end -- player quit the game while teleporting
  1194. -- check player is still standing in a portal
  1195. playerPos.y = playerPos.y + 0.1 -- Fix some glitches at -8000
  1196. if minetest.get_node(playerPos).name ~= portal_definition.wormhole_node_name then
  1197. return -- the player has moved out of the portal
  1198. end
  1199. -- debounce - check player is still standing in the *same* portal that called this function
  1200. local meta = minetest.get_meta(playerPos)
  1201. local local_p1, local_p2 = portal_definition.shape:get_p1_and_p2_from_anchorPos(local_anchorPos, local_orientation)
  1202. local p1_at_playerPos = minetest.string_to_pos(meta:get_string("p1"))
  1203. if p1_at_playerPos == nil or not vector.equals(local_p1, p1_at_playerPos) then
  1204. debugf("the player already teleported from %s, and is now standing in a different portal - %s", local_anchorPos, meta:get_string("p1"))
  1205. return -- the player already teleported, and is now standing in a different portal
  1206. end
  1207. local dest_wormhole_node = minetest.get_node_or_nil(destination_wormholePos)
  1208. if dest_wormhole_node == nil then
  1209. -- area not emerged yet, delay and retry
  1210. debugf("ensure_remote_portal_then_teleport() could not find anything yet at %s", destination_wormholePos)
  1211. minetest.after(1, ensure_remote_portal_then_teleport, playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos)
  1212. else
  1213. local local_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(local_anchorPos, local_orientation)
  1214. if dest_wormhole_node.name == portal_definition.wormhole_node_name then
  1215. -- portal exists
  1216. local destination_orientation = get_orientation_from_colorfacedir(dest_wormhole_node.param2)
  1217. local destination_anchorPos = portal_definition.shape.get_anchorPos_from_wormholePos(destination_wormholePos, destination_orientation)
  1218. portal_definition.shape.disable_portal_trap(destination_anchorPos, destination_orientation)
  1219. -- if the portal is already linked to a different portal then extinguish the other portal and
  1220. -- update the target portal to point back at this one.
  1221. local remoteMeta = minetest.get_meta(destination_wormholePos)
  1222. local remoteTarget = minetest.string_to_pos(remoteMeta:get_string("target"))
  1223. if remoteTarget == nil then
  1224. debugf("Failed to test whether target portal links back to this one")
  1225. elseif not vector.equals(remoteTarget, local_wormholePos) then
  1226. debugf("Target portal is already linked, extinguishing then relighting to point back at this one")
  1227. extinguish_portal(remoteTarget, portal_definition.frame_node_name, false)
  1228. set_portal_metadata_and_ignite(
  1229. portal_definition,
  1230. destination_anchorPos,
  1231. destination_orientation,
  1232. local_wormholePos
  1233. )
  1234. end
  1235. debugf("Teleporting player from wormholePos%s to wormholePos%s", local_wormholePos, destination_wormholePos)
  1236. -- play the teleport sound
  1237. if portal_definition.sounds.teleport ~= nil then
  1238. minetest.sound_play(portal_definition.sounds.teleport, {to_player = playerName})
  1239. end
  1240. -- rotate the player if the destination portal is a different orientation
  1241. local rotation_angle = math.rad(destination_orientation - local_orientation)
  1242. local offset = vector.subtract(playerPos, local_wormholePos) -- preserve player's position in the portal
  1243. local rotated_offset = {x = math.cos(rotation_angle) * offset.x - math.sin(rotation_angle) * offset.z, y = offset.y, z = math.sin(rotation_angle) * offset.x + math.cos(rotation_angle) * offset.z}
  1244. local new_playerPos = vector.add(destination_wormholePos, rotated_offset)
  1245. player:set_pos(new_playerPos)
  1246. player:set_look_horizontal(player:get_look_horizontal() + rotation_angle)
  1247. if portal_definition.on_player_teleported ~= nil then
  1248. portal_definition.on_player_teleported(portal_definition, player, playerPos, new_playerPos)
  1249. end
  1250. else
  1251. -- no wormhole node at destination - destination portal either needs to be built or ignited.
  1252. -- Note: A very rare edge-case that is difficult to set up:
  1253. -- If the destination portal is unlit and its frame shares a node with a lit portal that is linked to this
  1254. -- portal (but has not been travelled through, thus not linking this portal back to it), then igniting
  1255. -- the destination portal will extinguish the portal it's touching, which will extinguish this portal
  1256. -- which will leave a confused player.
  1257. -- I don't think this is worth preventing, but I document it incase someone describes entering a portal
  1258. -- and then the portal turning off.
  1259. debugf("ensure_remote_portal_then_teleport() saw %s at %s rather than a wormhole. Calling locate_or_build_portal()", dest_wormhole_node.name, destination_wormholePos)
  1260. local new_dest_anchorPos, new_dest_orientation = locate_or_build_portal(portal_definition, destination_wormholePos, local_orientation, local_wormholePos)
  1261. local new_dest_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(new_dest_anchorPos, new_dest_orientation)
  1262. if not vector.equals(destination_wormholePos, new_dest_wormholePos) then
  1263. -- Update the local portal's target to match where the existing remote portal was found
  1264. if minetest.get_meta(local_anchorPos):get_string("target") == "" then
  1265. -- The local portal has been extinguished!
  1266. -- Abort setting its metadata as that assumes it is active.
  1267. -- This shouldn't happen and may indicate a bug, I trap it incase when the destination
  1268. -- portal was found and extinguished, it somehow linked back to the local portal in a
  1269. -- misaligned fashion that wasn't recognized as being the local portal and caused the
  1270. -- local portal to also be extinguished.
  1271. local message = "Local portal at " .. minetest.pos_to_string(local_anchorPos) .. " was extinguished while linking to existing portal at " .. minetest.pos_to_string(new_dest_anchorPos)
  1272. minetest.log("error", message)
  1273. debugf("!ERROR! - " .. message)
  1274. else
  1275. destination_wormholePos = new_dest_wormholePos
  1276. debugf(" updating target to where remote portal was found - %s", destination_wormholePos)
  1277. set_portal_metadata(
  1278. portal_definition,
  1279. local_anchorPos,
  1280. local_orientation,
  1281. destination_wormholePos
  1282. )
  1283. end
  1284. end
  1285. minetest.after(0.1, ensure_remote_portal_then_teleport, playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos)
  1286. end
  1287. end
  1288. end
  1289. -- run_wormhole() is invoked once per second per portal, handling teleportation and particle effects.
  1290. -- See get_timerPos_from_p1_and_p2() for an explanation of the timerPos location
  1291. function run_wormhole(timerPos, time_elapsed)
  1292. local portal_definition -- will be used inside run_wormhole_node_func()
  1293. local run_wormhole_node_func = function(pos)
  1294. if math.random(2) == 1 then -- lets run only 3 particlespawners instead of 6 per portal
  1295. minetest.add_particlespawner({
  1296. amount = 16,
  1297. time = 2,
  1298. minpos = {x = pos.x - 0.25, y = pos.y - 0.25, z = pos.z - 0.25},
  1299. maxpos = {x = pos.x + 0.25, y = pos.y + 0.25, z = pos.z + 0.25},
  1300. minvel = {x = -0.8, y = -0.8, z = -0.8},
  1301. maxvel = {x = 0.8, y = 0.8, z = 0.8},
  1302. minacc = {x = 0, y = 0, z = 0},
  1303. maxacc = {x = 0, y = 0, z = 0},
  1304. minexptime = 0.5,
  1305. maxexptime = 1.7,
  1306. minsize = 0.5 * portal_definition.particle_texture_scale,
  1307. maxsize = 1.5 * portal_definition.particle_texture_scale,
  1308. collisiondetection = false,
  1309. texture = portal_definition.particle_texture_colored,
  1310. animation = portal_definition.particle_texture_animation,
  1311. glow = 5
  1312. })
  1313. end
  1314. for _, obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
  1315. if obj:is_player() then
  1316. local meta = minetest.get_meta(pos)
  1317. local destination_wormholePos = minetest.string_to_pos(meta:get_string("target"))
  1318. local local_p1 = minetest.string_to_pos(meta:get_string("p1"))
  1319. local local_p2 = minetest.string_to_pos(meta:get_string("p2"))
  1320. if destination_wormholePos ~= nil and local_p1 ~= nil and local_p2 ~= nil then
  1321. -- force emerge of target area
  1322. minetest.get_voxel_manip():read_from_map(destination_wormholePos, destination_wormholePos) -- force load
  1323. if minetest.get_node_or_nil(destination_wormholePos) == nil then
  1324. minetest.emerge_area(vector.subtract(destination_wormholePos, 4), vector.add(destination_wormholePos, 4))
  1325. end
  1326. local local_anchorPos, local_orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(local_p1, local_p2)
  1327. local playerName = obj:get_player_name()
  1328. minetest.after(
  1329. 3, -- hopefully target area is emerged in 3 seconds
  1330. function()
  1331. ensure_remote_portal_then_teleport(
  1332. playerName,
  1333. portal_definition,
  1334. local_anchorPos,
  1335. local_orientation,
  1336. destination_wormholePos
  1337. )
  1338. end
  1339. )
  1340. end
  1341. end
  1342. end
  1343. end
  1344. local p1, p2, portal_name
  1345. local meta = minetest.get_meta(timerPos)
  1346. if meta ~= nil then
  1347. p1 = minetest.string_to_pos(meta:get_string("p1"))
  1348. p2 = minetest.string_to_pos(meta:get_string("p2"))
  1349. portal_name = minetest.string_to_pos(meta:get_string("portal_type")) -- don't rely on this yet until you're sure everything works with old portals that don't have this set
  1350. end
  1351. if p1 ~= nil and p2 ~= nil then
  1352. -- figure out the portal shape so we know where the wormhole nodes will be located
  1353. local frame_node_name
  1354. if portal_name ~= nil and nether.registered_portals[portal_name] ~= nil then
  1355. portal_definition = nether.registered_portals[portal_name]
  1356. else
  1357. frame_node_name = minetest.get_node(timerPos).name -- timerPos should be a frame node if the shape is traditionalPortalShape
  1358. portal_definition = get_portal_definition(frame_node_name, p1, p2)
  1359. end
  1360. if portal_definition == nil then
  1361. minetest.log("error", "No portal with a \"" .. frame_node_name .. "\" frame is registered. run_wormhole" .. minetest.pos_to_string(timerPos) .. " was invoked but that location contains \"" .. frame_node_name .. "\"")
  1362. else
  1363. local anchorPos, orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(p1, p2)
  1364. portal_definition.shape.apply_func_to_wormhole_nodes(anchorPos, orientation, run_wormhole_node_func)
  1365. if portal_definition.on_run_wormhole ~= nil then
  1366. portal_definition.on_run_wormhole(portal_definition, anchorPos, orientation)
  1367. end
  1368. local wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(anchorPos, orientation)
  1369. ambient_sound_play(portal_definition, wormholePos, meta)
  1370. end
  1371. end
  1372. end
  1373. local function create_book(item_name, inventory_description, inventory_image, title, author, chapters)
  1374. local display_book = function(itemstack, user, pointed_thing)
  1375. local player_name = user:get_player_name()
  1376. minetest.sound_play("nether_book_open", {to_player = player_name, gain = 0.25})
  1377. local formspec =
  1378. "size[18,12.122]" ..
  1379. "background[0,0;18,11;nether_book_background.png;true]"..
  1380. "image_button_exit[17.3,0;0.8,0.8;nether_book_close.png;;]"..
  1381. "label[3.1,0.5;" .. minetest.formspec_escape(title) .. "]" ..
  1382. "label[3.6,0.9;" .. author .. "]"
  1383. local image_x_adj = -0.4
  1384. local image_width = 1.6
  1385. local image_padding = 0.06
  1386. for i, chapter in ipairs(chapters) do
  1387. local left_margin = 0.9
  1388. local top_margin = 1.7
  1389. local width = 7.9
  1390. local height = 12.0
  1391. local item_number = i
  1392. local items_on_page = math.floor(#chapters / 2)
  1393. if i > items_on_page then
  1394. -- page 2
  1395. left_margin = 10.1
  1396. top_margin = 0.8
  1397. height = 12.9
  1398. item_number = i - items_on_page
  1399. items_on_page = #chapters - items_on_page
  1400. end
  1401. local available_height = (height - top_margin) / items_on_page
  1402. local y = top_margin + (item_number - 1) * available_height
  1403. -- add chapter title
  1404. local title_height = 0
  1405. if chapter.title ~= nil then
  1406. title_height = 0.6
  1407. formspec = formspec .. "label[".. left_margin + 1.5 .. ","
  1408. .. y .. "; ──══♦♦♦◊ " .. minetest.formspec_escape(chapter.title) .. " ◊♦♦♦══──]"
  1409. end
  1410. -- add chapter image
  1411. local x_offset = 0
  1412. if chapter.image ~= nil then
  1413. x_offset = image_width + image_x_adj + image_padding
  1414. local image_height = image_width / chapter.image.width * chapter.image.height
  1415. formspec = formspec .. "image[" .. left_margin + image_x_adj .. "," .. y + title_height .. ";" .. image_width .. ","
  1416. .. image_height .. ";" .. chapter.image.image .. "]"
  1417. end
  1418. -- add chapter text
  1419. formspec = formspec .. "textarea[" .. left_margin + x_offset .. "," .. y + title_height .. ";" .. width - x_offset .. ","
  1420. .. available_height - title_height .. ";;" .. minetest.formspec_escape(chapter.text) .. ";]"
  1421. end
  1422. minetest.show_formspec(player_name, item_name, formspec)
  1423. end
  1424. minetest.register_craftitem(item_name, {
  1425. description = inventory_description,
  1426. inventory_image = inventory_image,
  1427. groups = {book = 1},
  1428. on_use = display_book,
  1429. _doc_items_hidden = true,
  1430. _doc_items_longdesc =
  1431. S("A guidebook for how to build portals to other realms. It can sometimes be found in dungeon chests, however a copy of this book is not needed as its contents are included in this Encyclopedia.") .. "\n" ..
  1432. S("Refer: \"Help\" > \"Basics\" > \"Building Portals\""),
  1433. })
  1434. end
  1435. local function add_book_as_treasure()
  1436. book_added_as_treasure = true
  1437. if minetest.get_modpath("loot") then
  1438. loot.register_loot({
  1439. weights = { generic = nether.PORTAL_BOOK_LOOT_WEIGHTING * 1000,
  1440. books = 100 },
  1441. payload = { stack = "nether:book_of_portals" }
  1442. })
  1443. end
  1444. if minetest.get_modpath("dungeon_loot") then
  1445. dungeon_loot.register({name = "nether:book_of_portals", chance = nether.PORTAL_BOOK_LOOT_WEIGHTING})
  1446. end
  1447. -- todo: add to Treasurer mod TRMP https://github.com/poikilos/trmp_minetest_game
  1448. end
  1449. -- Returns true if the Help-modpack was installed and Portal instructions were added to it
  1450. -- Help-modpack details can be found at https://forum.minetest.net/viewtopic.php?t=15912
  1451. local function add_book_to_help_modpack(chapters)
  1452. local result = false
  1453. if minetest.get_modpath("doc") ~= nil and minetest.global_exists("doc") then
  1454. if minetest.get_modpath("doc_basics") ~= nil then
  1455. local text = S("Portals to other realms can be opened by building a frame in the right shape with the right blocks, then using an item to activate it. A local copy of the guidebook to portals is published below.\n---\n\n")
  1456. local images = {}
  1457. for i, chapter in ipairs(chapters) do
  1458. if chapter.image ~= nil then
  1459. -- Portal chapters have images (from their portalDef.shape)
  1460. text = text .. "\n\n\n" .. (i - 1) .. ") " .. chapter.title .. "\n\n"
  1461. local aspect_3_to_2_width = chapter.image.width
  1462. local aspect_3_to_2_height = aspect_3_to_2_width / 3 * 2
  1463. if chapter.image.height > aspect_3_to_2_height then
  1464. aspect_3_to_2_height = chapter.image.height
  1465. aspect_3_to_2_width = aspect_3_to_2_height / 2 * 3
  1466. end
  1467. local image_conveted_to_3_2_ratio =
  1468. "[combine:"..aspect_3_to_2_width.."x"..aspect_3_to_2_height..":0,0="..chapter.image.image
  1469. images[#images + 1] = {image=image_conveted_to_3_2_ratio}
  1470. end
  1471. text = text .. chapter.text
  1472. end
  1473. result = doc.add_entry("basics", "portals_api", {
  1474. name = S("Building Portals"),
  1475. data = {
  1476. text = text,
  1477. images = images,
  1478. aspect_ratio=.5
  1479. }
  1480. })
  1481. end
  1482. end
  1483. return result
  1484. end
  1485. -- Updates nether:book_of_portals
  1486. -- A book the player can read to lean how to build the different portals
  1487. local function create_book_of_portals()
  1488. local chapters = {}
  1489. local intro_text
  1490. -- tell the player how many portal types there are
  1491. if nether.registered_portals_count == 1 then
  1492. intro_text = S("In all my travels, and time spent in the Great Libraries, I have encountered no shortage of legends surrounding preternatural doorways said to open into other worlds, yet only one can I confirm as being more than merely a story.")
  1493. else
  1494. intro_text = S("In all my travels, and time spent in the Great Libraries, I have encountered no shortage of legends surrounding preternatural doorways said to open into other worlds, yet only @1 can I confirm as being more than merely stories.", nether.registered_portals_count)
  1495. end
  1496. -- tell the player how to ignite portals
  1497. local ignition_item_description = "<error - ignition item not set>"
  1498. if ignition_item_name ~= nil and minetest.registered_items[ignition_item_name] ~= nil then
  1499. ignition_item_description = minetest.registered_items[ignition_item_name].description
  1500. end
  1501. intro_text = intro_text ..
  1502. S("\n\nThe key to opening such a doorway is to strike the frame with a @1, at which point the very air inside begins to crackle and glow.", ignition_item_description)
  1503. chapters[#chapters + 1] = {text = intro_text}
  1504. -- Describe how to create each type of portal, or perhaps just give clues or flavor text,
  1505. -- but ensure the Nether is always listed first on the first page so other definitions can
  1506. -- refer to it (pairs() returns order based on a random hash).
  1507. local portalDefs_in_order = {}
  1508. if nether.registered_portals["nether_portal"] then
  1509. portalDefs_in_order[#portalDefs_in_order + 1] = nether.registered_portals["nether_portal"]
  1510. end
  1511. for portalName, portalDef in pairs(nether.registered_portals) do
  1512. if portalName ~= "nether_portal" then
  1513. portalDefs_in_order[#portalDefs_in_order + 1] = portalDef
  1514. end
  1515. end
  1516. for _, portalDef in ipairs(portalDefs_in_order) do
  1517. chapters[#chapters + 1] = {
  1518. text = portalDef.book_of_portals_pagetext,
  1519. image = portalDef.shape.diagram_image,
  1520. title = portalDef.title
  1521. }
  1522. end
  1523. create_book(
  1524. ":nether:book_of_portals",
  1525. S("Book of Portals"),
  1526. "nether_book_of_portals.png",
  1527. S("A definitive guide to Rifts and Portals"),
  1528. "Riccard F. Burton", -- perhaps a Richard F. Burton of an alternate universe
  1529. chapters
  1530. )
  1531. local using_helpModpack = add_book_to_help_modpack(chapters)
  1532. if not using_helpModpack and not book_added_as_treasure and nether.PORTAL_BOOK_LOOT_WEIGHTING > 0 then
  1533. -- Only place the Book of Portals in chests if there are non-Nether (i.e. novel) portals
  1534. -- which players need a way to find out about.
  1535. if nether.registered_portals_count > 1 or (nether.registered_portals_count == 1 and nether.registered_portals["nether_portal"] == nil) then
  1536. add_book_as_treasure()
  1537. end
  1538. end
  1539. end
  1540. function register_frame_node(frame_node_name)
  1541. -- copy the existing node definition
  1542. local node_def = minetest.registered_nodes[frame_node_name]
  1543. local extended_node_def = {}
  1544. for key, value in pairs(node_def) do extended_node_def[key] = value end
  1545. extended_node_def.replaced_by_portalapi = {} -- allows chaining or restoration of original functions, if necessary
  1546. -- add portal portal functionality
  1547. extended_node_def.replaced_by_portalapi.mesecons = extended_node_def.mesecons
  1548. extended_node_def.mesecons = {effector = {
  1549. action_on = function (pos, node)
  1550. debugf("portal frame material: mesecons action ON")
  1551. ignite_portal(pos, nil, node.name)
  1552. end,
  1553. action_off = function (pos, node)
  1554. debugf("portal frame material: mesecons action OFF")
  1555. extinguish_portal(pos, node.name, false)
  1556. end
  1557. }}
  1558. extended_node_def.replaced_by_portalapi.on_destruct = extended_node_def.on_destruct
  1559. extended_node_def.on_destruct = function(pos)
  1560. debugf("portal frame material: destruct")
  1561. extinguish_portal(pos, frame_node_name, true)
  1562. end
  1563. extended_node_def.replaced_by_portalapi.on_blast = extended_node_def.on_blast
  1564. extended_node_def.on_blast = function(pos, intensity)
  1565. debugf("portal frame material: blast")
  1566. extinguish_portal(pos, frame_node_name, extended_node_def.replaced_by_portalapi.on_blast == nil)
  1567. if extended_node_def.replaced_by_portalapi.on_blast ~= nil then
  1568. extended_node_def.replaced_by_portalapi.on_blast(pos, intensity)
  1569. else
  1570. minetest.remove_node(pos)
  1571. end
  1572. end
  1573. extended_node_def.replaced_by_portalapi.on_timer = extended_node_def.on_timer
  1574. extended_node_def.on_timer = function(pos, elapsed)
  1575. run_wormhole(pos, elapsed)
  1576. return true
  1577. end
  1578. -- replace the node with the new extended definition
  1579. minetest.register_node(":" .. frame_node_name, extended_node_def)
  1580. end
  1581. function unregister_frame_node(frame_node_name)
  1582. -- copy the existing node definition
  1583. local node = minetest.registered_nodes[frame_node_name]
  1584. local restored_node_def = {}
  1585. for key, value in pairs(node) do restored_node_def[key] = value end
  1586. -- remove portal portal functionality
  1587. restored_node_def.mesecons = nil
  1588. restored_node_def.on_destruct = nil
  1589. restored_node_def.on_timer = nil
  1590. restored_node_def.replaced_by_portalapi = nil
  1591. if node.replaced_by_portalapi ~= nil then
  1592. for key, value in pairs(node.replaced_by_portalapi) do restored_node_def[key] = value end
  1593. end
  1594. -- replace the node with the restored definition
  1595. minetest.register_node(":" .. frame_node_name, restored_node_def)
  1596. end
  1597. -- check for mistakes people might make in custom shape definitions
  1598. function test_shapedef_is_valid(shape_defintion)
  1599. assert(shape_defintion ~= nil, "shape definition cannot be nil")
  1600. assert(shape_defintion.name ~= nil, "shape definition must have a name")
  1601. local result = true
  1602. local origin = vector.new()
  1603. local p1, p2 = shape_defintion:get_p1_and_p2_from_anchorPos(origin, 0)
  1604. assert(vector.equals(shape_defintion.size, vector.add(vector.subtract(p2, p1), 1)), "p1 and p2 of shape definition '" .. shape_defintion.name .. "' don't match shapeDef.size")
  1605. assert(shape_defintion.diagram_image ~= nil and shape_defintion.diagram_image.image ~= nil, "Shape definition '" .. shape_defintion.name .. "' does not provide an image for Help/Book of Portals")
  1606. assert(shape_defintion.diagram_image.width > 0 and shape_defintion.diagram_image.height > 0, "Shape definition '" .. shape_defintion.name .. "' does not provide the size of the image for Help/Book of Portals")
  1607. -- todo
  1608. return result
  1609. end
  1610. -- check for mistakes people might make in portal definitions
  1611. function test_portaldef_is_valid(portal_definition)
  1612. local result = test_shapedef_is_valid(portal_definition.shape)
  1613. assert(portal_definition.wormhole_node_color >= 0 and portal_definition.wormhole_node_color < 8, "portaldef.wormhole_node_color must be between 0 and 7 (inclusive)")
  1614. assert(portal_definition.is_within_realm ~= nil, "portaldef.is_within_realm() must be implemented")
  1615. assert(portal_definition.find_realm_anchorPos ~= nil, "portaldef.find_realm_anchorPos() must be implemented")
  1616. if portal_definition.frame_node_color ~= nil then
  1617. assert(portal_definition.frame_node_color >= 0 and portal_definition.frame_node_color < 8, "portal_definition.frame_node_color must be between 0 and 7 (inclusive)")
  1618. end
  1619. -- todo
  1620. return result
  1621. end
  1622. -- convert portals made with old ABM version of nether mod to use the timer instead
  1623. minetest.register_lbm({
  1624. label = "Start portal timer",
  1625. name = "nether:start_portal_timer",
  1626. nodenames = {"nether:portal"},
  1627. run_at_every_load = false,
  1628. action = function(pos, node)
  1629. local p1, p2
  1630. local meta = minetest.get_meta(pos)
  1631. if meta ~= nil then
  1632. p1 = minetest.string_to_pos(meta:get_string("p1"))
  1633. p2 = minetest.string_to_pos(meta:get_string("p2"))
  1634. end
  1635. if p1 ~= nil and p2 ~= nil then
  1636. local timerPos = get_timerPos_from_p1_and_p2(p1, p2)
  1637. local timer = minetest.get_node_timer(timerPos)
  1638. if timer ~= nil then
  1639. timer:start(1)
  1640. debugf("LBM started portal timer %s", timerPos)
  1641. else
  1642. debugf("get_node_timer%s returned null", timerPos)
  1643. end
  1644. end
  1645. end
  1646. })
  1647. -- Portal API functions --
  1648. -- ==================== --
  1649. -- the fallback defaults for wormhole nodedefs
  1650. local wormhole_nodedef_default = {
  1651. description = S("Portal wormhole"),
  1652. tiles = {
  1653. "nether_transparent.png",
  1654. "nether_transparent.png",
  1655. "nether_transparent.png",
  1656. "nether_transparent.png",
  1657. {
  1658. name = "nether_portal.png",
  1659. animation = {
  1660. type = "vertical_frames",
  1661. aspect_w = 16,
  1662. aspect_h = 16,
  1663. length = 0.9,
  1664. },
  1665. },
  1666. {
  1667. name = "nether_portal.png",
  1668. animation = {
  1669. type = "vertical_frames",
  1670. aspect_w = 16,
  1671. aspect_h = 16,
  1672. length = 0.9,
  1673. },
  1674. },
  1675. },
  1676. drawtype = "nodebox",
  1677. paramtype = "light",
  1678. paramtype2 = "colorfacedir",
  1679. palette = "nether_portals_palette.png",
  1680. post_effect_color = {
  1681. -- post_effect_color can't be changed dynamically in Minetest like the portal colour is.
  1682. -- If you need a different post_effect_color then use register_wormhole_node() to create
  1683. -- another wormhole node with the right post_effect_color and set it as the wormhole_node_name
  1684. -- in your portaldef.
  1685. -- Hopefully this colour is close enough to magenta to work with the traditional magenta
  1686. -- portals, close enough to red to work for a red portal, and also close enough to red to
  1687. -- work with blue & cyan portals - since blue portals are sometimes portrayed as being red
  1688. -- from the opposite side / from the inside.
  1689. a = 160, r = 128, g = 0, b = 80
  1690. },
  1691. sunlight_propagates = true,
  1692. use_texture_alpha = minetest.features.use_texture_alpha_string_modes
  1693. and "blend" or true,
  1694. walkable = false,
  1695. diggable = false,
  1696. pointable = false,
  1697. buildable_to = false,
  1698. is_ground_content = false,
  1699. drop = "",
  1700. light_source = 5,
  1701. node_box = {
  1702. type = "fixed",
  1703. fixed = {
  1704. {-0.5, -0.5, -0.1, 0.5, 0.5, 0.1},
  1705. },
  1706. },
  1707. groups = {not_in_creative_inventory = 1},
  1708. mesecons = {receptor = {
  1709. state = "on",
  1710. rules = function(node)
  1711. return nether.get_mesecon_emission_rules_from_colorfacedir(node.param2)
  1712. end
  1713. }}
  1714. }
  1715. -- Call only at load time
  1716. function nether.register_wormhole_node(name, nodedef)
  1717. assert(name ~= nil, "Unable to register wormhole node: Name is nil")
  1718. assert(nodedef ~= nil, "Unable to register wormhole node ''" .. name .. "'': nodedef is nil")
  1719. for key, value in pairs(wormhole_nodedef_default) do
  1720. if nodedef[key] == nil then nodedef[key] = value end
  1721. end
  1722. minetest.register_node(name, nodedef)
  1723. end
  1724. -- The fallback defaults for registered portaldef tables
  1725. local portaldef_default = {
  1726. title = S("Untitled portal"),
  1727. book_of_portals_pagetext = S("We know almost nothing about this portal"),
  1728. shape = nether.PortalShape_Traditional,
  1729. wormhole_node_name = "nether:portal",
  1730. wormhole_node_color = 0,
  1731. frame_node_name = "default:obsidian",
  1732. particle_texture = "nether_particle.png",
  1733. particle_texture_animation = nil,
  1734. particle_texture_scale = 1,
  1735. sounds = {
  1736. ambient = {name = "nether_portal_ambient", gain = 0.6, length = 3},
  1737. ignite = {name = "nether_portal_ignite", gain = 0.7},
  1738. extinguish = {name = "nether_portal_extinguish", gain = 0.6},
  1739. teleport = {name = "nether_portal_teleport", gain = 0.3}
  1740. }
  1741. }
  1742. function nether.register_portal(name, portaldef)
  1743. assert(name ~= nil, "Unable to register portal: Name is nil")
  1744. assert(portaldef ~= nil, "Unable to register portal ''" .. name .. "'': portaldef is nil")
  1745. if nether.registered_portals[name] ~= nil then
  1746. minetest.log("error", "Unable to register portal: '" .. name .. "' is already in use")
  1747. return false;
  1748. end
  1749. portaldef.name = name
  1750. portaldef.mod_name = minetest.get_current_modname() or "<mod name not recorded>"
  1751. -- use portaldef_default for any values missing from portaldef or portaldef.sounds
  1752. if portaldef.sounds ~= nil then setmetatable(portaldef.sounds, {__index = portaldef_default.sounds}) end
  1753. setmetatable(portaldef, {__index = portaldef_default})
  1754. if portaldef.particle_color == nil then
  1755. -- default the particle colours to be the same as the wormhole colour
  1756. assert(portaldef.wormhole_node_color >= 0 and portaldef.wormhole_node_color < 8, "portaldef.wormhole_node_color must be between 0 and 7 (inclusive)")
  1757. portaldef.particle_color = nether.portals_palette[portaldef.wormhole_node_color].asString
  1758. end
  1759. if portaldef.particle_texture_colored == nil then
  1760. -- Combine the particle texture with the particle color unless a particle_texture_colored was specified.
  1761. if type(portaldef.particle_texture) == "table" and portaldef.particle_texture.animation ~= nil then
  1762. portaldef.particle_texture_colored = portaldef.particle_texture.name .. "^[colorize:" .. portaldef.particle_color .. ":alpha"
  1763. portaldef.particle_texture_animation = portaldef.particle_texture.animation
  1764. portaldef.particle_texture_scale = portaldef.particle_texture.scale or 1
  1765. else
  1766. portaldef.particle_texture_colored = portaldef.particle_texture .. "^[colorize:" .. portaldef.particle_color .. ":alpha"
  1767. end
  1768. end
  1769. if portaldef.find_surface_anchorPos == nil then -- default to using find_surface_target_y()
  1770. portaldef.find_surface_anchorPos = function(pos, player_name)
  1771. local destination_pos = {x = pos.x, y = 0, z = pos.z}
  1772. local existing_portal_location, existing_portal_orientation =
  1773. nether.find_nearest_working_portal(name, destination_pos, 10, 0) -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the realm)
  1774. if existing_portal_location ~= nil then
  1775. return existing_portal_location, existing_portal_orientation
  1776. else
  1777. destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, name, player_name)
  1778. return destination_pos
  1779. end
  1780. end
  1781. end
  1782. if test_portaldef_is_valid(portaldef) then
  1783. -- check whether the portal definition clashes with anyone else's portal
  1784. local p1, p2 = portaldef.shape:get_p1_and_p2_from_anchorPos(vector.new(), 0)
  1785. local existing_portaldef = get_portal_definition(portaldef.frame_node_name, p1, p2)
  1786. if existing_portaldef ~= nil then
  1787. minetest.log("error",
  1788. portaldef.mod_name .." tried to register a portal '" .. portaldef.name .. "' made of " .. portaldef.frame_node_name ..
  1789. ", but it is the same material and shape as the portal '" .. existing_portaldef.name .. "' already registered by " .. existing_portaldef.mod_name ..
  1790. ". Edit the values one of those mods uses in its call to nether.register_portal() if you wish to resolve this clash.")
  1791. else
  1792. -- the new portaldef is good
  1793. nether.registered_portals[portaldef.name] = portaldef
  1794. -- Update registered_portals_count
  1795. local portalCount = 0
  1796. for _ in pairs(nether.registered_portals) do portalCount = portalCount + 1 end
  1797. nether.registered_portals_count = portalCount
  1798. create_book_of_portals()
  1799. if not nether.is_frame_node[portaldef.frame_node_name] then
  1800. -- add portal functions to the nodedef being used for the portal frame
  1801. register_frame_node(portaldef.frame_node_name)
  1802. nether.is_frame_node[portaldef.frame_node_name] = true
  1803. end
  1804. return true
  1805. end
  1806. end
  1807. return false
  1808. end
  1809. function nether.unregister_portal(name)
  1810. assert(name ~= nil, "Cannot unregister portal: Name is nil")
  1811. local portaldef = nether.registered_portals[name]
  1812. local result = portaldef ~= nil
  1813. if portaldef ~= nil then
  1814. nether.registered_portals[name] = nil
  1815. local portals_still_using_frame_node = list_portal_definitions_for_frame_node(portaldef.frame_node_name)
  1816. if next(portals_still_using_frame_node) == nil then
  1817. -- no portals are using this frame node any more
  1818. unregister_frame_node(portaldef.frame_node_name)
  1819. nether.is_frame_node[portaldef.frame_node_name] = nil
  1820. end
  1821. end
  1822. return result
  1823. end
  1824. function nether.register_portal_ignition_item(item_name, ignition_failure_sound)
  1825. minetest.override_item(item_name, {
  1826. on_place = function(stack, placer, pt)
  1827. local done = false
  1828. if pt.under and nether.is_frame_node[minetest.get_node(pt.under).name] then
  1829. done = ignite_portal(pt.under, placer:get_player_name())
  1830. if done and not minetest.settings:get_bool("creative_mode") then
  1831. stack:take_item()
  1832. end
  1833. end
  1834. if not done and ignition_failure_sound ~= nil then
  1835. minetest.sound_play(ignition_failure_sound, {pos = pt.under, max_hear_distance = 10})
  1836. end
  1837. return stack
  1838. end,
  1839. })
  1840. ignition_item_name = item_name
  1841. end
  1842. -- use this when determining where to spawn a portal, to avoid overwriting player builds
  1843. -- It checks the area for any nodes that aren't ground or trees.
  1844. -- player_name is optional, allowing a player to spawn a remote portal in their own protected areas.
  1845. -- (Water also fails this test, unless it is unemerged)
  1846. function nether.volume_is_natural_and_unprotected(minp, maxp, player_name)
  1847. local c_air = minetest.get_content_id("air")
  1848. local c_ignore = minetest.get_content_id("ignore")
  1849. local vm = minetest.get_voxel_manip()
  1850. local emin, emax = vm:read_from_map(minp, maxp)
  1851. local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
  1852. local data = vm:get_data()
  1853. for z = minp.z, maxp.z do
  1854. for y = minp.y, maxp.y do
  1855. local vi = area:index(minp.x, y, z)
  1856. for x = minp.x, maxp.x do
  1857. local id = data[vi] -- Existing node
  1858. if id == nil then debugf("nil block at index " .. vi) end
  1859. if id ~= c_air and id ~= c_ignore and id ~= nil then -- checked for common natural or not emerged
  1860. local name = minetest.get_name_from_content_id(id)
  1861. local nodedef = minetest.registered_nodes[name]
  1862. if nodedef and not nodedef.is_ground_content then
  1863. -- trees are natural but not "ground content"
  1864. local node_groups = nodedef.groups
  1865. if node_groups == nil or (node_groups.tree == nil and node_groups.leaves == nil and node_groups.leafdecay == nil) then
  1866. debugf("volume_is_natural_and_unprotected() found unnatural node %s", name)
  1867. return false
  1868. end
  1869. end
  1870. end
  1871. vi = vi + 1
  1872. end
  1873. end
  1874. end
  1875. if minetest.is_area_protected(minp, maxp, player_name or "") then
  1876. debugf("Volume is protected against player '%s', %s-%s", player_name, minp, maxp)
  1877. return false;
  1878. end
  1879. debugf("Volume is natural and unprotected for player '%s', %s-%s", player_name, minp, maxp)
  1880. return true
  1881. end
  1882. -- Deprecated, use nether.volume_is_natural_and_unprotected() instead.
  1883. function nether.volume_is_natural(minp, maxp)
  1884. if nether.deprecation_warning_volume_is_natural == nil then
  1885. local stack = debug.traceback("", 2);
  1886. local calling_func = (string.split(stack, "\n", false, 2, false)[2] or ""):trim()
  1887. minetest.log("warning",
  1888. "Deprecated function \"nether.volume_is_natural()\" invoked, use \"nether.volume_is_natural_and_unprotected()\" instead. " ..
  1889. calling_func)
  1890. nether.deprecation_warning_volume_is_natural = true;
  1891. end
  1892. return nether.volume_is_natural_and_unprotected(minp, maxp)
  1893. end
  1894. -- Gets the volume that may be altered if a portal is placed at the anchor_pos
  1895. -- orientation is optional, but specifying it will reduce the volume returned
  1896. -- portal_name is optional, but specifying it will reduce the volume returned
  1897. -- returns minp, maxp
  1898. function nether.get_schematic_volume(anchor_pos, orientation, portal_name)
  1899. if orientation == nil then
  1900. -- Return a volume large enough for any orientation
  1901. local minp0, maxp0 = nether.get_schematic_volume(anchor_pos, 0, portal_name)
  1902. local minp1, maxp1 = nether.get_schematic_volume(anchor_pos, 1, portal_name)
  1903. -- ToDo: If an asymmetric portal is used with an anchor not at the center of the
  1904. -- schematic then we will also need to check orientations 3 and 4.
  1905. -- (The currently existing portal-shapes are not affected)
  1906. return
  1907. {x = math.min(minp0.x, minp1.x), y = math.min(minp0.y, minp1.y), z = math.min(minp0.z, minp1.z)},
  1908. {x = math.max(maxp0.x, maxp1.x), y = math.max(maxp0.y, maxp1.y), z = math.max(maxp0.z, maxp1.z)}
  1909. end
  1910. -- Assume the largest possible portal shape unless we know it's a smaller one.
  1911. local shape_defintion = nether.PortalShape_Circular
  1912. if portal_name ~= nil and nether.registered_portals[portal_name] ~= nil then
  1913. shape_defintion = nether.registered_portals[portal_name].shape
  1914. end
  1915. local size = shape_defintion.schematic.size
  1916. local minp = shape_defintion.get_schematicPos_from_anchorPos(anchor_pos, orientation);
  1917. local maxp
  1918. if (orientation % 2) == 0 then
  1919. maxp = {x = minp.x + size.x - 1, y = minp.y + size.y - 1, z = minp.z + size.z - 1}
  1920. else
  1921. maxp = {x = minp.x + size.z - 1, y = minp.y + size.y - 1, z = minp.z + size.x - 1}
  1922. end
  1923. return minp, maxp
  1924. end
  1925. -- Can be used when implementing custom find_surface_anchorPos() functions
  1926. -- portal_name is optional, providing it allows existing portals on the surface to be reused, and
  1927. -- a potentially smaller volume to be checked by volume_is_natural_and_unprotected().
  1928. -- player_name is optional, allowing a player to spawn a remote portal in their own protected areas.
  1929. function nether.find_surface_target_y(target_x, target_z, portal_name, player_name)
  1930. assert(target_x ~= nil and target_z ~= nil, "Arguments `target_x` and `target_z` cannot be nil when calling find_surface_target_y()")
  1931. -- default to starting the search at -16 (probably underground) if we don't know the
  1932. -- surface, like paramat's original code from before get_spawn_level() was available:
  1933. -- https://github.com/minetest-mods/nether/issues/5#issuecomment-506983676
  1934. local start_y = -16
  1935. -- try to spawn on surface first
  1936. if minetest.get_spawn_level ~= nil then -- older versions of Minetest don't have this
  1937. local surface_level = minetest.get_spawn_level(target_x, target_z)
  1938. if surface_level ~= nil then -- test this since get_spawn_level() can return nil over water or steep/high terrain
  1939. -- get_spawn_level() tends to err on the side of caution and spawns the player a
  1940. -- block higher than the ground level. The implementation is mapgen specific
  1941. -- and -2 seems to be the right correction for v6, v5, carpathian, valleys, and flat,
  1942. -- but v7 only needs -1.
  1943. -- Perhaps this was not always the case, and -2 may be too much in older versions
  1944. -- of minetest, but half-buried portals are perferable to floating ones, and they
  1945. -- will clear a suitable hole around themselves.
  1946. if minetest.get_mapgen_setting("mg_name") == "v7" then
  1947. surface_level = surface_level - 1
  1948. else
  1949. surface_level = surface_level - 2
  1950. end
  1951. start_y = surface_level
  1952. end
  1953. end
  1954. local minp_schem, maxp_schem = nether.get_schematic_volume({x = target_x, y = 0, z = target_z}, nil, portal_name)
  1955. local minp = {x = minp_schem.x, y = 0, z = minp_schem.z}
  1956. local maxp = {x = maxp_schem.x, y = 0, z = maxp_schem.z}
  1957. -- Starting searchstep at -16 and making it larger by 2 after each step gives a 20-step search range down to -646:
  1958. -- 0, -16, -34, -54, -76, -100, -126, -154, -184, -216, -250, -286, -324, -364, -406, -450, -496, -544, -594, -646
  1959. local searchstep = -16;
  1960. local y = start_y
  1961. while y > start_y - 650 do
  1962. -- Check volume for non-natural nodes
  1963. minp.y = minp_schem.y + y
  1964. maxp.y = maxp_schem.y + y
  1965. if nether.volume_is_natural_and_unprotected(minp, maxp, player_name) then
  1966. return y
  1967. elseif portal_name ~= nil and nether.registered_portals[portal_name] ~= nil then
  1968. -- players have built here - don't grief.
  1969. -- but reigniting existing portals in portal rooms is fine - desirable even.
  1970. local anchorPos, orientation, is_ignited = is_within_portal_frame(nether.registered_portals[portal_name], {x = target_x, y = y, z = target_z})
  1971. if anchorPos ~= nil then
  1972. debugf("volume_is_natural_and_unprotected check failed, but a portal frame is here %s, so this is still a good target y level", anchorPos)
  1973. return y
  1974. end
  1975. end
  1976. y = y + searchstep
  1977. searchstep = searchstep - 2
  1978. end
  1979. return nil -- Portal ignition failure. Possibly due to a large protected area.
  1980. end
  1981. -- Returns the anchorPos, orientation of the nearest portal, or nil.
  1982. -- A y_factor of 0 means y does not affect the distance_limit, a y_factor of 1 means y is included,
  1983. -- and a y_factor of 2 would squash the search-sphere by a factor of 2 on the y-axis, etc.
  1984. -- Pass a negative distance_limit to indicate no distance limit
  1985. function nether.find_nearest_working_portal(portal_name, anchorPos, distance_limit, y_factor)
  1986. local portal_definition = nether.registered_portals[portal_name]
  1987. assert(portal_definition ~= nil, "find_nearest_working_portal() called with portal_name '" .. portal_name .. "', but no portal is registered with that name.")
  1988. assert(anchorPos ~= nil, "Argument `anchorPos` cannot be nil when calling find_nearest_working_portal()")
  1989. local contenders = list_closest_portals(portal_definition, anchorPos, distance_limit, y_factor)
  1990. -- sort by distance
  1991. local dist_list = {}
  1992. for dist, _ in pairs(contenders) do table.insert(dist_list, dist) end
  1993. table.sort(dist_list)
  1994. for _, dist in ipairs(dist_list) do
  1995. local portal_info = contenders[dist]
  1996. debugf("checking portal from mod_storage at %s orientation %s", portal_info.anchorPos, portal_info.orientation)
  1997. -- the mod_storage list of portals is unreliable - e.g. it won't know if inactive portals have been
  1998. -- destroyed, so check the portal is still there
  1999. local portalFound, portalIsActive = is_portal_at_anchorPos(portal_definition, portal_info.anchorPos, portal_info.orientation, true)
  2000. if portalFound then
  2001. return portal_info.anchorPos, portal_info.orientation
  2002. else
  2003. debugf("Portal wasn't found, removing portal from mod_storage at %s orientation %s",
  2004. portal_info.anchorPos, portal_info.orientation)
  2005. -- The portal at that location must have been destroyed
  2006. remove_portal_location_info(portal_name, portal_info.anchorPos)
  2007. end
  2008. end
  2009. return nil
  2010. end