Help with Table

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

Help with Table

Post by TheSAguy »

Hi,

I'm in the process of incorporating albatrosv13's deevolution mod: https://forums.factorio.com/forum/vie ... =14&t=8863 ,into mine, but would like to make each successive temple only x% as effective as the previous one.
Let's say 90%. So 1 temple would give me 100% reduction on evolution. 2 temples would give me 190%. 3 would give me 271%.

Chlue describes it here: https://forums.factorio.com/forum/vie ... =10#p74597

Code: Select all

I fear it is still way overpowered if a lot of them are build. So an additional constraint like reducing the effectivity after each 'scan' and setting effectivity back to 100% after each scancyle might counter this.
-->
example with effectivity scale 90%:
1 temple: 100% --> 90%, reset --> not affected / 100% yield
2 temple: 100% --> 90% --> 81%, reset --> 190% yield
5 temple: 100% --> 90% --> 81% --> .... 59.05%, reset --> 410%  yield
10 temple: 100% --> 90% --> 81% --> .... 34.87%, reset --> 651%  yield
50 temple: 100% --> 90% --> ... 0.52%, reset --> 995%  yield
Ok not sure if this is understandable, but the idea is that each additional temple only provides 90% of the one build before. As a result the player can build as many as he wants but after about 10 the additional ones are mostly useless 

So I'm reading the temples into a table as I build them:

Code: Select all

game.onevent(defines.events.onbuiltentity, function(event) -- Temple has been built
  if event.createdentity.name == "templev2" then
    table.insert(glob.temples, event.createdentity)
  end
end)
But how do I now update the code below,to make index 2, 90% effectiveness of index 1 and index 3, 90% of 2?:

Code: Select all

game.onevent(defines.events.onsectorscanned, function(event)
  if event.radar.name == "templev2" then
      game.evolutionfactor = game.evolutionfactor - 0.0002
  end
end)
Thanks.
User avatar
ThaPear
Fast Inserter
Fast Inserter
Posts: 226
Joined: Fri May 30, 2014 8:05 am
Contact:

Re: Help with Table

Post by ThaPear »

This might work:

Code: Select all

game.onevent(defines.events.onsectorscanned, function(event)
  if event.radar.name == "templev2" then
    -- Loop though the temples.
    for i, temple in pairs(glob.temples) do
      -- Is the event's radar this temple?
      if temple.equals(event.radar) then
        -- It's the i-th temple, so multiply its efficiency by 0.9 ^ i.
        game.evolutionfactor = game.evolutionfactor - 0.0002 * math.pow(0.9, i)
        return
      end
    end
  end
end)
It's not very efficient though.

A (much) more efficient solution:

Code: Select all

game.oninit(function() OnLoad() end)
game.onload(function() OnLoad() end)

game.onevent(defines.events.onrobotbuiltentity, function(event) OnBuilt(event.createdentity) end)
game.onevent(defines.events.onbuiltentity, function(event) OnBuilt(event.createdentity) end)

game.onevent(defines.events.onpreplayermineditem, function(event) OnRemoved(event.entity) end)
game.onevent(defines.events.onrobotpremined, function(event) OnRemoved(event.entity) end)
game.onevent(defines.events.onentitydied, function(event) OnRemoved(event.entity) end)

function OnLoad()
	if not glob.numTemples then
		glob.numTemples = 0
	end
end

function OnBuilt(entity)
	-- Temple has been built
	if entity.name == "templev2" then
		glob.numTemples = glob.numTemples + 1
		
		glob.factormultiplier = GetFactorPerTemple(glob.numTemples)
	end
end

function OnRemoved(entity)
	if entity.name == "templev2" then
		glob.numTemples = glob.numTemples - 1
		
		glob.factormultiplier = GetFactorPerTemple(glob.numTemples)
	end
end

function GetFactorPerTemple(numTemples)
	local res = 1
	-- Calculate the total pollution reduction.
	for i = 1, numTemples do
		res = res + math.pow(0.9, i)
	end
	-- Return the pollution reduction per temple.
	return res / numTemples
end

game.onevent(defines.events.onsectorscanned, function(event)
  if event.radar.name == "templev2" then
	game.evolutionfactor = game.evolutionfactor - 0.0002 * glob.factormultiplier
  end
end)
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1455
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Help with Table

Post by TheSAguy »

Thanks ThaPear!
This is what I needed.
Post Reply

Return to “Modding help”