berserker trait?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
Thorham
Posts: 80
Joined: Sat May 04, 2013 5:12 pm

Re: berserker trait?

Post by Thorham »

minmay wrote:The reason I didn't use onRecomputeStats is that onRecomputeStats hooks don't run if the champion doesn't have the trait.
Hadn't thought of that :oops:
minmay wrote:The onUnequipItem hook exists for a similar reason.
Those are easy to add, no problem.
minmay wrote:The timer/onRecomputeStats approach could be expanded to catch the items as they are unequipped via the mouse or quick weapon swap, since it can look at the mouse item and the champion's inventory (impossible to unequip AND drop an item on the same frame).
Never though of it, but an onUnequip hook seems much better anyway.
minmay wrote:If the trait can never be removed in your dungeon, then the timer is unnecessary and you can move it to an onRecomputeStats hook.p.quote]
That's exactly what I did.
minmay wrote:It already uses a ScriptComponent to store the weapon information...
I meant a script_entity in the editor ;)

Here's what I did (not well tested yet). It''s currently setup for melee and ranged weapons (throwing weapons, missile weapons and firearms are all ranged weapons in my mod):

This is added to every weapon using Notepad++'s search and replace in files with regex:

Code: Select all

onUnequipItem = function(self, champion, slot) traitHelper.script:unequip(champion, slot) end,
This goes into a skill (there's another one for ranged weapons):

Code: Select all

onRecomputeStats = function(champion, skillLevel)
    local adjust = 1 + skillLevel * 0.2
    local a = champion:getItem(ItemSlot.Weapon)
    local b = champion:getItem(ItemSlot.OffHand)

    traitHelper.script:updateMeleeWeapons(champion, adjust, a, b)
end
This is put into a script_entity in the editor:

Code: Select all

weaponPower = {}
weaponPower[ItemSlot.Weapon] = { -1, -1, -1, -1 }
weaponPower[ItemSlot.OffHand] = { -1, -1, -1, -1 }

function getWeaponType(item)
    if item then
        if item.go.meleeattack then
            return 1, item.go.meleeattack
        elseif item.go.throwattack then
            return 2, item.go.throwattack
        elseif item.go.rangedattack then
            return 2, item.go.rangedattack
        elseif item.go.firearmattack then
            return 2, item.go.firearmattack
        end
    end
    return 0, nil
end

function updateWeaponPower(self, champion, adjust, item, weaponType, store)
    local a = champion:getOrdinal()
    local b, c = getWeaponType(item)

    if b == weaponType then
        if store[a] == -1 then
            store[a] = c:getAttackPower()
        end
        c:setAttackPower(store[a] * adjust)
    end
end

function resetWeaponPower(self, champion, item, store)
    local a = champion:getOrdinal()
    local b, c = getWeaponType(item)

    if b > 0 and store[a] > -1 then
        c:setAttackPower(store[a])
        store[a] = -1
    end
end

function updateMeleeWeapons(self, champion, adjust, itemMain, itemOff)
    updateWeaponPower(self, champion, adjust, itemMain, 1, weaponPower[ItemSlot.Weapon])
    updateWeaponPower(self, champion, adjust, itemOff, 1, weaponPower[ItemSlot.OffHand])
end

function updateRangedWeapons(self, champion, adjust, itemMain, itemOff)
    updateWeaponPower(self, champion, adjust, itemMain, 2, weaponPower[ItemSlot.Weapon])
    updateWeaponPower(self, champion, adjust, itemOff, 2, weaponPower[ItemSlot.OffHand])
end

function unequip(self, champion, slot)
    if slot == ItemSlot.Weapon or slot == ItemSlot.OffHand then
        resetWeaponPower(self, champion, champion:getItem(slot), weaponPower[slot])
    end
end
Post Reply