Skip to content

Commit

Permalink
Merge pull request #535 from hemberger/refs-in-loops
Browse files Browse the repository at this point in the history
Avoid unnecessary references in loops
  • Loading branch information
hemberger authored Jun 7, 2018
2 parents 5210748 + 8e92429 commit 7ae8442
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 111 deletions.
23 changes: 10 additions & 13 deletions admin/Default/1.6/universe_create_locations.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
require_once(get_file_loc('SmrGalaxy.class.inc'));

$locations =& SmrLocation::getAllLocations();
$locations = SmrLocation::getAllLocations();

// Initialize all location counts to zero
$totalLocs = array();
foreach ($locations as &$location) {
foreach ($locations as $location) {
$totalLocs[$location->getTypeID()] = 0;
}

Expand Down Expand Up @@ -46,28 +46,25 @@ public function added($locID) {
// Set any extra information to be displayed with each location
$locText = array();
$categories = new Categories();
foreach ($locations as &$location) {
foreach ($locations as $location) {
$extra = '<span class="small"><br />';
if ($location->isWeaponSold()) {
$extra .= $categories->addLoc($location->getTypeID(), 'Weapons');
$weaponsSold =& $location->getWeaponsSold();
foreach($weaponsSold as &$weapon) {
foreach ($location->getWeaponsSold() as $weapon) {
$extra .= $weapon->getName() . '&nbsp;&nbsp;&nbsp;(' . $weapon->getShieldDamage() . '/' . $weapon->getArmourDamage() . '/' . $weapon->getBaseAccuracy() . ')<br />';
} unset($weapon);
}
}
if ($location->isShipSold()) {
$extra .= $categories->addLoc($location->getTypeID(), 'Ships');
$shipsSold =& $location->getShipsSold();
foreach ($shipsSold as &$shipSold) {
foreach ($location->getShipsSold() as $shipSold) {
$extra .= $shipSold['Name'] . '<br />';
} unset($shipSold);
}
}
if ($location->isHardwareSold()) {
$extra .= $categories->addLoc($location->getTypeID(), 'Hardware');
$hardwareSold =& $location->getHardwareSold();
foreach ($hardwareSold as &$hardware) {
foreach ($location->getHardwareSold() as $hardware) {
$extra .= $hardware['Name'] . '<br />';
} unset($hardware);
}
}
if ($location->isBar()) {
$extra .= $categories->addLoc($location->getTypeID(), 'Bars');
Expand All @@ -85,7 +82,7 @@ public function added($locID) {
$extra .= '</span>';

$locText[$location->getTypeID()] = $location->getName() . $extra;
} unset($location);
}
$template->assign('LocText', $locText);
$template->assign('LocTypes', $categories->locTypes);

Expand Down
6 changes: 3 additions & 3 deletions admin/Default/1.6/universe_create_save_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@
$galaxy->setMaxForceTime($_POST['forces' . $i] * 3600);
}
SmrGalaxy::saveGalaxies();
$galaxies =& SmrGalaxy::getGameGalaxies($var['game_id'],true);
foreach($galaxies as &$galaxy) {
$galaxies = SmrGalaxy::getGameGalaxies($var['game_id'],true);
foreach ($galaxies as $galaxy) {
$galaxy->generateSectors();
} unset($galaxy);
}
SmrSector::saveSectors();
$var['message'] = '<span class="green">Success</span> : Succesfully created galaxies.';
}
Expand Down
10 changes: 4 additions & 6 deletions admin/Default/1.6/universe_create_sector_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,25 @@
}
$PHP_OUTPUT.= '</select>';
$PHP_OUTPUT.= '<select name="port_race">';
$races =& Globals::getRaces();
foreach ($races as &$race) {
foreach (Globals::getRaces() as $race) {
$PHP_OUTPUT.= '<option value="' . $race['Race ID'] . '"';
if ($sector->hasPort() && $sector->getPort()->getRaceID() == $race['Race ID']) $PHP_OUTPUT.= 'selected';
$PHP_OUTPUT.= '>' . $race['Race Name'] . '</option>';
} unset($race);
}
$PHP_OUTPUT.= '</select>';
//goods determined randomly to sway admin abuse
$PHP_OUTPUT.= '<br /><br />';
$locations =& SmrLocation::getAllLocations();
$sectorLocations =& $sector->getLocations();
for ($i=0;$i<UNI_GEN_LOCATION_SLOTS;$i++) {
$PHP_OUTPUT.= 'Location ' . ($i + 1) . ': <select name="loc_type' . $i . '">';
$PHP_OUTPUT.= '<option value="0">No Location</option>';
foreach ($locations as &$location) {
foreach (SmrLocation::getAllLocations() as $location) {
$PHP_OUTPUT.= '<option value="' . $location->getTypeID() . '"';
if (isset($sectorLocations[$i]) && $sectorLocations[$i]->equals($location)) {
$PHP_OUTPUT.= 'selected';
}
$PHP_OUTPUT.= '>' . $location->getName() . '</option>';
} unset($location);
}
$PHP_OUTPUT.= '</select><br />';
}

Expand Down
6 changes: 3 additions & 3 deletions engine/Default/bar_galmap_buy.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
$PHP_OUTPUT.=create_echo_form($container);
$PHP_OUTPUT.=('<select type="select" name="gal_id">');
$PHP_OUTPUT.=('<option value="0">[Select a galaxy]</option>');
$gameGalaxies =& SmrGalaxy::getGameGalaxies($player->getGameID());
foreach ($gameGalaxies as &$galaxy) {
$gameGalaxies = SmrGalaxy::getGameGalaxies($player->getGameID());
foreach ($gameGalaxies as $galaxy) {
$PHP_OUTPUT.=('<option value="'.$galaxy->getGalaxyID().'">' . $galaxy->getName() . '</option>');
} unset($galaxy);
}
$PHP_OUTPUT.=('</select><br />');
$PHP_OUTPUT.=create_submit('Buy the map');
$PHP_OUTPUT.=('</form></div>');
Expand Down
6 changes: 3 additions & 3 deletions engine/Default/course_plot_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

$startExists = false;
$targetExists = false;
$galaxies =& SmrGalaxy::getGameGalaxies($player->getGameID());
foreach($galaxies as &$galaxy) {
$galaxies = SmrGalaxy::getGameGalaxies($player->getGameID());
foreach ($galaxies as $galaxy) {
if($galaxy->contains($start))
$startExists = true;
if($galaxy->contains($target))
$targetExists = true;
} unset($galaxy);
}

if($startExists===false || $targetExists===false)
create_error('The sectors have to exist!');
Expand Down
8 changes: 4 additions & 4 deletions engine/Default/forces_attack_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@
$attackers = $player->getSector()->getFightingTradersAgainstForces($player, $bump);

//decloak all attackers
foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
$attacker->getShip()->decloak();
if (!$bump) {
$attacker->setLastSectorID(0);
}
} unset($attacker);
}

