forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.cpp
805 lines (677 loc) · 18.7 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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 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
*/
/** \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 "ai.h"
#include "console.h"
#include "main.h"
#include "radar.h"
#include <string>
#include <istream>
/* Alex McLean, Pumpkin Studios, EIDOS Interactive */
#define DEFAULT_MESSAGE_DURATION GAME_TICKS_PER_SEC * 8
#define CON_BORDER_WIDTH 4
#define CON_BORDER_HEIGHT 4
struct CONSOLE
{
UDWORD topX;
UDWORD topY;
UDWORD width;
UDWORD textDepth;
bool permanent;
};
/* Definition of a message */
struct CONSOLE_MESSAGE
{
char text[MAX_CONSOLE_STRING_LENGTH]; // Text of the message
UDWORD timeAdded; // When was it added to our list?
UDWORD JustifyType;
UDWORD id;
SDWORD player; // Player who sent this message or SYSTEM_MESSAGE
CONSOLE_MESSAGE * psNext;
};
/** Is the console history on or off? */
static bool bConsoleDropped = false;
/** Stores the console dimensions and states */
static CONSOLE mainConsole;
/** Static storage for the maximum possible number of console messages */
static CONSOLE_MESSAGE consoleStorage[MAX_CONSOLE_MESSAGES];
/** Maximum drop */
#define MAX_DROP 32
static UDWORD history[MAX_DROP];
/** Pointer to linked list of active messages - points to elements of the array history */
static CONSOLE_MESSAGE *consoleMessages;
/** Where in the array are we - it's cyclic */
static UDWORD messageIndex;
/** How many lines in the console history */
static UDWORD consoleDrop = MAX_DROP;
static UDWORD maxDrop;
#define DROP_DROPPING 1
#define DROP_CLOSING 2
#define DROP_STATIC 3
#define DROP_CLOSED 4
#define DROP_STEP_INTERVAL (15)
/** Console history state */
static UDWORD dropState;
/** How many messages are presently active? */
static UDWORD numActiveMessages;
/** How long do messages last for? */
static UDWORD messageDuration;
static UDWORD lastDropChange = 0;
/** Is there a box under the console text? */
static bool bTextBoxActive;
/** Is the console being displayed? */
static bool bConsoleDisplayEnabled;
/** How many lines are displayed? */
static UDWORD consoleVisibleLines;
/** Whether new messages are allowed to be added */
static int allowNewMessages;
/** What's the default justification? */
static CONSOLE_TEXT_JUSTIFICATION defJustification;
static UDWORD messageId; // unique ID
/// Global string for new console messages.
char ConsoleString[MAX_CONSOLE_TMP_STRING_LENGTH];
/**
Specify how long messages will stay on screen.
*/
static void setConsoleMessageDuration(UDWORD time)
{
messageDuration = time;
}
/** Sets the system up */
void initConsoleMessages( void )
{
int TextLineSize = iV_GetTextLineSize();
messageIndex = 0;
/* Console can extend to half screen height */
if (TextLineSize)
{
maxDrop = ((pie_GetVideoBufferHeight() / TextLineSize)/2);
}
else
{
debug(LOG_FATAL, "Something is wrong with the fonts? Aborting.");
abort();
}
if(maxDrop>32) maxDrop = 32;
consoleDrop = maxDrop;//MAX_DROP;
dropState = DROP_CLOSED;
/* No active messages to begin with */
numActiveMessages = 0;
lastDropChange = 0;
bConsoleDropped = false;
/* Linked list is empty */
consoleMessages = NULL;
/* Setup how long messages are displayed for... */
setConsoleMessageDuration(DEFAULT_MESSAGE_DURATION);
/* No box under the text */
setConsoleBackdropStatus(true);
/* Turn on the console display */
enableConsoleDisplay(true);
/* Set left justification as default */
setDefaultConsoleJust(LEFT_JUSTIFY);
/* Set up the console size and postion
x,y,width */
setConsoleSizePos(16, 16, pie_GetVideoBufferWidth()-32);
setConsoleLineInfo(MAX_CONSOLE_MESSAGES/4 + 4);
/* We're not initially having permanent messages */
setConsolePermanence(false,true);
/* Allow new messages */
permitNewConsoleMessages(true);
}
/** Open the console when it's closed and close it when it's open. */
void toggleConsoleDrop( void )
{
/* If it's closed ... */
if(bConsoleDropped == false)
{
dropState = DROP_DROPPING;
consoleDrop = 0;
bConsoleDropped = true;
audio_PlayTrack(ID_SOUND_WINDOWOPEN);
}
else
{
/* It's already open (or opening) */
dropState = DROP_CLOSING;
audio_PlayTrack(ID_SOUND_WINDOWCLOSE);
}
}
/** Add a string to the console. */
bool addConsoleMessage(const char *Text, CONSOLE_TEXT_JUSTIFICATION jusType, SDWORD player)
{
int textLength;
CONSOLE_MESSAGE *psMessage;
/* Just don't add it if there's too many already */
if(numActiveMessages>=MAX_CONSOLE_MESSAGES-1)
{
return false;
}
/* Don't allow it to be added if we've disabled adding of new messages */
if(!allowNewMessages)
{
return false ;
}
std::istringstream stream(Text);
std::string lines;
char messageText[MAX_CONSOLE_STRING_LENGTH];
while(std::getline(stream, lines))
{
sstrcpy(messageText, lines.c_str());
/* Is the string too long? */
textLength = strlen(messageText);
// FIXME: We split the text correctly in the display routine, however, then the console has no idea it was split,
// so that is why you sometimes see text that is below the console line limit that was set via setConsoleLineInfo()
// As in the display routines, we should check to see if iV_GetTextWidth(lines.c_str()) > mainConsole.width, but that
// isn't working the way it should, so right now, we just throw a warning (only in debug mode) that the text it too long.
if (textLength > 72)
{
debug(LOG_NEVER, "This line is too long, and will be split in the display routines! Line is:[%s]", lines.c_str());
}
ASSERT( textLength < MAX_CONSOLE_STRING_LENGTH, "Attempt to add a message to the console that exceeds MAX_CONSOLE_STRING_LENGTH" );
/* Are we using a defualt justification? */
if(jusType == DEFAULT_JUSTIFY)
{
/* Then set it */
jusType = defJustification;
}
debug(LOG_CONSOLE, "(to player %d): %s", (int)player, messageText);
consoleStorage[messageIndex].player = player;
/* Precalculate and store (quicker!) the indent for justified text */
switch(jusType)
{
/* Allign to left edge of screen */
case LEFT_JUSTIFY:
consoleStorage[messageIndex].JustifyType = FTEXT_LEFTJUSTIFY;
break;
/* Allign to right edge of screen */
case RIGHT_JUSTIFY:
consoleStorage[messageIndex].JustifyType = FTEXT_RIGHTJUSTIFY;
break;
/* Allign to centre of the screen,NOT TO CENTRE OF CONSOLE!!!!!! */
case CENTRE_JUSTIFY:
consoleStorage[messageIndex].JustifyType = FTEXT_CENTRE;
break;
/* Gone tits up by the looks of it */
default:
debug( LOG_FATAL, "Weirdy type of text justification for console print" );
abort();
break;
}
/* Copy over the text of the message */
sstrcpy(consoleStorage[messageIndex].text, messageText);
/* Set the time when it was added - this might not be needed */
consoleStorage[messageIndex].timeAdded = gameTime2;
/* This is the present newest message */
consoleStorage[messageIndex].psNext = NULL;
consoleStorage[messageIndex].id = 0;
/* Are there no messages? */
if(consoleMessages == NULL)
{
consoleMessages = &consoleStorage[messageIndex];
}
else
{
/* Get to the last element in our message list */
for(psMessage = consoleMessages; psMessage->psNext; psMessage = psMessage->psNext)
{
/* NOP */
;
}
/* Add it to the end */
psMessage->psNext = &consoleStorage[messageIndex];
}
/* Move on in our array */
if(messageIndex++ >= MAX_CONSOLE_MESSAGES-1)
{
/* Reset */
messageIndex = 0;
}
/* There's one more active console message */
numActiveMessages++;
}
return true;
}
/// \return The number of console messages currently active
UDWORD getNumberConsoleMessages( void )
{
return(numActiveMessages);
}
/** Update the console messages.
This function will remove messages that are overdue.
*/
void updateConsoleMessages( void )
{
if(dropState == DROP_DROPPING)
{
if(gameTime - lastDropChange > DROP_STEP_INTERVAL)
{
lastDropChange = gameTime;
if(++consoleDrop > maxDrop)
{
consoleDrop = maxDrop;
dropState = DROP_STATIC;
}
}
}
else if (dropState == DROP_CLOSING)
{
if(gameTime - lastDropChange > DROP_STEP_INTERVAL)
{
lastDropChange = gameTime;
if(consoleDrop)
{
consoleDrop--;
}
else
{
dropState = DROP_CLOSED;
bConsoleDropped = false;
}
}
}
/* Don't do anything for DROP_STATIC */
/* If there are no messages or we're on permanent then exit */
if(consoleMessages == NULL || mainConsole.permanent)
{
return;
}
/* Time to kill the top one ?*/
if(gameTime2 - consoleMessages->timeAdded > messageDuration)
{
consoleMessages->id = messageId++;
/* Is this the only message? */
if(consoleMessages->psNext == NULL)
{
/* Then list is now empty */
consoleMessages = NULL;
}
else
{
/* Otherwise point it at the next one */
consoleMessages = consoleMessages->psNext;
}
/* There's one less active console message */
numActiveMessages--;
}
}
/**
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( void )
{
/* No point unless there is at least one */
if(consoleMessages!=NULL)
{
/* Is this the only message? */
if(consoleMessages->psNext == NULL)
{
/* Then list is now empty */
consoleMessages = NULL;
}
else
{
/* Otherwise point it at the next one */
consoleMessages = consoleMessages->psNext;
}
/* There's one less active console message */
numActiveMessages--;
}
}
/** Clears all console messages */
void flushConsoleMessages( void )
{
consoleMessages = NULL;
numActiveMessages = 0;
messageId = 0;
}
/** Sets console text color depending on message type */
static void setConsoleTextColor(SDWORD player)
{
// System messages
if(player == SYSTEM_MESSAGE)
{
iV_SetTextColour(WZCOL_CONS_TEXT_SYSTEM);
}
else if (player == NOTIFY_MESSAGE)
{
iV_SetTextColour(WZCOL_YELLOW);
}
else
{
// Don't use friend-foe colors in the lobby
if(bEnemyAllyRadarColor && (GetGameMode() == GS_NORMAL))
{
if(aiCheckAlliances(player,selectedPlayer))
{
iV_SetTextColour(WZCOL_CONS_TEXT_USER_ALLY);
}
else
{
iV_SetTextColour(WZCOL_CONS_TEXT_USER_ENEMY);
}
}
else
{
// Friend-foe is off
iV_SetTextColour(WZCOL_CONS_TEXT_USER);
}
}
}
/** Display up to the last 8 messages.
\return The number of messages actually shown */
static int displayOldMessages(void)
{
int i;
bool bGotIt;
bool bQuit;
int marker = 0;
int linePitch;
int MesY;
unsigned int count = 0;
/* Check there actually are any messages */
int thisIndex = messageId;
if(thisIndex)
{
bQuit = false;
while(!bQuit)
{
for(i=0,bGotIt = false; i<MAX_CONSOLE_MESSAGES && !bGotIt; i++)
{
if (consoleStorage[i].id == thisIndex-1)
{
bGotIt = true;
marker = i;
}
}
/* We found an older one */
if(bGotIt)
{
history[count++] = marker;
}
else
{
bQuit = true; // count holds how many we got
}
if(thisIndex)
{
/* Look for an older one */
thisIndex--;
}
else
{
bQuit = true; // We've reached the big bang - there is nothing older...
}
/* History can only hold so many */
if(count>=consoleDrop)
{
bQuit = true;
}
}
}
if(!count)
{
/* there are messages - just no old ones yet */
return(0);
}
if(count)
{
/* Get the line pitch */
linePitch = iV_GetTextLineSize();
/* How big a box is necessary? */
/* GET RID OF THE MAGIC NUMBERS BELOW */
iV_TransBoxFill(mainConsole.topX - CON_BORDER_WIDTH,mainConsole.topY-mainConsole.textDepth-CON_BORDER_HEIGHT,
mainConsole.topX+mainConsole.width ,mainConsole.topY+((count)*linePitch)+CON_BORDER_HEIGHT-linePitch);
}
/*
if(count)
{
sprintf(buildData,"%s,%s",__TIME__,__DATE__);
buildWidth = iV_GetTextWidth(buildData);
iV_DrawText(buildData,((mainConsole.topX+mainConsole.width) - buildWidth - 16),
mainConsole.topY);
}
*/
MesY = mainConsole.topY;
/* Render what we found */
for(i=count-1; i>0; i--)
{
/* Set text color depending on message type */
setConsoleTextColor(consoleStorage[history[i]].player);
/* Draw the text string */
MesY = iV_DrawFormattedText(consoleStorage[history[i]].text,
mainConsole.topX,
MesY,
mainConsole.width,
consoleStorage[history[i]].JustifyType);
}
/* Set text color depending on message type */
setConsoleTextColor(consoleStorage[history[0]].player);
/* Draw the top one */
iV_DrawFormattedText(consoleStorage[history[0]].text,
mainConsole.topX,
MesY,
mainConsole.width,
consoleStorage[history[0]].JustifyType);
/* Return how much to drop the existing console by... Fix this for lines>screenWIDTH */
if(count)
{
return((count)*linePitch);
}
else
{
return(0);
}
}
/** Displays all the console messages */
void displayConsoleMessages( void )
{
CONSOLE_MESSAGE *psMessage;
int linePitch;
int boxDepth;
int drop;
int MesY;
int clipDepth;
unsigned int exceed, numProcessed;
/* Are there any to display? */
if(consoleMessages == NULL && !bConsoleDropped)
{
/* No point - so get out */
return;
}
/* Return if it's disabled */
if(!bConsoleDisplayEnabled)
{
return;
}
iV_SetFont(font_regular);
/* Get the travel to the next line */
linePitch = iV_GetTextLineSize();
pie_SetDepthBufferStatus(DEPTH_CMP_ALWAYS_WRT_ON);
pie_SetFogStatus(false);
drop = 0;
if(bConsoleDropped)
{
drop = displayOldMessages();
}
if(consoleMessages==NULL)
{
return;
}
/* Do we want a box under it? */
if(bTextBoxActive)
{
for (psMessage = consoleMessages, exceed = 0;
psMessage && consoleVisibleLines > 0 && exceed < 4; // ho ho ho!!!
psMessage = psMessage->psNext)
{
if (iV_GetTextWidth(psMessage->text) > mainConsole.width)
{
++exceed;
}
}
/* How big a box is necessary? */
boxDepth = (numActiveMessages> consoleVisibleLines ? consoleVisibleLines-1 : numActiveMessages-1);
/* Add on the extra - hope it doesn't exceed two lines! */
boxDepth += exceed;
/* GET RID OF THE MAGIC NUMBERS BELOW */
clipDepth = (mainConsole.topY+(boxDepth*linePitch)+CON_BORDER_HEIGHT+drop);
if(clipDepth > pie_GetVideoBufferHeight() - linePitch)
{
clipDepth = (pie_GetVideoBufferHeight() - linePitch);
}
iV_TransBoxFill(mainConsole.topX - CON_BORDER_WIDTH,mainConsole.topY-mainConsole.textDepth-CON_BORDER_HEIGHT+drop+1,
mainConsole.topX+mainConsole.width ,clipDepth);
}
/* Stop when we've drawn enough or we're at the end */
MesY = mainConsole.topY + drop;
for (psMessage = consoleMessages, numProcessed = 0;
psMessage && numProcessed < consoleVisibleLines && MesY < pie_GetVideoBufferHeight() - linePitch;
psMessage = psMessage->psNext)
{
/* Set text color depending on message type */
setConsoleTextColor(psMessage->player);
/* Draw the text string */
MesY = iV_DrawFormattedText(psMessage->text, mainConsole.topX, MesY,
mainConsole.width, psMessage->JustifyType);
/* Move on */
++numProcessed;
}
}
/** 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;
}
/** Sets the default justification for text */
void setDefaultConsoleJust(CONSOLE_TEXT_JUSTIFICATION defJ)
{
switch(defJ)
{
case LEFT_JUSTIFY:
case RIGHT_JUSTIFY:
case CENTRE_JUSTIFY:
defJustification = defJ;
break;
default:
debug( LOG_FATAL, "Weird default text justification for console" );
abort();
break;
}
}
/** 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;
flushConsoleMessages();
}
/** 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;
}
}
/** true or false as to whether the mouse is presently over the console window */
bool mouseOverConsoleBox( void )
{
if (
((UDWORD)mouseX() > mainConsole.topX) // condition 1
&& ((UDWORD)mouseY() > mainConsole.topY) // condition 2
&& ((UDWORD)mouseX() < mainConsole.topX + mainConsole.width) //condition 3
&& ((UDWORD)mouseY() < (mainConsole.topY + iV_GetTextLineSize()*numActiveMessages)) //condition 4
)
{
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(void)
{
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( void )
{
return(bConsoleDisplayEnabled);
}
/** like debug_console, but for release */
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);
}