| 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_fog.cpp - The map fog of war handling. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2007 by Lutz Sammer, Vladi Shabanski, |
| 13 |
// Russell Smith, and Jimmy Salmon |
| 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 "stratagus.h" |
| 37 |
#include "player.h" |
| 38 |
#include "unittype.h" |
| 39 |
#include "unit.h" |
| 40 |
#include "unit_cache.h" |
| 41 |
#include "map.h" |
| 42 |
#include "ui.h" |
| 43 |
#include "../video/intern_video.h" |
| 44 |
|
| 45 |
/*---------------------------------------------------------------------------- |
| 46 |
-- Variables |
| 47 |
----------------------------------------------------------------------------*/ |
| 48 |
|
| 49 |
static const int FogOfWarOpacity = 128; /// Fog of war Opacity. |
| 50 |
CGraphic *CMap::FogGraphic; |
| 51 |
|
| 52 |
/** |
| 53 |
** Mapping for fog of war tiles. |
| 54 |
*/ |
| 55 |
static const int FogTable[16] = { |
| 56 |
0,11,10, 2, 13, 6, 14, 3, 12, 15, 4, 1, 8, 9, 7, 0, |
| 57 |
}; |
| 58 |
|
| 59 |
unsigned char *VisionTable[3]; |
| 60 |
int *VisionLookup; |
| 61 |
|
| 62 |
static unsigned short *VisibleTable; |
| 63 |
|
| 64 |
static SDL_Surface *OnlyFogSurface; |
| 65 |
static CGraphic *AlphaFogG; |
| 66 |
|
| 67 |
/*---------------------------------------------------------------------------- |
| 68 |
-- Functions |
| 69 |
----------------------------------------------------------------------------*/ |
| 70 |
|
| 71 |
/** |
| 72 |
** Find out if a field is seen (By player, or by shared vision) |
| 73 |
** This function will return > 1 with no fog of war. |
| 74 |
** |
| 75 |
** @param player Player to check for. |
| 76 |
** @param x X tile to check. |
| 77 |
** @param y Y tile to check. |
| 78 |
** |
| 79 |
** @return 0 unexplored, 1 explored, > 1 visible. |
| 80 |
*/ |
| 81 |
unsigned short CMap::IsTileVisible(const CPlayer *player, int x, int y) const |
| 82 |
{ |
| 83 |
unsigned short visiontype; |
| 84 |
unsigned short *visible; |
| 85 |
|
| 86 |
visible = this->Field(x, y)->Visible; |
| 87 |
visiontype = visible[player->Index]; |
| 88 |
|
| 89 |
if (visiontype > 1) { |
| 90 |
return visiontype; |
| 91 |
} |
| 92 |
if (!player->SharedVision) { |
| 93 |
if (visiontype) { |
| 94 |
return visiontype + (this->NoFogOfWar ? 1 : 0); |
| 95 |
} |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
for (int i = 0; i < PlayerMax ; ++i) { |
| 100 |
if (player->SharedVision & (1 << i) && |
| 101 |
(Players[i].SharedVision & (1 << player->Index))) { |
| 102 |
if (visible[i] > 1) { |
| 103 |
return 2; |
| 104 |
} |
| 105 |
visiontype |= visible[i]; |
| 106 |
} |
| 107 |
} |
| 108 |
if (visiontype) { |
| 109 |
return visiontype + (this->NoFogOfWar ? 1 : 0); |
| 110 |
} |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
** Find out what the tile flags are a tile is covered by fog |
| 116 |
** |
| 117 |
** @param player player who is doing operation |
| 118 |
** @param x X map location |
| 119 |
** @param y Y map location |
| 120 |
** @param mask input mask to filter |
| 121 |
** |
| 122 |
** @return Filtered mask after taking fog into account |
| 123 |
*/ |
| 124 |
int MapFogFilterFlags(CPlayer *player, int x, int y, int mask) |
| 125 |
{ |
| 126 |
int nunits; |
| 127 |
int unitcount; |
| 128 |
int fogmask; |
| 129 |
CUnit *table[UnitMax]; |
| 130 |
|
| 131 |
// Calculate Mask for tile with fog |
| 132 |
if (x < 0 || y < 0 || x >= Map.Info.MapWidth || y >= Map.Info.MapHeight) { |
| 133 |
return mask; |
| 134 |
} |
| 135 |
|
| 136 |
nunits = UnitCache.Select(x, y, table, UnitMax); |
| 137 |
fogmask = -1; |
| 138 |
unitcount = 0; |
| 139 |
while (unitcount < nunits) { |
| 140 |
if (!table[unitcount]->IsVisibleAsGoal(player)) { |
| 141 |
fogmask &= ~table[unitcount]->Type->FieldFlags; |
| 142 |
} |
| 143 |
++unitcount; |
| 144 |
} |
| 145 |
return mask & fogmask; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
** Mark a tile's sight. (Explore and make visible.) |
| 150 |
** |
| 151 |
** @param player Player to mark sight. |
| 152 |
** @param x X tile to mark. |
| 153 |
** @param y Y tile to mark. |
| 154 |
*/ |
| 155 |
void MapMarkTileSight(const CPlayer *player, int x, int y) |
| 156 |
{ |
| 157 |
Assert(0 <= x && x < Map.Info.MapWidth); |
| 158 |
Assert(0 <= y && y < Map.Info.MapHeight); |
| 159 |
|
| 160 |
unsigned short *v; |
| 161 |
|
| 162 |
v = &Map.Field(x, y)->Visible[player->Index]; |
| 163 |
if (*v == 0 || *v == 1) { // Unexplored or unseen |
| 164 |
// When there is no fog only unexplored tiles are marked. |
| 165 |
if (!Map.NoFogOfWar || *v == 0) { |
| 166 |
UnitsOnTileMarkSeen(player, x, y); |
| 167 |
} |
| 168 |
*v = 2; |
| 169 |
return; |
| 170 |
} |
| 171 |
Assert(*v != 65535); |
| 172 |
++*v; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
** Unmark a tile's sight. (Explore and make visible.) |
| 177 |
** |
| 178 |
** @param player Player to mark sight. |
| 179 |
** @param x X tile to mark. |
| 180 |
** @param y Y tile to mark. |
| 181 |
*/ |
| 182 |
void MapUnmarkTileSight(const CPlayer *player, int x, int y) |
| 183 |
{ |
| 184 |
Assert(0 <= x && x < Map.Info.MapWidth); |
| 185 |
Assert(0 <= y && y < Map.Info.MapHeight); |
| 186 |
|
| 187 |
unsigned short *v; |
| 188 |
|
| 189 |
v = &Map.Field(x, y)->Visible[player->Index]; |
| 190 |
switch (*v) { |
| 191 |
case 0: // Unexplored |
| 192 |
case 1: |
| 193 |
// We are at minimum, don't do anything shouldn't happen. |
| 194 |
Assert(0); |
| 195 |
break; |
| 196 |
case 2: |
| 197 |
// When there is NoFogOfWar units never get unmarked. |
| 198 |
if (!Map.NoFogOfWar) { |
| 199 |
UnitsOnTileUnmarkSeen(player, x, y); |
| 200 |
} |
| 201 |
// FALL THROUGH |
| 202 |
default: // seen -> seen |
| 203 |
--*v; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
** Mark the sight of unit. (Explore and make visible.) |
| 210 |
** |
| 211 |
** @param player player to mark the sight for (not unit owner) |
| 212 |
** @param x x location to mark |
| 213 |
** @param y y location to mark |
| 214 |
** @param w width to mark, in square |
| 215 |
** @param h height to mark, in square |
| 216 |
** @param range Radius to mark. |
| 217 |
** @param marker Function to mark or unmark sight |
| 218 |
*/ |
| 219 |
void MapSight(const CPlayer *player, int x, int y, int w, int h, int range, |
| 220 |
MapMarkerFunc *marker) |
| 221 |
{ |
| 222 |
int mx; |
| 223 |
int my; |
| 224 |
int cx[4]; |
| 225 |
int cy[4]; |
| 226 |
int steps; |
| 227 |
int cycle; |
| 228 |
|
| 229 |
// Units under construction have no sight range. |
| 230 |
if (!range) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
// Mark Horizontal sight for unit |
| 235 |
for (mx = x - range; mx < x + range + w; ++mx) { |
| 236 |
for (my = y; my < y + h; ++my) { |
| 237 |
if (mx >= 0 && mx < Map.Info.MapWidth) { |
| 238 |
marker(player, mx, my); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// Mark vertical sight for unit (don't remark self) (above unit) |
| 244 |
for (my = y - range; my < y; ++my) { |
| 245 |
for (mx = x; mx < x + w; ++mx) { |
| 246 |
if (my >= 0 && my < Map.Info.MapHeight) { |
| 247 |
marker(player, mx, my); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// Mark vertical sight for unit (don't remark self) (below unit) |
| 253 |
for (my = y + h; my < y + range + h; ++my) { |
| 254 |
for (mx = x; mx < x + w; ++mx) { |
| 255 |
if (my >= 0 && my < Map.Info.MapHeight) { |
| 256 |
marker(player, mx, my); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
// Now the cross has been marked, need to use loop to mark in circle |
| 262 |
steps = 0; |
| 263 |
while (VisionTable[0][steps] <= range) { |
| 264 |
// 0 - Top right Quadrant |
| 265 |
cx[0] = x + w - 1; |
| 266 |
cy[0] = y - VisionTable[0][steps]; |
| 267 |
// 1 - Top left Quadrant |
| 268 |
cx[1] = x; |
| 269 |
cy[1] = y - VisionTable[0][steps]; |
| 270 |
// 2 - Bottom Left Quadrant |
| 271 |
cx[2] = x; |
| 272 |
cy[2] = y + VisionTable[0][steps] + h - 1; |
| 273 |
// 3 - Bottom Right Quadrant |
| 274 |
cx[3] = x + w - 1; |
| 275 |
cy[3] = y + VisionTable[0][steps] + h - 1; |
| 276 |
// loop for steps |
| 277 |
++steps; // Increment past info pointer |
| 278 |
while (VisionTable[1][steps] != 0 || VisionTable[2][steps] != 0) { |
| 279 |
// Loop through for repeat cycle |
| 280 |
cycle = 0; |
| 281 |
while (cycle++ < VisionTable[0][steps]) { |
| 282 |
cx[0] += VisionTable[1][steps]; |
| 283 |
cy[0] += VisionTable[2][steps]; |
| 284 |
cx[1] -= VisionTable[1][steps]; |
| 285 |
cy[1] += VisionTable[2][steps]; |
| 286 |
cx[2] -= VisionTable[1][steps]; |
| 287 |
cy[2] -= VisionTable[2][steps]; |
| 288 |
cx[3] += VisionTable[1][steps]; |
| 289 |
cy[3] -= VisionTable[2][steps]; |
| 290 |
if (cx[0] < Map.Info.MapWidth && cy[0] >= 0) { |
| 291 |
marker(player, cx[0], cy[0]); |
| 292 |
} |
| 293 |
if (cx[1] >= 0 && cy[1] >= 0) { |
| 294 |
marker(player, cx[1], cy[1]); |
| 295 |
} |
| 296 |
if (cx[2] >= 0 && cy[2] < Map.Info.MapHeight) { |
| 297 |
marker(player, cx[2], cy[2]); |
| 298 |
} |
| 299 |
if (cx[3] < Map.Info.MapWidth && cy[3] < Map.Info.MapHeight) { |
| 300 |
marker(player, cx[3], cy[3]); |
| 301 |
} |
| 302 |
} |
| 303 |
++steps; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
** Update fog of war. |
| 310 |
*/ |
| 311 |
void UpdateFogOfWarChange(void) |
| 312 |
{ |
| 313 |
// |
| 314 |
// Global seen recount. |
| 315 |
// |
| 316 |
for (int x = 0; x < NumUnits; ++x) { |
| 317 |
UnitCountSeen(Units[x]); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/*---------------------------------------------------------------------------- |
| 322 |
-- Draw fog solid |
| 323 |
----------------------------------------------------------------------------*/ |
| 324 |
|
| 325 |
/** |
| 326 |
** Draw only fog of war |
| 327 |
** |
| 328 |
** @param x X position into video memory |
| 329 |
** @param y Y position into video memory |
| 330 |
*/ |
| 331 |
void VideoDrawOnlyFog(int x, int y) |
| 332 |
{ |
| 333 |
if (!UseOpenGL) { |
| 334 |
int oldx; |
| 335 |
int oldy; |
| 336 |
SDL_Rect srect; |
| 337 |
SDL_Rect drect; |
| 338 |
|
| 339 |
srect.x = 0; |
| 340 |
srect.y = 0; |
| 341 |
srect.w = OnlyFogSurface->w; |
| 342 |
srect.h = OnlyFogSurface->h; |
| 343 |
|
| 344 |
oldx = x; |
| 345 |
oldy = y; |
| 346 |
CLIP_RECTANGLE(x, y, srect.w, srect.h); |
| 347 |
srect.x += x - oldx; |
| 348 |
srect.y += y - oldy; |
| 349 |
|
| 350 |
drect.x = x; |
| 351 |
drect.y = y; |
| 352 |
|
| 353 |
SDL_BlitSurface(OnlyFogSurface, &srect, TheScreen, &drect); |
| 354 |
} else { |
| 355 |
Video.FillRectangleClip(Video.MapRGBA(0, 0, 0, 0, FogOfWarOpacity), |
| 356 |
x, y, TileSizeX, TileSizeY); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
/*---------------------------------------------------------------------------- |
| 361 |
-- Old version correct working but not 100% original |
| 362 |
----------------------------------------------------------------------------*/ |
| 363 |
|
| 364 |
/** |
| 365 |
** Draw fog of war tile. |
| 366 |
** |
| 367 |
** @param sx Offset into fields to current tile. |
| 368 |
** @param sy Start of the current row. |
| 369 |
** @param dx X position into video memory. |
| 370 |
** @param dy Y position into video memory. |
| 371 |
*/ |
| 372 |
static void DrawFogOfWarTile(int sx, int sy, int dx, int dy) |
| 373 |
{ |
| 374 |
int w; |
| 375 |
int tile; |
| 376 |
int tile2; |
| 377 |
int x; |
| 378 |
int y; |
| 379 |
|
| 380 |
#define IsMapFieldExploredTable(x, y) \ |
| 381 |
(VisibleTable[(y) * Map.Info.MapWidth + (x)]) |
| 382 |
#define IsMapFieldVisibleTable(x, y) \ |
| 383 |
(VisibleTable[(y) * Map.Info.MapWidth + (x)] > 1) |
| 384 |
|
| 385 |
w = Map.Info.MapWidth; |
| 386 |
tile = tile2 = 0; |
| 387 |
x = sx - sy; |
| 388 |
y = sy / Map.Info.MapWidth; |
| 389 |
|
| 390 |
// |
| 391 |
// Which Tile to draw for fog |
| 392 |
// |
| 393 |
// Investigate tiles around current tile |
| 394 |
// 1 2 3 |
| 395 |
// 4 * 5 |
| 396 |
// 6 7 8 |
| 397 |
|
| 398 |
if (sy) { |
| 399 |
if (sx != sy) { |
| 400 |
if (!IsMapFieldExploredTable(x - 1, y - 1)) { |
| 401 |
tile2 |= 2; |
| 402 |
tile |= 2; |
| 403 |
} else if (!IsMapFieldVisibleTable(x - 1, y - 1)) { |
| 404 |
tile |= 2; |
| 405 |
} |
| 406 |
} |
| 407 |
if (!IsMapFieldExploredTable(x, y - 1)) { |
| 408 |
tile2 |= 3; |
| 409 |
tile |= 3; |
| 410 |
} else if (!IsMapFieldVisibleTable(x, y - 1)) { |
| 411 |
tile |= 3; |
| 412 |
} |
| 413 |
if (sx != sy + w - 1) { |
| 414 |
if (!IsMapFieldExploredTable(x + 1, y - 1)) { |
| 415 |
tile2 |= 1; |
| 416 |
tile |= 1; |
| 417 |
} else if (!IsMapFieldVisibleTable(x + 1, y - 1)) { |
| 418 |
tile |= 1; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (sx != sy) { |
| 424 |
if (!IsMapFieldExploredTable(x - 1, y)) { |
| 425 |
tile2 |= 10; |
| 426 |
tile |= 10; |
| 427 |
} else if (!IsMapFieldVisibleTable(x - 1, y)) { |
| 428 |
tile |= 10; |
| 429 |
} |
| 430 |
} |
| 431 |
if (sx != sy + w - 1) { |
| 432 |
if (!IsMapFieldExploredTable(x + 1, y)) { |
| 433 |
tile2 |= 5; |
| 434 |
tile |= 5; |
| 435 |
} else if (!IsMapFieldVisibleTable(x + 1, y)) { |
| 436 |
tile |= 5; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if (sy + w < Map.Info.MapHeight * w) { |
| 441 |
if (sx != sy) { |
| 442 |
if (!IsMapFieldExploredTable(x - 1, y + 1)) { |
| 443 |
tile2 |= 8; |
| 444 |
tile |= 8; |
| 445 |
} else if (!IsMapFieldVisibleTable(x - 1, y + 1)) { |
| 446 |
tile |= 8; |
| 447 |
} |
| 448 |
} |
| 449 |
if (!IsMapFieldExploredTable(x, y + 1)) { |
| 450 |
tile2 |= 12; |
| 451 |
tile |= 12; |
| 452 |
} else if (!IsMapFieldVisibleTable(x, y + 1)) { |
| 453 |
tile |= 12; |
| 454 |
} |
| 455 |
if (sx != sy + w - 1) { |
| 456 |
if (!IsMapFieldExploredTable(x + 1, y + 1)) { |
| 457 |
tile2 |= 4; |
| 458 |
tile |= 4; |
| 459 |
} else if (!IsMapFieldVisibleTable(x + 1, y + 1)) { |
| 460 |
tile |= 4; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
tile = FogTable[tile]; |
| 466 |
tile2 = FogTable[tile2]; |
| 467 |
|
| 468 |
if (ReplayRevealMap) { |
| 469 |
tile2 = 0; |
| 470 |
tile = 0; |
| 471 |
} |
| 472 |
|
| 473 |
if (IsMapFieldVisibleTable(x, y) || ReplayRevealMap) { |
| 474 |
if (tile && tile != tile2) { |
| 475 |
if (UseOpenGL) { |
| 476 |
Map.FogGraphic->DrawFrameClipTrans(tile, dx, dy, FogOfWarOpacity); |
| 477 |
} else { |
| 478 |
AlphaFogG->DrawFrameClip(tile, dx, dy); |
| 479 |
} |
| 480 |
} |
| 481 |
} else { |
| 482 |
VideoDrawOnlyFog(dx, dy); |
| 483 |
} |
| 484 |
if (tile2) { |
| 485 |
Map.FogGraphic->DrawFrameClip(tile2, dx, dy); |
| 486 |
} |
| 487 |
|
| 488 |
#undef IsMapFieldExploredTable |
| 489 |
#undef IsMapFieldVisibleTable |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
** Draw the map fog of war. |
| 494 |
*/ |
| 495 |
void CViewport::DrawMapFogOfWar() const |
| 496 |
{ |
| 497 |
int sx, sy; |
| 498 |
int dx, dy; |
| 499 |
int ex, ey; |
| 500 |
int mx, my; |
| 501 |
|
| 502 |
// flags must redraw or not |
| 503 |
if (ReplayRevealMap) { |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
sx = std::max(MapX - 1, 0); |
| 508 |
ex = std::min(MapX + MapWidth + 1, Map.Info.MapWidth); |
| 509 |
my = std::max(MapY - 1, 0); |
| 510 |
ey = std::min(MapY + MapHeight + 1, Map.Info.MapHeight); |
| 511 |
|
| 512 |
// Update for visibility all tile in viewport |
| 513 |
// and 1 tile around viewport (for fog-of-war connection display) |
| 514 |
for (; my < ey; ++my) { |
| 515 |
for (mx = sx; mx < ex; ++mx) { |
| 516 |
VisibleTable[my * Map.Info.MapWidth + mx] = Map.IsTileVisible(ThisPlayer, mx, my); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
ex = EndX; |
| 521 |
sy = MapY * Map.Info.MapWidth; |
| 522 |
dy = Y - OffsetY; |
| 523 |
ey = EndY; |
| 524 |
|
| 525 |
while (dy <= ey) { |
| 526 |
sx = MapX + sy; |
| 527 |
dx = X - OffsetX; |
| 528 |
while (dx <= ex) { |
| 529 |
mx = (dx - X + OffsetX) / TileSizeX + MapX; |
| 530 |
my = (dy - Y + OffsetY) / TileSizeY + MapY; |
| 531 |
if (VisibleTable[my * Map.Info.MapWidth + mx]) { |
| 532 |
DrawFogOfWarTile(sx, sy, dx, dy); |
| 533 |
} else { |
| 534 |
Video.FillRectangleClip(ColorBlack, dx, dy, TileSizeX, TileSizeY); |
| 535 |
} |
| 536 |
++sx; |
| 537 |
dx += TileSizeX; |
| 538 |
} |
| 539 |
sy += Map.Info.MapWidth; |
| 540 |
dy += TileSizeY; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
** Initialize the fog of war. |
| 546 |
** Build tables, setup functions. |
| 547 |
*/ |
| 548 |
void CMap::InitFogOfWar(void) |
| 549 |
{ |
| 550 |
Uint8 r, g, b; |
| 551 |
Uint32 color; |
| 552 |
SDL_Surface *s; |
| 553 |
|
| 554 |
FogGraphic->Load(); |
| 555 |
|
| 556 |
if (!UseOpenGL) { |
| 557 |
if (!AlphaFogG) { |
| 558 |
// |
| 559 |
// Generate Only Fog surface. |
| 560 |
// |
| 561 |
s = SDL_CreateRGBSurface(SDL_SWSURFACE, TileSizeX, TileSizeY, |
| 562 |
32, RMASK, GMASK, BMASK, AMASK); |
| 563 |
|
| 564 |
SDL_GetRGB(ColorBlack, TheScreen->format, &r, &g, &b); |
| 565 |
color = Video.MapRGB(s->format, r, g, b); |
| 566 |
|
| 567 |
SDL_FillRect(s, NULL, color); |
| 568 |
OnlyFogSurface = SDL_DisplayFormat(s); |
| 569 |
SDL_SetAlpha(OnlyFogSurface, SDL_SRCALPHA | SDL_RLEACCEL, FogOfWarOpacity); |
| 570 |
SDL_FreeSurface(s); |
| 571 |
|
| 572 |
// |
| 573 |
// Generate Alpha Fog surface. |
| 574 |
// |
| 575 |
if (FogGraphic->Surface->format->BytesPerPixel == 1) { |
| 576 |
s = SDL_DisplayFormat(FogGraphic->Surface); |
| 577 |
SDL_SetAlpha(s, SDL_SRCALPHA | SDL_RLEACCEL, FogOfWarOpacity); |
| 578 |
} else { |
| 579 |
int i; |
| 580 |
int j; |
| 581 |
Uint32 c; |
| 582 |
Uint8 a; |
| 583 |
SDL_PixelFormat *f; |
| 584 |
|
| 585 |
// Copy the top row to a new surface |
| 586 |
f = FogGraphic->Surface->format; |
| 587 |
s = SDL_CreateRGBSurface(SDL_SWSURFACE, FogGraphic->Surface->w, TileSizeY, |
| 588 |
f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask); |
| 589 |
SDL_LockSurface(s); |
| 590 |
SDL_LockSurface(FogGraphic->Surface); |
| 591 |
for (i = 0; i < s->h; ++i) { |
| 592 |
memcpy((Uint8 *)s->pixels + i * s->pitch, |
| 593 |
(Uint8 *)FogGraphic->Surface->pixels + i * FogGraphic->Surface->pitch, |
| 594 |
FogGraphic->Surface->w * f->BytesPerPixel); |
| 595 |
} |
| 596 |
SDL_UnlockSurface(s); |
| 597 |
SDL_UnlockSurface(FogGraphic->Surface); |
| 598 |
|
| 599 |
// Convert any non-transparent pixels to use FogOfWarOpacity as alpha |
| 600 |
SDL_LockSurface(s); |
| 601 |
for (j = 0; j < s->h; ++j) { |
| 602 |
for (i = 0; i < s->w; ++i) { |
| 603 |
c = *(Uint32 *)&((Uint8*)s->pixels)[i * 4 + j * s->pitch]; |
| 604 |
Video.GetRGBA(c, s->format, &r, &g, &b, &a); |
| 605 |
if (a) { |
| 606 |
c = Video.MapRGBA(s->format, r, g, b, FogOfWarOpacity); |
| 607 |
*(Uint32 *)&((Uint8*)s->pixels)[i * 4 + j * s->pitch] = c; |
| 608 |
} |
| 609 |
|
| 610 |
} |
| 611 |
} |
| 612 |
SDL_UnlockSurface(s); |
| 613 |
} |
| 614 |
AlphaFogG = CGraphic::New(""); |
| 615 |
AlphaFogG->Surface = s; |
| 616 |
AlphaFogG->Width = TileSizeX; |
| 617 |
AlphaFogG->Height = TileSizeY; |
| 618 |
AlphaFogG->GraphicWidth = s->w; |
| 619 |
AlphaFogG->GraphicHeight = s->h; |
| 620 |
AlphaFogG->NumFrames = 1; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
delete[] VisibleTable; |
| 625 |
VisibleTable = new unsigned short[Info.MapWidth * Info.MapHeight]; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
** Cleanup the fog of war. |
| 630 |
*/ |
| 631 |
void CMap::CleanFogOfWar() |
| 632 |
{ |
| 633 |
delete[] VisibleTable; |
| 634 |
VisibleTable = NULL; |
| 635 |
|
| 636 |
CGraphic::Free(Map.FogGraphic); |
| 637 |
FogGraphic = NULL; |
| 638 |
|
| 639 |
if (!UseOpenGL) { |
| 640 |
if (OnlyFogSurface) { |
| 641 |
SDL_FreeSurface(OnlyFogSurface); |
| 642 |
OnlyFogSurface = NULL; |
| 643 |
} |
| 644 |
CGraphic::Free(AlphaFogG); |
| 645 |
AlphaFogG = NULL; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
** Initialize Vision and Goal Tables. |
| 651 |
*/ |
| 652 |
void InitVisionTable(void) |
| 653 |
{ |
| 654 |
int *visionlist; |
| 655 |
int maxsize; |
| 656 |
int sizex; |
| 657 |
int sizey; |
| 658 |
int maxsearchsize; |
| 659 |
int i; |
| 660 |
int VisionTablePosition; |
| 661 |
int marker; |
| 662 |
int direction; |
| 663 |
int right; |
| 664 |
int up; |
| 665 |
int repeat; |
| 666 |
|
| 667 |
// Initialize Visiontable to large size, can't be more entries than tiles. |
| 668 |
VisionTable[0] = new unsigned char[MaxMapWidth * MaxMapWidth]; |
| 669 |
VisionTable[1] = new unsigned char[MaxMapWidth * MaxMapWidth]; |
| 670 |
VisionTable[2] = new unsigned char[MaxMapWidth * MaxMapWidth]; |
| 671 |
|
| 672 |
VisionLookup = new int[MaxMapWidth + 2]; |
| 673 |
|
| 674 |
visionlist = new int[MaxMapWidth * MaxMapWidth]; |
| 675 |
//*2 as diagonal distance is longer |
| 676 |
|
| 677 |
maxsize = MaxMapWidth; |
| 678 |
maxsearchsize = MaxMapWidth; |
| 679 |
// Fill in table of map size |
| 680 |
for (sizex = 0; sizex < maxsize; ++sizex) { |
| 681 |
for (sizey = 0; sizey < maxsize; ++sizey) { |
| 682 |
visionlist[sizey * maxsize + sizex] = isqrt(sizex * sizex + sizey * sizey); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
VisionLookup[0] = 0; |
| 687 |
i = 1; |
| 688 |
VisionTablePosition = 0; |
| 689 |
while (i < maxsearchsize) { |
| 690 |
// Set Lookup Table |
| 691 |
VisionLookup[i] = VisionTablePosition; |
| 692 |
// Put in Null Marker |
| 693 |
VisionTable[0][VisionTablePosition] = i; |
| 694 |
VisionTable[1][VisionTablePosition] = 0; |
| 695 |
VisionTable[2][VisionTablePosition] = 0; |
| 696 |
++VisionTablePosition; |
| 697 |
|
| 698 |
|
| 699 |
// find i in left column |
| 700 |
marker = maxsize * i; |
| 701 |
direction = 0; |
| 702 |
right = 0; |
| 703 |
up = 0; |
| 704 |
|
| 705 |
// If not on top row, continue |
| 706 |
do { |
| 707 |
repeat = 0; |
| 708 |
do { |
| 709 |
// search for repeating |
| 710 |
// Test Right |
| 711 |
if ((repeat == 0 || direction == 1) && visionlist[marker + 1] == i) { |
| 712 |
right = 1; |
| 713 |
up = 0; |
| 714 |
++repeat; |
| 715 |
direction = 1; |
| 716 |
++marker; |
| 717 |
} else if ((repeat == 0 || direction == 2) && visionlist[marker - maxsize] == i) { |
| 718 |
up = 1; |
| 719 |
right = 0; |
| 720 |
++repeat; |
| 721 |
direction = 2; |
| 722 |
marker = marker - maxsize; |
| 723 |
} else if ((repeat == 0 || direction == 3) && visionlist[marker + 1 - maxsize] == i && |
| 724 |
visionlist[marker - maxsize] != i && visionlist[marker + 1] != i) { |
| 725 |
up = 1; |
| 726 |
right = 1; |
| 727 |
++repeat; |
| 728 |
direction = 3; |
| 729 |
marker = marker + 1 - maxsize; |
| 730 |
} else { |
| 731 |
direction = 0; |
| 732 |
break; |
| 733 |
} |
| 734 |
|
| 735 |
// search right |
| 736 |
// search up - store as down. |
| 737 |
// search diagonal |
| 738 |
} while (direction && marker > (maxsize * 2)); |
| 739 |
if (right || up) { |
| 740 |
VisionTable[0][VisionTablePosition] = repeat; |
| 741 |
VisionTable[1][VisionTablePosition] = right; |
| 742 |
VisionTable[2][VisionTablePosition] = up; |
| 743 |
++VisionTablePosition; |
| 744 |
} |
| 745 |
} while (marker > (maxsize * 2)); |
| 746 |
++i; |
| 747 |
} |
| 748 |
|
| 749 |
delete[] visionlist; |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
** Clean Up Generated Vision and Goal Tables. |
| 754 |
*/ |
| 755 |
void FreeVisionTable(void) |
| 756 |
{ |
| 757 |
// Free Vision Data |
| 758 |
delete[] VisionTable[0]; |
| 759 |
VisionTable[0] = NULL; |
| 760 |
delete[] VisionTable[1]; |
| 761 |
VisionTable[1] = NULL; |
| 762 |
delete[] VisionTable[2]; |
| 763 |
VisionTable[2] = NULL; |
| 764 |
delete[] VisionLookup; |
| 765 |
VisionLookup = NULL; |
| 766 |
} |
| 767 |
//@} |