Help with scenario & lua script

This is the place to share custom user maps, scenarios, and campaigns.
Post Reply
User avatar
LotA
Fast Inserter
Fast Inserter
Posts: 117
Joined: Fri Oct 10, 2014 11:41 am
Contact:

Help with scenario & lua script

Post by LotA »

Hi everyone,
First of all, sorry if I created the subject in the wrong subforum, i'm new here :d (even though i played factorio for hundreds of hours :oops: )

I'm creating a map (a custom scenario), something huge and i'm loosing my mind puting coal in each of my 5600 boilers and burner inserter... (yeah, that's a lot xD)

I still can't make sense of lua language (I swear i'm trying to ^^) but i've already seen functions that should help me do that. Here's my pseudocode :
at the start of map (game.oninit ?) get a list of entities containing my boilers and inserters (game.findentities ?) then do a for each loop on this list that will test if the entity is a boiler or an burner inserter and then insert a stack of coal inside them

I'm sure this is actually quite a simple script to write so i'm asking if someone's nice enough could write it for me :d (or at least give me scripts examples so i can study lua & factorio) and tell me if i can put it in a file that would be loaded when I start my map, I'm sure I read it's possible on the wiki but i couldn't find how to name the file and where to place it (in the map folder next to the blueprint.dat ?)

Thanks in advance for who bothers answering me :)

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Help with scenario & lua script

Post by prg »

Try this:

Code: Select all

require("defines")
game.onevent(defines.events.onchunkgenerated, function(event)
    a = game.findentitiesfiltered{name="burner-inserter", area=event.area}
    for _, v in ipairs(a) do
        v.insert{name="coal", count=5}
    end
    a = game.findentitiesfiltered{name="boiler", area=event.area}
    for _, v in ipairs(a) do
        v.insert{name="coal", count=5}
    end
end)
This should put five coal in all the boilers and burner inserters. onchunkgenerated seems to be called for all chunks when a new game is started so this was the easiest way to cover the whole area. At least it worked on the map I tried. And yes, you put that in control.lua next to blueprint.dat.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
LotA
Fast Inserter
Fast Inserter
Posts: 117
Joined: Fri Oct 10, 2014 11:41 am
Contact:

Re: Help with scenario & lua script

Post by LotA »

That's working like a charm !
I kneel to my savior ! Thanks prg :)

I have an other question : once the game is saved, is the script somehow saved within the save files or does it get it from it's original path (ie. the scenario folder) ?
I mean, if i change the script in the future, will it affect older saves of the map or will i have to re-launch the map to get the updated scripts?
I know it won't change anything regarding to my actual script, but considering actual "scenarios", with "in-game events" how does it works?

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Help with scenario & lua script

Post by prg »

Scripts are copied into the save game, so a change to the original shouldn't have any effect. But this script only does something useful for newly started games, since in already running games newly generated chunks won't contain any boilers or inserters.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
LotA
Fast Inserter
Fast Inserter
Posts: 117
Joined: Fri Oct 10, 2014 11:41 am
Contact:

Re: Help with scenario & lua script

Post by LotA »

Yep, indeed. Thanks again.

I've been reading lua doc for quite a long part of the night, seems to be quite a interesting langage. I managed to get a few functions working, i should manage to write the scripts by myself from now on but I still have some technical questions :

Is it possible for a script to create a file and write data in it ? It seems the io lib isn't there..
I'd like to export a list of entitits & their coordinates to a file so i could read it from another map,
let's say I would like to create a script (actually 2) that "copy-paste"a set of entities over different maps (using relative coordinates)

Also, I saw that control.lua and a script.dat (which i guess represent the current state of scripts) are stored in the save zip.
When you use the map2scenario functionalty, the generated scenario folder contains the script.dat but no control.lua.
Is the script source "lost" in the process, does the script.dat contains all the needed datas?
So if I launch a scenario generated by map2scenario will the scripts be still there and "resumed" to their state (ie. the state they had when the game was saved?)
My point is : can I reset/delete any scripts from a savegame? As my scriprs are for editing purpose, i'd like to get rid of all of them once the game is launched (and when i won't edit anything anymore via scripts) : i want a clean "vanilla" save

In a similar manner, can I reset completed researches without using a script that will "un-research" every researches but by deleting the part were research status is stored?

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Help with scenario & lua script

Post by drs9999 »

LotA wrote:Is it possible for a script to create a file and write data in it ? It seems the io lib isn't there..
I'd like to export a list of entitits & their coordinates to a file so i could read it from another map,
let's say I would like to create a script (actually 2) that "copy-paste"a set of entities over different maps (using relative coordinates)
There exists game.createfile or something like that (should be mentioned on the wiki) to create files, however there is no (easy) way to read the data from within factorio.

There is a mod to export and import blueprints (I forgot the name though), which handles that imo pretty smart. Basically what it does is instead of just writing blueprint information to the file, it writes a complete new mod which contains this information. Although it has to be moved from the output-folder into the mod-folder manually to read the data back in.

LotA wrote:My point is : can I reset/delete any scripts from a savegame? As my scriprs are for editing purpose, i'd like to get rid of all of them once the game is launched (and when i won't edit anything anymore via scripts) : i want a clean "vanilla" save
I believe yes, but not sure though...

User avatar
LotA
Fast Inserter
Fast Inserter
Posts: 117
Joined: Fri Oct 10, 2014 11:41 am
Contact:

Re: Help with scenario & lua script

Post by LotA »

hey, hi again ;)
Ok, so I managed to get those scripts working. (finally have my 4000 steam engines working !.. The 2GW spike when the 19k laser tower charges-up is lovely ^^')

So basically, you launch a map with your "copy" script then the following console commands are available :
setpos{p1={x=,y=},p2={x=,y=}} to adjust the area you want to copy (top-left & bot-right corners)
setname("filename") to precise the name of the file generated and the name of the function that will contains all the entity details (if unset, it'll use "p1.x p1.y.txt" as default filename)
proceed() to actually generate the file.
this way you can generate multiple files from a single map.

All you'll have to do then is to append the generated code (which is "pre-formatted" lua in the same manner as your blueprint mod) in the "paste" script then launch the map and use setref{x=,y=} to determine where to place the entities (being the top-left corner of the area) and then the the name of your generated function to actually create those entities in you map.

I could make the script more friendly-use (for instance, make the "paste" script fully generated) and share it if anyone's interested. Note that there is no "gui", all is done via console commands and even if it would be nice, i don't intend to add one as it would be time consuming and it's not really needed.


Anyway, i'm still asking for help ( :D ). This time, i'd like to know if we can (and how to) edit requester chests proprieties via lua scripts.
You can't edit requester chests in the editor (any added filter just vannish once you leave the menu) and i have like thousands of them so i would love to know what is the method to set them up. I couldn't find anything regarding this in the wiki.

User avatar
LotA
Fast Inserter
Fast Inserter
Posts: 117
Joined: Fri Oct 10, 2014 11:41 am
Contact:

Re: Help with scenario & lua script

Post by LotA »

Nevermind, i found out, I was looking for the setrequestslot() method.

Typical usage of it (set all requester chests within a certain area to request 2000 iron ore in their first request-slot)

Code: Select all

a = game.findentitiesfiltered{name = "logistic-chest-requester", area = {{xmin, ymin},{xmax, ymax}}}
for _, v in ipairs(a) do
      v.setrequestslot({name="iron-ore",count=2000},1)
end

Post Reply

Return to “Maps and Scenarios”