// If mines are bumped, the forces shoot first. Otherwise player shoots first.
if ($bump) {
$results['Forces'] =& $forces->shootPlayers($attackers, $bump);
}

$results['Attackers'] = array('TotalDamage' => 0);
foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
$playerResults =& $attacker->shootForces($forces);
$results['Attackers']['Traders'][$attacker->getAccountID()] =& $playerResults;
$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($attacker);
}

if (!$bump) {
$results['Forces'] =& $forces->shootPlayers($attackers, $bump);
Expand Down
14 changes: 7 additions & 7 deletions engine/Default/planet_attack_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@
'Forces' => array(),
'Forced' => false);

$attackers =& $sector->getFightingTradersAgainstPlanet($player, $planet);
$attackers = $sector->getFightingTradersAgainstPlanet($player, $planet);

$planet->attackedBy($player,$attackers);

//decloak all attackers
foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
$attacker->getShip()->decloak();
} unset($attacker);
}

foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
$playerResults =& $attacker->shootPlanet($planet, false);
$results['Attackers']['Traders'][$attacker->getAccountID()] =& $playerResults;
$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($attacker);
}
$results['Attackers']['Downgrades'] = $planet->checkForDowngrade($results['Attackers']['TotalDamage']);
$results['Planet'] =& $planet->shootPlayers($attackers);

Expand Down Expand Up @@ -87,10 +87,10 @@
}

// Update sector messages for attackers
foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
if(!$player->equals($attacker))
$db->query('REPLACE INTO sector_message VALUES(' . $attacker->getAccountID() . ',' . $attacker->getGameID() . ',' . $db->escapeString('[ATTACK_RESULTS]'.$logId) . ')');
} unset($attacker);
}

$container = create_container('skeleton.php', 'planet_attack.php');
$container['sector_id'] = $planet->getSectorID();
Expand Down
14 changes: 7 additions & 7 deletions engine/Default/port_attack_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@
'Forces' => array(),
'Forced' => false);

