Scripting noob needs help

Timbatim

New member
Hey guys,

I have been playing KOL for a while now and am getting consistent 3-day-runs as I use always the same pulls and almost always the same route.
Now I want to use a more automated approach, but all ascension scripts I tried do things I do not want or understand (they change my familiars, equip wierd items, etc.).
I want more control over those things, in the exact order I figured out.


For starters I need a pulling script to manage my needed items, so I read a bit in the wiki and tried to write a function I could use later in my own ascension-script.
Would this work?

Code:
boolean try_pull(string it, int pull_amount){
if(storage_amount(it) < pull_amount)
	{
		print("Not enough "+it+" in Storage.", "red");
		if(stash_amount(it) < pull_amount)
		{
			print("Not enough "+it+" in Clanstash.", "red");
			buy_using_storage(pull_amount, it, 75000);
			take_storage(pull_amount, it);
		}
		else
		{
			print(+it+" taken from Clanstash.", "green");
			take_stash(pull_amount, it);
		}
	}
else 
	{
		print(+it+" taken fom Storage.", "green");
		take_storage(pull_amount, it);
	}	
print(pulls_remaining() "left.")
return true;

As I do not know how to use external sources like text files, yet, I wanted to write:
It should always pull 3 different items, 1 of each.


Code:
void main() {
it = item1;
try_pull(it, 1);
it = item2;
try_pull(it, 1);
it = item3;
try_pull(it, 1);

etc.

return;
}


edit: I haven't been programming (C++) for a few years and am more than rusty.
 
Last edited:

AlbinoRhino

Active member
Data Types

Functions such as storage_amount() & take_storage() etc. are expecting "it" to be the $item[] data type. You are passing in a string type. So you need to convert "it" to item type ( to_item() ) or pass in item type to begin.
 

lostcalpolydude

Developer
Staff member
I'm pretty sure you can't pull from your clan stash while in ronin, so that part of the script won't work.

Code:
print(+it+" taken fom Storage.", "green");
The + at the start breaks that.
 

Timbatim

New member
Thanks AlbinoRhino and lostcalpolydude, I wasn't aware of that datatype and the stash does not work in Ronin indeed and is a bit easier.

Does the to_item function work that way?
I declare a new local var named it which is emtpy and define it with to_item(it_str).
The string imported via the try_pull function is renamed it_str, as I am not sure if those two can named equal without being defined in beforehand.


Code:
boolean try_pull(string it_str, int pull_amount){

item it = $item[ none ];
item it = to_item(it_str);

if(storage_amount(it) < pull_amount)
	{
		print("Not enough "+it+" in Storage, bought 1.", "red");
		buy_using_storage(pull_amount, it, 75000);
		take_storage(pull_amount, it);
        }
else 
	{
		print("1 "+it+" taken fom Storage.", "green");
		take_storage(pull_amount, it);
	}	
print(pulls_remaining() "left.")
return true;


void main() {
it = item1;
try_pull(it, 1);
it = item2;
try_pull(it, 1);
it = item3;
try_pull(it, 1);

etc.

return;
}
 

AlbinoRhino

Active member
Something like this should work. Some minor differences with what you already have....hopefully they are apparent.

PHP:
boolean try_pull(string it_str, int pull_amount)
{


    item it = to_item(it_str);


    if(storage_amount(it) < pull_amount)
    {
        print("Not enough "+it+" in Storage, bought"+pull_amount+".", "red");
        buy_using_storage(pull_amount, it, 75000);
        take_storage(pull_amount, it);
        print(pulls_remaining() +" pulls left.")
        return true;
    }
    else 
    {
        print(pull_amount+" "+it+" taken fom Storage.", "green");
        take_storage(pull_amount, it);
        print(pulls_remaining() +" pulls left.")
        return true;
    }




    print(pulls_remaining() +" pulls left.")


return false;
}




void main()
{    
    string i = "";

    i = "huge bowl of candy";
    try_pull(i, 1);

    i = "facsimile dictionary";
    try_pull(i, 1);

    i = "disassembled clover";
    try_pull(i, 1);
}

Edit: The "return false" is unreachable, i think, at this state of the development. There should probably be more error checking as to whether the purchasing & removing from storage actually succeed, as well.
 
Last edited:

AlbinoRhino

Active member
Here is a slightly more developed version...

PHP:
boolean try_pull(string it_str, int pull_amount)
{

	item it = to_item(it_str);
	string pleft() { return pulls_remaining() +" pulls left."; }
	
	print(pleft(), "blue");
	
	
	if	( pulls_remaining() < pull_amount )
	{
		print("Reducing pull amount to "+pulls_remaining()+".","red");
		pull_amount = pulls_remaining();
	}
	

	if(storage_amount(it) < pull_amount)
	{
		print("Not enough "+it+" in Storage, to pull "+pull_amount+".", "red");
		
		int gotit = buy_using_storage(pull_amount, it, 75000);
		
		if	( gotit == 0 )
		{
			print("Was unable to buy any "+it, "red");
			print(pleft(), "blue");
			return false;
		}
		else
		{
			print("Bought "+gotit+" "+it+". Pulling...","green");
			
			if	( take_storage(gotit, it) )
			{
				print("Pulled "+gotit+" "+it,"green");
				print(pleft(), "blue");
				return true;
			}
			else
			{
				print("Unable to pull "+gotit+" "+it,"red");
				print(pleft(), "blue");
				return false;
			}
		}
	}
	else 
	{
		
		if	( take_storage(pull_amount, it) )
		{
			print(pull_amount+" "+it+" taken fom Storage.", "green");
			print(pleft(), "blue");
			return true;
		}
		else
		{
			print("Unable to pull "+pull_amount+" "+it,"red");
			print(pleft(), "blue");
			return false;
		}
	}
}


void main()
{
	foreach i in $strings[
	huge bowl of candy,
	facsimile dictionary,
	disassembled clover,
	]
	{
		if	( try_pull(i, 1) )
		{
			print("Got "+i,"green");
		}
		else
		{
			print("Was unable to get "+i,"red");
		}
	}
}
 
Last edited:

Timbatim

New member
Hey thanks again, I copied your last code and tried to figure out which alterations were made and most importantly why they were made.
Took me a while, but I have additional questions :eek:


Code:
int gotit = buy_using_storage(pull_amount, it, 75000);

What I think it does:
It creates a new variable named gotit, activates the function and returns 1 for success/0 for failure?
I mean it does all that in that one line by defining the var?

edit: Moreso the boolean of the function gets an int, does it work that way?


Code:
if    ( take_storage(gotit, it) )
            {
                print("Pulled "+gotit+" "+it+, "green");
                //print(pleft(), "blue");
                return true;
            }
It can activate the function in the condition for the if-iteration?
In this case it doesn't need a > < or ==? Why though?
 
Last edited:

AlbinoRhino

Active member
Hey thanks again, I copied your last code and tried to figure out which alterations were made and most importantly why they were made.
Took me a while, but I have additional questions :eek:


Code:
int gotit = buy_using_storage(pull_amount, it, 75000);

What I think it does:
It creates a new variable named gotit, activates the function and returns 1 for success/0 for failure?
I mean it does all that in that one line by defining the var?

edit: Moreso the boolean of the function gets an int, does it work that way?


Code:
if    ( take_storage(gotit, it) )
            {
                print("Pulled "+gotit+" "+it+, "green");
                //print(pleft(), "blue");
                return true;
            }
It can activate the function in the condition for the if-iteration?
In this case it doesn't need a > < or ==? Why though?


buy_using_storage()

If you click the link above you will see that there is both a boolean and an integer version of this ASH function. When you specify a max price (as you did in your original code) this function returns an integer...the quantity that were obtained. So we created the variable "gotit" to capture how many were obtained.


http://wiki.kolmafia.us/index.php?title=Take_storage

The take_storage() ASH function returns a boolean... " if ( take_storage(gotit, it) )" ...is only going to be TRUE or FALSE. If TRUE, the bracketed code will run. If FALSE, execution will continue at the corresponding "else".

Edit: There were a couple of minor errors in the code I posted (I didn't actually try to run it.) Corrected the above post (#6) and confirmed that it works as intended. Sorry for posting broken code !
 
Last edited:

Timbatim

New member
Ah yes, I see the return of an int. What I meant was another thing, too.

Code:
int gotit = buy_using_storage(pull_amount, it, 75000);
We get an int and put it to gotit. But do actually use the function as in really buying something? It seems it is just a check if everything "could" be bought.


Code:
if    ( take_storage(gotit, it) )
            {
               [COLOR="#FF0000"] print("Pulled "+gotit+" "+it+, "green");[/COLOR]
                //print(pleft(), "blue");
                return true;
            }
When I try to verify the .ash file in the cli command, I always get a "value expected", I assume it is because nothing can really be bought by verifying so there is no real gotit.
 

AlbinoRhino

Active member
The function DOES execute and "gotit" will be how many were obtained...which is the return value of buy_using_storage() in this case.


"value expected"
You must still be using an old copy of the code I posted.
Take the extra "+" off after the "+it".
 
Last edited:

Timbatim

New member
Ah, yes. I misunderstood the function of the plus sign. I thought it was like " for everything not being plain text but it is meant to add text blocks.


Thank you very much, I learned a lot and remembered long forgotten things. This will be a good startingbase to make daily tasks easier :)
 
Top