summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2FireWork.cpp
blob: 11bb86fed0e338f73faa0a6f4698f57607ffad7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "T2BitImage.h"
#include "T2DLL.h"
#include "T2FireWork.h"
#include "T2SoundPlayer.h"

T2FireWork::T2FireWork(int inWidth, int inHeight, CPalette* inPalette) {
    Sounds->AddSound("Firework:Don", SoundPriority_0, 1000, AfxGetResourceHandle());

    RECT rect;
    rect.top = 0;
    rect.left = 0;
    rect.bottom = inHeight;
    rect.right = inWidth;

    mImage = new T2BitImage(rect);
    memset(mImage->mData, 0, mImage->mBitmap.header.biSizeImage);
#define ALIGN_ROW_SIZE(s) ((((s) + 3) / 4) * 4)
    mRowSize = ALIGN_ROW_SIZE(inWidth);

    SetRect(&mRect, 0, 0, inWidth, inHeight);

    mIsActive = false;

    int i;
    for (i = 0; i < 5; i++) {
        for (int j = 0; j < 8; j++) {
            int r, g, b;
            if (i == 0) {
                r = ((j + 1) * 255) / 8;
                g = ((j + 1) * 255) / 8;
                b = ((j + 1) * 100) / 8;
            } else if (i == 1) {
                r = ((j + 1) * 255) / 8;
                g = ((j + 1) * 186) / 8;
                b = ((j + 1) * 100) / 8;
            } else if (i == 2) {
                r = ((j + 1) * 255) / 8;
                g = ((j + 1) * 50) / 8;
                b = ((j + 1) * 50) / 8;
            } else if (i == 3) {
                r = ((j + 1) * 80) / 8;
                g = ((j + 1) * 80) / 8;
                b = ((j + 1) * 255) / 8;
            } else if (i == 4) {
                r = ((j + 1) * 255) / 8;
                g = ((j + 1) * 255) / 8;
                b = ((j + 1) * 200) / 8;
            }

            mPalettes[i][j] = inPalette->GetNearestPaletteIndex(RGB(r, g, b));
        }
    }

    for (i = 0; i < 20; i++)
        mUsed[i] = false;
}

T2FireWork::~T2FireWork() {
    delete mImage;

    for (int i = 0; i < 20; i++) {
        if (mUsed[i])
            free(mStarInfo[i]);
    }
}

T2BitImage* T2FireWork::GetBitImage() {
    return mImage;
}

void T2FireWork::Start() {
    mIsActive = true;
}

void T2FireWork::Stop() {
    mIsActive = false;
}

BOOL T2FireWork::IsDisposable() {
    if (mIsActive)
        return false;

    for (int i = 0; i < 20; i++) {
        if (mUsed[i])
            return false;
    }

    return true;
}

