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