formspecs.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. local S = minetest.get_translator("travelnet")
  2. local player_formspec_data = travelnet.player_formspec_data
  3. travelnet.formspecs = {}
  4. function travelnet.formspecs.current(options, player_name)
  5. local current_form = player_formspec_data[player_name] and player_formspec_data[player_name].current_form
  6. if current_form then
  7. return current_form(options, player_name)
  8. end
  9. if travelnet.is_falsey_string(options.station_network) then
  10. -- request initinal data
  11. if options.is_elevator then
  12. return travelnet.formspecs.edit_elevator(options, player_name)
  13. else
  14. return travelnet.formspecs.edit_travelnet(options, player_name)
  15. end
  16. else
  17. return travelnet.formspecs.primary(options, player_name)
  18. end
  19. end
  20. function travelnet.formspecs.error_message(options)
  21. if not options then options = {} end
  22. return ([[
  23. size[8,3]
  24. label[3,0;%s]
  25. textarea[0.5,0.5;7,1.5;;%s;]
  26. button[3.5,2.5;1.0,0.5;back;%s]
  27. button[6.8,2.5;1.0,0.5;station_exit;%s]
  28. ]]):format(
  29. minetest.formspec_escape(options.title or S("Error")),
  30. minetest.formspec_escape(options.message or "- nothing -"),
  31. S("Back"),
  32. S("Exit")
  33. )
  34. end
  35. function travelnet.formspecs.edit_travelnet(options)
  36. if not options then options = {} end
  37. -- some players seem to be confused with entering network names at first; provide them
  38. -- with a default name
  39. local default_network = "net1"
  40. return ([[
  41. size[10,6.0]
  42. label[2.0,0.0;--> %s <--]
  43. button[8.0,0.0;2.2,0.7;station_dig;%s]
  44. field[0.3,1.2;9,0.9;station_name;%s;%s]
  45. label[0.3,1.5;%s]
  46. field[0.3,2.8;9,0.9;station_network;%s;%s]
  47. label[0.3,3.1;%s]
  48. field[0.3,4.4;9,0.9;owner_name;%s;%s]
  49. label[0.3,4.7;%s]
  50. button[3.8,5.3;1.7,0.7;station_set;%s]
  51. button[6.3,5.3;1.7,0.7;station_exit;%s]
  52. ]]):format(
  53. S("Configure this travelnet station"),
  54. S("Remove station"),
  55. S("Name of this station:"),
  56. minetest.formspec_escape(options.station_name or ""),
  57. S("What do you call this place here? Example: \"my first house\", \"mine\", \"shop\"..."),
  58. S("Assign to network:"),
  59. minetest.formspec_escape(
  60. travelnet.is_falsey_string(options.station_network)
  61. and default_network
  62. or options.station_network
  63. ),
  64. S("You can have more than one network. If unsure, use \"@1\".", default_network),
  65. S("Owned by:"),
  66. minetest.formspec_escape(options.owner_name or ""),
  67. S("Unless you know what you are doing, leave this as is."),
  68. S("Save"),
  69. S("Exit")
  70. )
  71. end
  72. function travelnet.formspecs.edit_elevator(options)
  73. if not options then options = {} end
  74. return ([[
  75. size[10,6.0]
  76. label[2.0,0.0;--> %s <--]
  77. button[8.0,0.0;2.2,0.7;station_dig;%s]
  78. field[0.3,1.2;9,0.9;station_name;%s;%s]
  79. button[3.8,5.3;1.7,0.7;station_set;%s]
  80. button[6.3,5.3;1.7,0.7;station_exit;%s]
  81. ]]):format(
  82. S("Configure this elevator station"),
  83. S("Remove station"),
  84. S("Name of this station:"),
  85. minetest.formspec_escape(options.station_name),
  86. S("Save"),
  87. S("Exit")
  88. )
  89. end
  90. function travelnet.formspecs.primary(options, player_name)
  91. if not options then options = {} end
  92. -- add name of station + network + owner + update-button
  93. local formspec = ([[
  94. size[12,%s]
  95. label[3.3,0.0;%s]
  96. label[0.3,0.4;%s]
  97. label[6.3,0.4;%s]
  98. label[0.3,0.8;%s]
  99. label[6.3,0.8;%s]
  100. label[0.3,1.2;%s]
  101. label[6.3,1.2;%s]
  102. label[3.3,1.6;%s]
  103. button[11.3,0.0;1.0,0.5;station_exit;%s]
  104. ]]):format(
  105. tostring(options.height or 10),
  106. options.is_elevator and S("Elevator:") or S("Travelnet-Box:"),
  107. S("Name of this station:"),
  108. minetest.formspec_escape(options.station_name or "?"),
  109. S("Assigned to Network:"),
  110. minetest.formspec_escape(options.station_network or "?"),
  111. S("Owned by:"),
  112. minetest.formspec_escape(options.owner_name or "?"),
  113. S("Click on target to travel there:"),
  114. S("Exit")
  115. )
  116. local x = 0
  117. local y = 0
  118. local i = 0
  119. -- collect all station names in a table
  120. local stations = travelnet.get_ordered_stations(options.owner_name, options.station_network, options.is_elevator)
  121. -- if there are only 8 stations (plus this one), center them in the formspec
  122. if #stations < 10 then
  123. x = 4
  124. end
  125. local paging = (
  126. travelnet.MAX_STATIONS_PER_NETWORK == 0
  127. or travelnet.MAX_STATIONS_PER_NETWORK > 24
  128. ) and (#stations > 24)
  129. local column_size = paging and 7 or 8
  130. local page_size = column_size*3
  131. local pages = math.ceil(#stations/page_size)
  132. local page_number = options.page_number
  133. if not page_number then
  134. page_number = 1
  135. if paging then
  136. for number,k in ipairs(stations) do
  137. if k == options.station_name then
  138. page_number = math.ceil(number/page_size)
  139. break
  140. end
  141. end
  142. end
  143. end
  144. for n=((page_number-1)*page_size)+1,(page_number*page_size) do
  145. local k = stations[n]
  146. if not k then break end
  147. i = i+1
  148. -- new column
  149. if y == column_size then
  150. x = x + 4
  151. y = 0
  152. end
  153. -- check if there is an elevator door in front that needs to be opened
  154. if k == options.station_name then
  155. formspec = formspec ..
  156. ("button[%f,%f;1,0.5;open_door;<>]label[%f,%f;%s]")
  157. :format(x, y + 2.5, x + 0.9, y + 2.35, k)
  158. elseif options.is_elevator then
  159. local travelnets = travelnet.get_travelnets(options.owner_name)
  160. local network = travelnets[options.station_network]
  161. if not network then
  162. travelnet.log("action", "creating new elevator network for '" .. options.owner_name ..
  163. "' and station '" .. options.station_network .. "'")
  164. travelnets[options.station_network] = {}
  165. travelnet.set_travelnets(options.owner_name, travelnets)
  166. end
  167. formspec = formspec ..
  168. ("button[%f,%f;1,0.5;target;%s]label[%f,%f;%s]")
  169. :format(x, y + 2.5, minetest.formspec_escape(tostring(network[k].nr)), x + 0.9, y + 2.35, k)
  170. else
  171. local travelnets = travelnet.get_travelnets(options.owner_name)
  172. local network = travelnets[options.station_network]
  173. local target_station = network[k]
  174. local pos = player_formspec_data[player_name].pos
  175. local x1 = pos.x - target_station.pos.x
  176. local y1 = pos.y - target_station.pos.y
  177. local z1 = pos.z - target_station.pos.z
  178. local dist = math.sqrt(x1*x1 + y1*y1 + z1* z1)
  179. local cost = math.ceil(dist / 100)
  180. formspec = formspec ..
  181. ("button[%f,%f;4,0.5;target;%s]label[%f,%f;Cost: %s]")
  182. :format(x, y + 2.5, minetest.formspec_escape(k), x - 1, y + 2.5, cost)
  183. end
  184. y = y+1
  185. end
  186. if player_name == options.owner_name
  187. or minetest.get_player_privs(player_name)[travelnet.attach_priv]
  188. then
  189. formspec = formspec .. ([[
  190. label[8.0,1.6;%s]
  191. button[9.6,1.6;1.4,0.5;move_up;%s]
  192. button[10.9,1.6;1.4,0.5;move_down;%s]
  193. ]]):format(
  194. S("Position in list:"),
  195. S("move up"),
  196. S("move down")
  197. )
  198. end
  199. if player_name == options.owner_name
  200. or minetest.get_player_privs(player_name)[travelnet.remove_priv]
  201. or travelnet.allow_dig(player_name, options.owner_name, options.station_network, player_formspec_data[player_name].pos)
  202. then
  203. formspec = formspec .. ([[
  204. button[10.0,0.5;2.2,0.7;station_edit;%s]
  205. ]]):format(
  206. S("Edit station")
  207. )
  208. end
  209. if paging then
  210. if page_number > 2 then
  211. formspec = formspec .. ("button[0,9.2;2,1;first_page;%s]"):format(minetest.formspec_escape(S("<<")))
  212. end
  213. if page_number > 1 then
  214. formspec = formspec .. ("button[2,9.2;2,1;prev_page;%s]"):format(minetest.formspec_escape(S("<")))
  215. end
  216. formspec = formspec
  217. .. ("label[5,9.4;%s]"):format(minetest.formspec_escape(S("Page @1/@2", page_number, pages)))
  218. .. ("field[20,20;0.1,0.1;page_number;Page;%i]"):format(page_number)
  219. if page_number < pages then
  220. formspec = formspec .. ("button[8,9.2;2,1;next_page;%s]"):format(minetest.formspec_escape(S(">")))
  221. end
  222. if page_number < pages-1 then
  223. formspec = formspec .. ("button[10,9.2;2,1;last_page;%s]"):format(minetest.formspec_escape(S(">>")))
  224. end
  225. end
  226. return formspec
  227. end