3 // Ein sehr abstraktes Objekt, Abmasse, Beschreibung und Teil-Instanz werden hier gespeichert
4 class BaseSimulator extends BaseFrameworkSystem {
6 private $partInstance = null;
8 // Abmasse (Breite/Hoehe/Laenge)
13 // Aktuelles Schiff und Schiffsteil
14 private $currShip = null;
15 private $currPart = null;
17 // Faktoren zur Erweiterung der Masse. Beispielsweise soll der Maschinenraum groesser wie der Motor sein
18 private $resizeFactorArray = array(
25 private function __construct ($class) {
26 // Call highest constructor
27 parent::constructor($class);
29 if ((defined('DEBUG_CORE')) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.<br />\n",
33 // Set part description and class name
34 $this->setPartDescr("Simulator-Basis-Einheit");
36 // Etwas aufraeumen, dies sollte ganz zum Schluss erfolgen!
37 $this->removeResizeFactorArray();
38 $this->removeCurrPart();
39 $this->removeCurrShip();
43 public function constructor ($class) {
44 // Call real constructor
45 $this->__construct($class);
48 // Magic __isset method
49 private function __isset ($var) {
50 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Checking <strong>%s</strong> in class.<br />\n",
51 $this->__toString(), $var
53 return isset($this->$var);
56 // Magic __unset method
57 private function __unset($var) {
58 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Removing <strong>%s</strong> from class.<br />\n",
59 $this->__toString(), $var
64 // Setter-Methode fuer Laenge
65 public function setLength ($length) {
66 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%dm</strong> Länge gesetzt.<br />\n",
70 $this->length = (float) $length;
73 // Setter-Methode fuer Breite
74 public function setWidth ($width) {
75 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%dm</strong> Breite gesetzt.<br />\n",
79 $this->width = (float) $width;
82 // Setter-Methode fuer Hoehe
83 public function setHeight ($height) {
84 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] <strong>%dm</strong> Höhe gesetzt.<br />\n",
88 $this->height = (float) $height;
91 // Getter-Methode fuer Laenge
92 public function getLength () {
93 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Länge angefordert.<br />\n",
99 // Getter-Methode fuer Breite
100 public function getWidth () {
101 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Länge angefordert.<br />\n",
107 // Getter-Methode fuer Hoehe
108 public function getHeight () {
109 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Höhe angefordert.<br />\n",
112 return $this->height;
115 // Setter-Methode fuer Teil-Instanz
116 public function setPartInstance ($struct) {
117 $this->partInstance = (Object) $struct;
120 // Getter-Methode fuer Teil-Instanz
121 public function getPartInstance () {
122 if (!isset($this->partInstance)) {
125 return $this->partInstance;
128 // Remover-Methode fuer die Teil-Instanz
129 public function removePartInstance () {
130 if ($this->getPartInstance() !== null) {
132 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] partInstance ist nicht null! Instanz-Attribut wird nicht entfernt.<br />\n",
136 // Leere Instanz kann entfernt werden
137 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] partInstance wurde entfernt.<br />\n",
140 unset($this->partInstance);
144 // Prueft ob all Umberechnungsfaktoren gesetzt sind
145 private function isResizeFactorValid () {
146 return (($this->getResizeFactorElement('width') > 1)
147 || ($this->getResizeFactorElement('height') > 1)
148 || ($this->getResizeFactorElement('length') > 1)
152 // Baut einen Motor in das Schiff ein
153 public function addShipPartToShip (ConstructableShip $shipInstance, ConstructableShipPart $partInstance) {
154 // Schiff/-steil merken
155 $this->currShip = $shipInstance;
156 $this->currPart = $partInstance;
158 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",
160 $this->currPart->getPartDescr(),
161 $this->currShip->getShipName()
164 // Passt ueberhaupt das Schiffsteil in's Schiff?
165 if ($this->isShipPartSizeValid()) {
166 // Berechnungen fuer umliegendes Objekt anpassen
167 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",
169 $this->currPart->getPartDescr(),
170 $this->currPart->__toString(),
171 $this->currShip->getShipName()
174 // Muessen die Masse angepasst werden?
175 if ($this->isResizeFactorValid()) {
176 // Neue Angaben berechnen (wir lassen etwas Lust fuer Kabelbaeume, Roehren, Maschinisten, etc.)
177 $this->newWidth = (float) $this->currPart->getWidth() * $this->resizeFactorArray['width'];
178 $this->newHeight = (float) $this->currPart->getHeight() * $this->resizeFactorArray['height'];
179 $this->newLength = (float) $this->currPart->getLength() * $this->resizeFactorArray['length'];
181 // Passt dies nun immer noch?
182 if ($this->isNewSizeValid()) {
183 // Das passt auch, dann Werte setzen und Motor-Instanz merken
184 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> passt in das Schiff <strong>%s</strong> hinein.<br />\n",
186 $this->getPartDescr(),
187 $this->currShip->getShipName()
189 $this->setWidth($this->newWidth);
190 $this->setHeight($this->newHeight);
191 $this->setLength($this->newLength);
193 // Einige Dinge entfernen...
194 $this->removeAllNewAttr();
196 // Passt nicht! Also wieder Exception werfen...
197 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> vom Typ <strong>%s</strong> ist zu gross für das Schiff!",
198 $this->currPart->__toString(),
199 $this->currPart->getPartDescr(),
200 $this->currPart->__toString()
203 } elseif ($this->currPart != null) {
204 // Aktuelle Masse setzen
205 $this->setWidth($this->currPart->getWidth());
206 $this->setHeight($this->currPart->getHeight());
207 $this->setLength($this->currPart->getLength());
210 // Existiert ein Schiffsteil?
211 if (!is_null($this->currPart)) {
212 // Debug-Meldung ausgeben
213 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Schiffsteil <strong>%s</strong> gefunden.<br />\n",
214 $this->currPart->realClass,
215 $this->currPart->getPartDescr()
218 // Schiffsteil-Instanz setzen
219 $this->setPartInstance($this->currPart);
221 // Instanzen entfernen
222 $this->currPart->removeCurrShip();
223 $this->currPart->removeCurrPart();
224 $this->currPart->removePartInstance();
225 $this->currPart->removeResizeFactorArray();
229 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <u>%s</u> vom Typ <u>%s</u> passt nicht in das Schiff!",
230 $this->currPart->realClass,
231 $this->currPart->getPartDescr(),
232 $this->currPart->__toString()
236 // Nochmals etwas aufraeumen
237 $this->removeResizeFactorArray();
238 $this->removeCurrShip();
239 $this->removeCurrPart();
242 // Array fuer Umrechnungstabelle entfernen
243 public function removeResizeFactorArray () {
244 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] removeResizeFactor erreicht.<br />\n",
247 unset($this->resizeFactorArray);
250 // Alle newXXX-Attribute entfernen
251 public function removeAllNewAttr () {
252 unset($this->newWidth);
253 unset($this->newHeight);
254 unset($this->newLength);
257 // Aktuelle Schiff-Instanz entfernen
258 public function removeCurrShip () {
259 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] removeCurrShip erreicht.<br />\n",
262 unset($this->currShip);
265 // Aktuelle Schiff-Instanz entfernen
266 public function removeCurrPart () {
267 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] removeCurrPart erreicht.<br />\n",
270 unset($this->currPart);
274 public function removeWidth () {
275 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Breite entfernt.<br />\n",
282 public function removeHeight () {
283 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Höhe entfernt.<br />\n",
286 unset($this->height);
290 public function removeLength () {
291 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Länge entfernt.<br />\n",
294 unset($this->length);
297 // Tiefgang entfernen
298 public function removeDraught () {
299 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Tiefgang entfernt.<br />\n",
302 unset($this->draught);
305 // Getter-Methode fuer Element aus resizeFactor
306 public function getResizeFactorElement ($el) {
307 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] getResizeFactorElement erreicht. (element=%s)<br />\n",
311 if (isset($this->resizeFactorArray[$el])) {
313 return $this->resizeFactorArray[$el];
315 // Element nicht gefunden!
320 // Setter-Methode fuer Element in resizeFactor
321 public function setResizeFactorElement ($el, $value) {
322 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Umberechnungsfaktor <strong>%s</strong>=<strong>%s</strong> gesetzt.<br />\n",
327 $this->resizeFactorArray[$el] = (float) $value;
330 // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
331 public function isShipPartSizeValid () {
332 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] isShipPartSizeValid erreicht.<br />\n",
337 ( // Already defined ship messurings
338 ($this->currPart->getWidth() < $this->currShip->getWidth())
339 && ($this->currPart->getHeight() < $this->currShip->getDraught())
340 && ($this->currPart->getLength() < $this->currShip->getLength())
341 ) || ( // Ship messurings shall be calculated
342 ($this->currShip->getWidth() == 0)
343 && ($this->currShip->getHeight() == 0)
344 && ($this->currShip->getLength() == 0)
346 // The inserted part must be messured!
347 ) && ($this->currPart->getWidth() > 0)
348 && ($this->currPart->getHeight() > 0)
349 && ($this->currPart->getLength() > 0)
353 // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
354 public function isNewSizeValid () {
355 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] isNewSizeValid erreicht.<br />\n",
359 ( // Already defined ship messurings
360 ($this->newWidth < $this->currShip->getWidth())
361 && ($this->newHeight < $this->currShip->getDraught())
362 && ($this->newLength < $this->currShip->getLength())
363 ) || ( // Ship messurings shall be calculated
364 ($this->currShip->getWidth() == 0)
365 && ($this->currShip->getHeight() == 0)
366 && ($this->currShip->getLength() == 0)
372 public function extractDimensions ($dim) {
373 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] extractDimensions erreicht für <strong>%s</strong>.<br />\n",
375 $this->getPartDescr()
379 if ((isset($dim)) && (is_array($dim)) && (count($dim) == 3)) {
380 // Abmasse aus Array holen
381 $this->setWidth($dim[0]);
382 $this->setHeight($dim[1]);
383 $this->setLength($dim[2]);
385 // Nicht gefundene Abmasse!
386 throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);