add_station.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local S = minetest.get_translator("travelnet")
  2. return function (node_info, fields, player)
  3. local player_name = player:get_player_name()
  4. if not player_name then
  5. return false, S("The give player is not a player")
  6. end -- this should never happen, but just in case
  7. if not minetest.check_player_privs(player_name, { interact=true }) then
  8. return false, S("There is no player with interact privilege named '@1'. Aborting.", player_name)
  9. end
  10. local pos = node_info.pos
  11. local meta = node_info.meta
  12. local station_name = fields.station_name or node_info.props.station_name
  13. local station_network = fields.station_network or node_info.props.station_network
  14. local owner_name = fields.owner_name or node_info.props.owner_name
  15. -- if it is an elevator, determine the network name through x and z coordinates
  16. local is_elevator = node_info.props.is_elevator
  17. if is_elevator then
  18. station_network = travelnet.elevator_network(pos)
  19. if travelnet.is_falsey_string(station_name) then
  20. station_name = S("at @1 m", tostring(pos.y))
  21. end
  22. end
  23. if travelnet.is_falsey_string(station_name) then
  24. return false, S("Please provide a name for this station.")
  25. end
  26. if travelnet.is_falsey_string(station_network) then
  27. return false, S("Please provide the name of the network this station ought to be connected to.")
  28. end
  29. if travelnet.is_falsey_string(owner_name) or owner_name == player_name or is_elevator then -- elevator networks
  30. owner_name = player_name
  31. elseif not minetest.get_player_privs(player_name)[travelnet.attach_priv]
  32. and not travelnet.allow_attach(player_name, owner_name, station_network)
  33. then
  34. return false, S("You do not have the travelnet_attach priv which is required to attach your box to " ..
  35. "the network of someone else. Aborting.")
  36. end
  37. local travelnets = travelnet.get_travelnets(owner_name)
  38. local network = travelnets[station_network]
  39. if not network then
  40. network = {}
  41. travelnets[station_network] = network
  42. end
  43. -- lua doesn't allow efficient counting here
  44. local station_count = 1 -- start at one, assume the station about to be created already exists
  45. for existing_station_name in pairs(network) do
  46. if existing_station_name == station_name then
  47. return false, S("A station named '@1' already exists on this network. Please choose a different name!", station_name)
  48. end
  49. station_count = station_count+1
  50. end
  51. -- we don't want too many stations in the same network because that would get confusing when displaying the targets
  52. if travelnet.MAX_STATIONS_PER_NETWORK ~= 0 and station_count > travelnet.MAX_STATIONS_PER_NETWORK then
  53. return false, S("Network '@1', already contains the maximum number (@2) of allowed stations per network. " ..
  54. "Please choose a different/new network name.", station_network, travelnet.MAX_STATIONS_PER_NETWORK)
  55. end
  56. -- add this station
  57. local creation_timestamp = os.time()
  58. network[station_name] = {
  59. pos = pos,
  60. timestamp = creation_timestamp
  61. }
  62. -- save the updated network data
  63. travelnet.log("action", "adding station '" .. station_name .. "' to network '" .. station_network ..
  64. "' for player '" .. owner_name .. "'")
  65. travelnet.set_travelnets(owner_name, travelnets)
  66. -- do we have a new node to set up? (and are not just reading from a safefile?)
  67. if meta then
  68. minetest.chat_send_player(player_name,
  69. S("Station '@1'" .. " " ..
  70. "has been added to the network '@2'" ..
  71. ", which now consists of @3 station(s).", station_name, station_network, station_count))
  72. meta:set_string("station_name", station_name)
  73. meta:set_string("station_network", station_network)
  74. meta:set_string("owner", owner_name)
  75. meta:set_int ("timestamp", creation_timestamp)
  76. meta:set_string("infotext",
  77. S("Station '@1'" .. " " ..
  78. "on network '@2' (owned by @3)" .. " " ..
  79. "ready for usage.",
  80. tostring(station_name), tostring(station_network), tostring(owner_name)))
  81. return true, { formspec = travelnet.formspecs.primary, options = {
  82. station_name = station_name,
  83. station_network = station_network,
  84. owner_name = owner_name
  85. } }
  86. end
  87. end