Subversion Repositories Games.Carmageddon

Rev

Rev 20 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
#include "crush.h"
20 pmbaty 2
#include "brender/brender.h"
1 pmbaty 3
#include "car.h"
4
#include "displays.h"
5
#include "globvars.h"
6
#include "globvrkm.h"
7
#include "globvrpb.h"
8
#include "graphics.h"
9
#include "harness/trace.h"
10
#include "loading.h"
11
#include "mainloop.h"
12
#include "netgame.h"
13
#include "network.h"
14
#include "oil.h"
15
#include "opponent.h"
16
#include "pd/sys.h"
17
#include "pedestrn.h"
18
#include "piping.h"
19
#include "pratcam.h"
20
#include "raycast.h"
21
#include "replay.h"
22
#include "spark.h"
23
#include "structur.h"
24
#include "utility.h"
25
#include "world.h"
26
#include <stdlib.h>
27
 
28
float gWobble_spam_y[8] = { 0.0f, -0.15f, 0.4f, 0.15f, -0.4f, 0.25f, 0.0f, -0.25f };
29
float gWobble_spam_z[8] = { 0.4f, -0.25f, 0.0f, 0.25f, 0.0f, 0.15f, -0.4f, -0.15f };
30
br_scalar gWheel_circ_to_width = 0.16f;
31
tU8 gSmoke_damage_step[12] = { 20u, 20u, 0u, 10u, 10u, 10u, 10u, 10u, 10u, 10u, 10u, 10u };
15 pmbaty 32
int gSteal_ranks[5] = { 89, 72, 55, 38, 21 };
1 pmbaty 33
 
15 pmbaty 34
#define BIGAPC_OPPONENT_INDEX 4
35
 
1 pmbaty 36
// IDA: int __usercall ReadCrushData@<EAX>(FILE *pF@<EAX>, tCrush_data *pCrush_data@<EDX>)
37
int ReadCrushData(FILE* pF, tCrush_data* pCrush_data) {
38
    //char s[256]; // Pierre-Marie Baty -- unused variable
39
    //char* str; // Pierre-Marie Baty -- unused variable
40
    int i;
41
    int j;
42
    //int k; // Pierre-Marie Baty -- unused variable
43
    tCrush_point_spec* the_spec;
44
    tCrush_neighbour* the_neighbour;
45
    LOG_TRACE("(%p, %p)", pF, pCrush_data);
46
 
47
    pCrush_data->softness_factor = GetAFloat(pF);
48
    GetPairOfFloats(pF, &pCrush_data->min_fold_factor, &pCrush_data->max_fold_factor);
49
    pCrush_data->wibble_factor = GetAFloat(pF);
50
    pCrush_data->limit_deviant = GetAFloat(pF);
51
    pCrush_data->split_chance = GetAFloat(pF);
52
    pCrush_data->min_y_fold_down = GetAFloat(pF);
53
    pCrush_data->number_of_crush_points = GetAnInt(pF);
54
    pCrush_data->crush_points = (tCrush_point_spec*)BrMemAllocate(sizeof(tCrush_point_spec) * pCrush_data->number_of_crush_points, kMem_crush_data);
55
 
56
    for (i = 0, the_spec = pCrush_data->crush_points; i < pCrush_data->number_of_crush_points; i++, the_spec++) {
57
        the_spec->vertex_index = GetAnInt(pF);
58
        GetThreeFloats(pF, &the_spec->limits_neg.v[0], &the_spec->limits_neg.v[1], &the_spec->limits_neg.v[2]);
59
        GetThreeFloats(pF, &the_spec->limits_pos.v[0], &the_spec->limits_pos.v[1], &the_spec->limits_pos.v[2]);
60
        GetThreeFloats(pF, &the_spec->softness_neg.v[0], &the_spec->softness_neg.v[1], &the_spec->softness_neg.v[2]);
61
        GetThreeFloats(pF, &the_spec->softness_pos.v[0], &the_spec->softness_pos.v[1], &the_spec->softness_pos.v[2]);
62
        the_spec->number_of_neighbours = GetAnInt(pF);
63
        the_spec->neighbours = BrMemAllocate(sizeof(tCrush_neighbour) * the_spec->number_of_neighbours, kMem_crush_neighbours);
64
 
65
        for (j = 0, the_neighbour = the_spec->neighbours; j < the_spec->number_of_neighbours; j++, the_neighbour++) {
66
            the_neighbour->vertex_index = GetAnInt(pF);
67
            the_neighbour->factor = GetAnInt(pF);
68
        }
69
    }
70
    return 0;
71
}
72
 
73
// IDA: float __usercall SkipCrushData@<ST0>(FILE *pF@<EAX>)
74
float SkipCrushData(FILE* pF) {
75
    int i;
76
    int j;
77
    int count_1;
78
    int count_2;
79
    char s[256];
80
    float softness;
81
    LOG_TRACE("(%p)", pF);
82
 
83
    softness = GetAFloat(pF);
84
    for (i = 0; i < 5; ++i) {
85
        GetALineAndDontArgue(pF, s);
86
    }
87
    count_1 = GetAnInt(pF);
88
    for (i = 0; i < count_1; i++) {
89
        for (j = 0; j < 5; j++) {
90
            GetALineAndDontArgue(pF, s);
91
        }
92
        count_2 = GetAnInt(pF);
93
        for (j = 0; j < 2 * count_2; j++) {
94
            GetALineAndDontArgue(pF, s);
95
        }
96
    }
97
    return softness;
98
}
99
 
100
// IDA: int __usercall WriteCrushData@<EAX>(FILE *pF@<EAX>, tCrush_data *pCrush_data@<EDX>)
101
int WriteCrushData(FILE* pF, tCrush_data* pCrush_data) {
102
    int i;
103
    int j;
104
    //int k; // Pierre-Marie Baty -- unused variable
105
    tCrush_point_spec* the_spec;
106
    tCrush_neighbour* the_neighbour;
107
    LOG_TRACE("(%p, %p)", pF, pCrush_data);
108
 
109
    fprintf(pF, "%f\n\r", pCrush_data->softness_factor);
110
    fprintf(pF, "%f,%f\n\r", pCrush_data->min_fold_factor, pCrush_data->max_fold_factor);
111
    fprintf(pF, "%f\n\r", pCrush_data->wibble_factor);
112
    fprintf(pF, "%f\n\r", pCrush_data->limit_deviant);
113
    fprintf(pF, "%f\n\r", pCrush_data->split_chance);
114
    fprintf(pF, "%f\n\r", pCrush_data->min_y_fold_down);
115
    fprintf(pF, "%d\n\r", pCrush_data->number_of_crush_points);
116
    for (i = 0, the_spec = pCrush_data->crush_points; i < pCrush_data->number_of_crush_points; i++, the_spec++) {
117
        fprintf(pF, "%d\n\r", the_spec->vertex_index);
118
        fprintf(pF, "%f, %f, %f\n\r", the_spec->limits_neg.v[0], the_spec->limits_neg.v[1], the_spec->limits_neg.v[2]);
119
        fprintf(pF, "%f, %f, %f\n\r", the_spec->limits_pos.v[0], the_spec->limits_pos.v[1], the_spec->limits_pos.v[2]);
120
        fprintf(pF, "%f, %f, %f\n\r", the_spec->softness_neg.v[0], the_spec->softness_neg.v[1], the_spec->softness_neg.v[2]);
121
        fprintf(pF, "%f, %f, %f\n\r", the_spec->softness_pos.v[0], the_spec->softness_pos.v[1], the_spec->softness_pos.v[2]);
122
        fprintf(pF, "%d\n\r", the_spec->number_of_neighbours);
123
        for (j = 0, the_neighbour = the_spec->neighbours; j < the_spec->number_of_neighbours; j++, the_neighbour++) {
124
            fprintf(pF, "%d\n\r", the_neighbour->vertex_index);
125
            fprintf(pF, "%d\n\r", the_neighbour->factor);
126
        }
127
    }
128
    return 0;
129
}
130
 
131
// IDA: void __usercall DisposeCrushData(tCrush_data *pCrush_data@<EAX>)
132
void DisposeCrushData(tCrush_data* pCrush_data) {
133
    int i;
134
    LOG_TRACE("(%p)", pCrush_data);
135
 
136
    for (i = 0; i < pCrush_data->number_of_crush_points; i++) {
137
        if (pCrush_data->crush_points[i].neighbours != NULL) {
138
            BrMemFree(pCrush_data->crush_points[i].neighbours);
139
        }
140
    }
141
    if (pCrush_data->crush_points != NULL) {
142
        BrMemFree(pCrush_data->crush_points);
143
    }
144
}
145
 
