00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032
00033
00034
00035
00036 #include <string.h>
00037
00038 #include "stratagus.h"
00039 #include "unittype.h"
00040 #include "map.h"
00041 #include "minimap.h"
00042 #include "player.h"
00043 #include "unit.h"
00044 #include "unit_cache.h"
00045 #include "ui.h"
00046 #include "script.h"
00047
00048
00049
00050
00051
00052 CMap Map;
00053 int FlagRevealMap;
00054 int ReplayRevealMap;
00055 char CurrentMapPath[1024];
00056
00060 int TileSizeX = 32;
00061
00065 int TileSizeY = 32;
00066
00067
00068
00069
00070
00074 void CMap::Reveal(void)
00075 {
00076 int x;
00077
00078
00079
00080
00081 for (x = 0; x < this->Info.MapWidth; ++x) {
00082 for (int y = 0; y < this->Info.MapHeight; ++y) {
00083 for (int p = 0; p < PlayerMax; ++p) {
00084 if (!this->Field(x, y)->Visible[p]) {
00085 this->Field(x, y)->Visible[p] = 1;
00086 }
00087 }
00088 }
00089 }
00090
00091
00092
00093 for (x = 0; x < NumUnits; ++x) {
00094
00095
00096
00097 if (Units[x]->Player->Type == PlayerNeutral) {
00098 for (int p = 0; p < PlayerMax; ++p) {
00099 if (Players[p].Type != PlayerNobody &&
00100 (!(Units[x]->Seen.ByPlayer & (1 << p)))) {
00101 UnitGoesOutOfFog(Units[x], Players + p);
00102 UnitGoesUnderFog(Units[x], Players + p);
00103 }
00104 }
00105 }
00106 UnitCountSeen(Units[x]);
00107 }
00108 }
00109
00110
00111
00112
00113
00122 bool CMap::WaterOnMap(int tx, int ty) const
00123 {
00124 Assert(tx >= 0 && ty >= 0 && tx < Info.MapWidth && ty < Info.MapHeight);
00125 return (this->Field(tx, ty)->Flags & MapFieldWaterAllowed) != 0;
00126 }
00127
00136 bool CMap::CoastOnMap(int tx, int ty) const
00137 {
00138 Assert(tx >= 0 && ty >= 0 && tx < Info.MapWidth && ty < Info.MapHeight);
00139 return (this->Field(tx, ty)->Flags & MapFieldCoastAllowed) != 0;
00140
00141 }
00142
00152 bool CheckedCanMoveToMask(int x, int y, int mask)
00153 {
00154 if (x < 0 || y < 0 || x >= Map.Info.MapWidth || y >= Map.Info.MapHeight) {
00155 return false;
00156 }
00157
00158 return !(Map.Field(x, y)->Flags & mask);
00159 }
00160
00170 bool UnitTypeCanBeAt(const CUnitType *type, int x, int y)
00171 {
00172 Assert(type);
00173 for (int addx = 0; addx < type->TileWidth; ++addx) {
00174 for (int addy = 0; addy < type->TileHeight; ++addy) {
00175 if (!CheckedCanMoveToMask(x + addx, y + addy, type->MovementMask)) {
00176 return false;
00177 }
00178 }
00179 }
00180 return true;
00181 }
00182
00192 bool UnitCanBeAt(const CUnit *unit, int x, int y)
00193 {
00194 Assert(unit);
00195 return UnitTypeCanBeAt(unit->Type, x, y);
00196 }
00197
00203 void FreeMapInfo(CMapInfo *info)
00204 {
00205 if (info) {
00206 info->Description.clear();
00207 info->Filename.clear();
00208 info->MapWidth = info->MapHeight = 0;
00209 memset(info->PlayerSide, 0, sizeof(info->PlayerSide));
00210 memset(info->PlayerType, 0, sizeof(info->PlayerType));
00211 info->MapUID = 0;
00212 }
00213 }
00214
00218 void CMap::Init()
00219 {
00220 InitFogOfWar();
00221 Map.PatchManager.load();
00222 }
00223
00227 void CMap::Create()
00228 {
00229 Assert(!this->Fields);
00230
00231 this->Fields = new CMapField[this->Info.MapWidth * this->Info.MapHeight];
00232 this->Visible[0] = new unsigned[this->Info.MapWidth * this->Info.MapHeight / 2];
00233 memset(this->Visible[0], 0, this->Info.MapWidth * this->Info.MapHeight / 2 * sizeof(unsigned));
00234 UnitCache.Init(this->Info.MapWidth, this->Info.MapHeight);
00235 }
00236
00240 void CMap::Clean(void)
00241 {
00242 delete[] this->Fields;
00243 delete[] this->Visible[0];
00244
00245 FreeMapInfo(&this->Info);
00246 this->Fields = NULL;
00247 memset(this->Visible, 0, sizeof(this->Visible));
00248 this->NoFogOfWar = false;
00249
00250 this->PatchManager.clear();
00251
00252 FlagRevealMap = 0;
00253 ReplayRevealMap = 0;
00254
00255 UI.Minimap.Destroy();
00256 }
00257
00258
00259
00260
00261
00262
00268 void LoadStratagusMapInfo(const std::string &mapname)
00269 {
00270
00271 size_t loc = mapname.find(".smp");
00272 if (loc != std::string::npos) {
00273 Map.Info.Filename = mapname;
00274 Map.Info.Filename.replace(loc, 4, ".sms");
00275 }
00276
00277 LuaLoadFile(mapname);
00278 }
00279