[Solved] Check before Update

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Solved] Check before Update

Post by TheSAguy »

I have the below function that adds resistances to an entity type.
I'd like to modify it to only apply the change to resistances, IF the entities current resistance is lower that the resistance % I'm trying to add.

So let's take the standard inserter as an example. If I'm trying to add Physical resistance of 10% to the inserter, but it currently has 20%, is should not add my change. If it only currently has 5%, then implement the change.

This is the current function:

Code: Select all

-- Adds a resistance of all damage types to an entity
function NE_Functions.Add_ALL_Damage_Resists(Raw,Percent)
	if Raw ~= nil then	
		for k, v in pairs(data.raw["damage-type"]) do
		
			local Resist = {type = v.name, percent = Percent} -- or you could use k, and not v.name
			for i,d in pairs(Raw) do
				if d.resistances == nil then d.resistances = {} end
				table.insert(d.resistances, Resist)
			end
		end
	end
end
I tries something like the below, but it was complaining I'm trying to compare two table values:

Code: Select all

function NE_Functions.Add_ALL_Damage_Resists(Raw,Percent)
	if Raw ~= nil then	
		for k, v in pairs(data.raw["damage-type"]) do
		
			local Resist = {type = v.name, percent = Percent} -- or you could use k, and not v.name
			for i,d in pairs(Raw) do
				if d.resistances == nil then d.resistances = {} end
				if d.resistances < Resist then
					table.insert(d.resistances, Resist)
				end
			end
		end
	end
end
Last edited by TheSAguy on Wed Aug 10, 2016 3:07 am, edited 3 times in total.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

It won't work like this.

You need to expand the if section in the middle and also iterate through resistances.
A bit hard for me to write it up from memory exactly but should be like this:

Code: Select all

            if d.resistances == nil then
                 d.resistances = {}
                 table.insert(d.resistances, Resist)
            else
                 local found = false
                 for _, resistance in pairs(d.resistances) do
                      if resistance.type == Resist.type and resistance.percent < Resist.percent then
                             resistance.percent = Resist.percent
                             found = true
                             break
                      end
                 end
                 if not found then
                      table.insert(d.resistances, Resist)
                 end
            end
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

I stand corrected, not currently working, but I'll play around with it.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

TheSAguy wrote:I stand corrected, not currently working, but I'll play around with it.
Post me the code as it is and I can take a look on weekend if you can't get it to work.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

Okay, I have two functions, One I got working, the second still not working for me. Currently the second one is overriding the first.
The first only targets one specific inserter. The second All inserters.
First: Working

Code: Select all

for k, v in pairs(data.raw["damage-type"]) do
	local Resist = {type = v.name, percent = 70} -- or you could use k, and not v.name		
	
				if data.raw.inserter["combat-inserter"].resistances == nil then 
					data.raw.inserter["combat-inserter"].resistances = {}
					table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
				else
					local found = false
					for _, resistance in pairs(data.raw.inserter["combat-inserter"].resistances) do
						if resistance.type == Resist.type and resistance.percent > Resist.percent then
                            found = true
                            break
						elseif resistance.type == Resist.type and resistance.percent < Resist.percent then
                            resistance.percent = Resist.percent
                            found = true
							table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
                            break
						end
					end
                
					if not found then
					table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
					end
				
				end
	
end
Second:

Code: Select all

-- Adds a resistance of a single damage type to an entity
function NE_Functions.Add_Damage_Resists(D_Type,Raw,Percent)
	if data.raw["damage-type"][D_Type] ~= nil then
		local Resist = {type = D_Type, percent = Percent}
		for i,d in pairs(Raw) do
			if d.resistances == nil then 
				d.resistances = {}
				table.insert(d.resistances, Resist)
			else
				local found = false
				for _, resistance in pairs(d.resistances) do
					if resistance.type == Resist.D_Type and resistance.percent > Resist.Percent then
                           Resist.percent = resistance.percent 
						   found = true
                           break
					
					elseif resistance.type == Resist.D_Type and resistance.percent < Resist.Percent then
                            resistance.percent = Resist.percent
                            found = true
							table.insert(d.resistances, Resist)
                            break
					end
				end				
			
				if not found then
					table.insert(d.resistances, Resist)
				end
			
			end
		end
	end
