]> git.mxchange.org Git - city.git/blob - application/city/main/database/frontend/city_entities/sections/class_CitySectionsDatabaseWrapper.php
28826adeba3f9baf1002196fb47464582b415404
[city.git] / application / city / main / database / frontend / city_entities / sections / class_CitySectionsDatabaseWrapper.php
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         // 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';
39
40         /*
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.
43          */
44         const DB_COLUMN_LOT_ID                 = 'lot_id';
45
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';
49
50         // X-Y-Z position
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';
54
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';
62
63         // Other settings:
64         // Reserved section (see documentation)
65         const DB_COLUMN_SECTION_RESERVED       = 'section_reserved';
66
67         // Section types
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
72         // Sub sections
73         // @TODO All types of water are not supported yet.
74         const SUB_SECTION_TYPE_EMPTY = 'empty';
75
76         // Reserved flag
77         const IS_NOT_RESERVED = 0;
78         const IS_RESERVED     = 1;
79
80         /**
81          * Protected constructor
82          *
83          * @return      void
84          */
85         protected function __construct () {
86                 // Call parent constructor
87                 parent::__construct(__CLASS__);
88         }
89
90         /**
91          * Creates an instance of this database wrapper by a provided user class
92          *
93          * @return      $wrapperInstance        An instance of the created wrapper class
94          */
95         public static final function createCitySectionsDatabaseWrapper () {
96                 // Get a new instance
97                 $wrapperInstance = new CitySectionsDatabaseWrapper();
98
99                 // Set (primary!) table name
100                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
101
102                 // Return the instance
103                 return $wrapperInstance;
104         }
105
106         /**
107          * Checks if the given city id is found in sections table
108          *
109          * @param       $cityId         City id to check
110          * @return      $isFound        Whether the city id is found
111          */
112         public function ifCityHasSections ($cityId) {
113                 // Get search instance
114                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
115
116                 // Search for 'city_id'
117                 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
118
119                 /*
120                  * Only one entry is enough to find, else this query could run very
121                  * long on large maps.
122                  */
123                 $searchInstance->setLimit(1);
124
125                 // Execute it on database instance
126                 $resultInstance = $this->doSelectByCriteria($searchInstance);
127
128                 // Check if there is one entry
129                 $isFound = ($resultInstance->next() == 1);
130
131                 // Return result
132                 return $isFound;
133         }
134
135         /**
136          * Expands the sections table with initial data for given city id
137          *
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
141          */
142         public function doInitialCityExpansion ($cityId) {
143                 // Make sure this city has no sections
144                 assert(!$this->ifCityHasSections($cityId));
145
146                 /*
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.
150                  *
151                  * This gives a maxium initial area calculated as follows:
152                  *
153                  * totalInitialSections = (radius + 1) * (radius + 1)
154                  */
155                 $radius = $this->getConfigInstance()->getConfigEntry('city_max_initial_xy_expansion_radius');
156
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');
160
161                 // Extremely huge debug message:
162                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown);
163
164                 // Get data set instance
165                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
166
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);
183
184                 // Set primary key to 'city_id'/'section_id'
185                 $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
186
187                 // Add "zero point"
188                 $this->queryInsertDataSet($dataSetInstance);
189
190                 // Set section id to 2 as 1 is already initialized + init array
191                 $sections = array();
192
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)');
195
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++) {
202                                         // Debug message
203                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
204
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;
208
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;
212                                 } // END - for
213                         } // END - for
214                 } // END - for
215
216                 // Init section id with 2 as 1 is the "zero point"
217                 $sectionId = 2;
218
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) {
225                                         // Debug message
226                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
227
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']);
235
236                                         // Add value to database
237                                         $this->queryInsertDataSet($dataSetInstance);
238
239                                         // Count id up
240                                         $sectionId++;
241                                 } // END - foreach
242
243                                 // Debug message
244                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
245                         } // END - foreach
246
247                         // Debug message
248                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
249                 } // END - foreach
250
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 . '.');
253         }
254 }
255
256 // [EOF]
257 ?>