Altering the damage math?

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!
Post Reply
nichg
Posts: 48
Joined: Thu Oct 11, 2012 12:38 pm

Altering the damage math?

Post by nichg »

I'd like to do a bit of a redesign of items, skills, and stats, and for that I need to somehow get access to the engine's damage calculations. Specifically, what I want to do is make it so that different kinds of weapons vary in terms of a multiplier they apply to the relevant damage stat, rather than varying in their base damage. I figure there must be some way to do this kind of thing, because the skills like Heavy Weapons apply a percentage damage bonus to each hit.

For example, right now there's a sort of subtle multiplier effect going on where faster weapons effectively receive bigger bonuses for large stats, because they apply the stat more often during the DPS calculation. So e.g. a barbarian with super-high strength might well be better off wielding a Longsword than a Great Axe (in an extreme case, at 50 Strength, the Longsword does 2/3rds more damage before taking skills into account). Whether or not that's a good state of affairs is one thing, but it's a bit counter-intuitive since the Great Axe appears to have better stats (even if you calculate the DPS naively and factor in cooldown time but not the character's strength score).

What you could do to make this a bit more obvious would be to make it so that weapons automatically factor in different cooldown rates when applying damage from stats - so a weapon with a 7sec cooldown applies 2xStrength, whereas a weapon with 3.5sec cooldown applies 1xStrength. That's just one example of course. Other possibilities include things like giving damage bonuses against specific kinds of enemies (magic sword vs insects), adding magic items which have effects such as 'increase strength contribution to damage by x%', creating a mechanic whereby Dexterity gives you increased armor penetration even as a heavy weapons character, etc.

So I'd like to ask, does anyone know where these calculations are done by the engine, and is it somewhere that is accessible through scripting?
User avatar
Soaponarope
Posts: 180
Joined: Thu Oct 04, 2012 3:21 am

Re: Altering the damage math?

Post by Soaponarope »

Should be easy to do once asset pack is released, until then too hard to try to replicate AH's code.
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Altering the damage math?

Post by strangely »

Couldn't you redefine the skills using something similar to JKos's CloneObject script (CloneSkill) and add the hooks for onRecomputeStats, onComputeAccuracy, and onComputeCritChance?

I'm about to try this so I'll let you know how I get on...
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Altering the damage math?

Post by strangely »

OK, I got it working.
Here's my "CloneSkill" function which is basically a stripped-down version of JKos's "CloneObject" function.

Create a file called, "cloneSkill.lua".

Enter this as its contents:

Code: Select all

local olddefineSkill = defineSkill;
local defines = { };

defineSkill = function(data)
    defines[data.name] = data;
    olddefineSkill(data);
end

function table_merge(source, target)
    for key, val in pairs(source) do
        target[key] = val
    end
end

function cloneSkill(data)
    local baseObject = data.name
    local def = defines[baseObject]
    if def == nil then
        print("Def not found in " .. data.name)
    else

        table_merge(data, def)
        defineSkill(def)
    end
end

function modify_skill_cleanup()
    defines = nil
end
Add this line to the FIRST line of init.lua:
import "mod_assets/scripts/cloneSkill.lua"

Then, at the bottom of init.lua:

Code: Select all

cloneSkill
{
    name = "light_weapons",
    onRecomputeStats = function(champion, skillLevel)
        print("onRecomputeStats")
    end,
    onComputeAccuracy = function(champion, weapon, attack, attackType, skillLevel)
        print("onComputeAccuracy")
    end,
    onComputeCritChance = function(champion, weapon, attack, attackType, skillLevel)
        print("onComputeCritChance")
    end
}
By printing out the debug statements, you'll be able to see when each is called.
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Altering the damage math?

Post by strangely »

Hmm, this might not be solved as I thought it was.
I tried cloning the "light_weapons" skill and making it return 100 for both onComputeAccuracy and onComputeCritChance.
Now, both a branch (light weapon) and a cudgel (heavy weapon) always hit and always crit.

Not sure what's going on here. Need to investigate further...
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Altering the damage math?

Post by strangely »

I've managed to figure out that all overridden OnComputeAccuracy and OnComputeCritChance functions are called for all weapons, regardless of which skill the weapon uses.

I was able to work around this by checking weapon:hasTrait("light_weapon") for the "light_weapons" overrides, etc., but that seems like a hack to me.

Anyone know why all the functions are called each time?
nichg
Posts: 48
Joined: Thu Oct 11, 2012 12:38 pm

Re: Altering the damage math?

Post by nichg »

My understanding of how the skills work is that the hooks are used to allow skills to modify built-in variables like accuracy and crit chance at need. So you could e.g. make Alchemy give an Accuracy bonus if you want. If it only called the one skill, then that would mean that the skills are hardcoded in their function. To do something like make light weapons improve accuracy only when attacking with a light weapon, you probably need to implement that check yourself in the hook.

The issue with damage though is, I don't think there's a skill hook for it unless you want the skill to modify the champion's Strength score or something like that. But there are skills and items which apply percentage bonuses to damage dealt, so that's what's still confusing me - how do they do that without a hook?

I'm guessing the real answer is buried in 'champion' somewhere.
User avatar
strangely
Posts: 25
Joined: Mon Oct 29, 2012 2:28 am
Location: Tucson, AZ

Re: Altering the damage math?

Post by strangely »

Hmm, very interesting musings.
I'm looking forward to finding out what's going on.

Cheers!
Post Reply