summaryrefslogtreecommitdiff
path: root/src/d/d_ky_thunder.cpp
blob: 58ca5a9544f06d3daa7bac481ff7b1c01157fd71 (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
/**
 * d_ky_thunder.cpp
 * Environment - Thunder
 */

#include "d/d_ky_thunder.h"
#include "JSystem/JKernel/JKRSolidHeap.h"
#include "SSystem/SComponent/c_counter.h"
#include "SSystem/SComponent/c_math.h"
#include "d/com/d_com_inf_game.h"
#include "d/kankyo/d_kankyo_rain.h"
#include "f_op/f_op_kankyo_mng.h"

#define EF_THUNDER01_BMD 0x1E
#define EF_THUNDER02_BMD 0x1F
#define EF_THUNDER_BRK 0x2F

/* 801ADF58-801ADFB4 1A8898 005C+00 1/1 0/0 0/0 .text            createHeap__10dThunder_cFv */
int dThunder_c::createHeap() {
    if (mpHeap == NULL) {
        mpHeap = mDoExt_createSolidHeapFromGameToCurrent(0, 0x20);

        if (mpHeap == NULL) {
            return 0;
        }
    }

    return 1;
}

/* 801ADFB4-801AE000 1A88F4 004C+00 1/1 0/0 0/0 .text            adjustHeap__10dThunder_cFv */
void dThunder_c::adjustHeap() {
    mDoExt_restoreCurrentHeap();

    if ((s32)mDoExt_adjustSolidHeap(mpHeap) >= 0) {
        DCStoreRangeNoSync(mpHeap->getStartAddr(), mpHeap->getHeapSize());
    }
}

int dThunder_c::draw() {
    static cXyz l_offsetPos(0.0f, 40.0f, -250.0f);
    static cXyz l_scale(1.0f, 1.0f, 1.0f);

    camera_class* camera_p = dComIfGp_getCamera(0);

    mDoMtx_stack_c::transS(mThunderPos.x, mThunderPos.y, mThunderPos.z);
    mDoMtx_stack_c::YrotM(field_0x168);
    mDoMtx_stack_c::ZrotM(field_0x15c);
    mDoMtx_stack_c::XrotM(field_0x15c);

    Mtx m;
    MTXCopy(mDoMtx_stack_c::get(), m);

    mModelInfo.mpModel->setBaseScale(field_0x138);
    mModelInfo.mpModel->setBaseTRMtx(m);
    mModelInfo.mBrk.entry(mModelInfo.mpModel->getModelData());

    dComIfGd_setList();
    mDoExt_modelUpdateDL(mModelInfo.mpModel);
    mModelInfo.mBrk.remove(mModelInfo.mpModel->getModelData());
    return 1;
}

/* 801AE000-801AE19C 1A8940 019C+00 1/0 0/0 0/0 .text            dThunder_Draw__FP10dThunder_c */
static int dThunder_Draw(dThunder_c* i_this) {
    return i_this->draw();
}

int dThunder_c::execute(dThunder_c* i_thunder) {
    dScnKy_env_light_c* env_light = dKy_getEnvlight();

    if (env_light->mThunderEff.mStateTimer < 10) {
        cLib_addCalc(&mBrkSpeed, 0.1f, 0.05f, 0.017f, 9.999999747378752e-06f);
    }

    mModelInfo.mBrk.setPlaySpeed(mBrkSpeed);
    if (mModelInfo.mBrk.play()) {
        mDoAud_mEnvSe_startFarThunderSe(&mThunderPos);
        fopKyM_Delete(i_thunder);
    }

    f32 fvar7;
    if (dComIfGd_getView() != NULL) {
        fvar7 = dComIfGd_getView()->mFovy / 60.0f;
        if (fvar7 >= 1.0f) {
            fvar7 = 1.0f;
        }

        fvar7 = fvar7 * fvar7 * fvar7;
    } else {
        fvar7 = 1.0f;
    }

    fvar7 *= mBrkSpeed;

    if (env_light->mThunderEff.mStateTimer < 10) {
        field_0x138.x += fvar7 * field_0x138.x * 0.01f * cM_ssin(field_0x16a);
        field_0x138.y += fvar7 * field_0x138.y * 0.01f * cM_scos(field_0x16c);
        field_0x16a += -25000;
        field_0x16c += 28900;
    } else {
        field_0x138.x += fvar7 * field_0x138.x * 0.02f * cM_ssin(field_0x16a);
        field_0x138.y += fvar7 * field_0x138.y * 0.02f * cM_scos(field_0x16c);
        field_0x16a += -35000;
        field_0x16c += 38900;
    }

    return 1;
}

/* 801AE19C-801AE374 1A8ADC 01D8+00 1/0 0/0 0/0 .text            dThunder_Execute__FP10dThunder_c */
static int dThunder_Execute(dThunder_c* i_this) {
    return i_this->execute(i_this);
}

/* 801AE374-801AE37C 1A8CB4 0008+00 1/0 0/0 0/0 .text            dThunder_IsDelete__FP10dThunder_c
 */
static int dThunder_IsDelete(dThunder_c* i_this) {
    return 1;
}

int dThunder_c::thunder_delete(dThunder_c* i_thunder) {
    i_thunder->~dThunder_c();
    return 1;
}

/* 801AE37C-801AE3FC 1A8CBC 0080+00 1/0 0/0 0/0 .text            dThunder_Delete__FP10dThunder_c */
static int dThunder_Delete(dThunder_c* i_this) {
    return i_this->thunder_delete(i_this);
}

/* 801AE3FC-801AE458 1A8D3C 005C+00 1/0 0/0 0/0 .text            dThunder_Create__FP12kankyo_class
 */
static int dThunder_Create(kankyo_class* i_this) {
    dThunder_c* this_ = (dThunder_c*)i_this;

    if (!this_->createHeap()) {
        return cPhs_ERROR_e;
    }

    int ret = this_->create();
    this_->adjustHeap();

    return ret;
}

/* 801AE458-801AE938 1A8D98 04E0+00 1/1 0/0 0/0 .text            create__10dThunder_cFv */
int dThunder_c::create() {
    dScnKy_env_light_c* env_light = dKy_getEnvlight();
    camera_class* camera_p = dComIfGp_getCamera(0);
    cXyz spB8;
    cXyz spC4;
    cXyz spD0;
    f32 var_f31;
    f32 var_f30;
    f32 var_f29;
    f32 var_f29_2;
    f32 var_r28;
    f32 var_r27;

    new (this) dThunder_c();

    J3DModelData* modelData;
    if (cM_rndF(1.0f) < 0.5f) {
        modelData = (J3DModelData*)dComIfG_getObjectRes("Always", EF_THUNDER01_BMD);
    } else {
        modelData = (J3DModelData*)dComIfG_getObjectRes("Always", EF_THUNDER02_BMD);
    }

    mModelInfo.mpModel = mDoExt_J3DModel__create(modelData, 0x80000, 0x1000000);
    if (mModelInfo.mpModel == NULL) {
        return cPhs_ERROR_e;
    }

    J3DAnmTevRegKey* canm = (J3DAnmTevRegKey*)dComIfG_getObjectRes("Always", EF_THUNDER_BRK);
    if (!mModelInfo.mBrk.init(modelData, canm, TRUE, 0, 1.0f, 0, -1)) {
        return cPhs_ERROR_e;
    }

    mBrkSpeed = 1.0f;
    mModelInfo.mBrk.setPlaySpeed(mBrkSpeed);

    if (env_light->mThunderEff.mStateTimer < 10) {
        var_f29 = 1.0f;
    } else {
        var_f29 = 0.25f;
    }

    field_0x168 = cLib_targetAngleY(&camera_p->mLookat.mEye, &camera_p->mLookat.mCenter);

    if (!(g_Counter.mCounter0 & 1)) {
        field_0x168 += 0x7FFF;
    }

    var_r28 = 14.0f;
    var_r27 = 20.0f;
    var_f31 = var_r28;
    var_f30 = var_r27;
    spC4.set(60000.0f, -5000.0f, 0.0f);
    field_0x15c = 2000.0f;

    if (env_light->mThunderEff.mStateTimer >= 10) {
        var_f31 = var_r27;
        var_f30 = 24.0f;
        field_0x15c = 2500.0f;
        spC4.y = 6000.0f;
    }

    if (!strcmp(dComIfGp_getStartStageName(), "R_SP30") && dComIfGp_roomControl_getStayNo() == 4) {
        var_r28 = 0.1f;
        var_r27 = 0.25f;
        var_f31 = 0.15f;
        var_f30 = 0.2f;
    }

    field_0x15c = var_f29 * cM_rndFX(field_0x15c);
    field_0x138.x = var_f29 * (var_r28 + cM_rndF(var_r27 - var_r28));

    if (cM_rndFX(1.0f) >= 0.5) {
        field_0x138.x *= -1.0f;
    }

    field_0x138.y = var_f29 * (var_f31 + cM_rndF(var_f30 - var_f31));
    field_0x138.z = 1.0f;

    dKyr_get_vectle_calc(&camera_p->mLookat.mEye, &camera_p->mLookat.mCenter, &spB8);

    // JMAFastSqrt doesn't match here when it should, probably needs to be fixed
    // var_f29_2 = JMAFastSqrt(spB8.x * spB8.x + spB8.z * spB8.z);

    var_f29_2 = spB8.x * spB8.x + spB8.z * spB8.z;
    if (var_f29_2 > 0.0f) {
        f32 tmp = __frsqrtes(var_f29_2);
        var_f29_2 = tmp * var_f29_2;
    }

    s16 temp_r28 = cM_atan2s(spB8.x, spB8.z);
    s16 temp_r31 = cM_atan2s(spB8.y, var_f29_2);

    if (cM_rndFX(1.0f) >= 0.0f) {
        temp_r28 += 0x4000;
    } else {
        temp_r28 -= 0x4000;
    }

    spD0.x = cM_scos(temp_r31) * cM_ssin(temp_r28);
    spD0.z = cM_scos(temp_r31) * cM_scos(temp_r28);

    f32 temp_f1_3 = cM_rndF(spC4.x);
    mThunderPos.x = camera_p->mLookat.mEye.x + (spB8.x * 100000.0f) + (spD0.x * temp_f1_3);
    mThunderPos.y = camera_p->mLookat.mEye.y + 2000.0f + cM_rndF(spC4.y);
    mThunderPos.z = camera_p->mLookat.mEye.z + (spB8.z * 100000.0f) + (spD0.z * temp_f1_3);

    if (!strcmp(dComIfGp_getStartStageName(), "R_SP30") && dComIfGp_roomControl_getStayNo() == 4) {
        mThunderPos.x = cM_rndFX(260.0f) + 19635.0f;
        mThunderPos.y = 4592.0f;
        mThunderPos.z = cM_rndFX(100.0f) + 1507.0f;
    }

    if (cM_rndF(1.0f) < 0.3f) {
        field_0x150.x = -mThunderPos.x;
        field_0x150.y = -mThunderPos.y;
        field_0x150.z = -mThunderPos.z;
        mDoAud_mEnvSe_startFarThunderSe(&field_0x150);
    }

    return cPhs_COMPLEATE_e;
}

/* ############################################################################################## */
/* 803BC1D8-803BC1EC -00001 0014+00 1/0 0/0 0/0 .data            l_dThunder_Method */
static leafdraw_method_class l_dThunder_Method = {
    (process_method_func)dThunder_Create,  (process_method_func)dThunder_Delete,
    (process_method_func)dThunder_Execute, (process_method_func)dThunder_IsDelete,
    (process_method_func)dThunder_Draw,
};

/* 803BC1EC-803BC218 -00001 0028+04 0/0 0/0 1/0 .data            g_profile_KY_THUNDER */
extern kankyo_process_profile_definition g_profile_KY_THUNDER = {
    fpcLy_CURRENT_e,
    7,
    fpcPi_CURRENT_e,
    PROC_KY_THUNDER,
    &g_fpcLf_Method.mBase,
    sizeof(dThunder_c),
    0,
    0,
    &g_fopKy_Method,
    6,
    &l_dThunder_Method,
};