rnd-20090731-1-src
[rocksndiamonds.git] / src / game_sp / Globals.c
1 // ----------------------------------------------------------------------------
2 // Globals.c
3 // ----------------------------------------------------------------------------
4
5 #include "Globals.h"
6
7 static void ReadDemo();
8 void ReadLevel();
9
10 // static char *VB_Name = "modGlobals";
11
12 // --- Option Explicit
13 // --- Option Compare Text
14 // --- Option Base 0
15
16 // --- const long StretchWidth = 16;
17 // --- const long StretchWidth2 = StretchWidth / 2;
18 // --- const long BaseWidth = 16;
19 // --- const int TwoPixels = 2;
20
21 boolean Original;
22 boolean Cracked;
23 boolean Level_Arg;
24 boolean EGA_Arg;
25 boolean Record_Fix;
26 boolean SpeedKeys;
27 boolean Level_Fix;
28 boolean Dead_Code;
29 boolean Redundant;
30 boolean Alignments;
31 boolean Ctrl_Alt_Fix;
32 boolean Protection;
33 boolean EP_ENHANCE;
34 boolean EP_DEMO;
35 boolean EP_DEBUG;
36 boolean EXTRASPEED;
37 boolean TIMINGFIX;
38 boolean SafeRecord;
39 boolean Norm_Time;
40 boolean EP_OLD8;
41 boolean SAVEGAME;
42 boolean HP_DEMO;
43 boolean ScreenFix;
44 boolean DemoRecordFix;
45 boolean DebugSwitch;
46 boolean Ver62;
47 boolean Ver62test;
48 boolean Ver63;
49 boolean Ver64;
50
51 int LevelNumber;
52 char *CurPath, *OrigPath, *TmpPath;
53 boolean LevelLoaded;
54 long SignatureDelay;
55
56 boolean bCapturePane;
57
58 int FieldWidth; // = 60
59 int FieldHeight; // = 24
60 int HeaderSize; // = 96
61 int FieldMax, LevelMax;
62 long FileMax;
63 int *PlayField16;
64 byte *PlayField8;
65 byte *DisPlayField;
66
67 // Public DisplayMin%, DisplayMax%, DisplayWidth%, DisplayHeight%
68
69 int TimerVar, RandomSeed;
70 currency DeltaT; // Interval between two frames (in ms)
71 long DeltaTPlay, DeltaTDemo;
72 boolean BlockingSpeed;
73
74 // --- const int posFrameCorner = 55;
75 // --- const int posFrameVertical = 110;
76 // --- const int posFrameHorizontal = 111;
77
78 int FreezeZonks;
79
80 // constants for  Fixed Fields:
81 // --- const int fiSpace = 0; // &H00  space(28 = wall space ...)
82 // --- const int fiZonk = 1; // &H01  zonk
83 // --- const int fiBase = 2; // &H02  base
84 // --- const int fiMurphy = 3; // &H03  Murphy
85 // --- const int fiInfotron = 4; // &H04  infotron
86 // --- const int fiRAM = 5; // &H05  small RAM chip
87 // --- const int fiHardWare = 6; // &H06  hardware (square, standard pyramid shape)
88 // --- const int fiExit = 7; // &H07  exit
89 // --- const int fiOrangeDisk = 8; // &H08  brown/orange utility disk
90 // --- const int fiPortRight = 9; // &H09  port 1 left to right
91 // --- const int fiPortDown = 10; // &H0A  port 1 up to down
92 // --- const int fiPortLeft = 11; // &H0B  port 1 right to left
93 // --- const int fiPortUp = 12; // &H0C  port 1 down to up
94 // --- const int fiSpPortRight = 13; // &H0D  port 2 left to right (gravity change)
95 // --- const int fiSpPortDown = 14; // &H0E  port 2 up to down     (gravity change)
96 // --- const int fiSpPortLeft = 15; // &H0F  port 2 right to left (gravity change)
97 // --- const int fiSpPortUp = 16; // &H10  port 2 down to up     (gravity change)
98 // --- const int fiSnikSnak = 17; // &H11  snik snak
99 // --- const int fiYellowDisk = 18; // &H12  yellow utility disk
100 // --- const int fiTerminal = 19; // &H13  terminal
101 // --- const int fiRedDisk = 20; // &H14  red utility disk
102 // --- const int fiPortUpAndDown = 21; // &H15  vertical port
103 // --- const int fiPortLeftAndRight = 22; // &H16  horizontal port
104 // --- const int fiPortAllDirections = 23; // &H17  horizontal + vertical port
105 // --- const int fiElectron = 24; // &H18  electron
106 // --- const int fiBug = 25; // &H19  bug
107 // --- const int fiRAMLeft = 26; // &H1A  horizontal RAM chip, left (pin 1)
108 // --- const int fiRAMRight = 27; // &H1B  horizontal RAM chip, right
109 // --- const int fiHWFirst = 28; // &H1C  hardware (radial blue circular cap + coloured shapes)
110
111 // Public Const fiHW1% = 29               '  29 = 1D  hardware (green signal lamp)
112 // Public Const fiHW2% = 30               '  30 = 1E  hardware (blue signal lamp)
113 // Public Const fiHW3% = 31               '  31 = 1F  hardware (red signal lamp)
114 // Public Const fiHW4% = 32               '  32 = 20  hardware (yellow/black diagonal stripes)
115 // Public Const fiHW5% = 33               '  33 = 21  hardware (yellow resistor + blue + red shapes)
116 // Public Const fiHW6% = 34               '  34 = 22  hardware (horizontal red capacitor + smd shape)
117 // Public Const fiHW7% = 35               '  35 = 23  hardware (red + yellow + blue horizontal resistors)
118 // Public Const fiHW8% = 36               '  36 = 24  hardware (3 red vertical resistors)
119 // --- const int fiHWLast = 37;             //  37 = 25  hardware (3 yellow horizontal resistors)
120 // --- const int fiRAMTop = 38;             //  38 = 26  vertical RAM chip, top (pin 1)
121 // --- const int fiRAMBottom = 39;          //  39 = 27  vertical RAM chip, bottom
122
123 // Specials to experiment with ...
124 // --- const int fiWallSpace = 40;          //  40 = 28  invisible wall (can explode, zonks don't roll off)
125 // --- const int fiHWTrash1 = 41;           //  41 = 29  hardware trash
126 // --- const int fiHWTrash2 = 42;           //  42 = 2A  hardware trash
127 // --- const int fiHWMurphy = 43;           //  43 = 2B  hardware inverted Murphy ... (maybe nice for use?)
128
129 // --- const int fiExplosion = 0x1F;
130
131 // --- const int keyNone = 0;
132 // --- const int keyUp = 1;
133 // --- const int keyLeft = 2;
134 // --- const int keyDown = 3;
135 // --- const int keyRight = 4;
136 // --- const int keySpaceUp = 5;
137 // --- const int keySpaceLeft = 6;
138 // --- const int keySpaceDown = 7;
139 // --- const int keySpaceRight = 8;
140 // --- const int keySpace = 9;
141
142 #if 0
143 int *aniBug, *aniZonkRollRight, *aniZonkRollLeft;
144 int *aniInfotronRollRight, *aniInfotronRollLeft;
145 int *aniSnikSnak, *aniElectron, *aniExplosion;
146 int *aniTouchBase, *aniTouchInfotron, *aniTouchRedDisk;
147 // --- const int aniExplosionInfo = 111;
148 // --- const int aniSnikSnakUp = 159;
149 // --- const int aniSnikSnakDown = 167;
150 // --- const int aniSnikSnakLeft = 239;
151 // --- const int aniSnikSnakRight = 247;
152 // --- const int aniMurphyYawn = 56;
153 // --- const int aniMurphySleepLeft = 71;
154 // --- const int aniMurphySleepRight = 68;
155 int *aniMurphyExit; // , aniMurphyFaceLeft%, aniMurphyFaceRight%
156 int *aniMurphyEatLeft, *aniMurphyEatRight; // , aniMurphyEatRightRedDisk
157 int *aniMurphyEatUpLeft, *aniMurphyEatUpRight, *aniSplitUpDown;
158 int *aniYellowDisk, *aniOrangeDisk, *aniRedDisk;
159 // --- const int aniMurphyTouchUp = 46;
160 // --- const int aniMurphyTouchLeft = 95;
161 // --- const int aniMurphyTouchDown = 47;
162 // --- const int aniMurphyTouchRight = 94;
163 int *aniEatInfotronLeft, *aniEatInfotronRight;
164 // --- const int aniPushLeft = 45;
165 // --- const int aniPushRight = 44;
166 // --- const int aniPushUpDown = 79;
167 #endif
168
169 #if 1
170 int aniBug[] = { 74, 75, 76, 77, 78, 77, 76, 77, 78, 77, 76, 75, 74, 25 };
171 int aniZonkRollRight[] = { 198, 197, 196, 195, 194, 193, 192, 1, -1 };
172 int aniZonkRollLeft[] = { 192, 193, 194, 195, 196, 197, 198, 1, -1 };
173 int aniInfotronRollRight[] = { 206, 205, 204, 203, 202, 201, 200, 4 };
174 int aniInfotronRollLeft[] = { 200, 201, 202, 203, 204, 205, 206, 4 };
175 int aniSnikSnak[] = { 121, 122, 123, 124, 125, 126, 127, 120, 121 };
176 int aniElectron[] = { 144, 145, 146, 147, 148, 149, 150, 151, 144 };
177 int aniExplosion[] = { 3, 103, 104, 105, 106, 107, 108, 109, 0 };
178 int aniTouchBase[] = { 80, 81, 82, 83, 84, 85, 86, 0, -1 };
179 int aniTouchInfotron[] = { 87, 88, 89, 91, 92, 93, 0, -1 }; // Only seven frames!!!!
180 int aniTouchRedDisk[] = { 96, 97, 98, 99, 100, 101, 102, 0, -1 };
181 int aniMurphyExit[] = { 46, 46, 46, 46, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 0, 0, 0, 0, -1 };
182 int aniMurphyEatLeft[] = { 176, 177, 178, 179, 180, 181, 182, 183, -1 };
183 int aniMurphyEatRight[] = { 184, 185, 186, 187, 188, 189, 190, 191, -1 };
184 int aniMurphyEatUpLeft[] = { 183, 182, 181, 180, 179, 178, 177, 176, -1 };
185 int aniMurphyEatUpRight[] = { 191, 190, 189, 188, 187, 186, 185, 184, -1 };
186   // aniMurphyEatRightRedDisk = { 184, 184, 185, 186, 187, 188, 189, 190, 191, -1) '9 frames!
187 int aniEatInfotronLeft[] = { 209, 211, 213, 215, 217, 219, 221, 223, -1 };
188 int aniEatInfotronRight[] = { 224, 226, 228, 230, 232, 234, 236, 238, -1 };
189 int aniSplitUpDown[] = { 3, 3, 3, 3, 3, 3, 3, 3, -1 };
190 int aniYellowDisk[] = { 18, 18, 18, 18, 18, 18, 18, 18, -1 };
191 int aniOrangeDisk[] = { 8, 8, 8, 8, 8, 8, 8, 8, -1 };
192 int aniRedDisk[] = { 20, -1 };
193 #endif
194
195 void InitGlobals()
196 {
197 #if 0
198   aniBug = Array(74, 75, 76, 77, 78, 77, 76, 77, 78, 77, 76, 75, 74, 25);
199   aniZonkRollRight = Array(198, 197, 196, 195, 194, 193, 192, 1, -1);
200   aniZonkRollLeft = Array(192, 193, 194, 195, 196, 197, 198, 1, -1);
201   aniInfotronRollRight = Array(206, 205, 204, 203, 202, 201, 200, 4);
202   aniInfotronRollLeft = Array(200, 201, 202, 203, 204, 205, 206, 4);
203   aniSnikSnak = Array(121, 122, 123, 124, 125, 126, 127, 120, 121);
204   aniElectron = Array(144, 145, 146, 147, 148, 149, 150, 151, 144);
205   aniExplosion = Array(3, 103, 104, 105, 106, 107, 108, 109, 0);
206   aniTouchBase = Array(80, 81, 82, 83, 84, 85, 86, 0, -1);
207   aniTouchInfotron = Array(87, 88, 89, 91, 92, 93, 0, -1); // Only seven frames!!!!
208   aniTouchRedDisk = Array(96, 97, 98, 99, 100, 101, 102, 0, -1);
209   aniMurphyExit = Array(46, 46, 46, 46, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 0, 0, 0, 0, -1);
210   aniMurphyEatLeft = Array(176, 177, 178, 179, 180, 181, 182, 183, -1);
211   aniMurphyEatRight = Array(184, 185, 186, 187, 188, 189, 190, 191, -1);
212   aniMurphyEatUpLeft = Array(183, 182, 181, 180, 179, 178, 177, 176, -1);
213   aniMurphyEatUpRight = Array(191, 190, 189, 188, 187, 186, 185, 184, -1);
214   // aniMurphyEatRightRedDisk = Array(184, 184, 185, 186, 187, 188, 189, 190, 191, -1) '9 frames!
215   aniEatInfotronLeft = Array(209, 211, 213, 215, 217, 219, 221, 223, -1);
216   aniEatInfotronRight = Array(224, 226, 228, 230, 232, 234, 236, 238, -1);
217   aniSplitUpDown = Array(3, 3, 3, 3, 3, 3, 3, 3, -1);
218   aniYellowDisk = Array(18, 18, 18, 18, 18, 18, 18, 18, -1);
219   aniOrangeDisk = Array(8, 8, 8, 8, 8, 8, 8, 8, -1);
220   aniRedDisk = Array(20, -1);
221 #endif
222
223   InitPseudoCompileFlags();
224   UserDragFlag = False;
225   AutoScrollFlag = True;
226   FreezeZonks = 0;
227   BlockingSpeed = False;
228   LevelLoaded = False;
229   FieldWidth = 60;
230   FieldHeight = 24;
231   HeaderSize = 96;
232   FieldMax = (FieldWidth * FieldHeight) + HeaderSize - 1;
233   LevelMax = (FieldWidth * FieldHeight) - 1;
234   Let_ModifiedFlag(False);
235   bPlaying = False;
236   gSignature = "";
237   bSignatureAvailable = False;
238   FirstDemoByte = 0x81;
239   MySignature = "";
240   InitErrorReporting();
241 }
242
243 void InitPseudoCompileFlags()
244 {
245   Cracked = 1;       // If protection Then crack it
246   Level_Arg = 1;     // :number is cmd line option
247   // Level_Arg  = 0     ' Remove Level cmd line option
248   EGA_Arg = 1;       // EGA is command line option
249   Record_Fix = 1;      // Assemble with fixed Demo rec
250   SpeedKeys = 0;     // Remove Speed Keys fix
251   Level_Fix = 1;     // Assemble with Level Fix
252   Dead_Code = 0;     // Remove dead code
253   Redundant = 0;     // Remove redundant code
254   Alignments = 1;      // Assemble with alignments
255   Ctrl_Alt_Fix = 1;      // Assemble with Ctrl/Alt fix
256   Protection = 0;      // Remove protection code,do HP
257   // EP added by EP for version 5.
258   EP_ENHANCE = 1;      // Some more nice things (EP)
259   EP_DEMO = 1;       // Use .SP files for demos (EP)
260   // Including record demo!
261   EP_DEBUG = 0;      // little cmdline debugging
262   EXTRASPEED = 1;      // '@' option, superfast!
263   TIMINGFIX = 1;     // "Fixed" the timing problem..
264   // Inactive If DemoRecordFix Then1
265   SafeRecord = 1;      // skip debug keys in recording
266   Norm_Time = 1;     // force automatic speed test,
267   // save result and then do as
268   // requested from cmd line.
269   EP_OLD8 = 1;       // call old int8 from current.
270   SAVEGAME = 1;      // Allow saving to SUPAPLEX.SAV
271   HP_DEMO = 1;       // Use fixed demo routines 5.5
272   ScreenFix = 1;     // No menu-write to gamy field
273   DemoRecordFix = 1;     // Demo record timing fix on
274   DebugSwitch = 1;     // Allow Ctrl/Alt-ScrollLock
275   Ver62 = 1;       // Version 6.2 stuff
276   Ver62test = 0;     // Version 6.2 test stuff
277   Ver63 = 1;       // Version 6.3 stuff
278   Ver64 = 1;       // Version 6.4 stuff
279
280 }
281
282 int GetSI(int X, int Y)
283 {
284   int GetSI;
285
286   GetSI = Y * FieldWidth + X;
287
288   return GetSI;
289 }
290
291 int GetX(int si)
292 {
293   int GetX;
294
295   GetX = si % FieldWidth;
296
297   return GetX;
298 }
299
300 int GetY(int si)
301 {
302   int GetY;
303
304   GetY = si / FieldWidth;
305
306   return GetY;
307 }
308
309 int GetStretchX(int si)
310 {
311   int GetStretchX;
312
313   GetStretchX = StretchWidth * (si % FieldWidth);
314
315   return GetStretchX;
316 }
317
318 int GetStretchY(int si)
319 {
320   int GetStretchY;
321
322   GetStretchY = StretchWidth * (si / FieldWidth);
323
324   return GetStretchY;
325 }
326
327 void ReadLevel()
328 {
329   // int FNum;
330   FILE *FNum;
331   long i;
332   // byte T;
333
334 #if 1
335   // CurPath = "/home/aeglos/projects/rocksndiamonds/levels/TEST_supaplex/supaplex/levels.dat";
336   CurPath = "/home/aeglos/projects/rocksndiamonds/supaplex_demo_JENS0001.sp";
337   LevelNumber = 1;
338 #endif
339
340   DemoAvailable = False;
341
342   if (STRING_IS_LIKE(CurPath, "*.mpx"))
343   {
344     printf("::: reading MPX file ...\n");
345
346     ReadMPX();
347     return;
348   }
349
350   if (STRING_IS_LIKE(CurPath, "*.sp"))
351   {
352     printf("::: reading SP file ...\n");
353
354     ReadDemo();
355     return;
356   }
357
358   if (DemoFlag != 0)
359   {
360     printf("::: reading demo file ...\n");
361
362     ReadDemo();
363     return;
364   }
365
366   printf("::: reading level file ...\n");
367
368   FileMax = 0;
369   FieldWidth = 60;
370   FieldHeight = 24;
371   HeaderSize = 96;
372
373   FieldMax = (FieldWidth * FieldHeight) + HeaderSize - 1;
374   LevelMax = (FieldWidth * FieldHeight) - 1;
375
376   PlayField8 = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
377   DisPlayField = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
378
379   // FNum = FreeFile();
380
381   // --- On Error GoTo ReadLevelEH
382
383   printf("::: '%s', %d\n", CurPath, LevelNumber);
384
385   FNum = fopen(CurPath, "rb");
386
387   i = (LevelNumber - 1) * ((long)(FieldMax) + 1) + 1;
388 #if 1
389   FILE_GET(FNum, i, PlayField8, FieldMax + 1);
390 #else
391   FILE_GET(FNum, i, &PlayField8, sizeof(PlayField8));
392 #endif
393
394   i = (LevelNumber) * ((long)(FieldMax) + 1) + 1 - HeaderSize;
395   FILE_GET(FNum, i, &LInfo, sizeof(LInfo)); // store level info in an extra structure
396
397   fclose(FNum);
398
399   // --- On Error GoTo 0
400
401   if (FieldMax < FileMax)
402     DemoAvailable = True;
403
404   ReadSignature();
405
406   PlayField16 = REDIM_1D(sizeof(int), -FieldWidth, FieldMax);
407
408   for (i = 0; i <= FieldMax; i++)
409   {
410     PlayField16[i]  = PlayField8[i];
411     DisPlayField[i] = PlayField8[i];
412     PlayField8[i] = 0;
413   }
414
415 #if 0
416  {
417    int x, y;
418
419    for (x = 0; x < 60; x++)
420      printf("%02d.", x);
421    printf("\n");
422
423    for (x = 0; x < 60; x++)
424      printf("...");
425    printf("\n");
426
427    for (y = 0; y < 24; y++)
428    {
429      for (x = 0; x < 60; x++)
430      {
431        printf("%02d.", PlayField16[y * 60 + x]);
432      }
433
434      printf("\n");
435    }
436  }
437 #endif
438
439   AnimationPosTable = REDIM_1D(sizeof(int), 0, LevelMax - 2 * FieldWidth);
440   AnimationSubTable = REDIM_1D(sizeof(byte), 0, LevelMax - 2 * FieldWidth);
441   TerminalState = REDIM_1D(sizeof(byte), FieldWidth, LevelMax - FieldWidth);
442
443   GravityFlag = LInfo.InitialGravity;
444   FreezeZonks = LInfo.InitialFreezeZonks;
445
446   subRandomize();
447
448   LevelLoaded = True;
449
450   return;
451
452 #if 0
453 ReadLevelEH:
454   Close();
455 #endif
456 }
457
458 static void ReadDemo()
459 {
460   // int FNum, i;
461   FILE *FNum;
462   int i;
463   // byte T;
464
465   FieldWidth = 60;
466   FieldHeight = 24;
467   HeaderSize = 96;
468
469   FieldMax = (FieldWidth * FieldHeight) + HeaderSize - 1;
470   LevelMax = (FieldWidth * FieldHeight) - 1;
471
472   // --- On Error GoTo ReadDemoEH
473
474   FileMax = FileLen(CurPath) - 1;
475
476   PlayField8 = REDIM_1D(sizeof(byte), 0, FileMax + 1 - 1);
477   DisPlayField = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
478
479   // FNum = FreeFile();
480
481   FNum = fopen(CurPath, "rb");
482
483   i = (LevelNumber - 1) * ((long)(FieldMax) + 1) + 1;
484 #if 1
485   FILE_GET(FNum, i, PlayField8, FileMax + 1);
486 #else
487   FILE_GET(FNum, i, &PlayField8, sizeof(PlayField8));
488 #endif
489
490   i = (LevelNumber) * ((long)(FieldMax) + 1) + 1 - HeaderSize;
491   FILE_GET(FNum, i, &LInfo, sizeof(LInfo)); // store level info in an extra structure
492
493   fclose(FNum);
494
495   // --- On Error GoTo 0
496
497   if (FieldMax < FileMax)
498     DemoAvailable = True;
499
500   ReadSignature();
501
502   PlayField16 = REDIM_1D(sizeof(int), -FieldWidth, FieldMax);
503
504   for (i = 0; i <= FieldMax; i++)
505   {
506     PlayField16[i] = PlayField8[i];
507     DisPlayField[i] = PlayField8[i];
508     PlayField8[i] = 0;
509   }
510
511   AnimationPosTable = REDIM_1D(sizeof(int), 0, LevelMax - 2 * FieldWidth);
512   AnimationSubTable = REDIM_1D(sizeof(byte), 0, LevelMax - 2 * FieldWidth);
513   TerminalState = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
514
515   DemoPointer = FieldMax + 1;
516   DemoOffset = DemoPointer;
517   DemoKeyRepeatCounter = 0;
518
519   // DemoFlag = 1
520   // DemoAvailable = True
521
522   GravityFlag = LInfo.InitialGravity;
523   FreezeZonks = LInfo.InitialFreezeZonks;
524
525 #if 0
526   printf("::: Globals.c: ReadDemo(): %d / %d\n", GravityFlag, FreezeZonks);
527 #endif
528
529   RandomSeed = LInfo.DemoRandomSeed;
530
531   LevelLoaded = True;
532
533   return;
534
535 #if 0
536 ReadDemoEH:
537   Close();
538 #endif
539 }