<?php
/*	program TURN.C						*\
\*	WARNING: "advent.c" allocates GLOBAL storage space by	*\
\*		including "advdef.h".				*\
\*		All other modules use "advdec.h".		*/


//#include	"stdio.h"	/* drv = 1.1st file 2.def 3.A	*/
//#include	"advent.h"
//#include	"advdec.h"

//extern	int	printf();


/*
	Routine to take 1 turn
*/
function turn()
{
global $newloc,$closing,$loc,$panic,$clock2,$cond,$NOPIRAT,$DWARFMAX,$odloc,$dseen,$turns,$newloc,$wzdark,$oldloc2;
global $visited,$closed,$prop,$OYSTER,$MAXOBJ,$knfloc,$dbugflg,$verb,$object,$motion;
global $wordsentered;

if ($wordsentered || $turns==1) { // if the global wordsentered is set then the user has typed a command, otherwise
                     // we need to prompt them to enter some
	$i=0;

	/*
		if closing, then he can't leave except via
		the main office.
	*/
	if ($newloc < 9 && $newloc != 0 && $closing) {
		rspeak(130);
		$newloc = $loc;
		if (!$panic)
			$clock2 = 15;
		$panic = 1;
	}
	/*
		see if a dwarf has seen him and has come
		from where he wants to go.
	*/
	if ($newloc != $loc && !forced($loc) && $cond[$loc]&$NOPIRAT  ==  0)
		for ($i = 1; $i< ($DWARFMAX-1); ++$i)
			if ($odloc[$i] == $newloc && $dseen[$i]) {
				$newloc  =  $loc;
				rspeak(2);
				break;
			}

	dwarves();	/* & special dwarf(pirate who steals)	*/

	/* added by BDS C conversion */
	if ($loc != $newloc || $turns==1) {
		++$turns;
		$loc = $newloc;
/*	causes occasional "move" with two describe & descitem	*/
/*	}	*/			/* if (loc != newloc)	*/

        	/* check for death */
        	if ($loc == 0) {
        		death(); // does not return
        	}

        	/* check for forced move */
        	if (forced ($loc)) {
        		describe();
        		domove();
        		$words="";
        	}

        	/* check for wandering in dark */
        	if ($wzdark && dark() && pct(35)) {
        		rspeak(23);
        		$oldloc2 = $loc;
        		death(); // does not return
        	}
        	/* describe his situation */
        	describe();
        	if (!dark ()) {
        		++$visited[$loc];
        		descitem();
        	}
/*	causes occasional "move" with no describe & descitem	*/
	}				/* if (loc != newloc)	*/

	if ($closed) {
		if ($prop[$OYSTER] < 0 && toting ($OYSTER))
			pspeak($OYSTER, 1);
		for ($i = 1; $i <= $MAXOBJ; ++$i)
			if (toting ($i) && $prop[$i] < 0)
				$prop[$i] = -1-$prop[$i];
	}

	$wzdark = dark();
	if ($knfloc > 0 && $knfloc != $loc)
		$knfloc  =  0;

	stimer();	/* as the grains of sand slip by	*/

} // end of "if words entered"
        $words="";
	$wordsentered=english();

	if ($wordsentered) {	/* retrieved player instructions	*/

 	  if ($dbugflg)
		printf("loc = %d, verb = %d, object = %d, ".
		       "motion = %d<br>", $loc, $verb, $object, $motion);

	  if ($motion)		/* execute player instructions	*/
		domove();
	  else if ($object)
		doobj();
	  else
		itverb();
	}
	
	return $wordsentered;
}

/*
	Routine to describe current location
*/
function describe()
{
global $BEAR,$visited,$loc,$closing;
	if (toting($BEAR))
		rspeak(141);
	if (dark())
		rspeak(16);
	else if ($visited[$loc])
		descsh($loc);
	else
		desclg($loc);
	if ($loc == 33 && pct(25) && !$closing)
		rspeak(8);
}

