| 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_board.cpp - The board 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 "unittype.h" |
| 40 |
#include "animation.h" |
| 41 |
#include "player.h" |
| 42 |
#include "unit.h" |
| 43 |
#include "actions.h" |
| 44 |
#include "interface.h" |
| 45 |
#include "pathfinder.h" |
| 46 |
#include "map.h" |
| 47 |
|
| 48 |
/*---------------------------------------------------------------------------- |
| 49 |
-- Functions |
| 50 |
----------------------------------------------------------------------------*/ |
| 51 |
|
| 52 |
/** |
| 53 |
** Move to transporter. |
| 54 |
** |
| 55 |
** @param unit Pointer to unit, moving to transporter. |
| 56 |
** |
| 57 |
** @return >0 remaining path length, 0 wait for path, -1 |
| 58 |
** reached goal, -2 can't reach the goal. |
| 59 |
*/ |
| 60 |
static int MoveToTransporter(CUnit *unit) |
| 61 |
{ |
| 62 |
int i; |
| 63 |
int x; |
| 64 |
int y; |
| 65 |
|
| 66 |
x = unit->X; |
| 67 |
y = unit->Y; |
| 68 |
i = DoActionMove(unit); |
| 69 |
// We have to reset a lot, or else they will circle each other and stuff. |
| 70 |
if (x != unit->X || y != unit->Y) { |
| 71 |
unit->Orders[0]->Range = 1; |
| 72 |
NewResetPath(unit); |
| 73 |
} |
| 74 |
// New code has this as default. |
| 75 |
Assert(unit->Orders[0]->Action == UnitActionBoard); |
| 76 |
return i; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
** Wait for transporter. |
| 81 |
** |
| 82 |
** @param unit Pointer to unit. |
| 83 |
** |
| 84 |
** @return True if ship arrived/present, False otherwise. |
| 85 |
*/ |
| 86 |
static int WaitForTransporter(CUnit *unit) |
| 87 |
{ |
| 88 |
CUnit *trans; |
| 89 |
|
| 90 |
if (unit->Wait) { |
| 91 |
unit->Wait--; |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
trans = unit->Orders[0]->Goal; |
| 96 |
|
| 97 |
if (!trans || !CanTransport(trans, unit)) { |
| 98 |
// FIXME: destination destroyed?? |
| 99 |
unit->Wait = 6; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
if (!trans->IsVisibleAsGoal(unit->Player)) { |
| 104 |
DebugPrint("Transporter Gone\n"); |
| 105 |
trans->RefsDecrease(); |
| 106 |
unit->Orders[0]->Goal = NoUnitP; |
| 107 |
unit->Wait = 6; |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
if (MapDistanceBetweenUnits(unit, trans) == 1) { |
| 112 |
// enter transporter |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
// |
| 117 |
// FIXME: any enemies in range attack them, while waiting. |
| 118 |
// |
| 119 |
|
| 120 |
// n0b0dy: This means we have to search with a smaller range. |
| 121 |
// It happens only when you reach the shore,and the transporter |
| 122 |
// is not there. The unit searches with a big range, so it thinks |
| 123 |
// it's there. This is why we reset the search. The transporter |
| 124 |
// should be a lot closer now, so it's not as bad as it seems. |
| 125 |
unit->SubAction = 0; |
| 126 |
unit->Orders[0]->Range = 1; |
| 127 |
// Uhh wait a bit. |
| 128 |
unit->Wait = 10; |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
** Enter the transporter. |
| 135 |
** |
| 136 |
** @param unit Pointer to unit. |
| 137 |
*/ |
| 138 |
static void EnterTransporter(CUnit *unit) |
| 139 |
{ |
| 140 |
CUnit *transporter; |
| 141 |
|
| 142 |
unit->ClearAction(); |
| 143 |
|
| 144 |
transporter = unit->Orders[0]->Goal; |
| 145 |
if (!transporter->IsVisibleAsGoal(unit->Player)) { |
| 146 |
DebugPrint("Transporter gone\n"); |
| 147 |
transporter->RefsDecrease(); |
| 148 |
unit->Orders[0]->Goal = NoUnitP; |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
transporter->RefsDecrease(); |
| 153 |
unit->Orders[0]->Goal = NoUnitP; |
| 154 |
|
| 155 |
// |
| 156 |
// Place the unit inside the transporter. |
| 157 |
// |
| 158 |
|
| 159 |
if (transporter->BoardCount < transporter->Type->MaxOnBoard) { |
| 160 |
unit->Remove(transporter); |
| 161 |
transporter->BoardCount++; |
| 162 |
unit->Boarded = 1; |
| 163 |
if (!unit->Player->AiEnabled) { |
| 164 |
// Don't make anything funny after going out of the transporter. |
| 165 |
// FIXME: This is probably wrong, but it works for me (n0b0dy) |
| 166 |
unit->OrderCount = 1; |
| 167 |
unit->ClearAction(); |
| 168 |
} |
| 169 |
|
| 170 |
if (IsOnlySelected(transporter)) { |
| 171 |
SelectedUnitChanged(); |
| 172 |
} |
| 173 |
return; |
| 174 |
} |
| 175 |
DebugPrint("No free slot in transporter\n"); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
** The unit boards a transporter. |
| 180 |
** |
| 181 |
** @todo FIXME: While waiting for the transporter the units must defend themselves. |
| 182 |
** |
| 183 |
** @param unit Pointer to unit. |
| 184 |
*/ |
| 185 |
void HandleActionBoard(CUnit *unit) |
| 186 |
{ |
| 187 |
int i; |
| 188 |
CUnit *goal; |
| 189 |
|
| 190 |
switch (unit->SubAction) { |
| 191 |
// |
| 192 |
// Wait for transporter |
| 193 |
// |
| 194 |
case 201: |
| 195 |
if (WaitForTransporter(unit)) { |
| 196 |
unit->SubAction = 202; |
| 197 |
} else { |
| 198 |
UnitShowAnimation(unit, unit->Type->Animations->Still); |
| 199 |
} |
| 200 |
break; |
| 201 |
// |
| 202 |
// Enter transporter |
| 203 |
// |
| 204 |
case 202: |
| 205 |
EnterTransporter(unit); |
| 206 |
break; |
| 207 |
// |
| 208 |
// Move to transporter |
| 209 |
// |
| 210 |
case 0: |
| 211 |
if (unit->Wait) { |
| 212 |
unit->Wait--; |
| 213 |
return; |
| 214 |
} |
| 215 |
NewResetPath(unit); |
| 216 |
unit->SubAction = 1; |
| 217 |
// FALL THROUGH |
| 218 |
default: |
| 219 |
if (unit->SubAction <= 200) { |
| 220 |
// FIXME: if near transporter wait for enter |
| 221 |
if ((i = MoveToTransporter(unit))) { |
| 222 |
if (i == PF_UNREACHABLE) { |
| 223 |
if (++unit->SubAction == 200) { |
| 224 |
unit->ClearAction(); |
| 225 |
if ((goal = unit->Orders[0]->Goal)) { |
| 226 |
goal->RefsDecrease(); |
| 227 |
unit->Orders[0]->Goal = NoUnitP; |
| 228 |
} |
| 229 |
} else { |
| 230 |
// |
| 231 |
// Try with a bigger range. |
| 232 |
// |
| 233 |
if (unit->Orders[0]->Range <= Map.Info.MapWidth || |
| 234 |
unit->Orders[0]->Range <= Map.Info.MapHeight) { |
| 235 |
unit->Orders[0]->Range++; |
| 236 |
unit->SubAction--; |
| 237 |
} |
| 238 |
} |
| 239 |
} else if (i == PF_REACHED) { |
| 240 |
unit->SubAction = 201; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
//@} |