146
// IDA: void __usercall CrushModelPoint(tCar_spec *pCar@<EAX>, int pModel_index@<EDX>, br_model *pModel@<EBX>, int pCrush_point_index@<ECX>, br_vector3 *pEnergy_vector, br_scalar total_energy, tCrush_data *pCrush_data)
147
void CrushModelPoint(tCar_spec* pCar, int pModel_index, br_model* pModel, int pCrush_point_index, br_vector3* pEnergy_vector, br_scalar total_energy, tCrush_data* pCrush_data) {
148
    int i;
149
    int j;
150
    //int k; // Pierre-Marie Baty -- unused variable
151
    int pipe_vertex_count;
152
    int neighbour_index;
153
    int bend_axis;
154
    int default_bend_axis[3];
155
    tCrush_point_spec* the_crush_point;
156
    tCrush_neighbour* the_neighbour;
157
    br_vector3* target_point;
158
    br_vector3 old_vector;
159
    br_vector3 softnesss;
160
    br_vector3 movement;
161
    br_scalar random_range;
162
    br_scalar bend_amount;
163
    br_scalar min_y_fold_down;
164
    float default_bend_factor[3];
165
    float working_min_fold;
166
    float working_max_fold;
167
    float working_wibble;
168
    float working_limit_deviant;
169
    float working_split_chance;
170
    tChanged_vertex pipe_array[600];
171
    //tCar_spec* car; // Pierre-Marie Baty -- unused variable
172
    LOG_TRACE("(%p, %d, %p, %d, %p, %f, %p)", pCar, pModel_index, pModel, pCrush_point_index, pEnergy_vector, total_energy, pCrush_data);
173
 
174
    pipe_vertex_count = 0;
175
    if (gNet_mode == eNet_mode_host && pCar->car_model_actors[pModel_index].min_distance_squared == 0.0f) {
176
        NetSendPointCrush(pCar, pCrush_point_index, pEnergy_vector);
177
    }
178
    working_min_fold = pCrush_data->min_fold_factor * gCar_crush_min_fold;
179
    working_max_fold = pCrush_data->max_fold_factor * gCar_crush_max_fold;
180
    working_wibble = pCrush_data->wibble_factor * gCar_crush_wibble;
181
    working_limit_deviant = pCrush_data->limit_deviant * gCar_crush_limit_deviant;
182
    working_split_chance = pCrush_data->split_chance * gCar_crush_split_chance;
183
    min_y_fold_down = pCrush_data->min_y_fold_down;
184
    the_crush_point = &pCrush_data->crush_points[pCrush_point_index];
185
    if (pModel->nvertices <= the_crush_point->vertex_index) {
186
        return;
187
    }
188
    target_point = &pModel->vertices[the_crush_point->vertex_index].p;
189
    old_vector = *target_point;
190
    for (i = 0; i < 3; i++) {
191
        pEnergy_vector->v[i] = SRandomPosNeg(working_wibble * total_energy) + pEnergy_vector->v[i];
192
        random_range = (the_crush_point->limits_pos.v[i] - the_crush_point->limits_neg.v[i]) * working_limit_deviant;
193
        if (pEnergy_vector->v[i] >= 0.0f) {
194
            softnesss.v[i] = the_crush_point->softness_pos.v[i];
195
        } else {
196
            softnesss.v[i] = the_crush_point->softness_neg.v[i];
197
        }
198
        movement.v[i] = target_point->v[i];
199
        target_point->v[i] += pEnergy_vector->v[i] * softnesss.v[i];
200
        if (the_crush_point->limits_neg.v[i] <= target_point->v[i]) {
201
            if (target_point->v[i] > the_crush_point->limits_pos.v[i]) {
202
                target_point->v[i] = SRandomPosNeg(random_range) + the_crush_point->limits_pos.v[i];
203
            }
204
        } else {
205
            target_point->v[i] = SRandomPosNeg(random_range) + the_crush_point->limits_neg.v[i];
206
        }
207
        movement.v[i] = target_point->v[i] - movement.v[i];
208
        if (pEnergy_vector->v[i] * movement.v[i] < 0.0f) {
209
            movement.v[i] = 0.0f;
210
            target_point->v[i] = old_vector.v[i];
211
        }
212
    }
213
 
214
    if (IsActionReplayAvailable()) {
215
        pipe_array[pipe_vertex_count].vertex_index = the_crush_point->vertex_index;
216
        BrVector3Sub(&pipe_array[pipe_vertex_count].delta_coordinates, target_point, &old_vector);
217
        pipe_vertex_count++;
218
    }
219
    neighbour_index = -1;
220
    for (bend_axis = 0; bend_axis < 3; bend_axis++) {
221
        default_bend_axis[bend_axis] = (bend_axis + IRandomBetween(1, 2)) % 3;
222
        default_bend_factor[bend_axis] = FRandomBetween(working_min_fold, working_max_fold);
223
    }
224
 
225
    the_neighbour = the_crush_point->neighbours;
226
    for (j = 0; j < the_crush_point->number_of_neighbours; j++, the_neighbour++) {
227
        if (the_neighbour->vertex_index) {
228
            neighbour_index += the_neighbour->vertex_index;
229
            if (neighbour_index < 0 || pModel->nvertices <= neighbour_index) {
230
                return;
231
            }
232
            target_point = &pModel->vertices[neighbour_index].p;
233
            old_vector = *target_point;
234
            for (bend_axis = 0; bend_axis < 3; bend_axis++) {
235
                target_point->v[bend_axis] += (1.0f - the_neighbour->factor / 256.0f) * movement.v[bend_axis];
236
                float v12;
237
                if (the_neighbour->factor <= 128) {
238
                    v12 = the_neighbour->factor / 128.0f;
239
                } else {
240
                    v12 = 2.0 - the_neighbour->factor / 128.0f;
241
                }
242
                if (((int)((target_point->v[2] + target_point->v[1] + target_point->v[0]) * 63.0f) & 1) * v12 == 0.0) {
243
                    bend_amount = -default_bend_factor[bend_axis];
244
                } else {
245
                    bend_amount = default_bend_factor[bend_axis];
246
                }
247
                int axis_tmp = (((int)((target_point->v[2] + target_point->v[1] + target_point->v[0]) * 100.0f) + bend_axis - 1) & 1) % 3;
248
                target_point->v[axis_tmp] += fabs(movement.v[bend_axis]) * bend_amount;
249
            }
250
            if (IsActionReplayAvailable() && pipe_vertex_count < 600) {
251
                pipe_array[pipe_vertex_count].vertex_index = neighbour_index;
252
                BrVector3Sub(&pipe_array[pipe_vertex_count].delta_coordinates, target_point, &old_vector);
253
                pipe_vertex_count++;
254
            }
255
        } else {
256
            neighbour_index += the_neighbour->factor;
257
        }
258
    }
259
    if (IsActionReplayAvailable() && pipe_vertex_count) {
260
        PipeSingleModelGeometry(pCar->car_ID, pModel_index, pipe_vertex_count, pipe_array);
261
    }
262
}
263
 
264
// IDA: void __usercall CrushModel(tCar_spec *pCar@<EAX>, int pModel_index@<EDX>, br_actor *pActor@<EBX>, br_vector3 *pImpact_point@<ECX>, br_vector3 *pEnergy_vector, tCrush_data *pCrush_data)
265
void CrushModel(tCar_spec* pCar, int pModel_index, br_actor* pActor, br_vector3* pImpact_point, br_vector3* pEnergy_vector, tCrush_data* pCrush_data) {
266
    br_scalar this_distance;
267
    br_scalar total_energy;
268
    br_scalar nearest_so_far;
269
    br_vector3 impact_point_model;
270
    br_vector3 energy_vector_scaled;
271
    br_vector3 energy_vector_model;
272
    int i;
273
    int nearest_index;
274
    br_vertex* vertices;
275
    br_vertex* the_vertex;
276
    //br_matrix34 inverse_transform; // Pierre-Marie Baty -- unused variable
277
    LOG_TRACE("(%p, %d, %p, %p, %p, %p)", pCar, pModel_index, pActor, pImpact_point, pEnergy_vector, pCrush_data);
278
 
279
    if (gArrow_mode) {
280
        return;
281
    }
282
    if (pCrush_data->number_of_crush_points == 0) {
283
        return;
284
    }
285
    BrVector3Sub(&impact_point_model, pImpact_point, (br_vector3*)pActor->t.t.mat.m[3]);
286
    BrVector3Scale(&energy_vector_model, pEnergy_vector, pCrush_data->softness_factor * gCar_crush_softness);
287
    total_energy = BrVector3Length(&energy_vector_model);
288
    if (total_energy < 0.06f) {
289
        return;
290
    }
291
    BrVector3Scale(&energy_vector_scaled, &energy_vector_model, (total_energy - 0.06f) / total_energy);
292
    nearest_so_far = BR_SCALAR_MAX;
293
    vertices = pActor->model->vertices;
294
    nearest_index = -1;
295
    for (i = 0; i < pCrush_data->number_of_crush_points; i++) {
296
        the_vertex = &vertices[pCrush_data->crush_points[i].vertex_index];
297
        this_distance = (impact_point_model.v[2] - the_vertex->p.v[2]) * (impact_point_model.v[2] - the_vertex->p.v[2]) + (impact_point_model.v[1] - the_vertex->p.v[1]) * (impact_point_model.v[1] - the_vertex->p.v[1]) + (impact_point_model.v[0] - the_vertex->p.v[0]) * (impact_point_model.v[0] - the_vertex->p.v[0]);
298
        if (this_distance < nearest_so_far) {
299
            nearest_so_far = this_distance;
300
            nearest_index = i;
301
        }
302
    }
303
    if (nearest_index >= 0) {
304
        CrushModelPoint(pCar, pModel_index, pActor->model, nearest_index, &energy_vector_scaled, total_energy, pCrush_data);
305
        SetModelForUpdate(pActor->model, pCar, 1);
306
    }
307
}
308
 
