c4a548b3f8908a66198e26d4fa3ad45a021170b1
[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         private function __construct($class) {
42                 // Call parent constructor
43                 parent::constructor($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->setPartDescr("Schiff");
54
55                 // Array-Objekt generieren
56                 $this->createStructuresArray();
57
58                 // Instanz entfernen
59                 $this->removePartInstance();
60                 $this->removeNumberFormaters();
61         }
62
63         /**
64          * Calls the private constructor
65          *
66          * @param       $class  The class' name
67          * @return      void
68          */
69         public function constructor ($class) {
70                 $this->__construct($class);
71         }
72
73         // Array-Objekt anlegen
74         private function createStructuresArray () {
75                 $this->structures = new FrameworkArrayObject();
76         }
77
78         // Schiffsteil generieren (kann alles sein)
79         // buildInstance = Das was in das Schiffsteil evtl. eingebaut werden soll (null = kein besonderes Teil einbauen!)
80         // partClass = Das zu konstruierende Schiffsteil
81         public function createShipPart (ConstructableShipPart $buildInstance, $partClass) {
82                 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",
83                         __CLASS__,
84                         __LINE__,
85                         $this->getShipName(),
86                         $partClass
87                 ));
88
89                 // Ist die gewuenschte Klasse vorhanden?
90                 if (class_exists($partClass)) {
91                         // Befehl zusammenbauen
92                         $eval = sprintf("\$partInstance = %s::create%s();",
93                                 $partClass, $partClass
94                         );
95
96                         // Debug-Meldung ausgeben
97                         if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
98                                 __CLASS__,
99                                 __LINE__,
100                                 htmlentities($eval)
101                         ));
102
103                         // ... und ausfuehren
104                         eval($eval);
105                 } else {
106                         // Nicht vorhanden, dann Ausnahme werfen!
107                         throw new ClassNotFoundException($partClass, 0);
108                 }
109
110                 // Das Einbauen versuchen...
111                 try {
112                         $partInstance->addShipPartToShip($this, $buildInstance);
113                 } catch (MotorShipMismatchException $e) {
114                         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",
115                                 __CLASS__,
116                                 __LINE__,
117                                 $this->getShipName(),
118                                 $e->getMessage()
119                         ));
120                         return false;
121                 } catch (RoomShipMismatchException $e) {
122                         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",
123                                 __CLASS__,
124                                 __LINE__,
125                                 $this->getShipName(),
126                                 $e->getMessage()
127                         ));
128                         return false;
129
130                 } catch (StructureShipMismatchException $e) {
131                         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",
132                                 __CLASS__,
133                                 __LINE__,
134                                 $this->getShipName(),
135                                 $e->getMessage()
136                         ));
137                         return false;
138                 } catch (CabinShipMismatchException $e) {
139                         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",
140                                 __CLASS__,
141                                 __LINE__,
142                                 $this->getShipName(),
143                                 $e->getMessage()
144                         ));
145                         return false;
146                 } catch (DeckShipMismatchException $e) {
147                         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",
148                                 __CLASS__,
149                                 __LINE__,
150                                 $this->getShipName(),
151                                 $e->getMessage()
152                         ));
153                         return false;
154                 } catch (ExceptionNotChangedException $e) {
155                         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",
156                                 __CLASS__,
157                                 __LINE__,
158                                 $e->getMessage()
159                         ));
160                         return false;
161                 } catch (ExceptionNotFoundException $e) {
162                         if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Eine Exception wurde nicht gefunden. Details: <strong>%s</strong><br />\n",
163                                 __CLASS__,
164                                 __LINE__,
165                                 $e->getMessage()
166                         ));
167                         return false;
168                 }
169
170                 // Instanz im Aufbauten-Array vermerken
171                 $this->structures->append($partInstance);
172
173                 // Debug-Meldung ausgeben
174                 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",
175                         __CLASS__,
176                         __LINE__,
177                         $this->getShipName(),
178                         $partInstance->getPartDescr()
179                 ));
180
181                 // Alles klar!
182                 return true;
183         }
184
185         // Getter-Methode fuer Strukturen-Array
186         public final function getStructuresArray () {
187                 return $this->structures;
188         }
189
190         // STUB: Getter-Methode Anzahl Betten
191         public function calcTotalBeds () {
192                 $this->getDebugInstance()->output("[%s:%d] Stub! Anzahl Betten erreicht.<br />\n");
193                 return 0;
194         }
195
196         // Setter-Methode fuer Schiffsnamen
197         public final function setShipName ($shipName) {
198                 // Cast the string
199                 $shipName = (string) $shipName;
200
201                 // Debug message
202                 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",
203                         __CLASS__,
204                         __LINE__,
205                         $this->__toString(),
206                         $shipName
207                 ));
208
209                 // Set ship name
210                 $this->shipName = $shipName;
211         }
212
213         // Getter-Methode fuer Schiffsnamen
214         public final function getShipName () {
215                 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",
216                         __CLASS__,
217                         __LINE__,
218                         $this->__toString(),
219                         $this->shipName
220                 ));
221                 return $this->shipName;
222         }
223
224         // Setter-Methode fuer Tiefgang
225         public final function setDraught ($draught) {
226                 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",
227                         __CLASS__,
228                         __LINE__,
229                         $this->__toString(),
230                         $this->shipName,
231                         $draught
232                 ));
233                 $this->draught = (int) $draught;
234         }
235
236         // Getter-Methode fuer Tiefgang
237         public final function getDraught() {
238                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Tiefgang des Schiffes <strong>%s</strong> wurde angefordert.<br />\n",
239                         __CLASS__,
240                         __LINE__,
241                         $this->shipName
242                 ));
243                 return $this->draught;
244         }
245
246         // Setter-Methode fuer Anzahl Anker
247         public final function setNumAnchor ($numAnchor) {
248                 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",
249                         __CLASS__,
250                         __LINE__,
251                         $this->__toString(),
252                         $this->shipName,
253                         $numAnchor
254                 ));
255                 $this->numAnchor = (int) $numAnchor;
256         }
257 }
258
259 // [EOF]
260 ?>