/*
	Routine to describe visible items
*/
function descitem()
{
global $MAXOBJ,$STEPS,$NUGGET,$prop,$closed,$RUG,$CHAIN,$tally,$loc,$STEPS,$fixed,$tally2,$tally,$limit,$dbugflg;
	$i= $state=0;

	for ($i = 1;$i<$MAXOBJ; ++$i) {
		if (at($i)) {
			if ($i == $STEPS && toting($NUGGET))
				continue;
			if ($prop[$i]<0) {
				if ($closed)
					continue;
				else {
					$prop[$i] = 0;
					if ($i == $RUG || $i == $CHAIN)
						++$prop[$i];
					--$tally;
				}
			}
			if ($i == $STEPS && $loc == $fixed[$STEPS])
				$state = 1;
			else
				$state = $prop[$i];
			if ($dbugflg) print "pspeak(i,state)=($i,$state)<br>";
			pspeak($i, $state);
		}
	}
	if ($tally == $tally2 && $tally != 0 && $limit > 35)
		$limit = 35;
}

/*
	Routine to handle motion requests
*/
function domove()
{
global $loc,$motion,$NULLX,$BACK,$LOOK,$detail,$wzdark,$visited,$newloc,$CAVE,$oldloc2,$oldloc;
	gettrav($loc);
	switch($motion) {
	case $NULLX:
		break;
	case $BACK:
		goback();
		break;
	case $LOOK:
		if ($detail++<3)
			rspeak(15);
		$wzdark = 0;
		$visited[$loc] = 0;
		$newloc = $loc;
		$loc = 0;
		break;
	case $CAVE:
		if ($loc<8)
			rspeak(57);
		else
			rspeak(58);
		break;
	default:
		$oldloc2 = $oldloc;
		$oldloc = $loc;
		dotrav();
	}
}

/*
	Routine to handle request to return
	from whence we came!
*/
function goback()
{
global $forced,$oldloc,$oldloc2,$loc,$travelcond,$traveldest,$travelverb,$motion;
	$kk= $k2= $want= $temp=0;
//	struct trav strav[MAXTRAV];
        $stravcond=$stravdest=$stravverb=array($MAXTRAV);
        

	if (forced($oldloc))
		$want = $oldloc2;
	else
		$want = $oldloc;
	$oldloc2 = $oldloc;
	$oldloc = $loc;
	$k2 = 0;
	if ($want == $loc) {
		rspeak(91);
		return;
	}
	//copytrv(travel, strav);
	$stravcond=$travelcond;
	$stravdest=$traveldest;
	$stravverb=$travelverb;

	for ($kk = 0; $traveltdest[$kk] != -1; ++$kk) {
		if (!$traveltcond[$kk] && $traveltdest[$kk] == $want) {
			$motion = $traveltverb[$kk];
			dotrav();
			return;
		}
		if (!$traveltcond[$kk]) {
			$k2 = $kk;
			$temp = $traveltdest[$kk];
			gettrav($temp);
			if (forced($temp) && $traveltdest[0] == want)
				$k2 = $temp;
			//copytrv(strav, travel);
	                $travelcond=$stravcond;
	                $traveldest=$stravdest;
	                $travelverb=$stravverb;
		}
	}
	if ($k2) {
		$motion = $traveltverb[$k2];
		dotrav();
	}
	else
		rspeak(140);
}

/*
	Routine to copy a travel array
*/
//function copytrv(trav1, trav2)
//{
//	int i;

//	for (i = 0; i<MAXTRAV; ++i) {
//		trav2 -> tdest = trav1 -> tdest;
//		trav2 -> tverb = trav1 -> tverb;
//		trav2 -> tcond = trav1 -> tcond;
//	}
//}

