How can I modify player damage?

Place to get help with not working mods / modding interface.
Solinya
Long Handed Inserter
Long Handed Inserter
Posts: 79
Joined: Sun Mar 17, 2019 10:39 pm
Contact:

How can I modify player damage?

Post by Solinya »

I want to provide a non-technological bonus to player damage in my mod. Something like a 10%, 20%, ... 100% scaling increase to the player's manually fired weapons (shotguns, pistol, semi-machine gun, flamethrower, rockets, melee punching). This bonus should exclude "passive" (player doesn't press a button) damage effects like turrets, landmines, PLDs, etc. I'm indifferent on vehicle damage and grenades. This bonus is also per-player and not shared amongst the team.

There's a few approaches I've seen that I could attempt, but I'm wondering if there's an easier way:

One option seems to be create a variant of every type of weapon and bullet that deals slightly more damage and have the mod silently 'upgrade' all existing weaponry when the bonus is applied, but if I need a variant for everything that's a lot of prototype variants.

Another option would be to hook into the on_entity_damaged event and amplify damage from players there, but this isn't ideal either because this event fires after damage reduction has been calculated. So I'd need to undo the damage dealt, recalculate damage reduction with my bonus, and apply the new damage.

A third option might be create a non-researchable invisible tech similar to the military bonus techs and award that to the player, but I think this would violate the "bonuses aren't shared across the team" constraint if players are playing together.

Are there other options I'm overlooking?
AlienX
Fast Inserter
Fast Inserter
Posts: 105
Joined: Wed May 17, 2017 7:13 pm
Contact:

Re: How can I modify player damage?

Post by AlienX »

Hey,
I am new to modding, but have an idea.

Could you listen to the raised event "on_entity_damaged" (https://lua-api.factorio.com/latest/events.html), check the player against some global table for per-player damage values and then damage the target via code to make up for the difference?

Something like

Code: Select all


script.on_event(defines.events.on_entity_damaged, function(event)
	local ent = event.entity
	if ( ent.valid ) then
		-- Get the new heath of what the entity will be at
		local newHealth = ent.health-10
		
		-- if were at zero, or below zero - then kill the entity
		if ( newHeath <= 0 ) then
			ent.die()
		else
			-- otherwise, just set the heath of the thing
			ent.health = newHealth
		end
	end
end)
This'll work if you shoot anything not just aliens, so you'll have to check what the entity is first if you want to just make the players weapon affects biters, for example.
Solinya
Long Handed Inserter
Long Handed Inserter
Posts: 79
Joined: Sun Mar 17, 2019 10:39 pm
Contact:

Re: How can I modify player damage?

Post by Solinya »

on_entity_damaged is what I had mentioned in my second option. The challenge is this event is fired after damage is dealt, so it gets a little complicated if the damage bonus I provide would've pushed the original damage above the enemy's armor.

For example, based on the Damage Resistance formula, if a player deals 10 damage to an alien with 10 armor, the result is 1/2 and final_damage_amount will show 0.5 HP was subtracted. But if a player had a 10% damage bonus, then the calculation should have been (if there was a pre_entity_damaged) 11 damage vs 10 armor, for a final result of 1 damage.

In each case I'll need to calculate what I think the extra damage should be and adjust the target's HP accordingly and then pray this doesn't break interaction with other mods because I don't believe I can update the final_damage_amount passed to them.

I might ultimately have to go this route, but I was wondering if there was a simpler or safer alternative. Especially since there are a number of forum posts from Rseding saying this event is a performance risk.
Post Reply

Return to “Modding help”