init.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. tpad = {}
  2. tpad.version = "1.2"
  3. tpad.mod_name = minetest.get_current_modname()
  4. tpad.texture = "tpad-texture.png"
  5. tpad.mesh = "tpad-mesh.obj"
  6. tpad.nodename = "tpad:tpad"
  7. tpad.mod_path = minetest.get_modpath(tpad.mod_name)
  8. local PRIVATE_PAD_STRING = "Private (only owner)"
  9. local PUBLIC_PAD_STRING = "Public (only owner's network)"
  10. local GLOBAL_PAD_STRING = "Global (any network)"
  11. local PRIVATE_PAD = 1
  12. local PUBLIC_PAD = 2
  13. local GLOBAL_PAD = 4
  14. local RED_ESCAPE = minetest.get_color_escape_sequence("#FF0000")
  15. local GREEN_ESCAPE = minetest.get_color_escape_sequence("#00FF00")
  16. local BLUE_ESCAPE = minetest.get_color_escape_sequence("#0000FF")
  17. local YELLOW_ESCAPE = minetest.get_color_escape_sequence("#FFFF00")
  18. local CYAN_ESCAPE = minetest.get_color_escape_sequence("#00FFFF")
  19. local MAGENTA_ESCAPE = minetest.get_color_escape_sequence("#FF00FF")
  20. local WHITE_ESCAPE = minetest.get_color_escape_sequence("#FFFFFF")
  21. local OWNER_ESCAPE_COLOR = CYAN_ESCAPE
  22. local padtype_flag_to_string = {
  23. [PRIVATE_PAD] = PRIVATE_PAD_STRING,
  24. [PUBLIC_PAD] = PUBLIC_PAD_STRING,
  25. [GLOBAL_PAD] = GLOBAL_PAD_STRING,
  26. }
  27. local padtype_string_to_flag = {
  28. [PRIVATE_PAD_STRING] = PRIVATE_PAD,
  29. [PUBLIC_PAD_STRING] = PUBLIC_PAD,
  30. [GLOBAL_PAD_STRING] = GLOBAL_PAD,
  31. }
  32. local short_padtype_string = {
  33. [PRIVATE_PAD] = "private",
  34. [PUBLIC_PAD] = "public",
  35. [GLOBAL_PAD] = "global",
  36. }
  37. local smartfs = dofile(tpad.mod_path .. "/lib/smartfs.lua")
  38. local notify = dofile(tpad.mod_path .. "/notify.lua")
  39. -- workaround storage to tell the main dialog about the last clicked pad
  40. local last_clicked_pos = {}
  41. -- workaround storage to tell the main dialog about last selected pad in the lists
  42. local last_selected_index = {}
  43. local last_selected_global_index = {}
  44. -- memory of shown waypoints
  45. local waypoint_hud_ids = {}
  46. minetest.register_privilege("tpad_admin", {
  47. description = "Can edit and destroy any tpad",
  48. give_to_singleplayer = true,
  49. })
  50. -- ========================================================================
  51. -- local helpers
  52. -- ========================================================================
  53. local function copy_file(source, dest)
  54. local src_file = io.open(source, "rb")
  55. if not src_file then
  56. return false, "copy_file() unable to open source for reading"
  57. end
  58. local src_data = src_file:read("*all")
  59. src_file:close()
  60. local dest_file = io.open(dest, "wb")
  61. if not dest_file then
  62. return false, "copy_file() unable to open dest for writing"
  63. end
  64. dest_file:write(src_data)
  65. dest_file:close()
  66. return true, "files copied successfully"
  67. end
  68. -- alias to make copy_file() available to storage.lua
  69. tpad._copy_file = copy_file
  70. local function custom_or_default(modname, path, filename)
  71. local default_filename = "default/" .. filename
  72. local full_filename = path .. "/custom." .. filename
  73. local full_default_filename = path .. "/" .. default_filename
  74. os.rename(path .. "/" .. filename, full_filename)
  75. local file = io.open(full_filename, "rb")
  76. if not file then
  77. minetest.debug("[" .. modname .. "] Copying " .. default_filename .. " to " .. filename .. " (path: " .. path .. ")")
  78. local success, err = copy_file(full_default_filename, full_filename)
  79. if not success then
  80. minetest.debug("[" .. modname .. "] " .. err)
  81. return false
  82. end
  83. file = io.open(full_filename, "rb")
  84. if not file then
  85. minetest.debug("[" .. modname .. "] Unable to load " .. filename .. " file from path " .. path)
  86. return false
  87. end
  88. end
  89. file:close()
  90. return full_filename
  91. end
  92. -- load storage facilities and verify it
  93. dofile(tpad.mod_path .. "/storage.lua")
  94. -- ========================================================================
  95. -- load custom recipe
  96. -- ========================================================================
  97. local recipes_filename = custom_or_default(tpad.mod_name, tpad.mod_path, "recipes.lua")
  98. if recipes_filename then
  99. local recipes = dofile(recipes_filename)
  100. if type(recipes) == "table" and recipes[tpad.nodename] then
  101. minetest.register_craft({
  102. output = tpad.nodename,
  103. recipe = recipes[tpad.nodename],
  104. })
  105. end
  106. end
  107. -- ========================================================================
  108. -- callback bound in register_chatcommand("tpad")
  109. -- ========================================================================
  110. function tpad.command(playername, param)
  111. tpad.hud_off(playername)
  112. if(param == "off") then return end
  113. local player = minetest.get_player_by_name(playername)
  114. local pads = tpad._get_stored_pads(playername)
  115. local shortest_distance = nil
  116. local closest_pad = nil
  117. local playerpos = player:getpos()
  118. for strpos, pad in pairs(pads) do
  119. local pos = minetest.string_to_pos(strpos)
  120. local distance = vector.distance(pos, playerpos)
  121. if not shortest_distance or distance < shortest_distance then
  122. closest_pad = {
  123. pos = pos,
  124. name = pad.name .. " " .. strpos,
  125. }
  126. shortest_distance = distance
  127. end
  128. end
  129. if closest_pad then
  130. waypoint_hud_ids[playername] = player:hud_add({
  131. hud_elem_type = "waypoint",
  132. name = closest_pad.name,
  133. world_pos = closest_pad.pos,
  134. number = 0xFF0000,
  135. })
  136. notify(playername, "Waypoint to " .. closest_pad.name .. " displayed")
  137. end
  138. end
  139. function tpad.hud_off(playername)
  140. local player = minetest.get_player_by_name(playername)
  141. local hud_id = waypoint_hud_ids[playername]
  142. if hud_id then
  143. player:hud_remove(hud_id)
  144. end
  145. end
  146. -- ========================================================================
  147. -- callbacks bound in register_node()
  148. -- ========================================================================
  149. function tpad.get_pos_from_pointed(pointed)
  150. local node_above = minetest.get_node_or_nil(pointed.above)
  151. local node_under = minetest.get_node_or_nil(pointed.under)
  152. if not node_above or not node_under then return end
  153. local def_above = minetest.registered_nodes[node_above.name]
  154. or minetest.nodedef_default
  155. local def_under = minetest.registered_nodes[node_under.name]
  156. or minetest.nodedef_default
  157. if not def_above.buildable_to and not def_under.buildable_to then return end
  158. if def_under.buildable_to then
  159. return pointed.under
  160. end
  161. return pointed.above
  162. end
  163. function tpad.on_place(itemstack, placer, pointed_thing)
  164. if tpad.max_total_pads_reached(placer) then
  165. notify.warn(placer, "You can't place any more pads")
  166. return itemstack
  167. end
  168. local pos = tpad.get_pos_from_pointed(pointed_thing) or {}
  169. itemstack = minetest.rotate_node(itemstack, placer, pointed_thing)
  170. local placed = minetest.get_node_or_nil(pos)
  171. if placed and placed.name == tpad.nodename then
  172. local meta = minetest.get_meta(pos)
  173. local playername = placer:get_player_name()
  174. meta:set_string("owner", playername)
  175. meta:set_string("infotext", "TPAD Station by " .. playername .. " - right click to interact")
  176. tpad.set_pad_data(pos, "", PRIVATE_PAD_STRING)
  177. end
  178. return itemstack
  179. end
  180. local submit = {}
  181. function tpad.max_total_pads_reached(placer)
  182. local placername = placer:get_player_name()
  183. if minetest.get_player_privs(placername).tpad_admin then
  184. return false
  185. end
  186. local localnet = submit.local_helper(placername)
  187. return #localnet.by_index >= tpad.get_max_total_pads()
  188. end
  189. function tpad.max_global_pads_reached(playername)
  190. if minetest.get_player_privs(playername).tpad_admin then
  191. return false
  192. end
  193. local localnet = submit.local_helper(playername)
  194. local count = 0
  195. for _, pad in pairs(localnet.by_name) do
  196. if pad.type == GLOBAL_PAD then
  197. count = count + 1
  198. end
  199. end
  200. return count >= tpad.get_max_global_pads()
  201. end
  202. function submit.global_helper()
  203. local allpads = tpad._get_all_pads()
  204. local result = {
  205. by_name = {},
  206. by_index = {},
  207. }
  208. for ownername, pads in pairs(allpads) do
  209. for strpos, pad in pairs(pads) do
  210. if pad.type == GLOBAL_PAD then
  211. pad = tpad.decorate_pad_data(strpos, pad, ownername)
  212. table.insert(result.by_index, pad.global_fullname)
  213. result.by_name[pad.global_fullname] = pad
  214. end
  215. end
  216. end
  217. table.sort(result.by_index)
  218. return result
  219. end
  220. function submit.local_helper(ownername, omit_private_pads)
  221. local pads = tpad._get_stored_pads(ownername)
  222. local result = {
  223. by_name = {},
  224. by_index = {},
  225. }
  226. for strpos, pad in pairs(pads) do
  227. local skip = omit_private_pads and pad.type == PRIVATE_PAD
  228. if not skip then
  229. pad = tpad.decorate_pad_data(strpos, pad, ownername)
  230. table.insert(result.by_index, pad.local_fullname)
  231. result.by_name[pad.local_fullname] = pad
  232. end
  233. end
  234. table.sort(result.by_index)
  235. return result
  236. end
  237. function submit.save(form)
  238. if form.playername ~= form.ownername and not minetest.get_player_privs(form.playername).tpad_admin then
  239. notify.warn(form.playername, "The selected pad doesn't belong to you")
  240. return
  241. end
  242. local padname = form.state:get("padname_field"):getText()
  243. local strpadtype = form.state:get("padtype_dropdown"):getSelectedItem()
  244. if strpadtype == GLOBAL_PAD_STRING and tpad.max_global_pads_reached(form.playername) then
  245. notify.warn(form.playername, "Can't add more pads to the Global Network, set to 'Public' instead")
  246. strpadtype = PUBLIC_PAD_STRING
  247. end
  248. tpad.set_pad_data(form.clicked_pos, padname, strpadtype)
  249. end
  250. function submit.teleport(form)
  251. local pads_listbox = form.state:get("pads_listbox")
  252. local selected_item = pads_listbox:getSelectedItem()
  253. local pad
  254. if form.globalnet then
  255. pad = form.globalnet.by_name[selected_item]
  256. else
  257. pad = form.localnet.by_name[selected_item]
  258. end
  259. if not pad then
  260. notify.err(form.playername, "Error! Missing pad data!")
  261. return
  262. end
  263. local player = minetest.get_player_by_name(form.playername)
  264. player:move_to(pad.pos, false)
  265. local padname = form.globalnet and pad.global_fullname or pad.local_fullname
  266. notify(form.playername, "Teleported to " .. padname)
  267. tpad.hud_off(form.playername)
  268. minetest.after(0, function()
  269. minetest.close_formspec(form.playername, form.formname)
  270. end)
  271. end
  272. function submit.admin(form)
  273. form.state = tpad.forms.admin:show(form.playername)
  274. form.formname = "tpad.forms.admin"
  275. local max_total_field = form.state:get("max_total_field")
  276. max_total_field:setText(tpad.get_max_total_pads())
  277. local max_global_field = form.state:get("max_global_field")
  278. max_global_field:setText(tpad.get_max_global_pads())
  279. local function admin_save()
  280. local max_total = tonumber(max_total_field:getText())
  281. local max_global = tonumber(max_global_field:getText())
  282. tpad.set_max_total_pads(max_total)
  283. tpad.set_max_global_pads(max_global)
  284. minetest.after(0, function()
  285. minetest.close_formspec(form.playername, form.formname)
  286. end)
  287. end
  288. max_total_field:onKeyEnter(admin_save)
  289. max_total_field:onKeyEnter(admin_save)
  290. form.state:get("save_button"):onClick(admin_save)
  291. end
  292. function submit.global(form)
  293. if minetest.get_player_privs(form.playername).tpad_admin then
  294. form.state = tpad.forms.global_network_admin:show(form.playername)
  295. form.formname = "tpad.forms.global_network_admin"
  296. form.state:get("admin_button"):onClick(function()
  297. minetest.after(0, function()
  298. submit.admin(form)
  299. end)
  300. end)
  301. else
  302. form.state = tpad.forms.global_network:show(form.playername)
  303. form.formname = "tpad.forms.global_network"
  304. end
  305. form.globalnet = submit.global_helper()
  306. local last_index = last_selected_global_index[form.playername]
  307. local pads_listbox = form.state:get("pads_listbox")
  308. pads_listbox:clearItems()
  309. for _, pad_item in ipairs(form.globalnet.by_index) do
  310. pads_listbox:addItem(pad_item)
  311. end
  312. pads_listbox:setSelected(last_index)
  313. pads_listbox:onClick(function()
  314. last_selected_global_index[form.playername] = pads_listbox:getSelected()
  315. end)
  316. pads_listbox:onDoubleClick(function() submit.teleport(form) end)
  317. form.state:get("teleport_button"):onClick(function() submit.teleport(form) end)
  318. form.state:get("local_button"):onClick(function()
  319. minetest.after(0, function()
  320. tpad.on_rightclick(form.clicked_pos, form.node, form.clicker)
  321. end)
  322. end)
  323. end
  324. function submit.delete(form)
  325. minetest.after(0, function()
  326. local pads_listbox = form.state:get("pads_listbox")
  327. local selected_item = pads_listbox:getSelectedItem()
  328. local delete_pad = form.localnet.by_name[selected_item]
  329. if not delete_pad then
  330. notify.warn(form.playername, "Please select a pad first")
  331. return
  332. end
  333. if form.playername ~= form.ownername and not minetest.get_player_privs(form.playername).tpad_admin then
  334. notify.warn(form.playername, "The selected pad doesn't belong to you")
  335. return
  336. end
  337. if minetest.pos_to_string(delete_pad.pos) == minetest.pos_to_string(form.clicked_pos) then
  338. notify.warn(form.playername, "You can't delete the current pad, destroy it manually")
  339. return
  340. end
  341. local function reshow_main()
  342. minetest.after(0, function()
  343. tpad.on_rightclick(form.clicked_pos, form.node, minetest.get_player_by_name(form.playername))
  344. end)
  345. end
  346. local delete_state = tpad.forms.confirm_pad_deletion:show(form.playername)
  347. delete_state:get("padname_label"):setText(
  348. YELLOW_ESCAPE .. delete_pad.local_fullname ..
  349. WHITE_ESCAPE .. " by " ..
  350. OWNER_ESCAPE_COLOR .. form.ownername
  351. )
  352. local confirm_button = delete_state:get("confirm_button")
  353. confirm_button:onClick(function()
  354. last_selected_index[form.playername .. ":" .. form.ownername] = nil
  355. tpad.del_pad(form.ownername, delete_pad.pos)
  356. minetest.remove_node(delete_pad.pos)
  357. notify(form.playername, "Pad " .. delete_pad.local_fullname .. " destroyed")
  358. reshow_main()
  359. end)
  360. local deny_button = delete_state:get("deny_button")
  361. deny_button:onClick(reshow_main)
  362. end)
  363. end
  364. function tpad.on_rightclick(clicked_pos, node, clicker)
  365. local playername = clicker:get_player_name()
  366. local clicked_meta = minetest.get_meta(clicked_pos)
  367. local ownername = clicked_meta:get_string("owner")
  368. local pad = tpad.get_pad_data(clicked_pos)
  369. if not pad or not ownername then
  370. notify.err(playername, "Error! Missing pad data!")
  371. return
  372. end
  373. local form = {}
  374. form.playername = playername
  375. form.clicker = clicker
  376. form.ownername = ownername
  377. form.clicked_pos = clicked_pos
  378. form.node = node
  379. form.omit_private_pads = false
  380. last_clicked_pos[playername] = clicked_pos;
  381. if ownername == playername or minetest.get_player_privs(playername).tpad_admin then
  382. form.formname = "tpad.forms.main_owner"
  383. form.state = tpad.forms.main_owner:show(playername)
  384. local padname_field = form.state:get("padname_field")
  385. padname_field:setLabel("This pad name (owned by " .. OWNER_ESCAPE_COLOR .. ownername .. WHITE_ESCAPE .. ")")
  386. padname_field:setText(pad.name)
  387. padname_field:onKeyEnter(function() submit.save(form) end)
  388. form.state:get("save_button"):onClick(function() submit.save(form) end)
  389. form.state:get("delete_button"):onClick(function() submit.delete(form) end)
  390. form.state:get("padtype_dropdown"):setSelectedItem(padtype_flag_to_string[pad.type])
  391. elseif pad.type == PRIVATE_PAD then
  392. notify.warn(playername, "This pad is private")
  393. return
  394. else
  395. form.omit_private_pads = true
  396. form.formname = "tpad.forms.main_visitor"
  397. form.state = tpad.forms.main_visitor:show(playername)
  398. form.state:get("visitor_label"):setText("Pad \"" .. pad.name .. "\", owned by " .. OWNER_ESCAPE_COLOR .. ownername)
  399. end
  400. form.localnet = submit.local_helper(ownername, form.omit_private_pads)
  401. local last_click_key = playername .. ":" .. ownername
  402. local last_index = last_selected_index[last_click_key]
  403. local pads_listbox = form.state:get("pads_listbox")
  404. pads_listbox:clearItems()
  405. for _, pad_item in ipairs(form.localnet.by_index) do
  406. pads_listbox:addItem(pad_item)
  407. end
  408. pads_listbox:setSelected(last_index)
  409. pads_listbox:onClick(function()
  410. last_selected_index[last_click_key] = pads_listbox:getSelected()
  411. end)
  412. pads_listbox:onDoubleClick(function() submit.teleport(form) end)
  413. form.state:get("teleport_button"):onClick(function() submit.teleport(form) end)
  414. form.state:get("global_button"):onClick(function()
  415. minetest.after(0, function()
  416. submit.global(form)
  417. end)
  418. end)
  419. end
  420. function tpad.can_dig(pos, player)
  421. local meta = minetest.get_meta(pos)
  422. local ownername = meta:get_string("owner")
  423. local playername = player:get_player_name()
  424. if ownername == "" or ownername == nil or playername == ownername
  425. or minetest.get_player_privs(playername).tpad_admin then
  426. return true
  427. end
  428. notify.warn(playername, "This pad doesn't belong to you")
  429. return false
  430. end
  431. function tpad.on_destruct(pos)
  432. local meta = minetest.get_meta(pos)
  433. local ownername = meta:get_string("owner")
  434. tpad.del_pad(ownername, pos)
  435. end
  436. -- ========================================================================
  437. -- forms
  438. -- ========================================================================
  439. tpad.forms = {}
  440. local function forms_add_padlist(state, is_global)
  441. local pads_listbox = state:listbox(0.2, 2.4, 7.6, 4, "pads_listbox", {})
  442. local teleport_button = state:button(0.2, 7, 1.5, 0, "teleport_button", "Teleport")
  443. local close_button = state:button(6.5, 7, 1.5, 0, "close_button", "Close")
  444. close_button:setClose(true)
  445. if is_global then
  446. local local_button = state:button(1.8, 7, 2.5, 0, "local_button", "Local Network")
  447. local_button:setClose(true)
  448. else
  449. local global_button = state:button(1.8, 7, 2.5, 0, "global_button", "Global Network")
  450. global_button:setClose(true)
  451. end
  452. state:label(0.2, 7.5, "teleport_label", "(you can doubleclick on a pad to teleport)")
  453. end
  454. tpad.forms.main_owner = smartfs.create("tpad.forms.main_owner", function(state)
  455. state:size(8, 8);
  456. state:field(0.5, 1, 6, 0, "padname_field", "", "")
  457. local save_button = state:button(6.5, 0.7, 1.5, 0, "save_button", "Save")
  458. save_button:setClose(true)
  459. local padtype_dropdown = state:dropdown(0.2, 1.2, 6.4, 0, "padtype_dropdown")
  460. padtype_dropdown:addItem(PRIVATE_PAD_STRING)
  461. padtype_dropdown:addItem(PUBLIC_PAD_STRING)
  462. padtype_dropdown:addItem(GLOBAL_PAD_STRING)
  463. local delete_button = state:button(4.4, 7, 1.5, 0, "delete_button", "Delete")
  464. forms_add_padlist(state)
  465. end)
  466. tpad.forms.main_visitor = smartfs.create("tpad.forms.main_visitor", function(state)
  467. state:size(8, 8)
  468. state:label(0.2, 1, "visitor_label", "")
  469. forms_add_padlist(state)
  470. end)
  471. tpad.forms.confirm_pad_deletion = smartfs.create("tpad.forms.confirm_pad_deletion", function(state)
  472. state:size(8, 2.5)
  473. state:label(0, 0, "intro_label", "Are you sure you want to destroy pad")
  474. state:label(0, 0.5, "padname_label", "")
  475. state:label(0, 1, "outro_label", "(you will not get the pad back)")
  476. state:button(0, 2.2, 2, 0, "confirm_button", "Yes, delete it")
  477. state:button(6, 2.2, 2, 0, "deny_button", "No, keep it")
  478. end)
  479. tpad.forms.global_network = smartfs.create("tpad.forms.global_network", function(state)
  480. state:size(8, 8)
  481. state:label(0.2, 1, "visitor_label", "Pick a pad from the Global Pads Network")
  482. local is_global = true
  483. forms_add_padlist(state, is_global)
  484. end)
  485. tpad.forms.global_network_admin = smartfs.create("tpad.forms.global_network_admin", function(state)
  486. state:size(8, 8)
  487. state:label(0.2, 1, "visitor_label", "Pick a pad from the Global Pads Network")
  488. local admin_button = state:button(4.4, 7, 1.5, 0, "admin_button", "Admin")
  489. admin_button:setClose(true)
  490. local is_global = true
  491. forms_add_padlist(state, is_global)
  492. end)
  493. tpad.forms.admin = smartfs.create("tpad.forms.admin", function(state)
  494. state:size(8, 8)
  495. state:label(0.2, 0.2, "admin_label", "TPAD Settings")
  496. state:field(0.5, 2, 6, 0, "max_total_field", "Max total pads per player")
  497. state:field(0.5, 3.5, 6, 0, "max_global_field", "Max global pads per player")
  498. local save_button = state:button(6.5, 0.7, 1.5, 0, "save_button", "Save")
  499. save_button:setClose(true)
  500. local close_button = state:button(6.5, 7, 1.5, 0, "close_button", "Close")
  501. close_button:setClose(true)
  502. end)
  503. -- ========================================================================
  504. -- helper functions
  505. -- ========================================================================
  506. function tpad.decorate_pad_data(pos, pad, ownername)
  507. pad = table.copy(pad)
  508. if type(pos) == "string" then
  509. pad.strpos = pos
  510. pad.pos = minetest.string_to_pos(pos)
  511. else
  512. pad.pos = pos
  513. pad.strpos = minetest.pos_to_string(pos)
  514. end
  515. pad.owner = ownername
  516. pad.name = pad.name or ""
  517. pad.type = pad.type or PUBLIC_PAD
  518. pad.local_fullname = pad.name .. " " .. pad.strpos .. " " .. short_padtype_string[pad.type]
  519. pad.global_fullname = "[" .. ownername .. "] " .. pad.name .. " " .. pad.strpos
  520. return pad
  521. end
  522. function tpad.get_pad_data(pos)
  523. local meta = minetest.get_meta(pos)
  524. local ownername = meta:get_string("owner")
  525. local pads = tpad._get_stored_pads(ownername)
  526. local strpos = minetest.pos_to_string(pos)
  527. local pad = pads[strpos]
  528. if not pad then return end
  529. return tpad.decorate_pad_data(pos, pad, ownername)
  530. end
  531. function tpad.set_pad_data(pos, padname, padtype)
  532. local meta = minetest.get_meta(pos)
  533. local ownername = meta:get_string("owner")
  534. local pads = tpad._get_stored_pads(ownername)
  535. local strpos = minetest.pos_to_string(pos)
  536. local pad = pads[strpos]
  537. if not pad then
  538. pad = {}
  539. end
  540. pad.name = padname
  541. pad.type = padtype_string_to_flag[padtype]
  542. pads[strpos] = pad
  543. tpad._set_stored_pads(ownername, pads)
  544. end
  545. function tpad.del_pad(ownername, pos)
  546. local pads = tpad._get_stored_pads(ownername)
  547. pads[minetest.pos_to_string(pos)] = nil
  548. tpad._set_stored_pads(ownername, pads)
  549. end
  550. -- ========================================================================
  551. -- register node and bind callbacks
  552. -- ========================================================================
  553. local collision_box = {
  554. type = "fixed",
  555. fixed = {
  556. { -0.5, -0.5, -0.5, 0.5, -0.3, 0.5 },
  557. }
  558. }
  559. minetest.register_node(tpad.nodename, {
  560. drawtype = "mesh",
  561. tiles = { tpad.texture },
  562. mesh = tpad.mesh,
  563. paramtype = "light",
  564. paramtype2 = "facedir",
  565. on_place = tpad.on_place,
  566. collision_box = collision_box,
  567. selection_box = collision_box,
  568. description = "Teleporter Pad",
  569. groups = {choppy = 2, dig_immediate = 2},
  570. on_rightclick = tpad.on_rightclick,
  571. can_dig = tpad.can_dig,
  572. on_destruct = tpad.on_destruct,
  573. })
  574. minetest.register_chatcommand("tpad", {func = tpad.command})