rocksndiamonds-3.3.0.0
[rocksndiamonds.git] / src / game_sp / BugsTerminals.c
1 // ----------------------------------------------------------------------------
2 // BugsTerminals.c
3 // ----------------------------------------------------------------------------
4
5 #include "BugsTerminals.h"
6
7
8 #if 1
9 byte TerminalState[SP_MAX_PLAYFIELD_SIZE + SP_HEADER_SIZE];
10 #else
11 byte *TerminalState;
12 #endif
13
14 int TerminalMaxCycles;
15
16
17 // ==========================================================================
18 //                              SUBROUTINE
19 // Animate bugs
20 // ==========================================================================
21
22 void subAnimateBugs(int si)
23 {
24   int bl;
25
26   if (fiBug != LowByte(PlayField16[si]))
27     return;
28
29   bl = SgnHighByte(PlayField16[si]);    // get and increment sequence number
30
31   if ((TimerVar & 3) == 0)
32   {
33     bl = bl + 1;
34     if (bl >= 14)                       // bugs animation has 14 frames
35     {
36       bl = subGetRandomNumber();        // generate new random number
37       bl = -((bl & 0x3F) + 0x20);
38     }
39
40     MovHighByte(&PlayField16[si], bl);  // save sequence number
41   }
42
43   if (bl < 0)                           // bug sleeps / is inactive
44     return;
45
46   // now the bug is active! Beware Murphy!
47   if ((TimerVar & 3) == 0 &&
48       (LowByte(PlayField16[si - FieldWidth - 1]) == fiMurphy ||
49        LowByte(PlayField16[si - FieldWidth])     == fiMurphy ||
50        LowByte(PlayField16[si - FieldWidth + 1]) == fiMurphy ||
51        LowByte(PlayField16[si - 1])              == fiMurphy ||
52        LowByte(PlayField16[si + 1])              == fiMurphy ||
53        LowByte(PlayField16[si + FieldWidth - 1]) == fiMurphy ||
54        LowByte(PlayField16[si + FieldWidth])     == fiMurphy ||
55        LowByte(PlayField16[si + FieldWidth + 1]) == fiMurphy))
56     subSoundFX(si, fiBug, actActive);           // play dangerous sound
57
58   // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59   GfxGraphic[GetX(si)][GetY(si)] = (bl == 0  ? aniBugActivating :
60                                     bl == 12 ? aniBugDeactivating :
61                                     bl == 13 ? aniBug : aniBugActive);
62   // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
63 }
64
65
66 // ==========================================================================
67 //                              SUBROUTINE
68 // Animate terminals
69 // ==========================================================================
70
71 void subAnimateTerminals(int si)
72 {
73   int bl;
74
75   int lx = GetX(si);
76   int ly = GetY(si);
77   int graphic;
78
79   if (LowByte(PlayField16[si]) != fiTerminal)
80     return;
81
82   /* use native frame handling (undo frame incrementation in main loop) */
83   if (game.use_native_sp_graphics_engine)
84     GfxFrame[lx][ly]--;
85
86   /* get last random animation delay */
87   bl = SgnHighByte(PlayField16[si]);
88
89   bl = bl + 1;
90   if (bl <= 0)          /* return if random animation delay not yet reached */
91   {
92     MovHighByte(&PlayField16[si], bl);
93
94     return;
95   }
96
97   /* calculate new random animation delay */
98   bl = -(subGetRandomNumber() & TerminalMaxCycles); // generate new random number
99   MovHighByte(&PlayField16[si], bl); // save new sequence number
100
101   /* check terminal state (active or inactive) */
102   bl = TerminalState[si] + 1;
103   if (bl == 8)
104     bl = 0;
105   else if (15 < bl)
106     bl = 8;
107
108   TerminalState[si] = bl;
109
110   // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
111
112   graphic = (bl < 8 ? aniTerminal : aniTerminalActive);
113
114   if (game.use_native_sp_graphics_engine)
115     GfxFrame[lx][ly] += getGraphicInfo_Delay(graphic);
116
117   GfxGraphic[lx][ly] = (bl < 8 ? aniTerminal : aniTerminalActive);
118
119   // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
120 }
121
122
123 // ==========================================================================
124 //                              SUBROUTINE
125 // Randomize random number generator
126 // ==========================================================================
127
128 void subRandomize()
129 {
130   long Tick = MyGetTickCount();
131
132   RandomSeed = (Tick ^ (Tick >> 16)) & 0xFFFF;
133 }
134
135
136 // ==========================================================================
137 //                              SUBROUTINE
138 // Generate new random number, first method (see also sub_g_8580)
139 // ==========================================================================
140
141 int subGetRandomNumber()
142 {
143   RandomSeed = (RandomSeed * 0x5E5 + 0x31) & 0xFFFF;
144
145   return (RandomSeed >> 1);
146
147   //  Mov ax, randomseed
148   //  Mov bx, &H5E5
149   //  mul bx                          ' dx:ax = reg * ax
150   //  Add ax, &H31
151   //  Mov randomseed, ax
152   //  shr ax,1
153 }