Breaking free of the chains: Saving, and reading data with KOLMafia's CLI

I have waited for several days for final word from the forum leaders, and Holatuwol. Based on what I am told, Holatuwol doesn't mind if my methods are posted in these forums, and others seem to be interested in seeing the methods I have mentioned.

My first post on this subject the title has given away. Saving Data, then reading it back into KOLMafia. This is a task which has been said to not be possible, however it has truely been possible since early page 77 on KOL's forums KOLMafia thread:

efilnikufecin wrote:
I have an idea for a feature I would like to see implemented, however I do not know if it would be very reasonable to encode. The feature would allow writing of ini like files via ash scripting so that a person could keep track of statistical data easier.

Holatuwol replied
If you wanted specifically to write that data in that format, a feature most people aren't aware of is combining the "mirror" and "echo" commands in the standard CLI with the string concatenation and CLI_EXECUTE features of the ASH. Basically, what you would do is open a mirror stream to the file of your choice and print out the data you wanted using CLI_EXECUTE; this will print out all the ECHOs to the screen as well as to the file you've chosen. Naturally, you wouldn't be able to load the values back in, but your mission was just to print and do data tracking.

Yes he said that Naturally, you wouldn't be able to load the values back in, but he didn't say we wouldn't be able to read it back in at all. Minor play on words.

First we need the output file. To get the output file, we need a script written to write the output file:

