From a62c355e91a4cc9f5d49d32a24853256bd1b3172 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 17 Jul 2021 10:59:13 +0800 Subject: [PATCH] Add code to generate official karts list --- data/official_karts.xml | 21 ++++++ sources.cmake | 2 +- src/karts/official_karts.cpp | 128 +++++++++++++++++++++++++++++++++++ src/karts/official_karts.hpp | 20 ++++++ src/main.cpp | 9 +++ 5 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 data/official_karts.xml create mode 100644 src/karts/official_karts.cpp create mode 100644 src/karts/official_karts.hpp diff --git a/data/official_karts.xml b/data/official_karts.xml new file mode 100644 index 000000000..671aadf36 --- /dev/null +++ b/data/official_karts.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/sources.cmake b/sources.cmake index d4f28ae4d..ba4868d71 100644 --- a/sources.cmake +++ b/sources.cmake @@ -1,5 +1,5 @@ # Modify this file to change the last-modified date when you add/remove a file. -# This will then trigger a new cmake run automatically. +# This will then trigger a new cmake run automatically. file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp") file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp") file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*") diff --git a/src/karts/official_karts.cpp b/src/karts/official_karts.cpp new file mode 100644 index 000000000..c8d7b9f38 --- /dev/null +++ b/src/karts/official_karts.cpp @@ -0,0 +1,128 @@ +#include "karts/official_karts.hpp" + +#include "karts/kart_properties_manager.hpp" +#include "io/file_manager.hpp" +#include "io/xml_node.hpp" +#include "karts/kart_model.hpp" +#include "karts/kart_properties.hpp" +#include "utils/file_utils.hpp" +#include "utils/log.hpp" +#include "utils/vec3.hpp" + +#include +#include +#include +#include + +namespace OfficialKarts +{ +// ============================================================================ +struct OfficialKart +{ +std::string name; +std::string type; +float width; +float height; +float length; +Vec3 gravity_shift; +OfficialKart(const std::string& n, const std::string& t, float w, float h, + float l, const Vec3& g) : name(n), type(t), width(w), height(h), + length(l), gravity_shift(g) {} +}; // OfficialKart +std::vector g_official_karts; + +// ---------------------------------------------------------------------------- +void dumpOfficialKarts() +{ + std::stringstream ss; + ss << "\n"; + ss << "\n"; + for (unsigned i = 0; i < kart_properties_manager->getNumberOfKarts(); i++) + { + const KartProperties* kp = kart_properties_manager->getKartById(i); + if (kp->isAddon()) + continue; + auto km = kp->getKartModelCopy(); + ss << " getIdent() << "\" type=\"" << + kp->getKartType() << "\" width=\"" << km->getWidth() << + "\" height=\"" << km->getHeight() << "\" length=\"" << + km->getLength() << "\" gravity-shift=\"" << + kp->getGravityCenterShift().x() << " " << + kp->getGravityCenterShift().y() << " " << + kp->getGravityCenterShift().z() << "\"/>\n"; + } + ss << "\n"; + std::string s = ss.str(); + std::ofstream xml("official_karts.xml", std::ofstream::out); + xml << ss.rdbuf(); + xml.close(); +} // getAllData + +// ---------------------------------------------------------------------------- +void load() +{ + const std::string file_name = file_manager->getAsset("official_karts.xml"); + if (file_name.empty()) + Log::fatal("OfficialKarts", "Missing official_karts.xml"); + const XMLNode *root = file_manager->createXMLTree(file_name); + assert(root); + for (unsigned int i = 0; i < root->getNumNodes(); i++) + { + const XMLNode *node = root->getNode(i); + std::string name; + std::string type; + float width = 0.0f; + float height = 0.0f; + float length = 0.0f; + Vec3 gravity_shift; + node->get("name", &name); + node->get("type", &type); + node->get("width", &width); + node->get("height", &height); + node->get("length", &length); + node->get("gravity-shift", &gravity_shift); + g_official_karts.emplace_back(name, type, width, height, length, + gravity_shift); + } +} // load + +// ---------------------------------------------------------------------------- +std::set getOfficialKarts() +{ + std::set result; + for (OfficialKart& ok : g_official_karts) + result.insert(ok.name); + return result; +} // getOfficialKarts + +// ---------------------------------------------------------------------------- +const KartProperties* getKartByIdent(const std::string& ident, + float* width, float* height, + float* length, Vec3* gravity_shift) +{ + for (OfficialKart& ok : g_official_karts) + { + if (ok.name == ident) + { + for (unsigned i = 0; + i < kart_properties_manager->getNumberOfKarts(); i++) + { + const KartProperties* kp = + kart_properties_manager->getKartById(i); + if (kp->isAddon()) + continue; + if (kp->getKartType() == ok.type) + { + *width = ok.width; + *height = ok.height; + *length = ok.length; + *gravity_shift = ok.gravity_shift; + return kp; + } + } + } + } + return NULL; +} // getKartByIdent + +} // OfficialKarts diff --git a/src/karts/official_karts.hpp b/src/karts/official_karts.hpp new file mode 100644 index 000000000..399b82f83 --- /dev/null +++ b/src/karts/official_karts.hpp @@ -0,0 +1,20 @@ +#ifndef HEADER_OFFICIAL_KARTS_HPP +#define HEADER_OFFICIAL_KARTS_HPP + +#include +#include + +class KartProperties; +class Vec3; + +namespace OfficialKarts +{ +void dumpOfficialKarts(); +void load(); +std::set getOfficialKarts(); +const KartProperties* getKartByIdent(const std::string& ident, + float* width, float* height, + float* length, Vec3* gravity_shift); +} + +#endif diff --git a/src/main.cpp b/src/main.cpp index bc7811dff..2c1f71ab0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -241,6 +241,7 @@ extern "C" { #include "karts/kart_model.hpp" #include "karts/kart_properties.hpp" #include "karts/kart_properties_manager.hpp" +#include "karts/official_karts.hpp" #include "modes/cutscene_world.hpp" #include "modes/demo_world.hpp" #include "network/protocols/connect_to_server.hpp" @@ -685,6 +686,7 @@ void cmdLineHelp() " texture filtering.\n" " --shadows=n Set resolution of shadows (0 to disable).\n" " --render-driver=n Render driver to use (gl or directx9).\n" + " --dump-official-karts Dump official karts for current stk-assets.\n" " --apitrace This will disable buffer storage and\n" " writing gpu query strings to opengl, which\n" " can be seen later in apitrace.\n" @@ -1762,6 +1764,12 @@ int handleCmdLine(bool has_server_config, bool has_parent_process) CommandLine::has("-psn"); #endif + if (CommandLine::has("--dump-official-karts")) + { + OfficialKarts::dumpOfficialKarts(); + return 0; + } + CommandLine::reportInvalidParameters(); if (ProfileWorld::isProfileMode() || GUIEngine::isNoGraphics()) @@ -2266,6 +2274,7 @@ int main(int argc, char *argv[]) GUIEngine::addLoadingIcon( irr_driver->getTexture(FileManager::GUI_ICON, "options_video.png")); kart_properties_manager -> loadAllKarts (); + OfficialKarts::load(); handleXmasMode(); handleEasterEarMode();