示例#1
0
文件: npc.php 项目: smrealms/smrv2.0
function &findRoutes(&$player)
{
    debug('Finding Routes');
    $galaxies =& SmrGalaxy::getGameGalaxies($player->getGameID());
    $tradeGoods = array(GOOD_NOTHING => false);
    $goods =& Globals::getGoods();
    foreach ($goods as $goodID => &$good) {
        if ($player->meetsAlignmentRestriction($good['AlignRestriction'])) {
            $tradeGoods[$goodID] = true;
        } else {
            $tradeGoods[$goodID] = false;
        }
    }
    unset($good);
    $tradeRaces = array();
    $races =& Globals::getRaces();
    foreach ($races as $raceID => &$race) {
        $tradeRaces[$raceID] = false;
    }
    unset($race);
    $tradeRaces[$player->getRaceID()] = true;
    $galaxy =& $player->getSector()->getGalaxy();
    $maxNumberOfPorts = 2;
    $routesForPort = -1;
    $numberOfRoutes = 1000;
    $maxDistance = 15;
    $startSectorID = $galaxy->getStartSector();
    $endSectorID = $galaxy->getEndSector();
    $db = new SmrMySqlDatabase();
    $db->query('SELECT routes FROM route_cache WHERE game_id=' . $db->escapeNumber($player->getGameID()) . ' AND max_ports=' . $db->escapeNumber($maxNumberOfPorts) . ' AND goods_allowed=' . $db->escapeObject($tradeGoods) . ' AND races_allowed=' . $db->escapeObject($tradeRaces) . ' AND start_sector_id=' . $db->escapeNumber($startSectorID) . ' AND end_sector_id=' . $db->escapeNumber($endSectorID) . ' AND routes_for_port=' . $db->escapeNumber($routesForPort) . ' AND max_distance=' . $db->escapeNumber($maxDistance));
    if ($db->nextRecord()) {
        $routes = unserialize(gzuncompress($db->getField('routes')));
        debug('Using Cached Routes: #' . count($routes));
        return $routes;
    } else {
        debug('Generating Routes');
        $allSectors = array();
        foreach ($galaxies as &$galaxy) {
            $allSectors += $galaxy->getSectors();
            //Merge arrays
        }
        unset($galaxy);
        $distances =& Plotter::calculatePortToPortDistances($allSectors, $maxDistance, $startSectorID, $endSectorID);
        if ($maxNumberOfPorts == 1) {
            $allRoutes = RouteGenerator::generateOneWayRoutes($allSectors, $distances, $tradeGoods, $tradeRaces, $routesForPort);
        } else {
            $allRoutes = RouteGenerator::generateMultiPortRoutes($maxNumberOfPorts, $allSectors, $tradeGoods, $tradeRaces, $distances, $routesForPort, $numberOfRoutes);
        }
        unset($distances);
        $allRoutes =& $allRoutes[RouteGenerator::EXP_ROUTE];
        $routesMerged = array();
        foreach ($allRoutes as $multi => &$routesByMulti) {
            $routesMerged += $routesByMulti;
            //Merge arrays
        }
        unset($routesByMulti);
        unset($allSectors);
        SmrPort::clearCache();
        SmrSector::clearCache();
        $db->query('INSERT INTO route_cache ' . '(game_id, max_ports, goods_allowed, races_allowed, start_sector_id, end_sector_id, routes_for_port, max_distance, routes)' . ' VALUES (' . $db->escapeNumber($player->getGameID()) . ', ' . $db->escapeNumber($maxNumberOfPorts) . ', ' . $db->escapeObject($tradeGoods) . ', ' . $db->escapeObject($tradeRaces) . ', ' . $db->escapeNumber($startSectorID) . ', ' . $db->escapeNumber($endSectorID) . ', ' . $db->escapeNumber($routesForPort) . ', ' . $db->escapeNumber($maxDistance) . ', ' . $db->escapeObject($routesMerged, true) . ')');
        debug('Found Routes: #' . count($routesMerged));
        return $routesMerged;
    }
}
示例#2
0
require_once '../htdocs/config.inc';
require_once LIB . 'Default/Globals.class.inc';
require_once get_file_loc('RouteGenerator.class.inc');
require_once get_file_loc('SmrGalaxy.class.inc');
$gameID = 108;
$galaxies =& SmrGalaxy::getGameGalaxies($gameID);
$allSectors = array();
foreach ($galaxies as &$galaxy) {
    $allSectors = $allSectors + $galaxy->getSectors();
}
unset($galaxy);
$maxNumberOfPorts = 2;
$goods = array(true, true, true, true, false, false, false, false, false, false, false, false, false);
$races = array(true, true, true, true, false, false, false, false, false, false, false, false, false, false);
//$distances =& Plotter::calculatePortToPortDistances($allSectors,10,0,1440);
$distances =& Plotter::calculatePortToPortDistances($allSectors, 10, 0, 14);
//var_dump($distances);
$routesForPort = -1;
$numberOfRoutes = 5;
if ($maxNumberOfPorts == 1) {
    $allRoutes = RouteGenerator::generateOneWayRoutes($allSectors, $distances, $goods, $races, $routesForPort);
} else {
    $allRoutes = RouteGenerator::generateMultiPortRoutes($maxNumberOfPorts, $allSectors, $goods, $races, $distances, $routesForPort, $numberOfRoutes);
}
//var_dump($allRoutes);
foreach ($allRoutes as $routeType) {
    $c = 0;
    foreach ($routeType as $routeMulti) {
        $c += count($routeMulti);
        //		foreach($routeMulti as $multi => $route) {
        //			echo $multi . ":  ".$route->getRouteString();