Added full source code
This commit is contained in:
commit
33f8586698
1377 changed files with 123808 additions and 0 deletions
scripts/game/beta5/moving/library
60
scripts/game/beta5/moving/library/cloneObject.inc
Normal file
60
scripts/game/beta5/moving/library/cloneObject.inc
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_cloneObject {
|
||||
|
||||
function beta5_moving_cloneObject($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
$this->standby = $this->lib->game->getLib('beta5/standby');
|
||||
}
|
||||
|
||||
// Clone a moving object
|
||||
function run($oid) {
|
||||
// Get object record
|
||||
$q = $this->db->query("SELECT * FROM moving_object WHERE id=$oid");
|
||||
if (!($q && dbCount($q) == 1)) {
|
||||
return null;
|
||||
}
|
||||
$mo = dbFetchHash($q);
|
||||
|
||||
// Duplicate stand-by order if any
|
||||
if (!is_null($mo['wait_order'])) {
|
||||
$q = $this->db->query("SELECT * FROM hs_wait WHERE id=".$mo['wait_order']);
|
||||
$r = dbFetchHash($q);
|
||||
$wo = $this->standby->call('create', $r['time_left'],$r['drop_point'],$r['origin'],$r['time_spent']);
|
||||
} else {
|
||||
$wo = 'NULL';
|
||||
}
|
||||
|
||||
// Insert clone with a random value for time_left
|
||||
$rnd = rand($mo['time_left'],999999);
|
||||
$this->db->query("INSERT INTO moving_object(m_from,m_to,changed,time_left,hyperspace,wait_order) VALUES ("
|
||||
. $mo['m_from'] . "," . $mo['m_to'] . "," . $mo['changed'] . ",$rnd,"
|
||||
.dbBool($mo['hyperspace'] == 't').",$wo)");
|
||||
|
||||
// Get new entry's ID
|
||||
$q = $this->db->query("SELECT id FROM moving_object WHERE m_from=".$mo['m_from']." AND m_to=".$mo['m_to']." AND time_left=$rnd");
|
||||
list($moid) = dbFetchArray($q);
|
||||
|
||||
// Reset time_left to its real value
|
||||
$this->db->query("UPDATE moving_object SET time_left=".$mo['time_left']." WHERE id=$moid");
|
||||
|
||||
// Get trajectory data
|
||||
$q = $this->db->query("SELECT * FROM waypoint WHERE move_id=$oid");
|
||||
$qsl = array();
|
||||
while ($r = dbFetchArray($q)) {
|
||||
array_push($qsl, "($moid,{$r[1]},{$r[2]})");
|
||||
}
|
||||
logText(count($qsl) . " waypoint(s) to be copied from object #$oid to object #$moid", LOG_DEBUG);
|
||||
foreach ($qsl as $qs) {
|
||||
if (!$this->db->query("INSERT INTO waypoint VALUES $qs")) {
|
||||
$this->db->query("DELETE FROM moving_object WHERE id=$moid");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return $moid;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
183
scripts/game/beta5/moving/library/computeTrajectory.inc
Normal file
183
scripts/game/beta5/moving/library/computeTrajectory.inc
Normal file
|
@ -0,0 +1,183 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_computeTrajectory {
|
||||
var $trajCache = null;
|
||||
|
||||
function beta5_moving_computeTrajectory($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
$this->planets = $this->lib->game->getLib('beta5/planet');
|
||||
}
|
||||
|
||||
// Get a fleet's trajectory (using the trajectory cache)
|
||||
function run($srcId, $dstId, $speed, $cruisers) {
|
||||
$s = "$srcId:$dstId:$speed:".($cruisers?1:0);
|
||||
if (is_null($this->trajCache[$s])) {
|
||||
$this->trajCache[$s] = $this->computeTrajectory($srcId, $dstId, $speed, $cruisers);
|
||||
}
|
||||
return $this->trajCache[$s];
|
||||
}
|
||||
|
||||
// Computes a fleet's trajectory
|
||||
function computeTrajectory($srcId, $dstId, $speed, $cruisers) {
|
||||
$src = $this->planets->call('byId', $srcId);
|
||||
$dst = $this->planets->call('byId', $dstId);
|
||||
|
||||
// Get planet list and base travel time
|
||||
$pList = array();
|
||||
if ($src['x'] == $dst['x'] && $src['y'] == $dst['y']) {
|
||||
// Planets are in the same system
|
||||
$minId = min($srcId,$dstId); $maxId = max($srcId,$dstId);
|
||||
logText("min/max id = $minId / $maxId");
|
||||
$q = $this->db->query("SELECT id,status,system FROM planet WHERE id>=$minId AND id<=$maxId ORDER BY id "
|
||||
. ($srcId==$minId ? 'ASC' : 'DESC'));
|
||||
$bTTime = 0;
|
||||
while ($r = dbFetchArray($q)) {
|
||||
array_push($pList, array($r[0], $r[1], $r[2]));
|
||||
$bTTime += ($r[0] == $minId || $r[0] == $maxId) ? 6 : 12;
|
||||
}
|
||||
} else {
|
||||
// Planets are in different systems
|
||||
if ($src['x'] != $dst['x']) {
|
||||
// Trajectory looks like y = ax + b
|
||||
$slope = ($dst['y'] - $src['y']) / ($dst['x'] - $src['x']);
|
||||
$displ = $src['y'] - $src['x'] * $slope;
|
||||
$vert = false;
|
||||
|
||||
$minX = min($dst['x'],$src['x']); $maxX = max($dst['x'],$src['x']);
|
||||
$minY = min($dst['y'],$src['y']); $maxY = max($dst['y'],$src['y']);
|
||||
$qs = "SELECT id,x,y FROM system WHERE x>=$minX AND x<=$maxX AND y>=$minY AND y<=$maxY AND ";
|
||||
if ($slope > 0)
|
||||
$qs .= "y+0.5>$slope*(x-0.5)+$displ AND y-0.5<$slope*(x+0.5)+$displ";
|
||||
else
|
||||
$qs .= "y+0.5>$slope*(x+0.5)+$displ AND y-0.5<$slope*(x-0.5)+$displ";
|
||||
$qs .= " ORDER BY x " .($minX == $dst['x'] ? 'DESC' : 'ASC') . ", y " . ($src['y'] < $dst['y'] ? "ASC" : "DESC");
|
||||
} else {
|
||||
// Trajectory looks like x = c
|
||||
$vert = true;
|
||||
$minY = min($dst['y'],$src['y']); $maxY = max($dst['y'],$src['y']);
|
||||
$qs = "SELECT id,x,y FROM system WHERE y>=$minY AND y<=$maxY AND x=".$src['x'];
|
||||
$qs .= " ORDER BY y " .($minY == $dst['y'] ? 'DESC' : 'ASC');
|
||||
}
|
||||
|
||||
$dX = $src['x'] - $dst['x']; $dY = $src['y'] - $dst['y'];
|
||||
$distance = sqrt($dX*$dX+$dY*$dY);
|
||||
$bTTime = round($distance * (70 - $speed * 10) * ($cruisers ? 2 : 1));
|
||||
|
||||
// List orbits
|
||||
$q = $this->db->query($qs);
|
||||
while ($r = dbFetchArray($q)) {
|
||||
$dir = "DESC";
|
||||
|
||||
// If we are in source or destination system, get all planets with orbits >= source/dest planet
|
||||
if ($r[1] == $src['x'] && $r[2] == $src['y']) {
|
||||
$dir = "ASC";
|
||||
$minOrbit = $src['orbit'];
|
||||
} elseif ($r[1] == $dst['x'] && $r[2] == $dst['y']) {
|
||||
$minOrbit = $dst['orbit'];
|
||||
} elseif ($vert) {
|
||||
// Vertical trajectories: 4 orbits out of 6 get intersected (1 / sqrt(2))
|
||||
$minOrbit = 2;
|
||||
} else {
|
||||
// If we are intersecting another system, 6 different cases:
|
||||
// (1) passing through x = xs - 0.5 and y = ys - 0.5
|
||||
// (2) passing through x = xs - 0.5 and y = ys + 0.5
|
||||
// (3) passing through x = xs - 0.5 and x = xs + 0.5
|
||||
// (4) passing through x = xs + 0.5 and y = ys - 0.5
|
||||
// (5) passing through x = xs + 0.5 and y = ys + 0.5
|
||||
// (6) passing through y = ys - 0.5 and y = ys + 0.5
|
||||
$y1 = ($r[1] - 0.5) * $slope + $displ;
|
||||
if ($y1 < $r[2] - 0.5 || $y1 > $r[2] + 0.5) {
|
||||
// Case (4), (5) or (6)
|
||||
$y1 = ($r[1] + 0.5) * $slope + $displ;
|
||||
if ($y1 < $r[2] - 0.5 || $y1 > $r[2] + 0.5) {
|
||||
// Case (6)
|
||||
$y1 = $r[2] - 0.5;
|
||||
$x1 = ($y1 - $displ) / $slope;
|
||||
$y2 = $r[2] + 0.5;
|
||||
$x2 = ($y2 - $displ) / $slope;
|
||||
} else {
|
||||
// Case (4) or (5)
|
||||
$x1 = $r[1] + 0.5;
|
||||
$x2 = ($r[2] - 0.5 - $displ) / $slope;
|
||||
if ($x2 < $r[1] - 0.5 || $x2 > $r[1] + 0.5) {
|
||||
// Case (5)
|
||||
$y2 = $r[2] + 0.5;
|
||||
$x2 = ($y2 - $displ) / $slope;
|
||||
} else {
|
||||
// Case (4)
|
||||
$y2 = $r[2] - 0.5;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Case (1), (2) or (3)
|
||||
$x1 = $r[1] - 0.5;
|
||||
$y2 = ($r[1] + 0.5) * $slope + $displ;
|
||||
if ($y2 < $r[2] - 0.5 || $y2 > $r[2] + 0.5) {
|
||||
// Case (1) or (2)
|
||||
$x2 = ($r[2] - 0.5 - $displ) / $slope;
|
||||
if ($x2 < $r[1] - 0.5 || $x2 > $r[1] + 0.5) {
|
||||
// Case (2)
|
||||
$y2 = $r[2] + 0.5;
|
||||
$x2 = ($y2 - $displ) / $slope;
|
||||
} else {
|
||||
// Case (1)
|
||||
$y2 = $r[2] + 0.5;
|
||||
}
|
||||
} else {
|
||||
// Case (3)
|
||||
$x2 = $r[1] + 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
$dX = $x1 - $x2; $dY = $y1 - $y2; $d = sqrt($dX*$dX+$dY*$dY);
|
||||
$no = round(1 + 5 * $d / sqrt(2));
|
||||
$minOrbit = 6 - $no;
|
||||
}
|
||||
|
||||
// List orbits for this system
|
||||
$q2 = $this->db->query("SELECT id,status FROM planet WHERE system=".$r[0]." AND orbit>=$minOrbit ORDER BY orbit $dir");
|
||||
while ($r2 = dbFetchArray($q2)) {
|
||||
array_push($pList, array($r2[0], $r2[1], $r[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate trajectory
|
||||
$trajectory = array(
|
||||
"hs" => ($src['x'] != $dst['x'] || $src['y'] != $dst['y']),
|
||||
"list" => array()
|
||||
);
|
||||
|
||||
$opDelay = array(0,26,26,38,56,80);
|
||||
$tTime = $bTTime;
|
||||
$nPlanets = count($pList);
|
||||
$perPlanet = floor($bTTime / ($nPlanets - 1));
|
||||
$perEndpoint = ($bTTime - ($perPlanet * ($nPlanets - 2))) / 2;
|
||||
|
||||
for ($i=0;$i<$nPlanets;$i++) {
|
||||
$otime = $opDelay[$pList[$i][1]];
|
||||
|
||||
if ($i == 0) {
|
||||
$trtime = floor($perEndpoint);
|
||||
$otime /= 2;
|
||||
} elseif ($i == $nPlanets - 1) {
|
||||
$trtime = ceil($perEndpoint);
|
||||
$otime /= 2;
|
||||
} else {
|
||||
$trtime = $perPlanet;
|
||||
}
|
||||
|
||||
$tTime += $otime;
|
||||
array_push($trajectory['list'], array(
|
||||
"pid" => $pList[$i][0],
|
||||
"time" => $trtime + $otime
|
||||
));
|
||||
}
|
||||
$trajectory['time'] = $tTime;
|
||||
|
||||
return $trajectory;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
26
scripts/game/beta5/moving/library/getLocation.inc
Normal file
26
scripts/game/beta5/moving/library/getLocation.inc
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_getLocation {
|
||||
|
||||
function beta5_moving_getLocation($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
}
|
||||
|
||||
|
||||
// Get a moving object's current location
|
||||
function run($oid) {
|
||||
$q = $this->db->query(
|
||||
"SELECT w.location FROM waypoint w,moving_object o "
|
||||
. "WHERE o.id=$oid AND w.move_id=$oid AND w.until_eta<=o.time_left "
|
||||
. "ORDER BY w.until_eta DESC LIMIT 1"
|
||||
);
|
||||
if (!($q && dbCount($q) == 1)) {
|
||||
return null;
|
||||
}
|
||||
list($loc) = dbFetchArray($q);
|
||||
return $loc;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
30
scripts/game/beta5/moving/library/getTrajectory.inc
Normal file
30
scripts/game/beta5/moving/library/getTrajectory.inc
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_getTrajectory {
|
||||
|
||||
function beta5_moving_getTrajectory($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
}
|
||||
|
||||
|
||||
// Get an object's current trajectory
|
||||
function run($oid) {
|
||||
$q = $this->db->query("SELECT w.location AS id,w.until_eta AS eta,s.x AS x,s.y AS y,p.orbit AS orbit,p.status AS opacity,p.name AS name "
|
||||
. "FROM waypoint w,planet p,system s "
|
||||
. "WHERE w.move_id=$oid AND p.id=w.location AND s.id=p.system "
|
||||
. "ORDER BY w.until_eta DESC");
|
||||
if (!($q && dbCount($q))) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$rv = array();
|
||||
while ($r = dbFetchHash($q)) {
|
||||
array_push($rv, $r);
|
||||
}
|
||||
|
||||
return $rv;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
53
scripts/game/beta5/moving/library/newObject.inc
Normal file
53
scripts/game/beta5/moving/library/newObject.inc
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_newObject {
|
||||
|
||||
function beta5_moving_newObject($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
}
|
||||
|
||||
|
||||
// Generate a new moving object entry
|
||||
function run($srcId, $dstId, $speed, $cruisers, $wait) {
|
||||
// Compute trajectory
|
||||
$t = $this->lib->call('computeTrajectory', $srcId, $dstId, $speed, $cruisers);
|
||||
|
||||
// Insert object entry
|
||||
$qs = "INSERT INTO moving_object(m_from,m_to,changed,time_left,hyperspace";
|
||||
if (!is_null($wait)) {
|
||||
$qs .= ",wait_order";
|
||||
}
|
||||
$isHS = ($t['hs'] || !is_null($wait)) ? 1 : 0;
|
||||
$qs .= ") VALUES ($srcId,$dstId,0,".$t['time']."," . dbBool($isHS);
|
||||
if (!is_null($wait)) {
|
||||
$qs .= ",$wait";
|
||||
}
|
||||
$qs .= ")";
|
||||
$mid = $this->db->query($qs);
|
||||
if (!$mid) {
|
||||
logText("New object: mid was null :s", LOG_WARNING);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create trajectory data
|
||||
$qsl = array(); $sum = 0;
|
||||
$tl = array_reverse($t['list']);
|
||||
foreach ($tl as $w) {
|
||||
array_push($qsl, "($mid,".$w['pid'].",$sum)");
|
||||
$sum += $w['time'];
|
||||
}
|
||||
logText(count($qsl) . " waypoint(s) to be added for new object #$mid", LOG_DEBUG);
|
||||
|
||||
// Insert trajectory data
|
||||
foreach ($qsl as $qs) {
|
||||
if (!$this->db->query("INSERT INTO waypoint VALUES $qs")) {
|
||||
$this->db->query("DELETE FROM moving_object WHERE id=$mid");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return $mid;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
49
scripts/game/beta5/moving/library/redirect.inc
Normal file
49
scripts/game/beta5/moving/library/redirect.inc
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_redirect {
|
||||
|
||||
function beta5_moving_redirect($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
}
|
||||
|
||||
|
||||
// Redirects a moving object to a new destination
|
||||
function run($oid, $newDest, $speed, $cruisers, $newWait) {
|
||||
$loc = $this->lib->call('getLocation', $oid);
|
||||
if (is_null($loc)) {
|
||||
return false;
|
||||
} elseif ($loc == $newDest) {
|
||||
return $this->lib->call('stop', $oid, $newWait);
|
||||
}
|
||||
|
||||
$t = $this->lib->call('computeTrajectory', $loc, $newDest, $speed, $cruisers);
|
||||
|
||||
$this->db->query("UPDATE moving_object SET m_to=$newDest,changed=changed+10,"
|
||||
. "hyperspace=" . dbBool($t['hs'] || !is_null($newWait))
|
||||
. ",wait_order=".(is_null($newWait)?"NULL":$newWait)
|
||||
. ",time_left=".$t['time']." WHERE id=$oid");
|
||||
logText("Deleting waypoints for moving object #$oid", LOG_DEBUG);
|
||||
$this->db->query("DELETE FROM waypoint WHERE move_id=$oid");
|
||||
|
||||
// Create trajectory data
|
||||
$qsl = array(); $sum = 0;
|
||||
$tl = array_reverse($t['list']);
|
||||
foreach ($tl as $w) {
|
||||
array_push($qsl, "($oid,".$w['pid'].",$sum)");
|
||||
$sum += $w['time'];
|
||||
}
|
||||
logText(count($qsl) . " new waypoint(s) reinserted for moving object #$oid", LOG_DEBUG);
|
||||
|
||||
// Insert trajectory data
|
||||
foreach ($qsl as $qs) {
|
||||
if (!$this->db->query("INSERT INTO waypoint VALUES $qs")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
42
scripts/game/beta5/moving/library/stop.inc
Normal file
42
scripts/game/beta5/moving/library/stop.inc
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
class beta5_moving_stop {
|
||||
|
||||
function beta5_moving_stop($lib) {
|
||||
$this->lib = $lib;
|
||||
$this->db = $this->lib->game->db;
|
||||
}
|
||||
|
||||
// Stop a moving object
|
||||
function run($oid, $newWait) {
|
||||
$q = $this->db->query(
|
||||
"SELECT w.location,w.until_eta,o.time_left "
|
||||
. "FROM waypoint w,moving_object o "
|
||||
. "WHERE o.id=$oid AND w.move_id=$oid AND w.until_eta<=o.time_left "
|
||||
. "ORDER BY w.until_eta DESC LIMIT 1"
|
||||
);
|
||||
if (!($q && dbCount($q) == 1)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
list($loc,$ue,$tl) = dbFetchArray($q);
|
||||
if ($ue == 0) {
|
||||
$this->db->query("UPDATE moving_object SET wait_order="
|
||||
. (is_null($newWait)?"NULL":$newWait)
|
||||
. " WHERE id=$oid");
|
||||
return false;
|
||||
}
|
||||
|
||||
$newDelay = 1 + $tl - $ue;
|
||||
$this->db->query("UPDATE moving_object SET m_to=$loc,changed=changed+10,"
|
||||
. "wait_order=".(is_null($newWait)?"NULL":$newWait).","
|
||||
. "time_left=$newDelay WHERE id=$oid");
|
||||
logText("Deleting waypoints for moving object #$oid", LOG_DEBUG);
|
||||
$this->db->query("DELETE FROM waypoint WHERE move_id=$oid");
|
||||
logText("Inserting single waypoint moving object #$oid", LOG_DEBUG);
|
||||
$this->db->query("INSERT INTO waypoint VALUES($oid,$loc,0)");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in a new issue