rnd-20090721-2-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 (AutoScrollFlag)
107   {
108     if (bPlaying)
109     {
110       SoftScrollTo(ScreenScrollXPos, ScreenScrollYPos, 1000, 25);
111     }
112     else
113     {
114       ScrollTo(ScreenScrollXPos, ScreenScrollYPos);
115     }
116   }
117
118   return InitMurphyPosB;
119 } // InitMurphyPosB
120
121 // ==========================================================================
122 //                              SUBROUTINE
123 // Convert to easy symbols and reset Infotron count If not ThenVer62
124 // ==========================================================================
125
126 int subConvertToEasySymbols()
127 {
128   int subConvertToEasySymbols;
129
130   // int ax, bx, cx, dx, di, X, Y, i;
131   // int ah, bh, ch, dh, al, bl, cl, dl, ZF;
132   int ax, bx, cx, dx, i;
133   int al;
134
135   bx = 0;
136   dx = 0;
137   cx = LevelMax + 1;
138   i = 0;
139
140 loc_g_26C9:
141   ax = PlayField16[i];
142   al = LowByte(ax);
143   if (al == 0xF1) // converted explosion?
144   {
145     MovLowByte(&PlayField16[i], 0x1F);      // restore explosions
146     goto loc_g_2778;
147   }
148
149   if (LowByte(GameBusyFlag) != 1) // free screen write?
150   {
151     if (ax == fiInfotron) // Infotron? -> yes--count!
152       goto loc_g_2704;
153
154     if (ax == fiSnikSnak) // Snik Snak? -> yes--rearrange
155       goto loc_g_2713;
156
157     if (ax == fiElectron) // Electron? -> yes--rearrange
158       goto loc_g_2741;
159   }
160
161   // test for fancy RAM Chips:
162   if (ax == fiRAMLeft || ax == fiRAMRight)
163     goto loc_g_2707;
164
165   if (ax == fiRAMTop || ax == fiRAMBottom)
166     goto loc_g_2707;
167
168   if (ax < fiHWFirst) // All but deco hardware?
169     goto loc_g_26F8;
170
171   if (ax < fiRAMTop) // Decorative hardware?
172     goto loc_g_270D;
173
174 loc_g_26F8:
175   if (ax < fiSpPortRight) // Gravity change ports only?
176     goto loc_g_2778;
177
178   if (ax < fiSnikSnak) // Gravity change port! 'loc_g_2702:
179     goto loc_g_276F;
180
181   goto loc_g_2778;
182
183 loc_g_2704:                                     // INFOTRON
184   dx = dx + 1;                      // Count Infotrons
185   goto loc_g_2778;
186
187 loc_g_2707:                                     // DECO RAM CHIPS
188   PlayField16[i] = fiRAM; // Convert to standard RAM chip
189   goto loc_g_2778;
190
191 loc_g_270D:                                     // DECO HARDWARE
192   PlayField16[i] = fiHardWare; // Convert to standard hardware
193   goto loc_g_2778;
194
195 loc_g_2713:                                     // SNIK-SNAK
196   if (PlayField16[i - 1] != 0) // 1 field left empty? -> no--try up
197     goto loc_g_271F;
198
199   MovHighByte(&PlayField16[i], 1); // turn left, step = NorthWest
200   goto loc_g_2778;
201
202 loc_g_271F:
203   if (PlayField16[i - FieldWidth] != 0) // 1 field up empty? -> no--try right
204     goto loc_g_2730;
205
206   PlayField16[i - FieldWidth] = 0x1011; // SnikSnak accessing from below, step = 0
207   PlayField16[i] = 0xFFFF;
208   goto loc_g_2778;
209
210 loc_g_2730:
211   if (PlayField16[i + 1] != 0) // 1 field right empty? -> point up
212     goto loc_g_2778;
213
214   PlayField16[i + 1] = 0x2811; // SnikSnak accessing from left, step = 0
215   PlayField16[i] = 0xFFFF;
216   goto loc_g_2778;
217
218 loc_g_2741:                                     // ELECTRON
219   if (PlayField16[i - 1] != 0) // 1 field left empty? -> no--try up
220     goto loc_g_274D;
221
222   MovHighByte(&PlayField16[i], 1);
223   goto loc_g_2778;
224
225 loc_g_274D:
226   if (PlayField16[i - FieldWidth] != 0) // 1 field up empty? -> no--try right
227     goto loc_g_275E;
228
229   PlayField16[i - FieldWidth] = 0x1018; // 1 field up
230   PlayField16[i] = 0xFFFF;
231   goto loc_g_2778;
232
233 loc_g_275E:
234   if (PlayField16[i + 1] != 0) // 1 field right empty? -> no--point down
235     goto loc_g_2778;
236
237   PlayField16[i + 1] = 0x2818;
238   PlayField16[i] = 0xFFFF;
239   goto loc_g_2778;
240
241 loc_g_276F:                                     // GRAVITY CHANGING PORTS
242   PlayField16[i] = (ax - 4) | 0x100;    // Convert to standard ports
243   goto loc_g_2778;
244
245 loc_g_2778:
246   i = i + 1;                   // Next field
247   bx = bx + 1;
248   cx = cx - 1;
249   if (0 < cx) // Until all done 'loc_g_2782:
250     goto loc_g_26C9;
251
252   subConvertToEasySymbols = dx; // return InfotronCount
253
254   return subConvertToEasySymbols;
255 } // subConvertToEasySymbols
256
257 // ==========================================================================
258 //                              SUBROUTINE
259 // Reset Infotron count.  Call immediately after subConvertToEasySymbols
260 // ==========================================================================
261
262 int ResetInfotronsNeeded(int dx)
263 {
264   int ResetInfotronsNeeded;
265
266   if (LInfo.InfotronsNeeded != 0) // Jump If equal (autodetect)
267   {
268     dx = LInfo.InfotronsNeeded;
269   }                            // loc_g_278D:
270
271   InfotronsNeeded = LowByte(dx);           // Remaining Infotrons needed
272   TotalInfotronsNeeded = InfotronsNeeded;          // Number of Infotrons needed
273   subDisplayInfotronsNeeded();
274
275   return ResetInfotronsNeeded;
276 } // ResetInfotronsNeeded
277
278
279 // ==========================================================================
280 //                              SUBROUTINE
281 // Fetch and initialize a level
282 // ==========================================================================
283
284 int subFetchAndInitLevelB()
285 {
286   int subFetchAndInitLevelB;
287
288   boolean UpdatePlayTime;
289
290   MovLowByte(&data_SPtorunavail, 0);   // no SP file
291   data_scr_demo = 0;
292   UpdatePlayTime = (0 == demo_stopped ?  True :  True);
293   demo_stopped = 0;
294   subFetchAndInitLevelA(UpdatePlayTime);
295
296   return subFetchAndInitLevelB;
297 } // subFetchAndInitLevelb
298
299 int subFetchAndInitLevelA(boolean UpdatePlayTime)
300 {
301   int subFetchAndInitLevelA;
302
303   if (UpdatePlayTime && (0 == demo_stopped))
304   {
305     subUpdatePlayingTime();                 // update playing time
306   }
307
308   D_ModeFlag = 0; // 1=debug D pressed (CPU use)
309   if (0 != demo_stopped) // 1=demo, 0=game
310     DemoFlag = 1;
311
312   GameBusyFlag = 0; // restore scissors too
313
314   subFetchAndInitLevel();   // Fetch and initialize a level
315
316   GameBusyFlag = 1; // no free screen write
317
318   if (1 <= demo_stopped)
319   {
320     if (1 == demo_stopped)
321     {
322       DemoFlag = 0; // 1=demo, 0=game
323       demo_stopped = demo_stopped + 1;
324     }
325     else
326     {
327       DemoFlag = 0; // 1=demo, 0=game
328     }
329   }
330
331   DemoKeyCode = 0; // delete last demo key!
332   if (DemoFlag != 0) // don't allow during game! only in Demo
333   {
334     DemoOffset = DemoPointer;           // init demo pointer
335     DemoKeyRepeatCounter = 1;
336     subGetNextDemoKey();                 // get next demo byte
337   }
338
339   return subFetchAndInitLevelA;
340 } // subFetchAndInitLevela
341
342 int subFetchAndInitLevel()
343 {
344   int subFetchAndInitLevel;
345
346   int InfoCountInLevel;
347
348   Trace("modInitGameConditions", "--> subFetchAndInitLevel");
349   Trace("modInitGameConditions", "Call ReadLevel");
350
351   ReadLevel();                   // Read LEVELS.DAT
352
353   Trace("modInitGameConditions", "ReadLevel return subFetchAndInitLeveled");
354
355   if (RecordDemoFlag == 1)
356   {
357     RecDemoRandomSeed = RandomSeed;
358     // Debug.Print "FetchRec: " & Hex(RandomSeed)
359   }
360
361   //  If DemoFlag = 1 Then
362   // Debug.Print "FetchPlay: " & Hex(RandomSeed)
363   //  End If
364   GameBusyFlag = -GameBusyFlag;   // make <>1
365
366   Trace("modInitGameConditions", "subConvertToEasySymbols");
367
368   InfoCountInLevel = subConvertToEasySymbols(); // Convert to easy symbols
369   GameBusyFlag = -GameBusyFlag;     // restore
370
371   Trace("modInitGameConditions", "subDisplayLevel");
372
373   subDisplayLevel();               // Paint (Init) game field
374   subDisplayPanel();                 // Paint (Init) Panel
375
376   ResetInfotronsNeeded(InfoCountInLevel);  // and reset Infotron count
377
378   Data_SubRstFlg = 1;
379
380   Trace("modInitGameConditions", "subInitGameConditions");
381
382   subInitGameConditions();                 // Init game conditions (vars)
383
384   InitMurphyPos();                 // Locate Murphy + screen pos
385
386   Trace("modInitGameConditions", "<-- subFetchAndInitLevel");
387
388   return subFetchAndInitLevel;
389 } // subFetchAndInitLevel