updated to use new namedtuples

develop
doomchild 2010-04-23 14:32:38 -05:00
parent 8eacdbe7f9
commit ee9da5910b
4 changed files with 153 additions and 93 deletions

@ -201,7 +201,7 @@ static PyObject* DF_CreatureManager_WriteLabors(DF_CreatureManager* self, PyObje
sprintf(errBuff, "list must contain at least %u entries", NUM_CREATURE_LABORS); sprintf(errBuff, "list must contain at least %u entries", NUM_CREATURE_LABORS);
PyErr_SetString(PyExc_StandardError, errBuff) PyErr_SetString(PyExc_StandardError, errBuff);
return NULL; return NULL;
} }

@ -28,16 +28,60 @@ distribution.
#include "Python.h" #include "Python.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <vector>
using namespace std;
#include "DFTypes.h" #include "DFTypes.h"
#include "DF_Imports.cpp" #include "DF_Imports.cpp"
using namespace DFHack; using namespace DFHack;
#include "modules/Creatures.h" #include "modules/Creatures.h"
#include "modules/Materials.h"
#define DICTADD(d, name, item) PyDict_SetItemString(d, name, item); Py_DECREF(item) #define DICTADD(d, name, item) PyDict_SetItemString(d, name, item); Py_DECREF(item)
#define OBJSET(o, name, item) PyObject_SetAttrString(o, name, item); Py_DECREF(item) #define OBJSET(o, name, item) PyObject_SetAttrString(o, name, item); Py_DECREF(item)
static PyObject* BuildTileColor(uint16_t fore, uint16_t back, uint16_t bright)
{
PyObject *tObj, *args;
args = Py_BuildValue("iii", fore, back, bright);
tObj = PyObject_CallObject(TileColor_type, args);
Py_DECREF(args);
return tObj;
}
static PyObject* BuildPosition2D(uint16_t x, uint16_t y)
{
PyObject *posObj, *args;
args = Py_BuildValue("ii", x, y);
posObj = PyObject_CallObject(Position2D_type, args);
Py_DECREF(args);
return posObj;
}
static PyObject* BuildPosition3D(uint16_t x, uint16_t y, uint16_t z)
{
PyObject *posObj, *args;
args = Py_BuildValue("iii", x, y, z);
posObj = PyObject_CallObject(Position3D_type, args);
Py_DECREF(args);
return posObj;
}
static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss)
{ {
return Py_BuildValue("ii", matgloss.type, matgloss.index); return Py_BuildValue("ii", matgloss.type, matgloss.index);
@ -55,7 +99,15 @@ static DFHack::t_matglossPair ReverseBuildMatglossPair(PyObject* mObj)
static PyObject* BuildSkill(DFHack::t_skill& skill) static PyObject* BuildSkill(DFHack::t_skill& skill)
{ {
return Py_BuildValue("III", skill.id, skill.experience, skill.rating); PyObject *args, *skillObj;
args = Py_BuildValue("III", skill.id, skill.experience, skill.rating);
skillObj = PyObject_CallObject(Skill_type, args);
Py_DECREF(args);
return skillObj;
} }
static PyObject* BuildSkillList(DFHack::t_skill (&skills)[256], uint8_t numSkills) static PyObject* BuildSkillList(DFHack::t_skill (&skills)[256], uint8_t numSkills)
@ -75,24 +127,20 @@ static PyObject* BuildJob(DFHack::t_job& job)
static PyObject* BuildAttribute(DFHack::t_attrib& at) static PyObject* BuildAttribute(DFHack::t_attrib& at)
{ {
return Py_BuildValue("IIIIIII", at.level, at.field_4, at.field_8, at.field_C, at.leveldiff, at.field_14, at.field_18); PyObject *args, *attrObj;
args = Py_BuildValue("IIIIIII", at.level, at.field_4, at.field_8, at.field_C, at.leveldiff, at.field_14, at.field_18);
attrObj = PyObject_CallObject(Attribute_type, args);
Py_DECREF(args);
return attrObj;
} }
static PyObject* BuildItemType(DFHack::t_itemType& item) static PyObject* BuildItemType(DFHack::t_itemType& item)
{ {
PyObject *id, *name; return Py_BuildValue("ss", item.id, item.name);
if(item.id[0])
id = PyString_FromString(item.id);
else
id = PyString_FromString("");
if(item.name[0])
name = PyString_FromString(item.name);
else
name = PyString_FromString("");
return Py_BuildValue("OO", id, name);
} }
static PyObject* BuildLike(DFHack::t_like& like) static PyObject* BuildLike(DFHack::t_like& like)
@ -107,19 +155,20 @@ static PyObject* BuildLike(DFHack::t_like& like)
static PyObject* BuildNote(DFHack::t_note& note) static PyObject* BuildNote(DFHack::t_note& note)
{ {
PyObject* noteObj; PyObject* noteObj;
PyObject *args, *name, *position; PyObject *args, *position;
if(note.name[0]) args = Py_BuildValue("III", note.x, note.y, note.z);
name = PyString_FromString(note.name);
else position = PyObject_CallObject(Position3D_type, args);
name = PyString_FromString("");
position = Py_BuildValue("III", note.x, note.y, note.z); Py_DECREF(args);
args = Py_BuildValue("cIIsO", note.symbol, note.foreground, note.background, name, position); args = Py_BuildValue("cIIsO", note.symbol, note.foreground, note.background, note.name, position);
noteObj = PyObject_CallObject(Note_type, args); noteObj = PyObject_CallObject(Note_type, args);
Py_DECREF(args);
return noteObj; return noteObj;
} }
@ -245,33 +294,36 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj)
static PyObject* BuildSettlement(DFHack::t_settlement& settlement) static PyObject* BuildSettlement(DFHack::t_settlement& settlement)
{ {
PyObject* setDict; PyObject* setObj;
PyObject *local_pos1, *local_pos2; PyObject *world_pos, *local_pos, *args;
PyObject* temp;
args = Py_BuildValue("ii", settlement.world_x, settlement.world_y);
setDict = PyDict_New(); world_pos = PyObject_CallObject(Position2D_type, args);
temp = PyInt_FromLong(settlement.origin); Py_DECREF(args);
DICTADD(setDict, "origin", temp);
temp = BuildName(settlement.name); args = Py_BuildValue("iiii", settlement.local_x1, settlement.local_y1, settlement.local_x2, settlement.local_y2);
DICTADD(setDict, "name", temp);
temp = Py_BuildValue("ii", settlement.world_x, settlement.world_y); local_pos = PyObject_CallObject(Rectangle_type, args);
DICTADD(setDict, "world_pos", temp);
local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1); Py_DECREF(args);
local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2);
temp = Py_BuildValue("OO", local_pos1, local_pos2); args = Py_BuildValue("iOOO", settlement.origin, BuildName(settlement.name), world_pos, local_pos);
DICTADD(setDict, "local_pos", temp);
return setDict; setObj = PyObject_CallObject(Settlement_type, args);
Py_DECREF(args);
return setObj;
} }
static PyObject* BuildSoul(DFHack::t_soul& soul) static PyObject* BuildSoul(DFHack::t_soul& soul)
{ {
PyObject *soulDict, *skillList, *temp; PyObject *soulDict, *skillList, *temp, *emptyArgs;
PyObject* soulObj;
emptyArgs = Py_BuildValue("()");
soulDict = PyDict_New(); soulDict = PyDict_New();
@ -317,7 +369,11 @@ static PyObject* BuildSoul(DFHack::t_soul& soul)
temp = BuildAttribute(soul.social_awareness); temp = BuildAttribute(soul.social_awareness);
DICTADD(soulDict, "social_awareness", temp); DICTADD(soulDict, "social_awareness", temp);
return soulDict; soulObj = PyObject_Call(Soul_type, emptyArgs, soulDict);
Py_DECREF(emptyArgs);
return soulObj;
} }
#endif #endif

