Carts for Minetest, but optimized! Also compatible with MTG carts https://content.minetest.net/packages/Krock/boost_cart/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

457 lines
12KB

  1. function boost_cart:on_rail_step(entity, pos, distance)
  2. -- Play rail sound
  3. if entity.sound_counter <= 0 then
  4. minetest.sound_play("cart_rail", {
  5. pos = pos,
  6. max_hear_distance = 40,
  7. gain = 0.5
  8. })
  9. entity.sound_counter = math.random(4, 15)
  10. end
  11. entity.sound_counter = entity.sound_counter - distance
  12. if boost_cart.MESECONS then
  13. boost_cart:signal_detector_rail(pos)
  14. end
  15. end
  16. local cart_entity = {
  17. initial_properties = {
  18. physical = false,
  19. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  20. visual = "mesh",
  21. mesh = "cart.x",
  22. visual_size = {x=1, y=1},
  23. textures = {"cart.png"},
  24. },
  25. driver = nil,
  26. punched = false, -- used to re-send velocity and position
  27. velocity = {x=0, y=0, z=0}, -- only used on punch
  28. old_dir = {x=1, y=0, z=0}, -- random value to start the cart on punch
  29. old_pos = nil,
  30. old_switch = 0,
  31. sound_counter = 0,
  32. railtype = nil,
  33. attached_items = {}
  34. }
  35. -- Model and textures
  36. if boost_cart.MTG_CARTS then
  37. cart_entity.initial_properties.mesh = "carts_cart.b3d"
  38. cart_entity.initial_properties.textures = {"carts_cart.png"}
  39. end
  40. function cart_entity:on_rightclick(clicker)
  41. if not clicker or not clicker:is_player() then
  42. return
  43. end
  44. local player_name = clicker:get_player_name()
  45. if self.driver and player_name == self.driver then
  46. self.driver = nil
  47. boost_cart:manage_attachment(clicker, nil)
  48. elseif not self.driver then
  49. self.driver = player_name
  50. boost_cart:manage_attachment(clicker, self.object)
  51. if default.player_set_animation then
  52. -- player_api(/default) does not update the animation
  53. -- when the player is attached, reset to default animation
  54. default.player_set_animation(clicker, "stand")
  55. end
  56. end
  57. end
  58. function cart_entity:on_activate(staticdata, dtime_s)
  59. self.object:set_armor_groups({immortal=1})
  60. self.sound_counter = math.random(4, 15)
  61. if string.sub(staticdata, 1, string.len("return")) ~= "return" then
  62. return
  63. end
  64. local data = minetest.deserialize(staticdata)
  65. if type(data) ~= "table" then
  66. return
  67. end
  68. self.railtype = data.railtype
  69. self.old_dir = data.old_dir or self.old_dir
  70. self.old_pos = data.old_pos or self.old_pos
  71. -- Correct the position when the cart drives further after the last 'step()'
  72. if self.old_pos and boost_cart:is_rail(self.old_pos, self.railtype) then
  73. self.object:set_pos(self.old_pos)
  74. end
  75. end
  76. function cart_entity:get_staticdata()
  77. return minetest.serialize({
  78. railtype = self.railtype,
  79. old_dir = self.old_dir,
  80. old_pos = self.old_pos
  81. })
  82. end
  83. -- 0.5.x and later: When the driver leaves
  84. function cart_entity:on_detach_child(child)
  85. if child and child:get_player_name() == self.driver then
  86. self.driver = nil
  87. boost_cart:manage_attachment(child, nil)
  88. end
  89. end
  90. function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
  91. local pos = self.object:get_pos()
  92. local vel = self.object:get_velocity()
  93. if not self.railtype or vector.equals(vel, {x=0, y=0, z=0}) then
  94. local node = minetest.get_node(pos).name
  95. self.railtype = minetest.get_item_group(node, "connect_to_raillike")
  96. end
  97. if not puncher or not puncher:is_player() then
  98. local cart_dir = boost_cart:get_rail_direction(pos, self.old_dir, nil, nil, self.railtype)
  99. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  100. return
  101. end
  102. self.velocity = vector.multiply(cart_dir, 3)
  103. self.punched = true
  104. return
  105. end
  106. if puncher:get_player_control().sneak then
  107. -- Pick up cart: Drop all attachments
  108. if self.driver then
  109. if self.old_pos then
  110. self.object:set_pos(self.old_pos)
  111. end
  112. local player = minetest.get_player_by_name(self.driver)
  113. boost_cart:manage_attachment(player, nil)
  114. end
  115. for _, obj_ in pairs(self.attached_items) do
  116. if obj_ then
  117. obj_:set_detach()
  118. end
  119. end
  120. local leftover = puncher:get_inventory():add_item("main", "carts:cart")
  121. if not leftover:is_empty() then
  122. minetest.add_item(pos, leftover)
  123. end
  124. self.object:remove()
  125. return
  126. end
  127. -- Driver punches to accelerate the cart
  128. if puncher:get_player_name() == self.driver then
  129. if math.abs(vel.x + vel.z) > boost_cart.punch_speed_max then
  130. return
  131. end
  132. end
  133. local punch_dir = boost_cart:velocity_to_dir(puncher:get_look_dir())
  134. punch_dir.y = 0
  135. local cart_dir = boost_cart:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
  136. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  137. return
  138. end
  139. local punch_interval = 1
  140. if tool_capabilities and tool_capabilities.full_punch_interval then
  141. punch_interval = tool_capabilities.full_punch_interval
  142. end
  143. time_from_last_punch = math.min(time_from_last_punch or punch_interval, punch_interval)
  144. local f = 3 * (time_from_last_punch / punch_interval)
  145. self.velocity = vector.multiply(cart_dir, f)
  146. self.old_dir = cart_dir
  147. self.punched = true
  148. end
  149. local v3_len = vector.length
  150. function cart_entity:on_step(dtime)
  151. local vel = self.object:get_velocity()
  152. if self.punched then
  153. vel = vector.add(vel, self.velocity)
  154. self.object:set_velocity(vel)
  155. self.old_dir.y = 0
  156. elseif vector.equals(vel, {x=0, y=0, z=0}) then
  157. return
  158. end
  159. local pos = self.object:get_pos()
  160. local cart_dir = boost_cart:velocity_to_dir(vel)
  161. local same_dir = vector.equals(cart_dir, self.old_dir)
  162. local update = {}
  163. if self.old_pos and not self.punched and same_dir then
  164. local flo_pos = vector.round(pos)
  165. local flo_old = vector.round(self.old_pos)
  166. if vector.equals(flo_pos, flo_old) then
  167. -- Do not check one node multiple times
  168. return
  169. end
  170. end
  171. local ctrl, player
  172. local distance = 1
  173. -- Get player controls
  174. if self.driver then
  175. player = minetest.get_player_by_name(self.driver)
  176. if player then
  177. ctrl = player:get_player_control()
  178. end
  179. end
  180. local stop_wiggle = false
  181. if self.old_pos and same_dir then
  182. -- Detection for "skipping" nodes (perhaps use average dtime?)
  183. -- It's sophisticated enough to take the acceleration in account
  184. local acc = self.object:get_acceleration()
  185. distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc))
  186. local new_pos, new_dir = boost_cart:pathfinder(
  187. pos, self.old_pos, self.old_dir, distance, ctrl,
  188. self.old_switch, self.railtype
  189. )
  190. if new_pos then
  191. -- No rail found: set to the expected position
  192. pos = new_pos
  193. update.pos = true
  194. cart_dir = new_dir
  195. end
  196. elseif self.old_pos and self.old_dir.y ~= 1 and not self.punched then
  197. -- Stop wiggle
  198. stop_wiggle = true
  199. end
  200. -- dir: New moving direction of the cart
  201. -- switch_keys: Currently pressed L(1) or R(2) key,
  202. -- used to ignore the key on the next rail node
  203. local dir, switch_keys = boost_cart:get_rail_direction(
  204. pos, cart_dir, ctrl, self.old_switch, self.railtype
  205. )
  206. local dir_changed = not vector.equals(dir, self.old_dir)
  207. local acc = 0
  208. if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then
  209. vel = {x=0, y=0, z=0}
  210. local pos_r = vector.round(pos)
  211. if not boost_cart:is_rail(pos_r, self.railtype)
  212. and self.old_pos then
  213. pos = self.old_pos
  214. elseif not stop_wiggle then
  215. pos = pos_r
  216. else
  217. pos.y = math.floor(pos.y + 0.5)
  218. end
  219. update.pos = true
  220. update.vel = true
  221. else
  222. -- Direction change detected
  223. if dir_changed then
  224. vel = vector.multiply(dir, math.abs(vel.x + vel.z))
  225. update.vel = true
  226. if dir.y ~= self.old_dir.y then
  227. pos = vector.round(pos)
  228. update.pos = true
  229. end
  230. end
  231. -- Center on the rail
  232. if dir.z ~= 0 and math.floor(pos.x + 0.5) ~= pos.x then
  233. pos.x = math.floor(pos.x + 0.5)
  234. update.pos = true
  235. end
  236. if dir.x ~= 0 and math.floor(pos.z + 0.5) ~= pos.z then
  237. pos.z = math.floor(pos.z + 0.5)
  238. update.pos = true
  239. end
  240. -- Calculate current cart acceleration
  241. acc = nil
  242. local acc_meta = minetest.get_meta(pos):get_string("cart_acceleration")
  243. if acc_meta == "halt" and not self.punched then
  244. -- Stop rail
  245. vel = {x=0, y=0, z=0}
  246. acc = false
  247. pos = vector.round(pos)
  248. update.pos = true
  249. update.vel = true
  250. end
  251. if acc == nil then
  252. -- Meta speed modifier
  253. local speed_mod = tonumber(acc_meta)
  254. if speed_mod and speed_mod ~= 0 then
  255. -- Try to make it similar to the original carts mod
  256. acc = speed_mod * 10
  257. end
  258. end
  259. if acc == nil and boost_cart.MTG_CARTS then
  260. -- MTG Cart API adaption
  261. local rail_node = minetest.get_node(vector.round(pos))
  262. local railparam = carts.railparams[rail_node.name]
  263. if railparam and railparam.acceleration then
  264. acc = railparam.acceleration
  265. end
  266. end
  267. if acc ~= false then
  268. -- Handbrake
  269. if ctrl and ctrl.down then
  270. acc = (acc or 0) - 2
  271. elseif acc == nil then
  272. acc = -0.4
  273. end
  274. end
  275. if ctrl and ctrl.sneak then
  276. -- Descend when sneak is pressed
  277. boost_cart:manage_attachment(player, nil)
  278. player = nil
  279. ctrl = nil
  280. end
  281. if acc then
  282. -- Slow down or speed up, depending on Y direction
  283. acc = acc + dir.y * -2.1
  284. else
  285. acc = 0
  286. end
  287. end
  288. -- Limit cart speed
  289. local vel_len = vector.length(vel)
  290. if vel_len > boost_cart.speed_max then
  291. vel = vector.multiply(vel, boost_cart.speed_max / vel_len)
  292. update.vel = true
  293. end
  294. if vel_len >= boost_cart.speed_max and acc > 0 then
  295. acc = 0
  296. end
  297. self.object:set_acceleration(vector.multiply(dir, acc))
  298. self.old_pos = vector.round(pos)
  299. local old_y_dir = self.old_dir.y
  300. if not vector.equals(dir, {x=0, y=0, z=0}) and not stop_wiggle then
  301. self.old_dir = dir
  302. else
  303. -- Cart stopped, set the animation to 0
  304. self.old_dir.y = 0
  305. end
  306. self.old_switch = switch_keys
  307. boost_cart:on_rail_step(self, self.old_pos, distance)
  308. if self.punched then
  309. -- Collect dropped items
  310. for _, obj_ in pairs(minetest.get_objects_inside_radius(pos, 1)) do
  311. local ent = obj_:get_luaentity()
  312. -- Careful here: physical_state and disable_physics are item-internal APIs
  313. if ent and ent.name == "__builtin:item" and ent.physical_state then
  314. -- Check API to support 5.2.0 and older
  315. if ent.disable_physics then
  316. ent:disable_physics()
  317. end
  318. obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
  319. self.attached_items[#self.attached_items + 1] = obj_
  320. end
  321. end
  322. self.punched = false
  323. update.vel = true
  324. end
  325. if not (update.vel or update.pos) then
  326. return
  327. end
  328. -- Re-use "dir", localize self.old_dir
  329. dir = self.old_dir
  330. local yaw = 0
  331. if dir.x < 0 then
  332. yaw = 0.5
  333. elseif dir.x > 0 then
  334. yaw = 1.5
  335. elseif dir.z < 0 then
  336. yaw = 1
  337. end
  338. self.object:set_yaw(yaw * math.pi)
  339. local anim = {x=0, y=0}
  340. if dir.y == -1 then
  341. anim = {x=1, y=1}
  342. elseif dir.y == 1 then
  343. anim = {x=2, y=2}
  344. end
  345. self.object:set_animation(anim, 1, 0)
  346. -- Change player model rotation, depending on the Y direction
  347. if player and dir.y ~= old_y_dir then
  348. local feet = {x=0, y=-4, z=0}
  349. local eye = {x=0, y=-4, z=0}
  350. if dir.y ~= 0 then
  351. -- TODO: Find a better way to calculate this
  352. feet.y = feet.y + 4
  353. feet.z = -dir.y * 2
  354. eye.z = -dir.y * 8
  355. end
  356. player:set_attach(self.object, "", feet,
  357. {x=dir.y * -30, y=0, z=0})
  358. player:set_eye_offset(eye, eye)
  359. end
  360. if update.vel then
  361. self.object:set_velocity(vel)
  362. end
  363. if update.pos then
  364. if dir_changed then
  365. self.object:set_pos(pos)
  366. else
  367. self.object:move_to(pos)
  368. end
  369. end
  370. end
  371. minetest.register_entity(":carts:cart", cart_entity)
  372. -- Register item to place the entity
  373. if not boost_cart.MTG_CARTS then
  374. minetest.register_craftitem(":carts:cart", {
  375. description = "Cart (Sneak+Click to pick up)",
  376. inventory_image = minetest.inventorycube(
  377. "cart_top.png",
  378. "cart_side.png",
  379. "cart_side.png"
  380. ),
  381. wield_image = "cart_side.png",
  382. on_place = function(itemstack, placer, pointed_thing)
  383. if not pointed_thing.type == "node" then
  384. return
  385. end
  386. if boost_cart:is_rail(pointed_thing.under) then
  387. minetest.add_entity(pointed_thing.under, "carts:cart")
  388. elseif boost_cart:is_rail(pointed_thing.above) then
  389. minetest.add_entity(pointed_thing.above, "carts:cart")
  390. else
  391. return
  392. end
  393. if not minetest.settings:get_bool("creative_mode") then
  394. itemstack:take_item()
  395. end
  396. return itemstack
  397. end,
  398. })
  399. minetest.register_craft({
  400. output = "carts:cart",
  401. recipe = {
  402. {"default:steel_ingot", "", "default:steel_ingot"},
  403. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  404. },
  405. })
  406. end