From 638affee060278668256ffda91042f1df935e828 Mon Sep 17 00:00:00 2001 From: expwnent Date: Sun, 9 Jun 2013 16:23:41 -0400 Subject: [PATCH] diggingInvaders: tweaked pick creation. Don't recreate if they already have one. --- plugins/diggingInvaders/assignJob.cpp | 124 ++++++++++++-------- plugins/diggingInvaders/diggingInvaders.cpp | 17 ++- 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/plugins/diggingInvaders/assignJob.cpp b/plugins/diggingInvaders/assignJob.cpp index 29520df37..415497de8 100644 --- a/plugins/diggingInvaders/assignJob.cpp +++ b/plugins/diggingInvaders/assignJob.cpp @@ -14,8 +14,10 @@ #include "df/item.h" #include "df/itemdef_weaponst.h" #include "df/item_quality.h" +#include "df/item_type.h" #include "df/item_weaponst.h" #include "df/job.h" +#include "df/job_skill.h" #include "df/job_type.h" #include "df/unit.h" #include "df/unit_inventory_item.h" @@ -182,62 +184,84 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_mappath.path.z.clear(); Job::linkIntoWorld(job); jobId = job->id; - + //TODO: test if he already has a pick + bool hasPick = false; + for ( size_t a = 0; a < firstInvader->inventory.size(); a++ ) { + df::unit_inventory_item* inv_item = firstInvader->inventory[a]; + if ( inv_item->mode != df::unit_inventory_item::Weapon || inv_item->body_part_id != firstInvader->body.weapon_bp ) + continue; + df::item* oldItem = inv_item->item; + if ( oldItem->getType() != df::enums::item_type::WEAPON ) + continue; + df::item_weaponst* oldWeapon = (df::item_weaponst*)oldItem; + df::itemdef_weaponst* oldType = oldWeapon->subtype; + if ( oldType->skill_melee != df::enums::job_skill::MINING ) + continue; + hasPick = true; + break; + } - //create and give a pick - df::item_weaponst* pick = new df::item_weaponst; - pick->pos = firstInvader->pos; - pick->flags.bits.forbid = 1; - pick->flags.bits.on_ground = 1; - pick->id = (*df::global::item_next_id)++; - pick->ignite_point = -1; - pick->heatdam_point = -1; - pick->colddam_point = -1; - pick->boiling_point = 11000; - pick->melting_point = 10500; - pick->fixed_temp = -1; - pick->weight = 0; - pick->weight_fraction = 0; - pick->stack_size = 1; - pick->temperature.whole = 10059; - pick->temperature.fraction = 0; - pick->mat_type = 0; - pick->mat_index = 5; - pick->maker_race = 0; //hehe - pick->quality = (df::enums::item_quality::item_quality)0; - pick->skill_used = (df::enums::job_skill::job_skill)0; - pick->maker = -1; - df::itemdef_weaponst* itemdef = NULL; - for ( size_t a = 0; a < df::global::world->raws.itemdefs.weapons.size(); a++ ) { - df::itemdef_weaponst* candidate = df::global::world->raws.itemdefs.weapons[a]; - if ( candidate->id == "ITEM_WEAPON_PICK" ) { + if ( !hasPick ) { + //based on createitem + //df::reaction_product_itemst *prod +#if 1 + //create and give a pick + df::item_weaponst* pick = new df::item_weaponst; + pick->pos = firstInvader->pos; + pick->flags.bits.forbid = 1; + pick->flags.bits.on_ground = 1; + pick->id = (*df::global::item_next_id)++; + pick->ignite_point = -1; + pick->heatdam_point = -1; + pick->colddam_point = -1; + pick->boiling_point = 11000; + pick->melting_point = 10500; + pick->fixed_temp = -1; + pick->weight = 0; + pick->weight_fraction = 0; + pick->stack_size = 1; + pick->temperature.whole = 10059; + pick->temperature.fraction = 0; + pick->mat_type = 0; + pick->mat_index = 5; + pick->maker_race = 0; //hehe + pick->quality = (df::enums::item_quality::item_quality)0; + pick->skill_used = (df::enums::job_skill::job_skill)0; + pick->maker = -1; + df::itemdef_weaponst* itemdef = NULL; + for ( size_t a = 0; a < df::global::world->raws.itemdefs.weapons.size(); a++ ) { + df::itemdef_weaponst* candidate = df::global::world->raws.itemdefs.weapons[a]; + if ( candidate->skill_melee != df::enums::job_skill::MINING ) + continue; + itemdef = candidate; - break; } - } - if ( itemdef == NULL ) { - out.print("%s, %d: null itemdef.\n", __FILE__, __LINE__); - return -1; - } - pick->subtype = itemdef; - pick->sharpness = 5000; - - int32_t part = -1; - part = firstInvader->body.weapon_bp; //weapon_bp - if ( part == -1 ) { - out.print("%s, %d: no grasp part.\n", __FILE__, __LINE__); - return -1; - } - //check for existing item there - for ( size_t a = 0; a < firstInvader->inventory.size(); a++ ) { - df::unit_inventory_item* inv_item = firstInvader->inventory[a]; - if ( false || inv_item->body_part_id == part ) { - //throw it on the GROUND - Items::moveToGround(cache, inv_item->item, firstInvader->pos); + if ( itemdef == NULL ) { + out.print("%s, %d: null itemdef.\n", __FILE__, __LINE__); + return -1; } + pick->subtype = itemdef; + pick->sharpness = 5000; + pick->categorize(true); + + int32_t part = -1; + part = firstInvader->body.weapon_bp; //weapon_bp + if ( part == -1 ) { + out.print("%s, %d: no grasp part.\n", __FILE__, __LINE__); + return -1; + } + //check for existing item there + for ( size_t a = 0; a < firstInvader->inventory.size(); a++ ) { + df::unit_inventory_item* inv_item = firstInvader->inventory[a]; + if ( false || inv_item->body_part_id == part ) { + //throw it on the GROUND + Items::moveToGround(cache, inv_item->item, firstInvader->pos); + } + } +#endif + Items::moveToInventory(cache, pick, firstInvader, df::unit_inventory_item::T_mode::Weapon, part); } - Items::moveToInventory(cache, pick, firstInvader, df::unit_inventory_item::T_mode::Weapon, part); } } diff --git a/plugins/diggingInvaders/diggingInvaders.cpp b/plugins/diggingInvaders/diggingInvaders.cpp index bf66fc9fd..20c30860b 100644 --- a/plugins/diggingInvaders/diggingInvaders.cpp +++ b/plugins/diggingInvaders/diggingInvaders.cpp @@ -59,8 +59,8 @@ #include #include #include -#include #include +#include using namespace std; @@ -78,20 +78,17 @@ DFHACK_PLUGIN("diggingInvaders"); static int32_t lastInvasionJob=-1; static int32_t lastInvasionDigger = -1; static EventManager::EventHandler jobCompleteHandler(watchForJobComplete, 5); -static Plugin* diggingInvadersPlugin; static bool enabled=false; static unordered_set diggingRaces; DFhackCExport command_result plugin_init (color_ostream &out, std::vector &commands) { - diggingInvadersPlugin = Core::getInstance().getPluginManager()->getPluginByName("diggingInvaders"); EventManager::EventHandler invasionHandler(initiateDigging, 1000); - EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, diggingInvadersPlugin); - // Fill the command list with your commands. + EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, plugin_self); + commands.push_back(PluginCommand( "diggingInvaders", "Makes invaders dig to your dwarves.", diggingInvadersFunc, 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: " diggingInvaders enable\n enables the plugin\n" " diggingInvaders disable\n disables the plugin\n" " diggingInvaders add GOBLIN\n registers the race GOBLIN as a digging invader\n" @@ -115,7 +112,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan //TODO: check game mode lastInvasionJob = -1; //in case there are invaders when the game is loaded, we should check - EventManager::registerTick(invasionHandler, 10, diggingInvadersPlugin); + EventManager::registerTick(invasionHandler, 10, plugin_self); break; case DFHack::SC_WORLD_UNLOADED: // cleanup @@ -168,7 +165,7 @@ void initiateDigging(color_ostream& out, void* ptr) { tick = 1000 - tick; EventManager::EventHandler handle(initiateDigging, 1000); - EventManager::registerTick(handle, tick, diggingInvadersPlugin); + EventManager::registerTick(handle, tick, plugin_self); } void watchForJobComplete(color_ostream& out, void* ptr) { @@ -178,7 +175,7 @@ void watchForJobComplete(color_ostream& out, void* ptr) { if ( job->id != lastInvasionJob ) return; - EventManager::unregister(EventManager::EventType::JOB_COMPLETED, jobCompleteHandler, diggingInvadersPlugin); + EventManager::unregister(EventManager::EventType::JOB_COMPLETED, jobCompleteHandler, plugin_self); */ manageInvasion(out); @@ -232,7 +229,7 @@ int32_t manageInvasion(color_ostream& out) { lastInvasionJob = df::global::world->units.all[index]->job.current_job->id; } - //EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, jobCompleteHandler, diggingInvadersPlugin); + //EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, jobCompleteHandler, plugin_self); //out.print("DiggingInvaders: job assigned.\n"); return 0; //did something }