@@ -175,17 +175,36 @@ static class UnitHistory {
175
175
offset = 0 ;
176
176
}
177
177
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
+
178
196
void add_and_follow (color_ostream &out, df::unit *unit) {
179
197
// if we're currently following a unit, add it to the history if it's not already there
180
198
if (plotinfo->follow_unit > -1 && plotinfo->follow_unit != get_cur_unit_id ()) {
181
199
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 );
183
201
}
184
202
185
203
int32_t id = unit->id ;
186
- update_history (out, id);
204
+ add_to_history (out, id);
187
205
DEBUG (cycle,out).print (" now following unit %d: %s\n " , id, DF2CONSOLE (Units::getReadableName (unit)).c_str ());
188
206
Gui::revealInDwarfmodeMap (Units::getPosition (unit), false , World::ReadPauseState ());
207
+ plotinfo->follow_item = -1 ;
189
208
plotinfo->follow_unit = id;
190
209
}
191
210
@@ -199,6 +218,7 @@ static class UnitHistory {
199
218
DEBUG (cycle,out).print (" scanning back to unit %d at offset %zd\n " , unit_id, offset);
200
219
if (auto unit = df::unit::find (unit_id))
201
220
Gui::revealInDwarfmodeMap (Units::getPosition (unit), false , World::ReadPauseState ());
221
+ plotinfo->follow_item = -1 ;
202
222
plotinfo->follow_unit = unit_id;
203
223
}
204
224
@@ -214,6 +234,7 @@ static class UnitHistory {
214
234
DEBUG (cycle,out).print (" scanning forward to unit %d at offset %zd\n " , unit_id, offset);
215
235
if (auto unit = df::unit::find (unit_id))
216
236
Gui::revealInDwarfmodeMap (Units::getPosition (unit), false , World::ReadPauseState ());
237
+ plotinfo->follow_item = -1 ;
217
238
plotinfo->follow_unit = unit_id;
218
239
}
219
240
@@ -222,25 +243,6 @@ static class UnitHistory {
222
243
return -1 ;
223
244
return history[history.size () - (1 + offset)];
224
245
}
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
- }
244
246
} unit_history;
245
247
246
248
// ///////////////////////////////////////////////////
@@ -392,7 +394,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
392
394
DFhackCExport command_result plugin_onupdate (color_ostream &out) {
393
395
announcement_settings.on_update (out);
394
396
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 ())) {
396
398
DEBUG (cycle,out).print (" auto-disengage triggered\n " );
397
399
is_enabled = false ;
398
400
plotinfo->follow_unit = -1 ;
@@ -600,9 +602,19 @@ static void spectate_followNext(color_ostream &out) {
600
602
set_next_cycle_unpaused_ms (out);
601
603
};
602
604
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
+
603
614
DFHACK_PLUGIN_LUA_FUNCTIONS {
604
615
DFHACK_LUA_FUNCTION (spectate_setSetting),
605
616
DFHACK_LUA_FUNCTION (spectate_followPrev),
606
617
DFHACK_LUA_FUNCTION (spectate_followNext),
618
+ DFHACK_LUA_FUNCTION (spectate_addToHistory),
607
619
DFHACK_LUA_END
608
620
};
0 commit comments