init commit
This commit is contained in:
commit
15ab48343a
187 files changed
+12644
No files matched your search
@@ -0,0 +1,226 @@
|
||||
#include "game/game_modes.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace top_octane::game {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kPointsTable[8] = {10, 6, 4, 3, 2, 1, 0, 0};
|
||||
|
||||
std::filesystem::path slot_path(const std::filesystem::path& pref_dir, int slot) {
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "champ_%02d.dat", std::clamp(slot, 0, 7));
|
||||
return pref_dir / buf;
|
||||
}
|
||||
|
||||
std::filesystem::path clone_path(const std::filesystem::path& pref_dir, int track_num) {
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "clone_0%d.bin", std::clamp(track_num, 1, 6));
|
||||
return pref_dir / buf;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void ChampionshipState::start_new(const std::string& p_name, int vehicle_idx, int team_idx, int diff) {
|
||||
active = true;
|
||||
current_track = 1;
|
||||
difficulty = std::clamp(diff, 0, 2);
|
||||
player_vehicle = std::clamp(vehicle_idx, 0, 5);
|
||||
player_team = std::clamp(team_idx, 0, 7);
|
||||
player_name = p_name.empty() ? "PLAYER" : p_name;
|
||||
overall_rating_pts = 0;
|
||||
team_points.fill(0);
|
||||
|
||||
drivers[0] = {player_name, player_team, player_vehicle, 0, 0, 0};
|
||||
for (int i = 1; i < 8; ++i) {
|
||||
int ai = i - 1;
|
||||
int team = (ai == player_team) ? ((ai + 1) % 8) : (ai % 8);
|
||||
int veh = (ai * 5) % 6;
|
||||
drivers[i] = {sim::kPilotNames[ai], team, veh, 0, 0, 0};
|
||||
}
|
||||
}
|
||||
|
||||
void ChampionshipState::record_race_results(const std::vector<sim::CraftState>& crafts) {
|
||||
for (const auto& c : crafts) {
|
||||
int rank = std::clamp(c.placement - 1, 0, 7);
|
||||
int pts = kPointsTable[rank];
|
||||
int slot = (c.id >= 0 && c.id < 8) ? c.id : 0;
|
||||
|
||||
drivers[slot].points += pts;
|
||||
drivers[slot].kills += c.kills;
|
||||
if (c.placement == 1) drivers[slot].wins += 1;
|
||||
|
||||
int tid = std::clamp(drivers[slot].team_idx, 0, 7);
|
||||
team_points[tid] += pts;
|
||||
|
||||
if (c.is_human && c.player_index == 0) {
|
||||
overall_rating_pts += pts * 12 + c.kills * 15 - c.deaths * 5 + (c.placement == 1 ? 40 : 0);
|
||||
overall_rating_pts = std::max(0, overall_rating_pts);
|
||||
}
|
||||
}
|
||||
++current_track;
|
||||
}
|
||||
|
||||
std::string ChampionshipState::player_rating_title() const {
|
||||
return sim::rating_for_score(overall_rating_pts);
|
||||
}
|
||||
|
||||
bool ChampionshipState::save_slot(const std::filesystem::path& pref_dir, int slot) const {
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(pref_dir, ec);
|
||||
std::ofstream ofs(slot_path(pref_dir, slot), std::ios::trunc);
|
||||
if (!ofs) return false;
|
||||
|
||||
ofs << "TOPOCTANE_CHAMP_V1\n"
|
||||
<< (active ? 1 : 0) << " " << current_track << " " << difficulty << " "
|
||||
<< player_vehicle << " " << player_team << " " << overall_rating_pts << "\n"
|
||||
<< player_name << "\n";
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
ofs << drivers[i].team_idx << " " << drivers[i].vehicle_idx << " "
|
||||
<< drivers[i].points << " " << drivers[i].kills << " " << drivers[i].wins << " "
|
||||
<< drivers[i].name << "\n";
|
||||
}
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
ofs << team_points[i] << (i == 7 ? "\n" : " ");
|
||||
}
|
||||
return ofs.good();
|
||||
}
|
||||
|
||||
bool ChampionshipState::load_slot(const std::filesystem::path& pref_dir, int slot) {
|
||||
std::ifstream ifs(slot_path(pref_dir, slot));
|
||||
if (!ifs) return false;
|
||||
std::string magic;
|
||||
if (!std::getline(ifs, magic) || magic != "TOPOCTANE_CHAMP_V1") return false;
|
||||
|
||||
int act = 0;
|
||||
if (!(ifs >> act >> current_track >> difficulty >> player_vehicle >> player_team >> overall_rating_pts)) {
|
||||
return false;
|
||||
}
|
||||
active = (act != 0);
|
||||
ifs.ignore(256, '\n');
|
||||
if (!std::getline(ifs, player_name)) return false;
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (!(ifs >> drivers[i].team_idx >> drivers[i].vehicle_idx >> drivers[i].points
|
||||
>> drivers[i].kills >> drivers[i].wins)) {
|
||||
return false;
|
||||
}
|
||||
std::string rest;
|
||||
std::getline(ifs, rest);
|
||||
size_t first = rest.find_first_not_of(' ');
|
||||
drivers[i].name = (first == std::string::npos) ? "PILOT" : rest.substr(first);
|
||||
}
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (!(ifs >> team_points[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ChampionshipState::slot_exists(const std::filesystem::path& pref_dir, int slot) {
|
||||
std::error_code ec;
|
||||
return std::filesystem::is_regular_file(slot_path(pref_dir, slot), ec);
|
||||
}
|
||||
|
||||
void HighscoreTable::clear() {
|
||||
for (auto& t : tracks) {
|
||||
t = TrackRecord{};
|
||||
}
|
||||
}
|
||||
|
||||
int HighscoreTable::submit_race(int track_num, const std::vector<sim::CraftState>& crafts) {
|
||||
int idx = std::clamp(track_num - 1, 0, 5);
|
||||
TrackRecord& rec = tracks[idx];
|
||||
int flags = 0;
|
||||
for (const auto& c : crafts) {
|
||||
if (!c.is_human) continue;
|
||||
if (c.best_lap_time > 0.5f && (rec.best_lap <= 0.0f || c.best_lap_time < rec.best_lap)) {
|
||||
rec.best_lap = c.best_lap_time;
|
||||
rec.best_lap_driver = c.pilot_name;
|
||||
flags |= 1;
|
||||
}
|
||||
if (c.finished && c.race_time > 1.0f &&
|
||||
(rec.best_race <= 0.0f || c.race_time < rec.best_race)) {
|
||||
rec.best_race = c.race_time;
|
||||
rec.best_race_driver = c.pilot_name;
|
||||
flags |= 2;
|
||||
}
|
||||
}
|
||||
return flags;
|
||||
}
|
||||
|
||||
bool HighscoreTable::load(const std::filesystem::path& path) {
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs) return false;
|
||||
std::string magic;
|
||||
if (!std::getline(ifs, magic) || magic != "TOPOCTANE_HISCORE_V1") return false;
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
if (!(ifs >> tracks[i].best_lap >> tracks[i].best_race)) return false;
|
||||
ifs.ignore(256, '\n');
|
||||
if (!std::getline(ifs, tracks[i].best_lap_driver)) return false;
|
||||
if (!std::getline(ifs, tracks[i].best_race_driver)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HighscoreTable::save(const std::filesystem::path& path) const {
|
||||
std::error_code ec;
|
||||
if (path.has_parent_path()) std::filesystem::create_directories(path.parent_path(), ec);
|
||||
std::ofstream ofs(path, std::ios::trunc);
|
||||
if (!ofs) return false;
|
||||
ofs << "TOPOCTANE_HISCORE_V1\n" << std::setprecision(9);
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
ofs << tracks[i].best_lap << " " << tracks[i].best_race << "\n"
|
||||
<< tracks[i].best_lap_driver << "\n"
|
||||
<< tracks[i].best_race_driver << "\n";
|
||||
}
|
||||
return ofs.good();
|
||||
}
|
||||
|
||||
bool save_clone_recording(const std::filesystem::path& pref_dir, const sim::CloneRecording& rec) {
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(pref_dir, ec);
|
||||
std::ofstream ofs(clone_path(pref_dir, rec.track_number), std::ios::binary | std::ios::trunc);
|
||||
if (!ofs) return false;
|
||||
const uint32_t magic = 0x434C4E31u; // CLN1
|
||||
uint32_t count = static_cast<uint32_t>(rec.frames.size());
|
||||
ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
|
||||
ofs.write(reinterpret_cast<const char*>(&rec.track_number), sizeof(rec.track_number));
|
||||
ofs.write(reinterpret_cast<const char*>(&rec.vehicle_idx), sizeof(rec.vehicle_idx));
|
||||
ofs.write(reinterpret_cast<const char*>(&rec.team_idx), sizeof(rec.team_idx));
|
||||
ofs.write(reinterpret_cast<const char*>(&rec.best_race_time), sizeof(rec.best_race_time));
|
||||
ofs.write(reinterpret_cast<const char*>(&count), sizeof(count));
|
||||
if (count > 0) {
|
||||
ofs.write(reinterpret_cast<const char*>(rec.frames.data()),
|
||||
static_cast<std::streamsize>(count * sizeof(sim::CraftPose)));
|
||||
}
|
||||
return ofs.good();
|
||||
}
|
||||
|
||||
bool load_clone_recording(const std::filesystem::path& pref_dir, int track_num,
|
||||
sim::CloneRecording& out_rec) {
|
||||
std::ifstream ifs(clone_path(pref_dir, track_num), std::ios::binary);
|
||||
if (!ifs) return false;
|
||||
uint32_t magic = 0, count = 0;
|
||||
ifs.read(reinterpret_cast<char*>(&magic), sizeof(magic));
|
||||
if (magic != 0x434C4E31u) return false;
|
||||
ifs.read(reinterpret_cast<char*>(&out_rec.track_number), sizeof(out_rec.track_number));
|
||||
ifs.read(reinterpret_cast<char*>(&out_rec.vehicle_idx), sizeof(out_rec.vehicle_idx));
|
||||
ifs.read(reinterpret_cast<char*>(&out_rec.team_idx), sizeof(out_rec.team_idx));
|
||||
ifs.read(reinterpret_cast<char*>(&out_rec.best_race_time), sizeof(out_rec.best_race_time));
|
||||
ifs.read(reinterpret_cast<char*>(&count), sizeof(count));
|
||||
if (!ifs || count > 36000) return false;
|
||||
out_rec.frames.resize(count);
|
||||
if (count > 0) {
|
||||
ifs.read(reinterpret_cast<char*>(out_rec.frames.data()),
|
||||
static_cast<std::streamsize>(count * sizeof(sim::CraftPose)));
|
||||
}
|
||||
return ifs.good();
|
||||
}
|
||||
|
||||
} // namespace top_octane::game
|
||||
Reference in new issue
Block a user