diff --git a/api.lua b/api.lua index 33a7407..b6b93d7 100644 --- a/api.lua +++ b/api.lua @@ -195,6 +195,7 @@ function vehicles.object_drive(entity, dtime, def) local dir = entity.driver:get_look_dir() local vec_backward = {x=-dir.x*speed/4,y=velo.y+1*-2,z=-dir.z*speed/4} local yaw = entity.driver:get_look_yaw() + local creative_mode = creative and creative.is_enabled_for and creative.is_enabled_for(entity.driver:get_player_name()) --dummy variables local vec_rise = {} @@ -508,7 +509,7 @@ function vehicles.object_drive(entity, dtime, def) --shoot weapons if ctrl.sneak and shoots and entity.loaded then if inv:contains_item("main", arrow.."_item") or infinite_arrow then - local remov = inv:remove_item("main", arrow.."_item") + if not creative_mode then inv:remove_item("main", arrow.."_item") end entity.loaded = false local obj = minetest.env:add_entity({x=pos.x+0+dir.x*2,y=pos.y+shoot_y+dir.y,z=pos.z+0+dir.z*2}, arrow) local vec = {x=dir.x*14,y=dir.y*14+shoot_angle,z=dir.z*14} @@ -532,7 +533,7 @@ function vehicles.object_drive(entity, dtime, def) if ctrl.aux1 and shoots2 and entity.loaded2 then if inv:contains_item("main", arrow2.."_item") or infinite_arrow2 then - local remov = inv:remove_item("main", arrow2.."_item") + if not creative_mode then inv:remove_item("main", arrow2.."_item") end entity.loaded2 = false local obj = minetest.env:add_entity({x=pos.x+0+dir.x*2,y=pos.y+shoot_y2+dir.y,z=pos.z+0+dir.z*2}, arrow2) local vec = {x=dir.x*20,y=dir.y*20+shoot_angle,z=dir.z*20} @@ -742,8 +743,7 @@ function vehicles.on_punch(self, puncher) end vehicles.explodinate(self, 5) end - if not self.driver then - return end + if not self.driver then return end local creative_mode = creative and creative.is_enabled_for and creative.is_enabled_for(self.driver:get_player_name()) if self.driver == puncher and (hp == self.hp_max-5 or hp == self.hp_max or creative_mode) then local name = self.object:get_luaentity().name diff --git a/init.lua b/init.lua index 2410acd..a6a6536 100644 --- a/init.lua +++ b/init.lua @@ -9,6 +9,80 @@ local step = 1.1 local enable_built_in = true if enable_built_in then +local function missile_bullet_hit_check(self, obj, pos) + local pos = self.object:getpos() + do + local return_v = {} + local if_return = false + for _, obj in ipairs(minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2)) do + function no_launcher_or_not_attched() + local b1, b2 = pcall(function() return obj ~= self.launcher:get_attach() end) + if not b1 then + return true -- no launcher + else + return b2 -- obj ~= attched object + end + end + if obj:get_luaentity() ~= nil and obj ~= self.object and obj ~= self.vehicle and obj ~= self.launcher and no_launcher_or_not_attched() and obj:get_luaentity().name ~= "__builtin:item" then + if_return = true + return_v[#return_v+1]=obj + end + end + if if_return then + return return_v + end + end + + for dx=-1,1 do + for dy=-1,1 do + for dz=-1,1 do + local p = {x=pos.x+dx, y=pos.y, z=pos.z+dz} + local t = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz} + local n = minetest.env:get_node(p) + if n.name ~= "air" and n.drawtype ~= "airlike" then + return {} + end + end + end + end + return false +end +local function missile_on_step_auxiliary(self, obj, pos) + minetest.after(10, function() + self.object:remove() + end) + local pos = self.object:getpos() + local vec = self.object:getvelocity() + minetest.add_particlespawner({ + amount = 1, + time = 0.5, + minpos = {x=pos.x-0.2, y=pos.y, z=pos.z-0.2}, + maxpos = {x=pos.x+0.2, y=pos.y, z=pos.z+0.2}, + minvel = {x=-vec.x/2, y=-vec.y/2, z=-vec.z/2}, + maxvel = {x=-vec.x, y=-vec.y, z=-vec.z}, + minacc = {x=0, y=-1, z=0}, + maxacc = {x=0, y=-1, z=0}, + minexptime = 0.2, + maxexptime = 0.6, + minsize = 3, + maxsize = 4, + collisiondetection = false, + texture = "vehicles_smoke.png", + }) + local objs = missile_bullet_hit_check(self, obj, pos) + if objs then + for _, obj in ipairs(objs) do + local puncher = self.object + if self.launcher then puncher = self.launcher end + obj:punch(puncher, 1.0, { + full_punch_interval=1.0, + damage_groups={fleshy=12}, + }, nil) + end + tnt.boom(self.object:getpos(), {damage_radius=5,radius=5,ignore_protection=false}) + self.object:remove() + end +end minetest.register_entity("vehicles:missile", { visual = "mesh", mesh = "missile.b3d", @@ -21,9 +95,6 @@ minetest.register_entity("vehicles:missile", { clicker:set_attach(self.object, "", {x=0,y=0,z=0}, {x=0,y=1,z=0}) end, on_step = function(self, obj, pos) - minetest.after(10, function() - self.object:remove() - end) local player = self.launcher if player == nil or player:get_player_name() == "" then self.object:remove() @@ -34,54 +105,7 @@ minetest.register_entity("vehicles:missile", { local yaw = player:get_look_yaw() self.object:setyaw(yaw+math.pi/2) self.object:setvelocity(vec) - local pos = self.object:getpos() - local vec = self.object:getvelocity() - minetest.add_particlespawner({ - amount = 1, - time = 0.5, - minpos = {x=pos.x-0.2, y=pos.y, z=pos.z-0.2}, - maxpos = {x=pos.x+0.2, y=pos.y, z=pos.z+0.2}, - minvel = {x=-vec.x/2, y=-vec.y/2, z=-vec.z/2}, - maxvel = {x=-vec.x, y=-vec.y, z=-vec.z}, - minacc = {x=0, y=-1, z=0}, - maxacc = {x=0, y=-1, z=0}, - minexptime = 0.2, - maxexptime = 0.6, - minsize = 3, - maxsize = 4, - collisiondetection = false, - texture = "vehicles_smoke.png", - }) - local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2) - for k, obj in pairs(objs) do - if obj:get_luaentity() ~= nil then - if obj:get_luaentity().name ~= "vehicles:missile" and obj ~= self.vehicle and obj ~= self.launcher and obj:get_luaentity().name ~= "__builtin:item" then - obj:punch(self.object, 1.0, { - full_punch_interval=1.0, - damage_groups={fleshy=12}, - }, nil) - local pos = self.object:getpos() - tnt.boom(pos, {damage_radius=5,radius=5,ignore_protection=false}) - self.object:remove() - end - end - end - - for dx=-1,1 do - for dy=-1,1 do - for dz=-1,1 do - local p = {x=pos.x+dx, y=pos.y, z=pos.z+dz} - local t = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz} - local n = minetest.env:get_node(p).name - if n ~= "vehicles:missile" and n ~= "vehicles:jet" and n ~= "air" then - local pos = self.object:getpos() - tnt.boom(pos, {damage_radius=5,radius=5,ignore_protection=false}) - self.object:remove() - return - end - end - end - end + missile_on_step_auxiliary(self, obj, pos) end, }) @@ -106,9 +130,6 @@ minetest.register_entity("vehicles:missile_2", { damage = 2, collisionbox = {0, 0, 0, 0, 0, 0}, on_step = function(self, obj, pos) - minetest.after(10, function() - self.object:remove() - end) local velo = self.object:getvelocity() if velo.y <= 1.2 and velo.y >= -1.2 then self.object:set_animation({x=1, y=1}, 5, 0) @@ -117,67 +138,7 @@ minetest.register_entity("vehicles:missile_2", { elseif velo.y >= 1.2 then self.object:set_animation({x=2, y=2}, 5, 0) end - local pos = self.object:getpos() - minetest.add_particlespawner({ - amount = 2, - time = 0.5, - minpos = {x=pos.x-0.2, y=pos.y, z=pos.z-0.2}, - maxpos = {x=pos.x+0.2, y=pos.y, z=pos.z+0.2}, - minvel = {x=-velo.x/2, y=-velo.y/2, z=-velo.z/2}, - maxvel = {x=-velo.x, y=-velo.y, z=-velo.z}, - minacc = {x=0, y=-1, z=0}, - maxacc = {x=0, y=-1, z=0}, - minexptime = 0.2, - maxexptime = 0.6, - minsize = 3, - maxsize = 4, - collisiondetection = false, - texture = "vehicles_smoke.png", - }) - local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2) - for k, obj in pairs(objs) do - if obj:get_luaentity() ~= nil then - if obj:get_luaentity().name ~= "vehicles:missile_2" and obj ~= self.vehicle and obj:get_luaentity().name ~= "__builtin:item" then - obj:punch(self.launcher, 1.0, { - full_punch_interval=1.0, - damage_groups={fleshy=12}, - }, nil) - self.object:remove() - end - end - end - - for dx=-1,1 do - for dy=-1,1 do - for dz=-1,1 do - local p = {x=pos.x+dx, y=pos.y, z=pos.z+dz} - local t = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz} - local n = minetest.env:get_node(p).name - if n ~= "vehicles:missile_2" and n ~= "vehicles:tank" and n ~= "vehicles:jet" and n ~= "air" then - local pos = self.object:getpos() - minetest.add_particlespawner({ - amount = 30, - time = 0.5, - minpos = {x=pos.x-0.5, y=pos.y-0.5, z=pos.z-0.5}, - maxpos = {x=pos.x+0.5, y=pos.y+0.5, z=pos.z+0.5}, - minvel = {x=-1, y=-1, z=-1}, - maxvel = {x=1, y=1, z=1}, - minacc = {x=0, y=0.2, z=0}, - maxacc = {x=0, y=0.6, z=0}, - minexptime = 0.5, - maxexptime = 1, - minsize = 10, - maxsize = 20, - collisiondetection = false, - texture = "vehicles_explosion.png" - }) - tnt.boom(pos, {damage_radius=5,radius=5,ignore_protection=false}) - self.object:remove() - return - end - end - end - end + missile_on_step_auxiliary(self, obj, pos) end, }) @@ -237,32 +198,15 @@ minetest.register_entity("vehicles:bullet", { minetest.after(10, function() self.object:remove() end) - local pos = self.object:getpos() - local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2) - for k, obj in pairs(objs) do - if obj:get_luaentity() ~= nil then - if obj:get_luaentity().name ~= "vehicles:bullet" and obj ~= self.vehicle and obj:get_luaentity().name ~= "__builtin:item" then - obj:punch(self.launcher, 1.0, { - full_punch_interval=1.0, - damage_groups={fleshy=5}, - }, nil) - self.object:remove() - end - end - end - - for dx=-1,1 do - for dy=-1,1 do - for dz=-1,1 do - local p = {x=pos.x+dx, y=pos.y, z=pos.z+dz} - local t = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz} - local n = minetest.env:get_node(p).name - if n ~= "vehicles:bullet" and n ~= "air" then - self.object:remove() - return - end - end + local objs = missile_bullet_hit_check(self, obj, pos) + if objs then + for _, obj in ipairs(objs) do + obj:punch(self.launcher, 1.0, { + full_punch_interval=1.0, + damage_groups={fleshy=5}, + }, nil) end + self.object:remove() end end, }) @@ -1883,7 +1827,8 @@ minetest.register_tool("vehicles:rc", { local pname = placer:get_player_name() local inv = minetest.get_inventory({type="player", name=pname}) if inv:contains_item("main", "vehicles:missile_2_item") then - local remov = inv:remove_item("main", "vehicles:missile_2_item") + local creative_mode = creative and creative.is_enabled_for and creative.is_enabled_for(placer:get_player_name()) + if not creative_mode then inv:remove_item("main", "vehicles:missile_2_item") end local obj = minetest.env:add_entity({x=playerpos.x+0+dir.x,y=playerpos.y+1+dir.y,z=playerpos.z+0+dir.z}, "vehicles:missile") local object = obj:get_luaentity() object.launcher = placer