Expose shooter functions to global namespace and some tweaks

This commit is contained in:
stujones11 2014-10-24 20:22:51 +01:00
parent fad87d1ed3
commit de408aee18

View file

@ -7,7 +7,8 @@ shooter = {
reload_time = 0, reload_time = 0,
} }
SHOOTER_ENABLE_BLASTING = true SHOOTER_ENABLE_BLASTING = false
SHOOTER_ENABLE_CROSSBOW = true
SHOOTER_ENABLE_GUNS = true SHOOTER_ENABLE_GUNS = true
SHOOTER_ENABLE_FLARES = true SHOOTER_ENABLE_FLARES = true
SHOOTER_ENABLE_HOOK = true SHOOTER_ENABLE_HOOK = true
@ -64,7 +65,7 @@ local function get_particle_pos(p, v, d)
return vector.add(p, vector.multiply(v, {x=d, y=d, z=d})) return vector.add(p, vector.multiply(v, {x=d, y=d, z=d}))
end end
local function spawn_particles(pos, texture) function shooter:spawn_particles(pos, texture)
if SHOOTER_ENABLE_PARTICLE_FX == true then if SHOOTER_ENABLE_PARTICLE_FX == true then
if type(texture) ~= "string" then if type(texture) ~= "string" then
texture = SHOOTER_EXPLOSION_TEXTURE texture = SHOOTER_EXPLOSION_TEXTURE
@ -79,26 +80,20 @@ local function spawn_particles(pos, texture)
end end
end end
local function is_valid_object(object) function shooter:play_node_sound(node, pos)
if object then local item = minetest.registered_items[node.name]
if object:is_player() == true then if item then
return true if item.sounds then
end local spec = item.sounds.dug
if SHOOTER_ALLOW_ENTITIES == true then if spec then
local luaentity = object:get_luaentity() spec.pos = pos
if luaentity then minetest.sound_play(spec.name, spec)
if luaentity.name then
if allowed_entities[luaentity.name] then
return true
end end
end end
end end
end end
end
return false
end
local function punch_node(pos, def) function shooter:punch_node(pos, def)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if not node then if not node then
return return
@ -117,16 +112,11 @@ local function punch_node(pos, def)
local level = item.groups[k] or 0 local level = item.groups[k] or 0
if level >= v then if level >= v then
minetest.remove_node(pos) minetest.remove_node(pos)
local sounds = item.sounds shooter:play_node_sound(node, pos)
if item.sounds then
local spec = item.sounds.dug
if spec then
spec.pos = pos
minetest.sound_play(spec.name, spec)
end
end
if item.tiles then if item.tiles then
return item.tiles[1] if item.tiles[1] then
shooter:spawn_particles(pos, item.tiles[1])
end
end end
break break
end end
@ -134,6 +124,41 @@ local function punch_node(pos, def)
end end
end end
function shooter:is_valid_object(object)
if object then
if object:is_player() == true then
return SHOOTER_ALLOW_PLAYERS
end
if SHOOTER_ALLOW_ENTITIES == true then
local luaentity = object:get_luaentity()
if luaentity then
if luaentity.name then
if allowed_entities[luaentity.name] then
return true
end
end
end
end
end
end
function shooter:get_intersect_pos(ray, plane, collisionbox)
local v = vector.subtract(ray.pos, plane.pos)
local r1 = get_dot_product(v, plane.normal)
local r2 = get_dot_product(ray.dir, plane.normal)
if r2 ~= 0 then
local t = -(r1 / r2)
local td = vector.multiply(ray.dir, {x=t, y=t, z=t})
local pt = vector.add(ray.pos, td)
local pd = vector.subtract(pt, plane.pos)
if math.abs(pd.x) < collisionbox[4] and
math.abs(pd.y) < collisionbox[5] and
math.abs(pd.z) < collisionbox[6] then
return pt
end
end
end
function shooter:process_round(round) function shooter:process_round(round)
local target = {object=nil, distance=10000} local target = {object=nil, distance=10000}
local p1 = round.pos local p1 = round.pos
@ -142,42 +167,29 @@ function shooter:process_round(round)
local p2 = vector.add(ref.pos, ref.offset) local p2 = vector.add(ref.pos, ref.offset)
if p1 and p2 and ref.name ~= round.name then if p1 and p2 and ref.name ~= round.name then
local d = vector.distance(p1, p2) local d = vector.distance(p1, p2)
if d < round.def.step then if d < round.def.step and d < target.distance then
local n = vector.multiply(v1, {x=-1, y=0, z=-1}) local ray = {pos=p1, dir=v1}
local v2 = vector.subtract(p1, p2) local plane = {pos=p2, normal={x=-1, y=0, z=-1}}
local r1 = get_dot_product(n, v2) local pos = shooter:get_intersect_pos(ray, plane, ref.collisionbox)
local r2 = get_dot_product(n, v1) if pos then
if r2 ~= 0 then
local t = -(r1 / r2)
local td = vector.multiply(v1, {x=t, y=t, z=t})
local pt = vector.add(p1, td)
local pd = vector.subtract(pt, p2)
if math.abs(pd.x) < ref.collisionbox[4] and
math.abs(pd.y) < ref.collisionbox[5] and
math.abs(pd.z) < ref.collisionbox[6] then
target.object = ref.object target.object = ref.object
target.pos = pt target.pos = pos
target.distance = d target.distance = d
end end
end end
end end
end end
end
if target.object and target.pos then if target.object and target.pos then
local success, pos = minetest.line_of_sight(p1, target.pos, 1) local success, pos = minetest.line_of_sight(p1, target.pos, 1)
if success then if success then
local user = minetest.get_player_by_name(round.name) local user = minetest.get_player_by_name(round.name)
if user then if user then
target.object:punch(user, nil, round.def.tool_caps, v1) target.object:punch(user, nil, round.def.tool_caps, v1)
spawn_particles(target.pos, SHOOTER_EXPLOSION_TEXTURE) shooter:spawn_particles(target.pos, SHOOTER_EXPLOSION_TEXTURE)
end end
return 1 return 1
elseif pos and SHOOTER_ALLOW_NODES == true then elseif pos and SHOOTER_ALLOW_NODES == true then
local texture = punch_node(pos, round.def) shooter:punch_node(pos, round.def)
if texture then
local pp = get_particle_pos(p1, v1, vector.distance(p1, pos))
spawn_particles(pp, texture)
end
return 1 return 1
end end
elseif SHOOTER_ALLOW_NODES == true then elseif SHOOTER_ALLOW_NODES == true then
@ -185,11 +197,7 @@ function shooter:process_round(round)
local p2 = vector.add(p1, vector.multiply(v1, {x=d, y=d, z=d})) local p2 = vector.add(p1, vector.multiply(v1, {x=d, y=d, z=d}))
local success, pos = minetest.line_of_sight(p1, p2, 1) local success, pos = minetest.line_of_sight(p1, p2, 1)
if pos then if pos then
local texture = punch_node(pos, round.def) shooter:punch_node(pos, round.def)
if texture then
local pp = get_particle_pos(p1, v1, vector.distance(p1, pos))
spawn_particles(pp, texture)
end
return 1 return 1
end end
end end
@ -207,7 +215,7 @@ function shooter:register_weapon(name, def)
def.spec.name = user:get_player_name() def.spec.name = user:get_player_name()
if shots > 1 then if shots > 1 then
local step = def.spec.tool_caps.full_punch_interval local step = def.spec.tool_caps.full_punch_interval
for i = 0, step * (shots), step do for i = 0, step * shots, step do
minetest.after(i, function() minetest.after(i, function()
shooter:fire_weapon(user, pointed_thing, def.spec) shooter:fire_weapon(user, pointed_thing, def.spec)
end) end)
@ -254,20 +262,15 @@ function shooter:fire_weapon(user, pointed_thing, def)
) )
if pointed_thing.type == "node" and SHOOTER_ALLOW_NODES == true then if pointed_thing.type == "node" and SHOOTER_ALLOW_NODES == true then
local pos = minetest.get_pointed_thing_position(pointed_thing, false) local pos = minetest.get_pointed_thing_position(pointed_thing, false)
local texture = punch_node(pos, def) shooter:punch_node(pos, def)
if texture then
local pp = get_particle_pos(p1, v1, vector.distance(p1, pos))
pp.y = pp.y + 1.75
spawn_particles(pp, texture)
end
elseif pointed_thing.type == "object" then elseif pointed_thing.type == "object" then
local object = pointed_thing.ref local object = pointed_thing.ref
if is_valid_object(object) == true then if shooter:is_valid_object(object) == true then
object:punch(user, nil, def.tool_caps, v1) object:punch(user, nil, def.tool_caps, v1)
local p2 = object:getpos() local p2 = object:getpos()
local pp = get_particle_pos(p1, v1, vector.distance(p1, p2)) local pp = get_particle_pos(p1, v1, vector.distance(p1, p2))
pp.y = pp.y + 1.75 pp.y = pp.y + 1.75
spawn_particles(pp, SHOOTER_EXPLOSION_TEXTURE) shooter:spawn_particles(pp, SHOOTER_EXPLOSION_TEXTURE)
end end
else else
shooter:update_objects() shooter:update_objects()
@ -355,7 +358,7 @@ function shooter:blast(pos, radius, fleshy, distance, user)
local p1 = vector.subtract(pos, radius) local p1 = vector.subtract(pos, radius)
local p2 = vector.add(pos, radius) local p2 = vector.add(pos, radius)
minetest.sound_play("tnt_explode", {pos=pos, gain=1}) minetest.sound_play("tnt_explode", {pos=pos, gain=1})
if SHOOTER_ALLOW_NODES == true and SHOOTER_ENABLE_BLASTING then if SHOOTER_ALLOW_NODES and SHOOTER_ENABLE_BLASTING then
if SHOOTER_ENABLE_PROTECTION then if SHOOTER_ENABLE_PROTECTION then
if not minetest.is_protected(pos, name) then if not minetest.is_protected(pos, name) then
minetest.set_node(pos, {name="tnt:boom"}) minetest.set_node(pos, {name="tnt:boom"})