/*
	Routine to figure out a new location
	given current location and a motion.
*/
function dotrav()
{
global $newloc,$loc,$traveltdest,$traveltverb,$traveltcond,$rcond,$dbugflg,$motion,$prop;

	$mvflag= $hitflag= $kk=0;
	$rdest= $rverb= $rcond= $robject=0;
	$pctt=0;

	$newloc = $loc;
	$mvflag = $hitflag = 0;
	$pctt = rand(0,100)%100;

	for ($kk = 0; $traveltdest[$kk]>=0 && !$mvflag; ++$kk) {
		$rdest = $traveltdest[$kk];
		$rverb = $traveltverb[$kk];
		$rcond = $traveltcond[$kk];
		$robject = $rcond%100;

		if ($dbugflg)
			printf("rdest = %d, rverb = %d, rcond = %d,".
			"robject = %d in dotrav<br>", $rdest, $rverb,
			$rcond, $robject);
		if (($rverb != 1) && ($rverb != $motion) && !$hitflag)
			continue;
		++$hitflag;
		switch(floor($rcond/100)) {
		case 0:
			if (($rcond == 0) || ($pctt < $rcond))
				++$mvflag;
			if ($rcond && $dbugflg) {
				buffprint(sprintf("%% move pct:%d mvflag:%d rcond:%d",$pctt, $mvflag, $rcond));
				buffprint("\n");
			}
			break;
		case 1:
			if ($robject == 0)
				++$mvflag;
			else if (toting($robject))
				++$mvflag;
			break;
		case 2:
			if (toting($robject) || at($robject))
				++$mvflag;
			break;
		case 3:
		case 4:
		case 5:
		case 7: 
			if ($prop[$robject] != floor(($rcond/100))-3)
				++$mvflag;
			break;
		default: {print floor($rcond/100); bug(37);}
		}
	}
	if (!$mvflag)
		badmove();
	else if ($rdest>500)
		rspeak($rdest-500);
	else if ($rdest>300)
		spcmove($rdest);
	else {
		$newloc = $rdest;
		if ($dbugflg)
			printf("newloc in dotrav = %d<br>", $newloc);
	}
}

/*
	The player tried a poor move option.
*/
function badmove()
{
global $motion,$verb,$FIND,$INVENTORY;
	$msg=0;

	$msg = 12;
	if ($motion >= 43 && $motion <=50) $msg = 9;
	if ($motion == 29 || $motion == 30) $msg = 9;
	if ($motion == 7 || $motion == 36 || $motion == 37) $msg = 10;
	if ($motion == 11 || $motion == 19) $msg = 11;
	if ($verb == $FIND || $verb == $INVENTORY) $msg = 59;
	if ($motion == 62 || $motion == 65) $msg = 42;
	if ($motion == 17) $msg = 80;
	rspeak($msg);
}

/*
	Routine to handle very special movement.
*/
function spcmove($rdest)
{
global $holding,$EMERALD,$newloc,$loc,$prop,$TROLL,$MAXOBJ,$TROLL2,$CHASM,$BEAR,$SPICES,$tally2,$oldloc2;
	switch($rdest-300) {
	case 1:  /* plover movement via alcove */
		if (!$holding || ($holding == 1 && toting($EMERALD)))
			$newloc = (99+100)-$loc;
		else
			rspeak(117);
		break;
	case 2:  /* trying to remove plover, bad route */
		drop($EMERALD, $loc);
		break;
	case 3:  /* troll bridge */
		if ($prop[$TROLL] == 1) {
			pspeak($TROLL, 1);
			$prop[$TROLL] = 0;
			move($TROLL2, 0);
			move(($TROLL2+$MAXOBJ), 0);
			move($TROLL, 117);
			move(($TROLL+$MAXOBJ), 122);
			juggle($CHASM);
			$newloc = $loc;
		}
		else {
			$newloc = ($loc == 117 ? 122 : 117);
			if ($prop[$TROLL] == 0)
				++$prop[$TROLL];
			if (!toting ($BEAR))
				return;
			rspeak(162);
			$prop[$CHASM] = 1;
			$prop[$TROLL] = 2;
			drop($BEAR, $newloc);
			$fixed[$BEAR] = -1;
			$prop[$BEAR] = 3;
			if ($prop[$SPICES]<0)
				++$tally2;
			$oldloc2 = $newloc;
			death();
		}
		break;
	default:
		bug(38);
	}
}


