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