int poorTeeth = 0;
string sewers = visit_url ("adventure.php?snarfblat=166") ;
repeat 
	{
	// adventure in sewers
	visit_url ("adventure.php?snarfblat=166") ;
	// if it's a combat adventure, obey the custom combat script
	if (contains_text( sewers , "combat"))
		run_combat();
	// handles choice adventures
	string run_choice( string page_text )
		{
			while( contains_text( page_text , "choice.php" ) )
				{
 
				int begin_choice_adv_num = ( index_of( page_text , "whichchoice value=" ) + 18 );
				int end_choice_adv_num = index_of( page_text , "><input" , begin_choice_adv_num );
  
  
				string choice_adv_num = substring( page_text , begin_choice_adv_num , end_choice_adv_num );
  
				string choice_adv_prop = "choiceAdventure" + choice_adv_num;
				string choice_num = get_property( choice_adv_prop );
  
				if( choice_num == "" ) 
					abort( "Unsupported Choice Adventure!" );
  
				string url = "choice.php?pwd&whichchoice=" + choice_adv_num + "&option=" + choice_num;
				page_text = visit_url( url );
				// if you gnaw through the C.H.U.M. cage, make a note of it
				if (contains_text( sewers ,"Do you think you're popular enough") && waitForRescue == false && poorteeth < toothTolerance )
					{
					poorteeth +=1 ;
					print ("Ok, you just had to gnaw your way out of the cage.  Let's wait 30 seconds and let someone else be cage bait");
					wait (30);
					}
				if (contains_text( sewers ,"Do you think you're popular enough") && waitForRescue == false && poorteeth == toothTolerance)
					abort ("you already wasted" + toothTolerance * 10 + " turns gnawing out of the cage.  Wait an hour and run this script again, unless you want to waste even more adventures");
				// if the sewer is flooded while the script is running... abort
				if (contains_text( sewers , "terrible ghastly torrent of raw sewage"))
					abort ("Sewers are being reflooded.  Wait until the dungeon is open again");
				// if you tried to turn a valve, and all the grates are opened...
				if (contains_text (page_text, "You tug and tug, but you can't get the crank to budge"))
					grates = 20 ;
				// if you tried to lower the water level, but couldn't...
				if (contains_text (page_text, "but the level of the water doesn't change"))
					water = 20 ;
				if (turnGrates == True && grates <20)
					checkGrates ;
				if (lowerWater == True && water <20)
					checkWater ;
				}
			return page_text;
			
		}
	}
until (contains_text( sewers ,"onward and... downward!")) ;