Skip to content

Commit

Permalink
fix ammo field visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Nov 18, 2023
1 parent 48b37a8 commit d06a755
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 103 deletions.
168 changes: 66 additions & 102 deletions campaign/scripts/5e_char_weapon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,16 @@
-- attribution and copyright information.
--

-- luacheck: globals setAmmoVis maxammo.setLink
local function setAmmoVis()
local nodeWeapon = getDatabaseNode()
local bLoading = AmmunitionManager.hasLoadAction(nodeWeapon)
isloaded.setVisible(bLoading)

local nodeAmmoLink = AmmunitionManager.getAmmoNode(nodeWeapon)
local bRanged = AmmunitionManager.isWeaponRanged(nodeWeapon)
ammocounter.setVisible(bRanged and not nodeAmmoLink)
ammopicker.setComboBoxVisible(bRanged and nodeAmmoLink)
ammopicker.setComboBoxReadOnly(true)

local nodeCount
if nodeAmmoLink then nodeCount = DB.getChild(nodeAmmoLink, 'count') end
maxammo.setLink(nodeCount, nodeCount ~= nil)
end

-- luacheck: globals onDataChanged
function onInit()
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...");
DB.addHandler(nodeWeapon, "onChildUpdate", onDataChanged);
DB.addHandler(DB.getPath(nodeChar, "abilities.*.score"), "onUpdate", onDataChanged);
DB.addHandler(DB.getPath(nodeChar, "weapon.twoweaponfighting"), "onUpdate", onDataChanged);

onDataChanged(nodeWeapon);
-- luacheck: globals isLoading
function isLoading(nodeWeapon)
Debug.console('AmmunitionManager char_weapon isLoading - DEPRECATED - 2023-03-20 - Use AmmunitionManager.hasLoadAction')
return AmmunitionManager.hasLoadAction(nodeWeapon)
end

function onClose()
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...");
DB.removeHandler(nodeWeapon, "onChildUpdate", onDataChanged);
DB.removeHandler(DB.getPath(nodeChar, "abilities.*.score"), "onUpdate", onDataChanged);
DB.removeHandler(DB.getPath(nodeChar, "weapon.twoweaponfighting"), "onUpdate", onDataChanged);
end

-- luacheck: globals onLinkChanged
local m_sClass = "";
local m_sRecord = "";
-- luacheck: globals onLinkChanged
function onLinkChanged()
local node = getDatabaseNode();
local sClass, sRecord = DB.getValue(node, "shortcut", "", "");
Expand All @@ -56,50 +27,61 @@ function onLinkChanged()
end
end

-- luacheck: globals onAttackChanged onDamageChanged
function onDataChanged()
onLinkChanged();
onAttackChanged();
onDamageChanged();
-- luacheck: globals onDamageAction
function onDamageAction(draginfo)
local nodeWeapon = getDatabaseNode()
local nodeChar = DB.getChild(nodeWeapon, '...')

setAmmoVis()
end
-- Build basic damage action record
local rAction = CharWeaponManager.buildDamageAction(nodeChar, nodeWeapon)

-- luacheck: globals highlightAttack
function highlightAttack(bOnControl)
if bOnControl then
attackshade.setFrame("rowshade");
else
attackshade.setFrame(nil);
end
end
-- Perform damage action
local rActor = ActorManager.resolveActor(nodeChar)

function onAttackChanged()
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...")
-- Celestian adding itemPath to rActor so that when effects
-- are checked we can compare against action only effects
local _, sRecord = DB.getValue(nodeWeapon, 'shortcut', '', '')
rActor.itemPath = sRecord
-- end Adanced Effects piece ---

local nMod = CharWeaponManager.getAttackBonus(nodeChar, nodeWeapon);
-- bmos adding ammoPath for AmmunitionManager + Advanced Effects integration
-- add this in the onDamageAction function of other effects to maintain compatibility
if AmmunitionManager then
local nodeAmmo = AmmunitionManager.getAmmoNode(nodeWeapon, rActor)
if nodeAmmo then rActor.ammoPath = DB.getPath(nodeAmmo) end
end
-- end bmos adding ammoPath

attackview.setValue(nMod);
ActionDamage.performRoll(draginfo, rActor, rAction)
return true
end

-- luacheck: globals onAttackAction
function onAttackAction(draginfo)
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...")
-- luacheck: globals setAmmoVis maxammo.setLink
function setAmmoVis(nodeWeapon, ...)
if super and super.setAmmoVis then super.setAmmoVis(nodeWeapon, ...) end

-- Build basic attack action record
local rAction = CharWeaponManager.buildAttackAction(nodeChar, nodeWeapon);
local bLoading = AmmunitionManager.hasLoadAction(nodeWeapon)
isloaded.setVisible(bLoading)

