rnd-20100107-1-src
[rocksndiamonds.git] / src / game_sp / InitGameConditions.c
1 // ----------------------------------------------------------------------------
2 // InitGameConditions.c
3 // ----------------------------------------------------------------------------
4
5 #include "InitGameConditions.h"
6
7 // static char *VB_Name = "modInitGameConditions";
8
9 // --- Option Explicit
10
11 // ==========================================================================
12 //                              SUBROUTINE
13 // Init game conditions (variables)
14 // ==========================================================================
15
16 int subInitGameConditions()
17 {
18   int subInitGameConditions;
19
20   bCapturePane = False;
21
22   MurphyVar0DAC = MurphyYPos;
23   MurphyVar0DAE = MurphyXPos;
24   MurphyVarFaceLeft = 0;
25   KillMurphyFlag = 0;            // no "kill Murphy"
26   ExitToMenuFlag = 0;
27   LeadOutCounter = 0;           // quit flag: lead-out counter
28   RedDiskCount = 0; // Red disk counter
29   ShowRedDiskCounter = 0; // show-red-disk time-out
30
31   YawnSleepCounter = 0; // Wake up sleeping Murphy
32
33   data_h_0DA7 = 0xFF;
34   data_h_0DA8 = 0xFF;
35   data_h_0DA9 = 0xFF;
36   data_h_0D9E = 1;
37   data_h_0D9F = 0;
38   data_h_0DA0 = 0;
39   data_h_0DA1 = 0;
40   data_h_0DA2 = 0;
41   data_h_0DA4 = 0;
42   data_h_0DA5 = 0;
43   data_h_0DA6 = 0;
44
45   ExplosionShake = 0; // Force explosion flag off
46
47   TerminalMaxCycles = 0x7F;
48   YellowDisksExploded = 0;
49
50   TimerVar = 0;
51   // ShowPanel = 1 ' Force Panel on
52   // MainForm.PanelVisible = True;
53   EnterRepeatCounter = 0; // restart Enter repeat counter
54   SnikSnaksElectronsFrozen = 0; // Snik-Snaks and Electr. move!
55
56   SplitMoveFlag = 0; // Reset Split-through-ports
57   RedDiskReleasePhase = 0; // (re-)enable red disk release
58   RedDiskReleaseMurphyPos = 0; // Red disk was released here
59
60
61   return subInitGameConditions;
62 } // subInitGameConditions
63
64
65 // ==========================================================================
66 //                              SUBROUTINE
67 // Locate Murphy and init location.
68 // ==========================================================================
69
70 int InitMurphyPos()
71 {
72   int InitMurphyPos;
73
74   int si;
75
76   for (si = 0; si <= LevelMax - 1; si++)
77   {
78     if (PlayField16[si] == fiMurphy)
79       break;
80   }
81
82   InitMurphyPosB(si);
83   MurphyPosIndex = si;
84
85   return InitMurphyPos;
86 } // InitMurphyPos
87
88 int InitMurphyPosB(int si)
89 {
90   int InitMurphyPosB;
91
92   MurphyYPos = GetStretchY(si) / Stretch;
93   MurphyXPos = GetStretchX(si) / Stretch;
94
95   MurphyScreenXPos = GetStretchX(si);          // Murphy's screen x-position
96   MurphyScreenYPos = GetStretchY(si);         // Murphy's screen y-position
97
98   // To Do: draw Murphy in location ax
99   StretchedSprites.BltEx(MurphyScreenXPos, MurphyScreenYPos, fiMurphy);
100
101   MurphyScreenXPos = MurphyScreenXPos / Stretch;
102   MurphyScreenYPos = MurphyScreenYPos / Stretch;
103
104   subCalculateScreenScrollPos();           // calculate screen start addrs
105
106 #if 0
107   printf(":1: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
108          mScrollX, mScrollY,
109          mScrollX_last, mScrollY_last,
110          ScreenScrollXPos, ScreenScrollYPos,
111          ScrollX, ScrollY);
112 #endif
113
114   if (AutoScrollFlag)
115   {
116     if (bPlaying)
117     {
118       SoftScrollTo(ScreenScrollXPos, ScreenScrollYPos, 1000, 25);
119     }
120     else
121     {
122       ScrollTo(ScreenScrollXPos, ScreenScrollYPos);
123     }
124   }
125
126 #if 0
127   printf(":2: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
128          mScrollX, mScrollY,
129          mScrollX_last, mScrollY_last,
130          ScreenScrollXPos, ScreenScrollYPos,
131          ScrollX, ScrollY);
132 #endif
133
134   return InitMurphyPosB;
135 } // InitMurphyPosB
136
137 // ==========================================================================
138 //                              SUBROUTINE
139 // Convert to easy symbols and reset Infotron count If not ThenVer62
140 // ==========================================================================
141
142 int subConvertToEasySymbols()
143 {
144   int subConvertToEasySymbols;
145
146   // int ax, bx, cx, dx, di, X, Y, i;
147   // int ah, bh, ch, dh, al, bl, cl, dl, ZF;
148   int ax, bx, cx, dx, i;
149   int al;
150
151   bx = 0;
152   dx = 0;
153   cx = LevelMax + 1;
154   i = 0;
155
156 loc_g_26C9:
157   ax = PlayField16[i];
158   al = LowByte(ax);
159   if (al == 0xF1) // converted explosion?
160   {
161     MovLowByte(&PlayField16[i], 0x1F);      // restore explosions
162     goto loc_g_2778;
163   }
164
165   if (LowByte(GameBusyFlag) != 1) // free screen write?
166   {
167     if (ax == fiInfotron) // Infotron? -> yes--count!
168       goto loc_g_2704;
169
170     if (ax == fiSnikSnak) // Snik Snak? -> yes--rearrange
171       goto loc_g_2713;
172
173     if (ax == fiElectron) // Electron? -> yes--rearrange
174       goto loc_g_2741;
175   }
176
177   // test for fancy RAM Chips:
178   if (ax == fiRAMLeft || ax == fiRAMRight)
179     goto loc_g_2707;
180
181   if (ax == fiRAMTop || ax == fiRAMBottom)
182     goto loc_g_2707;
183
184   if (ax < fiHWFirst) // All but deco hardware?
185     goto loc_g_26F8;
186
187   if (ax < fiRAMTop) // Decorative hardware?
188     goto loc_g_270D;
189
190 loc_g_26F8:
191   if (ax < fiSpPortRight) // Gravity change ports only?
192     goto loc_g_2778;
193
194   if (ax < fiSnikSnak) // Gravity change port! 'loc_g_2702:
195     goto loc_g_276F;
196
197   goto loc_g_2778;
198
199 loc_g_2704:                                     // INFOTRON
200   dx = dx + 1;                      // Count Infotrons
201   goto loc_g_2778;
202
203 loc_g_2707:                                     // DECO RAM CHIPS
204   PlayField16[i] = fiRAM; // Convert to standard RAM chip
205   goto loc_g_2778;
206
207 loc_g_270D:                                     // DECO HARDWARE
208   PlayField16[i] = fiHardWare; // Convert to standard hardware
209   goto loc_g_2778;
210
211 loc_g_2713:                                     // SNIK-SNAK
212   if (PlayField16[i - 1] != 0) // 1 field left empty? -> no--try up
213     goto loc_g_271F;
214
215   MovHighByte(&PlayField16[i], 1); // turn left, step = NorthWest
216   goto loc_g_2778;
217
218 loc_g_271F:
219   if (PlayField16[i - FieldWidth] != 0) // 1 field up empty? -> no--try right
220     goto loc_g_2730;
221
222   PlayField16[i - FieldWidth] = 0x1011; // SnikSnak accessing from below, step = 0
223   PlayField16[i] = 0xFFFF;
224   goto loc_g_2778;
225
226 loc_g_2730:
227   if (PlayField16[i + 1] != 0) // 1 field right empty? -> point up
228     goto loc_g_2778;
229
230   PlayField16[i + 1] = 0x2811; // SnikSnak accessing from left, step = 0
231   PlayField16[i] = 0xFFFF;
232   goto loc_g_2778;
233
234 loc_g_2741:                                     // ELECTRON
235   if (PlayField16[i - 1] != 0) // 1 field left empty? -> no--try up
236     goto loc_g_274D;
237
238   MovHighByte(&PlayField16[i], 1);
239   goto loc_g_2778;
240
241 loc_g_274D:
242   if (PlayField16[i - FieldWidth] != 0) // 1 field up empty? -> no--try right
243     goto loc_g_275E;
244
245   PlayField16[i - FieldWidth] = 0x1018; // 1 field up
246   PlayField16[i] = 0xFFFF;
247   goto loc_g_2778;
248
249 loc_g_275E:
250   if (PlayField16[i + 1] != 0) // 1 field right empty? -> no--point down
251     goto loc_g_2778;
252
253   PlayField16[i + 1] = 0x2818;
254   PlayField16[i] = 0xFFFF;
255   goto loc_g_2778;
256
257 loc_g_276F:                                     // GRAVITY CHANGING PORTS
258   PlayField16[i] = (ax - 4) | 0x100;    // Convert to standard ports
259   goto loc_g_2778;
260
261 loc_g_2778:
262   i = i + 1;                   // Next field
263   bx = bx + 1;
264   cx = cx - 1;
265   if (0 < cx) // Until all done 'loc_g_2782:
266     goto loc_g_26C9;
267
268   subConvertToEasySymbols = dx; // return InfotronCount
269
270   return subConvertToEasySymbols;
271 } // subConvertToEasySymbols
272
273 // ==========================================================================
274 //                              SUBROUTINE
275 // Reset Infotron count.  Call immediately after subConvertToEasySymbols
276 // ==========================================================================
277
278 int ResetInfotronsNeeded(int dx)
279 {
280   int ResetInfotronsNeeded;
281
282   if (LInfo.InfotronsNeeded != 0) // Jump If equal (autodetect)
283   {
284     dx = LInfo.InfotronsNeeded;
285   }                            // loc_g_278D:
286
287   InfotronsNeeded = LowByte(dx);           // Remaining Infotrons needed
288   TotalInfotronsNeeded = InfotronsNeeded;          // Number of Infotrons needed
289   subDisplayInfotronsNeeded();
290
291   return ResetInfotronsNeeded;
292 } // ResetInfotronsNeeded
293
294
295 // ==========================================================================
296 //                              SUBROUTINE
297 // Fetch and initialize a level
298 // ==========================================================================
299
300 int subFetchAndInitLevelB()
301 {
302   int subFetchAndInitLevelB;
303
304   boolean UpdatePlayTime;
305
306   MovLowByte(&data_SPtorunavail, 0);   // no SP file
307   data_scr_demo = 0;
308   UpdatePlayTime = (0 == demo_stopped ? True : False);
309   demo_stopped = 0;
310   subFetchAndInitLevelA(UpdatePlayTime);
311
312   return subFetchAndInitLevelB;
313 } // subFetchAndInitLevelb
314
315 int subFetchAndInitLevelA(boolean UpdatePlayTime)
316 {
317   int subFetchAndInitLevelA;
318
319   if (UpdatePlayTime && (0 == demo_stopped))
320   {
321     subUpdatePlayingTime();                 // update playing time
322   }
323
324   D_ModeFlag = 0; // 1=debug D pressed (CPU use)
325   if (0 != demo_stopped) // 1=demo, 0=game
326     DemoFlag = 1;
327
328   GameBusyFlag = 0; // restore scissors too
329
330   subFetchAndInitLevel();   // Fetch and initialize a level
331
332   GameBusyFlag = 1; // no free screen write
333
334   if (1 <= demo_stopped)
335   {
336     if (1 == demo_stopped)
337     {
338       DemoFlag = 0; // 1=demo, 0=game
339       demo_stopped = demo_stopped + 1;
340     }
341     else
342     {
343       DemoFlag = 0; // 1=demo, 0=game
344     }
345   }
346
347   DemoKeyCode = 0; // delete last demo key!
348   if (DemoFlag != 0) // don't allow during game! only in Demo
349   {
350     DemoOffset = DemoPointer;           // init demo pointer
351     DemoKeyRepeatCounter = 1;
352     subGetNextDemoKey();                 // get next demo byte
353   }
354
355   return subFetchAndInitLevelA;
356 } // subFetchAndInitLevela
357
358 int subFetchAndInitLevel()
359 {
360   int subFetchAndInitLevel;
361
362   int InfoCountInLevel;
363
364   Trace("modInitGameConditions", "--> subFetchAndInitLevel");
365   Trace("modInitGameConditions", "Call ReadLevel");
366
367   ReadLevel();                   // Read LEVELS.DAT
368
369   Trace("modInitGameConditions", "ReadLevel return subFetchAndInitLeveled");
370
371   if (RecordDemoFlag == 1)
372   {
373     RecDemoRandomSeed = RandomSeed;
374     // Debug.Print "FetchRec: " & Hex(RandomSeed)
375   }
376
377   //  If DemoFlag = 1 Then
378   // Debug.Print "FetchPlay: " & Hex(RandomSeed)
379   //  End If
380   GameBusyFlag = -GameBusyFlag;   // make <>1
381
382   Trace("modInitGameConditions", "subConvertToEasySymbols");
383
384   InfoCountInLevel = subConvertToEasySymbols(); // Convert to easy symbols
385   GameBusyFlag = -GameBusyFlag;     // restore
386
387   Trace("modInitGameConditions", "subDisplayLevel");
388
389   subDisplayLevel();               // Paint (Init) game field
390   subDisplayPanel();                 // Paint (Init) Panel
391
392   ResetInfotronsNeeded(InfoCountInLevel);  // and reset Infotron count
393
394   Data_SubRstFlg = 1;
395
396   Trace("modInitGameConditions", "subInitGameConditions");
397
398   subInitGameConditions();                 // Init game conditions (vars)
399
400   InitMurphyPos();                 // Locate Murphy + screen pos
401
402   Trace("modInitGameConditions", "<-- subFetchAndInitLevel");
403
404   return subFetchAndInitLevel;
405 } // subFetchAndInitLevel