]> git.mxchange.org Git - city.git/blob
b8b57e5a36b057968e0e8ea998d825d296191544
[city.git] /
1 <?php
2 /**
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.
8  *
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.
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2015 City Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class CitySectionsDatabaseWrapper extends BaseDatabaseWrapper implements CitySectionsWrapper, Registerable {
33         // Constants for database table names
34         const DB_TABLE_CITY_SECTIONS = 'city_sections';
35
36         // Entry id
37         const DB_COLUMN_ENTRY_ID               = 'entry_id';
38
39         // Section id, an referenced city id and lot id
40         const DB_COLUMN_SECTION_ID             = 'city_section_id';
41         const DB_COLUMN_CITY_ID                = 'city_id';
42
43         /*
44          * Lot id, the lot id is only set if the player has "aquired" the sections
45          * and has created a lot based on these sections.
46          */
47         const DB_COLUMN_LOT_ID                 = 'lot_id';
48
49         // Section and sub type (e.g. residential, hut)
50         const DB_COLUMN_SECTION_TYPE           = 'section_type';
51         const DB_COLUMN_SECTION_SUB_TYPE       = 'section_sub_type';
52
53         // X-Y-Z position
54         const DB_COLUMN_SECTION_POSITION_X     = 'section_position_x';
55         const DB_COLUMN_SECTION_POSITION_Y     = 'section_position_y';
56         const DB_COLUMN_SECTION_POSITION_Z     = 'section_position_z';
57
58         // Connected neigbouring sections
59         const DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID  = 'section_neighbour_west_id';
60         const DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID  = 'section_neighbour_east_id';
61         const DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID = 'section_neighbour_north_id';
62         const DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID = 'section_neighbour_south_id';
63         const DB_COLUMN_SECTION_NEIGHBOUR_UP_ID    = 'section_neighbour_up_id';
64         const DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID  = 'section_neighbour_down_id';
65
66         // Other settings:
67         // Reserved section (see documentation)
68         const DB_COLUMN_SECTION_RESERVED       = 'section_reserved';
69
70         // Section types
71         // @TODO "water" is not yet supported and may end up in a very random land.
72         const SECTION_TYPE_WATER      = 'water';
73         const SECTION_TYPE_EMPTY_LAND = 'land';
74         const SECTION_TYPE_AIR        = 'air';
75
76         // Sub sections
77         // @TODO All types of water are not supported yet.
78         const SECTION_SUB_TYPE_GRASS = 'grass';
79         const SECTION_SUB_TYPE_AIR   = 'air';
80
81         // Reserved flag
82         const IS_NOT_RESERVED = 0;
83         const IS_RESERVED     = 1;
84
85         /**
86          * Protected constructor
87          *
88          * @return      void
89          */
90         protected function __construct () {
91                 // Call parent constructor
92                 parent::__construct(__CLASS__);
93         }
94
95         /**
96          * Creates an instance of this database wrapper by a provided user class
97          *
98          * @return      $wrapperInstance        An instance of the created wrapper class
99          */
100         public static final function createCitySectionsDatabaseWrapper () {
101                 // Get a new instance
102                 $wrapperInstance = new CitySectionsDatabaseWrapper();
103
104                 // Set (primary!) table name
105                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
106
107                 // Return the instance
108                 return $wrapperInstance;
109         }
110
111         /**
112          * Checks if the given city id is found in sections table
113          *
114          * @param       $cityId         City id to check
115          * @return      $isFound        Whether the city id is found
116          */
117         public function ifCityHasSections ($cityId) {
118                 // Get search instance
119                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
120
121                 // Search for 'city_id'
122                 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
123
124                 /*
125                  * Only one entry is enough to find, else this query could run very
126                  * long on large maps.
127                  */
128                 $searchInstance->setLimit(1);
129
130                 // Execute it on database instance
131                 $resultInstance = $this->doSelectByCriteria($searchInstance);
132
133                 // Check if there is one entry
134                 $isFound = ($resultInstance->next() == 1);
135
136                 // Return result
137                 return $isFound;
138         }
139
140         /**
141          * Expands the sections table with initial data for given city id
142          *
143          * @param       $cityId         City id to check
144          * @return      $ids            Sections ids from initial expansion
145          * @todo        Add handling of water types to make a more cooler map
146          */
147         public function doInitialCityExpansion ($cityId) {
148                 // Make sure this city has no sections
149                 assert(!$this->ifCityHasSections($cityId));
150
151                 /*
152                  * "Cache" max "radius" for initial city expansion. It is not a real
153                  * radius but more a max expansion area (expand +- half of "radius"
154                  * from 0/0). The "zero point" is always calculated in.
155                  *
156                  * This gives a maxium initial area calculated as follows:
157                  *
158                  * totalInitialSections = (radius + 1) * (radius + 1)
159                  */
160                 $radius = $this->getConfigInstance()->getConfigEntry('city_max_initial_xy_expansion_radius');
161
162                 // Max up and down ...
163                 $maxUp   = $this->getConfigInstance()->getConfigEntry('city_max_initial_up_expansion');
164                 $maxDown = $this->getConfigInstance()->getConfigEntry('city_max_initial_down_expansion');
165
166                 // Calculate total sections
167                 $totalSections = (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1));
168
169                 // Debug message:
170                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown . ',totalSections=' . $totalSections);
171
172                 // Get data set instance
173                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
174
175                 // Add values for "zero point"
176                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_ID                , 1);
177                 $dataSetInstance->addCriteria(self::DB_COLUMN_CITY_ID                   , $cityId);
178                 $dataSetInstance->addCriteria(self::DB_COLUMN_LOT_ID                    , 0);
179                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_TYPE              , self::SECTION_TYPE_AIR);
180                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_SUB_TYPE          , self::SECTION_SUB_TYPE_AIR);
181                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_X        , 0);
182                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_Y        , 0);
183                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_POSITION_Z        , 0);
184                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID, 0);
185                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID, 0);
186                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID , 0);
187                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID , 0);
188                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_UP_ID   , 0);
189                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID , 0);
190                 $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_RESERVED          , self::IS_NOT_RESERVED);
191
192                 // Set primary key to 'city_id'/'section_id'
193                 $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
194
195                 // Entry id for counting each entry
196                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_ENTRY_ID);
197
198                 // Add "zero point"
199                 $this->queryInsertDataSet($dataSetInstance);
200
201                 // Set section id to 2 as 1 is already initialized + init array
202                 $sections = array();
203
204                 // Output message to ask for user's patience ...
205                 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Writing ' . $totalSections . ' sections for city ' . $cityId . ' ... (this may takes some time)');
206
207                 // Expand half of it to north/south (north=positive, south=negative)
208                 for ($north = 1; $north < round($radius / 2); $north++) {
209                         // Expand half of it to west/east (west=positive, east=negative)
210                         for ($west = 1; $west < round($radius / 2); $west++) {
211                                 // Expand up/down (including "zero point")
212                                 for ($z = $maxDown; $z < ($maxUp + 1); $z++) {
213                                         // Debug message
214                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
215
216                                         // Fill array up with south/east/down ids
217                                         $sections[($north * -1)][($west * -1)][$z]['type'] = self::SECTION_TYPE_EMPTY_LAND;
218                                         $sections[($north * -1)][($west * -1)][$z]['sub']  = self::SECTION_SUB_TYPE_GRASS;
219
220                                         // Fill up array with north/west/up ids (only air)
221                                         $sections[$north][$west][$z]['type'] = self::SECTION_TYPE_AIR;
222                                         $sections[$north][$west][$z]['sub']  = self::SECTION_SUB_TYPE_AIR;
223                                 } // END - for
224                         } // END - for
225                 } // END - for
226
227                 // Init section id with 2 as 1 is the "zero point"
228                 $sectionId = 2;
229
230                 // Loop through whole array again for writing it to database: north/south
231                 foreach ($sections as $x => $sectionX) {
232                         // Loop through west/east values
233                         foreach ($sectionX as $y => $sectionY) {
234                                 // Loop through up/down values
235                                 foreach ($sectionY as $z => $sectionData) {
236                                         // Debug message
237                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
238
239                                         // Set all coordinates for positive directions
240                                         $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_ID        , $sectionId);
241                                         $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_X, $x);
242                                         $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Y, $y);
243                                         $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Z, $z);
244                                         $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_TYPE      , $sectionData['type']);
245                                         $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_SUB_TYPE  , $sectionData['sub']);
246
247                                         // Add value to database
248                                         $this->queryInsertDataSet($dataSetInstance);
249
250                                         // Count id up
251                                         $sectionId++;
252                                 } // END - foreach
253
254                                 // Debug message
255                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
256                         } // END - foreach
257
258                         // Debug message
259                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
260                 } // END - foreach
261
262                 // Thank you for waiting! :-)
263                 self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: A total of ' . $totalSections . ' sections has been written for city id ' . $cityId . '.');
264         }
265 }
266
267 // [EOF]
268 ?>