Added full source code

This commit is contained in:
Emmanuel BENOîT 2016-01-10 11:01:49 +01:00
commit 33f8586698
1377 changed files with 123808 additions and 0 deletions

View file

@ -0,0 +1,27 @@
<?php
class beta5_planet_buildFactories {
function beta5_planet_buildFactories($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
// Build factories on a planet
function run($id, $nb, $t) {
$q = $this->db->query("SELECT owner FROM planet WHERE id = $id");
list($uid) = dbFetchArray($q);
$ru = $this->rules->call('get', $uid);
$this->db->query("UPDATE planet SET ${t}fact = ${t}fact + $nb WHERE id = $id");
$cost = $ru[$t.'f_cost'] * $nb;
$this->db->query("UPDATE player SET cash = cash - $cost WHERE id = $uid");
$this->lib->call('updateFactHistory', $id, $t == "m", $nb);
$this->lib->call('updateHappiness', $id);
}
}
?>

View file

@ -0,0 +1,60 @@
<?php
class beta5_planet_checkBuildFactories {
function beta5_planet_checkBuildFactories($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
$this->players = $this->game->getLib('beta5/player');
$this->rules = $this->game->getLib('beta5/rules');
}
// Check a planet to see whether it's possible to build factories
function run($id, $nb, $t) {
// Get planet data
$q = $this->db->query("SELECT ifact+mfact,sale,pop,owner FROM planet WHERE id=$id");
list($nf, $sale, $pop, $owner) = dbFetchArray($q);
logText("checkBuildFact($id, $nb, $t) : nf=$nf, sale=$sale owner=$owner");
// Get owner rules
$ru = $this->rules->call('get', $owner);
$cost = $ru["{$t}f_cost"] * $nb;
$pinf = $this->players->call('get', $owner);
if ($pinf['cash'] < $cost) {
return 1;
}
// Prevent building more factories if the planet's for sale
if (!is_null($sale)) {
return 2;
}
// Get the amount of factories built / destroyed in the past "24h" (actually, the interval between
// day ticks)
$interval = $this->game->ticks['day']->interval;
$q = $this->db->query(
"SELECT SUM(change) FROM facthist "
. "WHERE planet=$id AND UNIX_TIMESTAMP(NOW()) - moment <= $interval"
);
if (dbCount($q)) {
list($ch24) = dbFetchArray($q);
} else {
$ch24 = 0;
}
$onf = $nf - $ch24;
// We can't build more than optFact*1.5 factories every 24h
$x = ($pop - 2000) / 48000; $allowed = round(1.5 * (($pop / 30) - 754 * $x * $x));
if ($nf + $nb > $onf + $allowed) {
return 3;
}
// FIXME : siege
return 0;
}
}
?>

View file

@ -0,0 +1,74 @@
<?php
class beta5_planet_checkDestroyFactories {
function beta5_planet_checkDestroyFactories($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
}
// Check a planet to see whether it's possible to destroy factories
function run($id, $nb, $t) {
$q = $this->db->query("SELECT ${t}fact,sale FROM planet WHERE id = $id");
list($nf,$sale) = dbFetchArray($q);
logText("checkDestFact($id, $nb, $t) : nf=$nf, sale=$sale");
if ($nf < $nb) {
return 1;
}
if (!is_null($sale)) {
return 4;
}
if ($t == 'm' && $nf == $nb) {
return 2;
}
// Get changes for this type of factories within the past "2h" (actually, two hour ticks)
$interval = $this->game->ticks['hour']->interval * 2;
$q = $this->db->query(
"SELECT SUM(change) FROM facthist "
. "WHERE planet=$id AND UNIX_TIMESTAMP(NOW()) - moment <= $interval "
. "AND " . ($t == 'm' ? "" : "NOT ") . "is_military"
);
if (dbCount($q)) {
list($ch2) = dbFetchArray($q);
if (is_null($ch2)) {
$ch2 = 0;
}
} else {
$ch2 = 0;
}
if ($ch2 > 0) {
return 30;
}
// Get changes for this type of factories within the past 24h
$interval = $this->game->ticks['day']->interval;
$q = $this->db->query(
"SELECT SUM(change) FROM facthist "
. "WHERE planet=$id AND UNIX_TIMESTAMP(NOW()) - moment <= $interval "
. "AND " . ($t == 'm' ? "" : "NOT ") . "is_military"
);
if (dbCount($q)) {
list($ch24) = dbFetchArray($q);
if (is_null($ch24)) {
$ch24 = 0;
}
} else {
$ch24 = 0;
}
logText("checkDestFact($id, $nb, $t) : ch24=$ch24 ch2=$ch2");
$onf = $nf - $ch24;
if (($onf > 100 && $nf - $nb < floor($onf * 0.9)) || ($onf < 100 && ($nf - $nb < 0 || $nf - $nb < $onf - 10)) ) {
return 3;
}
// FIXME : siege
return 0;
}
}
?>

View file

@ -0,0 +1,52 @@
<?php
class beta5_planet_destroyTurrets {
function beta5_planet_destroyTurrets($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
}
// Try to destroy turrets on a planet
function run($id, $nb) {
$q = $this->db->query("SELECT turrets,sale FROM planet WHERE id = $id");
list($nt,$sale) = dbFetchArray($q);
if ($nt < $nb) {
return 1;
}
if (!is_null($sale)) {
return 3;
}
$interval = $this->game->ticks['day']->interval;
$q = $this->db->query(
"SELECT SUM(change) FROM turhist "
. "WHERE planet = $id AND UNIX_TIMESTAMP(NOW()) - moment < $interval"
);
if (dbCount($q)) {
list($ch) = dbFetchArray($q);
if (is_null($ch)) {
$ch = 0;
}
} else {
$ch = 0;
}
$ont = $nt - $ch;
if ($nt - $nb < floor($ont * 0.8)) {
return 2;
}
// FIXME : siege
$tm = time();
$this->db->query("UPDATE planet SET turrets = turrets - $nb WHERE id = $id");
$this->db->query("INSERT INTO turhist VALUES ($id,$tm,-$nb)");
$this->lib->call('updateHappiness', $id);
return 0;
}
}
?>

View file

@ -0,0 +1,241 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/planet/library/detectFleets.inc
//
// This function checks fleets in HyperSpace standby around a planet and
// tries to detect them if possible.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_planet_detectFleets {
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
$this->ecm = $this->game->getLib('beta5/ecm');
$this->msgs = $this->game->getLib('beta5/msg');
$this->players = $this->game->getLib('beta5/player');
$this->fleets = $this->game->getLib('beta5/fleet');
}
public function run($planet) {
// Get information about the planet
$q = $this->db->query("SELECT owner, beacon, name FROM planet WHERE id = $planet");
list($owner, $beacon, $name) = dbFetchArray($q);
// If there's no beacon or no owner, we're done
$reqLevel = $this->game->params['fakebeacons'] ? 1 : 2;
if (is_null($owner) || $beacon < $reqLevel) {
return;
}
// Get fleets that are in Hyperspace stand-by above the planet
$hsFleets = $this->getHSFleets($planet);
if (empty($hsFleets)) {
return;
}
// Store planet name
if (is_null($this->pNames)) {
$this->pNames = array();
}
if (is_null($this->pNames[$planet])) {
$this->pNames[$planet] = $name;
}
// Get the planet owner's trusted allies and alliance
$allies = $this->getAllies($owner);
$alliance = $this->getAlliance($owner);
// Check each fleet for detection
foreach ($hsFleets as $fleet) {
if ($fleet['owner'] == $owner || in_array($fleet['owner'], $allies)) {
continue;
}
$this->tryDetect($planet, $owner, $alliance, $fleet);
}
}
private function getHSFleets($planet) {
$q = $this->db->query(
"SELECT f.id, f.owner, w.time_spent FROM fleet f, hs_wait w "
. "WHERE f.location IS NULL AND f.moving IS NULL "
. "AND w.id = f.waiting AND w.drop_point = $planet "
. "AND f.id NOT IN (SELECT fleet FROM beacon_detection WHERE planet = $planet)"
);
$fleets = array();
while ($r = dbFetchHash($q)) {
array_push($fleets, $r);
}
return $fleets;
}
private function getAllies($player) {
$rawAllies = $this->players->call('getAllies', $player);
$allies = array();
foreach ($rawAllies as $ally) {
array_push($allies, $ally['id']);
}
return $allies;
}
private function getAlliance($player) {
if (is_null($this->pAlliances)) {
$this->pAlliances = array();
}
if (is_null($this->pAlliances[$player])) {
$q = $this->db->query("SELECT alliance FROM player WHERE id = $player AND a_status = 'IN'");
if (dbCount($q)) {
list($alliance) = dbFetchArray($q);
} else {
$alliance = null;
}
$this->pAlliances[$player] = $alliance;
}
return $this->pAlliances[$player];
}
private function tryDetect($planet, $owner, $alliance, $fleet) {
// Compute probability of detection according to time spent
$tsProb = ($fleet['time_spent'] >= 3) ? 1 : (0.5 + $fleet['time_spent'] / 6);
// Get ECM/ECCM levels
$ecm = $this->getECMLevel($fleet['owner']);
$eccm = $this->getECCMLevel($owner);
// Compute probability of detection based on ECM/ECCM
$ecmProb = (1 + $this->ecm->call('getInformationLevel', $ecm, $eccm)) * 0.2;
// Get fleet owner's alliance
$fAlliance = $this->getAlliance($fleet['owner']);
// Actual probability
$prob = $tsProb * $ecmProb * 0.8;
if (!is_null($alliance) && $fAlliance === $alliance) {
$prob += 1/3;
}
$prob = min(1, $prob);
// Log it
logText("Planet #$planet: probability of detecting fleet #{$fleet['id']} is $prob "
. "(tsProb = $tsProb, ecmProb = $ecmProb)");
// Try detecting it
$rnd = rand(0, 100000) / 100000;
if ($rnd < $prob) {
$this->fleetDetected($planet, $owner, $fleet, $ecm, $eccm);
}
}
private function getECCMLevel($player) {
if (is_null($this->eccmLevel)) {
$q = $this->db->query("SELECT value FROM rule WHERE player = $player AND name = 'eccm_level'");
list($this->eccmLevel) = dbFetchArray($q);
}
return $this->eccmLevel;
}
private function getECMLevel($player) {
if (is_null($this->ecmLevel)) {
$this->ecmLevel = array();
}
if (is_null($this->ecmLevel[$player])) {
$q = $this->db->query("SELECT value FROM rule WHERE player = $player AND name = 'ecm_level'");
list($this->ecmLevel[$player]) = dbFetchArray($q);
}
return $this->ecmLevel[$player];
}
private function fleetDetected($planet, $owner, $fleet, $ecm, $eccm) {
// Get the information level
$iLevel = $this->ecm->call('getInformationLevel', $ecm, $eccm);
// Detected fleet size
if ($iLevel > 0) {
$fleetSize = $this->computeDetectedSize($iLevel, $fleet['id']);
} else {
$fleetSize = null;
}
logText("Planet #$planet: fleet #{$fleet['id']} detected at level $iLevel"
. ($iLevel == 0 ? "" : " (size: $fleetSize)"));
// Did we detect the owner?
if ($iLevel == 4) {
$fleetOwner = $fleet['owner'];
$fleetOwnerName = $this->players->call('getName', $fleetOwner);
} else {
$fleetOwner = $fleetOwnerName = null;
}
// Insert into the status table
$sQuery = "INSERT INTO beacon_detection(planet, fleet, i_level";
if ($iLevel > 0) {
$sQuery .= ", fl_size";
if ($iLevel == 4) {
$sQuery .= ", fl_owner";
}
}
$sQuery .= ") VALUES ($planet, {$fleet['id']}, $iLevel";
if ($iLevel > 0) {
$sQuery .= ", $fleetSize";
if ($iLevel == 4) {
$sQuery .= ", $fleetOwner";
}
}
$this->db->query("$sQuery)");
// Send messages
$this->msgs->call('send', $owner, 'detect', array(
"planet" => $planet,
"p_name" => $this->pNames[$planet],
"is_owner" => 'f',
"i_level" => $iLevel,
"fl_size" => $fleetSize,
"flo_id" => $fleetOwner,
"flo_name" => $fleetOwnerName
));
$this->msgs->call('send', $fleet['owner'], 'detect', array(
"planet" => $planet,
"p_name" => $this->pNames[$planet],
"is_owner" => 't',
"i_level" => $iLevel
));
}
private function computeDetectedSize($iLevel, $fleet) {
// Compute the actual fleet size
$fData = $this->fleets->call('get', $fleet);
$actualFleetSize = $this->fleets->call('getPower', $fData['owner'], $fData['gaships'],
$fData['fighters'], $fData['cruisers'], $fData['bcruisers']);
// Compute the detected fleet size
if ($iLevel >= 3) {
$variation = 0;
} elseif ($iLevel == 2) {
$variation = rand(5, 10) / 100;
} elseif ($iLevel == 1) {
$variation = rand(20, 30) / 100;
}
$variation = rand(0,1) ? $variation : (-$variation);
return round($actualFleetSize * ($variation + 1));
}
}
?>

