functions.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. -- This file is designed to be reloadable.
  2. teleports = teleports or {}
  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 math_floor = math.floor
  11. local math_random = math.random
  12. dofile(teleports.modpath .. "/sickness.lua")
  13. local nyanbow = "nyancat:nyancat_rainbow"
  14. -- Table of blocks which can be used to super-charge a teleport. Each block has a specific charge value.
  15. teleports.charge_blocks = {
  16. ["default:diamondblock"] = {charge=14 },
  17. ["default:mese"] = {charge=4 },
  18. ["default:steelblock"] = {charge=1 },
  19. ["default:copperblock"] = {charge=1.5 },
  20. ["default:bronzeblock"] = {charge=1.8 },
  21. ["default:goldblock"] = {charge=2 },
  22. ["moreores:silver_block"] = {charge=2 },
  23. ["moreores:tin_block"] = {charge=1.5 },
  24. ["moreores:mithril_block"] = {charge=24 },
  25. ["chromium:block"] = {charge=0.9 },
  26. ["zinc:block"] = {charge=1.8 },
  27. ["lead:block"] = {charge=0.4 },
  28. ["akalin:block"] = {charge=0.9 },
  29. ["alatro:block"] = {charge=0.7 },
  30. ["arol:block"] = {charge=0.5 },
  31. ["talinite:block"] = {charge=1.1 },
  32. }
  33. teleports.is_nyanbow_teleport = function(pos)
  34. local positions = {
  35. {x=pos.x-1, y=pos.y, z=pos.z},
  36. {x=pos.x+1, y=pos.y, z=pos.z},
  37. {x=pos.x, y=pos.y, z=pos.z-1},
  38. {x=pos.x, y=pos.y, z=pos.z+1},
  39. {x=pos.x-1, y=pos.y, z=pos.z-1},
  40. {x=pos.x+1, y=pos.y, z=pos.z+1},
  41. {x=pos.x-1, y=pos.y, z=pos.z+1},
  42. {x=pos.x+1, y=pos.y, z=pos.z-1},
  43. }
  44. local bows = 0
  45. for k, v in ipairs(positions) do
  46. local n = minetest.get_node(v).name
  47. if n == nyanbow then
  48. bows = bows + 1
  49. end
  50. end
  51. return (bows == 8)
  52. end
  53. teleports.save = function()
  54. local datastring = xban.serialize(teleports.teleports)
  55. if not datastring then
  56. return
  57. end
  58. local file, err = io.open(teleports.datafile, "w")
  59. if err then
  60. return
  61. end
  62. file:write(datastring)
  63. file:close()
  64. end
  65. teleports.load = function()
  66. local file, err = io.open(teleports.datafile, "r")
  67. if err then
  68. teleports.teleports = {}
  69. return
  70. end
  71. teleports.teleports = minetest.deserialize(file:read("*all"))
  72. if type(teleports.teleports) ~= "table" then
  73. teleports.teleports = {}
  74. end
  75. file:close()
  76. end
  77. teleports.clear_area = function(blockpos, action, calls_remaining, param)
  78. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  79. return
  80. end
  81. for x = param.minp.x, param.maxp.x, 1 do
  82. for y = param.minp.y, param.maxp.y, 1 do
  83. for z = param.minp.z, param.maxp.z, 1 do
  84. local pos = {x=x, y=y, z=z}
  85. local node = minetest.get_node(pos)
  86. if node.name ~= "ignore" then
  87. if node.name ~= "air" and node.name ~= "bones:bones" then
  88. minetest.remove_node(pos)
  89. end
  90. end
  91. end
  92. end
  93. end
  94. end
  95. function teleports.kill_players_at_pos(teleport_pos, pname)
  96. local dead_players = minetest.get_objects_inside_radius({x=teleport_pos.x, y=teleport_pos.y+1, z=teleport_pos.z}, 2)
  97. for k, v in ipairs(dead_players) do
  98. if v and v:is_player() then
  99. 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).
  100. -- Only if player isn't already dead.
  101. if v:get_hp() > 0 then
  102. -- If there's a player here already the map must be loaded, so we
  103. -- can put fire where they're standing no problem.
  104. local dp = vector_round(v:get_pos())
  105. local node = minetest.get_node(dp)
  106. if node.name == "air" then
  107. minetest.add_node(dp, {name="fire:basic_flame"})
  108. end
  109. v:set_hp(0)
  110. minetest.chat_send_all("# Server: <" .. rename.gpn(v:get_player_name()) .. "> was killed by a teleport. Noob!")
  111. end
  112. end
  113. end
  114. end
  115. end
  116. teleports.teleport_player = function(player, origin_pos, teleport_pos)
  117. if not player or not player:is_player() then
  118. return
  119. end
  120. local pname = player:get_player_name()
  121. if sheriff.is_cheater(pname) then
  122. if sheriff.punish_probability(pname) then
  123. sheriff.punish_player(pname)
  124. return
  125. end
  126. end
  127. local player_pos = player:get_pos()
  128. if (player_pos.y < origin_pos.y) or (vector_distance(player_pos, origin_pos) > 2) then
  129. minetest.chat_send_player(pname, "# Server: You must stand on portal activation surface!")
  130. return
  131. end
  132. -- Small chance to be teleported somewhere completely random.
  133. -- The chance increases a LOT if teleports are crowded.
  134. local use_random = false
  135. local random_chance = 1000 -- Actually 970, because nearby-count is always at least 1 (counting self).
  136. local count_nearby = 0
  137. for k, v in ipairs(teleports.teleports) do
  138. if vector_distance(v.pos, origin_pos) < 100 then
  139. count_nearby = count_nearby + 1
  140. end
  141. end
  142. random_chance = random_chance - (count_nearby * 30)
  143. if random_chance < 10 then
  144. random_chance = 10
  145. end
  146. --minetest.chat_send_all('chance: ' .. random_chance)
  147. if math_random(1, random_chance) == 1 then
  148. if #(teleports.teleports) > 0 then
  149. local tp = teleports.teleports[math_random(1, #(teleports.teleports))]
  150. if not tp then
  151. minetest.chat_send_player(pname, "# Server: Transport error! Aborted.")
  152. return
  153. end
  154. teleport_pos = tp.pos
  155. use_random = true
  156. end
  157. end
  158. local p = vector_round(teleport_pos)
  159. local minp = {x=p.x-1, y=p.y+1, z=p.z-1}
  160. local maxp = {x=p.x+1, y=p.y+3, z=p.z+1}
  161. local target = {x=p.x-1+math_random(0, 2), y=p.y+1, z=p.z-1+math_random(0, 2)}
  162. local pos = vector_round(target)
  163. -- Perform this check only if teleport target wasn't randomized.
  164. if not use_random then
  165. local start_realm = rc.current_realm_at_pos(origin_pos)
  166. local target_realm = rc.current_realm_at_pos(pos)
  167. if target_realm == "" or start_realm == "" or start_realm ~= target_realm then
  168. minetest.chat_send_player(pname, "# Server: Target location is in a different realm! Aborting.")
  169. return
  170. end
  171. end
  172. minetest.log("[teleports] teleporting player <" .. pname .. "> to " .. minetest.pos_to_string(pos))
  173. -- Teleport player to chosen location.
  174. preload_tp.execute({
  175. player_name = pname,
  176. target_position = pos,
  177. send_blocks = true,
  178. particle_effects = true,
  179. pre_teleport_callback = function()
  180. -- Kill players standing on target teleport pad.
  181. teleports.kill_players_at_pos(teleport_pos, pname)
  182. -- Delete 3x3x3 area above teleport.
  183. for x=minp.x, maxp.x do
  184. for y=minp.y, maxp.y do
  185. for z=minp.z, maxp.z do
  186. local pos = {x=x, y=y, z=z}
  187. local node = minetest.get_node(pos)
  188. if node.name ~= "ignore" then
  189. -- Do not destroy players' bones.
  190. if node.name ~= "air" and node.name ~= "bones:bones" then
  191. minetest.add_node(pos, {name="fire:basic_flame"})
  192. end
  193. end
  194. end
  195. end
  196. end
  197. end,
  198. post_teleport_callback = function()
  199. portal_sickness.on_use_portal(pname)
  200. if use_random then
  201. minetest.after(10, function()
  202. local RED = core.get_color_escape_sequence("#ff0000")
  203. minetest.chat_send_player(pname, RED .. "# Server: Coordinate translation error. Unknown destination!")
  204. chat_core.alert_player_sound(pname)
  205. end)
  206. end
  207. end,
  208. })
  209. teleports.ping_all_teleports()
  210. end
  211. teleports.find_nearby = function(pos, count, network, yespublic)
  212. local nearby = {}
  213. local trange, isnyan = teleports.calculate_range(pos)
  214. local start_realm = rc.current_realm_at_pos(pos)
  215. for i = #teleports.teleports, 1, -1 do
  216. local tp = teleports.teleports[i]
  217. if not vector.equals(tp.pos, pos) and vector_distance(tp.pos, pos) <= trange then
  218. local target_realm = rc.current_realm_at_pos(tp.pos)
  219. -- Only find teleports in the same dimension.
  220. if start_realm ~= "" and start_realm == target_realm then
  221. local othernet = tp.channel or ""
  222. if othernet == network or (othernet == "" and yespublic == 'true') then
  223. table.insert(nearby, tp)
  224. if #nearby >= count then
  225. break
  226. end
  227. end
  228. end
  229. end
  230. end
  231. return nearby
  232. end
  233. teleports.find_specific = function(pos)
  234. for i = 1, #teleports.teleports, 1 do
  235. local tp = teleports.teleports[i]
  236. if vector.equals(tp.pos, pos) then
  237. return i -- Return index of teleport.
  238. end
  239. end
  240. end
  241. teleports.calculate_charge = function(pos)
  242. local positions = {
  243. {x=pos.x-1, y=pos.y, z=pos.z},
  244. {x=pos.x+1, y=pos.y, z=pos.z},
  245. {x=pos.x, y=pos.y, z=pos.z-1},
  246. {x=pos.x, y=pos.y, z=pos.z+1},
  247. {x=pos.x-1, y=pos.y, z=pos.z-1},
  248. {x=pos.x+1, y=pos.y, z=pos.z+1},
  249. {x=pos.x-1, y=pos.y, z=pos.z+1},
  250. {x=pos.x+1, y=pos.y, z=pos.z-1},
  251. }
  252. local bows = 0
  253. local charge = 1 -- Ambient charge is at least 1 (the teleport block provides 1 KJ).
  254. for k, v in ipairs(positions) do
  255. local n = minetest.get_node(v).name
  256. local c = 0
  257. if teleports.charge_blocks[n] ~= nil then
  258. c = teleports.charge_blocks[n].charge
  259. end
  260. charge = charge + c
  261. if n == nyanbow then
  262. bows = bows + 1
  263. end
  264. end
  265. local is_nyanporter = false
  266. if bows == 8 then
  267. is_nyanporter = true
  268. end
  269. charge = math_floor(charge + 0.5)
  270. -- Combined teleports interfere with each other and reduce their range.
  271. local minp = vector.add(pos, {x=-2, y=0, z=-2})
  272. local maxp = vector.add(pos, {x=2, y=0, z=2})
  273. local others = minetest.find_nodes_in_area(minp, maxp, "teleports:teleport")
  274. local other_count = 1
  275. if others and #others > 0 then
  276. charge = charge / #others
  277. other_count = #others
  278. end
  279. return charge, other_count, is_nyanporter
  280. end
  281. local function cds(y)
  282. local scalar = 1
  283. if y < 0 then
  284. local depth = math.abs(y)
  285. scalar = depth / 30912
  286. if scalar > 1 then scalar = 1 end
  287. if scalar < 0 then scalar = 0 end
  288. scalar = (scalar * -1) + 1
  289. end
  290. return scalar
  291. end
  292. teleports.calculate_range = function(pos)
  293. -- Compute charge.
  294. local chg, other_cnt, nyan = teleports.calculate_charge(pos)
  295. if nyan then
  296. local owner = minetest.get_meta(pos):get_string("owner")
  297. -- There is an admin teleport pair between the Surface Colony and the City of Fire.
  298. -- This special exception code makes it work.
  299. if owner == "MustTest" then
  300. return 31000, nyan
  301. else
  302. -- Range of nyan teleports is reduced if they're crowded.
  303. return (7770 / other_cnt), nyan
  304. end
  305. end
  306. -- How much distance each unit of charge is good for.
  307. local inc = 25
  308. -- Compute extra range.
  309. local rng = math_floor(inc * chg)
  310. -- Calculate how much to scale extra range by depth.
  311. local scalar = cds(pos.y)
  312. -- Scale extra range by depth.
  313. rng = rng * scalar
  314. -- Add extra range to base (minimum) range.
  315. rng = rng + 250
  316. -- Teleport range shall not go below 250 meters.
  317. return math_floor(rng), nyan
  318. end
  319. function teleports.write_infotext(pos)
  320. local meta = minetest.get_meta(pos)
  321. local name = meta:get_string("name")
  322. local network = meta:get_string("network")
  323. local owner = meta:get_string("owner")
  324. local dname = rename.gpn(owner)
  325. local public = meta:get_int("public") or 1
  326. if public == 1 then public = 'true' else public = 'false' end
  327. local id = "<" .. name .. ">"
  328. local net = "<" .. network .. ">"
  329. local own = "<" .. dname .. ">"
  330. if name == "" then id = "NONE" end
  331. if network == "" then net = "PUBLIC" end
  332. if public == 'false' then net = "SUPPRESSED" end
  333. meta:set_string("infotext", "Teleporter. Punch to update controls.\nOwner: " .. own .. "\nBeacon ID: " .. id .. "\nBeacon Channel: " .. net)
  334. end
  335. teleports.update = function(pos)
  336. local meta = minetest.get_meta(pos)
  337. local network = meta:get_string("network") or ""
  338. local owner = meta:get_string("owner") or ""
  339. local name = meta:get_string("name") or ""
  340. local yespublic = meta:get_string("yespublic") or 'true'
  341. local buttons = "";
  342. local nearby = teleports.find_nearby(pos, 10, network, yespublic)
  343. local button_x = 8
  344. local button_y = 1
  345. for i, v in ipairs(nearby) do
  346. local tp = v.pos
  347. local data = tp.x .. "," .. tp.y .. "," .. tp.z
  348. local real_label = rc.pos_to_string(tp)
  349. meta:set_string("loc" .. (i), data)
  350. if v.name ~= nil then
  351. if v.name ~= "" then
  352. real_label = v.name
  353. end
  354. end
  355. buttons = buttons ..
  356. "button_exit[" .. button_x .. "," .. button_y ..
  357. ";3,0.5;tp" .. i .. ";" .. minetest.formspec_escape(real_label) .. "]";
  358. button_y = button_y + 1
  359. if button_y >= 6 then
  360. button_y = 1
  361. button_x = 5
  362. end
  363. end
  364. local public = meta:get_int("public") or 1
  365. if public == 1 then public = 'true' else public = 'false' end
  366. teleports.write_infotext(pos)
  367. local net = "<" .. network .. ">"
  368. local nm = "<" .. name .. ">"
  369. if name == "" then nm = "NONE" end
  370. if network == "" then net = "PUBLIC" end
  371. if public == 'false' then net = "SUPPRESSED" end
  372. local charge, count, isnyan = teleports.calculate_charge(pos)
  373. local range = teleports.calculate_range(pos)
  374. if isnyan then
  375. charge = "NYBW"
  376. end
  377. local formspec = "size[11,7;]" ..
  378. default.gui_bg ..
  379. default.gui_bg_img ..
  380. default.gui_slots ..
  381. "label[0,0;" .. 'Transport to nearby beacons! Need mossy cobble for energy.' .. "]" ..
  382. "label[1,0.70;Beacon ID: " .. minetest.formspec_escape(nm) .. "]" ..
  383. "label[1,1.2;Beacon Channel: " .. minetest.formspec_escape(net) .. "]" ..
  384. "field[0.3,2.7;2,0.5;id;Change Beacon ID;]" .. "button_exit[2,2.4;2,0.5;change_id;Confirm]" ..
  385. "field[0.3,3.9;2,0.5;network;Change Channel;]" .. "button_exit[2,3.6;2,0.5;change_network;Confirm]" ..
  386. buttons ..
  387. "button_exit[0,5.2;2,0.5;cancel;Close]" ..
  388. "checkbox[0.02,4.1;showhide;Show Channel;" .. public .. "]" ..
  389. "checkbox[2,4.1;yespublic;Connect Public;" .. yespublic .. "]" ..
  390. "label[2,5;Ambient Charge: " .. charge .. " KJ]" ..
  391. "label[2,5.35;Transport Range: " .. range .. " M]" ..
  392. "list[context;price;0,0.75;1,1;]" ..
  393. "list[current_player;main;0,6;11,1;]" ..
  394. "listring[]"
  395. meta:set_string("formspec", formspec)
  396. end
  397. teleports.on_receive_fields = function(pos, formname, fields, player)
  398. if not player then return end
  399. if not player:is_player() then return end
  400. if player:get_hp() <= 0 then return end -- Ignore dead players.
  401. local playername = player:get_player_name()
  402. local meta = minetest.get_meta(pos)
  403. local isnyan = teleports.is_nyanbow_teleport(pos)
  404. local owner = meta:get_string("owner") or ""
  405. local infinite_fuel = false
  406. if owner == "MustTest" then
  407. infinite_fuel = true
  408. end
  409. local admin = minetest.check_player_privs(playername, {server=true})
  410. local needsave = false
  411. -- Make sure this teleport, at this postion, has an entry.
  412. local tp_idx = teleports.find_specific(pos)
  413. if not tp_idx then
  414. minetest.chat_send_player(playername, "# Server: Transporter data error: 0xDEADBEEF.")
  415. easyvend.sound_error(playername)
  416. return
  417. end
  418. if not teleports.teleports[tp_idx] then
  419. minetest.chat_send_player(playername, "# Server: Transporter data error: 0xDEADBEEF.")
  420. easyvend.sound_error(playername)
  421. return
  422. end
  423. if fields.showhide then
  424. if owner == playername or admin then
  425. if fields.showhide == "true" then
  426. meta:set_int("public", 1)
  427. else
  428. meta:set_int("public", 0)
  429. end
  430. else
  431. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  432. easyvend.sound_error(playername)
  433. end
  434. end
  435. if fields.yespublic then
  436. if owner == playername or admin then
  437. if fields.yespublic == "true" then
  438. meta:set_string("yespublic", 'true')
  439. else
  440. meta:set_string("yespublic", 'false')
  441. end
  442. else
  443. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  444. easyvend.sound_error(playername)
  445. end
  446. end
  447. if fields.change_id and fields.id then
  448. if owner == playername or admin then
  449. meta:set_string("name", fields.id)
  450. teleports.teleports[tp_idx].name = fields.id
  451. needsave = true
  452. else
  453. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  454. easyvend.sound_error(playername)
  455. end
  456. end
  457. if fields.change_network and fields.network then
  458. if owner == playername or admin then
  459. meta:set_string("network", fields.network)
  460. teleports.teleports[tp_idx].channel = fields.network
  461. needsave = true
  462. else
  463. minetest.chat_send_player(playername, "# Server: Only the owner can change the configuration.")
  464. easyvend.sound_error(playername)
  465. end
  466. end
  467. if needsave == true then
  468. teleports.save()
  469. end
  470. local pressed_tp_button = false
  471. local pressed_tp_location
  472. for i = 1, 10, 1 do
  473. -- According to button names/data set in the machine update function.
  474. local btnname = "tp" .. i
  475. local posname = "loc" .. i
  476. if fields[btnname] then
  477. pressed_tp_button = true
  478. pressed_tp_location = meta:get_string(posname)
  479. break
  480. end
  481. end
  482. if pressed_tp_button then
  483. local have_biofuel = false
  484. local tpname = pressed_tp_location
  485. local have_target = false
  486. local target_pos = {x=0, y=0, z=0}
  487. if tpname and type(tpname) == "string" then
  488. local tppos = minetest.string_to_pos(tpname)
  489. if tppos then
  490. if vector_distance(tppos, pos) <= teleports.calculate_range(pos) then
  491. -- Do not permit teleporting from one realm to another.
  492. -- Doing so requires a different kind of teleport device.
  493. local start_realm = rc.current_realm_at_pos(pos)
  494. local target_realm = rc.current_realm_at_pos(tppos)
  495. if start_realm ~= "" and start_realm == target_realm then
  496. local exists = false
  497. for i = 1, #teleports.teleports, 1 do
  498. local tp = teleports.teleports[i]
  499. if vector.equals(tp.pos, tppos) then
  500. exists = true
  501. break
  502. end
  503. end
  504. if exists then
  505. have_target = true
  506. target_pos = tppos
  507. else
  508. minetest.chat_send_player(playername, "# Server: Transport control error: target no longer exists.")
  509. easyvend.sound_error(playername)
  510. end
  511. else
  512. minetest.chat_send_player(playername, "# Server: Cannot teleport between realm boundaries!")
  513. easyvend.sound_error(playername)
  514. end
  515. else
  516. minetest.chat_send_player(playername, "# Server: Transport control error: target out of range!")
  517. easyvend.sound_error(playername)
  518. end
  519. else
  520. minetest.chat_send_player(playername, "# Server: Transport control error: 0xDEADBEEF.")
  521. easyvend.sound_error(playername)
  522. end
  523. else
  524. minetest.chat_send_player(playername, "# Server: Transport control error: formspec.")
  525. easyvend.sound_error(playername)
  526. end
  527. if have_target == true then -- Don't use fuel unless a valid target is found.
  528. local inv = meta:get_inventory();
  529. if not admin and not isnyan and not infinite_fuel then -- Don't do fuel calculation if admin is using teleport.
  530. -- Cost is 1 item of fuel per 300 meters.
  531. -- This means players save on fuel when using long range teleports,
  532. -- instead of using a chain of short-range teleports.
  533. -- However, long range teleports cost more to make.
  534. local rcost = math_floor(vector_distance(pos, target_pos) / 300)
  535. if rcost < 1 then rcost = 1 end
  536. local price1 = {name="default:mossycobble", count=rcost, wear=0, metadata=""}
  537. local price2 = {name="flowers:waterlily", count=rcost, wear=0, metadata=""}
  538. if not inv:is_empty("price") then
  539. if inv:contains_item("price", price1) then
  540. inv:remove_item("price", price1)
  541. have_biofuel = true
  542. elseif inv:contains_item("price", price2) then
  543. inv:remove_item("price", price2)
  544. have_biofuel = true
  545. else
  546. minetest.chat_send_player(playername, "# Server: Insufficient stored energy for transport. Add more biofuel.")
  547. easyvend.sound_error(playername)
  548. end
  549. else
  550. minetest.chat_send_player(playername, "# Server: Transporter is on maintenance energy only. Add biofuel to use.")
  551. easyvend.sound_error(playername)
  552. end
  553. end
  554. if have_biofuel or admin or isnyan or infinite_fuel then
  555. local teleport_pos = {x=target_pos.x, y=target_pos.y, z=target_pos.z}
  556. teleports.teleport_player(player, pos, teleport_pos)
  557. end
  558. end
  559. end
  560. -- Always update the teleport formspec.
  561. teleports.update(pos)
  562. end
  563. teleports.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  564. local pname = player:get_player_name()
  565. -- Protection interferes with building public networks.
  566. --if minetest.test_protection(pos, pname) then return 0 end
  567. if listname == "price" and stack:get_name() == "default:mossycobble" then
  568. return stack:get_count()
  569. elseif listname == "price" and stack:get_name() == "flowers:waterlily" then
  570. return stack:get_count()
  571. end
  572. return 0
  573. end
  574. teleports.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  575. local pname = player:get_player_name()
  576. -- Protection interferes with building public networks.
  577. --if minetest.test_protection(pos, pname) then return 0 end
  578. return stack:get_count()
  579. end
  580. teleports.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  581. return 0
  582. end
  583. teleports.can_dig = function(pos)
  584. local meta = minetest.get_meta(pos)
  585. local inv = meta:get_inventory()
  586. return inv:is_empty("price")
  587. end
  588. teleports.after_place_node = function(pos, placer)
  589. if placer and placer:is_player() then
  590. local meta = minetest.get_meta(pos)
  591. local pname = placer:get_player_name()
  592. local dname = rename.gpn(pname)
  593. meta:set_string("owner", pname)
  594. meta:set_string("rename", dname)
  595. meta:set_string("name", "")
  596. meta:set_string("network", "")
  597. meta:set_int("public", 1)
  598. local inv = meta:get_inventory()
  599. inv:set_size("price", 1)
  600. local initialcharge = {name="default:mossycobble", count=30, wear=0, metadata=""}
  601. inv:add_item("price", initialcharge)
  602. teleports.update(pos)
  603. table.insert(teleports.teleports, {pos=vector_round(pos)})
  604. teleports.save()
  605. end
  606. end
  607. teleports.on_destruct = function(pos)
  608. --minetest.chat_send_all("# Server: Destructing teleport!")
  609. for i, EachTeleport in ipairs(teleports.teleports) do
  610. if vector.equals(EachTeleport.pos, pos) then
  611. table.remove(teleports.teleports, i)
  612. teleports.save()
  613. end
  614. end
  615. end
  616. function teleports.ping_all_teleports()
  617. local players = minetest.get_connected_players()
  618. local pp = {}
  619. for k, v in ipairs(players) do
  620. table.insert(pp, v:get_pos())
  621. end
  622. local ping = function(pos)
  623. local xd = 1
  624. local zd = 1
  625. minetest.add_particlespawner({
  626. amount = 80,
  627. time = 5,
  628. minpos = {x=pos.x-xd, y=pos.y+1, z=pos.z-zd},
  629. maxpos = {x=pos.x+xd, y=pos.y+3, z=pos.z+zd},
  630. minvel = {x=-1, y=-1, z=-1},
  631. maxvel = {x=1, y=1, z=1},
  632. minacc = {x=-1, y=-1, z=-1},
  633. maxacc = {x=1, y=1, z=1},
  634. minexptime = 0.5,
  635. maxexptime = 1.5,
  636. minsize = 0.5,
  637. maxsize = 2,
  638. collisiondetection = false,
  639. texture = "default_obsidian_shard.png",
  640. glow = 14,
  641. })
  642. end
  643. -- Spawn particles over every teleport that's near a player.
  644. for k, v in ipairs(teleports.teleports) do
  645. for i, j in ipairs(pp) do
  646. if vector_distance(v.pos, j) < 32 then
  647. ping(v.pos)
  648. if math_random(1, 1000) == 1 then
  649. minetest.after(math_random(1, 5), function()
  650. pm.spawn_random_wisp(vector_add(v.pos, {x=0, y=1, z=0}))
  651. end)
  652. end
  653. end
  654. end
  655. end
  656. end
  657. teleports.on_punch = function(pos, node, puncher, pointed_thing)
  658. teleports.update(pos)
  659. -- Maybe this is a bit too spammy and generally unnecessary?
  660. if puncher and puncher:is_player() then
  661. minetest.chat_send_player(puncher:get_player_name(), "# Server: This machine has been updated.")
  662. end
  663. end
  664. teleports.on_diamond_place = function(itemstack, placer, pointed_thing)
  665. local stack = ItemStack("default:diamondblock")
  666. local pos = pointed_thing.above
  667. local name = "default:diamondblock"
  668. if minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z}).name == name and
  669. minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z+1}).name == name and
  670. minetest.get_node({x=pos.x+1,y=pos.y,z=pos.z-1}).name == name and
  671. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z}).name == name and
  672. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z+1}).name == name and
  673. minetest.get_node({x=pos.x-1,y=pos.y,z=pos.z-1}).name == name and
  674. minetest.get_node({x=pos.x,y=pos.y,z=pos.z+1}).name == name and
  675. minetest.get_node({x=pos.x,y=pos.y,z=pos.z-1}).name == name
  676. then
  677. stack = ItemStack("teleports:teleport")
  678. end
  679. local ret = minetest.item_place(stack, placer, pointed_thing)
  680. if ret == nil then
  681. return itemstack
  682. else
  683. return ItemStack("default:diamondblock " .. itemstack:get_count() - (1 - ret:get_count()))
  684. end
  685. end