Files
top-octane/src/sim/race_sim.hpp
T

250 lines
7.3 KiB
C++

#pragma once
#include "assets/level.hpp"
#include "core/math.hpp"
#include "input/input_manager.hpp"
#include "sim/physics.hpp"
#include <cstdint>
#include <string>
#include <vector>
namespace top_octane::sim {
enum class GameMode {
SingleRace = 0,
DeathMatch = 1,
CloneRace = 2,
Championship = 3,
SplitScreen = 4,
};
enum class SimSoundCue {
None = 0,
StartBeep,
StartGo,
MinigunShot,
MissileLaunch,
Explosion,
Impact,
Scrape,
Boost,
Pickup,
Powerup,
Refuel,
LockOn,
Warning,
Stunt180,
Stunt360,
Stunt720,
SwitchTrigger,
PitComplete,
};
struct SimEvent {
SimSoundCue sound = SimSoundCue::None;
Vec3 pos{0, 0, 0};
int target_player = -1; // -1 = global, 0 = P1, 1 = P2
std::string banner_text;
float rumble_intensity = 0.0f;
};
struct Projectile {
bool active = false;
bool is_missile = false;
int owner_id = -1;
int target_id = -1;
Vec3 pos{0, 0, 0};
Vec3 vel{0, 0, 0};
float yaw = 0.0f;
float damage = 10.0f;
float life = 2.0f;
};
struct PickupItem {
uint16_t entity_id = 0;
uint8_t subtype = 0;
uint16_t group = 1;
Vec3 pos{0, 0, 0};
bool group_unlocked = true;
bool active = true;
float respawn_timer = 0.0f;
};
struct PropObject {
uint16_t entity_id = 0;
uint8_t subtype = 0;
std::string model_key;
Vec3 pos{0, 0, 0};
float yaw = 0.0f;
float hp = 25.0f;
bool alive = true;
float respawn_timer = 0.0f;
};
enum class ParticleKind : uint8_t {
AdditiveGlow = 0, // Booster flare, minigun tracers, sparks
Fireball = 1, // Expanding turbulent fireball (white-yellow -> orange -> crimson)
Smoke = 2, // Alpha-blended billowing volumetric smoke cloud
Debris = 3, // Alpha-blended rotating charred shrapnel affected by gravity
};
struct ParticleBurst {
Vec3 pos{0, 0, 0};
Vec3 vel{0, 0, 0};
float size = 0.4f;
Vec4 color{1.0f, 0.7f, 0.2f, 1.0f};
float life = 0.5f;
float max_life = 0.5f;
ParticleKind kind = ParticleKind::AdditiveGlow;
float start_size = 0.0f; // 0 => use initial size
float end_size = 0.0f; // 0 => keep constant size
float drag = 0.0f; // velocity damping (1/sec)
float gravity = 0.0f; // vertical acceleration (+ rises, - falls)
float seed = 0.0f; // procedural puff variation [0..1]
float base_alpha = -1.0f; // <0 => initialized from color.w on first update
};
struct CloneRecording {
int track_number = 1;
int vehicle_idx = 0;
int team_idx = 7;
float best_race_time = 1e9f;
std::vector<CraftPose> frames; // 60 Hz sampled poses
};
struct DirtyRect {
int x0 = 0, z0 = 0, x1 = 0, z1 = 0;
};
struct ProjectileTrace {
Vec3 p0{0, 0, 0};
Vec3 p1{0, 0, 0};
int owner_player = -1;
bool is_missile = false;
};
struct RaceSetup {
GameMode mode = GameMode::SingleRace;
int track_number = 1; // 1..6
int total_laps = 3;
int difficulty = 1; // 0=Easy, 1=Medium, 2=Hard
int num_ai = 7; // 0..7
int p1_vehicle = 0;
int p1_team = 7;
std::string p1_name = "PLAYER";
int p2_vehicle = 3;
int p2_team = 1;
std::string p2_name = "PLAYER 2";
int deathmatch_kill_limit = 5;
bool fuel_enabled = true;
bool damage_enabled = true;
bool weapons_enabled = true;
bool pickups_enabled = true;
bool track_morphing_enabled = true;
};
class RaceSim {
public:
static constexpr size_t kMaxParticles = 900;
bool start_race(const assets::LevelData& level, const RaceSetup& setup,
const CloneRecording* existing_clone = nullptr);
// Advances 1 fixed 60 Hz tick
void step(const CraftInput& p1_input, const CraftInput& p2_input, float dt);
const RaceSetup& setup() const { return setup_; }
const assets::LevelData& level() const { return level_; }
const std::vector<CraftState>& crafts() const { return crafts_; }
std::vector<CraftState>& crafts_mut() { return crafts_; }
const std::vector<Projectile>& projectiles() const { return projectiles_; }
const std::vector<PickupItem>& pickups() const { return pickups_; }
const std::vector<PropObject>& props() const { return props_; }
const std::vector<ParticleBurst>& particles() const { return particles_; }
const std::vector<SimEvent>& events() const { return events_; }
void clear_events() { events_.clear(); }
bool countdown_active() const { return countdown_timer_ > 0.0f; }
float countdown_timer() const { return countdown_timer_; }
bool race_finished() const { return race_over_; }
int winner_id() const { return winner_id_; }
bool has_clone_ghost() const { return clone_active_; }
CraftPose interpolated_clone_pose(float alpha) const;
const CloneRecording& current_recording() const { return recording_; }
const CloneRecording& playback_clone() const { return playback_clone_; }
// Checks if morph triggers fired and changed terrain heights or columns
bool consume_terrain_dirty(int& x0, int& z0, int& x1, int& z1);
bool consume_world_dirty(std::vector<DirtyRect>& out_terrain_rects, bool& out_columns_dirty);
private:
struct TriggerRuntime {
bool armed = false;
bool fired = false;
bool pending = false;
float timer = 0.0f;
int hit_accum = 0;
int pending_player = -1;
};
struct ActiveMorph {
size_t morph_idx = 0;
float elapsed = 0.0f;
float duration = 0.50f;
std::vector<assets::MapEntry> orig_dst;
std::vector<assets::MapEntry> orig_src;
};
void compute_start_grid(int count, std::vector<CraftPose>& out_poses) const;
void update_checkpoints_and_laps(CraftState& c, float dt);
void update_recharge_pads(CraftState& c, float dt);
void update_triggers(CraftState& c);
void update_timed_triggers(float dt);
void update_rocket_triggers(const std::vector<ProjectileTrace>& traces);
void update_active_morphs(float dt);
void apply_morph_fraction(const ActiveMorph& am, float s);
void push_dirty_rect(int x0, int z0, int x1, int z1);
void activate_group(uint16_t group, int actor_player, bool allow_banner, bool fast_morph = false);
void update_standings();
void finalize_unfinished_times(const std::vector<size_t>& order);
void update_steam_vents(float dt);
void update_particles(float dt);
RaceSetup setup_{};
assets::LevelData level_{};
std::vector<CraftState> crafts_;
std::vector<Projectile> projectiles_;
std::vector<PickupItem> pickups_;
std::vector<PropObject> props_;
std::vector<ParticleBurst> particles_;
std::vector<SimEvent> events_;
float countdown_timer_ = 3.0f;
int last_countdown_int_ = 4;
float elapsed_time_ = 0.0f;
bool race_over_ = false;
bool times_finalized_ = false;
int winner_id_ = -1;
// Trigger/morph tracking
std::vector<TriggerRuntime> trigger_states_;
std::vector<bool> morph_is_repeating_;
std::vector<bool> morph_fired_once_;
std::vector<ActiveMorph> active_morphs_;
std::vector<DirtyRect> dirty_rects_;
bool columns_dirty_ = false;
float morph_banner_cd_ = 0.0f;
std::vector<float> steam_vent_timers_;
uint32_t fx_tick_ = 0;
// Clone race recording & playback
CloneRecording recording_;
CloneRecording playback_clone_;
bool clone_active_ = false;
size_t clone_tick_ = 0;
};
} // namespace top_octane::sim