From ba9d2d14c2e96ace7816ad51811addabcc7c79f7 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Wed, 16 Jun 2010 07:55:12 +0800 Subject: [PATCH] Magic number cleaning --- library/include/dfhack/modules/Creatures.h | 2 ++ library/modules/Creatures.cpp | 29 +++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/library/include/dfhack/modules/Creatures.h b/library/include/dfhack/modules/Creatures.h index 9d58ec12d..670e37cff 100644 --- a/library/include/dfhack/modules/Creatures.h +++ b/library/include/dfhack/modules/Creatures.h @@ -280,6 +280,8 @@ namespace DFHack // FIXME: define in Memory.xml instead? #define NUM_CREATURE_TRAITS 30 #define NUM_CREATURE_LABORS 102 +#define NUM_CREATURE_MENTAL_ATTRIBUTES 13 +#define NUM_CREATURE_PHYSICAL_ATTRIBUTES 6 struct t_soul { diff --git a/library/modules/Creatures.cpp b/library/modules/Creatures.cpp index c47201ffc..53a9060c9 100644 --- a/library/modules/Creatures.cpp +++ b/library/modules/Creatures.cpp @@ -195,7 +195,9 @@ bool Creatures::ReadCreature (const int32_t index, t_creature & furball) p->readDWord (temp + offs.flags2_offset, furball.flags2.whole); // physical attributes - p->read(temp + offs.physical_offset, sizeof(t_attrib) * 6, (uint8_t *)&furball.strength); + p->read(temp + offs.physical_offset, + sizeof(t_attrib) * NUM_CREATURE_PHYSICAL_ATTRIBUTES, + (uint8_t *)&furball.strength); // mood stuff furball.mood = (int16_t) p->readWord (temp + offs.mood_offset); @@ -252,25 +254,34 @@ bool Creatures::ReadCreature (const int32_t index, t_creature & furball) */ uint32_t soul = p->readDWord(temp + offs.default_soul_offset); furball.has_default_soul = false; + if(soul) { furball.has_default_soul = true; // get first soul's skills DfVector skills(p, soul + offs.soul_skills_vector_offset); furball.defaultSoul.numSkills = skills.size(); + for (uint32_t i = 0; i < furball.defaultSoul.numSkills;i++) { uint32_t temp2 = skills[i]; // a byte: this gives us 256 skills maximum. ??? furball.defaultSoul.skills[i].id = p->readDWord (temp2); - furball.defaultSoul.skills[i].rating = p->readDWord (temp2 + 4); - furball.defaultSoul.skills[i].experience = p->readDWord (temp2 + 8); + furball.defaultSoul.skills[i].rating = + p->readDWord (temp2 + offsetof(t_skill, rating)); + furball.defaultSoul.skills[i].experience = + p->readDWord (temp2 + offsetof(t_skill, experience)); } + // mental attributes are part of the soul - p->read(soul + offs.soul_mental_offset, sizeof(t_attrib) * 13, (uint8_t *)&furball.defaultSoul.analytical_ability); + p->read(soul + offs.soul_mental_offset, + sizeof(t_attrib) * NUM_CREATURE_MENTAL_ATTRIBUTES, + (uint8_t *)&furball.defaultSoul.analytical_ability); // traits as well - p->read(soul + offs.soul_traits_offset, sizeof (uint16_t) * NUM_CREATURE_TRAITS, (uint8_t *) &furball.defaultSoul.traits); + p->read(soul + offs.soul_traits_offset, + sizeof (uint16_t) * NUM_CREATURE_TRAITS, + (uint8_t *) &furball.defaultSoul.traits); } DfVector app(p, temp + offs.appearance_vector_offset); @@ -402,8 +413,8 @@ bool Creatures::WriteSkills(const uint32_t index, const t_soul &soul) for (uint32_t i=0; iwriteDWord(temp2 + 4, soul.skills[i].rating); - p->writeDWord(temp2 + 8, soul.skills[i].experience); + p->writeDWord(temp2 + offsetof(t_skill, rating), soul.skills[i].rating); + p->writeDWord(temp2 + offsetof(t_skill, experiance), soul.skills[i].experience); } return true; @@ -423,12 +434,12 @@ bool Creatures::WriteAttributes(const uint32_t index, const t_creature &creature // physical attributes p->write(temp + d->creatures.physical_offset, - sizeof(t_attrib) * 6, + sizeof(t_attrib) * NUM_CREATURE_PHYSICAL_ATTRIBUTES, (uint8_t *)&creature.strength); // mental attributes are part of the soul p->write(souloff + d->creatures.soul_mental_offset, - sizeof(t_attrib) * 13, + sizeof(t_attrib) * NUM_CREATURE_MENTAL_ATTRIBUTES, (uint8_t *)&creature.defaultSoul.analytical_ability); return true;