| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name mainscr.cpp - The main screen. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer, Valery Shchedrin, and |
| 13 |
// Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
-- Includes |
| 34 |
----------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <stdarg.h> |
| 40 |
#include <math.h> |
| 41 |
#include <sstream> |
| 42 |
|
| 43 |
#include "stratagus.h" |
| 44 |
#include "video.h" |
| 45 |
#include "font.h" |
| 46 |
#include "sound.h" |
| 47 |
#include "unitsound.h" |
| 48 |
#include "unittype.h" |
| 49 |
#include "player.h" |
| 50 |
#include "unit.h" |
| 51 |
#include "upgrade.h" |
| 52 |
#include "icons.h" |
| 53 |
#include "interface.h" |
| 54 |
#include "ui.h" |
| 55 |
#include "map.h" |
| 56 |
#include "trigger.h" |
| 57 |
#include "network.h" |
| 58 |
#include "menus.h" |
| 59 |
#include "spells.h" |
| 60 |
|
| 61 |
/*---------------------------------------------------------------------------- |
| 62 |
-- MENU BUTTON |
| 63 |
----------------------------------------------------------------------------*/ |
| 64 |
|
| 65 |
/** |
| 66 |
** Draw menu button area. |
| 67 |
*/ |
| 68 |
void DrawMenuButtonArea(void) |
| 69 |
{ |
| 70 |
if (!IsNetworkGame()) { |
| 71 |
if (UI.MenuButton.X != -1) { |
| 72 |
DrawMenuButton(UI.MenuButton.Style, |
| 73 |
(ButtonAreaUnderCursor == ButtonAreaMenu && |
| 74 |
ButtonUnderCursor == ButtonUnderMenu ? MI_FLAGS_ACTIVE : 0) | |
| 75 |
(GameMenuButtonClicked ? MI_FLAGS_CLICKED : 0), |
| 76 |
UI.MenuButton.X, UI.MenuButton.Y, |
| 77 |
UI.MenuButton.Text); |
| 78 |
} |
| 79 |
} else { |
| 80 |
if (UI.NetworkMenuButton.X != -1) { |
| 81 |
DrawMenuButton(UI.NetworkMenuButton.Style, |
| 82 |
(ButtonAreaUnderCursor == ButtonAreaMenu && |
| 83 |
ButtonUnderCursor == ButtonUnderNetworkMenu ? MI_FLAGS_ACTIVE : 0) | |
| 84 |
(GameMenuButtonClicked ? MI_FLAGS_CLICKED : 0), |
| 85 |
UI.NetworkMenuButton.X, UI.NetworkMenuButton.Y, |
| 86 |
UI.NetworkMenuButton.Text); |
| 87 |
} |
| 88 |
if (UI.NetworkDiplomacyButton.X != -1) { |
| 89 |
DrawMenuButton(UI.NetworkDiplomacyButton.Style, |
| 90 |
(ButtonAreaUnderCursor == ButtonAreaMenu && |
| 91 |
ButtonUnderCursor == ButtonUnderNetworkDiplomacy ? MI_FLAGS_ACTIVE : 0) | |
| 92 |
(GameDiplomacyButtonClicked ? MI_FLAGS_CLICKED : 0), |
| 93 |
UI.NetworkDiplomacyButton.X, UI.NetworkDiplomacyButton.Y, |
| 94 |
UI.NetworkDiplomacyButton.Text); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/*---------------------------------------------------------------------------- |
| 100 |
-- Icons |
| 101 |
----------------------------------------------------------------------------*/ |
| 102 |
|
| 103 |
/** |
| 104 |
** Draw life bar of a unit at x,y. |
| 105 |
** Placed under icons on top-panel. |
| 106 |
** |
| 107 |
** @param unit Pointer to unit. |
| 108 |
** @param x Screen X postion of icon |
| 109 |
** @param y Screen Y postion of icon |
| 110 |
*/ |
| 111 |
static void UiDrawLifeBar(const CUnit *unit, int x, int y) |
| 112 |
{ |
| 113 |
// FIXME: add icon borders |
| 114 |
y += unit->Type->Icon.Icon->G->Height; |
| 115 |
Video.FillRectangleClip(ColorBlack, x, y, |
| 116 |
unit->Type->Icon.Icon->G->Width, 7); |
| 117 |
|
| 118 |
if (unit->Variable[HP_INDEX].Value) { |
| 119 |
Uint32 color; |
| 120 |
int f = (100 * unit->Variable[HP_INDEX].Value) / unit->Variable[HP_INDEX].Max; |
| 121 |
|
| 122 |
if (f > 75) { |
| 123 |
color = ColorDarkGreen; |
| 124 |
} else if (f > 50) { |
| 125 |
color = ColorYellow; |
| 126 |
} else if (f > 25) { |
| 127 |
color = ColorOrange; |
| 128 |
} else { |
| 129 |
color = ColorRed; |
| 130 |
} |
| 131 |
|
| 132 |
f = (f * (unit->Type->Icon.Icon->G->Width)) / 100; |
| 133 |
Video.FillRectangleClip(color, x + 1, y + 1, f, 5); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
** Draw charge bar of a unit at x,y. |
| 139 |
** Placed under icons on top-panel. |
| 140 |
** |
| 141 |
** @param unit Pointer to unit. |
| 142 |
** @param x Screen X postion of icon |
| 143 |
** @param y Screen Y postion of icon |
| 144 |
*/ |
| 145 |
static void UiDrawChargeBar(const CUnit *unit, int x, int y) |
| 146 |
{ |
| 147 |
// FIXME: add icon borders |
| 148 |
y += unit->Type->Icon.Icon->G->Height; |
| 149 |
Video.FillRectangleClip(ColorBlack, x, y + 3, |
| 150 |
unit->Type->Icon.Icon->G->Width, 4); |
| 151 |
|
| 152 |
if (unit->Stats->Variables[CHARGE_INDEX].Max) { |
| 153 |
int f = (100 * unit->Variable[CHARGE_INDEX].Value) / unit->Variable[CHARGE_INDEX].Max; |
| 154 |
f = (f * (unit->Type->Icon.Icon->G->Width)) / 100; |
| 155 |
Video.FillRectangleClip(ColorBlue, x + 1, y + 3 + 1, f, 2); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
** Draw unit stats |
| 161 |
*/ |
| 162 |
static void DrawUnitStats(const CUnit *unit) |
| 163 |
{ |
| 164 |
int x = UI.InfoPanel.X; |
| 165 |
int y = UI.InfoPanel.Y; |
| 166 |
CUnitType *type = unit->Type; |
| 167 |
|
| 168 |
// Armor |
| 169 |
std::ostringstream armor; |
| 170 |
armor << _("Armor: ") << type->Variable[ARMOR_INDEX].Value; |
| 171 |
VideoDrawText(x + 16, y + 83, GameFont, armor.str()); |
| 172 |
|
| 173 |
if (type->Variable[RADAR_INDEX].Value) { |
| 174 |
// Radar Range |
| 175 |
std::ostringstream radarRange; |
| 176 |
radarRange << _("Radar Range: ") << type->Variable[RADAR_INDEX].Value; |
| 177 |
VideoDrawText(x + 16, y + 97, GameFont, radarRange.str()); |
| 178 |
} else { |
| 179 |
// Sight Range |
| 180 |
std::ostringstream sightRange; |
| 181 |
sightRange << _("Sight Range: ") << type->Variable[SIGHTRANGE_INDEX].Value; |
| 182 |
VideoDrawText(x + 16, y + 97, GameFont, sightRange.str()); |
| 183 |
} |
| 184 |
|
| 185 |
if (type->CanAttack) { |
| 186 |
// Kills |
| 187 |
std::ostringstream kills; |
| 188 |
kills << _("Kills: ") << "~<" << unit->Variable[KILL_INDEX].Value << "~>"; |
| 189 |
VideoDrawTextCentered(x + 114, y + 52, GameFont, kills.str()); |
| 190 |
|
| 191 |
// Attack Range |
| 192 |
std::ostringstream attackRange; |
| 193 |
attackRange << _("Attack Range: ") << type->Variable[ATTACKRANGE_INDEX].Value; |
| 194 |
VideoDrawText(x + 16, y + 111, GameFont, attackRange.str()); |
| 195 |
|
| 196 |
// Damage |
| 197 |
int min_damage = std::max(1, type->Variable[PIERCINGDAMAGE_INDEX].Value / 2); |
| 198 |
int max_damage = type->Variable[PIERCINGDAMAGE_INDEX].Value + type->Variable[BASICDAMAGE_INDEX].Value; |
| 199 |
std::ostringstream damage; |
| 200 |
damage << _("Damage: ") << min_damage << "-" << max_damage; |
| 201 |
VideoDrawText(x + 16, y + 125, GameFont, damage.str()); |
| 202 |
} else if (unit->Variable[CHARGE_INDEX].Max != 0) { |
| 203 |
// Charge |
| 204 |
std::ostringstream charge; |
| 205 |
charge << _("Charge: ") << unit->Variable[CHARGE_INDEX].Value; |
| 206 |
VideoDrawText(x + 16, y + 111, GameFont, charge.str()); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
** Draw training units |
| 212 |
*/ |
| 213 |
static void DrawTrainingUnits(const CUnit *unit) |
| 214 |
{ |
| 215 |
if (unit->OrderCount == 1 || unit->Orders[1]->Action != UnitActionTrain) { |
| 216 |
if (!UI.SingleTrainingText.empty()) { |
| 217 |
VideoDrawText(UI.SingleTrainingTextX, UI.SingleTrainingTextY, |
| 218 |
UI.SingleTrainingFont, UI.SingleTrainingText); |
| 219 |
} |
| 220 |
if (UI.SingleTrainingButton) { |
| 221 |
CUIButton *button = UI.SingleTrainingButton; |
| 222 |
bool mouseOver = (ButtonAreaUnderCursor == ButtonAreaTraining && ButtonUnderCursor == 0); |
| 223 |
|
| 224 |
unit->Orders[0]->Type->Icon.Icon->DrawUnitIcon(unit->Player, button->Style, |
| 225 |
mouseOver ? (IconActive | (MouseButtons & LeftButton)) : 0, |
| 226 |
button->X, button->Y, ""); |
| 227 |
} |
| 228 |
} else { |
| 229 |
if (!UI.TrainingText.empty()) { |
| 230 |
VideoDrawTextCentered(UI.TrainingTextX, UI.TrainingTextY, |
| 231 |
UI.TrainingFont, UI.TrainingText); |
| 232 |
} |
| 233 |
if (!UI.TrainingButtons.empty()) { |
| 234 |
size_t currentButton = 0; |
| 235 |
|
| 236 |
for (int i = 0; i < unit->OrderCount; ++i) { |
| 237 |
if (unit->Orders[i]->Action == UnitActionTrain && currentButton < UI.TrainingButtons.size()) { |
| 238 |
CUIButton *button = &UI.TrainingButtons[i]; |
| 239 |
bool mouseOver = (ButtonAreaUnderCursor == ButtonAreaTraining && ButtonUnderCursor == i); |
| 240 |
|
| 241 |
unit->Orders[i]->Type->Icon.Icon->DrawUnitIcon(unit->Player, button->Style, |
| 242 |
mouseOver ? (IconActive | (MouseButtons & LeftButton)) : 0, |
| 243 |
button->X, button->Y, ""); |
| 244 |
|
| 245 |
currentButton++; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
** Draw transporting units |
| 254 |
*/ |
| 255 |
static void DrawTransportingUnits(const CUnit *unit) |
| 256 |
{ |
| 257 |
const CUnit *insideUnit = unit->UnitInside; |
| 258 |
int currentButton = 0; |
| 259 |
|
| 260 |
for (int i = 0; i < unit->InsideCount; ++i, insideUnit = insideUnit->NextContained) { |
| 261 |
if (insideUnit->Boarded && currentButton < (int)UI.TransportingButtons.size()) { |
| 262 |
CUIButton *button = &UI.TransportingButtons[currentButton]; |
| 263 |
bool mouseOver = (ButtonAreaUnderCursor == ButtonAreaTransporting && ButtonUnderCursor == currentButton); |
| 264 |
|
| 265 |
insideUnit->Type->Icon.Icon->DrawUnitIcon(unit->Player, button->Style, |
| 266 |
mouseOver ? (IconActive | (MouseButtons & LeftButton)) : 0, |
| 267 |
button->X, button->Y, ""); |
| 268 |
|
| 269 |
UiDrawLifeBar(insideUnit, button->X, button->Y); |
| 270 |
|
| 271 |
if (insideUnit->Type->CanCastSpell && insideUnit->Variable[CHARGE_INDEX].Max) { |
| 272 |
UiDrawChargeBar(insideUnit, button->X, button->Y); |
| 273 |
} |
| 274 |
|
| 275 |
if (mouseOver) { |
| 276 |
UI.StatusLine.Set(insideUnit->Type->Name); |
| 277 |
} |
| 278 |
|
| 279 |
++currentButton; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
** Draw the unit info in the info panel. |
| 286 |
** Called when a single unit is selected or the mouse hovers over a unit. |
| 287 |
** |
| 288 |
** @param unit Pointer to unit. |
| 289 |
*/ |
| 290 |
static void DrawUnitInfo(CUnit *unit) |
| 291 |
{ |
| 292 |
int x = UI.InfoPanel.X; |
| 293 |
int y = UI.InfoPanel.Y; |
| 294 |
bool isEnemy = unit->IsEnemy(ThisPlayer); |
| 295 |
bool isNeutral = (unit->Player->Type == PlayerNeutral); |
| 296 |
|
| 297 |
UpdateUnitVariables(unit); |
| 298 |
|
| 299 |
// Draw icon and life bar |
| 300 |
if (UI.SingleSelectedButton) { |
| 301 |
CUIButton *button = UI.SingleSelectedButton; |
| 302 |
bool mouseOver = (ButtonAreaUnderCursor == ButtonAreaSelected && ButtonUnderCursor == 0); |
| 303 |
|
| 304 |
unit->Type->Icon.Icon->DrawUnitIcon(unit->Player, button->Style, |
| 305 |
mouseOver ? (IconActive | (MouseButtons & LeftButton)) : 0, |
| 306 |
button->X, button->Y, ""); |
| 307 |
if (!isNeutral) { |
| 308 |
UiDrawLifeBar(unit, button->X, button->Y); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// Unit type name |
| 313 |
VideoDrawTextCentered(x + 114, y + 25, GameFont, unit->Type->Name); |
| 314 |
|
| 315 |
// Hit points |
| 316 |
if (!isEnemy && !isNeutral) { |
| 317 |
std::ostringstream os; |
| 318 |
os << unit->Variable[HP_INDEX].Value << "/" << unit->Variable[HP_INDEX].Max; |
| 319 |
VideoDrawTextCentered(x + 38, y + 62, SmallFont, os.str()); |
| 320 |
} |
| 321 |
|
| 322 |
// Resource amount |
| 323 |
if (unit->Type->CanHarvestFrom && isNeutral) { |
| 324 |
std::string resourceName; |
| 325 |
int amount = 0; |
| 326 |
|
| 327 |
for (int i = 0; i < MaxCosts; ++i) { |
| 328 |
if (unit->ResourcesHeld[i] != 0) { |
| 329 |
resourceName = _(DefaultDisplayResourceNames[i].c_str()); |
| 330 |
amount = unit->ResourcesHeld[i] / CYCLES_PER_SECOND; |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
std::ostringstream os; |
| 336 |
os << resourceName << ": " << amount; |
| 337 |
VideoDrawTextCentered(x + 76, y + 86, GameFont, os.str()); |
| 338 |
} |
| 339 |
|
| 340 |
// |
| 341 |
// Show extra info if only one unit is selected. |
| 342 |
// |
| 343 |
if (NumSelected == 1 && Selected[0] == unit) { |
| 344 |
// Training units. |
| 345 |
if (!isEnemy && unit->Orders[0]->Action == UnitActionTrain) { |
| 346 |
DrawTrainingUnits(unit); |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
// Transporting units. |
| 351 |
if (!isEnemy && unit->Type->CanTransport && unit->BoardCount) { |
| 352 |
DrawTransportingUnits(unit); |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
// My unit stats |
| 357 |
if (!isEnemy && !isNeutral && |
| 358 |
unit->Orders[0]->Action != UnitActionBuilt) { |
| 359 |
DrawUnitStats(unit); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/*---------------------------------------------------------------------------- |
| 365 |
-- RESOURCES |
| 366 |
----------------------------------------------------------------------------*/ |
| 367 |
|
| 368 |
/** |
| 369 |
** Draw the player resource in top line. |
| 370 |
** |
| 371 |
** @todo FIXME: make DrawResources more configurable (format, font). |
| 372 |
*/ |
| 373 |
void DrawResources(void) |
| 374 |
{ |
| 375 |
char tmp[128]; |
| 376 |
int totalproduction = 0; |
| 377 |
int totalrequested = 0; |
| 378 |
int i; |
| 379 |
int p = 0; |
| 380 |
|
| 381 |
for (i = 0; i < MaxCosts; ++i) { |
| 382 |
sprintf_s(tmp, sizeof(tmp), "%s:+%d-%d %d/%d", |
| 383 |
_(DefaultDisplayResourceNames[i].c_str()), |
| 384 |
ThisPlayer->ProductionRate[i], |
| 385 |
ThisPlayer->RequestedUtilizationRate[i], |
| 386 |
ThisPlayer->StoredResources[i] / CYCLES_PER_SECOND / 100, |
| 387 |
ThisPlayer->StorageCapacity[i] / CYCLES_PER_SECOND / 100); |
| 388 |
VideoDrawText(40 + 176 * i, 1, GameFont, tmp); |
| 389 |
totalproduction += ThisPlayer->ProductionRate[i]; |
| 390 |
totalrequested += ThisPlayer->RequestedUtilizationRate[i]; |
| 391 |
} |
| 392 |
|
| 393 |
if (totalproduction + totalrequested) { |
| 394 |
p = 100 - abs(totalproduction - totalrequested) * 100 / (totalproduction + totalrequested); |
| 395 |
} |
| 396 |
sprintf_s(tmp, sizeof(tmp), "%d%%", p); |
| 397 |
VideoDrawText(400, 1, GameFont, tmp); |
| 398 |
} |
| 399 |
|
| 400 |
/*---------------------------------------------------------------------------- |
| 401 |
-- MESSAGE |
| 402 |
----------------------------------------------------------------------------*/ |
| 403 |
|
| 404 |
#define MESSAGES_MAX 10 /// How many can be displayed |
| 405 |
|
| 406 |
static char MessagesEvent[MESSAGES_MAX][64]; /// Array of event messages |
| 407 |
static int MessagesEventX[MESSAGES_MAX]; /// X coordinate of event |
| 408 |
static int MessagesEventY[MESSAGES_MAX]; /// Y coordinate of event |
| 409 |
static int MessagesEventCount; /// Number of event messages |
| 410 |
static int MessagesEventIndex; /// Current event index |
| 411 |
|
| 412 |
class MessagesDisplay |
| 413 |
{ |
| 414 |
public: |
| 415 |
MessagesDisplay() |
| 416 |
{ |
| 417 |
CleanMessages(); |
| 418 |
} |
| 419 |
|
| 420 |
void UpdateMessages(); |
| 421 |
void AddUniqueMessage(const char *s); |
| 422 |
void DrawMessages(); |
| 423 |
void CleanMessages(); |
| 424 |
|
| 425 |
protected: |
| 426 |
void ShiftMessages(); |
| 427 |
void AddMessage(const char *msg); |
| 428 |
bool CheckRepeatMessage(const char *msg); |
| 429 |
|
| 430 |
private: |
| 431 |
char Messages[MESSAGES_MAX][128]; /// Array of messages |
| 432 |
int MessagesCount; /// Number of messages |
| 433 |
int MessagesSameCount; /// Counts same message repeats |
| 434 |
int MessagesScrollY; |
| 435 |
unsigned long MessagesFrameTimeout; /// Frame to expire message |
| 436 |
}; |
| 437 |
|
| 438 |
/** |
| 439 |
** Shift messages array by one. |
| 440 |
*/ |
| 441 |
void MessagesDisplay::ShiftMessages() |
| 442 |
{ |
| 443 |
if (MessagesCount) { |
| 444 |
--MessagesCount; |
| 445 |
for (int z = 0; z < MessagesCount; ++z) { |
| 446 |
strcpy_s(Messages[z], sizeof(Messages[z]), Messages[z + 1]); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
** Update messages |
| 453 |
*/ |
| 454 |
void MessagesDisplay::UpdateMessages() |
| 455 |
{ |
| 456 |
if (!MessagesCount) { |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
// Scroll/remove old message line |
| 461 |
unsigned long ticks = GetTicks(); |
| 462 |
if (MessagesFrameTimeout < ticks) { |
| 463 |
++MessagesScrollY; |
| 464 |
if (MessagesScrollY == UI.MessageFont->Height() + 1) { |
| 465 |
MessagesFrameTimeout = ticks + UI.MessageScrollSpeed * 1000; |
| 466 |
MessagesScrollY = 0; |
| 467 |
ShiftMessages(); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
** Draw message(s). |
| 474 |
*/ |
| 475 |
void MessagesDisplay::DrawMessages() |
| 476 |
{ |
| 477 |
// background so the text is easier to read |
| 478 |
if (MessagesCount) { |
| 479 |
Uint32 color = Video.MapRGB(TheScreen->format, 38, 38, 78); |
| 480 |
Video.FillTransRectangleClip(color, UI.MapArea.X + 8, UI.MapArea.Y + 8, |
| 481 |
UI.MapArea.EndX - UI.MapArea.X - 16, MessagesCount * (UI.MessageFont->Height() + 1) - MessagesScrollY, 0x80); |
| 482 |
} |
| 483 |
|
| 484 |
// Draw message line(s) |
| 485 |
for (int z = 0; z < MessagesCount; ++z) { |
| 486 |
if (z == 0) { |
| 487 |
PushClipping(); |
| 488 |
SetClipping(UI.MapArea.X + 8, UI.MapArea.Y + 8, Video.Width - 1, |
| 489 |
Video.Height - 1); |
| 490 |
} |
| 491 |
VideoDrawTextClip(UI.MapArea.X + 8, |
| 492 |
UI.MapArea.Y + 8 + z * (UI.MessageFont->Height() + 1) - MessagesScrollY, |
| 493 |
UI.MessageFont, Messages[z]); |
| 494 |
if (z == 0) { |
| 495 |
PopClipping(); |
| 496 |
} |
| 497 |
} |
| 498 |
if (MessagesCount < 1) { |
| 499 |
MessagesSameCount = 0; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
** Adds message to the stack |
| 505 |
** |
| 506 |
** @param msg Message to add. |
| 507 |
*/ |
| 508 |
void MessagesDisplay::AddMessage(const char *msg) |
| 509 |
{ |
| 510 |
char *ptr; |
| 511 |
char *message; |
| 512 |
char *next; |
| 513 |
unsigned long ticks = GetTicks(); |
| 514 |
|
| 515 |
if (!MessagesCount) { |
| 516 |
MessagesFrameTimeout = ticks + UI.MessageScrollSpeed * 1000; |
| 517 |
} |
| 518 |
|
| 519 |
if (MessagesCount == MESSAGES_MAX) { |
| 520 |
// Out of space to store messages, can't scroll smoothly |
| 521 |
ShiftMessages(); |
| 522 |
MessagesFrameTimeout = ticks + UI.MessageScrollSpeed * 1000; |
| 523 |
MessagesScrollY = 0; |
| 524 |
} |
| 525 |
|
| 526 |
message = Messages[MessagesCount]; |
| 527 |
// Split long message into lines |
| 528 |
if (strlen(msg) >= sizeof(Messages[0])) { |
| 529 |
strncpy(message, msg, sizeof(Messages[0]) - 1); |
| 530 |
ptr = message + sizeof(Messages[0]) - 1; |
| 531 |
*ptr-- = '\0'; |
| 532 |
next = ptr + 1; |
| 533 |
while (ptr >= message) { |
| 534 |
if (*ptr == ' ') { |
| 535 |
*ptr = '\0'; |
| 536 |
next = ptr + 1; |
| 537 |
break; |
| 538 |
} |
| 539 |
--ptr; |
| 540 |
} |
| 541 |
if (ptr < message) { |
| 542 |
ptr = next - 1; |
| 543 |
} |
| 544 |
} else { |
| 545 |
strcpy_s(message, sizeof(Messages[MessagesCount]), msg); |
| 546 |
next = ptr = message + strlen(message); |
| 547 |
} |
| 548 |
|
| 549 |
while (UI.MessageFont->Width(message) + 8 >= UI.MapArea.EndX - UI.MapArea.X) { |
| 550 |
while (1) { |
| 551 |
--ptr; |
| 552 |
if (*ptr == ' ') { |
| 553 |
*ptr = '\0'; |
| 554 |
next = ptr + 1; |
| 555 |
break; |
| 556 |
} else if (ptr == message) { |
| 557 |
break; |
| 558 |
} |
| 559 |
} |
| 560 |
// No space found, wrap in the middle of a word |
| 561 |
if (ptr == message) { |
| 562 |
ptr = next - 1; |
| 563 |
while (UI.MessageFont->Width(message) + 8 >= UI.MapArea.EndX - UI.MapArea.X) { |
| 564 |
*--ptr = '\0'; |
| 565 |
} |
| 566 |
next = ptr + 1; |
| 567 |
break; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
++MessagesCount; |
| 572 |
|
| 573 |
if (strlen(msg) != (size_t)(ptr - message)) { |
| 574 |
AddMessage(msg + (next - message)); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
** Check if this message repeats |
| 580 |
** |
| 581 |
** @param msg Message to check. |
| 582 |
** |
| 583 |
** @return true to skip this message |
| 584 |
*/ |
| 585 |
bool MessagesDisplay::CheckRepeatMessage(const char *msg) |
| 586 |
{ |
| 587 |
if (MessagesCount < 1) { |
| 588 |
return false; |
| 589 |
} |
| 590 |
if (!strcmp(msg, Messages[MessagesCount - 1])) { |
| 591 |
++MessagesSameCount; |
| 592 |
return true; |
| 593 |
} |
| 594 |
if (MessagesSameCount > 0) { |
| 595 |
char temp[128]; |
| 596 |
int n; |
| 597 |
|
| 598 |
n = MessagesSameCount; |
| 599 |
MessagesSameCount = 0; |
| 600 |
sprintf_s(temp, sizeof(temp), _("Last message repeated ~<%d~> times"), n + 1); |
| 601 |
AddMessage(temp); |
| 602 |
} |
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
** Add a new message to display only if it differs from the preceeding one. |
| 608 |
*/ |
| 609 |
void MessagesDisplay::AddUniqueMessage(const char *s) |
| 610 |
{ |
| 611 |
if (!CheckRepeatMessage(s)) { |
| 612 |
AddMessage(s); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
** Clean up messages. |
| 618 |
*/ |
| 619 |
void MessagesDisplay::CleanMessages() |
| 620 |
{ |
| 621 |
MessagesCount = 0; |
| 622 |
MessagesSameCount = 0; |
| 623 |
MessagesScrollY = 0; |
| 624 |
MessagesFrameTimeout = 0; |
| 625 |
|
| 626 |
MessagesEventCount = 0; |
| 627 |
MessagesEventIndex = 0; |
| 628 |
} |
| 629 |
|
| 630 |
static MessagesDisplay allmessages; |
| 631 |
|
| 632 |
/** |
| 633 |
** Update messages |
| 634 |
*/ |
| 635 |
void UpdateMessages() { |
| 636 |
allmessages.UpdateMessages(); |
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
** Clean messages |
| 641 |
*/ |
| 642 |
void CleanMessages() |
| 643 |
{ |
| 644 |
allmessages.CleanMessages(); |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
** Draw messages |
| 649 |
*/ |
| 650 |
void DrawMessages() |
| 651 |
{ |
| 652 |
allmessages.DrawMessages(); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
** Set message to display. |
| 657 |
** |
| 658 |
** @param fmt To be displayed in text overlay. |
| 659 |
*/ |
| 660 |
void SetMessage(const char *fmt, ...) |
| 661 |
{ |
| 662 |
char temp[512]; |
| 663 |
va_list va; |
| 664 |
|
| 665 |
va_start(va, fmt); |
| 666 |
vsnprintf(temp, sizeof(temp) - 1, fmt, va); |
| 667 |
temp[sizeof(temp) - 1] = '\0'; |
| 668 |
va_end(va); |
| 669 |
allmessages.AddUniqueMessage(temp); |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
** Shift messages events array by one. |
| 674 |
*/ |
| 675 |
void ShiftMessagesEvent(void) |
| 676 |
{ |
| 677 |
if (MessagesEventCount) { |
| 678 |
--MessagesEventCount; |
| 679 |
for (int z = 0; z < MessagesEventCount; ++z) { |
| 680 |
MessagesEventX[z] = MessagesEventX[z + 1]; |
| 681 |
MessagesEventY[z] = MessagesEventY[z + 1]; |
| 682 |
strcpy_s(MessagesEvent[z], sizeof(MessagesEvent[z]), MessagesEvent[z + 1]); |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
** Set message to display. |
| 689 |
** |
| 690 |
** @param x Message X map origin. |
| 691 |
** @param y Message Y map origin. |
| 692 |
** @param fmt To be displayed in text overlay. |
| 693 |
*/ |
| 694 |
void SetMessageEvent(int x, int y, const char *fmt, ...) |
| 695 |
{ |
| 696 |
char temp[128]; |
| 697 |
va_list va; |
| 698 |
|
| 699 |
va_start(va, fmt); |
| 700 |
vsnprintf(temp, sizeof(temp) - 1, fmt, va); |
| 701 |
temp[sizeof(temp) - 1] = '\0'; |
| 702 |
va_end(va); |
| 703 |
allmessages.AddUniqueMessage(temp); |
| 704 |
|
| 705 |
if (MessagesEventCount == MESSAGES_MAX) { |
| 706 |
ShiftMessagesEvent(); |
| 707 |
} |
| 708 |
|
| 709 |
if (x != -1) { |
| 710 |
strcpy_s(MessagesEvent[MessagesEventCount], sizeof(MessagesEvent[MessagesEventCount]), temp); |
| 711 |
MessagesEventX[MessagesEventCount] = x; |
| 712 |
MessagesEventY[MessagesEventCount] = y; |
| 713 |
MessagesEventIndex = MessagesEventCount; |
| 714 |
++MessagesEventCount; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
** Goto message origin. |
| 720 |
*/ |
| 721 |
void CenterOnMessage(void) |
| 722 |
{ |
| 723 |
if (MessagesEventIndex >= MessagesEventCount) { |
| 724 |
MessagesEventIndex = 0; |
| 725 |
} |
| 726 |
if (MessagesEventCount == 0) { |
| 727 |
return; |
| 728 |
} |
| 729 |
UI.SelectedViewport->Center( |
| 730 |
MessagesEventX[MessagesEventIndex], MessagesEventY[MessagesEventIndex], |
| 731 |
TileSizeX / 2, TileSizeY / 2); |
| 732 |
SetMessage(_("~<Event: %s~>"), MessagesEvent[MessagesEventIndex]); |
| 733 |
++MessagesEventIndex; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
/*---------------------------------------------------------------------------- |
| 738 |
-- STATUS LINE |
| 739 |
----------------------------------------------------------------------------*/ |
| 740 |
|
| 741 |
/** |
| 742 |
** Draw status line. |
| 743 |
*/ |
| 744 |
void CStatusLine::Draw(void) |
| 745 |
{ |
| 746 |
if (!this->StatusLine.empty()) { |
| 747 |
PushClipping(); |
| 748 |
SetClipping(this->TextX, this->TextY, |
| 749 |
this->TextX + this->Width - 1, Video.Height - 1); |
| 750 |
VideoDrawTextClip(this->TextX, this->TextY, this->Font, |
| 751 |
this->StatusLine); |
| 752 |
PopClipping(); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
** Change status line to new text. |
| 758 |
** |
| 759 |
** @param status New status line information. |
| 760 |
*/ |
| 761 |
void CStatusLine::Set(const std::string &status) |
| 762 |
{ |
| 763 |
if (KeyState != KeyStateInput) { |
| 764 |
this->StatusLine = status; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
** Clear status line. |
| 770 |
*/ |
| 771 |
void CStatusLine::Clear(void) |
| 772 |
{ |
| 773 |
if (KeyState != KeyStateInput) { |
| 774 |
this->StatusLine.clear(); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
/*---------------------------------------------------------------------------- |
| 779 |
-- COSTS |
| 780 |
----------------------------------------------------------------------------*/ |
| 781 |
|
| 782 |
static int CostsCharge; /// charge cost to display in status line |
| 783 |
static int Costs[MaxCosts]; /// costs to display in status line |
| 784 |
|
| 785 |
/** |
| 786 |
** Draw costs in status line. |
| 787 |
** |
| 788 |
** @todo FIXME: make DrawCosts more configurable. |
| 789 |
*/ |
| 790 |
void DrawCosts(void) |
| 791 |
{ |
| 792 |
int x = UI.StatusLine.TextX + 268; |
| 793 |
if (CostsCharge) { |
| 794 |
if (UI.Resources[EnergyCost].G->NumFrames >= 3) { |
| 795 |
UI.Resources[EnergyCost].G->DrawFrameClip(2, x, UI.StatusLine.TextY); |
| 796 |
} |
| 797 |
|
| 798 |
VideoDrawNumber(x + 15, UI.StatusLine.TextY, GameFont, CostsCharge); |
| 799 |
x += 60; |
| 800 |
} |
| 801 |
|
| 802 |
for (int i = 0; i < MaxCosts; ++i) { |
| 803 |
if (Costs[i]) { |
| 804 |
if (UI.Resources[i].G) { |
| 805 |
UI.Resources[i].G->DrawFrameClip(UI.Resources[i].IconFrame, |
| 806 |
x, UI.StatusLine.TextY); |
| 807 |
} |
| 808 |
VideoDrawNumber(x + 15, UI.StatusLine.TextY, GameFont, Costs[i] / CYCLES_PER_SECOND); |
| 809 |
x += 60; |
| 810 |
if (x > Video.Width - 60) { |
| 811 |
break; |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
/** |
| 818 |
** Set costs in status line. |
| 819 |
** |
| 820 |
** @param charge Charge costs. |
| 821 |
** @param costs Resource costs, NULL pointer if all are zero. |
| 822 |
*/ |
| 823 |
void SetCosts(int charge, const int *costs) |
| 824 |
{ |
| 825 |
CostsCharge = charge; |
| 826 |
if (costs) { |
| 827 |
memcpy(Costs, costs, MaxCosts * sizeof(*costs)); |
| 828 |
} else { |
| 829 |
memset(Costs, 0, sizeof(Costs)); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
/** |
| 834 |
** Clear costs in status line. |
| 835 |
*/ |
| 836 |
void ClearCosts(void) |
| 837 |
{ |
| 838 |
SetCosts(0, NULL); |
| 839 |
} |
| 840 |
|
| 841 |
/*---------------------------------------------------------------------------- |
| 842 |
-- INFO PANEL |
| 843 |
----------------------------------------------------------------------------*/ |
| 844 |
|
| 845 |
/** |
| 846 |
** Draw info panel with more than one unit selected |
| 847 |
*/ |
| 848 |
static void DrawInfoPanelMultipleSelected() |
| 849 |
{ |
| 850 |
// Draw icons and a health bar |
| 851 |
for (int i = 0; i < std::min(NumSelected, (int)UI.SelectedButtons.size()); ++i) { |
| 852 |
CUIButton *button = &UI.SelectedButtons[i]; |
| 853 |
bool mouseOver = (ButtonAreaUnderCursor == ButtonAreaSelected && ButtonUnderCursor == i); |
| 854 |
|
| 855 |
Selected[i]->Type->Icon.Icon->DrawUnitIcon(ThisPlayer, button->Style, |
| 856 |
mouseOver ? (IconActive | (MouseButtons & LeftButton)) : 0, |
| 857 |
button->X, button->Y, ""); |
| 858 |
UiDrawLifeBar(Selected[i], button->X, button->Y); |
| 859 |
|
| 860 |
if (mouseOver) { |
| 861 |
UI.StatusLine.Set(Selected[i]->Type->Name); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
// Selected more than we can display |
| 866 |
if (NumSelected > (int)UI.SelectedButtons.size()) { |
| 867 |
std::ostringstream os; |
| 868 |
os << "+" << (unsigned)(NumSelected - UI.SelectedButtons.size()); |
| 869 |
|
| 870 |
VideoDrawText(UI.MaxSelectedTextX, UI.MaxSelectedTextY, |
| 871 |
UI.MaxSelectedFont, os.str()); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
/** |
| 876 |
** Draw info panel with one unit selected |
| 877 |
*/ |
| 878 |
static void DrawInfoPanelSingleSelected() |
| 879 |
{ |
| 880 |
DrawUnitInfo(Selected[0]); |
| 881 |
if (ButtonAreaUnderCursor == ButtonAreaSelected && ButtonUnderCursor == 0) { |
| 882 |
UI.StatusLine.Set(Selected[0]->Type->Name); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
/** |
| 887 |
** Draw info panel with no units selected |
| 888 |
*/ |
| 889 |
static void DrawInfoPanelNoneSelected() |
| 890 |
{ |
| 891 |
// Check if a unit is under the cursor |
| 892 |
if (UnitUnderCursor && UnitUnderCursor->IsVisible(ThisPlayer)) { |
| 893 |
DrawUnitInfo(UnitUnderCursor); |
| 894 |
return; |
| 895 |
} |
| 896 |
|
| 897 |
std::string nc; |
| 898 |
std::string rc; |
| 899 |
int x = UI.InfoPanel.X + 16; |
| 900 |
int y = UI.InfoPanel.Y + 8; |
| 901 |
|
| 902 |
VideoDrawText(x, y, GameFont, "Bos Wars"); |
| 903 |
y += 16; |
| 904 |
VideoDrawText(x, y, GameFont, "Cycle:"); |
| 905 |
VideoDrawNumber(x + 48, y, GameFont, GameCycle); |
| 906 |
VideoDrawNumber(x + 110, y, GameFont, |
| 907 |
CYCLES_PER_SECOND * VideoSyncSpeed / 100); |
| 908 |
y += 20; |
| 909 |
|
| 910 |
GetDefaultTextColors(nc, rc); |
| 911 |
for (int i = 0; i < PlayerMax - 1; ++i) { |
| 912 |
if (Players[i].Type != PlayerNobody) { |
| 913 |
if (ThisPlayer->Allied & (1 << Players[i].Index)) { |
| 914 |
SetDefaultTextColors(FontGreen, rc); |
| 915 |
} else if (ThisPlayer->Enemy & (1 << Players[i].Index)) { |
| 916 |
SetDefaultTextColors(FontRed, rc); |
| 917 |
} else { |
| 918 |
SetDefaultTextColors(nc, rc); |
| 919 |
} |
| 920 |
|
| 921 |
VideoDrawNumber(x + 15, y, GameFont, i); |
| 922 |
|
| 923 |
Video.DrawRectangle(ColorWhite,x, y, 12, 12); |
| 924 |
Video.FillRectangle(Players[i].Color, x + 1, y + 1, 10, 10); |
| 925 |
|
| 926 |
VideoDrawText(x + 27, y, GameFont,Players[i].Name); |
| 927 |
VideoDrawNumber(x + 117, y, GameFont,Players[i].Score); |
| 928 |
y += 14; |
| 929 |
} |
| 930 |
} |
| 931 |
SetDefaultTextColors(nc, rc); |
| 932 |
} |
| 933 |
|
| 934 |
/** |
| 935 |
** Draw info panel. |
| 936 |
*/ |
| 937 |
void CInfoPanel::Draw(void) |
| 938 |
{ |
| 939 |
if (NumSelected > 1) { |
| 940 |
DrawInfoPanelMultipleSelected(); |
| 941 |
} else if (NumSelected == 1) { |
| 942 |
DrawInfoPanelSingleSelected(); |
| 943 |
} else { |
| 944 |
DrawInfoPanelNoneSelected(); |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
/*---------------------------------------------------------------------------- |
| 949 |
-- TIMER |
| 950 |
----------------------------------------------------------------------------*/ |
| 951 |
|
| 952 |
/** |
| 953 |
** Draw the timer |
| 954 |
*/ |
| 955 |
void DrawTimer(void) |
| 956 |
{ |
| 957 |
if (!GameTimer.Init) { |
| 958 |
return; |
| 959 |
} |
| 960 |
|
| 961 |
int sec = GameTimer.Cycles / CYCLES_PER_SECOND % 60; |
| 962 |
int min = (GameTimer.Cycles / CYCLES_PER_SECOND / 60) % 60; |
| 963 |
int hour = (GameTimer.Cycles / CYCLES_PER_SECOND / 3600); |
| 964 |
char buf[30]; |
| 965 |
|
| 966 |
if (hour) { |
| 967 |
sprintf_s(buf, sizeof(buf), "%d:%02d:%02d", hour, min, sec); |
| 968 |
} else { |
| 969 |
sprintf_s(buf, sizeof(buf), "%d:%02d", min, sec); |
| 970 |
} |
| 971 |
|
| 972 |
VideoDrawText(UI.Timer.X, UI.Timer.Y, UI.Timer.Font, buf); |
| 973 |
} |
| 974 |
|
| 975 |
/** |
| 976 |
** Update the timer |
| 977 |
*/ |
| 978 |
void UpdateTimer(void) |
| 979 |
{ |
| 980 |
if (GameTimer.Running) { |
| 981 |
if (GameTimer.Increasing) { |
| 982 |
GameTimer.Cycles += GameCycle - GameTimer.LastUpdate; |
| 983 |
} else { |
| 984 |
GameTimer.Cycles -= GameCycle - GameTimer.LastUpdate; |
| 985 |
if (GameTimer.Cycles < 0) { |
| 986 |
GameTimer.Cycles = 0; |
| 987 |
} |
| 988 |
} |
| 989 |
GameTimer.LastUpdate = GameCycle; |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
//@} |