| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name unit_draw.cpp - The draw routines for units. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer, Jimmy Salmon, Nehal Mistry |
| 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 |
#include <string.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
|
| 41 |
#include <vector> |
| 42 |
|
| 43 |
#include "video.h" |
| 44 |
#include "editor.h" |
| 45 |
#include "unittype.h" |
| 46 |
#include "player.h" |
| 47 |
#include "unit.h" |
| 48 |
#include "unit_cache.h" |
| 49 |
#include "map.h" |
| 50 |
#include "construct.h" |
| 51 |
#include "cursor.h" |
| 52 |
#include "interface.h" |
| 53 |
#include "font.h" |
| 54 |
#include "ui.h" |
| 55 |
#include "script.h" |
| 56 |
|
| 57 |
/*---------------------------------------------------------------------------- |
| 58 |
-- Variables |
| 59 |
----------------------------------------------------------------------------*/ |
| 60 |
|
| 61 |
/** |
| 62 |
** Decoration: health, charge. |
| 63 |
*/ |
| 64 |
class Decoration { |
| 65 |
public: |
| 66 |
Decoration() : HotX(0), HotY(0), Width(0), Height(0), Sprite(NULL) {} |
| 67 |
|
| 68 |
std::string File; /// File containing the graphics data |
| 69 |
int HotX; /// X drawing position (relative) |
| 70 |
int HotY; /// Y drawing position (relative) |
| 71 |
int Width; /// width of the decoration |
| 72 |
int Height; /// height of the decoration |
| 73 |
|
| 74 |
// --- FILLED UP --- |
| 75 |
CGraphic *Sprite; /// loaded sprite images |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
** Structure grouping all Sprites for decoration. |
| 81 |
*/ |
| 82 |
class DecoSpriteType { |
| 83 |
public: |
| 84 |
std::vector<std::string> Name; /// Name of the sprite. |
| 85 |
std::vector<Decoration> SpriteArray; /// Sprite to display variable. |
| 86 |
}; |
| 87 |
|
| 88 |
static DecoSpriteType DecoSprite; /// All sprite's infos. |
| 89 |
|
| 90 |
unsigned long ShowOrdersCount; /// Show orders for some time |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
// FIXME: not all variables of this file are here |
| 95 |
// FIXME: perhaps split this file into two or three parts? |
| 96 |
|
| 97 |
/*---------------------------------------------------------------------------- |
| 98 |
-- Functions |
| 99 |
----------------------------------------------------------------------------*/ |
| 100 |
|
| 101 |
// FIXME: clean split screen support |
| 102 |
// FIXME: integrate this with global versions of these functions in map.c |
| 103 |
|
| 104 |
const CViewport *CurrentViewport; /// FIXME: quick hack for split screen |
| 105 |
|
| 106 |
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 107 |
|
| 108 |
/** |
| 109 |
** Show selection marker around a unit. |
| 110 |
** |
| 111 |
** @param unit Pointer to unit. |
| 112 |
*/ |
| 113 |
void DrawUnitSelection(const CUnit *unit) |
| 114 |
{ |
| 115 |
Uint32 color; |
| 116 |
|
| 117 |
// FIXME: make these colors customizable with scripts. |
| 118 |
|
| 119 |
if (Editor.Running && unit == UnitUnderCursor && |
| 120 |
Editor.State == EditorSelecting) { |
| 121 |
color = ColorWhite; |
| 122 |
} else if (unit->Selected || unit->TeamSelected || (unit->Blink & 1)) { |
| 123 |
if (unit->Player->Index == PlayerNumNeutral) { |
| 124 |
color = ColorYellow; |
| 125 |
} else if ((unit->Selected || (unit->Blink & 1)) && |
| 126 |
(unit->Player == ThisPlayer || |
| 127 |
ThisPlayer->IsTeamed(unit))) { |
| 128 |
color = ColorGreen; |
| 129 |
} else if (ThisPlayer->IsEnemy(unit)) { |
| 130 |
color = ColorRed; |
| 131 |
} else { |
| 132 |
int i; |
| 133 |
|
| 134 |
for (i = 0; i < PlayerMax; ++i) { |
| 135 |
if (unit->TeamSelected & (1 << i)) { |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
if (i == PlayerMax) { |
| 140 |
color = unit->Player->Color; |
| 141 |
} else { |
| 142 |
color = Players[i].Color; |
| 143 |
} |
| 144 |
} |
| 145 |
} else if (CursorBuilding && unit->Type->Building && |
| 146 |
unit->Orders[0]->Action != UnitActionDie && |
| 147 |
(unit->Player == ThisPlayer || ThisPlayer->IsTeamed(unit))) { |
| 148 |
// If building mark all own buildings |
| 149 |
color = ColorGray; |
| 150 |
} else { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
const CUnitType *type = unit->Type; |
| 155 |
int x = CurrentViewport->Map2ViewportX(unit->X) + unit->IX + |
| 156 |
type->TileWidth * TileSizeX / 2 - type->BoxWidth / 2 - |
| 157 |
(type->Width - type->Sprite->Width) / 2; |
| 158 |
int y = CurrentViewport->Map2ViewportY(unit->Y) + unit->IY + |
| 159 |
type->TileHeight * TileSizeY / 2 - type->BoxHeight / 2 - |
| 160 |
(type->Height - type->Sprite->Height) / 2; |
| 161 |
|
| 162 |
DrawSelection(color, x, y, x + type->BoxWidth, y + type->BoxHeight); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
** Draw selected corners around the unit. |
| 167 |
** |
| 168 |
** @param color Color to draw corners. |
| 169 |
** @param x1,y1 Coordinates of the top left corner. |
| 170 |
** @param x2,y2 Coordinates of the bottom right corner. |
| 171 |
*/ |
| 172 |
void DrawSelection(Uint32 color, int x1, int y1, int x2, int y2) |
| 173 |
{ |
| 174 |
const int cornerPixels = 6; |
| 175 |
|
| 176 |
Video.DrawVLineClip(color, x1, y1, cornerPixels); |
| 177 |
Video.DrawHLineClip(color, x1 + 1, y1, cornerPixels - 1); |
| 178 |
|
| 179 |
Video.DrawVLineClip(color, x2, y1, cornerPixels); |
| 180 |
Video.DrawHLineClip(color, x2 - cornerPixels + 1, y1, cornerPixels - 1); |
| 181 |
|
| 182 |
Video.DrawVLineClip(color, x1, y2 - cornerPixels + 1, cornerPixels); |
| 183 |
Video.DrawHLineClip(color, x1, y2, cornerPixels - 1); |
| 184 |
|
| 185 |
Video.DrawVLineClip(color, x2, y2 - cornerPixels + 1, cornerPixels); |
| 186 |
Video.DrawHLineClip(color, x2 - cornerPixels + 1, y2, cornerPixels - 1); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
** Return the index of the sprite. |
| 192 |
** |
| 193 |
** @param spriteName Name of the sprite. |
| 194 |
** |
| 195 |
** @return Index of the sprite. -1 if not found. |
| 196 |
*/ |
| 197 |
int GetSpriteIndex(const std::string &spriteName) |
| 198 |
{ |
| 199 |
int index = -1; |
| 200 |
|
| 201 |
for (int i = 0; i < (int)DecoSprite.Name.size(); ++i) { |
| 202 |
if (spriteName == DecoSprite.Name[i]) { |
| 203 |
index = i; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
return index; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
** Define the sprite to show variables. |
| 212 |
** |
| 213 |
** @param l Lua_state |
| 214 |
*/ |
| 215 |
static int CclDefineSprites(lua_State *l) |
| 216 |
{ |
| 217 |
int args; |
| 218 |
|
| 219 |
args = lua_gettop(l); |
| 220 |
for (int i = 0; i < args; ++i) { |
| 221 |
Decoration deco; |
| 222 |
const char *name = NULL; |
| 223 |
|
| 224 |
lua_pushnil(l); |
| 225 |
while (lua_next(l, i + 1)) { |
| 226 |
const char *key = LuaToString(l, -2); // key name |
| 227 |
if (!strcmp(key, "Name")) { |
| 228 |
name = LuaToString(l, -1); |
| 229 |
} else if (!strcmp(key, "File")) { |
| 230 |
deco.File = LuaToString(l, -1); |
| 231 |
} else if (!strcmp(key, "Offset")) { |
| 232 |
LuaCheckTableSize(l, -1, 2); |
| 233 |
deco.HotX = LuaToNumber(l, -1, 1); |
| 234 |
deco.HotY = LuaToNumber(l, -1, 2); |
| 235 |
} else if (!strcmp(key, "Size")) { |
| 236 |
LuaCheckTableSize(l, -1, 2); |
| 237 |
deco.Width = LuaToNumber(l, -1, 1); |
| 238 |
deco.Height = LuaToNumber(l, -1, 2); |
| 239 |
} else { // Error. |
| 240 |
LuaError(l, "incorrect field '%s' for DefineSprites." _C_ key); |
| 241 |
} |
| 242 |
lua_pop(l, 1); // pop the value; |
| 243 |
} |
| 244 |
if (name == NULL) { |
| 245 |
LuaError(l, "CclDefineSprites requires the Name flag for sprite."); |
| 246 |
} |
| 247 |
|
| 248 |
int index = GetSpriteIndex(name); |
| 249 |
if (index == -1) { // new sprite. |
| 250 |
index = DecoSprite.SpriteArray.size(); |
| 251 |
DecoSprite.Name.push_back(name); |
| 252 |
DecoSprite.SpriteArray.push_back(deco); |
| 253 |
} else { |
| 254 |
DecoSprite.SpriteArray[index] = deco; |
| 255 |
} |
| 256 |
|
| 257 |
// Now verify validity. |
| 258 |
if (DecoSprite.SpriteArray[index].File.empty()) { |
| 259 |
LuaError(l, "CclDefineSprites requires the File flag for sprite."); |
| 260 |
} |
| 261 |
// FIXME: check if file is valid with good size ? |
| 262 |
} |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
** Register CCL features for decorations. |
| 268 |
*/ |
| 269 |
void DecorationCclRegister(void) |
| 270 |
{ |
| 271 |
lua_register(Lua, "DefineSprites", CclDefineSprites); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
** Load decoration. |
| 276 |
*/ |
| 277 |
void LoadDecorations(void) |
| 278 |
{ |
| 279 |
std::vector<Decoration>::iterator i; |
| 280 |
for (i = DecoSprite.SpriteArray.begin(); i != DecoSprite.SpriteArray.end(); ++i) { |
| 281 |
ShowLoadProgress("Decorations `%s'", (*i).File.c_str()); |
| 282 |
(*i).Sprite = CGraphic::New((*i).File, (*i).Width, (*i).Height); |
| 283 |
(*i).Sprite->Load(); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
** Clean decorations. |
| 289 |
*/ |
| 290 |
void CleanDecorations(void) |
| 291 |
{ |
| 292 |
for (size_t i = 0; i < DecoSprite.SpriteArray.size(); ++i) { |
| 293 |
CGraphic::Free(DecoSprite.SpriteArray[i].Sprite); |
| 294 |
} |
| 295 |
|
| 296 |
DecoSprite.Name.clear(); |
| 297 |
DecoSprite.SpriteArray.clear(); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
** Draw a sprite with is like a bar (several stages) |
| 302 |
** |
| 303 |
** @param x X screen pixel position |
| 304 |
** @param y Y screen pixel position |
| 305 |
** @param unit Unit pointer |
| 306 |
** @todo fix sprite configuration. |
| 307 |
*/ |
| 308 |
void CDecoVarSpriteBar::Draw(int x, int y, const CUnit *unit) const |
| 309 |
{ |
| 310 |
int n; |
| 311 |
CGraphic *sprite; |
| 312 |
Decoration *decosprite; |
| 313 |
|
| 314 |
Assert(unit->Variable[this->Index].Max); |
| 315 |
Assert(this->SpriteIndex != -1); |
| 316 |
|
| 317 |
decosprite = &DecoSprite.SpriteArray[(int)this->SpriteIndex]; |
| 318 |
sprite = decosprite->Sprite; |
| 319 |
x += decosprite->HotX; // in addition of OffsetX... Useful? |
| 320 |
y += decosprite->HotY; // in addition of OffsetY... Useful? |
| 321 |
|
| 322 |
n = sprite->NumFrames - 1; |
| 323 |
n -= (n * unit->Variable[this->Index].Value) / unit->Variable[this->Index].Max; |
| 324 |
|
| 325 |
if (this->IsCenteredInX) { |
| 326 |
x -= sprite->Width / 2; |
| 327 |
} |
| 328 |
if (this->IsCenteredInY) { |
| 329 |
y -= sprite->Height / 2; |
| 330 |
} |
| 331 |
sprite->DrawFrameClip(n, x, y); |
| 332 |
} |
| 333 |
|
| 334 |
extern void UpdateUnitVariables(const CUnit *unit); |
| 335 |
|
| 336 |
/** |
| 337 |
** Draw decoration (invis, for the unit.) |
| 338 |
** |
| 339 |
** @param unit Pointer to the unit. |
| 340 |
** @param type Type of the unit. |
| 341 |
** @param x Screen X position of the unit. |
| 342 |
** @param y Screen Y position of the unit. |
| 343 |
*/ |
| 344 |
static void DrawDecoration(const CUnit *unit, const CUnitType *type, int x, int y) |
| 345 |
{ |
| 346 |
UpdateUnitVariables(unit); |
| 347 |
// Now show decoration for each variable. |
| 348 |
for (std::vector<CDecoVar *>::const_iterator i = UnitTypeVar.DecoVar.begin(); |
| 349 |
i < UnitTypeVar.DecoVar.end(); ++i) { |
| 350 |
int value; |
| 351 |
int max; |
| 352 |
|
| 353 |
value = unit->Variable[(*i)->Index].Value; |
| 354 |
max = unit->Variable[(*i)->Index].Max; |
| 355 |
Assert(value <= max); |
| 356 |
|
| 357 |
if (!((value == max && !(*i)->ShowWhenMax) || |
| 358 |
(!(*i)->ShowIfNotEnable && !unit->Variable[(*i)->Index].Enable) || |
| 359 |
((*i)->ShowOnlySelected && !unit->Selected) || |
| 360 |
(unit->Player->Type == PlayerNeutral && (*i)->HideNeutral) || |
| 361 |
(ThisPlayer->IsEnemy(unit) && !(*i)->ShowOpponent) || |
| 362 |
(ThisPlayer->IsAllied(unit) && (unit->Player != ThisPlayer) && (*i)->HideAllied) || |
| 363 |
max == 0)) { |
| 364 |
(*i)->Draw( |
| 365 |
x + (*i)->OffsetX + (*i)->OffsetXPercent * unit->Type->TileWidth * TileSizeX / 100, |
| 366 |
y + (*i)->OffsetY + (*i)->OffsetYPercent * unit->Type->TileHeight * TileSizeY / 100, |
| 367 |
unit); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
// |
| 372 |
// Draw group number |
| 373 |
// |
| 374 |
if (unit->Selected && unit->GroupId != 0) { |
| 375 |
int num; |
| 376 |
int width; |
| 377 |
|
| 378 |
for (num = 0; !(unit->GroupId & (1 << num)); ++num) { |
| 379 |
; |
| 380 |
} |
| 381 |
width = GameFont->Width(std::string(1, '0' + num)); |
| 382 |
x += (type->TileWidth * TileSizeX + type->BoxWidth) / 2 - width; |
| 383 |
width = GameFont->Height(); |
| 384 |
y += (type->TileHeight * TileSizeY + type->BoxHeight) / 2 - width; |
| 385 |
VideoDrawNumberClip(x, y, GameFont, num); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
** Draw unit's shadow. |
| 391 |
** |
| 392 |
** @param unit Pointer to the unit. |
| 393 |
** @param type Pointer to the unit type. |
| 394 |
** @param frame Frame number |
| 395 |
** @param x Screen X position of the unit. |
| 396 |
** @param y Screen Y position of the unit. |
| 397 |
** |
| 398 |
** @todo FIXME: combine new shadow code with old shadow code. |
| 399 |
*/ |
| 400 |
void DrawShadow(const CUnit *unit, const CUnitType *type, int frame, |
| 401 |
int x, int y) |
| 402 |
{ |
| 403 |
if (!type) { |
| 404 |
Assert(unit); |
| 405 |
type = unit->Type; |
| 406 |
} |
| 407 |
Assert(type); |
| 408 |
Assert(!unit || unit->Type == type); |
| 409 |
|
| 410 |
// unit == NULL for the editor |
| 411 |
if (unit && unit->Orders[0]->Action == UnitActionDie) { |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
// Draw normal shadow sprite if available |
| 416 |
if (type->ShadowSprite) { |
| 417 |
x -= (type->ShadowWidth - type->TileWidth * TileSizeX) / 2; |
| 418 |
y -= (type->ShadowHeight - type->TileHeight * TileSizeY) / 2; |
| 419 |
x += type->OffsetX + type->ShadowOffsetX; |
| 420 |
y += type->OffsetY + type->ShadowOffsetY; |
| 421 |
|
| 422 |
if (type->Flip) { |
| 423 |
if (frame < 0) { |
| 424 |
type->ShadowSprite->DrawFrameClipX(-frame - 1, x, y); |
| 425 |
} else { |
| 426 |
type->ShadowSprite->DrawFrameClip(frame, x, y); |
| 427 |
} |
| 428 |
} else { |
| 429 |
int row = type->NumDirections / 2 + 1; |
| 430 |
if (frame < 0) { |
| 431 |
frame = ((-frame - 1) / row) * type->NumDirections + type->NumDirections - (-frame - 1) % row; |
| 432 |
} else { |
| 433 |
frame = (frame / row) * type->NumDirections + frame % row; |
| 434 |
} |
| 435 |
type->ShadowSprite->DrawFrameClip(frame, x, y); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
** Get the location of a unit's order. |
| 442 |
** |
| 443 |
** @param unit Pointer to unit. |
| 444 |
** @param order Pointer to order. |
| 445 |
** @param x Resulting screen X cordinate. |
| 446 |
** @param y Resulting screen Y cordinate. |
| 447 |
*/ |
| 448 |
static void GetOrderPosition(const CUnit *unit, const COrder *order, int *x, int *y) |
| 449 |
{ |
| 450 |
CUnit *goal; |
| 451 |
|
| 452 |
// FIXME: n0body: Check for goal gone? |
| 453 |
if ((goal = order->Goal) && (!goal->Removed)) { |
| 454 |
// Order has a goal, get it's location. |
| 455 |
*x = CurrentViewport->Map2ViewportX(goal->X) + goal->IX + |
| 456 |
goal->Type->TileWidth * TileSizeX / 2; |
| 457 |
*y = CurrentViewport->Map2ViewportY(goal->Y) + goal->IY + |
| 458 |
goal->Type->TileHeight * TileSizeY / 2; |
| 459 |
} else { |
| 460 |
if (order->X >= 0 && order->Y >= 0) { |
| 461 |
// Order is for a location, show that. |
| 462 |
*x = CurrentViewport->Map2ViewportX(order->X) + TileSizeX / 2; |
| 463 |
*y = CurrentViewport->Map2ViewportY(order->Y) + TileSizeY / 2; |
| 464 |
} else { |
| 465 |
// Some orders ignore x,y (like StandStill). |
| 466 |
// Use the unit's position instead. |
| 467 |
*x = CurrentViewport->Map2ViewportX(unit->X) + unit->IX + |
| 468 |
unit->Type->TileWidth * TileSizeX / 2; |
| 469 |
*y = CurrentViewport->Map2ViewportY(unit->Y) + unit->IY + |
| 470 |
unit->Type->TileHeight * TileSizeY / 2; |
| 471 |
} |
| 472 |
if (order->Action == UnitActionBuild) { |
| 473 |
*x += (order->Type->TileWidth - 1) * TileSizeX / 2; |
| 474 |
*y += (order->Type->TileHeight - 1) * TileSizeY / 2; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
** Show the order on map. |
| 481 |
** |
| 482 |
** @param unit Unit pointer. |
| 483 |
** @param x1 X pixel coordinate. |
| 484 |
** @param y1 Y pixel coordinate. |
| 485 |
** @param order Order to display. |
| 486 |
*/ |
| 487 |
static void ShowSingleOrder(const CUnit *unit, int x1, int y1, const COrder *order) |
| 488 |
{ |
| 489 |
int x2; |
| 490 |
int y2; |
| 491 |
Uint32 color; |
| 492 |
Uint32 e_color; |
| 493 |
bool dest; |
| 494 |
|
| 495 |
GetOrderPosition(unit, order, &x2, &y2); |
| 496 |
|
| 497 |
dest = false; |
| 498 |
switch (order->Action) { |
| 499 |
case UnitActionNone: |
| 500 |
e_color = color = ColorGray; |
| 501 |
break; |
| 502 |
|
| 503 |
case UnitActionStill: |
| 504 |
e_color = color = ColorGray; |
| 505 |
break; |
| 506 |
|
| 507 |
case UnitActionStandGround: |
| 508 |
e_color = color = ColorGreen; |
| 509 |
break; |
| 510 |
|
| 511 |
case UnitActionFollow: |
| 512 |
case UnitActionMove: |
| 513 |
e_color = color = ColorGreen; |
| 514 |
dest = true; |
| 515 |
break; |
| 516 |
|
| 517 |
case UnitActionPatrol: |
| 518 |
Video.DrawLineClip(ColorGreen, x1, y1, x2, y2); |
| 519 |
e_color = color = ColorBlue; |
| 520 |
x1 = CurrentViewport->Map2ViewportX(order->Arg1.Patrol.X) + TileSizeX / 2; |
| 521 |
y1 = CurrentViewport->Map2ViewportY(order->Arg1.Patrol.Y) + TileSizeY / 2; |
| 522 |
dest = true; |
| 523 |
break; |
| 524 |
|
| 525 |
case UnitActionRepair: |
| 526 |
e_color = color = ColorGreen; |
| 527 |
dest = true; |
| 528 |
break; |
| 529 |
|
| 530 |
case UnitActionAttackGround: |
| 531 |
x2 = CurrentViewport->Map2ViewportX(order->X) + TileSizeX / 2; |
| 532 |
y2 = CurrentViewport->Map2ViewportY(order->Y) + TileSizeY / 2; |
| 533 |
// FALL THROUGH |
| 534 |
case UnitActionAttack: |
| 535 |
if (unit->SubAction & 2) { // Show weak targets. |
| 536 |
e_color = ColorBlue; |
| 537 |
} else { |
| 538 |
e_color = ColorRed; |
| 539 |
} |
| 540 |
color = ColorRed; |
| 541 |
dest = true; |
| 542 |
break; |
| 543 |
|
| 544 |
case UnitActionBoard: |
| 545 |
e_color = color = ColorGreen; |
| 546 |
dest = true; |
| 547 |
break; |
| 548 |
|
| 549 |
case UnitActionUnload: |
| 550 |
e_color = color = ColorGreen; |
| 551 |
dest = true; |
| 552 |
break; |
| 553 |
|
| 554 |
case UnitActionDie: |
| 555 |
e_color = color = ColorGray; |
| 556 |
break; |
| 557 |
|
| 558 |
case UnitActionSpellCast: |
| 559 |
e_color = color = ColorBlue; |
| 560 |
dest = true; |
| 561 |
break; |
| 562 |
|
| 563 |
case UnitActionTrain: |
| 564 |
e_color = color = ColorGray; |
| 565 |
break; |
| 566 |
|
| 567 |
case UnitActionBuild: |
| 568 |
DrawSelection(ColorGray, x2 - order->Type->BoxWidth / 2, |
| 569 |
y2 - order->Type->BoxHeight / 2, |
| 570 |
x2 + order->Type->BoxWidth / 2, |
| 571 |
y2 + order->Type->BoxHeight / 2); |
| 572 |
e_color = color = ColorGreen; |
| 573 |
dest = true; |
| 574 |
break; |
| 575 |
|
| 576 |
case UnitActionBuilt: |
| 577 |
e_color = color = ColorGray; |
| 578 |
break; |
| 579 |
|
| 580 |
case UnitActionResource: |
| 581 |
e_color = color = ColorYellow; |
| 582 |
dest = true; |
| 583 |
break; |
| 584 |
|
| 585 |
default: |
| 586 |
e_color = color = ColorGray; |
| 587 |
DebugPrint("Unknown action %d\n" _C_ order->Action); |
| 588 |
break; |
| 589 |
} |
| 590 |
|
| 591 |
Video.FillCircleClip(color, x1, y1, 2); |
| 592 |
if (dest) { |
| 593 |
Video.DrawLineClip(color, x1, y1, x2, y2); |
| 594 |
Video.FillCircleClip(e_color, x2, y2, 3); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
** Show the current order of a unit. |
| 600 |
** |
| 601 |
** @param unit Pointer to the unit. |
| 602 |
*/ |
| 603 |
void ShowOrder(const CUnit *unit) |
| 604 |
{ |
| 605 |
int x1; |
| 606 |
int y1; |
| 607 |
COrder *order; |
| 608 |
|
| 609 |
if (unit->Destroyed) { |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
if (unit->Player != ThisPlayer && !ThisPlayer->IsAllied(unit)) { |
| 614 |
return; |
| 615 |
} |
| 616 |
|
| 617 |
// Get current position |
| 618 |
x1 = CurrentViewport->Map2ViewportX( |
| 619 |
unit->X) + unit->IX + unit->Type->TileWidth * TileSizeX / 2; |
| 620 |
y1 = CurrentViewport->Map2ViewportY( |
| 621 |
unit->Y) + unit->IY + unit->Type->TileHeight * TileSizeY / 2; |
| 622 |
|
| 623 |
// If the current order is cancelled show the next one |
| 624 |
if (unit->OrderCount > 1 && unit->OrderFlush) { |
| 625 |
order = unit->Orders[1]; |
| 626 |
} else { |
| 627 |
order = unit->Orders[0]; |
| 628 |
} |
| 629 |
ShowSingleOrder(unit, x1, y1, order); |
| 630 |
|
| 631 |
// Show the rest of the orders |
| 632 |
for (int i = 1 + (unit->OrderFlush ? 1 : 0); i < unit->OrderCount; ++i) { |
| 633 |
GetOrderPosition(unit, unit->Orders[i - 1], &x1, &y1); |
| 634 |
ShowSingleOrder(unit, x1, y1, unit->Orders[i]); |
| 635 |
} |
| 636 |
|
| 637 |
// Show order for new trained units |
| 638 |
if (!CanMove(unit)) { |
| 639 |
ShowSingleOrder(unit, x1, y1, &unit->NewOrder); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
/** |
| 644 |
** Draw additional informations of a unit. |
| 645 |
** |
| 646 |
** @param unit Unit pointer of drawn unit. |
| 647 |
** @param type Unit-type pointer. |
| 648 |
** @param x X screen pixel position of unit. |
| 649 |
** @param y Y screen pixel position of unit. |
| 650 |
** |
| 651 |
** @todo FIXME: The different styles should become a function call. |
| 652 |
*/ |
| 653 |
static void DrawInformations(const CUnit *unit, const CUnitType *type, int x, int y) |
| 654 |
{ |
| 655 |
const CUnitStats *stats; |
| 656 |
int r; |
| 657 |
|
| 658 |
#if 0 && DEBUG // This is for showing vis counts and refs. |
| 659 |
char buf[10]; |
| 660 |
sprintf_s(buf, sizeof(buf), "%d%c%c%d", unit->VisCount[ThisPlayer->Index], |
| 661 |
unit->Seen.ByPlayer & (1 << ThisPlayer->Index) ? 'Y' : 'N', |
| 662 |
unit->Seen.Destroyed & (1 << ThisPlayer->Index) ? 'Y' : 'N', |
| 663 |
unit->Refs); |
| 664 |
VideoDrawTextClip(x + 10, y + 10, 1, buf); |
| 665 |
#endif |
| 666 |
|
| 667 |
stats = unit->Stats; |
| 668 |
|
| 669 |
// |
| 670 |
// For debug draw sight, react and attack range! |
| 671 |
// |
| 672 |
if (NumSelected == 1 && unit->Selected) { |
| 673 |
if (Preference.ShowSightRange) { |
| 674 |
// Radius -1 so you can see all ranges |
| 675 |
Video.DrawCircleClip(ColorGreen, |
| 676 |
x + type->TileWidth * TileSizeX / 2, |
| 677 |
y + type->TileHeight * TileSizeY / 2, |
| 678 |
((stats->Variables[SIGHTRANGE_INDEX].Max + (type->TileWidth - 1)) * TileSizeX) - 1); |
| 679 |
} |
| 680 |
if (type->CanAttack) { |
| 681 |
if (Preference.ShowReactionRange) { |
| 682 |
r = (unit->Player->Type == PlayerPerson) ? |
| 683 |
type->ReactRangePerson : type->ReactRangeComputer; |
| 684 |
if (r) { |
| 685 |
Video.DrawCircleClip(ColorBlue, |
| 686 |
x + type->TileWidth * TileSizeX / 2, |
| 687 |
y + type->TileHeight * TileSizeY / 2, |
| 688 |
(r + (type->TileWidth - 1)) * TileSizeX); |
| 689 |
} |
| 690 |
} |
| 691 |
if (Preference.ShowAttackRange && stats->Variables[ATTACKRANGE_INDEX].Max) { |
| 692 |
// Radius + 1 so you can see all ranges |
| 693 |
Video.DrawCircleClip(ColorRed, |
| 694 |
x + type->TileWidth * TileSizeX / 2, |
| 695 |
y + type->TileHeight * TileSizeY / 2, |
| 696 |
(stats->Variables[ATTACKRANGE_INDEX].Max + (type->TileWidth - 1)) * TileSizeX + 1); |
| 697 |
} |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
// FIXME: johns: ugly check here, should be removed! |
| 702 |
if (unit->Orders[0]->Action != UnitActionDie && unit->IsVisible(ThisPlayer)) { |
| 703 |
DrawDecoration(unit, type, x, y); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
** Draw construction shadow. |
| 709 |
** |
| 710 |
** @param unit Unit pointer. |
| 711 |
** @param cframe Construction frame |
| 712 |
** @param frame Frame number to draw. |
| 713 |
** @param x X position. |
| 714 |
** @param y Y position. |
| 715 |
*/ |
| 716 |
static void DrawConstructionShadow(const CUnit *unit, const CConstructionFrame *cframe, |
| 717 |
int frame, int x, int y) |
| 718 |
{ |
| 719 |
if (cframe->File == ConstructionFileConstruction) { |
| 720 |
if (unit->Type->Construction->ShadowSprite) { |
| 721 |
x -= (unit->Type->Construction->Width - unit->Type->TileWidth * TileSizeX) / 2; |
| 722 |
x += unit->Type->OffsetX; |
| 723 |
y -= (unit->Type->Construction->Height - unit->Type->TileHeight * TileSizeY )/ 2; |
| 724 |
y += unit->Type->OffsetY; |
| 725 |
if (frame < 0) { |
| 726 |
unit->Type->Construction->ShadowSprite->DrawFrameClipX( |
| 727 |
-frame - 1, x, y); |
| 728 |
} else { |
| 729 |
unit->Type->Construction->ShadowSprite->DrawFrameClip( |
| 730 |
frame, x, y); |
| 731 |
} |
| 732 |
} |
| 733 |
} else { |
| 734 |
if (unit->Type->ShadowSprite) { |
| 735 |
x -= (unit->Type->ShadowWidth - unit->Type->TileWidth * TileSizeX) / 2; |
| 736 |
x += unit->Type->ShadowOffsetX + unit->Type->OffsetX; |
| 737 |
y -= (unit->Type->ShadowHeight - unit->Type->TileHeight * TileSizeY) / 2; |
| 738 |
y += unit->Type->ShadowOffsetY + unit->Type->OffsetY; |
| 739 |
if (frame < 0) { |
| 740 |
unit->Type->ShadowSprite->DrawFrameClipX(-frame - 1, x, y); |
| 741 |
} else { |
| 742 |
unit->Type->ShadowSprite->DrawFrameClip(frame, x, y); |
| 743 |
} |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
/** |
| 749 |
** Draw construction. |
| 750 |
** |
| 751 |
** @param unit Unit pointer. |
| 752 |
** @param cframe Construction frame to draw. |
| 753 |
** @param type Unit type. |
| 754 |
** @param frame Frame number. |
| 755 |
** @param x X position. |
| 756 |
** @param y Y position. |
| 757 |
*/ |
| 758 |
static void DrawConstruction(const CUnit *unit, const CConstructionFrame *cframe, |
| 759 |
const CUnitType *type, int frame, int x, int y) |
| 760 |
{ |
| 761 |
int player; |
| 762 |
|
| 763 |
player = unit->RescuedFrom ? unit->RescuedFrom->Index : unit->Player->Index; |
| 764 |
if (cframe->File == ConstructionFileConstruction) { |
| 765 |
const CConstruction *construction; |
| 766 |
|
| 767 |
construction = type->Construction; |
| 768 |
x -= construction->Width / 2; |
| 769 |
y -= construction->Height / 2; |
| 770 |
if (frame < 0) { |
| 771 |
construction->Sprite->DrawPlayerColorFrameClipX(player, -frame - 1, x, y); |
| 772 |
} else { |
| 773 |
construction->Sprite->DrawPlayerColorFrameClip(player, frame, x, y); |
| 774 |
} |
| 775 |
} else { |
| 776 |
x += type->OffsetX - type->Width / 2; |
| 777 |
y += type->OffsetY - type->Height / 2; |
| 778 |
if (frame < 0) { |
| 779 |
frame = -frame - 1; |
| 780 |
} |
| 781 |
type->Sprite->DrawPlayerColorFrameClip(player, frame, x, y); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
** Draw unit on map. |
| 787 |
*/ |
| 788 |
void CUnit::Draw() const |
| 789 |
{ |
| 790 |
int x; |
| 791 |
int y; |
| 792 |
int frame; |
| 793 |
int state; |
| 794 |
int constructed; |
| 795 |
CConstructionFrame *cframe; |
| 796 |
CUnitType *type; |
| 797 |
|
| 798 |
if (this->Type->Revealer) { // Revealers are not drawn |
| 799 |
return; |
| 800 |
} |
| 801 |
|
| 802 |
// Those should have been filtered. Check doesn't make sense with ReplayRevealMap |
| 803 |
Assert(ReplayRevealMap || this->Type->VisibleUnderFog || this->IsVisible(ThisPlayer)); |
| 804 |
|
| 805 |
if (ReplayRevealMap || this->IsVisible(ThisPlayer)) { |
| 806 |
type = this->Type; |
| 807 |
frame = this->Frame; |
| 808 |
y = this->IY; |
| 809 |
x = this->IX; |
| 810 |
x += CurrentViewport->Map2ViewportX(this->X); |
| 811 |
y += CurrentViewport->Map2ViewportY(this->Y); |
| 812 |
state = (this->Orders[0]->Action == UnitActionBuilt); |
| 813 |
constructed = this->Constructed; |
| 814 |
// Reset Type to the type being upgraded to |
| 815 |
if (state == 2) { |
| 816 |
type = this->Orders[0]->Type; |
| 817 |
} |
| 818 |
// This is trash unless the unit is being built, and that's when we use it. |
| 819 |
cframe = this->Data.Built.Frame; |
| 820 |
} else { |
| 821 |
y = this->Seen.IY; |
| 822 |
x = this->Seen.IX; |
| 823 |
x += CurrentViewport->Map2ViewportX(this->Seen.X); |
| 824 |
y += CurrentViewport->Map2ViewportY(this->Seen.Y); |
| 825 |
frame = this->Seen.Frame; |
| 826 |
type = this->Seen.Type; |
| 827 |
constructed = this->Seen.Constructed; |
| 828 |
state = this->Seen.State; |
| 829 |
cframe = this->Seen.CFrame; |
| 830 |
} |
| 831 |
|
| 832 |
if (!this->IsVisible(ThisPlayer) && frame == UnitNotSeen) { |
| 833 |
DebugPrint("FIXME: Something is wrong, unit %d not seen but drawn time %lu?.\n" _C_ |
| 834 |
this->Slot _C_ GameCycle); |
| 835 |
return; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
if (state == 1 && constructed) { |
| 840 |
DrawConstructionShadow(this, cframe, frame, x, y); |
| 841 |
} else { |
| 842 |
DrawShadow(this, NULL, frame, x, y); |
| 843 |
} |
| 844 |
|
| 845 |
// |
| 846 |
// Show that the unit is selected |
| 847 |
// |
| 848 |
DrawUnitSelection(this); |
| 849 |
|
| 850 |
// |
| 851 |
// Now draw! |
| 852 |
// Buildings under construction/upgrade/ready. |
| 853 |
// |
| 854 |
if (state == 1) { |
| 855 |
if (constructed) { |
| 856 |
DrawConstruction(this, cframe, type, frame, |
| 857 |
x + (type->TileWidth * TileSizeX) / 2, |
| 858 |
y + (type->TileHeight * TileSizeY) / 2); |
| 859 |
} |
| 860 |
// |
| 861 |
// Draw the future unit type, if upgrading to it. |
| 862 |
// |
| 863 |
} else if (state == 2) { |
| 864 |
// FIXME: this frame is hardcoded!!! |
| 865 |
DrawUnitType(type, type->Sprite, |
| 866 |
this->RescuedFrom ? this->RescuedFrom->Index : this->Player->Index, |
| 867 |
frame < 0 ? -1 - 1 : 1, x, y); |
| 868 |
} else { |
| 869 |
DrawUnitType(type, type->Sprite, |
| 870 |
this->RescuedFrom ? this->RescuedFrom->Index : this->Player->Index, |
| 871 |
frame, x, y); |
| 872 |
} |
| 873 |
|
| 874 |
// Unit's extras not fully supported.. need to be decorations themselves. |
| 875 |
DrawInformations(this, type, x, y); |
| 876 |
} |
| 877 |
|
| 878 |
/** |
| 879 |
** Compare what order 2 units should be drawn on the map |
| 880 |
** |
| 881 |
** @param v1 First Unit to compare (**Unit) |
| 882 |
** @param v2 Second Unit to compare (**Unit) |
| 883 |
** |
| 884 |
** @return -1 for v1 < v2, 1 for v2 < v1 |
| 885 |
*/ |
| 886 |
static int DrawLevelCompare(const void *v1, const void *v2) { |
| 887 |
|
| 888 |
const CUnit *c1, *c2; |
| 889 |
int drawlevel1, drawlevel2; |
| 890 |
int y1, y2; |
| 891 |
|
| 892 |
c1 = *(CUnit **)v1; |
| 893 |
c2 = *(CUnit **)v2; |
| 894 |
|
| 895 |
if (c1->Orders[0]->Action == UnitActionDie && c1->Type->CorpseType) { |
| 896 |
drawlevel1 = c1->Type->CorpseType->DrawLevel; |
| 897 |
} else { |
| 898 |
drawlevel1 = c1->Type->DrawLevel; |
| 899 |
} |
| 900 |
if (c2->Orders[0]->Action == UnitActionDie && c2->Type->CorpseType) { |
| 901 |
drawlevel2 = c2->Type->CorpseType->DrawLevel; |
| 902 |
} else { |
| 903 |
drawlevel2 = c2->Type->DrawLevel; |
| 904 |
} |
| 905 |
|
| 906 |
if (drawlevel1 != drawlevel2) { |
| 907 |
return drawlevel1 <= drawlevel2 ? -1 : 1; |
| 908 |
} |
| 909 |
|
| 910 |
// Compare the units' Y positions (center tile) on the map. |
| 911 |
// The size of the sprite (->Type->Height) plays no part in |
| 912 |
// this because many frames typically have a transparent area |
| 913 |
// at the bottom. |
| 914 |
y1 = (c1->Y * TileSizeY) + c1->IY + (c1->Type->TileHeight * TileSizeY / 2); |
| 915 |
y2 = (c2->Y * TileSizeY) + c2->IY + (c2->Type->TileHeight * TileSizeY / 2); |
| 916 |
if (y1 != y2) { |
| 917 |
return y1 - y2; |
| 918 |
} |
| 919 |
|
| 920 |
// Use X positions in case Y positions are equal. |
| 921 |
if (c1->X != c2->X) { |
| 922 |
return c1->X - c2->X; |
| 923 |
} |
| 924 |
|
| 925 |
// We don't really care which of these two units gets drawn in |
| 926 |
// front of the other. However, it must be consistent during |
| 927 |
// a frame, because qsort might crash otherwise, and should |
| 928 |
// also be consistent between frames, to avoid annoying |
| 929 |
// flicker. Therefore, use the slot numbers as the ultimate |
| 930 |
// fallback. |
| 931 |
return c1->Slot - c2->Slot; |
| 932 |
} |
| 933 |
/** |
| 934 |
** Find all units to draw in viewport. |
| 935 |
** |
| 936 |
** @param vp Viewport to be drawn. |
| 937 |
** @param table Table of units to return in sorted order |
| 938 |
** @param tablesize Size of table array |
| 939 |
*/ |
| 940 |
int FindAndSortUnits(const CViewport *vp, CUnit **table, int tablesize) |
| 941 |
{ |
| 942 |
// |
| 943 |
// Select all units touching the viewpoint. |
| 944 |
// |
| 945 |
int n = UnitCache.Select(vp->MapX - 1, vp->MapY - 1, vp->MapX + vp->MapWidth + 1, |
| 946 |
vp->MapY + vp->MapHeight + 1, table, tablesize); |
| 947 |
|
| 948 |
// If only one unit is selected, then draw that one even if it |
| 949 |
// is not in the viewport, as long as the player is entitled |
| 950 |
// to see it. This is because the sight, react, and attack |
| 951 |
// circles drawn by DrawInformations may still reach the |
| 952 |
// viewport. |
| 953 |
CUnit *addToTable = NULL; |
| 954 |
if (NumSelected == 1 |
| 955 |
&& ((ThisPlayer != NULL && Selected[0]->IsVisible(ThisPlayer)) |
| 956 |
|| ReplayRevealMap)) { |
| 957 |
addToTable = Selected[0]; |
| 958 |
} |
| 959 |
|
| 960 |
for (int i = 0; i < n; ++i) { |
| 961 |
if (!table[i]->IsVisibleInViewport(vp)) { |
| 962 |
table[i--] = table[--n]; |
| 963 |
} else if (table[i] == addToTable) { |
| 964 |
// The unit is already in the table and need |
| 965 |
// not be added. |
| 966 |
addToTable = NULL; |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
if (addToTable != NULL && n < tablesize) { |
| 971 |
table[n++] = addToTable; |
| 972 |
} |
| 973 |
|
| 974 |
if (n) { |
| 975 |
qsort((void *)table, n, sizeof(CUnit *), DrawLevelCompare); |
| 976 |
} |
| 977 |
|
| 978 |
return n; |
| 979 |
} |
| 980 |
|
| 981 |
//@} |