From 03650dbdbdc2a3fff47df77dfd8be17b1c5201d5 Mon Sep 17 00:00:00 2001 From: expwnent Date: Wed, 2 Jan 2013 17:32:11 -0500 Subject: [PATCH 1/6] True transformation. Temp commit. --- plugins/CMakeLists.txt | 1 + plugins/trueTransformation.cpp | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 plugins/trueTransformation.cpp diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 1c89d9ab7..077f0e4ad 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -132,6 +132,7 @@ if (BUILD_SUPPORTED) #DFHACK_PLUGIN(versionosd versionosd.cpp) DFHACK_PLUGIN(misery misery.cpp) #DFHACK_PLUGIN(dfstream dfstream.cpp LINK_LIBRARIES clsocket dfhack-tinythread) + DFHACK_PLUGIN(trueTransformation trueTransformation.cpp) endif() diff --git a/plugins/trueTransformation.cpp b/plugins/trueTransformation.cpp new file mode 100644 index 000000000..6cd6f16ea --- /dev/null +++ b/plugins/trueTransformation.cpp @@ -0,0 +1,85 @@ + +#include "Core.h" +#include "Console.h" +#include "DataDefs.h" +#include "Export.h" +#include "PluginManager.h" + +using namespace DFHack; + +DFHACK_PLUGIN("trueTransformation"); + +DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) +{ + commands.push_back(PluginCommand( + "skeleton", "Do nothing, look pretty.", + skeleton, false, /* true means that the command can't be used from non-interactive user interface */ + // Extended help string. Used by CR_WRONG_USAGE and the help command: + " This command does nothing at all.\n" + "Example:\n" + " skeleton\n" + " Does nothing.\n" + )); + return CR_OK; +} + +// This is called right before the plugin library is removed from memory. +DFhackCExport command_result plugin_shutdown ( color_ostream &out ) +{ + // You *MUST* kill all threads you created before this returns. + // If everything fails, just return CR_FAILURE. Your plugin will be + // in a zombie state, but things won't crash. + return CR_OK; +} + +// Called to notify the plugin about important state changes. +// Invoked with DF suspended, and always before the matching plugin_onupdate. +// More event codes may be added in the future. +/* +DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) +{ + switch (event) { + case SC_GAME_LOADED: + // initialize from the world just loaded + break; + case SC_GAME_UNLOADED: + // cleanup + break; + default: + break; + } + return CR_OK; +} +*/ + +// Whatever you put here will be done in each game step. Don't abuse it. +// It's optional, so you can just comment it out like this if you don't need it. +/* +DFhackCExport command_result plugin_onupdate ( color_ostream &out ) +{ + // whetever. You don't need to suspend DF execution here. + return CR_OK; +} +*/ + +// A command! It sits around and looks pretty. And it's nice and friendly. +command_result skeleton (color_ostream &out, std::vector & parameters) +{ + // It's nice to print a help message you get invalid options + // from the user instead of just acting strange. + // This can be achieved by adding the extended help string to the + // PluginCommand registration as show above, and then returning + // CR_WRONG_USAGE from the function. The same string will also + // be used by 'help your-command'. + if (!parameters.empty()) + return CR_WRONG_USAGE; + // Commands are called from threads other than the DF one. + // Suspend this thread until DF has time for us. If you + // use CoreSuspender, it'll automatically resume DF when + // execution leaves the current scope. + CoreSuspender suspend; + // Actually do something here. Yay. + out.print("Hello! I do nothing, remember?\n"); + // Give control back to DF. + return CR_OK; +} From d4afa4b6e45c3b8b76e0c1d551ce566b71607dd7 Mon Sep 17 00:00:00 2001 From: expwnent Date: Wed, 2 Jan 2013 19:21:25 -0500 Subject: [PATCH 2/6] True Transformation: turn into something temporarily, then permanently transform into another creature. If you try to do that with a syndrome, you can't do it recursively. --- plugins/trueTransformation.cpp | 133 +++++++++++++++++---------------- 1 file changed, 69 insertions(+), 64 deletions(-) diff --git a/plugins/trueTransformation.cpp b/plugins/trueTransformation.cpp index 6cd6f16ea..fd1e1a61b 100644 --- a/plugins/trueTransformation.cpp +++ b/plugins/trueTransformation.cpp @@ -5,81 +5,86 @@ #include "Export.h" #include "PluginManager.h" +#include "modules/EventManager.h" + +#include "df/caste_raw.h" +#include "df/creature_raw.h" +#include "df/syndrome.h" +#include "df/unit.h" +#include "df/unit_syndrome.h" +#include "df/world.h" + +#include + using namespace DFHack; +using namespace std; DFHACK_PLUGIN("trueTransformation"); +void syndromeHandler(color_ostream& out, void* ptr); + DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) { - commands.push_back(PluginCommand( - "skeleton", "Do nothing, look pretty.", - skeleton, false, /* true means that the command can't be used from non-interactive user interface */ - // Extended help string. Used by CR_WRONG_USAGE and the help command: - " This command does nothing at all.\n" - "Example:\n" - " skeleton\n" - " Does nothing.\n" - )); - return CR_OK; -} + EventManager::EventHandler syndrome(syndromeHandler); + Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("trueTransformation"); + EventManager::registerListener(EventManager::EventType::SYNDROME, syndrome, 1, me); -// This is called right before the plugin library is removed from memory. -DFhackCExport command_result plugin_shutdown ( color_ostream &out ) -{ - // You *MUST* kill all threads you created before this returns. - // If everything fails, just return CR_FAILURE. Your plugin will be - // in a zombie state, but things won't crash. return CR_OK; } -// Called to notify the plugin about important state changes. -// Invoked with DF suspended, and always before the matching plugin_onupdate. -// More event codes may be added in the future. -/* -DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) -{ - switch (event) { - case SC_GAME_LOADED: - // initialize from the world just loaded - break; - case SC_GAME_UNLOADED: - // cleanup - break; - default: - break; +void syndromeHandler(color_ostream& out, void* ptr) { + EventManager::SyndromeData* data = (EventManager::SyndromeData*)ptr; + //out.print("Syndrome started: unit %d, syndrome %d.\n", data->unitId, data->syndromeIndex); + + int32_t index = df::unit::binsearch_index(df::global::world->units.active, data->unitId); + if ( index < 0 ) { + out.print("%s, line %d: couldn't find unit.\n", __FILE__, __LINE__); + return; } - return CR_OK; -} -*/ + df::unit* unit = df::global::world->units.active[index]; + df::unit_syndrome* unit_syndrome = unit->syndromes.active[data->syndromeIndex]; + df::syndrome* syndrome = df::global::world->raws.syndromes.all[unit_syndrome->type]; + + bool foundIt = false; + int32_t raceId = -1; + df::creature_raw* creatureRaw = NULL; + int32_t casteId = -1; + for ( size_t a = 0; a < syndrome->syn_class.size(); a++ ) { + if ( *syndrome->syn_class[a] == "\\PERMANENT" ) { + foundIt = true; + } + if ( foundIt && raceId == -1 ) { + //find the race with the name + string& name = *syndrome->syn_class[a]; + for ( size_t b = 0; b < df::global::world->raws.creatures.all.size(); b++ ) { + df::creature_raw* creature = df::global::world->raws.creatures.all[b]; + if ( creature->creature_id != name ) + continue; + raceId = b; + creatureRaw = creature; + break; + } + continue; + } + if ( foundIt && raceId != -1 ) { + string& name = *syndrome->syn_class[a]; + for ( size_t b = 0; b < creatureRaw->caste.size(); b++ ) { + df::caste_raw* caste = creatureRaw->caste[b]; + if ( caste->caste_id != name ) + continue; + casteId = b; + break; + } + break; + } + } + out.print("foundIt = %d, raceId = %d, casteId = %d\n", (int32_t)foundIt, raceId, casteId); + if ( !foundIt || raceId == -1 || casteId == -1 ) + return; -// Whatever you put here will be done in each game step. Don't abuse it. -// It's optional, so you can just comment it out like this if you don't need it. -/* -DFhackCExport command_result plugin_onupdate ( color_ostream &out ) -{ - // whetever. You don't need to suspend DF execution here. - return CR_OK; + unit->enemy.normal_race = raceId; + unit->enemy.normal_caste = casteId; + out.print("Did the thing.\n"); + //that's it! } -*/ -// A command! It sits around and looks pretty. And it's nice and friendly. -command_result skeleton (color_ostream &out, std::vector & parameters) -{ - // It's nice to print a help message you get invalid options - // from the user instead of just acting strange. - // This can be achieved by adding the extended help string to the - // PluginCommand registration as show above, and then returning - // CR_WRONG_USAGE from the function. The same string will also - // be used by 'help your-command'. - if (!parameters.empty()) - return CR_WRONG_USAGE; - // Commands are called from threads other than the DF one. - // Suspend this thread until DF has time for us. If you - // use CoreSuspender, it'll automatically resume DF when - // execution leaves the current scope. - CoreSuspender suspend; - // Actually do something here. Yay. - out.print("Hello! I do nothing, remember?\n"); - // Give control back to DF. - return CR_OK; -} From a8b0a7e6958be517ea487202ca0e81b2edf9cf0e Mon Sep 17 00:00:00 2001 From: expwnent Date: Wed, 2 Jan 2013 19:25:24 -0500 Subject: [PATCH 3/6] EventManager: got rid of silly print statement. --- library/modules/EventManager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/library/modules/EventManager.cpp b/library/modules/EventManager.cpp index b3f22b109..8611417b0 100644 --- a/library/modules/EventManager.cpp +++ b/library/modules/EventManager.cpp @@ -480,7 +480,6 @@ static void manageSyndromeEvent(color_ostream& out) { for ( size_t b = 0; b < unit->syndromes.active.size(); b++ ) { df::unit_syndrome* syndrome = unit->syndromes.active[b]; uint32_t startTime = syndrome->year*ticksPerYear + syndrome->year_time; - out.print("start time = %d, time = %d\n", startTime, eventLastTick[EventType::SYNDROME]); if ( startTime <= eventLastTick[EventType::SYNDROME] ) continue; From 285ee3b399f6505b67ce305f952cf52e6f2b46fd Mon Sep 17 00:00:00 2001 From: expwnent Date: Wed, 2 Jan 2013 19:45:08 -0500 Subject: [PATCH 4/6] True transformation: got rid of debug print statements. --- plugins/trueTransformation.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/trueTransformation.cpp b/plugins/trueTransformation.cpp index fd1e1a61b..6da6b7e74 100644 --- a/plugins/trueTransformation.cpp +++ b/plugins/trueTransformation.cpp @@ -78,13 +78,11 @@ void syndromeHandler(color_ostream& out, void* ptr) { break; } } - out.print("foundIt = %d, raceId = %d, casteId = %d\n", (int32_t)foundIt, raceId, casteId); if ( !foundIt || raceId == -1 || casteId == -1 ) return; unit->enemy.normal_race = raceId; unit->enemy.normal_caste = casteId; - out.print("Did the thing.\n"); //that's it! } From 123f34bf85f6eb3b9331f563b3d4abd4438047f9 Mon Sep 17 00:00:00 2001 From: expwnent Date: Wed, 2 Jan 2013 21:06:48 -0500 Subject: [PATCH 5/6] Ignore swap files. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 9f2b009c6..b4a578ec0 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,6 @@ dfhack/python/dist build/CPack*Config.cmake /cmakeall.bat + +# vim swap files +*.swp From e1ad933068994c3441fa3e7bafed3dcbbfba1666 Mon Sep 17 00:00:00 2001 From: expwnent Date: Sat, 5 Jan 2013 12:58:06 -0500 Subject: [PATCH 6/6] True transformation: make it compile. --- plugins/trueTransformation.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/trueTransformation.cpp b/plugins/trueTransformation.cpp index 6da6b7e74..6c4245da8 100644 --- a/plugins/trueTransformation.cpp +++ b/plugins/trueTransformation.cpp @@ -25,9 +25,9 @@ void syndromeHandler(color_ostream& out, void* ptr); DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) { - EventManager::EventHandler syndrome(syndromeHandler); + EventManager::EventHandler syndrome(syndromeHandler, 1); Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("trueTransformation"); - EventManager::registerListener(EventManager::EventType::SYNDROME, syndrome, 1, me); + EventManager::registerListener(EventManager::EventType::SYNDROME, syndrome, me); return CR_OK; }