diggingInvaders: tweaked pick creation. Don't recreate if they already have one.

develop
expwnent 2013-06-09 16:23:41 -04:00
parent 08114cf574
commit 638affee06
2 changed files with 81 additions and 60 deletions

@ -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"
@ -184,7 +186,26 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
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;
}
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;
@ -211,10 +232,10 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
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 ( candidate->skill_melee != df::enums::job_skill::MINING )
continue;
itemdef = candidate;
break;
}
}
if ( itemdef == NULL ) {
out.print("%s, %d: null itemdef.\n", __FILE__, __LINE__);
@ -222,6 +243,7 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
}
pick->subtype = itemdef;
pick->sharpness = 5000;
pick->categorize(true);
int32_t part = -1;
part = firstInvader->body.weapon_bp; //weapon_bp
@ -237,9 +259,11 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
Items::moveToGround(cache, inv_item->item, firstInvader->pos);
}
}
#endif
Items::moveToInventory(cache, pick, firstInvader, df::unit_inventory_item::T_mode::Weapon, part);
}
}
}
#if 0
//tell EVERYONE to move there

@ -59,8 +59,8 @@
#include <map>
#include <set>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <unordered_set>
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<int32_t> diggingRaces;
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &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
}