pcnaming.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --pcnaming.lua
  2. --a.k.a Passive component naming
  3. --Allows to assign names to passive components, so they can be called like:
  4. --setstate("iamasignal", "green")
  5. atlatc.pcnaming={name_map={}}
  6. function atlatc.pcnaming.load(stuff)
  7. if type(stuff)=="table" then
  8. atlatc.pcnaming.name_map=stuff
  9. end
  10. end
  11. function atlatc.pcnaming.save()
  12. return atlatc.pcnaming.name_map
  13. end
  14. function atlatc.pcnaming.resolve_pos(pos, func_name)
  15. if type(pos)=="string" then
  16. local e = atlatc.pcnaming.name_map[pos]
  17. if e then return e end
  18. elseif type(pos)=="table" and pos.x and pos.y and pos.z then
  19. return pos
  20. end
  21. error("Invalid position supplied to " .. (func_name or "???")..": " .. dump(pos))
  22. end
  23. local pcrename = {}
  24. minetest.register_craftitem("advtrains_luaautomation:pcnaming",{
  25. description = attrans("Passive Component Naming Tool\n\nRight-click to name a passive component."),
  26. groups = {cracky=1}, -- key=name, value=rating; rating=1..3.
  27. inventory_image = "atlatc_pcnaming.png",
  28. wield_image = "atlatc_pcnaming.png",
  29. stack_max = 1,
  30. on_place = function(itemstack, placer, pointed_thing)
  31. local pname = placer:get_player_name()
  32. if not pname then
  33. return
  34. end
  35. if not minetest.check_player_privs(pname, {atlatc=true}) then
  36. minetest.chat_send_player(pname, "Missing privilege: atlatc")
  37. return
  38. end
  39. if pointed_thing.type=="node" then
  40. local pos=pointed_thing.under
  41. if advtrains.is_protected(pos, pname) then
  42. minetest.record_protection_violation(pos, pname)
  43. return
  44. end
  45. local node = advtrains.ndb.get_node(pos)
  46. local ndef = minetest.registered_nodes[node.name]
  47. if node.name and (
  48. minetest.get_item_group(node.name, "advtrains_signal")>0 --is IL signal
  49. or advtrains.is_passive(pos) -- is passive component
  50. or (ndef and ndef.luaautomation) -- is active component
  51. ) then
  52. --look if this one already has a name
  53. local pn=""
  54. for name, npos in pairs(atlatc.pcnaming.name_map) do
  55. if vector.equals(npos, pos) then
  56. pn=name
  57. end
  58. end
  59. pcrename[pname] = pos
  60. minetest.show_formspec(pname, "atlatc_naming", "field[pn;Set name of component (empty to clear);"..minetest.formspec_escape(pn).."]")
  61. end
  62. end
  63. end,
  64. })
  65. minetest.register_on_player_receive_fields(function(player, formname, fields)
  66. if formname == "atlatc_naming" then
  67. local pname = player:get_player_name()
  68. local pos=pcrename[pname]
  69. if fields.pn and pos then
  70. --first remove all occurences
  71. for name, npos in pairs(atlatc.pcnaming.name_map) do
  72. if vector.equals(npos, pos) then
  73. atlatc.pcnaming.name_map[name]=nil
  74. end
  75. end
  76. if fields.pn~="" then
  77. atlatc.pcnaming.name_map[fields.pn]=pos
  78. end
  79. end
  80. end
  81. end)