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/item.h"
#include "df/itemdef_weaponst.h" #include "df/itemdef_weaponst.h"
#include "df/item_quality.h" #include "df/item_quality.h"
#include "df/item_type.h"
#include "df/item_weaponst.h" #include "df/item_weaponst.h"
#include "df/job.h" #include "df/job.h"
#include "df/job_skill.h"
#include "df/job_type.h" #include "df/job_type.h"
#include "df/unit.h" #include "df/unit.h"
#include "df/unit_inventory_item.h" #include "df/unit_inventory_item.h"
@ -182,62 +184,84 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
firstInvader->path.path.z.clear(); firstInvader->path.path.z.clear();
Job::linkIntoWorld(job); Job::linkIntoWorld(job);
jobId = job->id; jobId = job->id;
//TODO: test if he already has a pick //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 if ( !hasPick ) {
df::item_weaponst* pick = new df::item_weaponst; //based on createitem
pick->pos = firstInvader->pos; //df::reaction_product_itemst *prod
pick->flags.bits.forbid = 1; #if 1
pick->flags.bits.on_ground = 1; //create and give a pick
pick->id = (*df::global::item_next_id)++; df::item_weaponst* pick = new df::item_weaponst;
pick->ignite_point = -1; pick->pos = firstInvader->pos;
pick->heatdam_point = -1; pick->flags.bits.forbid = 1;
pick->colddam_point = -1; pick->flags.bits.on_ground = 1;
pick->boiling_point = 11000; pick->id = (*df::global::item_next_id)++;
pick->melting_point = 10500; pick->ignite_point = -1;
pick->fixed_temp = -1; pick->heatdam_point = -1;
pick->weight = 0; pick->colddam_point = -1;
pick->weight_fraction = 0; pick->boiling_point = 11000;
pick->stack_size = 1; pick->melting_point = 10500;
pick->temperature.whole = 10059; pick->fixed_temp = -1;
pick->temperature.fraction = 0; pick->weight = 0;
pick->mat_type = 0; pick->weight_fraction = 0;
pick->mat_index = 5; pick->stack_size = 1;
pick->maker_race = 0; //hehe pick->temperature.whole = 10059;
pick->quality = (df::enums::item_quality::item_quality)0; pick->temperature.fraction = 0;
pick->skill_used = (df::enums::job_skill::job_skill)0; pick->mat_type = 0;
pick->maker = -1; pick->mat_index = 5;
df::itemdef_weaponst* itemdef = NULL; pick->maker_race = 0; //hehe
for ( size_t a = 0; a < df::global::world->raws.itemdefs.weapons.size(); a++ ) { pick->quality = (df::enums::item_quality::item_quality)0;
df::itemdef_weaponst* candidate = df::global::world->raws.itemdefs.weapons[a]; pick->skill_used = (df::enums::job_skill::job_skill)0;
if ( candidate->id == "ITEM_WEAPON_PICK" ) { 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; itemdef = candidate;
break;
} }
} if ( itemdef == NULL ) {
if ( itemdef == NULL ) { out.print("%s, %d: null itemdef.\n", __FILE__, __LINE__);
out.print("%s, %d: null itemdef.\n", __FILE__, __LINE__); return -1;
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);
} }
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);
} }
} }

@ -59,8 +59,8 @@
#include <map> #include <map>
#include <set> #include <set>
#include <vector> #include <vector>
#include <unordered_set>
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
using namespace std; using namespace std;
@ -78,20 +78,17 @@ DFHACK_PLUGIN("diggingInvaders");
static int32_t lastInvasionJob=-1; static int32_t lastInvasionJob=-1;
static int32_t lastInvasionDigger = -1; static int32_t lastInvasionDigger = -1;
static EventManager::EventHandler jobCompleteHandler(watchForJobComplete, 5); static EventManager::EventHandler jobCompleteHandler(watchForJobComplete, 5);
static Plugin* diggingInvadersPlugin;
static bool enabled=false; static bool enabled=false;
static unordered_set<int32_t> diggingRaces; static unordered_set<int32_t> diggingRaces;
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands) DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{ {
diggingInvadersPlugin = Core::getInstance().getPluginManager()->getPluginByName("diggingInvaders");
EventManager::EventHandler invasionHandler(initiateDigging, 1000); EventManager::EventHandler invasionHandler(initiateDigging, 1000);
EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, diggingInvadersPlugin); EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, plugin_self);
// Fill the command list with your commands.
commands.push_back(PluginCommand( commands.push_back(PluginCommand(
"diggingInvaders", "Makes invaders dig to your dwarves.", "diggingInvaders", "Makes invaders dig to your dwarves.",
diggingInvadersFunc, false, /* true means that the command can't be used from non-interactive user interface */ 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 enable\n enables the plugin\n"
" diggingInvaders disable\n disables the plugin\n" " diggingInvaders disable\n disables the plugin\n"
" diggingInvaders add GOBLIN\n registers the race GOBLIN as a digging invader\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 //TODO: check game mode
lastInvasionJob = -1; lastInvasionJob = -1;
//in case there are invaders when the game is loaded, we should check //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; break;
case DFHack::SC_WORLD_UNLOADED: case DFHack::SC_WORLD_UNLOADED:
// cleanup // cleanup
@ -168,7 +165,7 @@ void initiateDigging(color_ostream& out, void* ptr) {
tick = 1000 - tick; tick = 1000 - tick;
EventManager::EventHandler handle(initiateDigging, 1000); EventManager::EventHandler handle(initiateDigging, 1000);
EventManager::registerTick(handle, tick, diggingInvadersPlugin); EventManager::registerTick(handle, tick, plugin_self);
} }
void watchForJobComplete(color_ostream& out, void* ptr) { void watchForJobComplete(color_ostream& out, void* ptr) {
@ -178,7 +175,7 @@ void watchForJobComplete(color_ostream& out, void* ptr) {
if ( job->id != lastInvasionJob ) if ( job->id != lastInvasionJob )
return; return;
EventManager::unregister(EventManager::EventType::JOB_COMPLETED, jobCompleteHandler, diggingInvadersPlugin); EventManager::unregister(EventManager::EventType::JOB_COMPLETED, jobCompleteHandler, plugin_self);
*/ */
manageInvasion(out); manageInvasion(out);
@ -232,7 +229,7 @@ int32_t manageInvasion(color_ostream& out) {
lastInvasionJob = df::global::world->units.all[index]->job.current_job->id; 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"); //out.print("DiggingInvaders: job assigned.\n");
return 0; //did something return 0; //did something
} }