How to stop turrets from firing without electricity?

Place to get help with not working mods / modding interface.
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Keysivi wrote: Thu Dec 26, 2024 8:04 am New batch of errors

In the case of the function 'insert_trigger' in data-final-fixes.lua, there are no more errors, but there was one in control.lua:

Code: Select all

__ElectricPoweredBelts__/control.lua:28: attempt to index global 'util' (a nil value)
stack traceback:
__ElectricPoweredBelts__/control.lua:28: in main chunk
[/code]
Woops, you need to put "local util = require("util")" (probably at the top of your file)
Keysivi wrote: Thu Dec 26, 2024 8:04 am In the case of the function 'get_trigger', the error seems to be the same, but now it points to 'insert_trigger'. As far as I understand, the problem is not only in the missing letter 'g'.

Code: Select all

Failed to load the following mods: __ElectricPoweredBelts__/data-final-fixes.lua:33: attempt to call global 'insert_trigger' (a nil value)
stack traceback:
__ElectricPoweredBelts__/data-final-fixes.lua:33: in main chunk
Did you not copy the insert trigger function from my first suggestion?
Keysivi wrote: Thu Dec 26, 2024 8:04 am I also took the liberty of replacing:

Code: Select all

return tbl[field][1]
end
with:

Code: Select all

end
return tbl[field][1]
It didn't change anything though...
Now why did you do that? (It shouldn't make any difference in this particular case, but I think it's more logical the way it was)
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

This is going to take ages, so I'll just give you a control.lua file that I've tested myself. Give me a few minutes
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Here you go, I've tested the flamethrower turret and it seems to work.

Change the line in data-final-fixes.lua that says 'if true' to 'if false' and reload Factorio to test the other trigger mechanism.
I also found that reducing the drain and buffer_capacity for the electric energy interfaces made testing easier.
And FYI, if you click the 'entities' button on the top-left of the map editor (the one that looks like an assembling machine), you can then select the hidden electric energy interface and see how much energy it's stored.

[*] I added local util = require("util") to the top of control.lua


I've made the following changes to data-final-fixes.lua:
  1. I added the loop that starts with 'for _, v in pairs{data.raw["artillery-turret"]["artillery-turret"],' to data-final-fixes.lua, it was adapted from the code you posted at the start of this thread with the following changes:
    • I removed the energy_per_shot = "8kJ" as that does nothing
    • I removed the local x you where passing to data:extend as I don't know why it was there
    • I removed energy_usage as it seems redundant with drain
    • I made the loop operate specifically on the artillery and flamethrower turrets
  2. I uncommented the insert_trigger function and put it before the first loop
  3. I merged the two "for _, ammo in pairs{data.raw["ammo"]["artillery-shell"]," loops into one by using a single 'if'

And the following changes to control.lua:
  1. Sadly I had to re-add the storage.entities table, as for some reason (despite it working in one of my mods), doing storage.power_enitities[event.entity] wasn't working. Now there is a 'local id = event.entity.unit_number' that takes the place of the old 'local pos' (unit_number is supposed to be globally unique, unless you create more than 4,294,967,295 entities...)
  2. Where you do 'event.entity.type' I change it to 'event.entity.name' (event.entity.type is the name of the prototype's type, whereas event.entity.name is the prototype's name, e.g. the flamethrower turret has type 'fluid-turret' but name 'flamethrower-turret')
  3. I've changed where it says "if event.entity.type == "artillery-shell"", to now say "if event.entity.name== "artillery-turret"" (artillery-shell is the ammo item! not the entity that fires the ammo).
  4. To the line starting with 'script.on_event({defines.events.on_entity_died' I added defines.events.on_space_platform_pre_mined (to handle the case where the turret was deconstructed by a space platform, instead of being destroyed by damage, by a robot, or the player).
  5. To the line starting with "if turret.disabled_by_script", I changed the 'if' to 'elseif', and added the following before it:

    Code: Select all

    if not turret.valid then
    	interface.destroy()
    	storage.power_entities[id] = nil
    	storage.entities[id] = nil 
    I think this is only necessary if you destroy the turret in the editor, as I couldn't find an event to catch such destructions.
I also deleted lots of newlines before 'end', "}", and ")"s, because I prefer the way it looks.

