dfhack/plugins/export/export.cpp

184 lines
5.1 KiB
C++

2011-12-23 02:58:45 -07:00
// some headers required for a plugin. Nothing special, just the basics.
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
#define DFHACK_WANT_MISCUTILS
2012-01-02 04:52:09 -07:00
#include <Core.h>
#include <VersionInfo.h>
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <modules/Units.h>
#include <modules/Translation.h>
2012-01-27 06:46:42 -07:00
#include <df/ui.h>
#include <df/world.h>
#include <df/unit.h>
#include <df/unit_soul.h>
2011-12-23 02:58:45 -07:00
using namespace DFHack;
2012-01-27 06:46:42 -07:00
using namespace DFHack::Simple;
using namespace DFHack::Simple;
using df::global::ui;
using df::global::world;
2011-12-23 02:58:45 -07:00
// our own, empty header.
#include "export.h"
// Here go all the command declarations...
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
DFhackCExport command_result export_dwarves (Core * c, std::vector <std::string> & parameters);
// A plugins must be able to return its name. This must correspond to the filename - export.plug.so or export.plug.dll
DFhackCExport const char * plugin_name ( void )
{
return "export";
}
// Mandatory init function. If you have some global state, create it here.
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
// Fill the command list with your commands.
commands.clear();
commands.push_back(PluginCommand("export",
"Export dwarves to RuneSmith-compatible XML.",
export_dwarves /*,
true or false - true means that the command can't be used from non-interactive user interface'*/));
return CR_OK;
}
// This is called right before the plugin library is removed from memory.
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
static char* physicals[] = {
"Strength",
"Agility",
"Toughness",
"Endurance",
"Recuperation",
"DiseaseResistance",
};
static char* mentals[] = {
"Willpower",
"Memory",
"Focus",
"Intuition",
"Patience",
"Empathy",
"SocialAwareness",
"Creatvity", //Speeling deliberate
"Musicality",
"AnalyticalAbility",
"LinguisticAbility",
"SpatialSense",
"KinaestheticSense",
};
static void element(const char* name, const char* content, ostream& out, const char* extra_indent="") {
out << extra_indent << " <" << name << ">" << content << "</" << name << ">" << endl;
}
static void element(const char* name, const uint32_t content, ostream& out, const char* extra_indent="") {
out << extra_indent << " <" << name << ">" << content << "</" << name << ">" << endl;
}
2012-01-27 06:46:42 -07:00
static void printAttributes(Core* c, df::unit* cre, ostream& out) {
2011-12-23 02:58:45 -07:00
out << " <Attributes>" << endl;
for (int i = 0; i < NUM_CREATURE_PHYSICAL_ATTRIBUTES; i++) {
2012-01-27 06:46:42 -07:00
element(physicals[i], cre->body.physical_attrs[i].unk1, out, " ");
2011-12-23 02:58:45 -07:00
}
2012-01-27 06:46:42 -07:00
df::unit_soul * s = cre->status.current_soul;
2011-12-23 02:58:45 -07:00
if (s) {
for (int i = 0; i < NUM_CREATURE_MENTAL_ATTRIBUTES; i++) {
2012-01-27 06:46:42 -07:00
element(mentals[i], s->mental_attrs[i].unk1, out, " ");
2011-12-23 02:58:45 -07:00
}
}
out << " </Attributes>" << endl;
}
2012-01-27 06:46:42 -07:00
static void printTraits(Core* c, df::unit* cre, ostream& out) {
2011-12-23 02:58:45 -07:00
out << " <Traits>" << endl;
2012-01-27 06:46:42 -07:00
df::unit_soul * s = cre->status.current_soul;
2011-12-23 02:58:45 -07:00
if (s) {
for (int i = 0; i < NUM_CREATURE_TRAITS; i++) {
2011-12-28 12:59:10 -07:00
out << " <Trait name='" << c->vinfo->getTraitName(i) <<
"' value='" << s->traits[i] << "'>";
2011-12-23 02:58:45 -07:00
string trait = c->vinfo->getTrait(i, s->traits[i]);
if (!trait.empty()) {
2011-12-28 12:59:10 -07:00
out << trait.c_str();
2011-12-23 02:58:45 -07:00
}
2011-12-28 12:59:10 -07:00
out << "</Trait>" << endl;
2011-12-23 02:58:45 -07:00
}
}
out << " </Traits>" << endl;
}
// GDC needs:
// Name
// Nickname
// Sex
// Attributes
// Traits
2012-01-27 06:46:42 -07:00
static void export_dwarf(Core* c, df::unit* cre, ostream& out) {
2011-12-23 02:58:45 -07:00
string info = cre->name.first_name;
info += " ";
2012-01-27 06:46:42 -07:00
info += Translation::TranslateName(&cre->name, false);
2011-12-23 02:58:45 -07:00
info[0] = toupper(info[0]);
c->con.print("Exporting %s\n", info.c_str());
out << " <Creature>" << endl;
element("Name", info.c_str(), out);
2012-01-27 06:46:42 -07:00
element("Nickname", cre->name.nickname.c_str(), out);
2011-12-23 02:58:45 -07:00
element("Sex", cre->sex == 0 ? "Female" : "Male", out);
2012-01-27 06:46:42 -07:00
printAttributes(c, cre, out);
printTraits(c, cre, out);
2011-12-23 02:58:45 -07:00
out << " </Creature>" << endl;
}
DFhackCExport command_result export_dwarves (Core * c, std::vector <std::string> & parameters)
{
string filename;
if (parameters.size() == 1) {
filename = parameters[0];
} else {
c->con.print("export <filename>\n");
return CR_OK;
}
ofstream outf(filename);
if (!outf) {
c->con.printerr("Failed to open file %s\n", filename.c_str());
return CR_FAILURE;
}
c->Suspend();
2012-01-27 06:46:42 -07:00
uint32_t race = ui->race_id;
uint32_t civ = ui->civ_id;
2011-12-23 02:58:45 -07:00
2011-12-28 13:22:39 -07:00
outf << "<?xml version='1.0' encoding='ibm850'?>" << endl << "<Creatures>" << endl;
2011-12-23 02:58:45 -07:00
2012-01-27 06:46:42 -07:00
for (int i = 0; i < world->units.all.size(); ++i)
2011-12-23 02:58:45 -07:00
{
2012-01-27 06:46:42 -07:00
df::unit* cre = world->units.all[i];
if (cre->race == race && cre->civ_id == civ) {
export_dwarf(c, cre, outf);
2011-12-23 02:58:45 -07:00
}
}
outf << "</Creatures>" << endl;
c->Resume();
return CR_OK;
}