functions.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. function carts:get_sign(z)
  2. if z == 0 then
  3. return 0
  4. else
  5. return z / math.abs(z)
  6. end
  7. end
  8. function carts:manage_attachment(player, obj)
  9. if not player then
  10. return
  11. end
  12. local status = obj ~= nil
  13. local player_name = player:get_player_name()
  14. if player_api.player_attached[player_name] == status then
  15. return
  16. end
  17. player_api.player_attached[player_name] = status
  18. if status then
  19. player:set_attach(obj, "", {x=0, y=-4.5, z=0}, {x=0, y=0, z=0})
  20. player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
  21. else
  22. player:set_detach()
  23. player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})
  24. end
  25. end
  26. function carts:velocity_to_dir(v)
  27. if math.abs(v.x) > math.abs(v.z) then
  28. return {x=carts:get_sign(v.x), y=carts:get_sign(v.y), z=0}
  29. else
  30. return {x=0, y=carts:get_sign(v.y), z=carts:get_sign(v.z)}
  31. end
  32. end
  33. function carts:is_rail(pos, railtype)
  34. local node = minetest.get_node(pos).name
  35. if node == "ignore" then
  36. local vm = minetest.get_voxel_manip()
  37. local emin, emax = vm:read_from_map(pos, pos)
  38. local area = VoxelArea:new{
  39. MinEdge = emin,
  40. MaxEdge = emax,
  41. }
  42. local data = vm:get_data()
  43. local vi = area:indexp(pos)
  44. node = minetest.get_name_from_content_id(data[vi])
  45. end
  46. if minetest.get_item_group(node, "rail") == 0 then
  47. return false
  48. end
  49. if not railtype then
  50. return true
  51. end
  52. return minetest.get_item_group(node, "connect_to_raillike") == railtype
  53. end
  54. function carts:check_front_up_down(pos, dir_, check_up, railtype)
  55. local dir = vector.new(dir_)
  56. local cur
  57. -- Front
  58. dir.y = 0
  59. cur = vector.add(pos, dir)
  60. if carts:is_rail(cur, railtype) then
  61. return dir
  62. end
  63. -- Up
  64. if check_up then
  65. dir.y = 1
  66. cur = vector.add(pos, dir)
  67. if carts:is_rail(cur, railtype) then
  68. return dir
  69. end
  70. end
  71. -- Down
  72. dir.y = -1
  73. cur = vector.add(pos, dir)
  74. if carts:is_rail(cur, railtype) then
  75. return dir
  76. end
  77. return nil
  78. end
  79. function carts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
  80. local pos = vector.round(pos_)
  81. local cur
  82. local left_check, right_check = true, true
  83. -- Check left and right
  84. local left = {x=0, y=0, z=0}
  85. local right = {x=0, y=0, z=0}
  86. if dir.z ~= 0 and dir.x == 0 then
  87. left.x = -dir.z
  88. right.x = dir.z
  89. elseif dir.x ~= 0 and dir.z == 0 then
  90. left.z = dir.x
  91. right.z = -dir.x
  92. end
  93. if ctrl then
  94. if old_switch == 1 then
  95. left_check = false
  96. elseif old_switch == 2 then
  97. right_check = false
  98. end
  99. if ctrl.left and left_check then
  100. cur = carts:check_front_up_down(pos, left, false, railtype)
  101. if cur then
  102. return cur, 1
  103. end
  104. left_check = false
  105. end
  106. if ctrl.right and right_check then
  107. cur = carts:check_front_up_down(pos, right, false, railtype)
  108. if cur then
  109. return cur, 2
  110. end
  111. right_check = true
  112. end
  113. end
  114. -- Normal
  115. cur = carts:check_front_up_down(pos, dir, true, railtype)
  116. if cur then
  117. return cur
  118. end
  119. -- Left, if not already checked
  120. if left_check then
  121. cur = carts:check_front_up_down(pos, left, false, railtype)
  122. if cur then
  123. return cur
  124. end
  125. end
  126. -- Right, if not already checked
  127. if right_check then
  128. cur = carts:check_front_up_down(pos, right, false, railtype)
  129. if cur then
  130. return cur
  131. end
  132. end
  133. -- Backwards
  134. if not old_switch then
  135. cur = carts:check_front_up_down(pos, {
  136. x = -dir.x,
  137. y = dir.y,
  138. z = -dir.z
  139. }, true, railtype)
  140. if cur then
  141. return cur
  142. end
  143. end
  144. return {x=0, y=0, z=0}
  145. end
  146. function carts:pathfinder(pos_, old_pos, old_dir, ctrl, pf_switch, railtype)
  147. if vector.equals(old_pos, pos_) then
  148. return true
  149. end
  150. local pos = vector.round(pos_)
  151. local pf_pos = vector.round(old_pos)
  152. local pf_dir = vector.new(old_dir)
  153. for i = 1, 3 do
  154. pf_dir, pf_switch = carts:get_rail_direction(
  155. pf_pos, pf_dir, ctrl, pf_switch, railtype)
  156. if vector.equals(pf_dir, {x=0, y=0, z=0}) then
  157. -- No way forwards
  158. return false
  159. end
  160. pf_pos = vector.add(pf_pos, pf_dir)
  161. if vector.equals(pf_pos, pos) then
  162. -- Success! Cart moved on correctly
  163. return true
  164. end
  165. end
  166. -- Cart not found
  167. return false
  168. end
  169. function carts:register_rail(name, def_overwrite, railparams)
  170. local def = {
  171. drawtype = "raillike",
  172. paramtype = "light",
  173. sunlight_propagates = true,
  174. is_ground_content = false,
  175. stack_max = 4,
  176. walkable = false,
  177. selection_box = {
  178. type = "fixed",
  179. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  180. },
  181. sounds = default.node_sound_metal_defaults()
  182. }
  183. for k, v in pairs(def_overwrite) do
  184. def[k] = v
  185. end
  186. if not def.inventory_image then
  187. def.wield_image = def.tiles[1]
  188. def.inventory_image = def.tiles[1]
  189. end
  190. if railparams then
  191. carts.railparams[name] = table.copy(railparams)
  192. end
  193. minetest.register_node(name, def)
  194. end
  195. function carts:get_rail_groups(additional_groups)
  196. -- Get the default rail groups and add more when a table is given
  197. local groups = {
  198. dig_immediate = 2,
  199. attached_node = 1,
  200. rail = 1,
  201. connect_to_raillike = minetest.raillike_group("rail")
  202. }
  203. if type(additional_groups) == "table" then
  204. for k, v in pairs(additional_groups) do
  205. groups[k] = v
  206. end
  207. end
  208. return groups
  209. end