Variable functions

me259259

Member
Is there a way to use a variable to add an entire line of code? I don't want it to do something like "use(1, variable)", but I was hoping to do something such as "while (complicatedVariable)". Is this even possible?

I wrote a bunch of auto-adventuring scripts for myself. Right now I'm trying to make the code simpler... or at least make the scripts shorter.

The main thing I am looking at is how I set up the script to handle adventuring. Here's an example:

PHP:
    nextMonster = $monster[Hellion] ;
	nextLocation = $location[Dark Neck of the Woods] ;
	while (available_amount($item[dodecagram]) < 1)
		{
		if (my_adventures() == 0)
			abort ("ran out of adventures") ;
		if (have_effect($effect[on the trail]) < 1 )
			use_familiar(stasisFam) ;
		else if (my_mp() < my_maxmp()*.5)
			use_familiar(stasisFam) ;
		else (use_familiar(itemFam) ) ;
		adventure(1, nextLocation);
		}

The thing I am trying to shorten is everything after "while". I have 7+ lines every time I need to adventure somewhere. There has to be a way to handle this repetitive function, other than to write it every time I need to adventure...

The thing I'm looking at right now is to make a map (for some reason, maps just started to click with me, and only now am I starting to use them). Here's what I have so far (not tested yet):

In the regular scripts:

PHP:
nextMonster = $monster[Hellion] ;
nextLocation = $location[Dark Neck of the Woods] ;
int [item] itemgoals ;
itemgoals $item[dodecagram] = 1

and in a new script/function, the following code:

PHP:
foreach item in itemGoals
	while (available_amount(goals[item]) < goals[int])
		{
		if (my_adventures() == 0)
			abort ("ran out of adventures") ;
        if ((nextLocation == $location[dark neck of the woods] || nextLocation == $location[pandamonium slums]) && my_maxmp() > 39 ) //etc
			if (have_effect($effect[on the trail]) < 1 )
				use_familiar(stasisFam) ;
		if (my_mp() < my_maxmp()*.5)
			use_familiar(stasisFam) ;
		else if (wantMoreItems == True)
			use_familiar(itemFam) ;
		else 
			use_familiar (statFam) ;
		adventure(1, nextLocation);
		}
clear itemGoals ;

Nice and relatively simple. In the scripts I have now, I would reduce those 7+ lines down to 4+ lines, and have a function written only once to handle all my adventuring! And there was much rejoicing.

Except... The problem that I'm having is when I'm adventuring in areas for reasons other than to get items. Granted, the times that it happens is few and far between, but I would still like for my new adventuring logic to handle it. For example, in the beginning of the game when I'm adventuring in the haunted pantry to unlock spookyraven manor, my "goal" is:

PHP:
while (contains_text(visit_url("town_right.php"), "Haunted Pantry") )

I don't know how to translate that for a function that has it's goals set with variables. Is there a way to make something like this work?:

PHP:
string complicatedGoal = "contains_text(visit_url(\"town_right.php\"), \"Haunted Pantry\") "
while (complicatedGoal)
 
Last edited:

Bale

Minion
Is there a way to use a variable to add an entire line of code?
Yes. That is what we call a function.

Take a look at my example below. I'm showing a few alternate ways of doing thing that you are interested in. I'm also correcting the way that you were trying to use maps. I created the example to give you some interesting ideas, not to be optimal. You may find it a good inspiration.


PHP:
nextMonster = $monster[Hellion] ;
nextLocation = $location[Dark Neck of the Woods] ;
int [item] itemgoals;
itemgoals [$item[dodecagram]] = 1 

// check all items in your map and return a boolean so that you can use it in a while loop
boolean lack_goals(int [item] want) {
	foreach it in lack_goals
		if(available_amount(it) < want(it))
			return true;
	return false;
}

// keep your list of places to use olfaction and equip your olfaction familiar if true
boolean want_olfact(location l) {
	if(my_maxmp() < 40 || have_effect($effect[on the trail]) > 0) return false;
	switch(l) {
	case $location[dark neck of the woods]:
	case $location[pandamonium slums]:
	 // more places can easily be added to this list.
		use_familiar(stasisFam);
		return true;
	}
	return false;
}

void choose_fam() {
	if (my_mp() < my_maxmp()*.5)
		use_familiar(stasisFam) ;
	else if (wantMoreItems == True)
		use_familiar(itemFam) ;
	else 
		use_familiar (statFam) ;
}
	

while (lack_goals(itemgoals) && my_adventures() > 0)
	{
	if (!want_olfact(nextLocation))
		choose_fam();
	adventure(1, nextLocation);
	}
clear itemGoals;

Read the comments in the above example. Also notice how I tucked your check for my_adventures() into condition for the while loop.

You were a little confused about how to reference a map. Please notice the difference of how I did it in my example.
 
Last edited:

slyz

Developer
There has to be a way to handle this repetitive function, other than to write it every time I need to adventure...
You say it yourself, it's a repetitive function... Simply make it a real function!
PHP:
void my_adventure( location nextLocation )
{
        if (my_adventures() == 0)
            abort ("ran out of adventures") ;
        if (have_effect($effect[on the trail]) < 1 )
            use_familiar(stasisFam) ;
        else if (my_mp() < my_maxmp()*.5)
            use_familiar(stasisFam) ;
        else (use_familiar(itemFam) ) ;
        adventure(1, nextLocation);
}

nextMonster = $monster[Hellion] ;
nextLocation = $location[Dark Neck of the Woods] ;
while (available_amount($item[dodecagram]) < 1)
{
        my_adventure( nextLocation )
}

If you follow Bale's advice, you can break it down into further:
PHP:
void my_adventure( location nextLocation )
{
    if (!want_olfact(nextLocation)) 
        choose_fam(); 
    adventure(1, nextLocation);
}
If the need arises, you can make it return a boolean value ( return adventure(1, nextLocation) ), and you can add lots of useful stuff to prepare for combat in there too.

You could look at Bumcheekascend's bumadv() function for a little bit of inspiration. Of course, it must have grown over time, but the idea is to have a single function that handles all your adventuring.

If you want to add the condition handling part, you can have different functions to handle different types of conditions:
PHP:
void my_adventure( location nextLocation )
{
    if (!want_olfact(nextLocation)) 
        choose_fam(); 
    adventure(1, nextLocation);
}

void adventure_until( location nextLocation, int num, item it )
{
    while( item_amount( it ) < num )
        my_adventure( nextLocation );
}

void adventure_until( location nextLocation, string url, string check )
{
    while (visit_url( url ).contains_text( check) )
        my_adventure( nextLocation );
}
You can adapt it to accept a map of items instead of a single item, or add a adventure_until( location nextLocation, int level ) to adventure until you are a certain level etc...
 
Top