<?PHP

function printChildTriggers($fatherName, $area, $priorResponse, $trigcount, $attcount, $type){ 

  // escape fields
  $escaped_area=mysql_escape_string($area);

	// FOR NOW WE ASSUME THAT DIALOGUES ARE TREES, GRAPHS ARE NOT YET SUPPORTED
	$prior = $priorResponse; 
	// given the above assumption, plus the assumption that all triggers have the same attitude range when they share the response
	// we can use this query
	if ($type == 'queststep'){
		$query = "select trigger, max(response_id) from npc_triggers where prior_response_required=$prior and quest_id='" . $escaped_area . "' group by trigger order by response_id";
	}else{
		$query = "select trigger, max(response_id) from npc_triggers where prior_response_required=$prior and area='" . $escaped_area . "' and (quest_id=0 or quest_id is null) group by trigger order by response_id";
	} 
	// echo "query: $query<br>";
	$result = mysql_query2($query); 
	// every time the response changes we have a new trigger folder
	$prevResponse = -1;
	$prevTrigger = '';

	$atleastone = 0; 
	//echo "exploring prior: $prior\n<br>";
	// for every row result of the query
	while ($line = mysql_fetch_array($result, MYSQL_NUM)){ 
	  $numrows= mysql_num_rows($result);
	  //echo "numrows: $numrows\n<br>";
		// substitute any " with \" . Those are present in scripts
		$trigger_clean = str_replace("\"", "\\\"", $line[0]); 
		// detects if the trigger is a script
		$is_script = parseTriggerScript($line[0]);
		if ($is_script[0] == '1'){
      $trigger_clean = $is_script[1];
		}

    // escape fields
    $escaped_trigger=mysql_escape_string($line[0]);

		// get the id of the trigger to be used in urls instead of the trigger itself
		if ($type == 'queststep'){
			$query = "select id from npc_triggers where trigger='$escaped_trigger' and prior_response_required=$prior and quest_id='" . $escaped_area . "'";
		}else{
			$query = "select id from npc_triggers where trigger='$escaped_trigger' and prior_response_required=$prior and area='" . $escaped_area . "' and (quest_id=0 or quest_id is null)";
		}
		$result3 = mysql_query2($query);
		$line3 = mysql_fetch_array($result3, MYSQL_NUM);
		$triggerid = $line3[0];

		$atleastone = 1;
		$currResponse = $line[1];
		if ($prevResponse != $currResponse){ 
			// if it's not the first pass output attitudes
			if ($prevResponse != -1){
				$query = "select min_attitude_required, max_attitude_required, r.response1, response_id, t.id, t.prior_response_required from npc_responses r, npc_triggers t where r.id=t.response_id ";

				if ($type == 'queststep'){
					$query = $query . " and prior_response_required=$prior and t.quest_id=$escaped_area and t.trigger='$prevTrigger'";
				}else{
					$query = $query . " and prior_response_required=$prior and area='$escaped_area' and t.trigger='$prevTrigger' and (t.quest_id=0 or t.quest_id is null)";
				}
				//echo "$query<br>";
				$result2 = mysql_query2($query);
				while ($line2 = mysql_fetch_array($result2, MYSQL_NUM)){
					echo "att$attcount = insFld($currentTriggerName, gFld(\"attitude $line2[0] $line2[1]\", \"index.php?page=npc_actions&operation=viewattitude&triggerid=$line2[4]&area=$area&prior=$line2[5]&type=$type\"));\n";
					echo "att$attcount.iconSrc = ICONPATH + \"attitude.gif\"\n";
					echo "att$attcount.iconSrcClosed = ICONPATH + \"attitude.gif\"\n";
					$smaller = substr($line2[2],0,20);
					echo "nochilds = insDoc(att$attcount, gLnk(\"R\", \"responses $smaller\", \"index.php?page=npc_actions&operation=viewresponse&prior=$prior&responseid=$line2[3]&type=$type&area=$area\"));\n";
					// recursively go down in the tree with prior as current response
					//echo "attcount before: $attcount \n";
					$attfatherName = "att$attcount";
					$attcount++;

					$counts = printChildTriggers($attfatherName, $area, $line2[3], $trigcount, $attcount, $type); 

					// set values based on the recursive func result
          $trigcount = $counts[0];
          $attcount = $counts[1];

					//echo "attcount after: $attcount \n";


					// echo "attitude: $prevTrigger prior: $line2[3]\n";
				}
			}
			// echo "output trigger with prior $prior\n";

			// output trigger and phrase nodes
			$trigcount++;
			$currentTriggerName = "trig$trigcount";
			echo "$currentTriggerName = insFld($fatherName, gFld(\"trigger $trigger_clean\", \"index.php?page=npc_actions&operation=viewtrigger&triggerid=$triggerid&prior=$prior&type=$type&area=$area\"));\n";
			echo "temp = insDoc($currentTriggerName, gLnk(\"R\", \"phrase $trigger_clean\", \"index.php?page=npc_actions&operation=viewphrase&triggerid=$triggerid&prior=$prior&type=$type&area=$area\"));\n";
		}else{
			echo "temp = insDoc($currentTriggerName, gLnk(\"R\", \"phrase $trigger_clean\", \"index.php?page=npc_actions&operation=viewphrase&triggerid=$triggerid&prior=$prior&type=$type&area=$area\"));\n";
		}
		$prevResponse = $currResponse;
		$prevTrigger = mysql_escape_string($line[0]);
	}
	// adds last attitude
	if ($atleastone == 1){
		$query = "select min_attitude_required, max_attitude_required, r.response1, response_id, t.id, t.prior_response_required from npc_responses r, npc_triggers t where r.id=t.response_id ";
		if ($type == 'queststep'){
			$query = $query . " and prior_response_required=$prior and t.quest_id=$escaped_area and t.trigger='$prevTrigger'";
		}else{
			$query = $query . " and prior_response_required=$prior and area='$escaped_area' and t.trigger='$prevTrigger' and (t.quest_id=0 or t.quest_id is null)";
		} 
		// echo "$query";
		$result2 = mysql_query2($query);
		while ($line2 = mysql_fetch_array($result2, MYSQL_NUM)){
			echo "att$attcount = insFld($currentTriggerName, gFld(\"attitude $line2[0] $line2[1]\", \"index.php?page=npc_actions&operation=viewattitude&triggerid=$line2[4]&area=$area&prior=$line2[5]&type=$type\"));\n";
			echo "att$attcount.iconSrc = ICONPATH + \"attitude.gif\"\n";
			echo "att$attcount.iconSrcClosed = ICONPATH + \"attitude.gif\"\n";
			$smaller = substr($line2[2],0,20);
			echo "nochilds = insDoc(att$attcount, gLnk(\"R\", \"responses $smaller\", \"index.php?page=npc_actions&operation=viewresponse&prior=$prior&responseid=$line2[3]&type=$type&area=$area\"));\n"; 
			// recursively go down in the tree with prior as current response
			//echo "attcount before: $attcount\n";
			$attfatherName = "att$attcount";
			$attcount++;
			$counts = printChildTriggers($attfatherName, $area, $line2[3], $trigcount, $attcount, $type);

			// set values based on the recursive func result
      $trigcount = $counts[0];
      $attcount = $counts[1];
			//echo "attcount after: $attcount\n";


		}
	}

  $counts[0] = $trigcount;
  $counts[1] = $attcount;
  return $counts;
}