$attackers =& $sector->getFightingTradersAgainstPort($player, $port);
$attackers = $sector->getFightingTradersAgainstPort($player, $port);

$port->attackedBy($player,$attackers);

// take the turns and decloak all attackers
foreach($attackers as &$attacker) {
foreach($attackers as $attacker) {
$attacker->takeTurns(TURNS_TO_SHOOT_PORT,0);
$attacker->getShip()->decloak();
} unset($attacker);
}

foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
$playerResults =& $attacker->shootPort($port);
$results['Attackers']['Traders'][$attacker->getAccountID()] =& $playerResults;
$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($attacker);
}
$results['Attackers']['Downgrades'] = $port->checkForDowngrade($results['Attackers']['TotalDamage']);
$results['Port'] =& $port->shootPlayers($attackers);

Expand All @@ -61,11 +61,11 @@
$db->query('INSERT INTO combat_logs VALUES(\'\',' . $db->escapeNumber($player->getGameID()) . ',\'PORT\',' . $db->escapeNumber($port->getSectorID()) . ',' . $db->escapeNumber(TIME) . ',' . $db->escapeNumber($player->getAccountID()) . ',' . $db->escapeNumber($player->getAllianceID()) . ','.$db->escapeNumber(ACCOUNT_ID_PORT).',' . $db->escapeNumber(PORT_ALLIANCE_ID) . ',' . $db->escapeBinary(gzcompress($serializedResults)) . ')');
unserialize($serializedResults); //because of references we have to undo this.
$logId = $db->escapeString('[ATTACK_RESULTS]'.$db->getInsertID());
foreach($attackers as &$attacker) {
foreach ($attackers as $attacker) {
if(!$player->equals($attacker)) {
$db->query('REPLACE INTO sector_message VALUES(' . $db->escapeNumber($attacker->getAccountID()) . ',' . $db->escapeNumber($attacker->getGameID()) . ','.$logId.')');
}
} unset($attacker);
}

$container = create_container('skeleton.php','port_attack.php');

Expand Down
10 changes: 4 additions & 6 deletions engine/Default/sector_jump_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@
}

if ($sector->hasForces()) {
$sectorForces =& $sector->getForces();
foreach($sectorForces as &$forces) {
foreach ($sector->getForces() as $forces) {
if($forces->hasMines() && !$player->forceNAPAlliance($forces->getOwner())) {
create_error('You cannot jump when there are hostile mines in the sector!');
}
} unset($forces);
}
}

// create sector object for target sector
Expand Down Expand Up @@ -103,14 +102,13 @@
// send scout msg
$sector->enteringSector($player,MOVEMENT_JUMP);

