Page 1 of 1
[Solved] Check before Update
Posted: Thu Aug 04, 2016 6:43 pm
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
Re: Check before Update
Posted: Thu Aug 04, 2016 7:10 pm
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
Re: Check before Update
Posted: Thu Aug 04, 2016 7:53 pm
by TheSAguy
I stand corrected, not currently working, but I'll play around with it.
Re: Check before Update
Posted: Thu Aug 04, 2016 10:49 pm
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.
Re: Check before Update
Posted: Thu Aug 04, 2016 11:32 pm
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
Re: Check before Update
Posted: Fri Aug 05, 2016 12:01 am
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...
Will test some more later.
Re: Check before Update
Posted: Fri Aug 05, 2016 3:08 am
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.
Re: Check before Update
Posted: Fri Aug 05, 2016 4:11 pm
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 >=

So if it's already equal it will insert it anyway.
Re: Check before Update
Posted: Fri Aug 05, 2016 4:26 pm
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.
Re: Check before Update
Posted: Fri Aug 05, 2016 4:56 pm
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.
Re: Check before Update
Posted: Fri Aug 05, 2016 5:19 pm
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:
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
Re: Check before Update
Posted: Fri Aug 05, 2016 5:38 pm
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
Re: Check before Update
Posted: Fri Aug 05, 2016 8:49 pm
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"
Re: Check before Update
Posted: Fri Aug 05, 2016 9:28 pm
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

Re: Check before Update
Posted: Fri Aug 05, 2016 10:07 pm
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
Re: Check before Update
Posted: Sat Aug 06, 2016 1:29 pm
by orzelek
Fixed it up - there was a small error in the code that gave silly results.
Attached fixed versions.
Re: Check before Update
Posted: Sat Aug 06, 2016 11:38 pm
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!