master
Jordan Irwin 2021-06-05 10:06:38 -07:00
parent 67504ba362
commit fdf786552e
5 changed files with 124 additions and 123 deletions

158
api.lua
View File

@ -2,58 +2,58 @@
--- Slingshot mod API.
--
-- @module api
-- @module api
-- Displays mod name in square brackets at beginning of log messages
local log_header = '[' .. slingshot.modname .. '] '
local log_header = "[" .. slingshot.modname .. "] "
--- Info log message
--
-- Logs 'info' message if 'log_message' setting set to 'true'.
-- Logs 'info' message if 'log_message' setting set to 'true'.
--
-- @function slingshot.log
-- @tparam string message Message to be logged.
-- @function slingshot.log
-- @tparam string message Message to be logged.
function slingshot.log(message)
if core.settings:get_bool('log_mods') then
core.log('info', log_header .. message)
if core.settings:get_bool("log_mods") then
core.log("info", log_header .. message)
end
end
local debug = core.settings:get_bool('enable_debug_mods') == true
local debug = core.settings:get_bool("enable_debug_mods") == true
--- Debug log message.
--
-- Logs 'info' message if 'debug_log_level' setting set to 'verbose'.
-- Logs 'info' message if 'debug_log_level' setting set to 'verbose'.
--
-- @function slingshot.logDebug
-- @tparam string message Message to be logged.
-- @function slingshot.logDebug
-- @tparam string message Message to be logged.
function slingshot.logDebug(message)
if debug then
core.log(log_header .. 'DEBUG: ' .. message)
core.log(log_header .. "DEBUG: " .. message)
end
end
--- @setting creative_mode
--
-- @see settings.creative_mode
local creative = core.settings:get_bool('creative_mode')
-- @see settings.creative_mode
local creative = core.settings:get_bool("creative_mode")
--- @setting enable_weapon_wear
--
-- @see settings.enable_weapon_wear
local weapon_wear = core.settings:get_bool('enable_weapon_wear') ~= false
-- @see settings.enable_weapon_wear
local weapon_wear = core.settings:get_bool("enable_weapon_wear") ~= false
local tmp_throw = {}
local tmp_throw_timer = 0
local tmp_time = tonumber(core.settings:get('item_entity_ttl')) or 890
local tmp_time = tonumber(core.settings:get("item_entity_ttl")) or 890
-- Registers 'cooldown' time for repeat throws
core.register_globalstep(function(dtime)
tmp_throw_timer = tmp_throw_timer + dtime
if tmp_throw_timer < 0.2 then return end
-- Reset cooldown
tmp_throw_timer = 0
for i, t in pairs(tmp_throw) do
@ -61,15 +61,15 @@ core.register_globalstep(function(dtime)
t.timer = t.timer-0.25
if t.timer <= 0 or t.ob == nil or t.ob:getpos() == nil then table.remove(tmp_throw, i) return end
for ii, ob in pairs(core.get_objects_inside_radius(t.ob:getpos(), 1.5)) do
if (not ob:get_luaentity()) or (ob:get_luaentity() and (ob:get_luaentity().name ~= '__builtin:item')) then
if (not ob:get_luaentity()) or (ob:get_luaentity() and (ob:get_luaentity().name ~= "__builtin:item")) then
-- Which entities can be attacked (mobs & other players unless PVP is enabled)
if (not ob:is_player()) or (ob:is_player() and ob:get_player_name(ob) ~= t.user and core.settings:get_bool('enable_pvp') == true) then
if (not ob:is_player()) or (ob:is_player() and ob:get_player_name(ob) ~= t.user and core.settings:get_bool("enable_pvp") == true) then
ob:punch(puncher, 1.0, {damage_groups=t.damage_groups}, nil)
t.ob:setvelocity({x=0, y=0, z=0})
t.ob:setacceleration({x=0, y=-10, z=0})
t.ob:setvelocity({x=0, y=-10, z=0})
table.remove(tmp_throw, i)
core.sound_play('slingshot_hard_punch', {pos=ob:getpos(), gain=1.0, max_hear_distance=5,})
core.sound_play("slingshot_hard_punch", {pos=ob:getpos(), gain=1.0, max_hear_distance=5,})
break
end
end
@ -80,49 +80,49 @@ end)
--- Action to take when slingshot is used.
--
-- @function on_throw
-- @local
-- @param itemstack
-- @param user
-- @param veloc
-- @local
-- @function on_throw
-- @param itemstack
-- @param user
-- @param veloc
local function on_throw(itemstack, user, veloc, wear_rate, damage_groups)
local pos = user:getpos()
local upos = {x=pos.x, y=pos.y+2, z=pos.z}
local dir = user:get_look_dir()
local item = itemstack:to_table()
-- Throw items in slot to right
local item = user:get_inventory():get_stack('main', user:get_wield_index()+1):get_name()
if item == '' then return itemstack end
local item = user:get_inventory():get_stack("main", user:get_wield_index()+1):get_name()
if item == "" then return itemstack end
local e = core.add_item({x=pos.x, y=pos.y+2, z=pos.z}, item)
if e then
e:setvelocity({x=dir.x*veloc, y=dir.y*veloc, z=dir.z*veloc})
e:setacceleration({x=dir.x*-3, y=-5, z=dir.z*-3})
e:get_luaentity().age = tmp_time
if damage_groups == nil then
damage_groups = {fleshy=1}
end
table.insert(tmp_throw, {ob=e, timer=2, user=user:get_player_name(), damage_groups=damage_groups})
if not creative then
if weapon_wear then
if wear_rate == nil then
wear_rate = 100
end
slingshot.logDebug('Wear rate: ' .. tostring(wear_rate))
slingshot.logDebug("Wear rate: " .. tostring(wear_rate))
itemstack:add_wear(wear_rate)
end
user:get_inventory():remove_item('main', item)
user:get_inventory():remove_item("main", item)
end
core.sound_play('slingshot_throw', {pos=pos, gain=1.0, max_hear_distance=5,})
core.sound_play("slingshot_throw", {pos=pos, gain=1.0, max_hear_distance=5,})
return itemstack
end
end
@ -130,41 +130,41 @@ end
--- Registers a new slingshot.
--
-- 'def' should include 'description', 'damage_groups', & 'velocity'.
-- 'def' should include 'description', 'damage_groups', & 'velocity'.
--
-- @function slingshot.register
-- @tparam string name Name of the slingshot (e.g. ***"iron"***).
-- @tparam table def Slingshot definition table (see [slingshot.register.def](#slingshot.register.def)).
-- @function slingshot.register
-- @tparam string name Name of the slingshot (e.g. ***"iron"***).
-- @tparam table def Slingshot definition table (see [slingshot.register.def](#slingshot.register.def)).
function slingshot.register(name, def)
if def.inventory_image then
-- Inventory image will override image if set
def.image = def.inventory_image
end
if not def.image then
-- The default slingshot
if name == 'slingshot' then
def.image = 'slingshot.png'
if name == "slingshot" then
def.image = "slingshot.png"
else
def.image = 'slingshot_' .. name .. '.png'
def.image = "slingshot_" .. name .. ".png"
end
end
if not def.wield_image then
-- Use inventory image
def.wield_image = def.image
end
core.register_tool('slingshot:' .. name, {
core.register_tool("slingshot:" .. name, {
description = def.description,
range = 4,
inventory_image = def.image,
wield_image = def.wield_image,
on_use = function(itemstack, user, pointed_thing)
--[[ Disabled picking up items with slingshot in hand
if pointed_thing.ref and pointed_thing.ref:is_player() == false and pointed_thing.ref:get_luaentity().name == '__builtin:item' then
pointed_thing.ref:punch(user, {full_punch_interval=1.0, damage_groups=def.damage_groups}, '', nil)
if pointed_thing.ref and pointed_thing.ref:is_player() == false and pointed_thing.ref:get_luaentity().name == "__builtin:item" then
pointed_thing.ref:punch(user, {full_punch_interval=1.0, damage_groups=def.damage_groups}, "", nil)
return itemstack
end
]]
@ -172,37 +172,37 @@ function slingshot.register(name, def)
return itemstack
end,
})
-- def.ingredient overrides def.recipe
if def.ingredient ~= nil then
if slingshot.require_rubber_band and core.global_exists('technic') then
if slingshot.require_rubber_band and core.global_exists("technic") then
-- More complicated recipe for technic
def.recipe = {
{def.ingredient, 'slingshot:rubber_band', def.ingredient},
{'', def.ingredient, ''},
{'', def.ingredient, ''},
{def.ingredient, "slingshot:rubber_band", def.ingredient},
{"", def.ingredient, ""},
{"", def.ingredient, ""},
}
else
def.recipe = {
{def.ingredient, '', def.ingredient},
{'', def.ingredient, ''},
{'', def.ingredient, ''},
{def.ingredient, "", def.ingredient},
{"", def.ingredient, ""},
{"", def.ingredient, ""},
}
end
end
-- Optional register a craft recipe
if def.recipe ~= nil then
core.register_craft({
output = 'slingshot:' .. name,
output = "slingshot:" .. name,
recipe = def.recipe,
})
end
-- Optionally register aliases
if def.aliases ~= nil then
for index, alias in ipairs(def.aliases) do
core.register_alias(alias, 'slingshot:' .. name)
core.register_alias(alias, "slingshot:" .. name)
end
end
end
@ -210,18 +210,18 @@ end
--- Function Definition Tables.
--
-- @section fdtables
-- @section fdtables
--- Slingshot definition table.
--
-- @table slingshot.register.def
-- @tfield string description Human-readable description of slingshot (e.g. ***"Wooden Slinghot"***).
-- @tfield table damage_groups
-- - Same as [minetest.register_tool.tool_capabilities.damage_groups](https://github.com/minetest/minetest/blob/71b02d6/doc/lua_api.txt#L1551)
-- - Default: {fleshy=1}
-- @tfield int velocity Speed & distance at which items will be thrown.
-- @tfield int wear_rate Rate at which the slingshot will wear & break.
-- @tfield table recipe A custom recipe to use for crafting.
-- @tfield string ingredient Creates a standard craft recipe using this ingredient (overrides ***recipe***).
-- @tfield table aliases List of item names that should be uses as aliases for this slingshot.
-- @see slingshot.register
-- @table slingshot.register.def
-- @tfield string description Human-readable description of slingshot (e.g. ***"Wooden Slinghot"***).
-- @tfield table damage_groups
-- - Same as [minetest.register_tool.tool_capabilities.damage_groups](https://github.com/minetest/minetest/blob/71b02d6/doc/lua_api.txt#L1551)
-- - Default: {fleshy=1}
-- @tfield int velocity Speed & distance at which items will be thrown.
-- @tfield int wear_rate Rate at which the slingshot will wear & break.
-- @tfield table recipe A custom recipe to use for crafting.
-- @tfield string ingredient Creates a standard craft recipe using this ingredient (overrides ***recipe***).
-- @tfield table aliases List of item names that should be uses as aliases for this slingshot.
-- @see slingshot.register

View File

@ -4,17 +4,17 @@ slingshot.modname = core.get_current_modname()
slingshot.modpath = core.get_modpath(slingshot.modname)
if core.settings:get_bool('log_mods') then
core.log('action', '[slingshot] Require rubber band: ' .. tostring(slingshot.require_rubber_band))
if core.settings:get_bool("log_mods") then
core.log("action", "[slingshot] Require rubber band: " .. tostring(slingshot.require_rubber_band))
end
local scripts = {
'settings',
'api',
'weapons',
"settings",
"api",
"weapons",
}
for index, script in ipairs(scripts) do
dofile(slingshot.modpath .. '/' .. script .. '.lua')
dofile(slingshot.modpath .. "/" .. script .. ".lua")
end

View File

@ -2,40 +2,40 @@
--- Slingshot mod settings.
--
-- @module settings
-- @module settings
--- Common Settings.
--
-- Settings shared with other mods.
-- Settings shared with other mods.
--
-- @section settings_common
-- @section settings_common
--- Determines if game is being run in creative mode.
--
-- @setting creative_mode
-- @settype bool
-- @setting creative_mode
-- @settype bool
--- Enables/Disables wear/break of slingshots when used for attacking.
--
-- @setting enable_weapon_wear
-- @settype bool
-- @default true
-- @setting enable_weapon_wear
-- @settype bool
-- @default true
--- Unique Settings.
--
-- Settings unique to this mod.
-- Settings unique to this mod.
--
-- @section settings_unique
-- @section settings_unique
--- Require rubber band for slingshot craft recipe.
--
-- @setting slingshot.require_rubber_band
-- @settype bool
-- @default true
slingshot.require_rubber_band = core.settings:get_bool('slingshot.require_rubber_band') ~= false
-- @setting slingshot.require_rubber_band
-- @settype bool
-- @default true
slingshot.require_rubber_band = core.settings:get_bool("slingshot.require_rubber_band", true)

View File

@ -1,2 +1,3 @@
# Require rubber band as additional ingredient in slingshot craft recipes.
slingshot.require_rubber_band (Require rubber band in craft recipe) bool true

View File

@ -1,54 +1,54 @@
-- Registrations for slinghot mod
if core.global_exists('technic') then
core.register_craftitem('slingshot:rubber_band', {
description = 'Rubber band',
inventory_image = 'slingshot_rubber_band.png',
if core.global_exists("technic") then
core.register_craftitem("slingshot:rubber_band", {
description = "Rubber band",
inventory_image = "slingshot_rubber_band.png",
})
core.register_craft({
output = 'slingshot:rubber_band 20',
type = 'shapeless',
recipe = {'technic:rubber'},
output = "slingshot:rubber_band 20",
type = "shapeless",
recipe = {"technic:rubber"},
})
core.register_craft({
output = 'slingshot:rubber_band 2',
output = "slingshot:rubber_band 2",
recipe = {
{'technic:raw_latex', 'technic:raw_latex', ''},
{'technic:raw_latex', '', 'technic:raw_latex'},
{'', 'technic:raw_latex', 'technic:raw_latex'},
{"technic:raw_latex", "technic:raw_latex", ""},
{"technic:raw_latex", "", "technic:raw_latex"},
{"", "technic:raw_latex", "technic:raw_latex"},
}
})
end
-- A wooden slingshot
slingshot.register('wood', {
description = 'Wooden slingshot',
slingshot.register("wood", {
description = "Wooden slingshot",
damage_groups = {fleshy=1},
velocity = 10,
wear_rate = 500,
ingredient = 'group:stick',
ingredient = "group:stick",
aliases = {
slingshot.modname .. ':wooden',
'wood_slingshot',
'wooden_slingshot',
slingshot.modname .. ":wooden",
"wood_slingshot",
"wooden_slingshot",
}
})
-- A stronger iron slingshot
slingshot.register('iron', {
description = 'Iron Slingshot',
slingshot.register("iron", {
description = "Iron Slingshot",
damage_groups = {fleshy=3},
velocity = 15,
wear_rate = 250,
ingredient = 'default:steel_ingot',
ingredient = "default:steel_ingot",
aliases = {
slingshot.modname,
slingshot.modname .. ':slingshot',
'iron_slingshot',
slingshot.modname .. ":slingshot",
"iron_slingshot",
},
})