3 * The general simulator class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BaseSimulator extends BaseFrameworkSystem {
26 private $partInstance = null;
28 // Abmasse (Breite/Hoehe/Laenge)
33 // Aktuelles Schiff und Schiffsteil
34 private $currShip = null;
35 private $currPart = null;
37 // Faktoren zur Erweiterung der Masse. Beispielsweise soll der Maschinenraum groesser wie der Motor sein
38 private $resizeFactorArray = array(
45 protected function __construct ($className) {
46 // Call highest constructor
47 parent::__construct($className);
49 // Clean up a little, dies sollte ganz zum Schluss erfolgen!
50 $this->removeResizeFactorArray();
51 $this->removeCurrPart();
52 $this->removeCurrShip();
55 // Setter-Methode fuer Laenge
56 public final function setLength ($length) {
57 $this->length = (float) $length;
60 // Setter-Methode fuer Breite
61 public final function setWidth ($width) {
62 $this->width = (float) $width;
65 // Setter-Methode fuer Hoehe
66 public final function setHeight ($height) {
67 $this->height = (float) $height;
70 // Getter-Methode fuer Laenge
71 public final function getLength () {
75 // Getter-Methode fuer Breite
76 public final function getWidth () {
80 // Getter-Methode fuer Hoehe
81 public final function getHeight () {
85 // Setter-Methode fuer Teil-Instanz
86 public final function setPartInstance (ConstructableShipPart $partInstance) {
87 $this->partInstance = $partInstance;
90 // Getter-Methode fuer Teil-Instanz
91 public final function getPartInstance () {
92 if (!isset($this->partInstance)) {
95 return $this->partInstance;
98 // Remover-Methode fuer die Teil-Instanz
99 public final function removePartInstance () {
100 unset($this->partInstance);
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)
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;
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'];
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);
133 // Einige Dinge entfernen...
134 $this->removeAllNewAttr();
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ür das Schiff!",
138 $this->getCurrPart()->__toString(),
139 $this->getCurrPart()->getObjectDescription(),
140 $this->getCurrPart()->__toString()
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());
150 // Existiert ein Schiffsteil?
151 if (!is_null($this->currPart)) {
152 // Schiffsteil-Instanz setzen
153 $this->setPartInstance($this->currPart);
155 // Instanzen entfernen
156 $this->getCurrPart()->removeCurrShip();
157 $this->getCurrPart()->removeCurrPart();
158 $this->getCurrPart()->removePartInstance();
159 $this->getCurrPart()->removeResizeFactorArray();
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()
170 // Nochmals Clean up a little
171 $this->removeResizeFactorArray();
172 $this->removeCurrShip();
173 $this->removeCurrPart();
176 // Array fuer Umrechnungstabelle entfernen
177 public final function removeResizeFactorArray () {
178 unset($this->resizeFactorArray);
182 * Remove all new*** attributes
186 public final function removeAllNewAttr () {
187 unset($this->newWidth);
188 unset($this->newHeight);
189 unset($this->newLength);
193 * Remove current ship instance
197 public final function removeCurrShip () {
198 unset($this->currShip);
201 // Aktuelle Schiffsteil-Instanz entfernen
202 public final function removeCurrPart () {
203 unset($this->currPart);
207 public final function removeWidth () {
212 public final function removeHeight () {
213 unset($this->height);
217 public final function removeLength () {
218 unset($this->length);
221 // Tiefgang entfernen
222 public final function removeDraught () {
223 unset($this->draught);
226 // Getter-Methode fuer Element aus resizeFactor
227 public final function getResizeFactorElement ($el) {
228 if (isset($this->resizeFactorArray[$el])) {
230 return $this->resizeFactorArray[$el];
232 // Element nicht gefunden!
237 // Setter-Methode fuer Element in resizeFactor
238 public final function setResizeFactorElement ($el, $value) {
239 $this->resizeFactorArray[$el] = (float) $value;
242 // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
243 public function isShipPartSizeValid () {
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)
255 // The inserted part must be messured!
256 ) && ($this->getCurrPart()->getWidth() > 0)
257 && ($this->getCurrPart()->getHeight() > 0)
258 && ($this->getCurrPart()->getLength() > 0)
262 // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
263 public function isNewSizeValid () {
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)
278 public function extractDimensions ($dim) {
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]);
286 // Nicht gefundene Abmasse!
287 throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
292 * Getter for current part instance
294 * @return $currPart Instance of the current ship part object
296 public final function getCurrPart () {
297 return $this->currPart;