Skip to content

Commit

Permalink
npc.php: clean up canWeUNO function
Browse files Browse the repository at this point in the history
The logic for deciding to buy hardware has been significantly
simplified.
  • Loading branch information
hemberger committed Aug 20, 2021
1 parent 461ed5b commit 9815538
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/tools/npc/npc.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,27 +450,24 @@ function canWeUNO(AbstractSmrPlayer $player, bool $oppurtunisticOnly) : Page|fal
}
$sector = $player->getSector();

// We buy armour in preference to shields as it's cheaper.
// We buy cargo holds last if we have no newbie turns because we'd rather not die
$hardwareArray = array(HARDWARE_ARMOUR, HARDWARE_SHIELDS, HARDWARE_CARGO);

$amount = 0;
if ($player->getNewbieTurns() > MIN_NEWBIE_TURNS_TO_BUY_CARGO) {
// Buy cargo holds first if we have plenty of newbie turns left.
$hardwareArray = [HARDWARE_CARGO, HARDWARE_ARMOUR, HARDWARE_SHIELDS];
} else {
// We buy armour in preference to shields as it's cheaper.
// We buy cargo holds last if we have no newbie turns because we'd rather not die
$hardwareArray = [HARDWARE_ARMOUR, HARDWARE_SHIELDS, HARDWARE_CARGO];
}

foreach ($sector->getLocations() as $location) {
if ($location->isHardwareSold()) {
$hardwareSold = $location->getHardwareSold();
if ($player->getNewbieTurns() > MIN_NEWBIE_TURNS_TO_BUY_CARGO && !$ship->hasMaxCargoHolds() && isset($hardwareSold[HARDWARE_CARGO]) && ($amount = IFloor(($player->getCredits() - MINUMUM_RESERVE_CREDITS) / Globals::getHardwareCost(HARDWARE_CARGO))) > 0) { // Buy cargo holds first if we have plenty of newbie turns left.
$hardwareID = HARDWARE_CARGO;
} else {
foreach ($hardwareArray as $hardwareArrayID) {
if (!$ship->hasMaxHardware($hardwareArrayID) && isset($hardwareSold[$hardwareArrayID]) && ($amount = IFloor(($player->getCredits() - MINUMUM_RESERVE_CREDITS) / Globals::getHardwareCost($hardwareArrayID))) > 0) {
$hardwareID = $hardwareArrayID;
break;
}
}
foreach ($hardwareArray as $hardwareID) {
if (!$location->isHardwareSold($hardwareID)) {
continue;
}
if (isset($hardwareID)) {
$amount = min($ship->getType()->getMaxHardware($hardwareID) - $ship->getHardware($hardwareID), $amount);
$amountCanBuy = IFloor(($player->getCredits() - MINUMUM_RESERVE_CREDITS) / Globals::getHardwareCost($hardwareID));
$amountNeeded = $ship->getType()->getMaxHardware($hardwareID) - $ship->getHardware($hardwareID);
$amount = min($amountCanBuy, $amountNeeded);
if ($amount > 0) {
return doUNO($hardwareID, $amount, $sector->getSectorID());
}
}
Expand Down

0 comments on commit 9815538

Please sign in to comment.