_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

map.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2006 by Vladi Shabanski, Lutz Sammer, and
14 // Jimmy Salmon
15 //
16 // This program is free software; you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation; only version 2 of the License.
19 //
20 // This program is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with this program; if not, write to the Free Software
27 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 // 02111-1307, USA.
29 //
30 
31 #ifndef __MAP_H__
32 #define __MAP_H__
33 
34 #include "settings.h"
36 
37 /*----------------------------------------------------------------------------
38 -- Documentation
39 ----------------------------------------------------------------------------*/
40 
82 /*----------------------------------------------------------------------------
83 -- Includes
84 ----------------------------------------------------------------------------*/
85 
86 #include <string>
87 
88 #ifndef __MAP_TILE_H__
89 #include "tile.h"
90 #endif
91 
92 #include "color.h"
93 #include "vec2i.h"
94 
95 #include "settings.h"
96 
97 /*----------------------------------------------------------------------------
98 -- Declarations
99 ----------------------------------------------------------------------------*/
100 
101 class CGraphic;
102 class CPlayer;
103 class CFile;
104 class CTileset;
105 class CUnit;
106 class CUnitType;
107 
108 /*----------------------------------------------------------------------------
109 -- Map
110 ----------------------------------------------------------------------------*/
111 
112 #define MaxMapWidth 256
113 #define MaxMapHeight 256
114 
115 /*----------------------------------------------------------------------------
116 -- Map info structure
117 ----------------------------------------------------------------------------*/
118 
122 class CMapInfo
123 {
124 public:
125  bool IsPointOnMap(int x, int y) const
126  {
127  return (x >= 0 && y >= 0 && x < MapWidth && y < MapHeight);
128  }
129 
130  bool IsPointOnMap(const Vec2i &pos) const
131  {
132  return IsPointOnMap(pos.x, pos.y);
133  }
134 
135  void Clear();
136 
137 public:
138  std::string Description;
139  std::string Filename;
140  std::string Preamble;
141  std::string Postamble;
142  int MapWidth;
143  int MapHeight;
146  unsigned int MapUID;
147 };
148 
149 /*----------------------------------------------------------------------------
150 -- Map itself
151 ----------------------------------------------------------------------------*/
152 
154 class CMap
155 {
156 public:
157  CMap();
158  ~CMap();
159 
160  void AllocateTileset();
161 
162  unsigned int getIndex(int x, int y) const
163  {
164  return x + y * this->Info.MapWidth;
165  }
166  unsigned int getIndex(const Vec2i &pos) const
167  {
168  return getIndex(pos.x, pos.y);
169  }
170 
171  CMapField *Field(unsigned int index) const
172  {
173  return &this->Fields[index];
174  }
176  CMapField *Field(int x, int y) const
177  {
178  return &this->Fields[x + y * this->Info.MapWidth];
179  }
180  CMapField *Field(const Vec2i &pos) const
181  {
182  return Field(pos.x, pos.y);
183  }
184 
185  bool isInitialized() const
186  {
187  return this->isMapInitialized;
188  }
189 
191  void Create();
193  void Init();
195  void Clean(const bool isHardClean = false);
197  void ClearTile(const Vec2i &tilePos);
198 
200  Vec2i MapPixelPosToTilePos(const PixelPos &mapPos) const;
202  PixelPos TilePosToMapPixelPos_TopLeft(const Vec2i &tilePos) const;
204  PixelPos TilePosToMapPixelPos_Center(const Vec2i &tilePos) const;
205 
207  void MarkSeenTile(CMapField &mf);
208 
210  void RegenerateForest();
214  void Save(CFile &file) const;
215 
216  //
217  // Wall
218  //
220  void HitWall(const Vec2i &pos, unsigned damage);
222  void RemoveWall(const Vec2i &pos);
224  void SetWall(const Vec2i &pos, bool humanwall);
225 
227  bool WallOnMap(const Vec2i &pos) const;
229  bool HumanWallOnMap(const Vec2i &pos) const;
231  bool OrcWallOnMap(const Vec2i &pos) const;
232 
233  //UnitCache
234 
236  void Insert(CUnit &unit);
237 
239  void Remove(CUnit &unit);
240 
241  void Clamp(Vec2i &pos) const;
242 
243  //Warning: we expect typical usage as xmin = x - range
244  void FixSelectionArea(Vec2i &minpos, Vec2i &maxpos)
245  {
246  minpos.x = std::max<short>(0, minpos.x);
247  minpos.y = std::max<short>(0, minpos.y);
248 
249  maxpos.x = std::min<short>(maxpos.x, Info.MapWidth - 1);
250  maxpos.y = std::min<short>(maxpos.y, Info.MapHeight - 1);
251  }
252 
253 private:
255  void ClearWoodTile(const Vec2i &pos);
257  void ClearRockTile(const Vec2i &pos);
258 
260  void FixNeighbors(unsigned short type, int seen, const Vec2i &pos);
262  void FixTile(unsigned short type, int seen, const Vec2i &pos);
263 
265  void RegenerateForestTile(const Vec2i &pos);
266 
267 public:
269  bool NoFogOfWar;
270 
272  std::string TileModelsFileName;
274  bool isMapInitialized { false };
275 
277 };
278 
279 
280 /*----------------------------------------------------------------------------
281 -- Variables
282 ----------------------------------------------------------------------------*/
283 
284 extern CMap Map;
285 extern char CurrentMapPath[1024];
286 
288 extern unsigned int ForestRegeneration;
290 extern int ForestRegenerationFrequency;
294 extern int ReplayRevealMap;
295 
296 /*----------------------------------------------------------------------------
297 -- Functions
298 ----------------------------------------------------------------------------*/
299 //
300 // in map_fog.c
301 //
303 typedef void MapMarkerFunc(const CPlayer &player, const unsigned int index);
304 
306 extern int MapFogFilterFlags(CPlayer &player, const Vec2i &pos, int mask);
307 extern int MapFogFilterFlags(CPlayer &player, const unsigned int index, int mask);
316 
318 extern void MapSight(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w,
319  int h, int range, MapMarkerFunc *marker);
321 extern void UpdateFogOfWarChange();
322 
323 //
324 // in map_radar.c
325 //
326 
329 
332 
335 
338 
339 
340 //
341 // in map_wall.c
342 //
344 extern void MapFixSeenWallTile(const Vec2i &pos);
346 extern void MapFixSeenWallNeighbors(const Vec2i &pos);
348 extern void MapFixWallTile(const Vec2i &pos);
349 
350 //
351 // in script_map.cpp
352 //
354 extern void SetTile(unsigned int tile, const Vec2i &pos, int value = 0);
355 inline void SetTile(unsigned int tile, int x, int y, int value = 0)
356 {
357  const Vec2i pos(x, y);
358  SetTile(tile, pos, value);
359 }
360 
362 extern void MapCclRegister();
363 
364 //
365 // mixed sources
366 //
368 extern int SaveStratagusMap(const std::string &filename, CMap &map, int writeTerrain,
369  Vec2i newSize = {0, 0}, Vec2i offset = {0, 0});
370 
371 
373 extern bool LoadStratagusMapInfo(const std::string &mapname);
374 
376 extern bool CheckedCanMoveToMask(const Vec2i &pos, int mask);
378 extern bool UnitTypeCanBeAt(const CUnitType &type, const Vec2i &pos);
380 extern bool UnitCanBeAt(const CUnit &unit, const Vec2i &pos);
381 
383 extern void PreprocessMap();
384 
385 // in unit.c
386 //typedef void MapClearField(const Vec2i &tilePos);
387 
389 void MapMarkUnitSight(CUnit &unit);
391 void MapUnmarkUnitSight(CUnit &unit);
393 void MapRefreshUnitsSight(const Vec2i &tilePos, const bool resetSight = false);
395 void MapRefreshUnitsSight(const bool resetSight = false);
396 
397 /*----------------------------------------------------------------------------
398 -- Defines
399 ----------------------------------------------------------------------------*/
400 
402 inline bool CanMoveToMask(const Vec2i &pos, int mask)
403 {
404  return !Map.Field(pos)->CheckMask(mask);
405 }
406 
408 inline void MapMarkRadar(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
409 {
410  MapSight(player, unit, pos, w, h, range, MapMarkTileRadar);
411 }
412 inline void MapUnmarkRadar(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
413 {
414  MapSight(player, unit, pos, w, h, range, MapUnmarkTileRadar);
415 }
417 inline void MapMarkRadarJammer(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
418 {
419  MapSight(player, unit, pos, w, h, range, MapMarkTileRadarJammer);
420 }
421 inline void MapUnmarkRadarJammer(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
422 {
423  MapSight(player, unit, pos, w, h, range, MapUnmarkTileRadarJammer);
424 }
425 
427 
428 #endif // !__MAP_H__
CPlayer
Diplomacy states for CommandDiplomacy.
Definition: player.h:83
MapMarkTileRadar
MapMarkerFunc MapMarkTileRadar
Mark a tile as radar visible, or incrase radar vision.
CMap::Clean
void Clean(const bool isHardClean=false)
Clean the map.
Definition: map.cpp:359
CMap::RegenerateForest
void RegenerateForest()
Regenerate the forest.
Definition: map.cpp:676
MapUnmarkRadarJammer
void MapUnmarkRadarJammer(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
Definition: map.h:421
MapFixSeenWallNeighbors
void MapFixSeenWallNeighbors(const Vec2i &pos)
Correct the surrounding seen wall fields.
Definition: map_wall.cpp:154
LoadStratagusMapInfo
bool LoadStratagusMapInfo(const std::string &mapname)
Load map presentation.
Definition: map.cpp:699
MapMarkTileRadarJammer
MapMarkerFunc MapMarkTileRadarJammer
Mark a tile as radar jammed, or incrase radar jamming'ness.
CMapInfo::Preamble
std::string Preamble
Map filename.
Definition: map.h:140
ReplayRevealMap
int ReplayRevealMap
Flag must reveal map when in replay.
Definition: map.cpp:57
CMap::Create
void Create()
Alocate and initialise map table.
Definition: map.cpp:339
settings.h
CMap::Field
CMapField * Field(int x, int y) const
Get the MapField at location x,y.
Definition: map.h:176
MapMarkUnitSight
void MapMarkUnitSight(CUnit &unit)
Mark on vision table the Sight of the unit.
Definition: unit.cpp:873
CMap::Remove
void Remove(CUnit &unit)
Remove unit from cache.
Definition: unit_cache.cpp:76
MapMarkTileSight
MapMarkerFunc MapMarkTileSight
Mark a tile for normal sight.
CMap::HitWall
void HitWall(const Vec2i &pos, unsigned damage)
Wall is hit.
Definition: map_wall.cpp:280
MapUnmarkTileDetectCloak
MapMarkerFunc MapUnmarkTileDetectCloak
Unmark a tile for cloak detection.
CMap::getIndex
unsigned int getIndex(const Vec2i &pos) const
Definition: map.h:166
ForestRegeneration
unsigned int ForestRegeneration
Path to the current map.
Definition: map.cpp:58
CMap::Reveal
void Reveal(MapRevealModes mode=MapRevealModes::cKnown)
Set map reveal mode: hidden/known/fully explored.
Definition: map.cpp:133
CMap::TilePosToMapPixelPos_Center
PixelPos TilePosToMapPixelPos_Center(const Vec2i &tilePos) const
convert tilepos coordonates into map pixel pos (take the center of the tile)
Definition: map.cpp:187
CMapInfo::MapWidth
int MapWidth
Map postamble script.
Definition: map.h:142
MapMarkRadar
void MapMarkRadar(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
Handle Marking and Unmarking of radar vision.
Definition: map.h:408
CMap::NoFogOfWar
bool NoFogOfWar
fields on map
Definition: map.h:269
SaveStratagusMap
int SaveStratagusMap(const std::string &filename, CMap &map, int writeTerrain, Vec2i newSize={0, 0}, Vec2i offset={0, 0})
Save a stratagus map (smp format)
Definition: game.cpp:542
MapFogFilterFlags
int MapFogFilterFlags(CPlayer &player, const Vec2i &pos, int mask)
Filter map flags through fog.
Definition: map_fog.cpp:107
vec2i.h
CMapInfo::Postamble
std::string Postamble
Map preamble script.
Definition: map.h:141
Vec2T::y
T y
Definition: vec2i.h:43
CMap::AllocateTileset
void AllocateTileset()
Definition: map.cpp:330
CMapInfo::Description
std::string Description
Definition: map.h:138
CMap::Save
void Save(CFile &file) const
Save the map.
Definition: map.cpp:393
CUnitType
Definition: unittype.h:508
MapMarkerFunc
void MapMarkerFunc(const CPlayer &player, const unsigned int index)
Function to (un)mark the vision table.
Definition: map.h:303
CMap::RemoveWall
void RemoveWall(const Vec2i &pos)
Set wall on field.
Definition: map_wall.cpp:216
CMapInfo::PlayerType
PlayerTypes PlayerType[PlayerMax]
Map height.
Definition: map.h:144
Vec2T< short int >
CMap::MapPixelPosToTilePos
Vec2i MapPixelPosToTilePos(const PixelPos &mapPos) const
convert map pixelpos coordonates into tilepos
Definition: map.cpp:173
CanMoveToMask
bool CanMoveToMask(const Vec2i &pos, int mask)
Can a unit with 'mask' enter the field.
Definition: map.h:402
CMap::CMap
CMap()
Definition: map.cpp:321
FlagRevealMap
MapRevealModes FlagRevealMap
Flag must reveal the map.
Definition: map.cpp:56
CMap::WallOnMap
bool WallOnMap(const Vec2i &pos) const
Returns true, if wall on the map tile field.
Definition: map.cpp:199
CMap::Field
CMapField * Field(unsigned int index) const
Definition: map.h:171
CMap::MarkSeenTile
void MarkSeenTile(CMapField &mf)
Mark a tile as seen by the player.
Definition: map.cpp:71
CMap::Clamp
void Clamp(Vec2i &pos) const
Definition: unit_cache.cpp:96
CMap::Init
void Init()
Build tables for map.
Definition: map.cpp:350
CMapInfo
Definition: map.h:122
CMapInfo::Clear
void Clear()
Definition: map.cpp:311
MapSight
void MapSight(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range, MapMarkerFunc *marker)
Mark sight changes.
Definition: map_fog.cpp:347
MapFixWallTile
void MapFixWallTile(const Vec2i &pos)
Correct the real wall field, depending on the surrounding.
Definition: map_wall.cpp:168
PlayerMax
constexpr unsigned char PlayerMax
Definition: settings.h:62
CurrentMapPath
char CurrentMapPath[1024]
The current map.
Definition: map.cpp:60
CMap::Insert
void Insert(CUnit &unit)
Insert new unit into cache.
Definition: unit_cache.cpp:52
MapUnmarkTileRadarJammer
MapMarkerFunc MapUnmarkTileRadarJammer
Unmark a tile as jammed, decrease is jamming'ness.
CMapInfo::PlayerSide
int PlayerSide[PlayerMax]
Same player->Type.
Definition: map.h:145
CMap::isInitialized
bool isInitialized() const
Definition: map.h:185
SetTile
void SetTile(unsigned int tile, const Vec2i &pos, int value=0)
Set a tile.
Definition: script_map.cpp:753
MapUnmarkTileSight
MapMarkerFunc MapUnmarkTileSight
Unmark a tile for normal sight.
CMap::Fields
CMapField * Fields
Definition: map.h:268
UnitTypeCanBeAt
bool UnitTypeCanBeAt(const CUnitType &type, const Vec2i &pos)
Returns true, if the unit-type can enter the field.
Definition: map.cpp:252
MapRevealModes
ENUM_CLASS MapRevealModes
Definition: settings.h:209
CMap::SetWall
void SetWall(const Vec2i &pos, bool humanwall)
Set wall on field.
Definition: map_wall.cpp:241
MapUnmarkUnitSight
void MapUnmarkUnitSight(CUnit &unit)
Unmark on vision table the Sight of the unit.
Definition: unit.cpp:901
CheckedCanMoveToMask
bool CheckedCanMoveToMask(const Vec2i &pos, int mask)
Returns true, if the unit-type(mask can enter field with bounds check.
Definition: map.cpp:239
update-images.w
w
Definition: update-images.py:53
MapUnmarkTileRadar
MapMarkerFunc MapUnmarkTileRadar
Unmark a tile as radar visible, decrease is visible by other radar.
MapCclRegister
void MapCclRegister()
register ccl features
Definition: script_map.cpp:1000
CMapField::CheckMask
bool CheckMask(int mask) const
Check if a field flags.
Definition: mapfield.cpp:256
cKnown
ENUM_CLASS cKnown
Definition: settings.h:210
ForestRegenerationFrequency
int ForestRegenerationFrequency
Forest regeneration.
Definition: map.cpp:59
CMap::~CMap
~CMap()
Definition: map.cpp:325
CMap::OrcWallOnMap
bool OrcWallOnMap(const Vec2i &pos) const
Returns true, if orc wall on the map tile field.
Definition: map.cpp:225
CMap::getIndex
unsigned int getIndex(int x, int y) const
Definition: map.h:162
UnitCanBeAt
bool UnitCanBeAt(const CUnit &unit, const Vec2i &pos)
Returns true, if the unit can enter the field.
Definition: map.cpp:279
CMap
Describes the world map.
Definition: map.h:154
CMap::ClearTile
void ClearTile(const Vec2i &tilePos)
Remove wood, rock or wall from the map and update nearby unit's vision if needed.
Definition: map.cpp:551
CMap::TileModelsFileName
std::string TileModelsFileName
tileset data
Definition: map.h:272
CMap::Info
CMapInfo Info
Definition: map.h:276
CMapInfo::IsPointOnMap
bool IsPointOnMap(int x, int y) const
Definition: map.h:125
CMapInfo::IsPointOnMap
bool IsPointOnMap(const Vec2i &pos) const
Definition: map.h:130
CMap::Field
CMapField * Field(const Vec2i &pos) const
Definition: map.h:180
CMapInfo::MapHeight
int MapHeight
Map width.
Definition: map.h:143
CMap::TileGraphic
CGraphic * TileGraphic
lua filename that loads all tilemodels
Definition: map.h:273
Vec2T::x
T x
Definition: vec2i.h:42
MapMarkTileDetectCloak
MapMarkerFunc MapMarkTileDetectCloak
Mark a tile for cloak detection.
CMap::Tileset
CTileset * Tileset
fog of war disabled
Definition: map.h:271
MapMarkRadarJammer
void MapMarkRadarJammer(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
Handle Marking and Unmarking of radar vision.
Definition: map.h:417
CMapInfo::Filename
std::string Filename
Map description.
Definition: map.h:139
MapFixSeenWallTile
void MapFixSeenWallTile(const Vec2i &pos)
Correct the seen wall field, depending on the surrounding.
Definition: map_wall.cpp:124
UpdateFogOfWarChange
void UpdateFogOfWarChange()
Update fog of war.
Definition: map_fog.cpp:355
update-images.h
h
Definition: update-images.py:53
CMap::FixSelectionArea
void FixSelectionArea(Vec2i &minpos, Vec2i &maxpos)
Definition: map.h:244
tile.h
color.h
CMap::HumanWallOnMap
bool HumanWallOnMap(const Vec2i &pos) const
Returns true, if human wall on the map tile field.
Definition: map.cpp:212
CMap::TilePosToMapPixelPos_TopLeft
PixelPos TilePosToMapPixelPos_TopLeft(const Vec2i &tilePos) const
convert tilepos coordonates into map pixel pos (take the top left of the tile)
Definition: map.cpp:180
CTileset
Tileset definition.
Definition: tileset.h:126
CMapField
Describes a field of the map.
Definition: tile.h:186
CMap::isMapInitialized
bool isMapInitialized
graphic for all the tiles
Definition: map.h:274
PlayerTypes
ENUM_CLASS PlayerTypes
Special: Use map supplied.
Definition: settings.h:118
Map
CMap Map
Definition: map.cpp:55
CFile
Definition: iolib.h:102
CUnit
The big unit structure.
Definition: unit.h:135
CMapInfo::MapUID
unsigned int MapUID
Same player->Side.
Definition: map.h:146
PreprocessMap
void PreprocessMap()
Preprocess map, for internal use.
Definition: map.cpp:288
MapUnmarkRadar
void MapUnmarkRadar(const CPlayer &player, const CUnit &unit, const Vec2i &pos, int w, int h, int range)
Definition: map.h:412
MapRefreshUnitsSight
void MapRefreshUnitsSight(const Vec2i &tilePos, const bool resetSight=false)
Mark/Unmark on vision table the Sight for the units around the tilePos.
Definition: unit.cpp:934
CGraphic
Definition: video.h:91
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.