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 TODO

View file

@ -53,7 +53,6 @@ shooter.default_particles = {
texture = "shooter_hit.png", texture = "shooter_hit.png",
} }
local rounds = {}
local shots = {} local shots = {}
local shooting = {} local shooting = {}
local config = shooter.config local config = shooter.config
@ -78,16 +77,20 @@ shooter.register_weapon = function(name, def)
minetest.register_tool(name.."_loaded", { minetest.register_tool(name.."_loaded", {
description = def.description, description = def.description,
inventory_image = def.inventory_image, 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) local spec = table.copy(def.spec)
if shooter.fire_weapon(user, itemstack, spec) then if shooter.fire_weapon(user, itemstack, spec) then
itemstack:add_wear(def.spec.wear) itemstack:add_wear(def.spec.wear)
if itemstack:get_count() == 0 then if itemstack:get_count() == 0 then
itemstack = def.unloaded_item.name itemstack:replace(def.unloaded_item.name)
end end
end end
return itemstack return itemstack
end, end,
on_hit = def.on_hit,
groups = {not_in_creative_inventory=1}, groups = {not_in_creative_inventory=1},
}) })
-- Register unloaded item tool -- Register unloaded item tool
@ -95,16 +98,16 @@ shooter.register_weapon = function(name, def)
description = def.unloaded_item.description, description = def.unloaded_item.description,
inventory_image = def.unloaded_item.inventory_image, inventory_image = def.unloaded_item.inventory_image,
groups = def.unloaded_item.groups or {}, groups = def.unloaded_item.groups or {},
on_use = function(itemstack, user, pointed_thing) on_use = function(itemstack, user)
local inv = user:get_inventory() local inv = user:get_inventory()
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}) minetest.sound_play(def.sounds.reload, {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}) minetest.sound_play(def.sounds.fail_shot, {object=user})
end end
end end
return itemstack return itemstack
@ -188,11 +191,9 @@ shooter.is_valid_object = function(object)
end end
local function process_hit(pointed_thing, spec, dir) local function process_hit(pointed_thing, spec, dir)
if pointed_thing.type == "node" then if pointed_thing.type == "node" and config.allow_nodes == true then
if config.allow_nodes == true then local pos = minetest.get_pointed_thing_position(pointed_thing, false)
local pos = minetest.get_pointed_thing_position(pointed_thing, false) shooter.punch_node(pos, spec)
shooter.punch_node(pos, spec)
end
elseif pointed_thing.type == "object" then elseif pointed_thing.type == "object" then
local object = pointed_thing.ref local object = pointed_thing.ref
if shooter.is_valid_object(object) == true then if shooter.is_valid_object(object) == true then
@ -200,12 +201,16 @@ local function process_hit(pointed_thing, spec, dir)
if player then if player then
object:punch(player, nil, spec.tool_caps, dir) object:punch(player, nil, spec.tool_caps, dir)
local pos = pointed_thing.intersection_point or object:get_pos() local pos = pointed_thing.intersection_point or object:get_pos()
shooter.spawn_particles(pos, spec.particles) local groups = object:get_armor_groups() or {}
if groups.fleshy then
shooter.spawn_particles(pos, spec.particles)
end
end end
end end
end end
if type(spec.on_hit) == "function" then local def = minetest.registered_items[spec.name] or {}
return spec.on_hit(pointed_thing, spec, dir) if type(def.on_hit) == "function" then
return def.on_hit(pointed_thing, spec, dir)
end end
end end
@ -222,8 +227,8 @@ local function process_round(round)
return process_hit(pointed_thing, round.spec, round.dir) return process_hit(pointed_thing, round.spec, round.dir)
end end
round.pos = p2 round.pos = p2
minetest.after(shooter.config.rounds_update_time, function(round) minetest.after(shooter.config.rounds_update_time, function(...)
process_round(round) process_round(...)
end, round) end, round)
end end
@ -268,9 +273,10 @@ local function fire_weapon(player, itemstack, spec, extended)
end end
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.after(interval, function(player, itemstack, spec) minetest.after(interval, function(...)
if shooting[spec.user] then if shooting[spec.user] then
fire_weapon(player, itemstack, spec, true) local arg = {...}
fire_weapon(arg[1], arg[2], arg[3], true)
end end
end, player, itemstack, spec) end, player, itemstack, spec)
end end
@ -289,6 +295,7 @@ shooter.fire_weapon = function(player, itemstack, spec)
shooting[name] = true shooting[name] = true
end end
spec.user = name spec.user = name
spec.name = itemstack:get_name()
fire_weapon(player, itemstack, spec) fire_weapon(player, itemstack, spec)
return true return true
end end
@ -297,8 +304,8 @@ shooter.blast = function(pos, radius, fleshy, distance, user)
if not user then if not user then
return return
end end
pos = vector.round(pos)
local name = user:get_player_name() local name = user:get_player_name()
local pos = vector.round(pos)
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})
@ -376,10 +383,13 @@ shooter.blast = function(pos, radius, fleshy, distance, user)
vm:set_data(data) vm:set_data(data)
vm:update_liquids() vm:update_liquids()
vm:write_to_map() vm:write_to_map()
vm:update_map()
end end
end end
shooter.get_shooting = function(name)
return shooting[name]
end
shooter.set_shooting = function(name, is_shooting) shooter.set_shooting = function(name, is_shooting)
shooting[name] = is_shooting and true or nil shooting[name] = is_shooting and true or nil
end 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). then you should register the weapon and another support items (like ammo).
### Methods ### 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 `weapon_name` is the itemstring for a tool to be registered
Global Tables Global Tables
@ -42,7 +42,7 @@ Global Tables
Definition tables Definition tables
----------------- -----------------
### Weapon Definitions (`shooter.register_weapon`) ### Weapon Definitions (`shooter:register_weapon`)
{ {
description = "Rifle", -- Weapon description name description = "Rifle", -- Weapon description name

View file

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

View file

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

View file

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

View file

@ -54,7 +54,7 @@ minetest.register_entity("shooter_grenade:grenade_entity", {
self.timer = 0 self.timer = 0
end end
end, end,
get_staticdata = function(self) get_staticdata = function()
return "expired" return "expired"
end, 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 function throw_hook(itemstack, user, vel)
local inv = user:get_inventory()
local pos = user:get_pos() local pos = user:get_pos()
local dir = user:get_look_dir() local dir = user:get_look_dir()
local yaw = user:get_look_horizontal() local yaw = user:get_look_horizontal()
@ -73,7 +72,7 @@ minetest.register_entity("shooter_hook:hook", {
minetest.get_node(pos).name == "air" then minetest.get_node(pos).name == "air" then
local player = minetest.get_player_by_name(self.user) local player = minetest.get_player_by_name(self.user)
if player then if player then
player:moveto(pos) player:move_to(pos)
end end
end end
if minetest.get_item_group(node.name, "lava") == 0 then if minetest.get_item_group(node.name, "lava") == 0 then
@ -84,7 +83,7 @@ minetest.register_entity("shooter_hook:hook", {
self.timer = 0 self.timer = 0
end end
end, end,
get_staticdata = function(self) get_staticdata = function()
return "expired" return "expired"
end, end,
}) })
@ -104,9 +103,9 @@ minetest.register_tool("shooter_hook:grapple_hook", {
minetest.register_tool("shooter_hook:grapple_gun", { minetest.register_tool("shooter_hook:grapple_gun", {
description = "Grappling Gun", description = "Grappling Gun",
inventory_image = "shooter_hook_gun.png", inventory_image = "shooter_hook_gun.png",
on_use = function(itemstack, user, pointed_thing) on_use = function(itemstack, user)
local inv = user:get_inventory() local inv = user:get_inventory()
if inv:contains_item("main", "shooter_hook:grapple_hook") and if inv:contains_item("main", "shooter_hook:grapple_hook") and
inv:contains_item("main", "tnt:gunpowder") then inv:contains_item("main", "tnt:gunpowder") then
inv:remove_item("main", "tnt:gunpowder") inv:remove_item("main", "tnt:gunpowder")
minetest.sound_play("shooter_reload", {object=user}) minetest.sound_play("shooter_reload", {object=user})

View file

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

View file

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