9dc2c5576eb3e1dde483a673032eed5791391294
[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 >= 14)                       // bugs animation has 14 frames
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   int 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 = SgnHighByte(PlayField16[si]);
83
84   bl = bl + 1;
85   if (bl <= 0)          /* return if random animation delay not yet reached */
86   {
87     MovHighByte(&PlayField16[si], bl);
88
89     return;
90   }
91
92   /* calculate new random animation delay */
93   bl = -(subGetRandomNumber() & TerminalMaxCycles); // generate new random number
94   MovHighByte(&PlayField16[si], bl); // save new sequence number
95
96   /* check terminal state (active or inactive) */
97   bl = TerminalState[si] + 1;
98   if (bl == 8)
99     bl = 0;
100   else if (15 < bl)
101     bl = 8;
102
103   TerminalState[si] = bl;
104
105   // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
106
107   graphic = (bl < 8 ? aniTerminal : aniTerminalActive);
108
109   if (game.use_native_sp_graphics_engine)
110     GfxFrame[lx][ly] += getGraphicInfo_Delay(graphic);
111
112   GfxGraphic[lx][ly] = (bl < 8 ? aniTerminal : aniTerminalActive);
113
114   // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115 }
116
117
118 // ==========================================================================
119 //                              SUBROUTINE
120 // Randomize random number generator
121 // ==========================================================================
122
123 void subRandomize()
124 {
125   long Tick = MyGetTickCount();
126
127   RandomSeed = (Tick ^ (Tick >> 16)) & 0xFFFF;
128 }
129
130
131 // ==========================================================================
132 //                              SUBROUTINE
133 // Generate new random number, first method (see also sub_g_8580)
134 // ==========================================================================
135
136 int subGetRandomNumber()
137 {
138   RandomSeed = (RandomSeed * 0x5E5 + 0x31) & 0xFFFF;
139
140   return (RandomSeed >> 1);
141
142   //  Mov ax, randomseed
143   //  Mov bx, &H5E5
144   //  mul bx                          ' dx:ax = reg * ax
145   //  Add ax, &H31
146   //  Mov randomseed, ax
147   //  shr ax,1
148 }