Skip to content

Commit

Permalink
Add code to generate official karts list
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Jul 17, 2021
1 parent 6e17f2e commit 851290d
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
21 changes: 21 additions & 0 deletions data/official_karts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<karts>
<kart name="adiumy" type="medium" width="0.852" height="0.775" length="0.943" gravity-shift="0 0.2829 0"/>
<kart name="amanda" type="heavy" width="1.119" height="0.764" length="1.476" gravity-shift="0 0.382 0"/>
<kart name="beastie" type="heavy" width="0.837" height="0.847" length="1.49" gravity-shift="0 0.4235 0"/>
<kart name="emule" type="medium" width="0.625" height="0.735" length="1.146" gravity-shift="0 0.3438 0"/>
<kart name="gavroche" type="medium" width="0.77" height="0.614" length="1.272" gravity-shift="0 0.307 0"/>
<kart name="gnu" type="medium" width="0.77" height="0.949" length="1.345" gravity-shift="0 0.4035 0"/>
<kart name="hexley" type="light" width="0.611" height="0.858" length="1.608" gravity-shift="0 0.429 0"/>
<kart name="kiki" type="light" width="0.866" height="0.974" length="1.227" gravity-shift="0 0.3681 0"/>
<kart name="konqi" type="medium" width="0.931" height="0.857" length="1.588" gravity-shift="0 0.4285 0"/>
<kart name="nolok" type="medium" width="0.725" height="0.645" length="1.413" gravity-shift="0 0.3225 0"/>
<kart name="pidgin" type="heavy" width="0.781" height="0.627" length="1.243" gravity-shift="0 0.3135 0"/>
<kart name="puffy" type="heavy" width="1.109" height="0.621" length="1.573" gravity-shift="0 0.3105 0"/>
<kart name="sara_the_racer" type="light" width="0.459" height="0.989" length="1.152" gravity-shift="0 0.3456 0"/>
<kart name="sara_the_wizard" type="medium" width="0.5" height="0.959" length="1.22" gravity-shift="0 0.366 0"/>
<kart name="suzanne" type="medium" width="0.935" height="0.645" length="1.405" gravity-shift="0 0.3225 0"/>
<kart name="tux" type="medium" width="0.821" height="0.675" length="1.437" gravity-shift="0 0.3375 0"/>
<kart name="wilber" type="light" width="0.837" height="0.871" length="1.715" gravity-shift="0 0.4355 0"/>
<kart name="xue" type="medium" width="1.006" height="0.944" length="1.446" gravity-shift="0 0.4338 0"/>
</karts>
2 changes: 1 addition & 1 deletion sources.cmake
Original file line number Diff line number Diff line change
@@ -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/*")
Expand Down
128 changes: 128 additions & 0 deletions src/karts/official_karts.cpp
Original file line number Diff line number Diff line change
@@ -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 <cassert>
#include <fstream>
#include <sstream>
#include <vector>

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<OfficialKart> g_official_karts;

// ----------------------------------------------------------------------------
void dumpOfficialKarts()
{
std::stringstream ss;
ss << "<?xml version=\"1.0\"?>\n";
ss << "<karts>\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 << " <kart name=\"" << kp->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 << "</karts>\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<std::string> getOfficialKarts()
{
std::set<std::string> 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
20 changes: 20 additions & 0 deletions src/karts/official_karts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef HEADER_OFFICIAL_KARTS_HPP
#define HEADER_OFFICIAL_KARTS_HPP

#include <string>
#include <set>

class KartProperties;
class Vec3;

namespace OfficialKarts
{
void dumpOfficialKarts();
void load();
std::set<std::string> getOfficialKarts();
const KartProperties* getKartByIdent(const std::string& ident,
float* width, float* height,
float* length, Vec3* gravity_shift);
}

#endif
9 changes: 9 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
#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"
Expand Down Expand Up @@ -661,6 +662,7 @@ void cmdLineHelp()
" Takes precedence over trilinear or bilinear\n"
" texture filtering.\n"
" --shadows=n Set resolution of shadows (0 to disable).\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"
Expand Down Expand Up @@ -1660,6 +1662,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())
Expand Down Expand Up @@ -2093,6 +2101,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();

Expand Down

0 comments on commit 851290d

Please sign in to comment.