Debian Codegraph @

engine/map/map.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name map.cpp - The map. */
11 //
12 // (c) Copyright 1998-2008 by Lutz Sammer, Vladi Shabanski and
13 // Francois Beerten
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 //@{
31
32 /*----------------------------------------------------------------------------
33 -- Includes
34 ----------------------------------------------------------------------------*/
35
36 #include <string.h>
37
38 #include "stratagus.h"
39 #include "unittype.h"
40 #include "map.h"
41 #include "minimap.h"
42 #include "player.h"
43 #include "unit.h"
44 #include "unit_cache.h"
45 #include "ui.h"
46 #include "script.h"
47
48 /*----------------------------------------------------------------------------
49 -- Variables
50 ----------------------------------------------------------------------------*/
51
52 CMap Map; /// The current map
53 int FlagRevealMap; /// Flag must reveal the map
54 int ReplayRevealMap; /// Reveal Map is replay
55 char CurrentMapPath[1024]; /// Path of the current map
56
57 /**
58 ** Size of a tile in X
59 */
60 int TileSizeX = 32;
61
62 /**
63 ** Size of a tile in Y
64 */
65 int TileSizeY = 32;
66
67 /*----------------------------------------------------------------------------
68 -- Visible and explored handling
69 ----------------------------------------------------------------------------*/
70
71 /**
72 ** Reveal the entire map.
73 */
74 void CMap::Reveal(void)
75 {
76 int x;
77
78 //
79 // Mark every explored tile as visible. 1 turns into 2.
80 //
81 for (x = 0; x < this->Info.MapWidth; ++x) {
82 for (int y = 0; y < this->Info.MapHeight; ++y) {
83 for (int p = 0; p < PlayerMax; ++p) {
84 if (!this->Field(x, y)->Visible[p]) {
85 this->Field(x, y)->Visible[p] = 1;
86 }
87 }
88 }
89 }
90 //
91 // Global seen recount. Simple and effective.
92 //
93 for (x = 0; x < NumUnits; ++x) {
94 //
95 // Reveal neutral buildings. Gold mines:)
96 //
97 if (Units[x]->Player->Type == PlayerNeutral) {
98 for (int p = 0; p < PlayerMax; ++p) {
99 if (Players[p].Type != PlayerNobody &&
100 (!(Units[x]->Seen.ByPlayer & (1 << p)))) {
101 UnitGoesOutOfFog(Units[x], Players + p);
102 UnitGoesUnderFog(Units[x], Players + p);
103 }
104 }
105 }
106 UnitCountSeen(Units[x]);
107 }
108 }
109
110 /*----------------------------------------------------------------------------
111 -- Map queries
112 ----------------------------------------------------------------------------*/
113
114 /**
115 ** Water on map tile.
116 **
117 ** @param tx X map tile position.
118 ** @param ty Y map tile position.
119 **
120 ** @return True if water, false otherwise.
121 */
122 bool CMap::WaterOnMap(int tx, int ty) const
123 {
124 Assert(tx >= 0 && ty >= 0 && tx < Info.MapWidth && ty < Info.MapHeight);
125 return (this->Field(tx, ty)->Flags
126 & (MapFieldShallowWater | MapFieldDeepWater)) != 0;
127 }
128
129 /**
130 ** Coast on map tile.
131 **
132 ** @param tx X map tile position.
133 ** @param ty Y map tile position.
134 **
135 ** @return True if coast, false otherwise.
136 */
137 bool CMap::CoastOnMap(int tx, int ty) const
138 {
139 Assert(tx >= 0 && ty >= 0 && tx < Info.MapWidth && ty < Info.MapHeight);
140 return (this->Field(tx, ty)->Flags & MapFieldCoastAllowed) != 0;
141
142 }
143
144 /**
145 ** Can move to this point, applying mask.
146 **
147 ** @param x X map tile position.
148 ** @param y Y map tile position.
149 ** @param mask Mask for movement to apply.
150 **
151 ** @return True if could be entered, false otherwise.
152 */
153 bool CheckedCanMoveToMask(int x, int y, int mask)
154 {
155 if (x < 0 || y < 0 || x >= Map.Info.MapWidth || y >= Map.Info.MapHeight) {
156 return false;
157 }
158
159 return !(Map.Field(x, y)->Flags & mask);
160 }
161
162 /**
163 ** Can a unit of unit-type be placed at this point.
164 **
165 ** @param type unit-type to be checked.
166 ** @param x X map tile position.
167 ** @param y Y map tile position.
168 **
169 ** @return True if could be entered, false otherwise.
170 */
171 bool UnitTypeCanBeAt(const CUnitType *type, int x, int y)
172 {
173 Assert(type);
174 for (int addx = 0; addx < type->TileWidth; ++addx) {
175 for (int addy = 0; addy < type->TileHeight; ++addy) {
176 if (!CheckedCanMoveToMask(x + addx, y + addy, type->MovementMask)) {
177 return false;
178 }
179 }
180 }
181 return true;
182 }
183
184 /**
185 ** Can a unit be placed to this point.
186 **
187 ** @param unit unit to be checked.
188 ** @param x X map tile position.
189 ** @param y Y map tile position.
190 **
191 ** @return True if could be placeded, false otherwise.
192 */
193 bool UnitCanBeAt(const CUnit *unit, int x, int y)
194 {
195 Assert(unit);
196 return UnitTypeCanBeAt(unit->Type, x, y);
197 }
198
199 /**
200 ** Release info about a map.
201 **
202 ** @param info CMapInfo pointer.
203 */
204 void FreeMapInfo(CMapInfo *info)
205 {
206 if (info) {
207 info->Description.clear();
208 info->Filename.clear();
209 info->MapWidth = info->MapHeight = 0;
210 memset(info->PlayerSide, 0, sizeof(info->PlayerSide));
211 memset(info->PlayerType, 0, sizeof(info->PlayerType));
212 info->MapUID = 0;
213 }
214 }
215
216 /**
217 ** Initialize the map
218 */
219 void CMap::Init()
220 {
221 InitFogOfWar();
222 Map.PatchManager.load();
223 }
224
225 /**
226 ** Alocate and initialize map table
227 */
228 void CMap::Create()
229 {
230 Assert(!this->Fields);
231
232 this->Fields = new CMapField[this->Info.MapWidth * this->Info.MapHeight];
233 this->Visible[0] = new unsigned[this->Info.MapWidth * this->Info.MapHeight / 2];
234 memset(this->Visible[0], 0, this->Info.MapWidth * this->Info.MapHeight / 2 * sizeof(unsigned));
235 UnitCache.Init(this->Info.MapWidth, this->Info.MapHeight);
236 }
237
238 /**
239 ** Cleanup the map module.
240 */
241 void CMap::Clean(void)
242 {
243 delete[] this->Fields;
244 delete[] this->Visible[0];
245
246 FreeMapInfo(&this->Info);
247 this->Fields = NULL;
248 memset(this->Visible, 0, sizeof(this->Visible));
249 this->NoFogOfWar = false;
250
251 this->PatchManager.clear();
252
253 FlagRevealMap = 0;
254 ReplayRevealMap = 0;
255
256 UI.Minimap.Destroy();
257 }
258
259
260 /*----------------------------------------------------------------------------
261 -- Load Map Functions
262 ----------------------------------------------------------------------------*/
263
264 /**
265 ** Load the map presentation
266 **
267 ** @param mapname map filename
268 */
269 void LoadStratagusMapInfo(const std::string &mapname)
270 {
271 // Set the default map setup by replacing .smp with .sms
272 size_t loc = mapname.find(".smp");
273 if (loc != std::string::npos) {
274 Map.Info.Filename = mapname;
275 Map.Info.Filename.replace(loc, 4, ".sms");
276 }
277
278 LuaLoadFile(mapname);
279 }
280
281 //@}