title: 'Sample Script Examples' #
Sample Script Examples #
Updated: 11/23/13
Okay then, you’ve installed Perl, set up the Lacuna Game Client, and now you’re ready to start scripting.
But what exactly does that mean? There are sample script commands in the Wiki, but how exactly can they be used to script? You could type each command in a DOS window, but that would be tedious and time consuming. It’d be much easier to create a batch file to do that. That way, you could string together multiple commands or instructions and do several things at once.
The example scripts presented here are geared toward the casual user whose scripting needs are simple. They should give you some idea of what’s possible and what you may be able to accomplish.
I make no claims as to whether these examples are the best or easiest way to script. They should work on a Windows 7 PC. I use MS DOS because I’m familiar with it and it’s relatively easy. Other programming languages are going to be more powerful. If you’re interesting in learning more about MS DOS commands, a good starting point is: http://www.computerhope.com/msdos.htm
Let’s start off with some examples.
Using a text editor, like notepad, we create a simple batch file: docked.bat. It’s just like writing a text file, except we use the extension .bat instead of .txt. These are the contents of docked.bat:
@echo off
cd c:\le\glc\examples
perl docked_ships.pl
pause
Our first line: @echo off, is used to prevent each subsequent line from echoing or printing to the screen. If you really want to see each line on screen as the script runs, you can omit this instruction.
On line two, we’re pointing to the direction where Perl can find the file docked_ships.pl. .pl means it’s a perl file. cd stands for “change directory”. The file docked.pl can be found on the C drive in directory le->glc->examples. If your perl files where located on drive D in directory lacuna->data->perl, then your line two would read: cd d:\lacuna\data\perl
Line three is the actual perl command which uses the perl file docked.pl to list all docked ships on every colony you have. Note: you don’t necessarily have to preface commands with ‘perl’. Docked.pl should run ok by itself if you’ve set up Perl correctly on your system. If for some reason you’re having difficulty, then include perl before the command.
The last line, pause , prevents the batch file from ending until you press a key. This way, we can view the list. Otherwise, the batch file would terminate and the list would disappear.
When I want to run docked.bat, I use my file manager to locate that file on my hard drive and double click it. That’s the simplest way to use it. You don’t need to be logged in to the game to run it. If you wanted to automate your scripting, say, so you could run certain scripts everyday at a certain time, then you could use Windows Task Scheduler to run your batch files. But that’s beyond the scope of this tutorial.
Ok, let’s look at another example. This file will be called upgrade.bat:
@echo off
cd c:\le\glc\examples
upgrade_all.pl --planet "My Planet 1" --planet "My Planet 2" --match Shipyard
The first two lines are the same as in the previous example. The third line uses the file upgrade_all.pl to upgrade buildings on two of our planets. If we use the Sample Script Commands page in the Wiki to look up the options for this file, this is what we find:
Options:
--help - This info. --verbose - Print out more information --config FILE - Specify a GLC config file, normally lacuna.yml --planet NAME - Specify planet --dumpfile FILE - data dump for all the info we don't print --maxlevel INT - do not upgrade if this level has been achieved --maxnum INT - Use this if lower than dev ministry level --maxadd INT - Add at most INT buildings to the queue per pass --wait INT - Max number of seconds to wait to repeat loop --sleep INT - Pause between RPC calls. Default 1 --junk - Upgrade Junk Buildings --glyph - Upgrade Glyph Buildings --space - Upgrade spaceports --city - Upgrade LCOT --lab - Upgrade labs --match STRING - Only upgrade matching building names --skip STRING - Skip building names (multiple allowed) --extra STRING - Add matching names to usual list to upgrade --dry - Do not actually upgrade
In our example, we used the --planet NAME option twice. Some perl files may only allow you to use it once. Note that spacing is important. Also, spelling has to be exact and is case sensitive. The option --match allows us to specify a specific building and could also be used multiple times to specify multiple buildings. So line three is only upgrading shipyards. If we wanted to upgrade different buildings on other planets, we could add a fourth line, for example:
upgrade_all.pl --planet "My Planet 3" --skip Space Port
This last line would upgrade all buildings on that planet except the space ports. You can string together multiple lines of commands to perform many tasks in the game which is much easier than clicking on buildings on different planets.
Now let’s take a look at a script that builds ships. We’ll call this file fighters.bat:
@echo off
cd c:\le\glc\examples
build_ships.pl --yards shipyards.js --type fighter --number 50 --planet "My Planet 1"
On our colony My Planet 1, we have two level 30 shipyards. We want to build 50 fighters, though we could build a maximum of 60 ships at a time (1 per level of each shipyard). Shipyards.js is the file that stores info on the state of the shipyards on all your planets. It’s important to keep this file up to date if you’ve upgraded your shipyards. To do that, we can use the following command found on the Sample Script Commands page in the Wiki:
get_buildings.pl –config../ lacuna.yml --shipyard --shipfile shipyards--shipyards.js
You could type this line at the DOS (command) prompt, but it’s much easier to use it in a batch file:
@echo off
cd c:\le\glc\examples
get_buildings.pl --shipyard --shipfile shipyards.js
Notice I’ve altered the command. I’ve trimmed it down to just what’s required for it to run. Your stats and shipyard levels will determine how long it will take to build those fighters.
Now let’s say you want to build lots of ships. Assuming you have two level 30 shipyards on your planet, and that 60 sweepers would take 40 minutes to finish building. You could just keep rerunning the script every 40 minutes or so. Surely there must be some better ways available. Let’s take a look at some options:
@echo off cd c:\le\glc\examples
build_ships.pl --yards shipyards.js --type sweeper --number 60 --noreserve --planet "My Planet 1" echo sleeping for 40 mins... perl -e "sleep 2400" build_ships.pl --yards shipyards.js --type sweeper --number 60 --noreserve --planet "My Planet 1" echo sleeping for 40 mins... perl -e "sleep 2400"
In the above example, we’re using the perl sleep command to pause the batch file for 40 minutes. 40 minutes x 60sec’s=2400 sec’s. We also used the echo command to echo a message to the screen to remind us of what’s going on. Then we repeated the same instruction.
We also added --noreserve to our build ships instruction. This causes the instruction to ignore the reserve number in the datafile and ensures we build the maximum number of ships on each shipyard. We could repeat the building instructions in this way several times and build lots of ships. But there are better ways that require less typing:
@echo off cd c:\le\glc\examples
set /a count=0
:LOOP set /a count+=1 build_ships.pl --yards shipyards.js --type sweeper --number 60 --noreserve --planet " My Planet 1"
REM the number in the next line equals the number of building cycles
IF %count%==10 goto END echo Building sweepers. Cycle %count% of 10. Sleeping for 40 mins... perl -e "sleep 2400" goto :LOOP
:END
This batch file is using a loop to count the number of ship building cycles that we want. Let’s take a closer look at it.
In line 3, we’re setting a variable which we’re going to call ‘count’ to zero. You could call it whatever you wanted: counter, number1, N, etc.
Line 4 is where our loop begins. :LOOP is the name of the target label which represents a particular point in our program where we want come back to later on. As with the variable ‘count’, you can name your labels whatever you want, as long as it begins with a colon.
Line 5 is where we use the SET command to increase the value of ‘count’ by 1.
Line 6 is the instruction we’ve already seen for building our sweepers.
Line 7 has a REM statement, which is a way to record remarks inside the body of a batch file. The comments which come after REM remind us of something and are not displayed on the screen.
On line 8, we’re using the conditional command IF to determine if the variable ‘count’ is equal to 10. If it is, then the GOTO command instructs our program to skip to the target label called END. If ‘count’ is not =10, then the sequence continues onto the next line.
Line 9 echo’s a reminder as to which build cycle we’re currently on by displaying the current value of ‘count’. Every time the program goes thru the loop we’ve set up, the value of ‘count’ will increment by one, and that increase is what will be indicated here.
Line 10, as we’ve already seen, pauses the program for enough time to allow building to finish.
Line 11 sends the program back to up to :LOOP to repeat the process.
The last line is the target label :END. As there are no instructions after this point, the program will end.
So in this example, ship building will cycle ten times, and on the tenth cycle it will end. On the last cycle, I saw no need to pause or sleep, so the program ends before the last batch of ships are finished building. If I wanted the program to end when the last batch of ships were done, I could rearrange the commands like so:
@echo off cd c:\le\glc\examples
set /a count=0
:LOOP IF %count%==10 goto END set /a count+=1 build_ships.pl --yards shipyards.js --type sweeper --number 60 --noreserve --planet " My Planet 1" IF %count%==10 goto END echo Building sweepers. Cycle %count% of 10. Sleeping for 40 mins... perl -e "sleep 2400" goto :LOOP
:END
Notice I moved “set /a count+=1”. Now the program will have to loop back around one last time in order to check if ‘count’ has reached 10.
I could have also counted down instead of up.
I would have had to set the counter initially to 10: set/a count=10
Subtract instead of add 1: set /a count-=1
And ended the program at zero instead of ten: if %count%==0 goto end
It’s just a matter of preference.
Here’s an example of a more complex batch file that offers a choice of several ships we can build:
@echo off cd c:\le\glc\examples
echo. echo SELECT OPTION FOR My Planet 1 echo ----------------------------- echo.
:menu echo 1- Build Fighters echo 2- Build Snark's echo 3- Build Sweepers echo 4- Exit echo. set /p option=enter option 1 2 3 or 4: if "%option%"=="1" goto createftr if "%option%"=="2" goto createsnrk if "%option%"=="3" goto createsweep if "%option%"=="4" goto exit
:createftr build_ships.pl --yards shipyards.js --type fighter --number 60 --noreserve --planet " My Planet 1" goto exit
:createsnrk build_ships.pl --yards shipyards.js --type snark --number 60 --noreserve --planet " My Planet 1" goto exit
:createsweep build_ships.pl --yards shipyards.js --type sweeper --number 60 --noreserve --planet " My Planet 1" goto exit
:exit
As you can see, I can select which ship I want to build. I could add the same kind of loops we saw in the last example and build lots of ships. I could add many more options too.
Now let’s tie several perl commands together and look at a script that you might run once a day. We’ll call this batch file daily.bat:
@echo off cd c:\le\glc\examples
spy_run.pl --from "My Capitol" --target "My Capitol" --number 1 --task Security Sweep spy_run.pl --from " My Planet 2" --target "My Planet 2" --number 1 --task Security Sweep spy_run.pl --from " My Planet 3" --target "My Planet 3" --number 1 --task Security Sweep
push_glyphs.pl --from "My Planet 2" --to "My Capitol" --ship "Galleon" push_glyphs.pl --from "My Planet 3" --to "My Capitol" --ship "Galleon" push_plans.pl --from "My Planet 2" --to "My Capitol" --ship "Hulk" push_plans.pl --from "My Planet 3" --to "My Capitol" --ship "Hulk"
echo Pausing for 3 minutes... perl -e "sleep 180"
assemble_halls.pl --planet "My Capitol" --use-last
send_ships.pl --type scow_mega --from "My Capitol" --own-star send_ships.pl --type scow_mega --from "My Planet 2" --own-star send_ships.pl --type scow_large --from "My Planet 3" --own-star
call buildexcavators.bat
waste_chain.pl --planet "My Capitol” --equalize waste_chain.pl --planet "My Planet 1" --equalize waste_chain.pl --planet "My Planet 2" --equalize
Lines 3-5: We’re doing a security sweep on every planet in our empire. I want only one spy on each planet to perform this task.
Lines 6-7: pushing all glyphs from our planets to our capitol planet. If I don’t select a particular ship type, then one will be selected automatically. I’ve selected a ship which is large enough to move the glyphs we get on a daily basis. Smaller ships tend to be faster than larger ones.
Lines 8-9: pushing all plans from our planets to our capitol planet. As above, I’ve selected a particular ship type.
Lines 10-11: pausing to allow the shipped goods to arrive at our capitol planet (based on the speeds of the selected ships and the distance from sending planets to the receiving one).
Line 12: turn our glyphs into Halls in the Excavator Ministry, using the last of any glyph if necessary to build a hall plan.
Lines 13-15: dispose of waste on all our planets. Note that ship type: scow, scow_large, scow_fast or scow_mega must be specified. In this example, the amount of waste actually removed will depend on the amount on hand and the capacity and number of scows available.
Line 16: Uses the Call statement to call another batch file within a batch file. In this example, we’re transferring to another batch file (buildexcavators.bat) without aborting the execution of the calling batch file (daily.bat). Once buildexcavators.bat finishes, daily.bat will pick up where it left off and continue to the next line of instruction. Using this technique can be useful in so far as several small batch files can be easier to troubleshoot or modify than one large one.
Lines 17-19: Equalizes the waste production on all our planets so that waste production will be zero. This is a function found in the trade ministry. Note that if you don’t have enough scows assigned to a waste chain, or are producing more waste than the assigned ships can carry, then some waste will be continue to be produced.
I hope these examples have given you some ideas as to what you can do to make tasks easier and automated in the game. Happy scripting!
-Frobozz