-
Notifications
You must be signed in to change notification settings - Fork 13
/
scores.c
441 lines (396 loc) · 8.76 KB
/
scores.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
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "def.h"
#include "scores.h"
#include "main.h"
#include "draw_api.h"
#include "drawing.h"
#include "hardware.h"
#include "sound.h"
#include "sprite.h"
#include "input.h"
#include "digger.h"
#include "record.h"
#include "game.h"
static struct scdat
{
int32_t score, nextbs, tscore;
} scdat[DIGGERS];
static char highbuf[10];
static int32_t scorehigh[12]={0,0,0,0,0,0,0,0,0,0,0,0};
char scoreinit[11][4];
int32_t scoret=0;
static char hsbuf[36];
static char scorebuf[512];
uint16_t bonusscore=20000;
static void readscores(void);
static void writescores(void);
static void savescores(void);
static void getinitials(struct digger_draw_api *);
static void flashywait(struct digger_draw_api *, int16_t n);
static int16_t getinitial(struct digger_draw_api *, int16_t x,int16_t y);
static void shufflehigh(void);
static void writenum(struct digger_draw_api *, int32_t n,int16_t x,int16_t y,int16_t w,int16_t c);
static void numtostring(char *p,int32_t n);
#if defined FREEBSD && defined _VGL
#define SFNAME "/var/games/digger/digger.sco"
#elif defined UNIX && !defined _VGL && !defined(__EMSCRIPTEN__)
#define SFNAME strncat(strncpy((char*)alloca(PATH_MAX),getenv("HOME"),PATH_MAX),"/.digger.sco",PATH_MAX)
#else
#define SFNAME "DIGGER.SCO"
#endif
#ifdef INTDRF
int32_t getscore0(void)
{
return scdat[0].score;
}
#endif
int32_t gettscore(int n)
{
return scdat[n].tscore + scdat[n].score;
}
static void
readscores(void)
{
FILE *in;
scorebuf[0]=0;
if (!dgstate.levfflag) {
in = fopen(SFNAME, "rb");
if (in == NULL)
return;
} else {
in = fopen(dgstate.levfname, "rb");
if (in == NULL)
return;
if (fseek(in, 1202, 0) < 0)
goto out;
}
if (fread(scorebuf, 512, 1, in) <= 0) {
scorebuf[0]=0;
}
out:
fclose(in);
}
static void
writescores(void)
{
FILE *out;
if (!dgstate.levfflag) {
if ((out=fopen(SFNAME,"wb"))!=NULL) {
fwrite(scorebuf,512,1,out);
fclose(out);
}
}
else
if ((out=fopen(dgstate.levfname,"r+b"))!=NULL) {
if (fseek(out,1202,0) == 0)
fwrite(scorebuf,512,1,out);
fclose(out);
}
}
void initscores(struct digger_draw_api *ddap)
{
int i;
for (i=0;i<dgstate.diggers;i++)
addscore(ddap, i,0);
}
void loadscores(void)
{
int16_t p=0,i,x;
readscores();
if (dgstate.gauntlet)
p=111;
if (dgstate.diggers==2)
p+=222;
if (scorebuf[p++]!='s')
for (i=0;i<11;i++) {
scorehigh[i+1]=0;
strcpy(scoreinit[i],"...");
}
else
for (i=1;i<11;i++) {
for (x=0;x<3;x++)
scoreinit[i][x]=scorebuf[p++];
p+=2;
for (x=0;x<6;x++)
highbuf[x]=scorebuf[p++];
scorehigh[i+1]=atol(highbuf);
}
}
void zeroscores(void)
{
scdat[0].score = scdat[1].score = 0;
scdat[0].tscore = scdat[1].tscore = 0;
scdat[0].nextbs = scdat[1].nextbs = bonusscore;
scoret = 0;
}
void writecurscore(struct digger_draw_api *ddap, int col)
{
if (dgstate.curplayer==0)
writenum(ddap, scdat[0].score,0,0,6,col);
else
if (scdat[1].score<100000l)
writenum(ddap, scdat[1].score,236,0,6,col);
else
writenum(ddap, scdat[1].score,248,0,6,col);
}
void drawscores(struct digger_draw_api *ddap)
{
writenum(ddap, scdat[0].score,0,0,6,3);
if (dgstate.nplayers==2 || dgstate.diggers==2) {
if (scdat[1].score<100000l)
writenum(ddap, scdat[1].score,236,0,6,3);
else
writenum(ddap, scdat[1].score,248,0,6,3);
}
}
void addscore(struct digger_draw_api *ddap, int n,int16_t score)
{
scdat[n].score+=score;
if (scdat[n].score>999999l) {
scdat[n].tscore += scdat[n].score;
scdat[n].score=0;
}
if (n==0)
writenum(ddap, scdat[n].score,0,0,6,1);
else
if (scdat[n].score<100000l)
writenum(ddap, scdat[n].score,236,0,6,1);
else
writenum(ddap, scdat[n].score,248,0,6,1);
if (scdat[n].score>=scdat[n].nextbs+n) { /* +n to reproduce original bug */
if (getlives(n)<5 || dgstate.unlimlives) {
if (dgstate.gauntlet)
dgstate.cgtime+=17897715l; /* 15 second time bonus instead of the life */
else
addlife(n);
drawlives(ddap);
}
scdat[n].nextbs+=bonusscore;
}
incpenalty();
incpenalty();
incpenalty();
}
void endofgame(struct digger_draw_api *ddap)
{
int16_t i;
bool initflag=false;
for (i=0;i<dgstate.diggers;i++)
addscore(ddap, i,0);
if (playing || !drfvalid)
return;
if (dgstate.gauntlet) {
cleartopline();
outtext(ddap, "TIME UP",120,0,3);
for (i=0;i<50 && !escape;i++)
newframe();
erasetext(ddap, 7, 120,0,3);
}
for (i=dgstate.curplayer;i<dgstate.curplayer+dgstate.diggers;i++) {
scoret=scdat[i].score;
if (scoret>scorehigh[11]) {
ddap->gclear();
drawscores(ddap);
strcpy(dgstate.pldispbuf,"PLAYER ");
if (i==0)
strcat(dgstate.pldispbuf,"1");
else
strcat(dgstate.pldispbuf,"2");
outtext(ddap, dgstate.pldispbuf,108,0,2);
outtext(ddap, " NEW HIGH SCORE ",64,40,2);
getinitials(ddap);
shufflehigh();
savescores();
initflag=true;
}
}
if (!initflag && !dgstate.gauntlet) {
cleartopline();
outtext(ddap, "GAME OVER",104,0,3);
for (i=0;i<50 && !escape;i++)
newframe();
erasetext(ddap, 9, 104,0,3);
setretr(true);
}
}
void showtable(struct digger_draw_api *ddap)
{
int16_t i,col;
outtext(ddap, "HIGH SCORES",16,25,3);
col=2;
for (i=1;i<11;i++) {
strcpy(hsbuf,"");
strcat(hsbuf,scoreinit[i]);
strcat(hsbuf," ");
numtostring(highbuf,scorehigh[i+1]);
strcat(hsbuf,highbuf);
outtext(ddap, hsbuf,16,31+13*i,col);
col=1;
}
}
static void
savescores(void)
{
int16_t i,p=0,j;
if (dgstate.gauntlet)
p=111;
if (dgstate.diggers==2)
p+=222;
strcpy(scorebuf+p,"s");
for (i=1;i<11;i++) {
strcpy(hsbuf,"");
strcat(hsbuf,scoreinit[i]);
strcat(hsbuf," ");
numtostring(highbuf,scorehigh[i+1]);
strcat(hsbuf,highbuf);
for (j=0;j<11;j++)
scorebuf[p+j+i*11-10]=hsbuf[j];
}
writescores();
}
void getinitials(struct digger_draw_api *ddap)
{
int16_t k,i;
newframe();
outtext(ddap, "ENTER YOUR",100,70,3);
outtext(ddap, " INITIALS",100,90,3);
outtext(ddap, "_ _ _",128,130,3);
strcpy(scoreinit[0],"...");
killsound();
for (i=0;i<3;i++) {
k=0;
while (k==0) {
k=getinitial(ddap, i*24+128,130);
if (k==8 || k==127) {
if (i>0)
i--;
k=0;
}
}
if (k!=0) {
ddap->gwrite(i*24+128,130,k,3);
scoreinit[0][i]=k;
}
}
for (i=0;i<20;i++)
flashywait(ddap, 15);
setupsound();
ddap->gclear();
ddap->gpal(0);
ddap->ginten(0);
setretr(true);
recputinit(scoreinit[0]);
}
void flashywait(struct digger_draw_api *ddap, int16_t n)
{
int16_t i,gt,cx,p=0;
int8_t gap=19;
gethrt(true);
setretr(false);
for (i=0;i<(n<<1);i++)
for (cx=0;cx<volume;cx++) {
ddap->gpal(p=1-p);
ddap->gflush();
for (gt=0;gt<gap;gt++);
}
}
int16_t getinitial(struct digger_draw_api *ddap, int16_t x,int16_t y)
{
int16_t i;
ddap->gwrite(x,y,'_',3);
do {
for (i=0;i<40;i++) {
if (kbhit()) {
int16_t key = getkey(false);
if (!isalnum(key))
continue;
return key;
}
flashywait(ddap, 15);
}
for (i=0;i<40;i++) {
if (kbhit()) {
ddap->gwrite(x,y,'_',3);
return getkey(false);
}
flashywait(ddap, 15);
}
} while (1);
}
static void
shufflehigh(void)
{
int16_t i,j;
for (j=10;j>1;j--)
if (scoret<scorehigh[j])
break;
for (i=10;i>j;i--) {
scorehigh[i+1]=scorehigh[i];
strcpy(scoreinit[i],scoreinit[i-1]);
}
scorehigh[j+1]=scoret;
strcpy(scoreinit[j],scoreinit[0]);
}
void scorekill(struct digger_draw_api *ddap, int n)
{
addscore(ddap, n,250);
}
void scorekill2(struct digger_draw_api *ddap)
{
addscore(ddap, 0,125);
addscore(ddap, 1,125);
}
void scoreemerald(struct digger_draw_api *ddap, int n)
{
addscore(ddap, n,25);
}
void scoreoctave(struct digger_draw_api *ddap, int n)
{
addscore(ddap, n,250);
}
void scoregold(struct digger_draw_api *ddap, int n)
{
addscore(ddap, n,500);
}
void scorebonus(struct digger_draw_api *ddap, int n)
{
addscore(ddap, n,1000);
}
void scoreeatm(struct digger_draw_api *ddap, int n,int msc)
{
addscore(ddap, n,msc*200);
}
static void
writenum(struct digger_draw_api *ddap, int32_t n,int16_t x,int16_t y,int16_t w,int16_t c)
{
int16_t d,xp=(w-1)*12+x;
while (w>0) {
d=(int16_t)(n%10);
if (w>1 || d>0)
ddap->gwrite(xp,y,d+'0',c);
n/=10;
w--;
xp-=12;
}
}
static void
numtostring(char *p,int32_t n)
{
int x;
for (x=0;x<6;x++) {
p[5-x]=(int8_t)(n%10l)+'0';
n/=10l;
if (n==0l) {
x++;
break;
}
}
for (;x<6;x++)
p[5-x]=' ';
p[6]=0;
}