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);
PyErr_SetString(PyExc_StandardError, errBuff)
PyErr_SetString(PyExc_StandardError, errBuff);
return NULL;
}

@ -28,16 +28,60 @@ distribution.
#include "Python.h"
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#include "DFTypes.h"
#include "DF_Imports.cpp"
using namespace DFHack;
#include "modules/Creatures.h"
#include "modules/Materials.h"
#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)
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)
{
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)
{
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)
@ -75,24 +127,20 @@ static PyObject* BuildJob(DFHack::t_job& job)
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;
static PyObject* BuildItemType(DFHack::t_itemType& item)
{
PyObject *id, *name;
args = Py_BuildValue("IIIIIII", at.level, at.field_4, at.field_8, at.field_C, at.leveldiff, at.field_14, at.field_18);
if(item.id[0])
id = PyString_FromString(item.id);
else
id = PyString_FromString("");
attrObj = PyObject_CallObject(Attribute_type, args);
if(item.name[0])
name = PyString_FromString(item.name);
else
name = PyString_FromString("");
Py_DECREF(args);
return Py_BuildValue("OO", id, name);
return attrObj;
}
static PyObject* BuildItemType(DFHack::t_itemType& item)
{
return Py_BuildValue("ss", item.id, item.name);
}
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)
{
PyObject* noteObj;
PyObject *args, *name, *position;
PyObject *args, *position;
if(note.name[0])
name = PyString_FromString(note.name);
else
name = PyString_FromString("");
args = Py_BuildValue("III", note.x, note.y, note.z);
position = PyObject_CallObject(Position3D_type, args);
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);
Py_DECREF(args);
return noteObj;
}
@ -245,33 +294,36 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj)
static PyObject* BuildSettlement(DFHack::t_settlement& settlement)
{
PyObject* setDict;
PyObject *local_pos1, *local_pos2;
PyObject* temp;
PyObject* setObj;
PyObject *world_pos, *local_pos, *args;
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);
DICTADD(setDict, "origin", temp);
Py_DECREF(args);
temp = BuildName(settlement.name);
DICTADD(setDict, "name", temp);
args = Py_BuildValue("iiii", settlement.local_x1, settlement.local_y1, settlement.local_x2, settlement.local_y2);
temp = Py_BuildValue("ii", settlement.world_x, settlement.world_y);
DICTADD(setDict, "world_pos", temp);
local_pos = PyObject_CallObject(Rectangle_type, args);
local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1);
local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2);
Py_DECREF(args);
temp = Py_BuildValue("OO", local_pos1, local_pos2);
DICTADD(setDict, "local_pos", temp);
args = Py_BuildValue("iOOO", settlement.origin, BuildName(settlement.name), world_pos, local_pos);
return setDict;
setObj = PyObject_CallObject(Settlement_type, args);
Py_DECREF(args);
return setObj;
}
static PyObject* BuildSoul(DFHack::t_soul& soul)
{
PyObject *soulDict, *skillList, *temp;
PyObject *soulDict, *skillList, *temp, *emptyArgs;
PyObject* soulObj;
emptyArgs = Py_BuildValue("()");
soulDict = PyDict_New();
@ -317,7 +369,11 @@ static PyObject* BuildSoul(DFHack::t_soul& soul)
temp = BuildAttribute(soul.social_awareness);
DICTADD(soulDict, "social_awareness", temp);
return soulDict;
soulObj = PyObject_Call(Soul_type, emptyArgs, soulDict);
Py_DECREF(emptyArgs);
return soulObj;
}
#endif

@ -43,10 +43,23 @@ static PyObject* MapBlock40d_type = NULL;
static PyObject* Vein_type = NULL;
static PyObject* FrozenLiquidVein_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()
{
if(TypesModule == NULL)
if(FlagsModule == NULL)
{
FlagsModule = PyImport_ImportModule("pydfhackflags");
@ -56,7 +69,9 @@ static void DoImports()
OccupancyFlags_type = PyObject_GetAttrString(FlagsModule, "OccupancyFlags");
ItemFlags_type = PyObject_GetAttrString(FlagsModule, "ItemFlags");
BlockFlags_type = PyObject_GetAttrString(FlagsModule, "BlockFlags");
}
if(TypesModule == NULL)
{
TypesModule = PyImport_ImportModule("pydftypes");
Note_type = PyObject_GetAttrString(TypesModule, "Note");
@ -66,6 +81,19 @@ static void DoImports()
Vein_type = PyObject_GetAttrString(TypesModule, "Vein");
FrozenLiquidVein_type = PyObject_GetAttrString(TypesModule, "FrozenLiquidVein");
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;
#include "modules/Materials.h"
#include "DF_Imports.cpp"
#include "DF_Helpers.cpp"
using namespace DFHack;
@ -44,23 +46,16 @@ struct DF_Material
static PyObject* BuildMatgloss(t_matgloss& matgloss)
{
PyObject* matDict;
PyObject* list;
matDict = PyDict_New();
list = PyList_New(5);
PyObject* matObj;
PyObject* args;
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "id", matgloss.id));
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));
args = Py_BuildValue("siiis", matgloss.id, matgloss.fore, matgloss.back, matgloss.bright, 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)
@ -122,23 +117,16 @@ static PyObject* BuildMatglossList(std::vector<t_matgloss> & matVec)
static PyObject* BuildDescriptorColor(t_descriptor_color& color)
{
PyObject* matDict;
PyObject* list;
matDict = PyDict_New();
list = PyList_New(5);
PyObject* descObj;
PyObject* args;
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "id", color.id));
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));
args = Py_BuildValue("sfffs", color.id, color.r, color.v, color.b, 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)
@ -162,22 +150,16 @@ static PyObject* BuildDescriptorColorList(std::vector<t_descriptor_color>& color
static PyObject* BuildCreatureCaste(t_creaturecaste& caste)
{
PyObject* matDict;
PyObject* list;
PyObject* casteObj;
PyObject* args;
matDict = PyDict_New();
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));
args = Py_BuildValue("ssss", caste.rawname, caste.singular, caste.plural, 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)
@ -201,23 +183,17 @@ static PyObject* BuildCreatureCasteList(std::vector<t_creaturecaste>& castes)
static PyObject* BuildCreatureTypeEx(t_creaturetype& creature)
{
PyObject* c_type;
PyObject* list;
PyObject* cObj;
PyObject* args;
c_type = PyDict_New();
list = PyList_New(6);
args = Py_BuildValue("sOiO", creature.rawname, BuildCreatureCasteList(creature.castes), creature.tile_character, \
BuildTileColor(creature.tilecolor.fore, creature.tilecolor.back, creature.tilecolor.bright));
PyList_SET_ITEM(list, 0, Py_BuildValue("ss", "rawname", creature.rawname));
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));
cObj = PyObject_CallObject(CreatureTypeEx_type, args);
PyDict_MergeFromSeq2(c_type, list, 0);
Py_DECREF(list);
Py_DECREF(args);
return c_type;
return cObj;
}
static PyObject* BuildCreatureTypeExList(std::vector<t_creaturetype>& creatures)