Skip to content

Commit

Permalink
Revises channel-safely
Browse files Browse the repository at this point in the history
- fixes build errors
- begins implementing solution to job cancellation
  • Loading branch information
cppcooper committed Sep 13, 2024
1 parent 94d734d commit c9a5f19
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ if(BUILD_SUPPORTED)
dfhack_plugin(changeitem changeitem.cpp)
dfhack_plugin(changelayer changelayer.cpp)
dfhack_plugin(changevein changevein.cpp)
#add_subdirectory(channel-safely)
add_subdirectory(channel-safely)
dfhack_plugin(cleanconst cleanconst.cpp)
dfhack_plugin(cleaners cleaners.cpp)
dfhack_plugin(cleanowned cleanowned.cpp)
Expand Down
1 change: 1 addition & 0 deletions plugins/channel-safely/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ project(channel-safely)

include_directories(include)
SET(SOURCES
dwarves-onbreak.cpp
channel-groups.cpp
channel-manager.cpp
channel-safely-plugin.cpp)
Expand Down
6 changes: 2 additions & 4 deletions plugins/channel-safely/channel-groups.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <channel-groups.h>
#include <tile-cache.h>
#include <inlines.h>
#include <modules/Maps.h>
#include <tile-cache.h>
#include <df/block_square_event.h>
#include <df/block_square_event_designation_priorityst.h>

#include <random>

// iterates the DF job list and adds channel jobs to the `jobs` container
void ChannelJobs::load_channel_jobs() {
locations.clear();
Expand Down
9 changes: 4 additions & 5 deletions plugins/channel-safely/channel-manager.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <channel-manager.h>
#include <tile-cache.h>
#include <inlines.h>

#include <modules/EventManager.h> //hash function for df::coord
#include <tile-cache.h>
#include <df/block_square_event.h>
#include <df/block_square_event_designation_priorityst.h>
#include <df/unit.h>

#define NUMARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
#define d_assert(condition, ...) \
Expand Down Expand Up @@ -182,7 +181,7 @@ bool ChannelManager::manage_one(const df::coord &map_pos, bool set_marker_mode,
}
if (marker_mode) {
if (jobs.count(map_pos)) {
cancel_job(map_pos);
cancel_job(jobs.find_job(map_pos));
}
} else if (!block->flags.bits.designated) {
block->flags.bits.designated = true;
Expand Down
35 changes: 18 additions & 17 deletions plugins/channel-safely/channel-safely-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,19 @@ This skeletal logic has not been kept up-to-date since ~v0.5
-> NoDesignation: manage tile below
*/

#include "plugin.h"
#include "inlines.h"
#include "channel-manager.h"
#include "tile-cache.h"

#include <Debug.h>
#include <PluginManager.h>

#include <modules/EventManager.h>
#include <modules/Units.h>
#include <plugin.h>
#include <inlines.h>
#include <tile-cache.h>

#include <PluginManager.h>
#include <df/announcement_type.h>
#include <df/block_square_event_designation_priorityst.h>
#include <df/report.h>
#include <df/tile_traffic.h>
#include <df/world.h>
#include <df/unit.h>
#include <modules/Units.h>
#include <memory>

#include <cinttypes>
#include <unordered_map>
#include <unordered_set>

// Debugging
namespace DFHack {
Expand All @@ -90,6 +84,8 @@ namespace EM = EventManager;
using namespace DFHack;
using namespace EM::EventType;

std::unique_ptr<EM::EventHandler> HandleEnablingMining;

int32_t mapx, mapy, mapz;
Configuration config;
PersistentDataItem psetting;
Expand Down Expand Up @@ -314,7 +310,7 @@ namespace CSP {
return;
}
switch (report->type) {
case announcement_type::CANCEL_JOB:
case announcement_type::announcement_type::CANCEL_JOB:
if (config.insta_dig) {
if (report->text.find("cancels Dig") != std::string::npos ||
report->text.find("path") != std::string::npos) {
Expand Down Expand Up @@ -444,8 +440,8 @@ namespace CSP {

// prevent algorithm from re-enabling designation
for (auto &be: Maps::getBlock(job->pos)->block_events) {
if (auto bsedp = virtual_cast<df::block_square_event_designation_priorityst>(
be)) {
if (auto bsedp =
virtual_cast<df::block_square_event_designation_priorityst>(be)) {
df::coord local(job->pos);
local.x = local.x % 16;
local.y = local.y % 16;
Expand Down Expand Up @@ -498,7 +494,12 @@ namespace CSP {

command_result channel_safely(color_ostream &out, std::vector<std::string> &parameters);

void reset_mining_labor(color_ostream&, void*) {

}

DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) {
HandleEnablingMining = std::make_unique<EM::EventHandler>(plugin_self, reset_mining_labor, 2);
commands.push_back(PluginCommand("channel-safely",
"Automatically manage channel designations.",
channel_safely,
Expand Down
28 changes: 11 additions & 17 deletions plugins/channel-safely/include/inlines.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#pragma once
#include "plugin.h"
#include "channel-manager.h"
#include <channel-groups.h>
#include <channel-manager.h>
#include <dwarves-onbreak.h>
#include <cinttypes>
#include <random>

#include <TileTypes.h>
#include <LuaTools.h>
#include <LuaWrapper.h>
#include <modules/Maps.h>
#include <df/coord.h>
#include <df/job.h>

#include <cinttypes>
#include <unordered_set>
#include <random>
#include <df/unit.h>
#include <modules/Maps.h>

#define Coord(id) (id).x][(id).y
#define COORD "%" PRIi16 ",%" PRIi16 ",%" PRIi16
Expand Down Expand Up @@ -195,9 +194,9 @@ inline void cancel_job(df::job* job) {
df::tile_designation &designation = job_block->designation[x][y];
auto type = job->job_type;
ChannelManager::Get().jobs.erase(pos);
Job::removeWorker(job);
Job::removePostings(job, true);
Job::removeJob(job);
auto unit = Job::getWorker(job);
unit->status.labors[0] = false;
DwarvesOnBreak::register_dwarf(unit);
job_block->flags.bits.designated = true;
job_block->occupancy[x][y].bits.dig_marked = true;
switch (type) {
Expand Down Expand Up @@ -226,11 +225,6 @@ inline void cancel_job(df::job* job) {
}
}

inline void cancel_job(const df::coord &map_pos) {
cancel_job(ChannelManager::Get().jobs.find_job(map_pos));
ChannelManager::Get().jobs.erase(map_pos);
}

// executes dig designations for the specified tile coordinates
inline bool dig_now(color_ostream &out, const df::coord &map_pos) {
static std::default_random_engine rng;
Expand Down

0 comments on commit c9a5f19

Please sign in to comment.