end
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

I think I found the problem. My different modules were conflicting with each other. I have the same function in NE Expansion and was only updating the NE Enemies one... :oops:
Will test some more later.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

Okay, it was not the functions in the other mods fault. For some reason I just can't prevent the second function from overriding the first.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

TheSAguy wrote:Okay, it was not the functions in the other mods fault. For some reason I just can't prevent the second function from overriding the first.
Would it be per chance thatt you have the > and < there but there is no >= :D
So if it's already equal it will insert it anyway.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

Good point that I should add the "=", but that's not the problem.
In NE Enemies I add 12.5% resistance to acid to ALL inserters:
NE_Functions.Add_Damage_Resists("acid",data.raw["inserter"],(25/NE_Difficulty))
Using this:

Code: Select all

-- Adds a resistance of a single damage type to an entity
function NE_Functions.Add_Damage_Resists(D_Type,Raw,Percent)
	if data.raw["damage-type"][D_Type] ~= nil then
		local Resist = {type = D_Type, percent = Percent}
		for i,d in pairs(Raw) do
			if d.resistances == nil then 
				d.resistances = {}
				table.insert(d.resistances, Resist)
			else
				local found = false
				for _, resistance in pairs(d.resistances) do
					if resistance.type == Resist.D_Type and resistance.percent > Resist.Percent then
                           Resist.percent = resistance.percent 
						   found = true
						   table.insert(d.resistances, resistance.percent)
                           break
					
					elseif resistance.type == Resist.D_Type and resistance.percent < Resist.Percent then
                            resistance.percent = Resist.percent
                            found = true
							table.insert(d.resistances, Resist)
                            break
					end
				end				
			
				if not found then
					table.insert(d.resistances, Resist)
				end
			
			end
		end
	end
end
In NE buildings, I've got a new inserter, called the combat inserter. I'm trying to add 70% resistances to it, using:

Code: Select all

for k, v in pairs(data.raw["damage-type"]) do
	local Resist = {type = v.name, percent = 70} -- or you could use k, and not v.name		
	
	if data.raw.inserter["combat-inserter"].resistances == nil then 
		data.raw.inserter["combat-inserter"].resistances = {}
		table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
	else
		local found = false
		for _, resistance in pairs(data.raw.inserter["combat-inserter"].resistances) do
			if resistance.type == Resist.type and resistance.percent > Resist.percent then
                found = true
                break
			elseif resistance.type == Resist.type and resistance.percent < Resist.percent then
                    found = true
					table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
                    break
			end
		end
               
		if not found then
			table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
		end
			
	end
	
end
Since inserters already have fire of 90%, it's leaving that at 90% and giving everything else 70%. Except for Acid that's still at the 12.5% above.
I've tested ND Buildings alone and it works fine, adding to lower resistances and ignoring higher resistances. So the problem is with the NE Enemies function.
The only real difference is the NE Enemies looks at the "Type" and thus all inserters and NE Buildings targets a specific one.
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

First one is broken - it uses Resist.D_Type while it should be Resist.type in the if statements.
Both also might broken in different way - in section where resist is smaller it does the table.insert. It should change resistances.percent to new value. With inserts it will add another table to resistances and then in depends on which one will factorio use.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

orzelek wrote:First one is broken - it uses Resist.D_Type while it should be Resist.type in the if statements.
Both also might broken in different way - in section where resist is smaller it does the table.insert. It should change resistances.percent to new value. With inserts it will add another table to resistances and then in depends on which one will factorio use.
If I change D_Type to type I get an error:

Image

Code: Select all

function NE_Functions.Add_Damage_Resists(D_Type,Raw,Percent)
	if data.raw["damage-type"][D_Type] ~= nil then
		local Resist = {type = D_Type, percent = Percent}
		for i,d in pairs(Raw) do
			if d.resistances == nil then 
				d.resistances = {}
				table.insert(d.resistances, Resist)
			else
				local found = false
				for _, resistance in pairs(d.resistances) do
					if resistance.type == Resist.type and resistance.percent >= Resist.Percent then
                           --Resist.percent = resistance.percent 
						   found = true
						   --table.insert(d.resistances, resistance.percent)
                           break
					
					elseif resistance.type == Resist.type and resistance.percent < Resist.Percent then
                            resistance.percent = Resist.percent
                            found = true
							table.insert(d.resistances, Resist)
                            break
					end
				end				
			
				if not found then
					table.insert(d.resistances, Resist)
				end
			
			end
		end
	end
