diff --git a/changelog.txt b/changelog.txt index 2a6bab7fc5..ac7490dc00 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,7 @@ Template for new versions: ## New Tools - `add-recipe`: (reinstated) add reactions to your civ (e.g. for high boots if your civ didn't start with the ability to make high boots) +- `fix/corrupt-jobs`: prevents crashes by automatically removing corrupted jobs - `burial`: (reinstated) create tomb zones for unzoned coffins ## New Features diff --git a/docs/fix/corrupt-jobs.rst b/docs/fix/corrupt-jobs.rst new file mode 100644 index 0000000000..1894a59857 --- /dev/null +++ b/docs/fix/corrupt-jobs.rst @@ -0,0 +1,15 @@ +fix/corrupt-jobs +================ + +.. dfhack-tool:: + :summary: Removes jobs with an id of -1 from units. + :tags: fort bugfix + +This fix cleans up corrupt jobs so they don't cause crashes. It runs automatically on fort load, so you don't have to run it manually. + +Usage +----- + +:: + + fix/corrupt-jobs diff --git a/fix/corrupt-jobs.lua b/fix/corrupt-jobs.lua new file mode 100644 index 0000000000..b3c5965ccc --- /dev/null +++ b/fix/corrupt-jobs.lua @@ -0,0 +1,29 @@ +-- Deletes corrupted jobs from affected units +--@module = true + +local GLOBAL_KEY = 'corrupt-jobs' + +function remove_bad_jobs() + local count = 0 + + for _, unit in ipairs(df.global.world.units.all) do + if unit.job.current_job and unit.job.current_job.id == -1 then + unit.job.current_job = nil + count = count + 1 + end + end + + if count > 0 then + print(('removed %d corrupted job(s) from affected units'):format(count)) + end +end + +dfhack.onStateChange[GLOBAL_KEY] = function(sc) + if sc == SC_MAP_LOADED then + remove_bad_jobs() + end +end + +if dfhack_flags.module then + return +end