309
// IDA: void __cdecl JitModelUpdate(br_actor *actor, br_model *model, br_material *material, void *render_data, br_uint_8 style, int on_screen)
310
void JitModelUpdate(br_actor* actor, br_model* model, br_material* material, void* render_data, br_uint_8 style, int on_screen) {
311
    LOG_TRACE("(%p, %p, %p, %p, %d, %d)", actor, model, material, render_data, style, on_screen);
312
 
313
    BrModelUpdate(model, BR_MODU_VERTEX_POSITIONS);
314
    model->flags &= ~(BR_MODF_CUSTOM);
315
    BrZbModelRender(actor, model, material, style, BrOnScreenCheck(&model->bounds), 0);
316
}
317
 
318
// IDA: void __usercall SetModelForUpdate(br_model *pModel@<EAX>, tCar_spec *pCar@<EDX>, int crush_only@<EBX>)
319
void SetModelForUpdate(br_model* pModel, tCar_spec* pCar, int crush_only) {
320
    LOG_TRACE("(%p, %p, %d)", pModel, pCar, crush_only);
321
 
322
    if (crush_only && pCar != NULL && pCar->car_model_actors[pCar->principal_car_actor].actor->model == pModel) {
323
        CrushBoundingBox(pCar, crush_only);
324
    }
325
    if ((pModel->flags & BR_MODF_CUSTOM) != 0) {
326
        pModel->user = JitModelUpdate;
327
    } else {
328
        pModel->custom = JitModelUpdate;
329
        pModel->flags |= BR_MODF_CUSTOM;
330
    }
331
}
332
 
333
// IDA: void __usercall TotallySpamTheModel(tCar_spec *pCar@<EAX>, int pModel_index@<EDX>, br_actor *pActor@<EBX>, tCrush_data *pCrush_data@<ECX>, br_scalar pMagnitude)
334
void TotallySpamTheModel(tCar_spec* pCar, int pModel_index, br_actor* pActor, tCrush_data* pCrush_data, br_scalar pMagnitude) {
335
    //br_scalar total_energy; // Pierre-Marie Baty -- unused variable
336
    br_vector3 energy_vector_model;
337
    int i;
338
    int the_index;
339
    br_vertex* the_vertex;
340
    //br_vertex* vertices; // Pierre-Marie Baty -- unused variable
341
    LOG_TRACE("(%p, %d, %p, %p, %f)", pCar, pModel_index, pActor, pCrush_data, pMagnitude);
342
 
343
    if (gArrow_mode || pCrush_data->number_of_crush_points == 0) {
344
        return;
345
    }
346
    the_vertex = pActor->model->vertices;
347
    for (i = 0; i < 15; i++) {
348
        the_index = IRandomBetween(0, pCrush_data->number_of_crush_points - 1);
349
        energy_vector_model = the_vertex[pCrush_data->crush_points[the_index].vertex_index].p;
350
        BrVector3Normalise(&energy_vector_model, &energy_vector_model);
351
        BrVector3Scale(&energy_vector_model, &energy_vector_model, -pMagnitude);
352
        CrushModelPoint(pCar, pModel_index, pActor->model, the_index, &energy_vector_model, pMagnitude, pCrush_data);
353
    }
354
    SetModelForUpdate(pActor->model, pCar, 1);
355
}
356
 
357
// IDA: br_scalar __usercall RepairModel@<ST0>(tCar_spec *pCar@<EAX>, int pModel_index@<EDX>, br_actor *pActor@<EBX>, br_vertex *pUndamaged_vertices@<ECX>, br_scalar pAmount, br_scalar *pTotal_deflection)
358
br_scalar RepairModel(tCar_spec* pCar, int pModel_index, br_actor* pActor, br_vertex* pUndamaged_vertices, br_scalar pAmount, br_scalar* pTotal_deflection) {
359
    int i;
360
    int j;
361
    int pipe_vertex_count;
362
    br_vector3 old_point;
363
    br_vertex* model_vertex;
364
    br_scalar amount;
365
    //br_scalar deviation; // Pierre-Marie Baty -- unused variable
366
    tChanged_vertex pipe_array[600];
367
    LOG_TRACE("(%p, %d, %p, %p, %f, %p)", pCar, pModel_index, pActor, pUndamaged_vertices, pAmount, pTotal_deflection);
368
 
369
    pipe_vertex_count = 0;
370
    amount = 0.0f;
371
    *pTotal_deflection = 0.0f;
372
 
373
    for (i = 0; i < pActor->model->nvertices; i++) {
374
        model_vertex = &pActor->model->vertices[i];
375
        old_point = model_vertex->p;
376
        for (j = 0; j < 3; ++j) {
377
            *pTotal_deflection = fabsf(pUndamaged_vertices->p.v[j] - old_point.v[j]) + *pTotal_deflection;
378
            if (pUndamaged_vertices->p.v[j] >= old_point.v[j]) {
379
                if (pUndamaged_vertices->p.v[j] > old_point.v[j]) {
380
                    model_vertex->p.v[j] = model_vertex->p.v[j] + pAmount;
381
                    if (pUndamaged_vertices->p.v[j] < model_vertex->p.v[j]) {
382
                        model_vertex->p.v[j] = pUndamaged_vertices->p.v[j];
383
                    }
384
                    amount = model_vertex->p.v[j] - old_point.v[j] + amount;
385
                }
386
            } else {
387
                model_vertex->p.v[j] = model_vertex->p.v[j] - pAmount;
388
                if (pUndamaged_vertices->p.v[j] > model_vertex->p.v[j]) {
389
                    model_vertex->p.v[j] = pUndamaged_vertices->p.v[j];
390
                }
391
                amount = old_point.v[j] - model_vertex->p.v[j] + amount;
392
            }
393
        }
394
        if (amount != 0.0 && IsActionReplayAvailable() && pipe_vertex_count < COUNT_OF(pipe_array)) {
395
            pipe_array[pipe_vertex_count].vertex_index = i;
396
            BrVector3Sub(&pipe_array[pipe_vertex_count].delta_coordinates, &model_vertex->p, &old_point);
397
            pipe_vertex_count++;
398
        }
399
        pUndamaged_vertices++;
400
    }
401
    SetModelForUpdate(pActor->model, pCar, 0);
402
    if (IsActionReplayAvailable() && pipe_vertex_count) {
403
        PipeSingleModelGeometry(pCar->car_ID, pModel_index, pipe_vertex_count, pipe_array);
404
    }
405
    return amount;
406
}
407
 
408
// IDA: float __usercall RepairCar2@<ST0>(tCar_spec *pCar@<EAX>, tU32 pFrame_period@<EDX>, br_scalar *pTotal_deflection@<EBX>)
409
float RepairCar2(tCar_spec* pCar, tU32 pFrame_period, br_scalar* pTotal_deflection) {
410
    int i;
411
    tCar_actor* the_car_actor;
412
    br_scalar amount;
413
    br_scalar dummy;
414
    LOG_TRACE("(%p, %d, %p)", pCar, pFrame_period, pTotal_deflection);
415
 
416
    if (gArrow_mode) {
417
        return 0.0f;
418
    }
419
    *pTotal_deflection = 0.0;
420
    amount = 0.0;
421
 
422
    for (i = 0; i < gProgram_state.current_car.car_actor_count; i++) {
423
        the_car_actor = &pCar->car_model_actors[i];
424
        if (the_car_actor->min_distance_squared == 0.0 || !the_car_actor->undamaged_vertices) {
425
            if (the_car_actor->undamaged_vertices) {
426
                amount = RepairModel(pCar, i, the_car_actor->actor, the_car_actor->undamaged_vertices, pFrame_period * 0.00005f, pTotal_deflection);
427
            }
428
        } else {
429
            RepairModel(pCar, i, the_car_actor->actor, the_car_actor->undamaged_vertices, pFrame_period * 0.00005f, &dummy);
430
        }
431
    }
432
    pCar->repair_time += pFrame_period;
433
    return amount;
434
}
435
 
436
// IDA: float __usercall RepairCar@<ST0>(tU16 pCar_ID@<EAX>, tU32 pFrame_period@<EDX>, br_scalar *pTotal_deflection@<EBX>)
437
float RepairCar(tU16 pCar_ID, tU32 pFrame_period, br_scalar* pTotal_deflection) {
438
    LOG_TRACE("(%d, %d, %p)", pCar_ID, pFrame_period, pTotal_deflection);
439
 
440
    if (VEHICLE_TYPE_FROM_ID(pCar_ID) == eVehicle_self) {
441
        return RepairCar2(&gProgram_state.current_car, pFrame_period, pTotal_deflection);
442
    }
443
 
444
    return RepairCar2(GetCarSpec(VEHICLE_TYPE_FROM_ID(pCar_ID), VEHICLE_INDEX_FROM_ID(pCar_ID)), pFrame_period, pTotal_deflection);
445
}
446
 
