3 * The general simulator class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseSimulator extends BaseFrameworkSystem {
26 private $partInstance = null;
28 // Abmasse (Breite/Hoehe/Laenge)
33 // Aktuelles Schiff und Schiffsteil
34 private $currShip = null;
35 private $currPart = null;
37 // Faktoren zur Erweiterung der Masse. Beispielsweise soll der Maschinenraum groesser wie der Motor sein
38 private $resizeFactorArray = array(
45 protected function __construct ($class) {
46 // Call highest constructor
47 parent::__construct($class);
49 if ((defined('DEBUG_CORE')) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.<br />\n",
53 // Set part description and class name
54 $this->setObjectDescription("Simulator-Basis-Einheit");
56 // Clean up a little, dies sollte ganz zum Schluss erfolgen!
57 $this->removeResizeFactorArray();
58 $this->removeCurrPart();
59 $this->removeCurrShip();
62 // Setter-Methode fuer Laenge
63 public final function setLength ($length) {
64 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%dm</strong> Länge gesetzt.<br />\n",
68 $this->length = (float) $length;
71 // Setter-Methode fuer Breite
72 public final function setWidth ($width) {
73 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%dm</strong> Breite gesetzt.<br />\n",
77 $this->width = (float) $width;
80 // Setter-Methode fuer Hoehe
81 public final function setHeight ($height) {
82 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%dm</strong> Höhe gesetzt.<br />\n",
86 $this->height = (float) $height;
89 // Getter-Methode fuer Laenge
90 public final function getLength () {
91 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Länge angefordert.<br />\n",
97 // Getter-Methode fuer Breite
98 public final function getWidth () {
99 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Länge angefordert.<br />\n",
105 // Getter-Methode fuer Hoehe
106 public final function getHeight () {
107 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Höhe angefordert.<br />\n",
110 return $this->height;
113 // Setter-Methode fuer Teil-Instanz
114 public final function setPartInstance (ConstructableShipPart $partInstance) {
115 $this->partInstance = $partInstance;
118 // Getter-Methode fuer Teil-Instanz
119 public final function getPartInstance () {
120 if (!isset($this->partInstance)) {
123 return $this->partInstance;
126 // Remover-Methode fuer die Teil-Instanz
127 public final function removePartInstance () {
128 if ($this->getPartInstance() !== null) {
130 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] partInstance ist nicht null! Instanz-Attribut wird nicht entfernt.<br />\n",
134 // Leere Instanz kann entfernt werden
135 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] partInstance wurde entfernt.<br />\n",
138 unset($this->partInstance);
142 // Prueft ob all Umberechnungsfaktoren gesetzt sind
143 private function isResizeFactorValid () {
144 return (($this->getResizeFactorElement('width') > 1)
145 || ($this->getResizeFactorElement('height') > 1)
146 || ($this->getResizeFactorElement('length') > 1)
150 // Baut einen Motor in das Schiff ein
151 public function addShipPartToShip (ConstructableShip $shipInstance, ConstructableShipPart $partInstance) {
152 // Schiff/-steil merken
153 $this->currShip = $shipInstance;
154 $this->currPart = $partInstance;
156 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Maschinenraum mit Motor <strong>%s</strong> wird fuer das Schiff <strong>%s</strong> konstruiert.<br />\n",
158 $this->getCurrPart()->getObjectDescription(),
159 $this->currShip->getShipName()
162 // Passt ueberhaupt das Schiffsteil in's Schiff?
163 if ($this->isShipPartSizeValid()) {
164 // Berechnungen fuer umliegendes Objekt anpassen
165 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> vom Typ <strong>%s</strong> passt in das Schiff <strong>%s</strong> hinein.<br />\n",
167 $this->getCurrPart()->getObjectDescription(),
168 $this->getCurrPart()->__toString(),
169 $this->currShip->getShipName()
172 // Muessen die Masse angepasst werden?
173 if ($this->isResizeFactorValid()) {
174 // Neue Angaben berechnen (wir lassen etwas Lust fuer Kabelbaeume, Roehren, Maschinisten, etc.)
175 $this->newWidth = (float) $this->getCurrPart()->getWidth() * $this->resizeFactorArray['width'];
176 $this->newHeight = (float) $this->getCurrPart()->getHeight() * $this->resizeFactorArray['height'];
177 $this->newLength = (float) $this->getCurrPart()->getLength() * $this->resizeFactorArray['length'];
179 // Passt dies nun immer noch?
180 if ($this->isNewSizeValid()) {
181 // Das passt auch, dann Werte setzen und Motor-Instanz merken
182 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> passt in das Schiff <strong>%s</strong> hinein.<br />\n",
184 $this->getObjectDescription(),
185 $this->currShip->getShipName()
187 $this->setWidth($this->newWidth);
188 $this->setHeight($this->newHeight);
189 $this->setLength($this->newLength);
191 // Einige Dinge entfernen...
192 $this->removeAllNewAttr();
194 // Passt nicht! Also wieder Exception werfen...
195 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> vom Typ <strong>%s</strong> ist zu gross für das Schiff!",
196 $this->getCurrPart()->__toString(),
197 $this->getCurrPart()->getObjectDescription(),
198 $this->getCurrPart()->__toString()
201 } elseif ($this->currPart != null) {
202 // Aktuelle Masse setzen
203 $this->setWidth($this->getCurrPart()->getWidth());
204 $this->setHeight($this->getCurrPart()->getHeight());
205 $this->setLength($this->getCurrPart()->getLength());
208 // Existiert ein Schiffsteil?
209 if (!is_null($this->currPart)) {
210 // Debug-Meldung ausgeben
211 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Schiffsteil <strong>%s</strong> gefunden.<br />\n",
212 $this->getCurrPart()->realClass,
213 $this->getCurrPart()->getObjectDescription()
216 // Schiffsteil-Instanz setzen
217 $this->setPartInstance($this->currPart);
219 // Instanzen entfernen
220 $this->getCurrPart()->removeCurrShip();
221 $this->getCurrPart()->removeCurrPart();
222 $this->getCurrPart()->removePartInstance();
223 $this->getCurrPart()->removeResizeFactorArray();
227 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <u>%s</u> vom Typ <u>%s</u> passt nicht in das Schiff!",
228 $this->getCurrPart()->realClass,
229 $this->getCurrPart()->getObjectDescription(),
230 $this->getCurrPart()->__toString()
234 // Nochmals Clean up a little
235 $this->removeResizeFactorArray();
236 $this->removeCurrShip();
237 $this->removeCurrPart();
240 // Array fuer Umrechnungstabelle entfernen
241 public final function removeResizeFactorArray () {
242 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] removeResizeFactor erreicht.<br />\n",
245 unset($this->resizeFactorArray);
249 * Remove all new*** attributes
253 public final function removeAllNewAttr () {
254 unset($this->newWidth);
255 unset($this->newHeight);
256 unset($this->newLength);
260 * Remove current ship instance
264 public final function removeCurrShip () {
265 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] removeCurrShip erreicht.<br />\n",
268 unset($this->currShip);
271 // Aktuelle Schiffsteil-Instanz entfernen
272 public final function removeCurrPart () {
273 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] removeCurrPart erreicht.<br />\n",
276 unset($this->currPart);
280 public final function removeWidth () {
281 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Breite entfernt.<br />\n",
288 public final function removeHeight () {
289 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Höhe entfernt.<br />\n",
292 unset($this->height);
296 public final function removeLength () {
297 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Länge entfernt.<br />\n",
300 unset($this->length);
303 // Tiefgang entfernen
304 public final function removeDraught () {
305 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Tiefgang entfernt.<br />\n",
308 unset($this->draught);
311 // Getter-Methode fuer Element aus resizeFactor
312 public final function getResizeFactorElement ($el) {
313 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] getResizeFactorElement erreicht. (element=%s)<br />\n",
317 if (isset($this->resizeFactorArray[$el])) {
319 return $this->resizeFactorArray[$el];
321 // Element nicht gefunden!
326 // Setter-Methode fuer Element in resizeFactor
327 public final function setResizeFactorElement ($el, $value) {
328 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Umberechnungsfaktor <strong>%s</strong>=<strong>%s</strong> gesetzt.<br />\n",
333 $this->resizeFactorArray[$el] = (float) $value;
336 // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
337 public function isShipPartSizeValid () {
338 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] isShipPartSizeValid erreicht.<br />\n",
343 ( // Already defined ship messurings
344 ($this->getCurrPart()->getWidth() < $this->currShip->getWidth())
345 && ($this->getCurrPart()->getHeight() < $this->currShip->getDraught())
346 && ($this->getCurrPart()->getLength() < $this->currShip->getLength())
347 ) || ( // Ship messurings shall be calculated
348 ($this->currShip->getWidth() == 0)
349 && ($this->currShip->getHeight() == 0)
350 && ($this->currShip->getLength() == 0)
352 // The inserted part must be messured!
353 ) && ($this->getCurrPart()->getWidth() > 0)
354 && ($this->getCurrPart()->getHeight() > 0)
355 && ($this->getCurrPart()->getLength() > 0)
359 // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
360 public function isNewSizeValid () {
361 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] isNewSizeValid erreicht.<br />\n",
365 ( // Already defined ship messurings
366 ($this->newWidth < $this->currShip->getWidth())
367 && ($this->newHeight < $this->currShip->getDraught())
368 && ($this->newLength < $this->currShip->getLength())
369 ) || ( // Ship messurings shall be calculated
370 ($this->currShip->getWidth() == 0)
371 && ($this->currShip->getHeight() == 0)
372 && ($this->currShip->getLength() == 0)
378 public function extractDimensions ($dim) {
379 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] extractDimensions erreicht für <strong>%s</strong>.<br />\n",
381 $this->getObjectDescription()
385 if ((isset($dim)) && (is_array($dim)) && (count($dim) == 3)) {
386 // Abmasse aus Array holen
387 $this->setWidth($dim[0]);
388 $this->setHeight($dim[1]);
389 $this->setLength($dim[2]);
391 // Nicht gefundene Abmasse!
392 throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
397 * Getter for current part instance
399 * @return $currPart Instance of the current ship part object
401 public final function getCurrPart () {
402 return $this->currPart;