<?php
/*	program ENGLISH.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	char	*fgets();
//extern	int	fputc();
//extern	int	printf();
//extern	int	sscanf();


/*
	Analyze a two word sentence
*/
function english()
{
global $word1,$SAY,$word2,$verb,$motion,$object,$words;

	$msg="";
	$type1= $type2= $val= $val2= $ok=0;
	$gotinput=0;

	$verb = $object = $motion = 0;
	$type2 = $val2 = -1;
	$type1 = $val1 = -1;
	$msg = "bad grammar...";

	$gotinput=getwords();
	
        if ($gotinput) {

        	if (!($word1))
        		return(1);		/* ignore whitespace	*/
	
        	list($ok,$type1,$val1)=analyze($word1, $type1, $val1);
        	if (!$ok)	                /* check word1	*/
        		return(1);		/* didn't know it	*/

        	if ($type1 == 2 && $val1 == $SAY) {
        		$verb = $SAY;	/* repeat word & act upon if..	*/
        		$object = 1;
        		return(1);
        	}

        	if ($word2) {
        		list($ok,$type2,$val2)=analyze($word2, $type2, $val2);
        		if (!$ok)
        			return(1);	/* didn't know it	*/
                }
        	/* check his grammar */
        	if (($type1 == 3) && ($type2 == 3) && 
        	    ($val1 == 51) && ($val2 == 51)) {
        		outwords();
        	}
        	elseif ($type1 == 3) {
        		rspeak($val1);
        	}
        	elseif ($type2 == 3) {
        		rspeak($val2);
        	}
        	elseif ($type1 == 0) {
        		if ($type2 == 0) {
        			buffprint(sprintf("%s", $msg)); buffprint("\n");
        			$words="";
        		}
        		else
        			$motion = $val1;
        	}
        	elseif ($type2 == 0)
        		$motion = $val2;
        	elseif ($type1 == 1) {
        		$object = $val1;
        		if ($type2 == 2)
        			$verb = $val2;
        		if ($type2 == 1) {
        			buffprint(sprintf("%s", $msg)); buffprint("\n");
        			$words="";
        		}
        	}
        	elseif ($type1 == 2) {
        		$verb = $val1;
        		if ($type2 == 1)
        			$object = $val2;
        		if ($type2 == 2) {
        			buffprint(sprintf("%s", $msg)); buffprint("\n");
        			$words="";
        		}
        	}
        	else
        		bug(36);
        	
        }

	return($gotinput);
}


/*
		Routine to analyze a word.
*/
function analyze($word, $type, $value)
{
global $dbugflg;

	$wordval= $msg=0;

	/* make sure I understand */
	$wordval = vocab($word,0);
	if ($wordval == -1) {
		switch(rand() % 3) {
		case 0:
			$msg = 60;
			break;
		case 1:
			$msg = 61;
			break;
		default:
			$msg = 13;
		}
		rspeak($msg);
		return array(1,0,0);
	}
	$type = (int)floor($wordval/1000);
	$value = $wordval%1000;
	if ($dbugflg) print "word = $word, wordval = $wordval, type=$type, value=$value<br>";
	return array(1,$type,$value);
}

/*
	retrieve input line (max 80 chars), convert to lower case
	 & rescan for first two words (max. WORDSIZE-1 chars).
*/
function getwords()
{
global $word1,$word2,$dbugflg,$words,$PHP_SELF,$newwords;
	$wptr=0;
	$word1 = $word2 = '';

//	fputc('>', stdout);
//	fgets($words, 80, $stdin);
if (!$newwords) {
  print "<body onload=\"document.gwform.newwords.focus()\"><form method=post action=$PHP_SELF name=gwform>".
        "&gt;<input name=newwords length=80 width=200>".
        "</form>";
  return 0;
}
  $words=$newwords;
  $newwords="";
  buffprint ("&gt;$words\n");
  
	$wptr = $words;
	$words= strtolower($wptr);
	sscanf($words, "%19s %19s", $word1, $word2);
	if ($dbugflg)
		print("\$WORD1 = '$word1', \$WORD2 = '$word2'<br>");
	return 1;
}

/*
	output adventure word list (motion/0xxx & verb/2xxx) only
	6 words/line pausing at 20th line until keyboard active
*/
function outwords()
{
global $wc;
print_r($wc); // dump array of recognised words to screen
}


?>
