Skip to content

Commit eaac477

Browse files
committed
Improves a few inline functions, adds a remove_worker(df::job*) func.
1 parent 0d4ccda commit eaac477

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

plugins/channel-safely/include/inlines.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#include <LuaWrapper.h>
88
#include <modules/Maps.h>
99
#include <df/job.h>
10+
#include <df/unit.h>
1011

1112
#include <cinttypes>
1213
#include <unordered_set>
1314
#include <random>
15+
#include <df/general_ref.h>
1416

1517
#define Coord(id) (id).x][(id).y
1618
#define COORD "%" PRIi16 ",%" PRIi16 ",%" PRIi16
@@ -94,7 +96,10 @@ inline bool is_dig_job(const df::job* job) {
9496
}
9597

9698
inline bool is_channel_job(const df::job* job) {
97-
return job && (job->job_type == df::job_type::DigChannel);
99+
return job
100+
&& job->id >= 0
101+
&& (job->job_type == df::job_type::DigChannel)
102+
&& Maps::isValidTilePos(job->pos);
98103
}
99104

100105
inline bool is_group_job(const ChannelGroups &groups, const df::job* job) {
@@ -150,7 +155,8 @@ inline bool is_safe_to_dig_down(const df::coord &map_pos) {
150155
// todo: remove? this is probably not useful.. and seems like the only considerable difference to is_safe_fall (aside from where each stops looking)
151156
// the starting tile is open space, that's obviously not safe
152157
return false;
153-
} else if (!DFHack::isOpenTerrain(type)) {
158+
}
159+
if (!DFHack::isOpenTerrain(type)) {
154160
// a tile after the first one is not open space
155161
return true;
156162
}
@@ -185,21 +191,42 @@ inline bool has_any_groups_above(const ChannelGroups &groups, const Group &group
185191
return false;
186192
}
187193

194+
inline int remove_worker(df::job* job) {
195+
df::unit* worker = Job::getWorker(job);
196+
if unlikely(!worker) return 0;
197+
auto R = std::erase_if(job->general_refs, [](df::general_ref* ref) {
198+
return ref->getType() == general_ref_type::UNIT_WORKER;
199+
});
200+
if likely(R) {
201+
worker->job.current_job = nullptr;
202+
}
203+
return R;
204+
}
205+
188206
inline void cancel_job(df::job* job) {
189207
if (job) {
208+
if (job->id < 0) {
209+
// seems like the event manager maybe gave us a fubar event
210+
Job::removePostings(job, true);
211+
return;
212+
}
190213
const df::coord &pos = job->pos;
191214
df::map_block* job_block = Maps::getTileBlock(pos);
215+
if (!job_block) {
216+
INFO(jobs).print("we dun fukked it");
217+
return;
218+
}
192219
uint16_t x, y;
193220
x = pos.x % 16;
194221
y = pos.y % 16;
195-
df::tile_designation &designation = job_block->designation[x][y];
196222
auto type = job->job_type;
197223
ChannelManager::Get().jobs.erase(pos);
198224
Job::removeWorker(job);
199225
Job::removePostings(job, true);
200226
Job::removeJob(job);
201227
job_block->flags.bits.designated = true;
202228
job_block->occupancy[x][y].bits.dig_marked = true;
229+
df::tile_designation &designation = job_block->designation[x][y];
203230
switch (type) {
204231
case job_type::Dig:
205232
designation.bits.dig = df::tile_dig_designation::Default;
@@ -227,8 +254,12 @@ inline void cancel_job(df::job* job) {
227254
}
228255

229256
inline void cancel_job(const df::coord &map_pos) {
230-
cancel_job(ChannelManager::Get().jobs.find_job(map_pos));
231-
ChannelManager::Get().jobs.erase(map_pos);
257+
if (const auto job = ChannelManager::Get().jobs.find_job(map_pos)) {
258+
cancel_job(job);
259+
ChannelManager::Get().jobs.erase(map_pos);
260+
} else {
261+
INFO(jobs).print("Cannot cancel the job at (" COORD "). It no longer exists as a job, it likely finished before we could cancel.\n", COORDARGS(map_pos));
262+
}
232263
}
233264

234265
// executes dig designations for the specified tile coordinates

0 commit comments

Comments
 (0)