Code:
void main()
{
//many many other commands
cli_execute("mirror prep.ini");
cli_execute("echo meat=" + int_to_string(my_meat));
cli_execute("echo peng=" + int_to_string(item_amount($item[penguin skin]));
cli_execute("echo yeti=" + int_to_string(item_amount($item[yeti fur]));
cli_execute("mirror");
}

Now running this code in mafia will generate a file something like this:

Prep.ini
Code:
meat=1000
yeti=10
peng=4

OK we've got the info into a file. Now you are wondering how the heck do I get it back into KOLMafia the next time I run it? Well you might know that the easiest way to start KOLMafia's CLI is via a batch file. The command line to start KOLMafia is a really long one. So we'll start the CLI via a batch file, but before we start KOLMafia back up we'll do some other things:

StartMafia.bat
Code:
@echo off

:PrepMeat
find "meat=" prep.ini | sort /r | date | find "=" > en#er.bat
echo set curvalue=%%6> enter.bat
call en#er.bat
del en?er.bat > nul
echo echo int YesterdaysMeat()>>WriteScript.bat
echo echo {>>WriteScript.bat
echo echo return %curvalue%;>>WriteScript.bat
echo echo }>>WriteScript.bat

:PrepPeng
find "peng=" prep.ini | sort /r | date | find "=" > en#er.bat
echo set curvalue=%%6> enter.bat
call en#er.bat
del en?er.bat > nul
echo echo int Yesterdayspeng()>>WriteScript.bat
echo echo {>>WriteScript.bat
echo echo return %curvalue%;>>WriteScript.bat
echo echo }>>WriteScript.bat

:PrepYeti
find "yeti=" prep.ini | sort /r | date | find "=" > en#er.bat
echo set curvalue=%%6> enter.bat
call en#er.bat
del en?er.bat > nul
echo echo int Yesterdaysyeti()>>WriteScript.bat
echo echo {>>WriteScript.bat
echo echo return %curvalue%;>>WriteScript.bat
echo echo }>>WriteScript.bat

:WriteScript
call WriteScript.bat>results.ash

:Cleanup
del writescript.bat>nul
rem del prep.ini is only commented out for testing, and must be there for this 
rem set of files to work. It deletes prep.ini so KOLMafia doesn't append
rem it's output to the end of the previous days data
rem del prep.ini>nul

REM start KOLMafia here

That batch file does one dandy little trick! It writes an ash script based on the info stored in prep.ini The name of the ash script is
Code:
results.ash
and this is what it looks like:

Code:
int YesterdaysMeat()
{
return 1000;
}
int Yesterdayspeng()
{
return 4;
}
int Yesterdaysyeti()
{
return 10;
}

Now the first line of the script you want to pull the saved data into will need to contain the import command for results.ash, and the rest should be rather obvious to you if you managed to read this far into this post.

If need be, I can elaborate more on exactly what the batch file is doing, however you could obtain so much more info from the web by doing a google search for "batch" which will turn up multiple sights explaining MS-Dos Batch files.

The method written above requires windows nt/2000/xp it may work on 95/98/me, but I have my doubts. Just about every other platform has an equivelent to the MS-Dos batch file. The info about other platforms, and the methods required to accomplish this same thing should be available on the web.

I have included all 4 files, however you only need savedata.ash, and test.bat The other 2 files get deleted every time the batch file is run. 1 is written by the batch file, the other is written by KOLMafia.

I will stress this one last point: always always always read and understand any batch file you download from anywhere on the web! If you don't understand what a batch file is doing, please don't run it on your computer!

Edit: the following is the same batch file with several comments added to help explain what it is doing:
StartMafia.bat
Code:
REM lines starting with rem are a remark, and skipped
REM @echo off tells dos not to display the commands 
REM after it in the dos box
@echo off

REM lines starting with a : are section headers which can be used
REM with a goto command. We don't use goto here, and so those lines are ignored
:PrepMeat
REM the following line executes 3 different commands
REM piping the output of each one into the other, 
REM and piping the final output into a file "en#er.bat"
REM those commands are find, sort, and date
REM find is called twice
find "meat=" prep.ini | sort /r | date | find "=" > en#er.bat
REM the echo command displays text on the screen
REM in conjuction with the %%6 part of the previous lines
REM output is echoed, and the > symbol pipes the output
REM into a text file with the .bat extention
echo set curvalue=%%6> enter.bat

REM call the batch file we just created
REM it will add the variable curvalue so we can use it in this batch file
REM and any other batch files we run
call en#er.bat
REM delete the batch files we created. It is no longer needed
del en?er.bat > nul

REM using > overwrites a file
REM using >> appends to the file instead of overwriting it.
echo echo int YesterdaysMeat()>>WriteScript.bat
echo echo {>>WriteScript.bat
echo echo return %curvalue%;>>WriteScript.bat
echo echo }>>WriteScript.bat

:PrepPeng
REM The same series of commands with some strings changed
REM for the next piece of data we want available in KOLMafia
find "peng=" prep.ini | sort /r | date | find "=" > en#er.bat
echo set curvalue=%%6> enter.bat
call en#er.bat
del en?er.bat > nul
echo echo int Yesterdayspeng()>>WriteScript.bat
echo echo {>>WriteScript.bat
echo echo return %curvalue%;>>WriteScript.bat
echo echo }>>WriteScript.bat

:PrepYeti
REM and again
find "yeti=" prep.ini | sort /r | date | find "=" > en#er.bat
echo set curvalue=%%6> enter.bat
call en#er.bat
del en?er.bat > nul
echo echo int Yesterdaysyeti()>>WriteScript.bat
echo echo {>>WriteScript.bat
echo echo return %curvalue%;>>WriteScript.bat
echo echo }>>WriteScript.bat

:WriteScript
REM call the batch file we created with it's output piped to the file results.ash
call WriteScript.bat>results.ash

:Cleanup
REM we must delete this file at some point.
REM if not, it will be appended to the next time we run this batch file
REM and the resulting ash will have duplicate functions
REM pipe the output to nul "nowhere" so we don't get the annoying"
REM "are you sure" prompt repeatedly stopping execution
del writescript.bat>nul
rem del prep.ini is only commented out for testing, and must be there for this 
rem set of files to work. It deletes prep.ini so KOLMafia doesn't append
rem it's output to the end of the previous days data
rem del prep.ini>nul

REM start KOLMafia here

Though the explanation isn't 100% complete, it should be enough to help you decide if you want to explore into the world of MS-DOS batch files. I see no reason why a person couldn't write a combination of scripts and batch files to:
efficiently ascend a character
or even:
Run the mushroom plot to 3rd or even 4th generation without flaw

This method could be used to write huge scripts, and subscripts on the fly. If scripting more than one character, scripts could be written so that one character acts accordingly to what a previous character has achieved, or even what multiple other characters have achieved.

This method could also be used to run a whole clan of characters working together for the good of all with no communication with the outside world of KOL. The possibilities are endless, and only limited by your imagination.
 

Attachments

  • prep.ini
    26 bytes · Views: 93
  • results.ash
    121 bytes · Views: 94
  • SaveData.ash
    277 bytes · Views: 94
  • Test.bat
    1.1 KB · Views: 98

Nightmist

Member
Impressive. Impressive indeed!


[quote author=efilnikufecin link=topic=56.msg193#msg193 date=1144408874]
I will stress this one last point: always always always read and understand any batch file you download from anywhere on the web! If you don't understand what a batch file is doing, please don't run it on your computer!
[/quote]

Im going to have to back that statement up. I personally currently use my batch files to write all text usually printed to the cli into a text file and then renames it according to local date. My way of keeping "detailed" logs or so to speak.
 
If I ever get the time to study this, and (hopefully) understand it, it will be glorious.

This will springboard two projects I've been wanting to get done, but have been dreading to start.

Now to find the time.... :p

Thank you efilnikufecin.
 
Top