From d5f01e88c4d2431560825c46237d0049d9c40725 Mon Sep 17 00:00:00 2001 From: anarquimico Date: Sat, 4 Mar 2023 17:14:59 -0300 Subject: [PATCH 1/5] Make mobs ride minecarts --- mods/ENTITIES/mcl_minecarts/init.lua | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index 6a837207f..a951ae4b2 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -66,6 +66,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o on_rightclick = on_rightclick, _driver = nil, -- player who sits in and controls the minecart (only for minecart!) + _passenger = nil, -- for mobs _punched = false, -- used to re-send _velocity and position _velocity = {x=0, y=0, z=0}, -- only used on punch _start_pos = nil, -- Used to calculate distance for “On A Rail” achievement @@ -212,6 +213,34 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o end end + -- Grab mob + if not self._passenger then + if self.name == "mcl_minecarts:minecart" then + local mobsnear = minetest.get_objects_inside_radius(self.object:get_pos(), 1.3) + for n=1, #mobsnear do + local mob = mobsnear[n] + if mob then + local entity = mob:get_luaentity() + if entity and entity.is_mob then + self._passenger = entity.name + mob:set_attach(self.object, "", {x=0, y=-1.75, z=0}, {x=0, y=0, z=0}) + break + end + end + end + end + -- Make room in the minecart after the mob dies + elseif self._passenger then + local mobinside = minetest.get_objects_inside_radius(self.object:get_pos(), 1) + for n=1, #mobinside do + local mob = mobinside[n]:get_luaentity() + if not mob or mob.is_mob == false then + self._passenger = nil + break + end + end + end + -- Drop minecart if it isn't on a rail anymore if self._last_float_check >= mcl_minecarts.check_float_time then pos = self.object:get_pos() From f817fe7f729c9a0b4eb67967531af0c5e79eed88 Mon Sep 17 00:00:00 2001 From: anarquimico Date: Sun, 19 Mar 2023 19:15:05 -0300 Subject: [PATCH 2/5] Fix performance issues and better check for mobs death --- mods/ENTITIES/mcl_minecarts/init.lua | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index a951ae4b2..8d4a280a1 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -214,7 +214,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o end -- Grab mob - if not self._passenger then + if math.random(1,20) > 15 and not self._passenger then if self.name == "mcl_minecarts:minecart" then local mobsnear = minetest.get_objects_inside_radius(self.object:get_pos(), 1.3) for n=1, #mobsnear do @@ -222,7 +222,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o if mob then local entity = mob:get_luaentity() if entity and entity.is_mob then - self._passenger = entity.name + self._passenger = entity mob:set_attach(self.object, "", {x=0, y=-1.75, z=0}, {x=0, y=0, z=0}) break end @@ -231,13 +231,9 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o end -- Make room in the minecart after the mob dies elseif self._passenger then - local mobinside = minetest.get_objects_inside_radius(self.object:get_pos(), 1) - for n=1, #mobinside do - local mob = mobinside[n]:get_luaentity() - if not mob or mob.is_mob == false then - self._passenger = nil - break - end + dead = self._passenger:check_for_death() + if dead == true then + self._passenger = nil end end From 3eab946889935b30171ce97ed19b9db823217e36 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 20 Mar 2023 13:16:26 +0000 Subject: [PATCH 3/5] Make death check less frequent. Load passenger in minecart at shutdown. --- mods/ENTITIES/mcl_minecarts/init.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index 8d4a280a1..ee3dce438 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -87,6 +87,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o local data = minetest.deserialize(staticdata) if type(data) == "table" then self._railtype = data._railtype + self._passenger = data._passenger end self.object:set_armor_groups({immortal=1}) @@ -231,9 +232,11 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o end -- Make room in the minecart after the mob dies elseif self._passenger then - dead = self._passenger:check_for_death() - if dead == true then - self._passenger = nil + if math.random(1,20) > 1 then + local dead = self._passenger:check_for_death() + if dead == true then + self._passenger = nil + end end end From bcd7f38fa7900bd033aafe071bb6473e18510ec4 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 20 Mar 2023 13:29:25 +0000 Subject: [PATCH 4/5] Use new style vectors --- mods/ENTITIES/mcl_minecarts/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index ee3dce438..9c0199a4c 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -179,6 +179,8 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o cart.on_activate_by_rail = on_activate_by_rail + local passenger_attach_position = vector.new(0, -1.75, 0) + function cart:on_step(dtime) local ctrl, player = nil, nil if self._driver then @@ -224,7 +226,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o local entity = mob:get_luaentity() if entity and entity.is_mob then self._passenger = entity - mob:set_attach(self.object, "", {x=0, y=-1.75, z=0}, {x=0, y=0, z=0}) + mob:set_attach(self.object, "", passenger_attach_position, vector.zero()) break end end From ecf72db6844e3a035f72396373525dd2ee740b6e Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 20 Mar 2023 13:36:23 +0000 Subject: [PATCH 5/5] Fix ooops --- mods/ENTITIES/mcl_minecarts/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index 9c0199a4c..aa9ef7edc 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -234,7 +234,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o end -- Make room in the minecart after the mob dies elseif self._passenger then - if math.random(1,20) > 1 then + if math.random(1,20) == 1 then local dead = self._passenger:check_for_death() if dead == true then self._passenger = nil