| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name player.cpp - The players. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2010 by Lutz Sammer, Jimmy Salmon, Nehal Mistry |
| 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 <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <stdarg.h> |
| 39 |
#include <algorithm> |
| 40 |
|
| 41 |
#include "stratagus.h" |
| 42 |
#include "video.h" |
| 43 |
#include "sound.h" |
| 44 |
#include "unitsound.h" |
| 45 |
#include "unittype.h" |
| 46 |
#include "player.h" |
| 47 |
#include "unit.h" |
| 48 |
#include "map.h" |
| 49 |
#include "ai.h" |
| 50 |
#include "network.h" |
| 51 |
#include "netconnect.h" |
| 52 |
#include "interface.h" |
| 53 |
#include "iolib.h" |
| 54 |
#include "ui.h" |
| 55 |
|
| 56 |
/*---------------------------------------------------------------------------- |
| 57 |
-- Variables |
| 58 |
----------------------------------------------------------------------------*/ |
| 59 |
|
| 60 |
int NumPlayers; /// How many player slots used |
| 61 |
CPlayer Players[PlayerMax]; /// All players in play |
| 62 |
CPlayer *ThisPlayer; /// Player on this computer |
| 63 |
|
| 64 |
int NoRescueCheck; /// Disable rescue check |
| 65 |
|
| 66 |
/** |
| 67 |
** Colors used for minimap. |
| 68 |
*/ |
| 69 |
SDL_Color *PlayerColorsRGB[PlayerMax]; |
| 70 |
Uint32 *PlayerColors[PlayerMax]; |
| 71 |
|
| 72 |
std::string PlayerColorNames[PlayerMax]; |
| 73 |
|
| 74 |
/** |
| 75 |
** Which indexes to replace with player color |
| 76 |
*/ |
| 77 |
int PlayerColorIndexStart; |
| 78 |
int PlayerColorIndexCount; |
| 79 |
|
| 80 |
/*---------------------------------------------------------------------------- |
| 81 |
-- Functions |
| 82 |
----------------------------------------------------------------------------*/ |
| 83 |
|
| 84 |
/** |
| 85 |
** Init players. |
| 86 |
*/ |
| 87 |
void InitPlayers(void) |
| 88 |
{ |
| 89 |
for (int p = 0; p < PlayerMax; ++p) { |
| 90 |
Players[p].Index = p; |
| 91 |
if (!Players[p].Type) { |
| 92 |
Players[p].Type = PlayerNobody; |
| 93 |
} |
| 94 |
for (int x = 0; x < PlayerColorIndexCount; ++x) { |
| 95 |
PlayerColors[p][x] = Video.MapRGB(TheScreen->format, |
| 96 |
PlayerColorsRGB[p][x].r, |
| 97 |
PlayerColorsRGB[p][x].g, PlayerColorsRGB[p][x].b); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
** Clean up players. |
| 104 |
*/ |
| 105 |
void CleanPlayers(void) |
| 106 |
{ |
| 107 |
ThisPlayer = NULL; |
| 108 |
for (int i = 0; i < PlayerMax; ++i) { |
| 109 |
Players[i].Clear(); |
| 110 |
} |
| 111 |
NumPlayers = 0; |
| 112 |
|
| 113 |
NoRescueCheck = 0; |
| 114 |
} |
| 115 |
|
| 116 |
#ifdef DEBUG |
| 117 |
void FreePlayerColors() |
| 118 |
{ |
| 119 |
for (int i = 0; i < PlayerMax; ++i) { |
| 120 |
delete[] Players[i].UnitColors.Colors; |
| 121 |
delete[] PlayerColorsRGB[i]; |
| 122 |
PlayerColorsRGB[i] = NULL; |
| 123 |
delete[] PlayerColors[i]; |
| 124 |
PlayerColors[i] = NULL; |
| 125 |
} |
| 126 |
} |
| 127 |
#endif |
| 128 |
|
| 129 |
/** |
| 130 |
** Add to UnitsConsumingResources |
| 131 |
*/ |
| 132 |
void CPlayer::AddToUnitsConsumingResources(CUnit *unit, int costs[MaxCosts]) |
| 133 |
{ |
| 134 |
Assert(UnitsConsumingResourcesActual[unit] == NULL); |
| 135 |
Assert(UnitsConsumingResourcesRequested[unit] == NULL); |
| 136 |
|
| 137 |
int *c; |
| 138 |
|
| 139 |
c = new int[MaxCosts]; |
| 140 |
memset(c, 0, MaxCosts * sizeof(int)); |
| 141 |
UnitsConsumingResourcesActual[unit] = c; |
| 142 |
// no need to change ActualUtilizationRate here |
| 143 |
|
| 144 |
c = new int[MaxCosts]; |
| 145 |
memcpy(c, costs, MaxCosts * sizeof(int)); |
| 146 |
UnitsConsumingResourcesRequested[unit] = c; |
| 147 |
for (int i = 0; i < MaxCosts; ++i) { |
| 148 |
RequestedUtilizationRate[i] += costs[i]; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
** Remove from UnitsConsumingResources |
| 154 |
*/ |
| 155 |
void CPlayer::RemoveFromUnitsConsumingResources(CUnit *unit) |
| 156 |
{ |
| 157 |
int *cactual = UnitsConsumingResourcesActual[unit]; |
| 158 |
int *crequested = UnitsConsumingResourcesRequested[unit]; |
| 159 |
|
| 160 |
for (int i = 0; i < MaxCosts; ++i) { |
| 161 |
ActualUtilizationRate[i] -= cactual[i]; |
| 162 |
RequestedUtilizationRate[i] -= crequested[i]; |
| 163 |
} |
| 164 |
|
| 165 |
delete[] cactual; |
| 166 |
UnitsConsumingResourcesActual.erase(unit); |
| 167 |
delete[] crequested; |
| 168 |
UnitsConsumingResourcesRequested.erase(unit); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
** Update costs for unit in UnitsConsumingResources |
| 173 |
*/ |
| 174 |
void CPlayer::UpdateUnitsConsumingResources(CUnit *unit, int costs[MaxCosts]) |
| 175 |
{ |
| 176 |
int *c = UnitsConsumingResourcesActual[unit]; |
| 177 |
|
| 178 |
for (int i = 0; i < MaxCosts; ++i) { |
| 179 |
ActualUtilizationRate[i] -= c[i]; |
| 180 |
c[i] = costs[i]; |
| 181 |
ActualUtilizationRate[i] += c[i]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
** Calculate how many resources the unit needs to request |
| 187 |
** |
| 188 |
** @param utype builder unit type |
| 189 |
** @param bcosts costs of unit type being built or harvested |
| 190 |
** @param costs returns the resource amount |
| 191 |
*/ |
| 192 |
void CalculateRequestedAmount(CUnitType *utype, int bcosts[MaxCosts], int costs[MaxCosts]) |
| 193 |
{ |
| 194 |
if (bcosts[EnergyCost] == 0) { |
| 195 |
costs[EnergyCost] = 0; |
| 196 |
costs[MagmaCost] = utype->MaxUtilizationRate[MagmaCost]; |
| 197 |
} else if (bcosts[MagmaCost] == 0) { |
| 198 |
costs[EnergyCost] = utype->MaxUtilizationRate[EnergyCost]; |
| 199 |
costs[MagmaCost] = 0; |
| 200 |
} else { |
| 201 |
int f = 100 * bcosts[EnergyCost] * utype->MaxUtilizationRate[MagmaCost] / |
| 202 |
(bcosts[MagmaCost] * utype->MaxUtilizationRate[EnergyCost]); |
| 203 |
if (f > 100) { |
| 204 |
costs[EnergyCost] = utype->MaxUtilizationRate[EnergyCost]; |
| 205 |
costs[MagmaCost] = (utype->MaxUtilizationRate[MagmaCost] * 100 + f / 2) / f; |
| 206 |
} else if (f < 100) { |
| 207 |
costs[EnergyCost] = (utype->MaxUtilizationRate[EnergyCost] * f + 50) / 100; |
| 208 |
costs[MagmaCost] = utype->MaxUtilizationRate[MagmaCost]; |
| 209 |
} else { |
| 210 |
costs[EnergyCost] = utype->MaxUtilizationRate[EnergyCost]; |
| 211 |
costs[MagmaCost] = utype->MaxUtilizationRate[MagmaCost]; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
for (int i = 0; i < MaxCosts; ++i) { |
| 216 |
if (costs[i] > bcosts[i]) { |
| 217 |
costs[i] = bcosts[i]; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
** Go through the list of units owned by the player and rebuild |
| 224 |
** the UnitsConsumingResources list. |
| 225 |
*/ |
| 226 |
void CPlayer::RebuildUnitsConsumingResourcesList() |
| 227 |
{ |
| 228 |
for (int i = 0; i < TotalNumUnits; ++i) { |
| 229 |
int costs[MaxCosts]; |
| 230 |
CUnit *unit = Units[i]; |
| 231 |
|
| 232 |
if (unit->Orders[0]->Action == UnitActionTrain && unit->SubAction != 0) { |
| 233 |
CalculateRequestedAmount(unit->Type, unit->Orders[0]->Type->ProductionCosts, costs); |
| 234 |
AddToUnitsConsumingResources(unit, costs); |
| 235 |
} else if (unit->Orders[0]->Action == UnitActionRepair && unit->SubAction == 20) { |
| 236 |
CalculateRequestedAmount(unit->Type, unit->Orders[0]->Goal->Type->ProductionCosts, costs); |
| 237 |
AddToUnitsConsumingResources(unit, costs); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
** Clear all resource state variables. |
| 244 |
*/ |
| 245 |
void CPlayer::ClearResourceVariables() |
| 246 |
{ |
| 247 |
std::map<CUnit *, int *>::iterator i; |
| 248 |
for (i = UnitsConsumingResourcesActual.begin(); |
| 249 |
i != UnitsConsumingResourcesActual.end(); ++i) { |
| 250 |
delete[] (*i).second; |
| 251 |
} |
| 252 |
for (i = UnitsConsumingResourcesRequested.begin(); |
| 253 |
i != UnitsConsumingResourcesRequested.end(); ++i) { |
| 254 |
delete[] (*i).second; |
| 255 |
} |
| 256 |
|
| 257 |
UnitsConsumingResourcesActual.clear(); |
| 258 |
UnitsConsumingResourcesRequested.clear(); |
| 259 |
memset(ProductionRate, 0, sizeof(ProductionRate)); |
| 260 |
memset(ActualUtilizationRate, 0, sizeof(ActualUtilizationRate)); |
| 261 |
memset(RequestedUtilizationRate, 0, sizeof(RequestedUtilizationRate)); |
| 262 |
memset(StoredResources, 0, sizeof(StoredResources)); |
| 263 |
memset(StorageCapacity, 0, sizeof(StorageCapacity)); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
// p(type) = total production(type) + min(storage(type), storagerate(type)) |
| 268 |
static int AvailableResourcesRate(int type, CPlayer *p) |
| 269 |
{ |
| 270 |
// FIXME: change 50 to rate we can get resources out of storage |
| 271 |
int RateFromStorage = 50; |
| 272 |
return p->ProductionRate[type] + std::min<int>(p->StoredResources[type], RateFromStorage); |
| 273 |
} |
| 274 |
|
| 275 |
// f(type) = min(p(type)/ total requested(type), 1.0) |
| 276 |
static int SpecificEfficiency(int type, CPlayer *p) |
| 277 |
{ |
| 278 |
if (p->RequestedUtilizationRate[type] == 0) { |
| 279 |
return 100; |
| 280 |
} |
| 281 |
return std::min((100 * AvailableResourcesRate(type, p) + |
| 282 |
p->RequestedUtilizationRate[type] / 2) / p->RequestedUtilizationRate[type], 100); |
| 283 |
} |
| 284 |
|
| 285 |
// base efficiency = min(f(energy), f(magma)) |
| 286 |
static int BaseEfficiency(CPlayer *p) |
| 287 |
{ |
| 288 |
return std::min(SpecificEfficiency(EnergyCost, p), SpecificEfficiency(MagmaCost, p)); |
| 289 |
} |
| 290 |
|
| 291 |
// total p(type) - total requested(type) * base efficiency |
| 292 |
static int ExtraProduction(int type, CPlayer *p, int be) |
| 293 |
{ |
| 294 |
return 100 * AvailableResourcesRate(type, p) - p->RequestedUtilizationRate[type] * be; |
| 295 |
} |
| 296 |
|
| 297 |
// sum of needs(type) of all units who only require this resource) |
| 298 |
static int UniqueNeeds(int type, CPlayer *p) |
| 299 |
{ |
| 300 |
// FIXME: this doesn't have to be recalculated every time |
| 301 |
std::map<CUnit *, int *>::iterator i = p->UnitsConsumingResourcesRequested.begin(); |
| 302 |
std::map<CUnit *, int *>::iterator end = p->UnitsConsumingResourcesRequested.end(); |
| 303 |
int type2 = (type == EnergyCost ? MagmaCost : EnergyCost); |
| 304 |
int needs = 0; |
| 305 |
|
| 306 |
for (; i != end; ++i) { |
| 307 |
if ((*i).second[type] != 0 && (*i).second[type2] == 0) { |
| 308 |
needs += (*i).second[type]; |
| 309 |
} |
| 310 |
} |
| 311 |
return needs; |
| 312 |
} |
| 313 |
|
| 314 |
// min(extra production(type) / unique needs(type) + base efficiency, 1) |
| 315 |
static int UniqueEfficiency(int type, CPlayer *p) |
| 316 |
{ |
| 317 |
int be = BaseEfficiency(p); |
| 318 |
return std::min(ExtraProduction(type, p, be) / UniqueNeeds(type, p) + be, 100); |
| 319 |
} |
| 320 |
|
| 321 |
static int MaxRate(CUnit *unit, int res) |
| 322 |
{ |
| 323 |
int *costs = unit->Player->UnitsConsumingResourcesRequested[unit]; |
| 324 |
return costs[res]; |
| 325 |
} |
| 326 |
|
| 327 |
static void CalculateCosts(CUnit *unit, int costs[MaxCosts]) |
| 328 |
{ |
| 329 |
int i; |
| 330 |
int usedtypes = 0; |
| 331 |
int type = -1; |
| 332 |
int *c = unit->Player->UnitsConsumingResourcesRequested[unit]; |
| 333 |
|
| 334 |
for (i = 0; i < MaxCosts; ++i) { |
| 335 |
if (c[i] != 0) { |
| 336 |
++usedtypes; |
| 337 |
type = i; |
| 338 |
} |
| 339 |
} |
| 340 |
Assert(usedtypes > 0); |
| 341 |
|
| 342 |
// if unit requires both types: |
| 343 |
if (usedtypes > 1) { |
| 344 |
// unit effective rate(type) = unit max rate(type) * base efficiency |
| 345 |
int be = BaseEfficiency(unit->Player); |
| 346 |
for (i = 0; i < MaxCosts; ++i) { |
| 347 |
costs[i] = MaxRate(unit, i) * be / 100; |
| 348 |
} |
| 349 |
} else { |
| 350 |
// unit effective rate(type) = unit max rate(type) * unique efficiency(type) |
| 351 |
int ue = UniqueEfficiency(type, unit->Player); |
| 352 |
for (i = 0; i < MaxCosts; ++i) { |
| 353 |
costs[i] = MaxRate(unit, i) * ue / 100; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
** Save state of players to file. |
| 360 |
** |
| 361 |
** @param file Output file. |
| 362 |
** |
| 363 |
** @note FIXME: Not completely saved. |
| 364 |
*/ |
| 365 |
void SavePlayers(CFile *file) |
| 366 |
{ |
| 367 |
int j; |
| 368 |
Uint8 r, g, b; |
| 369 |
CPlayer *p; |
| 370 |
|
| 371 |
file->printf("\n--------------------------------------------\n"); |
| 372 |
file->printf("--- MODULE: players\n\n"); |
| 373 |
|
| 374 |
// |
| 375 |
// Dump all players |
| 376 |
// |
| 377 |
for (int i = 0; i < NumPlayers; ++i) { |
| 378 |
p = &Players[i]; |
| 379 |
file->printf("Player(%d,\n", i); |
| 380 |
file->printf(" \"name\", \"%s\",\n", p->Name.c_str()); |
| 381 |
file->printf(" \"type\", "); |
| 382 |
switch (p->Type) { |
| 383 |
case PlayerNeutral: file->printf("\"neutral\","); break; |
| 384 |
case PlayerNobody: file->printf("\"nobody\","); break; |
| 385 |
case PlayerComputer: file->printf("\"computer\","); break; |
| 386 |
case PlayerPerson: file->printf("\"person\","); break; |
| 387 |
case PlayerRescuePassive: file->printf("\"rescue-passive\",");break; |
| 388 |
case PlayerRescueActive: file->printf("\"rescue-active\","); break; |
| 389 |
default: file->printf("%d,",p->Type);break; |
| 390 |
} |
| 391 |
file->printf(" \"ai-name\", \"%s\",\n", p->AiName.c_str()); |
| 392 |
file->printf(" \"team\", %d,", p->Team); |
| 393 |
|
| 394 |
file->printf(" \"enemy\", \""); |
| 395 |
for (j = 0; j < PlayerMax; ++j) { |
| 396 |
file->printf("%c",(p->Enemy & (1 << j)) ? 'X' : '_'); |
| 397 |
} |
| 398 |
file->printf("\", \"allied\", \""); |
| 399 |
for (j = 0; j < PlayerMax; ++j) { |
| 400 |
file->printf("%c", (p->Allied & (1 << j)) ? 'X' : '_'); |
| 401 |
} |
| 402 |
file->printf("\", \"shared-vision\", \""); |
| 403 |
for (j = 0; j < PlayerMax; ++j) { |
| 404 |
file->printf("%c", (p->SharedVision & (1 << j)) ? 'X' : '_'); |
| 405 |
} |
| 406 |
file->printf("\",\n \"start\", {%d, %d},\n", p->StartX, |
| 407 |
p->StartY); |
| 408 |
|
| 409 |
// ProductionRate |
| 410 |
file->printf(" \"production-rate\", {"); |
| 411 |
for (j = 0; j < MaxCosts; ++j) { |
| 412 |
if (j) { |
| 413 |
file->printf(" "); |
| 414 |
} |
| 415 |
file->printf("\"%s\", %d,", DefaultResourceNames[j].c_str(), |
| 416 |
p->ProductionRate[j]); |
| 417 |
} |
| 418 |
// StoredResources |
| 419 |
file->printf("},\n \"stored-resources\", {"); |
| 420 |
for (j = 0; j < MaxCosts; ++j) { |
| 421 |
if (j) { |
| 422 |
file->printf(" "); |
| 423 |
} |
| 424 |
file->printf("\"%s\", %d,", DefaultResourceNames[j].c_str(), |
| 425 |
p->StoredResources[j]); |
| 426 |
} |
| 427 |
// StorageCapacity |
| 428 |
file->printf("},\n \"storage-capacity\", {"); |
| 429 |
for (j = 0; j < MaxCosts; ++j) { |
| 430 |
if (j) { |
| 431 |
file->printf(" "); |
| 432 |
} |
| 433 |
file->printf("\"%s\", %d,", DefaultResourceNames[j].c_str(), |
| 434 |
p->StorageCapacity[j]); |
| 435 |
} |
| 436 |
|
| 437 |
// UnitTypesCount done by load units. |
| 438 |
|
| 439 |
file->printf("},\n \"%s\",\n", p->AiEnabled ? |
| 440 |
"ai-enabled" : "ai-disabled"); |
| 441 |
|
| 442 |
// Ai done by load ais. |
| 443 |
// Units done by load units. |
| 444 |
// TotalNumUnits done by load units. |
| 445 |
// NumBuildings done by load units. |
| 446 |
|
| 447 |
file->printf(" \"unit-limit\", %d,", p->UnitLimit); |
| 448 |
file->printf(" \"building-limit\", %d,", p->BuildingLimit); |
| 449 |
file->printf(" \"total-unit-limit\", %d,", p->TotalUnitLimit); |
| 450 |
|
| 451 |
file->printf("\n \"score\", %d,", p->Score); |
| 452 |
file->printf("\n \"total-units\", %d,", p->TotalUnits); |
| 453 |
file->printf("\n \"total-buildings\", %d,", p->TotalBuildings); |
| 454 |
file->printf("\n \"total-resources\", {"); |
| 455 |
for (j = 0; j < MaxCosts; ++j) { |
| 456 |
if (j) { |
| 457 |
file->printf(" "); |
| 458 |
} |
| 459 |
file->printf("%d,", p->TotalResources[j]); |
| 460 |
} |
| 461 |
file->printf("},"); |
| 462 |
file->printf("\n \"total-razings\", %d,", p->TotalRazings); |
| 463 |
file->printf("\n \"total-kills\", %d,", p->TotalKills); |
| 464 |
|
| 465 |
SDL_GetRGB(p->Color, TheScreen->format, &r, &g, &b); |
| 466 |
file->printf("\n \"color\", { %d, %d, %d }", r, g, b); // no comma after last parameter |
| 467 |
|
| 468 |
// UnitColors done by init code. |
| 469 |
// Allow saved by allow. |
| 470 |
|
| 471 |
file->printf(")\n\n"); |
| 472 |
} |
| 473 |
|
| 474 |
DebugPrint("FIXME: must save unit-stats?\n"); |
| 475 |
|
| 476 |
// |
| 477 |
// Dump local variables |
| 478 |
// |
| 479 |
file->printf("ThisPlayer = Players[%d]\n\n", ThisPlayer->Index); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
** Create a new player. |
| 484 |
** |
| 485 |
** @param type Player type (Computer,Human,...). |
| 486 |
*/ |
| 487 |
void CreatePlayer(PlayerTypes type) |
| 488 |
{ |
| 489 |
int team; |
| 490 |
int i; |
| 491 |
CPlayer *player; |
| 492 |
|
| 493 |
if (NumPlayers == PlayerMax) { // already done for bigmaps! |
| 494 |
return; |
| 495 |
} |
| 496 |
player = &Players[NumPlayers]; |
| 497 |
player->Index = NumPlayers; |
| 498 |
|
| 499 |
// Allocate memory for the "list" of this player's units. |
| 500 |
// FIXME: brutal way, as we won't need UnitMax for this player... |
| 501 |
// FIXME: ARI: is this needed for 'PlayerNobody' ?? |
| 502 |
// FIXME: A: Johns: currently we need no init for the nobody player. |
| 503 |
memset(player->Units, 0, sizeof(player->Units)); |
| 504 |
|
| 505 |
// |
| 506 |
// Take first slot for person on this computer, |
| 507 |
// fill other with computer players. |
| 508 |
// |
| 509 |
if (type == PlayerPerson && !NetPlayers) { |
| 510 |
if (!ThisPlayer) { |
| 511 |
ThisPlayer = player; |
| 512 |
} else { |
| 513 |
type = PlayerComputer; |
| 514 |
} |
| 515 |
} |
| 516 |
if (NetPlayers && NumPlayers == NetLocalPlayerNumber) { |
| 517 |
ThisPlayer = &Players[NetLocalPlayerNumber]; |
| 518 |
} |
| 519 |
|
| 520 |
if (NumPlayers == PlayerMax) { |
| 521 |
static int already_warned; |
| 522 |
|
| 523 |
if (!already_warned) { |
| 524 |
DebugPrint("Too many players\n"); |
| 525 |
already_warned = 1; |
| 526 |
} |
| 527 |
return; |
| 528 |
} |
| 529 |
|
| 530 |
// |
| 531 |
// Make simple teams: |
| 532 |
// All person players are enemies. |
| 533 |
// |
| 534 |
switch (type) { |
| 535 |
case PlayerNeutral: |
| 536 |
case PlayerNobody: |
| 537 |
default: |
| 538 |
team = 0; |
| 539 |
player->SetName("Neutral"); |
| 540 |
break; |
| 541 |
case PlayerComputer: |
| 542 |
team = 1; |
| 543 |
player->SetName("Computer"); |
| 544 |
break; |
| 545 |
case PlayerPerson: |
| 546 |
team = 2 + NumPlayers; |
| 547 |
player->SetName("Person"); |
| 548 |
break; |
| 549 |
case PlayerRescuePassive: |
| 550 |
case PlayerRescueActive: |
| 551 |
// FIXME: correct for multiplayer games? |
| 552 |
player->SetName("Computer"); |
| 553 |
team = 2 + NumPlayers; |
| 554 |
break; |
| 555 |
} |
| 556 |
DebugPrint("CreatePlayer name %s\n" _C_ player->Name.c_str()); |
| 557 |
|
| 558 |
player->Type = type; |
| 559 |
player->Team = team; |
| 560 |
player->Enemy = 0; |
| 561 |
player->Allied = 0; |
| 562 |
player->AiName = "ai-passive"; |
| 563 |
|
| 564 |
// |
| 565 |
// Calculate enemy/allied mask. |
| 566 |
// |
| 567 |
for (i = 0; i < NumPlayers; ++i) { |
| 568 |
switch (type) { |
| 569 |
case PlayerNeutral: |
| 570 |
case PlayerNobody: |
| 571 |
default: |
| 572 |
break; |
| 573 |
case PlayerComputer: |
| 574 |
// Computer allied with computer and enemy of all persons. |
| 575 |
if (Players[i].Type == PlayerComputer) { |
| 576 |
player->Allied |= (1 << i); |
| 577 |
Players[i].Allied |= (1 << NumPlayers); |
| 578 |
} else if (Players[i].Type == PlayerPerson || |
| 579 |
Players[i].Type == PlayerRescueActive) { |
| 580 |
player->Enemy |= (1 << i); |
| 581 |
Players[i].Enemy |= (1 << NumPlayers); |
| 582 |
} |
| 583 |
break; |
| 584 |
case PlayerPerson: |
| 585 |
// Humans are enemy of all? |
| 586 |
if (Players[i].Type == PlayerComputer || |
| 587 |
Players[i].Type == PlayerPerson) { |
| 588 |
player->Enemy |= (1 << i); |
| 589 |
Players[i].Enemy |= (1 << NumPlayers); |
| 590 |
} else if (Players[i].Type == PlayerRescueActive || |
| 591 |
Players[i].Type == PlayerRescuePassive) { |
| 592 |
player->Allied |= (1 << i); |
| 593 |
Players[i].Allied |= (1 << NumPlayers); |
| 594 |
} |
| 595 |
break; |
| 596 |
case PlayerRescuePassive: |
| 597 |
// Rescue passive are allied with persons |
| 598 |
if (Players[i].Type == PlayerPerson) { |
| 599 |
player->Allied |= (1 << i); |
| 600 |
Players[i].Allied |= (1 << NumPlayers); |
| 601 |
} |
| 602 |
break; |
| 603 |
case PlayerRescueActive: |
| 604 |
// Rescue active are allied with persons and enemies of computer |
| 605 |
if (Players[i].Type == PlayerComputer) { |
| 606 |
player->Enemy |= (1 << i); |
| 607 |
Players[i].Enemy |= (1 << NumPlayers); |
| 608 |
} else if (Players[i].Type == PlayerPerson) { |
| 609 |
player->Allied |= (1 << i); |
| 610 |
Players[i].Allied |= (1 << NumPlayers); |
| 611 |
} |
| 612 |
break; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
memset(player->UnitTypesCount, 0, sizeof(player->UnitTypesCount)); |
| 617 |
|
| 618 |
player->NumBuildings = 0; |
| 619 |
player->TotalNumUnits = 0; |
| 620 |
player->Score = 0; |
| 621 |
|
| 622 |
player->Color = PlayerColors[NumPlayers][0]; |
| 623 |
|
| 624 |
if (Players[NumPlayers].Type == PlayerComputer || |
| 625 |
Players[NumPlayers].Type == PlayerRescueActive) { |
| 626 |
player->AiEnabled = 1; |
| 627 |
} else { |
| 628 |
player->AiEnabled = 0; |
| 629 |
} |
| 630 |
|
| 631 |
++NumPlayers; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
** Change player name. |
| 636 |
** |
| 637 |
** @param name New name. |
| 638 |
*/ |
| 639 |
void CPlayer::SetName(const std::string &name) |
| 640 |
{ |
| 641 |
Name = name; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
** Clear all player data excepts members which don't change. |
| 646 |
** |
| 647 |
** The fields that are not cleared are |
| 648 |
** UnitLimit, BuildingLimit, TotalUnitLimit and Allow. |
| 649 |
*/ |
| 650 |
void CPlayer::Clear() |
| 651 |
{ |
| 652 |
Index = 0; |
| 653 |
Name.clear(); |
| 654 |
Type = (PlayerTypes)0; |
| 655 |
AiName.clear(); |
| 656 |
Team = 0; |
| 657 |
Enemy = 0; |
| 658 |
Allied = 0; |
| 659 |
SharedVision = 0; |
| 660 |
StartX = 0; |
| 661 |
StartY = 0; |
| 662 |
memset(UnitTypesCount, 0, sizeof(UnitTypesCount)); |
| 663 |
AiEnabled = 0; |
| 664 |
Ai = 0; |
| 665 |
memset(Units, 0, sizeof(Units)); |
| 666 |
TotalNumUnits = 0; |
| 667 |
NumBuildings = 0; |
| 668 |
// FIXME: can't clear limits since it's initialized already |
| 669 |
// UnitLimit = 0; |
| 670 |
// BuildingLimit = 0; |
| 671 |
// TotalUnitLimit = 0; |
| 672 |
Score = 0; |
| 673 |
TotalUnits = 0; |
| 674 |
TotalBuildings = 0; |
| 675 |
memset(TotalResources, 0, sizeof(TotalResources)); |
| 676 |
TotalRazings = 0; |
| 677 |
TotalKills = 0; |
| 678 |
Color = 0; |
| 679 |
ClearResourceVariables(); |
| 680 |
} |
| 681 |
|
| 682 |
/*---------------------------------------------------------------------------- |
| 683 |
-- Resource management |
| 684 |
----------------------------------------------------------------------------*/ |
| 685 |
|
| 686 |
/** |
| 687 |
** Check if the unit-type didn't break any unit limits. |
| 688 |
** |
| 689 |
** @param type Type of unit. |
| 690 |
** |
| 691 |
** @return True if enough, negative on problem. |
| 692 |
** |
| 693 |
** @note The return values of the PlayerCheck functions are inconsistent. |
| 694 |
*/ |
| 695 |
int CPlayer::CheckLimits(const CUnitType *type) const |
| 696 |
{ |
| 697 |
// |
| 698 |
// Check game limits. |
| 699 |
// |
| 700 |
if (NumUnits < UnitMax) { |
| 701 |
if (type->Building && NumBuildings >= BuildingLimit) { |
| 702 |
Notify(NotifyYellow, -1, -1, "%s", _("Building Limit Reached")); |
| 703 |
return -1; |
| 704 |
} |
| 705 |
if (!type->Building && (TotalNumUnits - NumBuildings) >= UnitLimit) { |
| 706 |
Notify(NotifyYellow, -1, -1, "%s", _("Unit Limit Reached")); |
| 707 |
return -2; |
| 708 |
} |
| 709 |
if (TotalNumUnits >= TotalUnitLimit) { |
| 710 |
Notify(NotifyYellow, -1, -1, "%s", _("Total Unit Limit Reached")); |
| 711 |
return -4; |
| 712 |
} |
| 713 |
if (UnitTypesCount[type->Slot] >= Allow.Units[type->Slot]) { |
| 714 |
Notify(NotifyYellow, -1, -1, _("Limit of %d reached for this unit type"), |
| 715 |
Allow.Units[type->Slot]); |
| 716 |
return -6; |
| 717 |
} |
| 718 |
return 1; |
| 719 |
} else { |
| 720 |
Notify(NotifyYellow, -1, -1, "%s", _("Cannot create more units.")); |
| 721 |
if (AiEnabled) { |
| 722 |
// AiNoMoreUnits(player, type); |
| 723 |
} |
| 724 |
return -5; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
** Have unit of type. |
| 730 |
** |
| 731 |
** @param type Type of unit. |
| 732 |
** |
| 733 |
** @return How many exists, false otherwise. |
| 734 |
*/ |
| 735 |
int CPlayer::HaveUnitTypeByType(const CUnitType *type) const |
| 736 |
{ |
| 737 |
return UnitTypesCount[type->Slot]; |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
** Have unit of type. |
| 742 |
** |
| 743 |
** @param ident Identifier of unit-type that should be lookuped. |
| 744 |
** |
| 745 |
** @return How many exists, false otherwise. |
| 746 |
** |
| 747 |
** @note This function should not be used during run time. |
| 748 |
*/ |
| 749 |
int CPlayer::HaveUnitTypeByIdent(const std::string &ident) const |
| 750 |
{ |
| 751 |
return UnitTypesCount[UnitTypeByIdent(ident)->Slot]; |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
** Initialize the Ai for all players. |
| 756 |
*/ |
| 757 |
void PlayersInitAi(void) |
| 758 |
{ |
| 759 |
for (int player = 0; player < NumPlayers; ++player) { |
| 760 |
if (Players[player].AiEnabled) { |
| 761 |
AiInit(&Players[player]); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
** Handle AI of all players each game cycle. |
| 768 |
*/ |
| 769 |
void PlayersEachCycle(void) |
| 770 |
{ |
| 771 |
for (int player = 0; player < NumPlayers; ++player) { |
| 772 |
CPlayer *p = &Players[player]; |
| 773 |
|
| 774 |
// Update rate based economy |
| 775 |
for (int res = 0; res < MaxCosts; ++res) { |
| 776 |
int rate = p->ProductionRate[res] - p->ActualUtilizationRate[res]; |
| 777 |
if (rate > 0) { |
| 778 |
if (p->StoredResources[res] < p->StorageCapacity[res]) { |
| 779 |
p->StoredResources[res] += rate; |
| 780 |
if (p->StoredResources[res] > p->StorageCapacity[res]) { |
| 781 |
p->StoredResources[res] = p->StorageCapacity[res]; |
| 782 |
} |
| 783 |
} |
| 784 |
} else if (rate < 0) { |
| 785 |
rate = -rate; |
| 786 |
// Assert(p->StoredResources[res] >= rate); |
| 787 |
p->StoredResources[res] -= rate; |
| 788 |
if (p->StoredResources[res] < 0) { |
| 789 |
p->StoredResources[res] = 0; |
| 790 |
} |
| 791 |
} |
| 792 |
p->TotalResources[res] += p->ProductionRate[res]; |
| 793 |
} |
| 794 |
|
| 795 |
// Recalculate costs |
| 796 |
std::map<CUnit *, int *>::iterator it; |
| 797 |
for (it = p->UnitsConsumingResourcesActual.begin(); it != p->UnitsConsumingResourcesActual.end(); ++it) { |
| 798 |
int costs[MaxCosts]; |
| 799 |
CalculateCosts((*it).first, costs); |
| 800 |
p->UpdateUnitsConsumingResources((*it).first, costs); |
| 801 |
} |
| 802 |
|
| 803 |
// Ai |
| 804 |
if (p->AiEnabled) { |
| 805 |
AiEachCycle(p); |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
** Handle AI of a player each second. |
| 812 |
** |
| 813 |
** @param player the player to update AI |
| 814 |
*/ |
| 815 |
void PlayersEachSecond(int player) |
| 816 |
{ |
| 817 |
if (Players[player].AiEnabled) { |
| 818 |
AiEachSecond(&Players[player]); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
/** |
| 823 |
** Change current color set to new player. |
| 824 |
** |
| 825 |
** FIXME: use function pointer here. |
| 826 |
** |
| 827 |
** @param player Pointer to player. |
| 828 |
** @param sprite The sprite in which the colors should be changed. |
| 829 |
*/ |
| 830 |
void GraphicPlayerPixels(CPlayer *player, const CGraphic *sprite) |
| 831 |
{ |
| 832 |
Assert(PlayerColorIndexCount); |
| 833 |
|
| 834 |
if (sprite->Surface->format->palette == NULL) { |
| 835 |
// Cannot set the player colors when there is no palette. |
| 836 |
return; |
| 837 |
} |
| 838 |
|
| 839 |
// Skip units whose color palette does not cover the indexes |
| 840 |
// for the player color. |
| 841 |
if (sprite->Surface->format->palette->ncolors < PlayerColorIndexStart + PlayerColorIndexCount) { |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
SDL_LockSurface(sprite->Surface); |
| 846 |
SDL_SetColors(sprite->Surface, player->UnitColors.Colors, |
| 847 |
PlayerColorIndexStart, PlayerColorIndexCount); |
| 848 |
if (sprite->SurfaceFlip) { |
| 849 |
// The flipped surface is supposed to have a similar palette. |
| 850 |
Assert(sprite->SurfaceFlip->format->palette->ncolors |
| 851 |
== sprite->Surface->format->palette->ncolors); |
| 852 |
SDL_SetColors(sprite->SurfaceFlip, |
| 853 |
player->UnitColors.Colors, PlayerColorIndexStart, PlayerColorIndexCount); |
| 854 |
} |
| 855 |
SDL_UnlockSurface(sprite->Surface); |
| 856 |
} |
| 857 |
|
| 858 |
/** |
| 859 |
** Setup the player colors for the current palette. |
| 860 |
** |
| 861 |
** @todo FIXME: could be called before PixelsXX is setup. |
| 862 |
*/ |
| 863 |
void SetPlayersPalette(void) |
| 864 |
{ |
| 865 |
for (int i = 0; i < PlayerMax; ++i) { |
| 866 |
delete[] Players[i].UnitColors.Colors; |
| 867 |
Players[i].UnitColors.Colors = new SDL_Color[PlayerColorIndexCount]; |
| 868 |
memcpy(Players[i].UnitColors.Colors, PlayerColorsRGB[i], |
| 869 |
sizeof(SDL_Color) * PlayerColorIndexCount); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
/** |
| 874 |
** Output debug informations for players. |
| 875 |
*/ |
| 876 |
void DebugPlayers(void) |
| 877 |
{ |
| 878 |
#ifdef DEBUG |
| 879 |
int i; |
| 880 |
const char *playertype; |
| 881 |
|
| 882 |
DebugPrint("Nr Color I Name Type Ai\n"); |
| 883 |
DebugPrint("-- -------- - -------- ------------ -----\n"); |
| 884 |
for (i = 0; i < PlayerMax; ++i) { |
| 885 |
if (Players[i].Type == PlayerNobody) { |
| 886 |
continue; |
| 887 |
} |
| 888 |
switch (Players[i].Type) { |
| 889 |
case 2: playertype = "neutral "; break; |
| 890 |
case 3: playertype = "nobody "; break; |
| 891 |
case 4: playertype = "computer "; break; |
| 892 |
case 5: playertype = "person "; break; |
| 893 |
case 6: playertype = "rescue pas. "; break; |
| 894 |
case 7: playertype = "rescue akt. "; break; |
| 895 |
default : playertype = "?unknown? "; break; |
| 896 |
} |
| 897 |
DebugPrint("%2d: %8.8s %c %-8.8s %s %s\n" _C_ i _C_ PlayerColorNames[i].c_str() _C_ |
| 898 |
ThisPlayer == &Players[i] ? '*' : |
| 899 |
Players[i].AiEnabled ? '+' : ' ' _C_ |
| 900 |
Players[i].Name.c_str() _C_ playertype _C_ |
| 901 |
Players[i].AiName.c_str()); |
| 902 |
} |
| 903 |
#endif |
| 904 |
} |
| 905 |
|
| 906 |
/** |
| 907 |
** Notify player about a problem. |
| 908 |
** |
| 909 |
** @param type Problem type |
| 910 |
** @param x Map X tile position |
| 911 |
** @param y Map Y tile position |
| 912 |
** @param fmt Message format |
| 913 |
** @param ... Message varargs |
| 914 |
** |
| 915 |
** @note The parameter type, isn't yet used. |
| 916 |
** @todo FIXME: We must also notfiy allied players. |
| 917 |
*/ |
| 918 |
void CPlayer::Notify(int type, int x, int y, const char *fmt, ...) const |
| 919 |
{ |
| 920 |
char temp[128]; |
| 921 |
va_list va; |
| 922 |
|
| 923 |
// Notify me, and my TEAM members |
| 924 |
if (this != ThisPlayer && !IsTeamed(ThisPlayer)) { |
| 925 |
return; |
| 926 |
} |
| 927 |
|
| 928 |
va_start(va, fmt); |
| 929 |
temp[sizeof(temp) - 1] = '\0'; |
| 930 |
vsnprintf(temp, sizeof(temp) - 1, fmt, va); |
| 931 |
va_end(va); |
| 932 |
|
| 933 |
if (x != -1) { |
| 934 |
UI.Minimap.AddEvent(x, y); |
| 935 |
} |
| 936 |
if (this == ThisPlayer) { |
| 937 |
SetMessageEvent(x, y, "%s", temp); |
| 938 |
} else { |
| 939 |
SetMessageEvent(x, y, "(%s): %s", Name.c_str(), temp); |
| 940 |
} |
| 941 |
|
| 942 |
} |
| 943 |
|
| 944 |
/** |
| 945 |
** Check if the player is an enemy |
| 946 |
*/ |
| 947 |
bool CPlayer::IsEnemy(const CPlayer *x) const |
| 948 |
{ |
| 949 |
return (Enemy & (1 << x->Index)) != 0; |
| 950 |
} |
| 951 |
|
| 952 |
/** |
| 953 |
** Check if the unit is an enemy |
| 954 |
*/ |
| 955 |
bool CPlayer::IsEnemy(const CUnit *x) const |
| 956 |
{ |
| 957 |
return IsEnemy(x->Player); |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
** Check if the player is an ally |
| 962 |
*/ |
| 963 |
bool CPlayer::IsAllied(const CPlayer *x) const |
| 964 |
{ |
| 965 |
return (Allied & (1 << x->Index)) != 0; |
| 966 |
} |
| 967 |
|
| 968 |
/** |
| 969 |
** Check if the unit is an ally |
| 970 |
*/ |
| 971 |
bool CPlayer::IsAllied(const CUnit *x) const |
| 972 |
{ |
| 973 |
return IsAllied(x->Player); |
| 974 |
} |
| 975 |
|
| 976 |
/** |
| 977 |
** Check if the player shares vision with the player |
| 978 |
*/ |
| 979 |
bool CPlayer::IsSharedVision(const CPlayer *x) const |
| 980 |
{ |
| 981 |
return (SharedVision & (1 << x->Index)) != 0; |
| 982 |
} |
| 983 |
|
| 984 |
/** |
| 985 |
** Check if the player shares vision with the unit |
| 986 |
*/ |
| 987 |
bool CPlayer::IsSharedVision(const CUnit *x) const |
| 988 |
{ |
| 989 |
return IsSharedVision(x->Player); |
| 990 |
} |
| 991 |
|
| 992 |
/** |
| 993 |
** Check if the both players share vision |
| 994 |
*/ |
| 995 |
bool CPlayer::IsBothSharedVision(const CPlayer *x) const |
| 996 |
{ |
| 997 |
return (SharedVision & (1 << x->Index)) != 0 && |
| 998 |
(x->SharedVision & (1 << Index)) != 0; |
| 999 |
} |
| 1000 |
|
| 1001 |
/** |
| 1002 |
** Check if the player and the unit share vision |
| 1003 |
*/ |
| 1004 |
bool CPlayer::IsBothSharedVision(const CUnit *x) const |
| 1005 |
{ |
| 1006 |
return IsBothSharedVision(x->Player); |
| 1007 |
} |
| 1008 |
|
| 1009 |
/** |
| 1010 |
** Check if the player is teamed |
| 1011 |
*/ |
| 1012 |
bool CPlayer::IsTeamed(const CPlayer *x) const |
| 1013 |
{ |
| 1014 |
return Team == x->Team; |
| 1015 |
} |
| 1016 |
|
| 1017 |
/** |
| 1018 |
** Check if the unit is teamed |
| 1019 |
*/ |
| 1020 |
bool CPlayer::IsTeamed(const CUnit *x) const |
| 1021 |
{ |
| 1022 |
return IsTeamed(x->Player); |
| 1023 |
} |
| 1024 |
|
| 1025 |
//@} |