Copyright updated, menu class added for 'home'
[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, 2009 Ship-Simu Developer Team
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                 // Clean up a little, dies sollte ganz zum Schluss erfolgen!
50                 $this->removeSystemArray();
51                 $this->removeNumberFormaters();
52                 $this->removeResizeFactorArray();
53                 $this->removeCurrPart();
54                 $this->removeCurrShip();
55         }
56
57         // Setter-Methode fuer Laenge
58         public final function setLength ($length) {
59                 $this->length = (float) $length;
60         }
61
62         // Setter-Methode fuer Breite
63         public final function setWidth ($width) {
64                 $this->width = (float) $width;
65         }
66
67         // Setter-Methode fuer Hoehe
68         public final function setHeight ($height) {
69                 $this->height = (float) $height;
70         }
71
72         // Getter-Methode fuer Laenge
73         public final function getLength () {
74                 return $this->length;
75         }
76
77         // Getter-Methode fuer Breite
78         public final function getWidth () {
79                 return $this->width;
80         }
81
82         // Getter-Methode fuer Hoehe
83         public final function getHeight () {
84                 return $this->height;
85         }
86
87         // Setter-Methode fuer Teil-Instanz
88         public final function setPartInstance (ConstructableShipPart $partInstance) {
89                 $this->partInstance = $partInstance;
90         }
91
92         // Getter-Methode fuer Teil-Instanz
93         public final function getPartInstance () {
94                 if (!isset($this->partInstance)) {
95                         return null;
96                 }
97                 return $this->partInstance;
98         }
99
100         // Remover-Methode fuer die Teil-Instanz
101         public final function removePartInstance () {
102                 unset($this->partInstance);
103         }
104
105         // Prueft ob all Umberechnungsfaktoren gesetzt sind
106         private function isResizeFactorValid () {
107                 return (($this->getResizeFactorElement('width')  > 1)
108                         || ($this->getResizeFactorElement('height') > 1)
109                         || ($this->getResizeFactorElement('length') > 1)
110                 );
111         }
112
113         // Baut einen Motor in das Schiff ein
114         public function addShipPartToShip (ConstructableShip $shipInstance, ConstructableShipPart $partInstance) {
115                 // Schiff/-steil merken
116                 $this->currShip = $shipInstance;
117                 $this->currPart = $partInstance;
118
119                 // Passt ueberhaupt das Schiffsteil in's Schiff?
120                 if ($this->isShipPartSizeValid()) {
121                         // Muessen die Masse angepasst werden?
122                         if ($this->isResizeFactorValid()) {
123                                 // Neue Angaben berechnen (wir lassen etwas Lust fuer Kabelbaeume, Roehren, Maschinisten, etc.)
124                                 $this->newWidth  = (float) $this->getCurrPart()->getWidth()  * $this->resizeFactorArray['width'];
125                                 $this->newHeight = (float) $this->getCurrPart()->getHeight() * $this->resizeFactorArray['height'];
126                                 $this->newLength = (float) $this->getCurrPart()->getLength() * $this->resizeFactorArray['length'];
127
128                                 // Passt dies nun immer noch?
129                                 if ($this->isNewSizeValid()) {
130                                         // Das passt auch, dann Werte setzen und Motor-Instanz merken
131                                         $this->setWidth($this->newWidth);
132                                         $this->setHeight($this->newHeight);
133                                         $this->setLength($this->newLength);
134
135                                         // Einige Dinge entfernen...
136                                         $this->removeAllNewAttr();
137                                 } else {
138                                         // Passt nicht! Also wieder Exception werfen...
139                                         throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> vom Typ <strong>%s</strong> ist zu gross f&uuml;r das Schiff!",
140                                                 $this->getCurrPart()->__toString(),
141                                                 $this->getCurrPart()->getObjectDescription(),
142                                                 $this->getCurrPart()->__toString()
143                                         ), 2);
144                                 }
145                         } elseif ($this->currPart != null) {
146                                 // Aktuelle Masse setzen
147                                 $this->setWidth($this->getCurrPart()->getWidth());
148                                 $this->setHeight($this->getCurrPart()->getHeight());
149                                 $this->setLength($this->getCurrPart()->getLength());
150                         }
151
152                         // Existiert ein Schiffsteil?
153                         if (!is_null($this->currPart)) {
154                                 // Schiffsteil-Instanz setzen
155                                 $this->setPartInstance($this->currPart);
156
157                                 // Instanzen entfernen
158                                 $this->getCurrPart()->removeCurrShip();
159                                 $this->getCurrPart()->removeCurrPart();
160                                 $this->getCurrPart()->removePartInstance();
161                                 $this->getCurrPart()->removeResizeFactorArray();
162                         }
163                 } else {
164                         // Exception werfen!
165                         throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <u>%s</u> vom Typ <u>%s</u> passt nicht in das Schiff!",
166                                 $this->getCurrPart()->realClass,
167                                 $this->getCurrPart()->getObjectDescription(),
168                                 $this->getCurrPart()->__toString()
169                         ), 1);
170                 }
171
172                 // Nochmals Clean up a little
173                 $this->removeResizeFactorArray();
174                 $this->removeCurrShip();
175                 $this->removeCurrPart();
176         }
177
178         // Array fuer Umrechnungstabelle entfernen
179         public final function removeResizeFactorArray () {
180                 unset($this->resizeFactorArray);
181         }
182
183         /**
184          * Remove all new*** attributes
185          *
186          * @return      void
187          */
188         public final function removeAllNewAttr () {
189                 unset($this->newWidth);
190                 unset($this->newHeight);
191                 unset($this->newLength);
192         }
193
194         /**
195          * Remove current ship instance
196          *
197          * @return      void
198          */
199         public final function removeCurrShip () {
200                 unset($this->currShip);
201         }
202
203         // Aktuelle Schiffsteil-Instanz entfernen
204         public final function removeCurrPart () {
205                 unset($this->currPart);
206         }
207
208         // Breite entfernen
209         public final function removeWidth () {
210                 unset($this->width);
211         }
212
213         // Hoehe entfernen
214         public final function removeHeight () {
215                 unset($this->height);
216         }
217
218         // Laenge entfernen
219         public final function removeLength () {
220                 unset($this->length);
221         }
222
223         // Tiefgang entfernen
224         public final function removeDraught () {
225                 unset($this->draught);
226         }
227
228         // Getter-Methode fuer Element aus resizeFactor
229         public final function getResizeFactorElement ($el) {
230                 if (isset($this->resizeFactorArray[$el])) {
231                         // Element gefunden
232                         return $this->resizeFactorArray[$el];
233                 } else {
234                         // Element nicht gefunden!
235                         return null;
236                 }
237         }
238
239         // Setter-Methode fuer Element in resizeFactor
240         public final function setResizeFactorElement ($el, $value) {
241                 $this->resizeFactorArray[$el] = (float) $value;
242         }
243
244         // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
245         public function isShipPartSizeValid () {
246                 return (
247                         (
248                                 ( // Already defined ship messurings
249                                                  ($this->getCurrPart()->getWidth()  < $this->currShip->getWidth())
250                                         && ($this->getCurrPart()->getHeight() < $this->currShip->getDraught())
251                                         && ($this->getCurrPart()->getLength() < $this->currShip->getLength())
252                                 ) || ( // Ship messurings shall be calculated
253                                                  ($this->currShip->getWidth()  == 0)
254                                         && ($this->currShip->getHeight() == 0)
255                                         && ($this->currShip->getLength() == 0)
256                                 )
257                         // The inserted part must be messured!
258                         ) && ($this->getCurrPart()->getWidth()  > 0)
259                                 && ($this->getCurrPart()->getHeight() > 0)
260                                 && ($this->getCurrPart()->getLength() > 0)
261                 );
262         }
263
264         // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
265         public function isNewSizeValid () {
266                 return (
267                         ( // Already defined ship messurings
268                                          ($this->newWidth  < $this->currShip->getWidth())
269                                 && ($this->newHeight < $this->currShip->getDraught())
270                                 && ($this->newLength < $this->currShip->getLength())
271                         ) || ( // Ship messurings shall be calculated
272                                          ($this->currShip->getWidth()  == 0)
273                                 && ($this->currShip->getHeight() == 0)
274                                 && ($this->currShip->getLength() == 0)
275                         )
276                 );
277         }
278
279         // Masse extrahieren
280         public function extractDimensions ($dim) {
281                 // Abmasse setzen
282                 if ((isset($dim)) && (is_array($dim)) && (count($dim) == 3)) {
283                         // Abmasse aus Array holen
284                         $this->setWidth($dim[0]);
285                         $this->setHeight($dim[1]);
286                         $this->setLength($dim[2]);
287                 } else {
288                         // Nicht gefundene Abmasse!
289                         throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
290                 }
291         }
292
293         /**
294          * Getter for current part instance
295          *
296          * @return              $currPart       Instance of the current ship part object
297          */
298         public final function getCurrPart () {
299                 return $this->currPart;
300         }
301 }
302
303 // [EOF]
304 ?>