Skip to content

Commit 8b9810d

Browse files
authored
Merge pull request #5288 from myk002/myk_spectate_jump
[spectate] add API for gui/sitemap
2 parents 754a315 + 7d92f18 commit 8b9810d

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

data/init/dfhack.keybindings.init

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ keybinding add Ctrl-A@choose_start_site gui/embark-anywhere
4141
# in the game root directory
4242
keybinding add Ctrl-T@dwarfmode/ViewSheets/UNIT|dwarfmode/ViewSheets/ITEM|dungeonmode/ViewSheets/UNIT|dungeonmode/ViewSheets/ITEM markdown
4343

44+
keybinding add Ctrl-G@dwarfmode/Default|dungeonmode/Default gui/sitemap
4445

4546
######################
4647
# dwarfmode bindings #

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Template for new versions:
5959
- `spectate`: new global keybinding for toggling spectate mode: Ctrl-Shift-S
6060
- `spectate`: new overlay panel that allows you to cycle through following next/prevous units (regardless of whether spectate mode is enabled)
6161
- ``disable-vanilla-dimensions``: new binpatch to disable the vanilla dimensions tooltip
62+
- `gui/sitemap`: is now the official "go to" tool. new global hotkey for fort and adventure mode: Ctrl-G
6263

6364
## Fixes
6465
- Windows console: fix possible hang if the console returns a too-small window width (for any reason)

plugins/lua/spectate.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ FollowPanelOverlay.ATTRS{
563563
default_pos={x=6,y=-5},
564564
viewscreens='dwarfmode/Default',
565565
default_enabled=true,
566-
frame={w=39, h=1},
566+
frame={w=29, h=1},
567567
visible=follow_panel_is_visible,
568568
}
569569

@@ -582,13 +582,14 @@ function FollowPanelOverlay:init()
582582
widgets.Label{
583583
frame={l=10, t=0, w=25},
584584
text={
585-
' spectate mode: ',
586-
{text=function() return isEnabled() and 'enabled ' or 'disabled ' end},
585+
' spectate:',
586+
{text=function() return isEnabled() and ' on ' or 'off ' end,
587+
pen=function() return isEnabled() and COLOR_GREEN or COLOR_LIGHTRED end},
587588
},
588589
on_click=function() dfhack.run_command(isEnabled() and 'disable' or 'enable', 'spectate') end,
589590
},
590591
widgets.ConfigureButton{
591-
frame={l=36, t=0},
592+
frame={l=26, t=0},
592593
on_click=function() dfhack.run_script('gui/spectate') end,
593594
}
594595
}

plugins/spectate.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,36 @@ static class UnitHistory {
175175
offset = 0;
176176
}
177177

178+
void add_to_history(color_ostream &out, int32_t unit_id) {
179+
if (offset > 0) {
180+
DEBUG(cycle,out).print("trimming history forward of offset %zd\n", offset);
181+
history.resize(history.size() - offset);
182+
offset = 0;
183+
}
184+
if (history.size() && history.back() == unit_id) {
185+
DEBUG(cycle,out).print("unit %d is already current unit; not adding to history\n", unit_id);
186+
} else {
187+
history.push_back(unit_id);
188+
if (history.size() > MAX_HISTORY) {
189+
DEBUG(cycle,out).print("history full, truncating\n");
190+
history.pop_front();
191+
}
192+
}
193+
DEBUG(cycle,out).print("history now has %zd entries\n", history.size());
194+
}
195+
178196
void add_and_follow(color_ostream &out, df::unit *unit) {
179197
// if we're currently following a unit, add it to the history if it's not already there
180198
if (plotinfo->follow_unit > -1 && plotinfo->follow_unit != get_cur_unit_id()) {
181199
DEBUG(cycle,out).print("currently following unit %d that is not in history; adding\n", plotinfo->follow_unit);
182-
update_history(out, plotinfo->follow_unit);
200+
add_to_history(out, plotinfo->follow_unit);
183201
}
184202

185203
int32_t id = unit->id;
186-
update_history(out, id);
204+
add_to_history(out, id);
187205
DEBUG(cycle,out).print("now following unit %d: %s\n", id, DF2CONSOLE(Units::getReadableName(unit)).c_str());
188206
Gui::revealInDwarfmodeMap(Units::getPosition(unit), false, World::ReadPauseState());
207+
plotinfo->follow_item = -1;
189208
plotinfo->follow_unit = id;
190209
}
191210