@ -43,10 +43,23 @@ static PyObject* MapBlock40d_type = NULL;
static PyObject* Vein_type = NULL; static PyObject* Vein_type = NULL;
static PyObject* FrozenLiquidVein_type = NULL; static PyObject* FrozenLiquidVein_type = NULL;
static PyObject* SpatterVein_type = NULL; static PyObject* SpatterVein_type = NULL;
static PyObject* Position2D_type = NULL;
static PyObject* Position3D_type = NULL;
static PyObject* Rectangle_type = NULL;
static PyObject* Settlement_type = NULL;
static PyObject* Attribute_type = NULL;
static PyObject* Skill_type = NULL;
static PyObject* Soul_type = NULL;
static PyObject* Tree_type = NULL;
static PyObject* CreatureCaste_type = NULL;
static PyObject* Matgloss_type = NULL;
static PyObject* DescriptorColor_type = NULL;
static PyObject* CreatureTypeEx_type = NULL;
static PyObject* TileColor_type = NULL;
static void DoImports() static void DoImports()
{ {
if(TypesModule == NULL) if(FlagsModule == NULL)
{ {
FlagsModule = PyImport_ImportModule("pydfhackflags"); FlagsModule = PyImport_ImportModule("pydfhackflags");
@ -56,7 +69,9 @@ static void DoImports()
OccupancyFlags_type = PyObject_GetAttrString(FlagsModule, "OccupancyFlags"); OccupancyFlags_type = PyObject_GetAttrString(FlagsModule, "OccupancyFlags");
ItemFlags_type = PyObject_GetAttrString(FlagsModule, "ItemFlags"); ItemFlags_type = PyObject_GetAttrString(FlagsModule, "ItemFlags");
BlockFlags_type = PyObject_GetAttrString(FlagsModule, "BlockFlags"); BlockFlags_type = PyObject_GetAttrString(FlagsModule, "BlockFlags");
}
if(TypesModule == NULL)
{
TypesModule = PyImport_ImportModule("pydftypes"); TypesModule = PyImport_ImportModule("pydftypes");
Note_type = PyObject_GetAttrString(TypesModule, "Note"); Note_type = PyObject_GetAttrString(TypesModule, "Note");
@ -66,6 +81,19 @@ static void DoImports()
Vein_type = PyObject_GetAttrString(TypesModule, "Vein"); Vein_type = PyObject_GetAttrString(TypesModule, "Vein");
FrozenLiquidVein_type = PyObject_GetAttrString(TypesModule, "FrozenLiquidVein"); FrozenLiquidVein_type = PyObject_GetAttrString(TypesModule, "FrozenLiquidVein");
SpatterVein_type = PyObject_GetAttrString(TypesModule, "SpatterVein"); SpatterVein_type = PyObject_GetAttrString(TypesModule, "SpatterVein");
Position2D_type = PyObject_GetAttrString(TypesModule, "Position2D");
Position3D_type = PyObject_GetAttrString(TypesModule, "Position3D");
Rectangle_type = PyObject_GetAttrString(TypesModule, "Rectangle");
Settlement_type = PyObject_GetAttrString(TypesModule, "Settlement");
Attribute_type = PyObject_GetAttrString(TypesModule, "Attribute");
Skill_type = PyObject_GetAttrString(TypesModule, "Skill");
Soul_type = PyObject_GetAttrString(TypesModule, "Soul");
Tree_type = PyObject_GetAttrString(TypesModule, "Tree");
CreatureCaste_type = PyObject_GetAttrString(TypesModule, "CreatureCaste");
Matgloss_type = PyObject_GetAttrString(TypesModule, "Matgloss");
DescriptorColor_type = PyObject_GetAttrString(TypesModule, "DescriptorColor");
CreatureTypeEx_type = PyObject_GetAttrString(TypesModule, "CreatureTypeEx");
TileColor_type = PyObject_GetAttrString(TypesModule, "TileColor");
} }
} }

