From 5c9711c6410e415140ed14180dfe058d8f9078d0 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sun, 11 Apr 2010 21:07:23 -0500 Subject: [PATCH 01/34] added BuildSoul --- dfhack/python/DF_Helpers.cpp | 133 ++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 50 deletions(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 82890a4d8..97fe615f6 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -33,6 +33,8 @@ using namespace DFHack; #include "modules/Creatures.h" +#define DICTADD(d, name, item) PyDict_SetItemString(d, name, item); Py_DECREF(item) + static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) { return Py_BuildValue("ii", matgloss.type, matgloss.index); @@ -48,11 +50,26 @@ static PyObject* BuildSkill(DFHack::t_skill& skill) return Py_BuildValue("III", skill.id, skill.experience, skill.rating); } +static PyObject* BuildSkillList(DFHack::t_skill (&skills)[256], uint8_t numSkills) +{ + PyObject* list = PyList_New(numSkills); + + for(int i = 0; i < numSkills; i++) + PyList_SET_ITEM(list, i, BuildSkill(skills[i])); + + return list; +} + static PyObject* BuildJob(DFHack::t_job& job) { return Py_BuildValue("Oi", PyBool_FromLong((int)job.active), job.jobId); } +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); +} + static PyObject* BuildItemType(DFHack::t_itemType& item) { PyObject *id, *name; @@ -86,31 +103,20 @@ static PyObject* BuildNote(DFHack::t_note& note) PyObject* temp; temp = PyString_FromFormat("%c", note.symbol); - - PyDict_SetItemString(noteDict, "symbol", temp); - - Py_DECREF(temp); + DICTADD(noteDict, "symbol", temp); temp = Py_BuildValue("II", note.foreground, note.background); - - PyDict_SetItemString(noteDict, "fore_back", temp); - - Py_DECREF(temp); + DICTADD(noteDict, "fore_back", temp); if(note.name[0]) temp = PyString_FromString(note.name); else PyString_FromString(""); - - PyDict_SetItemString(noteDict, "name", temp); - Py_DECREF(temp); + DICTADD(noteDict, "name", temp); temp = Py_BuildValue("III", note.x, note.y, note.z); - - PyDict_SetItemString(noteDict, "position", temp); - - Py_DECREF(temp); + DICTADD(noteDict, "position", temp); return noteDict; } @@ -129,46 +135,33 @@ static PyObject* BuildName(DFHack::t_name& name) temp = PyString_FromString(name.first_name); else temp = PyString_FromString(""); - - PyDict_SetItemString(nameDict, "first_name", temp); - Py_DECREF(temp); + DICTADD(nameDict, "first_name", temp); if(name.nickname[0]) temp = PyString_FromString(name.nickname); else temp = PyString_FromString(""); - - PyDict_SetItemString(nameDict, "nickname", temp); - Py_DECREF(temp); + DICTADD(nameDict, "nickname", temp); temp = PyInt_FromLong(name.language); - - PyDict_SetItemString(nameDict, "language", temp); - - Py_DECREF(temp); + DICTADD(nameDict, "language", temp); temp = PyBool_FromLong((int)name.has_name); - - PyDict_SetItemString(nameDict, "has_name", temp); - - Py_DECREF(temp); + DICTADD(nameDict, "has_name", temp); wordList = PyList_New(wordCount); speechList = PyList_New(wordCount); for(int i = 0; i < wordCount; i++) { - PyList_SetItem(wordList, i, PyInt_FromLong(name.words[i])); - PyList_SetItem(wordList, i, PyInt_FromLong(name.parts_of_speech[i])); + PyList_SET_ITEM(wordList, i, PyInt_FromLong(name.words[i])); + PyList_SET_ITEM(speechList, i, PyInt_FromLong(name.parts_of_speech[i])); } - PyDict_SetItemString(nameDict, "words", wordList); - PyDict_SetItemString(nameDict, "parts_of_speech", speechList); - - Py_DECREF(wordList); - Py_DECREF(speechList); + DICTADD(nameDict, "words", wordList); + DICTADD(nameDict, "parts_of_speech", speechList); return nameDict; } @@ -182,32 +175,72 @@ static PyObject* BuildSettlement(DFHack::t_settlement& settlement) setDict = PyDict_New(); temp = PyInt_FromLong(settlement.origin); + DICTADD(setDict, "origin", temp); - PyDict_SetItemString(setDict, "origin", temp); + temp = BuildName(settlement.name); + DICTADD(setDict, "name", temp); - Py_DECREF(temp); + temp = Py_BuildValue("ii", settlement.world_x, settlement.world_y); + DICTADD(setDict, "world_pos", temp); - temp = BuildName(settlement.name); + local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1); + local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2); - PyDict_SetItemString(setDict, "name", temp); + temp = Py_BuildValue("OO", local_pos1, local_pos2); + DICTADD(setDict, "local_pos", temp); - Py_DECREF(temp); + return setDict; +} + +static PyObject* BuildSoul(DFHack::t_soul& soul) +{ + PyObject *soulDict, *skillList, *temp; - temp = Py_BuildValue("ii", settlement.world_x, settlement.world_y); + soulDict = PyDict_New(); - PyDict_SetItemString(setDict, "world_pos", temp); + skillList = BuildSkillList(soul.skills, soul.numSkills); + DICTADD(soulDict, "skills", skillList); - Py_DECREF(temp); + temp = BuildAttribute(soul.analytical_ability); + DICTADD(soulDict, "analytical_ability", temp); - local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1); - local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2); + temp = BuildAttribute(soul.focus); + DICTADD(soulDict, "focus", temp); - PyDict_SetItemString(setDict, "local_pos", Py_BuildValue("OO", local_pos1, local_pos2)); + temp = BuildAttribute(soul.willpower); + DICTADD(soulDict, "willpower", temp); - Py_DECREF(local_pos1); - Py_DECREF(local_pos2); + temp = BuildAttribute(soul.creativity); + DICTADD(soulDict, "creativity", temp); - return setDict; + temp = BuildAttribute(soul.intuition); + DICTADD(soulDict, "intuition", temp); + + temp = BuildAttribute(soul.patience); + DICTADD(soulDict, "patience", temp); + + temp = BuildAttribute(soul.memory); + DICTADD(soulDict, "memory", temp); + + temp = BuildAttribute(soul.linguistic_ability); + DICTADD(soulDict, "linguistic_ability", temp); + + temp = BuildAttribute(soul.spatial_sense); + DICTADD(soulDict, "spatial_sense", temp); + + temp = BuildAttribute(soul.musicality); + DICTADD(soulDict, "musicality", temp); + + temp = BuildAttribute(soul.kinesthetic_sense); + DICTADD(soulDict, "kinesthetic_sense", temp); + + temp = BuildAttribute(soul.empathy); + DICTADD(soulDict, "empathy", temp); + + temp = BuildAttribute(soul.social_awareness); + DICTADD(soulDict, "social_awareness", temp); + + return soulDict; } #endif \ No newline at end of file From 6e03283953cd817510316483ef895c9304c25811 Mon Sep 17 00:00:00 2001 From: doomchild Date: Sun, 11 Apr 2010 21:07:52 -0500 Subject: [PATCH 02/34] updated for DF2010 struct changes --- dfhack/python/DF_CreatureType.cpp | 113 ++++++++++-------------------- 1 file changed, 38 insertions(+), 75 deletions(-) diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index 7eae9569e..dbcd06a58 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -39,35 +39,26 @@ struct DF_Creature_Base // simple type stuff uint32_t origin; - uint32_t c_type; + uint32_t race; uint8_t profession; uint16_t mood; uint32_t happiness; uint32_t c_id; - uint32_t agility; - uint32_t strength; - uint32_t toughness; - uint32_t money; int32_t squad_leader_id; uint8_t sex; uint32_t pregnancy_timer; - int32_t blood_max, blood_current; - uint32_t bleed_rate; + uint32_t flags1, flags2; PyObject* custom_profession; // composites PyObject* position; - PyObject *name, *squad_name, *artifact_name; + PyObject *name, *artifact_name; PyObject* current_job; - - // customs - PyObject *flags1, *flags2; + PyObject *strength, *agility, *toughness, *endurance, *recuperation, *disease_resistance; + PyObject* defaultSoul; // lists - PyObject* skill_list; - PyObject* like_list; - PyObject* trait_list; PyObject* labor_list; }; @@ -82,30 +73,29 @@ static PyObject* DF_Creature_Base_new(PyTypeObject* type, PyObject* args, PyObje if(self != NULL) { self->origin = 0; - self->c_type = 0; self->profession = 0; self->mood = 0; self->happiness = 0; self->c_id = 0; - self->agility = 0; - self->strength = 0; - self->toughness = 0; - self->money = 0; self->squad_leader_id = 0; self->sex = 0; self->pregnancy_timer = 0; - self->blood_max = 0; - self->blood_current = 0; - self->bleed_rate = 0; + self->flags1 = 0; + self->flags2 = 0; self->custom_profession = PyString_FromString(""); self->name = PyString_FromString(""); - self->squad_name = PyString_FromString(""); self->artifact_name = PyString_FromString(""); - self->skill_list = NULL; - self->like_list = NULL; - self->trait_list = NULL; + self->strength = NULL; + self->agility = NULL; + self->toughness = NULL; + self->endurance = NULL; + self->recuperation = NULL; + self->disease_resistance = NULL; + + self->defaultSoul = NULL; + self->labor_list = NULL; } @@ -117,31 +107,22 @@ static void DF_Creature_Base_dealloc(DF_Creature_Base* self) if(self != NULL) { Py_XDECREF(self->position); - Py_XDECREF(self->flags1); - Py_XDECREF(self->flags2); Py_XDECREF(self->custom_profession); Py_XDECREF(self->name); - Py_XDECREF(self->squad_name); Py_XDECREF(self->artifact_name); Py_XDECREF(self->current_job); - Py_XDECREF(self->flags1); - Py_XDECREF(self->flags2); + Py_XDECREF(self->strength); + Py_XDECREF(self->agility); + Py_XDECREF(self->toughness); + Py_XDECREF(self->endurance); + Py_XDECREF(self->recuperation); + Py_XDECREF(self->disease_resistance); - Py_XDECREF(self->labor_list); - Py_XDECREF(self->trait_list); - Py_XDECREF(self->skill_list); - Py_XDECREF(self->like_list); + Py_XDECREF(self->defaultSoul); - // if(self->labor_list != NULL) - // PyList_Clear(self->labor_list); - // if(self->trait_list != NULL) - // PyList_Clear(self->trait_list); - // if(self->skill_list != NULL) - // PyList_Clear(self->skill_list); - // if(self->like_list != NULL) - // PyList_Clear(self->like_list); + Py_XDECREF(self->labor_list); self->ob_type->tp_free((PyObject*)self); } @@ -150,15 +131,17 @@ static void DF_Creature_Base_dealloc(DF_Creature_Base* self) static PyMemberDef DF_Creature_Base_members[] = { {"origin", T_UINT, offsetof(DF_Creature_Base, origin), 0, ""}, - {"type", T_UINT, offsetof(DF_Creature_Base, c_type), 0, ""}, - {"flags1", T_OBJECT_EX, offsetof(DF_Creature_Base, flags1), 0, ""}, - {"flags2", T_OBJECT_EX, offsetof(DF_Creature_Base, flags2), 0, ""}, + {"_flags1", T_UINT, offsetof(DF_Creature_Base, flags1), 0, ""}, + {"_flags2", T_UINT, offsetof(DF_Creature_Base, flags2), 0, ""}, {"name", T_OBJECT_EX, offsetof(DF_Creature_Base, name), 0, ""}, - {"squad_name", T_OBJECT_EX, offsetof(DF_Creature_Base, squad_name), 0, ""}, {"artifact_name", T_OBJECT_EX, offsetof(DF_Creature_Base, artifact_name), 0, ""}, {"profession", T_INT, offsetof(DF_Creature_Base, profession), 0, ""}, {"custom_profession", T_OBJECT_EX, offsetof(DF_Creature_Base, custom_profession), 0, ""}, {"happiness", T_SHORT, offsetof(DF_Creature_Base, happiness), 0, ""}, + {"mood", T_SHORT, offsetof(DF_Creature_Base, mood), 0, ""}, + {"squad_leader_id", T_INT, offsetof(DF_Creature_Base, squad_leader_id), 0, ""}, + {"sex", T_UBYTE, offsetof(DF_Creature_Base, sex), 0, ""}, + {"pregnancy_timer", T_UINT, offsetof(DF_Creature_Base, pregnancy_timer), 0, ""}, {NULL} //Sentinel }; @@ -225,53 +208,33 @@ static PyObject* BuildCreature(DFHack::t_creature& creature) { obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); obj->profession = creature.profession; - obj->c_type = creature.type; obj->mood = creature.mood; obj->happiness = creature.happiness; obj->c_id = creature.id; - obj->agility = creature.agility; - obj->strength = creature.strength; - obj->toughness = creature.toughness; - obj->money = creature.money; + obj->agility = BuildAttribute(creature.agility); + obj->strength = BuildAttribute(creature.strength); + obj->toughness = BuildAttribute(creature.toughness); + obj->endurance = BuildAttribute(creature.endurance); + obj->recuperation = BuildAttribute(creature.recuperation); + obj->disease_resistance = BuildAttribute(creature.disease_resistance); obj->squad_leader_id = creature.squad_leader_id; obj->sex = creature.sex; obj->pregnancy_timer = creature.pregnancy_timer; - obj->blood_max = creature.blood_max; - obj->blood_current = creature.blood_current; - obj->bleed_rate = creature.bleed_rate; if(creature.custom_profession[0]) obj->custom_profession = PyString_FromString(creature.custom_profession); - obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole), NULL); - obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(creature.flags2.whole), NULL); + obj->flags1 = creature.flags1.whole; + obj->flags2 = creature.flags2.whole; obj->current_job = BuildJob(creature.current_job); obj->name = BuildName(creature.name); - obj->squad_name = BuildName(creature.squad_name); obj->artifact_name = BuildName(creature.artifact_name); - obj->skill_list = PyList_New(creature.numSkills); - - for(int i = 0; i < creature.numSkills; i++) - PyList_SetItem(obj->skill_list, i, BuildSkill(creature.skills[i])); - - obj->like_list = PyList_New(creature.numLikes); - - for(int i = 0; i < creature.numLikes; i++) - PyList_SetItem(obj->like_list, i, BuildLike(creature.likes[i])); - obj->labor_list = PyList_New(NUM_CREATURE_LABORS); for(int i = 0; i < NUM_CREATURE_LABORS; i++) PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); - - obj->trait_list = PyList_New(NUM_CREATURE_TRAITS); - - for(int i = 0; i < NUM_CREATURE_TRAITS; i++) - PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); - - Py_INCREF((PyObject*)obj); return (PyObject*)obj; } From e9c592b5dd35922a85d3784a2d755ff0fd5ae292 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 12 Apr 2010 14:46:04 -0500 Subject: [PATCH 03/34] added debugging messages to deallocation --- dfhack/python/DF_API.cpp | 23 +++++++++++++++++++---- dfhack/python/DF_CreatureManager.cpp | 12 +++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 2d864d04d..89085b97e 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -86,22 +86,37 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) static void DF_API_dealloc(DF_API* self) { + PySys_WriteStdout("API dealloc\n"); + if(self != NULL) { - Py_CLEAR(self->mem_info); - Py_CLEAR(self->position); - Py_CLEAR(self->material); - Py_CLEAR(self->creature); + PySys_WriteStdout("creature xdecref\n"); + Py_XDECREF(self->creature); + + PySys_WriteStdout("mem_info xdecref\n"); + Py_XDECREF(self->mem_info); + + PySys_WriteStdout("position xdecref\n"); + Py_XDECREF(self->position); + + PySys_WriteStdout("material xdecref\n"); + Py_XDECREF(self->material); if(self->api_Ptr != NULL) { + PySys_WriteStdout("api_Ptr = %i\n", (int)self->api_Ptr); + delete self->api_Ptr; + PySys_WriteStdout("api_Ptr deleted\n"); + self->api_Ptr = NULL; } self->ob_type->tp_free((PyObject*)self); } + + PySys_WriteStdout("API dealloc done\n"); } // Accessors diff --git a/dfhack/python/DF_CreatureManager.cpp b/dfhack/python/DF_CreatureManager.cpp index 9a1d229f2..43e08f230 100644 --- a/dfhack/python/DF_CreatureManager.cpp +++ b/dfhack/python/DF_CreatureManager.cpp @@ -59,17 +59,27 @@ static int DF_CreatureManager_init(DF_CreatureManager* self, PyObject* args, PyO static void DF_CreatureManager_dealloc(DF_CreatureManager* self) { + PySys_WriteStdout("creature manager dealloc\n"); + if(self != NULL) - { + { + PySys_WriteStdout("creature manager not NULL\n"); + if(self->creature_Ptr != NULL) { + PySys_WriteStdout("creature_Ptr = %i\n", (int)self->creature_Ptr); + delete self->creature_Ptr; + PySys_WriteStdout("creature_Ptr deleted\n"); + self->creature_Ptr = NULL; } self->ob_type->tp_free((PyObject*)self); } + + PySys_WriteStdout("creature manager dealloc done\n"); } // Type methods From c296080cacaafb0ab5f2d1a6ee7ca0015c05e4d7 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 12 Apr 2010 14:46:53 -0500 Subject: [PATCH 04/34] switched to using PyList_SET_ITEM in BuildSkillList --- dfhack/python/DF_Helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 97fe615f6..cb5ff6e8a 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -26,7 +26,7 @@ distribution. #define __DFHELPERS__ #include "Python.h" -#include "DF_Imports.cpp" +//#include "DF_Imports.cpp" #include "DFTypes.h" using namespace DFHack; From 2eb66640bf27c937b7eaa91e294376c173026ad9 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 12 Apr 2010 14:47:11 -0500 Subject: [PATCH 05/34] added debugging messages to deallocation --- dfhack/python/DF_Material.cpp | 10 ++++++++++ dfhack/python/DF_MemInfo.cpp | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/dfhack/python/DF_Material.cpp b/dfhack/python/DF_Material.cpp index 6e4615441..6cc02cd18 100644 --- a/dfhack/python/DF_Material.cpp +++ b/dfhack/python/DF_Material.cpp @@ -166,17 +166,27 @@ static int DF_Material_init(DF_Material* self, PyObject* args, PyObject* kwds) static void DF_Material_dealloc(DF_Material* self) { + PySys_WriteStdout("material dealloc\n"); + if(self != NULL) { + PySys_WriteStdout("material not NULL\n"); + if(self->mat_Ptr != NULL) { + PySys_WriteStdout("mat_Ptr = %i\n", (int)self->mat_Ptr); + delete self->mat_Ptr; + PySys_WriteStdout("mat_Ptr deleted\n"); + self->mat_Ptr = NULL; } self->ob_type->tp_free((PyObject*)self); } + + PySys_WriteStdout("material dealloc done\n"); } // Type methods diff --git a/dfhack/python/DF_MemInfo.cpp b/dfhack/python/DF_MemInfo.cpp index 86efd35ff..4680b502e 100644 --- a/dfhack/python/DF_MemInfo.cpp +++ b/dfhack/python/DF_MemInfo.cpp @@ -63,17 +63,27 @@ static int DF_MemInfo_init(DF_MemInfo* self, PyObject* args, PyObject* kwds) static void DF_MemInfo_dealloc(DF_MemInfo* self) { + PySys_WriteStdout("mem_info dealloc\n"); + if(self != NULL) { + PySys_WriteStdout("mem_info not NULL\n"); + if(self->mem_Ptr != NULL) { + PySys_WriteStdout("mem_Ptr = %i\n", (int)self->mem_Ptr); + delete self->mem_Ptr; + PySys_WriteStdout("mem_Ptr deleted\n"); + self->mem_Ptr = NULL; } self->ob_type->tp_free((PyObject*)self); } + + PySys_WriteStdout("mem_info dealloc done\n"); } // Setters/Getters From 524d4f6025466a720047f7783cee2edae70f72f3 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 12 Apr 2010 14:47:42 -0500 Subject: [PATCH 06/34] turned off imports (I think they're hanging references, and I don't actually need them right now) --- dfhack/python/pydfhack.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index feb2da2cc..11320b346 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -23,7 +23,7 @@ distribution. */ #include "Python.h" -#include "DF_Imports.cpp" +//#include "DF_Imports.cpp" #include "DF_MemInfo.cpp" #include "DF_Material.cpp" #include "DF_CreatureType.cpp" @@ -93,8 +93,8 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); PyModule_AddObject(module, "Position", (PyObject*)&DF_Position_type); PyModule_AddObject(module, "Materials", (PyObject*)&DF_Material_type); - PyModule_AddObject(module, "Creature_Base", (PyObject*)&DF_Position_type); - PyModule_AddObject(module, "CreatureManager", (PyObject*)&DF_Material_type); + PyModule_AddObject(module, "Creature_Base", (PyObject*)&DF_Creature_Base_type); + PyModule_AddObject(module, "CreatureManager", (PyObject*)&DF_CreatureManager_type); - DoImports(); + //DoImports(); } \ No newline at end of file From 32cbdea87b73e4b98daa27d2497b2e67ad553121 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 12 Apr 2010 14:47:55 -0500 Subject: [PATCH 07/34] added type init --- dfhack/python/DF_CreatureType.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index dbcd06a58..a2e481338 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -27,7 +27,7 @@ distribution. #include "Python.h" #include "structmember.h" -#include "DF_Imports.cpp" +//#include "DF_Imports.cpp" #include "DF_Helpers.cpp" #include "modules/Creatures.h" @@ -102,6 +102,11 @@ static PyObject* DF_Creature_Base_new(PyTypeObject* type, PyObject* args, PyObje return (PyObject*)self; } +static int DF_Creature_Base_init(DF_Creature_Base* self, PyObject* args, PyObject* kwd) +{ + return 0; +} + static void DF_Creature_Base_dealloc(DF_Creature_Base* self) { if(self != NULL) @@ -193,7 +198,7 @@ static PyTypeObject DF_Creature_Base_type = 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0, /* tp_init */ + (initproc)DF_Creature_Base_init, /* tp_init */ 0, /* tp_alloc */ DF_Creature_Base_new, /* tp_new */ }; @@ -234,8 +239,8 @@ static PyObject* BuildCreature(DFHack::t_creature& creature) obj->labor_list = PyList_New(NUM_CREATURE_LABORS); for(int i = 0; i < NUM_CREATURE_LABORS; i++) - PyList_SetItem(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); - + PyList_SET_ITEM(obj->labor_list, i, PyInt_FromLong(creature.labors[i])); + return (PyObject*)obj; } From 244a3c1333d62fe0b50cd9ad8a75d8c71c87fa51 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 13 Apr 2010 08:55:39 -0500 Subject: [PATCH 08/34] fixed a couple of attribute accesses --- dfhack/python/DF_CreatureType.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dfhack/python/DF_CreatureType.cpp b/dfhack/python/DF_CreatureType.cpp index a2e481338..99a4e081a 100644 --- a/dfhack/python/DF_CreatureType.cpp +++ b/dfhack/python/DF_CreatureType.cpp @@ -87,6 +87,8 @@ static PyObject* DF_Creature_Base_new(PyTypeObject* type, PyObject* args, PyObje self->name = PyString_FromString(""); self->artifact_name = PyString_FromString(""); + self->position = NULL; + self->strength = NULL; self->agility = NULL; self->toughness = NULL; @@ -136,8 +138,10 @@ static void DF_Creature_Base_dealloc(DF_Creature_Base* self) static PyMemberDef DF_Creature_Base_members[] = { {"origin", T_UINT, offsetof(DF_Creature_Base, origin), 0, ""}, + {"position", T_OBJECT_EX, offsetof(DF_Creature_Base, position), 0, ""}, {"_flags1", T_UINT, offsetof(DF_Creature_Base, flags1), 0, ""}, {"_flags2", T_UINT, offsetof(DF_Creature_Base, flags2), 0, ""}, + {"race", T_UINT, offsetof(DF_Creature_Base, race), 0, ""}, {"name", T_OBJECT_EX, offsetof(DF_Creature_Base, name), 0, ""}, {"artifact_name", T_OBJECT_EX, offsetof(DF_Creature_Base, artifact_name), 0, ""}, {"profession", T_INT, offsetof(DF_Creature_Base, profession), 0, ""}, @@ -211,11 +215,13 @@ static PyObject* BuildCreature(DFHack::t_creature& creature) if(obj != NULL) { + obj->origin = creature.origin; obj->position = Py_BuildValue("III", creature.x, creature.y, creature.z); obj->profession = creature.profession; obj->mood = creature.mood; obj->happiness = creature.happiness; obj->c_id = creature.id; + obj->race = creature.race; obj->agility = BuildAttribute(creature.agility); obj->strength = BuildAttribute(creature.strength); obj->toughness = BuildAttribute(creature.toughness); From f047f96eefdb6a8c7aec49661933a3aeb39d24b4 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 13 Apr 2010 16:12:36 -0500 Subject: [PATCH 09/34] first commit --- dfhack/python/DF_Translate.cpp | 212 +++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 dfhack/python/DF_Translate.cpp diff --git a/dfhack/python/DF_Translate.cpp b/dfhack/python/DF_Translate.cpp new file mode 100644 index 000000000..77372c777 --- /dev/null +++ b/dfhack/python/DF_Translate.cpp @@ -0,0 +1,212 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFTRANSLATE__ +#define __DFTRANSLATE__ + +#include "Python.h" +#include +#include + +using namespace std; + +#include "modules/Translation.h" + +using namespace DFHack; + +struct DF_Translate +{ + PyObject_HEAD + PyObject* dict; + DFHack::Translation* tran_Ptr; +}; + +// API type Allocation, Deallocation, and Initialization + +static PyObject* DF_Translate_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Translate* self; + + self = (DF_Translate*)type->tp_alloc(type, 0); + + if(self != NULL) + self->tran_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Translate_init(DF_Translate* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Translate_dealloc(DF_Translate* self) +{ + PySys_WriteStdout("translate dealloc\n"); + + if(self != NULL) + { + PySys_WriteStdout("translate not NULL\n"); + + if(self->tran_Ptr != NULL) + { + Py_XDECREF(self->dict); + + PySys_WriteStdout("tran_Ptr = %i\n", (int)self->tran_Ptr); + + delete self->tran_Ptr; + + PySys_WriteStdout("tran_Ptr deleted\n"); + + self->tran_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } + + PySys_WriteStdout("translate dealloc done\n"); +} + +// Type methods + +static PyObject* DF_Translate_GetDicts(DF_Translate* self, PyObject* args) +{ + PyObject* dict; + Dicts* t_dicts; + + if(self->tran_Ptr != NULL) + { + t_dicts = self->tran_Ptr->getDicts(); + } +} + +static PyObject* DF_Translate_Start(DF_Translate* self, PyObject* args) +{ + Dicts* t_dicts; + + if(self->tran_Ptr != NULL) + { + if(self->tran_Ptr->Start()) + { + t_dicts = self->tran_Ptr->getDicts(); + + Py_RETURN_TRUE; + } + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Translate_Finish(DF_Translate* self, PyObject* args) +{ + if(self->tran_Ptr != NULL) + { + if(self->tran_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Translate_TranslateName(DF_Translate* self, PyObject* args) +{ + PyObject *nameObj, *retString; + DFHack::t_name name; + int inEnglish = 1; + + if(self->tran_Ptr != NULL) + { + if(PyArg_ParseTuple(args, "O|i", &nameObj, &inEnglish)) + return NULL; + + name = ReverseBuildName(nameObj); + + std::string nameStr = self->tran_Ptr->TranslateName(name, (bool)inEnglish); + + retString = PyString_FromString(nameStr.c_str()); + + return retString; + } + + Py_RETURN_NONE; +} + + + +static PyMethodDef DF_Translate_methods[] = +{ + {"Start", (PyCFunction)DF_Translate_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_Translate_Finish, METH_NOARGS, ""}, + {"Translate_Name", (PyCFunction)DF_Translate_TranslateName, METH_VARARGS, ""}, + {NULL} //Sentinel +}; + +static PyTypeObject DF_Translate_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Translate", /*tp_name*/ + sizeof(DF_Translate), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Translate_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Translate objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Translate_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Translate_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Translate_new, /* tp_new */ +}; + +#endif \ No newline at end of file From f6b2abca5bc138e04dcab271fcaf32ec4e7690a1 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 13 Apr 2010 16:12:54 -0500 Subject: [PATCH 10/34] added translate type --- dfhack/python/pydfhack.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 11320b346..25e544a2a 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -28,6 +28,7 @@ distribution. #include "DF_Material.cpp" #include "DF_CreatureType.cpp" #include "DF_CreatureManager.cpp" +#include "DF_Translate.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -80,6 +81,9 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_CreatureManager_type) < 0) return; + if(PyType_Ready(&DF_Translate_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); @@ -88,6 +92,7 @@ PyMODINIT_FUNC initpydfhack(void) Py_INCREF(&DF_Material_type); Py_INCREF(&DF_Creature_Base_type); Py_INCREF(&DF_CreatureManager_type); + Py_INCREF(&DF_Translate_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); @@ -95,6 +100,7 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "Materials", (PyObject*)&DF_Material_type); PyModule_AddObject(module, "Creature_Base", (PyObject*)&DF_Creature_Base_type); PyModule_AddObject(module, "CreatureManager", (PyObject*)&DF_CreatureManager_type); + PyModule_AddObject(module, "Translate", (PyObject*)&DF_Translate_type); //DoImports(); } \ No newline at end of file From ad642cf4e7194e24928f72cc11f7e8864e8c30a6 Mon Sep 17 00:00:00 2001 From: doomchild Date: Tue, 13 Apr 2010 16:13:22 -0500 Subject: [PATCH 11/34] added a reverse name builder (PyObject* to DFHack::t_name) --- dfhack/python/DF_Helpers.cpp | 84 +++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index cb5ff6e8a..dab0044a5 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -26,7 +26,8 @@ distribution. #define __DFHELPERS__ #include "Python.h" -//#include "DF_Imports.cpp" +#include +#include #include "DFTypes.h" using namespace DFHack; @@ -40,11 +41,6 @@ static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) return Py_BuildValue("ii", matgloss.type, matgloss.index); } -// static PyObject* BuildTreeDesc(DFHack::t_tree_desc& tree) -// { - // return Py_BuildValue("OO", BuildMatglossPair(tree.material), Py_BuildValue("III", tree.x, tree.y, tree.z)); -// } - static PyObject* BuildSkill(DFHack::t_skill& skill) { return Py_BuildValue("III", skill.id, skill.experience, skill.rating); @@ -166,6 +162,82 @@ static PyObject* BuildName(DFHack::t_name& name) return nameDict; } +static DFHack::t_name ReverseBuildName(PyObject* nameDict) +{ + PyObject *temp, *listTemp; + int boolTemp, arrLength; + Py_ssize_t listLength, strLength; + char* strTemp; + DFHack::t_name name; + + temp = PyDict_GetItemString(nameDict, "language"); + name.language = (uint32_t)PyInt_AsLong(temp); + + temp = PyDict_GetItemString(nameDict, "has_name"); + + boolTemp = (int)PyInt_AsLong(temp); + + if(boolTemp != 0) + name.has_name = true; + else + name.has_name = false; + + //I seriously doubt the name arrays will change length, but why take chances? + listTemp = PyDict_GetItemString(nameDict, "words"); + + arrLength = sizeof(name.words) / sizeof(uint32_t); + listLength = PyList_Size(listTemp); + + if(listLength < arrLength) + arrLength = listLength; + + for(int i = 0; i < arrLength; i++) + name.words[i] = (uint32_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); + + listTemp = PyDict_GetItemString(nameDict, "parts_of_speech"); + + arrLength = sizeof(name.parts_of_speech) / sizeof(uint16_t); + listLength = PyList_Size(listTemp); + + if(listLength < arrLength) + arrLength = listLength; + + for(int i = 0; i < arrLength; i++) + name.parts_of_speech[i] = (uint16_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); + + temp = PyDict_GetItemString(nameDict, "first_name"); + strLength = PyString_Size(temp); + strTemp = PyString_AsString(temp); + + if(strLength > 128) + { + strncpy(name.first_name, strTemp, 127); + name.first_name[127] = '\0'; + } + else + { + strncpy(name.first_name, strTemp, strLength); + name.first_name[strLength] = '\0'; + } + + temp = PyDict_GetItemString(nameDict, "nickname"); + strLength = PyString_Size(temp); + strTemp = PyString_AsString(temp); + + if(strLength > 128) + { + strncpy(name.nickname, strTemp, 127); + name.nickname[127] = '\0'; + } + else + { + strncpy(name.nickname, strTemp, strLength); + name.nickname[strLength] = '\0'; + } + + return name; +} + static PyObject* BuildSettlement(DFHack::t_settlement& settlement) { PyObject* setDict; From 2f730797b97cd505b4655e261767d05fcf841fea Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 10:49:39 -0500 Subject: [PATCH 12/34] first commit --- dfhack/python/DF_Creatures.cpp | 32 ----- dfhack/python/DF_Translation.cpp | 105 ---------------- dfhack/python/DF_Vegetation.cpp | 202 +++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 137 deletions(-) delete mode 100644 dfhack/python/DF_Creatures.cpp delete mode 100644 dfhack/python/DF_Translation.cpp create mode 100644 dfhack/python/DF_Vegetation.cpp diff --git a/dfhack/python/DF_Creatures.cpp b/dfhack/python/DF_Creatures.cpp deleted file mode 100644 index 0960b1722..000000000 --- a/dfhack/python/DF_Creatures.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* -www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any -damages arising from the use of this software. - -Permission is granted to anyone to use this software for any -purpose, including commercial applications, and to alter it and -redistribute it freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must -not claim that you wrote the original software. If you use this -software in a product, an acknowledgment in the product documentation -would be appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and -must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. -*/ - -#ifndef __DFCREATURES__ -#define __DFCREATURES__ - -#include "Python.h" -#include "DFTypes.h" -#include "DF_CreatureType.cpp" - -#endif \ No newline at end of file diff --git a/dfhack/python/DF_Translation.cpp b/dfhack/python/DF_Translation.cpp deleted file mode 100644 index ce34a28cb..000000000 --- a/dfhack/python/DF_Translation.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* -www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any -damages arising from the use of this software. - -Permission is granted to anyone to use this software for any -purpose, including commercial applications, and to alter it and -redistribute it freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must -not claim that you wrote the original software. If you use this -software in a product, an acknowledgment in the product documentation -would be appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and -must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. -*/ - -#ifndef __DFTRANSLATION__ -#define __DFTRANSLATION__ - -#include "Python.h" -#include "modules/Translation.h" - -using namespace - -struct DF_Translation -{ - PyObject_HEAD - DFHack::Translation* trans_Ptr; -}; - -// API type Allocation, Deallocation, and Initialization - -static PyObject* DF_Translation_new(PyTypeObject* type, PyObject* args, PyObject* kwds) -{ - DF_Translation* self; - - self = (DF_Translation*)type->tp_alloc(type, 0); - - if(self != NULL) - self->trans_Ptr = NULL; - - return (PyObject*)self; -} - -static int DF_Translation_init(DF_Translation* self, PyObject* args, PyObject* kwds) -{ - return 0; -} - -static void DF_Translation_dealloc(DF_Translation* self) -{ - if(self != NULL) - { - if(self->trans_Ptr != NULL) - { - delete self->trans_Ptr; - - self->trans_Ptr = NULL; - } - - self->ob_type->tp_free((PyObject*)self); - } -} - -// Type methods - -static PyObject* DF_Translation_Start(DF_Translation* self, PyObject* args) -{ - if(self->trans_Ptr != NULL) - { - if(self->trans_Ptr->Start()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_Translation_Finish(DF_Translation* self, PyObject* args) -{ - if(self->trans_Ptr != NULL) - { - if(self->trans_Ptr->Finish()) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - } - - Py_RETURN_NONE; -} - -static PyObject* DF_Translation_TranslateName(DF_Translation* self, PyObject* args) -{ -} - -#endif \ No newline at end of file diff --git a/dfhack/python/DF_Vegetation.cpp b/dfhack/python/DF_Vegetation.cpp new file mode 100644 index 000000000..93ba687dd --- /dev/null +++ b/dfhack/python/DF_Vegetation.cpp @@ -0,0 +1,202 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFVEGETATION__ +#define __DFVEGETATION__ + +#include "Python.h" +#include "modules/Vegetation.h" +#include "DF_Helpers.cpp" + +using namespace DFHack; + +static PyObject* BuildTree(DFHack::t_tree& tree) +{ + PyObject* t_dict; + PyObject* temp; + + t_dict = PyDict_New(); + + temp = PyInt_FromLong(tree.type); + DICTADD(t_dict, "type", temp); + + temp = PyInt_FromLong(tree.material); + DICTADD(t_dict, "material", temp); + + temp = PyTuple_Pack(3, tree.x, tree.y, tree.z); + DICTADD(t_dict, "position", temp); + + temp = PyInt_FromLong(tree.address); + DICTADD(t_dict, "address", temp); + + return t_dict; +} + +struct DF_Vegetation +{ + PyObject_HEAD + DFHack::Vegetation* veg_Ptr; +}; + +static PyObject* DF_Vegetation_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Vegetation* self; + + self = (DF_Vegetation*)type->tp_alloc(type, 0); + + if(self != NULL) + self->veg_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Vegetation_init(DF_Vegetation* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Vegetation_dealloc(DF_Vegetation* self) +{ + PySys_WriteStdout("vegetation dealloc\n"); + + if(self != NULL) + { + PySys_WriteStdout("vegetation not NULL\n"); + + if(self->veg_Ptr != NULL) + { + PySys_WriteStdout("veg_Ptr = %i\n", (int)self->veg_Ptr); + + delete self->veg_Ptr; + + PySys_WriteStdout("veg_Ptr deleted\n"); + + self->veg_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } + + PySys_WriteStdout("vegetation dealloc done\n"); +} + +// Type methods + +static PyObject* DF_Vegetation_Start(DF_Vegetation* self, PyObject* args) +{ + uint32_t numTrees = 0; + + if(self->veg_Ptr != NULL) + { + if(self->veg_Ptr->Start(numTrees)) + return PyInt_FromLong(numTrees); + else + return PyInt_FromLong(-1); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Vegetation_Finish(DF_Vegetation* self, PyObject* args) +{ + if(self->veg_Ptr != NULL) + { + if(self->veg_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Vegetation_Read(DF_Vegetation* self, PyObject* args) +{ + uint32_t index = 0; + t_tree tree; + + if(self->veg_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &index)) + return NULL; + + if(self->veg_Ptr->Read(index, tree)) + return BuildTree(tree); + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_Vegetation_methods[] = +{ + {"Start", (PyCFunction)DF_Vegetation_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_Vegetation_Finish, METH_NOARGS, ""}, + {"Read", (PyCFunction)DF_Vegetation_Read, METH_VARARGS, ""}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_Vegetation_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Vegetation", /*tp_name*/ + sizeof(DF_Vegetation), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Vegetation_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Vegetation objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Vegetation_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Vegetation_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Vegetation_new, /* tp_new */ +}; + +#endif \ No newline at end of file From 02334e640df2abe96cd2eed92eee3fbe2bdd1ca6 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 10:50:42 -0500 Subject: [PATCH 13/34] added vegetation type --- dfhack/python/pydfhack.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 25e544a2a..90bc7a59b 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -29,6 +29,7 @@ distribution. #include "DF_CreatureType.cpp" #include "DF_CreatureManager.cpp" #include "DF_Translate.cpp" +#include "DF_Vegetation.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -84,6 +85,9 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_Translate_type) < 0) return; + if(PyType_Ready(&DF_Vegetation_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); @@ -93,6 +97,7 @@ PyMODINIT_FUNC initpydfhack(void) Py_INCREF(&DF_Creature_Base_type); Py_INCREF(&DF_CreatureManager_type); Py_INCREF(&DF_Translate_type); + Py_INCREF(&DF_Vegetation_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); @@ -101,6 +106,7 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "Creature_Base", (PyObject*)&DF_Creature_Base_type); PyModule_AddObject(module, "CreatureManager", (PyObject*)&DF_CreatureManager_type); PyModule_AddObject(module, "Translate", (PyObject*)&DF_Translate_type); + PyModule_AddObject(module, "Vegetation", (PyObject*)&DF_Vegetation_type); //DoImports(); } \ No newline at end of file From 15a11355b39cf7c95dbecd3cd5bdda10c99e4174 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 10:50:59 -0500 Subject: [PATCH 14/34] added getter for dicts --- dfhack/python/DF_Translate.cpp | 97 ++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 15 deletions(-) diff --git a/dfhack/python/DF_Translate.cpp b/dfhack/python/DF_Translate.cpp index 77372c777..274e5ccdd 100644 --- a/dfhack/python/DF_Translate.cpp +++ b/dfhack/python/DF_Translate.cpp @@ -90,27 +90,70 @@ static void DF_Translate_dealloc(DF_Translate* self) // Type methods -static PyObject* DF_Translate_GetDicts(DF_Translate* self, PyObject* args) -{ - PyObject* dict; - Dicts* t_dicts; - - if(self->tran_Ptr != NULL) - { - t_dicts = self->tran_Ptr->getDicts(); - } -} - static PyObject* DF_Translate_Start(DF_Translate* self, PyObject* args) { - Dicts* t_dicts; + DFHack::Dicts* t_dicts; + std::vector wordVec; + PyObject *list, *tempDict; if(self->tran_Ptr != NULL) { if(self->tran_Ptr->Start()) { + Py_CLEAR(self->dict); + t_dicts = self->tran_Ptr->getDicts(); + DFHack::DFDict & translations = t_dicts->translations; + DFHack::DFDict & foreign_languages = t_dicts->foreign_languages; + + self->dict = PyDict_New(); + tempDict = PyDict_New(); + + for(uint32_t i = 0; i < translations.size(); i++) + { + uint32_t size = translations[i].size(); + uint32_t j = 0; + + list = PyList_New(size); + + for(; j < size; j++) + PyList_SET_ITEM(list, j, PyString_FromString(translations[i][j].c_str())); + + PyObject* key = PyInt_FromLong(j); + PyDict_SetItem(tempDict, key, list); + + Py_DECREF(key); + Py_DECREF(list); + } + + PyDict_SetItemString(self->dict, "translations", tempDict); + + Py_DECREF(tempDict); + + tempDict = PyDict_New(); + + for(uint32_t i = 0; i < foreign_languages.size(); i++) + { + uint32_t size = foreign_languages[i].size(); + uint32_t j = 0; + + list = PyList_New(size); + + for(; j < size; j++) + PyList_SET_ITEM(list, j, PyString_FromString(foreign_languages[i][j].c_str())); + + PyObject* key = PyInt_FromLong(j); + PyDict_SetItem(tempDict, key, list); + + Py_DECREF(key); + Py_DECREF(list); + } + + PyDict_SetItemString(self->dict, "foreign_languages", tempDict); + + Py_DECREF(tempDict); + Py_RETURN_TRUE; } else @@ -125,7 +168,14 @@ static PyObject* DF_Translate_Finish(DF_Translate* self, PyObject* args) if(self->tran_Ptr != NULL) { if(self->tran_Ptr->Finish()) + { + if(self->dict != NULL) + PyDict_Clear(self->dict); + + Py_CLEAR(self->dict); + Py_RETURN_TRUE; + } else Py_RETURN_FALSE; } @@ -156,8 +206,6 @@ static PyObject* DF_Translate_TranslateName(DF_Translate* self, PyObject* args) Py_RETURN_NONE; } - - static PyMethodDef DF_Translate_methods[] = { {"Start", (PyCFunction)DF_Translate_Start, METH_NOARGS, ""}, @@ -166,6 +214,25 @@ static PyMethodDef DF_Translate_methods[] = {NULL} //Sentinel }; +// Getters/Setters + +static PyObject* DF_Translate_getDicts(DF_Translate* self, PyObject* args) +{ + if(self->tran_Ptr != NULL) + { + if(self->dict != NULL) + return self->dict; + } + + Py_RETURN_NONE; +} + +static PyGetSetDef DF_Translate_getterSetters[] = +{ + {"dictionaries", (getter)DF_Translate_getDicts, NULL, "dictionaries", NULL}, + {NULL} // Sentinel +}; + static PyTypeObject DF_Translate_type = { PyObject_HEAD_INIT(NULL) @@ -198,7 +265,7 @@ static PyTypeObject DF_Translate_type = 0, /* tp_iternext */ DF_Translate_methods, /* tp_methods */ 0, /* tp_members */ - 0, /* tp_getset */ + DF_Translate_getterSetters, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ From 849b6e53c28423067c598fe34c7e5c850e69c87d Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 12:26:14 -0500 Subject: [PATCH 15/34] added building type --- dfhack/python/pydfhack.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 90bc7a59b..b23a93b45 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -30,6 +30,7 @@ distribution. #include "DF_CreatureManager.cpp" #include "DF_Translate.cpp" #include "DF_Vegetation.cpp" +#include "DF_Buildings.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -88,6 +89,9 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_Vegetation_type) < 0) return; + if(PyType_Ready(&DF_Building_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); @@ -107,6 +111,7 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "CreatureManager", (PyObject*)&DF_CreatureManager_type); PyModule_AddObject(module, "Translate", (PyObject*)&DF_Translate_type); PyModule_AddObject(module, "Vegetation", (PyObject*)&DF_Vegetation_type); + PyModule_AddObject(module, "Building", (PyObject*)&DF_Building_type); //DoImports(); } \ No newline at end of file From 64852e958e7c8ab752aa1efe5fc9d5e3cbafef7c Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 12:27:17 -0500 Subject: [PATCH 16/34] first commit --- dfhack/python/DF_Buildings.cpp | 205 +++++++++++++++++++++++++++ dfhack/python/DF_Constructions.cpp | 220 +++++++++++++++++++++++++++++ dfhack/python/build.bat | 4 + 3 files changed, 429 insertions(+) create mode 100644 dfhack/python/DF_Buildings.cpp create mode 100644 dfhack/python/DF_Constructions.cpp create mode 100644 dfhack/python/build.bat diff --git a/dfhack/python/DF_Buildings.cpp b/dfhack/python/DF_Buildings.cpp new file mode 100644 index 000000000..1028074f2 --- /dev/null +++ b/dfhack/python/DF_Buildings.cpp @@ -0,0 +1,205 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFBUILDINGS__ +#define __DFBUILDINGS__ + +#include "Python.h" +#include "modules/Buildings.h" +#include "DF_Helpers.cpp" + +using namespace DFHack; + +static PyObject* BuildBuilding(DFHack::t_building& building) +{ + PyObject* t_dict; + PyObject* temp; + + t_dict = PyDict_New(); + + temp = PyInt_FromLong(building.origin); + DICTADD(t_dict, "origin", temp); + + temp = PyInt_FromLong(building.vtable); + DICTADD(t_dict, "vtable", temp); + + temp = PyInt_FromLong(building.type); + DICTADD(t_dict, "type", temp); + + temp = BuildMatglossPair(building.material); + DICTADD(t_dict, "material", temp); + + temp = PyTuple_Pack(2, PyTuple_Pack(2, building.x1, building.y1), PyTuple_Pack(2, building.x2, building.y2)); + DICTADD(t_dict, "bounds", temp); + + return t_dict; +} + +struct DF_Building +{ + PyObject_HEAD + DFHack::Buildings* b_Ptr; +}; + +static PyObject* DF_Building_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Building* self; + + self = (DF_Building*)type->tp_alloc(type, 0); + + if(self != NULL) + self->b_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Building_init(DF_Building* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Building_dealloc(DF_Building* self) +{ + PySys_WriteStdout("building dealloc\n"); + + if(self != NULL) + { + PySys_WriteStdout("building not NULL\n"); + + if(self->b_Ptr != NULL) + { + PySys_WriteStdout("b_Ptr = %i\n", (int)self->b_Ptr); + + delete self->b_Ptr; + + PySys_WriteStdout("b_Ptr deleted\n"); + + self->b_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } + + PySys_WriteStdout("building dealloc done\n"); +} + +// Type methods + +static PyObject* DF_Building_Start(DF_Building* self, PyObject* args) +{ + uint32_t numBuildings = 0; + + if(self->b_Ptr != NULL) + { + if(self->b_Ptr->Start(numBuildings)) + return PyInt_FromLong(numBuildings); + else + return PyInt_FromLong(-1); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Building_Finish(DF_Building* self, PyObject* args) +{ + if(self->b_Ptr != NULL) + { + if(self->b_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Building_Read(DF_Building* self, PyObject* args) +{ + uint32_t index = 0; + t_building building; + + if(self->b_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &index)) + return NULL; + + if(self->b_Ptr->Read(index, building)) + return BuildBuilding(building); + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_Building_methods[] = +{ + {"Start", (PyCFunction)DF_Building_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_Building_Finish, METH_NOARGS, ""}, + {"Read", (PyCFunction)DF_Building_Read, METH_VARARGS, ""}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_Building_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Building", /*tp_name*/ + sizeof(DF_Building), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Building_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Building objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Building_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Building_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Building_new, /* tp_new */ +}; + +#endif \ No newline at end of file diff --git a/dfhack/python/DF_Constructions.cpp b/dfhack/python/DF_Constructions.cpp new file mode 100644 index 000000000..f4d4a8328 --- /dev/null +++ b/dfhack/python/DF_Constructions.cpp @@ -0,0 +1,220 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFConstructionS__ +#define __DFConstructionS__ + +#include "Python.h" +#include "modules/Constructions.h" +#include "DF_Helpers.cpp" + +using namespace DFHack; + +static PyObject* BuildConstruction(DFHack::t_construction& construction) +{ + PyObject* t_dict; + PyObject* temp; + + t_dict = PyDict_New(); + + temp = PyTuple_Pack(3, construction.x, construction.y, construction.z); + DICTADD(t_dict, "position", temp); + + temp = PyInt_FromLong(construction.form); + DICTADD(t_dict, "form", temp); + + temp = PyInt_FromLong(construction.unk_8); + DICTADD(t_dict, "unk_8", temp); + + temp = PyInt_FromLong(construction.mat_type); + DICTADD(t_dict, "mat_type", temp); + + temp = PyInt_FromLong(construction.mat_idx); + DICTADD(t_dict, "mat_idx", temp); + + temp = PyInt_FromLong(construction.unk3); + DICTADD(t_dict, "unk3", temp); + + temp = PyInt_FromLong(construction.unk4); + DICTADD(t_dict, "unk4", temp); + + temp = PyInt_FromLong(construction.unk5); + DICTADD(t_dict, "unk5", temp); + + temp = PyInt_FromLong(construction.unk6); + DICTADD(t_dict, "unk6", temp); + + temp = PyInt_FromLong(construction.origin); + DICTADD(t_dict, "origin", temp); + + return t_dict; +} + +struct DF_Construction +{ + PyObject_HEAD + DFHack::Constructions* c_Ptr; +}; + +static PyObject* DF_Construction_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Construction* self; + + self = (DF_Construction*)type->tp_alloc(type, 0); + + if(self != NULL) + self->c_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Construction_init(DF_Construction* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Construction_dealloc(DF_Construction* self) +{ + PySys_WriteStdout("construction dealloc\n"); + + if(self != NULL) + { + PySys_WriteStdout("construction not NULL\n"); + + if(self->c_Ptr != NULL) + { + PySys_WriteStdout("c_Ptr = %i\n", (int)self->c_Ptr); + + delete self->c_Ptr; + + PySys_WriteStdout("c_Ptr deleted\n"); + + self->c_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } + + PySys_WriteStdout("construction dealloc done\n"); +} + +// Type methods + +static PyObject* DF_Construction_Start(DF_Construction* self, PyObject* args) +{ + uint32_t numConstructions = 0; + + if(self->c_Ptr != NULL) + { + if(self->c_Ptr->Start(numConstructions)) + return PyInt_FromLong(numConstructions); + else + return PyInt_FromLong(-1); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Construction_Finish(DF_Construction* self, PyObject* args) +{ + if(self->c_Ptr != NULL) + { + if(self->c_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Construction_Read(DF_Construction* self, PyObject* args) +{ + uint32_t index = 0; + t_construction construction; + + if(self->c_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "I", &index)) + return NULL; + + if(self->c_Ptr->Read(index, construction)) + return BuildConstruction(construction); + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_Construction_methods[] = +{ + {"Start", (PyCFunction)DF_Construction_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_Construction_Finish, METH_NOARGS, ""}, + {"Read", (PyCFunction)DF_Construction_Read, METH_VARARGS, ""}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_Construction_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Construction", /*tp_name*/ + sizeof(DF_Construction), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Construction_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Construction objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Construction_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Construction_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Construction_new, /* tp_new */ +}; + +#endif \ No newline at end of file diff --git a/dfhack/python/build.bat b/dfhack/python/build.bat new file mode 100644 index 000000000..6165530b1 --- /dev/null +++ b/dfhack/python/build.bat @@ -0,0 +1,4 @@ +python setup.py build_ext +copy /Y .\build\lib.win32-2.6\pydfhack.pyd ..\..\output\pydfhack.pyd +rmdir /S /Q .\build +pause \ No newline at end of file From d4d026656b078a814892af1a0ecfe68f1acdcaec Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 16:53:52 -0500 Subject: [PATCH 17/34] fixed #define --- dfhack/python/DF_Constructions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dfhack/python/DF_Constructions.cpp b/dfhack/python/DF_Constructions.cpp index f4d4a8328..237efb479 100644 --- a/dfhack/python/DF_Constructions.cpp +++ b/dfhack/python/DF_Constructions.cpp @@ -22,8 +22,8 @@ must not be misrepresented as being the original software. distribution. */ -#ifndef __DFConstructionS__ -#define __DFConstructionS__ +#ifndef __DFCONSTRUCTIONS__ +#define __DFCONSTRUCTIONS__ #include "Python.h" #include "modules/Constructions.h" From 2284fb32d22355cb067712f71fecb2d2a0efd288 Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 16:54:11 -0500 Subject: [PATCH 18/34] added construction type --- dfhack/python/pydfhack.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index b23a93b45..7b2f38f07 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -31,6 +31,7 @@ distribution. #include "DF_Translate.cpp" #include "DF_Vegetation.cpp" #include "DF_Buildings.cpp" +#include "DF_Constructions.cpp" #include "DF_API.cpp" #ifndef PyMODINIT_FUNC @@ -92,6 +93,9 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_Building_type) < 0) return; + if(PyType_Ready(&DF_Construction_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); @@ -112,6 +116,7 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "Translate", (PyObject*)&DF_Translate_type); PyModule_AddObject(module, "Vegetation", (PyObject*)&DF_Vegetation_type); PyModule_AddObject(module, "Building", (PyObject*)&DF_Building_type); + PyModule_AddObject(module, "Construction", (PyObject*)&DF_Construction_type); //DoImports(); } \ No newline at end of file From 0d2554f829fdcb746c98671b67bd2b8dde8c74ad Mon Sep 17 00:00:00 2001 From: doomchild Date: Wed, 14 Apr 2010 16:54:27 -0500 Subject: [PATCH 19/34] first commit --- dfhack/python/DF_Maps.cpp | 458 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 458 insertions(+) create mode 100644 dfhack/python/DF_Maps.cpp diff --git a/dfhack/python/DF_Maps.cpp b/dfhack/python/DF_Maps.cpp new file mode 100644 index 000000000..52b32d43d --- /dev/null +++ b/dfhack/python/DF_Maps.cpp @@ -0,0 +1,458 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFMAPS__ +#define __DFMAPS__ + +#include "Python.h" +#include +#include + +using namespace std; + +#include "modules/Maps.h" +#include "DF_Helpers.cpp" + +using namespace DFHack; + +static PyObject* BuildVein(DFHack::t_vein& v) +{ + PyObject* t_dict; + PyObject* temp; + + t_dict = PyDict_New(); + + temp = PyInt_FromLong(v.vtable); + DICTADD(t_dict, "vtable", temp); + + temp = PyInt_FromLong(v.type); + DICTADD(t_dict, "type", temp); + + temp = PyInt_FromLong(v.flags); + DICTADD(t_dict, "flags", temp); + + temp = PyInt_FromLong(v.address_of); + DICTADD(t_dict, "address", temp); + + temp = PyList_New(16); + + for(int i = 0; i < 16; i++) + PyList_SET_ITEM(temp, i, PyInt_FromLong(v.assignment[i]); + + DICTADD(t_dict, "assignment", temp); + + return t_dict; +} + +static PyObject* BuildFrozenLiquidVein(DFHack::t_frozenliquidvein& v) +{ + PyObject* t_dict; + PyObject *temp, *list; + + t_dict = PyDict_New(); + + temp = PyInt_FromLong(v.vtable); + DICTADD(t_dict, "vtable", temp); + + temp = PyInt_FromLong(v.address_of); + DICTADD(t_dict, "address", temp); + + list = PyList_New(16); + + for(int i = 0; i < 16; i++) + { + temp = PyList_New(16); + + for(int j = 0; j < 16; j++) + PyList_SET_ITEM(temp, j, PyInt_FromLong(v.tiles[i][j]); + + PyList_SET_ITEM(list, i, temp); + } + + DICTADD(t_dict, "tiles", list); + + return t_dict; +} + +static PyObject* BuildSpatterVein(DFHack::t_spattervein& v) +{ + PyObject* t_dict; + PyObject *temp, *list; + + t_dict = PyDict_New(); + + temp = PyInt_FromLong(v.vtable); + DICTADD(t_dict, "vtable", temp); + + temp = PyInt_FromLong(v.address_of); + DICTADD(t_dict, "address", temp); + + temp = PyInt_FromLong(v.mat1); + DICTADD(t_dict, "mat1", temp); + + temp = PyInt_FromLong(v.unk1); + DICTADD(t_dict, "unk1", temp); + + temp = PyInt_FromLong(v.mat2); + DICTADD(t_dict, "mat2", temp); + + temp = PyInt_FromLong(v.mat3); + DICTADD(t_dict, "mat3", temp); + + list = PyList_New(16); + + for(int i = 0; i < 16; i++) + { + temp = PyList_New(16); + + for(int j = 0; j < 16; j++) + PyList_SET_ITEM(temp, j, PyInt_FromLong(v.intensity[i][j]); + + PyList_SET_ITEM(list, i, temp); + } + + DICTADD(t_dict, "intensity", list); + + return t_dict; +} + +static PyObject* BuildTileTypes40d(DFHack::tiletypes40d& types) +{ + PyObject *list, *temp; + + list = PyList_New(16); + + for(int i = 0; i < 16; i++) + { + temp = PyList_New(16); + + for(int j = 0; j < 16; j++) + PyList_SET_ITEM(temp, j, PyInt_FromLong(types[i][j])); + + PyList_SET_ITEM(list, i, temp); + } + + return list; +} + +static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) +{ + PyObject *list, *temp; + + list = PyList_New(16); + + for(int i = 0; i < 16; i++) + { + temp = PyList_New(16); + + for(int j = 0; j < 16; j++) + PyList_SET_ITEM(temp, j, PyInt_FromLong(occ[i][j].whole)); + + PyList_SET_ITEM(list, i, temp); + } + + return list; +} + +static PyObject* BuildDesignations40d(DFHack::designations40d& des) +{ + PyObject *list, *temp; + + list = PyList_New(16); + + for(int i = 0; i < 16; i++) + { + temp = PyList_New(16); + + for(int j = 0; j < 16; j++) + PyList_SET_ITEM(temp, j, PyInt_FromLong(des[i][j].whole)); + + PyList_SET_ITEM(list, i, temp); + } + + return list; +} + +static PyObject* BuildBiomeIndices40d(DFHack::biome_indices40d& idx) +{ + PyObject *list; + + list = PyList_New(16); + + for(int i = 0; i < 16; i++) + PyList_SET_ITEM(list, i, PyInt_FromLong(idx[i]); + + return list; +} + +static PyObject* BuildMapBlock40d(DFHack::mapblock40d& block) +{ + PyObject* t_dict; + PyObject* temp; + + t_dict = PyDict_New(); + + temp = BuildTileTypes40d(block.tiletypes); + DICTADD(t_dict, "tiletypes", temp); + + temp = BuildDesignations40d(block.designation); + DICTADD(t_dict, "designation", temp); + + temp = BuildOccupancies40d(block.occupancy); + DICTADD(t_dict, "occupancy", temp); + + temp = BuildBiomeIdices40d(block.biome_indices); + DICTADD(t_dict, "biome_indices", temp); + + temp = PyInt_FromLong(block.origin); + DICTADD(t_dict, "origin", temp); + + temp = PyInt_FromLong(block.blockflags.whole); + DICTADD(t_dict, "_blockflags", temp); + + return t_dict; +} + +struct DF_Map +{ + PyObject_HEAD + DFHack::Maps* m_Ptr; +}; + +static PyObject* DF_Map_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_Map* self; + + self = (DF_Map*)type->tp_alloc(type, 0); + + if(self != NULL) + self->m_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_Map_init(DF_Map* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_Map_dealloc(DF_Map* self) +{ + PySys_WriteStdout("map dealloc\n"); + + if(self != NULL) + { + PySys_WriteStdout("map not NULL\n"); + + if(self->m_Ptr != NULL) + { + PySys_WriteStdout("m_Ptr = %i\n", (int)self->m_Ptr); + + delete self->m_Ptr; + + PySys_WriteStdout("m_Ptr deleted\n"); + + self->m_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } + + PySys_WriteStdout("map dealloc done\n"); +} + +// Type methods + +static PyObject* DF_Map_Start(DF_Map* self, PyObject* args) +{ + if(self->m_Ptr != NULL) + { + if(self->m_Ptr->Start()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_Finish(DF_Map* self, PyObject* args) +{ + if(self->m_Ptr != NULL) + { + if(self->m_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_IsValidBlock(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + if(self->m_Ptr->isValidBlock(x, y, z)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_ReadTileTypes(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + tiletypes40d t_types; + + if(self->m_Ptr->ReadTileTypes(x, y, z, &t_types)) + return BuildTileTypes40d(t_types); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_ReadDesignations(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + designations40d des; + + if(self->m_Ptr->ReadDesignations(x, y, z, &des)) + return BuildDesignations40d(des); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_ReadOccupancy(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + occupancies40d occ; + + if(self->m_Ptr->ReadOccupancy(x, y, z, &occ)) + return BuildOccupancies40d(occ); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_ReadDirtyBit(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + bool bit = false; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + self->m_Ptr->ReadDirtyBit(x, y, z, &bit) + + if(bit) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_ReadBlockFlags(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + t_blockflags flags; + + if(self->m_Ptr->ReadBlockFlags(x, y, z, &flags)) + return PyInt_FromLong(flags.whole); + } + + Py_RETURN_NONE; +} + +static PyObject* DF_Map_ReadRegionOffsets(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + biome_indices40d biome; + + if(self->m_Ptr->ReadRegionOffsets(x, y, z, &biome)) + return BuildBiomeIndices40d(biome); + } + + Py_RETURN_NONE; +} + +// Getter/Setter + +static PyObject* DF_Map_getSize(DF_Map* self, void* closure) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + self->m_Ptr->getSize(x, y, z); + + return PyTuple_Pack(3, x, y, z); + } + + Py_RETURN_NONE; +} + +#endif \ No newline at end of file From 5ce44119bb1d124d74a0550377c5e8abd3f71fd6 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 10:03:07 -0500 Subject: [PATCH 20/34] removed some unneeded types and imports --- .../python/{pydftypes.py => pydfhackflags.py} | 45 +------------------ 1 file changed, 1 insertion(+), 44 deletions(-) rename dfhack/python/{pydftypes.py => pydfhackflags.py} (88%) diff --git a/dfhack/python/pydftypes.py b/dfhack/python/pydfhackflags.py similarity index 88% rename from dfhack/python/pydftypes.py rename to dfhack/python/pydfhackflags.py index 3061277bf..5bcae4e9c 100644 --- a/dfhack/python/pydftypes.py +++ b/dfhack/python/pydfhackflags.py @@ -1,48 +1,5 @@ # -*- coding: utf-8 -*- -from pydfhack import * -from ctypes import * - -class DFAPI(API): - def Read_Designations(self, x, y, z): - temp = API.Read_Designations(self, x, y, z) - - d_list = [] - - for i in temp: - d = [] - - for j in i: - d.append(DesignationFlags(j)) - - d_list.append(d) - - return d_list - def Write_Designations(self, x, y, z, d_list): - temp = [] - - for i in d_list: - t = [] - - for j in i: - t.append(j.whole) - - temp.append(t) - - API.Write_Designations(self, x, y, z, temp) - def Read_Occupancy(self, x, y, z): - temp = API.Read_Occupancy(self, x, y, z) - - o_list = [] - - for i in temp: - o = [] - - for j in i: - o.append(OccupancyFlags(j)) - - o_list.append(o) - - return o_list +from ctypes import Structure, Union class DesignationStruct(Structure): _fields_ = [("flow_size", c_uint, 3), From 95d364dfcabaf541400a97a5500e5adce7824f4e Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 10:03:45 -0500 Subject: [PATCH 21/34] moved flag types to pydfhackflags.py added namedtuples for a couple of composite types --- dfhack/python/pydfhack.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 7b2f38f07..1d1d13475 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -23,7 +23,7 @@ distribution. */ #include "Python.h" -//#include "DF_Imports.cpp" +#include "DF_Imports.cpp" #include "DF_MemInfo.cpp" #include "DF_Material.cpp" #include "DF_CreatureType.cpp" @@ -118,5 +118,5 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "Building", (PyObject*)&DF_Building_type); PyModule_AddObject(module, "Construction", (PyObject*)&DF_Construction_type); - //DoImports(); + DoImports(); } \ No newline at end of file From 0af57ac9724d48bfe6846aca8e284440fcd03022 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 10:04:10 -0500 Subject: [PATCH 22/34] added flag and composite imports --- dfhack/python/DF_Imports.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/dfhack/python/DF_Imports.cpp b/dfhack/python/DF_Imports.cpp index 300731e4f..a1625f7db 100644 --- a/dfhack/python/DF_Imports.cpp +++ b/dfhack/python/DF_Imports.cpp @@ -27,24 +27,35 @@ distribution. #include "Python.h" -static PyObject* TypesModule = NULL; +static PyObject* FlagsModule = NULL; static PyObject* CreatureFlags1_type = NULL; static PyObject* CreatureFlags2_type = NULL; static PyObject* DesignationFlags_type = NULL; static PyObject* OccupancyFlags_type = NULL; static PyObject* ItemFlags_type = NULL; +static PyObject* TypesModule = NULL; +static PyObject* Note_type = NULL; +static PyObject* Construction_type = NULL; +static PyObject* Name_type = NULL; + static void DoImports() { if(TypesModule == NULL) { + FlagsModule = PyImport_ImportModule("pydfhackflags"); + + CreatureFlags1_type = PyObject_GetAttrString(FlagsModule, "CreatureFlags1"); + CreatureFlags2_type = PyObject_GetAttrString(FlagsModule, "CreatureFlags2"); + DesignationFlags_type = PyObject_GetAttrString(FlagsModule, "DesignationFlags"); + OccupancyFlags_type = PyObject_GetAttrString(FlagsModule, "OccupancyFlags"); + ItemFlags_type = PyObject_GetAttrString(FlagsModule, "ItemFlags"); + TypesModule = PyImport_ImportModule("pydftypes"); - CreatureFlags1_type = PyObject_GetAttrString(TypesModule, "CreatureFlags1"); - CreatureFlags2_type = PyObject_GetAttrString(TypesModule, "CreatureFlags2"); - DesignationFlags_type = PyObject_GetAttrString(TypesModule, "DesignationFlags"); - OccupancyFlags_type = PyObject_GetAttrString(TypesModule, "OccupancyFlags"); - ItemFlags_type = PyObject_GetAttrString(TypesModule, "ItemFlags"); + Note_type = PyObject_GetAttrString(TypesModule, "Note"); + Construction_type = PyObject_GetAttrString(TypesModule, "Construction"); + Name_type = PyObject_GetAttrString(TypesModule, "Name"); } } From a68a998fd174ee40462f211264c00b26bdb17956 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 10:04:38 -0500 Subject: [PATCH 23/34] switched from using raw dictionaries to imported python types for Note and Name --- dfhack/python/DF_Helpers.cpp | 59 +++++++++++++++++------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index dab0044a5..9dce8c737 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -29,12 +29,14 @@ distribution. #include #include #include "DFTypes.h" +#include "DF_Imports.cpp" using namespace DFHack; #include "modules/Creatures.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* BuildMatglossPair(DFHack::t_matglossPair& matgloss) { @@ -92,60 +94,53 @@ static PyObject* BuildLike(DFHack::t_like& like) return Py_BuildValue("OOO", item, BuildMatglossPair(like.material), PyBool_FromLong((int)like.active)); } -//PyDict_SetItem and PyDict_SetItemString don't steal references, so this had to get a bit more complicated... static PyObject* BuildNote(DFHack::t_note& note) { - PyObject* noteDict = PyDict_New(); - PyObject* temp; - - temp = PyString_FromFormat("%c", note.symbol); - DICTADD(noteDict, "symbol", temp); - - temp = Py_BuildValue("II", note.foreground, note.background); - DICTADD(noteDict, "fore_back", temp); + PyObject* noteObj; + PyObject *args, *name, *position; if(note.name[0]) - temp = PyString_FromString(note.name); + name = PyString_FromString(note.name); else - PyString_FromString(""); + name = PyString_FromString(""); + + position = Py_BuildValue("III", note.x, note.y, note.z); - DICTADD(noteDict, "name", temp); + args = Py_BuildValue("cIIsO", note.symbol, note.foreground, note.background, name, position); - temp = Py_BuildValue("III", note.x, note.y, note.z); - DICTADD(noteDict, "position", temp); + noteObj = PyObject_CallObject(Note_type, args); - return noteDict; + return noteObj; } -//same here...reference counting is kind of a pain, assuming I'm even doing it right... static PyObject* BuildName(DFHack::t_name& name) { - PyObject* nameDict; + PyObject* nameObj; PyObject *wordList, *speechList; PyObject* temp; int wordCount = 7; - nameDict = PyDict_New(); + nameObj = PyObject_CallObject(Name_type, NULL); if(name.first_name[0]) temp = PyString_FromString(name.first_name); else temp = PyString_FromString(""); - DICTADD(nameDict, "first_name", temp); + OBJSET(nameObj, "first_name", temp); if(name.nickname[0]) temp = PyString_FromString(name.nickname); else temp = PyString_FromString(""); - DICTADD(nameDict, "nickname", temp); + OBJSET(nameObj, "nickname", temp); temp = PyInt_FromLong(name.language); - DICTADD(nameDict, "language", temp); + OBJSET(nameObj, "language", temp); temp = PyBool_FromLong((int)name.has_name); - DICTADD(nameDict, "has_name", temp); + OBJSET(nameObj, "has_name", temp); wordList = PyList_New(wordCount); speechList = PyList_New(wordCount); @@ -156,13 +151,13 @@ static PyObject* BuildName(DFHack::t_name& name) PyList_SET_ITEM(speechList, i, PyInt_FromLong(name.parts_of_speech[i])); } - DICTADD(nameDict, "words", wordList); - DICTADD(nameDict, "parts_of_speech", speechList); + OBJSET(nameObj, "words", wordList); + OBJSET(nameObj, "parts_of_speech", speechList); - return nameDict; + return nameObj; } -static DFHack::t_name ReverseBuildName(PyObject* nameDict) +static DFHack::t_name ReverseBuildName(PyObject* nameObj) { PyObject *temp, *listTemp; int boolTemp, arrLength; @@ -170,10 +165,10 @@ static DFHack::t_name ReverseBuildName(PyObject* nameDict) char* strTemp; DFHack::t_name name; - temp = PyDict_GetItemString(nameDict, "language"); + temp = PyObject_GetAttrString(nameObj, "language"); name.language = (uint32_t)PyInt_AsLong(temp); - temp = PyDict_GetItemString(nameDict, "has_name"); + temp = PyObject_GetAttrString(nameObj, "has_name"); boolTemp = (int)PyInt_AsLong(temp); @@ -183,7 +178,7 @@ static DFHack::t_name ReverseBuildName(PyObject* nameDict) name.has_name = false; //I seriously doubt the name arrays will change length, but why take chances? - listTemp = PyDict_GetItemString(nameDict, "words"); + listTemp = PyObject_GetAttrString(nameObj, "words"); arrLength = sizeof(name.words) / sizeof(uint32_t); listLength = PyList_Size(listTemp); @@ -194,7 +189,7 @@ static DFHack::t_name ReverseBuildName(PyObject* nameDict) for(int i = 0; i < arrLength; i++) name.words[i] = (uint32_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); - listTemp = PyDict_GetItemString(nameDict, "parts_of_speech"); + listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech"); arrLength = sizeof(name.parts_of_speech) / sizeof(uint16_t); listLength = PyList_Size(listTemp); @@ -205,7 +200,7 @@ static DFHack::t_name ReverseBuildName(PyObject* nameDict) for(int i = 0; i < arrLength; i++) name.parts_of_speech[i] = (uint16_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); - temp = PyDict_GetItemString(nameDict, "first_name"); + temp = PyObject_GetAttrString(nameObj, "first_name"); strLength = PyString_Size(temp); strTemp = PyString_AsString(temp); @@ -220,7 +215,7 @@ static DFHack::t_name ReverseBuildName(PyObject* nameDict) name.first_name[strLength] = '\0'; } - temp = PyDict_GetItemString(nameDict, "nickname"); + temp = PyObject_GetAttrString(nameObj, "nickname"); strLength = PyString_Size(temp); strTemp = PyString_AsString(temp); From 3e70644160de97b7d084701a18395a39f05ad233 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:55:12 -0500 Subject: [PATCH 24/34] first commit --- dfhack/python/DF_GUI.cpp | 205 +++++++++++++++++++++++++++++++++++++ dfhack/python/pydftypes.py | 10 ++ 2 files changed, 215 insertions(+) create mode 100644 dfhack/python/DF_GUI.cpp create mode 100644 dfhack/python/pydftypes.py diff --git a/dfhack/python/DF_GUI.cpp b/dfhack/python/DF_GUI.cpp new file mode 100644 index 000000000..015d115b9 --- /dev/null +++ b/dfhack/python/DF_GUI.cpp @@ -0,0 +1,205 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef __DFGUI__ +#define __DFGUI__ + +#include "Python.h" +#include "modules/Gui.h" + +using namespace DFHack; + +struct DF_GUI +{ + PyObject_HEAD + DFHack::Gui* g_Ptr; +}; + +static PyObject* DF_GUI_new(PyTypeObject* type, PyObject* args, PyObject* kwds) +{ + DF_GUI* self; + + self = (DF_GUI*)type->tp_alloc(type, 0); + + if(self != NULL) + self->g_Ptr = NULL; + + return (PyObject*)self; +} + +static int DF_GUI_init(DF_GUI* self, PyObject* args, PyObject* kwds) +{ + return 0; +} + +static void DF_GUI_dealloc(DF_GUI* self) +{ + PySys_WriteStdout("gui dealloc\n"); + + if(self != NULL) + { + PySys_WriteStdout("gui not NULL\n"); + + if(self->g_Ptr != NULL) + { + PySys_WriteStdout("g_Ptr = %i\n", (int)self->g_Ptr); + + delete self->g_Ptr; + + PySys_WriteStdout("g_Ptr deleted\n"); + + self->g_Ptr = NULL; + } + + self->ob_type->tp_free((PyObject*)self); + } + + PySys_WriteStdout("gui dealloc done\n"); +} + +// Type methods + +static PyObject* DF_GUI_Start(DF_GUI* self, PyObject* args) +{ + if(self->g_Ptr != NULL) + { + if(self->g_Ptr->Start()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_GUI_Finish(DF_GUI* self, PyObject* args) +{ + if(self->g_Ptr != NULL) + { + if(self->g_Ptr->Finish()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_GUI_ReadViewScreen(DF_GUI* self, PyObject* args) +{ + DFHack::t_viewscreen viewscreen; + + if(self->g_Ptr != NULL) + { + if(self->g_Ptr->ReadViewScreen(viewscreen)) + return PyInt_FromLong(viewscreen.type); + } + + Py_RETURN_NONE; +} + +static PyMethodDef DF_GUI_methods[] = +{ + {"Start", (PyCFunction)DF_GUI_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_GUI_Finish, METH_NOARGS, ""}, + {"Read_View_Screen", (PyCFunction)DF_GUI_ReadViewScreen, METH_NOARGS, ""}, + {NULL} //Sentinel +}; + +// Getter/Setter + +static PyObject* DF_GUI_getPauseState(DF_GUI* self, void* closure) +{ + if(self->g_Ptr != NULL) + { + if(self->g_Ptr->ReadPauseState()) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_GUI_getMenuState(DF_GUI* self, void* closure) +{ + if(self->g_Ptr != NULL) + { + return PyInt_FromLong(self->g_Ptr->ReadMenuState()); + } + + Py_RETURN_NONE; +} + +static PyGetSetDef DF_GUI_getterSetters[] = +{ + {"paused", (getter)DF_GUI_getPauseState, NULL, "paused", NULL}, + {"menu_state", (getter)DF_GUI_getMenuState, NULL, "menu_state", NULL}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_GUI_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.GUI", /*tp_name*/ + sizeof(DF_GUI), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_GUI_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack GUI objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_GUI_methods, /* tp_methods */ + 0, /* tp_members */ + DF_GUI_getterSetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_GUI_init, /* tp_init */ + 0, /* tp_alloc */ + DF_GUI_new, /* tp_new */ +}; + +#endif \ No newline at end of file diff --git a/dfhack/python/pydftypes.py b/dfhack/python/pydftypes.py new file mode 100644 index 000000000..73a2b24ee --- /dev/null +++ b/dfhack/python/pydftypes.py @@ -0,0 +1,10 @@ +from collections import namedtuple + +Note = namedtuple("Note", "symbol, foreground, background, name, position") +Construction = namedtuple("Construction", "position, form, unk_8, mat_type, mat_idx, unk3, unk4, unk5, unk6, origin") + +class Name(object): + __slots__ = ["first_name", "nickname", "language", "has_name", "words", "parts_of_speech"] + +class Soul(object): + pass \ No newline at end of file From d7d05132b228041e75e2008f22ab3ff821d1f9da Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:55:31 -0500 Subject: [PATCH 25/34] updated for new composite types --- dfhack/python/DF_Imports.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dfhack/python/DF_Imports.cpp b/dfhack/python/DF_Imports.cpp index a1625f7db..dec18f790 100644 --- a/dfhack/python/DF_Imports.cpp +++ b/dfhack/python/DF_Imports.cpp @@ -33,29 +33,33 @@ static PyObject* CreatureFlags2_type = NULL; static PyObject* DesignationFlags_type = NULL; static PyObject* OccupancyFlags_type = NULL; static PyObject* ItemFlags_type = NULL; +static PyObject* BlockFlags_type = NULL; static PyObject* TypesModule = NULL; static PyObject* Note_type = NULL; static PyObject* Construction_type = NULL; static PyObject* Name_type = NULL; +static PyObject* MapBlock40d_type = NULL; static void DoImports() { if(TypesModule == NULL) { FlagsModule = PyImport_ImportModule("pydfhackflags"); - + CreatureFlags1_type = PyObject_GetAttrString(FlagsModule, "CreatureFlags1"); CreatureFlags2_type = PyObject_GetAttrString(FlagsModule, "CreatureFlags2"); DesignationFlags_type = PyObject_GetAttrString(FlagsModule, "DesignationFlags"); OccupancyFlags_type = PyObject_GetAttrString(FlagsModule, "OccupancyFlags"); ItemFlags_type = PyObject_GetAttrString(FlagsModule, "ItemFlags"); + BlockFlags_type = PyObject_GetAttrString(FlagsModule, "BlockFlags"); TypesModule = PyImport_ImportModule("pydftypes"); - + Note_type = PyObject_GetAttrString(TypesModule, "Note"); Construction_type = PyObject_GetAttrString(TypesModule, "Construction"); Name_type = PyObject_GetAttrString(TypesModule, "Name"); + MapBlock40d_type = PyObject_GetAttrString(TypesModule, "MapBlock40d"); } } From b11e7d474871735b8847d32de1effa24c154aab4 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:55:44 -0500 Subject: [PATCH 26/34] added type definition --- dfhack/python/DF_Maps.cpp | 138 ++++++++++++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 21 deletions(-) diff --git a/dfhack/python/DF_Maps.cpp b/dfhack/python/DF_Maps.cpp index 52b32d43d..ca46c2736 100644 --- a/dfhack/python/DF_Maps.cpp +++ b/dfhack/python/DF_Maps.cpp @@ -32,6 +32,7 @@ distribution. using namespace std; #include "modules/Maps.h" +#include "DF_Imports.cpp" #include "DF_Helpers.cpp" using namespace DFHack; @@ -58,7 +59,7 @@ static PyObject* BuildVein(DFHack::t_vein& v) temp = PyList_New(16); for(int i = 0; i < 16; i++) - PyList_SET_ITEM(temp, i, PyInt_FromLong(v.assignment[i]); + PyList_SET_ITEM(temp, i, PyInt_FromLong(v.assignment[i])); DICTADD(t_dict, "assignment", temp); @@ -85,7 +86,7 @@ static PyObject* BuildFrozenLiquidVein(DFHack::t_frozenliquidvein& v) temp = PyList_New(16); for(int j = 0; j < 16; j++) - PyList_SET_ITEM(temp, j, PyInt_FromLong(v.tiles[i][j]); + PyList_SET_ITEM(temp, j, PyInt_FromLong(v.tiles[i][j])); PyList_SET_ITEM(list, i, temp); } @@ -127,7 +128,7 @@ static PyObject* BuildSpatterVein(DFHack::t_spattervein& v) temp = PyList_New(16); for(int j = 0; j < 16; j++) - PyList_SET_ITEM(temp, j, PyInt_FromLong(v.intensity[i][j]); + PyList_SET_ITEM(temp, j, PyInt_FromLong(v.intensity[i][j])); PyList_SET_ITEM(list, i, temp); } @@ -158,7 +159,7 @@ static PyObject* BuildTileTypes40d(DFHack::tiletypes40d& types) static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) { - PyObject *list, *temp; + PyObject *list, *temp, *args; list = PyList_New(16); @@ -167,7 +168,11 @@ static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) temp = PyList_New(16); for(int j = 0; j < 16; j++) - PyList_SET_ITEM(temp, j, PyInt_FromLong(occ[i][j].whole)); + { + args = Py_BuildValue("(I)", occ[i][j].whole); + + PyList_SET_ITEM(temp, j, PyObject_CallObject(OccupancyFlags_type, args)); + } PyList_SET_ITEM(list, i, temp); } @@ -177,7 +182,7 @@ static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) static PyObject* BuildDesignations40d(DFHack::designations40d& des) { - PyObject *list, *temp; + PyObject *list, *temp, *args; list = PyList_New(16); @@ -186,7 +191,11 @@ static PyObject* BuildDesignations40d(DFHack::designations40d& des) temp = PyList_New(16); for(int j = 0; j < 16; j++) - PyList_SET_ITEM(temp, j, PyInt_FromLong(des[i][j].whole)); + { + args = Py_BuildValue("(I)", des[i][j].whole); + + PyList_SET_ITEM(temp, j, PyObject_CallObject(DesignationFlags_type, args)); + } PyList_SET_ITEM(list, i, temp); } @@ -201,37 +210,40 @@ static PyObject* BuildBiomeIndices40d(DFHack::biome_indices40d& idx) list = PyList_New(16); for(int i = 0; i < 16; i++) - PyList_SET_ITEM(list, i, PyInt_FromLong(idx[i]); + PyList_SET_ITEM(list, i, PyInt_FromLong(idx[i])); return list; } static PyObject* BuildMapBlock40d(DFHack::mapblock40d& block) { - PyObject* t_dict; - PyObject* temp; + PyObject *b_Obj; + PyObject *temp, *args; - t_dict = PyDict_New(); + b_Obj = PyObject_CallObject(MapBlock40d_type, NULL); temp = BuildTileTypes40d(block.tiletypes); - DICTADD(t_dict, "tiletypes", temp); + OBJSET(b_Obj, "tiletypes", temp); temp = BuildDesignations40d(block.designation); - DICTADD(t_dict, "designation", temp); + OBJSET(b_Obj, "designation", temp); temp = BuildOccupancies40d(block.occupancy); - DICTADD(t_dict, "occupancy", temp); + OBJSET(b_Obj, "occupancy", temp); - temp = BuildBiomeIdices40d(block.biome_indices); - DICTADD(t_dict, "biome_indices", temp); + temp = BuildBiomeIndices40d(block.biome_indices); + OBJSET(b_Obj, "biome_indices", temp); temp = PyInt_FromLong(block.origin); - DICTADD(t_dict, "origin", temp); + OBJSET(b_Obj, "origin", temp); + + args = Py_BuildValue("(I)", block.blockflags.whole); + temp = PyObject_CallObject(BlockFlags_type, args); temp = PyInt_FromLong(block.blockflags.whole); - DICTADD(t_dict, "_blockflags", temp); + OBJSET(b_Obj, "blockflags", temp); - return t_dict; + return b_Obj; } struct DF_Map @@ -328,6 +340,26 @@ static PyObject* DF_Map_IsValidBlock(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_ReadBlock40d(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + mapblock40d mapblock; + + if(self->m_Ptr->ReadBlock40d(x, y, z, &mapblock)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadTileTypes(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -392,7 +424,7 @@ static PyObject* DF_Map_ReadDirtyBit(DF_Map* self, PyObject* args) if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) return NULL; - self->m_Ptr->ReadDirtyBit(x, y, z, &bit) + self->m_Ptr->ReadDirtyBit(x, y, z, bit); if(bit) Py_RETURN_TRUE; @@ -414,7 +446,7 @@ static PyObject* DF_Map_ReadBlockFlags(DF_Map* self, PyObject* args) t_blockflags flags; - if(self->m_Ptr->ReadBlockFlags(x, y, z, &flags)) + if(self->m_Ptr->ReadBlockFlags(x, y, z, flags)) return PyInt_FromLong(flags.whole); } @@ -439,6 +471,21 @@ static PyObject* DF_Map_ReadRegionOffsets(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyMethodDef DF_Map_methods[] = +{ + {"Start", (PyCFunction)DF_Map_Start, METH_NOARGS, ""}, + {"Finish", (PyCFunction)DF_Map_Finish, METH_NOARGS, ""}, + {"Is_Valid_Block", (PyCFunction)DF_Map_IsValidBlock, METH_VARARGS, ""}, + {"Read_Block_40d", (PyCFunction)DF_Map_ReadBlock40d, METH_VARARGS, ""}, + {"Read_Tile_Types", (PyCFunction)DF_Map_ReadTileTypes, METH_VARARGS, ""}, + {"Read_Designations", (PyCFunction)DF_Map_ReadDesignations, METH_VARARGS, ""}, + {"Read_Occupancy", (PyCFunction)DF_Map_ReadOccupancy, METH_VARARGS, ""}, + {"Read_Dirty_Bit", (PyCFunction)DF_Map_ReadDirtyBit, METH_VARARGS, ""}, + {"Read_Block_Flags", (PyCFunction)DF_Map_ReadBlockFlags, METH_VARARGS, ""}, + {"Read_Region_Offsets", (PyCFunction)DF_Map_ReadRegionOffsets, METH_VARARGS, ""}, + {NULL} //Sentinel +}; + // Getter/Setter static PyObject* DF_Map_getSize(DF_Map* self, void* closure) @@ -455,4 +502,53 @@ static PyObject* DF_Map_getSize(DF_Map* self, void* closure) Py_RETURN_NONE; } +static PyGetSetDef DF_Map_getterSetters[] = +{ + {"size", (getter)DF_Map_getSize, NULL, "dictionaries", NULL}, + {NULL} // Sentinel +}; + +static PyTypeObject DF_Map_type = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "pydfhack.Map", /*tp_name*/ + sizeof(DF_Map), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)DF_Map_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "pydfhack Map objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DF_Map_methods, /* tp_methods */ + 0, /* tp_members */ + DF_Map_getterSetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)DF_Map_init, /* tp_init */ + 0, /* tp_alloc */ + DF_Map_new, /* tp_new */ +}; + #endif \ No newline at end of file From b563a9a22d742f3c7d3c3ebdc7b8f9f5b089009b Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:55:54 -0500 Subject: [PATCH 27/34] added remaining module types --- dfhack/python/pydfhack.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/dfhack/python/pydfhack.cpp b/dfhack/python/pydfhack.cpp index 1d1d13475..323c2ad70 100644 --- a/dfhack/python/pydfhack.cpp +++ b/dfhack/python/pydfhack.cpp @@ -96,6 +96,12 @@ PyMODINIT_FUNC initpydfhack(void) if(PyType_Ready(&DF_Construction_type) < 0) return; + if(PyType_Ready(&DF_Map_type) < 0) + return; + + if(PyType_Ready(&DF_GUI_type) < 0) + return; + module = Py_InitModule3("pydfhack", module_methods, "pydfhack extension module"); Py_INCREF(&DF_API_type); @@ -106,6 +112,10 @@ PyMODINIT_FUNC initpydfhack(void) Py_INCREF(&DF_CreatureManager_type); Py_INCREF(&DF_Translate_type); Py_INCREF(&DF_Vegetation_type); + Py_INCREF(&DF_Building_type); + Py_INCREF(&DF_Construction_type); + Py_INCREF(&DF_Map_type); + Py_INCREF(&DF_GUI_type); PyModule_AddObject(module, "API", (PyObject*)&DF_API_type); PyModule_AddObject(module, "MemInfo", (PyObject*)&DF_MemInfo_type); @@ -116,7 +126,10 @@ PyMODINIT_FUNC initpydfhack(void) PyModule_AddObject(module, "Translate", (PyObject*)&DF_Translate_type); PyModule_AddObject(module, "Vegetation", (PyObject*)&DF_Vegetation_type); PyModule_AddObject(module, "Building", (PyObject*)&DF_Building_type); - PyModule_AddObject(module, "Construction", (PyObject*)&DF_Construction_type); + PyModule_AddObject(module, "ConstructionManager", (PyObject*)&DF_Construction_type); + PyModule_AddObject(module, "Map", (PyObject*)&DF_Map_type); + PyModule_AddObject(module, "GUI", (PyObject*)&DF_GUI_type); + DoImports(); } \ No newline at end of file From b2ab2037bb45c2a211aed3aa0b53cfe52a14289f Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:56:09 -0500 Subject: [PATCH 28/34] fixed ctypes import --- dfhack/python/pydfhackflags.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/dfhack/python/pydfhackflags.py b/dfhack/python/pydfhackflags.py index 5bcae4e9c..ee0399d1c 100644 --- a/dfhack/python/pydfhackflags.py +++ b/dfhack/python/pydfhackflags.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from ctypes import Structure, Union +from ctypes import Structure, Union, c_uint class DesignationStruct(Structure): _fields_ = [("flow_size", c_uint, 3), @@ -29,7 +29,7 @@ class DesignationFlags(Union): def __init__(self, initial = 0): self.whole = initial -class OccupancyStruct(Strucure): +class OccupancyStruct(Structure): _fields_ = [("building", c_uint, 3), ("unit", c_uint, 1), ("unit_grounded", c_uint, 1), @@ -179,3 +179,17 @@ traffic_types = { "normal" : 0, "low" : 1, "high" : 2, "restricted" : 3 } + +class BlockFlagStruct(Structure): + _fields_ = [("designated", c_uint, 1), + ("unk_1", c_uint, 1), + ("liquid_1", c_uint, 1), + ("liquid_2", c_uint, 1), + ("unk_2", c_uint, 28)] + +class BlockFlags(Union): + _fields_ = [("whole", c_uint, 32), + ("bits", BlockFlagStruct)] + + def __init__(self, inital = 0): + self.whole = initial From a89eb15375a0573478a248a6a99cff4786eb61eb Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:56:18 -0500 Subject: [PATCH 29/34] trimmed definitions --- dfhack/python/pydftypes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dfhack/python/pydftypes.py b/dfhack/python/pydftypes.py index 73a2b24ee..3dea283ea 100644 --- a/dfhack/python/pydftypes.py +++ b/dfhack/python/pydftypes.py @@ -7,4 +7,7 @@ class Name(object): __slots__ = ["first_name", "nickname", "language", "has_name", "words", "parts_of_speech"] class Soul(object): + pass + +class MapBlock40d(object): pass \ No newline at end of file From f65d4cfb297b60663813ae058dd25d69433e7105 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 12:56:33 -0500 Subject: [PATCH 30/34] added getters for new modules --- dfhack/python/DF_API.cpp | 180 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) diff --git a/dfhack/python/DF_API.cpp b/dfhack/python/DF_API.cpp index 89085b97e..0f227f55f 100644 --- a/dfhack/python/DF_API.cpp +++ b/dfhack/python/DF_API.cpp @@ -33,6 +33,11 @@ distribution. #include "DF_Position.cpp" #include "DF_Material.cpp" #include "DF_CreatureManager.cpp" +#include "DF_Maps.cpp" +#include "DF_GUI.cpp" +#include "DF_Vegetation.cpp" +#include "DF_Translate.cpp" +#include "DF_Constructions.cpp" using namespace std; using namespace DFHack; @@ -44,6 +49,11 @@ struct DF_API PyObject* position; PyObject* material; PyObject* creature; + PyObject* map; + PyObject* translate; + PyObject* construction; + PyObject* vegetation; + PyObject* gui; DFHack::API* api_Ptr; }; @@ -71,6 +81,11 @@ static int DF_API_init(DF_API* self, PyObject* args, PyObject* kwds) self->position = NULL; self->material = NULL; self->creature = NULL; + self->map = NULL; + self->translate = NULL; + self->construction = NULL; + self->vegetation = NULL; + self->gui = NULL; if(!PyArg_ParseTuple(args, "s", &memFileString)) return -1; @@ -102,6 +117,21 @@ static void DF_API_dealloc(DF_API* self) PySys_WriteStdout("material xdecref\n"); Py_XDECREF(self->material); + PySys_WriteStdout("map xdecref\n"); + Py_XDECREF(self->map); + + PySys_WriteStdout("translate xdecref\n"); + Py_XDECREF(self->translate); + + PySys_WriteStdout("construction xdecref\n"); + Py_XDECREF(self->construction); + + PySys_WriteStdout("vegetation xdecref\n"); + Py_XDECREF(self->vegetation); + + PySys_WriteStdout("gui xdecref\n"); + Py_XDECREF(self->gui); + if(self->api_Ptr != NULL) { PySys_WriteStdout("api_Ptr = %i\n", (int)self->api_Ptr); @@ -271,6 +301,151 @@ static PyObject* DF_API_getCreature(DF_API* self, void* closure) Py_RETURN_NONE; } +static PyObject* DF_API_getMap(DF_API* self, void* closure) +{ + if(self->map != NULL) + return self->map; + + try + { + if(self->api_Ptr != NULL) + { + self->map = PyObject_Call((PyObject*)&DF_Map_type, NULL, NULL); + + if(self->map != NULL) + { + ((DF_Map*)(self->map))->m_Ptr = self->api_Ptr->getMaps(); + + if(((DF_Map*)(self->map))->m_Ptr != NULL) + return self->map; + } + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read map"); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_API_getTranslation(DF_API* self, void* closure) +{ + if(self->translate != NULL) + return self->translate; + + try + { + if(self->api_Ptr != NULL) + { + self->translate = PyObject_Call((PyObject*)&DF_Translate_type, NULL, NULL); + + if(self->translate != NULL) + { + ((DF_Translate*)(self->translate))->tran_Ptr = self->api_Ptr->getTranslation(); + + if(((DF_Translate*)(self->translate))->tran_Ptr != NULL) + return self->translate; + } + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read translation"); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_API_getConstruction(DF_API* self, void* closure) +{ + if(self->construction != NULL) + return self->construction; + + try + { + if(self->api_Ptr != NULL) + { + self->construction = PyObject_Call((PyObject*)&DF_Construction_type, NULL, NULL); + + if(self->construction != NULL) + { + ((DF_Construction*)(self->construction))->c_Ptr = self->api_Ptr->getConstructions(); + + if(((DF_Construction*)(self->construction))->c_Ptr != NULL) + return self->construction; + } + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read constructions"); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_API_getVegetation(DF_API* self, void* closure) +{ + if(self->vegetation != NULL) + return self->vegetation; + + try + { + if(self->api_Ptr != NULL) + { + self->vegetation = PyObject_Call((PyObject*)&DF_Vegetation_type, NULL, NULL); + + if(self->vegetation != NULL) + { + ((DF_Vegetation*)(self->vegetation))->veg_Ptr = self->api_Ptr->getVegetation(); + + if(((DF_Vegetation*)(self->vegetation))->veg_Ptr != NULL) + return self->vegetation; + } + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read vegetation"); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject* DF_API_getGUI(DF_API* self, void* closure) +{ + if(self->gui != NULL) + return self->gui; + + try + { + if(self->api_Ptr != NULL) + { + self->gui = PyObject_Call((PyObject*)&DF_GUI_type, NULL, NULL); + + if(self->gui != NULL) + { + ((DF_GUI*)(self->gui))->g_Ptr = self->api_Ptr->getGui(); + + if(((DF_GUI*)(self->gui))->g_Ptr != NULL) + return self->gui; + } + } + } + catch(...) + { + PyErr_SetString(PyExc_ValueError, "Error trying to read gui"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyGetSetDef DF_API_getterSetters[] = { {"is_attached", (getter)DF_API_getIsAttached, NULL, "is_attached", NULL}, @@ -279,6 +454,11 @@ static PyGetSetDef DF_API_getterSetters[] = {"position", (getter)DF_API_getPosition, NULL, "position", NULL}, {"materials", (getter)DF_API_getMaterial, NULL, "materials", NULL}, {"creatures", (getter)DF_API_getCreature, NULL, "creatures", NULL}, + {"maps", (getter)DF_API_getMap, NULL, "maps", NULL}, + {"translation", (getter)DF_API_getTranslation, NULL, "translation", NULL}, + {"constructions", (getter)DF_API_getConstruction, NULL, "constructions", NULL}, + {"vegetation", (getter)DF_API_getVegetation, NULL, "vegetation", NULL}, + {"gui", (getter)DF_API_getGUI, NULL, "gui", NULL}, {NULL} // Sentinel }; From 3db399504eb1fdc314ef739e65ee2953bd2f1794 Mon Sep 17 00:00:00 2001 From: doomchild Date: Thu, 15 Apr 2010 15:56:13 -0500 Subject: [PATCH 31/34] got reading/writing working --- dfhack/python/DF_Maps.cpp | 192 +++++++++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 4 deletions(-) diff --git a/dfhack/python/DF_Maps.cpp b/dfhack/python/DF_Maps.cpp index ca46c2736..daabebfaa 100644 --- a/dfhack/python/DF_Maps.cpp +++ b/dfhack/python/DF_Maps.cpp @@ -157,6 +157,19 @@ static PyObject* BuildTileTypes40d(DFHack::tiletypes40d& types) return list; } +static void ReverseBuildTileTypes40d(PyObject* list, DFHack::tiletypes40d t_types) +{ + PyObject* innerList; + + for(int i = 0; i < 16; i++) + { + innerList = PyList_GetItem(list, i); + + for(int j = 0; j < 16; j++) + t_types[i][j] = (uint16_t)PyInt_AsLong(PyList_GetItem(innerList, j)); + } +} + static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) { PyObject *list, *temp, *args; @@ -172,6 +185,8 @@ static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) args = Py_BuildValue("(I)", occ[i][j].whole); PyList_SET_ITEM(temp, j, PyObject_CallObject(OccupancyFlags_type, args)); + + Py_DECREF(args); } PyList_SET_ITEM(list, i, temp); @@ -180,6 +195,19 @@ static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) return list; } +static void ReverseBuildOccupancies40d(PyObject* list, DFHack::occupancies40d occ) +{ + PyObject* innerList; + + for(int i = 0; i < 16; i++) + { + innerList = PyList_GetItem(list, i); + + for(int j = 0; j < 16; j++) + occ[i][j].whole = (uint32_t)PyInt_AsLong(PyList_GetItem(innerList, j)); + } +} + static PyObject* BuildDesignations40d(DFHack::designations40d& des) { PyObject *list, *temp, *args; @@ -194,7 +222,9 @@ static PyObject* BuildDesignations40d(DFHack::designations40d& des) { args = Py_BuildValue("(I)", des[i][j].whole); - PyList_SET_ITEM(temp, j, PyObject_CallObject(DesignationFlags_type, args)); + PyList_SET_ITEM(temp, j, PyObject_Call(DesignationFlags_type, args, NULL)); + + Py_DECREF(args); } PyList_SET_ITEM(list, i, temp); @@ -203,6 +233,19 @@ static PyObject* BuildDesignations40d(DFHack::designations40d& des) return list; } +static void ReverseBuildDesignations40d(PyObject* list, DFHack::designations40d& des) +{ + PyObject* innerList; + + for(int i = 0; i < 16; i++) + { + innerList = PyList_GetItem(list, i); + + for(int j = 0; j < 16; j++) + des[i][j].whole = (uint32_t)PyInt_AsLong(PyList_GET_ITEM(innerList, j)); + } +} + static PyObject* BuildBiomeIndices40d(DFHack::biome_indices40d& idx) { PyObject *list; @@ -215,6 +258,12 @@ static PyObject* BuildBiomeIndices40d(DFHack::biome_indices40d& idx) return list; } +static void ReverseBuildBiomeIndices40d(PyObject* list, DFHack::biome_indices40d bio) +{ + for(int i = 0; i < 16; i++) + bio[i] = (uint8_t)PyInt_AsLong(PyList_GetItem(list, i)); +} + static PyObject* BuildMapBlock40d(DFHack::mapblock40d& block) { PyObject *b_Obj; @@ -340,6 +389,21 @@ static PyObject* DF_Map_IsValidBlock(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_GetBlockPtr(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + return PyInt_FromLong(self->m_Ptr->getBlockPtr(x, y, z)); + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadBlock40d(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -378,6 +442,29 @@ static PyObject* DF_Map_ReadTileTypes(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_WriteTileTypes(DF_Map* self, PyObject* args) +{ + PyObject* tileList; + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "IIIO", &x, &y, &z, &tileList)) + return NULL; + + tiletypes40d t_types; + + ReverseBuildTileTypes40d(tileList, t_types); + + if(self->m_Ptr->WriteTileTypes(x, y, z, &t_types)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadDesignations(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -387,7 +474,7 @@ static PyObject* DF_Map_ReadDesignations(DF_Map* self, PyObject* args) if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) return NULL; - designations40d des; + DFHack::designations40d des; if(self->m_Ptr->ReadDesignations(x, y, z, &des)) return BuildDesignations40d(des); @@ -396,6 +483,29 @@ static PyObject* DF_Map_ReadDesignations(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_WriteDesignations(DF_Map* self, PyObject* args) +{ + PyObject* desList; + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "IIIO", &x, &y, &z, &desList)) + return NULL; + + designations40d des; + + ReverseBuildDesignations40d(desList, des); + + if(self->m_Ptr->WriteDesignations(x, y, z, &des)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadOccupancy(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -414,6 +524,29 @@ static PyObject* DF_Map_ReadOccupancy(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_WriteOccupancy(DF_Map* self, PyObject* args) +{ + PyObject* occList; + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "IIIO", &x, &y, &z, &occList)) + return NULL; + + occupancies40d occ; + + ReverseBuildOccupancies40d(occList, occ); + + if(self->m_Ptr->WriteOccupancy(x, y, z, &occ)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadDirtyBit(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -435,6 +568,30 @@ static PyObject* DF_Map_ReadDirtyBit(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_WriteDirtyBit(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z, b; + bool bit; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "IIIi", &x, &y, &z, &b)) + return NULL; + + if(b != 0) + bit = true; + else + bit = false; + + if(self->m_Ptr->WriteDirtyBit(x, y, z, bit)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadBlockFlags(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -453,6 +610,28 @@ static PyObject* DF_Map_ReadBlockFlags(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_WriteBlockFlags(DF_Map* self, PyObject* args) +{ + uint32_t x, y, z, whole; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "IIII", &x, &y, &z, &whole)) + return NULL; + + t_blockflags blockFlags; + + blockFlags.whole = whole; + + if(self->m_Ptr->WriteBlockFlags(x, y, z, blockFlags)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_ReadRegionOffsets(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -478,10 +657,15 @@ static PyMethodDef DF_Map_methods[] = {"Is_Valid_Block", (PyCFunction)DF_Map_IsValidBlock, METH_VARARGS, ""}, {"Read_Block_40d", (PyCFunction)DF_Map_ReadBlock40d, METH_VARARGS, ""}, {"Read_Tile_Types", (PyCFunction)DF_Map_ReadTileTypes, METH_VARARGS, ""}, + {"Write_Tile_Types", (PyCFunction)DF_Map_WriteTileTypes, METH_VARARGS, ""}, {"Read_Designations", (PyCFunction)DF_Map_ReadDesignations, METH_VARARGS, ""}, + {"Write_Designations", (PyCFunction)DF_Map_WriteDesignations, METH_VARARGS, ""}, {"Read_Occupancy", (PyCFunction)DF_Map_ReadOccupancy, METH_VARARGS, ""}, + {"Write_Occupancy", (PyCFunction)DF_Map_WriteOccupancy, METH_VARARGS, ""}, {"Read_Dirty_Bit", (PyCFunction)DF_Map_ReadDirtyBit, METH_VARARGS, ""}, + {"Write_Dirty_Bit", (PyCFunction)DF_Map_WriteDirtyBit, METH_VARARGS, ""}, {"Read_Block_Flags", (PyCFunction)DF_Map_ReadBlockFlags, METH_VARARGS, ""}, + {"Write_Block_Flags", (PyCFunction)DF_Map_WriteBlockFlags, METH_VARARGS, ""}, {"Read_Region_Offsets", (PyCFunction)DF_Map_ReadRegionOffsets, METH_VARARGS, ""}, {NULL} //Sentinel }; @@ -496,7 +680,7 @@ static PyObject* DF_Map_getSize(DF_Map* self, void* closure) { self->m_Ptr->getSize(x, y, z); - return PyTuple_Pack(3, x, y, z); + return Py_BuildValue("III", x, y, z); } Py_RETURN_NONE; @@ -504,7 +688,7 @@ static PyObject* DF_Map_getSize(DF_Map* self, void* closure) static PyGetSetDef DF_Map_getterSetters[] = { - {"size", (getter)DF_Map_getSize, NULL, "dictionaries", NULL}, + {"size", (getter)DF_Map_getSize, NULL, "size", NULL}, {NULL} // Sentinel }; From 9c6d889a2467996a593a92f8879eeed465fa1f4a Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 16 Apr 2010 11:59:38 -0500 Subject: [PATCH 32/34] added vein types --- dfhack/python/pydftypes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dfhack/python/pydftypes.py b/dfhack/python/pydftypes.py index 3dea283ea..4e77a2b1b 100644 --- a/dfhack/python/pydftypes.py +++ b/dfhack/python/pydftypes.py @@ -2,6 +2,9 @@ from collections import namedtuple Note = namedtuple("Note", "symbol, foreground, background, name, position") Construction = namedtuple("Construction", "position, form, unk_8, mat_type, mat_idx, unk3, unk4, unk5, unk6, origin") +Vein = namedtuple("Vein", "vtable, type, flags, address, assignment") +FrozenLiquidVein = namedtuple("FrozenLiquidVein", "vtable, address, tiles") +SpatterVein = namedtuple("SpatterVein", "vtable, address, mat1, unk1, mat2, mat3, intensity") class Name(object): __slots__ = ["first_name", "nickname", "language", "has_name", "words", "parts_of_speech"] From 9729ca420ebd41c7cc55047012083b7349002d75 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 16 Apr 2010 16:21:05 -0500 Subject: [PATCH 33/34] added new type imports --- dfhack/python/DF_Imports.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dfhack/python/DF_Imports.cpp b/dfhack/python/DF_Imports.cpp index dec18f790..15e93d2b3 100644 --- a/dfhack/python/DF_Imports.cpp +++ b/dfhack/python/DF_Imports.cpp @@ -40,6 +40,9 @@ static PyObject* Note_type = NULL; static PyObject* Construction_type = NULL; static PyObject* Name_type = NULL; static PyObject* MapBlock40d_type = NULL; +static PyObject* Vein_type = NULL; +static PyObject* FrozenLiquidVein_type = NULL; +static PyObject* SpatterVein_type = NULL; static void DoImports() { @@ -60,6 +63,9 @@ static void DoImports() Construction_type = PyObject_GetAttrString(TypesModule, "Construction"); Name_type = PyObject_GetAttrString(TypesModule, "Name"); MapBlock40d_type = PyObject_GetAttrString(TypesModule, "MapBlock40d"); + Vein_type = PyObject_GetAttrString(TypesModule, "Vein"); + FrozenLiquidVein_type = PyObject_GetAttrString(TypesModule, "FrozenLiquidVein"); + SpatterVein_type = PyObject_GetAttrString(TypesModule, "SpatterVein"); } } From 765d65a107caa8c1efd994822925b42804426da3 Mon Sep 17 00:00:00 2001 From: doomchild Date: Fri, 16 Apr 2010 16:21:16 -0500 Subject: [PATCH 34/34] added vein reading --- dfhack/python/DF_Maps.cpp | 173 ++++++++++++++++++++++++++++++++------ 1 file changed, 147 insertions(+), 26 deletions(-) diff --git a/dfhack/python/DF_Maps.cpp b/dfhack/python/DF_Maps.cpp index daabebfaa..788130444 100644 --- a/dfhack/python/DF_Maps.cpp +++ b/dfhack/python/DF_Maps.cpp @@ -39,45 +39,45 @@ using namespace DFHack; static PyObject* BuildVein(DFHack::t_vein& v) { - PyObject* t_dict; + PyObject* vObj; PyObject* temp; - t_dict = PyDict_New(); + vObj = PyObject_Call(Vein_type, NULL, NULL); temp = PyInt_FromLong(v.vtable); - DICTADD(t_dict, "vtable", temp); + OBJSET(vObj, "vtable", temp); temp = PyInt_FromLong(v.type); - DICTADD(t_dict, "type", temp); + OBJSET(vObj, "type", temp); temp = PyInt_FromLong(v.flags); - DICTADD(t_dict, "flags", temp); + OBJSET(vObj, "flags", temp); temp = PyInt_FromLong(v.address_of); - DICTADD(t_dict, "address", temp); + OBJSET(vObj, "address", temp); temp = PyList_New(16); for(int i = 0; i < 16; i++) PyList_SET_ITEM(temp, i, PyInt_FromLong(v.assignment[i])); - DICTADD(t_dict, "assignment", temp); + OBJSET(vObj, "assignment", temp); - return t_dict; + return vObj; } static PyObject* BuildFrozenLiquidVein(DFHack::t_frozenliquidvein& v) { - PyObject* t_dict; + PyObject* vObj; PyObject *temp, *list; - t_dict = PyDict_New(); + vObj = PyObject_Call(FrozenLiquidVein_type, NULL, NULL); temp = PyInt_FromLong(v.vtable); - DICTADD(t_dict, "vtable", temp); + OBJSET(vObj, "vtable", temp); temp = PyInt_FromLong(v.address_of); - DICTADD(t_dict, "address", temp); + OBJSET(vObj, "address", temp); list = PyList_New(16); @@ -91,35 +91,35 @@ static PyObject* BuildFrozenLiquidVein(DFHack::t_frozenliquidvein& v) PyList_SET_ITEM(list, i, temp); } - DICTADD(t_dict, "tiles", list); + OBJSET(vObj, "tiles", list); - return t_dict; + return vObj; } static PyObject* BuildSpatterVein(DFHack::t_spattervein& v) { - PyObject* t_dict; + PyObject* vObj; PyObject *temp, *list; - t_dict = PyDict_New(); + vObj = PyObject_Call(SpatterVein_type, NULL, NULL); temp = PyInt_FromLong(v.vtable); - DICTADD(t_dict, "vtable", temp); + OBJSET(vObj, "vtable", temp); temp = PyInt_FromLong(v.address_of); - DICTADD(t_dict, "address", temp); + OBJSET(vObj, "address", temp); temp = PyInt_FromLong(v.mat1); - DICTADD(t_dict, "mat1", temp); + OBJSET(vObj, "mat1", temp); temp = PyInt_FromLong(v.unk1); - DICTADD(t_dict, "unk1", temp); + OBJSET(vObj, "unk1", temp); temp = PyInt_FromLong(v.mat2); - DICTADD(t_dict, "mat2", temp); + OBJSET(vObj, "mat2", temp); temp = PyInt_FromLong(v.mat3); - DICTADD(t_dict, "mat3", temp); + OBJSET(vObj, "mat3", temp); list = PyList_New(16); @@ -133,9 +133,71 @@ static PyObject* BuildSpatterVein(DFHack::t_spattervein& v) PyList_SET_ITEM(list, i, temp); } - DICTADD(t_dict, "intensity", list); + OBJSET(vObj, "intensity", list); - return t_dict; + return vObj; +} + +static PyObject* BuildVeinDict(std::vector& veins, std::vector& ices, std::vector& splatter) +{ + PyObject* vDict; + PyObject* vList; + int veinSize; + + vDict = PyDict_New(); + + veinSize = veins.size(); + + if(veinSize <= 0) + { + Py_INCREF(Py_None); + DICTADD(vDict, "veins", Py_None); + } + else + { + vList = PyList_New(veinSize); + + for(int i = 0; i < veinSize; i++) + PyList_SET_ITEM(vList, i, BuildVein(veins[i])); + + DICTADD(vDict, "veins", vList); + } + + veinSize = ices.size(); + + if(veinSize <= 0) + { + Py_INCREF(Py_None); + DICTADD(vDict, "ices", Py_None); + } + else + { + vList = PyList_New(veinSize); + + for(int i = 0; i < veinSize; i++) + PyList_SET_ITEM(vList, i, BuildFrozenLiquidVein(ices[i])); + + DICTADD(vDict, "ices", vList); + } + + veinSize = splatter.size(); + + if(veinSize <= 0) + { + Py_INCREF(Py_None); + DICTADD(vDict, "spatter", Py_None); + } + else + { + vList = PyList_New(veinSize); + + for(int i = 0; i < veinSize; i++) + PyList_SET_ITEM(vList, i, BuildSpatterVein(splatter[i])); + + DICTADD(vDict, "spatter", vList); + } + + return vDict; } static PyObject* BuildTileTypes40d(DFHack::tiletypes40d& types) @@ -184,7 +246,7 @@ static PyObject* BuildOccupancies40d(DFHack::occupancies40d& occ) { args = Py_BuildValue("(I)", occ[i][j].whole); - PyList_SET_ITEM(temp, j, PyObject_CallObject(OccupancyFlags_type, args)); + PyList_SET_ITEM(temp, j, PyObject_Call(OccupancyFlags_type, args, NULL)); Py_DECREF(args); } @@ -269,7 +331,7 @@ static PyObject* BuildMapBlock40d(DFHack::mapblock40d& block) PyObject *b_Obj; PyObject *temp, *args; - b_Obj = PyObject_CallObject(MapBlock40d_type, NULL); + b_Obj = PyObject_Call(MapBlock40d_type, NULL, NULL); temp = BuildTileTypes40d(block.tiletypes); OBJSET(b_Obj, "tiletypes", temp); @@ -371,6 +433,42 @@ static PyObject* DF_Map_Finish(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_ReadGeology(DF_Map* self, PyObject* args) +{ + PyObject *list, *temp; + int outerSize, innerSize; + + if(self->m_Ptr != NULL) + { + std::vector< std::vector > geoVec; + + if(self->m_Ptr->ReadGeology(geoVec)) + { + outerSize = geoVec.size(); + + list = PyList_New(outerSize); + + for(int i = 0; i < outerSize; i++) + { + std::vector innerVec = geoVec[i]; + + innerSize = innerVec.size(); + + temp = PyList_New(innerSize); + + for(int j = 0; j < innerSize; j++) + PyList_SET_ITEM(temp, j, PyInt_FromLong(innerVec[i])); + + PyList_SET_ITEM(list, i, temp); + } + + return list; + } + } + + Py_RETURN_NONE; +} + static PyObject* DF_Map_IsValidBlock(DF_Map* self, PyObject* args) { uint32_t x, y, z; @@ -650,10 +748,33 @@ static PyObject* DF_Map_ReadRegionOffsets(DF_Map* self, PyObject* args) Py_RETURN_NONE; } +static PyObject* DF_Map_ReadVeins(DF_Map* self, PyObject* args, PyObject* kwds) +{ + uint32_t x, y, z; + + if(self->m_Ptr != NULL) + { + if(!PyArg_ParseTuple(args, "III", &x, &y, &z)) + return NULL; + + std::vector veins; + std::vector ices; + std::vector splatter; + + if(self->m_Ptr->ReadVeins(x, y, z, &veins, &ices, &splatter)) + { + return BuildVeinDict(veins, ices, splatter); + } + } + + Py_RETURN_NONE; +} + static PyMethodDef DF_Map_methods[] = { {"Start", (PyCFunction)DF_Map_Start, METH_NOARGS, ""}, {"Finish", (PyCFunction)DF_Map_Finish, METH_NOARGS, ""}, + {"Read_Geology", (PyCFunction)DF_Map_ReadGeology, METH_NOARGS, ""}, {"Is_Valid_Block", (PyCFunction)DF_Map_IsValidBlock, METH_VARARGS, ""}, {"Read_Block_40d", (PyCFunction)DF_Map_ReadBlock40d, METH_VARARGS, ""}, {"Read_Tile_Types", (PyCFunction)DF_Map_ReadTileTypes, METH_VARARGS, ""},