278 lines
11 KiB
C++
278 lines
11 KiB
C++
#include "assets/level.hpp"
|
|
#include "core/clock.hpp"
|
|
#include "core/math.hpp"
|
|
#include "core/vfs.hpp"
|
|
#include "sim/physics.hpp"
|
|
#include "sim/race_sim.hpp"
|
|
#include "sim/vehicle_defs.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
|
|
using namespace top_octane;
|
|
|
|
#define CHECK(cond) \
|
|
do { \
|
|
if (!(cond)) { \
|
|
std::cerr << "FAIL [" << __FILE__ << ":" << __LINE__ << "]: " #cond << "\n"; \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
int main() {
|
|
Vfs vfs;
|
|
CHECK(vfs.init(HIOCTANE_DEFAULT_DATA_DIR));
|
|
|
|
// 1. Verify starting grid across all 6 levels is collision-free and finite
|
|
for (int l = 1; l <= 6; ++l) {
|
|
assets::LevelData lvl;
|
|
CHECK(lvl.load(vfs, l));
|
|
|
|
sim::RaceSetup setup;
|
|
setup.track_number = l;
|
|
setup.num_ai = 7;
|
|
|
|
sim::RaceSim sim;
|
|
CHECK(sim.start_race(lvl, setup));
|
|
CHECK(sim.crafts().size() == 8);
|
|
|
|
for (const auto& c : sim.crafts()) {
|
|
CHECK(std::isfinite(c.curr_pose.pos.x));
|
|
CHECK(std::isfinite(c.curr_pose.pos.y));
|
|
CHECK(std::isfinite(c.curr_pose.pos.z));
|
|
CHECK(!lvl.is_cell_solid_at_y(static_cast<int>(std::floor(c.curr_pose.pos.x)),
|
|
static_cast<int>(std::floor(c.curr_pose.pos.z)),
|
|
c.curr_pose.pos.y));
|
|
}
|
|
for (size_t i = 0; i < sim.crafts().size(); ++i) {
|
|
for (size_t j = i + 1; j < sim.crafts().size(); ++j) {
|
|
const auto& ca = sim.crafts()[i];
|
|
const auto& cb = sim.crafts()[j];
|
|
float ra = sim::kVehicleSpecs[std::clamp(ca.vehicle_idx, 0, 5)].collision_radius;
|
|
float rb = sim::kVehicleSpecs[std::clamp(cb.vehicle_idx, 0, 5)].collision_radius;
|
|
float dx = cb.curr_pose.pos.x - ca.curr_pose.pos.x;
|
|
float dz = cb.curr_pose.pos.z - ca.curr_pose.pos.z;
|
|
CHECK(std::hypot(dx, dz) >= ra + rb);
|
|
}
|
|
}
|
|
}
|
|
std::cout << "[PASS] Start grid valid & collision-free across all 6 tracks\n";
|
|
|
|
// 2. Verify fixed-step determinism (running two identical sims produces bit-identical state)
|
|
assets::LevelData lvl1;
|
|
CHECK(lvl1.load(vfs, 1));
|
|
|
|
auto run_determinism = [&](int steps) -> sim::CraftPose {
|
|
sim::RaceSetup setup;
|
|
setup.track_number = 1;
|
|
setup.num_ai = 0;
|
|
sim::RaceSim s;
|
|
s.start_race(lvl1, setup);
|
|
CraftInput in;
|
|
in.throttle = 1.0f;
|
|
for (int i = 0; i < steps; ++i) {
|
|
in.steer = (i > 220 && i < 260) ? 0.5f : 0.0f;
|
|
in.boost = (i > 200 && i < 240);
|
|
s.step(in, CraftInput{}, static_cast<float>(FixedStepClock::kFixedDt));
|
|
}
|
|
return s.crafts()[0].curr_pose;
|
|
};
|
|
|
|
sim::CraftPose pa = run_determinism(400);
|
|
sim::CraftPose pb = run_determinism(400);
|
|
CHECK(pa.pos.x == pb.pos.x && pa.pos.y == pb.pos.y && pa.pos.z == pb.pos.z);
|
|
CHECK(pa.yaw == pb.yaw);
|
|
std::cout << "[PASS] Fixed-timestep 60Hz determinism verified\n";
|
|
|
|
// 3. Verify autopilot completes an entire 2-lap race cleanly on Level 1 without getting stuck
|
|
{
|
|
sim::RaceSetup setup;
|
|
setup.track_number = 1;
|
|
setup.total_laps = 2;
|
|
setup.num_ai = 0;
|
|
sim::RaceSim s;
|
|
CHECK(s.start_race(lvl1, setup));
|
|
s.crafts_mut()[0].autopilot = true;
|
|
|
|
float dt = static_cast<float>(FixedStepClock::kFixedDt);
|
|
// Simulate up to 180 seconds (10800 ticks)
|
|
for (int t = 0; t < 10800 && !s.race_finished(); ++t) {
|
|
s.step(CraftInput{}, CraftInput{}, dt);
|
|
// Replenish fuel if low so test isolates navigation/physics/checkpoints
|
|
if (s.crafts()[0].fuel < 20.0f) s.crafts_mut()[0].fuel = 100.0f;
|
|
}
|
|
const auto& p1 = s.crafts()[0];
|
|
std::cout << " Autopilot 2-lap test: finished=" << p1.finished
|
|
<< " lap=" << p1.lap << " cps=" << p1.checkpoints_passed
|
|
<< " time=" << p1.race_time << "s\n";
|
|
CHECK(p1.finished);
|
|
CHECK(p1.best_lap_time > 5.0f && p1.best_lap_time < 80.0f);
|
|
}
|
|
std::cout << "[PASS] Autopilot hover physics, checkpoints & 2-lap completion verified\n";
|
|
|
|
// 4. Verify wall containment (steering straight into a boundary/column never tunnels out of map)
|
|
{
|
|
sim::RaceSetup setup;
|
|
setup.track_number = 1;
|
|
setup.num_ai = 0;
|
|
sim::RaceSim s;
|
|
CHECK(s.start_race(lvl1, setup));
|
|
CraftInput ram;
|
|
ram.throttle = 1.0f;
|
|
ram.steer = 1.0f;
|
|
ram.boost = true;
|
|
for (int t = 0; t < 600; ++t) {
|
|
s.step(ram, CraftInput{}, static_cast<float>(FixedStepClock::kFixedDt));
|
|
const auto& pos = s.crafts()[0].curr_pose.pos;
|
|
CHECK(pos.x >= 1.0f && pos.x <= assets::kMapWidth - 2.0f);
|
|
CHECK(pos.z >= 1.0f && pos.z <= assets::kMapHeight - 2.0f);
|
|
}
|
|
}
|
|
std::cout << "[PASS] Map boundary & column containment verified\n";
|
|
|
|
// 5. Seam-free multi-tile wall sliding & 2-craft column squeeze containment
|
|
assets::LevelData syn;
|
|
syn.map.resize(assets::kMapWidth * assets::kMapHeight);
|
|
for (auto& m : syn.map) {
|
|
m.height = 2048;
|
|
m.tex_or_col = 0;
|
|
}
|
|
syn.columns[1].shape_mask = 0x03;
|
|
for (int x = 10; x <= 19; ++x) {
|
|
syn.cell_mut(x, 20).tex_or_col = -1;
|
|
}
|
|
|
|
{
|
|
sim::CraftState car;
|
|
car.id = 0;
|
|
car.vehicle_idx = 0; // CAR0- (r = 0.48)
|
|
car.curr_pose.pos = Vec3(11.0f, 0.28f, 19.45f);
|
|
car.curr_pose.yaw = kHalfPi;
|
|
car.prev_pose = car.curr_pose;
|
|
car.vel = Vec3(12.0f, 0.0f, 2.5f);
|
|
car.fuel = car.max_fuel = 100.0f;
|
|
car.shield = car.max_shield = 100.0f;
|
|
|
|
CraftInput neutral{};
|
|
float dt = static_cast<float>(FixedStepClock::kFixedDt);
|
|
for (int t = 0; t < 30; ++t) {
|
|
sim::step_craft_physics(car, neutral, syn, dt);
|
|
}
|
|
CHECK(car.curr_pose.pos.z <= 20.0f - 0.48f + 0.02f);
|
|
CHECK(car.curr_pose.pos.x > 14.5f);
|
|
CHECK(car.vel.x > 4.5f);
|
|
}
|
|
std::cout << "[PASS] Seam-free multi-tile wall sliding verified\n";
|
|
|
|
{
|
|
sim::CraftState a;
|
|
a.id = 0;
|
|
a.vehicle_idx = 2; // JUGGA (r = 0.60)
|
|
a.curr_pose.pos = Vec3(15.0f, 0.28f, 19.38f);
|
|
a.prev_pose = a.curr_pose;
|
|
a.vel = Vec3(0.0f, 0.0f, 0.0f);
|
|
a.fuel = a.max_fuel = 125.0f;
|
|
|
|
sim::CraftState b;
|
|
b.id = 1;
|
|
b.vehicle_idx = 1; // TANK (r = 0.56)
|
|
b.curr_pose.pos = Vec3(15.0f, 0.28f, 18.4f);
|
|
b.prev_pose = b.curr_pose;
|
|
b.vel = Vec3(0.0f, 0.0f, 12.0f);
|
|
b.fuel = b.max_fuel = 110.0f;
|
|
|
|
CraftInput neutral{};
|
|
float dt = static_cast<float>(FixedStepClock::kFixedDt);
|
|
for (int t = 0; t < 30; ++t) {
|
|
sim::step_craft_physics(a, neutral, syn, dt);
|
|
sim::step_craft_physics(b, neutral, syn, dt);
|
|
for (int iter = 0; iter < 2; ++iter) {
|
|
float rel = 0.0f;
|
|
sim::resolve_craft_pair_collision(a, b, rel);
|
|
float imp = 0.0f;
|
|
bool scr = false;
|
|
sim::resolve_world_collisions(a, syn, imp, scr);
|
|
sim::resolve_world_collisions(b, syn, imp, scr);
|
|
}
|
|
}
|
|
CHECK(!syn.is_cell_solid_at_y(static_cast<int>(std::floor(a.curr_pose.pos.x)),
|
|
static_cast<int>(std::floor(a.curr_pose.pos.z)),
|
|
a.curr_pose.pos.y));
|
|
CHECK(!syn.is_cell_solid_at_y(static_cast<int>(std::floor(b.curr_pose.pos.x)),
|
|
static_cast<int>(std::floor(b.curr_pose.pos.z)),
|
|
b.curr_pose.pos.y));
|
|
CHECK(a.curr_pose.pos.z <= 20.0f - sim::kVehicleSpecs[a.vehicle_idx].collision_radius + 0.02f);
|
|
CHECK(b.curr_pose.pos.z <= 20.0f - sim::kVehicleSpecs[b.vehicle_idx].collision_radius + 0.02f);
|
|
float sep = std::hypot(b.curr_pose.pos.x - a.curr_pose.pos.x,
|
|
b.curr_pose.pos.z - a.curr_pose.pos.z);
|
|
CHECK(sep >= 0.60f + 0.56f - 0.05f);
|
|
}
|
|
std::cout << "[PASS] 2-craft column squeeze containment verified\n";
|
|
|
|
// 6. Track 1 gantry overpass clearance & ceiling clamp
|
|
{
|
|
int gx = -1, gz = -1;
|
|
for (int z = 0; z < assets::kMapHeight && gx < 0; ++z) {
|
|
for (int x = 0; x < assets::kMapWidth; ++x) {
|
|
const auto& m = lvl1.cell(x, z);
|
|
if (m.tex_or_col < 0) {
|
|
int cidx = -m.tex_or_col;
|
|
if (cidx > 0 && cidx < assets::kMaxColumns) {
|
|
uint16_t mask = lvl1.columns[cidx].shape_mask;
|
|
if ((mask & 1u) == 0 && (mask & 0xFEu) != 0) {
|
|
gx = x;
|
|
gz = z;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
CHECK(gx >= 0 && gz >= 0);
|
|
float gy = lvl1.cell_height_world(gx, gz);
|
|
CHECK(!lvl1.is_cell_solid_at_y(gx, gz, gy + 0.28f));
|
|
|
|
float floor_y = 0.0f, ceil_y = 1e6f;
|
|
lvl1.sample_surface_and_ceiling(gx + 0.5f, gz + 0.5f, gy + 0.28f, floor_y, ceil_y);
|
|
CHECK(ceil_y < 1e5f && ceil_y >= gy + 0.95f);
|
|
|
|
sim::CraftState c;
|
|
c.vehicle_idx = 0;
|
|
c.curr_pose.pos = Vec3(gx + 0.5f, ceil_y - 0.10f, gz + 0.5f);
|
|
c.prev_pose = c.curr_pose;
|
|
c.vel = Vec3(0.0f, 5.0f, 0.0f);
|
|
float imp = 0.0f;
|
|
bool scr = false;
|
|
sim::resolve_world_collisions(c, lvl1, imp, scr);
|
|
CHECK(c.curr_pose.pos.y <= ceil_y - 0.45f);
|
|
CHECK(c.vel.y <= 0.0f);
|
|
}
|
|
std::cout << "[PASS] Overhead gantry overpass & ceiling clamping verified\n";
|
|
|
|
// 7. All 6 tracks 1-lap autopilot completion
|
|
for (int l = 1; l <= 6; ++l) {
|
|
assets::LevelData lvl;
|
|
CHECK(lvl.load(vfs, l));
|
|
sim::RaceSetup setup;
|
|
setup.track_number = l;
|
|
setup.total_laps = 1;
|
|
setup.num_ai = 0;
|
|
sim::RaceSim s;
|
|
CHECK(s.start_race(lvl, setup));
|
|
s.crafts_mut()[0].autopilot = true;
|
|
|
|
float dt = static_cast<float>(FixedStepClock::kFixedDt);
|
|
for (int t = 0; t < 9000 && !s.race_finished(); ++t) {
|
|
s.step(CraftInput{}, CraftInput{}, dt);
|
|
if (s.crafts()[0].fuel < 20.0f) s.crafts_mut()[0].fuel = 100.0f;
|
|
}
|
|
const auto& p1 = s.crafts()[0];
|
|
std::cout << " Track " << l << " 1-lap autopilot: finished=" << p1.finished
|
|
<< " time=" << p1.race_time << "s\n";
|
|
CHECK(p1.finished);
|
|
}
|
|
std::cout << "[PASS] 1-lap autopilot completion verified across all 6 tracks\n";
|
|
return 0;
|
|
}
|