CLI Noob Questions - Boolean operators, Some basic scripting Qs

moondog

New member
Hi everyone, I wasn't sure where to post this but as I haven't actually scripted anything yet, I thought this would be the proper place.

I've been trying to learn the basics of Mafia scripting by reading a lot of the posts on this forum, the user manual, the wiki and looking at several scripts. I'm not sure I want to try scripting in ASH quite yet. I'd like to keep my scripts simple; you know...the KISS formula.

Anyway here are my questions:

1. In the user manual under flow control, what the heck do the boolean operators ( == and != ) mean?

2. This is a bit more convoluted and is a multi-multi-part question. It has to do with the 'use' command and that it tries to 'acquire' the item before said item is used, no matter if it is in your inventory or if it has to be bought from the mall, etc. I'd like to 'use' several items ONLY if they are in my inventory?. Specifically, I'd like to 'use' black paisley oyster egg ONLY if they are in my inventory, Same thing for a black striped oyster egg
Similarly, I'd like to do the same thing with red, green, blue snowcones and green, lavender, pink candy hearts.

For the oyster eggs, would the code be simply:
if black paisley oyster egg >0; use * black paisley oyster egg;
if black striped oyster egg >0; use * black striped oyster egg;

...How 'bout for the snowcones?
if red snowcone >0; use 1 red snowcone;
if green snowcone >0; use 1 green snowcone;
if blue snowcone >0; use 1 blue snowcone;
...I know the snowcones are different - KOL will allow only 1 snowcone in use at a time, therefore I'm hoping this is sufficient. However, will I get an error message? For instance, if i have a red snowcone in inventory and use it, will the green and blue snowcone lines return an error message?

As for the candy hearts, I'm not really sure about this:
if green candy heart >0; use * green candy heart;
if lavender candy heart >0; use * lavender candy heart;
if pink candy heart >0; use * pink candy heart;

Are these viable code?

Thanks for your input

Moondog
 

Nightmist

