diff --git a/lib/Default/RouteGenerator.class.php b/lib/Default/RouteGenerator.class.php deleted file mode 100644 index 2aeed94f6..000000000 --- a/lib/Default/RouteGenerator.class.php +++ /dev/null @@ -1,391 +0,0 @@ - &$value) { - self::startRoutesToContinue($maxNumPorts, $key, $value, $routeLists); - if ($totalTasks % 10 === 0 && $totalTasks > $numberOfRoutes) { - self::trimRoutes($numberOfRoutes); - } - $totalTasks++; - } unset($value); -// $tasksCompleted = 0; - self::trimRoutes($numberOfRoutes); - $allRoutes = array(); - $allRoutes[self::EXP_ROUTE] =& self::$expRoutes; - $allRoutes[self::MONEY_ROUTE] =& self::$moneyRoutes; - return $allRoutes; - } - - /** - * Works by pass by reference so will update higher levels, hacky but works. - * - * @param startSectorId - * @param forwardRoutes - * @param routeLists - * @param expRoutes - */ - static function startRoutesToContinue($maxNumPorts, $startSectorId, array &$forwardRoutes, array &$routeLists) { - foreach($forwardRoutes as &$currentStepRoute) { - $currentStepBuySector = $currentStepRoute->getBuySectorId(); - if ($currentStepBuySector > $startSectorId) { // Not already checked - self::getContinueRoutes($maxNumPorts - 1, $startSectorId, $currentStepRoute, $routeLists[$currentStepBuySector], $routeLists, $currentStepRoute->getGoodID() === GOOD_NOTHING); - } - } unset($currentStepRoute); - } - - /** - * Works by pass by reference so will update higher levels, hacky but works. - * - * @param startSectorId - * @param routeToContinue - * @param forwardRoutes - * @param routeLists - * @param allRoutes - */ - private static function getContinueRoutes($maxNumPorts, $startSectorId, Route &$routeToContinue, array &$forwardRoutes, array &$routeLists, $lastGoodIsNothing) { - foreach($forwardRoutes as &$currentStepRoute) { - $currentStepBuySector = $currentStepRoute->getBuySectorId(); - if ($lastGoodIsNothing & ($lastGoodIsNothing = GOOD_NOTHING === $currentStepRoute->getGoodID())) - continue; // Don't do two nothings in a row - if ($currentStepBuySector >= $startSectorId) { // Not already checked or back to start - if ($currentStepBuySector === $startSectorId) { // Route returns to start - $mpr = new MultiplePortRoute($routeToContinue, $currentStepRoute); - self::addExpRoute($mpr); - self::addMoneyRoute($mpr); - } - else if ($maxNumPorts > 1 && !$routeToContinue->containsPort($currentStepBuySector)) { - $mpr = new MultiplePortRoute($routeToContinue, $currentStepRoute); - self::getContinueRoutes($maxNumPorts - 1, $startSectorId, $mpr, $routeLists[$currentStepBuySector], $routeLists, $lastGoodIsNothing); - } - } - } unset($currentStepRoute); - } - - private static function &findOneWayRoutes(array &$sectors, array &$distances, $routesForPort, array &$goods, array &$races) { - $routes = array(); - foreach($distances as $currentSectorId => &$d) { - $raceID = $sectors[$currentSectorId]->getPort()->getRaceID(); - if (isset($races[$raceID])===false) { - echo 'Error with Race ID: '.$sectors[$currentSectorId]->getPort()->getRaceID(); - continue; - } - if($races[$raceID]===false) - continue; - $rl = array(); - foreach($d as $targetSectorId => &$distance) { - if (!$races[$sectors[$targetSectorId]->getPort()->getRaceID()]) - continue; - if($routesForPort!==-1 && $currentSectorId !== $routesForPort && $targetSectorId !== $routesForPort) - continue; - - if ($goods[GOOD_NOTHING]===true) - $rl[] = new OneWayRoute($currentSectorId, $targetSectorId, $races[$raceID], $sectors[$targetSectorId]->getPort()->getRaceID(), 0, 0, $distance, GOOD_NOTHING); - - foreach (Globals::getGoods() as $goodId => $value) { - if ($goods[$goodId]===true) { - if ($sectors[$currentSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_SELLS && - $sectors[$targetSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_BUYS) { - $rl[] = new OneWayRoute($currentSectorId, $targetSectorId, $races[$raceID], $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getGoodDistance($goodId), $sectors[$targetSectorId]->getPort()->getGoodDistance($goodId), $distance, $goodId); - } - } - } - } unset($distance); - $routes[$sectors[$currentSectorId]->getSectorID()] = $rl; - } unset($d); - return $routes; - } - - public static function &generateOneWayRoutes(array &$sectors, array &$distances, array &$goods, &$races, &$routesForPort) { - self::$expRoutes = array(); - self::$moneyRoutes = array(); - foreach($distances as $currentSectorId => &$d) { - if ($races[$sectors[$currentSectorId]->getPort()->getRaceID()]===false) - continue; - foreach($d as $targetSectorId => &$distance) { - if ($races[$sectors[$targetSectorId]->getPort()->getRaceID()]===false) - continue; - if($routesForPort!==-1 && $currentSectorId !== $routesForPort && $targetSectorId !== $routesForPort) - continue; - - foreach (Globals::getGoods() as $goodId => $value) { - if ($goods[$goodId]===true) { - if ($sectors[$currentSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_SELLS && - $sectors[$targetSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_BUYS) { - $owr = new OneWayRoute($currentSectorId, $targetSectorId, $sectors[$currentSectorId]->getPort()->getRaceID(), $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getGoodDistance($goodId), $sectors[$targetSectorId]->getPort()->getGoodDistance($goodId), $distance, $goodId); - $fakeReturn = new OneWayRoute($targetSectorId, $currentSectorId, $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getRaceID(), 0, 0, $distance, GOOD_NOTHING); - $mpr = new MultiplePortRoute($owr, $fakeReturn); - self::addExpRoute($mpr); - self::addMoneyRoute($mpr); - } - } - } - } unset($distance); - } unset($d); - $allRoutes = array(); - $allRoutes[self::EXP_ROUTE] = self::$expRoutes; - $allRoutes[self::MONEY_ROUTE] = self::$moneyRoutes; - return $allRoutes; - } - - private static function addExpRoute(Route &$r) { - $overallMultiplier = $r->getOverallExpMultiplier(); - if ($overallMultiplier > self::$dontAddWorseThan[self::EXP_ROUTE]) { - if (isset(self::$expRoutes[$overallMultiplier])) { - $rl =& self::$expRoutes[$overallMultiplier]; - } - else { - $rl = array(); - } - $rl[] =& $r; - self::$expRoutes[$overallMultiplier] =& $rl; - } - } - - private static function addMoneyRoute(Route &$r) { - $overallMultiplier = $r->getOverallMoneyMultiplier(); - if ($overallMultiplier > self::$dontAddWorseThan[self::MONEY_ROUTE]) { - if (isset(self::$moneyRoutes[$overallMultiplier])) { - $rl =& self::$moneyRoutes[$overallMultiplier]; - } - else { - $rl = array(); - } - $rl[] =& $r; - self::$moneyRoutes[$overallMultiplier] =& $rl; - } - } - - public static function trimRoutes($trimToBestXRoutes) { - $i = 0; - krsort(self::$expRoutes); - foreach(self::$expRoutes as $multi => &$routesByMulti) { - if(count($routesByMulti)+$i < $trimToBestXRoutes) { - $i += count($routesByMulti); - } - else if($i > $trimToBestXRoutes) { - unset(self::$expRoutes[$multi]); - } - else { - foreach($routesByMulti as $key => &$value) { - $i++; - if($i < $trimToBestXRoutes) - continue; - if($i === $trimToBestXRoutes) { - self::$dontAddWorseThan[self::EXP_ROUTE] = $multi; - continue; - } - unset(self::$expRoutes[$multi][$key]); - } unset($value); - } - } unset($routesByMulti); - - $i = 0; - krsort(self::$moneyRoutes); - foreach(self::$moneyRoutes as $multi => &$routesByMulti) { - if(count($routesByMulti)+$i < $trimToBestXRoutes) { - $i += count($routesByMulti); - } - else if($i > $trimToBestXRoutes) { - unset(self::$moneyRoutes[$multi]); - continue; - } - else { - foreach($routesByMulti as $key => &$value) { - $i++; - if($i < $trimToBestXRoutes) - continue; - if($i === $trimToBestXRoutes) { - self::$dontAddWorseThan[self::MONEY_ROUTE] = $multi; - continue; - } - unset(self::$moneyRoutes[$multi][$key]); - } unset($value); - } - } unset($routesByMulti); - } -} - - - -abstract class Route { - public function getOverallExpMultiplier() { - return $this->getExpMultiplierSum() / $this->getTurnsForRoute(); - } - - public function getOverallMoneyMultiplier() { - return $this->getMoneyMultiplierSum() / $this->getTurnsForRoute(); - } - - public function getTurnsForRoute() { - return $this->getForwardRoute()->getTurnsForRoute() + $this->getReturnRoute()->getTurnsForRoute(); - } - - public function getMoneyMultiplierSum() { - return $this->getForwardRoute()->getExpMultiplierSum() + $this->getReturnRoute()->getExpMultiplierSum(); - } - - public function getExpMultiplierSum() { - return $this->getForwardRoute()->getExpMultiplierSum() + $this->getReturnRoute()->getExpMultiplierSum(); - } - - public function containsPort($sectorID) { - $route = $this->getReturnRoute(); - return ($route != null && $route->containsPort($sectorID)) || (($route = $this->getForwardRoute()) != null && $route->containsPort($sectorID)); - } - - public abstract function &getForwardRoute(); - - public abstract function &getReturnRoute(); - - public function getRouteString() { - return $this->getForwardRoute()->getRouteString() . "\r\n" . $this->getReturnRoute()->getRouteString(); - } -} - -class OneWayRoute extends Route { - private $sellSectorId; - private $buySectorId; - private $sellDi; - private $buyDi; - private $distance; - private $goodId; - private $sellPortRace; - private $buyPortRace; - - public function __construct($_sellSectorId, $_buySectorId, $_sellPortRace, $_buyPortRace, $_sellDi, $_buyDi, Distance &$_distance, $_goodId) { - $this->sellSectorId = $_sellSectorId; - $this->buySectorId = $_buySectorId; - $this->sellDi = $_sellDi; - $this->buyDi = $_buyDi; - $this->distance =& $_distance; - $this->goodId = $_goodId; - $this->sellPortRace = $_sellPortRace; - $this->buyPortRace = $_buyPortRace; - } - - public function getSellSectorId() { - return $this->sellSectorId; - } - - public function getBuySectorId() { - return $this->buySectorId; - } - - public function getSellPortRace() { - return $this->sellPortRace; - } - - public function getBuyPortRace() { - return $this->buyPortRace; - } - - public function getSellDi() { - return $this->sellDi; - } - - public function getBuyDi() { - return $this->buyDi; - } - - public function &getDistance() { - return $this->distance; - } - - public function getGoodID() { - return $this->goodId; - } - - public function getOverallExpMultiplier() { - return ($this->buyDi + $this->sellDi) / getTurnsForRoute(); - } - - public function getMoneyMultiplierSum() {// TODO sellDi stuff and check accuracy of formula - $buyRelFactor = 1; - $sellRelFactor = 1; - if (ROUTE_GEN_USE_RELATIONS_FACTOR===true) { - //TODO: This needs to be converted for PHP/SMR. -// $relations = max(PlayerPreferences.getRelationsForRace($this->buyPortRace), Settings.MAX_MONEY_RELATIONS); -// $buyRelFactor = ($relations + 350) / 8415.0; -// -// $sellRelFactor = 2 - (PlayerPreferences.getRelationsForRace($this->sellPortRace) + 50) / 850.0 * ((relations + 350)/1500); - } - $goodInfo = Globals::getGood($this->goodId); - $buyPrice = round(0.08 * $goodInfo['BasePrice'] * pow($this->buyDi, 1.3) * $buyRelFactor); - $sellPrice = round(0.03 * $goodInfo['BasePrice'] * pow($this->sellDi, 1.3) * $sellRelFactor); - return $buyPrice - $sellPrice; - } - - public function getExpMultiplierSum() { - return $this->buyDi + $this->sellDi; - } - - public function getTurnsForRoute() { - if ($this->goodId === GOOD_NOTHING) - $tradeTurns = 0; - else - $tradeTurns = 2 * TURNS_PER_TRADE; - return $this->distance->getTurns() + $tradeTurns; - } - - public function compareTo(OneWayRoute &$compare) { - if ($this->equals($compare)===true) - return 0; - if ($this->getOverallExpMultiplier() > $compare->getOverallExpMultiplier()) - return 1; - return -1; - } - - public function containsPort($sectorID) { - return $this->sellSectorId == $sectorID || $this->buySectorId == $sectorID; - } - - public function &getForwardRoute() { - return null; - } - - public function &getReturnRoute() { - return null; - } - - public function getRouteString() { - return $this->sellSectorId . ' (' . Globals::getRaceName($this->sellPortRace) . ') buy ' . Globals::getGoodName($this->goodId) . ' at ' . $this->sellDi . 'x to sell at (Distance: ' . $this->distance->getDistance() . ($this->distance->getNumWarps() > 0 ? ' + ' . $this->distance->getNumWarps() . ' warps) ' : ') ') . $this->buySectorId . ' (' . Globals::getRaceName($this->buyPortRace) . ') at ' . $this->buyDi . 'x'; - } -} - -class MultiplePortRoute extends Route { - private $forwardRoute; - private $returnRoute; - - public function __construct(OneWayRoute &$_forwardRoute, OneWayRoute &$_returnRoute) { - $this->forwardRoute =& $_forwardRoute; - $this->returnRoute =& $_returnRoute; - } - - public function &getForwardRoute() { - return $this->forwardRoute; - } - - public function &getReturnRoute() { - return $this->returnRoute; - } -} diff --git a/lib/Default/Routes/MultiplePortRoute.class.php b/lib/Default/Routes/MultiplePortRoute.class.php new file mode 100644 index 000000000..79917266d --- /dev/null +++ b/lib/Default/Routes/MultiplePortRoute.class.php @@ -0,0 +1,21 @@ +forwardRoute = $_forwardRoute; + $this->returnRoute = $_returnRoute; + } + + public function getForwardRoute() : ?OneWayRoute { + return $this->forwardRoute; + } + + public function getReturnRoute() : ?OneWayRoute { + return $this->returnRoute; + } +} diff --git a/lib/Default/Routes/OneWayRoute.class.php b/lib/Default/Routes/OneWayRoute.class.php new file mode 100644 index 000000000..7496fd7c0 --- /dev/null +++ b/lib/Default/Routes/OneWayRoute.class.php @@ -0,0 +1,113 @@ +sellSectorId = $_sellSectorId; + $this->buySectorId = $_buySectorId; + $this->sellDi = $_sellDi; + $this->buyDi = $_buyDi; + $this->distance = $_distance; + $this->goodId = $_goodId; + $this->sellPortRace = $_sellPortRace; + $this->buyPortRace = $_buyPortRace; + } + + public function getSellSectorId() : int { + return $this->sellSectorId; + } + + public function getBuySectorId() : int { + return $this->buySectorId; + } + + public function getSellPortRace() : int { + return $this->sellPortRace; + } + + public function getBuyPortRace() : int { + return $this->buyPortRace; + } + + public function getSellDi() : int { + return $this->sellDi; + } + + public function getBuyDi() : int { + return $this->buyDi; + } + + public function getDistance() : \Distance { + return $this->distance; + } + + public function getGoodID() : int { + return $this->goodId; + } + + public function getOverallExpMultiplier() : float { + return ($this->buyDi + $this->sellDi) / getTurnsForRoute(); + } + + public function getMoneyMultiplierSum() {// TODO sellDi stuff and check accuracy of formula + $buyRelFactor = 1; + $sellRelFactor = 1; + if (ROUTE_GEN_USE_RELATIONS_FACTOR===true) { + //TODO: This needs to be converted for PHP/SMR. +// $relations = max(PlayerPreferences.getRelationsForRace($this->buyPortRace), Settings.MAX_MONEY_RELATIONS); +// $buyRelFactor = ($relations + 350) / 8415.0; +// +// $sellRelFactor = 2 - (PlayerPreferences.getRelationsForRace($this->sellPortRace) + 50) / 850.0 * ((relations + 350)/1500); + } + $goodInfo = Globals::getGood($this->goodId); + $buyPrice = round(0.08 * $goodInfo['BasePrice'] * pow($this->buyDi, 1.3) * $buyRelFactor); + $sellPrice = round(0.03 * $goodInfo['BasePrice'] * pow($this->sellDi, 1.3) * $sellRelFactor); + return $buyPrice - $sellPrice; + } + + public function getExpMultiplierSum() : int { + return $this->buyDi + $this->sellDi; + } + + public function getTurnsForRoute() : int { + if ($this->goodId === GOOD_NOTHING) + $tradeTurns = 0; + else + $tradeTurns = 2 * TURNS_PER_TRADE; + return $this->distance->getTurns() + $tradeTurns; + } + + public function compareTo(OneWayRoute $compare) : int { + if ($this->equals($compare)===true) + return 0; + if ($this->getOverallExpMultiplier() > $compare->getOverallExpMultiplier()) + return 1; + return -1; + } + + public function containsPort(int $sectorID) : bool { + return $this->sellSectorId == $sectorID || $this->buySectorId == $sectorID; + } + + public function getForwardRoute() : ?OneWayRoute { + return null; + } + + public function getReturnRoute() : ?OneWayRoute { + return null; + } + + public function getRouteString() : string { + return $this->sellSectorId . ' (' . Globals::getRaceName($this->sellPortRace) . ') buy ' . Globals::getGoodName($this->goodId) . ' at ' . $this->sellDi . 'x to sell at (Distance: ' . $this->distance->getDistance() . ($this->distance->getNumWarps() > 0 ? ' + ' . $this->distance->getNumWarps() . ' warps) ' : ') ') . $this->buySectorId . ' (' . Globals::getRaceName($this->buyPortRace) . ') at ' . $this->buyDi . 'x'; + } +} diff --git a/lib/Default/Routes/Route.class.php b/lib/Default/Routes/Route.class.php new file mode 100644 index 000000000..9c1d90f8c --- /dev/null +++ b/lib/Default/Routes/Route.class.php @@ -0,0 +1,38 @@ +getExpMultiplierSum() / $this->getTurnsForRoute(); + } + + public function getOverallMoneyMultiplier() : float { + return $this->getMoneyMultiplierSum() / $this->getTurnsForRoute(); + } + + public function getTurnsForRoute() : int { + return $this->getForwardRoute()->getTurnsForRoute() + $this->getReturnRoute()->getTurnsForRoute(); + } + + public function getMoneyMultiplierSum() : int { + return $this->getForwardRoute()->getExpMultiplierSum() + $this->getReturnRoute()->getExpMultiplierSum(); + } + + public function getExpMultiplierSum() : int { + return $this->getForwardRoute()->getExpMultiplierSum() + $this->getReturnRoute()->getExpMultiplierSum(); + } + + public function containsPort(int $sectorID) : bool { + $route = $this->getReturnRoute(); + return ($route != null && $route->containsPort($sectorID)) || (($route = $this->getForwardRoute()) != null && $route->containsPort($sectorID)); + } + + public abstract function getForwardRoute() : ?OneWayRoute; + + public abstract function getReturnRoute() : ?OneWayRoute; + + public function getRouteString() : string { + return $this->getForwardRoute()->getRouteString() . "\r\n" . $this->getReturnRoute()->getRouteString(); + } +} diff --git a/lib/Default/Routes/RouteGenerator.class.php b/lib/Default/Routes/RouteGenerator.class.php new file mode 100644 index 000000000..a33ea1444 --- /dev/null +++ b/lib/Default/Routes/RouteGenerator.class.php @@ -0,0 +1,202 @@ + $value) { + self::startRoutesToContinue($maxNumPorts, $key, $value, $routeLists); + if ($totalTasks % 10 === 0 && $totalTasks > $numberOfRoutes) { + self::trimRoutes($numberOfRoutes); + } + $totalTasks++; + } + self::trimRoutes($numberOfRoutes); + $allRoutes = [ + self::EXP_ROUTE => self::$expRoutes, + self::MONEY_ROUTE => self::$moneyRoutes, + ]; + return $allRoutes; + } + + private static function startRoutesToContinue(int $maxNumPorts, int $startSectorId, array $forwardRoutes, array $routeLists) : void { + foreach ($forwardRoutes as $currentStepRoute) { + $currentStepBuySector = $currentStepRoute->getBuySectorId(); + if ($currentStepBuySector > $startSectorId) { // Not already checked + self::getContinueRoutes($maxNumPorts - 1, $startSectorId, $currentStepRoute, $routeLists[$currentStepBuySector], $routeLists, $currentStepRoute->getGoodID() === GOOD_NOTHING); + } + } + } + + private static function getContinueRoutes(int $maxNumPorts, int $startSectorId, Route $routeToContinue, array $forwardRoutes, array $routeLists, bool $lastGoodIsNothing) : void { + foreach ($forwardRoutes as $currentStepRoute) { + $currentStepBuySector = $currentStepRoute->getBuySectorId(); + if ($lastGoodIsNothing && ($lastGoodIsNothing = GOOD_NOTHING === $currentStepRoute->getGoodID())) + continue; // Don't do two nothings in a row + if ($currentStepBuySector >= $startSectorId) { // Not already checked or back to start + if ($currentStepBuySector === $startSectorId) { // Route returns to start + $mpr = new MultiplePortRoute($routeToContinue, $currentStepRoute); + self::addExpRoute($mpr); + self::addMoneyRoute($mpr); + } + else if ($maxNumPorts > 1 && !$routeToContinue->containsPort($currentStepBuySector)) { + $mpr = new MultiplePortRoute($routeToContinue, $currentStepRoute); + self::getContinueRoutes($maxNumPorts - 1, $startSectorId, $mpr, $routeLists[$currentStepBuySector], $routeLists, $lastGoodIsNothing); + } + } + } + } + + private static function findOneWayRoutes(array $sectors, array $distances, int $routesForPort, array $goods, array $races) : array { + $routes = array(); + foreach ($distances as $currentSectorId => $d) { + $raceID = $sectors[$currentSectorId]->getPort()->getRaceID(); + if (isset($races[$raceID])===false) { + echo 'Error with Race ID: '.$sectors[$currentSectorId]->getPort()->getRaceID(); + continue; + } + if($races[$raceID]===false) + continue; + $rl = array(); + foreach ($d as $targetSectorId => $distance) { + if (!$races[$sectors[$targetSectorId]->getPort()->getRaceID()]) + continue; + if($routesForPort!==-1 && $currentSectorId !== $routesForPort && $targetSectorId !== $routesForPort) + continue; + + if ($goods[GOOD_NOTHING]===true) + $rl[] = new OneWayRoute($currentSectorId, $targetSectorId, $races[$raceID], $sectors[$targetSectorId]->getPort()->getRaceID(), 0, 0, $distance, GOOD_NOTHING); + + foreach (Globals::getGoods() as $goodId => $value) { + if ($goods[$goodId]===true) { + if ($sectors[$currentSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_SELLS && + $sectors[$targetSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_BUYS) { + $rl[] = new OneWayRoute($currentSectorId, $targetSectorId, $races[$raceID], $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getGoodDistance($goodId), $sectors[$targetSectorId]->getPort()->getGoodDistance($goodId), $distance, $goodId); + } + } + } + } + $routes[$sectors[$currentSectorId]->getSectorID()] = $rl; + } + return $routes; + } + + public static function generateOneWayRoutes(array $sectors, array $distances, array $goods, array $races, int $routesForPort) : array { + self::$expRoutes = array(); + self::$moneyRoutes = array(); + foreach ($distances as $currentSectorId => $d) { + if ($races[$sectors[$currentSectorId]->getPort()->getRaceID()]===false) + continue; + foreach ($d as $targetSectorId => $distance) { + if ($races[$sectors[$targetSectorId]->getPort()->getRaceID()]===false) + continue; + if($routesForPort!==-1 && $currentSectorId !== $routesForPort && $targetSectorId !== $routesForPort) + continue; + + foreach (Globals::getGoods() as $goodId => $value) { + if ($goods[$goodId]===true) { + if ($sectors[$currentSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_SELLS && + $sectors[$targetSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_BUYS) { + $owr = new OneWayRoute($currentSectorId, $targetSectorId, $sectors[$currentSectorId]->getPort()->getRaceID(), $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getGoodDistance($goodId), $sectors[$targetSectorId]->getPort()->getGoodDistance($goodId), $distance, $goodId); + $fakeReturn = new OneWayRoute($targetSectorId, $currentSectorId, $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getRaceID(), 0, 0, $distance, GOOD_NOTHING); + $mpr = new MultiplePortRoute($owr, $fakeReturn); + self::addExpRoute($mpr); + self::addMoneyRoute($mpr); + } + } + } + } + } + $allRoutes = [ + self::EXP_ROUTE => self::$expRoutes, + self::MONEY_ROUTE => self::$moneyRoutes, + ]; + return $allRoutes; + } + + private static function addExpRoute(Route $r) : void { + $overallMultiplier = $r->getOverallExpMultiplier(); + if ($overallMultiplier > self::$dontAddWorseThan[self::EXP_ROUTE]) { + if (isset(self::$expRoutes[$overallMultiplier])) { + self::$expRoutes[$overallMultiplier][] = $r; + } else { + self::$expRoutes[$overallMultiplier] = [$r]; + } + } + } + + private static function addMoneyRoute(Route $r) : void { + $overallMultiplier = $r->getOverallMoneyMultiplier(); + if ($overallMultiplier > self::$dontAddWorseThan[self::MONEY_ROUTE]) { + if (isset(self::$moneyRoutes[$overallMultiplier])) { + self::$moneyRoutes[$overallMultiplier][] = $r; + } else { + self::$moneyRoutes[$overallMultiplier] = [$r]; + } + } + } + + private static function trimRoutes(int $trimToBestXRoutes) : void { + $i = 0; + krsort(self::$expRoutes); + foreach (self::$expRoutes as $multi => $routesByMulti) { + if(count($routesByMulti)+$i < $trimToBestXRoutes) { + $i += count($routesByMulti); + } + else if($i > $trimToBestXRoutes) { + unset(self::$expRoutes[$multi]); + } + else { + foreach($routesByMulti as $key => $value) { + $i++; + if($i < $trimToBestXRoutes) + continue; + if($i === $trimToBestXRoutes) { + self::$dontAddWorseThan[self::EXP_ROUTE] = $multi; + continue; + } + unset(self::$expRoutes[$multi][$key]); + } + } + } + + $i = 0; + krsort(self::$moneyRoutes); + foreach (self::$moneyRoutes as $multi => $routesByMulti) { + if(count($routesByMulti)+$i < $trimToBestXRoutes) { + $i += count($routesByMulti); + } + else if($i > $trimToBestXRoutes) { + unset(self::$moneyRoutes[$multi]); + continue; + } + else { + foreach($routesByMulti as $key => $value) { + $i++; + if($i < $trimToBestXRoutes) + continue; + if($i === $trimToBestXRoutes) { + self::$dontAddWorseThan[self::MONEY_ROUTE] = $multi; + continue; + } + unset(self::$moneyRoutes[$multi][$key]); + } + } + } + } +} diff --git a/tools/npc/npc.php b/tools/npc/npc.php index 8c8c8a42c..000fb68ac 100644 --- a/tools/npc/npc.php +++ b/tools/npc/npc.php @@ -203,18 +203,18 @@ function NPCStuff() { debug('We need to UNO, so off we go!'); processContainer($container); } - else if ($TRADE_ROUTE instanceof Route) { + else if ($TRADE_ROUTE instanceof \Routes\Route) { debug('Trade Route'); - $forwardRoute =& $TRADE_ROUTE->getForwardRoute(); - $returnRoute =& $TRADE_ROUTE->getReturnRoute(); + $forwardRoute = $TRADE_ROUTE->getForwardRoute(); + $returnRoute = $TRADE_ROUTE->getReturnRoute(); if ($forwardRoute->getBuySectorId() == $player->getSectorID() || $returnRoute->getBuySectorId() == $player->getSectorID()) { if ($forwardRoute->getBuySectorId() == $player->getSectorID()) { - $buyRoute =& $forwardRoute; - $sellRoute =& $returnRoute; + $buyRoute = $forwardRoute; + $sellRoute = $returnRoute; } else if ($returnRoute->getBuySectorId() == $player->getSectorID()) { - $buyRoute =& $returnRoute; - $sellRoute =& $forwardRoute; + $buyRoute = $returnRoute; + $sellRoute = $forwardRoute; } $ship = $player->getShip(); @@ -626,9 +626,6 @@ function &findRoutes($player) { $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()) { - // The "MultiPortRoute" class cannot be autoloaded because it is not - // in its own file. - require_once(get_file_loc('RouteGenerator.class.php')); $routes = unserialize(gzuncompress($db->getField('routes'))); debug('Using Cached Routes: #' . count($routes)); return $routes; @@ -644,17 +641,16 @@ function &findRoutes($player) { if ($maxNumberOfPorts == 1) - $allRoutes = RouteGenerator::generateOneWayRoutes($allSectors, $distances, $tradeGoods, $tradeRaces, $routesForPort); + $allRoutes = \Routes\RouteGenerator::generateOneWayRoutes($allSectors, $distances, $tradeGoods, $tradeRaces, $routesForPort); else - $allRoutes = RouteGenerator::generateMultiPortRoutes($maxNumberOfPorts, $allSectors, $tradeGoods, $tradeRaces, $distances, $routesForPort, $numberOfRoutes); + $allRoutes = \Routes\RouteGenerator::generateMultiPortRoutes($maxNumberOfPorts, $allSectors, $tradeGoods, $tradeRaces, $distances, $routesForPort, $numberOfRoutes); unset($distances); - $allRoutes =& $allRoutes[RouteGenerator::EXP_ROUTE]; $routesMerged = array(); - foreach ($allRoutes as $multi => &$routesByMulti) { + foreach ($allRoutes[\Routes\RouteGenerator::EXP_ROUTE] as $multi => $routesByMulti) { $routesMerged += $routesByMulti; //Merge arrays - } unset($routesByMulti); + } unset($allSectors); SmrPort::clearCache();