rc, bullet, missile, ...: fix bugs, fix creative mods, cleanup codes

This commit is contained in:
zaoqi 2020-01-05 20:25:23 +08:00
parent 6e2ae2c126
commit 11d0f6f724
2 changed files with 82 additions and 145 deletions

View File

@ -195,6 +195,7 @@ function vehicles.object_drive(entity, dtime, def)
local dir = entity.driver:get_look_dir() 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 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 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 --dummy variables
local vec_rise = {} local vec_rise = {}
@ -508,7 +509,7 @@ function vehicles.object_drive(entity, dtime, def)
--shoot weapons --shoot weapons
if ctrl.sneak and shoots and entity.loaded then if ctrl.sneak and shoots and entity.loaded then
if inv:contains_item("main", arrow.."_item") or infinite_arrow 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 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 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} 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 ctrl.aux1 and shoots2 and entity.loaded2 then
if inv:contains_item("main", arrow2.."_item") or infinite_arrow2 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 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 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} 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 end
vehicles.explodinate(self, 5) vehicles.explodinate(self, 5)
end end
if not self.driver then if not self.driver then return end
return end
local creative_mode = creative and creative.is_enabled_for and creative.is_enabled_for(self.driver:get_player_name()) 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 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 local name = self.object:get_luaentity().name

195
init.lua
View File