View file

@ -0,0 +1,52 @@
<?php
class beta5_planet_getIncome {
function beta5_planet_getIncome($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
// Computes a planet's income
function run($owner, $pop = null, $happ = null, $ifact = null, $mfact = null,
$turrets = null, $corruption = null) {
if (is_null($pop)) {
$info = $owner;
$owner = $info['owner'];
$pop = $info['pop'];
$happ = $info['happiness'];
$ifact = $info['ifact'];
$mfact = $info['mfact'];
$turrets = $info['turrets'];
$corruption = $info['corruption'];
}
$r = $this->rules->call('get', $owner);
if (is_null($r)) {
return array();
}
$biFact = ($happ >= 20) ? 1 : ($happ / 20);
$bi = floor($pop * $r['base_income'] * $biFact);
$ii = $ifact * $r['if_productivity'] * $r['if_productivity_factor'];
$fc = ($ifact + $mfact) * $r['factory_cost'];
$tc = $turrets * $r['turret_cost'];
$ti = $bi + $ii;
$cf = $corruption / 32000;
if ($cf > .1) {
$cf = $cf - .1;
$cl = ceil($ti * $cf);
} else {
$cl = 0;
}
$tot = $ti - $fc - $tc - $cl;
return array($tot, $bi, $ii, $fc, $tc, $cl);
}
}
?>

View file

@ -0,0 +1,36 @@
<?php
class beta5_planet_getPower {
var $ePower = array();
function beta5_planet_getPower($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
function run($pid, $turrets = null) {
if (is_null($turrets)) {
$q = $this->db->query("SELECT owner,turrets FROM planet WHERE id=$pid");
if (!($q && dbCount($q) == 1)) {
return 0;
}
list($owner,$turrets) = dbFetchArray($q);
} else {
$owner = $pid;
}
if (is_null($this->ePower[$owner])) {
$rules = $this->rules->call('get', $owner);
if (is_null($rules)) {
return 0;
}
$this->ePower[$owner] = floor($rules['turret_power'] * $rules['effective_fleet_power'] / 100);
}
return $turrets * $this->ePower[$owner];
}
}
?>

View file

@ -0,0 +1,37 @@
<?php
class beta5_planet_getStats {
function beta5_planet_getStats($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Returns data regarding current planets
function run($pid) {
$q = $this->db->query(
"SELECT COUNT(*), SUM(pop), SUM(ifact) + SUM(mfact), SUM(turrets), SUM(happiness), SUM(corruption) "
. " FROM planet WHERE owner = $pid GROUP BY owner"
);
$row = dbFetchArray($q);
if (!$row) {
return array(0, 'N/A', 0, 'N/A', 0, 'N/A', 0, 'N/A', 0, 'N/A');
}
$c = $row[0];
$pa = floor($row[1] / $c);
$fa = floor($row[2] / $c);
$ta = floor($row[3] / $c);
$ha = floor($row[4] / $c);
$ca = round($row[5] / ($c*320));
$q = $this->db->query(
"SELECT p.id FROM planet p, fleet f "
. "WHERE p.owner = $pid AND f.location = p.id AND f.attacking "
. "GROUP BY p.id"
);
$ua = dbCount($q);
return array($c, $ha, $row[1], $pa, $row[2], $fa, $row[3], $ta, $ua, $ca);
}
}
?>

View file

@ -0,0 +1,44 @@
<?php
class beta5_planet_ownerChange {
function beta5_planet_ownerChange($lib) {
$this->lib = $lib;
$this->db = $lib->game->db;
$this->bq = $lib->game->getLib('beta5/bq');
}
function run($planet, $newOwner = null, $noVacation = 'NO') {
$info = $this->lib->call('byId', $planet);
if (is_null($info)) {
logText("beta5::planet::ownerChange(): planet '$planet' not found", LOG_WARNING);
return;
}
$this->bq->call('flush', $planet);
$rqs = is_null($info['owner']) ? "" : (", renamed = " . time() . " ");
$this->db->query(
"UPDATE planet "
. "SET beacon = 0, built_probe = FALSE, probe_policy = NULL, abandon = NULL, "
. "vacation = '$noVacation', bh_prep = NULL, sale = NULL, "
. "owner = " . (is_null($newOwner) ? "NULL" : $newOwner) . $rqs
. "WHERE id = $planet"
);
if (is_null($newOwner)) {
$this->db->query("DELETE FROM planet_abandon_time WHERE id = $planet");
} elseif (is_null($info['owner'])) {
$this->db->query(
"INSERT INTO planet_abandon_time (id, time_required) "
. "VALUES ($planet, 6)"
);
} else {
$this->db->query(
"UPDATE planet_abandon_time SET time_required = 6 WHERE id = $planet"
);
}
$this->lib->call('updateHappiness', $planet);
return $this->lib->call('updateMaxPopulation', $planet, $info['owner'], $newOwner);
}
}

View file

@ -0,0 +1,53 @@
<?php
class beta5_planet_rename {
function beta5_planet_rename($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
// Renames a planet
function run($pid, $name) {
// Check the old name
$n = addslashes($name);
$q = $this->db->query("SELECT name,owner FROM planet WHERE id=$pid");
list($oName,$oid) = dbFetchArray($q);
if ($oName == $name) {
return;
}
// Get the list of players who have fleets or probes
// orbitting or moving towards the planet
$pl = array();
$q = $this->db->query("SELECT DISTINCT owner FROM fleet WHERE location=$pid" . (is_null($oid) ? "" : " AND owner<>$oid"));
while ($r = dbFetchArray($q)) {
$pl[$r[0]] = 'ORBIT';
}
$q = $this->db->query("SELECT DISTINCT f.owner FROM fleet f,moving_object o "
. "WHERE f.location IS NULL AND f.moving=o.id AND o.m_to=$pid"
. (is_null($oid) ? "" : " AND f.owner<>$oid"));
while ($r = dbFetchArray($q)) {
$pl[$r[0]] = 'MOVE';
}
// FIXME: probes
// Send messages to the players
$on = addslashes($oName);
$tm = time();
foreach ($pl as $plId => $st) {
$this->db->query("INSERT INTO message(player,sent_on,mtype,ftype,is_new)"
." VALUES($plId,$tm,'rename','INT',TRUE)");
$q = $this->db->query("SELECT id FROM message "
. "WHERE player=$plId AND sent_on=$tm AND mtype='rename' "
. "ORDER BY id DESC LIMIT 1");
list($mid) = dbFetchArray($q);
$this->db->query("INSERT INTO msg_rename VALUES($mid,$pid,'$st','$on','$n')");
}
$this->db->query("UPDATE planet SET name='$n',renamed=$tm,mod_check=FALSE WHERE id=$pid");
}
}
?>

View file

@ -0,0 +1,114 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// beta5/planet/library/restoreNeutral.inc
//
// This function handles a neutral planet by starting to restore it to
// a new player-friendly planet.
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class beta5_planet_restoreNeutral {
public function __construct($lib) {
$this->lib = $lib;
$this->game = $this->lib->game;
$this->db = $this->game->db;
}
public function run($planetID) {
// Get the planet
$planet = $this->lib->call('byId', $planetID);
// Compute optimal factories and turrets
$x = ($planet['pop'] - 2000) / 48000;
$optFact = ($planet['pop'] / 30) - 754 * $x * $x;
$optTurrets = ($planet['pop'] / 22) - 510 * $x * $x;
// Check if the planet is OK already
$facts = $planet['ifact'] + $planet['mfact'];
$turrets = $planet['turrets'];
if ($facts >= $optFact * 0.9 && $facts <= $optFact * 1.1 && $turrets <= 0.6 * $optTurrets) {
return true;
}
// If the planet's factories are outside the range, improve that
if ($facts < $optFact * 0.9) {
$facts = $this->buildFactories($planet, $optFact);
} else if ($facts > $optFact * 1.1) {
$facts = $this->destroyFactories($planet, $optFact);
}
// If the planet's turrets are outside the range, improve that
if ($turrets > 0.5 * $optTurrets) {
$turrets = $this->destroyTurrets($planet, $optTurrets * 0.5);
}
// Recompute happiness
$this->lib->call('updateHappiness', $planet['id']);
return ($facts >= $optFact * 0.9 && $facts <= $optFact * 1.1 && $turrets <= 0.6 * $optTurrets);
}
private function buildFactories($planet, $optimal) {
// Compute how many factories we should build
$total = $planet['ifact'] + $planet['mfact'];
$toBuild = ($optimal - $total) * 0.02;
// Use ratios to determine which types to build
$mFacts = ceil($toBuild * $planet['ifact'] / $total);
$iFacts = ceil($toBuild * $planet['mfact'] / $total);
// Update the planet
$this->db->query("UPDATE planet SET mfact = mfact + $mFacts, ifact = ifact + $iFacts "
. "WHERE id = {$planet['id']}");
// Log it
logText("NEUTRAL RESTORE (#{$planet['id']}): +$mFacts MFs/$iFacts IFs ($total => $optimal)");
return $total + $mFacts + $iFacts;
}
private function destroyFactories($planet, $optimal) {
// Compute how many factories we should destroy
$total = $planet['ifact'] + $planet['mfact'];
$toDestroy = ($total - $optimal) * 0.05;
// Use ratios to determine which types to destroy
$mFacts = ceil($toDestroy * $planet['mfact'] / $total);
$iFacts = ceil($toDestroy * $planet['ifact'] / $total);
// Update the planet
$this->db->query("UPDATE planet SET mfact = mfact - $mFacts, ifact = ifact - $iFacts "
. "WHERE id = {$planet['id']}");
// Log it
logText("NEUTRAL RESTORE (#{$planet['id']}): -$mFacts MFs/$iFacts IFs ($total => $optimal)");
return $total - ( $mFacts + $iFacts );
}
private function destroyTurrets($planet, $target) {
// Compute the amount of turrets to destroy
$turrets = $planet['turrets'];
$toDestroy = ceil(($turrets - $target) * 0.1);
// Update the planet
$this->db->query("UPDATE planet SET turrets = turrets - $toDestroy WHERE id = {$planet['id']}");
// Log it
logText("NEUTRAL RESTORE (#{$planet['id']}): -$toDestroy turrets ($turrets => $target)");
return $total - $toDestroy;
}
}
?>

View file

@ -0,0 +1,150 @@
<?php
class beta5_planet_updateHappiness {
var $avgFPower = null;
function beta5_planet_updateHappiness($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->players = $this->lib->game->getLib('beta5/player');
$this->fleets = $this->lib->game->getLib('beta5/fleet');
$this->rules = $this->lib->game->getLib('beta5/rules');
$this->msg = $this->lib->game->getLib('beta5/msg');
$this->main = $this->lib->game->getLib();
}
// Updates happiness for a planet
function run($id) {
$pinf = $this->lib->call('byId', $id);
if ($pinf['status'] != 0) {
return 0;
}
$oid = $pinf['owner'];
$rules = $this->rules->call('get', $oid);
if (!is_null($oid)) {
$q = $this->db->query("SELECT bh_unhappiness FROM player WHERE id=$oid");
list($bhu) = dbFetchArray($q);
} else {
$bhu = 0;
}
$uf = ($bhu * 7 + $rules['unhappiness_factor']) / 100;
// Factory happiness
$x = ($pinf['pop'] - 2000) / 48000;
$optFact = ($pinf['pop'] / 30) - 754 * $x * $x; $bFHap = 0.9;
$x = $pinf['mfact'] + $pinf['ifact'];
if ($x <= $optFact) {
$tmp = ($bFHap - 1) / $optFact;
$hapFact = $x * $x * $tmp / $optFact - 2 * $x * $tmp + $bFHap;
} elseif ($x <= 3 * $optFact) {
$tmp = 4 * $optFact;
$hapFact = - $x * $x / (2 * $tmp * $optFact) + $x / $tmp + 7 / 8;
} else {
$tmp = 2 * ($x - 3 * $optFact) / $optFact;
if ($tmp < 500) {
$hapFact = 1 - exp($tmp) / (exp($tmp) + 1);
} else {
$hapFact = 0;
}
}
// Turret happiness
$x = ($pinf['pop'] - 2000) / 48000;
$optTurrets = ($pinf['pop'] / 22) - 510 * $x * $x; $bTHap = 0.7;
$x = $pinf['turrets'];
if ($x <= $optTurrets) {
$tmp = ($bTHap - 1) / $optTurrets;
$hapTurrets = $x * $x * $tmp / $optTurrets - 2 * $x * $tmp + $bTHap;
} elseif ($x <= 4 * $optTurrets) {
$tmp = 9 * $optTurrets;
$hapTurrets = - $x * $x / (2 * $tmp * $optTurrets) + $x / $tmp + 17 / 18;
} else {
$tmp = 2 * ($x - 4 * $optTurrets) / $optTurrets;
if ($tmp < 500) {
$hapTurrets = 1 - exp($tmp) / (exp($tmp) + 1);
} else {
$hapTurrets = 0;
}
}
$hapTurrets = (1 + $hapTurrets * 2) / 3;
// Get average fleet power
if (is_null($this->avgFPower)) {
$q = $this->db->query("SELECT SUM(gaships),SUM(fighters),SUM(cruisers),SUM(bcruisers) FROM fleet");
list($g,$f,$c,$b) = dbFetchArray($q);
$np = $this->main->call('getPlayerCount');
if ($np) {
$g /= $np; $f /= $np; $c /= $np; $b /= $np;
$fpAvg = $g * 5 + $f * 10 + $c * 40 + $b * 80;
} else {
$fpAvg = 0;
}
$this->avgFPower = $fpAvg;
} else {
$fpAvg = $this->avgFPower;
}
// Get total and local fleet powers
if (!is_null($oid)) {
$q = $this->db->query("SELECT SUM(gaships),SUM(fighters),SUM(cruisers),SUM(bcruisers) FROM fleet WHERE location=$id AND owner=$oid");
list($g,$f,$c,$b) = dbFetchArray($q);
$fpLoc = $this->fleets->call('getPower', $oid, $g, $f, $c, $b);
$fpTot = $this->players->call('getPower', $oid);
} else {
$fpLoc = $fpTot = 0;
}
// Fleet happiness
if ($fpAvg) {
$hapFTot = exp($fpTot / $fpAvg) / (exp($fpTot / $fpAvg) + 1);
if ($fpTot > 0) {
$hapFLoc = $hapFTot * $fpLoc / $fpTot;
} else {
$hapFLoc = 0;
}
} else {
$hapFLoc = 0;
}
$hapFLoc = (9 + $hapFLoc) / 10;
// Compute local happiness
$hapTot = 100 * $hapFact * $hapTurrets * $hapFLoc;
$hapTot -= $hapTot * $pinf['bh_unhappiness'] / 30;
$hapTot = max(0,min(100,round($hapTot)));
// Compute modifier from planet count and unhappiness factor
$x = min($this->players->call('getPlanetCount', $oid), 40);
$pcMod = pow(1 - 0.63 * exp(($x-18)/2) / (exp(($x-18)/2)+1) - log($x) / 10, $uf*$uf);
$hapTot = round($hapTot * $pcMod);
if ($hapTot == 'NAN') {
$hapTot = 0;
}
// Check for revolt
if (!is_null($pinf['owner'])) {
$send = false;
if ($pinf['happiness'] >= 20 && $hapTot < 20) {
$rStat = 'TRUE';
$send = true;
} elseif ($pinf['happiness'] < 20 && $hapTot >= 20) {
$rStat = 'FALSE';
$send = true;
}
if ($send) {
$tm = time();
$player = $pinf['owner'];
$this->msg->call('send', $player, 'revolt', array(
'planet' => $id,
'pname' => $pinf['name'],
'started' => $rStat
));
}
}
$this->db->query("UPDATE planet SET happiness=$hapTot WHERE id=$id");
return $hapTot;
}
}
?>

View file

@ -0,0 +1,66 @@
<?php
class beta5_planet_updateMaxPopulation {
var $maxPops = array();
function beta5_planet_updateMaxPopulation($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->rules = $this->lib->game->getLib('beta5/rules');
}
// Updates a planet's maximum population
function run($id, $formerOwner, $newOwner) {
// Get the current technological levels for both players
$r = $this->rules->call('get', $formerOwner);
$oldLevel = ($r['planet_max_pop'] - 10000) / 10000;
if ($formerOwner == $newOwner) {
$oldLevel --;
}
$r = $this->rules->call('get', $newOwner);
$newLevel = ($r['planet_max_pop'] - 10000) / 10000;
// If the levels are equivalent, return
if ($oldLevel == $newLevel) {
return -1;
}
// Get the planet's maximum populations for each tech level
$maxPops = $this->getMaxPopulations($id);
// Get the planet's current population
$pinf = $this->lib->call('byId', $id);
$pop = $pinf['pop'];
// If the population's greater than the new max. pop., fix the "real" max pop
$maxPop = $maxPops[$newLevel];
if ($pop > $maxPop) {
$maxPop = $pop + rand(0, min(500, $maxPops[3] - $pop));
}
// Update the planet
$this->db->query("UPDATE planet SET max_pop=$maxPop WHERE id=$id");
return $maxPop;
}
// Get a planet's maximum populations
function getMaxPopulations($id) {
if (is_null($id)) {
return null;
}
if (!is_array($this->maxPops[$id])) {
$this->maxPops[$id] = array();
$q = $this->db->query("SELECT max_pop FROM planet_max_pop WHERE planet=$id ORDER BY tech_level ASC");
$i = 0;
while ($r = dbFetchArray($q)) {
$this->maxPops[$id][$i++] = $r[0];
}
}
return $this->maxPops[$id];
}
}
?>

View file

@ -0,0 +1,107 @@
<?php
class beta5_planet_updateMilStatus {
function beta5_planet_updateMilStatus($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->ecm = $this->lib->game->getLib('beta5/ecm');
$this->fleets = $this->lib->game->getLib('beta5/fleet');
}
function run($planet) {
// Get previous status
$q = $this->db->query("SELECT * FROM attacks WHERE planet=$planet");
if ($q && dbCount($q)) {
$attack = dbFetchHash($q);
} else {
$attack = null;
}
// Get current planet owner
$ownId = $this->lib->call('getOwner', $planet);
// If it's neutral, we're done
if (is_null($ownId)) {
if (is_array($attack)) {
// No attack status for neutrals
$this->db->query("DELETE FROM attacks WHERE planet=$planet");
}
return;
}
// Select all fleets on the planet
$q = $this->db->query("SELECT owner,attacking,gaships,fighters,cruisers,bcruisers FROM fleet WHERE location=$planet");
$fList = array(); $attPlayers = array(); $defPlayers = array();
while ($r = dbFetchHash($q)) {
array_push($fList, $r);
if ($r['attacking'] == 't' && !in_array($r['owner'], $attPlayers)) {
array_push($attPlayers, $r['owner']);
} elseif ($r['attacking'] == 'f' && !in_array($r['owner'], $defPlayers)) {
array_push($defPlayers, $r['owner']);
}
}
// Not under attack
if (!count($attPlayers)) {
// ... but were we before?
if (is_array($attack)) {
$this->db->query("DELETE FROM attacks WHERE planet=$planet");
}
return;
}
// Add planet owner to defending players if he's not there already
if (!in_array($ownId, $defPlayers)) {
array_push($defPlayers, $ownId);
}
// Get max. ECM and ECCM levels
$q = $this->db->query("SELECT MAX(value) FROM rule WHERE name='ecm_level' AND player IN (".join(',',$attPlayers).")");
list($ecm) = dbFetchArray($q);
$q = $this->db->query("SELECT MAX(value) FROM rule WHERE name='eccm_level' AND player IN (".join(',',$defPlayers).")");
list($eccm) = dbFetchArray($q);
// Compute fleet powers
$attPower = $defPower = 0;
foreach ($fList as $fl) {
$pow = $this->fleets->call('getPower', $fl['owner'], $fl['gaships'], $fl['fighters'], $fl['cruisers'], $fl['bcruisers']);
if ($fl['attacking'] == 'f') {
$defPower += $pow;
} else {
$attPower += $pow;
}
}
$defPower += $this->lib->call('getPower', $planet);
// If situation hasn't changed, we're done
if (is_array($attack) && $attack['ecm_level'] == $ecm && $attack['eccm_level'] == $eccm && $attack['friendly'] == $defPower && $attack['enemy'] == $attPower) {
return;
}
// Generate new information level
$ifl = $this->ecm->call('getInformationLevel', $ecm, $eccm);
// If we weren't under attack before, we must add an entry
if (is_null($attack)) {
$qs = "INSERT INTO attacks VALUES($planet,$ecm,$eccm,$defPower,$attPower,";
$qs .= dbBool($ifl == 4);
$vDP = $this->ecm->call('scrambleFleetPower', $ifl, $defPower);
$vAP = $this->ecm->call('scrambleFleetPower', $ifl, $attPower);
$qs .= ",$vDP,$vAP";
$this->db->query("$qs)");
return;
}
// Update attack status
$qs = "UPDATE attacks SET v_players=" . dbBool($ifl == 4) . ",friendly=$defPower,enemy=$attPower,";
$vDP = $this->ecm->call('scrambleFleetPower', $ifl, $defPower);
$vAP = $this->ecm->call('scrambleFleetPower', $ifl, $attPower);
$qs .= "v_friendly=$vDP,v_enemy=$vAP,ecm_level=$ecm,eccm_level=$eccm WHERE planet=$planet";
$this->db->query($qs);
}
}
?>