BOOL T2FireWork::Idle() {
    static DWORD lastUpdate;
    static DWORD counter;

    BOOL anyChange = false;
    DWORD now = GetTickCount();
    int ind, ind2;

    if (mIsActive && lastUpdate != now) {
        if ((counter % 35) == 0 || (rand() % 80) == 0) {
            for (ind = 0; ind < 20; ind++) {
                if (!mUsed[ind]) {
#pragma var_order(var24, var28, var2C, bufferSize, var34, var38)
                    float var24 = (((rand() % 40) - 20) / 100.0f) + 0.8f;
                    float var38 = var24;
                    float var28 = int((var38 * 6.283186) / 0.18) / 4;
                    int var34 = 1;

                    for (float var2C = 0.0f; var2C <= var28; var2C += 1.0f) {
#pragma var_order(var3C, var40, var44)
                        float var3C = var38 * cos((var2C * 1.5707965) / var28);
                        float var40 = int((var3C * 6.283186) / 0.095);

                        for (float var44 = 0.0f; var44 < var40 || var40 == 0.0f; var44 += 1.0f) {
                            var34++;
                            if (var40 == 0.0f)
                                break;
                        }
                    }

#pragma var_order(var24, var28, var2C, bufferSize, var34, var38)
                    mUsed[ind] = true;

                    int bufferSize = (var34 + 2) * sizeof(FIREWORK_STARINFO);
                    mStarInfo[ind] = (FIREWORK_STARINFO *) malloc(bufferSize);
                    memset(mStarInfo[ind], 0, bufferSize);

                    mStarInfo[ind]->m0 = (rand() % 200) / 100.0f - 1.0f;
                    mStarInfo[ind]->m4 = (rand() % 400) / 200.0f + -9.5f - 2.0f;
                    mStarInfo[ind]->m8 = ((mRect.right / 2) + (rand() % (mRect.right / 3))) - (mRect.right / 6);
                    mStarInfo[ind]->mC = mRect.bottom;
                    mStarInfo[ind]->m10 = 0.0f;
                    mStarInfo[ind]->m14 = 0.01f;
                    mStarInfo[ind]->m18 = 50.0f;
                    mStarInfo[ind]->m1C = 15.0f;
                    mStarInfo[ind]->m24 = var24;
                    mStarInfo[ind]->mFlags = 0x101;
                    break;
                }
            }
        }
        counter++;
    }

    lastUpdate = now;

    for (ind = 0; ind < 20; ind++) {
        if (mUsed[ind]) {
            FIREWORK_STARINFO *info = mStarInfo[ind];
            BOOL changed = false;

            for (ind2 = 0; info[ind2].mFlags != 0; ind2++) {
#pragma var_order(x, y, star, size, pixel)
                FIREWORK_STARINFO *star = info + ind2;
                int x, y, pixel, size;

                if (CalcStarData(star, star->m10 - star->m1C, &x, &y, &pixel, &size)) {
                    PutDot(x, y, size, 0);
                    changed = true;
                }

                if (CalcStarData(star, star->m10, &x, &y, &pixel, &size)) {
                    PutDot(x, y, size, mPalettes[HIBYTE(pixel)][LOBYTE(pixel)]);
                    changed = true;
                }

                star->m10 += 1.0f;
            }

            if (!(info->mFlags & 0x8000) && (info->m10 >= info->m18)) {
#pragma var_order(var64, var68, var6C, var70, var74, origInfo, var7C, var80, var84, var88)
                Sounds->Play("Firework:Don", SoundMask_10, SoundFlags_10, NULL, PlayMode_2, 100);

                FIREWORK_STARINFO *origInfo = info;
                origInfo->mFlags |= 0x8000;

                unsigned short var88 = 0;
                float var74 = 10.0f;
                float var64 = 0.0f;
                float var6C = rand() % 10 + 25;
                float var84 = mStarInfo[ind]->m24;
                switch (rand() % 10) {
                    case 0:
                        var88 = 8;
                        break;
                    case 1:
                        var88 = 0xC8;
                        break;
                    case 2:
                        var88 = 0x84;
                        break;
                    case 3:
                        var88 = 0x88;
                        break;
                    case 4:
                        var88 = 4;
                        break;
                    case 5:
                        var88 = 2;
                        break;
                    case 6:
                        var88 = 0x10;
                        break;
                    case 7:
                    case 8:
                        var88 = 0x28;
                        break;
                    case 9:
                        var88 = 0xD0;
                        break;
                }

                float var68 = int((var84 * 6.283186) / 0.18) / 4;
                int var80 = 1;
                float var7C = mRect.bottom * 0.016f;

                for (float var70 = 0.0f; var70 <= var68; var70 += 1.0f) {
#pragma var_order(var8C, var90, var94)
                    float var8C = var84 * cos((var70 * 1.5707965) / var68);
                    float var90 = int((var8C * 6.283186) / 0.095);

                    for (float var94 = 0.0f; var94 < var90 || var90 == 0.0f; var94 += 1.0f) {
#pragma var_order(var98, var9C, varA0, varA4, varA8, varAC, varB0)
                        float var98 = (var94 * 6.283186) / ((var90 != 0.0f) ? var90 : 1.0f);
                        float varA0 = float(cos(var98)) * var8C * var7C;
                        float varA4 = float(sin(var98)) * var8C * var7C;
                        int var9C, varA8, varB0, varAC;
                        CalcStarData(origInfo, origInfo->m10 - 1.0f, &var9C, &varA8, &varB0, &varAC);

                        info[var80].m0 = varA0;
                        info[var80].m4 = varA4;
                        info[var80].m8 = var9C;
                        info[var80].mC = varA8;
                        info[var80].m10 = 0.0f;
                        info[var80].m14 = var64;
                        info[var80].m18 = var6C;
                        info[var80].m1C = var74;
                        info[var80].mFlags = var88;
                        var80++;

                        if (var90 == 0.0f)
                            break;
                    }
                }
            }

            anyChange |= changed;

            if (!changed) {
                free(mStarInfo[ind]);
                mUsed[ind] = false;
            }
        }
    }

    return anyChange;
}

