<?php

include_once("internal/log.php");

include_once("internal/os_helpers.php");

// --------------------------------------------
function pick_random_map_index($map_count, &$avoid_these) {
	$use_these = array();
	_log("unusable maps: ");
	foreach ($avoid_these as $key => $value) {
		_log("  " . $key);
	}
	for ($i = 0; $i < $map_count; $i++) {
		if (!array_key_exists($i, $avoid_these)) {
			$use_these[] = $i;
		}
	}
	_log("usable maps: ");
	foreach ($use_these as $key => $value) {
		_log("  " . $value);
	}
	$index = rand(0,count($use_these) - 1);

	return $use_these[$index];
}

// --------------------------------------------
function starts_with($haystack, $needle) {
        return !strncmp($haystack, $needle, strlen($needle));
}

// --------------------------------------------
function check_command($message, $target) {
        return starts_with(strtolower($message), "/" . $target);
}

// --------------------------------------------
function parse_parameters($message, $command) {
	$a = explode(" ", substr(trim($message), strlen($command) + 2));
	return $a;
}

// --------------------------------------------
function parse_position($pos_str) {
	$pos_array = explode(" ", $pos_str);
	return $pos_array;
}

// --------------------------------------------
function position_array_to_string($pos_array) {
	$pos = implode(" ", $pos_array);
	return $pos;
}

// --------------------------------------------
function get_position_distance($pos1, $pos2) {
	//_log("get_position_distance, pos1=" . $pos1[0] . ", " . $pos1[1] . ", " . $pos1[2] . ", pos2=" . $pos2[0] . ", " . $pos2[1] . ", " . $pos2[2]);
	$d = array();

	$d[] = $pos1[0] - $pos2[0];
	$d[] = $pos1[1] - $pos2[1];
	$d[] = $pos1[2] - $pos2[2];

	$d[0] *= $d[0];
	$d[1] *= $d[1];
	$d[2] *= $d[2];

	$result = sqrt($d[0] + $d[1] + $d[2]);
	_log("  result=" . $result);

	return $result;
}

// --------------------------------------------
function check_range($pos1, $pos2, $range) {
	$length = get_position_distance($pos1, $pos2);
	return ($length <= $range); 
}

?>