From 11c5d36c558817df61542151076a21e0e0d130c4 Mon Sep 17 00:00:00 2001 From: Michieal Date: Sat, 2 Dec 2023 07:00:25 -0500 Subject: [PATCH 01/21] Initial Commit. --- mods/PLAYER/mcl_fovapi/api.md | 9 +++++ mods/PLAYER/mcl_fovapi/init.lua | 60 +++++++++++++++++++++++++++++++++ mods/PLAYER/mcl_fovapi/mod.conf | 4 +++ 3 files changed, 73 insertions(+) create mode 100644 mods/PLAYER/mcl_fovapi/api.md create mode 100644 mods/PLAYER/mcl_fovapi/init.lua create mode 100644 mods/PLAYER/mcl_fovapi/mod.conf diff --git a/mods/PLAYER/mcl_fovapi/api.md b/mods/PLAYER/mcl_fovapi/api.md new file mode 100644 index 000000000..39d6ee86d --- /dev/null +++ b/mods/PLAYER/mcl_fovapi/api.md @@ -0,0 +1,9 @@ + + + +mcl_fovapi = {} +mcl_fovapi.default_fov = {} +mcl_fovapi.registered_modifiers = {} +mcl_fovapi.applied_modifiers = {} +function mcl_fovapi.register_modifier(name, fov_factor, time, exclusive, on_start, on_end) +function mcl_fovapi.apply_modifier(player, modifier_name) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua new file mode 100644 index 000000000..17c0a9262 --- /dev/null +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -0,0 +1,60 @@ +--- +--- Copyright 2023, Michieal. +--- License: GPL3. (Default Mineclone2 License) +--- Created by michieal. +--- DateTime: 12/2/23 5:47 AM +--- + +mcl_fovapi = {} + +-- Handles default fov for players +mcl_fovapi.default_fov = {} +mcl_fovapi.registered_modifiers = {} +mcl_fovapi.applied_modifiers = {} + +-- set to blank on join (for 3rd party mods) +minetest.register_on_joinplayer(function(player) + local name = player:get_player_name() + -- Assign default FOV + mcl_fovapi.default_fov[name] = player:get_fov() +end) + +-- clear when player leaves +minetest.register_on_leaveplayer(function(player) + local name = player:get_player_name() + -- Remove default FOV + mcl_fovapi.default_fov[name] = nil +end) + +function mcl_fovapi.register_modifier(name, fov_factor, time, exclusive, on_start, on_end) + local def = { + modifer_name = name, + fov = fov_factor, + time = time, + exclusive = exclusive, + on_start = on_start, + on_end = on_end, + } + + mcl_fovapi.registered_modifiers[name] = def + +end + +function mcl_fovapi.apply_modifier(player, modifier_name) + + if modifier_name == nil then return end + if mcl_fovapi.registered_modifiers[modifier_name] == nil then return end + + local modifier = mcl_fovapi.registered_modifiers[modifier_name] + if modifier.on_start ~= nil then + modifier.on_start(player) + end + + mcl_fovapi.applied_modifiers[player][modifier_name] = true -- set the applied to be true. + + -- do modiifier apply code. + + + +end + diff --git a/mods/PLAYER/mcl_fovapi/mod.conf b/mods/PLAYER/mcl_fovapi/mod.conf new file mode 100644 index 000000000..b78c78596 --- /dev/null +++ b/mods/PLAYER/mcl_fovapi/mod.conf @@ -0,0 +1,4 @@ +name = mcl_fovapi +author = Michieal +description = An API for handling FOV changes. +depends = mcl_player \ No newline at end of file From fc80d4fb9f836cd1bee05720e4d0cdc9775c3d97 Mon Sep 17 00:00:00 2001 From: Michieal Date: Sun, 3 Dec 2023 06:47:28 -0500 Subject: [PATCH 02/21] Initial API state. --- mods/PLAYER/mcl_fovapi/api.md | 14 +++- mods/PLAYER/mcl_fovapi/init.lua | 135 +++++++++++++++++++++++++++++--- 2 files changed, 139 insertions(+), 10 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/api.md b/mods/PLAYER/mcl_fovapi/api.md index 39d6ee86d..05b8be29b 100644 --- a/mods/PLAYER/mcl_fovapi/api.md +++ b/mods/PLAYER/mcl_fovapi/api.md @@ -1,9 +1,21 @@ +registered_modifiers has defs of modifiers +applied_modifiers is indexed by player name mcl_fovapi = {} + mcl_fovapi.default_fov = {} + mcl_fovapi.registered_modifiers = {} + mcl_fovapi.applied_modifiers = {} -function mcl_fovapi.register_modifier(name, fov_factor, time, exclusive, on_start, on_end) + +function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end) + function mcl_fovapi.apply_modifier(player, modifier_name) + +function mcl_fovapi.remove_modifier(player, modifier_name) + +function mcl_fovapi.remove_all_modifiers(player, time) + diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 17c0a9262..649f582ab 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -5,18 +5,26 @@ --- DateTime: 12/2/23 5:47 AM --- +-- Locals (and cached) +local DEBUG = false -- debug constant for troubleshooting. +local pairs = pairs + +-- Globals mcl_fovapi = {} --- Handles default fov for players -mcl_fovapi.default_fov = {} +mcl_fovapi.default_fov = {} -- Handles default fov for players mcl_fovapi.registered_modifiers = {} mcl_fovapi.applied_modifiers = {} --- set to blank on join (for 3rd party mods) minetest.register_on_joinplayer(function(player) local name = player:get_player_name() -- Assign default FOV mcl_fovapi.default_fov[name] = player:get_fov() + + if DEBUG then + minetest.log("FOV::Player: " .. name .. "\nFOV: " .. player:get_fov()) + end + end) -- clear when player leaves @@ -26,24 +34,41 @@ minetest.register_on_leaveplayer(function(player) mcl_fovapi.default_fov[name] = nil end) -function mcl_fovapi.register_modifier(name, fov_factor, time, exclusive, on_start, on_end) +function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end) + if is_multiplier ~= true and is_multiplier ~= false then + is_multiplier = false + end + if exclusive ~= true and exclusive ~= false then + exclusive = false + end local def = { modifer_name = name, fov = fov_factor, time = time, + is_multiplier = is_multiplier, exclusive = exclusive, on_start = on_start, on_end = on_end, } + if DEBUG then + minetest.log("FOV::Modifier Definition Registered:\n" .. dump(def)) + end + mcl_fovapi.registered_modifiers[name] = def end function mcl_fovapi.apply_modifier(player, modifier_name) - - if modifier_name == nil then return end - if mcl_fovapi.registered_modifiers[modifier_name] == nil then return end + if player == nil then + return + end + if modifier_name == nil then + return + end + if mcl_fovapi.registered_modifiers[modifier_name] == nil then + return + end local modifier = mcl_fovapi.registered_modifiers[modifier_name] if modifier.on_start ~= nil then @@ -51,10 +76,102 @@ function mcl_fovapi.apply_modifier(player, modifier_name) end mcl_fovapi.applied_modifiers[player][modifier_name] = true -- set the applied to be true. + if DEBUG then + minetest.log("FOV::Player Applied Modifiers :" .. dump(mcl_fovapi.applied_modifiers[player])) + end + local pname = player:get_player_name() - -- do modiifier apply code. - + if DEBUG then + minetest.log("FOV::Modifier applied to player:" .. pname .. " modifier: " .. modifier_name) + end + -- modifier apply code. + if modifier.exclusive == true then + -- if exclusive, reset the player's fov, and apply the new fov. + if modifier.is_multiplier then + player:set_fov(0, false, 0) + end + player:set_fov(modifier.fov_factor, modifier.is_multiplier, modifier.time) + else + -- not exclusive? let's apply it in the mix. + -- assume is_multiplier is true. + player:set_fov(modifier.fov_factor, true, modifier.time) + end end +function mcl_fovapi.remove_modifier(player, modifier_name) + if player == nil then + return + end + + if DEBUG then + local name = player:get_player_name() + minetest.log("FOV::Player: " .. name .. " modifier: " .. modifier_name .. "removed.") + end + + mcl_fovapi.applied_modifiers[player][modifier_name] = nil + + -- check for other fov modifiers, and set them up, or reset to default. + + local applied = {} + for k, _ in pairs(mcl_fovapi.applied_modifiers[player]) do + applied[k] = mcl_fovapi.registered_modifiers[k] + end + + if #applied == 0 then + return + end + local exc = false + for k in applied do + if applied[k].exclusive == true then + exc = applied[k] + break + end + end + + -- handle exclusives. + if exc ~= false then + player:set_fov(exc.fov_factor, exc.is_multiplier, 0) -- we want this to be immediate. + else + -- handle normal fov modifiers. + player:set_fov(0, false, 0) -- we want this to be immediate. + for x in applied do + player:set_fov(x.fov_factor, true, 0) + end + end + + if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then + mcl_fovapi.registered_modifiers[modifier_name].on_end(player) + end +end + +function mcl_fovapi.remove_all_modifiers(player) + if player == nil then + return + end + + if DEBUG then + local name = player:get_player_name() + minetest.log("FOV::Player: " .. name .. " modifiers have been reset.") + end + + for x in mcl_fovapi.applied_modifiers[player] do + x = nil + end + + player:set_fov(0, false, 0) + +end + +--[[ +Notes: +set_fov(fov, is_multiplier, transition_time): Sets player's FOV + + fov: FOV value. + is_multiplier: Set to true if the FOV value is a multiplier. Defaults to false. + transition_time: If defined, enables smooth FOV transition. Interpreted as the time (in seconds) to reach target FOV. + If set to 0, FOV change is instantaneous. Defaults to 0. + Set fov to 0 to clear FOV override. + +--]] From bf41e116a1dbfd71be37a8e5f8aedc6686214884 Mon Sep 17 00:00:00 2001 From: Michieal Date: Sun, 3 Dec 2023 08:10:41 -0500 Subject: [PATCH 03/21] Fleshed out the API Documentation. Modified missing pieces of code. --- mods/PLAYER/mcl_fovapi/api.md | 78 ++++++++++++++++++++++++++++----- mods/PLAYER/mcl_fovapi/init.lua | 9 ++-- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/api.md b/mods/PLAYER/mcl_fovapi/api.md index 05b8be29b..43a54de53 100644 --- a/mods/PLAYER/mcl_fovapi/api.md +++ b/mods/PLAYER/mcl_fovapi/api.md @@ -1,21 +1,79 @@ +### FOV API -registered_modifiers has defs of modifiers + + * [FOV API](#fov-api) + * [Description](#description) + * [Troubleshooting](#troubleshooting) + * [Modifier Definition](#modifier-definition-) + * [Global MCL_FOVAPI Tables](#global-mclfovapi-tables) + * [Namespaces](#namespaces) + * [Functions](#functions) + -applied_modifiers is indexed by player name +#### Description +This API defines and applies different Field Of View effects to players via MODIFIERS. -mcl_fovapi = {} +#### Troubleshooting +In the `init.lua` file for this module, there is a `DEBUG` variable at the top that will turn on logging. +Use it to see what is going on. -mcl_fovapi.default_fov = {} +#### Modifier Definition +```lua +def = { + modifer_name = name, + fov_factor = fov_factor, + time = time, + is_multiplier = is_multiplier, + exclusive = exclusive, + on_start = on_start, + on_end = on_end, +} +``` +* Modifier Name: The name of the Modifier, used to identify the specific modifier. Case sensitive. +* FOV Factor: A float value defining the FOV to apply. Can be an absolute or percentage, depending on Exclusive and + Is_Multiplier. +* Time: A float value defining the number of seconds to take when applying the FOV Factor. + Used to smoothly move between FOVs. Use 0 for an immediate FOV Shift. (Transition time.) +* Is Multiplier: A bool value used to specify if the FOV Factor is an absolute FOV value or if it should be a percentage + of the current FOV. Defaults to `true` if not defined. +* Exclusive: A bool value used to specify whether the modifier will override all other FOV modifiers. An example of this + is how the spy glass sets the FOV to be a specific value regardless of any other FOV effects applied. Defaults to + `false` if not defined. +* On Start: the `on_start` is a callback function `on_start(player)` that is called if defined. The parameter `player` + is a ref to the player that had the modifier applied. Called from `mcl_fovapi.apply_modifier` immediately after + the FOV Modifier has been applied. +* On End: the `on_end` is a callback function `on_end(player)` that is called if defined. The parameter `player` + is a ref to the player that had the modifier applied. Called from `mcl_fovapi.remove_modifier` immediately after + the FOV Modifier has been removed. -mcl_fovapi.registered_modifiers = {} +Note: passing incorrect values in the definition will have unintended consequences. -mcl_fovapi.applied_modifiers = {} +#### Global MCL_FOVAPI Tables +There are three tables that are accessible via the API. They are `registered_modifiers` and `applied_modifiers`. -function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end) +`mcl_fovapi.registered_modifiers` has the definitions of all the registered FOV Modifiers. Indexed by Modifier Name. +And, `mcl_fovapi.applied_modifiers` is indexed by the Player Name. It contains the names of all the modifiers applied to the +player. The `mcl_fovapi.default_fov` table is indexed by the Player Name, and contains the Default FOVs of the player from the +settings. (Expressed as a value usable in `player:set_fov`.) -function mcl_fovapi.apply_modifier(player, modifier_name) +#### Namespaces +`mcl_fovapi` is the default API Namespace. -function mcl_fovapi.remove_modifier(player, modifier_name) +#### Functions +`mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end)` -function mcl_fovapi.remove_all_modifiers(player, time) +Used to register a new FOV Modifier for use. Must be called before applying said modifier to a player. +See Modifier Definition for what the parameters are. +`mcl_fovapi.apply_modifier(player, modifier_name)` + +Used to apply a registered FOV modifier to a player. Takes a reference to the player and the modifier's name (string). + +`mcl_fovapi.remove_modifier(player, modifier_name)` + +Used to remove a specific FOV modifier from a Player. Takes a reference to the player and the modifier's name (string). +Removed immediately. + +`mcl_fovapi.remove_all_modifiers(player)` + +Used to remove all FOV modifiers from a Player. Takes a reference to the Player. FOV change is instantaneous. diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 649f582ab..ace5ca96f 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -27,23 +27,24 @@ minetest.register_on_joinplayer(function(player) end) --- clear when player leaves minetest.register_on_leaveplayer(function(player) local name = player:get_player_name() - -- Remove default FOV + + -- handle clean up mcl_fovapi.default_fov[name] = nil + mcl_fovapi.applied_modifiers[name] = nil end) function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end) if is_multiplier ~= true and is_multiplier ~= false then - is_multiplier = false + is_multiplier = true end if exclusive ~= true and exclusive ~= false then exclusive = false end local def = { modifer_name = name, - fov = fov_factor, + fov_factor = fov_factor, time = time, is_multiplier = is_multiplier, exclusive = exclusive, From 6cfb55e853c31bf30136c313c447ed063f88ade2 Mon Sep 17 00:00:00 2001 From: Michieal Date: Sun, 3 Dec 2023 08:24:21 -0500 Subject: [PATCH 04/21] Added reset for player respawning to remove FOV modifiers. Fixed missing on_end call in remove_all_modifiers. Added mcl_fovapi to Bows, Sprint, and Spyglass. --- mods/ITEMS/mcl_bows/mod.conf | 2 +- mods/ITEMS/mcl_spyglass/mod.conf | 2 +- mods/PLAYER/mcl_fovapi/init.lua | 7 +++++++ mods/PLAYER/mcl_sprint/mod.conf | 5 +++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mods/ITEMS/mcl_bows/mod.conf b/mods/ITEMS/mcl_bows/mod.conf index 7b174826a..0fdd666a3 100644 --- a/mods/ITEMS/mcl_bows/mod.conf +++ b/mods/ITEMS/mcl_bows/mod.conf @@ -1,6 +1,6 @@ name = mcl_bows author = Arcelmi description = This mod adds bows and arrows for MineClone 2. -depends = controls, mcl_particles, mcl_enchanting, mcl_init, mcl_util, mcl_shields +depends = controls, mcl_particles, mcl_enchanting, mcl_init, mcl_util, mcl_shields, mcl_fovapi optional_depends = awards, mcl_achievements, mcl_core, mcl_mobitems, playerphysics, doc, doc_identifier, mesecons_button diff --git a/mods/ITEMS/mcl_spyglass/mod.conf b/mods/ITEMS/mcl_spyglass/mod.conf index c13b281e1..6a78e86a5 100644 --- a/mods/ITEMS/mcl_spyglass/mod.conf +++ b/mods/ITEMS/mcl_spyglass/mod.conf @@ -1,4 +1,4 @@ name = mcl_spyglass author = NO11 description = This mod adds a spyglass, which is an item that can be used for zooming in on specific locations. -depends = mcl_core, controls +depends = mcl_core, controls, mcl_fovapi diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index ace5ca96f..153cd47a9 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -60,6 +60,10 @@ function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exc end +minetest.register_on_respawnplayer(function(player) + mcl_fovapi.remove_all_modifiers(player:get_player_name()) +end) + function mcl_fovapi.apply_modifier(player, modifier_name) if player == nil then return @@ -162,6 +166,9 @@ function mcl_fovapi.remove_all_modifiers(player) end player:set_fov(0, false, 0) + if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then + mcl_fovapi.registered_modifiers[modifier_name].on_end(player) + end end diff --git a/mods/PLAYER/mcl_sprint/mod.conf b/mods/PLAYER/mcl_sprint/mod.conf index 0d20f80a3..9b9a7366b 100644 --- a/mods/PLAYER/mcl_sprint/mod.conf +++ b/mods/PLAYER/mcl_sprint/mod.conf @@ -1,4 +1,5 @@ name = mcl_sprint author = GunshipPenguin -description = Allows the player to sprint by pressing the “Use” key (default: E). -depends = mcl_playerinfo, playerphysics, mcl_hunger +description = Allows the player to sprint by pressing the “AUX” key (default: E). +depends = mcl_playerinfo, playerphysics, mcl_hunger, mcl_fovapi +optional = mcl_bows \ No newline at end of file From 0a17bbe731766c38fca8e42253862556d61c1d28 Mon Sep 17 00:00:00 2001 From: Michieal Date: Sun, 3 Dec 2023 08:29:53 -0500 Subject: [PATCH 05/21] Added reset for player respawning to remove FOV modifiers. Fixed missing on_end call in remove_all_modifiers. Added mcl_fovapi to Bows, Sprint, and Spyglass. Set up the Spyglass to use the new FOV API. --- mods/ITEMS/mcl_spyglass/init.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_spyglass/init.lua b/mods/ITEMS/mcl_spyglass/init.lua index fa1a82339..ab557ebbf 100644 --- a/mods/ITEMS/mcl_spyglass/init.lua +++ b/mods/ITEMS/mcl_spyglass/init.lua @@ -17,6 +17,8 @@ minetest.register_craft({ } }) +mcl_fovapi.register_modifier("spyglass", 8, 0.1,false, true, nil, nil) + local spyglass_scope = {} local function add_scope(player) @@ -37,7 +39,8 @@ local function remove_scope(player) player:hud_remove(spyglass_scope[player]) spyglass_scope[player] = nil player:hud_set_flags({wielditem = true}) - player:set_fov(86.1) + mcl_fovapi.remove_modifier(player, "spyglass") -- use the api to remove the FOV effect. + -- old code: player:set_fov(86.1) end end @@ -55,7 +58,8 @@ controls.register_on_hold(function(player, key, time) if key ~= "RMB" then return end local wielditem = player:get_wielded_item() if wielditem:get_name() == "mcl_spyglass:spyglass" then - player:set_fov(8, false, 0.1) + mcl_fovapi.apply_modifier(player, "spyglass") -- apply the FOV effect. + -- old code: player:set_fov(8, false, 0.1) if spyglass_scope[player] == nil then add_scope(player) end From 36f661743e18a8f3c0c1ed540d22e6e9244b27f9 Mon Sep 17 00:00:00 2001 From: Michieal Date: Sun, 3 Dec 2023 08:57:13 -0500 Subject: [PATCH 06/21] Set up the Bows to use the new FOV API. Bows now zoom in and clear out the zoom. --- mods/ITEMS/mcl_bows/bow.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mods/ITEMS/mcl_bows/bow.lua b/mods/ITEMS/mcl_bows/bow.lua index 174208c3c..cfbdfb421 100644 --- a/mods/ITEMS/mcl_bows/bow.lua +++ b/mods/ITEMS/mcl_bows/bow.lua @@ -33,6 +33,9 @@ local bow_load = {} -- Another player table, this one stores the wield index of the bow being charged local bow_index = {} +-- define FOV modifier(s) +mcl_fovapi.register_modifier("bowcomplete", 0.8, 1, true, false, nil, nil) + function mcl_bows.shoot_arrow(arrow_item, pos, dir, yaw, shooter, power, damage, is_critical, bow_stack, collectable) local obj = minetest.add_entity({x=pos.x,y=pos.y,z=pos.z}, arrow_item.."_entity") if power == nil then @@ -183,6 +186,9 @@ end -- Resets the bow charging state and player speed. To be used when the player is no longer charging the bow local function reset_bow_state(player, also_reset_bows) + -- clear the FOV change from the player. + mcl_fovapi.remove_modifier(player, "bowcomplete") -- for the complete zoom in FOV Modifier. + bow_load[player:get_player_name()] = nil bow_index[player:get_player_name()] = nil if minetest.get_modpath("playerphysics") then @@ -314,6 +320,9 @@ controls.register_on_hold(function(player, key, time) end bow_load[name] = minetest.get_us_time() bow_index[name] = player:get_wield_index() + + -- begin Bow Zoom. + mcl_fovapi.apply_modifier(player, "bowcomplete") else if player:get_wield_index() == bow_index[name] then if type(bow_load[name]) == "number" then From 4f3f59f4bce65b7c42a51891dce27a84be1e3fcf Mon Sep 17 00:00:00 2001 From: Michieal Date: Sun, 3 Dec 2023 09:03:01 -0500 Subject: [PATCH 07/21] Put in checks to prevent repeatedly applying the same FOV modifier. Added short circuit to remove_modifier if the modifier is not currently applied. --- mods/PLAYER/mcl_fovapi/init.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 153cd47a9..b64a78d78 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -74,6 +74,9 @@ function mcl_fovapi.apply_modifier(player, modifier_name) if mcl_fovapi.registered_modifiers[modifier_name] == nil then return end + if mcl_fovapi.applied_modifiers[player][modifier_name] and mcl_fovapi.applied_modifiers[player][modifier_name] == true then + return + end local modifier = mcl_fovapi.registered_modifiers[modifier_name] if modifier.on_start ~= nil then @@ -110,6 +113,8 @@ function mcl_fovapi.remove_modifier(player, modifier_name) return end + if mcl_fovapi.applied_modifiers[player][modifier_name] == nil then return end + if DEBUG then local name = player:get_player_name() minetest.log("FOV::Player: " .. name .. " modifier: " .. modifier_name .. "removed.") From 2f8389d3f52c18304976987d018bf5d5304d5de9 Mon Sep 17 00:00:00 2001 From: Michieal Date: Fri, 8 Dec 2023 20:38:07 -0500 Subject: [PATCH 08/21] Put in check to prevent Trying to Reference a Nil error in Apply_Modifier. --- mods/PLAYER/mcl_fovapi/init.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index b64a78d78..de3dfa01a 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -74,8 +74,10 @@ function mcl_fovapi.apply_modifier(player, modifier_name) if mcl_fovapi.registered_modifiers[modifier_name] == nil then return end - if mcl_fovapi.applied_modifiers[player][modifier_name] and mcl_fovapi.applied_modifiers[player][modifier_name] == true then - return + if mcl_fovapi.applied_modifiers ~= nil and mcl_fovapi.applied_modifiers[player] ~= nil and mcl_fovapi.applied_modifiers[player][modifier_name] ~= nil then + if mcl_fovapi.applied_modifiers[player][modifier_name] and mcl_fovapi.applied_modifiers[player][modifier_name] == true then + return + end end local modifier = mcl_fovapi.registered_modifiers[modifier_name] @@ -113,7 +115,9 @@ function mcl_fovapi.remove_modifier(player, modifier_name) return end - if mcl_fovapi.applied_modifiers[player][modifier_name] == nil then return end + if mcl_fovapi.applied_modifiers[player][modifier_name] == nil then + return + end if DEBUG then local name = player:get_player_name() From 580a1caa387b1e20cc9937f993fd67a2fcbdae62 Mon Sep 17 00:00:00 2001 From: Michieal Date: Fri, 8 Dec 2023 21:00:11 -0500 Subject: [PATCH 09/21] Reworked some of the Apply_Modifier code to help prevent errors. --- mods/PLAYER/mcl_fovapi/init.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index de3dfa01a..debe1142e 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -74,18 +74,23 @@ function mcl_fovapi.apply_modifier(player, modifier_name) if mcl_fovapi.registered_modifiers[modifier_name] == nil then return end - if mcl_fovapi.applied_modifiers ~= nil and mcl_fovapi.applied_modifiers[player] ~= nil and mcl_fovapi.applied_modifiers[player][modifier_name] ~= nil then + if mcl_fovapi.applied_modifiers and mcl_fovapi.applied_modifiers[player] and mcl_fovapi.applied_modifiers[player][modifier_name] then if mcl_fovapi.applied_modifiers[player][modifier_name] and mcl_fovapi.applied_modifiers[player][modifier_name] == true then return end end + if mcl_fovapi.applied_modifiers[player] == nil then + mcl_fovapi.applied_modifiers[player] = {} + end + local modifier = mcl_fovapi.registered_modifiers[modifier_name] if modifier.on_start ~= nil then modifier.on_start(player) end mcl_fovapi.applied_modifiers[player][modifier_name] = true -- set the applied to be true. + if DEBUG then minetest.log("FOV::Player Applied Modifiers :" .. dump(mcl_fovapi.applied_modifiers[player])) end From a650f8b3681a83dd37f4076f6f023dccc17917a3 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sat, 9 Dec 2023 23:22:36 +0100 Subject: [PATCH 10/21] Made fovapi registration more robust --- mods/PLAYER/mcl_fovapi/init.lua | 51 ++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index debe1142e..4eac2b03d 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -35,28 +35,42 @@ minetest.register_on_leaveplayer(function(player) mcl_fovapi.applied_modifiers[name] = nil end) -function mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end) - if is_multiplier ~= true and is_multiplier ~= false then - is_multiplier = true +function mcl_fovapi.register_modifier(def) + if type(def.name) ~= "string" then + error("Modifier name must be a string") end - if exclusive ~= true and exclusive ~= false then - exclusive = false + if type(def.fov_factor) ~= "number" then + error("FOV factor must be a number") end - local def = { - modifer_name = name, - fov_factor = fov_factor, - time = time, - is_multiplier = is_multiplier, - exclusive = exclusive, - on_start = on_start, - on_end = on_end, - } + if type(def.time) ~= "number" then + error("Transition time must be a number") + end + + if def.on_start ~= nil and type(def.on_start) ~= "function" then + error("Callback on_start must be a function") + end + if def.on_end ~= nil and type(def.on_end) ~= "function" then + error("Callback on_end must be a function") + end + + local mdef = {} + + mdef.fov_factor = def.fov_factor + mdef.time = def.time + + if def.is_multiplier == false then mdef.is_multiplier = false + else mdef.is_multiplier = true end + if def.exclusive == true then mdef.exclusive = true + else mdef.exclusive = false end + + mdef.on_start = def.on_start + mdef.on_end = def.on_end if DEBUG then minetest.log("FOV::Modifier Definition Registered:\n" .. dump(def)) end - mcl_fovapi.registered_modifiers[name] = def + mcl_fovapi.registered_modifiers[def.name] = mdef end @@ -130,6 +144,7 @@ function mcl_fovapi.remove_modifier(player, modifier_name) end mcl_fovapi.applied_modifiers[player][modifier_name] = nil + local modifier = mcl_fovapi.registered_modifiers[modifier_name] -- check for other fov modifiers, and set them up, or reset to default. @@ -139,6 +154,7 @@ function mcl_fovapi.remove_modifier(player, modifier_name) end if #applied == 0 then + player:set_fov(0, false, modifier.time) return end local exc = false @@ -154,10 +170,11 @@ function mcl_fovapi.remove_modifier(player, modifier_name) player:set_fov(exc.fov_factor, exc.is_multiplier, 0) -- we want this to be immediate. else -- handle normal fov modifiers. - player:set_fov(0, false, 0) -- we want this to be immediate. + local fov_factor = 1 for x in applied do - player:set_fov(x.fov_factor, true, 0) + fov_factor = fov_factor * x.fov_factor end + player:set_fov(fov_factor, true, modifier.time) end if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then From 3a007e3bb1c1b4d1663a21c8784221beaa7af8e1 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:14:16 +0100 Subject: [PATCH 11/21] Re-registered FOV mods using new API version --- mods/ITEMS/mcl_bows/bow.lua | 7 ++++++- mods/ITEMS/mcl_spyglass/init.lua | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_bows/bow.lua b/mods/ITEMS/mcl_bows/bow.lua index cfbdfb421..aee5545ab 100644 --- a/mods/ITEMS/mcl_bows/bow.lua +++ b/mods/ITEMS/mcl_bows/bow.lua @@ -34,7 +34,12 @@ local bow_load = {} local bow_index = {} -- define FOV modifier(s) -mcl_fovapi.register_modifier("bowcomplete", 0.8, 1, true, false, nil, nil) +mcl_fovapi.register_modifier({ + name = "bowcomplete", + fov_factor = 0.8, + time = 1, + is_multiplier = true, +}) function mcl_bows.shoot_arrow(arrow_item, pos, dir, yaw, shooter, power, damage, is_critical, bow_stack, collectable) local obj = minetest.add_entity({x=pos.x,y=pos.y,z=pos.z}, arrow_item.."_entity") diff --git a/mods/ITEMS/mcl_spyglass/init.lua b/mods/ITEMS/mcl_spyglass/init.lua index ab557ebbf..a4ea144e1 100644 --- a/mods/ITEMS/mcl_spyglass/init.lua +++ b/mods/ITEMS/mcl_spyglass/init.lua @@ -17,7 +17,13 @@ minetest.register_craft({ } }) -mcl_fovapi.register_modifier("spyglass", 8, 0.1,false, true, nil, nil) +mcl_fovapi.register_modifier({ + name = "spyglass", + fov_factor = 8, + time = 0.1, + is_multiplier = false, + exclusive = true, +}) local spyglass_scope = {} From 7f5ce4e033924f2cf4085c2674ad17595b287356 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:15:09 +0100 Subject: [PATCH 12/21] Fixed modifier application and removal --- mods/PLAYER/mcl_fovapi/init.lua | 38 +++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 4eac2b03d..61fc244b7 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -123,8 +123,21 @@ function mcl_fovapi.apply_modifier(player, modifier_name) player:set_fov(modifier.fov_factor, modifier.is_multiplier, modifier.time) else -- not exclusive? let's apply it in the mix. - -- assume is_multiplier is true. - player:set_fov(modifier.fov_factor, true, modifier.time) + local fov_factor, is_mult = player:get_fov() + if fov_factor == 0 then + fov_factor = 1 + is_mult = true + end + if modifier.is_multiplier or is_mult then + fov_factor = fov_factor * modifier.fov_factor + else + fov_factor = (fov_factor + modifier.fov_factor) / 2 + end + if modifier.is_multiplier and is_mult then + player:set_fov(fov_factor, true, modifier.time) + else + player:set_fov(fov_factor, false, modififer.time) + end end end @@ -153,12 +166,13 @@ function mcl_fovapi.remove_modifier(player, modifier_name) applied[k] = mcl_fovapi.registered_modifiers[k] end - if #applied == 0 then + local elem = next + if elem(applied) == nil then player:set_fov(0, false, modifier.time) return end local exc = false - for k in applied do + for k, _ in pairs(applied) do if applied[k].exclusive == true then exc = applied[k] break @@ -171,10 +185,20 @@ function mcl_fovapi.remove_modifier(player, modifier_name) else -- handle normal fov modifiers. local fov_factor = 1 - for x in applied do - fov_factor = fov_factor * x.fov_factor + local non_multiplier_added = false + for _, x in pairs(applied) do + if not x.is_multiplier then + if non_multiplier_added then + fov_factor = (fov_factor + x.fov_factor) / 2 + else + non_multiplier_added = true + fov_factor = fov_factor * x.fov_factor + end + else + fov_factor = fov_factor * x.fov_factor + end end - player:set_fov(fov_factor, true, modifier.time) + player:set_fov(fov_factor, not non_multiplier_added, modifier.time) end if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then From 42ec62562daee88d685cd5c6e06b6860c2a1a5dc Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:15:33 +0100 Subject: [PATCH 13/21] Moved sprinting into the new FOV api --- mods/PLAYER/mcl_player/init.lua | 2 +- mods/PLAYER/mcl_sprint/init.lua | 42 ++++++++++----------------------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/mods/PLAYER/mcl_player/init.lua b/mods/PLAYER/mcl_player/init.lua index 288b697e1..f3cc782d5 100644 --- a/mods/PLAYER/mcl_player/init.lua +++ b/mods/PLAYER/mcl_player/init.lua @@ -177,7 +177,7 @@ minetest.register_on_joinplayer(function(player) player_textures[name] = { "character.png", "blank.png", "blank.png" } --player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30) - player:set_fov(86.1) -- see >>> +-- player:set_fov(86.1) -- see >>> end) minetest.register_on_leaveplayer(function(player) diff --git a/mods/PLAYER/mcl_sprint/init.lua b/mods/PLAYER/mcl_sprint/init.lua index 7449ad18c..3d9ef984c 100644 --- a/mods/PLAYER/mcl_sprint/init.lua +++ b/mods/PLAYER/mcl_sprint/init.lua @@ -64,40 +64,24 @@ local function cancelClientSprinting(name) players[name].clientSprint = false end +mcl_fovapi.register_modifier({ + name = "sprint", + fov_factor = 1.1, + time = 0.15, + is_multiplier = true, +}) + local function setSprinting(playerName, sprinting) --Sets the state of a player (0=stopped/moving, 1=sprinting) if not sprinting and not mcl_sprint.is_sprinting(playerName) then return end local player = minetest.get_player_by_name(playerName) - local controls = player:get_player_control() if players[playerName] then players[playerName].sprinting = sprinting - local fov_old = players[playerName].fov - local fov_new = fov_old - local fade_time = .15 - if sprinting == true - or controls.RMB - and string.find(player:get_wielded_item():get_name(), "mcl_bows:bow") - and player:get_wielded_item():get_name() ~= "mcl_bows:bow" then - if sprinting == true then - fov_new = math.min(players[playerName].fov + 0.05, 1.2) - else - fov_new = .7 - players[playerName].fade_time = .3 - end - if sprinting == true then - playerphysics.add_physics_factor(player, "speed", "mcl_sprint:sprint", mcl_sprint.SPEED) - end - elseif sprinting == false - and player:get_wielded_item():get_name() ~= "mcl_bows:bow_0" - and player:get_wielded_item():get_name() ~= "mcl_bows:bow_1" - and player:get_wielded_item():get_name() ~= "mcl_bows:bow_2" then - fov_new = math.max(players[playerName].fov - 0.05, 1.0) - if sprinting == false then - playerphysics.remove_physics_factor(player, "speed", "mcl_sprint:sprint") - end - end - if fov_new ~= fov_old then - players[playerName].fov = fov_new - player:set_fov(fov_new, true, fade_time) + if sprinting then + playerphysics.add_physics_factor(player, "speed", "mcl_sprint:sprint", mcl_sprint.SPEED) + mcl_fovapi.apply_modifier(player, "sprint") + else + playerphysics.remove_physics_factor(player, "speed", "mcl_sprint:sprint") + mcl_fovapi.remove_modifier(player, "sprint") end return true end From 5bf6608483d97994f387f8893de9caf2de24188b Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:20:43 +0100 Subject: [PATCH 14/21] Made bow unfocus faster --- mods/ITEMS/mcl_bows/bow.lua | 1 + mods/PLAYER/mcl_fovapi/init.lua | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_bows/bow.lua b/mods/ITEMS/mcl_bows/bow.lua index aee5545ab..6ae64a14e 100644 --- a/mods/ITEMS/mcl_bows/bow.lua +++ b/mods/ITEMS/mcl_bows/bow.lua @@ -38,6 +38,7 @@ mcl_fovapi.register_modifier({ name = "bowcomplete", fov_factor = 0.8, time = 1, + reset_time = 0.3, is_multiplier = true, }) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 61fc244b7..84817e382 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -45,6 +45,9 @@ function mcl_fovapi.register_modifier(def) if type(def.time) ~= "number" then error("Transition time must be a number") end + if def.reset_time ~= nil and type(def.reset_time) ~= "number" then + error("Reset time, if provided, must be a number") + end if def.on_start ~= nil and type(def.on_start) ~= "function" then error("Callback on_start must be a function") @@ -57,6 +60,7 @@ function mcl_fovapi.register_modifier(def) mdef.fov_factor = def.fov_factor mdef.time = def.time + mdef.reset_time = def.reset_time or def.time if def.is_multiplier == false then mdef.is_multiplier = false else mdef.is_multiplier = true end @@ -168,7 +172,7 @@ function mcl_fovapi.remove_modifier(player, modifier_name) local elem = next if elem(applied) == nil then - player:set_fov(0, false, modifier.time) + player:set_fov(0, false, modifier.reset_time) return end local exc = false @@ -198,7 +202,7 @@ function mcl_fovapi.remove_modifier(player, modifier_name) fov_factor = fov_factor * x.fov_factor end end - player:set_fov(fov_factor, not non_multiplier_added, modifier.time) + player:set_fov(fov_factor, not non_multiplier_added, modifier.reset_time) end if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then From 08241f6ea3cf866068456ba7941669fa59d241ba Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:26:21 +0100 Subject: [PATCH 15/21] Updated the api.md file --- mods/PLAYER/mcl_fovapi/api.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/api.md b/mods/PLAYER/mcl_fovapi/api.md index 43a54de53..5d49bc349 100644 --- a/mods/PLAYER/mcl_fovapi/api.md +++ b/mods/PLAYER/mcl_fovapi/api.md @@ -1,13 +1,13 @@ ### FOV API - * [FOV API](#fov-api) - * [Description](#description) - * [Troubleshooting](#troubleshooting) - * [Modifier Definition](#modifier-definition-) - * [Global MCL_FOVAPI Tables](#global-mclfovapi-tables) - * [Namespaces](#namespaces) - * [Functions](#functions) +* [FOV API](#fov-api) + * [Description](#description) + * [Troubleshooting](#troubleshooting) + * [Modifier Definition](#modifier-definition-) + * [Global MCL_FOVAPI Tables](#global-mclfovapi-tables) + * [Namespaces](#namespaces) + * [Functions](#functions) #### Description @@ -20,20 +20,24 @@ Use it to see what is going on. #### Modifier Definition ```lua def = { - modifer_name = name, + name = name, fov_factor = fov_factor, time = time, + reset_time = reset_time, is_multiplier = is_multiplier, exclusive = exclusive, on_start = on_start, on_end = on_end, } ``` -* Modifier Name: The name of the Modifier, used to identify the specific modifier. Case sensitive. +* Name: The name of the Modifier, used to identify the specific modifier. Case sensitive. * FOV Factor: A float value defining the FOV to apply. Can be an absolute or percentage, depending on Exclusive and Is_Multiplier. * Time: A float value defining the number of seconds to take when applying the FOV Factor. Used to smoothly move between FOVs. Use 0 for an immediate FOV Shift. (Transition time.) +* Reset Time: A float value defining the number of seconds to take when removing the FOV Factor. + Used to smoothly move between FOVs. Use 0 for an immediate FOV Shift. (Reset transition time.) + *If not provided, defaults to **Time*** * Is Multiplier: A bool value used to specify if the FOV Factor is an absolute FOV value or if it should be a percentage of the current FOV. Defaults to `true` if not defined. * Exclusive: A bool value used to specify whether the modifier will override all other FOV modifiers. An example of this @@ -60,7 +64,7 @@ settings. (Expressed as a value usable in `player:set_fov`.) `mcl_fovapi` is the default API Namespace. #### Functions -`mcl_fovapi.register_modifier(name, fov_factor, time, is_multiplier, exclusive, on_start, on_end)` +`mcl_fovapi.register_modifier(def)` Used to register a new FOV Modifier for use. Must be called before applying said modifier to a player. See Modifier Definition for what the parameters are. From f9b192e68f90a9b44f1b1b075a06906bdaca6cf3 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:30:04 +0100 Subject: [PATCH 16/21] Updated credits --- mods/PLAYER/mcl_fovapi/mod.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/mod.conf b/mods/PLAYER/mcl_fovapi/mod.conf index b78c78596..86f174c41 100644 --- a/mods/PLAYER/mcl_fovapi/mod.conf +++ b/mods/PLAYER/mcl_fovapi/mod.conf @@ -1,4 +1,4 @@ name = mcl_fovapi -author = Michieal +author = Michieal, Herowl description = An API for handling FOV changes. -depends = mcl_player \ No newline at end of file +depends = mcl_player From 040ce8288ee0ad22fbb7bedcac44acea581f239f Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Sun, 10 Dec 2023 00:34:31 +0100 Subject: [PATCH 17/21] Script and documentation cleanup --- mods/PLAYER/mcl_fovapi/api.md | 5 ++--- mods/PLAYER/mcl_fovapi/init.lua | 6 ------ 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/api.md b/mods/PLAYER/mcl_fovapi/api.md index 5d49bc349..d4a9cb1fb 100644 --- a/mods/PLAYER/mcl_fovapi/api.md +++ b/mods/PLAYER/mcl_fovapi/api.md @@ -37,7 +37,7 @@ def = { Used to smoothly move between FOVs. Use 0 for an immediate FOV Shift. (Transition time.) * Reset Time: A float value defining the number of seconds to take when removing the FOV Factor. Used to smoothly move between FOVs. Use 0 for an immediate FOV Shift. (Reset transition time.) - *If not provided, defaults to **Time*** + Defaults to `time` if not defined. * Is Multiplier: A bool value used to specify if the FOV Factor is an absolute FOV value or if it should be a percentage of the current FOV. Defaults to `true` if not defined. * Exclusive: A bool value used to specify whether the modifier will override all other FOV modifiers. An example of this @@ -57,8 +57,7 @@ There are three tables that are accessible via the API. They are `registered_mod `mcl_fovapi.registered_modifiers` has the definitions of all the registered FOV Modifiers. Indexed by Modifier Name. And, `mcl_fovapi.applied_modifiers` is indexed by the Player Name. It contains the names of all the modifiers applied to the -player. The `mcl_fovapi.default_fov` table is indexed by the Player Name, and contains the Default FOVs of the player from the -settings. (Expressed as a value usable in `player:set_fov`.) +player. #### Namespaces `mcl_fovapi` is the default API Namespace. diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 84817e382..19fa393c0 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -12,15 +12,10 @@ local pairs = pairs -- Globals mcl_fovapi = {} -mcl_fovapi.default_fov = {} -- Handles default fov for players mcl_fovapi.registered_modifiers = {} mcl_fovapi.applied_modifiers = {} minetest.register_on_joinplayer(function(player) - local name = player:get_player_name() - -- Assign default FOV - mcl_fovapi.default_fov[name] = player:get_fov() - if DEBUG then minetest.log("FOV::Player: " .. name .. "\nFOV: " .. player:get_fov()) end @@ -31,7 +26,6 @@ minetest.register_on_leaveplayer(function(player) local name = player:get_player_name() -- handle clean up - mcl_fovapi.default_fov[name] = nil mcl_fovapi.applied_modifiers[name] = nil end) From 98b6ead591ce8945e957b6d74a72adfff14ac0d7 Mon Sep 17 00:00:00 2001 From: Michieal Date: Mon, 11 Dec 2023 03:46:38 +0000 Subject: [PATCH 18/21] Fixed a couple of errors Removed a debug statement that was broken. Changed `modififer` to `modifier` in a code block. --- mods/PLAYER/mcl_fovapi/init.lua | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 19fa393c0..70eb5b81e 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -15,13 +15,6 @@ mcl_fovapi = {} mcl_fovapi.registered_modifiers = {} mcl_fovapi.applied_modifiers = {} -minetest.register_on_joinplayer(function(player) - if DEBUG then - minetest.log("FOV::Player: " .. name .. "\nFOV: " .. player:get_fov()) - end - -end) - minetest.register_on_leaveplayer(function(player) local name = player:get_player_name() @@ -134,7 +127,7 @@ function mcl_fovapi.apply_modifier(player, modifier_name) if modifier.is_multiplier and is_mult then player:set_fov(fov_factor, true, modifier.time) else - player:set_fov(fov_factor, false, modififer.time) + player:set_fov(fov_factor, false, modifier.time) end end From 5afd0aa255c43d471a803863ee1bb16cdb08e354 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Tue, 12 Dec 2023 01:45:07 +0100 Subject: [PATCH 19/21] Fixed the exclusive modifiers not being exclusive --- mods/PLAYER/mcl_fovapi/init.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index 70eb5b81e..c3d2dff4c 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -89,6 +89,10 @@ function mcl_fovapi.apply_modifier(player, modifier_name) mcl_fovapi.applied_modifiers[player] = {} end + for k, _ in pairs(mcl_fovapi.applied_modifiers[player]) do + if mcl_fovapi.registered_modifiers[k].exclusive == true then return end + end + local modifier = mcl_fovapi.registered_modifiers[modifier_name] if modifier.on_start ~= nil then modifier.on_start(player) From e312955a14df85dc090e33701fc936bef937c67b Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Wed, 13 Dec 2023 00:08:35 +0100 Subject: [PATCH 20/21] Made spyglass reset instant --- mods/ITEMS/mcl_spyglass/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/mods/ITEMS/mcl_spyglass/init.lua b/mods/ITEMS/mcl_spyglass/init.lua index a4ea144e1..56b71b961 100644 --- a/mods/ITEMS/mcl_spyglass/init.lua +++ b/mods/ITEMS/mcl_spyglass/init.lua @@ -21,6 +21,7 @@ mcl_fovapi.register_modifier({ name = "spyglass", fov_factor = 8, time = 0.1, + reset_time = 0, is_multiplier = false, exclusive = true, }) From 8a5058e0327c0096368f122daf64b4724a9b67e2 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Wed, 13 Dec 2023 00:29:22 +0100 Subject: [PATCH 21/21] Unified and refactored FOV API code --- mods/PLAYER/mcl_fovapi/init.lua | 71 ++++++++++++++++----------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/mods/PLAYER/mcl_fovapi/init.lua b/mods/PLAYER/mcl_fovapi/init.lua index c3d2dff4c..92815d833 100644 --- a/mods/PLAYER/mcl_fovapi/init.lua +++ b/mods/PLAYER/mcl_fovapi/init.lua @@ -15,11 +15,17 @@ mcl_fovapi = {} mcl_fovapi.registered_modifiers = {} mcl_fovapi.applied_modifiers = {} +minetest.register_on_joinplayer(function(player) + local player_name = player:get_player_name() + + -- initialization + mcl_fovapi.applied_modifiers[player_name] = {} +end) minetest.register_on_leaveplayer(function(player) - local name = player:get_player_name() + local player_name = player:get_player_name() -- handle clean up - mcl_fovapi.applied_modifiers[name] = nil + mcl_fovapi.applied_modifiers[player_name] = nil end) function mcl_fovapi.register_modifier(def) @@ -66,47 +72,38 @@ function mcl_fovapi.register_modifier(def) end minetest.register_on_respawnplayer(function(player) - mcl_fovapi.remove_all_modifiers(player:get_player_name()) + mcl_fovapi.remove_all_modifiers(player) end) function mcl_fovapi.apply_modifier(player, modifier_name) - if player == nil then - return - end - if modifier_name == nil then + if not player or not modifier_name then return end if mcl_fovapi.registered_modifiers[modifier_name] == nil then return end - if mcl_fovapi.applied_modifiers and mcl_fovapi.applied_modifiers[player] and mcl_fovapi.applied_modifiers[player][modifier_name] then - if mcl_fovapi.applied_modifiers[player][modifier_name] and mcl_fovapi.applied_modifiers[player][modifier_name] == true then - return - end + local player_name = player:get_player_name() + if mcl_fovapi.applied_modifiers and mcl_fovapi.applied_modifiers[player_name] and mcl_fovapi.applied_modifiers[player_name][modifier_name] then + return end - if mcl_fovapi.applied_modifiers[player] == nil then - mcl_fovapi.applied_modifiers[player] = {} - end - - for k, _ in pairs(mcl_fovapi.applied_modifiers[player]) do + for k, _ in pairs(mcl_fovapi.applied_modifiers[player_name]) do if mcl_fovapi.registered_modifiers[k].exclusive == true then return end end local modifier = mcl_fovapi.registered_modifiers[modifier_name] - if modifier.on_start ~= nil then + if modifier.on_start then modifier.on_start(player) end - mcl_fovapi.applied_modifiers[player][modifier_name] = true -- set the applied to be true. + mcl_fovapi.applied_modifiers[player_name][modifier_name] = true -- set the applied to be true. if DEBUG then - minetest.log("FOV::Player Applied Modifiers :" .. dump(mcl_fovapi.applied_modifiers[player])) + minetest.log("FOV::Player Applied Modifiers :" .. dump(mcl_fovapi.applied_modifiers[player_name])) end - local pname = player:get_player_name() if DEBUG then - minetest.log("FOV::Modifier applied to player:" .. pname .. " modifier: " .. modifier_name) + minetest.log("FOV::Modifier applied to player:" .. player_name .. " modifier: " .. modifier_name) end -- modifier apply code. @@ -138,26 +135,27 @@ function mcl_fovapi.apply_modifier(player, modifier_name) end function mcl_fovapi.remove_modifier(player, modifier_name) - if player == nil then + if not player or not modifier_name then return end - if mcl_fovapi.applied_modifiers[player][modifier_name] == nil then - return + local player_name = player:get_player_name() + if not mcl_fovapi.applied_modifiers[player_name] + or not mcl_fovapi.applied_modifiers[player_name][modifier_name] then + return end if DEBUG then - local name = player:get_player_name() - minetest.log("FOV::Player: " .. name .. " modifier: " .. modifier_name .. "removed.") + minetest.log("FOV::Player: " .. player_name .. " modifier: " .. modifier_name .. "removed.") end - mcl_fovapi.applied_modifiers[player][modifier_name] = nil + mcl_fovapi.applied_modifiers[player_name][modifier_name] = nil local modifier = mcl_fovapi.registered_modifiers[modifier_name] -- check for other fov modifiers, and set them up, or reset to default. local applied = {} - for k, _ in pairs(mcl_fovapi.applied_modifiers[player]) do + for k, _ in pairs(mcl_fovapi.applied_modifiers[player_name]) do applied[k] = mcl_fovapi.registered_modifiers[k] end @@ -196,30 +194,29 @@ function mcl_fovapi.remove_modifier(player, modifier_name) player:set_fov(fov_factor, not non_multiplier_added, modifier.reset_time) end - if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then + if mcl_fovapi.registered_modifiers[modifier_name].on_end then mcl_fovapi.registered_modifiers[modifier_name].on_end(player) end end function mcl_fovapi.remove_all_modifiers(player) - if player == nil then + if not player then return end + local player_name = player:get_player_name() if DEBUG then - local name = player:get_player_name() - minetest.log("FOV::Player: " .. name .. " modifiers have been reset.") + minetest.log("FOV::Player: " .. player_name .. " modifiers have been reset.") end - for x in mcl_fovapi.applied_modifiers[player] do + for name, x in pairs(mcl_fovapi.applied_modifiers[player_name]) do x = nil + if mcl_fovapi.registered_modifiers[name].on_end then + mcl_fovapi.registered_modifiers[name].on_end(player) + end end player:set_fov(0, false, 0) - if mcl_fovapi.registered_modifiers[modifier_name].on_end ~= nil then - mcl_fovapi.registered_modifiers[modifier_name].on_end(player) - end - end --[[