I need help troubleshooting a script

I'm writing a script that will get keys and such for the Tower. WIP

I've got a little string param thing going. Its just the last thing to happen, even though its one of the first things being written in the script. I am looking to fix this.

Anyways. Here it is.

Code:
  #### This script will get you some of the necessary items for the tower.
  
  
  void obtain(item it, int num)
  {
    if(item_amount(it) < num)
    {
      buy( num - item_amount( it ), it );
    }
  }

  void main( string param ) {
  ## if "eat" this script will eat pies to obtain some of the keys
  ## if "adventure" this script will adventure instead
  
  
  if ( param == "eat" ) {
    if(item_amount($item[Milk of Magnesium]) < 2)  {
	   obtain( $item[Milk of Magnesium], 2 );
	   use( 2, $item[Milk of Magnesium]); }
    if(item_amount($item[Boris's key]) < 1)  {
       obtain( $item[Boris's key lime pie], 1 );
	   eat( 1, $item[Boris's key lime pie]);  }
    if(item_amount($item[Jarlsberg's key]) < 1)  {
       obtain( $item[Jarlsberg's key lime pie], 1 );
	   eat( 1, $item[Jarlsberg's key lime pie]);  }
    if(item_amount($item[Sneaky Pete's key]) < 1)  {
       obtain( $item[Sneaky Pete's key lime pie], 1 );
	   eat( 1, $item[Sneaky Pete's key lime pie]);  } }
	   
	
  ## Working on this section.	
  else if ( param == "adventure" ) {
    if(item_amount($item[Boris's key]) < 1)  {
	}
	if(item_amount($item[Jarlsberg's key]) < 1)  {
	}
	if(item_amount($item[Sneaky Pete's key]) < 1)  {
	}  }  }
	   
	      
  ## Obtain the rest of the keys.	   
    if(item_amount($item[digital key]) < 1)  {
       obtain( $item[White Pixel], 30 );
	   create(1, $item[digital key]);  }
    if(item_amount($item[Richard's star key]) < 1)  {
       obtain( $item[star chart], 1 );
       obtain( $item[star], 8 );
	   obtain( $item[line], 7 );
	   create(1, $item[Richard's star key]);  }
    if(item_amount($item[skeleton key]) < 1)  {
       obtain( $item[skeleton bone], 1 );
	   obtain( $item[loose teeth], 1 );
	   create( 1, $item[skeleton key]);  }
	   
	   
  ## Obtain misc. items
  if(item_amount($item[Wand of Nagamar]) < 1)  {
    obtain( $item[ruby W], 1 );
    obtain( $item[metallic A], 1 );
    obtain( $item[lowercase N], 1 );
    obtain( $item[heavy D], 1 );
	create( 1, $item[Wand of Nagamar] );  }
  if(item_amount($item[star hat]) < 1)  {
     obtain( $item[star chart], 1 );
     obtain( $item[star], 5 );
	 obtain( $item[line], 3 );
	 create( 1, $item[star hat]);  }
 

Catch-22

Active member
Just a note, you could use retrieve_item() instead of your obtain function. This accomplishes the same thing but will try to do it "in the least destructive manner" (usually means the cheapest).
 

Catch-22

Active member
Okay, the trouble is that a bunch of stuff is occurring outside of the main() class. Basically from your //Obtain the rest of the keys. line onwards, it's outside of the main(). KoLmafia will actually execute everything in the global space first before executing the main, but usually in the global space you'd just have things like imports and set a few constants.

I was going to rewrite your script how I would write it, but then I realized it's probably not as helpful as you improving it yourself :) Use the retrieve_item() function I mentioned above and I'd also suggest you turn your if (param == "whatever") {} logic into a switch/case. You could also break apart your main() into separate functions and call them from within your main().

Good luck :)

Edit: Well I decided to post the changes I made, the job was already half done while I was troubleshooting it anyway. I think you should give it a go yourself before seeing what I came up with :) I've attached it in the zip and the password is "itried".
 

Attachments

  • tower_items.zip
    640 bytes · Views: 29
Last edited:
Hey man thanks a lot :) And I come to the forums a lot with things that are impractical, looking for how to do something. I'll try by myself, and then look at your work.
 

lostcalpolydude

Developer
Staff member
Checking for 2 milk of magnesium looks like an assumption that the user doesn't have Impetuous Sauciness, and also an assumption that more than 10 fullness will be used (which is contradicted by the check for keys just after using milk).
 

Catch-22

Active member
If you're referring to AlvitrValkyrie's script, I agree.

If you're referring to mine, I tried not to rewrite things too much. I just made it clearer, but still do everything the original script was trying to do.

AlvitrValkyrie if you need help making your script more intuitive, let us know :)
 
I've got a question about your script. What is "switch (param)" for? I'm guessing it does a similar (or identical) thing.

Also, how do I check if someone already has the star starfish as a familiar...?
 
Last edited:
Also, in the case of Impetuous Sauciness...how about replacing the milk section with

Code:
{
if ( have_skill( $skill[ Impetuous Sauciness ] ) )  {
  retrieve_item(1, $item[Milk of Magnesium]);
  use(1, $item[Milk of Magnesium]); 
  }
  
else 
  retrieve_item(1, $item[Milk of Magnesium]);  {
  use(1, $item[Milk of Magnesium]);
  }
}
 

Catch-22

Active member
Oh I linked to the switch/case section of the wiki in my original post, it's really just for readability, it becomes easier if you wanted to add more "cases" down the track (just trying to teach good habits :)).

Also, in the case of Impetuous Sauciness...how about replacing the milk section with

Code:
{
if ( have_skill( $skill[ Impetuous Sauciness ] ) )  {
  retrieve_item(1, $item[Milk of Magnesium]);
  use(1, $item[Milk of Magnesium]); 
  }
  
else 
  retrieve_item(1, $item[Milk of Magnesium]);  {
  use(1, $item[Milk of Magnesium]);
  }
}

Yeah, now you're starting to get the hang of it :) although your else should look more like this:
Code:
else {
  retrieve_item(2, $item[Milk of Magnesium]); 
  use(2, $item[Milk of Magnesium]);
}
Notice the "{" immediately after the else? You want everything within the "{ }" to execute in the "else" case. Also I assume you meant 2 Milks if you don't have Impetuous Sauciness :)

Next step, buy/use milk only if you actually need to eat the pies!
 
Ah yes, rookie mistake.

"Next step, buy/use milk only if you actually need to eat the pies!"
Already done:

Code:
  case "eat":
    if ( have_skill( $skill[ Impetuous Sauciness ] ) )  {
      retrieve_item(1, $item[Milk of Magnesium]);
      use(1, $item[Milk of Magnesium]); 
    }
  
    else  {
      retrieve_item(2, $item[Milk of Magnesium]);
      use(2, $item[Milk of Magnesium]);
    }
 
Top