-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagicMiner.c
executable file
·1823 lines (1734 loc) · 55 KB
/
MagicMiner.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
/* Project and source code (c) Copyright by Joona Palaste */
/* Licenced under GNU GPL 3.0 */
/*
* Source machine generated by GadToolsBox V2.0b
* which is (c) Copyright 1991-1993 Jaba Development
*
* GUI Designed by : JIPsoft (Joona I Palaste)
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/asl.h>
#include <libraries/dos.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <devices/gameport.h>
#include <devices/inputevent.h>
#include <devices/timer.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "MagicMiner.h"
struct Screen *Scr = NULL;
APTR VisualInfo = NULL;
struct Window *MagicMinerWnd = NULL;
struct Gadget *MagicMinerGList = NULL;
struct Menu *MagicMinerMenus = NULL;
struct IntuiMessage MagicMinerMsg;
struct Gadget *MagicMinerGadgets[9];
UWORD MagicMinerLeft = 0;
UWORD MagicMinerTop = 0;
UWORD MagicMinerWidth = 320;
UWORD MagicMinerHeight = 256;
UBYTE *MagicMinerWdt = (UBYTE *)"Magic Miner v1.0";
struct TextAttr topaz8 = {
( STRPTR )"topaz.font", 8, 0x00, 0x01 };
struct IntuiText MagicMinerIText[] = {
1, 0, JAM1,16, 215, &topaz8, (UBYTE *)"Keys:", &MagicMinerIText[1],
1, 0, JAM1,216, 215, &topaz8, (UBYTE *)"Mode:", NULL };
struct NewMenu MagicMinerNewMenu[] = {
NM_TITLE, (STRPTR)"Game", NULL, 0, NULL, NULL,
NM_ITEM, (STRPTR)"Load level...", (STRPTR)"L", 0, 0L, (APTR)MagicMinerLoad,
NM_ITEM, (STRPTR)"About...", (STRPTR)"A", 0, 0L, (APTR)MagicMinerAbout,
NM_ITEM, (STRPTR)"Quit...", (STRPTR)"Q", 0, 0L, (APTR)MagicMinerQuit,
NM_END, NULL, NULL, 0, 0L, NULL };
UWORD MagicMinerGTypes[] = {
TEXT_KIND,
NUMBER_KIND,
NUMBER_KIND,
NUMBER_KIND,
NUMBER_KIND,
NUMBER_KIND,
NUMBER_KIND,
NUMBER_KIND,
NUMBER_KIND
};
struct NewGadget MagicMinerNGad[] = {
32, 226, 265, 11, NULL, NULL, GD_Message, 0, NULL, NULL,
72, 205, 25, 11, (UBYTE *)"Lives:", NULL, GD_Lives, PLACETEXT_LEFT, NULL, NULL,
152, 205, 25, 11, (UBYTE *)"Time:", NULL, GD_Time, PLACETEXT_LEFT, NULL, NULL,
264, 205, 25, 11, (UBYTE *)"Diamonds:", NULL, GD_Diamonds, PLACETEXT_LEFT, NULL, NULL,
80, 213, 25, 11, (UBYTE *)"B", NULL, GD_Blue, PLACETEXT_LEFT, NULL, NULL,
128, 213, 25, 11, (UBYTE *)"G", NULL, GD_Green, PLACETEXT_LEFT, NULL, NULL,
176, 213, 25, 11, (UBYTE *)"R", NULL, GD_Red, PLACETEXT_LEFT, NULL, NULL,
96, 197, 25, 11, (UBYTE *)"Level #", NULL, GD_Level, PLACETEXT_LEFT, NULL, NULL,
200, 197, 65, 11, (UBYTE *)"Score:", NULL, GD_Score, PLACETEXT_LEFT, NULL, NULL
};
ULONG MagicMinerGTags[] = {
(GTTX_Border), TRUE, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE),
(GTNM_Number), 0, (TAG_DONE)
};
UWORD DriPens[] = {
~0 };
struct MsgPort *port=NULL;
struct IOStdReq *req=NULL;
struct timeval tv;
ULONG secs,secs0,secs1;
LONG micro,micro0,micro1;
UBYTE type=GPCT_ABSJOYSTICK;
struct GamePortTrigger *gpt;
struct InputEvent *ie;
struct Image *image[258];
struct RastPort hidden_rp;
struct BitMap *hidden_bm=NULL;
UBYTE shadecolour,shadedir,gtl;
BYTE plrx,plry,oplrx,oplry,boxx,boxy;
UWORD diamonds;
struct level *level[256];
struct level *currentlevel=NULL;
/* Array declarations. These are copied straight from the AMOS original. */
UBYTE l2p[72]=
{ 0, 1, 3, 5, 7, 9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,28,30,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,66,70,74,75,76,
77,78,79,81,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102};
UBYTE p2l[103];
UBYTE blev[103]=
{0,0,0,0,0,0,0,1,1,2,2,2,2,0,0,0,2,2,2,2,2,2,3,3,
3,3,1,1,2,2,1,1,0,0,0,0,0,1,0,1,1,1,0,2,2,3,2,3,
3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,3,3,2,2,1,3,3,1,1,2,2,2,2,2,2,2,2,2,2,3,3,2,
2,3,3,3,3,1,0};
BYTE lev[4][72]=
{ 0, 1, 2, 3, 8, 9,10,24,25,26,27,28,30,34,71,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 8, 9,10,21,23,24,25,26,27,28,29,30,31,32,33,34,42,43,44,
49,51,70,71,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,21,22,23,24,25,26,27,
28,29,30,31,32,33,34,35,36,38,42,43,44,47,48,49,51,52,53,54,55,56,57,58,
59,60,61,64,65,70,71,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 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};
UBYTE prop[72]=
{1,2,2,2,2,1,2,1,0,2,2,2,2,2,2,2,2,0,
0,0,0,2,2,2,1,1,1,0,0,0,0,0,0,0,1,1,
1,1,1,1,1,1,0,0,1,6,5,6,6,0,0,6,6,6,
6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,6,6,6};
BYTE adir[8][2]=
{-1, 0, 1, 0, 0,-1, 0, 1,
-1,-1,-1, 1, 1,-1, 1, 1};
UBYTE maxblock[4]={15,28,55,72};
UBYTE *leveldata=NULL;
UBYTE *specialmap=NULL;
UBYTE *specialdata=NULL;
UBYTE *tempdata=NULL;
UBYTE inuse[256],key[3],kreq[3];
UBYTE current,cur2,gravity,base,new,new2,moveforced=0,fall=0,spacemode=0,newtwo;
UBYTE face=0,plrblock=0,dest,n;
UBYTE efall=0,eface=0,nmeblock=0,nmedir=RIGHT,nmecount=0,freshchange;
BYTE nmex,nmey,onmex,onmey;
BYTE dx,dy,edx,edy,cy,ddx,ddy,chx,chy,sgrav;
UBYTE ekey[3];
UWORD diamond,selblock;
LONG meter[8]={0,0,0,0,0,0,0,0};
UBYTE spacecolour[8]={0,26,14,20,2,32,13,71};
ULONG joy;
UBYTE character;
void *allocvec(ULONG length,ULONG flags)
{
return (void *)AllocVec(length,flags);
}
/* Block attribute functions. These will be used mainly in constructing the
levels and playing the game. */
UBYTE ob(UBYTE x,UBYTE y)
{
return *(leveldata+x+40*y);
}
UBYTE rock2(UBYTE o)
{
return (o>=1 && o<=10) || (o>=14 && o<=21);
}
UBYTE rock(UBYTE x,UBYTE y)
{
return rock2(ob(x,y));
}
UBYTE liquid2(UBYTE o)
{
return (o>=26 && o<=31);
}
UBYTE liquid(UBYTE x,UBYTE y)
{
return liquid2(ob(x,y));
}
UBYTE grid2(UBYTE o)
{
return (o>=50 && o<=65);
}
UBYTE grid(UBYTE x,UBYTE y)
{
return grid2(ob(x,y));
}
UBYTE horiz2(UBYTE o)
{
return (o>=66 && o<=69);
}
UBYTE horiz(UBYTE x,UBYTE y)
{
return horiz2(ob(x,y));
}
UBYTE vert2(UBYTE o)
{
return (o>=70 && o<=73);
}
UBYTE vert(UBYTE x,UBYTE y)
{
return vert2(ob(x,y));
}
UBYTE hollow(UBYTE n)
{
return prop[n]&1;
}
UBYTE solid(UBYTE n)
{
return (prop[n]&2)/2;
}
UBYTE special(UBYTE n)
{
return (prop[n]&4)/4;
}
UBYTE specialblock(UBYTE n)
{
return (n==81 || n==82 || n==92 || n==96 || n==101);
}
UBYTE gridno(UBYTE n)
{
return (n==50 || n==53 || n==55 || n==56 || n==57 || (n>=59 && n<=65));
}
UBYTE gridyes(UBYTE n)
{
return (n==51 || n==52 || n==54 || n==58);
}
/* Initialises some arrays needed by the program. */
void initarrays()
{
UWORD i;
for (i=0; i<103; i++)
p2l[i]=0;
for (i=0; i<72; i++)
p2l[l2p[i]]=i;
for (i=0; i<103; i++)
if (!p2l[i] && i)
p2l[i]=p2l[i-1];
for (i=0; i<256; i++)
level[i]=NULL;
tempdata=allocvec(9230,MEMF_CLEAR);
}
/* Sends an IO command to the device associated with the IO request, and returns
without waiting for completion. */
void io(UWORD command,ULONG length,APTR data)
{
req->io_Command=command;
req->io_Length=length;
req->io_Data=data;
SendIO(req);
}
/* Tries to set up gameport.device for reading an absolute joystick. Returns
TRUE if successful, FALSE if failed. */
UBYTE setupgameport()
{
if (!(port=CreateMsgPort()))
return FALSE;
if (!(req=(struct IOStdReq *)CreateIORequest(port,sizeof(struct IOStdReq))))
{
DeleteMsgPort(port);
port=NULL;
return FALSE;
}
if (OpenDevice("gameport.device",1,req,0))
{
DeleteIORequest((struct IORequest *)req);
req=NULL;
DeleteMsgPort(port);
port=NULL;
return FALSE;
}
io(GPD_SETCTYPE,1,&type);
gpt=allocvec(sizeof(struct GamePortTrigger),MEMF_CLEAR);
gpt->gpt_Keys=GPTF_DOWNKEYS|GPTF_UPKEYS;
gpt->gpt_XDelta=gpt->gpt_YDelta=1;
gpt->gpt_Timeout=8;
io(GPD_SETTRIGGER,sizeof(struct GamePortTrigger),gpt);
io(CMD_CLEAR,0,0);
ie=allocvec(sizeof(struct InputEvent),MEMF_CLEAR);
return TRUE;
}
/* Undoes everything setupgameport() did, freeing up gameport.device for other
programs. */
void closedowngameport()
{
if (req)
{
AbortIO(req);
WaitIO(req);
CloseDevice(req);
FreeVec(gpt); FreeVec(ie);
DeleteIORequest((struct IORequest *)req);
req=NULL;
if (port)
{
DeleteMsgPort(port);
port=NULL;
}
}
}
/* Reads the joystick port 1 with the gameport.device IO request, and returns a
bitpattern of the direction. Direction bits are like in AMOS. Normal fire
button is 0x4000, other fire buttons have different values. */
ULONG joystick()
{
static UBYTE iosent=FALSE;
ULONG joy=0L;
if (!iosent)
{
io(GPD_READEVENT,sizeof(struct InputEvent),ie);
iosent=TRUE;
}
if (CheckIO(req))
{
WaitIO(req);
if (ie->ie_Y==-1)
joy|=UP;
if (ie->ie_X==1)
joy|=RIGHT;
if (ie->ie_Y==1)
joy|=DOWN;
if (ie->ie_X==-1)
joy|=LEFT;
joy|=(ie->ie_Qualifier&~15);
io(GPD_READEVENT,sizeof(struct InputEvent),ie);
}
return joy;
}
/* Looks at the current system time and updates our timeval structure. */
void gettime()
{
GetSysTime(&tv);
secs=tv.tv_secs; micro=tv.tv_micro;
}
/* Reads a fully usable struct Image from the specified filehandle. Specify
image dimensions and depth. After use, first FreeVec(image->ImageData),
then FreeVec(image). */
struct Image *readimage(BPTR file,UWORD width,UWORD height,UBYTE depth)
{
struct Image *image;
UWORD *imagedata;
ULONG size;
UBYTE i;
if (!(image=allocvec((ULONG)sizeof(struct Image),MEMF_CHIP)))
return NULL;
size=width*height*depth/8;
if (!(imagedata=allocvec(size*2,MEMF_CHIP)))
{
FreeVec(image);
return NULL;
}
if (!Read(file,imagedata,size))
{
FreeVec(image); FreeVec(imagedata);
return NULL;
}
for (i=size-1; i; i--)
*((UBYTE *)imagedata+2*i)=*((UBYTE *)imagedata+i);
image->LeftEdge=0; image->TopEdge=0; image->Width=width; image->Height=height;
image->Depth=depth; image->ImageData=(UWORD *)imagedata;
image->PlanePick=(1<<depth)-1; image->PlaneOnOff=0; image->NextImage=NULL;
return image;
}
/* Safely frees the given struct Image. If called with a nonexistent image
(NULL pointer), does nothing. */
void freeimage(struct Image *image)
{
if (image)
{
FreeVec(image->ImageData); image->ImageData=NULL;
FreeVec(image); image=NULL;
}
}
/* Loads the graphics in the accompanying data file into struct Images. */
int loadgraphics()
{
BPTR file;
UWORD i;
file=Open("MagicMiner.gfx",MODE_OLDFILE);
if (!file)
return NULL;
for (i=0; i<258; i++)
image[i]=NULL;
for (i=0; i<258; i++)
if (!(image[i]=readimage(file,8,8,7)))
{
Close(file);
return NULL;
}
Close(file);
InitRastPort(&hidden_rp);
hidden_bm=AllocBitMap(320,208,7,BMF_CLEAR,NULL);
hidden_rp.BitMap=hidden_bm;
shadecolour=1; shadedir=0;
return TRUE;
}
/* Loads the palette in the accompanying data file into the screen's ViewPort. */
int loadpalette()
{
BPTR file;
UBYTE *data=NULL;
if (!(data=allocvec(1544,NULL)))
return NULL;
file=Open("MagicMiner.pal",MODE_OLDFILE);
if (!file)
{
FreeVec(data);
return NULL;
}
if (Read(file,data,1544)<1544)
{
Close(file);
FreeVec(data);
return NULL;
}
LoadRGB32(&(Scr->ViewPort),data);
Close(file);
FreeVec(data);
return TRUE;
}
/* Frees any struct Images we might have allocated. */
void cleanup()
{
int i;
for (i=0; i<258; i++)
freeimage(image[i]);
if (hidden_bm)
{
FreeBitMap(hidden_bm);
hidden_bm=NULL;
}
}
/* Allocates a new level with the specified number and difficulty. After this
the next and prev fields must be set to the array indeces of the next and
previous levels. These do not have to be in ascending order in the array. */
struct level *createlevel(UBYTE number,UBYTE difficulty)
{
struct level *l;
UBYTE *n,*d,*s,*sd;
if (!(n=allocvec(25,MEMF_CLEAR)))
return NULL;
if (!(d=allocvec(1000,MEMF_CLEAR)))
{
FreeVec(n);
return NULL;
}
if (!(s=allocvec(1000,MEMF_CLEAR)))
{
FreeVec(n); FreeVec(d);
return NULL;
}
if (!(sd=allocvec(8192,MEMF_CLEAR)))
{
FreeVec(n); FreeVec(d); FreeVec(s);
return NULL;
}
if (!(l=allocvec(sizeof(struct level),MEMF_CLEAR)))
{
FreeVec(n); FreeVec(d); FreeVec(s); FreeVec(sd);
return NULL;
}
l->name=n; l->data=d; l->special=s; l->specialdata=sd;
l->plrx=0; l->plry=0; l->nmex=255; l->nmey=255;
l->next=0; l->prev=0;
l->number=number; l->flags=(difficulty<<4)+1;
l->diamonds=0; l->time=300;
return l;
}
/* Deletes the specified level from memory. If called with a nonexistant level
(NULL pointer), does nothing. */
void freelevel(struct level *l)
{
if (l)
{
if (l->name)
FreeVec(l->name);
if (l->data)
FreeVec(l->data);
if (l->special)
FreeVec(l->special);
if (l->specialdata)
FreeVec(l->specialdata);
FreeVec(l);
l=NULL;
}
}
/* Displays any given request with Intution EasyRequesters. */
int request(UBYTE *title,UBYTE *text,UBYTE *buttons,APTR arg)
{
struct EasyStruct req;
req.es_StructSize=sizeof(struct EasyStruct);
req.es_Flags=0; req.es_Title=title;
req.es_TextFormat=text; req.es_GadgetFormat=buttons;
return EasyRequest(MagicMinerWnd,&req,NULL,arg);
}
/* Superimposes a struct Image over an existing struct Image on the hidden
rastport and copies it onto the displayed rastport. ANDs existing image
with mask and then ORs it with the actual block. */
void superimpose(UWORD mask,UWORD img,UWORD qx,UWORD qy,UWORD px,UWORD py)
{
DrawImage(&hidden_rp,image[mask],0,200);
ClipBlit(&hidden_rp,0,200,&hidden_rp,qx,qy,8,8,0x80);
DrawImage(&hidden_rp,image[img],0,200);
ClipBlit(&hidden_rp,0,200,&hidden_rp,qx,qy,8,8,0xE0);
ClipBlit(&hidden_rp,qx,qy,MagicMinerWnd->RPort,px,py,8,8,0xC0);
}
void siobject(UBYTE object)
{
switch (object)
{
case 0:
superimpose(222+4*gravity+2*face,103+4*gravity+2*face,currentlevel->plrx*8,currentlevel->plry*8,currentlevel->plrx*8,currentlevel->plry*8);
break;
case 1:
superimpose(222+4*gravity+2*eface,111+4*gravity+2*eface,currentlevel->nmex*8,currentlevel->nmey*8,currentlevel->nmex*8,currentlevel->nmey*8);
break;
case 2:
superimpose(257,256,boxx*8,boxy*8,boxx*8,boxy*8);
break;
}
}
/* Draws a block on the level display. Uses superimpose() to do the drawing.
Also draws player or enemy pictures if needed. */
void drawblock(UBYTE num,UBYTE x,UBYTE y)
{
UWORD px,py,qx,qy;
gravity=(currentlevel->flags&8)/8;
if (gravity)
sgrav=-1;
else
sgrav=1;
px=8*x; py=8*y;
if (y<25)
{
qx=px; qy=py;
}
else
{
qx=8; qy=200;
}
if (num!=255)
{
DrawImage(&hidden_rp,image[y + (y<25?230:214)],qx,qy);
superimpose(num+119,num,qx,qy,px,py);
if (x==currentlevel->plrx && y==currentlevel->plry)
siobject(0);
if (x==currentlevel->nmex && y==currentlevel->nmey)
siobject(1);
if (x==boxx && y==boxy)
siobject(2);
}
else
DrawImage(MagicMinerWnd->RPort,image[255],px,py);
if (leveldata && y<25)
*(leveldata+x+40*y)=num;
}
/* Adds a block into the actual level and draws it according to all block
unification rules. */
void addblock(UBYTE num,UBYTE x,UBYTE y)
{
UBYTE vrock,vliquid,u,r,d,l;
vrock=vliquid=u=r=d=l=0;
if (specialblock(ob(x,y)) && !specialblock(num))
inuse[*(specialmap+x+40*y)]=FALSE;
if (blev[num]>currentlevel->flags/16)
num=0;
if (!gtl)
{
if (ob(x,y)==42 && num!=42)
diamonds--;
if (ob(x,y)!=42 && num==42)
diamonds++;
if (currentlevel->diamonds>diamonds)
currentlevel->diamonds=diamonds;
}
else
if (ob(x,y)==42)
diamonds++;
if (rock2(num))
{
if (y<24 && !rock(x,y))
if (rock(x,y+1) && ob(x,y+1)<14)
drawblock(((ob(x,y+1)-1)&254)+1,x,y+1);
if (y)
vrock=1-rock(x,y-1);
else
vrock=1;
if (num<14)
num=((num-1)&254|vrock)+1;
}
if (rock(x,y) && !rock2(num))
if (y<24)
if (rock(x,y+1) && ob(x,y+1)<14)
drawblock(((ob(x,y+1)-1)|1)+1,x,y+1);
if (liquid2(num))
{
if (y<24 && !liquid(x,y))
if (liquid(x,y+1))
drawblock(ob(x,y+1)&254,x,y+1);
if (y)
vliquid=1-liquid(x,y-1);
else
vliquid=1;
num=num&254|vliquid;
}
if (liquid(x,y) && !liquid2(num))
if (y<24)
if (liquid(x,y+1))
drawblock(ob(x,y+1)|1,x,y+1);
if (grid2(num))
{
if (y)
if (u=grid(x,y-1))
drawblock(((ob(x,y-1)-2)|4)+2,x,y-1);
if (x<39)
if (r=grid(x+1,y))
drawblock(((ob(x+1,y)-2)|8)+2,x+1,y);
if (y<24)
if (d=grid(x,y+1))
drawblock(((ob(x,y+1)-2)|1)+2,x,y+1);
if (x)
if (l=grid(x-1,y))
drawblock(((ob(x-1,y)-2)|2)+2,x-1,y);
num=50+u+2*r+4*d+8*l;
}
if (grid(x,y) && !grid2(num))
{
if (y)
if (grid(x,y-1))
drawblock(((ob(x,y-1)-2)&251)+2,x,y-1);
if (x<39)
if (grid(x+1,y))
drawblock(((ob(x+1,y)-2)&247)+2,x+1,y);
if (y<24)
if (grid(x,y+1))
drawblock(((ob(x,y+1)-2)&254)+2,x,y+1);
if (x)
if (grid(x-1,y))
drawblock(((ob(x-1,y)-2)&253)+2,x-1,y);
}
if (horiz2(num))
{
if (x)
if (l=horiz(x-1,y))
drawblock(((ob(x-1,y)-2)|2)+2,x-1,y);
if (x<39)
if (r=horiz(x+1,y))
drawblock(((ob(x+1,y)-2)|1)+2,x+1,y);
num=66+l+2*r;
}
if (horiz(x,y) && !horiz2(num))
{
if (x)
if (horiz(x-1,y))
drawblock(((ob(x-1,y)-2)&253)+2,x-1,y);
if (x<39)
if (horiz(x+1,y))
drawblock(((ob(x+1,y)-2)&254)+2,x+1,y);
}
if (vert2(num))
{
if (y)
if (u=vert(x,y-1))
drawblock(((ob(x,y-1)-2)|2)+2,x,y-1);
if (y<24)
if (d=vert(x,y+1))
drawblock(((ob(x,y+1)-2)|1)+2,x,y+1);
num=70+u+2*d;
}
if (vert(x,y) && !vert2(num))
{
if (y)
if (vert(x,y-1))
drawblock(((ob(x,y-1)-2)&253)+2,x,y-1);
if (y<24)
if (vert(x,y+1))
drawblock(((ob(x,y+1)-2)&254)+2,x,y+1);
}
drawblock(num,x,y);
}
/* Changes the shading on the background according to the variables
shadecolour and shadedir. */
void changeshade()
{
ULONG *data;
ULONG value;
UBYTE gravity,i,r,g,b;
if (data=allocvec(392,NULL))
{
*data=(ULONG)0x00200060;
r=(shadecolour&4)/4; g=(shadecolour&2)/2; b=shadecolour&1;
for (i=0; i<32; i++)
{
if (!shadedir)
value=(i*8+7)<<24;
else
value=((31-i)*8+7)<<24;
*(data+3*i+1)=r*value;
*(data+3*i+2)=g*value;
*(data+3*i+3)=b*value;
}
*(data+97)=0L;
LoadRGB32(&(Scr->ViewPort),data);
FreeVec(data);
}
gravity=(currentlevel->flags&8)/8;
currentlevel->flags&=~15;
currentlevel->flags|=(shadedir*8)|shadecolour;
if (gravity!=shadedir)
{
drawblock(ob(currentlevel->plrx,currentlevel->plry),currentlevel->plrx,currentlevel->plry);
if (currentlevel->nmex!=255 && currentlevel->nmey!=255)
drawblock(ob(currentlevel->nmex,currentlevel->nmey),currentlevel->nmex,currentlevel->nmey);
}
}
void updateplr()
{
currentlevel->plrx=plrx; currentlevel->plry=plry;
addblock(plrblock,oplrx,oplry);
siobject(0);
if (plrx==boxx && plry==boxy)
siobject(2);
oplrx=plrx; oplry=plry;
}
void updatenme()
{
currentlevel->nmex=nmex; currentlevel->nmey=nmey;
addblock(nmeblock,onmex,onmey);
siobject(1);
if (nmex==boxx && nmey==boxy)
siobject(2);
onmex=nmex; onmey=nmey;
}
/* Selects the specified level as the current level and draws it on the editor
window. */
void gotolevel(WORD lev)
{
UBYTE i,j,b,used,found;
UBYTE olddif;
UWORD k;
if (currentlevel)
olddif=currentlevel->flags/16;
else
olddif=4;
currentlevel=level[lev];
leveldata=currentlevel->data;
specialmap=currentlevel->special;
specialdata=currentlevel->specialdata;
for (k=0; k<256; k++)
inuse[k]=FALSE;
gtl=TRUE; diamonds=0; used=FALSE; found=FALSE;
for (i=0; i<40; i++)
for (j=0; j<25; j++)
{
b=ob(i,j);
if (b==79 && !used)
{
b=80; used=TRUE; selblock=i+40*j;
}
if (b==94 || b==98)
found=TRUE;
addblock(b,i,j);
if (specialblock(*(leveldata+i+40*j)))
inuse[*(specialmap+i+40*j)]=TRUE;
}
gtl=FALSE;
if (found)
{
boxx=0; boxy=0; siobject(2);
}
else
{
boxx=255; boxy=255;
}
shadedir=(currentlevel->flags&8)/8;
shadecolour=currentlevel->flags&7;
changeshade();
secs0=0L; micro0=0L; secs1=0L; micro1=0L;
}
void swap(UBYTE *first,UBYTE *second)
{
UBYTE auxiliary;
auxiliary=*first;
*first=*second;
*second=auxiliary;
}
/* Returns a pseudo-random number from 0 to (maxvalue-1). */
int rnd(int maxvalue)
{
static int seed=0;
if (seed==0)
{
seed=(int)time(NULL);
srand(seed);
}
return rand()%maxvalue;
}
void addtime(LONG *secsvar,LONG *microvar,LONG dsecs,LONG dmicro)
{
*secsvar+=dsecs;
*microvar+=dmicro;
if (*microvar>=1000000L)
{
*microvar-=1000000L;
*secsvar++;
}
}
void subtime(LONG *secsvar,LONG *microvar,LONG dsecs,LONG dmicro)
{
*secsvar-=dsecs;
*microvar-=dmicro;
if (*microvar<0L)
{
*microvar+=1000000L;
*secsvar--;
}
}
void update(UBYTE what,LONG amount)
{
meter[what]=amount;
GT_SetGadgetAttrs(MagicMinerGadgets[what+1],MagicMinerWnd,NULL,GTNM_Number,amount,TAG_DONE);
}
void updatespacemode(UBYTE mode)
{
SetAPen(MagicMinerWnd->RPort,spacecolour[mode]);
RectFill(MagicMinerWnd->RPort,267,228,285,231);
spacemode=mode;
}
void findnextradio()
{
UWORD i;
if (selblock<65535)
{
i=selblock;
do
i=(i+1)%1000;
while (*(leveldata+i)!=79 && i!=selblock);
if (*(leveldata+i)==79)
{
selblock=i;
addblock(80,i%40,i/40);
}
else
selblock=65535;
}
}
void dospecial(UBYTE x,UBYTE y)
{
UBYTE o=ob(x,y);
UBYTE n,bx,by,b,flag;
BYTE blx,bly,nx,ny;
UWORD bn,i;
UBYTE *bdata;
UBYTE *msg;
chx=0; chy=0;
if (o==81 || o==82 || o==92 || o==96 || o==101)
{
n=*(specialmap+x+40*y);
bdata=specialdata+32*n;
if (o==81 || o==82)
{
bx=*bdata; by=*(bdata+1);
b=ob(bx,by);
if (b==80)
findnextradio();
if (*(bdata+2)==79 && selblock==65535)
{
*(bdata+2)=80; selblock=bx+40*by;
}
addblock(*(bdata+2),bx,by);
*(bdata+2)=b;
addblock(o^3,x,y);
}
if (o==92 || o==96)
{
bx=*bdata; by=*(bdata+1);
}
if (o==101)
{
msg=allocvec(33,MEMF_CLEAR);
CopyMem(bdata,msg,32);
request("Message","%s","Proceed",msg);
FreeVec(msg);
}
}
if (o>=83 && o<=90)
updatespacemode(o-83);
if (o==76 || o==77)
{
shadedir=o-76; gravity=shadedir;
if (shadedir)
sgrav=-1;
else
sgrav=1;
changeshade();
}
if (o>=91 && o<=98)
{
flag=TRUE;
if (o==91 || o==95)
do
{
bx=rnd(40); by=rnd(25);
}
while (!hollow(p2l[ob(bx,by)]));
if (o==93 || o==97)
{
bn=x+40*y; i=bn;
do
i=(i+1)%1000;
while (*(leveldata+i)!=93 && *(leveldata+i)!=97);
bx=i%40; by=i/40;
}
if (o==94 || o==98)
if (hollow(p2l[ob(boxx,boxy)]))
{
bx=boxx; by=boxy;
}
else
flag=FALSE;
if (flag)
{
if (character==PLAYER)
{
chx=bx-plrx; chy=by-plry;
}
else
{
chx=bx-nmex; chy=by-nmey;
}
new=p2l[ob(bx,by)]; new2=ob(bx,by);
}
}
if (o==74)
{
addblock(79,selblock%40,selblock/40);
findnextradio();
}
if (o==75)
{
if (selblock<65535)
{
blx=selblock%40; bly=selblock/40;
nx=blx+dx; ny=bly+dy;
if (nx>=0 && nx<=39 && ny>=0 && ny<=24 && (nx!=blx || ny!=bly))
{
b=p2l[ob(nx,ny)];
if (!b || b==22 || b==23)
{
addblock(0,blx,bly);
if (!b)
{
addblock(80,nx,ny);