Micropolis
map_type.h
Go to the documentation of this file.
1/* map_type.h
2 *
3 * Micropolis, Unix Version. This game was released for the Unix platform
4 * in or about 1990 and has been modified for inclusion in the One Laptop
5 * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
6 * you need assistance with this program, you may contact:
7 * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details. You should have received a
18 * copy of the GNU General Public License along with this program. If
19 * not, see <http://www.gnu.org/licenses/>.
20 *
21 * ADDITIONAL TERMS per GNU GPL Section 7
22 *
23 * No trademark or publicity rights are granted. This license does NOT
24 * give you any right, title or interest in the trademark SimCity or any
25 * other Electronic Arts trademark. You may not distribute any
26 * modification of this program using the trademark SimCity or claim any
27 * affliation or association with Electronic Arts Inc. or its employees.
28 *
29 * Any propagation or conveyance of this program must include this
30 * copyright notice and these terms.
31 *
32 * If you convey this program (or any modifications of it) and assume
33 * contractual liability for the program to recipients of it, you agree
34 * to indemnify Electronic Arts for any liability that those contractual
35 * assumptions impose on Electronic Arts.
36 *
37 * You may not misrepresent the origins of this program; modified
38 * versions of the program must be marked as such and not identified as
39 * the original program.
40 *
41 * This disclaimer supplements the one included in the General Public
42 * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
43 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
44 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
45 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
46 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
47 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
48 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
49 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
50 * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
51 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
52 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
53 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
54 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
55 * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
56 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
57 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
58 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
59 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
60 * NOT APPLY TO YOU.
61 */
62
79#ifndef H_MAP_TYPE
80#define H_MAP_TYPE
81
82
84// Constants
85
86
90static const int WORLD_W = 120;
91
95static const int WORLD_H = 100;
96
97
99// Template class definitions
100
101
111template <typename DATA, int BLKSIZE>
112class Map
113{
114public:
115 Map(DATA defaultValue);
119
121 const int MAP_BLOCKSIZE;
122 const int MAP_W;
123 const int MAP_H;
124
125 void fill(DATA val);
126 void clear();
127
128 inline void set(int x, int y, DATA val);
129 inline DATA get(int x, int y) const;
130 inline bool onMap(int x, int y) const;
131
132 inline void worldSet(int x, int y, DATA val);
133 inline DATA worldGet(int x, int y) const;
134 inline bool worldOnMap(int x, int y) const;
135
136 DATA *getBase();
137
138 inline size_t getTotalByteSize() const {
139 return sizeof(DATA) *
140 ((WORLD_W + BLKSIZE - 1) / BLKSIZE) *
141 ((WORLD_H + BLKSIZE - 1) / BLKSIZE);
142 }
143
144private:
146 DATA _mapData[((WORLD_W + BLKSIZE - 1) / BLKSIZE) *
147 ((WORLD_H + BLKSIZE - 1) / BLKSIZE)];
148
150};
151
152
158template <typename DATA, int BLKSIZE>
159Map<DATA, BLKSIZE>::Map(DATA defaultValue):
160 MAP_BLOCKSIZE(BLKSIZE),
161 MAP_W((WORLD_W + BLKSIZE - 1) / BLKSIZE),
162 MAP_H((WORLD_H + BLKSIZE - 1) / BLKSIZE),
163 _MAP_DEFAULT_VALUE(defaultValue)
164{
165}
166
167
169template <typename DATA, int BLKSIZE>
171 MAP_BLOCKSIZE(BLKSIZE),
172 MAP_W((WORLD_W + BLKSIZE - 1) / BLKSIZE),
173 MAP_H((WORLD_H + BLKSIZE - 1) / BLKSIZE),
174 _MAP_DEFAULT_VALUE(map._MAP_DEFAULT_VALUE)
175{
176 for (int i = 0; i < this->MAP_W * this->MAP_H; i++) {
177 this->_mapData[i] = map._mapData[i];
178 }
179}
180
181
183template <typename DATA, int BLKSIZE>
185{
186 if(this != &map) {
187 for (int i = 0; i < this->MAP_W * this->MAP_H; i++) {
188 this->_mapData[i] = map._mapData[i];
189 }
190 }
191 return *this;
192}
193
194
196template <typename DATA, int BLKSIZE>
200
201
207template <typename DATA, int BLKSIZE>
209{
210 for (int i = 0; i < this->MAP_W * this->MAP_H; i++) {
211 this->_mapData[i] = value;
212 }
213}
214
215
221template <typename DATA, int BLKSIZE>
223{
224 fill(this->_MAP_DEFAULT_VALUE);
225}
226
227
232template <typename DATA, int BLKSIZE>
234{
235 return this->_mapData;
236}
237
238
247template <typename DATA, int BLKSIZE>
248inline void Map<DATA, BLKSIZE>::set(int x, int y, DATA value)
249{
250 if(this->onMap(x, y)) {
251 this->_mapData[x * MAP_H + y] = value;
252 }
253}
254
255
264template <typename DATA, int BLKSIZE>
265inline DATA Map<DATA, BLKSIZE>::get(int x, int y) const
266{
267 if(!this->onMap(x, y)) {
268 return this->_MAP_DEFAULT_VALUE;
269 }
270
271 return this->_mapData[x * MAP_H + y];
272}
273
274
281template <typename DATA, int BLKSIZE>
282inline bool Map<DATA, BLKSIZE>::onMap(int x, int y) const
283{
284 return (x >= 0 && x < this->MAP_W) && (y >= 0 && y < this->MAP_H);
285}
286
287
296template <typename DATA, int BLKSIZE>
297inline void Map<DATA, BLKSIZE>::worldSet(int x, int y, DATA value)
298{
299 if(this->worldOnMap(x, y)) {
300 x /= BLKSIZE;
301 y /= BLKSIZE;
302 this->_mapData[x * MAP_H + y] = value;
303 }
304}
305
306
315template <typename DATA, int BLKSIZE>
316inline DATA Map<DATA, BLKSIZE>::worldGet(int x, int y) const
317{
318 if(!this->worldOnMap(x, y)) {
319 return this->_MAP_DEFAULT_VALUE;
320 }
321
322 x /= BLKSIZE;
323 y /= BLKSIZE;
324 return this->_mapData[x * MAP_H + y];
325}
326
327
334template <typename DATA, int BLKSIZE>
335inline bool Map<DATA, BLKSIZE>::worldOnMap(int x, int y) const
336{
337 return (x >= 0 && x < WORLD_W) && (y >= 0 && y < WORLD_H);
338}
339
340
342// Type definitions
343
344
349
350
352
353
354#endif
DATA worldGet(int x, int y) const
Definition map_type.h:316
~Map()
Definition map_type.h:197
Map(DATA defaultValue)
Definition map_type.h:159
const int MAP_BLOCKSIZE
Definition map_type.h:121
void set(int x, int y, DATA val)
Definition map_type.h:248
const int MAP_W
Number of clusters in horizontal direction.
Definition map_type.h:122
Map(const Map< DATA, BLKSIZE > &map)
Definition map_type.h:170
Map & operator=(const Map< DATA, BLKSIZE > &map)
Definition map_type.h:184
bool worldOnMap(int x, int y) const
Definition map_type.h:335
DATA _mapData[((WORLD_W+BLKSIZE - 1)/BLKSIZE) *((WORLD_H+BLKSIZE - 1)/BLKSIZE)]
Definition map_type.h:147
void clear()
Definition map_type.h:222
const DATA _MAP_DEFAULT_VALUE
Default value of a cluster.
Definition map_type.h:149
bool onMap(int x, int y) const
Definition map_type.h:282
const int MAP_H
Number of clusters in vertical direction.
Definition map_type.h:123
DATA * getBase()
Definition map_type.h:233
DATA get(int x, int y) const
Definition map_type.h:265
void fill(DATA val)
Definition map_type.h:208
void worldSet(int x, int y, DATA val)
Definition map_type.h:297
Map< short, 8 > MapShort8
Map of ::short, with cluster size 8.
Definition map_type.h:348
Map< Byte, 2 > MapByte2
Map of Byte, with cluster size 2.
Definition map_type.h:346
Map< Byte, 4 > MapByte4
Map of Byte, with cluster size 4.
Definition map_type.h:347
Map< Byte, 1 > MapByte1
Map of Byte, with cluster size 1.
Definition map_type.h:345
static const int WORLD_H
Definition map_type.h:95
static const int WORLD_W
Definition map_type.h:90