diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..560847f --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,18 @@ +unused_args = false +allow_defined_top = true + +globals = { + "minetest", +} + +read_globals = { + string = {fields = {"split"}}, + table = {fields = {"copy", "getn"}}, + + -- Builtin + "vector", "ItemStack", + "dump", "DIR_DELIM", "VoxelArea", "Settings", + + -- MTG + "default", "sfinv", "creative", +} diff --git a/API.md b/API.md new file mode 100644 index 0000000..61dc725 --- /dev/null +++ b/API.md @@ -0,0 +1,23 @@ +#Grenades API + +Still WIP. Please suggest new features here: https://forum.minetest.net/viewtopic.php?f=9&t=21466 + +##API + + `grenades.register_grenade("name", { -- Name of the grenade (Like 'smoke' or 'flashbang') + description = "", -- A short description of the grenade. + image = "", -- The name of the grenade's texture + on_explode = function(pos, name) + -- This function is called when the grenade 'explodes' + -- the place the grenade 'exploded' at + -- the name of the player that threw the grenade + end, + placeable = false, -- Optional, default is false + timeout = 5, -- Optional, default is 5 + particle = { -- Adds particles in the grenade's trail + image = "grenades_smoke.png", -- The particle's image + life = 1, -- How long (seconds) it takes for the particle to disappear + size = 4, -- Size of the particle + glow = 0, -- brightens the texture in darkness + } + })` \ No newline at end of file diff --git a/grenades.lua b/grenades.lua index 6677e2d..fe76c9d 100644 --- a/grenades.lua +++ b/grenades.lua @@ -4,22 +4,16 @@ local regular = settings:get_bool("enable_regular_grenade") local flash = settings:get_bool("enable_flashbang_grenade") local smoke = settings:get_bool("enable_smoke_grenade") -minetest.register_craftitem("grenades:gun_powder", { - description = "A dark powder used for crafting smoke grenades", - inventory_image = "grenades_gun_powder.png" -}) -minetest.register_craft({ - type = "shapeless", - output = "grenades:gun_powder", - recipe = {"default:coal_lump", "default:coal_lump", "default:coal_lump", "default:coal_lump"}, -}) +-- Regular Grenade if not regular or regular == true then grenades.register_grenade("regular", { description = "A regular grenade (Kills anyone near where it explodes)", image = "grenades_regular.png", - on_explode = function(pos, player, self) + on_explode = function(pos, name) + local player = minetest.get_player_by_name(name) + local radius = 3 minetest.add_particlespawner({ @@ -47,25 +41,21 @@ if not regular or regular == true then end end end, - recipe = { - {"", "default:steel_ingot", ""}, - {"default:steel_ingot", "default:coal_lump", "default:steel_ingot"}, - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} - }, - timeout = 3 }) end +-- Flashbang Grenade + if not flash or flash == true then grenades.register_grenade("flashbang", { description = "A flashbang grenade (Blinds all who look at the explosion)", image = "grenades_flashbang.png", - on_explode = function(pos, player, self) + on_explode = function(pos, name) for k, v in ipairs(minetest.get_objects_inside_radius(pos, 15)) do if v:is_player() and v:get_hp() > 0 then local playerdir = vector.round(v:get_look_dir()) local grenadedir = vector.round(vector.direction(v:get_pos(), pos)) - local name = v:get_player_name() + local pname = v:get_player_name() if playerdir.x == grenadedir.x and playerdir.z == grenadedir.z then for i = 1, 3, 1 do @@ -80,29 +70,26 @@ if not flash or flash == true then }) minetest.after(1.6*i, function() - if minetest.get_player_by_name(name) then - minetest.get_player_by_name(name):hud_remove(key) + if minetest.get_player_by_name(pname) then + minetest.get_player_by_name(pname):hud_remove(key) end end) end end + end end end, - recipe = { - {"", "default:steel_ingot", ""}, - {"default:steel_ingot", "default:torch", "default:steel_ingot"}, - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} - }, - timeout = 3 }) end +-- Smoke Grenade + if not smoke or smoke == true then grenades.register_grenade("smoke", { description = "A smoke grenade (Generates a lot of smoke around the detonation area)", image = "grenades_smoke_grenade.png", - on_explode = function(pos, player, self) + on_explode = function(pos, name) for i = 0, 5, 1 do minetest.add_particlespawner({ amount = 100, @@ -124,11 +111,73 @@ if not smoke or smoke == true then }) end end, - recipe = { - {"", "default:steel_ingot", ""}, - {"default:steel_ingot", "grenades:gun_powder", "default:steel_ingot"}, - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} - }, - timeout = 3 + particle = { + image = "grenades_smoke.png", + life = 1, + size = 4, + glow = 0, + } }) end + +-- +-- Crafts +-- + +if not settings:get_bool("enable_grenade_recipes") or settings:get_bool("enable_grenade_recipes") == true then + + -- Regular Grenade + + if not regular or regular == true then + minetest.register_craft({ + type = "shapeless", + output = "grenades:grenade_regular", + recipe = { + {"", "default:steel_ingot", ""}, + {"default:steel_ingot", "default:coal_lump", "default:steel_ingot"}, + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} + }, + }) + end + + -- Smoke Grenade + + if not smoke or smoke == true then + minetest.register_craft({ + type = "shapeless", + output = "grenades:grenade_smoke", + recipe = { + {"", "default:steel_ingot", ""}, + {"default:steel_ingot", "grenades:gun_powder", "default:steel_ingot"}, + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} + } + }) + end + + --Flashbang Grenade + + if not flash or flash == true then + minetest.register_craft({ + type = "shapeless", + output = "grenades:grenade_flashbang", + recipe = { + {"", "default:steel_ingot", ""}, + {"default:steel_ingot", "default:torch", "default:steel_ingot"}, + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} + }, + }) + end + + -- Other + + minetest.register_craftitem("grenades:gun_powder", { + description = "A dark powder used for crafting some grenades", + inventory_image = "grenades_gun_powder.png" + }) + + minetest.register_craft({ + type = "shapeless", + output = "grenades:gun_powder", + recipe = {"default:coal_lump", "default:coal_lump", "default:coal_lump", "default:coal_lump"}, + }) +end \ No newline at end of file diff --git a/init.lua b/init.lua index b336eee..e862ca3 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,3 @@ -local settings = minetest.settings - grenades = {} local function throw_grenade(name, player) @@ -16,7 +14,7 @@ local function throw_grenade(name, player) end function grenades.register_grenade(name, def) - if not def.type then def.type = "shaped" end + if not def.timeout then def.timeout = 5 end local grenade_entity = { physical = true, @@ -28,7 +26,7 @@ function grenades.register_grenade(name, def) on_step = function(self, dtime) local pos = self.object:getpos() local node = minetest.get_node(pos) - local player + local obj = self.object if self.timer then self.timer = self.timer + dtime @@ -36,20 +34,29 @@ function grenades.register_grenade(name, def) self.timer = dtime end - if self.thrower_name then - player = minetest.get_player_by_name(self.thrower_name) + if def.particle then + minetest.add_particle({ + pos = obj:get_pos(), + velocity = vector.divide(obj:get_velocity(), 2), + acceleration = vector.divide(obj:get_acceleration(), -5), + expirationtime = def.particle.life, + size = def.particle.size, + collisiondetection = true, + collision_removal = true, + vertical = false, + texture = def.particle.image, + glow = def.particle.glow + }) end - - if player and (self.timer > def.timeout or node.name ~= "air") then - def.on_explode(pos, player, self) - self.object:remove() - elseif self.timer > def.timeout or node.name ~= "air" then - self.object:remove() + if self.timer > def.timeout or node.name ~= "air" then + def.on_explode(pos, self.thrower_name) + + obj:remove() end end } - + minetest.register_entity("grenades:grenade_"..name, grenade_entity) if def.placeable == true then @@ -70,7 +77,6 @@ function grenades.register_grenade(name, def) groups = {oddly_breakable_by_hand = 2}, on_use = function(itemstack, user, pointed_thing) local player_name = user:get_player_name() - local inv = user:get_inventory() if pointed_thing.type ~= "node" then local grenade = throw_grenade("grenades:grenade_"..name, user) @@ -93,7 +99,6 @@ function grenades.register_grenade(name, def) inventory_image = def.image, on_use = function(itemstack, user, pointed_thing) local player_name = user:get_player_name() - local inv = user:get_inventory() if pointed_thing.type ~= "node" then local grenade = throw_grenade("grenades:grenade_"..name, user) @@ -109,15 +114,6 @@ function grenades.register_grenade(name, def) end }) end - - if def.recipe and (not settings:get_bool("enable_grenade_recipes") or - settings:get_bool("enable_grenade_recipes") == true) then - minetest.register_craft({ - type = def.type, - output = "grenades:grenade_"..name, - recipe = def.recipe - }) - end end dofile(minetest.get_modpath("grenades").."/grenades.lua")