functions.lua 5.4 KB

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