rnd-20090719-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 int *aniBug, *aniZonkRollRight, *aniZonkRollLeft;
143 int *aniInfotronRollRight, *aniInfotronRollLeft;
144 int *aniSnikSnak, *aniElectron, *aniExplosion;
145 int *aniTouchBase, *aniTouchInfotron, *aniTouchRedDisk;
146 // --- const int aniExplosionInfo = 111;
147 // --- const int aniSnikSnakUp = 159;
148 // --- const int aniSnikSnakDown = 167;
149 // --- const int aniSnikSnakLeft = 239;
150 // --- const int aniSnikSnakRight = 247;
151 // --- const int aniMurphyYawn = 56;
152 // --- const int aniMurphySleepLeft = 71;
153 // --- const int aniMurphySleepRight = 68;
154 int *aniMurphyExit; // , aniMurphyFaceLeft%, aniMurphyFaceRight%
155 int *aniMurphyEatLeft, *aniMurphyEatRight; // , aniMurphyEatRightRedDisk
156 int *aniMurphyEatUpLeft, *aniMurphyEatUpRight, *aniSplitUpDown;
157 int *aniYellowDisk, *aniOrangeDisk, *aniRedDisk;
158 // --- const int aniMurphyTouchUp = 46;
159 // --- const int aniMurphyTouchLeft = 95;
160 // --- const int aniMurphyTouchDown = 47;
161 // --- const int aniMurphyTouchRight = 94;
162 int *aniEatInfotronLeft, *aniEatInfotronRight;
163 // --- const int aniPushLeft = 45;
164 // --- const int aniPushRight = 44;
165 // --- const int aniPushUpDown = 79;
166
167 void InitGlobals()
168 {
169   aniBug = Array(74, 75, 76, 77, 78, 77, 76, 77, 78, 77, 76, 75, 74, 25);
170   aniZonkRollRight = Array(198, 197, 196, 195, 194, 193, 192, 1, -1);
171   aniZonkRollLeft = Array(192, 193, 194, 195, 196, 197, 198, 1, -1);
172   aniInfotronRollRight = Array(206, 205, 204, 203, 202, 201, 200, 4);
173   aniInfotronRollLeft = Array(200, 201, 202, 203, 204, 205, 206, 4);
174   aniSnikSnak = Array(121, 122, 123, 124, 125, 126, 127, 120, 121);
175   aniElectron = Array(144, 145, 146, 147, 148, 149, 150, 151, 144);
176   aniExplosion = Array(3, 103, 104, 105, 106, 107, 108, 109, 0);
177   aniTouchBase = Array(80, 81, 82, 83, 84, 85, 86, 0, -1);
178   aniTouchInfotron = Array(87, 88, 89, 91, 92, 93, 0, -1); // Only seven frames!!!!
179   aniTouchRedDisk = Array(96, 97, 98, 99, 100, 101, 102, 0, -1);
180   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);
181   aniMurphyEatLeft = Array(176, 177, 178, 179, 180, 181, 182, 183, -1);
182   aniMurphyEatRight = Array(184, 185, 186, 187, 188, 189, 190, 191, -1);
183   aniMurphyEatUpLeft = Array(183, 182, 181, 180, 179, 178, 177, 176, -1);
184   aniMurphyEatUpRight = Array(191, 190, 189, 188, 187, 186, 185, 184, -1);
185   // aniMurphyEatRightRedDisk = Array(184, 184, 185, 186, 187, 188, 189, 190, 191, -1) '9 frames!
186   aniEatInfotronLeft = Array(209, 211, 213, 215, 217, 219, 221, 223, -1);
187   aniEatInfotronRight = Array(224, 226, 228, 230, 232, 234, 236, 238, -1);
188   aniSplitUpDown = Array(3, 3, 3, 3, 3, 3, 3, 3, -1);
189   aniYellowDisk = Array(18, 18, 18, 18, 18, 18, 18, 18, -1);
190   aniOrangeDisk = Array(8, 8, 8, 8, 8, 8, 8, 8, -1);
191   aniRedDisk = Array(20, -1);
192   InitPseudoCompileFlags();
193   UserDragFlag = False;
194   AutoScrollFlag = True;
195   FreezeZonks = 0;
196   BlockingSpeed = False;
197   LevelLoaded = False;
198   FieldWidth = 60;
199   FieldHeight = 24;
200   HeaderSize = 96;
201   FieldMax = (FieldWidth * FieldHeight) + HeaderSize - 1;
202   LevelMax = (FieldWidth * FieldHeight) - 1;
203   Let_ModifiedFlag(False);
204   bPlaying = False;
205   gSignature = "";
206   bSignatureAvailable = False;
207   FirstDemoByte = 0x81;
208   MySignature = "";
209   InitErrorReporting();
210 }
211
212 void InitPseudoCompileFlags()
213 {
214   Cracked = 1;       // If protection Then crack it
215   Level_Arg = 1;     // :number is cmd line option
216   // Level_Arg  = 0     ' Remove Level cmd line option
217   EGA_Arg = 1;       // EGA is command line option
218   Record_Fix = 1;      // Assemble with fixed Demo rec
219   SpeedKeys = 0;     // Remove Speed Keys fix
220   Level_Fix = 1;     // Assemble with Level Fix
221   Dead_Code = 0;     // Remove dead code
222   Redundant = 0;     // Remove redundant code
223   Alignments = 1;      // Assemble with alignments
224   Ctrl_Alt_Fix = 1;      // Assemble with Ctrl/Alt fix
225   Protection = 0;      // Remove protection code,do HP
226   // EP added by EP for version 5.
227   EP_ENHANCE = 1;      // Some more nice things (EP)
228   EP_DEMO = 1;       // Use .SP files for demos (EP)
229   // Including record demo!
230   EP_DEBUG = 0;      // little cmdline debugging
231   EXTRASPEED = 1;      // '@' option, superfast!
232   TIMINGFIX = 1;     // "Fixed" the timing problem..
233   // Inactive If DemoRecordFix Then1
234   SafeRecord = 1;      // skip debug keys in recording
235   Norm_Time = 1;     // force automatic speed test,
236   // save result and then do as
237   // requested from cmd line.
238   EP_OLD8 = 1;       // call old int8 from current.
239   SAVEGAME = 1;      // Allow saving to SUPAPLEX.SAV
240   HP_DEMO = 1;       // Use fixed demo routines 5.5
241   ScreenFix = 1;     // No menu-write to gamy field
242   DemoRecordFix = 1;     // Demo record timing fix on
243   DebugSwitch = 1;     // Allow Ctrl/Alt-ScrollLock
244   Ver62 = 1;       // Version 6.2 stuff
245   Ver62test = 0;     // Version 6.2 test stuff
246   Ver63 = 1;       // Version 6.3 stuff
247   Ver64 = 1;       // Version 6.4 stuff
248
249 }
250
251 int GetSI(int X, int Y)
252 {
253   int GetSI;
254
255   GetSI = Y * FieldWidth + X;
256
257   return GetSI;
258 }
259
260 int GetX(int si)
261 {
262   int GetX;
263
264   GetX = si % FieldWidth;
265
266   return GetX;
267 }
268
269 int GetY(int si)
270 {
271   int GetY;
272
273   GetY = si / FieldWidth;
274
275   return GetY;
276 }
277
278 int GetStretchX(int si)
279 {
280   int GetStretchX;
281
282   GetStretchX = StretchWidth * (si % FieldWidth);
283
284   return GetStretchX;
285 }
286
287 int GetStretchY(int si)
288 {
289   int GetStretchY;
290
291   GetStretchY = StretchWidth * (si / FieldWidth);
292
293   return GetStretchY;
294 }
295
296 void ReadLevel()
297 {
298   // int FNum;
299   FILE *FNum;
300   long i;
301   // byte T;
302
303   DemoAvailable = False;
304
305   if (STRING_IS_LIKE(CurPath, "*.mpx"))
306   {
307     ReadMPX();
308     return;
309   }
310
311   if (STRING_IS_LIKE(CurPath, "*.sp"))
312   {
313     ReadDemo();
314     return;
315   }
316
317   if (DemoFlag != 0)
318   {
319     ReadDemo();
320     return;
321   }
322
323   FileMax = 0;
324   FieldWidth = 60;
325   FieldHeight = 24;
326   HeaderSize = 96;
327
328   FieldMax = (FieldWidth * FieldHeight) + HeaderSize - 1;
329   LevelMax = (FieldWidth * FieldHeight) - 1;
330
331   PlayField8 = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
332   DisPlayField = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
333
334   // FNum = FreeFile();
335
336   // --- On Error GoTo ReadLevelEH
337
338 #if 1
339   CurPath = "/home/aeglos/projects/rocksndiamonds/levels/TEST_supaplex/supaplex/levels.dat";
340   LevelNumber = 1;
341 #endif
342
343   printf("::: '%s', %d\n", CurPath, LevelNumber);
344
345   FNum = fopen(CurPath, "rb");
346
347   i = (LevelNumber - 1) * ((long)(FieldMax) + 1) + 1;
348 #if 1
349   FILE_GET(FNum, i, PlayField8, FieldMax + 1);
350 #else
351   FILE_GET(FNum, i, &PlayField8, sizeof(PlayField8));
352 #endif
353   i = (LevelNumber) * ((long)(FieldMax) + 1) + 1 - HeaderSize;
354   FILE_GET(FNum, i, &LInfo, sizeof(LInfo)); // store level info in an extra structure
355
356   fclose(FNum);
357
358   // --- On Error GoTo 0
359
360   if (FieldMax < FileMax)
361     DemoAvailable = True;
362
363   ReadSignature();
364
365   PlayField16 = REDIM_1D(sizeof(int), -FieldWidth, FieldMax);
366
367   for (i = 0; i <= FieldMax; i++)
368   {
369 #if 0
370     printf("::: %d: %d\n", i, PlayField8[i]);
371 #endif
372
373     PlayField16[i] = PlayField8[i];
374     DisPlayField[i] = PlayField8[i];
375     PlayField8[i] = 0;
376   }
377
378   AnimationPosTable = REDIM_1D(sizeof(int), 0, LevelMax - 2 *FieldWidth);
379   AnimationSubTable = REDIM_1D(sizeof(byte), 0, LevelMax - 2 *FieldWidth);
380   TerminalState = REDIM_1D(sizeof(byte), FieldWidth, LevelMax - FieldWidth);
381
382   GravityFlag = LInfo.InitialGravity;
383   FreezeZonks = LInfo.InitialFreezeZonks;
384
385   subRandomize();
386
387   LevelLoaded = True;
388
389   return;
390
391 #if 0
392 ReadLevelEH:
393   Close();
394 #endif
395 }
396
397 static void ReadDemo()
398 {
399   // int FNum, i;
400   FILE *FNum;
401   int i;
402   // byte T;
403
404   FieldWidth = 60;
405   FieldHeight = 24;
406   HeaderSize = 96;
407   FieldMax = (FieldWidth * FieldHeight) + HeaderSize - 1;
408   LevelMax = (FieldWidth * FieldHeight) - 1;
409
410   // --- On Error GoTo ReadDemoEH
411   FileMax = FileLen(CurPath) - 1;
412   PlayField8 = REDIM_1D(sizeof(byte), 0, FileMax + 1 - 1);
413   DisPlayField = REDIM_1D(sizeof(byte), 0, FieldMax + 1 - 1);
414   // FNum = FreeFile();
415   FNum = fopen(CurPath, "rb");
416   i = (LevelNumber - 1) * ((long)(FieldMax) + 1) + 1;
417 #if 1
418   FILE_GET(FNum, i, PlayField8, FileMax + 1);
419 #else
420   FILE_GET(FNum, i, &PlayField8, sizeof(PlayField8));
421 #endif
422   i = (LevelNumber) * ((long)(FieldMax) + 1) + 1 - HeaderSize;
423   FILE_GET(FNum, i, &LInfo, sizeof(LInfo)); // store level info in an extra structure
424   fclose(FNum);
425   // --- On Error GoTo 0
426
427   if (FieldMax < FileMax)
428     DemoAvailable = True;
429
430   ReadSignature();
431   PlayField16 = REDIM_1D(sizeof(int), -FieldWidth, FieldMax);
432   for (i = 0; i <= FieldMax; i++)
433   {
434     PlayField16[i] = PlayField8[i];
435     DisPlayField[i] = PlayField8[i];
436     PlayField8[i] = 0;
437   }
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), 0, FieldMax + 1 - 1);
442   DemoPointer = FieldMax + 1;
443   DemoOffset = DemoPointer;
444   DemoKeyRepeatCounter = 0;
445   // DemoFlag = 1
446   // DemoAvailable = True
447   GravityFlag = LInfo.InitialGravity;
448   FreezeZonks = LInfo.InitialFreezeZonks;
449   RandomSeed = LInfo.DemoRandomSeed;
450   LevelLoaded = True;
451   return;
452
453 #if 0
454 ReadDemoEH:
455   Close();
456 #endif
457 }