Even more conflicting calls fixed
[shipsimu.git] / application / ship-simu / main / class_BaseSimulator.php
1 <?php
2 /**
3  * The general simulator class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseSimulator extends BaseFrameworkSystem {
25         // Schiffsteilinstanz
26         private $partInstance = null;
27
28         // Abmasse (Breite/Hoehe/Laenge)
29         private $width  = 0;
30         private $height = 0;
31         private $length = 0;
32
33         // Aktuelles Schiff und Schiffsteil
34         private $currShip = null;
35         private $currPart = null;
36
37         // Faktoren zur Erweiterung der Masse. Beispielsweise soll der Maschinenraum groesser wie der Motor sein
38         private $resizeFactorArray = array(
39                 'width'  => 1,
40                 'height' => 1,
41                 'length' => 1
42         );
43
44         // Konstruktor
45         protected function __construct ($className) {
46                 // Call highest constructor
47                 parent::__construct($className);
48
49                 // Set part description and class name
50                 $this->setObjectDescription("Simulator-Basis-Einheit");
51
52                 // Clean up a little, dies sollte ganz zum Schluss erfolgen!
53                 $this->removeResizeFactorArray();
54                 $this->removeCurrPart();
55                 $this->removeCurrShip();
56         }
57
58         // Setter-Methode fuer Laenge
59         public final function setLength ($length) {
60                 $this->length = (float) $length;
61         }
62
63         // Setter-Methode fuer Breite
64         public final function setWidth ($width) {
65                 $this->width = (float) $width;
66         }
67
68         // Setter-Methode fuer Hoehe
69         public final function setHeight ($height) {
70                 $this->height = (float) $height;
71         }
72
73         // Getter-Methode fuer Laenge
74         public final function getLength () {
75                 return $this->length;
76         }
77
78         // Getter-Methode fuer Breite
79         public final function getWidth () {
80                 return $this->width;
81         }
82
83         // Getter-Methode fuer Hoehe
84         public final function getHeight () {
85                 return $this->height;
86         }
87
88         // Setter-Methode fuer Teil-Instanz
89         public final function setPartInstance (ConstructableShipPart $partInstance) {
90                 $this->partInstance = $partInstance;
91         }
92
93         // Getter-Methode fuer Teil-Instanz
94         public final function getPartInstance () {
95                 if (!isset($this->partInstance)) {
96                         return null;
97                 }
98                 return $this->partInstance;
99         }
100
101         // Remover-Methode fuer die Teil-Instanz
102         public final function removePartInstance () {
103                 unset($this->partInstance);
104         }
105
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)
111                 );
112         }
113
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;
119
120                 if (defined('DEBUG_CORE')) $this->getDebugInstance()->output(sprintf("[%s:] Maschinenraum mit Motor <strong>%s</strong> wird fuer das Schiff <strong>%s</strong> konstruiert.",
121                         $this->__toString(),
122                         $this->getCurrPart()->getObjectDescription(),
123                         $this->currShip->getShipName()
124                 ));
125
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.",
130                                 $this->__toString(),
131                                 $this->getCurrPart()->getObjectDescription(),
132                                 $this->getCurrPart()->__toString(),
133                                 $this->currShip->getShipName()
134                         ));
135
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'];
142
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.",
147                                                 $this->__toString(),
148                                                 $this->getObjectDescription(),
149                                                 $this->currShip->getShipName()
150                                         ));
151                                         $this->setWidth($this->newWidth);
152                                         $this->setHeight($this->newHeight);
153                                         $this->setLength($this->newLength);
154
155                                         // Einige Dinge entfernen...
156                                         $this->removeAllNewAttr();
157                                 } else {
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&uuml;r das Schiff!",
160                                                 $this->getCurrPart()->__toString(),
161                                                 $this->getCurrPart()->getObjectDescription(),
162                                                 $this->getCurrPart()->__toString()
163                                         ), 2);
164                                 }
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());
170                         }
171
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()
178                                 ));
179
180                                 // Schiffsteil-Instanz setzen
181                                 $this->setPartInstance($this->currPart);
182
183                                 // Instanzen entfernen
184                                 $this->getCurrPart()->removeCurrShip();
185                                 $this->getCurrPart()->removeCurrPart();
186                                 $this->getCurrPart()->removePartInstance();
187                                 $this->getCurrPart()->removeResizeFactorArray();
188                         }
189                 } else {
190                         // Exception werfen!
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()
195                         ), 1);
196                 }
197
198                 // Nochmals Clean up a little
199                 $this->removeResizeFactorArray();
200                 $this->removeCurrShip();
201                 $this->removeCurrPart();
202         }
203
204         // Array fuer Umrechnungstabelle entfernen
205         public final function removeResizeFactorArray () {
206                 unset($this->resizeFactorArray);
207         }
208
209         /**
210          * Remove all new*** attributes
211          *
212          * @return      void
213          */
214         public final function removeAllNewAttr () {
215                 unset($this->newWidth);
216                 unset($this->newHeight);
217                 unset($this->newLength);
218         }
219
220         /**
221          * Remove current ship instance
222          *
223          * @return      void
224          */
225         public final function removeCurrShip () {
226                 unset($this->currShip);
227         }
228
229         // Aktuelle Schiffsteil-Instanz entfernen
230         public final function removeCurrPart () {
231                 unset($this->currPart);
232         }
233
234         // Breite entfernen
235         public final function removeWidth () {
236                 unset($this->width);
237         }
238
239         // Hoehe entfernen
240         public final function removeHeight () {
241                 unset($this->height);
242         }
243
244         // Laenge entfernen
245         public final function removeLength () {
246                 unset($this->length);
247         }
248
249         // Tiefgang entfernen
250         public final function removeDraught () {
251                 unset($this->draught);
252         }
253
254         // Getter-Methode fuer Element aus resizeFactor
255         public final function getResizeFactorElement ($el) {
256                 if (isset($this->resizeFactorArray[$el])) {
257                         // Element gefunden
258                         return $this->resizeFactorArray[$el];
259                 } else {
260                         // Element nicht gefunden!
261                         return null;
262                 }
263         }
264
265         // Setter-Methode fuer Element in resizeFactor
266         public final function setResizeFactorElement ($el, $value) {
267                 $this->resizeFactorArray[$el] = (float) $value;
268         }
269
270         // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
271         public function isShipPartSizeValid () {
272                 return (
273                         (
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)
282                                 )
283                         // The inserted part must be messured!
284                         ) && ($this->getCurrPart()->getWidth()  > 0)
285                                 && ($this->getCurrPart()->getHeight() > 0)
286                                 && ($this->getCurrPart()->getLength() > 0)
287                 );
288         }
289
290         // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
291         public function isNewSizeValid () {
292                 return (
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)
301                         )
302                 );
303         }
304
305         // Masse extrahieren
306         public function extractDimensions ($dim) {
307                 // Abmasse setzen
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]);
313                 } else {
314                         // Nicht gefundene Abmasse!
315                         throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
316                 }
317         }
318
319         /**
320          * Getter for current part instance
321          *
322          * @return              $currPart       Instance of the current ship part object
323          */
324         public final function getCurrPart () {
325                 return $this->currPart;
326         }
327 }
328
329 // [EOF]
330 ?>