BOOL T2FireWork::CalcStarData(FIREWORK_STARINFO* inInfo, float inTime, int* outX, int* outY, int* outPixel, int* outSize) {
    if (inTime >= 0.0f && inTime < inInfo->m18) {
        float blend = 0.01f;
        blend = 1.0f - (1.0f / ((inTime / 8.0f) + 1.0f));

        float mult = inInfo->m0 * 30.0f;
        *outX = (mult * blend) + inInfo->m8;

        mult = inInfo->m4 * 30.0f;
        *outY = (mult * blend) + inInfo->mC + (inInfo->m14 * inTime * inTime);

        int group = 0;
        int color = 7;

        if (inInfo->mFlags & 2)
            group = 0;
        else if (inInfo->mFlags & 4)
            group = 1;
        else if (inInfo->mFlags & 8)
            group = 2;
        else if (inInfo->mFlags & 0x10)
            group = 3;
        else if (inInfo->mFlags & 1)
            group = 4;

        if (inInfo->mFlags & 0x20) {
            if ((inInfo->m18 / 2.0f) < inTime)
                group++;
        } else if (inInfo->mFlags & 0x40) {
            if ((inInfo->m18 - 2.0f) < inTime)
                group++;
        }

        while (group >= 5)
            group -= 5;

        if (inInfo->mFlags & 0x100) {
            color = (inTime * 10.0f) / inInfo->m18;
            if (color >= 8)
                color = 7;
        } else if (inInfo->mFlags & 0x200) {
            if ((inInfo->m18 / 2.0f) < inTime)
                color = ((inInfo->m18 - inTime) * 8.0f) / (inInfo->m18 / 2.0f);
            if (inTime >= 8.0f)
                inTime = 7.0f;
        } else {
            color = (((inTime * 8.0f) / 2.0f) / inInfo->m18) + 4.0f;
        }

        *outPixel = (group << 8) | color;

        if (inInfo->mFlags & 0x40) {
            if (inTime > (inInfo->m18 - 2.0f))
                *outSize = 3;
            else
                *outSize = 1;
        } else {
            *outSize = 1;
        }

        return true;
    }

    return false;
}

void T2FireWork::PutDot(int inX, int inY, int inSize, unsigned char inCol) {
    RECT theDotRect, theSubRect;

    if (inSize == 1)
        SetRect(&theDotRect, inX, inY, inX + 1, inY + 1);
    else if (inSize == 2)
        SetRect(&theDotRect, inX, inY, inX + 2, inY + 2);
    else if (inSize == 3 || inSize == 4)
        SetRect(&theDotRect, inX - 1, inY - 1, inX + 2, inY + 2);

    SubtractRect(&theSubRect, &theDotRect, &mRect);
    if (!IsRectEmpty(&theSubRect))
        return;

    int p = inX + (mRowSize * inY);
    unsigned char *theData = mImage->mData;

    if (inSize == 1) {
        theData[p] = inCol;
    } else if (inSize == 2) {
        theData[p] = inCol;
        theData[p + 1] = inCol;
        theData[p + mRowSize] = inCol;
        theData[p + mRowSize + 1] = inCol;
    } else if (inSize == 3) {
        theData[p - 1] = inCol;
        theData[p] = inCol;
        theData[p + 1] = inCol;
        theData[p - mRowSize] = inCol;
        theData[p + mRowSize] = inCol;
    } else if (inSize == 4) {
        theData[p - mRowSize - 1] = inCol;
        theData[p - mRowSize] = inCol;
        theData[p - mRowSize + 1] = inCol;
        theData[p - 1] = inCol;
        theData[p] = inCol;
        theData[p + 1] = inCol;
        theData[p + mRowSize - 1] = inCol;
        theData[p + mRowSize] = inCol;
        theData[p + mRowSize + 1] = inCol;
    }
}