init.lua 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. --[[
  2. Teleporter networks that allow players to choose a destination out of a list
  3. Copyright (C) 2013 Sokomine
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Version: 2.3 (click button to dig)
  15. Please configure this mod in config.lua
  16. Changelog:
  17. 10.03.19 - Added the extra config buttons for locked_travelnet mod.
  18. 09.03.19 - Several PRs merged (sound added, locale changed etc.)
  19. Version bumped to 2.3
  20. 26.02.19 - Removing a travelnet can now be done by clicking on a button (no need to
  21. wield a diamond pick anymore)
  22. 26.02.19 - Added compatibility with MineClone2
  23. 22.09.18 - Move up/move down no longer close the formspec.
  24. 22.09.18 - If in creative mode, wield a diamond pick to dig the station. This avoids
  25. conflicts with too fast punches.
  26. 24.12.17 - Added support for localization through intllib.
  27. Added localization for German (de).
  28. Door opening/closing can now handle more general doors.
  29. 17.07.17 - Added more detailled licence information.
  30. TNT and DungeonMasters ought to leave travelnets and elevators untouched now.
  31. Added function to register elevator doors.
  32. Added elevator doors made out of tin ingots.
  33. Provide information about the nearest elevator network when placing a new elevator. This
  34. ought to make it easier to find the right spot.
  35. Improved formspec.
  36. 16.07.17 - Merged several PR from others (Typo, screenshot, documentation, mesecon support, bugfix).
  37. Added buttons to move stations up or down in the list, independent on when they where added.
  38. Fixed undeclared globals.
  39. Changed deprecated functions set_look_yaw/pitch to current functions.
  40. 22.07.17 - Fixed bug with locked travelnets beeing removed from the network due to not beeing recognized.
  41. 30.08.16 - If the station the traveller just travelled to no longer exists, the player is sent back to the
  42. station where he/she came from.
  43. 30.08.16 - Attaching a travelnet box to a non-existant network of another player is possible (requested by OldCoder).
  44. Still requires the travelnet_attach-priv.
  45. 05.10.14 - Added an optional abm so that the travelnet network can heal itshelf in case of loss of the savefile.
  46. If you want to use this, set
  47. travelnet.enable_abm = true
  48. in config.lua and edit the interval in the abm to suit your needs.
  49. 19.11.13 - moved doors and travelnet definition into an extra file
  50. - moved configuration to config.lua
  51. 05.08.13 - fixed possible crash when the node in front of the travelnet is unknown
  52. 26.06.13 - added inventory image for elevator (created by VanessaE)
  53. 21.06.13 - bugfix: wielding an elevator while digging a door caused the elevator_top to be placed
  54. - leftover floating elevator_top nodes can be removed by placing a new travelnet:elevator underneath them and removing that afterwards
  55. - homedecor-doors are now opened and closed correctly as well
  56. - removed nodes that are not intended for manual use from creative inventory
  57. - improved naming of station levels for the elevator
  58. 21.06.13 - elevator stations are sorted by height instead of date of creation as is the case with travelnet boxes
  59. - elevator stations are named automaticly
  60. 20.06.13 - doors can be opened and closed from inside the travelnet box/elevator
  61. - the elevator can only move vertically; the network name is defined by its x and z coordinate
  62. 13.06.13 - bugfix
  63. - elevator added (written by kpoppel) and placed into extra file
  64. - elevator doors added
  65. - groups changed to avoid accidental dig/drop on dig of node beneath
  66. - added new priv travelnet_remove for digging of boxes owned by other players
  67. - only the owner of a box or players with the travelnet_remove priv can now dig it
  68. - entering your own name as owner_name does no longer abort setup
  69. 22.03.13 - added automatic detection if yaw can be set
  70. - beam effect is disabled by default
  71. 20.03.13 - added inventory image provided by VanessaE
  72. - fixed bug that made it impossible to remove stations from the net
  73. - if the station a player beamed to no longer exists, the station will be removed automaticly
  74. - with the travelnet_attach priv, you can now attach your box to the nets of other players
  75. - in newer versions of Minetest, the players yaw is set so that he/she looks out of the receiving box
  76. - target list is now centered if there are less than 9 targets
  77. --]]
  78. -- Required to save the travelnet data properly in all cases
  79. if not minetest.safe_file_write then
  80. error("[Mod travelnet] Your Minetest version is no longer supported. (version < 0.4.17)")
  81. end
  82. travelnet = {};
  83. travelnet.targets = {};
  84. travelnet.path = minetest.get_modpath(minetest.get_current_modname())
  85. -- Intllib
  86. local S = dofile(travelnet.path .. "/intllib.lua")
  87. travelnet.S = S
  88. minetest.register_privilege("travelnet_attach", { description = S("allows to attach travelnet boxes to travelnets of other players"), give_to_singleplayer = false});
  89. minetest.register_privilege("travelnet_remove", { description = S("allows to dig travelnet boxes which belog to nets of other players"), give_to_singleplayer = false});
  90. -- read the configuration
  91. dofile(travelnet.path.."/config.lua"); -- the normal, default travelnet
  92. travelnet.mod_data_path = minetest.get_worldpath().."/mod_travelnet.data"
  93. -- TODO: save and restore ought to be library functions and not implemented in each individual mod!
  94. -- called whenever a station is added or removed
  95. travelnet.save_data = function()
  96. local data = minetest.serialize( travelnet.targets );
  97. local success = minetest.safe_file_write( travelnet.mod_data_path, data );
  98. if( not success ) then
  99. print(S("[Mod travelnet] Error: Savefile '%s' could not be written.")
  100. :format(travelnet.mod_data_path));
  101. end
  102. end
  103. travelnet.restore_data = function()
  104. local file = io.open( travelnet.mod_data_path, "r" );
  105. if( not file ) then
  106. print(S("[Mod travelnet] Error: Savefile '%s' not found.")
  107. :format(travelnet.mod_data_path));
  108. return;
  109. end
  110. local data = file:read("*all");
  111. travelnet.targets = minetest.deserialize( data );
  112. if( not travelnet.targets ) then
  113. local backup_file = travelnet.mod_data_path..".bak"
  114. print(S("[Mod travelnet] Error: Savefile '%s' is damaged. Saved the backup as '%s'.")
  115. :format(travelnet.mod_data_path, backup_file));
  116. minetest.safe_file_write( backup_file, data );
  117. travelnet.targets = {};
  118. end
  119. file:close();
  120. end
  121. -- punching the travelnet updates its formspec and shows it to the player;
  122. -- however, that would be very annoying when actually trying to dig the thing.
  123. -- Thus, check if the player is wielding a tool that can dig nodes of the
  124. -- group cracky
  125. travelnet.check_if_trying_to_dig = function( puncher, node )
  126. -- if in doubt: show formspec
  127. if( not( puncher) or not( puncher:get_wielded_item())) then
  128. return false;
  129. end
  130. -- show menu when in creative mode
  131. if( creative
  132. and creative.is_enabled_for(puncher:get_player_name())
  133. -- and (not(puncher:get_wielded_item())
  134. -- or puncher:get_wielded_item():get_name()~="default:pick_diamond")) then
  135. ) then
  136. return false;
  137. end
  138. local tool_capabilities = puncher:get_wielded_item():get_tool_capabilities();
  139. if( not( tool_capabilities )
  140. or not( tool_capabilities["groupcaps"])
  141. or not( tool_capabilities["groupcaps"]["cracky"])) then
  142. return false;
  143. end
  144. -- tools which can dig cracky items can start digging immediately
  145. return true;
  146. end
  147. -- minetest.chat_send_player is sometimes not so well visible
  148. travelnet.show_message = function( pos, player_name, title, message )
  149. if( not( pos ) or not( player_name ) or not( message )) then
  150. return;
  151. end
  152. local formspec = "size[8,3]"..
  153. "label[3,0;"..minetest.formspec_escape( title or "Error").."]"..
  154. "textlist[0,0.5;8,1.5;;"..minetest.formspec_escape( message or "- nothing -")..";]"..
  155. "button_exit[3.5,2.5;1.0,0.5;back;"..S("Back").."]"..
  156. "button_exit[6.8,2.5;1.0,0.5;station_exit;"..S("Exit").."]"..
  157. "field[20,20;0.1,0.1;pos2str;Pos;".. minetest.pos_to_string( pos ).."]";
  158. minetest.show_formspec(player_name, "travelnet:show", formspec);
  159. end
  160. -- show the player the formspec he would see when right-clicking the node;
  161. -- needs to be simulated this way as calling on_rightclick would not do
  162. travelnet.show_current_formspec = function( pos, meta, player_name )
  163. if( not( pos ) or not( meta ) or not( player_name )) then
  164. return;
  165. end
  166. -- we need to supply the position of the travelnet box
  167. formspec = meta:get_string("formspec")..
  168. "field[20,20;0.1,0.1;pos2str;Pos;".. minetest.pos_to_string( pos ).."]";
  169. -- show the formspec manually
  170. minetest.show_formspec(player_name, "travelnet:show", formspec);
  171. end
  172. -- a player clicked on something in the formspec he was manually shown
  173. -- (back from help page, moved travelnet up or down etc.)
  174. travelnet.form_input_handler = function( player, formname, fields)
  175. if(formname == "travelnet:show" and fields and fields.pos2str) then
  176. local pos = minetest.string_to_pos( fields.pos2str );
  177. if( locks and (fields.locks_config or fields.locks_authorize)) then
  178. return locks:lock_handle_input( pos, formname, fields, player )
  179. end
  180. -- back button leads back to the main menu
  181. if( fields.back and fields.back ~= "" ) then
  182. return travelnet.show_current_formspec( pos,
  183. minetest.get_meta( pos ), player:get_player_name());
  184. end
  185. return travelnet.on_receive_fields(pos, formname, fields, player);
  186. end
  187. end
  188. -- most formspecs the travelnet uses are stored in the travelnet node itself,
  189. -- but some may require some "back"-button functionality (i.e. help page,
  190. -- move up/down etc.)
  191. minetest.register_on_player_receive_fields( travelnet.form_input_handler );
  192. travelnet.reset_formspec = function( meta )
  193. if( not( meta )) then
  194. return;
  195. end
  196. meta:set_string("infotext", S("Travelnet-box (unconfigured)"));
  197. meta:set_string("station_name", "");
  198. meta:set_string("station_network","");
  199. meta:set_string("owner", "");
  200. -- some players seem to be confused with entering network names at first; provide them
  201. -- with a default name
  202. if( not( station_network ) or station_network == "" ) then
  203. station_network = "net1";
  204. end
  205. -- request initinal data
  206. meta:set_string("formspec",
  207. "size[10,6.0]"..
  208. "label[2.0,0.0;--> "..S("Configure this travelnet station").." <--]"..
  209. "button_exit[8.0,0.0;2.2,0.7;station_dig;"..S("Remove station").."]"..
  210. "field[0.3,1.2;9,0.9;station_name;"..S("Name of this station")..":;"..
  211. minetest.formspec_escape(station_name or "").."]"..
  212. "label[0.3,1.5;"..S("How do you call this place here? Example: \"my first house\", \"mine\", \"shop\"...").."]"..
  213. "field[0.3,2.8;9,0.9;station_network;"..S("Assign to Network:")..";"..
  214. minetest.formspec_escape(station_network or "").."]"..
  215. "label[0.3,3.1;"..S("You can have more than one network. If unsure, use \"%s\""):format(tostring(station_network))..".]"..
  216. "field[0.3,4.4;9,0.9;owner;"..S("Owned by:")..";]"..
  217. "label[0.3,4.7;"..S("Unless you know what you are doing, leave this empty.").."]"..
  218. "button_exit[1.3,5.3;1.7,0.7;station_help_setup;"..S("Help").."]"..
  219. "button_exit[3.8,5.3;1.7,0.7;station_set;"..S("Save").."]"..
  220. "button_exit[6.3,5.3;1.7,0.7;station_exit;"..S("Exit").."]");
  221. end
  222. travelnet.update_formspec = function( pos, puncher_name, fields )
  223. local meta = minetest.get_meta(pos);
  224. local this_node = minetest.get_node( pos );
  225. local is_elevator = false;
  226. if( this_node ~= nil and this_node.name == 'travelnet:elevator' ) then
  227. is_elevator = true;
  228. end
  229. if( not( meta )) then
  230. return;
  231. end
  232. local owner_name = meta:get_string( "owner" );
  233. local station_name = meta:get_string( "station_name" );
  234. local station_network = meta:get_string( "station_network" );
  235. if( not( owner_name )
  236. or not( station_name ) or station_network == ''
  237. or not( station_network )) then
  238. if( is_elevator == true ) then
  239. travelnet.add_target( nil, nil, pos, puncher_name, meta, owner_name );
  240. return;
  241. end
  242. -- minetest.chat_send_player(puncher_name, "DEBUG DATA: owner: "..(owner_name or "?")..
  243. -- " station_name: "..(station_name or "?")..
  244. -- " station_network: "..(station_network or "?")..".");
  245. -- minetest.chat_send_player(puncher_name, "data: "..minetest.serialize( travelnet.targets ));
  246. travelnet.reset_formspec( meta );
  247. travelnet.show_message( pos, puncher_name, "Error", S("Update failed! Resetting this box on the travelnet."));
  248. return;
  249. end
  250. -- if the station got lost from the network for some reason (savefile corrupted?) then add it again
  251. if( not( travelnet.targets[ owner_name ] )
  252. or not( travelnet.targets[ owner_name ][ station_network ] )
  253. or not( travelnet.targets[ owner_name ][ station_network ][ station_name ] )) then
  254. -- first one by this player?
  255. if( not( travelnet.targets[ owner_name ] )) then
  256. travelnet.targets[ owner_name ] = {};
  257. end
  258. -- first station on this network?
  259. if( not( travelnet.targets[ owner_name ][ station_network ] )) then
  260. travelnet.targets[ owner_name ][ station_network ] = {};
  261. end
  262. local zeit = meta:get_int("timestamp");
  263. if( not( zeit) or type(zeit)~="number" or zeit<100000 ) then
  264. zeit = os.time();
  265. end
  266. -- add this station
  267. travelnet.targets[ owner_name ][ station_network ][ station_name ] = {pos=pos, timestamp=zeit };
  268. minetest.chat_send_player(owner_name, S("Station '%s'"):format(station_name).." "..
  269. S(" has been reattached to the network '%s'."):format(station_network));
  270. travelnet.save_data();
  271. end
  272. -- add name of station + network + owner + update-button
  273. local zusatzstr = "";
  274. local trheight = "10";
  275. if( this_node and this_node.name=="locked_travelnet:travelnet" and locks) then
  276. zusatzstr = "field[0.3,11;6,0.7;locks_sent_lock_command;"..S("Locked travelnet. Type /help for help:")..";]"..
  277. locks.get_authorize_button(10,"10.5")..
  278. locks.get_config_button(11,"10.5")
  279. trheight = "11.5";
  280. end
  281. local formspec = "size[12,"..trheight.."]"..
  282. "label[3.3,0.0;"..S("Travelnet-Box")..":]".."label[6.3,0.0;"..S("Punch box to update target list.").."]"..
  283. "label[0.3,0.4;"..S("Name of this station:").."]".."label[6.3,0.4;"..minetest.formspec_escape(station_name or "?").."]"..
  284. "label[0.3,0.8;"..S("Assigned to Network:").."]" .."label[6.3,0.8;"..minetest.formspec_escape(station_network or "?").."]"..
  285. "label[0.3,1.2;"..S("Owned by:").."]" .."label[6.3,1.2;"..minetest.formspec_escape(owner_name or "?").."]"..
  286. "label[3.3,1.6;"..S("Click on target to travel there:").."]"..
  287. zusatzstr;
  288. -- "button_exit[5.3,0.3;8,0.8;do_update;Punch box to update destination list. Click on target to travel there.]"..
  289. local x = 0;
  290. local y = 0;
  291. local i = 0;
  292. -- collect all station names in a table
  293. local stations = {};
  294. for k,v in pairs( travelnet.targets[ owner_name ][ station_network ] ) do
  295. table.insert( stations, k );
  296. end
  297. -- minetest.chat_send_player(puncher_name, "stations: "..minetest.serialize( stations ));
  298. local ground_level = 1;
  299. if( is_elevator ) then
  300. table.sort( stations, function(a,b) return travelnet.targets[ owner_name ][ station_network ][ a ].pos.y >
  301. travelnet.targets[ owner_name ][ station_network ][ b ].pos.y end);
  302. -- find ground level
  303. local vgl_timestamp = 999999999999;
  304. for index,k in ipairs( stations ) do
  305. if( not( travelnet.targets[ owner_name ][ station_network ][ k ].timestamp )) then
  306. travelnet.targets[ owner_name ][ station_network ][ k ].timestamp = os.time();
  307. end
  308. if( travelnet.targets[ owner_name ][ station_network ][ k ].timestamp < vgl_timestamp ) then
  309. vgl_timestamp = travelnet.targets[ owner_name ][ station_network ][ k ].timestamp;
  310. ground_level = index;
  311. end
  312. end
  313. for index,k in ipairs( stations ) do
  314. if( index == ground_level ) then
  315. travelnet.targets[ owner_name ][ station_network ][ k ].nr = S('G');
  316. else
  317. travelnet.targets[ owner_name ][ station_network ][ k ].nr = tostring( ground_level - index );
  318. end
  319. end
  320. else
  321. -- sort the table according to the timestamp (=time the station was configured)
  322. table.sort( stations, function(a,b) return travelnet.targets[ owner_name ][ station_network ][ a ].timestamp <
  323. travelnet.targets[ owner_name ][ station_network ][ b ].timestamp end);
  324. end
  325. -- does the player want to move this station one position up in the list?
  326. -- only the owner and players with the travelnet_attach priv can change the order of the list
  327. -- Note: With elevators, only the "G"(round) marking is actually moved
  328. if( fields
  329. and (fields.move_up or fields.move_down)
  330. and owner_name
  331. and owner_name ~= ""
  332. and ((owner_name == puncher_name)
  333. or (minetest.check_player_privs(puncher_name, {travelnet_attach=true})))
  334. ) then
  335. local current_pos = -1;
  336. for index,k in ipairs( stations ) do
  337. if( k==station_name ) then
  338. current_pos = index;
  339. end
  340. end
  341. local swap_with_pos = -1;
  342. if( fields.move_up ) then
  343. swap_with_pos = current_pos - 1;
  344. else
  345. swap_with_pos = current_pos + 1;
  346. end
  347. -- handle errors
  348. if( swap_with_pos < 1) then
  349. travelnet.show_message( pos, puncher_name, "Info", S("This station is already the first one on the list."));
  350. return;
  351. elseif( swap_with_pos > #stations ) then
  352. travelnet.show_message( pos, puncher_name, "Info", S("This station is already the last one on the list."));
  353. return;
  354. else
  355. -- swap the actual data by which the stations are sorted
  356. local old_timestamp = travelnet.targets[ owner_name ][ station_network ][ stations[swap_with_pos]].timestamp;
  357. travelnet.targets[ owner_name ][ station_network ][ stations[swap_with_pos]].timestamp =
  358. travelnet.targets[ owner_name ][ station_network ][ stations[current_pos ]].timestamp;
  359. travelnet.targets[ owner_name ][ station_network ][ stations[current_pos ]].timestamp =
  360. old_timestamp;
  361. -- for elevators, only the "G"(round) marking is moved; no point in swapping stations
  362. if( not( is_elevator )) then
  363. -- actually swap the stations
  364. local old_val = stations[ swap_with_pos ];
  365. stations[ swap_with_pos ] = stations[ current_pos ];
  366. stations[ current_pos ] = old_val;
  367. end
  368. -- store the changed order
  369. travelnet.save_data();
  370. end
  371. end
  372. -- if there are only 8 stations (plus this one), center them in the formspec
  373. if( #stations < 10 ) then
  374. x = 4;
  375. end
  376. for index,k in ipairs( stations ) do
  377. -- check if there is an elevator door in front that needs to be opened
  378. local open_door_cmd = false;
  379. if( k==station_name ) then
  380. open_door_cmd = true;
  381. end
  382. if( k ~= station_name or open_door_cmd) then
  383. i = i+1;
  384. -- new column
  385. if( y==8 ) then
  386. x = x+4;
  387. y = 0;
  388. end
  389. if( open_door_cmd ) then
  390. formspec = formspec .."button_exit["..(x)..","..(y+2.5)..";1,0.5;open_door;<>]"..
  391. "label["..(x+0.9)..","..(y+2.35)..";"..tostring( k ).."]";
  392. elseif( is_elevator ) then
  393. formspec = formspec .."button_exit["..(x)..","..(y+2.5)..";1,0.5;target;"..tostring( travelnet.targets[ owner_name ][ station_network ][ k ].nr ).."]"..
  394. "label["..(x+0.9)..","..(y+2.35)..";"..tostring( k ).."]";
  395. else
  396. formspec = formspec .."button_exit["..(x)..","..(y+2.5)..";4,0.5;target;"..k.."]";
  397. end
  398. -- if( is_elevator ) then
  399. -- formspec = formspec ..' ('..tostring( travelnet.targets[ owner_name ][ station_network ][ k ].pos.y )..'m)';
  400. -- end
  401. -- formspec = formspec .. ']';
  402. y = y+1;
  403. --x = x+4;
  404. end
  405. end
  406. formspec = formspec..
  407. "label[8.0,1.6;"..S("Position in list:").."]"..
  408. "button_exit[11.3,0.0;1.0,0.5;station_exit;"..S("Exit").."]"..
  409. "button_exit[10.0,0.5;2.2,0.7;station_dig;"..S("Remove station").."]"..
  410. "button[9.6,1.6;1.4,0.5;move_up;"..S("move up").."]"..
  411. "button[10.9,1.6;1.4,0.5;move_down;"..S("move down").."]";
  412. meta:set_string( "formspec", formspec );
  413. meta:set_string( "infotext", S("Station '%s'"):format(tostring( station_name )).." "..
  414. S("on travelnet '%s'"):format(tostring( station_network )).." "..
  415. S("(owned by %s)"):format(tostring( owner_name )).." "..
  416. S("ready for usage. Right-click to travel, punch to update."));
  417. -- show the player the updated formspec
  418. travelnet.show_current_formspec( pos, meta, puncher_name );
  419. end
  420. -- add a new target; meta is optional
  421. travelnet.add_target = function( station_name, network_name, pos, player_name, meta, owner_name )
  422. -- if it is an elevator, determine the network name through x and z coordinates
  423. local this_node = minetest.get_node( pos );
  424. local is_elevator = false;
  425. if( this_node.name == 'travelnet:elevator' ) then
  426. -- owner_name = '*'; -- the owner name is not relevant here
  427. is_elevator = true;
  428. network_name = tostring( pos.x )..','..tostring( pos.z );
  429. if( not( station_name ) or station_name == '' ) then
  430. station_name = S('at %s m'):format(tostring( pos.y ));
  431. end
  432. end
  433. if( station_name == "" or not(station_name )) then
  434. travelnet.show_message( pos, player_name, S("Error"), S("Please provide a name for this station." ));
  435. return;
  436. end
  437. if( network_name == "" or not( network_name )) then
  438. travelnet.show_message( pos, player_name, S("Error"),
  439. S("Please provide the name of the network this station ought to be connected to."));
  440. return;
  441. end
  442. if( owner_name == nil or owner_name == '' or owner_name == player_name) then
  443. owner_name = player_name;
  444. elseif( is_elevator ) then -- elevator networks
  445. owner_name = player_name;
  446. elseif( not( minetest.check_player_privs(player_name, {interact=true}))) then
  447. travelnet.show_message( pos, player_name, S("Error"),
  448. S("There is no player with interact privilege named '%s'. Aborting."):format(tostring( player_name )));
  449. return;
  450. elseif( not( minetest.check_player_privs(player_name, {travelnet_attach=true}))
  451. and not( travelnet.allow_attach( player_name, owner_name, network_name ))) then
  452. travelnet.show_message( pos, player_name, S("Error"),
  453. S("You do not have the travelnet_attach priv which is required to attach your box to "..
  454. "the network of someone else. Aborting."));
  455. return;
  456. end
  457. -- first one by this player?
  458. if( not( travelnet.targets[ owner_name ] )) then
  459. travelnet.targets[ owner_name ] = {};
  460. end
  461. -- first station on this network?
  462. if( not( travelnet.targets[ owner_name ][ network_name ] )) then
  463. travelnet.targets[ owner_name ][ network_name ] = {};
  464. end
  465. -- lua doesn't allow efficient counting here
  466. local anz = 0;
  467. for k,v in pairs( travelnet.targets[ owner_name ][ network_name ] ) do
  468. if( k == station_name ) then
  469. travelnet.show_message( pos, player_name, S("Error"),
  470. S("A station named '%s' already exists on this network. Please choose a diffrent name!"):format(station_name));
  471. return;
  472. end
  473. anz = anz + 1;
  474. end
  475. -- we don't want too many stations in the same network because that would get confusing when displaying the targets
  476. if( anz+1 > travelnet.MAX_STATIONS_PER_NETWORK ) then
  477. travelnet.show_message( pos, player_name, S("Error"),
  478. S("Network '%s',"):format(network_name).." "..
  479. S("already contains the maximum number (=%s) of allowed stations per network. "..
  480. "Please choose a diffrent/new network name."):format(travelnet.MAX_STATIONS_PER_NETWORK));
  481. return;
  482. end
  483. -- add this station
  484. travelnet.targets[ owner_name ][ network_name ][ station_name ] = {pos=pos, timestamp=os.time() };
  485. -- do we have a new node to set up? (and are not just reading from a safefile?)
  486. if( meta ) then
  487. minetest.chat_send_player(player_name, S("Station '%s'"):format(station_name).." "..
  488. S("has been added to the network '%s'"):format(network_name)..
  489. S(", which now consists of %s station(s)."):format(anz+1));
  490. meta:set_string( "station_name", station_name );
  491. meta:set_string( "station_network", network_name );
  492. meta:set_string( "owner", owner_name );
  493. meta:set_int( "timestamp", travelnet.targets[ owner_name ][ network_name ][ station_name ].timestamp);
  494. meta:set_string("formspec",
  495. "size[12,10]"..
  496. "field[0.3,0.6;6,0.7;station_name;"..S("Station:")..";".. minetest.formspec_escape(meta:get_string("station_name")).."]"..
  497. "field[0.3,3.6;6,0.7;station_network;"..S("Network:")..";"..minetest.formspec_escape(meta:get_string("station_network")).."]" );
  498. -- display a list of all stations that can be reached from here
  499. travelnet.update_formspec( pos, player_name, nil );
  500. -- save the updated network data in a savefile over server restart
  501. travelnet.save_data();
  502. end
  503. end
  504. -- allow doors to open
  505. travelnet.open_close_door = function( pos, player, mode )
  506. local this_node = minetest.get_node_or_nil( pos );
  507. -- give up if the area is *still* not loaded
  508. if( this_node == nil ) then
  509. return
  510. end
  511. local pos2 = {x=pos.x,y=pos.y,z=pos.z};
  512. if( this_node.param2 == 0 ) then pos2 = {x=pos.x,y=pos.y,z=(pos.z-1)};
  513. elseif( this_node.param2 == 1 ) then pos2 = {x=(pos.x-1),y=pos.y,z=pos.z};
  514. elseif( this_node.param2 == 2 ) then pos2 = {x=pos.x,y=pos.y,z=(pos.z+1)};
  515. elseif( this_node.param2 == 3 ) then pos2 = {x=(pos.x+1),y=pos.y,z=pos.z};
  516. end
  517. local door_node = minetest.get_node( pos2 );
  518. if( door_node ~= nil and door_node.name ~= 'ignore' and door_node.name ~= 'air' and minetest.registered_nodes[ door_node.name ] ~= nil and minetest.registered_nodes[ door_node.name ].on_rightclick ~= nil) then
  519. -- at least for homedecor, same facedir would mean "door closed"
  520. -- do not close the elevator door if it is already closed
  521. if( mode==1 and ( string.sub( door_node.name, -7 ) == '_closed'
  522. -- handle doors that change their facedir
  523. or ( door_node.param2 == ((this_node.param2 + 2)%4)
  524. and door_node.name ~= 'travelnet:elevator_door_glass_open'
  525. and door_node.name ~= 'travelnet:elevator_door_tin_open'
  526. and door_node.name ~= 'travelnet:elevator_door_steel_open'))) then
  527. return;
  528. end
  529. -- do not open the doors if they are already open (works only on elevator-doors; not on doors in general)
  530. if( mode==2 and ( string.sub( door_node.name, -5 ) == '_open'
  531. -- handle doors that change their facedir
  532. or ( door_node.param2 ~= ((this_node.param2 + 2)%4)
  533. and door_node.name ~= 'travelnet:elevator_door_glass_closed'
  534. and door_node.name ~= 'travelnet:elevator_door_tin_closed'
  535. and door_node.name ~= 'travelnet:elevator_door_steel_closed'))) then
  536. return;
  537. end
  538. if( mode==2 ) then
  539. minetest.after( 1, minetest.registered_nodes[ door_node.name ].on_rightclick, pos2, door_node, player );
  540. else
  541. minetest.registered_nodes[ door_node.name ].on_rightclick(pos2, door_node, player);
  542. end
  543. end
  544. end
  545. travelnet.on_receive_fields = function(pos, formname, fields, player)
  546. if( not( pos )) then
  547. return;
  548. end
  549. local meta = minetest.get_meta(pos);
  550. local name = player:get_player_name();
  551. -- the player wants to quit/exit the formspec; do not save/update anything
  552. if( fields and fields.station_exit and fields.station_exit ~= "" ) then
  553. return;
  554. end
  555. -- show special locks buttons if needed
  556. if( locks and (fields.locks_config or fields.locks_authorize)) then
  557. return locks:lock_handle_input( pos, formname, fields, player )
  558. end
  559. -- show help text
  560. if( fields and fields.station_help_setup and fields.station_help_setup ~= "") then
  561. -- simulate right-click
  562. local node = minetest.get_node( pos );
  563. if( node and node.name and minetest.registered_nodes[ node.name ] ) then
  564. travelnet.show_message( pos, name, "--> Help <--",
  565. -- TODO: actually add help page
  566. S("No help available yet."));
  567. end
  568. return;
  569. end
  570. -- the player wants to remove the station
  571. if( fields.station_dig ) then
  572. local owner = meta:get_string( "owner" );
  573. local node = minetest.get_node(pos)
  574. local description = "station"
  575. if( node and node.name and node.name == "travelnet:travelnet") then
  576. description = "travelnet box"
  577. elseif( node and node.name and node.name == "travelnet:elevator") then
  578. description = "elevator"
  579. elseif( node and node.name and node.name == "locked_travelnet:travelnet") then
  580. description = "locked travelnet"
  581. else
  582. minetest.chat_send_player(name, "Error: Unkown node.");
  583. return
  584. end
  585. -- players with travelnet_remove priv can dig the station
  586. if( not(minetest.check_player_privs(name, {travelnet_remove=true}))
  587. -- the function travelnet.allow_dig(..) may allow additional digging
  588. and not(travelnet.allow_dig( name, owner, network_name ))
  589. -- the owner can remove the station
  590. and owner ~= name
  591. -- stations without owner can be removed by anybody
  592. and owner ~= "") then
  593. minetest.chat_send_player(name, S("This %s belongs to %s. You can't remove it."):format(description, tostring( meta:get_string('owner'))));
  594. return
  595. end
  596. local pinv = player:get_inventory()
  597. if(not(pinv:room_for_item("main", node.name))) then
  598. minetest.chat_send_player(name, S("You do not have enough room in your inventory."));
  599. return
  600. end
  601. -- give the player the box
  602. pinv:add_item("main", node.name)
  603. -- remove the box from the data structure
  604. travelnet.remove_box( pos, nil, meta:to_table(), player );
  605. -- remove the node as such
  606. minetest.remove_node(pos)
  607. return;
  608. end
  609. -- if the box has not been configured yet
  610. if( meta:get_string("station_network")=="" ) then
  611. travelnet.add_target( fields.station_name, fields.station_network, pos, name, meta, fields.owner );
  612. return;
  613. end
  614. if( fields.open_door ) then
  615. travelnet.open_close_door( pos, player, 0 );
  616. return;
  617. end
  618. -- the owner or players with the travelnet_attach priv can move stations up or down in the list
  619. if( fields.move_up or fields.move_down) then
  620. travelnet.update_formspec( pos, name, fields );
  621. return;
  622. end
  623. if( not( fields.target )) then
  624. minetest.chat_send_player(name, S("Please click on the target you want to travel to."));
  625. return;
  626. end
  627. -- if there is something wrong with the data
  628. local owner_name = meta:get_string( "owner" );
  629. local station_name = meta:get_string( "station_name" );
  630. local station_network = meta:get_string( "station_network" );
  631. if( not( owner_name )
  632. or not( station_name )
  633. or not( station_network )
  634. or not( travelnet.targets[ owner_name ] )
  635. or not( travelnet.targets[ owner_name ][ station_network ] )) then
  636. if( owner_name
  637. and station_name
  638. and station_network ) then
  639. travelnet.add_target( station_name, station_network, pos, owner_name, meta, owner_name );
  640. else
  641. minetest.chat_send_player(name, S("Error")..": "..
  642. S("There is something wrong with the configuration of this station.")..
  643. " DEBUG DATA: owner: "..( owner_name or "?")..
  644. " station_name: "..(station_name or "?")..
  645. " station_network: "..(station_network or "?")..".");
  646. return
  647. end
  648. end
  649. if( not( owner_name )
  650. or not( station_network )
  651. or not( travelnet.targets )
  652. or not( travelnet.targets[ owner_name ] )
  653. or not( travelnet.targets[ owner_name ][ station_network ] )) then
  654. minetest.chat_send_player(name, S("Error")..": "..
  655. S("This travelnet is lacking data and/or improperly configured."));
  656. print( "ERROR: The travelnet at "..minetest.pos_to_string( pos ).." has a problem: "..
  657. " DATA: owner: "..( owner_name or "?")..
  658. " station_name: "..(station_name or "?")..
  659. " station_network: "..(station_network or "?")..".");
  660. return;
  661. end
  662. local this_node = minetest.get_node( pos );
  663. if( this_node ~= nil and this_node.name == 'travelnet:elevator' ) then
  664. for k,v in pairs( travelnet.targets[ owner_name ][ station_network ] ) do
  665. if( travelnet.targets[ owner_name ][ station_network ][ k ].nr --..' ('..tostring( travelnet.targets[ owner_name ][ station_network ][ k ].pos.y )..'m)'
  666. == fields.target) then
  667. fields.target = k;
  668. end
  669. end
  670. end
  671. -- if the target station is gone
  672. if( not( travelnet.targets[ owner_name ][ station_network ][ fields.target ] )) then
  673. minetest.chat_send_player(name, S("Station '%s'"):format( fields.target or "?").." "..
  674. S("does not exist (anymore?) on this network."));
  675. travelnet.update_formspec( pos, name, nil );
  676. return;
  677. end
  678. if( not( travelnet.allow_travel( name, owner_name, station_network, station_name, fields.target ))) then
  679. return;
  680. end
  681. minetest.chat_send_player(name, S("Initiating transfer to station '%s'."):format( fields.target or "?"));
  682. if( travelnet.travelnet_sound_enabled ) then
  683. if ( this_node.name == 'travelnet:elevator' ) then
  684. minetest.sound_play("travelnet_bell", {pos = pos, gain = 0.75, max_hear_distance = 10,});
  685. else
  686. minetest.sound_play("travelnet_travel", {pos = pos, gain = 0.75, max_hear_distance = 10,});
  687. end
  688. end
  689. if( travelnet.travelnet_effect_enabled ) then
  690. minetest.add_entity( {x=pos.x,y=pos.y+0.5,z=pos.z}, "travelnet:effect"); -- it self-destructs after 20 turns
  691. end
  692. -- close the doors at the sending station
  693. travelnet.open_close_door( pos, player, 1 );
  694. -- transport the player to the target location
  695. local target_pos = travelnet.targets[ owner_name ][ station_network ][ fields.target ].pos;
  696. player:moveto( target_pos, false);
  697. if( travelnet.travelnet_effect_enabled ) then
  698. minetest.add_entity( {x=target_pos.x,y=target_pos.y+0.5,z=target_pos.z}, "travelnet:effect"); -- it self-destructs after 20 turns
  699. end
  700. -- check if the box has at the other end has been removed.
  701. local node2 = minetest.get_node_or_nil( target_pos );
  702. if( node2 ~= nil and node2.name ~= 'ignore' and node2.name ~= 'travelnet:travelnet' and node2.name ~= 'travelnet:elevator' and node2.name ~= "locked_travelnet:travelnet" and node2.name ~= "travelnet:travelnet_private") then
  703. -- provide information necessary to identify the removed box
  704. local oldmetadata = { fields = { owner = owner_name,
  705. station_name = fields.target,
  706. station_network = station_network }};
  707. travelnet.remove_box( target_pos, nil, oldmetadata, player );
  708. -- send the player back as there's no receiving travelnet
  709. player:moveto( pos, false );
  710. else
  711. travelnet.rotate_player( target_pos, player, 0 )
  712. end
  713. end
  714. travelnet.rotate_player = function( target_pos, player, tries )
  715. -- try later when the box is loaded
  716. local node2 = minetest.get_node_or_nil( target_pos );
  717. if( node2 == nil ) then
  718. if( tries < 30 ) then
  719. minetest.after( 0, travelnet.rotate_player, target_pos, player, tries+1 )
  720. end
  721. return
  722. end
  723. -- play sound at the target position as well
  724. if( travelnet.travelnet_sound_enabled ) then
  725. if ( node2.name == 'travelnet:elevator' ) then
  726. minetest.sound_play("travelnet_bell", {pos = target_pos, gain = 0.75, max_hear_distance = 10,});
  727. else
  728. minetest.sound_play("travelnet_travel", {pos = target_pos, gain = 0.75, max_hear_distance = 10,});
  729. end
  730. end
  731. -- do this only on servers where the function exists
  732. if( player.set_look_horizontal ) then
  733. -- rotate the player so that he/she can walk straight out of the box
  734. local yaw = 0;
  735. local param2 = node2.param2;
  736. if( param2==0 ) then
  737. yaw = 180;
  738. elseif( param2==1 ) then
  739. yaw = 90;
  740. elseif( param2==2 ) then
  741. yaw = 0;
  742. elseif( param2==3 ) then
  743. yaw = 270;
  744. end
  745. player:set_look_horizontal( math.rad( yaw ));
  746. player:set_look_vertical( math.rad( 0 ));
  747. end
  748. travelnet.open_close_door( target_pos, player, 2 );
  749. end
  750. travelnet.remove_box = function( pos, oldnode, oldmetadata, digger )
  751. if( not( oldmetadata ) or oldmetadata=="nil" or not(oldmetadata.fields)) then
  752. minetest.chat_send_player( digger:get_player_name(), S("Error")..": "..
  753. S("Could not find information about the station that is to be removed."));
  754. return;
  755. end
  756. local owner_name = oldmetadata.fields[ "owner" ];
  757. local station_name = oldmetadata.fields[ "station_name" ];
  758. local station_network = oldmetadata.fields[ "station_network" ];
  759. -- station is not known? then just remove it
  760. if( not( owner_name )
  761. or not( station_name )
  762. or not( station_network )
  763. or not( travelnet.targets[ owner_name ] )
  764. or not( travelnet.targets[ owner_name ][ station_network ] )) then
  765. minetest.chat_send_player( digger:get_player_name(), S("Error")..": "..
  766. S("Could not find the station that is to be removed."));
  767. return;
  768. end
  769. travelnet.targets[ owner_name ][ station_network ][ station_name ] = nil;
  770. -- inform the owner
  771. minetest.chat_send_player( owner_name, S("Station '%s'"):format(station_name ).." "..
  772. S("has been REMOVED from the network '%s'."):format(station_network));
  773. if( digger ~= nil and owner_name ~= digger:get_player_name() ) then
  774. minetest.chat_send_player( digger:get_player_name(), S("Station '%s'"):format(station_name)..
  775. S("has been REMOVED from the network '%s'."):format(station_network));
  776. end
  777. -- save the updated network data in a savefile over server restart
  778. travelnet.save_data();
  779. end
  780. travelnet.can_dig = function( pos, player, description )
  781. -- forbid digging of the travelnet
  782. return false;
  783. end
  784. -- obsolete function
  785. travelnet.can_dig_old = function( pos, player, description )
  786. if( not( player )) then
  787. return false;
  788. end
  789. local name = player:get_player_name();
  790. local meta = minetest.get_meta( pos );
  791. local owner = meta:get_string('owner');
  792. local network_name = meta:get_string( "station_network" );
  793. -- in creative mode, accidental digging could happen too easily when trying to update the net
  794. if(creative and creative.is_enabled_for(player:get_player_name())) then
  795. -- only a diamond pick can dig the travelnet
  796. if( not(player:get_wielded_item())
  797. or player:get_wielded_item():get_name()~="default:pick_diamond") then
  798. return false;
  799. end
  800. end
  801. -- players with that priv can dig regardless of owner
  802. if( minetest.check_player_privs(name, {travelnet_remove=true})
  803. or travelnet.allow_dig( name, owner, network_name )) then
  804. return true;
  805. end
  806. if( not( meta ) or not( owner) or owner=='') then
  807. minetest.chat_send_player(name, S("This %s has not been configured yet. Please set it up first to claim it. Afterwards you can remove it because you are then the owner."):format(description));
  808. return false;
  809. elseif( owner ~= name ) then
  810. minetest.chat_send_player(name, S("This %s belongs to %s. You can't remove it."):format(description, tostring( meta:get_string('owner'))));
  811. return false;
  812. end
  813. return true;
  814. end
  815. if( travelnet.travelnet_effect_enabled ) then
  816. minetest.register_entity( 'travelnet:effect', {
  817. hp_max = 1,
  818. physical = false,
  819. weight = 5,
  820. collisionbox = {-0.4,-0.5,-0.4, 0.4,1.5,0.4},
  821. visual = "upright_sprite",
  822. visual_size = {x=1, y=2},
  823. -- mesh = "model",
  824. textures = { "travelnet_flash.png" }, -- number of required textures depends on visual
  825. -- colors = {}, -- number of required colors depends on visual
  826. spritediv = {x=1, y=1},
  827. initial_sprite_basepos = {x=0, y=0},
  828. is_visible = true,
  829. makes_footstep_sound = false,
  830. automatic_rotate = true,
  831. anz_rotations = 0,
  832. on_step = function( self, dtime )
  833. -- this is supposed to be more flickering than smooth animation
  834. self.object:setyaw( self.object:getyaw()+1);
  835. self.anz_rotations = self.anz_rotations + 1;
  836. -- eventually self-destruct
  837. if( self.anz_rotations > 15 ) then
  838. self.object:remove();
  839. end
  840. end
  841. })
  842. end
  843. if( travelnet.travelnet_enabled ) then
  844. dofile(travelnet.path.."/travelnet.lua"); -- the travelnet node definition
  845. end
  846. if( travelnet.elevator_enabled ) then
  847. dofile(travelnet.path.."/elevator.lua"); -- allows up/down transfers only
  848. end
  849. if( travelnet.doors_enabled ) then
  850. dofile(travelnet.path.."/doors.lua"); -- doors that open and close automaticly when the travelnet or elevator is used
  851. end
  852. if( travelnet.abm_enabled ) then
  853. dofile(travelnet.path.."/restore_network_via_abm.lua"); -- restore travelnet data when players pass by broken networks
  854. end
  855. -- upon server start, read the savefile
  856. travelnet.restore_data();