From 82dbf08d0ba1f1fb2025026dd13bb7151e8c5d4d Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 28 Apr 2010 18:09:32 +0200 Subject: [PATCH] Modified the material API --- dfhack/include/modules/Materials.h | 22 +++++++++++-- dfhack/modules/Materials.cpp | 36 +++++++++++++-------- examples/catsplosion.cpp | 15 +++++---- examples/construction_dump.cpp | 5 ++- examples/creaturedump.cpp | 50 +++++++++++++++--------------- examples/materialtest.cpp | 48 +++++++++++++--------------- examples/spatterdump.cpp | 7 ++--- examples/treedump.cpp | 5 ++- tools/moodump.cpp | 26 +++++----------- tools/prospector.cpp | 5 ++- 10 files changed, 113 insertions(+), 106 deletions(-) diff --git a/dfhack/include/modules/Materials.h b/dfhack/include/modules/Materials.h index 65a18776b..784a72096 100644 --- a/dfhack/include/modules/Materials.h +++ b/dfhack/include/modules/Materials.h @@ -75,19 +75,35 @@ namespace DFHack Materials(DFHack::APIPrivate * _d); ~Materials(); + + std::vector inorganic; + std::vector organic; + std::vector tree; + std::vector plant; + std::vector race; + std::vector raceEx; + std::vector color; + bool ReadInorganicMaterials (void); + bool ReadOrganicMaterials (void); + bool ReadWoodMaterials (void); + bool ReadPlantMaterials (void); + bool ReadCreatureTypes (void); + bool ReadCreatureTypesEx (void); + bool ReadDescriptorColors(void); + + void ReadAllMaterials(void); + /* bool ReadInorganicMaterials (std::vector & output); bool ReadOrganicMaterials (std::vector & output); - bool ReadWoodMaterials (std::vector & output); bool ReadPlantMaterials (std::vector & output); - // bool ReadPlantMaterials (std::vector & output); // TODO: maybe move to creatures? bool ReadCreatureTypes (std::vector & output); bool ReadCreatureTypesEx (vector & creatures); - bool ReadDescriptorColors(std::vector & output); + */ private: class Private; Private* d; diff --git a/dfhack/modules/Materials.cpp b/dfhack/modules/Materials.cpp index 8f00b96c6..f39859f1d 100644 --- a/dfhack/modules/Materials.cpp +++ b/dfhack/modules/Materials.cpp @@ -197,33 +197,33 @@ inline bool ReadNamesOnly(Process* p, uint32_t address, vector & nam return true; } -bool Materials::ReadInorganicMaterials (vector & inorganic) +bool Materials::ReadInorganicMaterials (void) { return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_inorganics"), inorganic ); } -bool Materials::ReadOrganicMaterials (vector & organic) +bool Materials::ReadOrganicMaterials (void) { return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_organics_all"), organic ); } -bool Materials::ReadWoodMaterials (vector & trees) +bool Materials::ReadWoodMaterials (void) { - return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_organics_trees"), trees ); + return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_organics_trees"), tree ); } -bool Materials::ReadPlantMaterials (vector & plants) +bool Materials::ReadPlantMaterials (void) { - return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_organics_plants"), plants ); + return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("mat_organics_plants"), plant ); } -bool Materials::ReadCreatureTypes (vector & creatures) +bool Materials::ReadCreatureTypes (void) { - return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("creature_type_vector"), creatures ); + return ReadNamesOnly(d->owner, d->owner->getDescriptor()->getAddress ("creature_type_vector"), race ); return true; } -bool Materials::ReadDescriptorColors (vector & color) +bool Materials::ReadDescriptorColors (void) { Process * p = d->owner; DfVector p_colors (p, p->getDescriptor()->getAddress ("descriptor_colors_vector")); @@ -246,7 +246,7 @@ bool Materials::ReadDescriptorColors (vector & color) return true; } -bool Materials::ReadCreatureTypesEx (vector & creatures) +bool Materials::ReadCreatureTypesEx (void) { Process *p = d->owner; memory_info *mem = d->owner->getDescriptor(); @@ -257,8 +257,8 @@ bool Materials::ReadCreatureTypesEx (vector & creatures) uint32_t sizecas = 0; uint32_t tile_offset = mem->getOffset ("creature_tile"); uint32_t tile_color_offset = mem->getOffset ("creature_tile_color"); - creatures.clear(); - creatures.reserve (size); + raceEx.clear(); + raceEx.reserve (size); for (uint32_t i = 0; i < size;i++) { t_creaturetype mat; @@ -279,8 +279,18 @@ bool Materials::ReadCreatureTypesEx (vector & creatures) mat.tilecolor.fore = p->readWord( p_races[i] + tile_color_offset ); mat.tilecolor.back = p->readWord( p_races[i] + tile_color_offset + 2 ); mat.tilecolor.bright = p->readWord( p_races[i] + tile_color_offset + 4 ); - creatures.push_back(mat); + raceEx.push_back(mat); } return true; } +void Materials::ReadAllMaterials(void) +{ + this->ReadInorganicMaterials(); + this->ReadOrganicMaterials(); + this->ReadWoodMaterials(); + this->ReadPlantMaterials(); + this->ReadCreatureTypes(); + this->ReadCreatureTypesEx(); + this->ReadDescriptorColors(); +} diff --git a/examples/catsplosion.cpp b/examples/catsplosion.cpp index 90fabf180..27f8c8fa5 100644 --- a/examples/catsplosion.cpp +++ b/examples/catsplosion.cpp @@ -28,7 +28,6 @@ using namespace DFHack; int main ( int argc, char** argv ) { - vector creaturestypes; DFHack::memory_info *mem; DFHack::Process *proc; uint32_t creature_pregnancy_offset; @@ -89,7 +88,7 @@ int main ( int argc, char** argv ) DFHack::Creatures *Cre = DF.getCreatures(); creature_pregnancy_offset = mem->getOffset("creature_pregnancy"); - if(!Mats->ReadCreatureTypesEx(creaturestypes)) + if(!Mats->ReadCreatureTypesEx()) { cerr << "Can't get the creature types." << endl; #ifndef LINUX_BUILD @@ -123,17 +122,17 @@ int main ( int argc, char** argv ) { DFHack::t_creature creature; Cre->ReadCreature(i,creature); - DFHack::t_creaturetype & crt = creaturestypes[creature.race]; + DFHack::t_creaturetype & crt = Mats->raceEx[creature.race]; string castename = crt.castes[creature.sex].rawname; if(castename == "FEMALE") { - female_counts[creaturestypes[creature.race].rawname].push_back(creature); - male_counts[creaturestypes[creature.race].rawname].size(); + female_counts[Mats->raceEx[creature.race].rawname].push_back(creature); + male_counts[Mats->raceEx[creature.race].rawname].size(); } else // male, other, etc. { - male_counts[creaturestypes[creature.race].rawname].push_back(creature); - female_counts[creaturestypes[creature.race].rawname].size(); //auto initialize the females as well + male_counts[Mats->raceEx[creature.race].rawname].push_back(creature); + female_counts[Mats->raceEx[creature.race].rawname].size(); //auto initialize the females as well } } @@ -175,4 +174,4 @@ int main ( int argc, char** argv ) cin.ignore(); #endif return 0; -} \ No newline at end of file +} diff --git a/examples/construction_dump.cpp b/examples/construction_dump.cpp index 1f81faeb1..19c52d1e1 100644 --- a/examples/construction_dump.cpp +++ b/examples/construction_dump.cpp @@ -43,8 +43,7 @@ int main (int numargs, const char ** args) DFHack::Constructions *Cons = DF.getConstructions(); DFHack::Materials *Mats = DF.getMaterials(); - vector inorganics; - Mats->ReadInorganicMaterials(inorganics); + Mats->ReadInorganicMaterials(); uint32_t numConstr; Cons->Start(numConstr); @@ -65,7 +64,7 @@ int main (int numargs, const char ** args) if(con.mat_type == 0) { if(con.mat_idx != 0xffffffff) - matstr = inorganics[con.mat_idx].id; + matstr = Mats->inorganic[con.mat_idx].id; else matstr = "inorganic"; } switch(con.form) diff --git a/examples/creaturedump.cpp b/examples/creaturedump.cpp index 120af3b57..8aedd677b 100644 --- a/examples/creaturedump.cpp +++ b/examples/creaturedump.cpp @@ -18,14 +18,6 @@ using namespace std; #include #include "miscutils.h" -struct matGlosses -{ - vector plantMat; - vector woodMat; - vector stoneMat; - vector metalMat; - vector creatureMat; -}; enum likeType { FAIL = 0, @@ -34,8 +26,7 @@ enum likeType FOOD = 3 }; -vector creaturestypes; -matGlosses mat; +DFHack::Materials * Materials; vector< vector > itemTypes; DFHack::memory_info *mem; vector< vector > englishWords; @@ -149,11 +140,11 @@ likeType printLike40d(DFHack::t_like like, const matGlosses & mat,const vector< void printCreature(DFHack::API & DF, const DFHack::t_creature & creature) { - cout << "address: " << hex << creature.origin << dec << " creature type: " << creaturestypes[creature.race].rawname - << "[" << creaturestypes[creature.race].tile_character - << "," << creaturestypes[creature.race].tilecolor.fore - << "," << creaturestypes[creature.race].tilecolor.back - << "," << creaturestypes[creature.race].tilecolor.bright + cout << "address: " << hex << creature.origin << dec << " creature type: " << Materials->raceEx[creature.race].rawname + << "[" << Materials->raceEx[creature.race].tile_character + << "," << Materials->raceEx[creature.race].tilecolor.fore + << "," << Materials->raceEx[creature.race].tilecolor.back + << "," << Materials->raceEx[creature.race].tilecolor.bright << "]" << ", position: " << creature.x << "x " << creature.y << "y "<< creature.z << "z" << endl; bool addendl = false; @@ -242,7 +233,7 @@ void printCreature(DFHack::API & DF, const DFHack::t_creature & creature) } cout << endl; - if(creature.mood != -1) + if((creature.mood != -1) && (creature.mood<5)) { cout << "mood: " << creature.mood << ", skill: " << mem->getSkill(creature.mood_skill) << endl; vector mymat; @@ -257,8 +248,8 @@ void printCreature(DFHack::API & DF, const DFHack::t_creature & creature) case 0: if(mymat[i].typeD>=0) { - if(mymat[i].typeD<=mat.metalMat.size()) - sprintf(maintype, "%s bar", mat.metalMat[mymat[i].typeD].id); + if(mymat[i].typeD<=Materials->inorganic.size()) + sprintf(maintype, "%s bar", Materials->inorganic[mymat[i].typeD].id); else strcpy(maintype, "invalid metal bar"); } @@ -289,12 +280,21 @@ void printCreature(DFHack::API & DF, const DFHack::t_creature & creature) case 24: strcpy(maintype, "weapon?"); break; + case 26: + strcpy(maintype, "footwear"); + break; + case 28: + strcpy(maintype, "headwear"); + break; case 54: strcpy(maintype, "leather"); break; case 57: strcpy(maintype, "cloth"); break; + case 71: + strcpy(maintype, "food"); + break; default: strcpy(maintype, "unknown"); break; @@ -464,12 +464,12 @@ int main (int numargs, char ** args) } mem = DF.getMemoryInfo(); - if(!Materials->ReadInorganicMaterials(mat.metalMat)) - { - cerr << "Can't get the inorganics types." << endl; - return 1; - } - if(!Materials->ReadCreatureTypesEx(creaturestypes)) + if(!Materials->ReadInorganicMaterials()) + { + cerr << "Can't get the inorganics types." << endl; + return 1; + } + if(!Materials->ReadCreatureTypesEx()) { cerr << "Can't get the creature types." << endl; return 1; @@ -486,7 +486,7 @@ int main (int numargs, char ** args) { DFHack::t_creature temp; Creatures->ReadCreature(i,temp); - if(check.empty() || string(creaturestypes[temp.race].rawname) == check) + if(check.empty() || string(Materials->raceEx[temp.race].rawname) == check) { cout << "index " << i << " "; diff --git a/examples/materialtest.cpp b/examples/materialtest.cpp index 487cd9a12..77be49ba7 100644 --- a/examples/materialtest.cpp +++ b/examples/materialtest.cpp @@ -43,39 +43,36 @@ int main (int numargs, const char ** args) DFHack::Materials *Materials = DF.getMaterials(); cout << "----==== Inorganic ====----" << endl; - vector matgloss; - Materials->ReadInorganicMaterials (matgloss); - for(uint32_t i = 0; i < matgloss.size();i++) + Materials->ReadInorganicMaterials (); + for(uint32_t i = 0; i < Materials->inorganic.size();i++) { - cout << i << ": " << matgloss[i].id << endl; + cout << i << ": " << Materials->inorganic[i].id << endl; } cout << endl << "----==== Organic ====----" << endl; - vector organic; - Materials->ReadOrganicMaterials (matgloss); - for(uint32_t i = 0; i < matgloss.size();i++) + Materials->ReadOrganicMaterials (); + for(uint32_t i = 0; i < Materials->organic.size();i++) { - cout << i << ": " << matgloss[i].id << endl; + cout << i << ": " << Materials->organic[i].id << endl; } cout << endl << "----==== Organic - trees ====----" << endl; - Materials->ReadWoodMaterials (matgloss); - for(uint32_t i = 0; i < matgloss.size();i++) + Materials->ReadWoodMaterials (); + for(uint32_t i = 0; i < Materials->tree.size();i++) { - cout << i << ": " << matgloss[i].id << endl; + cout << i << ": " << Materials->tree[i].id << endl; } cout << endl << "----==== Organic - plants ====----" << endl; - Materials->ReadPlantMaterials (matgloss); - for(uint32_t i = 0; i < matgloss.size();i++) + Materials->ReadPlantMaterials (); + for(uint32_t i = 0; i < Materials->plant.size();i++) { - cout << i << ": " << matgloss[i].id << endl; + cout << i << ": " << Materials->plant[i].id << endl; } cout << endl << "----==== Creature types ====----" << endl; - vector creature; - Materials->ReadCreatureTypesEx (creature); - for(uint32_t i = 0; i < creature.size();i++) + Materials->ReadCreatureTypesEx (); + for(uint32_t i = 0; i < Materials->raceEx.size();i++) { - cout << i << ": " << creature[i].rawname << endl; - vector & castes = creature[i].castes; + cout << i << ": " << Materials->raceEx[i].rawname << endl; + vector & castes = Materials->raceEx[i].castes; for(uint32_t j = 0; j < castes.size();j++) { cout << " [" @@ -88,14 +85,13 @@ int main (int numargs, const char ** args) cout << endl; } cout << endl << "----==== Color descriptors ====----" << endl; - vector colors; - Materials->ReadDescriptorColors(colors); - for(uint32_t i = 0; i < colors.size();i++) + Materials->ReadDescriptorColors(); + for(uint32_t i = 0; i < Materials->color.size();i++) { - cout << i << ": " << colors[i].id << " - " << colors[i].name << "[" - << (unsigned int) (colors[i].r*255) << ":" - << (unsigned int) (colors[i].v*255) << ":" - << (unsigned int) (colors[i].b*255) << ":" + cout << i << ": " << Materials->color[i].id << " - " << Materials->color[i].name << "[" + << (unsigned int) (Materials->color[i].r*255) << ":" + << (unsigned int) (Materials->color[i].v*255) << ":" + << (unsigned int) (Materials->color[i].b*255) << ":" << "]" << endl; } #ifndef LINUX_BUILD diff --git a/examples/spatterdump.cpp b/examples/spatterdump.cpp index 69d1aeedd..09207de42 100644 --- a/examples/spatterdump.cpp +++ b/examples/spatterdump.cpp @@ -100,9 +100,8 @@ int main (int numargs, const char ** args) DFHack::Maps *Maps =DF.getMaps(); DFHack::Position *Pos =DF.getPosition(); DFHack::Materials *Mats =DF.getMaterials(); - vector creature_types; - Mats->ReadCreatureTypes(creature_types); + Mats->ReadCreatureTypes(); // init the map if(!Maps->Start()) @@ -133,7 +132,7 @@ int main (int numargs, const char ** args) for(uint32_t i = 0; i < splatter.size(); i++) { printf("Splatter %d\nmat1: %d\nunknown: %d\nmat2: %d\nmat3: %d\n",i,splatter[i].mat1,splatter[i].unk1,splatter[i].mat2,splatter[i].mat3); - cout << PrintSplatterType(splatter[i].mat1,splatter[i].mat2,creature_types) << endl; + cout << PrintSplatterType(splatter[i].mat1,splatter[i].mat2,Mats->race) << endl; printf("Address 0x%08x\n",splatter[i].address_of); for(uint32_t yyy = 0; yyy < 16; yyy++) { @@ -168,7 +167,7 @@ int main (int numargs, const char ** args) for(uint32_t i = 0; i < splatter.size(); i++) { printf("Splatter %d\nmat1: %d\nunknown: %d\nmat2: %d\nmat3: %d\n",i,splatter[i].mat1,splatter[i].unk1,splatter[i].mat2,splatter[i].mat3); - PrintSplatterType(splatter[i].mat1,splatter[i].mat2,creature_types); + PrintSplatterType(splatter[i].mat1,splatter[i].mat2,Mats->race); cout << endl; printf("Address 0x%08x\n",splatter[i].address_of); for(uint32_t y = 0; y < 16; y++) diff --git a/examples/treedump.cpp b/examples/treedump.cpp index a5f00d35e..4b9aefc1d 100644 --- a/examples/treedump.cpp +++ b/examples/treedump.cpp @@ -49,8 +49,7 @@ int main (int numargs, const char ** args) DFHack::Position * pos = DF.getPosition(); DFHack::Vegetation * v = DF.getVegetation(); DFHack::Materials * mat = DF.getMaterials(); - vector organics; - mat->ReadOrganicMaterials(organics); + mat->ReadOrganicMaterials(); int32_t x,y,z; pos->getCursorCoords(x,y,z); @@ -82,7 +81,7 @@ int main (int numargs, const char ** args) { cout << "near-water "; } - cout << organics[tree.material].id << " "; + cout << mat->organic[tree.material].id << " "; if(tree.type == 0 || tree.type == 1) { cout << "tree"; diff --git a/tools/moodump.cpp b/tools/moodump.cpp index 2864cfd70..a7864b5cf 100644 --- a/tools/moodump.cpp +++ b/tools/moodump.cpp @@ -15,17 +15,7 @@ using namespace std; #include #include -struct matGlosses -{ - vector plantMat; - vector woodMat; - vector stoneMat; - vector metalMat; - vector creatureMat; -}; - -vector creaturestypes; -matGlosses mat; +DFHack::Materials * Materials; vector< vector > itemTypes; DFHack::memory_info *mem; vector< vector > englishWords; @@ -53,7 +43,7 @@ int main (int numargs, char ** args) check = args[1]; DFHack::Creatures * Creatures = DF.getCreatures(); - DFHack::Materials * Materials = DF.getMaterials(); + Materials = DF.getMaterials(); DFHack::Translation * Tran = DF.getTranslation(); uint32_t numCreatures; @@ -75,13 +65,13 @@ int main (int numargs, char ** args) } mem = DF.getMemoryInfo(); - if(!Materials->ReadInorganicMaterials(mat.metalMat)) - { - cerr << "Can't get the inorganics types." << endl; - return 1; - } + if(!Materials->ReadInorganicMaterials()) + { + cerr << "Can't get the inorganics types." << endl; + return 1; + } - if(!Materials->ReadCreatureTypesEx(creaturestypes)) + if(!Materials->ReadCreatureTypesEx()) { cerr << "Can't get the creature types." << endl; return 1; diff --git a/tools/prospector.cpp b/tools/prospector.cpp index 73ac9bcd2..4f36ffe8c 100644 --- a/tools/prospector.cpp +++ b/tools/prospector.cpp @@ -57,7 +57,6 @@ int main (int argc, const char* argv[]) DFHack::mapblock40d Block; map materials; materials.clear(); - vector stonetypes; vector global_features; std::map > local_features; @@ -110,7 +109,7 @@ int main (int argc, const char* argv[]) return 1; } // get stone matgloss mapping - if(!Mats->ReadInorganicMaterials(stonetypes)) + if(!Mats->ReadInorganicMaterials()) { //DF.DestroyMap(); cerr << "Can't get the materials." << endl; @@ -289,7 +288,7 @@ int main (int argc, const char* argv[]) } else { - cout << stonetypes[p->first].id << " : " << p->second << endl; + cout << Mats->inorganic[p->first].id << " : " << p->second << endl; } } DF.Detach();