functions.lua 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. -- This file is designed to be reloadable.
  2. if not minetest.global_exists("teleports") then teleports = {} end
  3. teleports.teleports = teleports.teleports or {}
  4. teleports.min_range = 250
  5. teleports.datafile = minetest.get_worldpath() .. "/teleports.txt"
  6. -- Localize for performance.
  7. local vector_distance = vector.distance
  8. local vector_round = vector.round
  9. local vector_add = vector.add
  10. local vector_equals = vector.equals
  11. local math_floor = math.floor
  12. local math_random = math.random
  13. dofile(teleports.modpath .. "/sickness.lua")
  14. local nyanbow = "rosestone:tail"
  15. -- Table of blocks which can be used to super-charge a teleport. Each block has a specific charge value.
  16. teleports.charge_blocks = {
  17. ["default:diamondblock"] = {charge=15 },
  18. ["default:mese"] = {charge=5 },
  19. ["default:steelblock"] = {charge=2 },
  20. ["default:copperblock"] = {charge=2.5 },
  21. ["default:bronzeblock"] = {charge=2.8 },
  22. ["default:goldblock"] = {charge=3 },
  23. ["moreores:silver_block"] = {charge=3 },
  24. ["moreores:tin_block"] = {charge=2.5 },
  25. ["moreores:mithril_block"] = {charge=25 },
  26. ["chromium:block"] = {charge=1.9 },
  27. ["zinc:block"] = {charge=2.8 },
  28. ["lead:block"] = {charge=1.4 },
  29. ["akalin:block"] = {charge=1.9 },
  30. ["alatro:block"] = {charge=1.7 },
  31. ["arol:block"] = {charge=1.5 },
  32. ["talinite:block"] = {charge=2.1 },
  33. }
  34. function teleports.delete_blocks_from_area(minp, maxp)
  35. local i = 1
  36. local blocks = teleports.teleports
  37. ::do_next::
  38. if i > #blocks then
  39. return
  40. end
  41. local p = blocks[i].pos
  42. if p.x >= minp.x and p.x <= maxp.x and
  43. p.y >= minp.y and p.y <= maxp.y and
  44. p.z >= minp.z and p.z <= maxp.z then
  45. -- Don't need to worry about relative ordering.
  46. -- This is your standard swap'n'pop.
  47. blocks[i] = blocks[#blocks]
  48. blocks[#blocks] = nil
  49. goto do_next
  50. end
  51. i = i + 1
  52. goto do_next
  53. end
  54. -- Build list of all teleports in same realm as 'origin', then return a random
  55. -- TP from that list, or nil.
  56. function teleports.get_random_teleport(origin)
  57. if #(teleports.teleports) == 0 then
  58. return
  59. end
  60. local realm = rc.current_realm_at_pos(origin)
  61. local ports = teleports.teleports
  62. local caned = {}
  63. if realm == "" then
  64. return
  65. end
  66. for i = 1, #ports do
  67. local p = ports[i]
  68. if not vector_equals(p.pos, origin) then
  69. if rc.current_realm_at_pos(p.pos) == realm then
  70. caned[#caned + 1] = p
  71. end
  72. end
  73. end
  74. if #caned > 0 then
  75. return caned[math_random(1, #caned)]
  76. end
  77. end
  78. function teleports.nearest_beacons_to_position(pos, num, rangelim)
  79. local get_rn = rc.current_realm_at_pos
  80. local realm = get_rn(pos)
  81. -- Copy the master table's indices so we don't modify it.
  82. -- We do not need to copy the inner table data itself. Just the indices.
  83. -- Only copy over blocks in the same realm, too.
  84. local blocks = {}
  85. local sblocks = teleports.teleports
  86. for i=1, #sblocks, 1 do
  87. local v = sblocks[i]
  88. local p = v.pos
  89. if v.is_recall then
  90. if rangelim then
  91. if vector_distance(p, pos) < rangelim then
  92. if get_rn(p) == realm then
  93. blocks[#blocks+1] = v
  94. end
  95. end
  96. else
  97. if get_rn(p) == realm then
  98. blocks[#blocks+1] = v
  99. end
  100. end
  101. end
  102. end
  103. -- Sort blocks, nearest blocks first.
  104. table.sort(blocks,
  105. function(a, b)
  106. local d1 = vector_distance(a.pos, pos)
  107. local d2 = vector_distance(b.pos, pos)
  108. return d1 < d2
  109. end)
  110. -- Return N-nearest blocks (should be at the front of the sorted table).
  111. local ret = {}
  112. for i=1, num, 1 do
  113. if i <= #blocks then
  114. ret[#ret+1] = blocks[i]
  115. else
  116. break
  117. end
  118. end
  119. return ret
  120. end
  121. teleports.is_nyanbow_teleport = function(pos)
  122. local positions = {
  123. {x=pos.x-1, y=pos.y, z=pos.z},
  124. {x=pos.x+1, y=pos.y, z=pos.z},
  125. {x=pos.x, y=pos.y, z=pos.z-1},
  126. {x=pos.x, y=pos.y, z=pos.z+1},
  127. {x=pos.x-1, y=pos.y, z=pos.z-1},
  128. {x=pos.x+1, y=pos.y, z=pos.z+1},
  129. {x=pos.x-1, y=pos.y, z=pos.z+1},
  130. {x=pos.x+1, y=pos.y, z=pos.z-1},
  131. }
  132. local bows = 0
  133. for k, v in ipairs(positions) do
  134. local n = minetest.get_node(v).name
  135. if n == nyanbow then
  136. bows = bows + 1
  137. end
  138. end
  139. return (bows == 8)
  140. end
  141. teleports.save = function()
  142. local datastring = xban.serialize(teleports.teleports)
  143. if not datastring then
  144. return
  145. end
  146. minetest.safe_file_write(teleports.datafile, datastring)
  147. --[[
  148. local file, err = io.open(teleports.datafile, "w")
  149. if err then
  150. return
  151. end
  152. file:write(datastring)
  153. file:close()
  154. --]]
  155. end
  156. teleports.load = function()
  157. local file, err = io.open(teleports.datafile, "r")
  158. if err then
  159. teleports.teleports = {}
  160. return
  161. end
  162. teleports.teleports = minetest.deserialize(file:read("*all"))
  163. if type(teleports.teleports) ~= "table" then
  164. teleports.teleports = {}
  165. end
  166. file:close()
  167. end
  168. teleports.clear_area = function(minp, maxp)
  169. for x = minp.x, maxp.x, 1 do
  170. for y = minp.y, maxp.y, 1 do
  171. for z = minp.z, maxp.z, 1 do
  172. local pos = {x=x, y=y, z=z}
  173. local node = minetest.get_node(pos)
  174. if node.name ~= "ignore" then
  175. if node.name ~= "air" and node.name ~= "bones:bones" and
  176. node.name ~= "bedrock:bedrock" then
  177. -- Only nodes not defined ans unbreakable.
  178. if minetest.get_item_group(node.name, "unbreakable") == 0 then
  179. minetest.remove_node(pos)
  180. end
  181. end
  182. end
  183. end
  184. end
  185. end
  186. end
  187. function teleports.kill_players_at_pos(teleport_pos, pname)
  188. local dead_players = minetest.get_objects_inside_radius({x=teleport_pos.x, y=teleport_pos.y+1, z=teleport_pos.z}, 2)
  189. for k, v in ipairs(dead_players) do
  190. if v and v:is_player() then
  191. if not gdac.player_is_admin(v) and v:get_player_name() ~= pname then -- Don't kill admin or self (can happen due to lag).
  192. -- Only if player isn't already dead.
  193. if v:get_hp() > 0 then
  194. -- If there's a player here already the map must be loaded, so we
  195. -- can put fire where they're standing no problem.
  196. local dp = vector_round(v:get_pos())
  197. local node = minetest.get_node(dp)
  198. if node.name == "air" then
  199. minetest.add_node(dp, {name="fire:basic_flame"})
  200. end
  201. -- Kill player absolutely dead. Bypass armor processing.
  202. v:set_hp(0, {reason="portal"})
  203. minetest.chat_send_all("# Server: <" .. rename.gpn(v:get_player_name()) .. "> was killed by a teleport. Noob!")
  204. end
  205. end
  206. end
  207. end
  208. end
  209. -- Calculates the probability scalar of a TP to misjump based on range vs max range.
  210. local function tpc(r1, r2)
  211. if r1 > r2 then return 0 end
  212. local d = r1 / r2
  213. d = d * -1 + 1
  214. for i=1, 10, 1 do
  215. d = d * 1.719 + 1
  216. d = math.log(d)
  217. end
  218. if d < 0 then d = 0 end
  219. if d > 1 then d = 1 end
  220. return d
  221. end
  222. teleports.teleport_player = function(player, origin_pos, teleport_pos, teleport_range)
  223. if not player or not player:is_player() then
  224. return
  225. end
  226. local pname = player:get_player_name()
  227. if sheriff.is_cheater(pname) then
  228. if sheriff.punish_probability(pname) then
  229. sheriff.punish_player(pname)
  230. return
  231. end
  232. end
  233. local player_pos = player:get_pos()
  234. if (player_pos.y < origin_pos.y) or (vector_distance(player_pos, origin_pos) > 2) then
  235. minetest.chat_send_player(pname, "# Server: You must stand on portal activation surface!")
  236. return
  237. end
  238. -- Small chance to be teleported somewhere completely random.
  239. -- The chance increases a LOT if teleports are crowded.
  240. -- You could theorize that their signals interfere with each other.
  241. local use_random = false
  242. local random_chance = 1030 -- Actually 1000, because nearby-count is always at least 1 (counting self).
  243. local count_nearby = 0
  244. -- Count number of nearby teleports (including self).
  245. for k, v in ipairs(teleports.teleports) do
  246. if vector_distance(v.pos, origin_pos) < 100 then
  247. count_nearby = count_nearby + 1
  248. end
  249. end
  250. -- Chance of misjump increases if teleports are crowded.
  251. random_chance = random_chance - (count_nearby * 30)
  252. if random_chance < 0 then random_chance = 0 end
  253. -- Chance of misjump increases as teleport is operated closer to its max range.
  254. local teleport_distance = vector_distance(origin_pos, teleport_pos)
  255. random_chance = random_chance * tpc(teleport_distance, teleport_range)
  256. -- Chance should never be worse than 1 in 50.
  257. if random_chance < 50 then
  258. random_chance = 50
  259. end
  260. random_chance = math_floor(random_chance)
  261. --minetest.chat_send_all('chance: ' .. random_chance)
  262. if math_random(1, random_chance) == 1 then
  263. local tp = teleports.get_random_teleport(origin_pos)
  264. if not tp then
  265. minetest.chat_send_player(pname, "# Server: Transport error! Aborted.")
  266. return
  267. end
  268. teleport_pos = tp.pos
  269. use_random = true
  270. end
  271. local p = vector_round(teleport_pos)
  272. local minp = {x=p.x-1, y=p.y+1, z=p.z-1}
  273. local maxp = {x=p.x+1, y=p.y+3, z=p.z+1}
  274. local target = {x=p.x-1+math_random(0, 2), y=p.y+1, z=p.z-1+math_random(0, 2)}
  275. local pos = vector_round(target)
  276. local start_realm = rc.current_realm_at_pos(origin_pos)
  277. local target_realm = rc.current_realm_at_pos(pos)
  278. if target_realm == "" or start_realm == "" or start_realm ~= target_realm then
  279. minetest.chat_send_player(pname, "# Server: Target location is in a different realm! Aborting.")
  280. return
  281. end
  282. minetest.log("action", "[teleports] teleporting player <" .. pname .. "> to " .. minetest.pos_to_string(pos))
  283. -- Teleport player to chosen location.
  284. preload_tp.execute({
  285. player_name = pname,
  286. target_position = pos,
  287. send_blocks = true,
  288. particle_effects = true,
  289. pre_teleport_callback = function()
  290. -- Kill players standing on target teleport pad.
  291. teleports.kill_players_at_pos(teleport_pos, pname)
  292. -- Delete 3x3x3 area above teleport.
  293. -- Do it again to prevent possible exploit.
  294. teleports.clear_area(minp, maxp)
  295. end,
  296. on_map_loaded = function()
  297. -- Delete 3x3x3 area above teleport.
  298. teleports.clear_area(minp, maxp)
  299. end,
  300. post_teleport_callback = function()
  301. portal_sickness.on_use_portal(pname)
  302. if use_random then
  303. minetest.after(10, function()
  304. local RED = core.get_color_escape_sequence("#ff0000")
  305. minetest.chat_send_player(pname, RED .. "# Server: Coordinate translation error. Unknown destination!")
  306. chat_core.alert_player_sound(pname)
  307. end)
  308. end
  309. end,
  310. })
  311. teleports.ping_all_teleports(origin_pos, player)
  312. end
  313. -- Find N nearest teleports.
  314. teleports.find_nearby = function(pos, count, network, yespublic)
  315. local nearby = {}
  316. local trange, isnyan = teleports.calculate_range(pos)
  317. local start_realm = rc.current_realm_at_pos(pos)
  318. if start_realm == "" then
  319. return nearby
  320. end
  321. -- Why am I iterating backwards here?
  322. for i = #teleports.teleports, 1, -1 do
  323. local tp = teleports.teleports[i]
  324. if not vector_equals(tp.pos, pos) and vector_distance(tp.pos, pos) <= trange then
  325. local target_realm = rc.current_realm_at_pos(tp.pos)
  326. -- Only find teleports in the same dimension.
  327. if start_realm == target_realm then
  328. local othernet = tp.channel or ""
  329. if othernet == network or (othernet == "" and yespublic == 'true') then
  330. nearby[#nearby + 1] = tp
  331. end
  332. end
  333. end
  334. end
  335. -- Sort blocks, nearest blocks first.
  336. table.sort(nearby,
  337. function(a, b)
  338. local d1 = vector_distance(a.pos, pos)
  339. local d2 = vector_distance(b.pos, pos)
  340. return d1 < d2
  341. end)
  342. -- Return N-nearest blocks (should be at the front of the sorted table).
  343. local ret = {}
  344. for i = 1, count, 1 do
  345. if i <= #nearby then
  346. ret[#ret + 1] = nearby[i]
  347. else
  348. break
  349. end
  350. end
  351. return ret
  352. end
  353. teleports.find_specific = function(pos)
  354. for i = 1, #teleports.teleports, 1 do
  355. local tp = teleports.teleports[i]
  356. if vector_equals(tp.pos, pos) then
  357. return i -- Return index of teleport.
  358. end
  359. end
  360. end
  361. teleports.calculate_charge = function(pos)
  362. local positions = {
  363. {x=pos.x-1, y=pos.y, z=pos.z},
  364. {x=pos.x+1, y=pos.y, z=pos.z},
  365. {x=pos.x, y=pos.y, z=pos.z-1},
  366. {x=pos.x, y=pos.y, z=pos.z+1},
  367. {x=pos.x-1, y=pos.y, z=pos.z-1},
  368. {x=pos.x+1, y=pos.y, z=pos.z+1},
  369. {x=pos.x-1, y=pos.y, z=pos.z+1},
  370. {x=pos.x+1, y=pos.y, z=pos.z-1},
  371. }
  372. local bows = 0
  373. local charge = 1 -- Ambient charge is at least 1 (the teleport block provides 1 KJ).
  374. for k, v in ipairs(positions) do
  375. local n = minetest.get_node(v).name
  376. local c = 0
  377. if teleports.charge_blocks[n] ~= nil then
  378. c = teleports.charge_blocks[n].charge
  379. end
  380. charge = charge + c
  381. if n == nyanbow then
  382. bows = bows + 1
  383. end
  384. end
  385. local is_nyanporter = false
  386. if bows == 8 then
  387. is_nyanporter = true
  388. end
  389. charge = math_floor(charge + 0.5)
  390. -- Nyan teleports get a fixed charge which should result in 7770 range after
  391. -- combined with 'inc = 25' variable.
  392. if is_nyanporter then
  393. charge = 310.8
  394. end
  395. -- Combined teleports interfere with each other and reduce their range.
  396. local minp = vector.add(pos, {x=-2, y=0, z=-2})
  397. local maxp = vector.add(pos, {x=2, y=0, z=2})
  398. local others = minetest.find_nodes_in_area(minp, maxp, "teleports:teleport")
  399. -- Range of teleports is reduced if they're crowded.
  400. local other_count = 1
  401. if others and #others > 0 then
  402. charge = charge / #others
  403. other_count = #others
  404. end
  405. return charge, other_count, is_nyanporter
  406. end
  407. -- Smoothly scale teleport range based on depth in the overworld.
  408. local function cds(pos, nyan)
  409. local y = pos.y
  410. local scalar = 1
  411. local realm = rc.current_realm_at_pos(pos)
  412. -- Note: 'nyan' parameter is for backward compatibility.
  413. -- From Overworld surface to nether base.
  414. if realm == "overworld" and not nyan then
  415. -- You can probably tell that I'm really, really bad at math.
  416. local depth = math.abs(y)
  417. scalar = depth / 30912
  418. -- Clamp.
  419. if scalar > 1 then scalar = 1 end
  420. if scalar < 0 then scalar = 0 end
  421. -- Invert.
  422. scalar = (scalar * -1) + 1
  423. -- Magic!
  424. -- The number of iterations determines the steepness of the curve.
  425. for i=1, 5, 1 do
  426. scalar = scalar * 1.719
  427. scalar = scalar + 1
  428. -- Input to log should be [1, 2.719].
  429. -- Log should return something in range [0, 1].
  430. scalar = math.log(scalar)
  431. end
  432. -- Clamp.
  433. if scalar > 1 then scalar = 1 end
  434. if scalar < 0 then scalar = 0 end
  435. elseif realm == "naraxen" then
  436. if os.time() >= os.time({month=1,day=1,year=2024}) then
  437. scalar = 0.1
  438. end
  439. end
  440. return scalar
  441. end
  442. teleports.calculate_range = function(pos)
  443. -- Compute charge.
  444. local meta = minetest.get_meta(pos)
  445. local chg, other_cnt, nyan = teleports.calculate_charge(pos)
  446. if nyan then
  447. local owner = meta:get_string("owner")
  448. -- There is an admin teleport pair between the Surface Colony and the City of Fire.
  449. -- This special exception code makes it work.
  450. if owner == gdac.name_of_admin then
  451. return 31000, nyan
  452. end
  453. end
  454. -- How much distance each unit of charge is good for.
  455. local inc = 25
  456. -- Compute extra range.
  457. local rng = math_floor(inc * chg)
  458. -- Calculate how much to scale extra range by depth.
  459. local is_nyan = nyan
  460. -- For new teleports, we no longer care if they're nyan for purposes of range
  461. -- calculation.
  462. if meta:get_int("construction_time") ~= 0 then
  463. is_nyan = false
  464. end
  465. local scalar = cds(pos, is_nyan)
  466. -- Scale extra range by depth.
  467. rng = rng * scalar
  468. -- Teleport range shall not go below 250 meters.
  469. rng = math.max(rng, 250)
  470. return math_floor(rng), nyan
  471. end
  472. function teleports.write_infotext(pos)
  473. local meta = minetest.get_meta(pos)
  474. local inv = meta:get_inventory()
  475. local name = meta:get_string("name")
  476. local network = meta:get_string("network")
  477. local owner = meta:get_string("owner")
  478. local dname = rename.gpn(owner)
  479. local public = meta:get_int("public") or 1
  480. if public == 1 then public = 'true' else public = 'false' end
  481. local id = "<" .. name .. ">"
  482. local net = "<" .. network .. ">"
  483. local own = "<" .. dname .. ">"
  484. if name == "" then id = "NONE" end
  485. if network == "" then net = "PUBLIC" end
  486. if public == 'false' then net = "SUPPRESSED" end
  487. local beacon = ""
  488. local item = {name="rosestone:head", count=1, wear=0, metadata=""}
  489. if inv:contains_item("price", item) then
  490. beacon = "\nRecall signal emission normal"
  491. end
  492. meta:set_string("infotext", "Teleporter. Punch to update controls.\nOwner: " .. own .. "\nBeacon ID: " .. id .. "\nBeacon Channel: " .. net .. beacon)
  493. end
  494. teleports.update = function(pos)
  495. local meta = minetest.get_meta(pos)
  496. local network = meta:get_string("network") or ""
  497. local owner = meta:get_string("owner") or ""
  498. local name = meta:get_string("name") or ""
  499. local yespublic = meta:get_string("yespublic") or 'true'
  500. local buttons = "";
  501. local nearby = teleports.find_nearby(pos, 10, network, yespublic)
  502. local button_x = 8
  503. local button_y = 1
  504. for i, v in ipairs(nearby) do
  505. local tp = v.pos
  506. local data = tp.x .. "," .. tp.y .. "," .. tp.z
  507. local real_label = rc.pos_to_string(tp)
  508. meta:set_string("loc" .. (i), data)
  509. if v.name ~= nil then
  510. if v.name ~= "" then
  511. real_label = v.name
  512. end
  513. end
  514. buttons = buttons ..
  515. "button_exit[" .. button_x .. "," .. button_y ..
  516. ";3,0.5;tp" .. i .. ";" .. minetest.formspec_escape(real_label) .. "]";
  517. button_y = button_y + 1
  518. if button_y >= 6 then
  519. button_y = 1
  520. button_x = 5
  521. end
  522. end
  523. local public = meta:get_int("public") or 1
  524. if public == 1 then public = 'true' else public = 'false' end
  525. teleports.write_infotext(pos)
  526. local net = "<" .. network .. ">"
  527. local nm = "<" .. name .. ">"
  528. if name == "" then nm = "NONE" end
  529. if network == "" then net = "PUBLIC" end
  530. if public == 'false' then net = "SUPPRESSED" end
  531. local charge, count, isnyan = teleports.calculate_charge(pos)
  532. local range = teleports.calculate_range(pos)
  533. if isnyan then
  534. charge = "ROSE"
  535. end
  536. local formspec = "size[11,7;]" ..
  537. default.gui_bg ..
  538. default.gui_bg_img ..
  539. default.gui_slots ..
  540. "label[0,0;" .. 'Transport to nearby beacons! Need mossy cobble for energy.' .. "]" ..
  541. "label[1,0.70;Beacon ID: " .. minetest.formspec_escape(nm) .. "]" ..
  542. "label[1,1.2;Beacon Channel: " .. minetest.formspec_escape(net) .. "]" ..
  543. "field[0.3,2.7;2,0.5;id;Change Beacon ID;]" .. "button_exit[2,2.4;2,0.5;change_id;Confirm]" ..
  544. "field[0.3,3.9;2,0.5;network;Change Channel;]" .. "button_exit[2,3.6;2,0.5;change_network;Confirm]" ..
  545. buttons ..
  546. "button_exit[0,5.2;2,0.5;cancel;Close]" ..
  547. "checkbox[0.02,4.1;showhide;Show Channel;" .. public .. "]" ..
  548. "checkbox[2,4.1;yespublic;Connect Public;" .. yespublic .. "]" ..
  549. "label[2,5;Ambient Charge: " .. charge .. " KJ]" ..
  550. "label[2,5.35;Transport Range: " .. range .. " M]" ..
  551. "list[context;price;0,0.75;1,1;]" ..
  552. "list[current_player;main;0,6;11,1;]" ..
  553. "listring[]"
  554. meta:set_string("formspec", formspec)
  555. end
  556. teleports.on_receive_fields = function(pos, formname, fields, player)
  557. if not player then return end
  558. if not player:is_player() then return end
  559. if player:get_hp() <= 0 then return end -- Ignore dead players.
  560. local playername = player:get_player_name()
  561. local meta = minetest.get_meta(pos)
  562. local isnyan = teleports.is_nyanbow_teleport(pos)
  563. local owner = meta:get_string("owner") or ""
  564. local infinite_fuel = false
  565. if owner == "MustTest" then
  566. infinite_fuel = true
  567. else
  568. local inv = meta:get_inventory()
  569. local item = {name="rosestone:head", count=1, wear=0, metadata=""}
  570. if inv:contains_item("price", item) then
  571. infinite_fuel = true
  572. end
  573. end
  574. local admin = minetest.check_player_privs(playername, {server=true})
  575. local needsave = false
  576. -- Make sure this teleport, at this postion, has an entry.
  577. local tp_idx = teleports.find_specific(pos)
  578. if not tp_idx then
  579. minetest.chat_send_player(playername, "# Server: Transporter data error: 0xDEADBEEF.")
  580. easyvend.sound_error(playername)
  581. return
  582. end
  583. if not teleports.teleports[tp_idx] then
  584. minetest.chat_send_player(playername, "# Server: Transporter data error: 0xDEADBEEF.")
  585. easyvend.sound_error(playername)
  586. return
  587. end
  588. if fields.showhide then
  589. if owner == playername or admin then
  590. if fields.showhide == "true" then
  591. meta:set_int("public", 1)
  592. else
  593. meta:set_int("public", 0)
  594. end
  595. else
  596. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  597. easyvend.sound_error(playername)
  598. end
  599. end
  600. if fields.yespublic then
  601. if owner == playername or admin then
  602. if fields.yespublic == "true" then
  603. meta:set_string("yespublic", 'true')
  604. else
  605. meta:set_string("yespublic", 'false')
  606. end
  607. else
  608. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  609. easyvend.sound_error(playername)
  610. end
  611. end
  612. if fields.change_id and fields.id then
  613. if owner == playername or admin then
  614. meta:set_string("name", fields.id)
  615. teleports.teleports[tp_idx].name = fields.id
  616. needsave = true
  617. else
  618. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  619. easyvend.sound_error(playername)
  620. end
  621. end
  622. if fields.change_network and fields.network then
  623. if owner == playername or admin then
  624. meta:set_string("network", fields.network)
  625. teleports.teleports[tp_idx].channel = fields.network
  626. needsave = true
  627. else
  628. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  629. easyvend.sound_error(playername)
  630. end
  631. end
  632. if needsave == true then
  633. teleports.save()
  634. end
  635. local pressed_tp_button = false
  636. local pressed_tp_location
  637. for i = 1, 10, 1 do
  638. -- According to button names/data set in the machine update function.
  639. local btnname = "tp" .. i
  640. local posname = "loc" .. i
  641. if fields[btnname] then
  642. pressed_tp_button = true
  643. pressed_tp_location = meta:get_string(posname)
  644. break
  645. end
  646. end
  647. if pressed_tp_button then
  648. local have_biofuel = false
  649. local tpname = pressed_tp_location
  650. local have_target = false
  651. local target_pos = {x=0, y=0, z=0}
  652. local teleport_range = nil
  653. if tpname and type(tpname) == "string" then
  654. local tppos = minetest.string_to_pos(tpname)
  655. if tppos then
  656. teleport_range = teleports.calculate_range(pos)
  657. if vector_distance(tppos, pos) <= teleport_range then
  658. -- Do not permit teleporting from one realm to another.
  659. -- Doing so requires a different kind of teleport device.
  660. local start_realm = rc.current_realm_at_pos(pos)
  661. local target_realm = rc.current_realm_at_pos(tppos)
  662. if start_realm ~= "" and start_realm == target_realm then
  663. local exists = false
  664. for i = 1, #teleports.teleports, 1 do
  665. local tp = teleports.teleports[i]
  666. if vector_equals(tp.pos, tppos) then
  667. exists = true
  668. break
  669. end
  670. end
  671. if exists then
  672. have_target = true
  673. target_pos = tppos
  674. else
  675. minetest.chat_send_player(playername, "# Server: Transport control error: target no longer exists.")
  676. easyvend.sound_error(playername)
  677. end
  678. else
  679. minetest.chat_send_player(playername, "# Server: Cannot teleport between realm boundaries!")
  680. easyvend.sound_error(playername)
  681. end
  682. else
  683. minetest.chat_send_player(playername, "# Server: Transport control error: target out of range!")
  684. easyvend.sound_error(playername)
  685. end
  686. else
  687. minetest.chat_send_player(playername, "# Server: Transport control error: 0xDEADBEEF.")
  688. easyvend.sound_error(playername)
  689. end
  690. else
  691. minetest.chat_send_player(playername, "# Server: Transport control error: formspec.")
  692. easyvend.sound_error(playername)
  693. end
  694. if have_target == true then -- Don't use fuel unless a valid target is found.
  695. local inv = meta:get_inventory();
  696. if not admin and not infinite_fuel then -- Don't do fuel calculation if admin is using teleport.
  697. -- Cost is 1 item of fuel per 300 meters.
  698. -- This means players save on fuel when using long range teleports,
  699. -- instead of using a chain of short-range teleports.
  700. -- However, long range teleports cost more to make.
  701. local rcost = math_floor(vector_distance(pos, target_pos) / 300)
  702. if isnyan then
  703. -- Nyan teleports have much greater fuel efficiency.
  704. rcost = math_floor(vector_distance(pos, target_pos) / 600)
  705. end
  706. if rcost < 1 then rcost = 1 end
  707. -- If using lilies as fuel, fewer are required.
  708. -- Lilies are bit harder to get.
  709. local lcost = math_floor(rcost * 0.5)
  710. if lcost < 1 then lcost = 1 end
  711. local price1 = {name="default:mossycobble", count=rcost, wear=0, metadata=""}
  712. local price2 = {name="flowers:waterlily", count=lcost, wear=0, metadata=""}
  713. if not inv:is_empty("price") then
  714. if inv:contains_item("price", price1) then
  715. inv:remove_item("price", price1)
  716. have_biofuel = true
  717. elseif inv:contains_item("price", price2) then
  718. inv:remove_item("price", price2)
  719. have_biofuel = true
  720. else
  721. minetest.chat_send_player(playername, "# Server: Insufficient stored energy for transport. Add more biofuel.")
  722. easyvend.sound_error(playername)
  723. end
  724. else
  725. minetest.chat_send_player(playername, "# Server: Transporter is on maintenance energy only. Add biofuel to use.")
  726. easyvend.sound_error(playername)
  727. end
  728. end
  729. if have_biofuel or admin or infinite_fuel then
  730. local teleport_pos = {x=target_pos.x, y=target_pos.y, z=target_pos.z}
  731. teleports.teleport_player(player, pos, teleport_pos, teleport_range)
  732. end
  733. end
  734. end
  735. -- Always update the teleport formspec.
  736. teleports.update(pos)
  737. end
  738. teleports.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  739. local pname = player:get_player_name()
  740. -- Protection interferes with building public networks.
  741. --if minetest.test_protection(pos, pname) then return 0 end
  742. if listname == "price" and stack:get_name() == "default:mossycobble" then
  743. return stack:get_count()
  744. elseif listname == "price" and stack:get_name() == "flowers:waterlily" then
  745. return stack:get_count()
  746. elseif listname == "price" and stack:get_name() == "rosestone:head" then
  747. if minetest.test_protection(pos, pname) then return 0 end
  748. return stack:get_count()
  749. end
  750. return 0
  751. end
  752. teleports.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  753. local pname = player:get_player_name()
  754. -- Protection interferes with building public networks.
  755. --if minetest.test_protection(pos, pname) then return 0 end
  756. if stack:get_name() == "rosestone:head" then
  757. if minetest.test_protection(pos, pname) then return 0 end
  758. return stack:get_count()
  759. end
  760. return stack:get_count()
  761. end
  762. teleports.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  763. return 0
  764. end
  765. function teleports.update_beacon_data(pos)
  766. local meta = minetest.get_meta(pos)
  767. local inv = meta:get_inventory()
  768. local item = {name="rosestone:head", count=1, wear=0, metadata=""}
  769. if inv:contains_item("price", item) then
  770. for k, v in ipairs(teleports.teleports) do
  771. if vector_equals(v.pos, pos) then
  772. if not v.is_recall then
  773. v.is_recall = true
  774. teleports.save()
  775. end
  776. end
  777. end
  778. else
  779. for k, v in ipairs(teleports.teleports) do
  780. if vector_equals(v.pos, pos) then
  781. if v.is_recall then
  782. v.is_recall = nil
  783. teleports.save()
  784. end
  785. end
  786. end
  787. end
  788. end
  789. function teleports.on_metadata_inventory_put(pos, listname, index, stack, player)
  790. teleports.update_beacon_data(pos)
  791. end
  792. function teleports.on_metadata_inventory_take(pos, listname, index, stack, player)
  793. teleports.update_beacon_data(pos)
  794. end
  795. teleports.can_dig = function(pos)
  796. local meta = minetest.get_meta(pos)
  797. local inv = meta:get_inventory()
  798. return inv:is_empty("price")
  799. end
  800. teleports.after_place_node = function(pos, placer)
  801. if placer and placer:is_player() then
  802. local meta = minetest.get_meta(pos)
  803. local pname = placer:get_player_name()
  804. local dname = rename.gpn(pname)
  805. meta:set_string("owner", pname)
  806. meta:set_string("rename", dname)
  807. meta:set_string("name", "")
  808. meta:set_string("network", "")
  809. meta:set_int("public", 1)
  810. meta:set_int("construction_time", os.time())
  811. local inv = meta:get_inventory()
  812. inv:set_size("price", 1)
  813. local initialcharge = {name="default:mossycobble", count=30, wear=0, metadata=""}
  814. inv:add_item("price", initialcharge)
  815. teleports.update(pos)
  816. table.insert(teleports.teleports, {pos=vector_round(pos)})
  817. teleports.save()
  818. end
  819. end
  820. teleports.on_destruct = function(pos)
  821. --minetest.chat_send_all("# Server: Destructing teleport!")
  822. for i, EachTeleport in ipairs(teleports.teleports) do
  823. if vector_equals(EachTeleport.pos, pos) then
  824. table.remove(teleports.teleports, i)
  825. teleports.save()
  826. end
  827. end
  828. end
  829. function teleports.ping_all_teleports(origin_pos, initiating_player)
  830. local players = minetest.get_connected_players()
  831. local start_realm = rc.current_realm_at_pos(origin_pos)
  832. local ping = function(pos)
  833. local xd = 1
  834. local zd = 1
  835. minetest.add_particlespawner({
  836. amount = 80,
  837. time = 5,
  838. minpos = {x=pos.x-xd, y=pos.y+1, z=pos.z-zd},
  839. maxpos = {x=pos.x+xd, y=pos.y+3, z=pos.z+zd},
  840. minvel = {x=-1, y=-1, z=-1},
  841. maxvel = {x=1, y=1, z=1},
  842. minacc = {x=-1, y=-1, z=-1},
  843. maxacc = {x=1, y=1, z=1},
  844. minexptime = 0.5,
  845. maxexptime = 1.5,
  846. minsize = 1,
  847. maxsize = 1.5,
  848. collisiondetection = false,
  849. texture = "nether_particle_anim4.png",
  850. animation = {
  851. type = "vertical_frames",
  852. aspect_w = 7,
  853. aspect_h = 7,
  854. -- Disabled for now due to causing older clients to hang.
  855. --length = -1,
  856. length = 0.3,
  857. },
  858. glow = 14,
  859. })
  860. end
  861. -- Spawn particles over every teleport that's near a player.
  862. local ports = teleports.teleports
  863. local tlen = #ports
  864. local plen = #players
  865. for k = 1, tlen, 1 do
  866. local porthub = ports[k]
  867. local portpos = porthub.pos
  868. for i = 1, plen, 1 do
  869. local pref = players[i]
  870. local playerpos = pref:get_pos()
  871. local dist = vector_distance(portpos, playerpos)
  872. -- Don't add particles for the initiating player above the teleport they
  873. -- are actually using (but spawn particles for them over any nearby).
  874. if dist < 32 and (pref ~= initiating_player or dist > 2) then
  875. local tp_realm = rc.current_realm_at_pos(portpos)
  876. if tp_realm == start_realm then
  877. ping(portpos)
  878. if math_random(1, 500) == 1 then
  879. minetest.after(math_random(1, 5), function()
  880. pm.spawn_random_wisp(vector_add(portpos, {x=0, y=1, z=0}))
  881. end)
  882. end
  883. end
  884. end
  885. end
  886. end
  887. end
  888. teleports.on_punch = function(pos, node, puncher, pointed_thing)
  889. teleports.update(pos)
  890. -- Maybe this is a bit too spammy and generally unnecessary?
  891. if puncher and puncher:is_player() then
  892. minetest.chat_send_player(puncher:get_player_name(), "# Server: This machine has been updated.")
  893. end
  894. end
  895. teleports.on_diamond_place = function(itemstack, placer, pointed_thing)
  896. local stack = ItemStack("default:diamondblock")
  897. local pos = pointed_thing.above
  898. local name = "default:diamondblock"
  899. if minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z}).name == name and
  900. minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z+1}).name == name and
  901. minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z-1}).name == name and
  902. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z}).name == name and
  903. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z+1}).name == name and
  904. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z-1}).name == name and
  905. minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1}).name == name and
  906. minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1}).name == name
  907. then
  908. stack = ItemStack("teleports:teleport")
  909. end
  910. local ret = minetest.item_place(stack, placer, pointed_thing)
  911. if ret == nil then
  912. return itemstack
  913. else
  914. return ItemStack("default:diamondblock " .. itemstack:get_count() - (1 - ret:get_count()))
  915. end
  916. end
  917. -- Admin API function, refills ALL teleports with fuel (if fuel is empty).
  918. function teleports.refill_all()
  919. local tps = teleports.teleports
  920. for k, v in ipairs(tps) do
  921. local meta = minetest.get_meta(v.pos)
  922. if meta then
  923. local inv = meta:get_inventory()
  924. if inv then
  925. if inv:is_empty("price") then
  926. inv:set_stack("price", 1, ItemStack("flowers:waterlily 64"))
  927. else
  928. local stack = inv:get_stack("price", 1)
  929. if stack:get_name() == "default:mossycobble" then
  930. stack:set_count(64)
  931. inv:set_stack("price", 1, stack)
  932. elseif stack:get_name() == "flowers:waterlily" then
  933. stack:set_count(64)
  934. inv:set_stack("price", 1, stack)
  935. end
  936. end
  937. end
  938. end
  939. end
  940. end