3 * The general simulator class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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->removeSystemArray();
51 $this->removeNumberFormaters();
52 $this->removeResizeFactorArray();
53 $this->removeCurrPart();
54 $this->removeCurrShip();
57 // Setter-Methode fuer Laenge
58 public final function setLength ($length) {
59 $this->length = (float) $length;
62 // Setter-Methode fuer Breite
63 public final function setWidth ($width) {
64 $this->width = (float) $width;
67 // Setter-Methode fuer Hoehe
68 public final function setHeight ($height) {
69 $this->height = (float) $height;
72 // Getter-Methode fuer Laenge
73 public final function getLength () {
77 // Getter-Methode fuer Breite
78 public final function getWidth () {
82 // Getter-Methode fuer Hoehe
83 public final function getHeight () {
87 // Setter-Methode fuer Teil-Instanz
88 public final function setPartInstance (ConstructableShipPart $partInstance) {
89 $this->partInstance = $partInstance;
92 // Getter-Methode fuer Teil-Instanz
93 public final function getPartInstance () {
94 if (!isset($this->partInstance)) {
97 return $this->partInstance;
100 // Remover-Methode fuer die Teil-Instanz
101 public final function removePartInstance () {
102 unset($this->partInstance);
105 // Prueft ob all Umberechnungsfaktoren gesetzt sind
106 private function isResizeFactorValid () {
107 return (($this->getResizeFactorElement('width') > 1)
108 || ($this->getResizeFactorElement('height') > 1)
109 || ($this->getResizeFactorElement('length') > 1)
113 // Baut einen Motor in das Schiff ein
114 public function addShipPartToShip (ConstructableShip $shipInstance, ConstructableShipPart $partInstance) {
115 // Schiff/-steil merken
116 $this->currShip = $shipInstance;
117 $this->currPart = $partInstance;
119 // Passt ueberhaupt das Schiffsteil in's Schiff?
120 if ($this->isShipPartSizeValid()) {
121 // Muessen die Masse angepasst werden?
122 if ($this->isResizeFactorValid()) {
123 // Neue Angaben berechnen (wir lassen etwas Lust fuer Kabelbaeume, Roehren, Maschinisten, etc.)
124 $this->newWidth = (float) $this->getCurrPart()->getWidth() * $this->resizeFactorArray['width'];
125 $this->newHeight = (float) $this->getCurrPart()->getHeight() * $this->resizeFactorArray['height'];
126 $this->newLength = (float) $this->getCurrPart()->getLength() * $this->resizeFactorArray['length'];
128 // Passt dies nun immer noch?
129 if ($this->isNewSizeValid()) {
130 // Das passt auch, dann Werte setzen und Motor-Instanz merken
131 $this->setWidth($this->newWidth);
132 $this->setHeight($this->newHeight);
133 $this->setLength($this->newLength);
135 // Einige Dinge entfernen...
136 $this->removeAllNewAttr();
138 // Passt nicht! Also wieder Exception werfen...
139 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <strong>%s</strong> vom Typ <strong>%s</strong> ist zu gross für das Schiff!",
140 $this->getCurrPart()->__toString(),
141 $this->getCurrPart()->getObjectDescription(),
142 $this->getCurrPart()->__toString()
145 } elseif ($this->currPart != null) {
146 // Aktuelle Masse setzen
147 $this->setWidth($this->getCurrPart()->getWidth());
148 $this->setHeight($this->getCurrPart()->getHeight());
149 $this->setLength($this->getCurrPart()->getLength());
152 // Existiert ein Schiffsteil?
153 if (!is_null($this->currPart)) {
154 // Schiffsteil-Instanz setzen
155 $this->setPartInstance($this->currPart);
157 // Instanzen entfernen
158 $this->getCurrPart()->removeCurrShip();
159 $this->getCurrPart()->removeCurrPart();
160 $this->getCurrPart()->removePartInstance();
161 $this->getCurrPart()->removeResizeFactorArray();
165 throw new StructureShipMismatchException(sprintf("[%s:] Das Schiffsteil <u>%s</u> vom Typ <u>%s</u> passt nicht in das Schiff!",
166 $this->getCurrPart()->realClass,
167 $this->getCurrPart()->getObjectDescription(),
168 $this->getCurrPart()->__toString()
172 // Nochmals Clean up a little
173 $this->removeResizeFactorArray();
174 $this->removeCurrShip();
175 $this->removeCurrPart();
178 // Array fuer Umrechnungstabelle entfernen
179 public final function removeResizeFactorArray () {
180 unset($this->resizeFactorArray);
184 * Remove all new*** attributes
188 public final function removeAllNewAttr () {
189 unset($this->newWidth);
190 unset($this->newHeight);
191 unset($this->newLength);
195 * Remove current ship instance
199 public final function removeCurrShip () {
200 unset($this->currShip);
203 // Aktuelle Schiffsteil-Instanz entfernen
204 public final function removeCurrPart () {
205 unset($this->currPart);
209 public final function removeWidth () {
214 public final function removeHeight () {
215 unset($this->height);
219 public final function removeLength () {
220 unset($this->length);
223 // Tiefgang entfernen
224 public final function removeDraught () {
225 unset($this->draught);
228 // Getter-Methode fuer Element aus resizeFactor
229 public final function getResizeFactorElement ($el) {
230 if (isset($this->resizeFactorArray[$el])) {
232 return $this->resizeFactorArray[$el];
234 // Element nicht gefunden!
239 // Setter-Methode fuer Element in resizeFactor
240 public final function setResizeFactorElement ($el, $value) {
241 $this->resizeFactorArray[$el] = (float) $value;
244 // Kontrolliert, ob die Abmasse Schiffsteil->Schiff stimmen
245 public function isShipPartSizeValid () {
248 ( // Already defined ship messurings
249 ($this->getCurrPart()->getWidth() < $this->currShip->getWidth())
250 && ($this->getCurrPart()->getHeight() < $this->currShip->getDraught())
251 && ($this->getCurrPart()->getLength() < $this->currShip->getLength())
252 ) || ( // Ship messurings shall be calculated
253 ($this->currShip->getWidth() == 0)
254 && ($this->currShip->getHeight() == 0)
255 && ($this->currShip->getLength() == 0)
257 // The inserted part must be messured!
258 ) && ($this->getCurrPart()->getWidth() > 0)
259 && ($this->getCurrPart()->getHeight() > 0)
260 && ($this->getCurrPart()->getLength() > 0)
264 // Kontrolliert, ob die Abmasse Maschinenraum->Schiff stimmen
265 public function isNewSizeValid () {
267 ( // Already defined ship messurings
268 ($this->newWidth < $this->currShip->getWidth())
269 && ($this->newHeight < $this->currShip->getDraught())
270 && ($this->newLength < $this->currShip->getLength())
271 ) || ( // Ship messurings shall be calculated
272 ($this->currShip->getWidth() == 0)
273 && ($this->currShip->getHeight() == 0)
274 && ($this->currShip->getLength() == 0)
280 public function extractDimensions ($dim) {
282 if ((isset($dim)) && (is_array($dim)) && (count($dim) == 3)) {
283 // Abmasse aus Array holen
284 $this->setWidth($dim[0]);
285 $this->setHeight($dim[1]);
286 $this->setLength($dim[2]);
288 // Nicht gefundene Abmasse!
289 throw new DimNotFoundInArrayException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
294 * Getter for current part instance
296 * @return $currPart Instance of the current ship part object
298 public final function getCurrPart () {
299 return $this->currPart;