Method constructor() removed, several small fixes
[shipsimu.git] / application / ship-simu / main / ships / class_BaseShip.php
1 <?php
2 /**
3  * A general ship class for all other kinds of ships even small sail ships
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 BaseShip extends BaseSimulator {
25         // Name des Shipes
26         private $shipName   = "Unbekanntes Schiff";
27
28         // Anzahl Anker
29         private $numAnchor  = 0;
30
31         // Tiefgang in Meter
32         private $draught    = 0;
33
34         // Besatzung-Objekte
35         private $crewList   = null;
36
37         // Aufbauten-Objekte
38         private $structures = null;
39
40         // Namenloses Ship generieren
41         protected function __construct($class) {
42                 // Call parent constructor
43                 parent::__construct($class);
44
45                 // Beim Schiff angelangt
46                 if (((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT')))
47                         $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
48                                 __CLASS__,
49                                 __LINE__
50                         ));
51
52                 // Bezeichnung setzen
53                 $this->setObjectDescription("Schiff");
54
55                 // Array-Objekt generieren
56                 $this->createStructuresArray();
57
58                 // Instanz entfernen
59                 $this->removePartInstance();
60                 $this->removeNumberFormaters();
61         }
62
63         // Array-Objekt anlegen
64         private function createStructuresArray () {
65                 $this->structures = new FrameworkArrayObject();
66         }
67
68         // Schiffsteil generieren (kann alles sein)
69         // buildInstance = Das was in das Schiffsteil evtl. eingebaut werden soll (null = kein besonderes Teil einbauen!)
70         // partClass = Das zu konstruierende Schiffsteil
71         public function createShipPart (ConstructableShipPart $buildInstance, $partClass) {
72                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> erh&auml;lt ein neues Schiffsteil (%s).<br />\n",
73                         __CLASS__,
74                         __LINE__,
75                         $this->getShipName(),
76                         $partClass
77                 ));
78
79                 // Ist die gewuenschte Klasse vorhanden?
80                 if (class_exists($partClass)) {
81                         // Befehl zusammenbauen
82                         $eval = sprintf("\$partInstance = %s::create%s();",
83                                 $partClass, $partClass
84                         );
85
86                         // Debug-Meldung ausgeben
87                         if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
88                                 __CLASS__,
89                                 __LINE__,
90                                 htmlentities($eval)
91                         ));
92
93                         // ... und ausfuehren
94                         eval($eval);
95                 } else {
96                         // Nicht vorhanden, dann Ausnahme werfen!
97                         throw new ClassNotFoundException($partClass, 0);
98                 }
99
100                 // Das Einbauen versuchen...
101                 try {
102                         $partInstance->addShipPartToShip($this, $buildInstance);
103                 } catch (MotorShipMismatchException $e) {
104                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keinen Motor erhalten! Grund: <strong>%s</strong><br />\n",
105                                 __CLASS__,
106                                 __LINE__,
107                                 $this->getShipName(),
108                                 $e->getMessage()
109                         ));
110                         return false;
111                 } catch (RoomShipMismatchException $e) {
112                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keinen Maschinenraum erhalten! Grund: <strong>%s</strong><br />\n",
113                                 __CLASS__,
114                                 __LINE__,
115                                 $this->getShipName(),
116                                 $e->getMessage()
117                         ));
118                         return false;
119
120                 } catch (StructureShipMismatchException $e) {
121                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keine Aufbauten erhalten! Grund: <strong>%s</strong><br />\n",
122                                 __CLASS__,
123                                 __LINE__,
124                                 $this->getShipName(),
125                                 $e->getMessage()
126                         ));
127                         return false;
128                 } catch (CabinShipMismatchException $e) {
129                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat keine Kabine erhalten! Grund: <strong>%s</strong><br />\n",
130                                 __CLASS__,
131                                 __LINE__,
132                                 $this->getShipName(),
133                                 $e->getMessage()
134                         ));
135                         return false;
136                 } catch (DeckShipMismatchException $e) {
137                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat kein Deck erhalten! Grund: <strong>%s</strong><br />\n",
138                                 __CLASS__,
139                                 __LINE__,
140                                 $this->getShipName(),
141                                 $e->getMessage()
142                         ));
143                         return false;
144                 } catch (ExceptionNotChangedException $e) {
145                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Eine Exception wurde nicht ge&auml;ndert. Details: <strong>%s</strong><br />\n",
146                                 __CLASS__,
147                                 __LINE__,
148                                 $e->getMessage()
149                         ));
150                         return false;
151                 } catch (ExceptionNotFoundException $e) {
152                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Eine Exception wurde nicht gefunden. Details: <strong>%s</strong><br />\n",
153                                 __CLASS__,
154                                 __LINE__,
155                                 $e->getMessage()
156                         ));
157                         return false;
158                 }
159
160                 // Instanz im Aufbauten-Array vermerken
161                 $this->structures->append($partInstance);
162
163                 // Debug-Meldung ausgeben
164                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> hat das Schiffsteil <strong>%s</strong> eingebaut bekommen.<br />\n",
165                         __CLASS__,
166                         __LINE__,
167                         $this->getShipName(),
168                         $partInstance->getObjectDescription()
169                 ));
170
171                 // Alles klar!
172                 return true;
173         }
174
175         // Getter-Methode fuer Strukturen-Array
176         public final function getStructuresArray () {
177                 return $this->structures;
178         }
179
180         // STUB: Getter-Methode Anzahl Betten
181         public function calcTotalBeds () {
182                 $this->getDebugInstance()->output("[%s:%d] Stub! Anzahl Betten erreicht.<br />\n");
183                 return 0;
184         }
185
186         // Setter-Methode fuer Schiffsnamen
187         public final function setShipName ($shipName) {
188                 // Cast the string
189                 $shipName = (string) $shipName;
190
191                 // Debug message
192                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das neue Schiff vom Typ <strong>%s</strong> wird auf den Namen <strong>%s</strong> getauft.<br />\n",
193                         __CLASS__,
194                         __LINE__,
195                         $this->__toString(),
196                         $shipName
197                 ));
198
199                 // Set ship name
200                 $this->shipName = $shipName;
201         }
202
203         // Getter-Methode fuer Schiffsnamen
204         public final function getShipName () {
205                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das <strong>%s</strong> ist auf den Namen <strong>%s</strong> getauft worden.<br />\n",
206                         __CLASS__,
207                         __LINE__,
208                         $this->__toString(),
209                         $this->shipName
210                 ));
211                 return $this->shipName;
212         }
213
214         // Setter-Methode fuer Tiefgang
215         public final function setDraught ($draught) {
216                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das <strong>%s</strong> mit dem Namen <strong>%s</strong> hat einen Tiefgang von <strong>%sm</strong>.<br />\n",
217                         __CLASS__,
218                         __LINE__,
219                         $this->__toString(),
220                         $this->shipName,
221                         $draught
222                 ));
223                 $this->draught = (int) $draught;
224         }
225
226         // Getter-Methode fuer Tiefgang
227         public final function getDraught() {
228                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Tiefgang des Schiffes <strong>%s</strong> wurde angefordert.<br />\n",
229                         __CLASS__,
230                         __LINE__,
231                         $this->shipName
232                 ));
233                 return $this->draught;
234         }
235
236         // Setter-Methode fuer Anzahl Anker
237         public final function setNumAnchor ($numAnchor) {
238                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das <strong>%s</strong> mit dem Namen <strong>%s</strong> hat <strong>%s</strong> Anker.<br />\n",
239                         __CLASS__,
240                         __LINE__,
241                         $this->__toString(),
242                         $this->shipName,
243                         $numAnchor
244                 ));
245                 $this->numAnchor = (int) $numAnchor;
246         }
247 }
248
249 // [EOF]
250 ?>