Just some sanity checks for bow charging

This commit is contained in:
Wuzzy 2018-05-07 16:42:51 +02:00
parent 88ef857fb4
commit db755036c3
2 changed files with 28 additions and 5 deletions

View File

@ -4,7 +4,7 @@ local mod_awards = minetest.get_modpath("awards") and minetest.get_modpath("mcl_
minetest.register_craftitem("mcl_throwing:arrow", { minetest.register_craftitem("mcl_throwing:arrow", {
description = "Arrow", description = "Arrow",
_doc_items_longdesc = "Arrows are ammunition for bows and dispensers.", _doc_items_longdesc = "Arrows are ammunition for bows and dispensers.",
_doc_items_usagehelp = "To use arrows as ammunition for a bow, just put them anywhere in your inventory, they will be used up automatically. To use arrows as ammunition for a dispenser, place them in the dispenser's inventory.", _doc_items_usagehelp = "To use arrows as ammunition for a bow, just put them anywhere in your inventory, they will be used up automatically. To use arrows as ammunition for a dispenser, place them in the dispenser's inventory. Arrows fired from a dispenser always deal 3 damage.",
inventory_image = "mcl_throwing_arrow_inv.png", inventory_image = "mcl_throwing_arrow_inv.png",
groups = { ammo=1, ammo_bow=1 }, groups = { ammo=1, ammo_bow=1 },
_on_dispense = function(itemstack, dispenserpos, droppos, dropnode, dropdir) _on_dispense = function(itemstack, dispenserpos, droppos, dropnode, dropdir)

View File

@ -6,13 +6,22 @@ local arrows = {
local GRAVITY = 9.81 local GRAVITY = 9.81
local BOW_DURABILITY = 385 local BOW_DURABILITY = 385
local BOW_CHARGE_TIME_HALF = 500000
local BOW_CHARGE_TIME_FULL = 1000000 -- Charging time in microseconds
local BOW_CHARGE_TIME_HALF = 500000 -- bow level 1
local BOW_CHARGE_TIME_FULL = 1000000 -- bow level 2 (full charge)
-- TODO: Use Minecraft speed (ca. 53 m/s) -- TODO: Use Minecraft speed (ca. 53 m/s)
-- Currently nerfed because at full speed the arrow would easily get out of the range of the loaded map. -- Currently nerfed because at full speed the arrow would easily get out of the range of the loaded map.
local BOW_MAX_SPEED = 26 local BOW_MAX_SPEED = 26
--[[ Store the charging state of each player.
keys: player name
value:
false or nil = not charging or player not existing
number: currently charging, the number is the time from minetest.get_us_time
in which the charging has started
]]
local bow_load = {} local bow_load = {}
mcl_throwing.shoot_arrow = function(arrow_item, pos, dir, yaw, shooter, power, damage) mcl_throwing.shoot_arrow = function(arrow_item, pos, dir, yaw, shooter, power, damage)
@ -129,8 +138,19 @@ controls.register_on_release(function(player, key, time)
local has_shot = false local has_shot = false
local speed, damage local speed, damage
local charge = minetest.get_us_time() - bow_load[player:get_player_name()] local p_load = bow_load[player:get_player_name()]
local charge
-- Type sanity check
if type(p_load) == "number" then
charge = minetest.get_us_time() - p_load
else
-- In case something goes wrong ...
-- Just assume minimum charge.
charge = 0
minetest.log("warning", "[mcl_throwing] Player "..player:get_player_name().." fires arrow with non-numeric bow_load!")
end
charge = math.max(math.min(charge, BOW_CHARGE_TIME_FULL), 0) charge = math.max(math.min(charge, BOW_CHARGE_TIME_FULL), 0)
local charge_ratio = charge / BOW_CHARGE_TIME_FULL local charge_ratio = charge / BOW_CHARGE_TIME_FULL
charge_ratio = math.max(math.min(charge_ratio, 1), 0) charge_ratio = math.max(math.min(charge_ratio, 1), 0)
@ -139,7 +159,6 @@ controls.register_on_release(function(player, key, time)
if charge >= BOW_CHARGE_TIME_FULL then if charge >= BOW_CHARGE_TIME_FULL then
speed = BOW_MAX_SPEED speed = BOW_MAX_SPEED
local r = math.random(1,5) local r = math.random(1,5)
-- Damage and range have been nerfed because the arrow charges very quickly
if r == 1 then if r == 1 then
-- 20% chance for critical hit -- 20% chance for critical hit
damage = 10 damage = 10
@ -208,6 +227,10 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
minetest.register_on_leaveplayer(function(player)
bow_load[player:get_player_name()] = nil
end)
if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then
minetest.register_craft({ minetest.register_craft({
output = 'mcl_throwing:bow', output = 'mcl_throwing:bow',