@ -9,31 +9,40 @@ local step = 1.1
local enable_built_in = true local enable_built_in = true
if enable_built_in then if enable_built_in then
minetest.register_entity("vehicles:missile", { local function missile_bullet_hit_check(self, obj, pos)
visual = "mesh", local pos = self.object:getpos()
mesh = "missile.b3d", do
textures = {"vehicles_missile.png"}, local return_v = {}
velocity = 15, local if_return = false
acceleration = -5, for _, obj in ipairs(minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2)) do
damage = 2, if obj:get_luaentity() ~= nil and obj ~= self.object and obj ~= self.vehicle and obj ~= self.launcher and obj ~= self.launcher:get_attach() and obj:get_luaentity().name ~= "__builtin:item" then
collisionbox = {-1, -0.5, -1, 1, 0.5, 1}, if_return = true
on_rightclick = function(self, clicker) return_v[#return_v+1]=obj
clicker:set_attach(self.object, "", {x=0,y=0,z=0}, {x=0,y=1,z=0}) end
end, end
on_step = function(self, obj, pos) 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() minetest.after(10, function()
self.object:remove() self.object:remove()
end) end)
local player = self.launcher
if player == nil or player:get_player_name() == "" then
self.object:remove()
return
end
local dir = player:get_look_dir()
local vec = {x=dir.x*16,y=dir.y*16,z=dir.z*16}
local yaw = player:get_look_yaw()
self.object:setyaw(yaw+math.pi/2)
self.object:setvelocity(vec)
local pos = self.object:getpos() local pos = self.object:getpos()
local vec = self.object:getvelocity() local vec = self.object:getvelocity()
minetest.add_particlespawner({ minetest.add_particlespawner({
@ -52,36 +61,43 @@ minetest.register_entity("vehicles:missile", {
collisiondetection = false, collisiondetection = false,
texture = "vehicles_smoke.png", texture = "vehicles_smoke.png",
}) })
local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2) local objs = missile_bullet_hit_check(self, obj, pos)
for k, obj in pairs(objs) do if objs then
if obj:get_luaentity() ~= nil then for _, obj in ipairs(objs) do
if obj:get_luaentity().name ~= "vehicles:missile" and obj ~= self.vehicle and obj ~= self.launcher and obj:get_luaentity().name ~= "__builtin:item" then local puncher = self.object
obj:punch(self.object, 1.0, { if self.launcher then puncher = self.launcher end
obj:punch(puncher, 1.0, {
full_punch_interval=1.0, full_punch_interval=1.0,
damage_groups={fleshy=12}, damage_groups={fleshy=12},
}, nil) }, nil)
local pos = self.object:getpos() end
tnt.boom(pos, {damage_radius=5,radius=5,ignore_protection=false}) tnt.boom(self.object:getpos(), {damage_radius=5,radius=5,ignore_protection=false})
self.object:remove() self.object:remove()
end end
end end
end minetest.register_entity("vehicles:missile", {
visual = "mesh",
for dx=-1,1 do mesh = "missile.b3d",
for dy=-1,1 do textures = {"vehicles_missile.png"},
for dz=-1,1 do velocity = 15,
local p = {x=pos.x+dx, y=pos.y, z=pos.z+dz} acceleration = -5,
local t = {x=pos.x+dx, y=pos.y+dy, z=pos.z+dz} damage = 2,
local n = minetest.env:get_node(p).name collisionbox = {-1, -0.5, -1, 1, 0.5, 1},
if n ~= "vehicles:missile" and n ~= "vehicles:jet" and n ~= "air" then on_rightclick = function(self, clicker)
local pos = self.object:getpos() clicker:set_attach(self.object, "", {x=0,y=0,z=0}, {x=0,y=1,z=0})
tnt.boom(pos, {damage_radius=5,radius=5,ignore_protection=false}) end,
on_step = function(self, obj, pos)
local player = self.launcher
if player == nil or player:get_player_name() == "" then
self.object:remove() self.object:remove()
return return
end end
end local dir = player:get_look_dir()
end local vec = {x=dir.x*16,y=dir.y*16,z=dir.z*16}
end local yaw = player:get_look_yaw()
self.object:setyaw(yaw+math.pi/2)
self.object:setvelocity(vec)
missile_on_step_auxiliary(self, obj, pos)
end, end,
}) })
@ -106,9 +122,6 @@ minetest.register_entity("vehicles:missile_2", {
damage = 2, damage = 2,
collisionbox = {0, 0, 0, 0, 0, 0}, collisionbox = {0, 0, 0, 0, 0, 0},
on_step = function(self, obj, pos) on_step = function(self, obj, pos)
minetest.after(10, function()
self.object:remove()
end)
local velo = self.object:getvelocity() local velo = self.object:getvelocity()
if velo.y <= 1.2 and velo.y >= -1.2 then if velo.y <= 1.2 and velo.y >= -1.2 then
self.object:set_animation({x=1, y=1}, 5, 0) self.object:set_animation({x=1, y=1}, 5, 0)
@ -117,67 +130,7 @@ minetest.register_entity("vehicles:missile_2", {
elseif velo.y >= 1.2 then elseif velo.y >= 1.2 then
self.object:set_animation({x=2, y=2}, 5, 0) self.object:set_animation({x=2, y=2}, 5, 0)
end end
local pos = self.object:getpos() missile_on_step_auxiliary(self, obj, pos)
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
end, end,
}) })
@ -237,33 +190,16 @@ minetest.register_entity("vehicles:bullet", {
minetest.after(10, function() minetest.after(10, function()
self.object:remove() self.object:remove()
end) end)
local pos = self.object:getpos() local objs = missile_bullet_hit_check(self, obj, pos)
local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2) if objs then
for k, obj in pairs(objs) do for _, obj in ipairs(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, { obj:punch(self.launcher, 1.0, {
full_punch_interval=1.0, full_punch_interval=1.0,
damage_groups={fleshy=5}, damage_groups={fleshy=5},
}, nil) }, nil)
end
self.object:remove() self.object:remove()
end 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
end
end
end, end,
}) })
@ -1883,7 +1819,8 @@ minetest.register_tool("vehicles:rc", {
local pname = placer:get_player_name() local pname = placer:get_player_name()
local inv = minetest.get_inventory({type="player", name=pname}) local inv = minetest.get_inventory({type="player", name=pname})
if inv:contains_item("main", "vehicles:missile_2_item") then 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 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() local object = obj:get_luaentity()
object.launcher = placer object.launcher = placer