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