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';
71 const SECTION_TYPE_AIR = 'air';
74 // @TODO All types of water are not supported yet.
75 const SECTION_SUB_TYPE_GRASS = 'grass';
76 const SECTION_SUB_TYPE_AIR = 'air';
79 const IS_NOT_RESERVED = 0;
80 const IS_RESERVED = 1;
83 * Protected constructor
87 protected function __construct () {
88 // Call parent constructor
89 parent::__construct(__CLASS__);
93 * Creates an instance of this database wrapper by a provided user class
95 * @return $wrapperInstance An instance of the created wrapper class
97 public static final function createCitySectionsDatabaseWrapper () {
99 $wrapperInstance = new CitySectionsDatabaseWrapper();
101 // Set (primary!) table name
102 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
104 // Return the instance
105 return $wrapperInstance;
109 * Checks if the given city id is found in sections table
111 * @param $cityId City id to check
112 * @return $isFound Whether the city id is found
114 public function ifCityHasSections ($cityId) {
115 // Get search instance
116 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
118 // Search for 'city_id'
119 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
122 * Only one entry is enough to find, else this query could run very
123 * long on large maps.
125 $searchInstance->setLimit(1);
127 // Execute it on database instance
128 $resultInstance = $this->doSelectByCriteria($searchInstance);
130 // Check if there is one entry
131 $isFound = ($resultInstance->next() == 1);
138 * Expands the sections table with initial data for given city id
140 * @param $cityId City id to check
141 * @return $ids Sections ids from initial expansion
142 * @todo Add handling of water types to make a more cooler map
144 public function doInitialCityExpansion ($cityId) {
145 // Make sure this city has no sections
146 assert(!$this->ifCityHasSections($cityId));
149 * "Cache" max "radius" for initial city expansion. It is not a real
150 * radius but more a max expansion area (expand +- half of "radius"
151 * from 0/0). The "zero point" is always calculated in.
153 * This gives a maxium initial area calculated as follows:
155 * totalInitialSections = (radius + 1) * (radius + 1)
157 $radius = $this->getConfigInstance()->getConfigEntry('city_max_initial_xy_expansion_radius');
159 // Max up and down ...
160 $maxUp = $this->getConfigInstance()->getConfigEntry('city_max_initial_up_expansion');
161 $maxDown = $this->getConfigInstance()->getConfigEntry('city_max_initial_down_expansion');
163 // Extremely huge debug message:
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown);
166 // Get data set instance
167 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
169 // Add values for "zero point"
170 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_ID , 1);
171 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID , $cityId);
172 $dataSetInstance->addCriteria(self::DB_COLUMN_LOT_ID , 0);
173 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_TYPE , self::SECTION_TYPE_AIR);
174 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_SUB_TYPE , self::SECTION_SUB_TYPE_AIR);
175 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_X , 0);
176 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_Y , 0);
177 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_Z , 0);
178 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID, 0);
179 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID, 0);
180 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID , 0);
181 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID , 0);
182 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_UP_ID , 0);
183 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID , 0);
184 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_RESERVED , self::IS_NOT_RESERVED);
186 // Set primary key to 'city_id'/'section_id'
187 $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
190 $this->queryInsertDataSet($dataSetInstance);
192 // Set section id to 2 as 1 is already initialized + init array
195 // Output message to ask for user's patience ...
196 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Writing ' . (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1)) . ' sections for city ' . $cityId . ' ... (this may takes some time)');
198 // Expand half of it to north/south (north=positive, south=negative)
199 for ($north = 1; $north < round($radius / 2); $north++) {
200 // Expand half of it to west/east (west=positive, east=negative)
201 for ($west = 1; $west < round($radius / 2); $west++) {
202 // Expand up/down (including "zero point")
203 for ($z = $maxDown; $z < ($maxUp + 1); $z++) {
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
207 // Fill array up with south/east/down ids
208 $sections[($north * -1)][($west * -1)][$z]['type'] = self::SECTION_TYPE_EMPTY_LAND;
209 $sections[($north * -1)][($west * -1)][$z]['sub'] = self::SECTION_SUB_TYPE_GRASS;
211 // Fill up array with north/west/up ids (only air)
212 $sections[$north][$west][$z]['type'] = self::SECTION_TYPE_AIR;
213 $sections[$north][$west][$z]['sub'] = self::SECTION_SUB_TYPE_AIR;
218 // Init section id with 2 as 1 is the "zero point"
221 // Loop through whole array again for writing it to database: north/south
222 foreach ($sections as $x => $sectionX) {
223 // Loop through west/east values
224 foreach ($sectionX as $y => $sectionY) {
225 // Loop through up/down values
226 foreach ($sectionY as $z => $sectionData) {
228 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
230 // Set all coordinates for positive directions
231 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_ID , $sectionId);
232 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_X, $x);
233 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Y, $y);
234 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Z, $z);
235 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_TYPE , $sectionData['type']);
236 $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_SUB_TYPE , $sectionData['sub']);
238 // Add value to database
239 $this->queryInsertDataSet($dataSetInstance);
246 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
250 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
253 // Thank you for waiting! :-)
254 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: ' . (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1)) . ' sections has been written for city ' . $cityId . '.');