end
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

I think I see what might be going on.
There are resistance entries with percent and ones with decrease.
So we also need to make sure that we are looking at percent one by cehcking if resistance.percent actually exists. And don't touch the decrease ones.

It would amount to this:

Code: Select all

function NE_Functions.Add_Damage_Resists(D_Type,Raw,Percent)
	if data.raw["damage-type"][D_Type] ~= nil then
		local Resist = {type = D_Type, percent = Percent}
		for i,d in pairs(Raw) do
			if d.resistances == nil then 
				d.resistances = {}
				table.insert(d.resistances, Resist)
			else
				local found = false
				for _, resistance in pairs(d.resistances) do
					if resistance.type == Resist.type and resistance.percent then
						if resistance.percent < Resist.Percent then
							resistance.percent = Resist.percent
						end
						found = true
					end            
					
					if not found then
						table.insert(d.resistances, Resist)
					end
				end
			end
		end
	end
end
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

Just gave this a quick try. Getting same error as above. (Just line 32 this time. )

Code: Select all

 if resistance.percent < Resist.Percent then
"Attempt to compare nil with number"
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

TheSAguy wrote:Just gave this a quick try. Getting same error as above. (Just line 32 this time. )

Code: Select all

 if resistance.percent < Resist.Percent then
"Attempt to compare nil with number"
Thats because it should read:

Code: Select all

 if resistance.percent < Resist.percent then
Coding on "paper" is silly like that ;)
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

Okay, that fixed the error, but still no luck. I'm starting to distrust myself, so I've attached the two mods so you can look for yourself when you have a moment over the weekend.
I'll be out for the next few days.

The code above is in Natural_Evolution_Enemies_6.2.0\libs\NE_Functions.lua
This is called in Natural_Evolution_Enemies_6.2.0\data-updates.lua lines 79 and 90:
NE_Functions.Add_Damage_Resists("poison",data.raw["inserter"],(25/NE_Difficulty))
NE_Functions.Add_Damage_Resists("acid",data.raw["inserter"],(25/NE_Difficulty))


In Natural_Evolution_Buildings_6.2.0\data-updates.lua I have the below that works fine:

Code: Select all

-- Adds a resitance of all damage types to an entity
for k, v in pairs(data.raw["damage-type"]) do
	local Resist = {type = v.name, percent = 70} -- or you could use k, and not v.name		
	
				if data.raw.inserter["combat-inserter"].resistances == nil then 
					data.raw.inserter["combat-inserter"].resistances = {}
					table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
				else
				local found = false
				for _, resistance in pairs(data.raw.inserter["combat-inserter"].resistances) do
					if resistance.type == Resist.type and resistance.percent then
						if resistance.percent < Resist.percent then
							resistance.percent = Resist.percent
						end
						found = true
					end            
				
					if not found then
						table.insert(data.raw.inserter["combat-inserter"].resistances, Resist)
					end
				end
			end

	
end
Attachments
Natural_Evolution_Buildings_6.2.0.zip
NE B
(1.54 MiB) Downloaded 52 times
Natural_Evolution_Enemies_6.2.0.zip
NE E
(249.33 KiB) Downloaded 53 times
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Check before Update

Post by orzelek »

Fixed it up - there was a small error in the code that gave silly results.
Attached fixed versions.
Attachments
Natural_Evolution_Buildings_6.2.0.zip
(1.54 MiB) Downloaded 56 times
Natural_Evolution_Enemies_6.2.0.zip
(251.28 KiB) Downloaded 53 times
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Check before Update

Post by TheSAguy »

Thanks Orzelek,
I'm out on vacation the next 4 days, but will check it out when I get back.

EDIT: Works like a charm! Thanks Orzelek!
Post Reply

Return to “Modding help”