init.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. marker = marker or {}
  2. marker.modpath = minetest.get_modpath("marker")
  3. marker.players = marker.players or {}
  4. marker.gui = marker.gui or {}
  5. marker.steptime = 1
  6. marker.max_waypoints = 100
  7. marker.max_lists = 50
  8. -- Max distance of players who wish to share marker lists.
  9. marker.proximity_range = 10
  10. -- Min/max ranges for visual particles.
  11. marker.pr_min = 40
  12. marker.pr_max = 50
  13. -- Min/max ranges for HUD text objects.
  14. marker.ht_min = 12
  15. marker.ht_max1 = 100
  16. marker.ht_max2 = 110
  17. local timer = 0
  18. local delay = marker.steptime
  19. function marker.on_globalstep(dtime)
  20. timer = timer + dtime
  21. if timer < delay then return end
  22. timer = 0
  23. marker.update_huds()
  24. end
  25. function marker.update_huds()
  26. -- localize
  27. -- create gui for player if not created already
  28. local players = marker.players
  29. for k, v in pairs(players) do
  30. marker.update_single_hud(k)
  31. end
  32. end
  33. function marker.on_leaveplayer(pref, timeout)
  34. local pname = pref:get_player_name()
  35. marker.players[pname] = nil
  36. marker.gui[pname] = nil
  37. end
  38. function marker.update_single_hud(player)
  39. local allgui = marker.gui
  40. if not allgui[player] then
  41. marker.create_gui(player)
  42. end
  43. local gui = allgui[player]
  44. local waypoints = gui.waypoints
  45. local pref = minetest.get_player_by_name(player)
  46. if not pref or not pref:is_player() then
  47. return
  48. end
  49. local d = vector.distance
  50. local p2 = pref:get_pos()
  51. for i = 1, #waypoints, 1 do
  52. local data = waypoints[i]
  53. local dist = d(data.pos, p2)
  54. if dist > marker.ht_min and dist < marker.ht_max1 then
  55. -- add hud element if nearby and not already added
  56. if not data.hnd then
  57. local wp = vector.add(data.pos, {x=0, y=1, z=0})
  58. local number = "7902626"
  59. if data.highlight then
  60. number = "8739932"
  61. end
  62. data.hnd = pref:hud_add({
  63. hud_elem_type = "waypoint",
  64. name = "Marker",
  65. number = number,
  66. world_pos = wp,
  67. })
  68. end
  69. elseif dist < marker.ht_min or dist > marker.ht_max2 then
  70. -- remove hud element if too far and not yet removed
  71. if data.hnd then
  72. pref:hud_remove(data.hnd)
  73. data.hnd = nil
  74. end
  75. end
  76. if dist < marker.pr_min then
  77. if not data.pts then
  78. local wp = vector.add(data.pos, {x=0, y=0.5, z=0})
  79. local particles = {
  80. amount = 20,
  81. time = 0,
  82. minpos = vector.add(wp, {x=-0.1, y=-0.1, z=-0.1}),
  83. maxpos = vector.add(wp, {x=0.1, y=0.1, z=0.1}),
  84. minvel = vector.new(-0.5, -0.5, -0.5),
  85. maxvel = vector.new(0.5, 0.5, 0.5),
  86. minacc = {x=0, y=0, z=0},
  87. maxacc = {x=0, y=0, z=0},
  88. minexptime = 0.5,
  89. maxexptime = 2.0,
  90. minsize = 1,
  91. maxsize = 1,
  92. collisiondetection = false,
  93. collision_removal = false,
  94. vertical = false,
  95. texture = "quartz_crystal_piece.png",
  96. glow = 14,
  97. playername = player,
  98. }
  99. data.pts = minetest.add_particlespawner_single(particles)
  100. end
  101. elseif dist > marker.pr_max then
  102. if data.pts then
  103. minetest.delete_particlespawner(data.pts, player)
  104. data.pts = nil
  105. end
  106. end
  107. end
  108. end
  109. function marker.update_hud_markers(player, list, highlight)
  110. -- localize
  111. local allgui = marker.gui
  112. if not allgui[player] then
  113. marker.create_gui(player)
  114. end
  115. local gui = allgui[player]
  116. local waypoints = gui.waypoints
  117. -- remove all existing hud elements
  118. local pref = minetest.get_player_by_name(player)
  119. if not pref or not pref:is_player() then
  120. return
  121. end
  122. for i = 1, #waypoints, 1 do
  123. local data = waypoints[i]
  124. if data.hnd then
  125. pref:hud_remove(data.hnd)
  126. data.hnd = nil
  127. end
  128. if data.pts then
  129. minetest.delete_particlespawner(data.pts, player)
  130. data.pts = nil
  131. end
  132. if data.highlight then
  133. data.highlight = nil
  134. end
  135. end
  136. -- update waypoint list data
  137. gui.waypoints = {}
  138. waypoints = gui.waypoints
  139. if gui.show and list and list ~= "" then
  140. local data = marker.get_list(player, list)
  141. for i = 1, #data, 1 do
  142. waypoints[#waypoints + 1] = {pos=table.copy(data[i])}
  143. if highlight and highlight == i then
  144. waypoints[#waypoints].highlight = true
  145. end
  146. end
  147. end
  148. marker.update_single_hud(player)
  149. end
  150. function marker.highlight_marker(player, index)
  151. -- localize
  152. local allgui = marker.gui
  153. if not allgui[player] then
  154. marker.create_gui(player)
  155. end
  156. local gui = allgui[player]
  157. local waypoints = gui.waypoints
  158. for i = 1, #waypoints, 1 do
  159. local data = waypoints[i]
  160. if data.highlight then
  161. data.highlight = nil
  162. -- return marker to regular color
  163. if data.hnd then
  164. local pref = minetest.get_player_by_name(player)
  165. if pref and pref:is_player() then
  166. pref:hud_change(data.hnd, "number", "7902626")
  167. end
  168. end
  169. end
  170. end
  171. if index >= 1 and index <= #waypoints then
  172. local data = waypoints[index]
  173. if not data.highlight then
  174. data.highlight = true
  175. -- change (highlight) marker color
  176. if data.hnd then
  177. local pref = minetest.get_player_by_name(player)
  178. if pref and pref:is_player() then
  179. pref:hud_change(data.hnd, "number", "8739932")
  180. end
  181. end
  182. end
  183. end
  184. end
  185. -- private: load player data
  186. function marker.load_player(player)
  187. -- localize
  188. local players = marker.players
  189. -- load player data from mod storage
  190. local str = marker.storage:get_string(player)
  191. assert(type(str) == "string")
  192. if str == "" then
  193. players[player] = {}
  194. return
  195. end
  196. local lists = minetest.deserialize(str)
  197. if not lists then
  198. players[player] = {}
  199. return
  200. end
  201. -- data is now loaded
  202. assert(type(lists) == "table")
  203. players[player] = lists
  204. end
  205. -- private: save player data
  206. function marker.save_player(player)
  207. -- localize
  208. local players = marker.players
  209. -- localize
  210. local lists = players[player] or {}
  211. local str = minetest.serialize(lists)
  212. assert(type(str) == "string")
  213. -- send data to mod storage
  214. marker.storage:set_string(player, str)
  215. end
  216. -- api: player name, position, list name
  217. function marker.add_waypoint(player, pos, list)
  218. -- localize
  219. local players = marker.players
  220. -- load data for player if not loaded already
  221. if not players[player] then
  222. marker.load_player(player)
  223. end
  224. -- localize
  225. local lists = players[player]
  226. -- create waypoint list if not created already
  227. local found = false
  228. local positions
  229. for i = 1, #lists, 1 do
  230. if lists[i].name == list then
  231. found = true
  232. positions = lists[i].data
  233. break
  234. end
  235. end
  236. if not found then
  237. lists[#lists + 1] = {name=list, data={}}
  238. positions = lists[#lists].data
  239. end
  240. -- add position
  241. positions[#positions + 1] = vector.round(pos)
  242. -- save changes
  243. marker.save_player(player)
  244. end
  245. -- api: player name, index, list name
  246. function marker.remove_waypoint(player, index, list)
  247. -- localize
  248. local players = marker.players
  249. -- load data for player if not loaded already
  250. if not players[player] then
  251. marker.load_player(player)
  252. end
  253. -- localize
  254. local lists = players[player]
  255. -- search player data for named list
  256. -- ignore if list doesn't exist
  257. local found = false
  258. local positions
  259. for i = 1, #lists, 1 do
  260. if lists[i].name == list then
  261. found = true
  262. positions = lists[i].data
  263. break
  264. end
  265. end
  266. if not found then
  267. return
  268. end
  269. -- localize
  270. local equals = vector.equals
  271. local changed = false
  272. local pos
  273. -- erase position from positions list
  274. if index >= 1 and index <= #positions then
  275. pos = positions[index]
  276. table.remove(positions, index)
  277. changed = true
  278. end
  279. -- save changes if needed
  280. if changed then
  281. marker.save_player(player)
  282. end
  283. -- return pos or nil
  284. return pos
  285. end
  286. -- private: get the list of positions for a given list name
  287. --
  288. -- important: the list can be modified, and if modified, must be saved afterward
  289. -- by calling marker.save_player(), otherwise changes will be lost eventually
  290. function marker.get_list(player, list)
  291. assert(type(list) == "string" and list ~= "")
  292. -- localize
  293. local players = marker.players
  294. -- load data for player if not loaded already
  295. if not players[player] then
  296. marker.load_player(player)
  297. end
  298. -- localize
  299. local lists = players[player]
  300. -- if named list doesn't exist, create it
  301. local found = false
  302. local positions
  303. for i = 1, #lists, 1 do
  304. if lists[i].name == list then
  305. found = true
  306. positions = lists[i].data
  307. break
  308. end
  309. end
  310. if not found then
  311. lists[#lists + 1] = {name=list, data={}}
  312. positions = lists[#lists].data
  313. -- a new list was added, need to save data
  314. marker.save_player(player)
  315. end
  316. -- return the named list
  317. -- the list can be modifed
  318. -- remember to save the data
  319. return positions
  320. end
  321. -- api: player name, list index
  322. function marker.get_list_name(player, index)
  323. -- localize
  324. local players = marker.players
  325. -- load data for player if not loaded already
  326. if not players[player] then
  327. marker.load_player(player)
  328. end
  329. -- localize
  330. local lists = players[player]
  331. local name = ""
  332. for i = 1, #lists, 1 do
  333. if lists[i].name == "default" then
  334. if index >= i then
  335. index = index + 1
  336. -- fix corner case
  337. if index > #lists then
  338. index = #lists
  339. end
  340. end
  341. end
  342. if i == index then
  343. name = lists[i].name
  344. break
  345. end
  346. end
  347. return name
  348. end
  349. -- api: player name, list name
  350. function marker.have_list(player, list)
  351. local players = marker.players
  352. if not players[player] then
  353. return false
  354. end
  355. local alists = players[player]
  356. local found = false
  357. for i = 1, #alists, 1 do
  358. if alists[i].name == list then
  359. found = true
  360. break
  361. end
  362. end
  363. return found
  364. end
  365. -- api: player name, list name
  366. function marker.list_size(player, list)
  367. local players = marker.players
  368. if not players[player] then
  369. return 0
  370. end
  371. local alists = players[player]
  372. local size = 0
  373. for i = 1, #alists, 1 do
  374. if alists[i].name == list then
  375. size = #(alists[i].data)
  376. break
  377. end
  378. end
  379. return size
  380. end
  381. function marker.list_count(player)
  382. local players = marker.players
  383. if not players[player] then
  384. return 0
  385. end
  386. local alists = players[player]
  387. local size = 0
  388. for i = 1, #alists, 1 do
  389. if alists[i].name ~= "default" then
  390. size = size + 1
  391. end
  392. end
  393. return size
  394. end
  395. -- api: player name, list name
  396. function marker.remove_list(player, list)
  397. local players = marker.players
  398. if not players[player] then
  399. return
  400. end
  401. local changed = false
  402. local alists = players[player]
  403. for i = 1, #alists, 1 do
  404. if alists[i].name == list then
  405. table.remove(alists, i)
  406. changed = true
  407. break
  408. end
  409. end
  410. if changed then
  411. marker.save_player(player)
  412. end
  413. end
  414. -- private: create gui-data for player
  415. function marker.create_gui(player)
  416. marker.gui[player] = {
  417. index1 = -1,
  418. index2 = -1,
  419. index3 = -1,
  420. listname = "",
  421. playername = "",
  422. waypoints = {},
  423. }
  424. end
  425. -- private: assemble a formspec string
  426. function marker.get_formspec(player)
  427. -- localize
  428. local players = marker.players
  429. local allgui = marker.gui
  430. -- load data for player if not loaded already
  431. if not players[player] then
  432. marker.load_player(player)
  433. end
  434. local alists = players[player]
  435. -- create gui for player if not created already
  436. if not allgui[player] then
  437. marker.create_gui(player)
  438. end
  439. local gui = allgui[player]
  440. local deflist = marker.get_list(player, "default")
  441. local formspec = "size[9,7]" ..
  442. default.gui_bg ..
  443. default.gui_bg_img ..
  444. default.gui_slots
  445. formspec = formspec ..
  446. "item_image[0,0;1,1;passport:passport_adv]" ..
  447. "label[1,0;Key Device Marker System]" ..
  448. "field[0.3,1.3;2.9,1;listname;;" .. minetest.formspec_escape(gui.listname) .. "]" ..
  449. "field[0.3,2.15;2.9,1;player;;" .. minetest.formspec_escape(gui.playername) .. "]"
  450. formspec = formspec ..
  451. "textlist[5.0,0.0;3.7,2.6;lists;"
  452. local comma = ""
  453. --minetest.log(dump(alists))
  454. for i = 1, #alists, 1 do
  455. local k = alists[i].name
  456. if k ~= "default" then
  457. formspec = formspec .. comma .. k
  458. comma = ","
  459. end
  460. end
  461. formspec = formspec .. ";" .. gui.index1 .. "]" ..
  462. "textlist[0.0,3.0;3.7,3.0;markers;"
  463. for i = 1, #deflist, 1 do
  464. local s = rc.pos_to_namestr(deflist[i])
  465. formspec = formspec .. minetest.formspec_escape(s)
  466. if i < #deflist then
  467. formspec = formspec .. ","
  468. end
  469. end
  470. formspec = formspec .. ";" .. gui.index2 .. "]" ..
  471. "textlist[5.0,3.0;3.7,4.0;positions;"
  472. local lname = marker.get_list_name(player, gui.index1)
  473. if lname and lname ~= "" then
  474. local data = marker.get_list(player, lname)
  475. comma = ""
  476. for i = 1, #data, 1 do
  477. local s = minetest.formspec_escape(rc.pos_to_namestr(data[i]))
  478. formspec = formspec .. comma .. s
  479. comma = ","
  480. end
  481. end
  482. formspec = formspec .. ";" .. gui.index3 .. "]"
  483. formspec = formspec ..
  484. "button[3.0,1.0;1,1;addlist;>]" ..
  485. "button[4.0,1.0;1,1;dellist;X]" ..
  486. "button[3.0,1.85;2,1;sendlist;Send List]" ..
  487. "button[4.0,3.0;1,1;ls;<]" ..
  488. "button[4.0,4.0;1,1;mark;Mark]" ..
  489. "button[4.0,5.0;1,1;rs;>]" ..
  490. "button[0.0,6.25;1,1;done;Done]" ..
  491. "button[1.0,6.25;1,1;delete;Erase]"
  492. if not gui.show then
  493. formspec = formspec .. "button[2.0,6.25;3,1;show;Enable Scan]"
  494. else
  495. formspec = formspec .. "button[2.0,6.25;3,1;hide;Disable Scan]"
  496. end
  497. return formspec
  498. end
  499. -- api: show formspec to player
  500. function marker.show_formspec(player)
  501. local formspec = marker.get_formspec(player)
  502. minetest.show_formspec(player, "marker:fs", formspec)
  503. end
  504. marker.on_receive_fields = function(player, formname, fields)
  505. if formname ~= "marker:fs" then return end
  506. local pname = player:get_player_name()
  507. -- security check to make sure player can use this feature
  508. local inv = player:get_inventory()
  509. if not inv:contains_item("main", "passport:passport_adv") then
  510. return true
  511. end
  512. -- ensure user-data is loaded and available
  513. if not marker.players[pname] then
  514. marker.load_player(pname)
  515. end
  516. -- localize
  517. -- create gui for player if not created already
  518. local allgui = marker.gui
  519. if not allgui[pname] then
  520. marker.create_gui(pname)
  521. end
  522. local gui = allgui[pname]
  523. if fields.done then
  524. passport.show_formspec(pname)
  525. return true
  526. end
  527. if fields.quit then
  528. return true
  529. end
  530. if fields.listname then
  531. gui.listname = fields.listname
  532. end
  533. if fields.player then
  534. gui.playername = fields.player
  535. end
  536. if fields.show then
  537. gui.show = true
  538. if gui.listname and gui.listname ~= "" then
  539. marker.update_hud_markers(pname, gui.listname)
  540. end
  541. end
  542. if fields.hide then
  543. gui.show = nil
  544. marker.update_hud_markers(pname)
  545. end
  546. if fields.addlist then
  547. if marker.list_count(pname) < marker.max_lists then
  548. local name = fields.listname or ""
  549. name = name:trim()
  550. if name == "" then
  551. minetest.chat_send_player(pname, "# Server: Cannot add list with empty name.")
  552. elseif name == "default" then
  553. minetest.chat_send_player(pname, "# Server: Cannot add list with reserved name.")
  554. else
  555. if marker.have_list(pname, name) then
  556. minetest.chat_send_player(pname, "# Server: Cannot add list, it already exists.")
  557. else
  558. -- this automatically adds the list if it doesn't exist
  559. local pos = player:get_pos()
  560. marker.add_waypoint(pname, pos, name)
  561. gui.index1 = #(marker.players[pname])
  562. gui.index3 = -1
  563. marker.update_hud_markers(pname, name)
  564. minetest.chat_send_player(pname, "# Server: Added list.")
  565. end
  566. end
  567. else
  568. minetest.chat_send_player(pname, "# Server: Marker list roster is full, cannot create a new marker list.")
  569. end
  570. elseif fields.dellist then
  571. local name = fields.listname or ""
  572. name = name:trim()
  573. if name == "" then
  574. minetest.chat_send_player(pname, "# Server: Cannot remove list with empty name.")
  575. elseif name == "default" then
  576. minetest.chat_send_player(pname, "# Server: Cannot remove list with reserved name.")
  577. else
  578. if marker.have_list(pname, name) then
  579. marker.remove_list(pname, name)
  580. gui.index1 = -1
  581. gui.listname = ""
  582. marker.update_hud_markers(pname)
  583. minetest.chat_send_player(pname, "# Server: Removed marker list.")
  584. else
  585. minetest.chat_send_player(pname, "# Server: Cannot remove non-existent list.")
  586. end
  587. end
  588. elseif fields.sendlist then
  589. local targetname = fields.player or ""
  590. targetname = targetname:trim()
  591. if targetname ~= "" then
  592. targetname = rename.grn(targetname)
  593. if targetname ~= pname then
  594. local ptarget = minetest.get_player_by_name(targetname)
  595. if ptarget and ptarget:is_player() then
  596. if vector.distance(ptarget:get_pos(), player:get_pos()) < marker.proximity_range then
  597. local inv = ptarget:get_inventory()
  598. if inv:contains_item("main", "passport:passport_adv") then
  599. local name = marker.get_list_name(pname, gui.index1)
  600. if name and name ~= "" then
  601. -- load data for target player if not loaded already
  602. -- otherwise checking to see if player already has list could fail
  603. -- in a very wrong way
  604. if not marker.players[targetname] then
  605. marker.load_player(targetname)
  606. end
  607. if marker.have_list(targetname, name) then
  608. minetest.chat_send_player(pname, "# Server: The other Key already hosts a marker list with that name. Cannot transfer data!")
  609. else
  610. if marker.list_count(targetname) < marker.max_lists then
  611. local datafrom = marker.get_list(pname, name)
  612. -- because we checked to make sure the target key doesn't have this list already,
  613. -- we have assured that this will create it for the first time, and it will be empty
  614. local datato = marker.get_list(targetname, name)
  615. for i = 1, #datafrom, 1 do
  616. datato[#datato + 1] = table.copy(datafrom[i])
  617. end
  618. minetest.chat_send_player(pname, "# Server: Marker list sent!")
  619. minetest.chat_send_player(targetname, "# Server: <" .. rename.gpn(pname) .. "> sent a marker list to your Key!")
  620. else
  621. minetest.chat_send_player(pname, "# Server: The other Key's marker list storage is full! Cannot transfer data.")
  622. end
  623. end
  624. else
  625. minetest.chat_send_player(pname, "# Server: You must select a marker list, first.")
  626. end
  627. else
  628. minetest.chat_send_player(pname, "# Server: The other player does not have a Key!")
  629. end
  630. else
  631. minetest.chat_send_player(pname, "# Server: You need to stand close to the other player's Key to transfer a marker list.")
  632. end
  633. else
  634. minetest.chat_send_player(pname, "# Server: The specified player is not available.")
  635. end
  636. else
  637. minetest.chat_send_player(pname, "# Server: Cannot send marker list to your own Key.")
  638. end
  639. else
  640. minetest.chat_send_player(pname, "# Server: You must specify the name of a player to send a marker list to.")
  641. end
  642. elseif fields.ls then
  643. local name = marker.get_list_name(pname, gui.index1)
  644. if name and name ~= "" then
  645. if marker.list_size(pname, "default") < marker.max_waypoints then
  646. local data = marker.get_list(pname, name)
  647. if gui.index3 >= 1 and gui.index3 <= #data then
  648. local pos = marker.remove_waypoint(pname, gui.index3, name)
  649. if pos then
  650. marker.add_waypoint(pname, pos, "default")
  651. local deflist = marker.get_list(pname, "default")
  652. gui.index2 = #deflist
  653. gui.index3 = -1
  654. marker.update_hud_markers(pname, name)
  655. minetest.chat_send_player(pname, "# Server: Moved marker to free-list.")
  656. else
  657. minetest.chat_send_player(pname, "# Server: Could not remove marker from list.")
  658. end
  659. else
  660. minetest.chat_send_player(pname, "# Server: You need to have selected a marker to be moved.")
  661. end
  662. else
  663. minetest.chat_send_player(pname, "# Server: Cannot remove marker from list, free-list storage is full.")
  664. end
  665. else
  666. minetest.chat_send_player(pname, "# Server: You must select a marker list, first.")
  667. end
  668. elseif fields.rs then
  669. local name = marker.get_list_name(pname, gui.index1)
  670. if name and name ~= "" then
  671. if marker.list_size(pname, name) < marker.max_waypoints then
  672. local data = marker.get_list(pname, "default")
  673. if gui.index2 >= 1 and gui.index2 <= #data then
  674. local pos = marker.remove_waypoint(pname, gui.index2, "default")
  675. if pos then
  676. marker.add_waypoint(pname, pos, name)
  677. local thelist = marker.get_list(pname, name)
  678. gui.index3 = #thelist
  679. gui.index2 = -1
  680. marker.update_hud_markers(pname, name, gui.index3)
  681. minetest.chat_send_player(pname, "# Server: Moved unattached marker to list.")
  682. else
  683. minetest.chat_send_player(pname, "# Server: Could not remove marker from free-list.")
  684. end
  685. else
  686. minetest.chat_send_player(pname, "# Server: You need to have selected a marker to be moved.")
  687. end
  688. else
  689. minetest.chat_send_player(pname, "# Server: Cannot add marker to list, list storage is full.")
  690. end
  691. else
  692. minetest.chat_send_player(pname, "# Server: You must select a marker list, first.")
  693. end
  694. elseif fields.mark then
  695. if marker.list_size(pname, "default") < marker.max_waypoints then
  696. local pos = player:get_pos()
  697. marker.add_waypoint(pname, pos, "default")
  698. local deflist = marker.get_list(pname, "default")
  699. gui.index2 = #deflist
  700. minetest.chat_send_player(pname, "# Server: Placed marker at " .. rc.pos_to_namestr(vector.round(pos)) .. ".")
  701. else
  702. minetest.chat_send_player(pname, "# Server: Cannot place a new marker, storage is full.")
  703. end
  704. elseif fields.delete then
  705. local index = gui.index2
  706. local deflist = marker.get_list(pname, "default")
  707. if index >= 1 and index <= #deflist then
  708. local s = rc.pos_to_namestr(deflist[index])
  709. marker.remove_waypoint(pname, index, "default")
  710. gui.index2 = -1
  711. minetest.chat_send_player(pname, "# Server: Removed marker: " .. s .. ".")
  712. else
  713. minetest.chat_send_player(pname, "# Server: You must select a marker from the marker list, to erase it.")
  714. end
  715. elseif fields.lists then
  716. local event = minetest.explode_textlist_event(fields.lists)
  717. if event.type == "CHG" then
  718. local index = event.index
  719. gui.index1 = index
  720. gui.index3 = -1
  721. gui.listname = marker.get_list_name(pname, index)
  722. marker.update_hud_markers(pname, gui.listname)
  723. end
  724. elseif fields.markers then
  725. local event = minetest.explode_textlist_event(fields.markers)
  726. if event.type == "CHG" then
  727. local index = event.index
  728. gui.index2 = index
  729. end
  730. elseif fields.positions then
  731. local event = minetest.explode_textlist_event(fields.positions)
  732. if event.type == "CHG" then
  733. local index = event.index
  734. gui.index3 = index
  735. marker.highlight_marker(pname, index)
  736. end
  737. elseif fields.listname then
  738. -- nothing here atm
  739. elseif fields.player then
  740. -- nothing here atm
  741. end
  742. marker.show_formspec(pname)
  743. return true
  744. end
  745. if not marker.registered then
  746. marker.storage = minetest.get_mod_storage()
  747. minetest.register_on_player_receive_fields(function(...)
  748. return marker.on_receive_fields(...)
  749. end)
  750. minetest.register_globalstep(function(...)
  751. return marker.on_globalstep(...)
  752. end)
  753. minetest.register_on_leaveplayer(function(...)
  754. return marker.on_leaveplayer(...)
  755. end)
  756. local c = "marker:core"
  757. local f = marker.modpath .. "/init.lua"
  758. reload.register_file(c, f, false)
  759. marker.registered = true
  760. end