outback.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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+400, 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+400, 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(vector.add(v.pos, {x=0, y=400, z=0}), 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(vector.add(v.pos, {x=0, y=400, z=0}))
  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(vector.add(v, {x=0, y=400, z=0}))
  238. timer:start(1)
  239. end
  240. end
  241. -- Find outback surface under sunlight.
  242. local function find_ground(pos)
  243. local n1 = minetest.get_node(pos)
  244. local p2 = vector.offset(pos, 0, -1, 0)
  245. local n2 = minetest.get_node(p2)
  246. local count = 0
  247. while n2.name == "air" and count < 32 do
  248. pos = p2
  249. n1 = minetest.get_node(pos)
  250. p2 = vector.offset(pos, 0, -1, 0)
  251. n2 = minetest.get_node(p2)
  252. count = count + 1
  253. end
  254. if n2.name == "rackstone:cobble" and n1.name == "air" then
  255. if (minetest.get_node_light(pos, 0.5)) or 0 == 15 then
  256. return pos
  257. end
  258. end
  259. end
  260. local function place_random_farms(minp, maxp)
  261. for count = 1, 50 do
  262. local p = {
  263. x = math.random(minp.x + 10, maxp.x - 10),
  264. y = maxp.y,
  265. z = math.random(minp.z + 10, maxp.z - 10),
  266. }
  267. local g = find_ground(p)
  268. if g then
  269. -- Check corners.
  270. local c1 = find_ground(vector.offset(g, -2, 16, -2))
  271. local c2 = find_ground(vector.offset(g, 2, 16, -2))
  272. local c3 = find_ground(vector.offset(g, -2, 16, 2))
  273. local c4 = find_ground(vector.offset(g, 2, 16, 2))
  274. local b1, b2, b3, b4 = false, false, false, false
  275. -- Make sure ground is mostly flat.
  276. if c1 and math.abs(c1.y - g.y) < 2 then b1 = true end
  277. if c2 and math.abs(c2.y - g.y) < 2 then b2 = true end
  278. if c3 and math.abs(c3.y - g.y) < 2 then b3 = true end
  279. if c4 and math.abs(c4.y - g.y) < 2 then b4 = true end
  280. -- Can't be protected. This relies on protectors and protector meta being
  281. -- set *before* we place the farms!
  282. if minetest.test_protection(c1, "") then b1 = false end
  283. if minetest.test_protection(c2, "") then b2 = false end
  284. if minetest.test_protection(c3, "") then b3 = false end
  285. if minetest.test_protection(c4, "") then b4 = false end
  286. if b1 and b2 and b3 and b4 then
  287. local schematic = rc.modpath .. "/outback_small_farm.mts"
  288. local d = vector.offset(g, -2, -2, -2)
  289. minetest.place_schematic(d, schematic, "random", {}, true, "")
  290. end
  291. end
  292. end
  293. end
  294. local function callback(blockpos, action, calls_remaining, param)
  295. -- We don't do anything until the last callback.
  296. if calls_remaining ~= 0 then
  297. return
  298. end
  299. -- Check if there was an error on the LAST call.
  300. -- Note: this will usually fail if the area to emerge intersects the map edge.
  301. -- But usually we don't try to do that, here.
  302. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  303. return
  304. end
  305. -- Locate all nodes with metadata.
  306. local minp = table.copy(rc.get_realm_data("abyss").minp)
  307. local maxp = table.copy(rc.get_realm_data("abyss").maxp)
  308. local pos_metas = minetest.find_nodes_with_meta(minp, maxp)
  309. -- Locate all bones and load their meta into memory.
  310. local bones = {}
  311. for k, v in ipairs(pos_metas) do
  312. local node = minetest.get_node(v)
  313. if node.name == "bones:bones" then
  314. local meta = minetest.get_meta(v)
  315. local data = {}
  316. data.pos = v
  317. data.meta = meta:to_table()
  318. data.node = node
  319. bones[#bones + 1] = data
  320. end
  321. end
  322. -- Place schematic. This overwrites all nodes, but not necessarily their meta.
  323. local schematic = rc.modpath .. "/outback_map.mts"
  324. local apron_schematic = rc.modpath .. "/outback_apron.mts"
  325. local pos = {x=-9274, y=4000+400, z=5682}
  326. local apron_pos = {x=-9314, y=4141+400, z=5642}
  327. local replacements = {}
  328. if minetest.registered_nodes["basictrees:acacia_branch"] then
  329. replacements = {
  330. ["stairs:slope_acacia_trunk_outer"] = "basictrees:acacia_branch",
  331. }
  332. end
  333. minetest.place_schematic(apron_pos, apron_schematic, "0", replacements, true, "")
  334. minetest.place_schematic(pos, schematic, "0", replacements, true, "")
  335. -- Erase all stale metadata.
  336. for k, v in ipairs(pos_metas) do
  337. local meta = minetest.get_meta(v)
  338. meta:from_table(nil)
  339. end
  340. teleports.delete_blocks_from_area(minp, maxp)
  341. city_block.delete_blocks_from_area(minp, maxp)
  342. -- Restore all bones.
  343. for k, v in ipairs(bones) do
  344. local np = vector.add(v.pos, {x=0, y=0, z=0})
  345. minetest.set_node(np, v.node)
  346. minetest.get_meta(np):from_table(v.meta)
  347. minetest.get_node_timer(np):start(10)
  348. end
  349. -- Finally, rebuild the core metadata and node structure.
  350. rebuild_nodes()
  351. rebuild_metadata()
  352. restart_timers()
  353. place_random_farms(minp, maxp)
  354. end
  355. -- This API may be called to completely reset the Outback realm.
  356. -- It will restore the realm's map and metadata.
  357. function serveressentials.rebuild_outback()
  358. local p1 = table.copy(rc.get_realm_data("abyss").minp)
  359. local p2 = table.copy(rc.get_realm_data("abyss").maxp)
  360. minetest.emerge_area(p1, p2, callback, {})
  361. end