If you don't understand any of the code, let me know and I can try and explain!
Attachments
data-final-fixes.lua
(2.47 KiB) Downloaded 15 times
control.lua
(2.3 KiB) Downloaded 17 times
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Oh and I suggest changing the value of the refresh_rate to 1, and only increasing it if you get UPS drops.
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

Thank you very much! You have done a colossal job!

There is one small bug: If you install turrets through the editor, then an invisible entity "artillery-turret-power" or "flamethower-turret-power" is installed along with them, which remains in their place when the turrets are deleted.

This is not observed when installing turrets directly in the game.

I suspect that this is somehow related to:
5. To the line starting with "if turret.disabled_by_script", I changed the 'if' to 'elseif', and added the following before it:

Code: Select all

Code: Select all

if not turret.valid then
	interface.destroy()
	storage.power_entities[id] = nil
	storage.entities[id] = nil 
I think this is only necessary if you destroy the turret in the editor, as I couldn't find an event to catch such destructions.
I will also have to create two tables instead of your one. because this mod will need to be made compatible with this mod: https://mods.factorio.com/mod/Turrets-n ... rgy-source

And there are different parameters for "artillery-turret" and "flamethower-turret"
data.raw["fluid-turret"]["flamethrower-turret"].energy_source = {
type = "electric",
buffer_capacity = "1000kJ",
input_flow_limit = "200kW",
usage_priority = "primary-input",
drain = "20kW"
}
data.raw["fluid-turret"]["flamethrower-turret"].energy_per_shot = "10kJ"

data.raw["artillery-turret"]["artillery-turret"].energy_source = {
type = "electric",
buffer_capacity = "2000kJ",
input_flow_limit = "300kW",
usage_priority = "primary-input",
drain = "50kW"
}
data.raw["artillery-turret"]["artillery-turret"].energy_per_shot = "10MJ"
I think I can do it myself. But if you want, you can offer a better option.

And the last thing - the data "artillery-turret-power" or "flamethower-turret-power" is not displayed when selecting "artillery-turret" or "flamethower-turret" - is there a way to fix this?
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Keysivi wrote: Thu Dec 26, 2024 1:04 pm There is one small bug: If you install turrets through the editor, then an invisible entity "artillery-turret-power" or "flamethower-turret-power" is installed along with them, which remains in their place when the turrets are deleted.

This is not observed when installing turrets directly in the game.

I suspect that this is somehow related to:
5. To the line starting with "if turret.disabled_by_script", I changed the 'if' to 'elseif', and added the following before it:

Code: Select all

if not turret.valid then
	interface.destroy()
	storage.power_entities[id] = nil
	storage.entities[id] = nil 
I think this is only necessary if you destroy the turret in the editor, as I couldn't find an event to catch such destructions.
That code should destroy the power interface automatically (it will take at most refresh_rate ticks though). If it's not working though, my guess is you're using this on a save that had an old version of the mod, or things will be annoying. In which case I could write some code to automatically fix things if you want to keep using such saves.
Keysivi wrote: Thu Dec 26, 2024 1:04 pm I will also have to create two tables instead of your one. because this mod will need to be made compatible with this mod: https://mods.factorio.com/mod/Turrets-n ... rgy-source

And there are different parameters for "artillery-turret" and "flamethower-turret"
data.raw["fluid-turret"]["flamethrower-turret"].energy_source = {
type = "electric",
buffer_capacity = "1000kJ",
input_flow_limit = "200kW",
usage_priority = "primary-input",
drain = "20kW"
}
data.raw["fluid-turret"]["flamethrower-turret"].energy_per_shot = "10kJ"

data.raw["artillery-turret"]["artillery-turret"].energy_source = {
type = "electric",
buffer_capacity = "2000kJ",
input_flow_limit = "300kW",
usage_priority = "primary-input",
drain = "50kW"
}
data.raw["artillery-turret"]["artillery-turret"].energy_per_shot = "10MJ"
I think I can do it myself. But if you want, you can offer a better option.

I'm not sure what you mean by 'two' tables, but if you want the electric energy interfaces to have different properties for the different turrets, change the 'for _, v in pairs{data.raw["artillery-turret"]["artillery-turret"], data.raw["fluid-turret"]["flamethrower-turret"]} do'
loop to

