| 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_radar.cpp - The map radar handling. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2004-2007 by Russell Smith and Jimmy Salmon. |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include "stratagus.h" |
| 36 |
#include "player.h" |
| 37 |
#include "unittype.h" |
| 38 |
#include "unit.h" |
| 39 |
#include "map.h" |
| 40 |
|
| 41 |
/*---------------------------------------------------------------------------- |
| 42 |
-- Functions |
| 43 |
----------------------------------------------------------------------------*/ |
| 44 |
|
| 45 |
/** |
| 46 |
** Find out if a unit is visible under radar (By player, or by shared vision) |
| 47 |
** |
| 48 |
** @param pradar Player to who has radar. |
| 49 |
** @param punit Unit to check for. |
| 50 |
** |
| 51 |
** @return true if radar visible, false if jammed or not radar visible. |
| 52 |
*/ |
| 53 |
bool UnitVisibleOnRadar(const CPlayer *pradar, const CUnit *punit) |
| 54 |
{ |
| 55 |
for (int i = punit->X; i < punit->X + punit->Type->TileWidth; ++i) { |
| 56 |
for (int j = punit->Y; j < punit->Y + punit->Type->TileHeight; ++j) { |
| 57 |
if (IsTileRadarVisible(pradar, punit->Player, i, j) != 0) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Can't exit till the end, as we might be be able to see a different tile |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
** Find out if a unit is visible under radar (By player, or by shared vision) |
| 69 |
** |
| 70 |
** @param pradar Player who has radar. |
| 71 |
** @param punit Player who is being check. |
| 72 |
** @param x x tile location to check. |
| 73 |
** @param y y tile location to check. |
| 74 |
** |
| 75 |
** @return 0 jammed or not radar visible, >0 radar visible. |
| 76 |
*/ |
| 77 |
unsigned char IsTileRadarVisible(const CPlayer *pradar, const CPlayer *punit, int x, int y) |
| 78 |
{ |
| 79 |
unsigned char radarvision; |
| 80 |
unsigned char *radar; |
| 81 |
unsigned char *jamming; |
| 82 |
|
| 83 |
jamming = Map.Field(x, y)->RadarJammer; |
| 84 |
if (jamming[punit->Index]) { |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
radar = Map.Field(x, y)->Radar; |
| 89 |
radarvision = radar[pradar->Index]; |
| 90 |
|
| 91 |
if (!pradar->SharedVision) { |
| 92 |
return radarvision; |
| 93 |
} |
| 94 |
|
| 95 |
// Check jamming first, if we are jammed, exit |
| 96 |
for (int i = 0; i < PlayerMax; ++i) { |
| 97 |
if (jamming[i] > 0 && (punit->SharedVision & (1 << i)) && |
| 98 |
(Players[i].SharedVision & (1 << punit->Index))) { |
| 99 |
// We are jammed, return nothing |
| 100 |
return 0; |
| 101 |
} |
| 102 |
if (pradar->SharedVision & (1 << i) && |
| 103 |
(Players[i].SharedVision & (1 << pradar->Index))) { |
| 104 |
radarvision |= radar[i]; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Can't exit until the end, as we might be jammed |
| 109 |
return radarvision; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
** Mark Radar Vision for a tile |
| 114 |
** |
| 115 |
** @param player The player you are marking for |
| 116 |
** @param x the X tile to mark. |
| 117 |
** @param y the Y tile to mark. |
| 118 |
*/ |
| 119 |
void MapMarkTileRadar(const CPlayer *player, int x, int y) |
| 120 |
{ |
| 121 |
Assert(0 <= x && x < Map.Info.MapWidth); |
| 122 |
Assert(0 <= y && y < Map.Info.MapHeight); |
| 123 |
Assert(Map.Field(x, y)->Radar[player->Index] != 255); |
| 124 |
|
| 125 |
Map.Field(x, y)->Radar[player->Index]++; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
** Unmark Radar Vision for a tile |
| 130 |
** |
| 131 |
** @param player The player you are marking for |
| 132 |
** @param x the X tile to mark. |
| 133 |
** @param y the Y tile to mark. |
| 134 |
*/ |
| 135 |
void MapUnmarkTileRadar(const CPlayer *player, int x, int y) |
| 136 |
{ |
| 137 |
unsigned char *v; |
| 138 |
|
| 139 |
Assert(0 <= x && x < Map.Info.MapWidth); |
| 140 |
Assert(0 <= y && y < Map.Info.MapHeight); |
| 141 |
|
| 142 |
// Reduce radar coverage if it exists. |
| 143 |
v = &Map.Field(x, y)->Radar[player->Index]; |
| 144 |
if (*v) { |
| 145 |
--*v; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
** Mark Radar Jamming Vision for a tile |
| 151 |
** |
| 152 |
** @param player The player you are marking for |
| 153 |
** @param x the X tile to mark. |
| 154 |
** @param y the Y tile to mark. |
| 155 |
*/ |
| 156 |
void MapMarkTileRadarJammer(const CPlayer *player, int x, int y) |
| 157 |
{ |
| 158 |
Assert(0 <= x && x < Map.Info.MapWidth); |
| 159 |
Assert(0 <= y && y < Map.Info.MapHeight); |
| 160 |
Assert(Map.Field(x, y)->RadarJammer[player->Index] != 255); |
| 161 |
|
| 162 |
Map.Field(x, y)->RadarJammer[player->Index]++; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
** Unmark Radar Vision for a tile |
| 167 |
** |
| 168 |
** @param player The player you are marking for |
| 169 |
** @param x the X tile to mark. |
| 170 |
** @param y the Y tile to mark. |
| 171 |
*/ |
| 172 |
void MapUnmarkTileRadarJammer(const CPlayer *player, int x, int y) |
| 173 |
{ |
| 174 |
unsigned char *v; |
| 175 |
|
| 176 |
Assert(0 <= x && x < Map.Info.MapWidth); |
| 177 |
Assert(0 <= y && y < Map.Info.MapHeight); |
| 178 |
|
| 179 |
// Reduce radar coverage if it exists. |
| 180 |
v = &Map.Field(x, y)->RadarJammer[player->Index]; |
| 181 |
if (*v) { |
| 182 |
--*v; |
| 183 |
} |
| 184 |
} |