Skip to content

Commit

Permalink
fixes for 5e adding/removing weapons
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Oct 2, 2023
1 parent bc98162 commit 01201e9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 59 deletions.
171 changes: 113 additions & 58 deletions campaign/scripts/5e_char_weapon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,8 @@
-- attribution and copyright information.
--

-- luacheck: globals isLoading
function isLoading(nodeWeapon)
Debug.console('AmmunitionManager char_weapon isLoading - DEPRECATED - 2023-03-20 - Use AmmunitionManager.hasLoadAction')
return AmmunitionManager.hasLoadAction(nodeWeapon)
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)

-- Perform damage action
local rActor = ActorManager.resolveActor(nodeChar)

-- 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 ---

-- 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

ActionDamage.performRoll(draginfo, rActor, rAction)
return true
end

-- luacheck: globals setAmmoVis maxammo.setLink
function setAmmoVis(nodeWeapon, ...)
local function setAmmoVis(nodeWeapon, ...)
if super and super.setAmmoVis then super.setAmmoVis(nodeWeapon, ...) end

local bLoading = AmmunitionManager.hasLoadAction(nodeWeapon)
Expand All @@ -57,13 +22,85 @@ function setAmmoVis(nodeWeapon, ...)
end

-- luacheck: globals onDataChanged
function onDataChanged(nodeWeapon) self.setAmmoVis(nodeWeapon) end
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);

local onAttackAction_old
local function onAttackAction_new(draginfo, ...)
local nodeWeapon = getDatabaseNode()
local nodeChar = DB.getChild(nodeWeapon, '...')
local rActor = ActorManager.resolveActor(nodeChar)
onDataChanged(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 = "";
function onLinkChanged()
local node = getDatabaseNode();
local sClass, sRecord = DB.getValue(node, "shortcut", "", "");
if sClass ~= m_sClass or sRecord ~= m_sRecord then
m_sClass = sClass;
m_sRecord = sRecord;

local sInvList = DB.getPath(DB.getChild(node, "..."), "inventorylist") .. ".";
if sRecord:sub(1, #sInvList) == sInvList then
carried.setLink(DB.findNode(DB.getPath(sRecord, "carried")));
end
end
end

-- luacheck: globals onAttackChanged onDamageChanged
function onDataChanged(nodeWeapon)
onLinkChanged();
onAttackChanged();
onDamageChanged();

setAmmoVis(nodeWeapon)
end

-- luacheck: globals highlightAttack
function highlightAttack(bOnControl)
if bOnControl then
attackshade.setFrame("rowshade");
else
attackshade.setFrame(nil);
end
end

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

local nMod = CharWeaponManager.getAttackBonus(nodeChar, nodeWeapon);

attackview.setValue(nMod);
end

-- luacheck: globals onAttackAction
function onAttackAction(draginfo)
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...")

-- Build basic attack action record
local rAction = CharWeaponManager.buildAttackAction(nodeChar, nodeWeapon);

-- Decrement ammo
if rAction.range == "R" then
CharWeaponManager.decrementAmmo(nodeChar, nodeWeapon);
end

-- Perform action
local rActor = ActorManager.resolveActor(nodeChar);

-- AMMUNITION MANAGER CHANGES
local nAmmo, bInfiniteAmmo = AmmunitionManager.getAmmoRemaining(rActor, nodeWeapon, AmmunitionManager.getAmmoNode(nodeWeapon))
local messagedata = { text = '', sender = rActor.sName, font = 'emotefont' }

Expand All @@ -74,7 +111,6 @@ local function onAttackAction_new(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 @@ -84,29 +120,48 @@ local function onAttackAction_new(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 bmos only allowing attacks when ammo is sufficient
-- END AMMUNITION MANAGER CHANGES

ActionAttack.performRoll(draginfo, rActor, rAction);
return true;
end

function onInit()
if super and super.onInit then super.onInit() end
function onDamageChanged()
local nodeWeapon = getDatabaseNode();
local nodeChar = DB.getChild(nodeWeapon, "...")

if super and super.onAttackAction then
onAttackAction_old = super.onAttackAction
super.onAttackAction = onAttackAction_new
end
local sDamage = CharWeaponManager.buildDamageString(nodeChar, nodeWeapon);

damageview.setValue(sDamage);
end

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

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

function onClose()
if super and super.onClose then super.onClose() end
-- Perform damage action
local rActor = ActorManager.resolveActor(nodeChar)

-- 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 ---

if super and super.onAttackAction then super.onAttackAction = onAttackAction_old 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

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

<announcement text="https://github.com/bmos/FG-PFRPG-Ammunition-Manager\nAmmunition Manager v4.4:\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.4-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 01201e9

Please sign in to comment.