forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-pages.lua
More file actions
267 lines (255 loc) · 9.62 KB
/
codex-pages.lua
File metadata and controls
267 lines (255 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
-- Add pages to written content that have no pages.
local function isBook(item)
if item and
df.item_bookst:is_instance(item) or
df.item_toolst:is_instance(item) and
(item:getSubtype() == dfhack.items.findSubtype('TOOL:ITEM_TOOL_QUIRE') or
item:getSubtype() == dfhack.items.findSubtype('TOOL:ITEM_TOOL_SCROLL')) and
item:hasWriting()
then
return true
end
return false
end
local function GetBooks(target)
local books = {}
local item
if target.selected then
local item = dfhack.gui.getSelectedItem(true)
if item and isBook(item) then table.insert(books, item) end
elseif target.site then
local siteTools = df.global.world.items.other.TOOL
for _, item in ipairs(siteTools) do
if isBook(item) then table.insert(books, item) end
end
local siteBooks = df.global.world.items.other.BOOK
for _, item in ipairs(siteBooks) do
if isBook(item) then table.insert(books, item) end
end
end
return books
end
local function GetWrittenContent(book)
for _, improvement in ipairs(book.improvements) do
if df.itemimprovement_pagesst:is_instance(improvement) or
df.itemimprovement_writingst:is_instance(improvement)
then
for _, content in ipairs(improvement.contents) do
return df.written_content.find(content)
end
end
end
return nil
end
local function GetPageCount(targetWcType)
-- These values are based on polling page counts from various saves and may not be accurate.
local types = {
['NONE'] = {upperCount = 1, lowerCount = 1, mode = 1},
['Manual'] = {upperCount = 250, lowerCount = 20, mode = 80},
['Guide'] = {upperCount = 250, lowerCount = 20, mode = 100},
['Chronicle'] = {upperCount = 450, lowerCount = 100, mode = nil},
['ShortStory'] = {upperCount = 50, lowerCount = 10, mode = nil},
['Novel'] = {upperCount = 450, lowerCount = 100, mode = 200},
['Biography'] = {upperCount = 400, lowerCount = 100, mode = 250},
['Autobiography'] = {upperCount = 450, lowerCount = 100, mode = 250},
['Poem'] = {upperCount = 10, lowerCount = 1, mode = 1},
['Play'] = {upperCount = 50, lowerCount = 20, mode = 30},
['Letter'] = {upperCount = 10, lowerCount = 1, mode = nil},
['Essay'] = {upperCount = 50, lowerCount = 10, mode = nil},
['Dialog'] = {upperCount = 30, lowerCount = 5, mode = nil},
['MusicalComposition'] = {upperCount = 20, lowerCount = 1, mode = 1},
['Choreography'] = {upperCount = 1, lowerCount = 1, mode = 1},
['ComparativeBiography'] = {upperCount = 300, lowerCount = 150, mode = nil},
['BiographicalDictionary'] = {
upperCount = math.max(300, math.min(500, math.ceil(df.global.hist_figure_next_id / 1000))),
lowerCount = math.max(100, math.min(150, math.floor(df.global.hist_figure_next_id / 10000))),
mode = nil}, -- Very few samples were available, so this one is mostly arbitrary.
['Genealogy'] = {upperCount = 5, lowerCount = 1, mode = 4},
['Encyclopedia'] = {upperCount = 150, lowerCount = 50, mode = nil},
['CulturalHistory'] = {upperCount = 450, lowerCount = 100, mode = 200},
['CulturalComparison'] = {upperCount = 400, lowerCount = 100, mode = 200},
['AlternateHistory'] = {upperCount = 250, lowerCount = 100, mode = 150},
['TreatiseOnTechnologicalEvolution'] = {upperCount = 300, lowerCount = 100, mode = nil},
['Dictionary'] = {upperCount = 450, lowerCount = 100, mode = 250},
['StarChart'] = {upperCount = 1, lowerCount = 1, mode = 1},
['StarCatalogue'] = {upperCount = 150, lowerCount = 10, mode = 100},
['Atlas'] = {upperCount = 30, lowerCount = 10, mode = 25},
}
local upperCount, lowerCount = 1, 1
local mode
for wcType, tab in pairs(types) do
if df.written_content_type[wcType] == targetWcType then
upperCount = tab.upperCount
lowerCount = tab.lowerCount
mode = tab.mode
end
end
return upperCount, lowerCount, mode
end
local function GetPageCountModifier(targetStyle, targetStrength)
-- These values are arbitrary and may not even have any effect on page count in vanilla DF.
local styles = {
['NONE'] = 0,
['Meandering'] = 0.5,
['Cheerful'] = 0,
['Depressing'] = 0.1,
['Rigid'] = 0,
['Serious'] = 0,
['Disjointed'] = 0.2,
['Ornate'] = 0.2,
['Forceful'] = 0,
['Humorous'] = 0,
['Immature'] = 0.3,
['SelfIndulgent'] = 0.5,
['Touching'] = 0,
['Compassionate'] = 0,
['Vicious'] = 0,
['Concise'] = -0.2,
['Scornful'] = 0,
['Witty'] = 0,
['Ranting'] = 1,
}
local strength = {
['NONE'] = 1,
['Thorough'] = 1.5,
['Somewhat'] = 1,
['Hint'] = 0.5,
}
local pageCountModifier = 0
for style, modifier in pairs(styles) do
if df.written_content_style[style] == targetStyle then
pageCountModifier = modifier
break
end
end
for strength, addModifier in pairs(strength) do
if df.writing_style_modifier_type[strength] == targetStrength then
if pageCountModifier ~= 0 then
pageCountModifier = pageCountModifier * addModifier
break
end
end
end
return pageCountModifier
end
local rng = dfhack.random.new(nil, 10)
local seed = dfhack.world.ReadCurrentTick()
local function SetPageCount(upperCount, lowerCount, mode)
if upperCount > 1 then
local range = upperCount - lowerCount
local increment = 1 + math.floor(range ^ 2)
local weightedTable = {}
local weight = 0
for i = lowerCount, upperCount, 1 do
weight = weight + increment - math.floor(math.abs(i - mode) ^ 2)
if i == mode and mode == 1 then
-- Set heavy bias for very short written forms with mostly 1 page long works.
weight = weight + increment ^ 2
end
table.insert(weightedTable, weight)
end
local limit = weight
rng:init(seed, 10)
local result = rng:random(limit)
for i, weight in ipairs(weightedTable) do
if result <= weight then
return i + lowerCount - 1
end
end
end
return 1
end
local function AddPages(wc)
local pages = 0
if wc.page_start == -1 and wc.page_end == -1 then
local wcType = wc.type
local upperCount, lowerCount, mode = GetPageCount(wcType)
if upperCount and lowerCount then
local modifier = 1
for i, style in ipairs(wc.styles) do
if wc.style_strength[i] then
modifier = modifier + GetPageCountModifier(style, wc.style_strength[i])
end
end
upperCount = math.max(1, math.ceil(upperCount * modifier))
lowerCount = math.max(1, math.floor(lowerCount * modifier))
if mode and mode ~= 1 then
mode = math.max(1, math.floor(mode * modifier))
end
else
upperCount, lowerCount = 1, 1
end
mode = mode or math.ceil((lowerCount + upperCount) / 2)
wc.page_start = 1
wc.page_end = SetPageCount(upperCount, lowerCount, mode)
pages = wc.page_end
end
return pages
end
local function FixPageCount(target)
local writtenContents = {}
if not target.all then
local books = GetBooks(target)
if #books == 0 then
if target.selected then
print('No book with written content selected.')
elseif target.site then
print('No books available in site.')
end
return
end
for _, book in ipairs(books) do
table.insert(writtenContents, GetWrittenContent(book))
end
else
writtenContents = df.global.world.written_contents.all
end
local booksModified = 0
local pagesAdded = 0
for _, wc in ipairs(writtenContents) do
local pages = 0
pages = AddPages(wc)
if pages > 0 then
local title
if wc.title == '' then
title = 'an untitled work'
else
title = ('"%s"'):format(wc.title)
end
print(('%d pages added to %s.'):format(pages, title))
pagesAdded = pagesAdded + pages
seed = seed + pages
booksModified = booksModified + 1
end
end
if booksModified > 0 then
local plural = ''
if booksModified > 1 then plural = 's' end
print(('\nA total of %d pages were added to %d book%s.'):format(pagesAdded, booksModified, plural))
elseif target.selected then
print('Selected book already has pages in it.')
else
print('No written content with unspecified page counts were found; no pages were added to any books.')
end
end
local function Main(args)
local target = {
selected = false,
site = false,
all = false,
}
if #args > 0 then
if args[1] == 'help' then
print(dfhack.script_help())
return
end
if args[1] == 'this' then target.selected = true end
if args[1] == 'site' then target.site = true end
if args[1] == 'all' then target.all = true end
FixPageCount(target)
end
end
if not dfhack.isSiteLoaded() and not dfhack.world.isFortressMode() then
qerror('This script requires the game to be in fortress mode.')
end
Main({...})