447
// IDA: void __usercall TotallyRepairACar(tCar_spec *pCar@<EAX>)
448
void TotallyRepairACar(tCar_spec* pCar) {
449
    int i;
450
    int j;
451
    int k;
452
    int pipe_vertex_count;
453
    tCar_actor* the_car_actor;
454
    tChanged_vertex pipe_array[600];
455
    br_bounds storage_bounds;
456
    LOG_TRACE("(%p)", pCar);
457
 
458
    StopCarSmokingInstantly(pCar);
459
    if (IsActionReplayAvailable()) {
460
        PipeInstantUnSmudge(pCar);
461
    }
462
    pCar->repair_time += 100000;
463
    for (i = 0; i < COUNT_OF(pCar->damage_units); i++) {
464
        pCar->damage_units[i].damage_level = 0;
465
        pCar->damage_units[i].last_level = 0;
466
        pCar->damage_units[i].smoke_last_level = 0;
467
    }
468
    memcpy(&storage_bounds, &pCar->bounds[1], sizeof(br_bounds));
469
    memcpy(&pCar->bounds[1], &pCar->max_bounds[1], sizeof(br_bounds));
470
    if (TestForCarInSensiblePlace(pCar)) {
471
        for (j = 0; j < pCar->car_actor_count; j++) {
472
            the_car_actor = &pCar->car_model_actors[j];
473
            if (the_car_actor->undamaged_vertices != NULL) {
474
                pipe_vertex_count = 0;
475
                for (k = 0; k < the_car_actor->actor->model->nvertices; k++) {
476
                    if (pipe_vertex_count < COUNT_OF(pipe_array)) {
477
                        BrVector3Sub(&pipe_array[pipe_vertex_count].delta_coordinates,
478
                            &the_car_actor->undamaged_vertices[k].p, &the_car_actor->actor->model->vertices[k].p);
479
                        if (!Vector3IsZero(&pipe_array[pipe_vertex_count].delta_coordinates)) {
480
                            pipe_array[pipe_vertex_count].vertex_index = k;
481
                            pipe_vertex_count++;
482
                        }
483
                    }
484
                }
485
                memcpy(the_car_actor->actor->model->vertices,
486
                    the_car_actor->undamaged_vertices,
487
                    the_car_actor->actor->model->nvertices * sizeof(br_vertex));
20 pmbaty 488
                // FIXME: BrModelUpdate(..., BR_MODU_VERTEX_COLOURS | BR_MODU_VERTEX_POSITIONS) fails on TELL_ME_IF_WE_PASS_THIS_WAY
489
                // BrModelUpdate(the_car_actor->actor->model, BR_MODU_VERTEX_COLOURS | BR_MODU_VERTEX_POSITIONS);
490
                BrModelUpdate(the_car_actor->actor->model, BR_MODU_ALL);
1 pmbaty 491
                if (pipe_vertex_count != 0 && IsActionReplayAvailable()) {
492
                    PipeSingleModelGeometry(pCar->car_ID, j, pipe_vertex_count, pipe_array);
493
                }
494
            }
495
        }
496
    } else {
497
        memcpy(&pCar->bounds[1], &storage_bounds, sizeof(br_bounds));
498
    }
499
}
500
 
501
// IDA: void __cdecl TotallyRepairCar()
502
void TotallyRepairCar(void) {
503
    LOG_TRACE("()");
504
 
505
    if (!gArrow_mode) {
506
        TotallyRepairACar(&gProgram_state.current_car);
507
    }
508
}
509
 
510
// IDA: void __cdecl CheckLastCar()
511
void CheckLastCar(void) {
512
    LOG_TRACE("()");
513
 
514
    if (gNet_mode == eNet_mode_none && GetCarCount(eVehicle_opponent) != 0 && NumberOfOpponentsLeft() == 0) {
21 pmbaty 515
        NewTextHeadupSlot(eHeadupSlot_misc, 0, 5000, -4, GetMiscString(kMiscString_EveryOpponentWasted));
1 pmbaty 516
        RaceCompleted(eRace_over_opponents);
517
    }
518
}
519
 
520
// IDA: void __usercall KnackerThisCar(tCar_spec *pCar@<EAX>)
521
void KnackerThisCar(tCar_spec* pCar) {
522
    LOG_TRACE("(%p)", pCar);
523
 
524
    pCar->knackered = 1;
525
    QueueWastedMassage(pCar->index);
526
    CheckLastCar();
527
    QueueOilSpill(pCar);
528
    if (gNet_mode == eNet_mode_none) {
529
        KillGroovadelic(pCar->index);
530
        KillFunkotronic(pCar->index);
531
    }
532
}
533
 
534
// IDA: void __usercall SetKnackeredFlag(tCar_spec *pCar@<EAX>)
535
void SetKnackeredFlag(tCar_spec* pCar) {
536
    LOG_TRACE("(%p)", pCar);
537
 
538
    if (gNet_mode != eNet_mode_client
539
        && !pCar->knackered
540
        && (pCar->damage_units[eDamage_engine].damage_level >= 99
541
            || pCar->damage_units[eDamage_transmission].damage_level >= 99
542
            || pCar->damage_units[eDamage_driver].damage_level >= 99
543
            || (pCar->damage_units[eDamage_lf_wheel].damage_level >= 99
544
                && pCar->damage_units[eDamage_rf_wheel].damage_level >= 99
545
                && pCar->damage_units[eDamage_lr_wheel].damage_level >= 99
546
                && pCar->damage_units[eDamage_rr_wheel].damage_level >= 99))) {
547
        KnackerThisCar(pCar);
548
        if (gNet_mode == eNet_mode_none) {
549
            if (IRandomBetween(0, 1)) {
550
                if (gNet_mode == eNet_mode_none) {
551
                    StopCarSmoking(pCar);
552
                    CreateSmokeColumn(pCar, 0, IRandomBetween(0, 11), 20000);
553
                }
554
            }
555
            CreateSmokeColumn(pCar, 0, IRandomBetween(0, 11), 180000);
556
        }
557
    }
558
}
559
 
560
// IDA: void __usercall DamageUnit2(tCar_spec *pCar@<EAX>, int pUnit_type@<EDX>, int pDamage_amount@<EBX>)
561
void DamageUnit2(tCar_spec* pCar, int pUnit_type, int pDamage_amount) {
562
    tDamage_unit* the_damage;
563
    int last_level;
564
    LOG_TRACE("(%p, %d, %d)", pCar, pUnit_type, pDamage_amount);
565
 
566
    the_damage = &pCar->damage_units[pUnit_type];
567
    if ((pCar->driver < eDriver_net_human || pUnit_type != eDamage_driver) && pDamage_amount >= 5 && !pCar->invulnerable) {
568
        last_level = the_damage->damage_level;
569
        the_damage->damage_level = pDamage_amount + last_level;
570
        if (the_damage->damage_level >= 99) {
571
            if (pDamage_amount >= 10) {
572
                the_damage->damage_level = 99;
573
            } else {
574
                the_damage->damage_level = last_level;
575
            }
576
        }
577
        if (pCar->driver == eDriver_oppo || gNet_mode != eNet_mode_none) {
578
            SetKnackeredFlag(pCar);
579
        } else if ((pCar->damage_units[eDamage_engine].damage_level >= 99 && pCar->damage_units[eDamage_engine].last_level < 99 && pCar->damage_units[eDamage_transmission].last_level < 99)
580
            || (pCar->damage_units[eDamage_transmission].damage_level >= 99 && pCar->damage_units[eDamage_engine].last_level < 99 && pCar->damage_units[eDamage_transmission].last_level < 99)) {
581
            QueueOilSpill(pCar);
582
        }
583
    }
584
}
585
 
586
// IDA: void __usercall RecordLastDamage(tCar_spec *pCar@<EAX>)
587
void RecordLastDamage(tCar_spec* pCar) {
588
    int i;
589
    LOG_TRACE("(%p)", pCar);
590
 
591
    for (i = 0; i < COUNT_OF(pCar->damage_units); i++) {
592
        pCar->damage_units[i].last_level = pCar->damage_units[i].damage_level;
593
    }
594
    pCar->damage_magnitude_accumulator = 0.0;
595
    pCar->last_impact_location = eImpact_unknown;
596
    pCar->pre_car_col_mat = pCar->car_master_actor->t.t.mat;
597
    pCar->pre_car_col_speed = pCar->speed;
598
    pCar->pre_car_col_knackered = pCar->knackered;
599
    pCar->pre_car_col_direction = pCar->direction;
600
    pCar->pre_car_col_velocity = pCar->v;
601
    pCar->pre_car_col_velocity_car_space = pCar->velocity_car_space;
602
}
603
 
604
// IDA: void __usercall DoDamage(tCar_spec *pCar@<EAX>, tDamage_type pDamage_type@<EDX>, float pMagnitude, float pNastiness)
605
void DoDamage(tCar_spec* pCar, tDamage_type pDamage_type, float pMagnitude, float pNastiness) {
606
    LOG_TRACE("(%p, %d, %f, %f)", pCar, pDamage_type, pMagnitude, pNastiness);
607
 
608
    if (pCar->driver < eDriver_net_human) {
609
        DamageUnit2(pCar, pDamage_type, ((gCurrent_race.suggested_rank < 10 ? 0.5f : gCurrent_race.suggested_rank) / 20.0f + 1.0f) * (pNastiness * pMagnitude * 10.0f));
610
    } else if (gNet_mode != eNet_mode_none) {
611
        DamageUnit2(pCar, pDamage_type, pNastiness * pMagnitude * 15.0f);
612
    } else if (PercentageChance(pNastiness * pMagnitude * 1500.0f)) {
613
        DamageUnit2(pCar, pDamage_type, pNastiness * pMagnitude * 30.0f);
614
    }
615
}
616
 
