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