cart_entity.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. -- Localize for performance.
  2. local vector_round = vector.round
  3. local math_floor = math.floor
  4. local math_min = math.min
  5. local cart_entity = {
  6. physical = false, -- otherwise going uphill breaks
  7. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  8. visual = "mesh",
  9. mesh = "carts_cart.b3d",
  10. visual_size = {x=1, y=1},
  11. textures = {"carts_cart.png"},
  12. driver = nil,
  13. punched = false, -- used to re-send velocity and position
  14. velocity = {x=0, y=0, z=0}, -- only used on punch
  15. old_dir = {x=1, y=0, z=0}, -- random value to start the cart on punch
  16. old_pos = nil,
  17. old_switch = 0,
  18. railtype = nil,
  19. attached_items = {}
  20. }
  21. function cart_entity:on_rightclick(clicker)
  22. if not clicker or not clicker:is_player() then
  23. return
  24. end
  25. if clicker:get_hp() == 0 then
  26. return
  27. end
  28. local player_name = clicker:get_player_name()
  29. if self.driver and player_name == self.driver then
  30. self.driver = nil
  31. carts:manage_attachment(clicker, nil)
  32. elseif not self.driver then
  33. if not default.player_attached[player_name] then
  34. self.driver = player_name
  35. carts:manage_attachment(clicker, self.object)
  36. end
  37. end
  38. end
  39. function cart_entity:on_activate(staticdata, dtime_s)
  40. self.object:set_armor_groups({immortal=1})
  41. if string.sub(staticdata, 1, string.len("return")) ~= "return" then
  42. return
  43. end
  44. local data = minetest.deserialize(staticdata)
  45. if not data or type(data) ~= "table" then
  46. return
  47. end
  48. self.railtype = data.railtype
  49. if data.old_dir then
  50. self.old_dir = data.old_dir
  51. end
  52. if data.old_vel then
  53. self.old_vel = data.old_vel
  54. end
  55. end
  56. function cart_entity:get_staticdata()
  57. return minetest.serialize({
  58. railtype = self.railtype,
  59. old_dir = self.old_dir,
  60. old_vel = self.old_vel
  61. })
  62. end
  63. function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
  64. local pos = self.object:get_pos()
  65. local vel = self.object:get_velocity()
  66. if not self.railtype or vector.equals(vel, {x=0, y=0, z=0}) then
  67. local node = minetest.get_node(pos).name
  68. self.railtype = minetest.get_item_group(node, "connect_to_raillike")
  69. end
  70. if not puncher or not puncher:is_player() then
  71. local cart_dir = carts:get_rail_direction(pos, self.old_dir, nil, nil, self.railtype)
  72. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  73. return
  74. end
  75. self.velocity = vector.multiply(cart_dir, 3)
  76. self.punched = true
  77. return
  78. end
  79. if puncher:get_player_control().sneak then
  80. if self.sound_handle then
  81. minetest.sound_stop(self.sound_handle)
  82. end
  83. -- Pick up cart: Drop all attachments
  84. if self.driver then
  85. if self.old_pos then
  86. self.object:set_pos(self.old_pos)
  87. end
  88. local player = minetest.get_player_by_name(self.driver)
  89. carts:manage_attachment(player, nil)
  90. end
  91. for _,obj_ in ipairs(self.attached_items) do
  92. if obj_ then
  93. obj_:set_detach()
  94. end
  95. end
  96. local inv = puncher:get_inventory()
  97. local leftover = inv:add_item("main", "carts:cart")
  98. if not leftover:is_empty() then
  99. minetest.add_item(self.object:get_pos(), leftover)
  100. end
  101. self.object:remove()
  102. return
  103. end
  104. if puncher:get_player_name() == self.driver then
  105. if math.abs(vel.x + vel.z) > carts.punch_speed_max then
  106. return
  107. end
  108. end
  109. local punch_dir = carts:velocity_to_dir(puncher:get_look_dir())
  110. punch_dir.y = 0
  111. local cart_dir = carts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
  112. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  113. return
  114. end
  115. local punch_interval = 1
  116. if tool_capabilities and tool_capabilities.full_punch_interval then
  117. punch_interval = tool_capabilities.full_punch_interval
  118. end
  119. time_from_last_punch = math_min(time_from_last_punch or punch_interval, punch_interval)
  120. local f = 2 * (time_from_last_punch / punch_interval)
  121. self.velocity = vector.multiply(cart_dir, f)
  122. self.old_dir = cart_dir
  123. self.punched = true
  124. end
  125. local function rail_on_step_event(handler, obj, dtime)
  126. if handler then
  127. handler(obj, dtime)
  128. end
  129. end
  130. -- sound refresh interval = 1.0sec
  131. local function rail_sound(self, dtime)
  132. if not self.sound_ttl then
  133. self.sound_ttl = 1.0
  134. return
  135. elseif self.sound_ttl > 0 then
  136. self.sound_ttl = self.sound_ttl - dtime
  137. return
  138. end
  139. self.sound_ttl = 1.0
  140. if self.sound_handle then
  141. local handle = self.sound_handle
  142. self.sound_handle = nil
  143. minetest.after(0.2, minetest.sound_stop, handle)
  144. end
  145. local vel = self.object:get_velocity()
  146. local speed = vector.length(vel)
  147. if speed > 0 then
  148. self.sound_handle = minetest.sound_play(
  149. "carts_cart_moving", {
  150. object = self.object,
  151. gain = (speed / carts.speed_max) / 2,
  152. loop = true,
  153. })
  154. end
  155. end
  156. local function get_railparams(pos)
  157. local node = minetest.get_node(pos)
  158. return carts.railparams[node.name] or {}
  159. end
  160. local function rail_on_step(self, dtime)
  161. local vel = self.object:get_velocity()
  162. if self.punched then
  163. vel = vector.add(vel, self.velocity)
  164. self.object:set_velocity(vel)
  165. self.old_dir.y = 0
  166. elseif vector.equals(vel, {x=0, y=0, z=0}) then
  167. return
  168. end
  169. local pos = self.object:get_pos()
  170. local update = {}
  171. -- stop cart if velocity vector flips
  172. if self.old_vel and self.old_vel.y == 0 and
  173. (self.old_vel.x * vel.x < 0 or self.old_vel.z * vel.z < 0) then
  174. self.old_vel = {x = 0, y = 0, z = 0}
  175. self.old_pos = pos
  176. self.object:set_velocity(vector.new())
  177. self.object:set_acceleration(vector.new())
  178. rail_on_step_event(get_railparams(pos).on_step, self, dtime)
  179. return
  180. end
  181. self.old_vel = vector.new(vel)
  182. if self.old_pos and not self.punched then
  183. local flo_pos = vector_round(pos)
  184. local flo_old = vector_round(self.old_pos)
  185. if vector.equals(flo_pos, flo_old) then
  186. -- Do not check one node multiple times
  187. return
  188. end
  189. end
  190. local ctrl, player
  191. -- Get player controls
  192. if self.driver then
  193. player = minetest.get_player_by_name(self.driver)
  194. if player then
  195. ctrl = player:get_player_control()
  196. end
  197. end
  198. if self.old_pos then
  199. -- Detection for "skipping" nodes
  200. local found_path = carts:pathfinder(
  201. pos, self.old_pos, self.old_dir, ctrl, self.old_switch, self.railtype
  202. )
  203. if not found_path then
  204. -- No rail found: reset back to the expected position
  205. pos = vector.new(self.old_pos)
  206. update.pos = true
  207. end
  208. end
  209. local cart_dir = carts:velocity_to_dir(vel)
  210. local railparams
  211. -- dir: New moving direction of the cart
  212. -- switch_keys: Currently pressed L/R key, used to ignore the key on the next rail node
  213. local dir, switch_keys = carts:get_rail_direction(
  214. pos, cart_dir, ctrl, self.old_switch, self.railtype
  215. )
  216. local new_acc = {x=0, y=0, z=0}
  217. if vector.equals(dir, {x=0, y=0, z=0}) then
  218. vel = {x = 0, y = 0, z = 0}
  219. pos = vector_round(pos)
  220. update.pos = true
  221. update.vel = true
  222. else
  223. -- Direction change detected
  224. if not vector.equals(dir, self.old_dir) then
  225. vel = vector.multiply(dir, math.abs(vel.x + vel.z))
  226. update.vel = true
  227. if dir.y ~= self.old_dir.y then
  228. pos = vector_round(pos)
  229. update.pos = true
  230. end
  231. end
  232. -- Center on the rail
  233. if dir.z ~= 0 and math_floor(pos.x + 0.5) ~= pos.x then
  234. pos.x = math_floor(pos.x + 0.5)
  235. update.pos = true
  236. end
  237. if dir.x ~= 0 and math_floor(pos.z + 0.5) ~= pos.z then
  238. pos.z = math_floor(pos.z + 0.5)
  239. update.pos = true
  240. end
  241. -- Slow down or speed up..
  242. local acc = dir.y * -4.0
  243. -- Get rail for corrected position
  244. railparams = get_railparams(pos)
  245. -- no need to check for railparams == nil since we always make it exist.
  246. local speed_mod = railparams.acceleration
  247. if speed_mod and speed_mod ~= 0 then
  248. -- Try to make it similar to the original carts mod
  249. acc = acc + speed_mod
  250. else
  251. -- Handbrake
  252. if ctrl and ctrl.down then
  253. acc = acc - 3
  254. else
  255. acc = acc - 0.4
  256. end
  257. end
  258. new_acc = vector.multiply(dir, acc)
  259. end
  260. -- Limits
  261. local max_vel = carts.speed_max
  262. for _, v in pairs({"x","y","z"}) do
  263. if math.abs(vel[v]) > max_vel then
  264. vel[v] = carts:get_sign(vel[v]) * max_vel
  265. new_acc[v] = 0
  266. update.vel = true
  267. end
  268. end
  269. self.object:set_acceleration(new_acc)
  270. self.old_pos = vector.new(pos)
  271. if not vector.equals(dir, {x=0, y=0, z=0}) then
  272. self.old_dir = vector.new(dir)
  273. end
  274. self.old_switch = switch_keys
  275. if self.punched then
  276. -- Collect dropped items
  277. for _, obj_ in pairs(minetest.get_objects_inside_radius(pos, 1)) do
  278. if not obj_:is_player() and
  279. obj_:get_luaentity() and
  280. not obj_:get_luaentity().physical_state and
  281. obj_:get_luaentity().name == "__builtin:item" then
  282. obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
  283. self.attached_items[#self.attached_items + 1] = obj_
  284. end
  285. end
  286. self.punched = false
  287. update.vel = true
  288. end
  289. railparams = railparams or get_railparams(pos)
  290. if not (update.vel or update.pos) then
  291. rail_on_step_event(railparams.on_step, self, dtime)
  292. return
  293. end
  294. local yaw = 0
  295. if self.old_dir.x < 0 then
  296. yaw = 0.5
  297. elseif self.old_dir.x > 0 then
  298. yaw = 1.5
  299. elseif self.old_dir.z < 0 then
  300. yaw = 1
  301. end
  302. self.object:setyaw(yaw * math.pi)
  303. local anim = {x=0, y=0}
  304. if dir.y == -1 then
  305. anim = {x=1, y=1}
  306. elseif dir.y == 1 then
  307. anim = {x=2, y=2}
  308. end
  309. self.object:set_animation(anim, 1, 0)
  310. self.object:set_velocity(vel)
  311. if update.pos then
  312. self.object:set_pos(pos)
  313. end
  314. -- call event handler
  315. rail_on_step_event(railparams.on_step, self, dtime)
  316. end
  317. function cart_entity:on_step(dtime)
  318. rail_on_step(self, dtime)
  319. rail_sound(self, dtime)
  320. end
  321. minetest.register_entity("carts:cart", cart_entity)
  322. minetest.register_craftitem("carts:cart", {
  323. description = "Mine Cart\n\nShift+punch to pick up.",
  324. inventory_image = minetest.inventorycube("carts_cart_top.png", "carts_cart_side.png", "carts_cart_side.png"),
  325. wield_image = "carts_cart_side.png",
  326. on_place = function(itemstack, placer, pointed_thing)
  327. local under = pointed_thing.under
  328. local node = minetest.get_node(under)
  329. local udef = minetest.reg_ns_nodes[node.name]
  330. if udef and udef.on_rightclick and
  331. not (placer and placer:get_player_control().sneak) then
  332. return udef.on_rightclick(under, node, placer, itemstack,
  333. pointed_thing) or itemstack
  334. end
  335. if not pointed_thing.type == "node" then
  336. return
  337. end
  338. if carts:is_rail(pointed_thing.under) then
  339. minetest.add_entity(pointed_thing.under, "carts:cart")
  340. elseif carts:is_rail(pointed_thing.above) then
  341. minetest.add_entity(pointed_thing.above, "carts:cart")
  342. else
  343. return
  344. end
  345. minetest.sound_play({name = "default_place_node_metal", gain = 0.5},
  346. {pos = pointed_thing.above})
  347. itemstack:take_item()
  348. return itemstack
  349. end,
  350. })
  351. minetest.register_craft({
  352. output = "carts:cart",
  353. recipe = {
  354. {"default:steel_ingot", "xdecor:lever_off", "group:leather_padding"},
  355. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  356. {"carbon_steel:ingot", "techcrafts:electric_motor", "carbon_steel:ingot"},
  357. },
  358. })