@ -31,6 +31,8 @@ distribution.
using namespace std; using namespace std;
#include "modules/Materials.h" #include "modules/Materials.h"
#include "DF_Imports.cpp"
#include "DF_Helpers.cpp"
using namespace DFHack; using namespace DFHack;
@ -44,23 +46,16 @@ struct DF_Material
static PyObject* BuildMatgloss(t_matgloss& matgloss) static PyObject* BuildMatgloss(t_matgloss& matgloss)
{ {
PyObject* matDict; PyObject* matObj;
PyObject* list; PyObject* args;
matDict = PyDict_New();
list = PyList_New(5);
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "id", matgloss.id)); args = Py_BuildValue("siiis", matgloss.id, matgloss.fore, matgloss.back, matgloss.bright, matgloss.name);
PyList_SET_ITEM(list, 1, Py_BuildValue("si", "fore", matgloss.fore));
PyList_SET_ITEM(list, 2, Py_BuildValue("si", "back", matgloss.back));
PyList_SET_ITEM(list, 3, Py_BuildValue("si", "bright", matgloss.bright));
PyList_SET_ITEM(list, 4, Py_BuildValue("ss", "name", matgloss.name));
PyDict_MergeFromSeq2(matDict, list, 0); matObj = PyObject_CallObject(Matgloss_type, args);
Py_DECREF(list); Py_DECREF(args);
return matDict; return matObj;
} }
static PyObject* BuildMatglossPlant(t_matglossPlant& matgloss) static PyObject* BuildMatglossPlant(t_matglossPlant& matgloss)
@ -122,23 +117,16 @@ static PyObject* BuildMatglossList(std::vector<t_matgloss> & matVec)
static PyObject* BuildDescriptorColor(t_descriptor_color& color) static PyObject* BuildDescriptorColor(t_descriptor_color& color)
{ {
PyObject* matDict; PyObject* descObj;
PyObject* list; PyObject* args;
matDict = PyDict_New();
list = PyList_New(5);
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "id", color.id)); args = Py_BuildValue("sfffs", color.id, color.r, color.v, color.b, color.name);
PyList_SET_ITEM(list, 1, Py_BuildValue("sf", "r", color.r));
PyList_SET_ITEM(list, 2, Py_BuildValue("sf", "v", color.v));
PyList_SET_ITEM(list, 3, Py_BuildValue("sf", "b", color.b));
PyList_SET_ITEM(list, 4, Py_BuildValue("ss", "name", color.name));
PyDict_MergeFromSeq2(matDict, list, 0); descObj = PyObject_CallObject(DescriptorColor_type, args);
Py_DECREF(list); Py_DECREF(args);
return matDict; return descObj;
} }
static PyObject* BuildDescriptorColorList(std::vector<t_descriptor_color>& colors) static PyObject* BuildDescriptorColorList(std::vector<t_descriptor_color>& colors)
@ -162,22 +150,16 @@ static PyObject* BuildDescriptorColorList(std::vector<t_descriptor_color>& color
static PyObject* BuildCreatureCaste(t_creaturecaste& caste) static PyObject* BuildCreatureCaste(t_creaturecaste& caste)
{ {
PyObject* matDict; PyObject* casteObj;
PyObject* list; PyObject* args;
matDict = PyDict_New(); args = Py_BuildValue("ssss", caste.rawname, caste.singular, caste.plural, caste.adjective);
list = PyList_New(4);
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "rawname", caste.rawname));
PyList_SET_ITEM(list, 1, Py_BuildValue("ss", "singular", caste.singular));
PyList_SET_ITEM(list, 2, Py_BuildValue("ss", "plural", caste.plural));
PyList_SET_ITEM(list, 3, Py_BuildValue("ss", "adjective", caste.adjective));
PyDict_MergeFromSeq2(matDict, list, 0); casteObj = PyObject_CallObject(CreatureCaste_type, args);
Py_DECREF(list); Py_DECREF(args);
return matDict; return casteObj;
} }
static PyObject* BuildCreatureCasteList(std::vector<t_creaturecaste>& castes) static PyObject* BuildCreatureCasteList(std::vector<t_creaturecaste>& castes)
@ -201,23 +183,17 @@ static PyObject* BuildCreatureCasteList(std::vector<t_creaturecaste>& castes)
static PyObject* BuildCreatureTypeEx(t_creaturetype& creature) static PyObject* BuildCreatureTypeEx(t_creaturetype& creature)
{ {
PyObject* c_type; PyObject* cObj;
PyObject* list; PyObject* args;
c_type = PyDict_New(); args = Py_BuildValue("sOiO", creature.rawname, BuildCreatureCasteList(creature.castes), creature.tile_character, \
list = PyList_New(6); BuildTileColor(creature.tilecolor.fore, creature.tilecolor.back, creature.tilecolor.bright));
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "rawname", creature.rawname)); cObj = PyObject_CallObject(CreatureTypeEx_type, args);
PyList_SET_ITEM(list, 1, Py_BuildValue("sO", "castes", BuildCreatureCasteList(creature.castes)));
PyList_SET_ITEM(list, 2, Py_BuildValue("si", "tile_character", creature.tile_character));
PyList_SET_ITEM(list, 3, Py_BuildValue("si", "fore", creature.tilecolor.fore));
PyList_SET_ITEM(list, 4, Py_BuildValue("si", "back", creature.tilecolor.back));
PyList_SET_ITEM(list, 5, Py_BuildValue("si", "bright", creature.tilecolor.bright));
PyDict_MergeFromSeq2(c_type, list, 0); Py_DECREF(args);
Py_DECREF(list);
return c_type; return cObj;
} }
static PyObject* BuildCreatureTypeExList(std::vector<t_creaturetype>& creatures) static PyObject* BuildCreatureTypeExList(std::vector<t_creaturetype>& creatures)