functions.lua 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. teleports.clear_area(minp, maxp)
  294. end,
  295. post_teleport_callback = function()
  296. portal_sickness.on_use_portal(pname)
  297. if use_random then
  298. minetest.after(10, function()
  299. local RED = core.get_color_escape_sequence("#ff0000")
  300. minetest.chat_send_player(pname, RED .. "# Server: Coordinate translation error. Unknown destination!")
  301. chat_core.alert_player_sound(pname)
  302. end)
  303. end
  304. end,
  305. })
  306. teleports.ping_all_teleports(origin_pos, player)
  307. end
  308. -- Find N nearest teleports.
  309. teleports.find_nearby = function(pos, count, network, yespublic)
  310. local nearby = {}
  311. local trange, isnyan = teleports.calculate_range(pos)
  312. local start_realm = rc.current_realm_at_pos(pos)
  313. if start_realm == "" then
  314. return nearby
  315. end
  316. -- Why am I iterating backwards here?
  317. for i = #teleports.teleports, 1, -1 do
  318. local tp = teleports.teleports[i]
  319. if not vector_equals(tp.pos, pos) and vector_distance(tp.pos, pos) <= trange then
  320. local target_realm = rc.current_realm_at_pos(tp.pos)
  321. -- Only find teleports in the same dimension.
  322. if start_realm == target_realm then
  323. local othernet = tp.channel or ""
  324. if othernet == network or (othernet == "" and yespublic == 'true') then
  325. nearby[#nearby + 1] = tp
  326. end
  327. end
  328. end
  329. end
  330. -- Sort blocks, nearest blocks first.
  331. table.sort(nearby,
  332. function(a, b)
  333. local d1 = vector_distance(a.pos, pos)
  334. local d2 = vector_distance(b.pos, pos)
  335. return d1 < d2
  336. end)
  337. -- Return N-nearest blocks (should be at the front of the sorted table).
  338. local ret = {}
  339. for i = 1, count, 1 do
  340. if i <= #nearby then
  341. ret[#ret + 1] = nearby[i]
  342. else
  343. break
  344. end
  345. end
  346. return ret
  347. end
  348. teleports.find_specific = function(pos)
  349. for i = 1, #teleports.teleports, 1 do
  350. local tp = teleports.teleports[i]
  351. if vector_equals(tp.pos, pos) then
  352. return i -- Return index of teleport.
  353. end
  354. end
  355. end
  356. teleports.calculate_charge = function(pos)
  357. local positions = {
  358. {x=pos.x-1, y=pos.y, z=pos.z},
  359. {x=pos.x+1, y=pos.y, z=pos.z},
  360. {x=pos.x, y=pos.y, z=pos.z-1},
  361. {x=pos.x, y=pos.y, z=pos.z+1},
  362. {x=pos.x-1, y=pos.y, z=pos.z-1},
  363. {x=pos.x+1, y=pos.y, z=pos.z+1},
  364. {x=pos.x-1, y=pos.y, z=pos.z+1},
  365. {x=pos.x+1, y=pos.y, z=pos.z-1},
  366. }
  367. local bows = 0
  368. local charge = 1 -- Ambient charge is at least 1 (the teleport block provides 1 KJ).
  369. for k, v in ipairs(positions) do
  370. local n = minetest.get_node(v).name
  371. local c = 0
  372. if teleports.charge_blocks[n] ~= nil then
  373. c = teleports.charge_blocks[n].charge
  374. end
  375. charge = charge + c
  376. if n == nyanbow then
  377. bows = bows + 1
  378. end
  379. end
  380. local is_nyanporter = false
  381. if bows == 8 then
  382. is_nyanporter = true
  383. end
  384. charge = math_floor(charge + 0.5)
  385. -- Nyan teleports get a fixed charge which should result in 7770 range after
  386. -- combined with 'inc = 25' variable.
  387. if is_nyanporter then
  388. charge = 310.8
  389. end
  390. -- Combined teleports interfere with each other and reduce their range.
  391. local minp = vector.add(pos, {x=-2, y=0, z=-2})
  392. local maxp = vector.add(pos, {x=2, y=0, z=2})
  393. local others = minetest.find_nodes_in_area(minp, maxp, "teleports:teleport")
  394. -- Range of teleports is reduced if they're crowded.
  395. local other_count = 1
  396. if others and #others > 0 then
  397. charge = charge / #others
  398. other_count = #others
  399. end
  400. return charge, other_count, is_nyanporter
  401. end
  402. -- Smoothly scale teleport range based on depth in the overworld.
  403. local function cds(pos, nyan)
  404. local y = pos.y
  405. local scalar = 1
  406. local realm = rc.current_realm_at_pos(pos)
  407. -- Note: 'nyan' parameter is for backward compatibility.
  408. -- From Overworld surface to nether base.
  409. if realm == "overworld" and not nyan then
  410. -- You can probably tell that I'm really, really bad at math.
  411. local depth = math.abs(y)
  412. scalar = depth / 30912
  413. -- Clamp.
  414. if scalar > 1 then scalar = 1 end
  415. if scalar < 0 then scalar = 0 end
  416. -- Invert.
  417. scalar = (scalar * -1) + 1
  418. -- Magic!
  419. -- The number of iterations determines the steepness of the curve.
  420. for i=1, 5, 1 do
  421. scalar = scalar * 1.719
  422. scalar = scalar + 1
  423. -- Input to log should be [1, 2.719].
  424. -- Log should return something in range [0, 1].
  425. scalar = math.log(scalar)
  426. end
  427. -- Clamp.
  428. if scalar > 1 then scalar = 1 end
  429. if scalar < 0 then scalar = 0 end
  430. elseif realm == "naraxen" then
  431. if os.time() >= os.time({month=1,day=1,year=2024}) then
  432. scalar = 0.1
  433. end
  434. end
  435. return scalar
  436. end
  437. teleports.calculate_range = function(pos)
  438. -- Compute charge.
  439. local meta = minetest.get_meta(pos)
  440. local chg, other_cnt, nyan = teleports.calculate_charge(pos)
  441. if nyan then
  442. local owner = meta:get_string("owner")
  443. -- There is an admin teleport pair between the Surface Colony and the City of Fire.
  444. -- This special exception code makes it work.
  445. if owner == gdac.name_of_admin then
  446. return 31000, nyan
  447. end
  448. end
  449. -- How much distance each unit of charge is good for.
  450. local inc = 25
  451. -- Compute extra range.
  452. local rng = math_floor(inc * chg)
  453. -- Calculate how much to scale extra range by depth.
  454. local is_nyan = nyan
  455. -- For new teleports, we no longer care if they're nyan for purposes of range
  456. -- calculation.
  457. if meta:get_int("construction_time") ~= 0 then
  458. is_nyan = false
  459. end
  460. local scalar = cds(pos, is_nyan)
  461. -- Scale extra range by depth.
  462. rng = rng * scalar
  463. -- Teleport range shall not go below 250 meters.
  464. rng = math.max(rng, 250)
  465. return math_floor(rng), nyan
  466. end
  467. function teleports.write_infotext(pos)
  468. local meta = minetest.get_meta(pos)
  469. local inv = meta:get_inventory()
  470. local name = meta:get_string("name")
  471. local network = meta:get_string("network")
  472. local owner = meta:get_string("owner")
  473. local dname = rename.gpn(owner)
  474. local public = meta:get_int("public") or 1
  475. if public == 1 then public = 'true' else public = 'false' end
  476. local id = "<" .. name .. ">"
  477. local net = "<" .. network .. ">"
  478. local own = "<" .. dname .. ">"
  479. if name == "" then id = "NONE" end
  480. if network == "" then net = "PUBLIC" end
  481. if public == 'false' then net = "SUPPRESSED" end
  482. local beacon = ""
  483. local item = {name="rosestone:head", count=1, wear=0, metadata=""}
  484. if inv:contains_item("price", item) then
  485. beacon = "\nRecall signal emission normal"
  486. end
  487. meta:set_string("infotext", "Teleporter. Punch to update controls.\nOwner: " .. own .. "\nBeacon ID: " .. id .. "\nBeacon Channel: " .. net .. beacon)
  488. end
  489. teleports.update = function(pos)
  490. local meta = minetest.get_meta(pos)
  491. local network = meta:get_string("network") or ""
  492. local owner = meta:get_string("owner") or ""
  493. local name = meta:get_string("name") or ""
  494. local yespublic = meta:get_string("yespublic") or 'true'
  495. local buttons = "";
  496. local nearby = teleports.find_nearby(pos, 10, network, yespublic)
  497. local button_x = 8
  498. local button_y = 1
  499. for i, v in ipairs(nearby) do
  500. local tp = v.pos
  501. local data = tp.x .. "," .. tp.y .. "," .. tp.z
  502. local real_label = rc.pos_to_string(tp)
  503. meta:set_string("loc" .. (i), data)
  504. if v.name ~= nil then
  505. if v.name ~= "" then
  506. real_label = v.name
  507. end
  508. end
  509. buttons = buttons ..
  510. "button_exit[" .. button_x .. "," .. button_y ..
  511. ";3,0.5;tp" .. i .. ";" .. minetest.formspec_escape(real_label) .. "]";
  512. button_y = button_y + 1
  513. if button_y >= 6 then
  514. button_y = 1
  515. button_x = 5
  516. end
  517. end
  518. local public = meta:get_int("public") or 1
  519. if public == 1 then public = 'true' else public = 'false' end
  520. teleports.write_infotext(pos)
  521. local net = "<" .. network .. ">"
  522. local nm = "<" .. name .. ">"
  523. if name == "" then nm = "NONE" end
  524. if network == "" then net = "PUBLIC" end
  525. if public == 'false' then net = "SUPPRESSED" end
  526. local charge, count, isnyan = teleports.calculate_charge(pos)
  527. local range = teleports.calculate_range(pos)
  528. if isnyan then
  529. charge = "ROSE"
  530. end
  531. local formspec = "size[11,7;]" ..
  532. default.gui_bg ..
  533. default.gui_bg_img ..
  534. default.gui_slots ..
  535. "label[0,0;" .. 'Transport to nearby beacons! Need mossy cobble for energy.' .. "]" ..
  536. "label[1,0.70;Beacon ID: " .. minetest.formspec_escape(nm) .. "]" ..
  537. "label[1,1.2;Beacon Channel: " .. minetest.formspec_escape(net) .. "]" ..
  538. "field[0.3,2.7;2,0.5;id;Change Beacon ID;]" .. "button_exit[2,2.4;2,0.5;change_id;Confirm]" ..
  539. "field[0.3,3.9;2,0.5;network;Change Channel;]" .. "button_exit[2,3.6;2,0.5;change_network;Confirm]" ..
  540. buttons ..
  541. "button_exit[0,5.2;2,0.5;cancel;Close]" ..
  542. "checkbox[0.02,4.1;showhide;Show Channel;" .. public .. "]" ..
  543. "checkbox[2,4.1;yespublic;Connect Public;" .. yespublic .. "]" ..
  544. "label[2,5;Ambient Charge: " .. charge .. " KJ]" ..
  545. "label[2,5.35;Transport Range: " .. range .. " M]" ..
  546. "list[context;price;0,0.75;1,1;]" ..
  547. "list[current_player;main;0,6;11,1;]" ..
  548. "listring[]"
  549. meta:set_string("formspec", formspec)
  550. end
  551. teleports.on_receive_fields = function(pos, formname, fields, player)
  552. if not player then return end
  553. if not player:is_player() then return end
  554. if player:get_hp() <= 0 then return end -- Ignore dead players.
  555. local playername = player:get_player_name()
  556. local meta = minetest.get_meta(pos)
  557. local isnyan = teleports.is_nyanbow_teleport(pos)
  558. local owner = meta:get_string("owner") or ""
  559. local infinite_fuel = false
  560. if owner == "MustTest" then
  561. infinite_fuel = true
  562. else
  563. local inv = meta:get_inventory()
  564. local item = {name="rosestone:head", count=1, wear=0, metadata=""}
  565. if inv:contains_item("price", item) then
  566. infinite_fuel = true
  567. end
  568. end
  569. local admin = minetest.check_player_privs(playername, {server=true})
  570. local needsave = false
  571. -- Make sure this teleport, at this postion, has an entry.
  572. local tp_idx = teleports.find_specific(pos)
  573. if not tp_idx then
  574. minetest.chat_send_player(playername, "# Server: Transporter data error: 0xDEADBEEF.")
  575. easyvend.sound_error(playername)
  576. return
  577. end
  578. if not teleports.teleports[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 fields.showhide then
  584. if owner == playername or admin then
  585. if fields.showhide == "true" then
  586. meta:set_int("public", 1)
  587. else
  588. meta:set_int("public", 0)
  589. end
  590. else
  591. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  592. easyvend.sound_error(playername)
  593. end
  594. end
  595. if fields.yespublic then
  596. if owner == playername or admin then
  597. if fields.yespublic == "true" then
  598. meta:set_string("yespublic", 'true')
  599. else
  600. meta:set_string("yespublic", 'false')
  601. end
  602. else
  603. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  604. easyvend.sound_error(playername)
  605. end
  606. end
  607. if fields.change_id and fields.id then
  608. if owner == playername or admin then
  609. meta:set_string("name", fields.id)
  610. teleports.teleports[tp_idx].name = fields.id
  611. needsave = true
  612. else
  613. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  614. easyvend.sound_error(playername)
  615. end
  616. end
  617. if fields.change_network and fields.network then
  618. if owner == playername or admin then
  619. meta:set_string("network", fields.network)
  620. teleports.teleports[tp_idx].channel = fields.network
  621. needsave = true
  622. else
  623. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  624. easyvend.sound_error(playername)
  625. end
  626. end
  627. if needsave == true then
  628. teleports.save()
  629. end
  630. local pressed_tp_button = false
  631. local pressed_tp_location
  632. for i = 1, 10, 1 do
  633. -- According to button names/data set in the machine update function.
  634. local btnname = "tp" .. i
  635. local posname = "loc" .. i
  636. if fields[btnname] then
  637. pressed_tp_button = true
  638. pressed_tp_location = meta:get_string(posname)
  639. break
  640. end
  641. end
  642. if pressed_tp_button then
  643. local have_biofuel = false
  644. local tpname = pressed_tp_location
  645. local have_target = false
  646. local target_pos = {x=0, y=0, z=0}
  647. local teleport_range = nil
  648. if tpname and type(tpname) == "string" then
  649. local tppos = minetest.string_to_pos(tpname)
  650. if tppos then
  651. teleport_range = teleports.calculate_range(pos)
  652. if vector_distance(tppos, pos) <= teleport_range then
  653. -- Do not permit teleporting from one realm to another.
  654. -- Doing so requires a different kind of teleport device.
  655. local start_realm = rc.current_realm_at_pos(pos)
  656. local target_realm = rc.current_realm_at_pos(tppos)
  657. if start_realm ~= "" and start_realm == target_realm then
  658. local exists = false
  659. for i = 1, #teleports.teleports, 1 do
  660. local tp = teleports.teleports[i]
  661. if vector_equals(tp.pos, tppos) then
  662. exists = true
  663. break
  664. end
  665. end
  666. if exists then
  667. have_target = true
  668. target_pos = tppos
  669. else
  670. minetest.chat_send_player(playername, "# Server: Transport control error: target no longer exists.")
  671. easyvend.sound_error(playername)
  672. end
  673. else
  674. minetest.chat_send_player(playername, "# Server: Cannot teleport between realm boundaries!")
  675. easyvend.sound_error(playername)
  676. end
  677. else
  678. minetest.chat_send_player(playername, "# Server: Transport control error: target out of range!")
  679. easyvend.sound_error(playername)
  680. end
  681. else
  682. minetest.chat_send_player(playername, "# Server: Transport control error: 0xDEADBEEF.")
  683. easyvend.sound_error(playername)
  684. end
  685. else
  686. minetest.chat_send_player(playername, "# Server: Transport control error: formspec.")
  687. easyvend.sound_error(playername)
  688. end
  689. if have_target == true then -- Don't use fuel unless a valid target is found.
  690. local inv = meta:get_inventory();
  691. if not admin and not infinite_fuel then -- Don't do fuel calculation if admin is using teleport.
  692. -- Cost is 1 item of fuel per 300 meters.
  693. -- This means players save on fuel when using long range teleports,
  694. -- instead of using a chain of short-range teleports.
  695. -- However, long range teleports cost more to make.
  696. local rcost = math_floor(vector_distance(pos, target_pos) / 300)
  697. if isnyan then
  698. -- Nyan teleports have much greater fuel efficiency.
  699. rcost = math_floor(vector_distance(pos, target_pos) / 600)
  700. end
  701. if rcost < 1 then rcost = 1 end
  702. -- If using lilies as fuel, fewer are required.
  703. -- Lilies are bit harder to get.
  704. local lcost = math_floor(rcost * 0.5)
  705. if lcost < 1 then lcost = 1 end
  706. local price1 = {name="default:mossycobble", count=rcost, wear=0, metadata=""}
  707. local price2 = {name="flowers:waterlily", count=lcost, wear=0, metadata=""}
  708. if not inv:is_empty("price") then
  709. if inv:contains_item("price", price1) then
  710. inv:remove_item("price", price1)
  711. have_biofuel = true
  712. elseif inv:contains_item("price", price2) then
  713. inv:remove_item("price", price2)
  714. have_biofuel = true
  715. else
  716. minetest.chat_send_player(playername, "# Server: Insufficient stored energy for transport. Add more biofuel.")
  717. easyvend.sound_error(playername)
  718. end
  719. else
  720. minetest.chat_send_player(playername, "# Server: Transporter is on maintenance energy only. Add biofuel to use.")
  721. easyvend.sound_error(playername)
  722. end
  723. end
  724. if have_biofuel or admin or infinite_fuel then
  725. local teleport_pos = {x=target_pos.x, y=target_pos.y, z=target_pos.z}
  726. teleports.teleport_player(player, pos, teleport_pos, teleport_range)
  727. end
  728. end
  729. end
  730. -- Always update the teleport formspec.
  731. teleports.update(pos)
  732. end
  733. teleports.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  734. local pname = player:get_player_name()
  735. -- Protection interferes with building public networks.
  736. --if minetest.test_protection(pos, pname) then return 0 end
  737. if listname == "price" and stack:get_name() == "default:mossycobble" then
  738. return stack:get_count()
  739. elseif listname == "price" and stack:get_name() == "flowers:waterlily" then
  740. return stack:get_count()
  741. elseif listname == "price" and stack:get_name() == "rosestone:head" then
  742. if minetest.test_protection(pos, pname) then return 0 end
  743. return stack:get_count()
  744. end
  745. return 0
  746. end
  747. teleports.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  748. local pname = player:get_player_name()
  749. -- Protection interferes with building public networks.
  750. --if minetest.test_protection(pos, pname) then return 0 end
  751. if stack:get_name() == "rosestone:head" then
  752. if minetest.test_protection(pos, pname) then return 0 end
  753. return stack:get_count()
  754. end
  755. return stack:get_count()
  756. end
  757. teleports.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  758. return 0
  759. end
  760. function teleports.update_beacon_data(pos)
  761. local meta = minetest.get_meta(pos)
  762. local inv = meta:get_inventory()
  763. local item = {name="rosestone:head", count=1, wear=0, metadata=""}
  764. if inv:contains_item("price", item) then
  765. for k, v in ipairs(teleports.teleports) do
  766. if vector_equals(v.pos, pos) then
  767. if not v.is_recall then
  768. v.is_recall = true
  769. teleports.save()
  770. end
  771. end
  772. end
  773. else
  774. for k, v in ipairs(teleports.teleports) do
  775. if vector_equals(v.pos, pos) then
  776. if v.is_recall then
  777. v.is_recall = nil
  778. teleports.save()
  779. end
  780. end
  781. end
  782. end
  783. end
  784. function teleports.on_metadata_inventory_put(pos, listname, index, stack, player)
  785. teleports.update_beacon_data(pos)
  786. end
  787. function teleports.on_metadata_inventory_take(pos, listname, index, stack, player)
  788. teleports.update_beacon_data(pos)
  789. end
  790. teleports.can_dig = function(pos)
  791. local meta = minetest.get_meta(pos)
  792. local inv = meta:get_inventory()
  793. return inv:is_empty("price")
  794. end
  795. teleports.after_place_node = function(pos, placer)
  796. if placer and placer:is_player() then
  797. local meta = minetest.get_meta(pos)
  798. local pname = placer:get_player_name()
  799. local dname = rename.gpn(pname)
  800. meta:set_string("owner", pname)
  801. meta:set_string("rename", dname)
  802. meta:set_string("name", "")
  803. meta:set_string("network", "")
  804. meta:set_int("public", 1)
  805. meta:set_int("construction_time", os.time())
  806. local inv = meta:get_inventory()
  807. inv:set_size("price", 1)
  808. local initialcharge = {name="default:mossycobble", count=30, wear=0, metadata=""}
  809. inv:add_item("price", initialcharge)
  810. teleports.update(pos)
  811. table.insert(teleports.teleports, {pos=vector_round(pos)})
  812. teleports.save()
  813. end
  814. end
  815. teleports.on_destruct = function(pos)
  816. --minetest.chat_send_all("# Server: Destructing teleport!")
  817. for i, EachTeleport in ipairs(teleports.teleports) do
  818. if vector_equals(EachTeleport.pos, pos) then
  819. table.remove(teleports.teleports, i)
  820. teleports.save()
  821. end
  822. end
  823. end
  824. function teleports.ping_all_teleports(origin_pos, initiating_player)
  825. local players = minetest.get_connected_players()
  826. local start_realm = rc.current_realm_at_pos(origin_pos)
  827. local ping = function(pos)
  828. local xd = 1
  829. local zd = 1
  830. minetest.add_particlespawner({
  831. amount = 80,
  832. time = 5,
  833. minpos = {x=pos.x-xd, y=pos.y+1, z=pos.z-zd},
  834. maxpos = {x=pos.x+xd, y=pos.y+3, z=pos.z+zd},
  835. minvel = {x=-1, y=-1, z=-1},
  836. maxvel = {x=1, y=1, z=1},
  837. minacc = {x=-1, y=-1, z=-1},
  838. maxacc = {x=1, y=1, z=1},
  839. minexptime = 0.5,
  840. maxexptime = 1.5,
  841. minsize = 1,
  842. maxsize = 1.5,
  843. collisiondetection = false,
  844. texture = "nether_particle_anim4.png",
  845. animation = {
  846. type = "vertical_frames",
  847. aspect_w = 7,
  848. aspect_h = 7,
  849. -- Disabled for now due to causing older clients to hang.
  850. --length = -1,
  851. length = 0.3,
  852. },
  853. glow = 14,
  854. })
  855. end
  856. -- Spawn particles over every teleport that's near a player.
  857. local ports = teleports.teleports
  858. local tlen = #ports
  859. local plen = #players
  860. for k = 1, tlen, 1 do
  861. local porthub = ports[k]
  862. local portpos = porthub.pos
  863. for i = 1, plen, 1 do
  864. local pref = players[i]
  865. local playerpos = pref:get_pos()
  866. local dist = vector_distance(portpos, playerpos)
  867. -- Don't add particles for the initiating player above the teleport they
  868. -- are actually using (but spawn particles for them over any nearby).
  869. if dist < 32 and (pref ~= initiating_player or dist > 2) then
  870. local tp_realm = rc.current_realm_at_pos(portpos)
  871. if tp_realm == start_realm then
  872. ping(portpos)
  873. if math_random(1, 500) == 1 then
  874. minetest.after(math_random(1, 5), function()
  875. pm.spawn_random_wisp(vector_add(portpos, {x=0, y=1, z=0}))
  876. end)
  877. end
  878. end
  879. end
  880. end
  881. end
  882. end
  883. teleports.on_punch = function(pos, node, puncher, pointed_thing)
  884. teleports.update(pos)
  885. -- Maybe this is a bit too spammy and generally unnecessary?
  886. if puncher and puncher:is_player() then
  887. minetest.chat_send_player(puncher:get_player_name(), "# Server: This machine has been updated.")
  888. end
  889. end
  890. teleports.on_diamond_place = function(itemstack, placer, pointed_thing)
  891. local stack = ItemStack("default:diamondblock")
  892. local pos = pointed_thing.above
  893. local name = "default:diamondblock"
  894. if minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z}).name == name and
  895. minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z+1}).name == name and
  896. minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z-1}).name == name and
  897. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z}).name == name and
  898. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z+1}).name == name and
  899. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z-1}).name == name and
  900. minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1}).name == name and
  901. minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1}).name == name
  902. then
  903. stack = ItemStack("teleports:teleport")
  904. end
  905. local ret = minetest.item_place(stack, placer, pointed_thing)
  906. if ret == nil then
  907. return itemstack
  908. else
  909. return ItemStack("default:diamondblock " .. itemstack:get_count() - (1 - ret:get_count()))
  910. end
  911. end
  912. -- Admin API function, refills ALL teleports with fuel (if fuel is empty).
  913. function teleports.refill_all()
  914. local tps = teleports.teleports
  915. for k, v in ipairs(tps) do
  916. local meta = minetest.get_meta(v.pos)
  917. if meta then
  918. local inv = meta:get_inventory()
  919. if inv then
  920. if inv:is_empty("price") then
  921. inv:set_stack("price", 1, ItemStack("flowers:waterlily 64"))
  922. else
  923. local stack = inv:get_stack("price", 1)
  924. if stack:get_name() == "default:mossycobble" then
  925. stack:set_count(64)
  926. inv:set_stack("price", 1, stack)
  927. elseif stack:get_name() == "flowers:waterlily" then
  928. stack:set_count(64)
  929. inv:set_stack("price", 1, stack)
  930. end
  931. end
  932. end
  933. end
  934. end
  935. end