Type-hints fixed, header docs fixed, exceptions deprecated
[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($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
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("FakedShipStructures");
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                         // Get an instance back from our object factory
75                         $partInstance = ObjectFactory::createObjectByName($partClass);
76                 } else {
77                         // Nicht vorhanden, dann Ausnahme werfen!
78                         throw new ClassNotFoundException($partClass, self::EXCEPTION_CLASS_NOT_FOUND);
79                 }
80
81                 // Das Einbauen versuchen...
82                 try {
83                         $partInstance->addShipPartToShip($this, $buildInstance);
84                 } catch (MotorShipMismatchException $e) {
85                         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",
86                                 __CLASS__,
87                                 __LINE__,
88                                 $this->getShipName(),
89                                 $e->getMessage()
90                         ));
91                         return false;
92                 } catch (RoomShipMismatchException $e) {
93                         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",
94                                 __CLASS__,
95                                 __LINE__,
96                                 $this->getShipName(),
97                                 $e->getMessage()
98                         ));
99                         return false;
100
101                 } catch (StructureShipMismatchException $e) {
102                         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",
103                                 __CLASS__,
104                                 __LINE__,
105                                 $this->getShipName(),
106                                 $e->getMessage()
107                         ));
108                         return false;
109                 } catch (CabinShipMismatchException $e) {
110                         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",
111                                 __CLASS__,
112                                 __LINE__,
113                                 $this->getShipName(),
114                                 $e->getMessage()
115                         ));
116                         return false;
117                 } catch (DeckShipMismatchException $e) {
118                         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",
119                                 __CLASS__,
120                                 __LINE__,
121                                 $this->getShipName(),
122                                 $e->getMessage()
123                         ));
124                         return false;
125                 }
126
127                 // Instanz im Aufbauten-Array vermerken
128                 $this->structures->append($partInstance);
129
130                 // Debug-Meldung ausgeben
131                 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",
132                         __CLASS__,
133                         __LINE__,
134                         $this->getShipName(),
135                         $partInstance->getObjectDescription()
136                 ));
137
138                 // Alles klar!
139                 return true;
140         }
141
142         // Getter-Methode fuer Strukturen-Array
143         public final function getStructuresArray () {
144                 return $this->structures;
145         }
146
147         // STUB: Getter-Methode Anzahl Betten
148         public function calcTotalBeds () {
149                 $this->getDebugInstance()->output("[%s:%d] Stub! Anzahl Betten erreicht.<br />\n");
150                 return 0;
151         }
152
153         // Setter-Methode fuer Schiffsnamen
154         public final function setShipName ($shipName) {
155                 // Cast the string
156                 $shipName = (string) $shipName;
157
158                 // Debug message
159                 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",
160                         __CLASS__,
161                         __LINE__,
162                         $this->__toString(),
163                         $shipName
164                 ));
165
166                 // Set ship name
167                 $this->shipName = $shipName;
168         }
169
170         // Getter-Methode fuer Schiffsnamen
171         public final function getShipName () {
172                 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",
173                         __CLASS__,
174                         __LINE__,
175                         $this->__toString(),
176                         $this->shipName
177                 ));
178                 return $this->shipName;
179         }
180
181         // Setter-Methode fuer Tiefgang
182         public final function setDraught ($draught) {
183                 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",
184                         __CLASS__,
185                         __LINE__,
186                         $this->__toString(),
187                         $this->shipName,
188                         $draught
189                 ));
190                 $this->draught = (int) $draught;
191         }
192
193         // Getter-Methode fuer Tiefgang
194         public final function getDraught() {
195                 if ((defined('DEBUG_SHIP')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Der Tiefgang des Schiffes <strong>%s</strong> wurde angefordert.<br />\n",
196                         __CLASS__,
197                         __LINE__,
198                         $this->shipName
199                 ));
200                 return $this->draught;
201         }
202
203         // Setter-Methode fuer Anzahl Anker
204         public final function setNumAnchor ($numAnchor) {
205                 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",
206                         __CLASS__,
207                         __LINE__,
208                         $this->__toString(),
209                         $this->shipName,
210                         $numAnchor
211                 ));
212                 $this->numAnchor = (int) $numAnchor;
213         }
214 }
215
216 // [EOF]
217 ?>