$treeview $search $mathjax
Stratagus
2.2.6
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 // _________ __ __ 00002 // / _____// |_____________ _/ |______ ____ __ __ ______ 00003 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00004 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00005 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00006 // \/ \/ \//_____/ \/ 00007 // ______________________ ______________________ 00008 // T H E W A R B E G I N S 00009 // Stratagus - A free fantasy real time strategy game engine 00010 // 00012 // 00013 // (c) Copyright 1998-2006 by Vladi Shabanski, Lutz Sammer, and 00014 // Jimmy Salmon 00015 // 00016 // This program is free software; you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation; only version 2 of the License. 00019 // 00020 // This program is distributed in the hope that it will be useful, 00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 // GNU General Public License for more details. 00024 // 00025 // You should have received a copy of the GNU General Public License 00026 // along with this program; if not, write to the Free Software 00027 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00028 // 02111-1307, USA. 00029 // 00030 00031 #ifndef __MAP_H__ 00032 #define __MAP_H__ 00033 00035 00036 /*---------------------------------------------------------------------------- 00037 -- Documentation 00038 ----------------------------------------------------------------------------*/ 00039 00081 /*---------------------------------------------------------------------------- 00082 -- Includes 00083 ----------------------------------------------------------------------------*/ 00084 00085 #include <string> 00086 #include <vector> 00087 #include <algorithm> 00088 00089 #ifndef __UNIT_CACHE_H__ 00090 #include "unit_cache.h" 00091 #endif 00092 00093 #include "iocompat.h" 00094 00095 #ifndef __TILESET_H__ 00096 #include "tileset.h" 00097 #endif 00098 00099 #ifndef __PLAYER_H__ 00100 #include "player.h" 00101 #endif 00102 00103 #ifndef __MAP_TILE_H__ 00104 #include "tile.h" 00105 #endif 00106 00107 #include "vec2i.h" 00108 00109 /*---------------------------------------------------------------------------- 00110 -- Declarations 00111 ----------------------------------------------------------------------------*/ 00112 00113 class CGraphic; 00114 class CPlayer; 00115 class CFile; 00116 class CUnit; 00117 class CUnitType; 00118 00119 /*---------------------------------------------------------------------------- 00120 -- Map 00121 ----------------------------------------------------------------------------*/ 00122 00123 #define MaxMapWidth 256 00124 #define MaxMapHeight 256 00125 00126 // Not used until now: 00127 #define MapFieldSpeedMask 0x0007 00128 00129 #define MapFieldHuman 0x0008 00130 00131 #define MapFieldLandAllowed 0x0010 00132 #define MapFieldCoastAllowed 0x0020 00133 #define MapFieldWaterAllowed 0x0040 00134 #define MapFieldNoBuilding 0x0080 00135 00136 #define MapFieldUnpassable 0x0100 00137 //#define MapFieldWall 0x0200 00138 00139 #define MapFieldLandUnit 0x1000 00140 #define MapFieldAirUnit 0x2000 00141 #define MapFieldSeaUnit 0x4000 00142 #define MapFieldBuilding 0x8000 00143 00144 /*---------------------------------------------------------------------------- 00145 -- Map info structure 00146 ----------------------------------------------------------------------------*/ 00147 00151 class CMapInfo { 00152 public: 00153 std::string Description; 00154 std::string Filename; 00155 int MapWidth; 00156 int MapHeight; 00157 int PlayerType[PlayerMax]; 00158 int PlayerSide[PlayerMax]; 00159 unsigned int MapUID; 00160 00161 inline bool IsPointOnMap(int x, int y) const 00162 { 00163 return (x >= 0 && y >= 0 && x < MapWidth && y < MapHeight); 00164 } 00165 00166 bool IsPointOnMap(const Vec2i &pos) const 00167 { 00168 return IsPointOnMap(pos.x, pos.y); 00169 } 00170 00171 void Clear(); 00172 00173 }; 00174 00175 /*---------------------------------------------------------------------------- 00176 -- Map itself 00177 ----------------------------------------------------------------------------*/ 00178 00180 class CMap { 00181 public: 00182 inline unsigned int getIndex(int x, int y) const 00183 { 00184 return x + y * this->Info.MapWidth; 00185 } 00186 00187 unsigned int getIndex(const Vec2i &pos) const 00188 { 00189 return getIndex(pos.x, pos.y); 00190 } 00191 inline CMapField *Field(unsigned int index) const { 00192 return &this->Fields[index]; 00193 } 00194 00196 void Create(); 00198 void Init(); 00200 void Clean(); 00202 void CleanFogOfWar(); 00204 void ClearTile(unsigned short type, const Vec2i &pos); 00205 00215 unsigned short IsTileVisible(const CPlayer &player, const unsigned int index) const 00216 { 00217 const CMapField *const mf = &this->Fields[index]; 00218 unsigned short visiontype = mf->Visible[player.Index]; 00219 00220 if (visiontype > 1) { 00221 return visiontype; 00222 } 00223 if (player.SharedVision) { 00224 for (int i = 0; i < PlayerMax ; ++i) { 00225 if (player.SharedVision & (1 << i) && 00226 (Players[i].SharedVision & (1 << player.Index))) { 00227 if (mf->Visible[i] > 1) { 00228 return 2; 00229 } 00230 visiontype |= mf->Visible[i]; 00231 } 00232 } 00233 } 00234 if (visiontype) { 00235 return visiontype + (NoFogOfWar ? 1 : 0); 00236 } 00237 return 0; 00238 } 00239 00241 bool CheckMask(const unsigned int index, const int mask) const 00242 { 00243 return (this->Fields[index].Flags & mask) != 0; 00244 } 00245 00246 bool CheckMask(const Vec2i &pos, int mask) const 00247 { 00248 return CheckMask(getIndex(pos), mask); 00249 } 00250 00252 bool IsFieldExplored(const CPlayer &player, const unsigned int index) const 00253 { 00254 return this->Fields[index].IsExplored(player.Index); 00255 } 00256 00258 bool IsFieldVisible(const CPlayer &player, const unsigned int index) const 00259 { 00260 return IsTileVisible(player, index) > 1; 00261 } 00262 00263 unsigned short IsTileVisible(const CPlayer &player, const Vec2i &pos) const 00264 { 00265 return IsTileVisible(player, getIndex(pos)); 00266 } 00267 00269 bool IsFieldExplored(const CPlayer &player, const Vec2i &pos) 00270 { 00271 Assert(Info.IsPointOnMap(pos)); 00272 return IsFieldExplored(player, getIndex(pos)); 00273 } 00274 00275 00277 bool IsFieldVisible(const CPlayer &player, const Vec2i &pos) 00278 { 00279 return IsTileVisible(player, getIndex(pos)) > 1; 00280 } 00281 00282 00284 void MarkSeenTile(const unsigned int index); 00285 00287 void MarkSeenTile(const Vec2i &pos) 00288 { 00289 Assert(Info.IsPointOnMap(pos)); 00290 MarkSeenTile(getIndex(pos)); 00291 } 00292 00293 00295 void RegenerateForest(); 00297 void Reveal(); 00299 void Save(CFile *file) const; 00300 00302 inline CMapField *Field(int x, int y) const { 00303 return &this->Fields[x + y * this->Info.MapWidth]; 00304 } 00305 CMapField* Field(const Vec2i &pos) const 00306 { 00307 return Field(pos.x, pos.y); 00308 } 00309 00310 // 00311 // Wall 00312 // 00314 void HitWall(const Vec2i &pos, unsigned damage); 00316 void RemoveWall(const Vec2i &pos); 00318 void SetWall(const Vec2i &pos, int humanwall); 00319 00321 bool WallOnMap(const Vec2i &pos) const; 00323 bool HumanWallOnMap(const Vec2i &pos) const; 00325 bool OrcWallOnMap(const Vec2i &pos) const; 00326 00327 00328 // 00329 // Tile type. 00330 // 00331 00333 bool WaterOnMap(const unsigned int index) const 00334 { 00335 return CheckMask(index, MapFieldWaterAllowed); 00336 }; 00337 00345 bool WaterOnMap(const Vec2i &pos) const 00346 { 00347 Assert(Info.IsPointOnMap(pos)); 00348 return WaterOnMap(getIndex(pos)); 00349 } 00350 00352 bool CoastOnMap(const unsigned int index) const 00353 { 00354 return CheckMask(index, MapFieldCoastAllowed); 00355 }; 00356 00364 bool CoastOnMap(const Vec2i &pos) const 00365 { 00366 Assert(Info.IsPointOnMap(pos)); 00367 return CoastOnMap(getIndex(pos)); 00368 } 00369 00370 00372 bool ForestOnMap(const unsigned int index) const 00373 { 00374 return CheckMask(index, MapFieldForest); 00375 }; 00376 00384 bool ForestOnMap(const Vec2i& pos) const 00385 { 00386 Assert(Info.IsPointOnMap(pos)); 00387 return ForestOnMap(getIndex(pos)); 00388 } 00389 00390 00392 bool RockOnMap(const unsigned int index) const 00393 { 00394 return CheckMask(index, MapFieldRocks); 00395 }; 00396 00404 bool RockOnMap(const Vec2i& pos) const 00405 { 00406 Assert(Info.IsPointOnMap(pos)); 00407 return RockOnMap(getIndex(pos)); 00408 }; 00409 00410 //UnitCache 00411 00413 void Insert(CUnit &unit); 00414 00416 void Remove(CUnit &unit); 00417 00418 //Warning: we expect typical usage as xmin = x - range 00419 void FixSelectionArea(Vec2i &minpos, Vec2i &maxpos) 00420 { 00421 minpos.x = std::max<short>(0, minpos.x); 00422 minpos.y = std::max<short>(0, minpos.y); 00423 00424 maxpos.x = std::min<short>(maxpos.x, Info.MapWidth - 1); 00425 maxpos.y = std::min<short>(maxpos.y, Info.MapHeight - 1); 00426 } 00427 00429 int Select(int x1, int y1, int x2, int y2, CUnit *table[], 00430 const int tablesize = UnitMax); 00431 int SelectFixed(const Vec2i <pos, const Vec2i &rbpos, CUnit*table[], 00432 const int tablesize = UnitMax); 00433 00434 00435 // Select units on map tile. - helper funtion. don't use directly 00436 int Select(const Vec2i &pos, CUnit *table[], const int tablesize = UnitMax); 00437 00438 00439 private: 00441 void InitFogOfWar(); 00442 00444 void FixNeighbors(unsigned short type, int seen, const Vec2i &pos); 00446 void FixTile(unsigned short type, int seen, const Vec2i &pos); 00447 00449 void RegenerateForestTile(int x, int y); 00450 00451 00452 public: 00453 CMapField *Fields; 00454 00455 bool NoFogOfWar; 00456 00457 CTileset Tileset; 00458 char TileModelsFileName[PATH_MAX]; 00459 CGraphic *TileGraphic; 00460 static CGraphic *FogGraphic; 00461 00462 CMapInfo Info; 00463 }; 00464 00465 /*---------------------------------------------------------------------------- 00466 -- Variables 00467 ----------------------------------------------------------------------------*/ 00468 00469 extern CMap Map; 00470 extern char CurrentMapPath[1024]; 00471 00473 extern int FogOfWarOpacity; 00475 extern int FogOfWarColor[3]; 00477 extern int ForestRegeneration; 00479 extern int FlagRevealMap; 00481 extern int ReplayRevealMap; 00482 00483 /*---------------------------------------------------------------------------- 00484 -- Functions 00485 ----------------------------------------------------------------------------*/ 00486 #define MARKER_ON_INDEX 00487 // 00488 // in map_fog.c 00489 // 00491 #ifndef MARKER_ON_INDEX 00492 typedef void MapMarkerFunc(const CPlayer &player, const Vec2i &pos); 00493 #else 00494 typedef void MapMarkerFunc(const CPlayer &player, const unsigned int index); 00495 #endif 00496 00498 extern int MapFogFilterFlags(CPlayer &player, const Vec2i &pos, int mask); 00499 extern int MapFogFilterFlags(CPlayer &player, const unsigned int index, int mask); 00501 extern MapMarkerFunc MapMarkTileSight; 00503 extern MapMarkerFunc MapUnmarkTileSight; 00505 extern MapMarkerFunc MapMarkTileDetectCloak; 00507 extern MapMarkerFunc MapUnmarkTileDetectCloak; 00508 00510 extern void MapSight(const CPlayer &player, const Vec2i &pos, int w, 00511 int h, int range, MapMarkerFunc *marker); 00513 extern void UpdateFogOfWarChange(); 00514 00515 // 00516 // in map_radar.c 00517 // 00518 00520 extern MapMarkerFunc MapMarkTileRadar; 00521 00523 extern MapMarkerFunc MapUnmarkTileRadar; 00524 00526 extern MapMarkerFunc MapMarkTileRadarJammer; 00527 00529 extern MapMarkerFunc MapUnmarkTileRadarJammer; 00530 00531 00532 // 00533 // in map_wall.c 00534 // 00536 extern void MapFixSeenWallTile(const Vec2i &pos); 00538 extern void MapFixSeenWallNeighbors(const Vec2i &pos); 00540 extern void MapFixWallTile(const Vec2i &pos); 00541 00542 // 00543 // in script_map.c 00544 // 00546 extern void SetTile(int tile, const Vec2i &pos, int value = 0); 00547 00548 inline void SetTile(int tile, int x, int y, int value = 0) 00549 { 00550 const Vec2i pos = {x, y}; 00551 SetTile(tile, pos, value); 00552 } 00553 00555 extern void MapCclRegister(); 00556 00557 // 00558 // mixed sources 00559 // 00561 extern int SaveStratagusMap(const std::string &filename, CMap &map, int writeTerrain); 00562 00563 00565 extern void LoadStratagusMapInfo(const std::string &mapname); 00566 00568 extern bool CheckedCanMoveToMask(const Vec2i &pos, int mask); 00570 extern bool UnitTypeCanBeAt(const CUnitType &type, const Vec2i &pos); 00572 extern bool UnitCanBeAt(const CUnit &unit, const Vec2i &pos); 00573 00575 extern void PreprocessMap(); 00576 00577 // in unit.c 00578 00580 void MapMarkUnitSight(CUnit &unit); 00582 void MapUnmarkUnitSight(CUnit &unit); 00583 00584 void MapMarkUnitGuard(CUnit &unit); 00585 void MapUnmarkUnitGuard(CUnit &unit); 00586 00587 /*---------------------------------------------------------------------------- 00588 -- Defines 00589 ----------------------------------------------------------------------------*/ 00590 00592 inline bool CanMoveToMask(const Vec2i &pos, int mask) { 00593 return !Map.CheckMask(pos, mask); 00594 } 00595 00597 inline void MapMarkRadar(const CPlayer &player, const Vec2i &pos, int w, int h, int range) { 00598 MapSight(player, pos, w, h, range, MapMarkTileRadar); 00599 } 00600 inline void MapUnmarkRadar(const CPlayer &player, const Vec2i &pos, int w, int h, int range) { 00601 MapSight(player, pos, w, h, range, MapUnmarkTileRadar); 00602 } 00604 inline void MapMarkRadarJammer(const CPlayer &player, const Vec2i &pos, int w, int h, int range) { 00605 MapSight(player, pos, w, h, range, MapMarkTileRadarJammer); 00606 } 00607 inline void MapUnmarkRadarJammer(const CPlayer &player, const Vec2i &pos, int w, int h, int range) { 00608 MapSight(player, pos, w, h, range, MapUnmarkTileRadarJammer); 00609 } 00610 00612 00613 #endif // !__MAP_H__