Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFRPG: Fixes and improvements for grenade handling #34

Merged
merged 9 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions campaign/scripts/sfrpg_ammolist.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--
-- Please see the LICENSE.md file included with this distribution for
-- attribution and copyright information.
--
-- luacheck: globals getAmmoType onFilter onInit

function getAmmoType(itemNode)
local _, ammoType = AmmunitionManager.parseWeaponCapacity(DB.getValue(itemNode, "capacity", ""))
if ammoType:find('grenade') then
return 'Grenade'
end
return 'Ammunition'
end

local attackNode, itemNode, ammoSubtype

function onInit()
attackNode = window.parentcontrol.window.getDatabaseNode()
itemNode = AmmunitionManager.getShortcutNode(attackNode)
ammoSubtype = getAmmoType(itemNode)
end

function onFilter(w)
return w.subtype.getValue() == ammoSubtype and w.location.getValue() == ''
end
11 changes: 2 additions & 9 deletions campaign/scripts/sfrpg_char_reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
-- Please see the LICENSE.md file included with this distribution for attribution and copyright information.
--

local function parseWeaponCapacity(capacity)
capacity = capacity:lower()
if capacity == 'drawn' then return 0, capacity end
local splitCapacity = StringManager.splitWords(capacity)
return tonumber(splitCapacity[1]), splitCapacity[2]
end

local function getContainedItems(nodeContainer)
local containerName = ItemManager.getSortName(nodeContainer)
local containedItems = {}
Expand All @@ -25,7 +18,7 @@ end

local function loadCartridges(weaponActionNode, newAmmoNode, loadedAmmoNode)
local weaponInventoryNode = AmmunitionManager.getShortcutNode(weaponActionNode, 'shortcut')
local maxAmmo, _ = parseWeaponCapacity(DB.getValue(weaponInventoryNode, 'capacity', ''))
local maxAmmo, _ = AmmunitionManager.parseWeaponCapacity(DB.getValue(weaponInventoryNode, 'capacity', ''))
local currentAmmoCount = 0
if loadedAmmoNode then
currentAmmoCount = DB.getValue(loadedAmmoNode, 'count', 0)
Expand Down Expand Up @@ -56,7 +49,7 @@ local function moveInventoryAmmunition(weaponActionNode, newAmmoNode)
return newAmmoNode
end

local _, ammoType = parseWeaponCapacity(DB.getValue(weaponInventoryNode, 'capacity', ''))
local _, ammoType = AmmunitionManager.parseWeaponCapacity(DB.getValue(weaponInventoryNode, 'capacity', ''))
if ammoType == 'drawn' then return newAmmoNode end

if ammoType == 'charges' then
Expand Down
12 changes: 9 additions & 3 deletions campaign/scripts/sfrpg_char_weapon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,19 @@ function isThrownAttack()
return (string.find(sSpecial, 'thrown') and bRanged)
end

-- luacheck: globals isAmmoAutolinkable
function isAmmoAutolinkable()
return isThrownAttack() or DB.getValue(getDatabaseNode(), 'subtype', ''):lower():find('grenade')
end


-- luacheck: globals onInit
function onInit()
if not isThrownAttack() and not DB.getValue(getDatabaseNode(), 'subtype', ''):lower():find('grenade') then return end
if not isAmmoAutolinkable() then return end

local shortcutNode = getDatabaseNode().getChild('shortcut')
if not shortcutNode then return end

local ammoShortcuttNode = getDatabaseNode().getChild('ammoshortcut', shortcutNode.getType())
DB.copyNode(shortcutNode, ammoShortcuttNode)
local ammoShortcutNode = getDatabaseNode().createChild('ammoshortcut', shortcutNode.getType())
DB.copyNode(shortcutNode, ammoShortcutNode)
end
5 changes: 4 additions & 1 deletion campaign/scripts/sfrpg_crosslink_ammocounter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ function onValueChanged()

bLocked = true

DB.setValue(sLink, 'number', (getMaxValue() - getCurrentValue()) * AmmunitionManager.getWeaponUsage(window.getDatabaseNode()))
local currentAmmo = DB.getValue(sLink, 'number', 0)
local usage = AmmunitionManager.getWeaponUsage(window.getDatabaseNode())
local excess = math.fmod(currentAmmo, usage)
DB.setValue(sLink, 'number', math.min(excess + (getMaxValue() - getCurrentValue()) * usage, getMaxValue() * usage))

bLocked = false
end
Expand Down
8 changes: 2 additions & 6 deletions campaign/sfrpg_record_char_actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script file="common/scripts/number_crosslink_unlink.lua" />
<script>
function onDoubleClick()
if not window.isThrownAttack() then
if not window.isAmmoAutolinkable() then
Interface.openWindow('char_weapon_reload', window.getDatabaseNode())
return true
end
Expand Down Expand Up @@ -91,11 +91,7 @@
<child><backcolor>1A40301E</backcolor></child>
<datasource>...inventorylist</datasource>
<class>inv_ammoitem</class>
<script>
function onFilter(w)
return w.subtype.getValue() == 'Ammunition' and w.location.getValue() == ''
end
</script>
<script file="campaign/scripts/sfrpg_ammolist.lua" />
</windowlist>
<scrollbar>
<anchored to="ammunitionlist" />
Expand Down
8 changes: 8 additions & 0 deletions scripts/manager_ammunition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function getShortcutNode(node, shortcutName)
if sRecord and sRecord ~= '' then return DB.findNode(sRecord) end
end

-- luacheck: globals parseWeaponCapacity
function parseWeaponCapacity(capacity)
capacity = capacity:lower()
if capacity == 'drawn' then return 0, capacity end
local splitCapacity = StringManager.splitWords(capacity)
return tonumber(splitCapacity[1]), splitCapacity[2]
end

--- This function finds the correct node for a weapon's ammunition.
-- It first checks for a path saved in ammoshortcut. If found, databasenode record is returned.
-- If no path is found, it checks to see if the ammo name is known.
Expand Down