cart_entity.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. -- carts/cart_entity.lua
  2. -- support for MT game translation.
  3. local S = carts.get_translator
  4. local cart_entity = {
  5. initial_properties = {
  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. },
  13. driver = nil,
  14. punched = false, -- used to re-send velocity and position
  15. velocity = {x=0, y=0, z=0}, -- only used on punch
  16. old_dir = {x=1, y=0, z=0}, -- random value to start the cart on punch
  17. old_pos = nil,
  18. old_switch = 0,
  19. railtype = nil,
  20. attached_items = {}
  21. }
  22. function cart_entity:on_rightclick(clicker)
  23. if not clicker or not clicker:is_player() then
  24. return
  25. end
  26. local player_name = clicker:get_player_name()
  27. if self.driver and player_name == self.driver then
  28. carts:manage_attachment(clicker, nil)
  29. elseif not self.driver then
  30. carts:manage_attachment(clicker, self.object)
  31. self.driver = player_name
  32. end
  33. end
  34. function cart_entity:on_activate(staticdata, dtime_s)
  35. self.object:set_armor_groups({immortal=1})
  36. if string.sub(staticdata, 1, string.len("return")) ~= "return" then
  37. return
  38. end
  39. local data = minetest.deserialize(staticdata)
  40. if type(data) ~= "table" then
  41. return
  42. end
  43. self.railtype = data.railtype
  44. self.old_dir = data.old_dir or self.old_dir
  45. end
  46. function cart_entity:get_staticdata()
  47. return minetest.serialize({
  48. railtype = self.railtype,
  49. old_dir = self.old_dir
  50. })
  51. end
  52. -- 0.5.x and later: When the driver leaves
  53. function cart_entity:on_detach_child(child)
  54. if child and child:get_player_name() == self.driver then
  55. -- Clean up eye height
  56. carts:manage_attachment(child, nil)
  57. self.driver = nil
  58. end
  59. end
  60. function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
  61. local pos = self.object:get_pos()
  62. local vel = self.object:get_velocity()
  63. if not self.railtype or vector.equals(vel, {x=0, y=0, z=0}) then
  64. local node = minetest.get_node(pos).name
  65. self.railtype = minetest.get_item_group(node, "connect_to_raillike")
  66. end
  67. -- Punched by non-player
  68. if not puncher or not puncher:is_player() then
  69. local cart_dir = carts:get_rail_direction(pos, self.old_dir, nil, nil, self.railtype)
  70. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  71. return
  72. end
  73. self.velocity = vector.multiply(cart_dir, 2)
  74. self.punched = true
  75. return
  76. end
  77. -- Player digs cart by sneak-punch
  78. if puncher:get_player_control().sneak then
  79. if self.sound_handle then
  80. minetest.sound_stop(self.sound_handle)
  81. end
  82. -- Detach driver and items
  83. if self.driver then
  84. if self.old_pos then
  85. self.object:set_pos(self.old_pos)
  86. end
  87. local player = minetest.get_player_by_name(self.driver)
  88. carts:manage_attachment(player, nil)
  89. end
  90. for _, obj_ in ipairs(self.attached_items) do
  91. if obj_ then
  92. obj_:set_detach()
  93. end
  94. end
  95. -- Pick up cart
  96. local inv = puncher:get_inventory()
  97. if not minetest.is_creative_enabled(puncher:get_player_name())
  98. or not inv:contains_item("main", "carts:cart") then
  99. local leftover = inv:add_item("main", "carts:cart")
  100. -- If no room in inventory add a replacement cart to the world
  101. if not leftover:is_empty() then
  102. minetest.add_item(self.object:get_pos(), leftover)
  103. end
  104. end
  105. self.object:remove()
  106. return
  107. end
  108. -- Player punches cart to alter velocity
  109. if puncher:get_player_name() == self.driver then
  110. if math.abs(vel.x + vel.z) > carts.punch_speed_max then
  111. return
  112. end
  113. end
  114. local punch_dir = carts:velocity_to_dir(puncher:get_look_dir())
  115. punch_dir.y = 0
  116. local cart_dir = carts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
  117. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  118. return
  119. end
  120. local punch_interval = 1
  121. -- Faulty tool registrations may cause the interval to be set to 0 !
  122. if tool_capabilities and (tool_capabilities.full_punch_interval or 0) > 0 then
  123. punch_interval = tool_capabilities.full_punch_interval
  124. end
  125. time_from_last_punch = math.min(time_from_last_punch or punch_interval, punch_interval)
  126. local f = 2 * (time_from_last_punch / punch_interval)
  127. self.velocity = vector.multiply(cart_dir, f)
  128. self.old_dir = cart_dir
  129. self.punched = true
  130. end
  131. local function rail_on_step_event(handler, obj, dtime)
  132. if handler then
  133. handler(obj, dtime)
  134. end
  135. end
  136. -- sound refresh interval = 1.0sec
  137. local function rail_sound(self, dtime)
  138. if not self.sound_ttl then
  139. self.sound_ttl = 1.0
  140. return
  141. elseif self.sound_ttl > 0 then
  142. self.sound_ttl = self.sound_ttl - dtime
  143. return
  144. end
  145. self.sound_ttl = 1.0
  146. if self.sound_handle then
  147. local handle = self.sound_handle
  148. self.sound_handle = nil
  149. minetest.after(0.2, minetest.sound_stop, handle)
  150. end
  151. local vel = self.object:get_velocity()
  152. local speed = vector.length(vel)
  153. if speed > 0 then
  154. self.sound_handle = minetest.sound_play(
  155. "carts_cart_moving", {
  156. object = self.object,
  157. gain = (speed / carts.speed_max) / 2,
  158. loop = true,
  159. })
  160. end
  161. end
  162. local function get_railparams(pos)
  163. local node = minetest.get_node(pos)
  164. return carts.railparams[node.name] or {}
  165. end
  166. local v3_len = vector.length
  167. local function rail_on_step(self, dtime)
  168. local vel = self.object:get_velocity()
  169. if self.punched then
  170. vel = vector.add(vel, self.velocity)
  171. self.object:set_velocity(vel)
  172. self.old_dir.y = 0
  173. elseif vector.equals(vel, {x=0, y=0, z=0}) then
  174. return
  175. end
  176. local pos = self.object:get_pos()
  177. local dir = carts:velocity_to_dir(vel)
  178. local dir_changed = not vector.equals(dir, self.old_dir)
  179. local update = {}
  180. if self.old_pos and not self.punched and not dir_changed then
  181. local flo_pos = vector.round(pos)
  182. local flo_old = vector.round(self.old_pos)
  183. if vector.equals(flo_pos, flo_old) then
  184. -- Do not check one node multiple times
  185. return
  186. end
  187. end
  188. local ctrl, player
  189. -- Get player controls
  190. if self.driver then
  191. player = minetest.get_player_by_name(self.driver)
  192. if player then
  193. ctrl = player:get_player_control()
  194. end
  195. end
  196. local stop_wiggle = false
  197. if self.old_pos and not dir_changed then
  198. -- Detection for "skipping" nodes (perhaps use average dtime?)
  199. -- It's sophisticated enough to take the acceleration in account
  200. local acc = self.object:get_acceleration()
  201. local distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc))
  202. local new_pos, new_dir = carts:pathfinder(
  203. pos, self.old_pos, self.old_dir, distance, ctrl,
  204. self.old_switch, self.railtype
  205. )
  206. if new_pos then
  207. -- No rail found: set to the expected position
  208. pos = new_pos
  209. update.pos = true
  210. dir = new_dir
  211. end
  212. elseif self.old_pos and self.old_dir.y ~= 1 and not self.punched then
  213. -- Stop wiggle
  214. stop_wiggle = true
  215. end
  216. local railparams
  217. -- dir: New moving direction of the cart
  218. -- switch_keys: Currently pressed L(1) or R(2) key,
  219. -- used to ignore the key on the next rail node
  220. local switch_keys
  221. dir, switch_keys = carts:get_rail_direction(
  222. pos, dir, ctrl, self.old_switch, self.railtype
  223. )
  224. dir_changed = not vector.equals(dir, self.old_dir)
  225. local acc = 0
  226. if stop_wiggle or vector.equals(dir, {x=0, y=0, z=0}) then
  227. dir = vector.new(self.old_dir)
  228. vel = {x = 0, y = 0, z = 0}
  229. local pos_r = vector.round(pos)
  230. if not carts:is_rail(pos_r, self.railtype)
  231. and self.old_pos then
  232. pos = self.old_pos
  233. elseif not stop_wiggle then
  234. -- End of rail: Smooth out.
  235. pos = pos_r
  236. dir_changed = false
  237. dir.y = 0
  238. else
  239. pos.y = math.floor(pos.y + 0.5)
  240. end
  241. update.pos = true
  242. update.vel = true
  243. else
  244. -- Direction change detected
  245. if dir_changed then
  246. vel = vector.multiply(dir, math.abs(vel.x + vel.z))
  247. update.vel = true
  248. if dir.y ~= self.old_dir.y then
  249. pos = vector.round(pos)
  250. update.pos = true
  251. end
  252. end
  253. -- Center on the rail
  254. if dir.z ~= 0 and math.floor(pos.x + 0.5) ~= pos.x then
  255. pos.x = math.floor(pos.x + 0.5)
  256. update.pos = true
  257. end
  258. if dir.x ~= 0 and math.floor(pos.z + 0.5) ~= pos.z then
  259. pos.z = math.floor(pos.z + 0.5)
  260. update.pos = true
  261. end
  262. -- Slow down or speed up..
  263. acc = dir.y * -4.0
  264. -- Get rail for corrected position
  265. railparams = get_railparams(pos)
  266. -- no need to check for railparams == nil since we always make it exist.
  267. local speed_mod = railparams.acceleration
  268. if speed_mod and speed_mod ~= 0 then
  269. -- Try to make it similar to the original carts mod
  270. acc = acc + speed_mod
  271. else
  272. -- Handbrake or coast
  273. if ctrl and ctrl.down then
  274. acc = acc - 3
  275. else
  276. acc = acc - 0.4
  277. end
  278. end
  279. end
  280. -- Limit cart speed
  281. local vel_len = vector.length(vel)
  282. if vel_len > carts.speed_max then
  283. vel = vector.multiply(vel, carts.speed_max / vel_len)
  284. update.vel = true
  285. end
  286. if vel_len >= carts.speed_max and acc > 0 then
  287. acc = 0
  288. end
  289. self.object:set_acceleration(vector.multiply(dir, acc))
  290. self.old_pos = vector.round(pos)
  291. self.old_dir = vector.new(dir)
  292. self.old_switch = switch_keys
  293. if self.punched then
  294. -- Collect dropped items
  295. for _, obj_ in pairs(minetest.get_objects_inside_radius(pos, 1)) do
  296. local ent = obj_:get_luaentity()
  297. -- Careful here: physical_state and disable_physics are item-internal APIs
  298. if ent and ent.name == "__builtin:item" and ent.physical_state then
  299. ent:disable_physics()
  300. obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
  301. self.attached_items[#self.attached_items + 1] = obj_
  302. end
  303. end
  304. self.punched = false
  305. update.vel = true
  306. end
  307. railparams = railparams or get_railparams(pos)
  308. if not (update.vel or update.pos) then
  309. rail_on_step_event(railparams.on_step, self, dtime)
  310. return
  311. end
  312. local yaw = 0
  313. if dir.x < 0 then
  314. yaw = 0.5
  315. elseif dir.x > 0 then
  316. yaw = 1.5
  317. elseif dir.z < 0 then
  318. yaw = 1
  319. end
  320. self.object:set_yaw(yaw * math.pi)
  321. local anim = {x=0, y=0}
  322. if dir.y == -1 then
  323. anim = {x=1, y=1}
  324. elseif dir.y == 1 then
  325. anim = {x=2, y=2}
  326. end
  327. self.object:set_animation(anim, 1, 0)
  328. if update.vel then
  329. self.object:set_velocity(vel)
  330. end
  331. if update.pos then
  332. if dir_changed then
  333. self.object:set_pos(pos)
  334. else
  335. self.object:move_to(pos)
  336. end
  337. end
  338. -- call event handler
  339. rail_on_step_event(railparams.on_step, self, dtime)
  340. end
  341. function cart_entity:on_step(dtime)
  342. rail_on_step(self, dtime)
  343. rail_sound(self, dtime)
  344. end
  345. minetest.register_entity("carts:cart", cart_entity)
  346. minetest.register_craftitem("carts:cart", {
  347. description = S("Cart") .. "\n" .. S("(Sneak+Click to pick up)"),
  348. inventory_image = minetest.inventorycube("carts_cart_top.png", "carts_cart_front.png", "carts_cart_side.png"),
  349. wield_image = "carts_cart_front.png",
  350. on_place = function(itemstack, placer, pointed_thing)
  351. local under = pointed_thing.under
  352. local node = minetest.get_node(under)
  353. local udef = minetest.registered_nodes[node.name]
  354. if udef and udef.on_rightclick and
  355. not (placer and placer:is_player() and
  356. placer:get_player_control().sneak) then
  357. return udef.on_rightclick(under, node, placer, itemstack,
  358. pointed_thing) or itemstack
  359. end
  360. if pointed_thing.type ~= "node" then
  361. return
  362. end
  363. if carts:is_rail(pointed_thing.under) then
  364. minetest.add_entity(pointed_thing.under, "carts:cart")
  365. elseif carts:is_rail(pointed_thing.above) then
  366. minetest.add_entity(pointed_thing.above, "carts:cart")
  367. else
  368. return
  369. end
  370. minetest.sound_play({name = "default_place_node_metal", gain = 0.5},
  371. {pos = pointed_thing.above}, true)
  372. if not minetest.is_creative_enabled(placer:get_player_name()) then
  373. itemstack:take_item()
  374. end
  375. return itemstack
  376. end,
  377. })
  378. minetest.register_craft({
  379. output = "carts:cart",
  380. recipe = {
  381. {"default:steel_ingot", "", "default:steel_ingot"},
  382. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  383. },
  384. })