function getKAs(){ 
	// search all KAs
	$query = "select distinct(area) from npc_triggers";
	$result = mysql_query2($query);
	$i = 0;
	while ($line = mysql_fetch_array($result, MYSQL_NUM)){
		$areas[$i] = strtolower($line[0]);
		$i++;
	} 
	// search names of npcs
	$query2 = "select distinct(name) from characters where npc_master_id!=0";
	$result2 = mysql_query2($query2);
	$i = 0;
	while ($line = mysql_fetch_array($result2, MYSQL_NUM)){
		$names[$i] = strtolower($line[0]);
		$i++;
	} 
	// search KAs excluding npc specific kas
	$result = array_values(array_diff ($areas, $names)); 
	// DEBUG
	if (0){
		for ($i = 0; $i < sizeof($areas); $i++){
			echo "$i - $areas[$i]<br>";
		}
		echo "------------------<br>";
		for ($i = 0; $i < sizeof($names); $i++){
			echo "$i - $names[$i]<br>";
		}
		echo "------------------<br>";
		for ($i = 0; $i < sizeof($result); $i++){
			echo "$i - $result[$i]<br>";
		}
	}

	return $result;
}

function redirectOnType($type, $area){
	if ($type == 'ka'){

		?><SCRIPT language="javascript">
            top.location = "index.php?page=viewka&area=<? echo $area;
		?>";
         </script>
      <?PHP
	}
	if ($type == 'npc'){
		$query_string = "select id from characters where name='$area'";
		echo "$area <br>";
		$result = mysql_query2($query_string);
		$line = mysql_fetch_array($result, MYSQL_NUM);
		$id = $line[0];

		?><SCRIPT language="javascript">
            top.location = "index.php?page=viewnpc&id=<? echo $id;
		?>";
         </script>
      <?PHP
	}else{

		?><SCRIPT language="javascript">
            top.location = "index.php?page=viewqueststep&id=<? echo $area;
		?>";
         </script>
      <?PHP
	}
}

function getIdForReload($type, $area){
	if ($type == 'queststep'){
		$id = $area;
	}else{
		$query = "select id from characters where name='$area'";
		$result = mysql_query2($query);
		$line = mysql_fetch_array($result, MYSQL_NUM);
		$id = $line[0];
	}
	return $id;
}

function triggerFromID($triggerid){
	$query = "select trigger from npc_triggers where id=$triggerid";
	$result = mysql_query2($query);
	$line = mysql_fetch_array($result, MYSQL_NUM);
	$trigger = $line[0];

	return $trigger;
}

function parseTriggerScript($trigger){
	$pos = stristr($trigger, "<l");
	$istrigger = 0;
	$result[0] = 0;
	if ($pos != false) {
		$istrigger = 1;
		$result[0] = 1;
	}

  // parse trigger
  if ($istrigger==1)
  {
    $pos = strpos($trigger, "money");
		if ( $pos != 0){
		  $pos = strpos($trigger, "\"");
		  $endmoney = substr($trigger,$pos+1);
		  $pos = strpos($endmoney, "\"");
		  $endmoney = substr($endmoney,0,$pos);
			$result[1] = "!EXCHANGE SCRIPT!";
			$result[2] = $endmoney;
			$pos = strpos($trigger, "n=\"");
			if ($pos) {
			  $item = substr($trigger,$pos+3);
			  //echo "TEST TEST $item TEST $pos<br>";
			  $pos = strpos($item, "\"");
			  $item = substr($item,0,$pos);
			  //echo "TEST TEST $item TEST $pos<br>";
  			$result[3] = $item;

  			$pos = strpos($trigger, "c=\"");
  			$itemcount = substr($trigger,$pos+3);
			  $pos = strpos($itemcount, "\"");
			  $itemcount = substr($itemcount,0,$pos);
  			$result[4] = $itemcount;
  		}

		}else{
			$result[1] = "!UNKNOWN SCRIPT!";
		}
  }
  
  return $result;
  
}

?>