Code tidy, add .luacheckrc

Improve callbacks
This commit is contained in:
stujones11 2019-03-19 19:23:23 +00:00
parent fde2b72a68
commit 2a0b7808a1
11 changed files with 114 additions and 56 deletions

50
.luacheckrc Normal file
View file

@ -0,0 +1,50 @@
allow_defined_top = true
read_globals = {
"ItemStack",
"vector",
"VoxelArea",
"VoxelManip",
"PseudoRandom",
string = {fields = {"split"}},
table = {fields = {"copy"}},
math = {fields = {"hypot"}},
os = {fields = {"time"}},
}
globals = {
"minetest",
}
files["shooter/init.lua"] = {
globals = {"shooter"}
}
files["shooter_crossbow/init.lua"] = {
globals = {"shooter", "SHOOTER_ARROW_TOOL_CAPS", "dye"}
}
files["shooter_flaregun/init.lua"] = {
globals = {"shooter"}
}
files["shooter_grenade/init.lua"] = {
globals = {"shooter"}
}
files["shooter_guns/init.lua"] = {
globals = {"shooter"}
}
files["shooter_hook/init.lua"] = {
globals = {"shooter"}
}
files["shooter_rocket/init.lua"] = {
globals = {"shooter"}
}
files["shooter_turret/init.lua"] = {
globals = {"shooter", "player_api"}
}

View file

@ -67,3 +67,4 @@ API Documentation
-----------------
TODO

View file

