| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name build.cpp - The units. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2007 by Lutz Sammer 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 "unit.h" |
| 37 |
#include "unittype.h" |
| 38 |
#include "unit_cache.h" |
| 39 |
#include "map.h" |
| 40 |
#include "player.h" |
| 41 |
|
| 42 |
/*---------------------------------------------------------------------------- |
| 43 |
-- Functions |
| 44 |
----------------------------------------------------------------------------*/ |
| 45 |
|
| 46 |
/** |
| 47 |
** Find the building restriction that gives me this unit built on top |
| 48 |
** Allows you to define how the restriction is effecting the build |
| 49 |
** |
| 50 |
** @param unit the unit that is "OnTop" |
| 51 |
** @param parent the parent unit if known. (guess otherwise) |
| 52 |
** |
| 53 |
** @return the BuildingRestrictionDetails |
| 54 |
*/ |
| 55 |
CBuildRestrictionOnTop *OnTopDetails(const CUnit *unit, const CUnitType *parent) |
| 56 |
{ |
| 57 |
CBuildRestrictionAnd *andb; |
| 58 |
CBuildRestrictionOnTop *ontopb; |
| 59 |
|
| 60 |
for (std::vector<CBuildRestriction *>::iterator i = unit->Type->BuildingRules.begin(); |
| 61 |
i != unit->Type->BuildingRules.end(); ++i) { |
| 62 |
if ((ontopb = dynamic_cast<CBuildRestrictionOnTop *>(*i))) { |
| 63 |
if (!parent) { |
| 64 |
// Guess this is right |
| 65 |
return ontopb; |
| 66 |
} |
| 67 |
if (parent == ontopb->Parent) { |
| 68 |
return ontopb; |
| 69 |
} |
| 70 |
} else if ((andb = dynamic_cast<CBuildRestrictionAnd *>(*i))) { |
| 71 |
for (std::vector<CBuildRestriction *>::iterator it = andb->_or_list.begin(); |
| 72 |
it != andb->_or_list.end(); ++it) { |
| 73 |
if ((ontopb = dynamic_cast<CBuildRestrictionOnTop *>(*it))) { |
| 74 |
if (!parent) { |
| 75 |
// Guess this is right |
| 76 |
return ontopb; |
| 77 |
} |
| 78 |
if (parent == ontopb->Parent) { |
| 79 |
return ontopb; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
** Check And Restriction |
| 90 |
*/ |
| 91 |
bool CBuildRestrictionAnd::Check(const CUnitType *type, int x, int y, CUnit *&ontoptarget) const |
| 92 |
{ |
| 93 |
for (std::vector<CBuildRestriction*>::const_iterator i = _or_list.begin(); |
| 94 |
i != _or_list.end(); ++i) { |
| 95 |
if (!(*i)->Check(type, x, y, ontoptarget)) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
** Check Distance Restriction |
| 104 |
*/ |
| 105 |
bool CBuildRestrictionDistance::Check(const CUnitType *type, int x, int y, CUnit *&ontoptarget) const |
| 106 |
{ |
| 107 |
CUnit *table[UnitMax]; |
| 108 |
int n; |
| 109 |
int x1 = 0; |
| 110 |
int x2 = 0; |
| 111 |
int y1 = 0; |
| 112 |
int y2 = 0; |
| 113 |
int distance = 0; |
| 114 |
|
| 115 |
if (this->DistanceType == LessThanEqual || |
| 116 |
this->DistanceType == GreaterThan || |
| 117 |
this->DistanceType == Equal || |
| 118 |
this->DistanceType == NotEqual) { |
| 119 |
x1 = std::max(x - this->Distance, 0); |
| 120 |
y1 = std::max(y - this->Distance, 0); |
| 121 |
x2 = std::min(x + type->TileWidth + this->Distance, Map.Info.MapWidth); |
| 122 |
y2 = std::min(y + type->TileHeight + this->Distance, Map.Info.MapHeight); |
| 123 |
distance = this->Distance; |
| 124 |
} else if (this->DistanceType == LessThan || |
| 125 |
this->DistanceType == GreaterThanEqual) { |
| 126 |
x1 = std::max(x - this->Distance - 1, 0); |
| 127 |
y1 = std::max(y - this->Distance - 1, 0); |
| 128 |
x2 = std::min(x + type->TileWidth + this->Distance + 1, Map.Info.MapWidth); |
| 129 |
y2 = std::min(y + type->TileHeight + this->Distance + 1, Map.Info.MapHeight); |
| 130 |
distance = this->Distance - 1; |
| 131 |
} |
| 132 |
n = UnitCache.Select(x1, y1, x2, y2, table, UnitMax); |
| 133 |
|
| 134 |
switch (this->DistanceType) { |
| 135 |
case GreaterThan : |
| 136 |
case GreaterThanEqual : |
| 137 |
for (int i = 0; i < n; ++i) { |
| 138 |
if (this->RestrictType == table[i]->Type && |
| 139 |
MapDistanceBetweenTypes(type, x, y, table[i]->Type, table[i]->X, table[i]->Y) <= distance) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
return true; |
| 144 |
case LessThan : |
| 145 |
case LessThanEqual : |
| 146 |
for (int i = 0; i < n; ++i) { |
| 147 |
if (this->RestrictType == table[i]->Type && |
| 148 |
MapDistanceBetweenTypes(type, x, y, table[i]->Type, table[i]->X, table[i]->Y) <= distance) { |
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
return false; |
| 153 |
case Equal : |
| 154 |
for (int i = 0; i < n; ++i) { |
| 155 |
if (this->RestrictType == table[i]->Type && |
| 156 |
MapDistanceBetweenTypes(type, x, y, table[i]->Type, table[i]->X, table[i]->Y) == distance) { |
| 157 |
return true; |
| 158 |
} |
| 159 |
} |
| 160 |
return false; |
| 161 |
case NotEqual : |
| 162 |
for (int i = 0; i < n; ++i) { |
| 163 |
if (this->RestrictType == table[i]->Type && |
| 164 |
MapDistanceBetweenTypes(type, x, y, table[i]->Type, table[i]->X, table[i]->Y) == distance) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
} |
| 168 |
return true; |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
** Check AddOn Restriction |
| 175 |
*/ |
| 176 |
bool CBuildRestrictionAddOn::Check(const CUnitType *type, int x, int y, CUnit *&ontoptarget) const |
| 177 |
{ |
| 178 |
CUnit *table[UnitMax]; |
| 179 |
int n; |
| 180 |
int i; |
| 181 |
int x1; |
| 182 |
int y1; |
| 183 |
|
| 184 |
x1 = x - this->OffsetX < 0 ? -1 : x - this->OffsetX; |
| 185 |
x1 = x1 >= Map.Info.MapWidth ? -1 : x1; |
| 186 |
y1 = y - this->OffsetY < 0 ? -1 : y - this->OffsetY; |
| 187 |
y1 = y1 >= Map.Info.MapHeight ? -1 : y1; |
| 188 |
if (!(x1 == -1 || y1 == -1)) { |
| 189 |
n = UnitCache.Select(x1, y1, table, UnitMax); |
| 190 |
for (i = 0; i < n; ++i) { |
| 191 |
if (table[i]->Type == this->Parent && |
| 192 |
table[i]->X == x1 && table[i]->Y == y1) { |
| 193 |
return true; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
** Check OnTop Restriction |
| 202 |
*/ |
| 203 |
bool CBuildRestrictionOnTop::Check(const CUnitType *type, int x, int y, CUnit *&ontoptarget) const |
| 204 |
{ |
| 205 |
CUnit *table[UnitMax]; |
| 206 |
int n; |
| 207 |
int i; |
| 208 |
|
| 209 |
ontoptarget = NULL; |
| 210 |
n = UnitCache.Select(x, y, table, UnitMax); |
| 211 |
for (i = 0; i < n; ++i) { |
| 212 |
if (table[i]->X == x && table[i]->Y == y && !table[i]->Destroyed && |
| 213 |
table[i]->Orders[0]->Action != UnitActionDie) { |
| 214 |
if (table[i]->Type == this->Parent && |
| 215 |
table[i]->Orders[0]->Action != UnitActionBuilt) { |
| 216 |
// Available to build on |
| 217 |
ontoptarget = table[i]; |
| 218 |
} else { |
| 219 |
// Something else is built on this already |
| 220 |
ontoptarget = NULL; |
| 221 |
return false; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
return ontoptarget != NULL; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
** Check Terrain Restriction |
| 230 |
*/ |
| 231 |
bool CBuildRestrictionTerrain::Check(const CUnitType *type, int xMin, int yMin, CUnit *&ontoptarget) const |
| 232 |
{ |
| 233 |
Assert(xMin >= 0); |
| 234 |
Assert(xMin + type->TileWidth <= Map.Info.MapWidth); |
| 235 |
Assert(yMin >= 0); |
| 236 |
Assert(yMin + type->TileHeight <= Map.Info.MapHeight); |
| 237 |
|
| 238 |
int count = 0; |
| 239 |
for (int y = yMin; y < yMin + type->TileHeight; y++) { |
| 240 |
for (int x = xMin; x < xMin + type->TileWidth; x++) { |
| 241 |
// In principle, if the player hasn't explored |
| 242 |
// the map field yet, this function should not |
| 243 |
// leak any information about it. However, |
| 244 |
// CanBuildHere seems to be the only caller, |
| 245 |
// and after Check returns true, it verifies |
| 246 |
// that the tiles have been explored, and |
| 247 |
// prevents building if not. |
| 248 |
if (Map.Field(x, y)->Flags & this->FieldFlags) { |
| 249 |
count++; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return count >= this->Min && count <= this->Max; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
** Can build unit here. |
| 259 |
** |
| 260 |
** @param unit Unit doing the building |
| 261 |
** @param type unit-type to be checked. |
| 262 |
** @param x Map X position. |
| 263 |
** @param y Map Y position. |
| 264 |
** |
| 265 |
** @return OnTop, parent unit, builder on true or 1 if unit==NULL, NULL false. |
| 266 |
*/ |
| 267 |
CUnit *CanBuildHere(const CUnit *unit, const CUnitType *type, int x, int y) |
| 268 |
{ |
| 269 |
CUnit *ontoptarget; |
| 270 |
|
| 271 |
// |
| 272 |
// Can't build outside the map |
| 273 |
// |
| 274 |
if (x + type->TileWidth > Map.Info.MapWidth) { |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
if (y + type->TileHeight > Map.Info.MapHeight) { |
| 278 |
return NULL; |
| 279 |
} |
| 280 |
|
| 281 |
// Must be checked before oil! |
| 282 |
if (type->ShoreBuilding) { |
| 283 |
bool success = false; |
| 284 |
|
| 285 |
// Need at least one coast tile |
| 286 |
for (int h = type->TileHeight; h--;) { |
| 287 |
for (int w = type->TileWidth; w--;) { |
| 288 |
if (Map.Field(x + w, y + h)->Flags & MapFieldCoastAllowed) { |
| 289 |
h = w = 0; |
| 290 |
success = true; |
| 291 |
break; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
if (!success) { |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (type->BuildingRules.empty()) { |
| 301 |
return (unit == NULL) ? (CUnit *)1 : const_cast<CUnit *>(unit); |
| 302 |
} |
| 303 |
|
| 304 |
ontoptarget = NULL; |
| 305 |
for (std::vector<CBuildRestriction *>::const_iterator ib = type->BuildingRules.begin(); |
| 306 |
ib != type->BuildingRules.end(); ++ib) { |
| 307 |
// All checks processed, did we really have success |
| 308 |
if ((*ib)->Check(type, x, y, ontoptarget)) { |
| 309 |
// We passed a full ruleset return |
| 310 |
if (unit == NULL) { |
| 311 |
return ontoptarget ? ontoptarget : (CUnit *)1; |
| 312 |
} else { |
| 313 |
return ontoptarget ? ontoptarget : (CUnit *)unit; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
** Can build on this point. |
| 322 |
** |
| 323 |
** @param x X tile map position. |
| 324 |
** @param y Y tile map position. |
| 325 |
** @param mask terrain mask |
| 326 |
** |
| 327 |
** @return if we can build on this point. |
| 328 |
*/ |
| 329 |
bool CanBuildOn(int x, int y, int mask) |
| 330 |
{ |
| 331 |
if (x < 0 || y < 0 || x >= Map.Info.MapWidth || y >= Map.Info.MapHeight) { |
| 332 |
return false; |
| 333 |
} |
| 334 |
return (Map.Field(x, y)->Flags & mask) ? false : true; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
** Can build unit-type at this point. |
| 339 |
** |
| 340 |
** @param unit Worker that want to build the building or NULL. |
| 341 |
** @param type Building unit-type. |
| 342 |
** @param x X tile map position. |
| 343 |
** @param y Y tile map position. |
| 344 |
** @param real Really build, or just placement |
| 345 |
** |
| 346 |
** @return OnTop, parent unit, builder on true, NULL false. |
| 347 |
*/ |
| 348 |
CUnit *CanBuildUnitType(const CUnit *unit, const CUnitType *type, int x, int y, int real) |
| 349 |
{ |
| 350 |
int testmask; |
| 351 |
CPlayer *player; |
| 352 |
CUnit *ontop; |
| 353 |
|
| 354 |
// Terrain Flags don't matter if building on top of a unit. |
| 355 |
ontop = CanBuildHere(unit, type, x, y); |
| 356 |
if (ontop == NULL) { |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
if (ontop != (CUnit *)1 && ontop != unit) { |
| 360 |
return ontop; |
| 361 |
} |
| 362 |
|
| 363 |
// |
| 364 |
// Remove unit that is building! |
| 365 |
// |
| 366 |
if (unit) { |
| 367 |
UnmarkUnitFieldFlags(unit); |
| 368 |
} |
| 369 |
|
| 370 |
player = NULL; |
| 371 |
|
| 372 |
if (unit && unit->Player->Type == PlayerPerson) { |
| 373 |
player = unit->Player; |
| 374 |
} |
| 375 |
|
| 376 |
for (int h = type->TileHeight; h--;) { |
| 377 |
for (int w = type->TileWidth; w--;) { |
| 378 |
if (player && !real) { |
| 379 |
testmask = MapFogFilterFlags(player, x + w, y + h, type->MovementMask); |
| 380 |
} else { |
| 381 |
testmask = type->MovementMask; |
| 382 |
} |
| 383 |
if (!CanBuildOn(x + w, y + h, testmask)) { |
| 384 |
if (unit) { |
| 385 |
MarkUnitFieldFlags(unit); |
| 386 |
} |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
if (player && !Map.IsFieldExplored(player, x + w, y + h)) { |
| 390 |
if (unit) { |
| 391 |
MarkUnitFieldFlags(unit); |
| 392 |
} |
| 393 |
return NULL; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
if (unit) { |
| 398 |
MarkUnitFieldFlags(unit); |
| 399 |
} |
| 400 |
|
| 401 |
// |
| 402 |
// We can build here: check distance to gold mine/oil patch! |
| 403 |
// |
| 404 |
return ontop; |
| 405 |
} |
| 406 |
|
| 407 |
//@} |