Jump to content

Lose Sanity from killing certain types of players


Recommended Posts

yeah that's entirely possible.

in combat.lua for example in line 417 you can find this

                if attacker ~= nil then
                    attacker:PushEvent("killed", { victim = self.inst })
                end
                if self.onkilledbyother ~= nil then
                    self.onkilledbyother(self.inst, attacker)
                end
            end

this is part of Combat:GetAttacked(attacker, damage, weapon, stimuli)

Sadly due to the structure of getAttacked() you're going to have to replace the entire function - I could be wrong tho :p

So what you want to add to your modmain.lua will look something like this:

--               name of your own function
local function GetAttackedPostInit(self)
	-- replace original function with your code
	self.GetAttacked = function(self, attacker)
            self.lastwasattackedtime = GetTime()

    		--print ("ATTACKED", self.inst, attacker, damage)
    		local blocked = false
    		local damageredirecttarget = self.redirectdamagefn ~= nil and self.redirectdamagefn(self.inst, attacker, damage, weapon, stimuli) or nil

    		self.lastattacker = attacker

    		if self.inst.components.health ~= nil and damage ~= nil and damageredirecttarget == nil then
        		if self.inst.components.inventory ~= nil then
            		damage = self.inst.components.inventory:ApplyDamage(damage, attacker, weapon)
        		end
        		if damage > 0 and not self.inst.components.health:IsInvincible() then
            		--Bonus damage only applies after unabsorbed damage gets through your armor
           			if attacker ~= nil and attacker.components.combat ~= nil and attacker.components.combat.bonusdamagefn ~= nil then
               			damage = damage + attacker.components.combat.bonusdamagefn(attacker, self.inst, damage, weapon) or 0
           		 	end
            		local cause = attacker == self.inst and weapon or attacker
            		self.inst.components.health:DoDelta(-damage, nil, cause ~= nil and (cause.nameoverride or cause.prefab) or "NIL", nil, cause)
           		 if self.inst.components.health:IsDead() then
               		 if attacker ~= nil then
                   		 attacker:PushEvent("killed", { victim = self.inst })
               		 end
                		if self.onkilledbyother ~= nil then
                            --this is where you want to adjust sanity.
                            -- I don't know how to adress sanity but inst.hasKilledPlayer will tell you whether the killer just killed an innocent man or not
                    		self.onkilledbyother(self.inst, attacker)
                		end
            		end
        		else
            		blocked = true
        		end
    		end

    		local redirect_combat = damageredirecttarget ~= nil and damageredirecttarget.components.combat or nil
    		if redirect_combat ~= nil then
        		redirect_combat:GetAttacked(attacker, damage, weapon, stimuli)
    		end

    		if self.inst.SoundEmitter ~= nil and not self.inst:IsInLimbo() then
        		local hitsound = self:GetImpactSound(damageredirecttarget or self.inst, weapon)
        		if hitsound ~= nil then
            		self.inst.SoundEmitter:PlaySound(hitsound)
        		end
        		if damageredirecttarget ~= nil then
            		if redirect_combat ~= nil and redirect_combat.hurtsound ~= nil then
                		self.inst.SoundEmitter:PlaySound(redirect_combat.hurtsound)
            		end
        		elseif self.hurtsound ~= nil then
            		self.inst.SoundEmitter:PlaySound(self.hurtsound)
        		end
    		end

    		if not blocked then
        		self.inst:PushEvent("attacked", { attacker = attacker, damage = damage, weapon = weapon, stimuli = stimuli, redirected=damageredirecttarget })

        		if self.onhitfn ~= nil then
            		self.onhitfn(self.inst, attacker, damage)
        		end

        		if attacker ~= nil then
            		attacker:PushEvent("onhitother", { target = self.inst, damage = damage, stimuli = stimuli, redirected=damageredirecttarget })
            		if attacker.components.combat ~= nil and attacker.components.combat.onhitotherfn ~= nil then
                		attacker.components.combat.onhitotherfn(attacker, self.inst, damage, stimuli)
            		end
        		end
    		else
        		self.inst:PushEvent("blocked", { attacker = attacker })
    		end

    		return not blocked
		end	
	end
end

--inject your code into combat.lua
AddComponentPostInit("combat", GetAttackedPostInit)

dont get confused or demoralized by the size of the function. You're only interested in the self.onkilledbyother condition.

Also don't use Tabs for indentaion use 4 spaces instead >_> now the code looks kinda messy cause I didnt D: and don't copy paste this. just copy paste the original function from the file or you're bound to get errors.

Edited by Andmann
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...