617
// IDA: void __usercall CheckPiledriverBonus(tCar_spec *pCar@<EAX>, br_vector3 *pImpact_point@<EDX>, br_vector3 *pEnergy@<EBX>)
618
void CheckPiledriverBonus(tCar_spec* pCar, br_vector3* pImpact_point, br_vector3* pEnergy) {
619
    br_actor* child;
620
    br_vector3 norm_impact;
621
    br_vector3 norm_child;
622
    br_vector3 norm_energy;
623
    //br_scalar dp; // Pierre-Marie Baty -- unused variable
624
    LOG_TRACE("(%p, %p, %p)", pCar, pImpact_point, pEnergy);
625
 
626
    if (pCar->current_car_actor < 0) {
627
        return;
628
    }
629
 
630
    BrVector3Normalise(&norm_impact, pImpact_point);
631
    norm_impact.v[1] = 0.f;
632
    BrVector3Normalise(&norm_energy, pEnergy);
633
 
634
    for (child = pCar->car_master_actor->children; child != NULL; child = child->next) {
635
        if (ActorIsPedestrian(child) && PedestrianActorIsPerson(child) && pCar->speed > 0.001f) {
636
            BrVector3Normalise(&norm_child, &child->t.t.translate.t);
637
            norm_child.v[1] = 0.f;
638
            if (BrVector3Dot(&norm_child, &norm_impact) > 0.8f && BrVector3Dot(&norm_energy, &norm_child) < -.65) {
639
                DoFancyHeadup(kFancyHeadupPileDriverBonus);
640
                EarnCredits(((GetPedestrianValue(child) / 2 + 12) / 25) * 25);
641
                return;
642
            }
643
        }
644
    }
645
}
646
 
647
// IDA: tImpact_location __usercall CalcModifiedLocation@<EAX>(tCar_spec *pCar@<EAX>)
648
tImpact_location CalcModifiedLocation(tCar_spec* pCar) {
649
    LOG_TRACE("(%p)", pCar);
650
 
651
    if (pCar->last_impact_location != eImpact_left && pCar->last_impact_location != eImpact_right && pCar->last_impact_location != eImpact_top && pCar->last_impact_location != eImpact_bottom) {
652
        return pCar->last_impact_location;
653
    }
654
    if (pCar->last_col_prop_z < 0.25f) {
655
        return eImpact_front;
656
    }
657
    if (pCar->last_col_prop_z > 0.75f) {
658
        return eImpact_back;
659
    } else {
660
        return pCar->last_impact_location;
661
    }
662
}
663
 
664
// IDA: void __usercall DoPratcamHit(br_vector3 *pHit_vector@<EAX>)
665
void DoPratcamHit(br_vector3* pHit_vector) {
20 pmbaty 666
    //int strength_modifier; // Pierre-Marie Baty -- unused variable
667
    //br_scalar strength; // Pierre-Marie Baty -- unused variable
1 pmbaty 668
    LOG_TRACE("(%p)", pHit_vector);
669
 
20 pmbaty 670
    STUB_ONCE();
1 pmbaty 671
}
672
 
673
// IDA: void __usercall DamageSystems(tCar_spec *pCar@<EAX>, br_vector3 *pImpact_point@<EDX>, br_vector3 *pEnergy_vector@<EBX>, int pWas_hitting_a_car@<ECX>)
674
void DamageSystems(tCar_spec* pCar, br_vector3* pImpact_point, br_vector3* pEnergy_vector, int pWas_hitting_a_car) {
675
    int i;
676
    int j;
677
    int result;
678
    br_bounds crushed_car_bounds;
679
    float proportion_x;
680
    float proportion_y;
681
    float proportion_z;
682
    float energy_magnitude;
683
    float pure_energy_magnitude;
684
    br_scalar x;
685
    br_scalar y;
686
    br_scalar z;
687
    br_scalar x1;
688
    br_scalar x2;
689
    br_scalar y1;
690
    br_scalar y2;
691
    br_scalar z1;
692
    br_scalar z2;
693
    //br_scalar distance; // Pierre-Marie Baty -- unused variable
694
    tImpact_location impact_location;
695
    tDamage_program* the_program;
696
    tDamage_clause* the_clause;
697
    tDamage_condition* the_condition;
698
    tDamage_effect* the_effect;
699
    //tImpact_location modified_location; // Pierre-Marie Baty -- unused variable
700
    LOG_TRACE("(%p, %p, %p, %d)", pCar, pImpact_point, pEnergy_vector, pWas_hitting_a_car);
701
 
702
#if defined(DETHRACE_FIX_BUGS)
703
    proportion_x = 0;
704
    proportion_y = 0;
705
    proportion_z = 0;
706
#endif
707
 
708
    pure_energy_magnitude = BrVector3Length(pEnergy_vector);
709
    if (pure_energy_magnitude == 0.0f && !pWas_hitting_a_car) {
710
        return;
711
    }
712
 
713
    energy_magnitude = pCar->car_model_actors[pCar->principal_car_actor].crush_data.softness_factor * pure_energy_magnitude / 0.7f;
714
    BrVector3InvScale(&crushed_car_bounds.min, &pCar->bounds[1].min, WORLD_SCALE);
715
    BrVector3InvScale(&crushed_car_bounds.max, &pCar->bounds[1].max, WORLD_SCALE);
716
 
717
    x1 = pImpact_point->v[0] - crushed_car_bounds.min.v[0];
718
    x2 = crushed_car_bounds.max.v[0] - pImpact_point->v[0];
719
    if (x1 >= x2) {
720
        x = x2;
721
    } else {
722
        x = x1;
723
    }
724
    y1 = pImpact_point->v[1] - crushed_car_bounds.min.v[1];
725
    y2 = crushed_car_bounds.max.v[1] - pImpact_point->v[1];
726
    if (y1 >= y2) {
727
        y = y2;
728
    } else {
729
        y = y1;
730
    }
731
    z1 = pImpact_point->v[2] - crushed_car_bounds.min.v[2];
732
    z2 = crushed_car_bounds.max.v[2] - pImpact_point->v[2];
733
    if (z1 >= z2) {
734
        z = z2;
735
    } else {
736
        z = z1;
737
    }
738
    if (z > x || z > y) {
739
        if (x > y || x > z) {
740
            impact_location = y1 < y2 ? eImpact_bottom : eImpact_top;
741
            proportion_z = z1 / (crushed_car_bounds.max.v[2] - crushed_car_bounds.min.v[2]);
742
            proportion_x = x1 / (crushed_car_bounds.max.v[0] - crushed_car_bounds.min.v[0]);
743
        } else {
744
            impact_location = x1 >= x2 ? eImpact_right : eImpact_left;
745
            proportion_z = z1 / (crushed_car_bounds.max.v[2] - crushed_car_bounds.min.v[2]);
746
            proportion_y = y1 / (crushed_car_bounds.max.v[1] - crushed_car_bounds.min.v[1]);
747
        }
748
    } else {
749
        impact_location = z1 >= z2 ? eImpact_back : eImpact_front;
750
        proportion_x = x1 / (crushed_car_bounds.max.v[0] - crushed_car_bounds.min.v[0]);
751
        proportion_y = y1 / (crushed_car_bounds.max.v[1] - crushed_car_bounds.min.v[1]);
752
    }
753
    if (pWas_hitting_a_car && pCar->last_impact_location == eImpact_unknown) {
754
        pCar->last_impact_location = impact_location;
755
        pCar->last_col_prop_x = proportion_x;
756
        pCar->last_col_prop_y = proportion_y;
757
        pCar->last_col_prop_z = proportion_z;
758
    }
759
 
760
    if (energy_magnitude != 0.0f && !pCar->invulnerable) {
761
        if (!pWas_hitting_a_car && impact_location == eImpact_bottom) {
762
            energy_magnitude = energy_magnitude / 2.0f;
763
        }
764
 
765
        the_program = &pCar->damage_programs[impact_location];
766
        the_clause = the_program->clauses;
767
        for (i = 0; i < the_program->clause_count; i++) {
768
            result = 1;
769
            the_condition = the_clause->conditions;
770
            for (j = 0; j < the_clause->condition_count; j++) {
771
                switch (the_condition->axis_comp) {
772
                case eAxis_x:
773
                    if (the_condition->condition_operator == eCondition_greater_than) {
774
                        if (the_condition->comparitor >= proportion_x) {
775
                            result = 0;
776
                        }
777
                    } else if (the_condition->comparitor <= proportion_x) {
778
                        result = 0;
779
                    }
780
                    break;
781
 
782
                case eAxis_y:
783
                    if (the_condition->condition_operator == eCondition_greater_than) {
784
                        if (the_condition->comparitor >= proportion_y) {
785
                            result = 0;
786
                        }
787
                    } else if (the_condition->comparitor <= proportion_y) {
788
                        result = 0;
789
                    }
790
                    break;
791
 
792
                case eAxis_z:
793
                    if (the_condition->condition_operator == eCondition_greater_than) {
794
                        if (the_condition->comparitor >= proportion_z) {
795
                            result = 0;
796
                        }
797
                    } else if (the_condition->comparitor <= proportion_z) {
798
                        result = 0;
799
                    }
800
                    break;
801
                }
802
 
803
                if (!result) {
804
                    break;
805
                }
806
                the_condition++;
807
            }
808
            if (result) {
809
                for (j = 0; j < the_clause->effect_count; j++) {
810
                    the_effect = &the_clause->effects[j];
811
                    DoDamage(pCar, the_effect->type, energy_magnitude, the_effect->weakness_factor);
812
                }
813
            }
814
            the_clause++;
815
        }
816
        if (pCar->driver == eDriver_local_human) {
817
            switch (impact_location) {
818
            case eImpact_top:
819
            case eImpact_bottom:
820
                NewScreenWobble(
821
                    FRandomBetween(energy_magnitude * 5.0f, energy_magnitude * 20.0f),
822
                    FRandomBetween(energy_magnitude * 30.0f, energy_magnitude * 60.0f),
823
                    FRandomBetween(1.0f / energy_magnitude, 5.0f / energy_magnitude));
824
                break;
825
            case eImpact_left:
826
                NewScreenWobble(
827
                    FRandomBetween(energy_magnitude * 50.0f, energy_magnitude * 100.0f),
828
                    FRandomBetween(energy_magnitude * 5.0f, energy_magnitude * 20.0f),
829
                    FRandomBetween(4.0f / energy_magnitude, 7.0 / energy_magnitude));
830
                break;
831
            case eImpact_right:
832
                NewScreenWobble(
833
                    -FRandomBetween(energy_magnitude * 50.0f, energy_magnitude * 100.0f),
834
                    FRandomBetween(energy_magnitude * 5.0f, energy_magnitude * 20.0f),
835
                    FRandomBetween(4.0f / energy_magnitude, 7.0f / energy_magnitude));
836
 
837
                break;
838
            case eImpact_front:
839
                NewScreenWobble(
840
                    FRandomBetween(energy_magnitude * 5.0f, energy_magnitude * 20.0f),
841
                    FRandomBetween(energy_magnitude * 50.0f, energy_magnitude * 150.0f),
842
                    FRandomBetween(7.0f / energy_magnitude, 25.0f / energy_magnitude));
843
                break;
844
            case eImpact_back:
845
                NewScreenWobble(
846
                    FRandomBetween(energy_magnitude * 5.0f, energy_magnitude * 20.0f),
847
                    FRandomBetween(-energy_magnitude * 50.0f, energy_magnitude * 150.0f),
848
                    FRandomBetween(7.0f / energy_magnitude, 25.0f / energy_magnitude));
849
                break;
850
            default:
851
                break;
852
            }
853
            CheckPiledriverBonus(pCar, pImpact_point, pEnergy_vector);
854
        }
855
    }
856
}
857
 
