]> git.mxchange.org Git - city.git/blob
0dc60030b3d007a9f0ae678606dd3c31b40a02c3
[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         // 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         const SECTION_TYPE_AIR        = 'air';
72
73         // Sub sections
74         // @TODO All types of water are not supported yet.
75         const SECTION_SUB_TYPE_GRASS = 'grass';
76         const SECTION_SUB_TYPE_AIR   = 'air';
77
78         // Reserved flag
79         const IS_NOT_RESERVED = 0;
80         const IS_RESERVED     = 1;
81
82         /**
83          * Protected constructor
84          *
85          * @return      void
86          */
87         protected function __construct () {
88                 // Call parent constructor
89                 parent::__construct(__CLASS__);
90         }
91
92         /**
93          * Creates an instance of this database wrapper by a provided user class
94          *
95          * @return      $wrapperInstance        An instance of the created wrapper class
96          */
97         public static final function createCitySectionsDatabaseWrapper () {
98                 // Get a new instance
99                 $wrapperInstance = new CitySectionsDatabaseWrapper();
100
101                 // Set (primary!) table name
102                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
103
104                 // Return the instance
105                 return $wrapperInstance;
106         }
107
108         /**
109          * Checks if the given city id is found in sections table
110          *
111          * @param       $cityId         City id to check
112          * @return      $isFound        Whether the city id is found
113          */
114         public function ifCityHasSections ($cityId) {
115                 // Get search instance
116                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
117
118                 // Search for 'city_id'
119                 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
120
121                 /*
122                  * Only one entry is enough to find, else this query could run very
123                  * long on large maps.
124                  */
125                 $searchInstance->setLimit(1);
126
127                 // Execute it on database instance
128                 $resultInstance = $this->doSelectByCriteria($searchInstance);
129
130                 // Check if there is one entry
131                 $isFound = ($resultInstance->next() == 1);
132
133                 // Return result
134                 return $isFound;
135         }
136
137         /**
138          * Expands the sections table with initial data for given city id
139          *
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
143          */
144         public function doInitialCityExpansion ($cityId) {
145                 // Make sure this city has no sections
146                 assert(!$this->ifCityHasSections($cityId));
147
148                 /*
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.
152                  *
153                  * This gives a maxium initial area calculated as follows:
154                  *
155                  * totalInitialSections = (radius + 1) * (radius + 1)
156                  */
157                 $radius = $this->getConfigInstance()->getConfigEntry('city_max_initial_xy_expansion_radius');
158
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');
162
163                 // Extremely huge debug message:
164                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown);
165
166                 // Get data set instance
167                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
168
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);
185
186                 // Set primary key to 'city_id'/'section_id'
187                 $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
188
189                 // Add "zero point"
190                 $this->queryInsertDataSet($dataSetInstance);
191
192                 // Set section id to 2 as 1 is already initialized + init array
193                 $sections = array();
194
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)');
197
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++) {
204                                         // Debug message
205                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
206
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;
210
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;
214                                 } // END - for
215                         } // END - for
216                 } // END - for
217
218                 // Init section id with 2 as 1 is the "zero point"
219                 $sectionId = 2;
220
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) {
227                                         // Debug message
228                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
229
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']);
237
238                                         // Add value to database
239                                         $this->queryInsertDataSet($dataSetInstance);
240
241                                         // Count id up
242                                         $sectionId++;
243                                 } // END - foreach
244
245                                 // Debug message
246                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
247                         } // END - foreach
248
249                         // Debug message
250                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
251                 } // END - foreach
252
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 . '.');
255         }
256 }
257
258 // [EOF]
259 ?>