-
Notifications
You must be signed in to change notification settings - Fork 199
/
open-legends.lua
203 lines (177 loc) · 6.39 KB
/
open-legends.lua
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
-- open legends screen when in fortress mode
local dialogs = require('gui.dialogs')
local gui = require('gui')
local utils = require('utils')
local widgets = require('gui.widgets')
tainted = tainted or false
-- --------------------------------
-- LegendsManager
--
LegendsManager = defclass(LegendsManager, gui.ZScreen)
LegendsManager.ATTRS {
focus_path='open-legends',
defocused=true,
no_autoquit=false,
}
function LegendsManager:init()
tainted = true
-- back up what we can to make a return to the previous mode possible.
-- note that even with these precautions, data **is lost** when switching
-- to legends mode and back. testing shows that a savegame made directly
-- after returning from legends mode will be **smaller** than a savegame
-- made just before entering legends mode. We don't know exactly what is
-- missing, but it shows that jumping back and forth between modes is not
-- safe.
self.region_details_backup = {} --as:df.world_region_details[]
local vec = df.global.world.world_data.midmap_data.region_details
utils.assign(self.region_details_backup, vec)
vec:resize(0)
self.gametype_backup = df.global.gametype
df.global.gametype = df.game_type.VIEW_LEGENDS
local legends = df.viewscreen_legendsst:new()
legends.page:insert("#", {new=true, header="Legends", mode=0, index=-1})
dfhack.screen.show(legends)
self:addviews{
widgets.Panel{
frame=gui.get_interface_frame(),
subviews={
widgets.Panel{
view_id='done_mask',
frame={t=1, r=2, w=8, h=3},
},
},
},
}
end
function LegendsManager:isMouseOver()
return self.subviews.done_mask:getMouseFramePos()
end
function LegendsManager:onInput(keys)
if keys.LEAVESCREEN or keys._MOUSE_R or (keys._MOUSE_L and self.subviews.done_mask:getMousePos()) then
if self.no_autoquit then
self:dismiss()
else
dialogs.showYesNoPrompt('Exiting to avoid save corruption',
'Dwarf Fortress is in a non-playable state\nand will now exit to protect your savegame.',
COLOR_YELLOW,
self:callback('dismiss'))
end
return true
end
return LegendsManager.super.onInput(self, keys)
end
function LegendsManager:onDestroy()
if not self.no_autoquit then
dfhack.run_command('die')
else
df.global.gametype = self.gametype_backup
local vec = df.global.world.world_data.midmap_data.region_details
vec:resize(0)
utils.assign(vec, self.region_details_backup)
dfhack.run_script('devel/pop-screen')
-- disable autosaves for the remainder of this session
df.global.d_init.feature.autosave = df.d_init_autosave.NONE
end
end
-- --------------------------------
-- LegendsWarning
--
LegendsWarning = defclass(LegendsWarning, widgets.Window)
LegendsWarning.ATTRS {
frame_title='Open Legends Mode',
frame={w=50, h=14},
autoarrange_subviews=true,
autoarrange_gap=1,
no_autoquit=false,
}
function LegendsWarning:init()
if not self.no_autoquit then
self.frame.h = self.frame.h + 5
end
if dfhack.world.isFortressMode() then
self.frame.h = self.frame.h + (self.no_autoquit and 7 or 2)
end
self:addviews{
widgets.Label{
text={
'This script allows you to jump into legends', NEWLINE,
'mode from a active game, but beware that this', NEWLINE,
'is a', {gap=1, text='ONE WAY TRIP', pen=COLOR_RED}, '.', NEWLINE,
NEWLINE,
'Returning to play from legends mode', NEWLINE,
'would make the game unstable, so to protect', NEWLINE,
'your savegame, Dwarf Fortress will exit when', NEWLINE,
'you are done browsing.',
},
visible=not self.no_autoquit,
},
widgets.Label{
text={
'You have opted for a ', {text='two-way ticket', pen=COLOR_RED} ,' to legends', NEWLINE,
'mode. Remember to ', {text='quit to desktop and restart', pen=COLOR_RED}, NEWLINE,
'DF when you\'re done to avoid save corruption.'
},
visible=self.no_autoquit,
},
widgets.Label{
text={
'When you return to fort mode, automatic', NEWLINE,
'autosaves will be disabled until you restart', NEWLINE,
'DF to avoid accidentally overwriting good', NEWLINE,
'savegames.',
},
visible=self.no_autoquit and dfhack.world.isFortressMode(),
},
widgets.Label{
text='This is your last chance to save your game.',
text_pen=COLOR_LIGHTRED,
},
widgets.HotkeyLabel{
frame={l=0},
key='CUSTOM_SHIFT_S',
label='Please click here to create an Autosave',
text_pen=COLOR_YELLOW,
on_activate=function() dfhack.run_command('quicksave') end,
visible=dfhack.world.isFortressMode(),
},
widgets.Label{
text={
(dfhack.world.isFortressMode() and 'Alternately,' or 'You can') .. ' exit out of this dialog and', NEWLINE,
'create a named save of your choice.',
},
},
widgets.HotkeyLabel{
key='CUSTOM_ALT_L',
label='Click here to continue to legends mode',
text_pen=self.no_autoquit and COLOR_RED or nil,
auto_width=true,
on_activate=function()
self.parent_view:dismiss()
LegendsManager{no_autoquit=self.no_autoquit}:show()
end,
},
}
end
LegendsWarningScreen = defclass(LegendsWarningScreen, gui.ZScreenModal)
LegendsWarningScreen.ATTRS {
focus_path='open-legends/warning',
no_autoquit=false,
}
function LegendsWarningScreen:init()
self:addviews{LegendsWarning{no_autoquit=self.no_autoquit}}
end
function LegendsWarningScreen:onDismiss()
view = nil
end
if not dfhack.isWorldLoaded() then
qerror('no world loaded')
end
local function main(args)
local no_autoquit = args[1] == '--no-autoquit'
if tainted then
LegendsManager{no_autoquit=no_autoquit}:show()
else
view = view and view:raise() or LegendsWarningScreen{no_autoquit=no_autoquit}:show()
end
end
main{...}