@@ -199,6 +218,7 @@ static class UnitHistory {
199218
DEBUG(cycle,out).print("scanning back to unit %d at offset %zd\n", unit_id, offset);
200219
if (auto unit = df::unit::find(unit_id))
201220
Gui::revealInDwarfmodeMap(Units::getPosition(unit), false, World::ReadPauseState());
221+
plotinfo->follow_item = -1;
202222
plotinfo->follow_unit = unit_id;
203223
}
204224

@@ -214,6 +234,7 @@ static class UnitHistory {
214234
DEBUG(cycle,out).print("scanning forward to unit %d at offset %zd\n", unit_id, offset);
215235
if (auto unit = df::unit::find(unit_id))
216236
Gui::revealInDwarfmodeMap(Units::getPosition(unit), false, World::ReadPauseState());
237+
plotinfo->follow_item = -1;
217238
plotinfo->follow_unit = unit_id;
218239
}
219240

@@ -222,25 +243,6 @@ static class UnitHistory {
222243
return -1;
223244
return history[history.size() - (1 + offset)];
224245
}
225-
226-
private:
227-
void update_history(color_ostream &out, int32_t unit_id) {
228-
if (offset > 0) {
229-
DEBUG(cycle,out).print("trimming history forward of offset %zd\n", offset);
230-
history.resize(history.size() - offset);
231-
offset = 0;
232-
}
233-
if (history.size() && history.back() == unit_id) {
234-
DEBUG(cycle,out).print("unit %d is already current unit; not adding to history\n", unit_id);
235-
} else {
236-
history.push_back(unit_id);
237-
if (history.size() > MAX_HISTORY) {
238-
DEBUG(cycle,out).print("history full, truncating\n");
239-
history.pop_front();
240-
}
241-
}
242-
DEBUG(cycle,out).print("history now has %zd entries\n", history.size());
243-
}
244246
} unit_history;
245247

246248
/////////////////////////////////////////////////////
@@ -392,7 +394,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
392394
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
393395
announcement_settings.on_update(out);
394396

395-
if (config.auto_disengage && (plotinfo->follow_unit < 0 || is_squads_open())) {
397+
if (config.auto_disengage && (plotinfo->follow_unit < 0 || plotinfo->follow_item > -1 || is_squads_open())) {
396398
DEBUG(cycle,out).print("auto-disengage triggered\n");
397399
is_enabled = false;
398400
plotinfo->follow_unit = -1;
@@ -600,9 +602,19 @@ static void spectate_followNext(color_ostream &out) {
600602
set_next_cycle_unpaused_ms(out);
601603
};
602604

605+
static void spectate_addToHistory(color_ostream &out, int32_t unit_id) {
606+
DEBUG(control,out).print("entering spectate_addToHistory; unit_id=%d\n", unit_id);
607+
if (!df::unit::find(unit_id)) {
608+
WARN(control,out).print("unit with id %d not found; not adding to history\n", unit_id);
609+
return;
610+
}
611+
unit_history.add_to_history(out, unit_id);
612+
}
613+
603614
DFHACK_PLUGIN_LUA_FUNCTIONS {
604615
DFHACK_LUA_FUNCTION(spectate_setSetting),
605616
DFHACK_LUA_FUNCTION(spectate_followPrev),
606617
DFHACK_LUA_FUNCTION(spectate_followNext),
618+
DFHACK_LUA_FUNCTION(spectate_addToHistory),
607619
DFHACK_LUA_END
608620
};

0 commit comments

Comments
 (0)