Member
[quote author=moondog link=topic=772.msg3713#msg3713 date=1171531713]
*Snippy*
[/quote]
1. "==" is just "Equal to" and "!=" is "Not equal to" (Not sure if you need to use "==" instead of a single "=" anymore but that change might of been ASH only? I'm not sure)

2. While I'm more of a ASH person so your CLI scripts draw blanks from me, they "look" alright but I'm not too sure here.
 

holatuwol

Developer
[quote author=moondog link=topic=772.msg3713#msg3713 date=1171531713]Are these viable code?[/quote]
No; if statements don't work the way you think they do.  Contrary to what you seem to think, though, if you omit them entirely, the script will work correctly due to KoLmafia's built-in consumption limit detection.
 

moondog

New member
Whew, thanks Nightmist, that cleared that up! (But would'nt <> be the same as !=)?

Holatuwol:
OK, now i'm confused.
No; if statements don't work the way you think they do.
???

From the kolmafia end-user manual on basic scripting;
KoLmafia also provides flow control statements for programmer types who do not yet feel like
transitioning to the ASH. Note, however, please note that only the line following the conditional
statement will be executed; therefore, if you wish to execute a series of commands, all of the
commands which activate on the conditional must be combined on one line using semi-colons, or
all of the commands must be inside a single "script".

I assumed the IF statement was a conditional test similar to an IF/THEN statement in BASIC and
that the THEN part was implicit.

Am I assuming things I should'nt?

Maybe it's a grammar/syntax error on my part but according to the instructions, it should'nt matter wether I include what I want to happen after the IF statement on the same line as the IF statement or enter it on a separate line directly after the IF condition.

If this isn't the case, then how does the IF statement work?

Also:
Contrary to what you seem to think, though, if you omit them entirely, the script will work correctly due to KoLmafia's built-in consumption limit detection.

From the kolmafia end-user manual on basic scripting;
This pertains to the USE command:
KoLmafia implicitly uses the "acquire" command when attempting to use the item

This pertains to the ACQUIRE command:
Attempts to acquire the specified number of the specified item by any means possible.
, which includes buying said item at the mall

It seems to me that if I eliminate the IF statement AND don't have the item, then the use (implicit acquire) statement will automatically come into play and attempt to get the item by any means necessary, including buying it at the mall

I don't want to acquire said item by any means possible.

I understand the Kolmafia's consumption limit detection, however, all I'm looking to do is check
the item's availability against my inventory, then use it IF and only IF it's in my inventory.

Again, maybe my grammer/syntax was in error.

Help me out here, I need some enlightenment.
 
Code:
use * something
when a * is placed where the quantity goes, kolmafia uses all that you have. effectively kolmafia auto-replaces * with the quantity you have of the item.


Code:
if red snowcone >0; use 1 red snowcone;
hmm, red snowcone is item number 1415 so isn't it always greater than 0? Oh wait a minute...you want to know how many is in your display case...well that might be 0, or could it be the quantity in your store? closet?

OK I know you want how many you have in your inventory, so when the next version of kolmafia is released you might try:

PHP:
use * black paisley oyster egg;
use * black striped oyster egg;

<inline-ash-script>
if( item_amount($item[red snowcone]) >0)
  { 
  use(1, $item[red snowcone]); 
  }
  else
  {
  if(  item_amount($item[green snowcone]) >0) 
    {
    use(1, $item[green snowcone]);
    }
    else
    {
    if(  item_amount($item[blue snowcone]) >0) 
      {
      use(1, $item[blue snowcone]);
      }
    }
  }
</inline-ash-script>

use * green candy heart;
use * lavender candy heart;
use * pink candy heart;

as mentioned this ability wont be here till the next version, and you might want to add a check for a colored tongue in there to avoid that potential issue.
 

Nightmist

Member
[quote author=efilnikufecin link=topic=772.msg3736#msg3736 date=1171712902]
*snip*
[/quote]
Uhh whats wrong with using "if itemname > 0"? I mean the last time I used CLI scripts it took that as the number in your inventory?
 

holatuwol

Developer
Maybe it's a grammar/syntax error on my part but according to the instructions, it shouldn't matter whether I include what I want to happen after the IF statement on the same line as the IF statement or enter it on a separate line directly after the IF condition.

If this isn't the case, then how does the IF statement work?


I didn't think I was that vague, but apparently I was.  The "following line" means "the line following the line which contains the if-statement".  Chaining multiple commands together allows you to associate multiple commands with the same if-statement, not associate the current line with the if-statement.

So, it does matter if you include the commands on the same line as the if-statement.  I've posted a more clear description in the secret features thread since it appears this is a secret feature.



I understand the Kolmafia's consumption limit detection, however, all I'm looking to do is check the item's availability against my inventory, then use it IF and only IF it's in my inventory.

Oh. You're using use 1 instead of use *. In that case, you're right, it would buy stuff from the mall.
 
I'm bringing this question here because it's on topic for the thread.

[quote author=holatuwol link=topic=730.msg3738#msg3738 date=1171735387]
I think the following script clearly explains what KoLmafia's basic CLI scripting engine does when it encounters an if-statement (similar arguments apply for a while, but it's slightly harder to illustrate a while example with fake data):


PHP:
if level < 400; if level < 300; echo SRSLY. 
echo ORLY?; echo YARLY. 
echo NOWAI!

Contrary to what you might expect, the script above prints out "ORLY? YARLY. NOWAI! SRSLY."

Contrary to what you might expect, the script above prints out "ORLY? YARLY. NOWAI! SRSLY."

An if/while branch in a basic script tells the script interpreter to read in the next full line and execute it if the condition is true and discard it if the condition is false. If there is no such line, it's as if the if-statement didn't exist. So, after handling the next line according to the result of the conditional, the next command on the same line line as the if-statement is executed, if available
[/quote]

OK, based on the description given including the "read in the next full line" part I attempt to process this script in my mind and come up with an output of: "ORLY? YARLY. SRSLY. NOWAI!" What am I missing? Why does it print "NOWAI!" before "SRSLY."?

[quote author=Nightmist link=topic=772.msg3737#msg3737 date=1171720652]

Uhh whats wrong with using "if itemname > 0"? I mean the last time I used CLI scripts it took that as the number in your inventory?
[/quote]

Thinking back to the knob goblin perfume issue, I think that it is allowed. The issue was is it the effect or the item?
 

holatuwol

Developer
efilnikufecin said:
OK, based on the description given including the "read in the next full line" part I attempt to process this script in my mind and come up with an output of: "ORLY? YARLY. SRSLY. NOWAI!" What am I missing? Why does it print "NOWAI!" before "SRSLY."?

When a line is "read in" it is consumed from the input stream, therefore it is no longer the "next line" because it no longer exists.  The actual "next line" is the line that isn't consumed.  (Edit: I'll go ahead and clarify that in the secret features thread.)

So when the interpreter sees the second "if level < 300" statement, it reads in the next line (echo NOWAI!), executes it because the condition is true, then comes back to the echo SRSLY, because that's the next command on the same line as the if-statement.

As mentioned, this means that the intent of if-statements in the CLI can easily be obfuscated when you try to do stuff like put a bunch of them all on one line.  Why does it work that way?  Because when it was implemented, that made the most sense to me.
 

holatuwol

Developer
Now, I could change things to work where if there's more stuff on the current line, it's assumed the person writing the script means for it to be the current line rather than the following line, if that seems to be what people intuitively think KoLmafia should do (since the issue has come up before).

That changes the output of the sample script (and it messes up the way the "repeat" command currently works, which can also be really obfuscated).  But, I'm fine with it either way, since I never use semicolons on the same line as the if-statement, so a change wouldn't impact me at all.
Okay, rather than leaving a confusing post online and possibly raising other "I didn't think it worked that way" questions, I've made KoLmafia's if-handling ... less obfuscated, I guess you could say.  Things on the same line as the if-statement will be treated as "what you want to execute", and if there's nothing on that line, it behaves as it did before.

Edit: It goes without saying, but this will be present in the next release.
 

Nightmist

Member
Ohhh I get what was the problem now, heh I always just assumed that Ifs ran "what is the next command" rather then "what is the next command on the next line". (It never really proved to be a problem for me since I never "chained" commands//ifs on the same line, I've always had my separated so it always worked as I had expected, thus I never got prompted to think the command worked in a different manner)
 

holatuwol

Developer
Nested if's should already work in the current version (provided everything is on a separate line). I'm taking out "while" from the next release, though, because it has always been funky.
 

moondog

New member
Thank you all very much.

What great teachers you all are. The If statement is no longer a mystery.

My apologies to all if I seemed like a dunce; I'm not, really. Not usually, anyway.
...OK, OK, maybe a little. :D

I'll get better, really I will...with a lot of time and adult beverages. ;)

I'd like to echo Daychilde's comments about Kolmafia. It ROCKS! As does this community!

Holatuwol, you da man!
 
Top