/*
	Routine to handle player's demise via
	waking up the dwarves...
*/
function dwarfend()
{
	death();
	normend();
}

/*
	normal end of game
*/
function normend()
{
	score();	
	doexit();
}

function doexit(){
  dumpscreen(); // flush output buffer before finishing
  // The next part scrolls the window to bottom of screen [if javascript on, otherwise do it manually!]
  // so you can see your score as browsers always display from the top of the screen
  echo '
<script language=javascript>
<!--
  self.scrollTo(0,999999); // scrollTo(x,y) - y is so big it is undoubtedly beyond the bottom
-->
</script>'; 
  die("<a href=?reset=1>Click here to re-start</a><br>"); // don't return to main program
}

/*
	scoring
*/
function score()
{
global $MAXTRS,$CHEST,$prop,$place,$MAXDIE,$numdie,$gaveup,$dflag,$closing,$closed,$bonus,$place,$MAGAZINE;
	$t= $i= $k= $s=0;
	$s = $t = 0;
	for ($i = 50; $i<=$MAXTRS; ++$i) {
		if ($i == $CHEST)
			$k = 14;
		else if ($i > $CHEST)
			$k = 16;
		else 
			$k = 12;
		if ($prop[$i] >= 0)
			$t += 2;
		if ($place[$i] == 3 && $prop[$i] == 0)
			$t += $k-2;
	}
	buffprint(sprintf("%-20s%d", "Treasures:", $s = $t));
	buffprint("\n");
	$t = ($MAXDIE - $numdie)*10;
	if ($t) {
		buffprint(sprintf("%-20s%d", "Survival:", $t));
		buffprint("\n");
	}
	$s += $t;
	if (!$gaveup)
		$s += 4;
	$t = $dflag ? 25 : 0;
	if ($t) {
		buffprint(sprintf("%-20s%d", "Getting well in:", $t));
		buffprint("\n");
	}
	$s += $t;
	$t = $closing ? 25 : 0;
	if ($t) {
		buffprint(sprintf("%-20s%d", "Masters section:", $t));
		buffprint("\n");
	}
	$s += $t;
	if ($closed) {
		if ($bonus == 0)
			$t = 10;
		else if ($bonus == 135)
			$t = 25;
		else if ($bonus == 134)
			$t = 30;
		else if ($bonus == 133)
			$t = 45;
		buffprint(sprintf("%-20s%d", "Bonus:", $t));
		buffprint("\n");
                $s += $t;
	}
	if ($place[$MAGAZINE] == 108)
		$s += 1;
	$s += 2;
	buffprint(sprintf("%-20s%d", "Score:", $s));
	buffprint("\n");
}

/*
	Routine to handle the passing on of one
	of the player's incarnations...
*/
function death()
{
global $closing,$numdie,$MAXDIE,$WATER,$OIL,$LAMP,$newloc,$oldloc,$loc;
global $dodeath,$answer81;

	$yea= $i= $j= $k=0;
        
	if (!$closing && $numdie < $MAXDIE) { // bug fix M.Cox; don't prompt to re-incarnate if run out of chances
          $dodeath=1; // cache that we need to get answer before we continue
   	  list($answer81,$yea) = yes((81+$numdie*2), (82+$numdie*2), 54);
          if ($answer81) {
                  $answer81=0;
                  $numdie+=1;
                  $dodeath=0;
		  if (!$yea)
			normend();
		  else {
		        $place[$WATER] = 0;
		        $place[$OIL] = 0;
		        if (toting($LAMP))
		        	$prop[$LAMP] = 0;
		        for ($j = 1; $j<101; ++$j) {
		        	$i = 101-$j;
		        	if (toting ($i))
		        		drop($i, $i == $LAMP ? 1:$oldloc2);
		  }
		  $newloc = 3;
		  $oldloc = $loc;
		  return;
		  }
		}
	}
	if ($numdie >= $MAXDIE)
	  normend();
	elseif ($closing) {
 	  /*
	   closing -- no resurrection...
	  */
	  rspeak(131);
	  ++$numdie;
	  normend();
	}
}

