fixed initializer and deallocator

develop
doomchild 2010-04-07 14:33:24 -05:00
parent 07f81674ad
commit 73d0a103fd
1 changed files with 125 additions and 68 deletions

@ -38,8 +38,7 @@ struct DF_Creature_Base
PyObject_HEAD PyObject_HEAD
// simple type stuff // simple type stuff
uint32_t position; uint32_t origin;
PyObject* origin;
uint32_t c_type; uint32_t c_type;
uint8_t profession; uint8_t profession;
uint16_t mood; uint16_t mood;
@ -58,6 +57,7 @@ struct DF_Creature_Base
PyObject* custom_profession; PyObject* custom_profession;
// composites // composites
PyObject* position;
PyObject *name, *squad_name, *artifact_name; PyObject *name, *squad_name, *artifact_name;
PyObject* current_job; PyObject* current_job;
@ -71,87 +71,77 @@ struct DF_Creature_Base
PyObject* labor_list; PyObject* labor_list;
}; };
static PyObject* BuildCreature(DFHack::t_creature& creature) // API type Allocation, Deallocation, and Initialization
static PyObject* DF_Creature_Base_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{ {
DF_Creature_Base* obj; DF_Creature_Base* self;
obj = (DF_Creature_Base*)PyObject_Call((PyObject*)&DF_Creature_Base_type);
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->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)
obj->custom_profession = PyString_FromString(creature.custom_profession);
obj->flags1 = PyObject_Call(CreatureFlags1_type, PyInt_FromLong(creature.flags1.whole));
obj->flags2 = PyObject_Call(CreatureFlags2_type, PyInt_FromLong(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); self = (DF_Creature_Base*)type->tp_alloc(type, 0);
for(int i = 0; i < NUM_CREATURE_TRAITS; i++) if(self != NULL)
PyList_SetItem(obj->trait_list, i, PyInt_FromLong(creature.traits[i])); {
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->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->labor_list = NULL;
}
return obj; return (PyObject*)self;
} }
// API type Allocation, Deallocation, and Initialization
static void DF_Creature_Base_dealloc(DF_Creature_Base* self) static void DF_Creature_Base_dealloc(DF_Creature_Base* self)
{ {
if(self != NULL) if(self != NULL)
{ {
Py_CLEAR(self->position); Py_CLEAR(self->position);
Py_CLEAR(self->c_type);
Py_CLEAR(self->flags1); Py_CLEAR(self->flags1);
Py_CLEAR(self->flags2); Py_CLEAR(self->flags2);
Py_CLEAR(self->custom_profession);
Py_CLEAR(self->name); Py_CLEAR(self->name);
Py_CLEAR(self->squad_name); Py_CLEAR(self->squad_name);
Py_CLEAR(self->artifact_name); Py_CLEAR(self->artifact_name);
Py_CLEAR(self->current_job);
Py_CLEAR(self->flags1); Py_CLEAR(self->flags1);
Py_CLEAR(self->flags2); Py_CLEAR(self->flags2);
if(self->labor_list != NULL) Py_CLEAR(self->labor_list);
PyList_Clear(self->labor_list); Py_CLEAR(self->trait_list);
if(self->trait_list != NULL) Py_CLEAR(self->skill_list);
PyList_Clear(self->trait_list); Py_CLEAR(self->like_list);
if(self->skill_list != NULL)
PyList_Clear(self->skill_list); // if(self->labor_list != NULL)
if(self->like_list != NULL) // PyList_Clear(self->labor_list);
PyList_Clear(self->like_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);
self->ob_type->tp_free((PyObject*)self); self->ob_type->tp_free((PyObject*)self);
} }
@ -163,13 +153,23 @@ static PyMemberDef DF_Creature_Base_members[] =
{"type", T_UINT, offsetof(DF_Creature_Base, c_type), 0, ""}, {"type", T_UINT, offsetof(DF_Creature_Base, c_type), 0, ""},
{"flags1", T_OBJECT_EX, offsetof(DF_Creature_Base, flags1), 0, ""}, {"flags1", T_OBJECT_EX, offsetof(DF_Creature_Base, flags1), 0, ""},
{"flags2", T_OBJECT_EX, offsetof(DF_Creature_Base, flags2), 0, ""}, {"flags2", T_OBJECT_EX, offsetof(DF_Creature_Base, flags2), 0, ""},
{"name", T_OBJECT_EX, offsetof(DF_Creature_base, name), 0, ""}, {"name", T_OBJECT_EX, offsetof(DF_Creature_Base, name), 0, ""},
{"squad_name", T_OBJECT_EX, offsetof(DF_Creature_Base, squad_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, ""}, {"artifact_name", T_OBJECT_EX, offsetof(DF_Creature_Base, artifact_name), 0, ""},
{"profession", T_INT, offsetof(DF_Creature_base, profession), 0, ""}, {"profession", T_INT, offsetof(DF_Creature_Base, profession), 0, ""},
{"custom_profession", T_OBJECT_EX, offsetof(DF_Creature_Base, custom_profession), 0, ""}, {"custom_profession", T_OBJECT_EX, offsetof(DF_Creature_Base, custom_profession), 0, ""},
{"happiness", T_SHORT, offsetof(DF_Creature_Base, happiness), 0, ""}, {"happiness", T_SHORT, offsetof(DF_Creature_Base, happiness), 0, ""},
{NULL} {NULL} //Sentinel
};
static PyGetSetDef DF_Creature_Base_getterSetters[] =
{
{NULL} //Sentinel
};
static PyMethodDef DF_Creature_Base_methods[] =
{
{NULL} //Sentinel
}; };
static PyTypeObject DF_Creature_Base_type = static PyTypeObject DF_Creature_Base_type =
@ -203,7 +203,7 @@ static PyTypeObject DF_Creature_Base_type =
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
DF_Creature_Base_methods, /* tp_methods */ DF_Creature_Base_methods, /* tp_methods */
0, /* tp_members */ DF_Creature_Base_members, /* tp_members */
DF_Creature_Base_getterSetters, /* tp_getset */ DF_Creature_Base_getterSetters, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
0, /* tp_dict */ 0, /* tp_dict */
@ -212,7 +212,64 @@ static PyTypeObject DF_Creature_Base_type =
0, /* tp_dictoffset */ 0, /* tp_dictoffset */
0, /* tp_init */ 0, /* tp_init */
0, /* tp_alloc */ 0, /* tp_alloc */
0, /* tp_new */ DF_Creature_Base_new, /* tp_new */
}; };
static PyObject* BuildCreature(DFHack::t_creature& creature)
{
DF_Creature_Base* obj;
obj = (DF_Creature_Base*)PyObject_Call((PyObject*)&DF_Creature_Base_type, NULL, NULL);
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->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->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]));
return (PyObject*)obj;
}
#endif #endif