858
// IDA: tImpact_location __usercall GetDirection@<EAX>(br_vector3 *pVelocity@<EAX>)
859
tImpact_location GetDirection(br_vector3* pVelocity) {
860
    br_scalar mag_x;
861
    br_scalar mag_y;
862
    br_scalar mag_z;
863
    LOG_TRACE("(%p)", pVelocity);
864
 
865
    mag_x = fabsf(pVelocity->v[0]);
866
    mag_y = fabsf(pVelocity->v[1]);
867
    mag_z = fabsf(pVelocity->v[2]);
868
    if (mag_y >= mag_x || mag_z >= mag_x) {
869
        if (mag_y <= mag_x || mag_z >= mag_y) {
870
            if (pVelocity->v[2] >= 0.0f) {
871
                return eImpact_back;
872
            } else {
873
                return eImpact_front;
874
            }
875
        } else {
876
            return pVelocity->v[1] < 0.0;
877
        }
878
    } else if (pVelocity->v[0] >= 0.0) {
879
        return eImpact_right;
880
    } else {
881
        return eImpact_left;
882
    }
883
}
884
 
885
// IDA: void __usercall SetSmokeLastDamageLevel(tCar_spec *pCar@<EAX>)
886
void SetSmokeLastDamageLevel(tCar_spec* pCar) {
887
    int i;
888
    LOG_TRACE("(%p)", pCar);
889
 
890
    for (i = 0; i < COUNT_OF(pCar->damage_units); i++) {
891
        pCar->damage_units[i].smoke_last_level = pCar->damage_units[i].damage_level;
892
    }
893
}
894
 
895
// IDA: void __usercall SortOutSmoke(tCar_spec *pCar@<EAX>)
896
void SortOutSmoke(tCar_spec* pCar) {
897
    int i;
898
    int colour;
899
    int old_colour;
900
    int step;
901
    //int pass; // Pierre-Marie Baty -- unused variable
902
    //int repeat; // Pierre-Marie Baty -- unused variable
903
    LOG_TRACE("(%p)", pCar);
904
 
905
    if (!pCar || pCar->driver <= eDriver_non_car) {
906
        return;
907
    }
908
    for (i = 0; i < COUNT_OF(pCar->damage_units); i++) {
909
        if (pCar->damage_units[i].damage_level != pCar->damage_units[i].smoke_last_level) {
910
            step = gSmoke_damage_step[i];
911
            if (step) {
912
                if (pCar->damage_units[i].damage_level > pCar->damage_units[i].smoke_last_level) {
913
                    old_colour = (99 - pCar->damage_units[i].smoke_last_level) / step;
914
                    colour = (99 - pCar->damage_units[i].damage_level) / step;
915
                    if (old_colour != colour && colour <= 2) {
916
                        ConditionalSmokeColumn(pCar, i, colour);
917
                    }
918
                }
919
            }
920
        }
921
    }
922
    SetSmokeLastDamageLevel(pCar);
923
}
924
 
925
// IDA: void __usercall StealCar(tCar_spec *pCar@<EAX>)
926
void StealCar(tCar_spec* pCar) {
927
    LOG_TRACE("(%p)", pCar);
928
 
929
    pCar->has_been_stolen = 1;
930
    gProgram_state.cars_available[gProgram_state.number_of_cars] = pCar->index;
931
    gProgram_state.number_of_cars++;
932
    gOpponents[pCar->index].dead = 1;
933
}
934
 
