Subversion Repositories Games.Carmageddon

Rev

Rev 1 | Rev 15 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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