Jump to content

Need help trying to make a throwable item apply increased damage to target


Recommended Posts

hopefully i posted this in the right place this time

Currently im working on a mod of The Sniper from Team Fortress 2, and he owns an item item named Jarate. In the game, Jarate is a thrown jar that splashes upon impact. When splashed on enemies, they take 35% more damage for a period of time. I'm trying to replicate this in Don't Starve Together and have used the Mad Milk from CheeseNuggets and Jack's Scout mod as a base.

The item appearing, being throwable and applying the 'jarate' color all worked as intended, but I can't seem to figure out how to get the 35% increased damage taken effect to work.

In modmain.lua, there's the damage mutiplier made by Xupalex but so far I haven't gotten that to work with Jarate either. Mostly because I have no idea what I am doing.

Basically what I tried doing was add a damage mutiplier onto someone covered in Jarate, as seen near line 47 of jarate.lua

        if data.attacker and data.attacker.components.health then
            data.attacker.components.health:DoDelta(data.damage / 2,false,data.weapon)
            data.attacker.components.combat:AddDamageModifier("jaratebonus",1.35)
        end
            end
        other:ListenForEvent("attacked", other.refunddamage, other)
        other.jaratetask = other:DoTaskInTime(TUNING.JARATED_DURATION, function()
        other:RemoveTag("jarated")
        other.components.combat:RemoveDamageModifier("jaratebonus")
            other.AnimState:SetAddColour(0,0,0,0)
        other:RemoveEventCallback("attacked", other.refunddamage, other)

What then happens is that I get the damage multiplier on the second hit, and then it doesn't go away.

Here's the .zip if anyone's willing to help me.

sniper.rar

Link to comment
Share on other sites

Rather than adding a damage modifer to the user wouldn't it make more sense adding a armor penalty to the mobs affected.

The problem I think I see is maybe the mob dies, before it can wear off, you could either add a additional listen for event on the mob for when it dies to remove the modifer. But then you got a wierd scenario where multiple mobs could have the modifer and do wierd stuff. Also listen for events are called after damage is calculated, so your current method will always be 1 attack behind.

Instead adding a armor penalty on hit to the mobs, will be ready immediately and you don't have to worry about listenforevents rather just a timer that'll remove it.

I added a modifed version of Zupalex combat component adjustments to include a armour modifer

0.25 armour means +25% armour, so to make them take increased damage you'll need to reduce armour -0.25

1/1.35 is roughly -.26 armour penalty

Spoiler

-------------------------------------------------
---- Combat Component Adjustments by ZupaleX ----
----  to add/remove damage modifier
----  modified by Iron_Hunter
----  to also add/remove armour modifier
-------------------------------------------------
local function AddDamageModifier(self, key, mod) self.attack_damage_modifiers[key] = mod end
--owner.components.combat:AddDamageModifier("brewinghatbonus",1.2) -- multiplies the damage with 1.2
local function RemoveDamageModifier(self, key) self.attack_damage_modifiers[key] = nil end
--owner.components.combat:RemoveDamageModifier("brewinghatbonus") -- remove the bons and only this bonus, no other boni.
local function AddArmourModifier(self, key, mod) self.armour_modifiers[key] = mod end
local function RemoveArmourModifier(self, key) self.armour_modifiers[key] = nil end
local function GetDamageModifier(self)
   local mod = 1
   for k,v in pairs(self.attack_damage_modifiers) do
     mod = mod+(v-1)
   end
   return mod
end
local function GetArmourModifier(self)
  local mod = 1
  for k,v in pairs(self.armour_modifiers) do
    mod = mod * (1-v)
  end
  return mod
end
local function RegisterDamageModifiers(self)
	self.attack_damage_modifiers = {} -- % modifiers on self:CalcDamage()
	self.AddDamageModifier = AddDamageModifier
	self.RemoveDamageModifier = RemoveDamageModifier
	self.GetDamageModifier = GetDamageModifier
end
local function RegisterArmourModifiers(self)
	self.armour_modifiers = {} -- % modifiers on self:GetAttacked()
	self.AddArmourModifier = AddArmourModifier
	self.RemoveArmourModifier = RemoveArmourModifier
	self.GetArmourModifier = GetArmourModifier
end
AddComponentPostInit("combat", function(self)
    if self.attack_damage_modifiers == nil then -- check if another mod already added this. cause we should not add this twice
        RegisterDamageModifiers(self)
        local CalcDamageOld = self.CalcDamage
        self.CalcDamage = function(self, target, weapon, multiplier)
           local dmg = CalcDamageOld(self, target, weapon, multiplier)
           local bonus = self.damagebonus or 0 --not affected by multipliers
           return (dmg-bonus) * self:GetDamageModifier() + bonus
        end
	end
	if self.armour_modifiers == nil then
		RegisterArmourModifiers(self)
		local oldGetAttacked = self.GetAttacked
		function self:GetAttacked(attacker, damage, weapon, stimuli)
			damage = damage * self:GetArmourModifier()
			return oldGetAttacked(self, attacker, damage, weapon, stimuli)
		end
    end
end)

 

Edited by IronHunter
Link to comment
Share on other sites

This is how I would of done it, I am tight for time atm I have to head out again. But I'll check back later:

Spoiler

local function applyjarate(inst, target)
	if target == nil then return end
    if target.components.inventoryitem ~= nil then target.components.inventoryitem:AddMoisture(20)end
	target:AddTag("jarated") --do you really need this tag?
	if target and target.AnimState then target.AnimState:SetAddColour(.5,.5,0,0)end
	target.components.combat:AddArmourModifier("jaratebonus", -0.26) --1/1.35 or about 35% more damage taken.
	target.jaratetask = target:DoTaskInTime(TUNING.JARATED_DURATION, function(target)
		target:RemoveTag("jarated") --do you really need this tag?
		target.components.combat:RemoveArmourModifier("jaratebonus")
		target.AnimState:SetAddColour(0,0,0,0)
	end)
end
local function OnHitJarate(inst, attacker, target)
	local x, y, z = inst.Transform:GetWorldPosition()
    SpawnPrefab("waterballoon_splash").Transform:SetPosition(x, y, z)
	inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
	inst.components.wateryprotection:SpreadProtection(inst)
	for i, ent in pairs(TheSim:FindEntities(x, y, z, 5, {"_combat", "_health"}, "structure")) do
	--only apply jarate to entities with combat and health components tags, doesn't apply to structures
		if ent ~= attacker and (TheNet:GetPVPEnabled() or not ent:HasTag("player")) then
		--if pvp is enabled then we apply to everybody, if pvp is off we apply to nonplayers. We never apply to our owner
			applyjarate(inst, ent)
		end
	end
    inst:Remove()
end

 

 

I am almost certain I forgot a bracket, or a typo. But this is a loose structure I whipped up with the time I have on my break.

jarate.lua

Edited by IronHunter
Link to comment
Share on other sites

5 hours ago, Danielance said:

It works great! Thanks a lot for all your help.

local function applyjarate(inst, target)
	if target == nil then return end
    if target.components.inventoryitem ~= nil then target.components.inventoryitem:AddMoisture(20)end
	target:AddTag("jarated") --do you really need this tag?
	if target.jaratetask ~= nil then target.jaratetask:Cancel() end
	if target and target.AnimState then target.AnimState:SetAddColour(.5,.5,0,0)end
	target.components.combat:AddArmourModifier("jaratebonus", -0.26) --1/1.35 or about 35% more damage taken.
	target.jaratetask = target:DoTaskInTime(TUNING.JARATED_DURATION, function(target)
		target:RemoveTag("jarated") --do you really need this tag?
		target.components.combat:RemoveArmourModifier("jaratebonus")
		target.AnimState:SetAddColour(0,0,0,0)
		target.jaratetask = nil
	end)
end

Minor update, so your jarate extends duration, so we prevent early wearing off of jarate when applying it on existing jarated mobs.

Haven't tested it but it seems fine

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...