Looking to condense a script a little.

I've got a diet script.

Code:
void main ()
{  
  #### Obtain Ode to Booze for drinking.
  ## Modify by sending the right meat amount to your buffBot of choice.
  if(have_effect($effect[Ode to Booze]) < 1)
  {
    cli_execute("csend 1 meat to Testudinata");
  }
  
  
  #### Wait for the buff to appear.
  if(have_effect($effect[Ode to Booze]) < 1)
  {
   repeat {wait(5); refresh_status();}
   until (have_effect($effect[Ode to Booze]) > 1);
  }
  
  
  #### Buy/Use [Potion] Milks of Magnesia
  if(item_amount($item[Milk of Magnesium]) < 2)
  {
    buy( 2 - item_amount( $item[Milk of Magnesium]) , $item[Milk of Magnesium]);
  }
  use(2, $item[Milk of Magnesium]);
  
  
  #### Buy [Usable] Ol' Scratch's salad forks
  if(item_amount($item[Ol' Scratch's salad fork]) < 3)
  {
	buy( 3 - item_amount($item[Ol' Scratch's salad fork]) , $item(Ol' Scratch's salad fork);
  }
  
  
  #### Buy [Usable] Munchies Pills
  if(item_amount($item[Munchies Pill]) < 3)
  {
	buy( 3 - item_amount($item[Munchies Pill]) , $item(Munchies Pill);
  }
  
  
  #### Buy [Usable] Frosty's frosty mugs
  if(item_amount($item[Frosty's frosty mug]) < 5)
  {
	buy( 5 - item_amount($item[Frosty's frosty mug]) , $item(Frosty's frosty mug);
  }
  
  
  #### Buy [Usable] Scrolls of Drastic Healing
  if(item_amount($item[Scroll of Drastic Healing]) < 8)
  {
    buy (8 - item_amount($item[Scroll of Drastic Healing]) , &item(Scroll of Drastic Healing);
  }
  
  
  #### Buy [Food] Extra-Greasy Sliders
  if(item_amount($item[Extra-Greasy Slider]) < 3)
  {
    buy( 3 - item_amount($item[Extra-Greasy Slider]) , $item[Extra-Greasy Slider]);
  }
 
  
  #### Buy [Booze] Jars of Fermented Pickle Juice & Hodgman's Blankets
  if(item_amount($item[Jar of Fermented Pickle Juice]) < 3)
  {
    buy( 3 - item_amount($item[Jar of Fermented Pickle Juice]) , $item[Jar of Fermented Pickle Juice]);
  }
  if(item_amount($item[Hodgman's blanket]) < 2)
  {
    buy( 2 - item_amount($item[Hodgman's blanket]) , $item[Hodgman's Blanket]);
  }
  
  
  #### Buy [Usable] Prismatic Wads
  if(item_amount($item[Prismatic Wad]) < 15)
  {
    buy( 15 - item_amount($item[Prismatic Wad]) , $item[Prismatic Wad]);
  }
  
  
  #### Use [Usable] 5 Prismatic Wads.
  use(5, $item[Prismatic Wad]);
  
  
  #### Eat [Food] Extra-Greasy Sliders w/forks/munchies
  use(1, $item[Munchies Pill]);
  use(1, $item[Ol' Scratch's salad fork]);
  eat(1, $item[Extra-Greasy Slider]);
  use(1, $item[Scroll of Drastic Healing]);
  use(1, $item[Munchies Pill]);
  use(1, $item[Ol' Scratch's salad fork]);
  eat(1, $item[Extra-Greasy Slider]);
  use(1, $item[Scroll of Drastic Healing]);
  use(1, $item[Munchies Pill]);  
  use(1, $item[Ol' Scratch's salad fork]);
  eat(1, $item[Extra-Greasy Slider]);
  use(1, $item[Scroll of Drastic Healing]); 
  
  
  #### Use [Usable] 5 Prismatic Wads.
  use(5, $item[Prismatic Wad]);
  
  
  #### Drink [Booze] Jars of Fermented Pickle Juice & Hodgman's Blanket w/mugs
  use(1, $item[Frosty's frosty mug]);
  drink(1, $item[Jar of Fermented Pickle Juice]);
  use(1, $item[Scroll of Drastic Healing]);
  use(1, $item[Frosty's frosty mug]);
  drink(1, $item[Jar of Fermented Pickle Juice]);
  use(1, $item[Scroll of Drastic Healing]);
  use(1, $item[Frosty's frosty mug]);
  drink(1, $item[Jar of Fermented Pickle Juice]);
  use(1, $item[Scroll of Drastic Healing]);
  use(1, $item[Frosty's frosty mug]);
  drink(1, $item[Hodgman's Blanket]);
  use(1, $item[Scroll of Drastic Healing]);
  use(1, $item[Frosty's frosty mug]);
  drink(1, $item[Hodgman's Blanket]);
  use(1, $item[Scroll of Drastic Healing]);
  
  
  #### Use [Usable] 5 Prismatic Wads.
  use(5, $item[Prismatic Wad]);
 
 
  #### This will print the message in the ""
  print("Blargh blargh blargh.");
}


As you can see, the code gets a little redundant when it comes to sliders, forks, etc. Is it possible to tell the code to do a set of actions multiple times? IN what ways can I make the code faster?
 

Theraze

Active member
Don't Munchies queue? But anyways...

Could do something like
Code:
for randomnumber from 1 to 3 {
  use(1, $item[Munchies Pill]);
  use(1, $item[Ol' Scratch's salad fork]);
  eat(1, $item[Extra-Greasy Slider]);
}
 
Hmmm okay. Just tested the script and it fails when it comes to the salad forks because they must be eaten. Is it possible to call the script from a specific line without running the whole thing over again?
 

roippi

Developer
you could make an item=>int map, and iterate over the keyset to see if item_amount(key) > value. If not, buy the difference.

I don't know if that will speed up execution time any, but it's cleaner to look at.

edit: oh, you were looking to clean up the eating portion.
 
Last edited:

Grotfang

Developer
You also have repeated checks that can be abstracted out to a function:

Code:
void obtain( item it, int num ) {
	if( item_amount( it ) < num )
		buy( num - item_amount( it ), it );
}

void main() {  
	if( have_effect( $effect[Ode to Booze] ) < 1 )
		cli_execute( "csend 1 meat to Testudinata" );
	while( have_effect( $effect[Ode to Booze] ) < 1 ) {
		refresh_status();
		wait( 5 ); 
	}
	# Acquire items
	obtain( $item[Milk of Magnesium], 2 );
	obtain( $item[Hodgman's blanket], 2 );
	foreach i in $items[Ol' Scratch's salad fork, Munchies Pill, Extra-Greasy Slider, Jar of Fermented Pickle Juice]]
		obtain( i, 3 );
	obtain( $item[Frosty's frosty mug], 5 );
	obtain( $item[Scroll of Drastic Healing], 8 );
	obtain( $item[Prismatic Wad], 15 );
	# Initial spleen
	use( 5, $item[Prismatic Wad] );
	# Eat and spleen
	for i from 1 to 3 {
		use( 1, $item[Munchies Pill] );
		eat( 1, $item[Ol' Scratch's salad fork] );
		eat( 1, $item[Extra-Greasy Slider] );
		use( 1, $item[Scroll of Drastic Healing] );
	}
	use( 5, $item[Prismatic Wad] );
	# Drink and spleen
	for i from 1 to 5 {
		use( 1, $item[Frosty's frosty mug] );
		drink( 1, $item[Jar of Fermented Pickle Juice] );
		use( 1, $item[Scroll of Drastic Healing] );
	}
	use(5, $item[Prismatic Wad]);
	# Done
	print("Blargh blargh blargh.");
}
 
Wow. That is insane. Will take me a little time to understand, but I get the jist of it.

Code:
foreach i in $items[Ol' Scratch's salad fork, Munchies Pill, Extra-Greasy Slider, Jar of Fermented Pickle Juice]]

Is the double ] at the end supposed to be like that? If yes, what does having two in that instance do?
what does the "i" stand for?
 
Last edited:

Bale

Minion
The i is simply a variable that can be anything. It's purpose is to be used in the following line. As long as the same variable is used in both places it can be any combination of characters and underscores.

The double ] is a typo. Only use one.
 
Yes I look at other scripts and try to understand :)
And thanks for the link!


Okay, so here's the code. It would be great if someone could do a quick glance-over.


Code:
void obtain(item it, int num) 
{
  if(item_amount(it) < num)
  {
    buy( num - item_amount( it ), it );
  }
}


void main ()
{  
  #### Que a buffbot for Ode & wait for the buff to appear.
  ## Modify by sending the right meat amount to your buffBot of choice.
  if(have_effect($effect[Ode to Booze]) < 1)
  {
    cli_execute("csend 1 meat to Testudinata");
  }
  if(have_effect($effect[Ode to Booze]) < 1)
  {
    repeat {wait(5); refresh_status();}
    until (have_effect($effect[Ode to Booze]) > 1);
  }
  
  
  #### Buy all the required items
  obtain( $item[Milk of Magnesium], 2 );
  obtain( $item[Hodgman's blanket], 2 );
  foreach x in $items[Ol' Scratch's salad fork, Munchies Pill, Extra-Greasy Slider, Jar of Fermented Pickle Juice]
  {
    obtain( x, 3 );
  }
  obtain( $item[Frosty's frosty mug], 5 );
  obtain( $item[Scroll of Drastic Healing], 8 );
  obtain( $item[Prismatic Wad], 15 );
  
  
  #### Eat/Spleen/Heal
  use(2, $item[milk of magnesium]);
  use(5, $item[prismatic wad]);
  for 3 
  {
    use(1, $item[Munchies Pill]);
    eat(1, $item[Ol' Scratch's salad fork]);
    eat(1, $item[Extra-Greasy Slider]);
    use(1, $item[Scroll of Drastic Healing]);
  }
  use(5, $item[prismatic was]);
  
  #### Drink the Jars and Blankets w/Ode/mugs & Use scrolls for healing
  #### Spleen after drinking
  for 3
  {
    drink(1, $item[Frosty's frosty mug]);
    drink(1, $item[Jar of Fermented Pickle Juice]);
    use(1, $item[Scroll of Drastic Healing]);
  }
  for 2
  {
    drink(1, $item[Frosty's frosty mug]);
    drink(1, $item[Hodgman's Blanket]);
    use(1, $item[Scroll of Drastic Healing]);
  }
  use(5, $item[prismatic wad]);
}


Couple things I have questions about:

Code:
  foreach x in $items[Ol' Scratch's salad fork, Munchies Pill, Extra-Greasy Slider, Jar of Fermented Pickle Juice]
  {
    obtain( x, 3 );
  }

Does the obtain( x, 3 ); work with the first void(), as in will it only buy the number of those items required or will it buy 3 of each no matter what? I'm guessing the first. Just makin sure.

Second:

Code:
  for 3
  {
    drink(1, $item[Frosty's frosty mug]);
    drink(1, $item[Jar of Fermented Pickle Juice]);
    use(1, $item[Scroll of Drastic Healing]);
  }

Does "for 3" mean "3 times" or something?

And thanks ^^
 

roippi

Developer
Couple things I have questions about:

Does the obtain( x, 3 ); work with the first void(), as in will it only buy the number of those items required or will it buy 3 of each no matter what? I'm guessing the first. Just makin sure.

The foreach is a loop that will plug each of the items in that list into x, one after another. It calls the function obtain() on each of them. Look up in the script for what obtain() does.
Second:

Code:
  for 3
  {
    drink(1, $item[Frosty's frosty mug]);
    drink(1, $item[Jar of Fermented Pickle Juice]);
    use(1, $item[Scroll of Drastic Healing]);
  }

Does "for 3" mean "3 times" or something?

"for 3" is meaningless. You need to re-look at what grotfang posted.
 

lostcalpolydude

Developer
Staff member
You can use a hundred munchies pills all at once and then be good to go for the next several weeks. It's tracked by mafia in munchiesPillsUsed.
 

Grotfang

Developer
Code:
for x from 1 to 3 {

So that should do it? How exactly does that line of code work?

That is the correct expression.

The way it works is that it iterates through the expression for each of the values of x. It just happens that you don't use x.

Code:
for i from 1 to 3 {
  print( i );
}

This will produce:

Code:
1
2
3

In your code, we use the fact that it will go through the expression 3 times, but don't do anything with the value of i.

Finally, sorry about the double ] in the example above. Sometimes I forego using verify and thereby produce invalid code. It is lazy and I apologise.
 

Theraze

Active member
Bah, I gave that example back in post 2. ;) But yeah, for loops are a great way of running multiple iterations, and you can debug based on that as well... in the for x example, you could do a
Code:
print("We're eating now... step "+x);
or something similar to see which step you're on if something is going wrong.
 
Bah, I gave that example back in post 2. ;) But yeah, for loops are a great way of running multiple iterations, and you can debug based on that as well... in the for x example, you could do a
Code:
print("We're eating now... step "+x);
or something similar to see which step you're on if something is going wrong.

Yea I know. I was actually looking at what you said, but was a little confused.

Anyways. The script works! Thanks everyone.
 

Grotfang

Developer
Bah, I gave that example back in post 2. ;)

Sorry Theraze -- wasn't trying to steal credit for that. I had thought I'd made it clear from "You also have repeated checks that can be abstracted out to a function:" that it was the "void obtain" function that I was adding.

@AlvitrValkyrie:

Yes, it is a simple way of making something happen multiple times, but in this instance it isn't what I personally would use. I would actually tie the loop in with my drunkeness and fullness, since that is what is actually making you repeat the action a set number of times. However, that is NOT something to worry about now and is probably me being overly obsessive about avoiding "magic numbers".
 

Theraze

Active member
Hehe. No big, was just like... wait a sec, that looks familiar. ;)

And yeah... like Grotfang says, best way if you're looking for the script to be extendable is to work based on available room left, but that takes a bit more thinking, and only helps if you're actually wanting to extend it later. If you're just using this for aftercore non-teetotaller/oxygen runs, your liver should consistently have the same room, so you don't need to worry (too much) about overdrinking improperly. If you're using this during SC runs, or you want this to work after HCT or HCO runs...
 
Top