935
// IDA: int __usercall DoCrashEarnings@<EAX>(tCar_spec *pCar1@<EAX>, tCar_spec *pCar2@<EDX>)
936
int DoCrashEarnings(tCar_spec* pCar1, tCar_spec* pCar2) {
937
    tCar_spec* culprit;
938
    tCar_spec* victim;
939
    int i;
940
    int net_loop;
941
    int head_on;
942
    int bonus_level;
943
    int credits;
944
    int impact_in_moving_direction_1;
945
    int impact_in_moving_direction_2;
946
    int car_off_ground_1;
947
    int car_off_ground_2;
948
    int total_units_of_damage;
949
    int inherited_damage;
950
    int dam_acc_1;
951
    int dam_acc_2;
952
    int car_1_culpable;
953
    int car_2_culpable;
954
    int mutual_culpability;
955
    tU32 the_time;
956
    tU32 time;
957
    float credits_squared;
958
    static tU32 last_earn_time;
959
    //char s[256]; // Pierre-Marie Baty -- unused variable
960
    tImpact_location modified_location_1;
961
    tImpact_location modified_location_2;
962
    tImpact_location car_direction_1;
963
    tImpact_location car_direction_2;
964
    br_scalar car_1_height;
965
    br_scalar car_2_height;
966
    br_scalar dp;
967
    br_vector3 car_1_pos;
968
    br_vector3 car_2_pos;
969
    br_vector3 car_1_offset;
970
    br_vector3 car_2_offset;
971
    tNet_message* message;
972
    LOG_TRACE("(%p, %p)", pCar1, pCar2);
973
 
974
    culprit = 0;
975
    victim = 0;
976
    head_on = 0;
977
    bonus_level = 1;
978
    car_1_culpable = 0;
979
    car_2_culpable = 0;
980
    mutual_culpability = 0;
981
    the_time = PDGetTotalTime();
982
    inherited_damage = 0;
983
#if defined(DETHRACE_FIX_BUGS)
984
    total_units_of_damage = 0;
985
#endif
986
    if (pCar1->driver <= eDriver_non_car) {
987
        dam_acc_1 = 0;
988
    } else {
989
        dam_acc_1 = pCar1->damage_magnitude_accumulator;
990
    }
991
 
992
    dam_acc_2 = 0;
993
    if (pCar2 != NULL) {
994
        if (pCar2->driver <= eDriver_non_car) {
995
            dam_acc_2 = 0;
996
        } else {
997
            dam_acc_2 = pCar2->damage_magnitude_accumulator != 0;
998
        }
999
    }
1000
 
1001
    if (pCar1->driver <= eDriver_non_car) {
1002
        if (pCar2 == NULL || pCar2->driver <= eDriver_non_car) {
1003
            return 0;
1004
        }
1005
        pCar1 = pCar2;
1006
        pCar2 = NULL;
1007
    }
1008
    if (pCar2 != NULL && pCar2->driver <= eDriver_non_car) {
1009
        pCar2 = NULL;
1010
    }
1011
 
1012
    if (pCar1->pre_car_col_knackered || (pCar2 && pCar2->pre_car_col_knackered) || (pCar2 && pCar2->damage_magnitude_accumulator <= 0.00005f && pCar1->damage_magnitude_accumulator <= 0.00005f)) {
1013
        return dam_acc_1 || (pCar2 && dam_acc_2);
1014
    }
1015
 
1016
    modified_location_1 = CalcModifiedLocation(pCar1);
1017
    car_direction_1 = GetDirection(&pCar1->pre_car_col_velocity_car_space);
1018
    impact_in_moving_direction_1 = car_direction_1 == modified_location_1;
1019
    if (pCar2 != NULL) {
1020
        modified_location_2 = CalcModifiedLocation(pCar2);
1021
        car_direction_2 = GetDirection(&pCar2->pre_car_col_velocity_car_space);
1022
        impact_in_moving_direction_2 = car_direction_2 == modified_location_2;
1023
    }
1024
    if (pCar1->driver >= eDriver_net_human && pCar2) {
1025
        if (impact_in_moving_direction_1 && (pCar1->driver < eDriver_net_human || (pCar1->pre_car_col_velocity_car_space.v[2] != 0.0 && (pCar1->pre_car_col_velocity_car_space.v[2] > 0.0) != (pCar1->gear > 0) && (pCar1->keys.acc != 0 || pCar1->joystick.acc > 0x8000)))) {
1026
            pCar2->time_last_hit = the_time;
1027
            pCar2->last_hit_by = pCar1;
1028
        }
1029
    } else if (pCar2 && pCar2->driver >= eDriver_net_human && impact_in_moving_direction_2 && (pCar2->driver < eDriver_net_human || (pCar2->pre_car_col_velocity_car_space.v[2] != 0.0f && (pCar2->pre_car_col_velocity_car_space.v[2] > 0.0f) != (pCar2->gear > 0) && (pCar2->keys.acc != 0 || pCar2->joystick.acc > 0x8000)))) {
1030
        pCar1->time_last_hit = the_time;
1031
        pCar1->last_hit_by = pCar2;
1032
    }
1033
    if (pCar2) {
1034
        if (impact_in_moving_direction_1
1035
            && pCar1->pre_car_col_speed * 5.0f > pCar2->pre_car_col_speed
1036
            && pCar1->pre_car_col_speed > 0.0005f
1037
            && (pCar1->driver < eDriver_net_human
1038
                || (pCar1->pre_car_col_velocity_car_space.v[2] != 0.0f
1039
                    && (pCar1->pre_car_col_velocity_car_space.v[2] > 0.0f) != (pCar1->gear > 0)
1040
                    && (pCar1->keys.acc != 0 || pCar1->joystick.acc > 0x8000)))) {
1041
            car_1_culpable = 1;
1042
        }
1043
        if (impact_in_moving_direction_2
1044
            && pCar2->pre_car_col_speed * 5.0f > pCar1->pre_car_col_speed
1045
            && pCar2->pre_car_col_speed > 0.0005f
1046
            && (pCar2->driver < eDriver_net_human
1047
                || (pCar2->pre_car_col_velocity_car_space.v[2] != 0.0f
1048
                    && (pCar2->pre_car_col_velocity_car_space.v[2] > 0.0f) != (pCar2->gear > 0)
1049
                    && (pCar2->keys.acc != 0 || pCar2->joystick.acc > 0x8000)))) {
1050
            car_2_culpable = 1;
1051
        }
1052
        if (gNet_mode && car_1_culpable && car_2_culpable) {
1053
            mutual_culpability = 1;
1054
        } else {
1055
            if (car_2_culpable && pCar2->driver == eDriver_local_human) {
1056
                car_1_culpable = 0;
1057
            }
1058
            if (car_1_culpable) {
1059
                culprit = pCar1;
1060
                victim = pCar2;
1061
                dp = BrVector3Dot(&pCar1->pre_car_col_direction, &pCar2->pre_car_col_direction);
1062
                if (modified_location_1 == eImpact_front && modified_location_2 == eImpact_front && pCar1->pre_car_col_speed > 0.001f && pCar2->pre_car_col_speed > 0.001f && dp < -0.7f) {
1063
                    head_on = 1;
1064
                    bonus_level = 2;
1065
                } else {
1066
                    bonus_level = 1;
1067
                }
1068
            } else if (car_2_culpable) {
1069
                culprit = pCar2;
1070
                victim = pCar1;
1071
                dp = BrVector3Dot(&pCar1->pre_car_col_direction, &pCar2->pre_car_col_direction);
1072
                if (modified_location_1 == eImpact_front && modified_location_2 == eImpact_front && pCar1->pre_car_col_speed > 0.001f && pCar2->pre_car_col_speed > 0.001f && dp < -0.7f) {
1073
                    head_on = 1;
1074
                    bonus_level = 2;
1075
                } else {
1076
                    bonus_level = 1;
1077
                }
1078
            }
1079
        }
1080
    } else {
1081
        if (the_time - pCar1->time_last_hit >= 3000) {
1082
            return 1;
1083
        }
1084
        culprit = pCar1->last_hit_by;
1085
        victim = pCar1;
1086
        bonus_level = 1;
1087
        inherited_damage = 1;
1088
    }
1089
    if (!mutual_culpability && (!victim || culprit->driver < eDriver_net_human)) {
1090
        if (pCar2 && pCar2->last_culprit == pCar1 && the_time - pCar2->time_last_victim < 750) {
1091
            inherited_damage = 1;
1092
            culprit = pCar1;
1093
            victim = pCar2;
1094
        } else if (pCar2 && pCar1->last_culprit == pCar2 && the_time - pCar1->time_last_victim < 750) {
1095
            inherited_damage = 1;
1096
            culprit = pCar2;
1097
            victim = pCar1;
1098
        } else if (!pCar2 && the_time - pCar1->time_last_victim < 750) {
1099
            inherited_damage = 1;
1100
            culprit = pCar1->last_culprit;
1101
            victim = pCar1;
1102
        }
1103
    }
1104
    if (culprit && victim) {
1105
        RecordOpponentTwattageOccurrence(culprit, victim);
1106
        total_units_of_damage = 0;
1107
        for (i = 0; i < COUNT_OF(victim->damage_units); i++) {
1108
            if (victim->damage_units[i].damage_level > victim->damage_units[i].last_level) {
1109
                victim->damage_units[i].damage_level = (victim->damage_units[i].damage_level - victim->damage_units[i].last_level) * 2.0f + victim->damage_units[i].last_level;
1110
                if (victim->damage_units[i].damage_level >= 99) {
1111
                    victim->damage_units[i].damage_level = 99;
1112
                }
1113
                total_units_of_damage += victim->damage_units[i].damage_level - victim->damage_units[i].last_level;
1114
            }
1115
            if (culprit->damage_units[i].damage_level > culprit->damage_units[i].last_level) {
1116
                culprit->damage_units[i].damage_level = (culprit->damage_units[i].damage_level - culprit->damage_units[i].last_level) * 0.1f + (double)culprit->damage_units[i].last_level;
1117
                if (culprit->damage_units[i].damage_level < 0) {
1118
                    culprit->damage_units[i].damage_level = 0;
1119
                }
1120
            }
1121
        }
1122
    }
1123
    // TODO: tidy this up
1124
    for (net_loop = 0; 2 - (mutual_culpability == 0) > net_loop; net_loop++) {
1125
        if (mutual_culpability) {
1126
            if (net_loop) {
1127
                culprit = pCar1;
1128
                victim = pCar2;
1129
            } else {
1130
                culprit = pCar2;
1131
                victim = pCar1;
1132
            }
1133
            total_units_of_damage = 0;
1134
            for (i = 0; i < COUNT_OF(victim->damage_units); i++) {
1135
                if (victim->damage_units[i].damage_level > victim->damage_units[i].last_level) {
1136
                    total_units_of_damage += victim->damage_units[i].damage_level - victim->damage_units[i].last_level;
1137
                }
1138
            }
1139
        }
1140
        if (culprit && (culprit->driver == eDriver_local_human || gNet_mode) && victim) {
1141
            SetKnackeredFlag(victim);
1142
            if (victim->knackered && !victim->pre_car_col_knackered) {
1143
                victim->pre_car_col_knackered = 1;
1144
                credits_squared = sqr(0.7f / victim->car_model_actors[victim->principal_car_actor].crush_data.softness_factor) * gWasted_creds[gProgram_state.skill_level] + 50.0f;
1145
                credits = 100 * (int)(credits_squared / 100.0f);
1146
                if (gNet_mode) {
1147
                    message = NetBuildMessage(0x18u, 0);
1148
                    message->contents.data.wasted.victim = NetPlayerFromCar(victim)->ID;
1149
                    if (NetPlayerFromCar(culprit)) {
1150
                        message->contents.data.wasted.culprit = NetPlayerFromCar(culprit)->ID;
1151
                    } else {
1152
                        message->contents.data.wasted.culprit = -2;
1153
                    }
1154
                    NetGuaranteedSendMessageToEverybody(gCurrent_net_game, message, NULL);
1155
                    NetEarnCredits(NetPlayerFromCar(culprit), credits);
1156
                } else {
1157
                    PratcamEvent(32);
20 pmbaty 1158
                    DoFancyHeadup(11);
1 pmbaty 1159
                    credits_squared = sqr(0.7f / victim->car_model_actors[victim->principal_car_actor].crush_data.softness_factor) * gWasted_creds[gProgram_state.skill_level] + 50.0f;
1160
                    credits = 100 * (int)(credits_squared / 100.0);
1161
                    AwardTime(gWasted_time[gProgram_state.skill_level]);
1162
                    EarnCredits(credits);
15 pmbaty 1163
                    if (victim->can_be_stolen && !gOpponents[victim->index].dead
1164
                        // strength_rating is between 1 and 5
1165
                        && ((PercentageChance(50) && gProgram_state.rank <= gSteal_ranks[gOpponents[victim->index].strength_rating - 1]) || victim->index == BIGAPC_OPPONENT_INDEX)) {
1 pmbaty 1166
                        StealCar(victim);
1167
                    }
1168
                }
1169
            }
1170
            victim->time_last_hit = the_time;
1171
            victim->last_hit_by = culprit;
1172
            if (!inherited_damage) {
1173
                victim->time_last_victim = the_time;
1174
                victim->last_culprit = culprit;
1175
            }
1176
            if (victim && (fabs(victim->omega.v[0]) > 4.0f || fabs(victim->omega.v[1]) > 6.0f || fabs(victim->omega.v[2]) > 4.0f)) {
1177
                bonus_level *= 2;
1178
            }
1179
            if (pCar1->number_of_wheels_on_ground) {
1180
                car_off_ground_1 = 0;
1181
            } else {
1182
                BrVector3InvScale(&car_1_pos, &pCar1->car_master_actor->t.t.translate.t, WORLD_SCALE);
1183
                BrMatrix34ApplyV(&car_1_offset, &pCar1->car_model_actors[pCar1->principal_car_actor].actor->t.t.translate.t, &pCar1->car_master_actor->t.t.mat);
1184
                BrVector3Accumulate(&car_1_pos, &car_1_offset);
1185
                car_1_pos.v[1] += 0.15f;
1186
                car_1_height = FindYVerticallyBelow2(&car_1_pos);
1187
                car_off_ground_1 = car_1_height > -100.0f
1188
                    && pCar1->car_model_actors[pCar1->principal_car_actor].actor->t.t.translate.t.v[1] * 4.0f <= car_1_pos.v[1] - car_1_height - 0.15f;
1189
            }
1190
            if (!pCar2 || pCar2->number_of_wheels_on_ground) {
1191
                car_off_ground_2 = 0;
1192
            } else {
1193
                BrVector3InvScale(&car_2_pos, &pCar2->car_master_actor->t.t.translate.t, WORLD_SCALE);
1194
                BrMatrix34ApplyV(&car_2_offset, &pCar2->car_model_actors[pCar2->principal_car_actor].actor->t.t.translate.t, &pCar2->car_master_actor->t.t.mat);
1195
                BrVector3Accumulate(&car_2_pos, &car_2_offset);
1196
                car_2_pos.v[1] += 0.15f;
1197
                car_2_height = FindYVerticallyBelow2(&car_2_pos);
1198
                car_off_ground_2 = car_2_height > -100.0f
1199
                    && pCar2->car_model_actors[pCar2->principal_car_actor].actor->t.t.translate.t.v[1] * 4.0f <= car_2_pos.v[1] - car_2_height - 0.15f;
1200
            }
1201
            if (car_off_ground_1) {
1202
                bonus_level *= 2;
1203
            }
1204
            if (car_off_ground_2) {
1205
                bonus_level *= 2;
1206
            }
1207
            total_units_of_damage = 0.7f / victim->car_model_actors[victim->principal_car_actor].crush_data.softness_factor * total_units_of_damage;
1208
            if (!victim->has_been_stolen) {
1209
                credits = 100 * (int)((gCar_cred_value[gProgram_state.skill_level] * MIN(bonus_level, 8) * total_units_of_damage + 50.0f) / 100.0f);
1210
                if (credits || victim->knackered) {
1211
                    if (!victim->knackered) {
1212
                        if (gNet_mode) {
1213
                            NetEarnCredits(NetPlayerFromCar(culprit), MIN(credits, 2000));
1214
                        } else {
1215
                            EarnCredits(MIN(credits, 2000));
1216
                        }
1217
                        last_earn_time = the_time;
1218
                        if (gNet_mode == eNet_mode_none) {
1219
                            time = 5 * (int)((total_units_of_damage * gCar_time_value[gProgram_state.skill_level] + 2.5f) / 5.0f);
1220
                            AwardTime(MIN(time, 90));
1221
                            if (pCar2) {
1222
                                if (head_on) {
20 pmbaty 1223
                                    DoFancyHeadup(10);
1 pmbaty 1224
                                } else if (bonus_level <= 2) {
1225
                                    if (bonus_level > 1) {
20 pmbaty 1226
                                        DoFancyHeadup(2);
1 pmbaty 1227
                                    }
1228
                                } else {
20 pmbaty 1229
                                    DoFancyHeadup(3);
1 pmbaty 1230
                                }
1231
                            }
1232
                        }
1233
                    }
1234
                    for (i = 0; i < COUNT_OF(victim->damage_units); i++) {
1235
                        victim->damage_units[i].last_level = victim->damage_units[i].damage_level;
1236
                    }
1237
                }
1238
            }
1239
        } else {
1240
            pCar1->time_last_hit = 0;
1241
            if (pCar2) {
1242
                pCar2->time_last_hit = 0;
1243
            }
1244
        }
1245
    }
1246
    pCar1->damage_magnitude_accumulator = 0.0f;
1247
    if (pCar2) {
1248
        pCar2->damage_magnitude_accumulator = 0.0f;
1249
    }
1250
    return 1;
1251
}
1252
 
