KoLmafia Guide: Your first Script

From Kolmafia
Jump to navigation Jump to search

Our first objective is an auto-buffing script. This one takes an input (in tens of adventures for simplicity) and then casts buffs on your character. In this case, I use cheap buffs that boost item-find. This is for undersea exploration or other places where boosting item find is handy or profitable.

It is super easy to use this template to create ANY personalized buff script. You can cut&paste and replace my buffs with whatever you want to cast any buffable effect from potions/items on yourself.

Executing: Cut&Paste the code into a text file. Turn off Word-Wrap and make sure no line has a line-break. Save the file as 'itemfind.ash' for example. Now you can run it as shown in previous lessons. (You put the .ash file into KoLmafia/scripts directory, refresh from the browser, then you can run it)

Learning: The entire script is below all my 'letter points'. Basically, read each letter point and then find the corresponding part of the script. It might help to browse it in 2 windows.


a) First, notice the void main statement. You should go ahead and make all your scripts start with this. Note the int numberOfTurnsInTens in parenthesis. This means when you run the script, it will pop up and ASK you to enter a value for 'numberOfTurnsInTens'. So make your variable names descriptive enough to remind you exactly what they are. Alternatively, you can type 'itemFind.ash 5' and it will automatically set '5' for the variable numberOfTurnsInTens. So why didn't we just ask for turns? The main reason is simplicity. You cannot buff yourself for 12 adventures anyways, so you would have to round up or down to 10 or 20 turns. This just adds extra complexity to the script which we are trying to avoid.

b) Second, lets look at the main logic structure. We use an IF statement to check and see if we have enough of the potion to use. If not, we buy the number we need. After that we should be pretty much guaranteed to have the item. So, we then use the right number of that item.

c) item_amount($item[xxx]) is a function that will tell you how many of an item you currently possess.

d) The buy( #, $item[xxx]) is what you use to buy stuff from the mall. You NEED to use this function as it specifically overrides your normal preferences. If you use the 'acquire' command, it will not buy from the mall if you have told KoLmafia not to (which we did in an earlier lesson).

e) The use(#, $item[xxx]) is pretty obvious. As mentioned above, we either have or have bought the appropriate number of the item to be used.

f) For the last item, you notice that I use a 2* in front of most logic. This is because that particular buff only lasts 5 adventures, whereas the others last 10 adventures. Therefore the easy solution is to use double the amount of the shorter duration buff item.

g) You can cut&paste a section and fill in ANY buff you want! I just picked the cost-effective ones for me.. and I have tons of Polka Pops. You can use Ermine Eyedrops, snowcones, or whatever. You could also use the same code to boost a Stat with potions for Sea fighting, Monster Level for Sliming, or whatever.

## Buffs Item Find in Turns*10.
void main( int numTenTurns )
{
  ## If we do not have enough Polka Pops, buy more.
  if ( item_amount( $item[Polka Pop] ) < numTenTurns )
  {
    buy( numTenTurns - item_amount( $item[Polka Pop] ), $item[Polka Pop] );
  }
  use( numTenTurns, $item[Polka Pop] );


  if ( item_amount( $item[Knob Goblin eyedrops] ) < numTenTurns )
  {
    buy( numTenTurns - item_amount( $item[Knob Goblin eyedrops] ), $item[Knob Goblin eyedrops] );
  }
  use( numTenTurns, $item[Knob Goblin eyedrops] );



  if ( item_amount( $item[Love song of disturbing obsession] ) < numTenTurns*2 )
  {
    buy( numTenTurns*2 - item_amount( $item[Love song of disturbing obsession] ), $item[Love song of disturbing obsession] );
  }
  use( numTenTurns*2, $item[Love song of disturbing obsession] );
}

Continue to Post-Lesson 2: Automatic Mall Selling