Files
SpaghettiKart/src/engine/vehicles/Car.cpp
T
quarrel07 8629bec320 Fix the warning-flagged real bugs across the codebase (#691, 1 of 3) (#725)
* menu_items: fix the real-bug-tier compiler warnings (#691 batch 1)

Six fixes, all in menu_items.c, all verified by menu playtest on macOS:

- BSWAP16(*color0++) advanced the pointer twice per pixel on little-endian
  builds (the macro evaluates its argument twice) and mixed bytes from two
  different pixels. Read first, increment separately. Note: this function
  (func_8009A9FC) currently has no callers, so the corruption was latent,
  not live.
- func_8009B9D0 fell off the end on a lookup miss, returning garbage (the
  existing code comment already suspected this). The miss path now returns
  the display list head unchanged, i.e. draws nothing.
- update_ok_menu_item read an uninitialized stack slot for unknown menu
  item types; now selects an explicit no-animation value, matching what
  the garbage read did in practice.
- pause_menu_item_box_cursor: removed the three empty 'if (x2);' matching
  artifacts. The x/y/z spin state itself is untouched.
- func_800A1FB0: initialized var_s4/var_s5 against the guarded-but-warned
  switch default.
- func_800A54EC: initialized the pause cursor position pointer against its
  unreachable mode-switch default (would have been a null-deref class bug
  if mode values ever grew).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* collision: define the G_ENDDL opcode shifts, return 0 when no tyre surface found

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* replays: cast staff-ghost pointer comparisons, return 0 for empty ghost buffer

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* update_objects: return 0 from conditional step helpers, fix TLUT pointer comparison, init train draw distances

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* render_player: remove the impossible lamp range (vanilla bug, never glowed on N64 either)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* math_util_2: return the vector, not the address of the parameter slot

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Crab: remove inner declaration shadowing the initialized objectIndex

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* editor: honor InverseMatrix failure (bool was compared against 2, always true)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Track.h: drop dead null checks on array members

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* shells: drop always-true angle and surface gates (behavior unchanged, s16 made them tautological)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* particles: make the no-return particle setters void

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* effects/stubs/skybox/main: align signatures with functions that return nothing

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* menus/save: type the pak status variable as s32, return BAD_READ on the fall-off path

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* audio: remove uninitialized-read matching artifacts, init isSound

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* code_80005FD0/code_80086E70: init dead bomb kart pointer and the no_init variable

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* port/engine UI: fix printf-style format types and non-literal format string

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* render_objects: feed the unused texture param to the uninitialized img walker

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Format the changed lines per .clang-format

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Address review: strip explanatory comments, drop vec3f pointer returns

Comments moved to the PR record; the one flagged worth keeping stays.
vec3f_set_xyz/normalize/cross_product return void now since the out
argument is the interface and nothing used the pointer.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Update actors_extended.c

* Add checkbox for 'Shells Shoot Straight' option

* Update render_player.c

* Update PortMenu.cpp

* Update render_player.c

* Update actors_extended.c

* Update render_player.c

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: MegaMech <MegaMech@users.noreply.github.com>
2026-08-01 22:29:56 -06:00

378 lines
13 KiB
C++

#include <libultraship.h>
#include "Car.h"
#include <vector>
#include "engine/vehicles/Utils.h"
#include "port/Game.h"
extern "C" {
#include "macros.h"
#include "main.h"
#include "defines.h"
#include "code_80005FD0.h"
#include "racing/actors.h"
#include "racing/math_util.h"
#include "sounds.h"
#include "update_objects.h"
#include "render_player.h"
#include "audio/external.h"
extern s8 gPlayerCount;
}
size_t ACar::_count = 0;
// pathIndex, array of spawn points
std::map<uint32_t, std::vector<uint32_t>> ACar::CarCounts;
ACar::ACar(const SpawnParams& params) : AActor(params) {
Name = "Car";
ResourceName = "mk:car";
BoundingBoxSize = 2.0f;
TrackPathPoint* temp_v0;
u16 waypointOffset;
s32 numWaypoints = gPathCountByPathIndex[0];
Index = _count;
PathIndex = params.PathIndex.value_or(0);
PathPoint = 0;
SpawnType = static_cast<ACar::SpawnMode>(params.Type.value_or(0));
switch(SpawnType) {
case SpawnMode::POINT: // Spawn car at a specific path point
PathPoint = params.PathPoint.value_or(0);
CarCounts[PathIndex].push_back(PathPoint);
break;
case SpawnMode::AUTO: // Automatically distribute cars based on a specific path point
printf("vehicle path size %zu\n", gVehiclePathSize);
PathPoint = GetVehiclePathPointDistributed(CarCounts[PathIndex], gVehiclePathSize);
CarCounts[PathIndex].push_back(PathPoint);
printf("train spawn path point: %d\n", PathPoint);
break;
}
waypointOffset = PathPoint;
temp_v0 = &gTrackPaths[PathIndex][PathPoint];
Position[0] = (f32) temp_v0->x;
Position[1] = (f32) temp_v0->y;
Position[2] = (f32) temp_v0->z;
ActorIndex = -1;
WaypointIndex = waypointOffset;
Velocity[0] = 0.0f;
Velocity[1] = 0.0f;
Velocity[2] = 0.0f;
if (gModeSelection == TIME_TRIALS) {
SomeType = ((Index - 1) % 3);
} else {
SomeType = random_int(3);
}
SomeMultiplierTheSequel = (f32) ((f64) (f32) (SomeType - 1) * 0.6);
if (((gCCSelection > CC_50) || (gModeSelection == TIME_TRIALS)) && (SomeType == 2)) {
Speed = params.Speed.value_or(0);
} else {
Speed = params.SpeedB.value_or(0);
}
Rotation[0] = 0;
Rotation[2] = 0;
if (gIsInExtra == 0) {
Rotation[1] = func_8000D6D0(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0, 3);
} else {
Rotation[1] = func_8000D940(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0);
}
sVehicleSoundRenderCounter = 10;
spawn_vehicle_on_road(Position, Rotation, Velocity, WaypointIndex, SomeMultiplierTheSequel, Speed);
ActorIndex = add_actor_to_empty_slot(Position, Rotation, Velocity, ACTOR_CAR);
_count++;
}
void ACar::SetSpawnParams(SpawnParams& params) {
params.Name = "mk:car";
params.Type = static_cast<uint16_t>(SpawnType);
params.PathIndex = PathIndex;
params.PathPoint = PathPoint;
params.Speed = Speed;
params.SpeedB = SpeedB;
}
bool ACar::IsMod() {
return true;
}
void ACar::Tick() {
f32 temp_f0_2;
f32 temp_f0_3;
f32 sp5C;
f32 sp58;
f32 sp54;
f32 temp_f2_2;
s16 var_a1;
s16 thing;
Vec3f sp40;
Vec3f sp34;
sp5C = Position[0];
sp58 = Position[1];
sp54 = Position[2];
sp40[0] = sp58;
sp40[1] = 0.0f;
sp40[2] = 0.0f;
temp_f0_2 = func_80013C74(SomeType, WaypointIndex);
if (SomeMultiplierTheSequel < temp_f0_2) {
SomeMultiplierTheSequel = SomeMultiplierTheSequel + 0.06;
if (temp_f0_2 < SomeMultiplierTheSequel) {
SomeMultiplierTheSequel = temp_f0_2;
}
}
if (temp_f0_2 < SomeMultiplierTheSequel) {
SomeMultiplierTheSequel = SomeMultiplierTheSequel - 0.06;
if (SomeMultiplierTheSequel < temp_f0_2) {
SomeMultiplierTheSequel = temp_f0_2;
}
}
if (gIsInExtra == 0) {
var_a1 = func_8000D6D0(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0, 3);
} else {
var_a1 = func_8000D940(Position, (s16*) &WaypointIndex, Speed, SomeMultiplierTheSequel, 0);
}
adjust_angle(&Rotation[1], var_a1, 100);
temp_f0_3 = Position[0] - sp5C;
temp_f2_2 = Position[2] - sp54;
sp34[0] = Position[1];
sp34[1] = 0.0f;
sp34[2] = sqrtf((temp_f0_3 * temp_f0_3) + (temp_f2_2 * temp_f2_2));
thing = get_angle_between_two_vectors(sp40, sp34);
adjust_angle(&Rotation[0], -thing, 100);
Velocity[0] = Position[0] - sp5C;
Velocity[1] = Position[1] - sp58;
Velocity[2] = Position[2] - sp54;
struct Actor* vehicleActor = GET_ACTOR(ActorIndex);
vehicleActor->pos[0] = Position[0];
vehicleActor->pos[1] = Position[1];
vehicleActor->pos[2] = Position[2];
vehicleActor->rot[0] = Rotation[0];
if (gIsMirrorMode != 0) {
vehicleActor->rot[1] = -Rotation[1];
} else {
vehicleActor->rot[1] = Rotation[1];
}
vehicleActor->rot[2] = Rotation[2];
vehicleActor->velocity[0] = Velocity[0];
vehicleActor->velocity[1] = Velocity[1];
vehicleActor->velocity[2] = Velocity[2];
}
void ACar::Draw(Camera* camera) {
s32 var_v0;
s32 var_s2;
s32 waypointCount;
u16 temp_a1;
waypointCount = gPathCountByPathIndex[0];
if (!(gPlayers[camera->playerId].speed < 1.6666666666666667)) {
temp_a1 = WaypointIndex;
for (var_v0 = 0; var_v0 < 0x18; var_v0 += 3) {
if (((sSomeNearestPathPoint + var_v0) % waypointCount) == temp_a1) {
gPlayerTrackPositionFactorInstruction[camera->playerId].target =
player_track_position_factor_vehicle(SomeType, gTrackPositionFactor[camera->playerId], temp_a1);
return;
}
}
}
}
void ACar::VehicleCollision(s32 playerId, Player* player) {
f32 temp_f12;
f32 temp_f14;
f32 temp_f22;
f32 spC4;
f32 spC0;
f32 spBC;
if (((D_801631E0[playerId] != 1) || ((((player->type & PLAYER_HUMAN) != 0)) && !(player->type & PLAYER_CPU))) &&
!(player->effects & 0x01000000)) {
spC4 = player->pos[0];
spC0 = player->pos[1];
spBC = player->pos[2];
temp_f12 = spC4 - Position[0];
temp_f22 = spC0 - Position[1];
temp_f14 = spBC - Position[2];
if (((temp_f12) > -100.0) && ((temp_f12) < 100.0)) {
if ((temp_f22 > -20.0) && (temp_f22 < 20.0)) {
if (((temp_f14) > -100.0) && ((temp_f14) < 100.0)) {
if (is_collide_with_vehicle(Position[0], Position[2], Velocity[0], Velocity[2], SomeArg3, SomeArg4,
spC4, spBC) == (s32) 1) {
player->triggers |= VERTICAL_TUMBLE_TRIGGER;
}
}
}
}
if ((player->type & PLAYER_HUMAN) && !(player->type & PLAYER_CPU)) {
if (((temp_f12) > -300.0) && ((temp_f12) < 300.0) && ((temp_f22 > -20.0)) && (temp_f22 < 20.0) &&
(((temp_f14) > -300.0)) && ((temp_f14) < 300.0)) {
if ((sVehicleSoundRenderCounter > 0) && (SomeFlags == 0)) {
sVehicleSoundRenderCounter -= 1;
SomeFlags |= (RENDER_VEHICLE << playerId);
func_800C9D80(Position, Velocity, SoundBits);
}
} else {
if (SomeFlags != 0) {
SomeFlags &= ~(RENDER_VEHICLE << playerId);
if (SomeFlags == 0) {
sVehicleSoundRenderCounter += 1;
func_800C9EF4(Position, SoundBits);
}
}
}
if (((temp_f12) > -200.0) && ((temp_f12) < 200.0) && ((temp_f22 > -20.0)) && (temp_f22 < 20.0) &&
(((temp_f14) > -200.0)) && ((temp_f14) < 200.0)) {
if (!(SomeFlagsTheSequel & ((1 << playerId)))) {
s32 var_s1 = 0;
u16 path = gPathCountByPathIndex[0];
s32 t1;
s32 t2;
switch (gIsInExtra) {
case 0:
t1 = is_path_point_in_range(WaypointIndex, gNearestPathPointByPlayerId[playerId], 10, 0,
path);
if ((gIsPlayerWrongDirection[playerId] == 0) && (t1 > 0) && (player->speed < Speed)) {
var_s1 = 1;
}
if ((gIsPlayerWrongDirection[playerId] == 1) && (t1 > 0)) {
var_s1 = 1;
}
break;
case 1:
t2 = is_path_point_in_range(WaypointIndex, gNearestPathPointByPlayerId[playerId], 0, 10,
path);
if (t2 > 0) {
if (random_int(2) == 0) {
// temp_v1_2 = gIsPlayerWrongDirection[playerId];
if (gIsPlayerWrongDirection[playerId] == 0) {
var_s1 = 1;
}
if ((gIsPlayerWrongDirection[playerId] == 1) && (player->speed < Speed)) {
var_s1 = 1;
}
} else {
SomeFlagsTheSequel |= ((1 << playerId));
}
}
break;
}
if (var_s1 == 1) {
u32 soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3B);
switch (SoundBits) {
case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x05):
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3B);
if (random_int(4) == 0) {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3C);
}
break;
case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x02):
if (random_int(2) != 0) {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3D);
} else {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3E);
}
break;
case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x03):
if (random_int(2) != 0) {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x3F);
} else {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x40);
}
break;
case SOUND_ARG_LOAD(0x51, 0x01, 0x80, 0x04):
if (random_int(2) != 0) {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x41);
} else {
soundBits2 = SOUND_ARG_LOAD(0x19, 0x01, 0x70, 0x42);
}
break;
}
SomeFlagsTheSequel |= ((1 << playerId));
func_800C98B8(Position, Velocity, soundBits2);
}
}
} else {
if (SomeFlagsTheSequel & ((1 << playerId))) {
SomeFlagsTheSequel &= ~((1 << playerId));
}
}
}
}
}
void ACar::DrawEditorProperties() {
ImGui::Text("Spawn Mode");
ImGui::SameLine();
int32_t type = static_cast<int32_t>(SpawnType);
const char* items[] = { "POINT", "AUTO" };
if (ImGui::Combo("##Type", &type, items, IM_ARRAYSIZE(items))) {
SpawnType = static_cast<ACar::SpawnMode>(type);
}
if (type == ACar::SpawnMode::POINT) {
ImGui::Text("Path Index");
ImGui::SameLine();
int pathIndex = static_cast<int>(PathIndex);
if (ImGui::InputInt("##PathIndex", &pathIndex)) {
if (pathIndex < 0) pathIndex = 0;
PathIndex = static_cast<uint32_t>(pathIndex);
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_UNDO "##ResetPathIndex")) {
PathIndex = 0;
}
ImGui::Text("Path Point");
ImGui::SameLine();
int pathPoint = static_cast<int>(PathPoint);
if (ImGui::InputInt("##PathPoint", &pathPoint)) {
if (pathPoint < 0) pathPoint = 0;
PathPoint = static_cast<uint32_t>(pathPoint);
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_UNDO "##ResetPathPoint")) {
PathPoint = 0;
}
}
ImGui::Text("Speed");
ImGui::SameLine();
float speed = Speed;
if (ImGui::DragFloat("##Speed", &speed, 0.1f)) {
Speed = speed;
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_UNDO "##ResetSpeed")) {
Speed = 0.0f;
}
ImGui::Text("SpeedB");
ImGui::SameLine();
float speed2 = SpeedB;
if (ImGui::DragFloat("##SpeedB", &speed2, 0.1f)) {
SpeedB = speed2;
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_UNDO "##ResetSpeedB")) {
SpeedB = 0.0f;
}
}