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