Skip to content

Commit

Permalink
add list command and tighten up code
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Nov 28, 2024
1 parent dbce375 commit 7b745f6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Template for new versions:
## New Tools
- `fix/wildlife`: prevent wildlife from getting stuck when trying to exit the map. This fix needs to be enabled manually in `gui/control-panel` on the Bug Fixes tab since not all players want this bug to be fixed.
- `immortal-cravings`: allow immortals to satisfy their cravings for food and drink
- `justice`: various functions pertaining to the justice system, currently with a command to pardon a unit's prison sentence.
- `justice`: various functions pertaining to the justice system, currently with a command to pardon a unit's prison sentence

## New Features
- `force`: support the ``Wildlife`` event to allow additional wildlife to enter the map
Expand Down
12 changes: 10 additions & 2 deletions docs/justice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ justice
=======

.. dfhack-tool::
:summary: Commands related to the justice system.
:summary: Mess with the justice system.
:tags: fort armok units

This tool allows control over aspects of the justice system, such as the
Expand All @@ -12,15 +12,23 @@ Usage
-----

::
justice [list]
justice pardon [--unit <id>]

Pardon the selected unit or the one specified by unit id (if provided).
Currently only applies to prison time and doesn't cancel beatings or
hammerings.

Examples
--------

``justice``
List the convicts currently serving sentences.
``justice pardon``
Commutes the sentence of the currently selected convict.

Options
-------

``-u``, ``--unit <id>``
Specifies the unit id of the target of the command.
Specifies a specific unit instead of using a selected unit.
45 changes: 32 additions & 13 deletions justice.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@

local argparse = require('argparse')

local TICKS_PER_SEASON_TICK = 10
local TICKS_PER_DAY = 1200

local function list_convicts()
local found = false
for _,punishment in ipairs(df.global.plotinfo.punishments) do
local unit = df.unit.find(punishment.criminal)
if unit and punishment.prison_counter > 0 then
found = true
local days = math.ceil((punishment.prison_counter * TICKS_PER_SEASON_TICK) / TICKS_PER_DAY)
print(('%s (id: %d): serving a sentence of %d day(s)'):format(
dfhack.units.getReadableName(unit), unit.id, days))
end
end
if not found then
print('No criminals currently serving sentences.')
end
end

local function pardon_unit(unit)
for _,punishment in ipairs(df.global.plotinfo.punishments) do
if punishment.criminal == unit.id then
Expand All @@ -14,29 +32,30 @@ end
local function command_pardon(unit_id)
local unit = nil
if not unit_id then
unit = dfhack.gui.getSelectedUnit()
if not unit then qerror("No unit selected!") end
unit = dfhack.gui.getSelectedUnit(true)
if not unit then qerror('No unit selected!') end
else
unit = df.unit.find(unit_id)
if not unit then qerror(("No unit with id %i"):format(unit_id)) end
if not unit then qerror(('No unit with id %d'):format(unit_id)) end
end
if unit then pardon_unit(unit) end
pardon_unit(unit)
end

local unit_id = nil

local args = {...}

local positionals = argparse.processArgsGetopt(args,
{'u', 'unit', hasArg=true, handler=function(optarg) unit_id = optarg end}
local positionals = argparse.processArgsGetopt({...},
{
{'u', 'unit', hasArg=true,
handler=function(optarg) unit_id = argparse.nonnegativeInt(optarg, 'unit') end},
}
)

local command = positionals[1]

if command == "pardon" then
if command == 'pardon' then
command_pardon(unit_id)
elseif not command then
qerror('Missing command')
elseif not command or command == 'list' then
list_convicts()
else
qerror(("Unrecognised command: %s"):format(command))
qerror(('Unrecognised command: %s'):format(command))
end

0 comments on commit 7b745f6

Please sign in to comment.