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 ($className) {
46 // Call highest constructor
47 parent::__construct($className);
49 // Set part description and class name
50 $this->setObjectDescription("Simulator-Basis-Einheit");
52 // Clean up a little, dies sollte ganz zum Schluss erfolgen!
53 $this->removeResizeFactorArray();
54 $this->removeCurrPart();
55 $this->removeCurrShip();
58 // Setter-Methode fuer Laenge
59 public final function setLength ($length) {
60 $this->length = (float) $length;
63 // Setter-Methode fuer Breite
64 public final function setWidth ($width) {
65 $this->width = (float) $width;
68 // Setter-Methode fuer Hoehe
69 public final function setHeight ($height) {
70 $this->height = (float) $height;
73 // Getter-Methode fuer Laenge
74 public final function getLength () {
78 // Getter-Methode fuer Breite
79 public final function getWidth () {
83 // Getter-Methode fuer Hoehe
84 public final function getHeight () {
88 // Setter-Methode fuer Teil-Instanz
89 public final function setPartInstance (ConstructableShipPart $partInstance) {
90 $this->partInstance = $partInstance;
93 // Getter-Methode fuer Teil-Instanz
94 public final function getPartInstance () {
95 if (!isset($this->partInstance)) {
98 return $this->partInstance;
101 // Remover-Methode fuer die Teil-Instanz
102 public final function removePartInstance () {
103 unset($this->partInstance);
106 // Prueft ob all Umberechnungsfaktoren gesetzt sind
107 private function isResizeFactorValid () {
108 return (($this->getResizeFactorElement('width') > 1)
109 || ($this->getResizeFactorElement('height') > 1)
110 || ($this->getResizeFactorElement('length') > 1)
114 // Baut einen Motor in das Schiff ein
115 public function addShipPartToShip (ConstructableShip $shipInstance, ConstructableShipPart $partInstance) {
116 // Schiff/-steil merken
117 $this->currShip = $shipInstance;
118 $this->currPart = $partInstance;
120 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Maschinenraum mit Motor <strong>%s</strong> wird fuer das Schiff <strong>%s</strong> konstruiert.",
122 $this->getCurrPart()->getObjectDescription(),
123 $this->currShip->getShipName()
126 // Passt ueberhaupt das Schiffsteil in's Schiff?
127 if ($this->isShipPartSizeValid()) {
128 // Berechnungen fuer umliegendes Objekt anpassen
129 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.",
131 $this->getCurrPart()->getObjectDescription(),
132 $this->getCurrPart()->__toString(),
133 $this->currShip->getShipName()
136 // Muessen die Masse angepasst werden?
137 if ($this->isResizeFactorValid()) {
138 // Neue Angaben berechnen (wir lassen etwas Lust fuer Kabelbaeume, Roehren, Maschinisten, etc.)
139 $this->newWidth = (float) $this->getCurrPart()->getWidth() * $this->resizeFactorArray['width'];
140 $this->newHeight = (float) $this->getCurrPart()->getHeight() * $this->resizeFactorArray['height'];
141 $this->newLength = (float) $this->getCurrPart()->getLength() * $this->resizeFactorArray['length'];
143 // Passt dies nun immer noch?
144 if ($this->isNewSizeValid()) {
145 // Das passt auch, dann Werte setzen und Motor-Instanz merken
146 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> passt in das Schiff <strong>%s</strong> hinein.",
148 $this->getObjectDescription(),
149 $this->currShip->getShipName()
151 $this->setWidth($this->newWidth);
152 $this->setHeight($this->newHeight);
153 $this->setLength($this->newLength);
155 // Einige Dinge entfernen...
156 $this->removeAllNewAttr();
158 // Passt nicht! Also wieder Exception werfen...
159 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> vom Typ <strong>%s</strong> ist zu gross für das Schiff!",
160 $this->getCurrPart()->__toString(),
161 $this->getCurrPart()->getObjectDescription(),
162 $this->getCurrPart()->__toString()
165 } elseif ($this->currPart != null) {
166 // Aktuelle Masse setzen
167 $this->setWidth($this->getCurrPart()->getWidth());
168 $this->setHeight($this->getCurrPart()->getHeight());
169 $this->setLength($this->getCurrPart()->getLength());
172 // Existiert ein Schiffsteil?
173 if (!is_null($this->currPart)) {
174 // Debug-Meldung ausgeben
175 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Schiffsteil <strong>%s</strong> gefunden.",
176 $this->getCurrPart()->realClass,
177 $this->getCurrPart()->getObjectDescription()
180 // Schiffsteil-Instanz setzen
181 $this->setPartInstance($this->currPart);
183 // Instanzen entfernen
184 $this->getCurrPart()->removeCurrShip();
185 $this->getCurrPart()->removeCurrPart();
186 $this->getCurrPart()->removePartInstance();
187 $this->getCurrPart()->removeResizeFactorArray();
191 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <u>%s</u> vom Typ <u>%s</u> passt nicht in das Schiff!",
192 $this->getCurrPart()->realClass,
193 $this->getCurrPart()->getObjectDescription(),
194 $this->getCurrPart()->__toString()
198 // Nochmals Clean up a little
199 $this->removeResizeFactorArray();
200 $this->removeCurrShip();
201 $this->removeCurrPart();
204 // Array fuer Umrechnungstabelle entfernen
205 public final function removeResizeFactorArray () {
206 unset($this->resizeFactorArray);
210 * Remove all new*** attributes
214 public final function removeAllNewAttr () {
215 unset($this->newWidth);
216 unset($this->newHeight);
217 unset($this->newLength);
221 * Remove current ship instance
225 public final function removeCurrShip () {
226 unset($this->currShip);
229 // Aktuelle Schiffsteil-Instanz entfernen
230 public final function removeCurrPart () {
231 unset($this->currPart);
235 public final function removeWidth () {
240 public final function removeHeight () {
241 unset($this->height);
245 public final function removeLength () {
246 unset($this->length);
249 // Tiefgang entfernen
250 public final function removeDraught () {
251 unset($this->draught);
254 // Getter-Methode fuer Element aus resizeFactor
255 public final function getResizeFactorElement ($el) {
256 if (isset($this->resizeFactorArray[$el])) {
258 return $this->resizeFactorArray[$el];
260 // Element nicht gefunden!
265 // Setter-Methode fuer Element in resizeFactor
266 public final function setResizeFactorElement ($el, $value) {
267 $this->resizeFactorArray[$el] = (float) $value;
270 // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
271 public function isShipPartSizeValid () {
274 ( // Already defined ship messurings
275 ($this->getCurrPart()->getWidth() < $this->currShip->getWidth())
276 && ($this->getCurrPart()->getHeight() < $this->currShip->getDraught())
277 && ($this->getCurrPart()->getLength() < $this->currShip->getLength())
278 ) || ( // Ship messurings shall be calculated
279 ($this->currShip->getWidth() == 0)
280 && ($this->currShip->getHeight() == 0)
281 && ($this->currShip->getLength() == 0)
283 // The inserted part must be messured!
284 ) && ($this->getCurrPart()->getWidth() > 0)
285 && ($this->getCurrPart()->getHeight() > 0)
286 && ($this->getCurrPart()->getLength() > 0)
290 // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
291 public function isNewSizeValid () {
293 ( // Already defined ship messurings
294 ($this->newWidth < $this->currShip->getWidth())
295 && ($this->newHeight < $this->currShip->getDraught())
296 && ($this->newLength < $this->currShip->getLength())
297 ) || ( // Ship messurings shall be calculated
298 ($this->currShip->getWidth() == 0)
299 && ($this->currShip->getHeight() == 0)
300 && ($this->currShip->getLength() == 0)
306 public function extractDimensions ($dim) {
308 if ((isset($dim)) && (is_array($dim)) && (count($dim) == 3)) {
309 // Abmasse aus Array holen
310 $this->setWidth($dim[0]);
311 $this->setHeight($dim[1]);
312 $this->setLength($dim[2]);
314 // Nicht gefundene Abmasse!
315 throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
320 * Getter for current part instance
322 * @return $currPart Instance of the current ship part object
324 public final function getCurrPart () {
325 return $this->currPart;