Skip to content

Commit a7e289c

Browse files
committed
init research info
1 parent e50474e commit a7e289c

4 files changed

Lines changed: 239 additions & 0 deletions

File tree

‎gui/spectate.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ function Spectate:init()
244244

245245
create_row({t=27}, 'Job', 'J', 'job', colFollow, colHover),
246246
create_row({t=28}, 'Activity', 'A', 'activity', colFollow, colHover),
247+
create_row({t=27}, 'Research', 'R', 'research', colFollow, colHover),
247248
create_row({t=29}, 'Name', 'N', 'name', colFollow, colHover),
248249
create_row({t=30}, 'Stress', 'S', 'stress', colFollow, colHover),
249250
create_stress_list({t=31}, colFollow, colHover),

‎internal/research-info/info.lua‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
-- Main module for research-info
2+
--@ module = true
3+
4+
---@param unit_id df.unit.id
5+
function getHistoricalFigure(unit_id)
6+
for _, historical_figure in ipairs(df.global.world.history.figures) do
7+
if historical_figure.unit_id == unit_id then
8+
return historical_figure
9+
end
10+
end
11+
print("No historical figure found for unit ID: " .. unit_id)
12+
return nil
13+
end
14+
15+
function getKnowledgeGoalCategoryInfo(category_id)
16+
-- alt: df.scholar_knowledgest
17+
local topic_categories = {
18+
[-1] = "None", -- None
19+
[0] = "Philosophy", -- PHILOSOPHY_FLAG
20+
[1] = "Philosophy Adv", -- PHILOSOPHY_FLAG2
21+
[2] = "Mathematics", -- MATHEMATICS_FLAG
22+
[3] = "Mathematics Adv", -- MATHEMATICS_FLAG2
23+
[4] = "History", -- HISTORY_FLAG
24+
[5] = "Astronomy", -- ASTRONOMY_FLAG
25+
[6] = "Naturalist", -- NATURALIST_FLAG
26+
[7] = "Chemistry", -- CHEMISTRY_FLAG
27+
[8] = "Geography", -- GEOGRAPHY_FLAG
28+
[9] = "Medicine 1", -- MEDICINE_FLAG
29+
[10] = "Medicine 2", -- MEDICINE_FLAG2
30+
[11] = "Medicine 3", -- MEDICINE_FLAG3
31+
[12] = "Engineering", -- ENGINEERING_FLAG
32+
[13] = "Engineering Adv" -- ENGINEERING_FLAG2
33+
}
34+
return topic_categories[category_id] or string.format("Unknown Category: %s", category_id)
35+
end
36+
37+
function getGoalInfo(goal, category)
38+
local goal_info = ''
39+
if not goal then
40+
return goal_info
41+
end
42+
43+
local goal_info = ""
44+
for i = 0, 31 do
45+
if goal[i] then
46+
local global_flag_index = category * 32 + i -- wizards move
47+
goal_info = string.format("%s", df.dfhack_knowledge_scholar_flag[global_flag_index]) or string.format("Unknown Flag: %s", i)
48+
end
49+
end
50+
return goal_info
51+
end
52+
53+
local function getTopicInfo(topic)
54+
if not topic then
55+
return "No topic information available."
56+
end
57+
58+
local topic_info = ""
59+
60+
for key, value in pairs(topic) do
61+
if type(value) == "boolean" and value then
62+
-- Если значение булевое и равно true, выводим тему как изученную
63+
topic_info = topic_info .. string.format("\n\t\t%s", key)
64+
end
65+
end
66+
67+
if topic_info == "" then
68+
return "No learned topics."
69+
end
70+
71+
return topic_info
72+
end
73+
74+
---@param historical_figure df.historical_figure
75+
function getHistoricKnowledge(historical_figure)
76+
if not historical_figure then
77+
print("Historical figure not found.")
78+
return nil, "Historical figure not found."
79+
end
80+
81+
local known_info = historical_figure.info.known_info
82+
83+
if not known_info then
84+
return nil, "No known_info found."
85+
end
86+
87+
local knowledge = known_info.knowledge
88+
89+
if not knowledge then
90+
return nil, "No knowledge found."
91+
end
92+
93+
return knowledge
94+
end
95+
96+
---@param unit_id df.unit.id
97+
function getUnitKnowledge(unit_id)
98+
return getHistoricKnowledge(getHistoricalFigure(unit_id))
99+
end
100+
101+
---@param historical_figure df.historical_figure
102+
function getResearchData(historical_figure)
103+
local knowledge = getHistoricKnowledge(historical_figure)
104+
local data = {
105+
106+
name = dfhack.translation.translateName(historical_figure.name), --dfhack.translation.TranslateName(historical_figure.name),
107+
knowledge_goal_category = getKnowledgeGoalCategoryInfo(knowledge.knowledge_goal_category),
108+
--knowledge_goal = getGoalInfo(knowledge.knowledge_goal),
109+
knowledge_goal = getGoalInfo(knowledge.knowledge_goal, knowledge.knowledge_goal_category),
110+
research_points = knowledge.research_points,
111+
research_percentage = string.format("%.2f", (knowledge.research_points / 100000) * 100),
112+
times_pondered = knowledge.times_pondered,
113+
research_topics = {}
114+
}
115+
116+
for i, topic in pairs(knowledge) do
117+
if type(topic) == "userdata" and i ~= "knowledge_goal" then
118+
local topic_info = getTopicInfo(topic)
119+
if topic_info ~= "\tNo topic information available." and topic_info ~= "No learned topics." then
120+
table.insert(data.research_topics, {name = i, info = topic_info})
121+
end
122+
end
123+
end
124+
125+
return {data}
126+
end

