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>
|
2012-03-09 13:13:07 -07:00
|
|
|
#include <df/unit_labor.h>
|
|
|
|
#include <df/unit_skill.h>
|
2012-01-27 06:46:42 -07:00
|
|
|
|
2011-12-23 02:58:45 -07:00
|
|
|
using namespace DFHack;
|
2012-01-27 06:46:42 -07:00
|
|
|
using df::global::ui;
|
|
|
|
using df::global::world;
|
2011-12-23 02:58:45 -07:00
|
|
|
|
|
|
|
// our own, empty header.
|
2012-02-05 12:41:12 -07:00
|
|
|
#include "dwarfexport.h"
|
2012-02-08 19:07:26 -07:00
|
|
|
#include <df/personality_facet_type.h>
|
2011-12-23 02:58:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Here go all the command declarations...
|
|
|
|
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result export_dwarves (color_ostream &con, std::vector <std::string> & parameters);
|
2011-12-23 02:58:45 -07:00
|
|
|
|
2012-02-21 10:19:17 -07:00
|
|
|
DFHACK_PLUGIN("dwarfexport");
|
2011-12-23 02:58:45 -07:00
|
|
|
|
|
|
|
// Mandatory init function. If you have some global state, create it here.
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init (color_ostream &con, std::vector <PluginCommand> &commands)
|
2011-12-23 02:58:45 -07:00
|
|
|
{
|
|
|
|
// Fill the command list with your commands.
|
2012-02-05 12:41:12 -07:00
|
|
|
commands.push_back(PluginCommand("dwarfexport",
|
2011-12-23 02:58:45 -07:00
|
|
|
"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.
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown (color_ostream &con)
|
2011-12-23 02:58:45 -07:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:41:12 -07:00
|
|
|
static const char* physicals[] = {
|
2011-12-23 02:58:45 -07:00
|
|
|
"Strength",
|
|
|
|
"Agility",
|
|
|
|
"Toughness",
|
|
|
|
"Endurance",
|
|
|
|
"Recuperation",
|
|
|
|
"DiseaseResistance",
|
|
|
|
};
|
|
|
|
|
2012-02-05 12:41:12 -07:00
|
|
|
static const char* mentals[] = {
|
2012-03-14 05:57:53 -06:00
|
|
|
"AnalyticalAbility",
|
2011-12-23 02:58:45 -07:00
|
|
|
"Focus",
|
2012-03-14 05:57:53 -06:00
|
|
|
"Willpower",
|
|
|
|
"Creatvity", //Speeling deliberate
|
2011-12-23 02:58:45 -07:00
|
|
|
"Intuition",
|
|
|
|
"Patience",
|
2012-03-14 05:57:53 -06:00
|
|
|
"Memory",
|
2011-12-23 02:58:45 -07:00
|
|
|
"LinguisticAbility",
|
|
|
|
"SpatialSense",
|
2012-03-14 05:57:53 -06:00
|
|
|
"Musicality",
|
2011-12-23 02:58:45 -07:00
|
|
|
"KinaestheticSense",
|
2012-03-14 05:57:53 -06:00
|
|
|
"Empathy",
|
|
|
|
"SocialAwareness",
|
2011-12-23 02:58:45 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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-03-10 04:55:42 -07:00
|
|
|
static void printAttributes(color_ostream &con, 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-03-10 04:55:42 -07:00
|
|
|
static void printTraits(color_ostream &con, df::unit* cre, ostream& out)
|
2012-02-08 19:07:26 -07:00
|
|
|
{
|
|
|
|
|
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;
|
2012-02-08 19:07:26 -07:00
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
FOR_ENUM_ITEMS(personality_facet_type,index)
|
|
|
|
{
|
2012-03-15 19:43:27 -06:00
|
|
|
out << " <Trait name='" << ENUM_KEY_STR(personality_facet_type, index) <<
|
2012-02-08 19:07:26 -07:00
|
|
|
"' value='" << s->traits[index] << "'>";
|
|
|
|
//FIXME: needs reimplementing trait string generation
|
|
|
|
/*
|
2012-03-10 04:55:42 -07:00
|
|
|
string trait = con->vinfo->getTrait(i, s->traits[i]);
|
2011-12-23 02:58:45 -07:00
|
|
|
if (!trait.empty()) {
|
2011-12-28 12:59:10 -07:00
|
|
|
out << trait.c_str();
|
2011-12-23 02:58:45 -07:00
|
|
|
}
|
2012-02-08 19:07:26 -07:00
|
|
|
*/
|
2011-12-28 12:59:10 -07:00
|
|
|
out << "</Trait>" << endl;
|
2012-02-08 19:07:26 -07:00
|
|
|
|
2011-12-23 02:58:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out << " </Traits>" << endl;
|
|
|
|
}
|
|
|
|
|
2012-03-09 13:13:07 -07:00
|
|
|
static int32_t getCreatureAge(df::unit* cre)
|
|
|
|
{
|
|
|
|
int32_t yearDifference = *df::global::cur_year - cre->relations.birth_year;
|
|
|
|
|
|
|
|
// If the birthday this year has not yet passed, subtract one year.
|
|
|
|
// ASSUMPTION: birth_time is on the same scale as cur_year_tick
|
|
|
|
if (cre->relations.birth_time >= *df::global::cur_year_tick) {
|
|
|
|
yearDifference--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return yearDifference;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void printLabors(color_ostream &con, df::unit* cre, ostream& out)
|
2012-03-09 13:13:07 -07:00
|
|
|
{
|
|
|
|
// Using British spelling here, consistent with Runesmith
|
|
|
|
out << " <Labours>" << endl;
|
2012-03-15 19:43:27 -06:00
|
|
|
FOR_ENUM_ITEMS(unit_labor, iCount)
|
2012-03-09 13:13:07 -07:00
|
|
|
{
|
|
|
|
if (cre->status.labors[iCount]) {
|
|
|
|
// Get the caption for the labor index.
|
2012-03-15 19:43:27 -06:00
|
|
|
element("Labour", ENUM_ATTR_STR(unit_labor, caption, iCount), out);
|
2012-03-09 13:13:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out << " </Labours>" << endl;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void printSkill(color_ostream &con, df::unit_skill* skill, ostream& out)
|
2012-03-09 13:13:07 -07:00
|
|
|
{
|
|
|
|
out << " <Skill>" << endl;
|
|
|
|
|
2012-03-15 19:43:27 -06:00
|
|
|
element("Name", ENUM_ATTR_STR(job_skill, caption, skill->id), out);
|
2012-03-09 13:13:07 -07:00
|
|
|
element("Level", skill->rating, out);
|
|
|
|
|
|
|
|
out << " </Skill>" << endl;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void printSkills(color_ostream &con, df::unit* cre, ostream& out)
|
2012-03-09 13:13:07 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<df::unit_skill* > vSkills = cre->status.current_soul->skills;
|
|
|
|
|
|
|
|
out << " <Skills>" << endl;
|
|
|
|
for (int iCount = 0; iCount < vSkills.size(); iCount++)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
printSkill(con, vSkills.at(iCount), out);
|
2012-03-09 13:13:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
out << " </Skills>" << endl;
|
|
|
|
}
|
|
|
|
|
2011-12-23 02:58:45 -07:00
|
|
|
// GDC needs:
|
|
|
|
// Name
|
|
|
|
// Nickname
|
|
|
|
// Sex
|
|
|
|
// Attributes
|
|
|
|
// Traits
|
2012-03-10 04:55:42 -07:00
|
|
|
static void export_dwarf(color_ostream &con, 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]);
|
2012-03-10 04:55:42 -07:00
|
|
|
con.print("Exporting %s\n", info.c_str());
|
2012-03-09 13:13:07 -07:00
|
|
|
|
2011-12-23 02:58:45 -07:00
|
|
|
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-03-09 13:13:07 -07:00
|
|
|
element("Age", getCreatureAge(cre), out); // Added age, active labors, and skills March 9, 2012
|
2012-03-10 04:55:42 -07:00
|
|
|
printAttributes(con, cre, out);
|
|
|
|
printTraits(con, cre, out);
|
|
|
|
printLabors(con, cre, out);
|
|
|
|
printSkills(con, cre, out);
|
2011-12-23 02:58:45 -07:00
|
|
|
|
|
|
|
out << " </Creature>" << endl;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result export_dwarves (color_ostream &con, std::vector <std::string> & parameters)
|
2011-12-23 02:58:45 -07:00
|
|
|
{
|
|
|
|
string filename;
|
|
|
|
if (parameters.size() == 1) {
|
|
|
|
filename = parameters[0];
|
|
|
|
} else {
|
2012-03-10 04:55:42 -07:00
|
|
|
con.print("export <filename>\n");
|
2011-12-23 02:58:45 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofstream outf(filename);
|
|
|
|
if (!outf) {
|
2012-03-10 04:55:42 -07:00
|
|
|
con.printerr("Failed to open file %s\n", filename.c_str());
|
2011-12-23 02:58:45 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
CoreSuspender suspend;
|
2011-12-23 02:58:45 -07:00
|
|
|
|
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) {
|
2012-03-10 04:55:42 -07:00
|
|
|
export_dwarf(con, cre, outf);
|
2011-12-23 02:58:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
outf << "</Creatures>" << endl;
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|