3 * A database wrapper for city sections. Sections are the smalles entity of a
4 * city. They can be connected with each other and form a lot. Therefore only
5 * sections of same type (and sub type) can be linked combined, else you will
6 * something really strange, a residential building with some industry parts
7 * for example is not possible in real world.
9 * Sure you can have a small shop (commercial) on ground level and some
10 * residentials on higher levels. This city simulation does support such
11 * creations as the levels (Z axis) is different.
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2015 City Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class CitySectionsDatabaseWrapper extends BaseDatabaseWrapper implements CitySectionsWrapper, Registerable {
33 // Constants for database table names
34 const DB_TABLE_CITY_SECTIONS = 'city_sections';
36 // Section id, an referenced city id and lot id
37 const DB_COLUMN_SECTION_ID = 'city_section_id';
38 const DB_COLUMN_CITY_ID = 'city_id';
41 * Lot id, the lot id is only set if the player has "aquired" the sections
42 * and has created a lot based on these sections.
44 const DB_COLUMN_LOT_ID = 'lot_id';
46 // Section and sub type (e.g. residential, hut)
47 const DB_COLUMN_SECTION_TYPE = 'section_type';
48 const DB_COLUMN_SECTION_SUB_TYPE = 'section_sub_type';
51 const DB_COLUMN_SECTION_POSITION_X = 'section_position_x';
52 const DB_COLUMN_SECTION_POSITION_Y = 'section_position_y';
53 const DB_COLUMN_SECTION_POSITION_Z = 'section_position_z';
55 // Connected neigbouring sections
56 const DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID = 'section_neighbour_west_id';
57 const DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID = 'section_neighbour_east_id';
58 const DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID = 'section_neighbour_north_id';
59 const DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID = 'section_neighbour_south_id';
60 const DB_COLUMN_SECTION_NEIGHBOUR_UP_ID = 'section_neighbour_up_id';
61 const DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID = 'section_neighbour_down_id';
64 // Reserved section (see documentation)
65 const DB_COLUMN_SECTION_RESERVED = 'section_reserved';
68 // @TODO "water" is not yet supported and may end up in a very random land.
69 const SECTION_TYPE_WATER = 'water';
70 const SECTION_TYPE_EMPTY_LAND = 'land';
73 // @TODO All types of water are not supported yet.
74 const SUB_SECTION_TYPE_EMPTY = 'empty';
77 const IS_NOT_RESERVED = 0;
78 const IS_RESERVED = 1;
81 * Protected constructor
85 protected function __construct () {
86 // Call parent constructor
87 parent::__construct(__CLASS__);
91 * Creates an instance of this database wrapper by a provided user class
93 * @return $wrapperInstance An instance of the created wrapper class
95 public static final function createCitySectionsDatabaseWrapper () {
97 $wrapperInstance = new CitySectionsDatabaseWrapper();
99 // Set (primary!) table name
100 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
102 // Return the instance
103 return $wrapperInstance;
107 * Checks if the given city id is found in sections table
109 * @param $cityId City id to check
110 * @return $isFound Whether the city id is found
112 public function ifCityHasSections ($cityId) {
113 // Get search instance
114 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
116 // Search for 'city_id'
117 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
120 * Only one entry is enough to find, else this query could run very
121 * long on large maps.
123 $searchInstance->setLimit(1);
125 // Execute it on database instance
126 $resultInstance = $this->doSelectByCriteria($searchInstance);
128 // Check if there is one entry
129 $isFound = ($resultInstance->next() == 1);
136 * Expands the sections table with initial data for given city id
138 * @param $cityId City id to check
139 * @return $ids Sections ids from initial expansion
140 * @todo Add handling of water types to make a more cooler map
142 public function doInitialCityExpansion ($cityId) {
143 // Make sure this city has no sections
144 assert(!$this->ifCityHasSections($cityId));
147 * "Cache" max "radius" for initial city expansion. It is not a real
148 * radius but more a max expansion area (expand +- half of "radius"
149 * from 0/0). The "zero point" is always calculated in.
151 * This gives a maxium initial area calculated as follows:
153 * totalInitialSections = (radius + 1) * (radius + 1)
155 $radius = $this->getConfigInstance()->getConfigEntry('city_max_initial_xy_expansion_radius');
157 // Max up and down ...
158 $maxUp = $this->getConfigInstance()->getConfigEntry('city_max_initial_up_expansion');
159 $maxDown = $this->getConfigInstance()->getConfigEntry('city_max_initial_down_expansion');
161 // Extremely huge debug message:
162 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown);
164 // Get data set instance
165 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
167 // Add values for "zero point"
168 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_ID , 1);
169 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , $cityId);
170 $dataSetInstance->addCriteria(self::DB_COLUMN_LOT_ID , 0);
171 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_TYPE , self::SECTION_TYPE_EMPTY_LAND);
172 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_SUB_TYPE , self::SUB_SECTION_TYPE_EMPTY);
173 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_X , 0);
174 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_Y , 0);
175 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_Z , 0);
176 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID, 0);
177 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID, 0);
178 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID , 0);
179 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID , 0);
180 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_UP_ID , 0);
181 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID , 0);
182 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_RESERVED , self::IS_NOT_RESERVED);
184 // Set primary key to 'city_id'/'section_id'
185 $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
188 $this->queryInsertDataSet($dataSetInstance);
190 // Set section id to 2 as 1 is already initialized + init array
193 // Output message to ask for user's patience ...
194 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Writing ' . (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1)) . ' sections for city ' . $cityId . ' ... (this may takes some time)');
196 // Expand half of it to north/south (north=positive, south=negative)
197 for ($north = 1; $north < round($radius / 2); $north++) {
198 // Expand half of it to west/east (west=positive, east=negative)
199 for ($west = 1; $west < round($radius / 2); $west++) {
200 // Expand up/down (including "zero point")
201 for ($z = $maxDown; $z < ($maxUp + 1); $z++) {
203 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
205 // Fill array up with south/east/down ids
206 $sections[($north * -1)][($west * -1)][$z]['type'] = self::SECTION_TYPE_EMPTY_LAND;
207 $sections[($north * -1)][($west * -1)][$z]['sub'] = self::SUB_SECTION_TYPE_EMPTY;
209 // Fill up array with north/west/up ids
210 $sections[$north][$west][$z]['type'] = self::SECTION_TYPE_EMPTY_LAND;
211 $sections[$north][$west][$z]['sub'] = self::SUB_SECTION_TYPE_EMPTY;
216 // Init section id with 2 as 1 is the "zero point"
219 // Loop through whole array again for writing it to database: north/south
220 foreach ($sections as $x => $sectionX) {
221 // Loop through west/east values
222 foreach ($sectionX as $y => $sectionY) {
223 // Loop through up/down values
224 foreach ($sectionY as $z => $sectionData) {
226 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
228 // Set all coordinates for positive directions
229 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_ID , $sectionId);
230 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_X, $x);
231 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Y, $y);
232 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Z, $z);
233 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_TYPE , $sectionData['type']);
234 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_SUB_TYPE , $sectionData['sub']);
236 // Add value to database
237 $this->queryInsertDataSet($dataSetInstance);
244 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
248 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
251 // Thank you for waiting! :-)
252 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ' . (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1)) . ' sections has been written for city ' . $cityId . '.');