‎internal/research-info/overlay.lua‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Spectate-Overlay module for research-info
2+
--@ module = true
3+
4+
local researchInfo = reqscript("internal/research-info/info")
5+
6+
local function formatKnowledgeString(str)
7+
if not str or type(str) ~= "string" or str == "" then
8+
return str or ""
9+
end
10+
11+
local parts = {}
12+
for part in str:gmatch("[^_]+") do
13+
-- Первую букву делаем заглавной, остальные - строчными
14+
local formatted = part:sub(1,1):upper() .. part:sub(2):lower()
15+
table.insert(parts, formatted)
16+
end
17+
18+
return table.concat(parts, " ")
19+
end
20+
21+
function GetUnitResearchInfo(unit_id)
22+
local info = ''
23+
24+
historical_figure = researchInfo.getHistoricalFigure(unit_id)
25+
local knowledge = researchInfo.getHistoricKnowledge(historical_figure)
26+
local data = {
27+
knowledge_goal_category = researchInfo.getKnowledgeGoalCategoryInfo(knowledge.knowledge_goal_category),
28+
knowledge_goal = formatKnowledgeString(researchInfo.getGoalInfo(knowledge.knowledge_goal, knowledge.knowledge_goal_category)),
29+
research_points = knowledge.research_points, --TOTAL: 100'000
30+
research_percentage = string.format("%.2f", (knowledge.research_points / 100000) * 100),
31+
times_pondered = knowledge.times_pondered
32+
}
33+
34+
if((data.knowledge_goal ~= 'nil') and ((data.times_pondered > 0) or (knowledge.research_points > 0))) then
35+
info = data.knowledge_goal..string.format(' %s%%', data.research_percentage)..string.format(' [%d] ', data.times_pondered)
36+
end
37+
38+
return info
39+
end
40+
41+
function GetUnitKnowledge(unit_id)
42+
return researchInfo.getUnitKnowledge(unit_id)
43+
end

‎research-info.lua‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
local researchInfo = reqscript("internal/research-info/info")
2+
3+
local function sortResearchDataByPercAndPondered(data_list)
4+
table.sort(data_list, function(a, b)
5+
if tonumber(a.research_percentage) == tonumber(b.research_percentage) then
6+
return a.times_pondered > b.times_pondered
7+
end
8+
return tonumber(a.research_percentage) > tonumber(b.research_percentage)
9+
end)
10+
11+
return data_list
12+
end
13+
14+
local function printResearchData(data)
15+
if not data then
16+
print("No data to display.")
17+
return
18+
end
19+
20+
if data.research_points == 0 and data.knowledge_goal == "" and data.times_pondered == 0 then
21+
return
22+
end
23+
24+
print("==================")
25+
print("Dwarf: " .. data.name)
26+
print("\tKnowledge Goal Category: " .. data.knowledge_goal_category)
27+
print("\tKnowledge Goal: " .. data.knowledge_goal)
28+
print("\tResearch Points: " .. data.research_points .. "/100000")
29+
print("\tResearch Percentage: " .. data.research_percentage .. "%")
30+
print("\tTimes Pondered: " .. data.times_pondered)
31+
32+
if #data.research_topics > 0 then
33+
print("Knowledge:")
34+
for _, topic in ipairs(data.research_topics) do
35+
print("\t" .. topic.name .. ": " .. topic.info)
36+
end
37+
end
38+
end
39+
40+
--
41+
local unit = dfhack.units.getCitizens()
42+
local selected_unit = dfhack.gui.getSelectedUnit()
43+
44+
if unit then
45+
local research_data_list = {}
46+
for i, unit in pairs(dfhack.units.getCitizens()) do
47+
48+
if selected_unit and selected_unit.id ~= unit.id then
49+
goto continue
50+
end
51+
52+
local historical_figure = researchInfo.getHistoricalFigure(unit.id)
53+
if historical_figure then
54+
local research_data, error_msg = researchInfo.getResearchData(historical_figure)
55+
if research_data then
56+
for _, data in ipairs(research_data) do
57+
table.insert(research_data_list, data)
58+
end
59+
end
60+
end
61+
::continue::
62+
end
63+
64+
research_data_list = sortResearchDataByPercAndPondered(research_data_list)
65+
66+
for _, data in ipairs(research_data_list) do
67+
printResearchData(data)
68+
end
69+
end

0 commit comments

Comments
 (0)