pcnaming.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. minetest.register_craftitem("advtrains_luaautomation:pcnaming",{
  24. description = attrans("Passive Component Naming Tool\n\nRight-click to name a passive component."),
  25. groups = {cracky=1}, -- key=name, value=rating; rating=1..3.
  26. inventory_image = "atlatc_pcnaming.png",
  27. wield_image = "atlatc_pcnaming.png",
  28. stack_max = 1,
  29. on_place = function(itemstack, placer, pointed_thing)
  30. local pname = placer:get_player_name()
  31. if not pname then
  32. return
  33. end
  34. if not minetest.check_player_privs(pname, {atlatc=true}) then
  35. minetest.chat_send_player(pname, "Missing privilege: atlatc")
  36. return
  37. end
  38. if pointed_thing.type=="node" then
  39. local pos=pointed_thing.under
  40. if advtrains.is_protected(pos, pname) then
  41. minetest.record_protection_violation(pos, pname)
  42. return
  43. end
  44. local node = advtrains.ndb.get_node(pos)
  45. local ndef = minetest.registered_nodes[node.name]
  46. if node.name and (
  47. minetest.get_item_group(node.name, "advtrains_signal")>0 --is IL signal
  48. or advtrains.is_passive(pos) -- is passive component
  49. or (ndef and ndef.luaautomation) -- is active component
  50. ) then
  51. --look if this one already has a name
  52. local pn=""
  53. for name, npos in pairs(atlatc.pcnaming.name_map) do
  54. if vector.equals(npos, pos) then
  55. pn=name
  56. end
  57. end
  58. minetest.show_formspec(pname, "atlatc_naming_"..minetest.pos_to_string(pos), "field[pn;Set name of component (empty to clear);"..minetest.formspec_escape(pn).."]")
  59. end
  60. end
  61. end,
  62. })
  63. minetest.register_on_player_receive_fields(function(player, formname, fields)
  64. local pts=string.match(formname, "^atlatc_naming_(.+)")
  65. if pts then
  66. local pos=minetest.string_to_pos(pts)
  67. if fields.pn then
  68. --first remove all occurences
  69. for name, npos in pairs(atlatc.pcnaming.name_map) do
  70. if vector.equals(npos, pos) then
  71. atlatc.pcnaming.name_map[name]=nil
  72. end
  73. end
  74. if fields.pn~="" then
  75. atlatc.pcnaming.name_map[fields.pn]=pos
  76. end
  77. end
  78. end
  79. end)