forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.cpp
610 lines (550 loc) · 16.8 KB
/
console.cpp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2019 Warzone 2100 Project
Warzone 2100 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; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Alex McLean, Pumpkin Studios, EIDOS Interactive */
/** \file
Functions for the in-game console.
*/
#include "lib/framework/frame.h"
#include "lib/framework/input.h"
#include "lib/gamelib/gtime.h"
#include "lib/ivis_opengl/pieblitfunc.h"
#include "lib/ivis_opengl/piestate.h"
#include "lib/ivis_opengl/textdraw.h"
#include "lib/sound/audio.h"
#include "lib/sound/audio_id.h"
#include "loadsave.h"
#include "ai.h"
#include "console.h"
#include "main.h"
#include "radar.h"
#include "hci.h"
#include <string>
#include <istream>
#include <sstream>
#include <deque>
// FIXME: When we switch over to full JS, use class version of this file
#define DEFAULT_MESSAGE_DURATION GAME_TICKS_PER_SEC * 4
// Chat/history "window"
#define CON_BORDER_WIDTH 4
#define CON_BORDER_HEIGHT 4
#define HISTORYBOX_X RET_X
#define HISTORYBOX_Y RET_Y - 80
#define NumDisplayLines 4
struct CONSOLE
{
UDWORD topX;
UDWORD topY;
UDWORD width;
UDWORD textDepth;
bool permanent;
};
struct CONSOLE_MESSAGE
{
WzText display;
UDWORD timeAdded; // When was it added to our list?
CONSOLE_TEXT_JUSTIFICATION JustifyType; // text justification
int player; // Player who sent this message or SYSTEM_MESSAGE
CONSOLE_MESSAGE(const std::string &text, iV_fonts fontID, UDWORD time, CONSOLE_TEXT_JUSTIFICATION justify, int plr)
: display(text, fontID), timeAdded(time), JustifyType(justify), player(plr) {}
CONSOLE_MESSAGE& operator =(CONSOLE_MESSAGE&& input)
{
display = std::move(input.display);
timeAdded = input.timeAdded;
JustifyType = input.JustifyType;
player = input.player;
return *this;
}
};
static std::deque<CONSOLE_MESSAGE> ActiveMessages; // we add all messages to this container
static std::deque<CONSOLE_MESSAGE> TeamMessages; // history of team/private communications
static std::deque<CONSOLE_MESSAGE> HistoryMessages; // history of all other communications
static std::deque<CONSOLE_MESSAGE> InfoMessages;
static bool bConsoleDropped = false; // Is the console history on or off?
static bool HistoryMode = false; // toggle between team & global history
static int updatepos = 0; // if user wants to scroll back the history log
static int linePitch = 0; // the pitch of a line
static bool showBackgroundColor = false; // if user wants to add more contrast to the history display
static CONSOLE mainConsole; // Stores the console dimensions and states
static CONSOLE historyConsole; // Stores the History console dimensions and states
static UDWORD messageDuration; /** How long do messages last for? */
static bool bTextBoxActive = false; /** Is there a box under the console text? */
static bool bConsoleDisplayEnabled = false; /** Is the console being displayed? */
static UDWORD consoleVisibleLines; /** How many lines are displayed? */
static int allowNewMessages; /** Whether new messages are allowed to be added */
char ConsoleString[MAX_CONSOLE_TMP_STRING_LENGTH]; /// Globally available string for new console messages.
static CONSOLE_CALC_LAYOUT_FUNC calcLayoutFunc;
/**
Specify how long messages will stay on screen.
*/
static void setConsoleMessageDuration(UDWORD time)
{
messageDuration = time;
}
void setConsoleCalcLayout(const CONSOLE_CALC_LAYOUT_FUNC& layoutFunc)
{
calcLayoutFunc = layoutFunc;
if (calcLayoutFunc != nullptr)
{
calcLayoutFunc();
}
}
/** Sets the system up */
void initConsoleMessages()
{
linePitch = iV_GetTextLineSize(font_regular);
bConsoleDropped = false;
setConsoleMessageDuration(DEFAULT_MESSAGE_DURATION); // Setup how long messages are displayed for
setConsoleBackdropStatus(true); // No box under the text
enableConsoleDisplay(true); // Turn on the console display
// Set up the main console size and position x,y,width
setConsoleCalcLayout([]() {
setConsoleSizePos(16, 32, pie_GetVideoBufferWidth() - 32);
});
historyConsole.topX = HISTORYBOX_X;
historyConsole.topY = HISTORYBOX_Y;
historyConsole.width = pie_GetVideoBufferWidth() - 32;
setConsoleLineInfo(MAX_CONSOLE_MESSAGES / 4 + 4);
setConsolePermanence(false, true); // We're not initially having permanent messages
permitNewConsoleMessages(true); // Allow new messages
}
void consoleScreenDidChangeSize(unsigned int oldWidth, unsigned int oldHeight, unsigned int newWidth, unsigned int newHeight)
{
if (calcLayoutFunc == nullptr) return;
calcLayoutFunc();
}
// toggle between team & global history
void setHistoryMode(bool mode)
{
HistoryMode = mode;
}
/** Open the console when it's closed and close it when it's open. */
void toggleConsoleDrop()
{
if (!bConsoleDropped)
{ // it was closed, so play open sound
bConsoleDropped = true;
audio_PlayTrack(ID_SOUND_WINDOWOPEN);
}
else
{
// play closing sound
audio_PlayTrack(ID_SOUND_WINDOWCLOSE);
bConsoleDropped = false;
}
}
/** Add a string to the console. */
bool addConsoleMessage(const char *Text, CONSOLE_TEXT_JUSTIFICATION jusType, SDWORD player, bool team)
{
if (!allowNewMessages)
{
return false; // Don't allow it to be added if we've disabled adding of new messages
}
std::istringstream stream(Text);
std::string lines;
while (std::getline(stream, lines))
{
// We got one "line" from the total string, now we must check
// to see if it fits, if not, we truncate it. (Full text is in the logs)
// NOTE: may want to break up line into multi-line so it matches the log
std::string FitText(lines);
while (!FitText.empty())
{
int pixelWidth = iV_GetTextWidth(FitText.c_str(), font_regular);
if (pixelWidth <= mainConsole.width)
{
break;
}
FitText.resize(FitText.length() - 1); // Erase last char.
}
debug(LOG_CONSOLE, "(to player %d): %s", (int)player, FitText.c_str());
if (player == INFO_MESSAGE)
{
InfoMessages.emplace_back(FitText, font_regular, realTime, jusType, player);
}
else
{
ActiveMessages.emplace_back(FitText, font_regular, realTime, jusType, player); // everything gets logged here for a specific period of time
if (team)
{
TeamMessages.emplace_back(FitText, font_regular, realTime, jusType, player); // persistent team specific logs
}
HistoryMessages.emplace_back(FitText, font_regular, realTime, jusType, player); // persistent messages (all types)
}
}
return true;
}
/// \return The number of active console messages
int getNumberConsoleMessages()
{
return (ActiveMessages.size());
}
/** Update the console messages.
This function will remove messages that are overdue.
*/
void updateConsoleMessages()
{
// If there are no messages or we're on permanent (usually for scripts) then exit
if ((!getNumberConsoleMessages() && InfoMessages.empty()) || mainConsole.permanent)
{
return;
}
for (auto i = InfoMessages.begin(); i != InfoMessages.end();)
{
if (realTime - i->timeAdded > messageDuration)
{
i = InfoMessages.erase(i);
}
else
{
++i;
}
}
// Time to kill all expired ones
for (auto i = ActiveMessages.begin(); i != ActiveMessages.end();)
{
if (realTime - i->timeAdded > messageDuration)
{
i = ActiveMessages.erase(i);
}
else
{
++i;
}
}
}
/**
Remove the top message on screen.
This and setConsoleMessageDuration should be sufficient to allow
us to put up messages that stay there until we remove them
ourselves - be sure and reset message duration afterwards
*/
void removeTopConsoleMessage()
{
if (getNumberConsoleMessages())
{
ActiveMessages.pop_front();
}
}
/** Clears just Active console messages */
void clearActiveConsole()
{
ActiveMessages.clear();
}
/** Clears all console messages */
void flushConsoleMessages()
{
ActiveMessages.clear();
TeamMessages.clear();
HistoryMessages.clear();
}
/** Sets console text color depending on message type */
static PIELIGHT getConsoleTextColor(int player)
{
// System messages
if (player == SYSTEM_MESSAGE)
{
return WZCOL_CONS_TEXT_SYSTEM;
}
else if (player == NOTIFY_MESSAGE)
{
return WZCOL_YELLOW;
}
else if (player == INFO_MESSAGE)
{
return WZCOL_CONS_TEXT_INFO;
}
else
{
// Don't use friend-foe colors in the lobby
if (bEnemyAllyRadarColor && (GetGameMode() == GS_NORMAL))
{
if (aiCheckAlliances(player, selectedPlayer))
{
return WZCOL_CONS_TEXT_USER_ALLY;
}
else
{
return WZCOL_CONS_TEXT_USER_ENEMY;
}
}
// Friend-foe is off
return WZCOL_TEXT_BRIGHT;
}
}
static void console_drawtext(WzText &display, PIELIGHT colour, int x, int y, CONSOLE_TEXT_JUSTIFICATION justify, int width)
{
switch (justify)
{
case LEFT_JUSTIFY:
break; // do nothing
case RIGHT_JUSTIFY:
x = x + width - display.width();
break;
case CENTRE_JUSTIFY:
x = x + (width - display.width()) / 2;
break;
}
display.render(x, y, colour);
}
// Show global (mode=true) or team (mode=false) history messages
void displayOldMessages(bool mode)
{
int startpos = 0;
std::deque<CONSOLE_MESSAGE> *WhichMessages;
if (mode)
{
WhichMessages = &TeamMessages;
}
else
{
WhichMessages = &HistoryMessages;
}
if (!WhichMessages->empty())
{
unsigned int count = WhichMessages->size(); // total number of messages
if (count > NumDisplayLines) // if we have more than we can display
{
startpos = count - NumDisplayLines; // show last X lines
startpos += updatepos; // unless user wants to start at something else
if (startpos < 0) // don't underflow
{
startpos = 0;
updatepos = (count - NumDisplayLines) * -1; // reset back to first entry
count = NumDisplayLines;
}
else if (count + updatepos <= count)
{
count += updatepos; // user may want something different
}
else
{
// reset all, we got overflow
count = WhichMessages->size();
updatepos = 0;
startpos = count - NumDisplayLines;
}
}
int nudgeright = 0;
int TextYpos = historyConsole.topY + linePitch - 2;
if (isSecondaryWindowUp()) // see if (build/research/...)window is up
{
nudgeright = RET_FORMWIDTH + 2; // move text over
}
// if user wants to add a bit more contrast to the text
if (showBackgroundColor)
{
iV_TransBoxFill(historyConsole.topX + nudgeright - CON_BORDER_WIDTH, historyConsole.topY - historyConsole.textDepth - CON_BORDER_HEIGHT,
historyConsole.topX + historyConsole.width, historyConsole.topY + (NumDisplayLines * linePitch) + CON_BORDER_HEIGHT);
}
for (int i = startpos; i < count; ++i)
{
PIELIGHT colour = mode ? WZCOL_CONS_TEXT_USER_ALLY : getConsoleTextColor((*WhichMessages)[i].player);
console_drawtext((*WhichMessages)[i].display, colour, historyConsole.topX + nudgeright, TextYpos, (*WhichMessages)[i].JustifyType, historyConsole.width);
TextYpos += (*WhichMessages)[i].display.lineSize();
}
}
}
/** Displays all the console messages */
void displayConsoleMessages()
{
// Check if we have any messages we want to show
if (!getNumberConsoleMessages() && !bConsoleDropped && InfoMessages.empty())
{
return;
}
// scripts can disable the console
if (!bConsoleDisplayEnabled && InfoMessages.empty())
{
return;
}
pie_SetDepthBufferStatus(DEPTH_CMP_ALWAYS_WRT_ON);
pie_SetFogStatus(false);
if (bConsoleDropped)
{
displayOldMessages(HistoryMode);
}
if (!InfoMessages.empty())
{
auto i = InfoMessages.end() - 1; // we can only show the last one...
int tmp = pie_GetVideoBufferWidth();
drawBlueBox(0, 0,tmp, 18);
tmp -= i->display.width();
console_drawtext(i->display, getConsoleTextColor(i->player), tmp - 6, linePitch - 2, i->JustifyType, i->display.width());
}
int TextYpos = mainConsole.topY;
// Draw the blue background for the text (only in game, not lobby)
if (bTextBoxActive && GetGameMode() == GS_NORMAL)
{
iV_TransBoxFill(mainConsole.topX - CON_BORDER_WIDTH, mainConsole.topY - mainConsole.textDepth - CON_BORDER_HEIGHT,
mainConsole.topX + mainConsole.width, mainConsole.topY + (getNumberConsoleMessages() * linePitch) + CON_BORDER_HEIGHT - linePitch);
}
for (auto & ActiveMessage : ActiveMessages)
{
console_drawtext(ActiveMessage.display, getConsoleTextColor(ActiveMessage.player), mainConsole.topX, TextYpos, ActiveMessage.JustifyType, mainConsole.width);
TextYpos += ActiveMessage.display.lineSize();
}
}
/** destroy CONPRINTF messages **/
void clearInfoMessages()
{
InfoMessages.clear();
}
/** Allows toggling of the box under the console text */
void setConsoleBackdropStatus(bool state)
{
bTextBoxActive = state;
}
/**
Turns on and off display of console. It's worth
noting that this is just the display so if you want
to make sure that when it's turned back on again, there
are no messages, the call flushConsoleMessages first.
*/
void enableConsoleDisplay(bool state)
{
bConsoleDisplayEnabled = state;
}
/** Allows positioning of the console on screen */
void setConsoleSizePos(UDWORD x, UDWORD y, UDWORD width)
{
mainConsole.topX = x;
mainConsole.topY = y;
mainConsole.width = width;
/* Should be done below */
mainConsole.textDepth = 8;
// Do not call flushConsoleMessages() when simply changing console size/position -
// it is possible for the console size/pos to change during display.
}
/** Establishes whether the console messages stay there */
void setConsolePermanence(bool state, bool bClearOld)
{
if (mainConsole.permanent == true && state == false)
{
if (bClearOld)
{
flushConsoleMessages();
}
mainConsole.permanent = false;
}
else
{
if (bClearOld)
{
flushConsoleMessages();
}
mainConsole.permanent = state;
}
}
/** Check if mouse is over the Active console 'window' area */
bool mouseOverConsoleBox()
{
int gotMessages = getNumberConsoleMessages();
if (gotMessages &&
((UDWORD)mouseX() > mainConsole.topX)
&& ((UDWORD)mouseY() > mainConsole.topY)
&& ((UDWORD)mouseX() < mainConsole.topX + mainConsole.width)
&& ((UDWORD)mouseY() < (mainConsole.topY + 4 + linePitch * gotMessages)))
{
return true;
}
return false;
}
/** Check if mouse is over the History console 'window' area */
bool mouseOverHistoryConsoleBox()
{
int nudgeright = 0;
if (isSecondaryWindowUp())
{ // if a build/research/... is up, we need to move text over by this much
nudgeright = RET_FORMWIDTH;
}
// enable below to see the hitbox of the history console window
#if 0
if (isSecondaryWindowUp())
{
iV_Box2(historyConsole.topX + nudgeright, historyConsole.topY, historyConsole.topX + historyConsole.width, (historyConsole.topY + 4 + linePitch * NumDisplayLines), WZCOL_RED, WZCOL_GREEN);
}
else
{
iV_Box2(historyConsole.topX, historyConsole.topY, historyConsole.topX + historyConsole.width, (historyConsole.topY + 4 + linePitch * NumDisplayLines), WZCOL_GREY, WZCOL_GREY);
}
#endif
// check to see if mouse is in the area when console is enabled
if (bConsoleDropped &&
((UDWORD)mouseX() > historyConsole.topX + nudgeright)
&& ((UDWORD)mouseY() > historyConsole.topY)
&& ((UDWORD)mouseX() < historyConsole.topX + historyConsole.width)
&& ((UDWORD)mouseY() < (historyConsole.topY + 4 + linePitch * NumDisplayLines)))
{
if (mousePressed(MOUSE_WUP))
{
updatepos--;
}
else if (mousePressed(MOUSE_WDN))
{
updatepos++;
}
if (keyDown(KEY_LCTRL))
{
showBackgroundColor = true;
}
else
{
showBackgroundColor = false;
}
return (true);
}
else
{
return (false);
}
}
/** Sets up how many lines are allowed and how many are visible */
void setConsoleLineInfo(UDWORD vis)
{
ASSERT(vis <= MAX_CONSOLE_MESSAGES, "Request for more visible lines in the console than exist");
consoleVisibleLines = vis;
}
/** get how many lines are allowed and how many are visible */
UDWORD getConsoleLineInfo()
{
return consoleVisibleLines;
}
/// Set if new messages may be added to the console
void permitNewConsoleMessages(bool allow)
{
allowNewMessages = allow;
}
/// \return the visibility of the console
bool getConsoleDisplayStatus()
{
return (bConsoleDisplayEnabled);
}
/** Use console() for when you want to display a console message,
and keep it in the history logs.
Use the macro CONPRINTF if you don't want it to be in the history logs.
**/
void console(const char *pFormat, ...)
{
char aBuffer[500]; // Output string buffer
va_list pArgs; // Format arguments
/* Print out the string */
va_start(pArgs, pFormat);
vsnprintf(aBuffer, sizeof(aBuffer), pFormat, pArgs);
va_end(pArgs);
/* Output it */
addConsoleMessage(aBuffer, DEFAULT_JUSTIFY, SYSTEM_MESSAGE);
}