Fix autogems typedefs for MSVC and log errors to stderr

develop
lethosor 2016-01-16 11:45:04 -05:00
parent 514ae890be
commit a7b2d5e38f
1 changed files with 36 additions and 36 deletions

@ -31,8 +31,8 @@ DFHACK_PLUGIN_IS_ENABLED(enabled);
REQUIRE_GLOBAL(ui);
REQUIRE_GLOBAL(world);
typedef decltype(df::item::id) item_id;
typedef decltype(df::job_item::mat_index) mat_index;
typedef int32_t item_id;
typedef int32_t mat_index;
typedef std::map<mat_index, int> gem_map;
bool running = false;
@ -54,40 +54,40 @@ const char *usage = (
void add_task(mat_index gem_type, df::building_workshopst *workshop) {
// Create a single task in the specified workshop.
// Partly copied from Buildings::linkForConstruct(); perhaps a refactor is in order.
auto ref = df::allocate<df::general_ref_building_holderst>();
if (!ref) {
//Core::printerr("Could not allocate general_ref_building_holderst\n");
std::cerr << "Could not allocate general_ref_building_holderst" << std::endl;
return;
}
ref->building_id = workshop->id;
auto item = new df::job_item();
if (!item) {
//Core::printerr("Could not allocate job_item\n");
std::cerr << "Could not allocate job_item" << std::endl;
return;
}
item->item_type = df::item_type::ROUGH;
item->mat_type = df::builtin_mats::INORGANIC;
item->mat_index = gem_type;
item->quantity = 1;
item->vector_id = df::job_item_vector_id::ROUGH;
auto job = new df::job();
if (!job) {
//Core::printerr("Could not allocate job\n");
std::cerr << "Could not allocate job" << std::endl;
return;
}
job->job_type = df::job_type::CutGems;
job->pos = df::coord(workshop->centerx, workshop->centery, workshop->z);
job->mat_type = df::builtin_mats::INORGANIC;
job->mat_index = gem_type;
job->general_refs.push_back(ref);
job->job_items.push_back(item);
workshop->jobs.push_back(job);
Job::linkIntoWorld(job);
}
@ -97,7 +97,7 @@ void add_tasks(gem_map &gem_types, df::building_workshopst *workshop) {
if (slots <= 0) {
return;
}
for (auto g = gem_types.begin(); g != gem_types.end() && slots > 0; ++g) {
while (g->second > 0 && slots > 0) {
add_task(g->first, workshop);
@ -114,26 +114,26 @@ void create_jobs() {
std::set<df::building_workshopst*> unlinked;
gem_map available;
auto workshops = &world->buildings.other[df::buildings_other_id::WORKSHOP_JEWELER];
for (auto w = workshops->begin(); w != workshops->end(); ++w) {
auto workshop = virtual_cast<df::building_workshopst>(*w);
auto links = workshop->links.take_from_pile;
if (workshop->construction_stage < 3) {
// Construction in progress.
continue;
}
if (workshop->jobs.size() == 1 && workshop->jobs[0]->job_type == df::job_type::DestroyBuilding) {
// Queued for destruction.
continue;
}
if (links.size() > 0) {
for (auto l = links.begin(); l != links.end() && workshop->jobs.size() <= MAX_WORKSHOP_JOBS; ++l) {
auto stockpile = virtual_cast<df::building_stockpilest>(*l);
gem_map piled;
Buildings::StockpileIterator stored;
for (stored.begin(stockpile); !stored.done(); ++stored) {
auto item = *stored;
@ -142,7 +142,7 @@ void create_jobs() {
piled[item->getMaterialIndex()] += 1;
}
}
// Decrement current jobs from all linked workshops, not just this one.
auto outbound = stockpile->links.give_to_workshop;
for (auto ws = outbound.begin(); ws != outbound.end(); ++ws) {
@ -158,7 +158,7 @@ void create_jobs() {
}
}
}
add_tasks(piled, workshop);
}
} else {
@ -169,13 +169,13 @@ void create_jobs() {
available[job->mat_index] -= job->flags.bits.repeat? 100: 1;
}
}
if (workshop->jobs.size() <= MAX_WORKSHOP_JOBS) {
unlinked.insert(workshop);
}
}
}
if (unlinked.size() > 0) {
// Count how many gems of each type are available to be cut.
// Gems in stockpiles linked to specific workshops don't count.
@ -187,7 +187,7 @@ void create_jobs() {
available[item->getMaterialIndex()] += 1;
}
}
for (auto w = unlinked.begin(); w != unlinked.end(); ++w) {
add_tasks(available, *w);
}
@ -199,7 +199,7 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
last_frame_count = world->frame_counter;
create_jobs();
}
return CR_OK;
}
@ -209,37 +209,37 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
*/
struct autogem_hook : public df::viewscreen_dwarfmodest {
typedef df::viewscreen_dwarfmodest interpose_base;
bool in_menu() {
// Determines whether we're looking at the Workshop Orders screen.
return ui->main.mode == ui_sidebar_mode::OrdersWorkshop;
}
bool handleInput(std::set<df::interface_key> *input) {
if (!in_menu()) {
return false;
}
if (input->count(interface_key::CUSTOM_G)) {
// Toggle whether gems are auto-cut for this fort.
auto config = World::GetPersistentData(CONFIG_KEY, NULL);
if (config.isValid()) {
config.ival(0) = running;
}
running = !running;
return true;
}
return false;
}
DEFINE_VMETHOD_INTERPOSE(void, feed, (std::set<df::interface_key> *input)) {
if (!handleInput(input)) {
INTERPOSE_NEXT(feed)(input);
}
}
DEFINE_VMETHOD_INTERPOSE(void, render, ()) {
INTERPOSE_NEXT(render)();
if (in_menu()) {
@ -247,11 +247,11 @@ struct autogem_hook : public df::viewscreen_dwarfmodest {
int x = dims.menu_x1 + 1;
int y = dims.y1 + 12;
Screen::Pen pen = Screen::readTile(x, y);
while (pen.valid() && pen.ch != ' ') {
pen = Screen::readTile(x, ++y);
}
if (pen.valid()) {
OutputHotkeyString(x, y, (running? "Auto Cut Gems": "No Auto Cut Gems"), "g", false, x, COLOR_WHITE, COLOR_LIGHTRED);
}
@ -274,7 +274,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
} else if (event == DFHack::SC_MAP_UNLOADED) {
running = false;
}
return CR_OK;
}
@ -284,11 +284,11 @@ DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
out.printerr("Could not %s autogem hooks!\n", enable? "insert": "remove");
return CR_FAILURE;
}
enabled = enable;
running = enabled && World::isFortressMode();
}
return CR_OK;
}