Skip to content

Commit 7b745f6

Browse files
committed
add list command and tighten up code
1 parent dbce375 commit 7b745f6

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

changelog.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Template for new versions:
2929
## New Tools
3030
- `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.
3131
- `immortal-cravings`: allow immortals to satisfy their cravings for food and drink
32-
- `justice`: various functions pertaining to the justice system, currently with a command to pardon a unit's prison sentence.
32+
- `justice`: various functions pertaining to the justice system, currently with a command to pardon a unit's prison sentence
3333

3434
## New Features
3535
- `force`: support the ``Wildlife`` event to allow additional wildlife to enter the map

docs/justice.rst

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ justice
22
=======
33

44
.. dfhack-tool::
5-
:summary: Commands related to the justice system.
5+
:summary: Mess with the justice system.
66
:tags: fort armok units
77

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

1414
::
15+
justice [list]
1516
justice pardon [--unit <id>]
1617

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

22+
Examples
23+
--------
24+
25+
``justice``
26+
List the convicts currently serving sentences.
27+
``justice pardon``
28+
Commutes the sentence of the currently selected convict.
2129

2230
Options
2331
-------
2432

2533
``-u``, ``--unit <id>``
26-
Specifies the unit id of the target of the command.
34+
Specifies a specific unit instead of using a selected unit.

justice.lua

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
21
local argparse = require('argparse')
32

3+
local TICKS_PER_SEASON_TICK = 10
4+
local TICKS_PER_DAY = 1200
5+
6+
local function list_convicts()
7+
local found = false
8+
for _,punishment in ipairs(df.global.plotinfo.punishments) do
9+
local unit = df.unit.find(punishment.criminal)
10+
if unit and punishment.prison_counter > 0 then
11+
found = true
12+
local days = math.ceil((punishment.prison_counter * TICKS_PER_SEASON_TICK) / TICKS_PER_DAY)
13+
print(('%s (id: %d): serving a sentence of %d day(s)'):format(
14+
dfhack.units.getReadableName(unit), unit.id, days))
15+
end
16+
end
17+
if not found then
18+
print('No criminals currently serving sentences.')
19+
end
20+
end
21+
422
local function pardon_unit(unit)
523
for _,punishment in ipairs(df.global.plotinfo.punishments) do
624
if punishment.criminal == unit.id then
@@ -14,29 +32,30 @@ end
1432
local function command_pardon(unit_id)
1533
local unit = nil
1634
if not unit_id then
17-
unit = dfhack.gui.getSelectedUnit()
18-
if not unit then qerror("No unit selected!") end
35+
unit = dfhack.gui.getSelectedUnit(true)
36+
if not unit then qerror('No unit selected!') end
1937
else
2038
unit = df.unit.find(unit_id)
21-
if not unit then qerror(("No unit with id %i"):format(unit_id)) end
39+
if not unit then qerror(('No unit with id %d'):format(unit_id)) end
2240
end
23-
if unit then pardon_unit(unit) end
41+
pardon_unit(unit)
2442
end
2543

2644
local unit_id = nil
2745

28-
local args = {...}
29-
30-
local positionals = argparse.processArgsGetopt(args,
31-
{'u', 'unit', hasArg=true, handler=function(optarg) unit_id = optarg end}
46+
local positionals = argparse.processArgsGetopt({...},
47+
{
48+
{'u', 'unit', hasArg=true,
49+
handler=function(optarg) unit_id = argparse.nonnegativeInt(optarg, 'unit') end},
50+
}
3251
)
3352

3453
local command = positionals[1]
3554

36-
if command == "pardon" then
55+
if command == 'pardon' then
3756
command_pardon(unit_id)
38-
elseif not command then
39-
qerror('Missing command')
57+
elseif not command or command == 'list' then
58+
list_convicts()
4059
else
41-
qerror(("Unrecognised command: %s"):format(command))
60+
qerror(('Unrecognised command: %s'):format(command))
4261
end

0 commit comments

Comments
 (0)