-- Decrement ammo
if rAction.range == "R" then
CharWeaponManager.decrementAmmo(nodeChar, nodeWeapon);
end
local nodeAmmoLink = AmmunitionManager.getAmmoNode(nodeWeapon)
local bRanged = AmmunitionManager.isWeaponRanged(nodeWeapon)
ammocounter.setVisible(bRanged and not nodeAmmoLink)
ammopicker.setComboBoxVisible(bRanged and nodeAmmoLink)
ammopicker.setComboBoxReadOnly(true)

-- Perform action
local rActor = ActorManager.resolveActor(nodeChar);
local nodeCount
if nodeAmmoLink then nodeCount = DB.getChild(nodeAmmoLink, 'count') end
maxammo.setLink(nodeCount, nodeCount ~= nil)
end

-- AMMUNITION MANAGER CHANGES
-- luacheck: globals onDataChanged
function onDataChanged(nodeWeapon) self.setAmmoVis(nodeWeapon) end

local onAttackAction_old
local function onAttackAction_new(draginfo, ...)
local nodeWeapon = getDatabaseNode()
local nodeChar = DB.getChild(nodeWeapon, '...')
local rActor = ActorManager.resolveActor(nodeChar)
local nAmmo, bInfiniteAmmo = AmmunitionManager.getAmmoRemaining(rActor, nodeWeapon, AmmunitionManager.getAmmoNode(nodeWeapon))
local messagedata = { text = '', sender = rActor.sName, font = 'emotefont' }

Expand All @@ -110,6 +92,7 @@ function onAttackAction(draginfo)
if not bLoading or bIsLoaded then
if bInfiniteAmmo or nAmmo > 0 then
if bLoading then DB.setValue(nodeAmmoManager, 'isloaded', 'number', 0) end
return onAttackAction_old(draginfo, ...)
end
messagedata.text = Interface.getString('char_message_atkwithnoammo')
Comm.deliverChatMessage(messagedata)
Expand All @@ -119,48 +102,29 @@ function onAttackAction(draginfo)
local sWeaponName = DB.getValue(nodeWeapon, 'name', 'weapon')
messagedata.text = string.format(Interface.getString('char_actions_notloaded'), sWeaponName, true, rActor)
Comm.deliverChatMessage(messagedata)
return false
end
-- END AMMUNITION MANAGER CHANGES

ActionAttack.performRoll(draginfo, rActor, rAction);
return true;
-- end bmos only allowing attacks when ammo is sufficient
end

function onDamageChanged()
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...")

local sDamage = CharWeaponManager.buildDamageString(nodeChar, nodeWeapon);
function onInit()
if super and super.onInit then super.onInit() end

damageview.setValue(sDamage);
end
if super and super.onAttackAction then
onAttackAction_old = super.onAttackAction
super.onAttackAction = onAttackAction_new
end

-- luacheck: globals onDamageAction
function onDamageAction(draginfo)
local nodeWeapon = getDatabaseNode()
local nodeChar = DB.getChild(nodeWeapon, '...')

-- Build basic damage action record
local rAction = CharWeaponManager.buildDamageAction(nodeChar, nodeWeapon)
DB.addHandler(DB.getPath(nodeWeapon), 'onChildUpdate', onDataChanged)

-- Perform damage action
local rActor = ActorManager.resolveActor(nodeChar)
self.onDataChanged(nodeWeapon)
end

-- Celestian adding itemPath to rActor so that when effects
-- are checked we can compare against action only effects
local _, sRecord = DB.getValue(nodeWeapon, 'shortcut', '', '')
rActor.itemPath = sRecord
-- end Adanced Effects piece ---
function onClose()
if super and super.onClose then super.onClose() end

-- bmos adding ammoPath for AmmunitionManager + Advanced Effects integration
-- add this in the onDamageAction function of other effects to maintain compatibility
if AmmunitionManager then
local nodeAmmo = AmmunitionManager.getAmmoNode(nodeWeapon, rActor)
if nodeAmmo then rActor.ammoPath = DB.getPath(nodeAmmo) end
end
-- end bmos adding ammoPath
if super and super.onAttackAction then super.onAttackAction = onAttackAction_old end

ActionDamage.performRoll(draginfo, rActor, rAction)
return true
end
local nodeWeapon = getDatabaseNode()
DB.removeHandler(DB.getPath(nodeWeapon), 'onChildUpdate', onDataChanged)
end
2 changes: 1 addition & 1 deletion extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<loadorder>34</loadorder>
</properties>

<announcement text="https://github.com/bmos/FG-PFRPG-Ammunition-Manager\nAmmunition Manager v4.5:\nThis extension aids in tracking ammunition and whether ranged weapons are loaded." font="emotefont" icon="archery_ammomanager" />
<announcement text="https://github.com/bmos/FG-PFRPG-Ammunition-Manager\nAmmunition Manager v4.5-hotfix.1:\nThis extension aids in tracking ammunition and whether ranged weapons are loaded." font="emotefont" icon="archery_ammomanager" />

<base>
<!-- Campaign Records -->
Expand Down

0 comments on commit d06a755

Please sign in to comment.