From 174d9d0739d7a0afc6b889eafc154b8b446dc932 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Tue, 24 Jan 2012 15:36:30 +0400 Subject: [PATCH] Tweak showmood, add a few validity checks, commit a dfhack.init example. --- dfhack.init-example | 34 +++++++++++++++++++++++ library/include/modules/Materials.h | 11 ++++++++ library/modules/Materials.cpp | 38 +++++++++++++++++++++---- plugins/cleaners.cpp | 5 ++++ plugins/deramp.cpp | 6 ++++ plugins/liquids.cpp | 7 +++++ plugins/showmood.cpp | 43 ++++++++++++++++++----------- 7 files changed, 123 insertions(+), 21 deletions(-) create mode 100644 dfhack.init-example diff --git a/dfhack.init-example b/dfhack.init-example new file mode 100644 index 000000000..4a4c04376 --- /dev/null +++ b/dfhack.init-example @@ -0,0 +1,34 @@ +############################## +# Generic dwarfmode bindings # +############################## + +keybinding add Ctrl-W twaterlvl + +# with cursor: +keybinding add Ctrl-V vdig +keybinding add Ctrl-Shift-V "vdig x" +keybinding add Ctrl-C spotclean +keybinding add Ctrl-Shift-K autodump-destroy-here + +# any item: +keybinding add Ctrl-K autodump-destroy-item + +############################# +# Context-specific bindings # +############################# + +# q->stockpile; p +keybinding add Alt-P copystock + +# q->workshop +keybinding add Ctrl-D job-duplicate + +# materials: q->workshop; b->select items +keybinding add Shift-A "job-material ALUNITE" +keybinding add Shift-M "job-material MICROCLINE" +keybinding add Shift-D "job-material DACITE" +keybinding add Shift-R "job-material RHYOLITE" +keybinding add Shift-I "job-material CINNABAR" +keybinding add Shift-B "job-material COBALTITE" +keybinding add Shift-O "job-material OBSIDIAN" +keybinding add Shift-G "job-material GLASS_GREEN" diff --git a/library/include/modules/Materials.h b/library/include/modules/Materials.h index 4ee315fbc..3b4d78203 100644 --- a/library/include/modules/Materials.h +++ b/library/include/modules/Materials.h @@ -74,6 +74,7 @@ namespace DFHack df::material *material; enum Mode { + None, Builtin, Inorganic, Creature, @@ -94,6 +95,15 @@ namespace DFHack bool isValid() const { return material != NULL; } + bool isNone() const { return mode == None; } + bool isBuiltin() const { return mode == Builtin; } + bool isInorganic() const { return mode == Inorganic; } + bool isCreature() const { return mode == Creature; } + bool isPlant() const { return mode == Plant; } + + bool isAnyInorganic() const { return type == 0; } + bool isInorganicWildcard() const { return isAnyInorganic() && isBuiltin(); } + bool decode(int16_t type, int32_t index = -1); bool decode(df::item *item); bool decode(const df::material_vec_ref &vr, int idx); @@ -110,6 +120,7 @@ namespace DFHack bool findPlant(const std::string &token, const std::string &subtoken); bool findCreature(const std::string &token, const std::string &subtoken); + std::string getToken(); std::string toString(uint16_t temp = 10015, bool named = true); bool isAnyCloth(); diff --git a/library/modules/Materials.cpp b/library/modules/Materials.cpp index 553c99db3..41cb1c18f 100644 --- a/library/modules/Materials.cpp +++ b/library/modules/Materials.cpp @@ -95,9 +95,14 @@ bool MaterialInfo::decode(int16_t type, int32_t index) inorganic = NULL; plant = NULL; creature = NULL; figure = NULL; + if (type < 0) { + mode = None; + return false; + } + df::world_raws &raws = world->raws; - if (type < 0 || type >= sizeof(raws.mat_table.builtin)/sizeof(void*)) + if (type >= sizeof(raws.mat_table.builtin)/sizeof(void*)) return false; if (index < 0) @@ -159,7 +164,7 @@ bool MaterialInfo::find(const std::string &token) std::vector items; split_string(&items, token, ":"); - if (items[0] == "INORGANIC") + if (items[0] == "INORGANIC" && items.size() > 1) return findInorganic(vector_get(items,1)); if (items[0] == "CREATURE_MAT" || items[0] == "CREATURE") return findCreature(vector_get(items,1), vector_get(items,2)); @@ -267,12 +272,35 @@ bool MaterialInfo::findCreature(const std::string &token, const std::string &sub return decode(-1); } -std::string MaterialInfo::toString(uint16_t temp, bool named) +std::string MaterialInfo::getToken() { - if (type == -1) + if (isNone()) return "NONE"; + + if (!material) + return stl_sprintf("INVALID:%d:%d", type, index); + + switch (mode) { + case Builtin: + return material->id; + case Inorganic: + return "INORGANIC:" + inorganic->id; + case Creature: + return "CREATURE:" + creature->creature_id + ":" + material->id; + case Plant: + return "PLANT:" + plant->id + ":" + material->id; + default: + return stl_sprintf("INVALID_MODE:%d:%d", type, index); + } +} + +std::string MaterialInfo::toString(uint16_t temp, bool named) +{ + if (isNone()) + return "any"; + if (!material) - return stl_sprintf("INVALID %d:%d", type, index); + return stl_sprintf("INVALID:%d:%d", type, index); df::matter_state state = matter_state::Solid; if (temp >= material->heat.melting_point) diff --git a/plugins/cleaners.cpp b/plugins/cleaners.cpp index f8a5ba59e..2ba87717f 100644 --- a/plugins/cleaners.cpp +++ b/plugins/cleaners.cpp @@ -122,6 +122,11 @@ DFhackCExport command_result spotclean (Core * c, vector & parameters) c->con.printerr("The cursor is not active.\n"); return CR_WRONG_USAGE; } + if (!Maps::IsValid()) + { + c->con.printerr("Map is not available.\n"); + return CR_FAILURE; + } df::map_block *block = Maps::getBlockAbs(cursor->x, cursor->y, cursor->z); if (block == NULL) { diff --git a/plugins/deramp.cpp b/plugins/deramp.cpp index 233397568..bc3e4b7ab 100644 --- a/plugins/deramp.cpp +++ b/plugins/deramp.cpp @@ -33,6 +33,12 @@ DFhackCExport command_result df_deramp (Core * c, vector & parameters) CoreSuspender suspend(c); + if (!Maps::IsValid()) + { + c->con.printerr("Map is not available!\n"); + return CR_FAILURE; + } + int count = 0; int countbad = 0; diff --git a/plugins/liquids.cpp b/plugins/liquids.cpp index bc930ff30..64a1c3251 100644 --- a/plugins/liquids.cpp +++ b/plugins/liquids.cpp @@ -180,6 +180,13 @@ DFhackCExport command_result df_liquids (Core * c, vector & parameters) return CR_OK; } } + + if (!Maps::IsValid()) + { + c->con.printerr("Map is not available!\n"); + return CR_FAILURE; + } + Brush * brush = new RectangleBrush(1,1); string brushname = "point"; bool end = false; diff --git a/plugins/showmood.cpp b/plugins/showmood.cpp index a82b5b900..2cecf0f90 100644 --- a/plugins/showmood.cpp +++ b/plugins/showmood.cpp @@ -6,6 +6,7 @@ #include "PluginManager.h" #include "modules/Materials.h" #include "modules/Translation.h" +#include "modules/Items.h" #include "DataDefs.h" #include "df/world.h" @@ -157,8 +158,6 @@ DFhackCExport command_result df_showmood (Core * c, vector & parameters MaterialInfo matinfo(item->mat_type, item->mat_index); string mat_name = matinfo.toString(); - if (mat_name == "NONE") - mat_name = "any"; switch (item->item_type) { @@ -172,9 +171,9 @@ DFhackCExport command_result df_showmood (Core * c, vector & parameters c->con.print("%s logs", mat_name.c_str()); break; case item_type::BAR: - if (mat_name == "rock") + if (matinfo.isInorganicWildcard()) mat_name = "metal"; - if ((matinfo.mode == MaterialInfo::Inorganic) && (matinfo.inorganic->flags.is_set(inorganic_flags::WAFERS))) + if (matinfo.inorganic && matinfo.inorganic->flags.is_set(inorganic_flags::WAFERS)) c->con.print("%s wafers", mat_name.c_str()); else c->con.print("%s bars", mat_name.c_str()); @@ -183,9 +182,9 @@ DFhackCExport command_result df_showmood (Core * c, vector & parameters c->con.print("%s cut gems", mat_name.c_str()); break; case item_type::ROUGH: - if (item->mat_type == 0) + if (matinfo.isAnyInorganic()) { - if (item->mat_index == -1) + if (matinfo.isInorganicWildcard()) mat_name = "any"; c->con.print("%s rough gems", mat_name.c_str()); } @@ -196,7 +195,7 @@ DFhackCExport command_result df_showmood (Core * c, vector & parameters c->con.print("%s leather", mat_name.c_str()); break; case item_type::CLOTH: - if (mat_name == "any") + if (matinfo.isNone()) { if (item->flags2.bits.plant) mat_name = "any plant fiber"; @@ -221,20 +220,32 @@ DFhackCExport command_result df_showmood (Core * c, vector & parameters else if (item->flags2.bits.ivory_tooth) c->con.print("%s ivory/teeth", mat_name.c_str()); else - c->con.print("%s unknown body parts (%08x:%08x:%08x)", mat_name.c_str(), item->flags1.whole, item->flags2.whole, item->flags3.whole); + c->con.print("%s unknown body parts (%s:%s:%s)", + mat_name.c_str(), + bitfieldToString(item->flags1).c_str(), + bitfieldToString(item->flags2).c_str(), + bitfieldToString(item->flags3).c_str()); } else - c->con.print("indeterminate %s item (%08x:%08x:%08x)", mat_name.c_str(), item->flags1.whole, item->flags2.whole, item->flags3.whole); + c->con.print("indeterminate %s item (%s:%s:%s)", + mat_name.c_str(), + bitfieldToString(item->flags1).c_str(), + bitfieldToString(item->flags2).c_str(), + bitfieldToString(item->flags3).c_str()); break; default: - c->con.print("item %s:%s with flags %08x,%08x,%08x", - df::enums::item_type::get_key(item->item_type), - (item->item_subtype == -1) ? "NONE" : "???", - item->flags1.whole, - item->flags2.whole, - item->flags3.whole); - break; + { + ItemTypeInfo itinfo(item->item_type, item->item_subtype); + + c->con.print("item %s material %s flags (%s:%s:%s)", + itinfo.toString().c_str(), mat_name.c_str(), + bitfieldToString(item->flags1).c_str(), + bitfieldToString(item->flags2).c_str(), + bitfieldToString(item->flags3).c_str()); + break; + } } + c->con.print(", quantity %i\n", item->quantity); } }