Code: Select all

for v, energy in pairs{
 	[data.raw["artillery-turret"]["artillery-turret"]] =  {
		type = "electric",
		buffer_capacity = "800kJ", 
		input_flow_limit = "100kW",  
		usage_priority = "primary-input",
		drain = "10kW" },
	[data.raw["fluid-turret"]["flamethrower-turret"]] = {
		... }} do -- same as above, but you can use different values
And then below that where it had energy_source = { ... }, replace that with just energy_source = energy.
Keysivi wrote: Thu Dec 26, 2024 1:04 pm And the last thing - the data "artillery-turret-power" or "flamethower-turret-power" is not displayed when selecting "artillery-turret" or "flamethower-turret" - is there a way to fix this?
Hmm, making it selectable would be possible, the problem is that it's got the same bounding box as the turret, and so hovering your mouse may show only the turret or only the power information, but with no way for the user to choose which one.
If you make the bounding box different, you could have it so you can hover over a different part, to see the interface.
The downside is that this new smaller part will need to be within range of a power pole, and not the entire turret.
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Ok, to make the power selectable, try this. Immediately after the 'do' in the loop I mentioned in my previous commet, add

Code: Select all

local template = data.raw["electric-energy-interface"]["electric-energy-interface"]
Then in the call to data:extend delete the lines that say

Code: Select all

drawing_box = v.drawing_box,
selectable_in_game = false,
flags = {"player-creation", "not-deconstructable","not-blueprintable"},
And add these lines:

Code: Select all

flags = {"not-on-map", "player-creation", "not-deconstructable","not-blueprintable", "not-upgradable"},
gui_mode = "all", -- I'm guessing this makes a GUI show up when you click it?
selection_priority = (v.selection_priority or 50) + 1, -- so hovering the cursor over the power interface and turret will select the interface
selection_box = template.selection_box, -- should be smaller than the turrets
drawing_box_vertical_extension = template.drawing_box_vertical_extension,
picture = template.picture,
This will make it visible and selectable, you can change all the stuff that uses 'template' to give it a different appearance and/or size.
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

Unfortunately, the idea is unsuccessful.

Firstly, it became impossible to interact with the turret itself, for example, to manually charge it. Plus, it is now impossible to view the data of the turret itself.

Secondly, an image of a battery was superimposed on top of the turrets.

Probably, if everything is so complicated, we will have to return to the first option.
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Keysivi wrote: Thu Dec 26, 2024 5:23 pm Firstly, it became impossible to interact with the turret itself, for example, to manually charge it. Plus, it is now impossible to view the data of the turret itself.
the selection box is probably too big, try

Code: Select all

selection_box = {{-0.5, -0.5}, {0.5, 0.5}},
Or some other numbers?
Keysivi wrote: Thu Dec 26, 2024 5:23 pm Secondly, an image of a battery was superimposed on top of the turrets.
Yes, that was intentional, giving the user some visual feedbacck, if you don't want anything to show, just delete the lines

Code: Select all

drawing_box_vertical_extension = template.drawing_box_vertical_extension,
picture = template.picture,
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

Overall, I think it's satisfactory. I still nerfed 'buffer_capacity' and 'input_flow_limit'. They were shooting for too long.

I created the mod and packed it into an archive. Since in fact - you did all the work - I indicated you as the creator of the mod.

As for posting the mod on the portal - it's up to you:
You can post it yourself on your own behalf. Or, I can do it myself.

Thank you very much for everything!
Attachments
Electric_artillery_and_flamethrower_turrets_0.0.1.zip
(678.25 KiB) Downloaded 16 times
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Great work! I'll test it soon, in particular, I want to work out the difference between adding the flamethrower trigger to a 'Stream' TrigerDeliver, vs adding it to an 'instant' one.
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

IsaacOscar wrote: Fri Dec 27, 2024 6:31 am Great work! I'll test it soon, in particular, I want to work out the difference between adding the flamethrower trigger to a 'Stream' TrigerDeliver, vs adding it to an 'instant' one.
Good day! So how is the mod? Have you tried it yet?
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Keysivi wrote: Sun Dec 29, 2024 12:33 pm
IsaacOscar wrote: Fri Dec 27, 2024 6:31 am Great work! I'll test it soon, in particular, I want to work out the difference between adding the flamethrower trigger to a 'Stream' TrigerDeliver, vs adding it to an 'instant' one.
Good day! So how is the mod? Have you tried it yet?
Sorry! Was lazy, busy, and sick. And then I was working on my own mods (I now have 9!)
I'll look at it tomorrow!
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

