outback.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. -- The current location of the Outback's gateway exit.
  2. -- Note: this is updated to the correct position (as stored in mod-storage)
  3. -- on first load or whenever the mod is reloaded.
  4. serveressentials.gateway_exit_position = {x=0, y=0, z=0}
  5. -- Localize for performance.
  6. local vector_distance = vector.distance
  7. local vector_round = vector.round
  8. -- Called by the protector mod to determine if a protector can be placed here,
  9. -- with respect to the Outback gateway's current exit location.
  10. local PROTECTOR_DISTANCE_FROM_EXIT = 50
  11. function serveressentials.protector_can_place(pos)
  12. local p2 = serveressentials.gateway_exit_position
  13. if vector_distance(pos, p2) > PROTECTOR_DISTANCE_FROM_EXIT then
  14. return true
  15. end
  16. return false
  17. end
  18. local function get_exit_location()
  19. local meta = serveressentials.modstorage
  20. local s = meta:get_string("outback_exit_location")
  21. if s and s ~= "" then
  22. local p = minetest.string_to_pos(s)
  23. if p then
  24. return s
  25. end
  26. end
  27. -- Fallback.
  28. return "(0,-7,0)"
  29. end
  30. serveressentials.get_exit_location = get_exit_location
  31. serveressentials.gateway_exit_position = minetest.string_to_pos(get_exit_location())
  32. function serveressentials.get_current_exit_location()
  33. -- Also need to update the gate itself, right away.
  34. local m2 = minetest.get_meta({x=-9164, y=4101, z=5780})
  35. local s2 = m2:get_string("obsidian_gateway_destination_ew")
  36. return s2
  37. end
  38. function serveressentials.update_exit_location(pos)
  39. pos = vector_round(pos)
  40. -- Update position stored in memory.
  41. local p = serveressentials.gateway_exit_position
  42. p.x = pos.x
  43. p.y = pos.y
  44. p.z = pos.z
  45. -- Update the location stored in mod-storage.
  46. local meta = serveressentials.modstorage
  47. local s = minetest.pos_to_string(pos)
  48. meta:set_string("outback_exit_location", s)
  49. -- Also need to update the gate itself, right away.
  50. local m2 = minetest.get_meta({x=-9164, y=4101, z=5780})
  51. m2:set_string("obsidian_gateway_destination_ew", s)
  52. end
  53. local nodes = {
  54. -- Replace torches with real lanterns in front of the gate.
  55. {pos={x=-9167, y=4103, z=5779}, node={name="xdecor:lantern", param2=1}},
  56. {pos={x=-9167, y=4103, z=5785}, node={name="xdecor:lantern", param2=1}},
  57. -- Add a furnace to the miner's hut.
  58. {pos={x=-9177, y=4176, z=5745}, node={name="redstone_furnace:inactive", param2=3}},
  59. -- Graveyard protector.
  60. {pos={x=-9266, y=4170, z=5724}, node={name="protector:protect3", param2=0}},
  61. -- Farm protectors.
  62. {pos={x=-9082, y=4179, z=5720}, node={name="protector:protect3", param2=0}},
  63. {pos={x=-9139, y=4168, z=5795}, node={name="protector:protect3", param2=0}},
  64. {pos={x=-9199, y=4169, z=5836}, node={name="protector:protect3", param2=0}},
  65. -- Protector in the miner's hut.
  66. {pos={x=-9176, y=4175, z=5745}, node={name="protector:protect3", param2=0}},
  67. -- Spawn protector.
  68. {pos={x=-9223, y=4168, z=5861}, node={name="protector:protect3", param2=0}},
  69. -- Extra signs at spawn.
  70. {pos={x=-9221, y=4169, z=5861}, node={name="signs:sign_wall_wood", param2=2}},
  71. {pos={x=-9221, y=4169, z=5860}, node={name="signs:sign_wall_wood", param2=2}},
  72. -- Extra pillar between Oerkki spawn point and the gate.
  73. {pos={x=-9169, y=4104, z=5782}, node={name="pillars:rackstone_cobble_top", param2=3}},
  74. {pos={x=-9169, y=4103, z=5782}, node={name="walls:rackstone_cobble_noconnect", param2=0}},
  75. {pos={x=-9169, y=4102, z=5782}, node={name="walls:rackstone_cobble_noconnect", param2=0}},
  76. {pos={x=-9169, y=4101, z=5782}, node={name="walls:rackstone_cobble_noconnect", param2=0}},
  77. {pos={x=-9169, y=4100, z=5782}, node={name="pillars:rackstone_cobble_bottom", param2=3}},
  78. }
  79. local function rebuild_nodes()
  80. for k, v in ipairs(nodes) do
  81. minetest.set_node(v.pos, v.node)
  82. end
  83. end
  84. local metadata = {
  85. -- Gate room, protection on floor.
  86. {pos={x=-9174, y=4099, z=5782}, meta={fields={
  87. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  88. owner = "MustTest",
  89. placedate = "2020/02/12 UTC",
  90. rename = "MustTest",
  91. }}},
  92. -- Gate room, protection on ceiling.
  93. {pos={x=-9174, y=4106, z=5782}, meta={fields={
  94. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  95. owner = "MustTest",
  96. placedate = "2020/02/12 UTC",
  97. rename = "MustTest",
  98. }}},
  99. -- Gate protection, left side.
  100. {pos={x=-9165, y=4103, z=5785}, meta={fields={
  101. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  102. owner = "MustTest",
  103. placedate = "2020/02/12 UTC",
  104. rename = "MustTest",
  105. }}},
  106. -- Gate protection, right side.
  107. {pos={x=-9165, y=4103, z=5779}, meta={fields={
  108. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  109. owner = "MustTest",
  110. placedate = "2020/02/12 UTC",
  111. rename = "MustTest",
  112. }}},
  113. -- Poem, left side.
  114. {pos={x=-9172, y=4100, z=5783}, meta={fields={
  115. infotext = "Make your choice, adventurous Stranger,\nStrike the Gate and bide the Danger!",
  116. author = "MustTest",
  117. text = "Make your choice, adventurous Stranger,%nStrike the Gate and bide the Danger!"
  118. }}},
  119. -- Poem, right side.
  120. {pos={x=-9172, y=4100, z=5781}, meta={fields={
  121. infotext = "Or else wonder, till it drives you mad,\nWhat would have followed, if you had.",
  122. author = "MustTest",
  123. text = "Or else wonder, till it drives you mad,%nWhat would have followed, if you had."
  124. }}},
  125. -- Door on the miner's hut.
  126. {pos={x=-9174, y=4175, z=5744}, meta={fields={
  127. state = "1",
  128. }}},
  129. -- Door on the calico stash (safe from Indians!).
  130. {pos={x=-9090, y=4182, z=5869}, meta={fields={
  131. state = "0",
  132. }}},
  133. -- The gateway portal itself.
  134. {pos={x=-9164, y=4101, z=5780}, meta={fields={
  135. obsidian_gateway_success_ew = "yes",
  136. obsidian_gateway_return_gate_ew = "0",
  137. obsidian_gateway_owner_ew = "MustTest",
  138. obsidian_gateway_destination_ew = get_exit_location(),
  139. }}},
  140. -- Gravesite sign, left.
  141. {pos={x=-9265, y=4172, z=5724}, meta={fields={
  142. infotext = "Henry D. Miner\nApril 13, 1821 - October 3, 1890\n\"An ardent Abolitionist, a true Republican, and a determined teetotaler.\"",
  143. author = "MustTest",
  144. text = "Henry D. Miner%nApril 13, 1821 - October 3, 1890%n\"An ardent Abolitionist, a true Republican, and a determined teetotaler.\""
  145. }}},
  146. -- Gravesite sign, right.
  147. {pos={x=-9267, y=4172, z=5724}, meta={fields={
  148. infotext = "Martha Ann Lee Miner\nNovember 2, 1829 - February 19, 1897",
  149. author = "MustTest",
  150. text = "Martha Ann Lee Miner%nNovember 2, 1829 - February 19, 1897"
  151. }}},
  152. -- Spawn sign, left.
  153. {pos={x=-9221, y=4170, z=5861}, meta={fields={
  154. infotext = "Use /spawn to get back here.",
  155. author = "MustTest",
  156. text = "Use /spawn to get back here."
  157. }}},
  158. -- Spawn sign, right.
  159. {pos={x=-9221, y=4170, z=5860}, meta={fields={
  160. infotext = "Use /info to get help.",
  161. author = "MustTest",
  162. text = "Use /info to get help."
  163. }}},
  164. -- Spawn sign, bottom left.
  165. {pos={x=-9221, y=4169, z=5861}, meta={fields={
  166. infotext = "See \"http://arklegacy.duckdns.org\" for important info.",
  167. author = "MustTest",
  168. text = "See \"http://arklegacy.duckdns.org\" for important info."
  169. }}},
  170. -- Spawn sign, bottom right.
  171. {pos={x=-9221, y=4169, z=5860}, meta={fields={
  172. infotext = "Take care, don't rush!",
  173. author = "MustTest",
  174. text = "Take care, don't rush!"
  175. }}},
  176. -- Graveyard protector.
  177. {pos={x=-9266, y=4170, z=5724}, meta={fields={
  178. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  179. owner = "MustTest",
  180. placedate = "2020/02/12 UTC",
  181. rename = "MustTest",
  182. }}},
  183. -- Farm protectors.
  184. {pos={x=-9082, y=4179, z=5720}, meta={fields={
  185. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  186. owner = "MustTest",
  187. placedate = "2020/02/12 UTC",
  188. rename = "MustTest",
  189. }}},
  190. {pos={x=-9139, y=4168, z=5795}, meta={fields={
  191. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  192. owner = "MustTest",
  193. placedate = "2020/02/12 UTC",
  194. rename = "MustTest",
  195. }}},
  196. {pos={x=-9199, y=4169, z=5836}, meta={fields={
  197. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  198. owner = "MustTest",
  199. placedate = "2020/02/12 UTC",
  200. rename = "MustTest",
  201. }}},
  202. -- Protector in the miner's hut.
  203. {pos={x=-9176, y=4175, z=5745}, meta={fields={
  204. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  205. owner = "MustTest",
  206. placedate = "2020/02/12 UTC",
  207. rename = "MustTest",
  208. }}},
  209. -- Spawn protector.
  210. {pos={x=-9223, y=4168, z=5861}, meta={fields={
  211. infotext = "Protection (Owned by <MustTest>!)\nPlaced on 2020/02/12 UTC",
  212. owner = "MustTest",
  213. placedate = "2020/02/12 UTC",
  214. rename = "MustTest",
  215. }}},
  216. }
  217. local function rebuild_metadata()
  218. for k, v in ipairs(metadata) do
  219. local meta = minetest.get_meta(v.pos)
  220. meta:from_table(v.meta)
  221. end
  222. end
  223. local timers = {
  224. -- First farm.
  225. {x=-9139, y=4172, z=5796},
  226. {x=-9140, y=4172, z=5796},
  227. -- Hilltop farm.
  228. {x=-9083, y=4183, z=5721},
  229. {x=-9082, y=4183, z=5721},
  230. {x=-9081, y=4183, z=5721},
  231. {x=-9083, y=4183, z=5719},
  232. {x=-9082, y=4183, z=5719},
  233. {x=-9081, y=4183, z=5719},
  234. }
  235. local function restart_timers()
  236. for k, v in ipairs(timers) do
  237. local timer = minetest.get_node_timer(v)
  238. timer:start(1)
  239. end
  240. end
  241. local function callback(blockpos, action, calls_remaining, param)
  242. -- We don't do anything until the last callback.
  243. if calls_remaining ~= 0 then
  244. return
  245. end
  246. -- Check if there was an error on the LAST call.
  247. -- Note: this will usually fail if the area to emerge intersects the map edge.
  248. -- But usually we don't try to do that, here.
  249. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  250. return
  251. end
  252. -- Locate all nodes with metadata.
  253. local minp = table.copy(rc.get_realm_data("abyss").minp)
  254. local maxp = table.copy(rc.get_realm_data("abyss").maxp)
  255. local pos_metas = minetest.find_nodes_with_meta(minp, maxp)
  256. -- Locate all bones and load their meta into memory.
  257. local bones = {}
  258. for k, v in ipairs(pos_metas) do
  259. local node = minetest.get_node(v)
  260. if node.name == "bones:bones" then
  261. local meta = minetest.get_meta(v)
  262. local data = {}
  263. data.pos = v
  264. data.meta = meta:to_table()
  265. data.node = node
  266. bones[#bones + 1] = data
  267. end
  268. end
  269. -- Place schematic. This overwrites all nodes, but not necessarily their meta.
  270. local schematic = rc.modpath .. "/outback_map.mts"
  271. local pos = {x=-9274, y=4000, z=5682}
  272. local replacements = {}
  273. if minetest.registered_nodes["basictrees:acacia_branch"] then
  274. replacements = {
  275. ["stairs:slope_acacia_trunk_outer"] = "basictrees:acacia_branch",
  276. }
  277. end
  278. minetest.place_schematic(pos, schematic, "0", replacements, true, "")
  279. -- Erase all stale metadata.
  280. for k, v in ipairs(pos_metas) do
  281. local meta = minetest.get_meta(v)
  282. meta:from_table(nil)
  283. end
  284. -- Restore all bones.
  285. for k, v in ipairs(bones) do
  286. minetest.set_node(v.pos, v.node)
  287. minetest.get_meta(v.pos):from_table(v.meta)
  288. minetest.get_node_timer(v.pos):start(10)
  289. end
  290. -- Finally, rebuild the core metadata and node structure.
  291. rebuild_nodes()
  292. rebuild_metadata()
  293. restart_timers()
  294. end
  295. -- This API may be called to completely reset the Outback realm.
  296. -- It will restore the realm's map and metadata.
  297. function serveressentials.rebuild_outback()
  298. local p1 = table.copy(rc.realms[4].minp)
  299. local p2 = table.copy(rc.realms[4].maxp)
  300. minetest.emerge_area(p1, p2, callback, {})
  301. end