Page 1 of 1

[0.17.23] Assembly machine with a speed module switches status to low power

Posted: Sat Mar 30, 2019 5:34 pm
by kasandraen
Hi
I'm trying to make a machine that has both a burner fuel (a mold that's being used up) and electrical supply. To achieve this I create a dummy invisible assembly machine under the one I want and use it to drain electrical energy. Using control code I check if the machine has energy, if it has and its state is not low power, the original machine can run. If the original machine is crafting, the invisible machine is given an recipe that takes close to forever to finish to simulate power drain, and when the original machine stops crafting, so does the invisible one. So far everything works.
When I add a module to the original machine, it copies the module to the invisible one as well to also increase the electrical demand. Here is where the issue arises;

When my machine with 500kw power need gets any module that increases energy usage, its starts having the status change to status number 9, aka "defines.entity_status.low_power" despite it running fine

My control-code is as following: (entity is the original machine, Drainer is the machine to simulate electricity drain)

Code: Select all

log(serpent.block(Drainer.status))
if Drainer.status ~= defines.entity_status.low_power and Drainer.status ~= defines.entity_status.no_power then
		some code

else 	
		rendering.draw_text{text="Not enough power supplied", surface = "nauvis", target = entity,alignment ="center",color = {r=1,g=0,b=0},time_to_live = 61,target_offset = {0,-0.7}}	
		entity.active = false
end

if entity.is_crafting() and entity.status == defines.entity_status.working then
		
		Drainer.set_recipe("mold-crafting-invisble")
		--log(serpent.block(Drainer.get_recipe().ingredients[1].name)) 
		if Drainer.get_inventory(defines.inventory.assembling_machine_input).is_empty() then
			Drainer.get_inventory(defines.inventory.assembling_machine_input).insert({name = Drainer.get_recipe().ingredients[1].name,count = 1})
		end
		
		if entity.status == defines.entity_status.item_production_overload then
			Drainer.active = false
		else
			Drainer.active = true
		end
else	
		Drainer.get_inventory(defines.inventory.assembling_machine_input).clear()
		Drainer.get_inventory(defines.inventory.assembling_machine_output).clear()
		Drainer.set_recipe(nil)
end
	
	

And heres my log

Code: Select all

 335.939 Script @__KasTech_Metallurgy__/control.lua:73: 9
 336.439 Script @__KasTech_Metallurgy__/control.lua:73: 4
 336.939 Script @__KasTech_Metallurgy__/control.lua:73: 9
 337.439 Script @__KasTech_Metallurgy__/control.lua:73: 4
 337.938 Script @__KasTech_Metallurgy__/control.lua:73: 9
 338.438 Script @__KasTech_Metallurgy__/control.lua:73: 4
 338.939 Script @__KasTech_Metallurgy__/control.lua:73: 9
 339.438 Script @__KasTech_Metallurgy__/control.lua:73: 4
 339.939 Script @__KasTech_Metallurgy__/control.lua:73: 9
 340.439 Script @__KasTech_Metallurgy__/control.lua:73: 4
 340.938 Script @__KasTech_Metallurgy__/control.lua:73: 9

As you can see it has the status "low-power" which stops the original machine, which then causes the invisible machine to stop crafting untill it has no power problem again, then it turns on again and on and on it goes.

Attached is my mod, along with a save file where the testing is done.

Re: [0.17.23] Assembly machine with a speed module switches status to low power

Posted: Mon Apr 01, 2019 12:28 pm
by Rseding91
Thanks for the report. The problem is you're erasing and re-setting the modules in the inventory each time you run update. When you do that, it first removes the module from the inventory (which reduces the entities energy buffer size) then adds it back (which increases the entities energy buffer size). As a result the buffer is partially empty and your check reads "low energy".

You can fix it by not erasing and re-setting the modules each tick. Check if the contents are the same and if not then set and ignore the energy for that tick.

Re: [0.17.23] Assembly machine with a speed module switches status to low power

Posted: Tue Apr 02, 2019 8:59 am
by kasandraen
Ah that would explain it!
I had a lengthy discussion with someone on discord about how to deal with that and we kinda settled on there being more work(both writing it and in data time ingame) comparing the inventories than what I would be to just overwrite it, but now I see that it has a side-effect of messing with the power... hmm.
Thanks for telling me!