@ -53,7 +53,6 @@ shooter.default_particles = {
texture = "shooter_hit.png",
}
local rounds = {}
local shots = {}
local shooting = {}
local config = shooter.config
@ -78,16 +77,20 @@ shooter.register_weapon = function(name, def)
minetest.register_tool(name.."_loaded", {
description = def.description,
inventory_image = def.inventory_image,
on_use = function(itemstack, user)
on_use = function(itemstack, user, pointed_thing)
if type(def.on_use) == "function" then
itemstack = def.on_use, pointed_thing)
end
local spec = table.copy(def.spec)
if shooter.fire_weapon(user, itemstack, spec) then
itemstack:add_wear(def.spec.wear)
if itemstack:get_count() == 0 then
itemstack = def.unloaded_item.name
itemstack:replace(def.unloaded_item.name)
end
end
return itemstack
end,
on_hit = def.on_hit,
groups = {not_in_creative_inventory=1},
})
-- Register unloaded item tool
@ -95,16 +98,16 @@ shooter.register_weapon = function(name, def)
description = def.unloaded_item.description,
inventory_image = def.unloaded_item.inventory_image,
groups = def.unloaded_item.groups or {},
on_use = function(itemstack, user, pointed_thing)
on_use = function(itemstack, user)
local inv = user:get_inventory()
if inv then
local stack = def.reload_item
if inv:contains_item("main", stack) then
minetest.sound_play((def.sounds.reload), {object=user})
minetest.sound_play(def.sounds.reload, {object=user})
inv:remove_item("main", stack)
itemstack:replace(name.."_loaded 1 1")
else
minetest.sound_play((def.sounds.fail_shot), {object=user})
minetest.sound_play(def.sounds.fail_shot, {object=user})
end
end
return itemstack
@ -188,11 +191,9 @@ shooter.is_valid_object = function(object)
end
local function process_hit(pointed_thing, spec, dir)
if pointed_thing.type == "node" then
if config.allow_nodes == true then
if pointed_thing.type == "node" and config.allow_nodes == true then
local pos = minetest.get_pointed_thing_position(pointed_thing, false)
shooter.punch_node(pos, spec)
end
elseif pointed_thing.type == "object" then
local object = pointed_thing.ref
if shooter.is_valid_object(object) == true then
@ -200,12 +201,16 @@ local function process_hit(pointed_thing, spec, dir)
if player then
object:punch(player, nil, spec.tool_caps, dir)
local pos = pointed_thing.intersection_point or object:get_pos()
local groups = object:get_armor_groups() or {}
if groups.fleshy then
shooter.spawn_particles(pos, spec.particles)
end
end
end
if type(spec.on_hit) == "function" then
return spec.on_hit(pointed_thing, spec, dir)
end
local def = minetest.registered_items[spec.name] or {}
if type(def.on_hit) == "function" then
return def.on_hit(pointed_thing, spec, dir)
end
end
@ -222,8 +227,8 @@ local function process_round(round)
return process_hit(pointed_thing, round.spec, round.dir)
end
round.pos = p2
minetest.after(shooter.config.rounds_update_time, function(round)
process_round(round)
minetest.after(shooter.config.rounds_update_time, function(...)
process_round(...)
end, round)
end
@ -268,9 +273,10 @@ local function fire_weapon(player, itemstack, spec, extended)
end
local interval = spec.tool_caps.full_punch_interval
shots[spec.user] = minetest.get_us_time() / 1000000 + interval
minetest.after(interval, function(player, itemstack, spec)
minetest.after(interval, function(...)
if shooting[spec.user] then
fire_weapon(player, itemstack, spec, true)
local arg = {...}
fire_weapon(arg[1], arg[2], arg[3], true)
end
end, player, itemstack, spec)
end
@ -289,6 +295,7 @@ shooter.fire_weapon = function(player, itemstack, spec)
shooting[name] = true
end
spec.user = name
spec.name = itemstack:get_name()
fire_weapon(player, itemstack, spec)
return true
end
@ -297,8 +304,8 @@ shooter.blast = function(pos, radius, fleshy, distance, user)
if not user then
return
end
pos = vector.round(pos)
local name = user:get_player_name()
local pos = vector.round(pos)
local p1 = vector.subtract(pos, radius)
local p2 = vector.add(pos, radius)
minetest.sound_play("tnt_explode", {pos=pos, gain=1})
@ -376,10 +383,13 @@ shooter.blast = function(pos, radius, fleshy, distance, user)
vm:set_data(data)
vm:update_liquids()
vm:write_to_map()
vm:update_map()
end
end
shooter.get_shooting = function(name)
return shooting[name]
end
shooter.set_shooting = function(name, is_shooting)
shooting[name] = is_shooting and true or nil
end

View file

@ -32,7 +32,7 @@ The API works with tools and items that represent a weapon,
then you should register the weapon and another support items (like ammo).
### Methods
* `shooter.register_weapon(weapon_name, {Weapon Definitions})`: Register a weapon item
* `shooter:register_weapon(weapon_name, {Weapon Definitions})`: Register a weapon item
`weapon_name` is the itemstring for a tool to be registered
Global Tables
@ -42,7 +42,7 @@ Global Tables
Definition tables
-----------------
### Weapon Definitions (`shooter.register_weapon`)
### Weapon Definitions (`shooter:register_weapon`)
{
description = "Rifle", -- Weapon description name

View file

@ -35,13 +35,11 @@ local input = io.open(modpath.."/shooter.conf", "r")
if input then
dofile(modpath.."/shooter.conf")
input:close()
input = nil
end
input = io.open(worldpath.."/shooter.conf", "r")
if input then
dofile(worldpath.."/shooter.conf")
input:close()
input = nil
end
for name, _ in pairs(shooter.config) do
local global = "SHOOTER_"..name:upper()
@ -63,6 +61,7 @@ for name, config in pairs(shooter.config) do
shooter.config[name] = setting
end
end
shooter.default_particles.texture = shooter.config.explosion_texture
-- Legacy Entity Support
@ -77,7 +76,7 @@ minetest.register_entity("shooter:turret_entity", {
-- Automatic Firing
if shooter.config.automatic_weapons == true then
minetest.register_globalstep(function(dtime)
minetest.register_globalstep(function()
for _,player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if name then

View file

@ -78,7 +78,6 @@ local function strike(arrow, pointed_thing, name)
if pointed_thing.type == "object" then
local target = pointed_thing.ref
if shooter.is_valid_object(target) then
local puncher = minetest.get_player_by_name(name)
if puncher and puncher ~= target then
local groups = target:get_armor_groups() or {}
if groups.fleshy then
@ -201,7 +200,7 @@ minetest.register_entity("shooter_crossbow:arrow_entity", {
self.timer = 0
end
end,
get_staticdata = function(self)
get_staticdata = function()
return "expired"
end,
})
@ -215,7 +214,7 @@ for _, color in pairs(dye_basecolors) do
description = "Crossbow",
inventory_image = get_texture("crossbow_loaded", color),
groups = {not_in_creative_inventory=1},
on_use = function(itemstack, user, pointed_thing)
on_use = function(itemstack, user)
minetest.sound_play("shooter_click", {object=user})
if not minetest.setting_getbool("creative_mode") then
itemstack:add_wear(65535 / config.crossbow_uses)
@ -245,7 +244,7 @@ for _, color in pairs(dye_basecolors) do
obj:set_animation({x=frame, y=frame}, 0)
obj:set_velocity({x=dir.x * 14, y=dir.y * 14, z=dir.z * 14})
obj:set_acceleration({x=dir.x * -3, y=-5, z=dir.z * -3})
pointed_thing = get_pointed_thing(pos, dir, 5)
local pointed_thing = get_pointed_thing(pos, dir, 5)
if pointed_thing then
strike(ent, pointed_thing, ent.user)
end
@ -259,7 +258,7 @@ end
minetest.register_tool("shooter_crossbow:crossbow", {
description = "Crossbow",
inventory_image = "shooter_crossbow.png",
on_use = function(itemstack, user, pointed_thing)
on_use = function(itemstack, user)
local inv = user:get_inventory()
local stack = inv:get_stack("main", user:get_wield_index() + 1)
local color = string.match(stack:get_name(), "shooter_crossbow:arrow_(%a+)")
@ -270,13 +269,13 @@ minetest.register_tool("shooter_crossbow:crossbow", {
end
return "shooter_crossbow:crossbow_loaded_"..color.." 1 "..itemstack:get_wear()
end
for _, color in pairs(dye_basecolors) do
if inv:contains_item("main", "shooter_crossbow:arrow_"..color) then
for _, clr in pairs(dye_basecolors) do
if inv:contains_item("main", "shooter_crossbow:arrow_"..clr) then
minetest.sound_play("shooter_reload", {object=user})
if not minetest.setting_getbool("creative_mode") then
inv:remove_item("main", "shooter_crossbow:arrow_"..color.." 1")
inv:remove_item("main", "shooter_crossbow:arrow_"..clr.." 1")
end
return "shooter_crossbow:crossbow_loaded_"..color.." 1 "..itemstack:get_wear()
return "shooter_crossbow:crossbow_loaded_"..clr.." 1 "..itemstack:get_wear()
end
end
minetest.sound_play("shooter_click", {object=user})

View file

@ -39,7 +39,7 @@ minetest.register_abm({
nodenames = "shooter_flaregun:flare_light",
interval = 5,
chance = 1,
action = function(pos, node)
action = function(pos)
local time = os.time()
local meta = minetest.get_meta(pos)
local init_time = meta:get_int("init_time") or 0
@ -112,8 +112,8 @@ minetest.register_entity("shooter_flaregun:flare_entity", {
loop = true,
max_hear_distance = 8,
})
minetest.after(30, function(sound)
minetest.sound_stop(sound)
minetest.after(30, function(...)
minetest.sound_stop(...)
end, sound)
end
self.object:remove()
@ -121,7 +121,7 @@ minetest.register_entity("shooter_flaregun:flare_entity", {
self.timer = 0
end
end,
get_staticdata = function(self)
get_staticdata = function()
return "expired"
end,
})
@ -129,7 +129,7 @@ minetest.register_entity("shooter_flaregun:flare_entity", {
minetest.register_tool("shooter_flaregun:flaregun", {
description = "Flare Gun",
inventory_image = "shooter_flaregun.png",
on_use = function(itemstack, user, pointed_thing)
on_use = function(itemstack, user)
local inv = user:get_inventory()
if not inv:contains_item("main", "shooter_flaregun:flare") then
minetest.sound_play("shooter_click", {object=user})

View file

@ -54,7 +54,7 @@ minetest.register_entity("shooter_grenade:grenade_entity", {
self.timer = 0
end
end,
get_staticdata = function(self)
get_staticdata = function()
return "expired"
end,
})

View file

@ -18,7 +18,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
]]--
local function throw_hook(itemstack, user, vel)
local inv = user:get_inventory()
local pos = user:get_pos()
local dir = user:get_look_dir()
local yaw = user:get_look_horizontal()
@ -73,7 +72,7 @@ minetest.register_entity("shooter_hook:hook", {
minetest.get_node(pos).name == "air" then
local player = minetest.get_player_by_name(self.user)
if player then
player:moveto(pos)
player:move_to(pos)
end
end
if minetest.get_item_group(node.name, "lava") == 0 then
@ -84,7 +83,7 @@ minetest.register_entity("shooter_hook:hook", {
self.timer = 0
end
end,
get_staticdata = function(self)
get_staticdata = function()
return "expired"
end,
})
@ -104,7 +103,7 @@ minetest.register_tool("shooter_hook:grapple_hook", {
minetest.register_tool("shooter_hook:grapple_gun", {
description = "Grappling Gun",
inventory_image = "shooter_hook_gun.png",
on_use = function(itemstack, user, pointed_thing)
on_use = function(itemstack, user)
local inv = user:get_inventory()
if inv:contains_item("main", "shooter_hook:grapple_hook") and
inv:contains_item("main", "tnt:gunpowder") then

View file

@ -60,7 +60,7 @@ minetest.register_entity("shooter_rocket:rocket_entity", {
self.timer = 0
end
end,
get_staticdata = function(self)
get_staticdata = function()
return "expired"
end,
})
@ -105,7 +105,7 @@ minetest.register_tool("shooter_rocket:rocket_gun_loaded", {
minetest.register_tool("shooter_rocket:rocket_gun", {
description = "Rocket Gun",
inventory_image = "shooter_rocket_gun.png",
on_use = function(itemstack, user, pointed_thing)
on_use = function(itemstack, user)
local inv = user:get_inventory()
if inv:contains_item("main", "shooter_rocket:rocket") then
minetest.sound_play("shooter_reload", {object=user})

View file

@ -52,7 +52,7 @@ minetest.register_entity("shooter_turret:turret_entity", {
pitch = 40,
yaw = 0,
firing = false,
on_activate = function(self, staticdata)
on_activate = function(self)
self.pos = self.object:get_pos()
self.yaw = self.object:get_yaw()
if minetest.get_node(self.pos).name ~= "shooter_turret:turret" then
@ -60,7 +60,7 @@ minetest.register_entity("shooter_turret:turret_entity", {
return
end
self.object:set_animation({x=self.pitch, y=self.pitch}, 0)
self.object:set_armor_groups({fleshy=0})
self.object:set_armor_groups({immortal=1})
-- Remove duplicates
get_turret_entity(self.pos)
end,
@ -222,7 +222,7 @@ minetest.register_node("shooter_turret:turret", {
local inv = meta:get_inventory()
inv:set_size("main", 16)
end,
after_place_node = function(pos, placer)
after_place_node = function(pos)
local node = minetest.get_node({x=pos.x, y=pos.y + 1, z=pos.z})
if node.name == "air" then
if not get_turret_entity(pos) then
@ -230,12 +230,12 @@ minetest.register_node("shooter_turret:turret", {
end
end
end,
can_dig = function(pos, player)
can_dig = function(pos)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
after_destruct = function(pos, oldnode)
after_destruct = function(pos)
local ent = get_turret_entity(pos)
if ent then
ent.object:remove()
@ -243,7 +243,7 @@ minetest.register_node("shooter_turret:turret", {
end,
mesecons = {
effector = {
action_on = function(pos, node)
action_on = function(pos)
local ent = get_turret_entity(pos)
if ent then
if ent.firing == false then
@ -252,7 +252,7 @@ minetest.register_node("shooter_turret:turret", {
end
end
end,
action_off = function(pos, node)
action_off = function(pos)
local ent = get_turret_entity(pos)
if ent then
ent.firing = false
@ -266,7 +266,7 @@ minetest.register_abm({
nodenames = {"shooter_turret:turret"},
interval = 15,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
action = function(pos)
if not get_turret_entity(pos) then
minetest.add_entity(pos, "shooter_turret:turret_entity")
end