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