About This File
Spoiler
local EQUIPSLOTS = GLOBAL.EQUIPSLOTS local TUNING = GLOBAL.TUNING ----------------------------- ----< Iron Hunter >---- ----< Crit Users >---- ----------------------------- --example -- inst.components.combat:SetCrit(1/6, nil, function(inst, target) -- --by default crits can cause explosions -- SpawnPrefab("explode_small").Transform:SetPosition(target.Transform:GetWorldPosition()) -- end) if TUNING.CRITENABLED == nil then TUNING.CRITENABLED = true AddComponentPostInit("combat", function(self)-- place in modmain self.SetCrit = function(self, _crit, _critdmg, _critfn)--don't use this on items only entities self.crit = _crit ~= nil and _crit or 0 --alternative is to inst.components.combat.crit self.critdmg = _critdmg ~= nil and _critdmg or 2--alternative is to inst.components.combat.critdmg self.critfn = _critfn ~= nil and _critfn or nil--alternative is to inst.components.combat.critfn end self.crit = 0 --default value should be 0 so mobs don't crit self.critdmg = 2 self.critfn = nil local OldCalcDamage = self.CalcDamage self.CalcDamage = function(self, target, weapon, ...) local old_damage = nil local crit = self.crit local critdmg = self.critdmg local critfns = {} if self.inst.components.inventory then local critgear = {} for k, v in pairs(EQUIPSLOTS)do table.insert(critgear, self.inst.components.inventory:GetEquippedItem(v))end for k,v in pairs(critgear)do if v.crit then crit = 1-((1-crit)*(1-v.crit)) end if v.critdmg then critdmg = 1-((1-critdmg)*(1-v.critdmg)) end if v.critfn then table.insert(critfns, v.critfn)end end end if crit > 0 and math.random() <= crit and target ~= nil then --change to your prefab if weapon then -- if its a weapon old_damage = weapon.components.weapon.damage weapon.components.weapon.damage = old_damage * critdmg else -- if we attack with something not using the weapon component old_damage = self.defaultdamage self.defaultdamage = old_damage * critdmg end -- visual effects and other stuff you can add --critfns can be applied to individual items as well as the user. --make custom effects when we crit, can also be used to make crit lifesteal, on crit effects etc. if self.critfn then self.critfn(self.inst, target, weapon, ...)end for k, v in pairs(critfns) do if v ~= nil then v(self, self.inst, target,weapon, ...)end end end local ret = OldCalcDamage(self, target, weapon, ...)-- returning crit damage if old_damage then-- reseting back to non crit damage if weapon then weapon.components.weapon.damage = old_damage else self.defaultdamage = old_damage end end return ret end end) end
This code is designed for Don't Starve Together to allow for critical hits and items with critical hit properties.
By design this allows all mobs the potential to be able to crit if they have crit chance greater than 0
Using inst.components.combat:SetCrit(critchance, critdmg, critfn) we can define a basic crit settings for a mob.
Currently equipment have crit variables as inst.variables rather than component ones.
Some fun examples if you have a hat that has crit chance, we can give it to a pigman and he'll be able to crit.
Currently tested on a caves enabled server and works as advertised.
Suggestions and bugs found for improvements welcome.
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now