elevator.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. -- This version of the travelnet box allows to move up or down only.
  2. -- The network name is determined automaticly from the position (x/z coordinates).
  3. -- >utor: Sokomine
  4. local S = travelnet.S;
  5. travelnet.show_nearest_elevator = function( pos, owner_name, param2 )
  6. if( not( pos ) or not(pos.x) or not(pos.z) or not( owner_name )) then
  7. return;
  8. end
  9. if( not( travelnet.targets[ owner_name ] )) then
  10. minetest.chat_send_player( owner_name, S("Congratulations! This is your first elevator."..
  11. "You can build an elevator network by placing further elevators somewhere above "..
  12. "or below this one. Just make sure that the x and z coordinate are the same."));
  13. return;
  14. end
  15. local network_name = tostring( pos.x )..','..tostring( pos.z );
  16. -- will this be an elevator that will be added to an existing network?
  17. if( travelnet.targets[ owner_name ][ network_name ]
  18. -- does the network have any members at all?
  19. and next( travelnet.targets[ owner_name ][ network_name ], nil )) then
  20. minetest.chat_send_player( owner_name, S("This elevator will automaticly connect to the "..
  21. "other elevators you have placed at diffrent heights. Just enter a station name "..
  22. "and click on \"store\" to set it up. Or just punch it to set the height as station "..
  23. "name."));
  24. return;
  25. end
  26. local nearest_name = "";
  27. local nearest_dist = 100000000;
  28. local nearest_dist_x = 0;
  29. local nearest_dist_z = 0;
  30. for network_name, data in pairs( travelnet.targets[ owner_name ] ) do
  31. local station_name = next( data, nil );
  32. if( station_name and data[ station_name ][ "nr" ] and data[ station_name ].pos) then
  33. local station_pos = data[ station_name ].pos;
  34. local dist = math.ceil(math.sqrt(
  35. ( station_pos.x - pos.x ) * ( station_pos.x - pos.x )
  36. + ( station_pos.z - pos.z ) * ( station_pos.z - pos.z )));
  37. -- find the nearest one; store network_name and (minimal) distance
  38. if( dist < nearest_dist ) then
  39. nearest_dist = dist;
  40. nearest_dist_x = station_pos.x - pos.x;
  41. nearest_dist_z = station_pos.z - pos.z;
  42. nearest_name = network_name;
  43. end
  44. end
  45. end
  46. if( nearest_name ~= "" ) then
  47. local text = S("Your nearest elevator network is located").." ";
  48. -- in front of/behind
  49. if( (param2==0 and nearest_dist_z>=0)or (param2==2 and nearest_dist_z<=0)) then
  50. text = text..tostring( math.abs(nearest_dist_z )).." "..S("m behind this elevator and");
  51. elseif((param2==1 and nearest_dist_x>=0)or (param2==3 and nearest_dist_x<=0)) then
  52. text = text..tostring( math.abs(nearest_dist_x )).." "..S("m behind this elevator and");
  53. elseif((param2==0 and nearest_dist_z< 0)or (param2==2 and nearest_dist_z> 0)) then
  54. text = text..tostring( math.abs(nearest_dist_z )).." "..S("m in front of this elevator and");
  55. elseif((param2==1 and nearest_dist_x< 0)or (param2==3 and nearest_dist_x> 0)) then
  56. text = text..tostring( math.abs(nearest_dist_x )).." "..S("m in front of this elevator and");
  57. else text = text..S(" ERROR");
  58. end
  59. text = text.." ";
  60. -- right/left
  61. if( (param2==0 and nearest_dist_x< 0)or (param2==2 and nearest_dist_x> 0)) then
  62. text = text..tostring( math.abs(nearest_dist_x )).." "..S("m to the left");
  63. elseif((param2==1 and nearest_dist_z>=0)or (param2==3 and nearest_dist_z<=0)) then
  64. text = text..tostring( math.abs(nearest_dist_z )).." "..S("m to the left");
  65. elseif((param2==0 and nearest_dist_x>=0)or (param2==2 and nearest_dist_x<=0)) then
  66. text = text..tostring( math.abs(nearest_dist_x )).." "..S("m to the right");
  67. elseif((param2==1 and nearest_dist_z< 0)or (param2==3 and nearest_dist_z> 0)) then
  68. text = text..tostring( math.abs(nearest_dist_z )).." "..S("m to the right");
  69. else text = text..S(" ERROR");
  70. end
  71. minetest.chat_send_player( owner_name, text..
  72. S(", located at x").."="..tostring( pos.x+nearest_dist_x)..
  73. ", z="..tostring( pos.z+nearest_dist_z)..
  74. ". "..S("This elevator here will start a new shaft/network."));
  75. else
  76. minetest.chat_send_player( owner_name, S("This is your first elevator. It differs from "..
  77. "travelnet networks by only allowing movement in vertical direction (up or down). "..
  78. "All further elevators which you will place at the same x,z coordinates at differnt "..
  79. "heights will be able to connect to this elevator."));
  80. end
  81. end
  82. minetest.register_node("travelnet:elevator", {
  83. description = S("Elevator"),
  84. drawtype = "mesh",
  85. mesh = "travelnet_elevator.obj",
  86. sunlight_propagates = true,
  87. paramtype = 'light',
  88. paramtype2 = "facedir",
  89. wield_scale = {x=0.6, y=0.6, z=0.6},
  90. selection_box = {
  91. type = "fixed",
  92. fixed = { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 }
  93. },
  94. collision_box = {
  95. type = "fixed",
  96. fixed = {
  97. { 0.48, -0.5,-0.5, 0.5, 0.5, 0.5},
  98. {-0.5 , -0.5, 0.48, 0.48, 0.5, 0.5},
  99. {-0.5, -0.5,-0.5 ,-0.48, 0.5, 0.5},
  100. --groundplate to stand on
  101. { -0.5,-0.5,-0.5,0.5,-0.48, 0.5},
  102. },
  103. },
  104. tiles = travelnet.tiles_elevator,
  105. inventory_image = travelnet.elevator_inventory_image,
  106. groups = {}, --cracky=1,choppy=1,snappy=1,
  107. light_source = 10,
  108. after_place_node = function(pos, placer, itemstack)
  109. local meta = minetest.get_meta(pos);
  110. meta:set_string("infotext", S("Elevator (unconfigured)"));
  111. meta:set_string("station_name", "");
  112. meta:set_string("station_network","");
  113. meta:set_string("owner", placer:get_player_name() );
  114. -- request initial data
  115. meta:set_string("formspec",
  116. "size[12,10]"..
  117. "field[0.3,5.6;6,0.7;station_name;"..S("Name of this station:")..";]"..
  118. -- "field[0.3,6.6;6,0.7;station_network;Assign to Network:;]"..
  119. -- "field[0.3,7.6;6,0.7;owner_name;(optional) owned by:;]"..
  120. "button_exit[6.3,6.2;1.7,0.7;station_set;"..S("Store").."]" );
  121. local p = {x=pos.x, y=pos.y+1, z=pos.z}
  122. local p2 = minetest.dir_to_facedir(placer:get_look_dir())
  123. minetest.add_node(p, {name="travelnet:elevator_top", paramtype2="facedir", param2=p2})
  124. travelnet.show_nearest_elevator( pos, placer:get_player_name(), p2 );
  125. end,
  126. on_receive_fields = travelnet.on_receive_fields,
  127. on_punch = function(pos, node, puncher)
  128. travelnet.update_formspec(pos, puncher:get_player_name())
  129. end,
  130. can_dig = function( pos, player )
  131. return travelnet.can_dig( pos, player, 'elevator' )
  132. end,
  133. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  134. travelnet.remove_box( pos, oldnode, oldmetadata, digger )
  135. end,
  136. -- TNT and overenthusiastic DMs do not destroy elevators either
  137. on_blast = function(pos, intensity)
  138. end,
  139. -- taken from VanessaEs homedecor fridge
  140. on_place = function(itemstack, placer, pointed_thing)
  141. local pos = pointed_thing.above;
  142. local node = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z});
  143. -- leftover elevator_top nodes can be removed by placing a new elevator underneath
  144. if( node ~= nil and node.name ~= "air" and node.name ~= 'travelnet:elevator_top') then
  145. minetest.chat_send_player( placer:get_player_name(), S('Not enough vertical space to place the travelnet box!'))
  146. return;
  147. end
  148. return minetest.item_place(itemstack, placer, pointed_thing);
  149. end,
  150. on_destruct = function(pos)
  151. local p = {x=pos.x, y=pos.y+1, z=pos.z}
  152. minetest.remove_node(p)
  153. end
  154. })
  155. minetest.register_alias("travelnet:elevator_top", "air")
  156. --if( minetest.get_modpath("technic") ~= nil ) then
  157. -- minetest.register_craft({
  158. -- output = "travelnet:elevator",
  159. -- recipe = {
  160. -- {"default:steel_ingot", "technic:motor", "default:steel_ingot", },
  161. -- {"default:steel_ingot", "technic:control_logic_unit", "default:steel_ingot", },
  162. -- {"default:steel_ingot", "moreores:copper_ingot", "default:steel_ingot", }
  163. -- }
  164. -- })
  165. --else
  166. minetest.register_craft({
  167. output = "travelnet:elevator",
  168. recipe = travelnet.elevator_recipe,
  169. })
  170. --end