forked from pngwen/xboing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscore.c
1109 lines (933 loc) · 28.3 KB
/
highscore.c
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file highscore.c
* @author Group 1
* @date 2024-10-08
* @brief
*
* XBoing - An X11 blockout style computer game
*
* (c) Copyright 1993, 1994, 1995, Justin C. Kibell, All Rights Reserved
*
* The X Consortium, and any party obtaining a copy of these files from
* the X Consortium, directly or indirectly, is granted, free of charge, a
* full and unrestricted irrevocable, world-wide, paid up, royalty-free,
* nonexclusive right and license to deal in this software and
* documentation files (the "Software"), including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons who receive
* copies from any such party to do so. This license includes without
* limitation a license to do the foregoing actions under any patents of
* the party supplying this software to the X Consortium.
*
* In no event shall the author be liable to any party for direct, indirect,
* special, incidental, or consequential damages arising out of the use of
* this software and its documentation, even if the author has been advised
* of the possibility of such damage.
*
* The author specifically disclaims any warranties, including, but not limited
* to, the implied warranties of merchantability and fitness for a particular
* purpose. The software provided hereunder is on an "AS IS" basis, and the
* author has no obligation to provide maintenance, support, updates,
* enhancements, or modifications.
*
* =========================================================================
*
* $Id: highscore.c,v 1.1.1.1 1994/12/16 01:36:46 jck Exp $
* $Source: /usr5/legends/jck/xb/master/xboing/highscore.c,v $
* $Revision: 1.1.1.1 $
* $Date: 1994/12/16 01:36:46 $
*
* $Log: highscore.c,v $
* Revision 1.1.1.1 1994/12/16 01:36:46 jck
* The XBoing distribution requires configuration management. This is why the
* cvs utility is being used. This is the initial import of all source etc..
*
*
* =========================================================================
*/
/*
* Include file dependencies:
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include <file.h>
#include <sys/param.h>
//#include <X11/Xlib.h>
//#include <X11/Xutil.h>
//#include <X11/Xos.h>
#include <netinet/in.h>
//#include <xpm.h>
#include "error.h"
#include "misc.h"
#include "main.h"
#include "preview.h"
#include "audio.h"
#include "special.h"
#include "init.h"
#include "inst.h"
#include "stage.h"
#include "blocks.h"
#include "ball.h"
#include "sfx.h"
#include "score.h"
#include "paddle.h"
#include "level.h"
#include "mess.h"
#include "intro.h"
#include "presents.h"
#include "bitmaps/highscr.xpm"
#include "highscore.h"
/*
* Internal macro definitions:
*/
#define GAP 13
#define NUM_HIGHSCORES 10
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
/* My locking defines only */
#define LOCK_FILE 0
#define UNLOCK_FILE 1
/* System locking defines */
#ifndef NO_LOCKING
#ifndef LOCK_EX
#define LOCK_EX F_LOCK
#endif
#ifndef LOCK_UN
#define LOCK_UN F_ULOCK
#endif
#endif /* NO_LOCKING */
/*
* Internal type declarations:
*/
static void SetHighScoreWait(enum HighScoreStates newMode, int waitFrame);
static void InitialiseHighScores(void);
static void SortHighScores(void);
static void DeleteScore(int i);
static int LockUnlock(int cmd);
/*
* Internal variable declarations:
*/
static int nextFrame = 0;
static int endFrame = 0;
enum HighScoreStates HighScoreState;
static Pixmap titlePixmap, titlePixmapM;
static int waitingFrame;
enum HighScoreStates waitMode;
static int sparkley = 0;
static int sindex = 0;
static int si = 0;
static int scoreType = GLOBAL;
static char nickName[22];
highScoreEntry highScores[NUM_HIGHSCORES];
highScoreHeader scoresHeader;
void SetNickName(char *nick)
{
/* Change the users nick name */
strncpy(nickName, nick, 20);
}
void SetBoingMasterText(char *message)
{
/* Change the boing masters text */
strcpy(scoresHeader.masterText, message);
}
char *GetNickName(void)
{
/* Return the nickname or NULL */
if (nickName[0] == '\0')
return NULL;
else
return nickName;
}
void SetUpHighScore(Display *display, Window window, Colormap colormap)
{
XpmAttributes attributes;
int XpmErrorStatus;
attributes.valuemask = XpmColormap;
attributes.colormap = colormap;
/* Load the highscore title pixmap */
XpmErrorStatus = XpmCreatePixmapFromData(display, window, highscores_xpm,
&titlePixmap, &titlePixmapM, &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseHighScore()");
/* Free the xpm pixmap attributes */
XpmFreeAttributes(&attributes);
/* Setup the high score table */
InitialiseHighScores();
ResetHighScore(GLOBAL);
}
static void DoTitle(Display *display, Window window)
{
char string[80];
DrawStageBackground(display, window, BACKGROUND_SPACE, True);
/* Draw the highscore title */
RenderShape(display, window, titlePixmap, titlePixmapM,
59, 20, 377, 37, False);
RenderShape(display, window, earthPixmap, earthPixmapM,
PLAY_WIDTH / 2 - 200, PLAY_HEIGHT / 2 - 160, 400, 400, False);
/* Let the dudes know how to start the game */
strcpy(string, "Insert coin to start the game");
DrawShadowCentredText(display, window, textFont,
string, PLAY_HEIGHT - 40, tann, PLAY_WIDTH);
/* Set the message window to have the other display toggle key */
if (scoreType == GLOBAL)
SetCurrentMessage(display, messWindow,
"<H> - Personal Best", False);
else
SetCurrentMessage(display, messWindow,
"<h> - Roll of Honour", False);
SetHighScoreWait(HIGHSCORE_SHOW, frame + 10);
}
static void DoHighScores(Display *display, Window window)
{
int i, len, plen;
int xr = 30;
int ym = 75;
int xs = xr + 30;
int xl = xs + 75;
int xt = xl + 40;
int xg = xt + 65;
int xn = xg + 95;
int y = 200;
char string[80];
char string2[80];
char *p;
time_t theTime;
/* Read the high score table */
if (ReadHighScoreTable(GLOBAL) == False)
InitialiseHighScores();
/* Draw the boing master at top of the list */
strcpy(string, "Boing Master");
DrawShadowCentredText(display, window, textFont,
string, ym, red, PLAY_WIDTH);
ym += textFont->ascent + GAP;
/* Render the boing master's name */
strcpy(string, highScores[0].name);
DrawShadowCentredText(display, window, titleFont,
string, ym, yellow, PLAY_WIDTH);
ym += textFont->ascent + GAP;
/* Render the boing master's words of wisdom */
strcpy(string, scoresHeader.masterText);
DrawShadowCentredText(display, window, dataFont,
string, ym, green, PLAY_WIDTH);
ym += textFont->ascent + GAP * 2;
/* Read the high score table */
if (ReadHighScoreTable(scoreType) == False)
InitialiseHighScores();
/* Explain that this is the roll of honour */
if (scoreType == GLOBAL)
strcpy(string, "- The Roll of Honour -");
else
strcpy(string, "- Personal Best -");
DrawShadowCentredText(display, window, textFont,
string, ym, red, PLAY_WIDTH);
ym += textFont->ascent + GAP;
/* Draw the titles for the highscore table */
strcpy(string, "#");
len = strlen(string);
DrawText(display, window, xr+2, y+2, textFont, black, string, len);
DrawText(display, window, xr, y, textFont, yellow, string, len);
strcpy(string, "Score");
len = strlen(string);
DrawText(display, window, xs+2, y+2, textFont, black, string, len);
DrawText(display, window, xs, y, textFont, yellow, string, len);
strcpy(string, "L");
len = strlen(string);
DrawText(display, window, xl+2, y+2, textFont, black, string, len);
DrawText(display, window, xl, y, textFont, yellow, string, len);
strcpy(string, "Time");
len = strlen(string);
DrawText(display, window, xt+2, y+2, textFont, black, string, len);
DrawText(display, window, xt, y, textFont, yellow, string, len);
strcpy(string, "Date");
len = strlen(string);
DrawText(display, window, xg+2, y+2, textFont, black, string, len);
DrawText(display, window, xg, y, textFont, yellow, string, len);
strcpy(string, "Player");
len = strlen(string);
DrawText(display, window, xn+2, y+2, textFont, black, string, len);
DrawText(display, window, xn, y, textFont, yellow, string, len);
y += textFont->ascent + GAP / 2;
/* Draw the line above the table */
DrawLine(display, window, 22, y+2, PLAY_WIDTH - 18, y+2, black, 3);
DrawLine(display, window, 20, y, PLAY_WIDTH - 20, y, white, 3);
y += textFont->ascent;
/* Draw the scores into the table */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
if (ntohl(highScores[i].score) > (u_long) 0)
{
/* Draw the rank */
sprintf(string, "%d", i+1);
if (ntohl(highScores[i].score) != score)
DrawShadowText(display, window, textFont, string, xr, y, tann);
else
DrawShadowText(display, window, textFont, string, xr, y, green);
/* Draw the score */
sprintf(string, "%ld", ntohl(highScores[i].score));
if (ntohl(highScores[i].score) != score)
DrawShadowText(display, window, textFont, string, xs, y, red);
else
DrawShadowText(display, window, textFont, string, xs, y, green);
/* Write out the level reached */
sprintf(string, "%ld", ntohl(highScores[i].level));
DrawShadowText(display, window, textFont, string, xl, y, green);
/* Game duration in minutes and seconds */
sprintf(string, "%ld'%ld'%ld\"",
ntohl(highScores[i].gameTime) / 3600,
ntohl(highScores[i].gameTime) / 60,
ntohl(highScores[i].gameTime) % 60);
if (ntohl(highScores[i].score) != score)
DrawShadowText(display, window, textFont, string, xt, y, tann);
else
DrawShadowText(display, window, textFont, string, xt, y, green);
/* Construct the date for the highscore entry */
theTime = (time_t) ntohl(highScores[i].time);
strftime(string, 10, "%d %b %y", localtime(&theTime));
string[9] = '\0'; /* Just to be sure */
if (ntohl(highScores[i].score) != score)
DrawShadowText(display, window, textFont, string, xg, y, white);
else
DrawShadowText(display, window, textFont, string, xg, y, green);
/* Name of the boing master */
strcpy(string, highScores[i].name);
plen = XTextWidth(textFont, string, len);
/* Only use the first name if too big for screen */
if ((plen + xn) > PLAY_WIDTH)
{
/* Find the first space and null terminate there */
p = strchr(string, ' ');
*p = '\0';
strcpy(string2, string);
/* Draw a much smaller version of your name */
if (ntohl(highScores[i].score) != score)
DrawShadowText(display, window, textFont, string2, xn, y,
yellow);
else
DrawShadowText(display, window, textFont, string2, xn, y,
green);
}
else
{
/* Write out users name */
if (ntohl(highScores[i].score) != score)
DrawShadowText(display, window, textFont, string, xn, y,
yellow);
else
DrawShadowText(display, window, textFont, string, xn, y,
green);
}
}
else
{
/* This bit is for when the table entry is blank */
sprintf(string, "%d", i+1);
DrawShadowText(display, window, textFont, string, xr, y, tann);
/* Draw dashes for blank entries */
strcpy(string, "--");
DrawShadowText(display, window, textFont, string, xs, y, red);
DrawShadowText(display, window, textFont, string, xl, y, green);
DrawShadowText(display, window, textFont, string, xt, y, tann);
DrawShadowText(display, window, textFont, string, xg, y, white);
DrawShadowText(display, window, textFont, string, xn, y, yellow);
}
y += textFont->ascent + GAP;
}
/* Draw the line above the table */
DrawLine(display, window, 22, y+2, PLAY_WIDTH - 18, y+2, black, 3);
DrawLine(display, window, 20, y, PLAY_WIDTH - 20, y, white, 3);
SetHighScoreWait(HIGHSCORE_SPARKLE, frame + 2);
}
static void DoTitleSparkle(Display *display, Window window)
{
static int sindex = 0;
static int delay = 30;
if ((frame % delay) == 0)
{
if (delay == 800) delay = 30;
/* Draw stars either side of high scores title */
RenderShape(display, window, stars[sindex], starsM[sindex],
25, 27, 20, 20, True);
RenderShape(display, window, stars[10-sindex], starsM[10-sindex],
PLAY_WIDTH-45, 27, 20, 20, True);
sindex++;
if (sindex == 11)
{
/* Clear away the stars */
XClearArea(display, window, 25, 27, 20, 20, False);
XClearArea(display, window, PLAY_WIDTH-45, 27, 20, 20, False);
sindex = 0;
if (delay == 30) delay = 800;
}
}
}
static void DoSparkle(Display *display, Window window)
{
static Pixmap store; /* backing store for the sparkle */
static int x = 6; /* X position of the sparkle */
if (!store)
{
/* Create some backing store for the sparkle star */
store = XCreatePixmap(display, window, 20, 20,
DefaultDepth(display, XDefaultScreen(display)));
}
if (frame == endFrame)
{
/* Bug out of the sparkle and goto next sequence */
si = 0;
sindex = 0;
sparkley = 200 + textFont->ascent + 20;
/* End the sparkle and now set up for finish */
SetHighScoreWait(HIGHSCORE_FINISH, frame + 1);
return;
}
if (sindex == 0)
XCopyArea(display, window, store, gc, x, sparkley, 20, 20, 0, 0);
if (frame == nextFrame)
{
/* Draw the sparkle frame */
RenderShape(display, window, stars[sindex], starsM[sindex],
x, sparkley, 20, 20, True);
sindex++;
nextFrame = frame + 30;
/* Last frame of sparkle so reset */
if (sindex == 11)
{
XCopyArea(display, store, window, gc, 0, 0, 20, 20, x, sparkley);
sindex = 0;
nextFrame = frame + 100;
sparkley += textFont->ascent + GAP;
si++;
if ((ntohl(highScores[si].score) <= (u_long) 0) || (si == 10))
{
si = 0;
sindex = 0;
sparkley = 200 + textFont->ascent + 20;
}
}
}
}
static void SetHighScoreWait(enum HighScoreStates newMode, int waitFrame)
{
waitingFrame = waitFrame;
waitMode = newMode;
HighScoreState = HIGHSCORE_WAIT;
}
void DoHighScoreWait(void)
{
/* Wait for the end frame then change mode */
if (frame == waitingFrame)
HighScoreState = waitMode;
}
static void DoFinish(Display *display, Window window)
{
mode = MODE_PREVIEW;
HighScoreState = HIGHSCORE_TITLE;
ResetPreviewLevel();
if (noSound == False)
playSoundFile("gate", 50);
SetGameSpeed(FAST_SPEED);
}
void HighScore(Display *display, Window window)
{
/* Switch on the highscore table state */
switch (HighScoreState)
{
case HIGHSCORE_TITLE:
if (getSpecialEffects(display) == True)
{
/* Clear bffer and draw title */
DrawStageBackground(display, bufferWindow, BACKGROUND_SPACE,
True);
DrawStageBackground(display, window, BACKGROUND_SPACE,
False);
DoTitle(display, bufferWindow);
}
else
DoTitle(display, window);
break;
case HIGHSCORE_SHOW:
if (getSpecialEffects(display) == True)
{
DoHighScores(display, bufferWindow);
while (WindowBlindEffect(display, window));
}
else
DoHighScores(display, window);
break;
case HIGHSCORE_SPARKLE:
DoTitleSparkle(display, window);
DoSparkle(display, window);
if ((frame % FLASH) == 0)
RandomDrawSpecials(display);
BorderGlow(display, window);
break;
case HIGHSCORE_FINISH:
DoFinish(display, window);
break;
case HIGHSCORE_WAIT:
DoHighScoreWait();
break;
default:
break;
}
}
void CommandlineHighscorePrint(void)
{
char string[11];
int i;
time_t theTime;
InitialiseHighScores();
/* Must have table initialised with scores */
if (ReadHighScoreTable(GLOBAL) == False)
InitialiseHighScores();
/* Print out a pretty title message for scores */
fprintf(stdout,
" __ ___ __ ____ \n");
fprintf(stdout,
" / /_/ (_)__ _/ / / __/______ _______ ___\n");
fprintf(stdout,
" / __ / / _ `/ _ \\ _\\ \\/ __/ _ \\/ __/ -_|_-<\n");
fprintf(stdout,
" /_/ /_/_/\\_, /_//_/ /___/\\__/\\___/_/ \\__/___/\n");
fprintf(stdout,
" /___/ \n");
fprintf(stdout, "\nRank\tScore\t Level\tTime\tDate Name\n");
fprintf(stdout,
"----------------------------------------------------------------\n");
/* Zoom through the highscore table from the top */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
theTime = (time_t) ntohl(highScores[i].time);
strftime(string, 10, "%d %b %y", localtime(&theTime));
if (ntohl(highScores[i].score) > 0)
{
/* Print out the actual record */
fprintf(stdout, "%d\t%ld\t %ld\t%ld'%ld'%ld\"\t%s %s\n",
i + 1, ntohl(highScores[i].score),
ntohl(highScores[i].level),
ntohl(highScores[i].gameTime) / 3600,
ntohl(highScores[i].gameTime) / 60,
ntohl(highScores[i].gameTime) % 60,
string, highScores[i].name);
}
}
/* Print a trailing line to make the table look good */
fprintf(stdout,
"----------------------------------------------------------------\n");
fflush(stdout);
/* Now display the personal highscore table */
InitialiseHighScores();
/* Must have table initialised with scores */
if (ReadHighScoreTable(PERSONAL) == False)
InitialiseHighScores();
/* Print out a pretty title message for scores */
fprintf(stdout, "\nPersonal Best\n\n");
fprintf(stdout, "Rank\tScore\t Level\tTime\tDate Name\n");
fprintf(stdout,
"----------------------------------------------------------------\n");
/* Zoom through the highscore table from the top */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
theTime = (time_t) ntohl(highScores[i].time);
strftime(string, 10, "%d %b %y", localtime(&theTime));
if (ntohl(highScores[i].score) > 0)
{
/* Print out the actual record */
fprintf(stdout, "%d\t%ld\t %ld\t%ld'%ld'%ld\"\t%s %s\n",
i + 1, ntohl(highScores[i].score),
ntohl(highScores[i].level),
ntohl(highScores[i].gameTime) / 3600,
ntohl(highScores[i].gameTime) / 60,
ntohl(highScores[i].gameTime) % 60,
string, highScores[i].name);
}
}
/* Print a trailing line to make the table look good */
fprintf(stdout,
"----------------------------------------------------------------\n");
fflush(stdout);
}
int GetHighScoreRanking(u_long score)
{
int i;
/* Must have table initialised with scores */
if (ReadHighScoreTable(GLOBAL) == False)
InitialiseHighScores();
/* Zoom through the highscore table from the top */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
/* Is my score better than theirs */
if (score >= ntohl(highScores[i].score))
return (i + 1);
}
/* Not even in highscore table yet! */
return -1;
}
static void ShiftScoresDown(int j, u_long score, u_long level,
time_t gameTime, char *name)
{
/* This function will shift all score below the index down
* towards the end and kill off the last dude. Sorry mate.
*/
int i;
/* Move all the scores down one notch */
for (i = NUM_HIGHSCORES-1; i > j; i--)
{
/* Shift the scores down one notch */
highScores[i].score = highScores[i-1].score;
highScores[i].level = highScores[i-1].level;
highScores[i].time = highScores[i-1].time;
highScores[i].gameTime = highScores[i-1].gameTime;
highScores[i].userId = highScores[i-1].userId;
strcpy(highScores[i].name, highScores[i-1].name);
}
/* Add our new high score to the high score table */
highScores[j].score = htonl(score);
highScores[j].level = htonl(level);
highScores[j].gameTime = htonl(gameTime);
highScores[j].userId = htonl(getuid());
highScores[j].time = htonl(time(NULL));
strcpy(highScores[j].name, name);
}
static void DeleteScore(int j)
{
/* Delete the given score and shift all others up to fill in the hole */
int i;
/* Move all the scores down one notch */
for (i = j; i < NUM_HIGHSCORES-1; i++)
{
/* Shift the scores up one notch */
highScores[i].score = highScores[i+1].score;
highScores[i].level = highScores[i+1].level;
highScores[i].time = highScores[i+1].time;
highScores[i].gameTime = highScores[i+1].gameTime;
highScores[i].userId = highScores[i+1].userId;
strcpy(highScores[i].name, highScores[i+1].name);
}
highScores[i].score = htonl((u_long)0);
highScores[i].level = htonl((u_long)1);
highScores[i].gameTime = htonl(0);
highScores[i].userId = htonl(getuid());
highScores[i].time = htonl(time(NULL));
strcpy(highScores[i].name, "To be announced!");
}
int CheckAndAddScoreToHighScore(u_long score, u_long level, time_t gameTime,
int type, char *message)
{
int i;
int id = -1;
char name[80];
/* Lock the file for me only */
if (type == GLOBAL)
id = LockUnlock(LOCK_FILE);
/* Read in the lastest scores */
if (ReadHighScoreTable(type) == False)
InitialiseHighScores();
/* Speed up by obtaining users name */
if (nickName[0] != '\0')
strcpy(name, nickName);
else
strcpy(name, getUsersFullName());
if (type == GLOBAL)
{
/* Go through the highscore table */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
if (ntohl(highScores[i].userId) == getuid())
{
/* Can the last score be added to the highscores */
if (score > ntohl(highScores[i].score))
{
/* Delete and move up all old scores */
DeleteScore(i);
break;
}
else
{
/* Don't add as score is smaller */
if (id != -1)
id = LockUnlock(UNLOCK_FILE);
return False;
}
}
} /* for */
/* Now add the new score into the table */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
/* Can the last game be added to the highscores */
if (score > ntohl(highScores[i].score))
{
ShiftScoresDown(i, score, level, gameTime, name);
/* Add the boing master message if on top */
if (i == 0)
SetBoingMasterText(message);
/* Add to the highscore by writing it out */
(void) WriteHighScoreTable(type);
/* Unlock the file now thanks */
if (id != -1)
id = LockUnlock(UNLOCK_FILE);
/* Yes - it was placed in the highscore */
return True;
}
}
/* Unlock the file now thanks */
if (id != -1)
id = LockUnlock(UNLOCK_FILE);
/* Not even a highscore - loser! */
return False;
}
else /* Type == PERSONAL */
{
/* Go through the highscore table */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
/* Can the last game be added to the highscores */
if (score > ntohl(highScores[i].score))
{
ShiftScoresDown(i, score, level, gameTime, name);
/* Add to the highscore by writing it out */
(void) WriteHighScoreTable(type);
/* Yes - it was placed in the highscore */
return True;
}
}
/* Not even a highscore - loser! */
return False;
}
}
static void SortHighScores(void)
{
int i, j;
highScoreEntry tempHighScore;
/*
* Perform a simple bubble sort of the entries. I haven't used a
* fancy sorting method as we have only 10 entries to sort.
*/
for (i = 0; i < NUM_HIGHSCORES - 1; ++i)
{
for (j = NUM_HIGHSCORES - 1; j > i; --j)
{
/* Who has the higher score */
if (ntohl(highScores[j-1].score)
< ntohl(highScores[j].score))
{
/* Swap the entries around - use memcpy in future */
tempHighScore.score = highScores[j-1].score;
tempHighScore.level = highScores[j-1].level;
tempHighScore.gameTime = highScores[j-1].gameTime;
tempHighScore.userId = highScores[j-1].userId;
strcpy(tempHighScore.name, highScores[j-1].name);
highScores[j-1].score = highScores[j].score;
highScores[j-1].level = highScores[j].level;
highScores[j-1].gameTime = highScores[j].gameTime;
highScores[j-1].userId = highScores[j].userId;
strcpy(highScores[j-1].name, highScores[j].name);
highScores[j].score = tempHighScore.score;
highScores[j].level = tempHighScore.level;
highScores[j].gameTime = tempHighScore.gameTime;
highScores[j].userId = tempHighScore.userId;
strcpy(highScores[j].name, tempHighScore.name);
}
}
}
}
static void InitialiseHighScores(void)
{
int i;
/* Setup the header structure */
scoresHeader.version = htonl((u_long) SCORE_VERSION);
SetBoingMasterText("Anyone play this game?");
/* Loop down table clearing everything out */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
/* Null out all entries */
highScores[i].score = htonl((u_long) 0);
highScores[i].level = htonl((u_long) 1);
highScores[i].gameTime = htonl(0);
highScores[i].userId = htonl(0);
highScores[i].time = htonl(time(NULL));
strcpy(highScores[i].name, "To be announced!");
}
}
int ReadHighScoreTable(int type)
{
/* Read the high score table into memory structure */
FILE *hsfp;
int i;
char filename[MAXPATHLEN];
char *str;
/* Are we going to use the global or personal highscore file */
if (type == GLOBAL)
{
/* Use the environment variable if it exists */
if ((str = getenv("XBOING_SCORE_FILE")) != NULL)
strcpy(filename, str);
else
strcpy(filename, HIGH_SCORE_FILE);
}
else
sprintf(filename, "%s/.xboing-scores", GetHomeDir());
/* Open the high score file */
if ((hsfp = fopen(filename, "r")) == NULL)
{
/* Cannot open the high score file */
WarningMessage("Cannot open high score file for reading.");
return False;
}
/* Write the highscore header */
if (fread((char*)&scoresHeader, sizeof(highScoreHeader), 1, hsfp) != 1)
{
if (fclose(hsfp) < 0)
WarningMessage("Cannot close high score file.");
return False;
}
if (ntohl(scoresHeader.version) != (u_long) SCORE_VERSION)
{
WarningMessage("Old version of high score files encountered.");
if (fclose(hsfp) < 0)
WarningMessage("Cannot close high score file.");
return False;
}
/* Read all high score entries */
for (i = 0; i < NUM_HIGHSCORES; i++)
{
/* Read the highscore entry */
if (fread((char*)&highScores[i], sizeof(highScoreEntry), 1, hsfp) != 1)
{
if (fclose(hsfp) < 0)
WarningMessage("Cannot close high score file.");
return False;
}
}
/* Close the high score file */
if (fclose(hsfp) < 0)
WarningMessage("Cannot close high score file.");
return True;
}
int WriteHighScoreTable(int type)
{
/* write the high score table to the high score file */
FILE *hsfp;
int i;
char filename[MAXPATHLEN];
char *str;
/* Make sure the table is sorted */
SortHighScores();
/* Are we going to use the global or personal highscore file */
if (type == GLOBAL)
{
/* Use the environment variable if it exists */
if ((str = getenv("XBOING_SCORE_FILE")) != NULL)
strcpy(filename, str);
else
strcpy(filename, HIGH_SCORE_FILE);
}
else
sprintf(filename, "%s/.xboing-scores", GetHomeDir());
/* Open the high score file */
if ((hsfp = fopen(filename, "w+")) == NULL)
{
/* Cannot open the high score file */
WarningMessage("Cannot open high score file for writing.");
return False;
}
/* Setup the header */
scoresHeader.version = htonl((u_long)SCORE_VERSION);
/* Write the highscore header */
if (fwrite((char*)&scoresHeader, sizeof(highScoreHeader), 1, hsfp) != 1)
{
if (fclose(hsfp) < 0)
WarningMessage("Cannot close high score file.");
return False;
}