| 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_train.cpp - The building train action. */ |
| 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 <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
|
| 38 |
#include "stratagus.h" |
| 39 |
#include "video.h" |
| 40 |
#include "sound.h" |
| 41 |
#include "unitsound.h" |
| 42 |
#include "unittype.h" |
| 43 |
#include "animation.h" |
| 44 |
#include "player.h" |
| 45 |
#include "unit.h" |
| 46 |
#include "actions.h" |
| 47 |
#include "sound.h" |
| 48 |
#include "ai.h" |
| 49 |
#include "interface.h" |
| 50 |
#include "ui.h" |
| 51 |
|
| 52 |
/*---------------------------------------------------------------------------- |
| 53 |
-- Functions |
| 54 |
----------------------------------------------------------------------------*/ |
| 55 |
|
| 56 |
/** |
| 57 |
** Get the production cost of a unit type |
| 58 |
** Use the energy cost if it's not 0, otherwise use magma cost |
| 59 |
*/ |
| 60 |
static int GetProductionCost(CUnitType *type) |
| 61 |
{ |
| 62 |
int *costs = type->ProductionCosts; |
| 63 |
return costs[EnergyCost] ? costs[EnergyCost] : costs[MagmaCost]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
** Unit can handle order. |
| 68 |
** |
| 69 |
** @param unit Newly trained unit. |
| 70 |
** @param order New order for the unit. |
| 71 |
** |
| 72 |
** @return 1 if the the unit can do it, 0 otherwise. |
| 73 |
*/ |
| 74 |
static int CanHandleOrder(CUnit *unit, COrder *order) |
| 75 |
{ |
| 76 |
if (order->Action == UnitActionResource) { |
| 77 |
// Check if new unit can harvest. |
| 78 |
if (!unit->Type->Harvester) { |
| 79 |
return 0; |
| 80 |
} |
| 81 |
return 1; |
| 82 |
} |
| 83 |
if (order->Action == UnitActionRepair) { |
| 84 |
return !!unit->Type->RepairRange; |
| 85 |
} |
| 86 |
if (order->Action == UnitActionAttack && !unit->Type->CanAttack) { |
| 87 |
return 0; |
| 88 |
} |
| 89 |
if (order->Action == UnitActionBoard && unit->Type->UnitType != UnitTypeLand) { |
| 90 |
return 0; |
| 91 |
} |
| 92 |
return 1; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
** Unit trains unit! |
| 97 |
** |
| 98 |
** @param unit Unit that trains. |
| 99 |
*/ |
| 100 |
void HandleActionTrain(CUnit *unit) |
| 101 |
{ |
| 102 |
CUnit *nunit; |
| 103 |
const CUnitType *type; |
| 104 |
CPlayer *player; |
| 105 |
int food; |
| 106 |
int pcost = GetProductionCost(unit->Orders[0]->Type); |
| 107 |
|
| 108 |
// |
| 109 |
// First entry |
| 110 |
// |
| 111 |
if (!unit->SubAction) { |
| 112 |
// Check the terrain here rather than in |
| 113 |
// CommandTrainUnit because, if the train command is |
| 114 |
// queued after a move, CommandTrainUnit might not |
| 115 |
// know in advance where the move will end. Usually |
| 116 |
// though, units that can train are buildings and |
| 117 |
// unable to move. |
| 118 |
if (!TerrainAllowsTraining(unit, unit->Orders[0]->Type)) { |
| 119 |
unit->Player->Notify(NotifyYellow, unit->X, unit->Y, |
| 120 |
_("Cannot train %s here"), |
| 121 |
unit->Orders[0]->Type->Name.c_str()); |
| 122 |
|
| 123 |
if (unit->Player->AiEnabled) { |
| 124 |
// There is no AiCanNotTrain but this |
| 125 |
// call will work. To the AI, the |
| 126 |
// primary difference between training |
| 127 |
// and building is that trained units |
| 128 |
// get drafted into forces; there is |
| 129 |
// no such unit here because the |
| 130 |
// training attempt failed. |
| 131 |
AiCanNotBuild(unit, unit->Orders[0]->Type); |
| 132 |
} |
| 133 |
|
| 134 |
if (unit->OrderCount == 1) { |
| 135 |
unit->ClearAction(); |
| 136 |
} else { |
| 137 |
unit->OrderFlush = 1; |
| 138 |
unit->SubAction = 0; |
| 139 |
} |
| 140 |
|
| 141 |
if (IsOnlySelected(unit)) { |
| 142 |
UI.ButtonPanel.Update(); |
| 143 |
} |
| 144 |
|
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
unit->Data.Train.Ticks = 0; |
| 149 |
unit->SubAction = 1; |
| 150 |
|
| 151 |
int costs[MaxCosts]; |
| 152 |
CalculateRequestedAmount(unit->Type, unit->Orders[0]->Type->ProductionCosts, costs); |
| 153 |
unit->Player->AddToUnitsConsumingResources(unit, costs); |
| 154 |
} else { |
| 155 |
int *costs = unit->Player->UnitsConsumingResourcesActual[unit]; |
| 156 |
int cost = costs[EnergyCost] ? costs[EnergyCost] : costs[MagmaCost]; |
| 157 |
unit->Data.Train.Ticks += cost * SpeedTrain; |
| 158 |
if (unit->Data.Train.Ticks > pcost) { |
| 159 |
unit->Data.Train.Ticks = pcost; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
unit->Type->Animations->Train ? |
| 164 |
UnitShowAnimation(unit, unit->Type->Animations->Train) : |
| 165 |
UnitShowAnimation(unit, unit->Type->Animations->Still); |
| 166 |
if (unit->Wait) { |
| 167 |
unit->Wait--; |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
player = unit->Player; |
| 172 |
|
| 173 |
if (unit->Data.Train.Ticks >= pcost) { |
| 174 |
unit->Data.Train.Ticks = pcost; |
| 175 |
|
| 176 |
// |
| 177 |
// Check if there are still unit slots. |
| 178 |
// |
| 179 |
if (NumUnits >= UnitMax) { |
| 180 |
unit->Wait = CYCLES_PER_SECOND / 6; |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
// |
| 185 |
// Check if enough supply available. |
| 186 |
// |
| 187 |
food = player->CheckLimits(unit->Orders[0]->Type); |
| 188 |
if (food < 0) { |
| 189 |
unit->Data.Train.Ticks = pcost; |
| 190 |
unit->Wait = CYCLES_PER_SECOND / 6; |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
nunit = MakeUnit(unit->Orders[0]->Type, player); |
| 195 |
if (nunit != NoUnitP) { |
| 196 |
nunit->X = unit->X; |
| 197 |
nunit->Y = unit->Y; |
| 198 |
type = unit->Type; |
| 199 |
|
| 200 |
// DropOutOnSide set unit to belong to the building |
| 201 |
// training it. This was an ugly hack, setting X and Y is enough, |
| 202 |
// no need to add the unit only to be removed. |
| 203 |
nunit->X = unit->X; |
| 204 |
nunit->Y = unit->Y; |
| 205 |
|
| 206 |
player->RemoveFromUnitsConsumingResources(unit); |
| 207 |
|
| 208 |
// New unit might supply food |
| 209 |
UpdateForNewUnit(nunit, 0); |
| 210 |
|
| 211 |
DropOutOnSide(nunit, LookingW, type->TileWidth, type->TileHeight); |
| 212 |
|
| 213 |
// Set life span |
| 214 |
if (type->DecayRate) { |
| 215 |
nunit->TTL = GameCycle + type->DecayRate * 6 * CYCLES_PER_SECOND; |
| 216 |
} |
| 217 |
|
| 218 |
player->Notify(NotifyYellow, nunit->X, nunit->Y, |
| 219 |
_("New %s ready"), nunit->Type->Name.c_str()); |
| 220 |
if (player == ThisPlayer) { |
| 221 |
PlayUnitSound(nunit, VoiceReady); |
| 222 |
} |
| 223 |
if (unit->Player->AiEnabled) { |
| 224 |
AiTrainingComplete(unit, nunit); |
| 225 |
} |
| 226 |
|
| 227 |
if (unit->OrderCount == 1) { |
| 228 |
unit->ClearAction(); |
| 229 |
} else { |
| 230 |
unit->OrderFlush = 1; |
| 231 |
unit->SubAction = 0; |
| 232 |
} |
| 233 |
|
| 234 |
if (unit->NewOrder.Goal) { |
| 235 |
if (unit->NewOrder.Goal->Destroyed) { |
| 236 |
DebugPrint("Destroyed unit in train unit\n"); |
| 237 |
unit->NewOrder.Goal->RefsDecrease(); |
| 238 |
unit->NewOrder.Goal = NoUnitP; |
| 239 |
unit->NewOrder.Action = UnitActionStill; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if (!CanHandleOrder(nunit, &unit->NewOrder)) { |
| 244 |
DebugPrint("Wrong order for unit\n"); |
| 245 |
|
| 246 |
// Tell the unit to move instead of trying any funny stuff. |
| 247 |
*nunit->Orders[0] = unit->NewOrder; |
| 248 |
nunit->Orders[0]->Action = UnitActionMove; |
| 249 |
if (nunit->Orders[0]->Goal) { |
| 250 |
nunit->Orders[0]->Goal->RefsIncrease(); |
| 251 |
} |
| 252 |
} else { |
| 253 |
*nunit->Orders[0] = unit->NewOrder; |
| 254 |
|
| 255 |
// |
| 256 |
// FIXME: Pending command uses any references? |
| 257 |
// |
| 258 |
if (nunit->Orders[0]->Goal) { |
| 259 |
nunit->Orders[0]->Goal->RefsIncrease(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
if (IsOnlySelected(unit)) { |
| 264 |
UI.ButtonPanel.Update(); |
| 265 |
} |
| 266 |
return; |
| 267 |
} else { |
| 268 |
player->Notify(NotifyYellow, unit->X, unit->Y, |
| 269 |
_("Unable to train %s"), unit->Orders[0]->Type->Name.c_str()); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
unit->Wait = CYCLES_PER_SECOND / 6; |
| 274 |
} |
| 275 |
|
| 276 |
//@} |