1253
// IDA: void __usercall DoWheelDamage(tU32 pFrame_period@<EAX>)
1254
void DoWheelDamage(tU32 pFrame_period) {
1255
    int i;
1256
    int j;
1257
    int damage;
1258
    tCar_spec* car;
1259
    br_scalar y_amount;
1260
    br_scalar z_amount;
1261
    br_scalar wheel_circum;
1262
    br_scalar old_offset;
1263
    br_vector3 temp_vector;
1264
    br_vector3 wonky_vector;
1265
    static int kev_index[4];
1266
    LOG_TRACE("(%d)", pFrame_period);
1267
 
1268
    if (gAction_replay_mode && ReplayIsPaused()) {
1269
        return;
1270
    }
1271
 
1272
    for (i = 0; i < gNum_active_cars; i++) {
1273
        car = gActive_car_list[i];
1274
        for (j = 0; j < COUNT_OF(car->wheel_dam_offset); j++) {
1275
            if (car->wheel_actors[j] == NULL) {
1276
                continue;
1277
            }
1278
            old_offset = car->wheel_dam_offset[j];
1279
            damage = car->damage_units[j + 8].damage_level;
1280
            if (damage <= 30 || gRace_finished) {
1281
                car->wheel_dam_offset[j] = 0.0f;
1282
                continue;
1283
            }
1284
            if (PointOutOfSight(&car->pos, 32.0f)) {
1285
                break;
1286
            }
1287
            y_amount = (damage - 30) * gWobble_spam_y[damage & 7];
1288
            z_amount = (damage - 30) * gWobble_spam_z[damage & 7];
1289
            BrMatrix34PreRotateY(&car->wheel_actors[j]->t.t.mat, BrDegreeToAngle(y_amount < 0 ? y_amount + 360.f : y_amount));
1290
            BrMatrix34PreRotateZ(&car->wheel_actors[j]->t.t.mat, BrDegreeToAngle(z_amount < 0 ? z_amount + 360.f : z_amount));
1291
            if (j < 2 && car->wheel_actors[j + 4] != NULL) {
1292
                BrMatrix34PreRotateY(&car->wheel_actors[j + 4]->t.t.mat, BrDegreeToAngle(y_amount < 0 ? y_amount + 360.f : y_amount));
1293
                BrMatrix34PreRotateZ(&car->wheel_actors[j + 4]->t.t.mat, BrDegreeToAngle(z_amount < 0 ? z_amount + 360.f : z_amount));
1294
            }
1295
            switch (j) {
1296
            case 0:
1297
                if (car->driven_wheels_spin_ref_1 < 0) {
1298
                    wheel_circum = car->non_driven_wheels_circum;
1299
                } else {
1300
                    wheel_circum = car->driven_wheels_circum;
1301
                }
1302
                break;
1303
            case 1:
1304
                if (car->driven_wheels_spin_ref_2 < 0) {
1305
                    wheel_circum = car->non_driven_wheels_circum;
1306
                } else {
1307
                    wheel_circum = car->driven_wheels_circum;
1308
                }
1309
                break;
1310
            case 2:
1311
                if (car->driven_wheels_spin_ref_3 < 0) {
1312
                    wheel_circum = car->non_driven_wheels_circum;
1313
                } else {
1314
                    wheel_circum = car->driven_wheels_circum;
1315
                }
1316
                break;
1317
            case 3:
1318
                if (car->driven_wheels_spin_ref_4 < 0) {
1319
                    wheel_circum = car->non_driven_wheels_circum;
1320
                } else {
1321
                    wheel_circum = car->driven_wheels_circum;
1322
                }
1323
                break;
1324
            default:
1325
                TELL_ME_IF_WE_PASS_THIS_WAY();
1326
                break;
1327
            }
1328
            if (gNet_mode == eNet_mode_none || car->driver == eDriver_local_human) {
1329
                BrVector3Set(&temp_vector, wheel_circum * gWheel_circ_to_width, 0.f, 0.f);
1330
                BrMatrix34ApplyV(&wonky_vector, &temp_vector, &car->wheel_actors[j]->t.t.mat);
1331
                car->wheel_dam_offset[j] = fabsf(wonky_vector.v[1]);
1332
            }
1333
        }
1334
    }
1335
}
1336
 
1337
// IDA: void __usercall CrashEarnings(tCar_spec *pCar1@<EAX>, tCar_spec *pCar2@<EDX>)
1338
void CrashEarnings(tCar_spec* pCar1, tCar_spec* pCar2) {
1339
    LOG_TRACE("(%p, %p)", pCar1, pCar2);
1340
 
1341
    if (DoCrashEarnings(pCar1, pCar2)) {
1342
        SortOutSmoke(pCar1);
1343
        SortOutSmoke(pCar2);
1344
    }
1345
}