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