$sectorForces =& $sector->getForces();
$mineOwnerID = false;
foreach($sectorForces as &$forces) {
foreach ($sector->getForces() as $forces) {
if(!$mineOwnerID && $forces->hasMines() && !$player->forceNAPAlliance($forces->getOwner())) {
$mineOwnerID = $forces->getOwnerID();
break;
}
} unset($forces);
}
if($mineOwnerID) {
if ($player->hasNewbieTurns()) {
$container = create_container('skeleton.php', 'current_sector.php');
Expand Down
10 changes: 4 additions & 6 deletions engine/Default/sector_scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@
$enemy_vessel = 0;

// iterate over all forces in the target sector
$scanSectorAllForces =& $scanSector->getForces();
foreach ($scanSectorAllForces as &$scanSectorForces) {
foreach ($scanSector->getForces() as $scanSectorForces) {
// decide if it's a friendly or enemy stack
if ($player->sameAlliance($scanSectorForces->getOwner()))
$friendly_forces += $scanSectorForces->getMines() * 3 + $scanSectorForces->getCDs() * 2 + $scanSectorForces->getSDs();
else
$enemy_forces += $scanSectorForces->getMines() * 3 + $scanSectorForces->getCDs() * 2 + $scanSectorForces->getSDs();
} unset($scanSectorForces);
}

$scanSectorPlayers =& $scanSector->getOtherTraders($player);
foreach($scanSectorPlayers as &$scanSectorPlayer) {
foreach ($scanSector->getOtherTraders($player) as $scanSectorPlayer) {
$scanSectorShip =& $scanSectorPlayer->getShip();

// he's a friend if he's in our alliance (and we are not in a 0 alliance
if ($player->traderMAPAlliance($scanSectorPlayer))
$friendly_vessel += $scanSectorShip->getAttackRating();
else
$enemy_vessel += $scanSectorShip->getDefenseRating() * 10;
} unset($scanSectorPlayer);
}

$PHP_OUTPUT.=('<p>');
$PHP_OUTPUT.=('<table class="standard">');
Expand Down
18 changes: 9 additions & 9 deletions engine/Default/trader_attack_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@
else if($targetPlayer->hasFederalProtection())
create_error('Target is under federal protection.');

$fightingPlayers =& $sector->getFightingTraders($player,$targetPlayer);
$fightingPlayers = $sector->getFightingTraders($player,$targetPlayer);

//decloak all fighters
foreach($fightingPlayers as &$teamPlayers) {
foreach($teamPlayers as &$teamPlayer) {
foreach ($fightingPlayers as $teamPlayers) {
foreach ($teamPlayers as $teamPlayer) {
$teamPlayer->getShip()->decloak();
} unset($teamPlayer);
} unset($teamPlayers);
}
}

// Take off the 3 turns for attacking
$player->takeTurns(3);
$player->update();

$results = array('Attackers' => array('Traders' => array(), 'TotalDamage' => 0),
'Defenders' => array('Traders' => array(), 'TotalDamage' => 0));
foreach($fightingPlayers['Attackers'] as $accountID => &$teamPlayer) {
foreach ($fightingPlayers['Attackers'] as $accountID => $teamPlayer) {
$playerResults =& $teamPlayer->shootPlayers($fightingPlayers['Defenders']);
$results['Attackers']['Traders'][$teamPlayer->getAccountID()] =& $playerResults;
$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($teamPlayer);
foreach($fightingPlayers['Defenders'] as $accountID => &$teamPlayer) {
}
foreach ($fightingPlayers['Defenders'] as $accountID => $teamPlayer) {
$playerResults =& $teamPlayer->shootPlayers($fightingPlayers['Attackers']);
$results['Defenders']['Traders'][$teamPlayer->getAccountID()] =& $playerResults;
$results['Defenders']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($teamPlayer);
}
$ship->removeUnderAttack(); //Don't show attacker the under attack message.

$account->log(LOG_TYPE_TRADER_COMBAT, 'Player attacks player, their team does ' . $results['Attackers']['TotalDamage'].' and the other team does '.$results['Defenders']['TotalDamage'], $sector->getSectorID());
Expand Down
4 changes: 2 additions & 2 deletions templates/Default/engine/Default/GalaxyMap.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<form name="GalaxyMapForm" method="GET">
<label for="galaxy_id">Switch galaxy</label>&nbsp;
<select name="galaxy_id" id="galaxy_id" onchange="this.form.submit()"><?php
foreach($GameGalaxies as &$GameGalaxy) {
foreach ($GameGalaxies as $GameGalaxy) {
$GalaxyID = $GameGalaxy->getGalaxyID(); ?>
<option value="<?php echo $GalaxyID; ?>"<?php if($ThisGalaxy->equals($GameGalaxy)){ ?> selected="selected"<?php } ?>>
<?php echo $GameGalaxy->getName(); ?>
</option><?php
} unset($GameGalaxy); ?>
} ?>
</select>&nbsp;
<input type="submit" value="View"/>
</form>
Expand Down
6 changes: 3 additions & 3 deletions templates/Default/engine/Default/alliance_message_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
?><th></th><?php
} ?>
</tr><?php
foreach($Thread['Replies'] as &$Reply) { ?>
foreach ($Thread['Replies'] as $Reply) { ?>
<tr>
<td class="shrink noWrap top"><?php echo $Reply['Sender']; ?></td>
<td><?php echo bbifyMessage($Reply['Message']); ?></td>
Expand All @@ -47,7 +47,7 @@
?><td class="shrink noWrap top"><a href="<?php echo $Reply['DeleteHref']; ?>"><img src="images/silk/cross.png" width="16" height="16" alt="Delete" title="Delete Post"/></a></td><?php
} ?>
</tr><?php
} unset($Reply); ?>
} ?>
</table>
</div><?php
if(isset($Thread['CreateThreadReplyFormHref'])) { ?>
Expand All @@ -63,4 +63,4 @@
<input class="submit" type="submit" name="action" value="Create Reply">&nbsp;<input type="submit" name="action" value="Preview Reply" id="InputFields" />
</form><?php
}
?>
?>
Loading

0 comments on commit 7ae8442

Please sign in to comment.