/*
	Routine to process an object.
*/
function doobj()
{
global $fixed,$object,$loc,$GRATE,$motion,$DEPRESSION,$ENTRANCE,$dflag,$DWARF,$BOTTLE,$PLANT,$PLANT2,$prop;
global $KNIFE,$knfloc,$ROD,$ROD2;
global $dbugflg;
	$i=0;
	/*
	   is object here?  if so, transitive
	*/
	
	if ($fixed[$object] == $loc || here($object))
		trobj();
	/*
		did he give grate as destination?
	*/
	else if ($object == $GRATE) {
		if ($loc == 1 || $loc == 4 || $loc == 7) {
			$motion = $DEPRESSION;
			domove();
		}
		else if ($loc>9 && $loc<15) {
			$motion = $ENTRANCE;
			domove();
		}
	}
	/*
		is it a dwarf he is after?
	*/
	else if (dcheck() && $dflag >= 2) {
		$object = $DWARF;
		trobj();
	}
	/*
	   is he trying to get/use a liquid?
	*/
	else if ((liq() == $object && here($BOTTLE)) ||
		 liqloc($loc) == $object)
		trobj();
	else if ($object == $PLANT && at($PLANT2) &&
		$prop[$PLANT2] == 0) {
		$object = $PLANT2;
		trobj();
	}
	/*
	   is he trying to grab a knife?
	*/
	else if ($object == $KNIFE && $knfloc == $loc) {
		rspeak(116);
		$knfloc = -1;
	}
	/*
	   is he trying to get at dynamite?
	*/
	else if ($object == $ROD && here($ROD2)) {
		$object = $ROD2;
		trobj();
	}
	else {
		buffprint(sprintf("I see no %s here.", probj($object)));
		buffprint("\n");
	}
}

/*
	Routine to process an object being
	referred to.
*/
function trobj()
{
global $verb,$object;
	if ($verb)
		trverb();
	else {
		buffprint(sprintf("What do you want to do with the ".probj($object)."?"));
		buffprint("\n");
	}
}

