From db96e86d5704f97e609b236b0fb5e0063f5c725d Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 7 Apr 2024 07:44:29 +0000 Subject: [PATCH] Fix rail detach crash, make tnt minecarts explode if they hit something hard (off rails) --- .../ENTITIES/mcl_minecarts/carts/with_tnt.lua | 20 ++++++++++++++----- mods/ENTITIES/mcl_minecarts/movement.lua | 12 ++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/carts/with_tnt.lua b/mods/ENTITIES/mcl_minecarts/carts/with_tnt.lua index fcbe03f8d..82bc1718e 100644 --- a/mods/ENTITIES/mcl_minecarts/carts/with_tnt.lua +++ b/mods/ENTITIES/mcl_minecarts/carts/with_tnt.lua @@ -3,7 +3,12 @@ local modpath = minetest.get_modpath(modname) local mod = mcl_minecarts local S = minetest.get_translator(modname) --- Minecart with TNT +local function detonate_tnt_minecart(self) + local pos = self.object:get_pos() + self.object:remove() + mcl_explosions.explode(pos, 6, { drop_chance = 1.0 }) +end + local function activate_tnt_minecart(self, timer) if self._boomtimer then return @@ -76,19 +81,24 @@ mod.register_minecart({ on_activate_by_rail = activate_tnt_minecart, creative = true, _mcl_minecarts_on_step = function(self, dtime) - -- Update TNT stuff + -- Impacts reduce the speed greatly. Use this to trigger explosions + local current_speed = vector.length(self.object:get_velocity()) + if current_speed < (self._old_speed or 0) - 6 then + detonate_tnt_minecart(self) + end + self._old_speed = current_speed + if self._boomtimer then -- Explode self._boomtimer = self._boomtimer - dtime - local pos = self.object:get_pos() if self._boomtimer <= 0 then - self.object:remove() - mcl_explosions.explode(pos, 6, { drop_chance = 1.0 }) + detonate_tnt_minecart(self) return else tnt.smoke_step(pos) end end + if self._blinktimer then self._blinktimer = self._blinktimer - dtime if self._blinktimer <= 0 then diff --git a/mods/ENTITIES/mcl_minecarts/movement.lua b/mods/ENTITIES/mcl_minecarts/movement.lua index ba2ea827f..fd2edb422 100644 --- a/mods/ENTITIES/mcl_minecarts/movement.lua +++ b/mods/ENTITIES/mcl_minecarts/movement.lua @@ -24,6 +24,10 @@ mod.detach_minecart = detach_minecart local function try_detach_minecart(self) local staticdata = self._staticdata + if not staticdata then return end + + -- Don't try to detach if alread detached + if not staticdata.connected_at then return end local node = minetest.get_node(staticdata.connected_at) if minetest.get_item_group(node.name, "rail") == 0 then @@ -461,7 +465,13 @@ local function do_detached_movement(self, dtime) friction.y = 0 local accel = vector.new(0,-9.81,0) -- gravity - accel = vector.add(accel, vector.multiply(friction,-0.9)) + + -- Don't apply friction in the air + local pos_rounded = vector.round(self.object:get_pos()) + if minetest.get_node(vector.offset(pos_rounded,0,-1,0)).name ~= "air" then + accel = vector.add(accel, vector.multiply(friction,-0.9)) + end + self.object:set_acceleration(accel) end