-
Notifications
You must be signed in to change notification settings - Fork 13
/
bags.c
439 lines (418 loc) · 10.2 KB
/
bags.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
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include <string.h>
#include "def.h"
#include "bags.h"
#include "main.h"
#include "sprite.h"
#include "sound.h"
#include "drawing.h"
#include "monster.h"
#include "digger.h"
#include "scores.h"
#include "game.h"
static struct bag {
int16_t x,y,h,v,xr,yr,dir,wt,gt,fallh;
bool wobbling,unfallen,exist;
} bagdat1[BAGS],bagdat2[BAGS],bagdat[BAGS];
static int16_t pushcount=0,goldtime=0;
static void updatebag(struct digger_draw_api *, int16_t bag);
static void baghitground(int16_t bag);
static bool pushbag(struct digger_draw_api *, int16_t bag,int16_t dir);
static void removebag(int16_t bn);
static void getgold(struct digger_draw_api *, int16_t bag);
void initbags(void)
{
int16_t bag,x,y;
pushcount=0;
goldtime=150-levof10()*10;
for (bag=0;bag<BAGS;bag++)
bagdat[bag].exist=false;
bag=0;
for (x=0;x<MWIDTH;x++)
for (y=0;y<MHEIGHT;y++)
if (getlevch(x,y,levplan())=='B')
if (bag<BAGS) {
bagdat[bag].exist=true;
bagdat[bag].gt=0;
bagdat[bag].fallh=0;
bagdat[bag].dir=DIR_NONE;
bagdat[bag].wobbling=false;
bagdat[bag].wt=15;
bagdat[bag].unfallen=true;
bagdat[bag].x=x*20+12;
bagdat[bag].y=y*18+18;
bagdat[bag].h=x;
bagdat[bag].v=y;
bagdat[bag].xr=0;
bagdat[bag++].yr=0;
}
if (dgstate.curplayer==0)
memcpy(bagdat1,bagdat,BAGS*sizeof(struct bag));
else
memcpy(bagdat2,bagdat,BAGS*sizeof(struct bag));
}
void drawbags(void)
{
int16_t bag;
for (bag=0;bag<BAGS;bag++) {
if (dgstate.curplayer==0)
memcpy(&bagdat[bag],&bagdat1[bag],sizeof(struct bag));
else
memcpy(&bagdat[bag],&bagdat2[bag],sizeof(struct bag));
if (bagdat[bag].exist)
movedrawspr(bag+FIRSTBAG,bagdat[bag].x,bagdat[bag].y);
}
}
void cleanupbags(void)
{
int16_t bag;
soundfalloff();
for (bag=0;bag<BAGS;bag++) {
if (bagdat[bag].exist && ((bagdat[bag].h==7 && bagdat[bag].v==9) ||
bagdat[bag].xr!=0 || bagdat[bag].yr!=0 || bagdat[bag].gt!=0 ||
bagdat[bag].fallh!=0 || bagdat[bag].wobbling)) {
bagdat[bag].exist=false;
erasespr(bag+FIRSTBAG);
}
if (dgstate.curplayer==0)
memcpy(&bagdat1[bag],&bagdat[bag],sizeof(struct bag));
else
memcpy(&bagdat2[bag],&bagdat[bag],sizeof(struct bag));
}
}
void dobags(struct digger_draw_api *ddap)
{
int16_t bag;
bool soundfalloffflag=true,soundwobbleoffflag=true;
for (bag=0;bag<BAGS;bag++)
if (bagdat[bag].exist) {
if (bagdat[bag].gt!=0) {
if (bagdat[bag].gt==1) {
soundbreak();
drawgold(bag,4,bagdat[bag].x,bagdat[bag].y);
incpenalty();
}
if (bagdat[bag].gt==3) {
drawgold(bag,5,bagdat[bag].x,bagdat[bag].y);
incpenalty();
}
if (bagdat[bag].gt==5) {
drawgold(bag,6,bagdat[bag].x,bagdat[bag].y);
incpenalty();
}
bagdat[bag].gt++;
if (bagdat[bag].gt==goldtime)
removebag(bag);
else
if (bagdat[bag].v<MHEIGHT-1 && bagdat[bag].gt<goldtime-10)
if ((getfield(bagdat[bag].h,bagdat[bag].v+1)&0x2000)==0)
bagdat[bag].gt=goldtime-10;
}
else
updatebag(ddap, bag);
}
for (bag=0;bag<BAGS;bag++) {
if (bagdat[bag].dir==DIR_DOWN && bagdat[bag].exist)
soundfalloffflag=false;
if (bagdat[bag].dir!=DIR_DOWN && bagdat[bag].wobbling && bagdat[bag].exist)
soundwobbleoffflag=false;
}
if (soundfalloffflag)
soundfalloff();
if (soundwobbleoffflag)
soundwobbleoff();
}
static int16_t wblanim[4]={2,0,1,0};
static void
updatebag(struct digger_draw_api *ddap, int16_t bag)
{
int16_t x,h,xr,y,v,yr,wbl;
x=bagdat[bag].x;
h=bagdat[bag].h;
xr=bagdat[bag].xr;
y=bagdat[bag].y;
v=bagdat[bag].v;
yr=bagdat[bag].yr;
switch (bagdat[bag].dir) {
case DIR_NONE:
if (y<180 && xr==0) {
if (bagdat[bag].wobbling) {
if (bagdat[bag].wt==0) {
bagdat[bag].dir=DIR_DOWN;
soundfall();
break;
}
bagdat[bag].wt--;
wbl=bagdat[bag].wt%8;
if (!(wbl&1)) {
drawgold(bag,wblanim[wbl>>1],x,y);
incpenalty();
soundwobble();
}
}
else
if ((getfield(h,v+1)&0xfdf)!=0xfdf)
if (!checkdiggerunderbag(h,v+1))
bagdat[bag].wobbling=true;
}
else {
bagdat[bag].wt=15;
bagdat[bag].wobbling=false;
}
break;
case DIR_RIGHT:
case DIR_LEFT:
if (xr==0) {
if (y<180 && (getfield(h,v+1)&0xfdf)!=0xfdf) {
bagdat[bag].dir=DIR_DOWN;
bagdat[bag].wt=0;
soundfall();
}
else
baghitground(bag);
}
break;
case DIR_DOWN:
if (yr==0)
bagdat[bag].fallh++;
if (y>=180)
baghitground(bag);
else
if ((getfield(h,v+1)&0xfdf)==0xfdf)
if (yr==0)
baghitground(bag);
checkmonscared(bagdat[bag].h);
}
if (bagdat[bag].dir!=DIR_NONE) {
if (bagdat[bag].dir!=DIR_DOWN && pushcount!=0)
pushcount--;
else
pushbag(ddap, bag,bagdat[bag].dir);
}
}
static void
baghitground(int16_t bag)
{
int clfirst[TYPES],clcoll[SPRITES],i;
if (bagdat[bag].dir==DIR_DOWN && bagdat[bag].fallh>1)
bagdat[bag].gt=1;
else
bagdat[bag].fallh=0;
bagdat[bag].dir=DIR_NONE;
bagdat[bag].wt=15;
bagdat[bag].wobbling=false;
drawgold(bag,0,bagdat[bag].x,bagdat[bag].y);
for (i=0;i<TYPES;i++)
clfirst[i]=first[i];
for (i=0;i<SPRITES;i++)
clcoll[i]=coll[i];
incpenalty();
i=clfirst[1];
while (i!=-1) {
removebag(i-FIRSTBAG);
i=clcoll[i];
}
}
static bool
pushbag(struct digger_draw_api *ddap, int16_t bag,int16_t dir)
{
int16_t x,y,h,v,ox,oy;
int clfirst[TYPES],clcoll[SPRITES],i;
bool push=true,digf;
ox=x=bagdat[bag].x;
oy=y=bagdat[bag].y;
h=bagdat[bag].h;
v=bagdat[bag].v;
if (bagdat[bag].gt!=0) {
getgold(ddap, bag);
return true;
}
if (bagdat[bag].dir==DIR_DOWN && (dir==DIR_RIGHT || dir==DIR_LEFT)) {
drawgold(bag,3,x,y);
for (i=0;i<TYPES;i++)
clfirst[i]=first[i];
for (i=0;i<SPRITES;i++)
clcoll[i]=coll[i];
incpenalty();
i=clfirst[4];
while (i!=-1) {
if (diggery(i-FIRSTDIGGER+dgstate.curplayer)>=y)
killdigger(i-FIRSTDIGGER+dgstate.curplayer,1,bag);
i=clcoll[i];
}
if (clfirst[2]!=-1)
squashmonsters(bag,clfirst,clcoll);
return 1;
}
if ((x==292 && dir==DIR_RIGHT) || (x==12 && dir==DIR_LEFT) ||
(y==180 && dir==DIR_DOWN) || (y==18 && dir==DIR_UP))
push=false;
if (push) {
switch (dir) {
case DIR_RIGHT:
x+=4;
break;
case DIR_LEFT:
x-=4;
break;
case DIR_DOWN:
if (bagdat[bag].unfallen) {
bagdat[bag].unfallen=false;
drawsquareblob(x,y);
drawtopblob(x,y+21);
}
else
drawfurryblob(x,y);
eatfield(x,y,dir);
killemerald(h,v);
y+=6;
}
switch(dir) {
case DIR_DOWN:
drawgold(bag,3,x,y);
for (i=0;i<TYPES;i++)
clfirst[i]=first[i];
for (i=0;i<SPRITES;i++)
clcoll[i]=coll[i];
incpenalty();
i=clfirst[4];
while (i!=-1) {
if (diggery(i-FIRSTDIGGER+dgstate.curplayer)>=y)
killdigger(i-FIRSTDIGGER+dgstate.curplayer,1,bag);
i=clcoll[i];
}
if (clfirst[2]!=-1)
squashmonsters(bag,clfirst,clcoll);
break;
case DIR_RIGHT:
case DIR_LEFT:
bagdat[bag].wt=15;
bagdat[bag].wobbling=false;
drawgold(bag,0,x,y);
for (i=0;i<TYPES;i++)
clfirst[i]=first[i];
for (i=0;i<SPRITES;i++)
clcoll[i]=coll[i];
incpenalty();
pushcount=1;
if (clfirst[1]!=-1)
if (!pushbags(ddap, dir,clfirst,clcoll)) {
x=ox;
y=oy;
drawgold(bag,0,ox,oy);
incpenalty();
push=false;
}
i=clfirst[4];
digf=false;
while (i!=-1) {
if (digalive(i-FIRSTDIGGER+dgstate.curplayer))
digf=true;
i=clcoll[i];
}
if (digf || clfirst[2]!=-1) {
x=ox;
y=oy;
drawgold(bag,0,ox,oy);
incpenalty();
push=false;
}
}
if (push)
bagdat[bag].dir=dir;
else
bagdat[bag].dir=reversedir(dir);
bagdat[bag].x=x;
bagdat[bag].y=y;
bagdat[bag].h=(x-12)/20;
bagdat[bag].v=(y-18)/18;
bagdat[bag].xr=(x-12)%20;
bagdat[bag].yr=(y-18)%18;
}
return push;
}
bool pushbags(struct digger_draw_api *ddap, int16_t dir,int *clfirst,int *clcoll)
{
bool push=true;
int next=clfirst[1];
while (next!=-1) {
if (!pushbag(ddap, next-FIRSTBAG,dir))
push=false;
next=clcoll[next];
}
return push;
}
bool pushudbags(struct digger_draw_api *ddap, int *clfirst,int *clcoll)
{
bool push=true;
int next=clfirst[1];
while (next!=-1) {
if (bagdat[next-FIRSTBAG].gt!=0)
getgold(ddap, next-FIRSTBAG);
else
push=false;
next=clcoll[next];
}
return push;
}
static void
removebag(int16_t bag)
{
if (bagdat[bag].exist) {
bagdat[bag].exist=false;
erasespr(bag+FIRSTBAG);
}
}
bool bagexist(int bag)
{
return bagdat[bag].exist;
}
int16_t bagy(int16_t bag)
{
return bagdat[bag].y;
}
int16_t getbagdir(int16_t bag)
{
if (bagdat[bag].exist)
return bagdat[bag].dir;
return -1;
}
void removebags(int *clfirst,int *clcoll)
{
int next=clfirst[1];
while (next!=-1) {
removebag(next-FIRSTBAG);
next=clcoll[next];
}
}
int16_t getnmovingbags(void)
{
int16_t bag,n=0;
for (bag=0;bag<BAGS;bag++)
if (bagdat[bag].exist && bagdat[bag].gt<10 &&
(bagdat[bag].gt!=0 || bagdat[bag].wobbling))
n++;
return n;
}
static void
getgold(struct digger_draw_api *ddap, int16_t bag)
{
bool f=true;
int i;
drawgold(bag,6,bagdat[bag].x,bagdat[bag].y);
incpenalty();
i=first[4];
while (i!=-1) {
if (digalive(i-FIRSTDIGGER+dgstate.curplayer)) {
scoregold(ddap, i-FIRSTDIGGER+dgstate.curplayer);
soundgold();
digresettime(i-FIRSTDIGGER+dgstate.curplayer);
f=false;
}
i=coll[i];
}
if (f)
mongold();
removebag(bag);
}