Fix api inconsistencies

This commit is contained in:
stujones11 2019-04-02 18:44:25 +01:00
parent 886e5803e0
commit 6b4ea389ce
2 changed files with 34 additions and 16 deletions

View file

@ -66,20 +66,23 @@ local sqrt = math.sqrt
local phi = (math.sqrt(5) + 1) / 2 -- Golden ratio local phi = (math.sqrt(5) + 1) / 2 -- Golden ratio
shooter.register_weapon = function(name, def) shooter.register_weapon = function(name, def)
-- Backwards compatibility
if not def.spec.sounds then
def.spec.sounds = def.sounds or {}
end
if not def.spec.sounds.shot and def.spec.sound then
def.spec.sounds.shot = def.spec.sound
end
-- Fix definition table -- Fix definition table
def.sounds = def.sounds or {} def.spec.reload_item = def.reload_item or "shooter:ammo"
def.sounds.reload = def.sounds.reload or "shooter_reload"
def.sounds.fail_shot = def.sounds.fail_shot or "shooter_click"
def.reload_item = def.reload_item or "shooter:ammo"
def.spec.tool_caps.full_punch_interval = math.max(server_step, def.spec.tool_caps.full_punch_interval = math.max(server_step,
def.spec.tool_caps.full_punch_interval) def.spec.tool_caps.full_punch_interval)
def.spec.wear = math.ceil(65535 / def.spec.rounds) def.spec.wear = math.ceil(65535 / def.spec.rounds)
def.spec.unloaded_item = name
def.unloaded_item = def.unloaded_item or { def.unloaded_item = def.unloaded_item or {
name = name,
description = def.description.." (unloaded)", description = def.description.." (unloaded)",
inventory_image = def.inventory_image, inventory_image = def.inventory_image,
} }
def.unloaded_item.name = name
shooter.registered_weapons[name] = table.copy(def) shooter.registered_weapons[name] = table.copy(def)
-- Register loaded item tool -- Register loaded item tool
minetest.register_tool(name.."_loaded", { minetest.register_tool(name.."_loaded", {
@ -101,6 +104,7 @@ shooter.register_weapon = function(name, def)
end end
return itemstack return itemstack
end, end,
unloaded_item = def.unloaded_item,
on_hit = def.on_hit, on_hit = def.on_hit,
groups = {not_in_creative_inventory=1}, groups = {not_in_creative_inventory=1},
}) })
@ -114,11 +118,13 @@ shooter.register_weapon = function(name, def)
if inv then if inv then
local stack = def.reload_item local stack = def.reload_item
if inv:contains_item("main", stack) then if inv:contains_item("main", stack) then
minetest.sound_play(def.sounds.reload, {object=user}) local sound = def.spec.sounds.reload or "shooter_reload"
minetest.sound_play(sound, {object=user})
inv:remove_item("main", stack) inv:remove_item("main", stack)
itemstack:replace(name.."_loaded 1 1") itemstack:replace(name.."_loaded 1 1")
else else
minetest.sound_play(def.sounds.fail_shot, {object=user}) local sound = def.spec.sounds.fail_shot or "shooter_click"
minetest.sound_play(sound, {object=user})
end end
end end
return itemstack return itemstack
@ -373,8 +379,9 @@ local function fire_weapon(player, itemstack, spec, extended)
spec.origin = v3d.add(pos, dir) spec.origin = v3d.add(pos, dir)
local interval = spec.tool_caps.full_punch_interval local interval = spec.tool_caps.full_punch_interval
shots[spec.user] = minetest.get_us_time() / 1000000 + interval shots[spec.user] = minetest.get_us_time() / 1000000 + interval
minetest.sound_play(spec.sound, {object=player}) local sound = spec.sounds.shot or "shooter_pistol"
local speed = spec.step / shooter.config.rounds_update_time minetest.sound_play(sound, {object=player})
local speed = spec.step / (config.rounds_update_time * 2)
local time = spec.range / speed local time = spec.range / speed
local directions = get_directions(dir, spec) local directions = get_directions(dir, spec)
for _, d in pairs(directions) do for _, d in pairs(directions) do
@ -398,7 +405,10 @@ local function fire_weapon(player, itemstack, spec, extended)
if extended then if extended then
itemstack:add_wear(spec.wear) itemstack:add_wear(spec.wear)
if itemstack:get_count() == 0 then if itemstack:get_count() == 0 then
itemstack = spec.unloaded_item local def = shooter.registered_weapons[spec.name] or {}
if def.unloaded_item then
itemstack = def.unloaded_item.name or ""
end
player:set_wielded_item(itemstack) player:set_wielded_item(itemstack)
return return
end end

View file

@ -26,7 +26,9 @@ shooter.register_weapon("shooter_guns:pistol", {
step = 20, step = 20,
tool_caps = {full_punch_interval=0.5, damage_groups={fleshy=2}}, tool_caps = {full_punch_interval=0.5, damage_groups={fleshy=2}},
groups = {snappy=3, fleshy=3, oddly_breakable_by_hand=3}, groups = {snappy=3, fleshy=3, oddly_breakable_by_hand=3},
sound = "shooter_pistol", sounds = {
shot = "shooter_pistol",
},
bullet_image = "shooter_cap.png", bullet_image = "shooter_cap.png",
particles = { particles = {
amount = 8, amount = 8,
@ -45,7 +47,9 @@ shooter.register_weapon("shooter_guns:rifle", {
step = 30, step = 30,
tool_caps = {full_punch_interval=1.0, damage_groups={fleshy=3}}, tool_caps = {full_punch_interval=1.0, damage_groups={fleshy=3}},
groups = {snappy=3, crumbly=3, choppy=3, fleshy=2, oddly_breakable_by_hand=2}, groups = {snappy=3, crumbly=3, choppy=3, fleshy=2, oddly_breakable_by_hand=2},
sound = "shooter_rifle", sounds = {
shot = "shooter_rifle",
},
bullet_image = "shooter_bullet.png", bullet_image = "shooter_bullet.png",
particles = { particles = {
amount = 12, amount = 12,
@ -66,7 +70,9 @@ shooter.register_weapon("shooter_guns:shotgun", {
spread = 10, spread = 10,
tool_caps = {full_punch_interval=0.5, damage_groups={fleshy=2}}, tool_caps = {full_punch_interval=0.5, damage_groups={fleshy=2}},
groups = {snappy=3, fleshy=3, oddly_breakable_by_hand=3}, groups = {snappy=3, fleshy=3, oddly_breakable_by_hand=3},
sound = "shooter_shotgun", sounds = {
shot = "shooter_shotgun",
},
bullet_image = "shooter_cap.png", bullet_image = "shooter_cap.png",
particles = { particles = {
amount = 8, amount = 8,
@ -81,12 +87,14 @@ shooter.register_weapon("shooter_guns:machine_gun", {
inventory_image = "shooter_smgun.png", inventory_image = "shooter_smgun.png",
spec = { spec = {
automatic = true, automatic = true,
rounds = 100, rounds = 10,
range = 160, range = 160,
step = 20, step = 20,
tool_caps = {full_punch_interval=0.1, damage_groups={fleshy=2}}, tool_caps = {full_punch_interval=0.1, damage_groups={fleshy=2}},
groups = {snappy=3, fleshy=3, oddly_breakable_by_hand=3}, groups = {snappy=3, fleshy=3, oddly_breakable_by_hand=3},
sound = "shooter_pistol", sounds = {
shot = "shooter_pistol",
},
bullet_image = "shooter_cap.png", bullet_image = "shooter_cap.png",
particles = { particles = {
amount = 4, amount = 4,