<?php
if (MING != "merciless") require ('index.php');
else {
/*
TODO:

Test this function A LOT. It has virtually not be tested at all.


*/
$random_default_power = POWER_HTML;
$random_file_item_delimeter = '------';
$random_disallow_blank = TRUE;
$random_order_separator = ' ';



if(!function_exists('str_split')){ 
   function str_split($string,$split_length=1){ 
       $count = strlen($string);  
       if($split_length < 1){ 
           return false;  
       } elseif($split_length > $count){ 
           return array($string); 
       } else { 
           $num = (int)ceil($count/$split_length);  
           $ret = array();  
           for($i=0;$i<$num;$i++){  
               $ret[] = substr($string,$i*$split_length,$split_length);  
           }  
           return $ret; 
       }      
   }  
}




function random_order_doaction ($arg = FALSE)
{
  $choices = random_generate_choice_list($arg, TRUE);
  if ($choices === FALSE)
    return FALSE;
  else
  {
    global $random_power, $random_disallow_blank, $random_order_separator;
    shuffle($choices);
    foreach($choices as $choice)
      if ($random_disallow_blank === FALSE || strlen(trim($choice)) > 1) 
	   outblurt($choice . $random_order_separator, $random_power);
    return TRUE;
  }
}


function random_select_doaction ($arg = FALSE)
{
  return random_main_doaction($arg);
}

function random_main_doaction ($arg = FALSE)
{
  $choices = random_generate_choice_list($arg, FALSE);
  if ($choices === FALSE)
  {
    return FALSE;
  }
  else
  {
    global $random_choice, $random_disallow_blank, $random_power;
    if ($random_choice === FALSE)
    {
      $i = 0;
      while( (strlen(trim($random_choice = ($choices[array_rand($choices)]))) < 1) && ($random_disallow_blank === TRUE))
      {
        $i++;
	if($i > 40)
	  //Too many blank spaces!------
	  return FALSE;
      }
    }
    outblurt($random_choice, $random_power);
    return TRUE;
  }

}


function random_generate_choice_list ( $arg = FALSE, $list_required = FALSE )
{
  if ($arg === FALSE || sizeof($arg) < 1)
  {
    outblurt("Error random_1: arg required.", POWER_DEBUG1);
    return FALSE;
  }
  global $random_power, $random_choice;
  $choices = Array();
  $random_choice = FALSE;
  if(strpos($arg, '|') !== FALSE) // we have choices between the 
  {
    global $random_default_power;
    $exploded = explode('|', $arg);
    $random_power = $random_default_power;
    foreach($exploded as $c)
      $choices[] = trim($c);
  }
  else if ( strpos($arg, ' ') !== FALSE )
  {
    global $random_default_power;
    $exploded = explode(' ', $arg);
    $random_power = $random_default_power;
    foreach($exploded as $c)
      $choices[] = trim($c);
  }
  // else if ( strpos($arg, '-') !== FALSE && is_numeric( explode('-', $arg, 2)[0] ) && is_numeric( explode('-', $arg, 2)[1]) )
  else if ( ( ereg('^[0-9]*\-[0-9]*$', trim($arg))) !== FALSE )
  {
    $random_power = POWER_HTML;
    $exploded =  explode('-', $arg, 2 );
    $random_choice = rand($exploded[0], $exploded[1]);
    if($list_required === TRUE) $choices = range($exploded[0], $exploded[1]);
    else $choices = TRUE;
    
  }
  else
  {
	// Assume that it is a file path.  
	$file = main_get_file($arg);
	if($file === FALSE)
	{
		$choices = Array($arg);
		return $choices;
	}
	$contents =  $file->contents;
	$random_power = $file->file_type;
	
    if ( strpos($arg, '/') !== FALSE  )
    {
      $file_name = substr($arg, strrpos($arg,'/') +1);
      $dir = substr($arg, 0, strrpos($arg,'/') );
    }
    else
    {
      $file_name = $arg;
      $dir = '';
    }
	
    global $random_file_item_delimeter;
    if ( strpos($contents, $random_file_item_delimeter ) !== FALSE)
    {
      $choices = explode($random_file_item_delimeter, $contents);
    }
    else
    {
      $choices = explode("\n", $contents);
    }
  }
  return $choices;
   
}


}
?>
