| 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_unload.cpp - The unload 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 "player.h" |
| 41 |
#include "unit.h" |
| 42 |
#include "actions.h" |
| 43 |
#include "map.h" |
| 44 |
#include "interface.h" |
| 45 |
#include "pathfinder.h" |
| 46 |
|
| 47 |
/*---------------------------------------------------------------------------- |
| 48 |
-- Functions |
| 49 |
----------------------------------------------------------------------------*/ |
| 50 |
|
| 51 |
|
| 52 |
// Flag for searching a valid tileset for unloading |
| 53 |
#define LandUnitMask ( \ |
| 54 |
MapFieldLandUnit | \ |
| 55 |
MapFieldBuilding | \ |
| 56 |
MapFieldCoastAllowed | \ |
| 57 |
MapFieldShallowWater | \ |
| 58 |
MapFieldDeepWater | \ |
| 59 |
MapFieldUnpassable) |
| 60 |
|
| 61 |
#define NavalUnitMask ( \ |
| 62 |
MapFieldLandUnit | \ |
| 63 |
MapFieldBuilding | \ |
| 64 |
MapFieldCoastAllowed | \ |
| 65 |
MapFieldLandAllowed | \ |
| 66 |
MapFieldUnpassable) |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
** Find a free position close to x, y |
| 71 |
** |
| 72 |
** @param x Original x search position |
| 73 |
** @param y Original y search position |
| 74 |
** @param resx Unload x position. |
| 75 |
** @param resy Unload y position. |
| 76 |
** @param mask Movement mask for the unit to be droped. |
| 77 |
** |
| 78 |
** @return True if a position was found, False otherwise. |
| 79 |
** @note resx and resy are undefined if a position is not found. |
| 80 |
** |
| 81 |
** @bug FIXME: Place unit only on fields reachable from the transporter |
| 82 |
** @bug FIXME: This function fails for units larger than 1x1. |
| 83 |
*/ |
| 84 |
static int FindUnloadPosition(int x, int y, int *resx, int *resy, int mask) |
| 85 |
{ |
| 86 |
int i; |
| 87 |
int n; |
| 88 |
int addx; |
| 89 |
int addy; |
| 90 |
|
| 91 |
addx = addy = 1; |
| 92 |
--x; |
| 93 |
for (n = 0; n < 2; ++n) { |
| 94 |
// Nobody: There was some code here to check for unloading units that can |
| 95 |
// only go on even tiles. It's useless, since we can only unload land units. |
| 96 |
for (i = addy; i--; ++y) { |
| 97 |
if (CheckedCanMoveToMask(x, y, mask)) { |
| 98 |
*resx = x; |
| 99 |
*resy = y; |
| 100 |
return 1; |
| 101 |
} |
| 102 |
} |
| 103 |
++addx; |
| 104 |
for (i = addx; i--; ++x) { |
| 105 |
if (CheckedCanMoveToMask(x, y, mask)) { |
| 106 |
*resx = x; |
| 107 |
*resy = y; |
| 108 |
return 1; |
| 109 |
} |
| 110 |
} |
| 111 |
++addy; |
| 112 |
for (i = addy; i--; --y) { |
| 113 |
if (CheckedCanMoveToMask(x, y, mask)) { |
| 114 |
*resx = x; |
| 115 |
*resy = y; |
| 116 |
return 1; |
| 117 |
} |
| 118 |
} |
| 119 |
++addx; |
| 120 |
for (i = addx; i--; --x) { |
| 121 |
if (CheckedCanMoveToMask(x, y, mask)) { |
| 122 |
*resx = x; |
| 123 |
*resy = y; |
| 124 |
return 1; |
| 125 |
} |
| 126 |
} |
| 127 |
++addy; |
| 128 |
} |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
** Reappear unit on map. |
| 134 |
** |
| 135 |
** @param unit Unit to drop out. |
| 136 |
** |
| 137 |
** @return True if unit can be unloaded. |
| 138 |
** |
| 139 |
** @bug FIXME: Place unit only on fields reachable from the transporter |
| 140 |
*/ |
| 141 |
int UnloadUnit(CUnit *unit) |
| 142 |
{ |
| 143 |
int x; |
| 144 |
int y; |
| 145 |
|
| 146 |
Assert(unit->Removed); |
| 147 |
if (!FindUnloadPosition(unit->X, unit->Y, &x, &y, unit->Type->MovementMask)) { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
unit->Boarded = 0; |
| 151 |
unit->Place(x, y); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
** Find the closest piece of coast you can unload units on |
| 157 |
** |
| 158 |
** @param x start location for the search |
| 159 |
** @param y start location for the search |
| 160 |
** @param resx coast x position |
| 161 |
** @param resy coast y position |
| 162 |
** |
| 163 |
** @return 1 if a location was found, 0 otherwise |
| 164 |
*/ |
| 165 |
static int ClosestFreeCoast(int x, int y, int *resx, int *resy) |
| 166 |
{ |
| 167 |
int i; |
| 168 |
int addx; |
| 169 |
int addy; |
| 170 |
int nullx; |
| 171 |
int nully; |
| 172 |
int n; |
| 173 |
|
| 174 |
addx = addy = 1; |
| 175 |
if (Map.CoastOnMap(x, y) && |
| 176 |
FindUnloadPosition(x, y, &nullx, &nully, LandUnitMask)) { |
| 177 |
*resx = x; |
| 178 |
*resy = y; |
| 179 |
return 1; |
| 180 |
} |
| 181 |
--x; |
| 182 |
// The maximum distance to the coast. We have to stop somewhere... |
| 183 |
n = 20; |
| 184 |
while (n--) { |
| 185 |
for (i = addy; i--; ++y) { |
| 186 |
if (x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight && |
| 187 |
Map.CoastOnMap(x, y) && !UnitOnMapTile(x, y) && |
| 188 |
FindUnloadPosition(x, y, &nullx, &nully, LandUnitMask)) { |
| 189 |
*resx = x; |
| 190 |
*resy = y; |
| 191 |
return 1; |
| 192 |
} |
| 193 |
} |
| 194 |
++addx; |
| 195 |
for (i = addx; i--; ++x) { |
| 196 |
if (x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight && |
| 197 |
Map.CoastOnMap(x, y) && !UnitOnMapTile(x ,y) && |
| 198 |
FindUnloadPosition(x, y, &nullx, &nully, LandUnitMask)) { |
| 199 |
*resx = x; |
| 200 |
*resy = y; |
| 201 |
return 1; |
| 202 |
} |
| 203 |
} |
| 204 |
++addy; |
| 205 |
for (i = addy; i--; --y) { |
| 206 |
if (x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight && |
| 207 |
Map.CoastOnMap(x, y) && !UnitOnMapTile(x, y) && |
| 208 |
FindUnloadPosition(x, y, &nullx, &nully, LandUnitMask)) { |
| 209 |
*resx = x; |
| 210 |
*resy = y; |
| 211 |
return 1; |
| 212 |
} |
| 213 |
} |
| 214 |
++addx; |
| 215 |
for (i = addx; i--; --x) { |
| 216 |
if (x >= 0 && y >= 0 && x < Map.Info.MapWidth && y < Map.Info.MapHeight && |
| 217 |
Map.CoastOnMap(x, y) && !UnitOnMapTile(x, y) && |
| 218 |
FindUnloadPosition(x, y, &nullx, &nully, LandUnitMask)) { |
| 219 |
*resx = x; |
| 220 |
*resy = y; |
| 221 |
return 1; |
| 222 |
} |
| 223 |
} |
| 224 |
++addy; |
| 225 |
} |
| 226 |
DebugPrint("Try clicking closer to an actual coast.\n"); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
** Find the closest available drop zone for a transporter. |
| 232 |
** Fail if transporter don't transport any unit.. |
| 233 |
** |
| 234 |
** @param transporter the transporter |
| 235 |
** @param x start location for the search |
| 236 |
** @param y start location for the search |
| 237 |
** @param resx coast x position |
| 238 |
** @param resy coast y position |
| 239 |
** |
| 240 |
** @return 1 if a location was found, 0 otherwise |
| 241 |
** |
| 242 |
*/ |
| 243 |
static int ClosestFreeDropZone(CUnit *transporter, int x, int y, int *resx, int *resy) |
| 244 |
{ |
| 245 |
// Type (land/fly/naval) of the transporter |
| 246 |
int transporterType; |
| 247 |
// Type (land/fly/naval) of the units to unload |
| 248 |
int loadedType; |
| 249 |
|
| 250 |
// Check there are units onboard |
| 251 |
if (!transporter->UnitInside) { |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
transporterType = transporter->Type->UnitType; |
| 256 |
// Take the type of the onboard unit |
| 257 |
loadedType = transporter->UnitInside->Type->UnitType; |
| 258 |
|
| 259 |
// Don't move in thoses cases |
| 260 |
if ((transporterType == loadedType) || (loadedType == UnitTypeFly)) { |
| 261 |
*resx = x; |
| 262 |
*resy = y; |
| 263 |
return 1; |
| 264 |
} |
| 265 |
|
| 266 |
switch (transporterType) { |
| 267 |
case UnitTypeLand: |
| 268 |
// in this case, loadedType == UnitTypeSea |
| 269 |
return ClosestFreeCoast(x, y, resx, resy); |
| 270 |
case UnitTypeNaval: |
| 271 |
// Same ( but reversed... ) |
| 272 |
return ClosestFreeCoast(x, y, resx, resy); |
| 273 |
case UnitTypeFly: |
| 274 |
// Here we have loadedType in [ UnitTypeLand,UnitTypeNaval ] |
| 275 |
if (loadedType == UnitTypeLand) { |
| 276 |
return FindUnloadPosition(x, y, resx, resy, LandUnitMask); |
| 277 |
} else { |
| 278 |
return FindUnloadPosition(x, y, resx, resy, NavalUnitMask); |
| 279 |
} |
| 280 |
} |
| 281 |
// Just to avoid a warning |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
** Move to dropzone. |
| 287 |
** |
| 288 |
** @param unit Pointer to unit. |
| 289 |
** |
| 290 |
** @return -1 if unreachable, True if reached, False otherwise. |
| 291 |
*/ |
| 292 |
static int MoveToDropZone(CUnit *unit) |
| 293 |
{ |
| 294 |
switch (DoActionMove(unit)) { // reached end-point? |
| 295 |
case PF_UNREACHABLE: |
| 296 |
return -1; |
| 297 |
case PF_REACHED: |
| 298 |
break; |
| 299 |
default: |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
Assert(unit->Orders[0]->Action == UnitActionUnload); |
| 304 |
return 1; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
** Make one or more unit leave the transporter. |
| 309 |
** |
| 310 |
** @param unit Pointer to unit. |
| 311 |
*/ |
| 312 |
static void LeaveTransporter(CUnit *unit) |
| 313 |
{ |
| 314 |
int i; |
| 315 |
int stillonboard; |
| 316 |
CUnit *goal; |
| 317 |
|
| 318 |
stillonboard = 0; |
| 319 |
goal = unit->Orders[0]->Goal; |
| 320 |
// |
| 321 |
// Goal is the specific unit unit that you want to unload. |
| 322 |
// This can be NULL, in case you want to unload everything. |
| 323 |
// |
| 324 |
if (goal) { |
| 325 |
unit->Orders[0]->Goal = NoUnitP; |
| 326 |
if (goal->Destroyed) { |
| 327 |
DebugPrint("destroyed unit unloading?\n"); |
| 328 |
goal->RefsDecrease(); |
| 329 |
return; |
| 330 |
} |
| 331 |
goal->RefsDecrease(); |
| 332 |
goal->X = unit->X; |
| 333 |
goal->Y = unit->Y; |
| 334 |
// Try to unload the unit. If it doesn't work there is no problem. |
| 335 |
if (UnloadUnit(goal)) { |
| 336 |
unit->BoardCount--; |
| 337 |
} |
| 338 |
} else { |
| 339 |
// Unload all units. |
| 340 |
goal = unit->UnitInside; |
| 341 |
for (i = unit->InsideCount; i; --i, goal = goal->NextContained) { |
| 342 |
if (goal->Boarded) { |
| 343 |
goal->X = unit->X; |
| 344 |
goal->Y = unit->Y; |
| 345 |
if (!UnloadUnit(goal)) { |
| 346 |
++stillonboard; |
| 347 |
} else { |
| 348 |
unit->BoardCount--; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
if (IsOnlySelected(unit)) { |
| 354 |
SelectedUnitChanged(); |
| 355 |
} |
| 356 |
|
| 357 |
// We still have some units to unload, find a piece of free coast. |
| 358 |
if (stillonboard) { |
| 359 |
// We tell it to unload at it's current position. This can't be done, |
| 360 |
// so it will search for a piece of free coast nearby. |
| 361 |
unit->Orders[0]->Action = UnitActionUnload; |
| 362 |
unit->Orders[0]->Goal = NoUnitP; |
| 363 |
unit->Orders[0]->X = unit->X; |
| 364 |
unit->Orders[0]->Y = unit->Y; |
| 365 |
unit->SubAction = 0; |
| 366 |
} else { |
| 367 |
unit->ClearAction(); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
** The transporter unloads a unit. |
| 373 |
** |
| 374 |
** @param unit Pointer to unit. |
| 375 |
*/ |
| 376 |
void HandleActionUnload(CUnit *unit) |
| 377 |
{ |
| 378 |
int i; |
| 379 |
int x; |
| 380 |
int y; |
| 381 |
|
| 382 |
if (!CanMove(unit)) { |
| 383 |
unit->SubAction = 2; |
| 384 |
} |
| 385 |
switch (unit->SubAction) { |
| 386 |
// |
| 387 |
// Move the transporter |
| 388 |
// |
| 389 |
case 0: |
| 390 |
if (!unit->Orders[0]->Goal) { |
| 391 |
if (!ClosestFreeDropZone(unit, unit->Orders[0]->X, unit->Orders[0]->Y, |
| 392 |
&x, &y)) { |
| 393 |
// Sorry... I give up. |
| 394 |
unit->ClearAction(); |
| 395 |
return; |
| 396 |
} |
| 397 |
unit->Orders[0]->X = x; |
| 398 |
unit->Orders[0]->Y = y; |
| 399 |
} |
| 400 |
|
| 401 |
NewResetPath(unit); |
| 402 |
unit->SubAction = 1; |
| 403 |
case 1: |
| 404 |
// The Goal is the unit that we have to unload. |
| 405 |
if (!unit->Orders[0]->Goal) { |
| 406 |
// We have to unload everything |
| 407 |
if ((i = MoveToDropZone(unit))) { |
| 408 |
if (i == PF_REACHED) { |
| 409 |
if (++unit->SubAction == 1) { |
| 410 |
unit->ClearAction(); |
| 411 |
} |
| 412 |
} else { |
| 413 |
unit->SubAction = 2; |
| 414 |
} |
| 415 |
} |
| 416 |
break; |
| 417 |
} |
| 418 |
// |
| 419 |
// Leave the transporter |
| 420 |
// |
| 421 |
case 2: |
| 422 |
// FIXME: show still animations ? |
| 423 |
LeaveTransporter(unit); |
| 424 |
if (CanMove(unit) && unit->Orders[0]->Action != UnitActionStill) { |
| 425 |
HandleActionUnload(unit); |
| 426 |
} |
| 427 |
break; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
//@} |