| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name action_build.cpp - The build building action. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer, Jimmy Salmon, and |
| 13 |
// Russell Smith |
| 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 <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
#include "unittype.h" |
| 41 |
#include "animation.h" |
| 42 |
#include "player.h" |
| 43 |
#include "unit.h" |
| 44 |
#include "unit_cache.h" |
| 45 |
#include "sound.h" |
| 46 |
#include "actions.h" |
| 47 |
#include "map.h" |
| 48 |
#include "ai.h" |
| 49 |
#include "interface.h" |
| 50 |
#include "pathfinder.h" |
| 51 |
#include "construct.h" |
| 52 |
|
| 53 |
/*---------------------------------------------------------------------------- |
| 54 |
-- Functions |
| 55 |
----------------------------------------------------------------------------*/ |
| 56 |
|
| 57 |
/** |
| 58 |
** Get the production cost of a unit type |
| 59 |
** Use the energy cost if it's not 0, otherwise use magma cost |
| 60 |
*/ |
| 61 |
static int GetProductionCost(CUnitType *type) |
| 62 |
{ |
| 63 |
int *costs = type->ProductionCosts; |
| 64 |
return costs[EnergyCost] ? costs[EnergyCost] : costs[MagmaCost]; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
** Update construction frame |
| 69 |
** |
| 70 |
** @param unit The building under construction. |
| 71 |
*/ |
| 72 |
static void UpdateConstructionFrame(CUnit *unit) |
| 73 |
{ |
| 74 |
CConstructionFrame *cframe; |
| 75 |
CConstructionFrame *tmp; |
| 76 |
int percent = unit->Data.Built.Progress * 100 / GetProductionCost(unit->Type); |
| 77 |
|
| 78 |
// Find which construction frame to use |
| 79 |
cframe = tmp = unit->Type->Construction->Frames; |
| 80 |
while (tmp) { |
| 81 |
if (percent < tmp->Percent) { |
| 82 |
break; |
| 83 |
} |
| 84 |
cframe = tmp; |
| 85 |
tmp = tmp->Next; |
| 86 |
} |
| 87 |
|
| 88 |
// Update the frame |
| 89 |
if (cframe != unit->Data.Built.Frame) { |
| 90 |
unit->Data.Built.Frame = cframe; |
| 91 |
if (unit->Frame < 0) { |
| 92 |
unit->Frame = -cframe->Frame - 1; |
| 93 |
} else { |
| 94 |
unit->Frame = cframe->Frame; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
** Move to build location |
| 101 |
** |
| 102 |
** @param unit Unit to move |
| 103 |
*/ |
| 104 |
static void MoveToLocation(CUnit *unit) |
| 105 |
{ |
| 106 |
// First entry |
| 107 |
if (!unit->SubAction) { |
| 108 |
unit->SubAction = 1; |
| 109 |
NewResetPath(unit); |
| 110 |
} |
| 111 |
|
| 112 |
if (unit->Wait) { |
| 113 |
// FIXME: show still animation while we wait? |
| 114 |
unit->Wait--; |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
switch (DoActionMove(unit)) { // reached end-point? |
| 119 |
case PF_UNREACHABLE: |
| 120 |
// |
| 121 |
// Some tries to reach the goal |
| 122 |
// |
| 123 |
if (unit->SubAction++ < 10) { |
| 124 |
// To keep the load low, retry each 10 cycles |
| 125 |
// NOTE: we can already inform the AI about this problem? |
| 126 |
unit->Wait = 10; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 131 |
"%s", _("You cannot reach building place")); |
| 132 |
if (unit->Player->AiEnabled) { |
| 133 |
AiCanNotReach(unit, unit->Orders[0]->Type); |
| 134 |
} |
| 135 |
|
| 136 |
unit->ClearAction(); |
| 137 |
return; |
| 138 |
|
| 139 |
case PF_REACHED: |
| 140 |
unit->SubAction = 20; |
| 141 |
return; |
| 142 |
|
| 143 |
default: |
| 144 |
// Moving... |
| 145 |
return; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
** Check if we're already building this type at this location |
| 151 |
*/ |
| 152 |
static CUnit *CheckAlreadyBuilding(CUnit *unit, CUnitType *type, int x, int y) |
| 153 |
{ |
| 154 |
CUnit *table[UnitMax]; |
| 155 |
int n; |
| 156 |
|
| 157 |
n = UnitCache.Select(x, y, table, UnitMax); |
| 158 |
for (int i = 0; i < n; ++i) { |
| 159 |
if (!table[i]->Destroyed && table[i]->Type == type && |
| 160 |
(table[i]->Player == unit->Player || unit->IsAllied(table[i]))) { |
| 161 |
return table[i]; |
| 162 |
} |
| 163 |
} |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
** Check if the unit can build |
| 169 |
** |
| 170 |
** @param unit Unit to check |
| 171 |
*/ |
| 172 |
static CUnit *CheckCanBuild(CUnit *unit) |
| 173 |
{ |
| 174 |
int x; |
| 175 |
int y; |
| 176 |
CUnitType *type; |
| 177 |
CUnit *ontop; |
| 178 |
|
| 179 |
if (unit->Wait) { |
| 180 |
// FIXME: show still animation while we wait? |
| 181 |
unit->Wait--; |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
x = unit->Orders[0]->X; |
| 186 |
y = unit->Orders[0]->Y; |
| 187 |
type = unit->Orders[0]->Type; |
| 188 |
|
| 189 |
// |
| 190 |
// Check if the building could be built there. |
| 191 |
// |
| 192 |
if ((ontop = CanBuildUnitType(unit, type, x, y, 1)) == NULL) { |
| 193 |
if ((ontop = CheckAlreadyBuilding(unit, type, x, y)) != NULL) { |
| 194 |
unit->Orders[0]->Init(); |
| 195 |
unit->Orders[0]->Action = UnitActionRepair; |
| 196 |
unit->Orders[0]->Goal = ontop; |
| 197 |
unit->Orders[0]->Goal->RefsIncrease(); |
| 198 |
unit->Orders[0]->Range = unit->Type->RepairRange; |
| 199 |
unit->SubAction = 0; |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
// |
| 204 |
// Some tries to build the building. |
| 205 |
// |
| 206 |
if (unit->SubAction++ < 30) { |
| 207 |
// To keep the load low, retry each 10 cycles |
| 208 |
// NOTE: we can already inform the AI about this problem? |
| 209 |
unit->Wait = 10; |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 214 |
_("You cannot build %s here"), type->Name.c_str()); |
| 215 |
if (unit->Player->AiEnabled) { |
| 216 |
AiCanNotBuild(unit, type); |
| 217 |
} |
| 218 |
|
| 219 |
unit->ClearAction(); |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
|
| 223 |
// |
| 224 |
// Check if hiting any limits for the building. |
| 225 |
// |
| 226 |
if (unit->Player->CheckLimits(type) < 0) { |
| 227 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 228 |
_("Can't build more units %s"), type->Name.c_str()); |
| 229 |
if (unit->Player->AiEnabled) { |
| 230 |
AiCanNotBuild(unit, type); |
| 231 |
} |
| 232 |
|
| 233 |
unit->ClearAction(); |
| 234 |
return NULL; |
| 235 |
} |
| 236 |
|
| 237 |
return ontop; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
** Start building |
| 242 |
*/ |
| 243 |
static void StartBuilding(CUnit *unit, CUnit *ontop) |
| 244 |
{ |
| 245 |
int x; |
| 246 |
int y; |
| 247 |
CUnitType *type; |
| 248 |
CUnit *build; |
| 249 |
|
| 250 |
x = unit->Orders[0]->X; |
| 251 |
y = unit->Orders[0]->Y; |
| 252 |
type = unit->Orders[0]->Type; |
| 253 |
|
| 254 |
build = MakeUnit(type, unit->Player); |
| 255 |
|
| 256 |
// If unable to make unit, stop, and report message |
| 257 |
if (build == NoUnitP) { |
| 258 |
// FIXME: Should we retry this? |
| 259 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 260 |
_("Unable to create building %s"), type->Name.c_str()); |
| 261 |
if (unit->Player->AiEnabled) { |
| 262 |
AiCanNotBuild(unit, type); |
| 263 |
} |
| 264 |
|
| 265 |
unit->ClearAction(); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
build->Constructed = 1; |
| 270 |
build->CurrentSightRange = 0; |
| 271 |
|
| 272 |
// Building on top of something, may remove what is beneath it |
| 273 |
if (ontop != unit) { |
| 274 |
CBuildRestrictionOnTop *b; |
| 275 |
|
| 276 |
build->ProductionEfficiency = ontop->Type->ProductionEfficiency; |
| 277 |
b = static_cast<CBuildRestrictionOnTop *>(OnTopDetails(build, ontop->Type)); |
| 278 |
Assert(b); |
| 279 |
if (b->ReplaceOnBuild) { |
| 280 |
// We capture the value of what is beneath. |
| 281 |
memcpy(build->ResourcesHeld, ontop->ResourcesHeld, sizeof(build->ResourcesHeld)); |
| 282 |
ontop->Remove(NULL); // Destroy building beneath |
| 283 |
UnitLost(ontop); |
| 284 |
UnitClearOrders(ontop); |
| 285 |
ontop->Release(); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
// Must set action before placing, otherwise it will incorrectly mark radar |
| 290 |
build->Orders[0]->Action = UnitActionBuilt; |
| 291 |
|
| 292 |
// Must place after previous for map flags |
| 293 |
build->Place(x, y); |
| 294 |
|
| 295 |
// HACK: the building is not ready yet |
| 296 |
build->Player->UnitTypesCount[type->Slot]--; |
| 297 |
|
| 298 |
// Make sure the building doesn't cancel itself out right away. |
| 299 |
build->Data.Built.Progress = 0; |
| 300 |
build->Variable[HP_INDEX].Value = 1; |
| 301 |
UpdateConstructionFrame(build); |
| 302 |
|
| 303 |
// We need somebody to work on it. |
| 304 |
// Use repair to do the building |
| 305 |
unit->Orders[0]->Action = UnitActionRepair; |
| 306 |
unit->Orders[0]->Goal = build; |
| 307 |
unit->Orders[0]->X = unit->Orders[0]->Y = -1; |
| 308 |
unit->Orders[0]->Range = unit->Type->RepairRange; |
| 309 |
unit->SubAction = 0; |
| 310 |
unit->Direction = DirectionToHeading(x - unit->X, y - unit->Y); |
| 311 |
UnitUpdateHeading(unit); |
| 312 |
build->RefsIncrease(); |
| 313 |
// Mark the new building seen. |
| 314 |
MapMarkUnitSight(build); |
| 315 |
|
| 316 |
UpdateConstructionFrame(build); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
** Unit builds a building. |
| 321 |
** |
| 322 |
** @param unit Unit that builds a building |
| 323 |
*/ |
| 324 |
void HandleActionBuild(CUnit *unit) |
| 325 |
{ |
| 326 |
CUnit *ontop; |
| 327 |
|
| 328 |
if (unit->SubAction <= 10) { |
| 329 |
MoveToLocation(unit); |
| 330 |
} |
| 331 |
if (20 <= unit->SubAction && unit->SubAction <= 30) { |
| 332 |
if ((ontop = CheckCanBuild(unit))) { |
| 333 |
StartBuilding(unit, ontop); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/** |
| 340 |
** Unit under Construction |
| 341 |
** |
| 342 |
** @param unit Unit that is being built |
| 343 |
*/ |
| 344 |
void HandleActionBuilt(CUnit *unit) |
| 345 |
{ |
| 346 |
int pcost = GetProductionCost(unit->Type); |
| 347 |
|
| 348 |
// |
| 349 |
// Check if construction should be canceled |
| 350 |
// |
| 351 |
if (unit->Data.Built.Cancel || unit->Data.Built.Progress < 0) { |
| 352 |
DebugPrint("%s canceled.\n" _C_ unit->Type->Name.c_str()); |
| 353 |
|
| 354 |
// Cancel building |
| 355 |
LetUnitDie(unit); |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
// |
| 360 |
// Check if building ready. Note we can both build and repair. |
| 361 |
// |
| 362 |
if (unit->Data.Built.Progress >= pcost || |
| 363 |
unit->Variable[HP_INDEX].Value >= unit->Stats->Variables[HP_INDEX].Max) { |
| 364 |
DebugPrint("Building ready.\n"); |
| 365 |
if (unit->Variable[HP_INDEX].Value > unit->Stats->Variables[HP_INDEX].Max) { |
| 366 |
unit->Variable[HP_INDEX].Value = unit->Stats->Variables[HP_INDEX].Max; |
| 367 |
} |
| 368 |
unit->ClearAction(); |
| 369 |
// HACK: the building is ready now |
| 370 |
unit->Player->UnitTypesCount[unit->Type->Slot]++; |
| 371 |
unit->Constructed = 0; |
| 372 |
if (unit->Frame < 0) { |
| 373 |
unit->Frame = -unit->Type->StillFrame - 1; |
| 374 |
} else { |
| 375 |
unit->Frame = unit->Type->StillFrame; |
| 376 |
} |
| 377 |
|
| 378 |
unit->Player->Notify(NotifyGreen, unit->X, unit->Y, |
| 379 |
_("New %s done"), unit->Type->Name.c_str()); |
| 380 |
if (unit->Player == ThisPlayer) { |
| 381 |
if (unit->Type->Sound.Ready.Sound) { |
| 382 |
PlayUnitSound(unit, VoiceReady); |
| 383 |
} else { |
| 384 |
PlayUnitSound(unit, VoiceBuilding); |
| 385 |
} |
| 386 |
} |
| 387 |
if (unit->Player->AiEnabled) { |
| 388 |
AiWorkComplete(NULL, unit); |
| 389 |
} |
| 390 |
|
| 391 |
UpdateForNewUnit(unit, 0); |
| 392 |
|
| 393 |
// Set the direction of the building if it supports them |
| 394 |
if (unit->Type->NumDirections > 1) { |
| 395 |
unit->Direction = (MyRand() >> 3) & 0xFF; // random heading |
| 396 |
UnitUpdateHeading(unit); |
| 397 |
} |
| 398 |
|
| 399 |
if (IsOnlySelected(unit) || unit->Player == ThisPlayer) { |
| 400 |
SelectedUnitChanged(); |
| 401 |
} |
| 402 |
unit->CurrentSightRange = unit->Stats->Variables[SIGHTRANGE_INDEX].Max; |
| 403 |
MapMarkUnitSight(unit); |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
UpdateConstructionFrame(unit); |
| 408 |
} |
| 409 |
|
| 410 |
//@} |