cart_entity.lua 11 KB

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