cart_entity.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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:getvelocity()
  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. if not minetest.setting_getbool("creative_mode")
  98. or not inv:contains_item("main", "carts:cart") then
  99. local leftover = inv:add_item("main", "carts:cart")
  100. if not leftover:is_empty() then
  101. minetest.add_item(self.object:get_pos(), leftover)
  102. end
  103. end
  104. self.object:remove()
  105. return
  106. end
  107. if puncher:get_player_name() == self.driver then
  108. if math.abs(vel.x + vel.z) > carts.punch_speed_max then
  109. return
  110. end
  111. end
  112. local punch_dir = carts:velocity_to_dir(puncher:get_look_dir())
  113. punch_dir.y = 0
  114. local cart_dir = carts:get_rail_direction(pos, punch_dir, nil, nil, self.railtype)
  115. if vector.equals(cart_dir, {x=0, y=0, z=0}) then
  116. return
  117. end
  118. local punch_interval = 1
  119. if tool_capabilities and tool_capabilities.full_punch_interval then
  120. punch_interval = tool_capabilities.full_punch_interval
  121. end
  122. time_from_last_punch = math_min(time_from_last_punch or punch_interval, punch_interval)
  123. local f = 2 * (time_from_last_punch / punch_interval)
  124. self.velocity = vector.multiply(cart_dir, f)
  125. self.old_dir = cart_dir
  126. self.punched = true
  127. end
  128. local function rail_on_step_event(handler, obj, dtime)
  129. if handler then
  130. handler(obj, dtime)
  131. end
  132. end
  133. -- sound refresh interval = 1.0sec
  134. local function rail_sound(self, dtime)
  135. if not self.sound_ttl then
  136. self.sound_ttl = 1.0
  137. return
  138. elseif self.sound_ttl > 0 then
  139. self.sound_ttl = self.sound_ttl - dtime
  140. return
  141. end
  142. self.sound_ttl = 1.0
  143. if self.sound_handle then
  144. local handle = self.sound_handle
  145. self.sound_handle = nil
  146. minetest.after(0.2, minetest.sound_stop, handle)
  147. end
  148. local vel = self.object:getvelocity()
  149. local speed = vector.length(vel)
  150. if speed > 0 then
  151. self.sound_handle = minetest.sound_play(
  152. "carts_cart_moving", {
  153. object = self.object,
  154. gain = (speed / carts.speed_max) / 2,
  155. loop = true,
  156. })
  157. end
  158. end
  159. local function get_railparams(pos)
  160. local node = minetest.get_node(pos)
  161. return carts.railparams[node.name] or {}
  162. end
  163. local function rail_on_step(self, dtime)
  164. local vel = self.object:getvelocity()
  165. if self.punched then
  166. vel = vector.add(vel, self.velocity)
  167. self.object:setvelocity(vel)
  168. self.old_dir.y = 0
  169. elseif vector.equals(vel, {x=0, y=0, z=0}) then
  170. return
  171. end
  172. local pos = self.object:get_pos()
  173. local update = {}
  174. -- stop cart if velocity vector flips
  175. if self.old_vel and self.old_vel.y == 0 and
  176. (self.old_vel.x * vel.x < 0 or self.old_vel.z * vel.z < 0) then
  177. self.old_vel = {x = 0, y = 0, z = 0}
  178. self.old_pos = pos
  179. self.object:setvelocity(vector.new())
  180. self.object:setacceleration(vector.new())
  181. rail_on_step_event(get_railparams(pos).on_step, self, dtime)
  182. return
  183. end
  184. self.old_vel = vector.new(vel)
  185. if self.old_pos and not self.punched then
  186. local flo_pos = vector_round(pos)
  187. local flo_old = vector_round(self.old_pos)
  188. if vector.equals(flo_pos, flo_old) then
  189. -- Do not check one node multiple times
  190. return
  191. end
  192. end
  193. local ctrl, player
  194. -- Get player controls
  195. if self.driver then
  196. player = minetest.get_player_by_name(self.driver)
  197. if player then
  198. ctrl = player:get_player_control()
  199. end
  200. end
  201. if self.old_pos then
  202. -- Detection for "skipping" nodes
  203. local found_path = carts:pathfinder(
  204. pos, self.old_pos, self.old_dir, ctrl, self.old_switch, self.railtype
  205. )
  206. if not found_path then
  207. -- No rail found: reset back to the expected position
  208. pos = vector.new(self.old_pos)
  209. update.pos = true
  210. end
  211. end
  212. local cart_dir = carts:velocity_to_dir(vel)
  213. local railparams
  214. -- dir: New moving direction of the cart
  215. -- switch_keys: Currently pressed L/R key, used to ignore the key on the next rail node
  216. local dir, switch_keys = carts:get_rail_direction(
  217. pos, cart_dir, ctrl, self.old_switch, self.railtype
  218. )
  219. local new_acc = {x=0, y=0, z=0}
  220. if vector.equals(dir, {x=0, y=0, z=0}) then
  221. vel = {x = 0, y = 0, z = 0}
  222. pos = vector_round(pos)
  223. update.pos = true
  224. update.vel = true
  225. else
  226. -- Direction change detected
  227. if not vector.equals(dir, self.old_dir) then
  228. vel = vector.multiply(dir, math.abs(vel.x + vel.z))
  229. update.vel = true
  230. if dir.y ~= self.old_dir.y then
  231. pos = vector_round(pos)
  232. update.pos = true
  233. end
  234. end
  235. -- Center on the rail
  236. if dir.z ~= 0 and math_floor(pos.x + 0.5) ~= pos.x then
  237. pos.x = math_floor(pos.x + 0.5)
  238. update.pos = true
  239. end
  240. if dir.x ~= 0 and math_floor(pos.z + 0.5) ~= pos.z then
  241. pos.z = math_floor(pos.z + 0.5)
  242. update.pos = true
  243. end
  244. -- Slow down or speed up..
  245. local acc = dir.y * -4.0
  246. -- Get rail for corrected position
  247. railparams = get_railparams(pos)
  248. -- no need to check for railparams == nil since we always make it exist.
  249. local speed_mod = railparams.acceleration
  250. if speed_mod and speed_mod ~= 0 then
  251. -- Try to make it similar to the original carts mod
  252. acc = acc + speed_mod
  253. else
  254. -- Handbrake
  255. if ctrl and ctrl.down then
  256. acc = acc - 3
  257. else
  258. acc = acc - 0.4
  259. end
  260. end
  261. new_acc = vector.multiply(dir, acc)
  262. end
  263. -- Limits
  264. local max_vel = carts.speed_max
  265. for _, v in pairs({"x","y","z"}) do
  266. if math.abs(vel[v]) > max_vel then
  267. vel[v] = carts:get_sign(vel[v]) * max_vel
  268. new_acc[v] = 0
  269. update.vel = true
  270. end
  271. end
  272. self.object:setacceleration(new_acc)
  273. self.old_pos = vector.new(pos)
  274. if not vector.equals(dir, {x=0, y=0, z=0}) then
  275. self.old_dir = vector.new(dir)
  276. end
  277. self.old_switch = switch_keys
  278. if self.punched then
  279. -- Collect dropped items
  280. for _, obj_ in pairs(minetest.get_objects_inside_radius(pos, 1)) do
  281. if not obj_:is_player() and
  282. obj_:get_luaentity() and
  283. not obj_:get_luaentity().physical_state and
  284. obj_:get_luaentity().name == "__builtin:item" then
  285. obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
  286. self.attached_items[#self.attached_items + 1] = obj_
  287. end
  288. end
  289. self.punched = false
  290. update.vel = true
  291. end
  292. railparams = railparams or get_railparams(pos)
  293. if not (update.vel or update.pos) then
  294. rail_on_step_event(railparams.on_step, self, dtime)
  295. return
  296. end
  297. local yaw = 0
  298. if self.old_dir.x < 0 then
  299. yaw = 0.5
  300. elseif self.old_dir.x > 0 then
  301. yaw = 1.5
  302. elseif self.old_dir.z < 0 then
  303. yaw = 1
  304. end
  305. self.object:setyaw(yaw * math.pi)
  306. local anim = {x=0, y=0}
  307. if dir.y == -1 then
  308. anim = {x=1, y=1}
  309. elseif dir.y == 1 then
  310. anim = {x=2, y=2}
  311. end
  312. self.object:set_animation(anim, 1, 0)
  313. self.object:setvelocity(vel)
  314. if update.pos then
  315. self.object:set_pos(pos)
  316. end
  317. -- call event handler
  318. rail_on_step_event(railparams.on_step, self, dtime)
  319. end
  320. function cart_entity:on_step(dtime)
  321. rail_on_step(self, dtime)
  322. rail_sound(self, dtime)
  323. end
  324. minetest.register_entity("carts:cart", cart_entity)
  325. minetest.register_craftitem("carts:cart", {
  326. description = "Mine Cart\n\nShift+punch to pick up.",
  327. inventory_image = minetest.inventorycube("carts_cart_top.png", "carts_cart_side.png", "carts_cart_side.png"),
  328. wield_image = "carts_cart_side.png",
  329. on_place = function(itemstack, placer, pointed_thing)
  330. local under = pointed_thing.under
  331. local node = minetest.get_node(under)
  332. local udef = minetest.reg_ns_nodes[node.name]
  333. if udef and udef.on_rightclick and
  334. not (placer and placer:get_player_control().sneak) then
  335. return udef.on_rightclick(under, node, placer, itemstack,
  336. pointed_thing) or itemstack
  337. end
  338. if not pointed_thing.type == "node" then
  339. return
  340. end
  341. if carts:is_rail(pointed_thing.under) then
  342. minetest.add_entity(pointed_thing.under, "carts:cart")
  343. elseif carts:is_rail(pointed_thing.above) then
  344. minetest.add_entity(pointed_thing.above, "carts:cart")
  345. else
  346. return
  347. end
  348. minetest.sound_play({name = "default_place_node_metal", gain = 0.5},
  349. {pos = pointed_thing.above})
  350. if not minetest.setting_getbool("creative_mode") then
  351. itemstack:take_item()
  352. end
  353. return itemstack
  354. end,
  355. })
  356. minetest.register_craft({
  357. output = "carts:cart",
  358. recipe = {
  359. {"default:steel_ingot", "xdecor:lever_off", "group:leather_padding"},
  360. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  361. {"carbon_steel:ingot", "techcrafts:electric_motor", "carbon_steel:ingot"},
  362. },
  363. })