IsaacOscar wrote: Sun Dec 29, 2024 12:36 pm Sorry! Was lazy, busy, and sick. And then I was working on my own mods (I now have 9!)
I'll look at it tomorrow!
Got it. Congratulations! Get well soon! Just wanted to remind you... What if you forgot?... After all, so much work has been done - it's a shame to abandon it....

I also have a small question about Unquality https://mods.factorio.com/mod/unquality . Am I right in understanding that the developers' idea with "Quality" is not the best. Because I paid attention to this mod, but I doubted - how relevant is it?

Thanks
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Ok, here you go.

I've made 3 major changes (other than correcting some bugs/potential-bugs):
  1. The electric energy interface now has what I consider a nicer name and position, and it should work with all languages (not just English and Russian):
    English
    Russian
  2. Flamethrower turrets now consume energy every time the stream hits an enemy, as opposed to everytime they start firing the stream. For example, this means if it starts firing but doesn't hit anything (e.g. the target moved away, or was killed by something else first), it won't consume any energy. But if the turret gets attacked by a swarm, and it sends a constant stream to kill them all, it will use a lot of energy, and not just one 'shot' worth (as it was doing before). If you have another idea for when it should use energy, let me know. (Personally, I think having it consume energy for every tick that it is shooting would be the best, but that will likely be performance heavy, and I'm not actually sure how to do that).
  3. You can now load the mod on existing saves, any turrets that were built before the mod was activated will now require electricity, just as if they were built after loading the mod.
Here is the detailed list of changes I made to your code:
Changes
As for publishing the mod, I'd prefer if you did it as it was you're idea, and I'm trying to only make mods for things I think should be in Factorio Vanilla.

And a completely unrelated thing I noticed when testing that it worked in Russian, the name of the space-age mod isn't translated to russian (although the description is), perhaps you can fix this on https://crowdin.com/project/factorio or post a bug report? (I don't know Russian so I don't know what it should say!).
Attachments
Electric_artillery_and_flamethrower_turrets_0.0.1.zip
(678.66 KiB) Downloaded 13 times
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Keysivi wrote: Sun Dec 29, 2024 3:10 pm I also have a small question about Unquality https://mods.factorio.com/mod/unquality . Am I right in understanding that the developers' idea with "Quality" is not the best. Because I paid attention to this mod, but I doubted - how relevant is it?
The reason I created the mod, is that I find space age already very complex, so I wanted to simplify it by simply removing the quality mechanic. However, the space age mod requires the Quality mod, and I want to keep the recycler (which is in the Quality mod).

I haven't actually properly played with quality though, but I've noticed people on the forums complaining about this:
  • Unintended extra click when setting up recipes due to the "Quality" dropdown. (11752)
  • Allow us to make rocket parts with quality components OR give as a method to lower quality of resources (120353, I've made a mod that fixes this!)
  • More Exciting Quality Effects (pls) (118785, basically there are many stats in the game that are not effected by quality, making many quality buildings practically useless)
  • Ability to craft lower quality with higher quality (116817, otherwise you need to build a whole smart recycling setup to really benefit from quality)
  • Have quality seeds grow quality plants (121647)
  • Output more fluid when using higher quality recipes (i.e. for Holmium Solution) (120829, for some reason you can use quality ingredients in a lot of fluid recipes, but it's a total waste)
  • Add more ways for quality to affect items (120398)
  • Tiles ignore quality (122146, putting quality tiles down destroys the quality!)
  • fish placed in water lose their quality (119548, you can't keep quality fish fresh in water, as they will lose their quality!)
  • (lots of people were also complaining about the names of the quality levels, even before it got released!, e.g. 108777, so I'm very surprised the developers didn't change them, maybe they just couldn't decide which set of names to change them to).
Hopefully 2.1 will come with some major changes to quality, and I might try it out then.
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

IsaacOscar wrote: Mon Dec 30, 2024 8:14 am As for publishing the mod, I'd prefer if you did it as it was you're idea, and I'm trying to only make mods for things I think should be in Factorio Vanilla.

And a completely unrelated thing I noticed when testing that it worked in Russian, the name of the space-age mod isn't translated to russian (although the description is), perhaps you can fix this on https://crowdin.com/project/factorio or post a bug report? (I don't know Russian so I don't know what it should say!).
It's Clear. Fine. I Will Publish this mod. Thank you very much for your work and your Help!

I will make adjustments in translation .

Happy Holiday to You! I wish you all the best in the New Year!
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

Hello! I found one nasty bug. Unfortunately, the mod does not work in case of modified shells.

It seems that the mod does not define ammo_category = "artillery-shell", but name = "artillery-shell". And such shells as name = "napalm-artillery-shell", name = "atomic-artillery-shell", name = "poison-artillery-shell" - the mod ignores.

Can I register these shells through "or"? For example, like this:

Code: Select all

for _, obj in pairs{data.raw["ammo"]["artillery-shell" or "napalm-artillery-shell" or "atomic-artillery-shell" or "poison-artillery-shell"].ammo_type, data.raw["stream"]["flamethrower-fire-stream"]} do
	local action = get_trigger(obj, "action", {type = "direct"})
	local action_delivery = get_trigger(action, "action_delivery", {type = "instant"})
	insert_trigger(action_delivery, "source_effects", {type = "script", effect_id = "turret-consume-energy"}) end
Or will it be necessary to write a more detailed script there?
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by IsaacOscar »

Keysivi wrote: Wed Jan 08, 2025 6:46 pm Hello! I found one nasty bug. Unfortunately, the mod does not work in case of modified shells.
Not technically a 'bug' as I was not trying to make it work with mods.
Keysivi wrote: Wed Jan 08, 2025 6:46 pm Can I register these shells through "or"? For example, like this:

Code: Select all

for _, obj in pairs{data.raw["ammo"]["artillery-shell" or "napalm-artillery-shell" or "atomic-artillery-shell" or "poison-artillery-shell"].ammo_type, data.raw["stream"]["flamethrower-fire-stream"]} do
	local action = get_trigger(obj, "action", {type = "direct"})
	local action_delivery = get_trigger(action, "action_delivery", {type = "instant"})
	insert_trigger(action_delivery, "source_effects", {type = "script", effect_id = "turret-consume-energy"}) end
No, "or" does not do that, what you wrote wi do exactly the same thing as before (see https://www.lua.org/manual/5.2/manual.html#3.4.4),
Keysivi wrote: Wed Jan 08, 2025 6:46 pm Or will it be necessary to write a more detailed script there?
Yes sadly. The easiest way is probably to use two loops:

Code: Select all

local ammos = {data.raw["stream"]["flamethrower-fire-stream"]}

for _, a in pairs(data.raw["ammo"]) do
	if a.ammo_category == "artillery-shell" then
		table.insert(ammos, a.ammo_type) end end

for _, obj in pairs(ammos) do
	-- the rest is the same code as before
local action = get_trigger(obj, "action", {type = "direct"})
	local action_delivery = get_trigger(action, "action_delivery", {type = "instant"})
	insert_trigger(action_delivery, "source_effects", {type = "script", effect_id = "turret-consume-energy"}) end
Keysivi
Fast Inserter
Fast Inserter
Posts: 109
Joined: Mon Feb 07, 2022 5:29 pm
Contact:

Re: How to stop turrets from firing without electricity?

Post by Keysivi »

IsaacOscar wrote: Thu Jan 09, 2025 12:20 pm
Keysivi wrote: Wed Jan 08, 2025 6:46 pm Hello! I found one nasty bug. Unfortunately, the mod does not work in case of modified shells.
Not technically a 'bug' as I was not trying to make it work with mods.
I see... Although it doesn't change the essence of the problem. I tested your modified code - it seems to work. I posted the mod on the forum at https://mods.factorio.com/mod/Electric_ ... om=updated

Thank you very much!

Just in case. I also made adjusted versions of the icons for the mod https://mods.factorio.com/mod/coloured- ... 53b87b7f77 . Take a look - maybe they will work?
Post Reply

Return to “Modding help”