/*
	Routine to print word corresponding to object
*/
function probj($object)
{
global $word1,$word2;
	$wtype= $wval=0;
	analyze($word1, $wtype, $wval);
	return ($wtype == 1 ? $word1 : $word2);
}
/*
	dwarf stuff.
*/
function dwarves()
{
global $newloc,$cond,$NOPIRAT,$dflag,$dloc,$DWARFMAX,$daltloc,$odloc,$AXE;
global $dseen,$knfloc,$oldloc2;
global $dbugflg;
	$i= $j= $k= $try= $attack= $stick= $dtotal=0;
	/*
		see if dwarves allowed here
	*/
	if ($newloc == 0 || forced($newloc) || $cond[$newloc]&$NOPIRAT)
		return;
	/*
		see if dwarves are active.
	*/
	if (!$dflag) {
		if ($newloc > 15)
			++$dflag;
		return;
	}
	/*
		if first close encounter (of 3rd kind)
		kill 0, 1 or 2
	*/
	if ($dflag == 1) {
		if ($newloc < 15 || pct (95))
			return;
		++$dflag;
		for ($i = 1; $i<3; ++$i)
			if (pct (50))
				$dloc[rand(1,6)] = 0;
		for ($i = 1; $i< ($DWARFMAX-1); ++$i) {
			if ($dloc[$i] == $newloc)
				$dloc[$i] = $daltloc;
			$odloc[$i] = $dloc[$i];
		}
		rspeak(3);
		drop($AXE, $newloc);
		return;
	}
	$dtotal = $attack = $stick = 0;
	for ($i = 1; $i<$DWARFMAX; ++$i) {
		if ($dloc[$i] == 0)
			continue;
		/*
			move a dwarf at random.  we don't
			have a matrix around to do it
			as in the original version...
		*/
		for ($try = 1; $try<20; ++$try) {
			$j = rand(0,106)+15; /* allowed area */
			if ($j != $odloc[$i] && $j != $dloc[$i] &&
			    !($i == ($DWARFMAX-1) && $cond[$j]&$NOPIRAT == 1))
				break;
		}
		if ($j == 0)
			$j = $odloc[$i];
		$odloc[$i] = $dloc[$i];
		$dloc[$i] = $j;
		if (($dseen[$i] && $newloc >= 15) ||
		    $dloc[$i] == $newloc || $odloc[$i] == $newloc)
			$dseen[$i] = 1;
		else
			$dseen[$i] = 0;
		if (!$dseen[$i])
			continue;
		$dloc[$i] = $newloc;
		if ($i == 6)
			dopirate();
		else {
			++$dtotal;
			if ($odloc[$i] == $dloc[$i]) {
				++$attack;
				if ($knfloc >= 0)
					$knfloc = $newloc;
				if (rand(0,1000) < 95*($dflag-2))
					++$stick;
			}
		}
	}
	if ($dtotal == 0)
		return;
	if ($dtotal > 1) {
		buffprint(sprintf("There are %d threatening little dwarves in the room with you!", $dtotal));
		buffprint("\n");
	}
	else
		rspeak(4);
	if ($attack == 0)
		return;
	if ($dflag == 2)
		++$dflag;
	if ($attack > 1) {
		buffprint(sprintf("%d of them throw knives at you!!", $attack));
		buffprint("\n");
		$k = 6;
	}
	else {
		rspeak(5);
		$k = 52;
	}
	if ($stick <= 1) {
		rspeak($stick+$k);
		if ($stick == 0)
			return;
	}
	else {
		buffprint(sprintf("%d of them get you !!!", $stick));
		buffprint("\n");
	}
	$oldloc2 = $newloc;
	death();
}
/*
	pirate stuff
*/
function dopirate()
{
global $newloc,$chloc,$prop,$CHEST,$MAXTRS,$PYRAMID,$place,$EMERALD,$tally,$tally2,$LAMP,$MESSAGE,$chloc2;
	$j= $k=0;
	$stealit=0;
	if ($newloc == $chloc || $prop[$CHEST] >= 0)
		return;
	$k = 0;
	for ($j = 50; $j<=$MAXTRS; ++$j)
		if ($j != $PYRAMID ||
		    ($newloc != $place[$PYRAMID] &&
		     $newloc != $place[$EMERALD])) {
			if (toting($j)) {
				$stealit=1;
				break;
			}
			if (here($j))
				++$k;
		}
  switch($stealit){
  case 0:
	if ($tally == $tally2+1 && $k == 0 && $place[$CHEST] == 0 &&
	    here($LAMP) && $prop[$LAMP] == 1) {
		rspeak(186);
		move($CHEST, $chloc);
		move($MESSAGE, $chloc2);
		$dloc[6] = $chloc;
		$odloc[6] = $chloc;
		$dseen[6] = 0;
		return;
	}
	if ($odloc[6] != $dloc[6] && pct(20)) {
		rspeak(127);
		return;
	}
	break;
  case 1:

	rspeak(128);
	if ($place[$MESSAGE] == 0)
		move($CHEST, $chloc);
	move($MESSAGE, $chloc2);
	for ($j = 50; $j<=$MAXTRS; ++$j) {
		if ($j == $PYRAMID &&
		    ($newloc == $place[$PYRAMID] ||
		     $newloc == $place[$EMERALD]))
			continue;
		if (at($j) && $fixed[$j] == 0)
			carry($j, $newloc);
		if (toting($j))
			drop($j, $chloc);
	}
	$dloc[6] = $chloc;
	$odloc[6] = $chloc;
	$dseen[6] = 0;
  }
}
/*
	special time limit stuff...
*/
function stimer()
{
global $foobar,$tally,$loc,$clock,$prop,$GRATE,$FISSURE,$TROLL,$MAXOBJ,$TROLL2,$CHASM,$BEAR,$CHAIN,$AXE,$closing,$clock2;
global $BOTTLE,$PLANT,$OYSTER,$LAMP,$ROD,$DWARF,$oldloc,$newloc,$SNAKE,$BIRD,$CAGE,$ROD2,$PILLOW,$MIRROR,$fixed,$limit;
global $BATTERIES,$lmwarn,$panic,$closed;
	$i=0;
	$foobar = $foobar > 0 ?  -$foobar : 0; 

	if ($tally == 0 && $loc >= 15 && $loc != 33)
		--$clock;
	if ($clock == 0) {
		/*
			start closing the cave
		*/
		$prop[$GRATE] = 0;
		$prop[$FISSURE] = 0;
		for ($i = 1; $i<$DWARFMAX; ++$i)
			$dseen[$i] = 0;
		move($TROLL, 0);
		move(($TROLL+$MAXOBJ), 0);
		move($TROLL2, 117);
		move(($TROLL2+$MAXOBJ), 122);
		juggle($CHASM);
		if ($prop[$BEAR] != 3)
			dstroy($BEAR);
		$prop[$CHAIN] = 0;
		$fixed[$CHAIN] = 0;
		$prop[$AXE] = 0;
		$fixed[$AXE] = 0;
		rspeak(129);
		$clock = -1;
		$closing = 1;
		return(0);
	}
	if ($clock < 0)
		--$clock2;
	if ($clock2 == 0) {
		/*
			set up storage room...
			and close the cave...
		*/
		$prop[$BOTTLE] = put($BOTTLE, 115, 1);
		$prop[$PLANT] = put($PLANT, 115, 0);
		$prop[$OYSTER] = put($OYSTER, 115, 0);
		$prop[$LAMP] = put($LAMP, 115, 0);
		$prop[$ROD] = put($ROD, 115, 0);
		$prop[$DWARF] = put($DWARF, 115, 0);
		$loc = 115;
		$oldloc = 115;
		$newloc = 115;
		put($GRATE, 116, 0);
		$prop[$SNAKE] = put($SNAKE, 116, 1);
		$prop[$BIRD] = put($BIRD, 116, 1);
		$prop[$CAGE] = put($CAGE, 116, 0);
		$prop[$ROD2] = put($ROD2, 116, 0);
		$prop[$PILLOW] = put($PILLOW, 116, 0);
		$prop[$MIRROR] = put($MIRROR, 115, 0);
		$fixed[$MIRROR] = 116;
		for ($i = 1; $i<= $MAXOBJ; ++$i)
			if (toting($i))
				dstroy($i);
		rspeak(132);
		$closed = 1;
		return(1);
	}
	if ($prop[$LAMP] == 1)
		--$limit;
	if ($limit <= 30 &&
	    here($BATTERIES) && $prop[$BATTERIES] == 0 &&
	    here($LAMP)) {
		rspeak(188);
		$prop[$BATTERIES] = 1;
		if (toting($BATTERIES))
			drop($BATTERIES, $loc);
		$limit += 2500;
		$lmwarn = 0;
		return(0);
	}
	if ($limit == 0) {
		--$limit;
		$prop[$LAMP] = 0;
		if (here($LAMP))
			rspeak(184);
		return(0);
	}
	if ($limit < 0 && $loc <= 8) {
		rspeak(185);
		$gaveup = 1;
		normend();
	}
	if ($limit <= 30) {
		if ($lmwarn || !here($LAMP))
			return(0);
		$lmwarn = 1;
		$i = 187;
		if ($place[$BATTERIES] == 0)
			$i = 183;
		if ($prop[$BATTERIES] == 1)
			$i = 189;
		rspeak($i);
		return(0);
	}
	return(0);
}

/*
	random number seed
*/
$rnum = 0;

function my_srand($n)
{
// not used. PHP built-in rand function seeded from clock each time

	$rnum = $n;
}

/*
	random number
*/
function my_rand()
{
// rand - not used: PHP built-in rand function used instead

	$rnum = $rnum * 0x41C64E6D + 0x3039;
	return(($rnum >> 16) & 0x7FFF);
}


?>
