arrays

alphacow

New member
So, just today I decided to start experimenting with ash a little. I have my own little farming CLI routine, and today I wrote this up to supplement it:
Code:
#  get correct number of pet-buff and wasabi sinus buffs

void getKnobGoblinMoneyBuffs()
{
	# using floats since they will be divided by 10 later on
	float heavy_petting;
	float wasabi;
	
	# determine how many of each buff I need
	heavy_petting = my_adventures() - have_effect( $effect[ Heavy Petting ] );
	wasabi = 		my_adventures() - have_effect( $effect[ Wasabi Sinuses ] );
	
	# get the right number of heavy_petting buffs
	if( heavy_petting > 0 )
	{
		heavy_petting = ceil( heavy_petting / 10 );
		if( item_amount( $item[ Knob Goblin pet-buffing spray ] ) < heavy_petting )
		{
			buy( heavy_petting - item_amount( $item[ Knob Goblin pet-buffing spray ] ),
			    $item[ Knob Goblin pet-buffing spray ] );
		}
		use( heavy_petting, $item[ Knob Goblin pet-buffing spray ] );
	}
	
	# get the right number of heavy_petting buffs
	if( wasabi > 0 )
	{
		wasabi = ceil( wasabi / 10 );
		if( item_amount( $item[ Knob Goblin nasal spray ] ) < wasabi )
		{
			buy( wasabi - item_amount( $item[ Knob Goblin nasal spray ] ),
			    $item[ Knob Goblin nasal spray ] );
		}
		use( wasabi, $item[ Knob Goblin nasal spray ] );
	}
}
My question is, how can I make this an array-driven loop, instead of repeating code? It would have to be something like (to use php code):
Code:
$Buffs = array(
   array( 'Knob Goblin pet-buffing spray', 'Heavy Petting'),
   array( 'Knob Goblin nasal spray', 'Wasabi Sinuses'),
);
and then just reference the appropriate parts of the array ($buffs[n][0] = item name, $buffs[n][0] = effect name). Thanks!
 
One possible solution (untested, beware of syntax errors!):
Code:
void getKnobGoblinMoneyBuffs()
{
	float [item] [effect] buffs;
	buffs[$item[pet-buffing spray]][$effect[Heavy Petting]] = 0.0;
	buffs[$item[nasal spray]][$effect[Wasabi Sinuses]] = 0.0;
	
	foreach it, eff in buffs
	{
		buffs[it][eff] = my_adventures() - have_effect(eff);
		
		if (buffs[it][eff] > 0)
		{
			buffs[it][eff] = ceil(buffs[it][eff] / 10);
			if (item_amount(it) < buffs[it][eff])
			{
				buy(buffs[it][eff] - item_amount(it), it);
			}
			use(buffs[it][eff], it);
		}
	}
}

It is possible there are more elegant ways to do this, but I believe it is more-or-less what you are seeking.

(Edit: the most elegant, for example, is to buy a whole ton of the items and set up the buffs in your mood. But this is perhaps useful as a maps example anyway.)
 

slyz

Developer
In ASH they are called maps. See this thread for starters: http://wiki.kolmafia.us/index.php?title=Data_structures

Your code would become:
Code:
effect[item] KnobBuffs;
KnobBuffs[$item[Knob Goblin pet-buffing spray]] = $effect[Heavy Petting];
KnobBuffs[$item[Knob Goblin nasal spray]] = $effect[Wasabi Sinuses];

foreach knobitem in KnobBuffs {
	float TurnsNeeded = my_adventures() - have_effect( KnobBuffs[knobitem] );
	if( TurnsNeeded > 0 )
	{
		int NumNeeded = ceil( TurnsNeeded / 10 );
		if( item_amount(knobitem) < NumNeeded )
			buy( (NumNeeded-item_amount(knobitem)), knobitem);
		use( NumNeeded, knobitem );
	}
}

This is a nice way to get used to ASH, but in this case, defining a mood would be a lot more easier, wouldn't it ? (that and letting Mafia buy from NPCs)

EDIT: pointed to the wiki, it's a far better source of information =)
 
Last edited:

alphacow

New member
That's funny, it didn't even occur to me to use moods for this. I wanted to have it automate the buying of stuff also, though. I guess I could have a simple
Code:
if( item_amount[whatever] < 500 )
   buy(whatever, 500);
at the beginning of my script and let moods take care of the rest. Huh.

Well, at least it was good ash practice. :/
 
Top