Skip to content

Add empty chronicle notice and item creation events #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions chronicle.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Chronicles fortress events: unit deaths, artifact creation, and invasions
-- Chronicles fortress events: unit deaths, item creation, and invasions
--@module = true
--@enable = true

Expand Down Expand Up @@ -43,18 +43,22 @@ end

local function on_item_created(item_id)
local item = df.item.find(item_id)
if not item or not item.flags.artifact then return end
if not item then return end

local gref = dfhack.items.getGeneralRef(item, df.general_ref_type.IS_ARTIFACT)
local rec = gref and df.artifact_record.find(gref.artifact_id) or nil
if not rec then return end
local date = format_date(df.global.cur_year, df.global.cur_year_tick)

local name = dfhack.translation.translateName(rec.name)
local date = format_date(rec.year, rec.season_tick or 0)
if rec.id > state.last_artifact_id then
state.last_artifact_id = rec.id
if item.flags.artifact then
local gref = dfhack.items.getGeneralRef(item, df.general_ref_type.IS_ARTIFACT)
local rec = gref and df.artifact_record.find(gref.artifact_id) or nil
local name = rec and dfhack.translation.translateName(rec.name) or 'unknown artifact'
if rec and rec.id > state.last_artifact_id then
state.last_artifact_id = rec.id
end
add_entry(string.format('Artifact "%s" created on %s', name, date))
else
local desc = dfhack.items.getDescription(item, 0, true)
add_entry(string.format('Item "%s" created on %s', desc, date))
end
add_entry(string.format('Artifact "%s" created on %s', name, date))
end

local function on_invasion(invasion_id)
Expand Down Expand Up @@ -134,8 +138,12 @@ elseif cmd == 'clear' then
state.entries = {}
persist_state()
elseif cmd == 'print' then
for _, entry in ipairs(state.entries) do
print(entry)
if #state.entries == 0 then
print('Chronicle is empty.')
else
for _, entry in ipairs(state.entries) do
print(entry)
end
end
else
print(dfhack.script_help())
Expand Down
7 changes: 4 additions & 3 deletions docs/chronicle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ chronicle
=========

.. dfhack-tool::
:summary: Record fortress events like deaths, artifacts, and invasions.
:summary: Record fortress events like deaths, item creation, and invasions.
:tags: fort gameplay

This tool automatically records notable events in a chronicle that is stored
with your save. Unit deaths, artifact creation, and invasions are recorded.
with your save. Unit deaths, all item creation events, and invasions are
recorded.

Usage
-----
Expand All @@ -23,6 +24,6 @@ Usage
``chronicle disable``
Stop recording events.
``chronicle print``
Print all recorded events.
Print all recorded events. Prints a notice if the chronicle is empty.
``chronicle clear``
Delete the chronicle.