Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1fd0e4

Browse files
committed
refine/fix logic around restoring zones
1 parent c7ebc64 commit a1fd0e4

2 files changed

Lines changed: 27 additions & 21 deletions

File tree

plugins/lua/preserve-rooms.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ local GLOBAL_KEY = 'preserve-rooms'
1212
--
1313

1414
local function print_status()
15-
local features = preserve_rooms_getState()
15+
local features, stats = preserve_rooms_getState()
1616
print('Features:')
1717
for feature,enabled in pairs(features) do
1818
print((' %20s: %s'):format(feature, enabled))
1919
end
20+
print()
21+
print('Rooms reserved for traveling units:', stats.reservations)
2022
end
2123

2224
local function do_set_feature(enabled, feature)

plugins/preserve-rooms.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,6 @@ DFhackCExport command_result plugin_save_site_data (color_ostream &out) {
237237
return CR_OK;
238238
}
239239

240-
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
241-
if (event == DFHack::SC_WORLD_UNLOADED) {
242-
if (is_enabled) {
243-
DEBUG(control,out).print("world unloaded; disabling %s\n", plugin_name);
244-
is_enabled = false;
245-
}
246-
}
247-
return CR_OK;
248-
}
249-
250240
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
251241
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode())
252242
return CR_OK;
@@ -308,7 +298,7 @@ static void assign_nobles(color_ostream &out) {
308298
continue;
309299
Buildings::setOwner(zone, unit);
310300
INFO(cycle,out).print("preserve-rooms: assigning %s to a %s-associated %s\n",
311-
Units::getReadableName(unit).c_str(), code.c_str(),
301+
DF2CONSOLE(Units::getReadableName(unit)).c_str(), code.c_str(),
312302
ENUM_KEY_STR(civzone_type, zone->type).c_str());
313303
break;
314304
}
@@ -371,13 +361,14 @@ static void handle_missing_assignments(color_ostream &out,
371361
if (!zone)
372362
continue;
373363
// unit is off-map or is dead; if we can assign room to spouse then we don't need to reserve the room
374-
if (auto spouse_hf = df::historical_figure::find(spouse_hfid); spouse_hf && share_with_spouse) {
364+
auto spouse_hf = df::historical_figure::find(spouse_hfid);
365+
if (spouse_hf && share_with_spouse) {
375366
if (auto spouse = df::unit::find(spouse_hf->unit_id);
376367
spouse && Units::isActive(spouse) && !Units::isDead(spouse) && active_unit_ids.contains(spouse->id))
377368
{
378369
DEBUG(cycle,out).print("assigning zone %d (%s) to spouse %s\n",
379370
zone_id, ENUM_KEY_STR(civzone_type, zone->type).c_str(),
380-
Units::getReadableName(spouse).c_str());
371+
DF2CONSOLE(Units::getReadableName(spouse)).c_str());
381372
Buildings::setOwner(zone, spouse);
382373
continue;
383374
}
@@ -387,7 +378,7 @@ static void handle_missing_assignments(color_ostream &out,
387378
// register the hf ids for reassignment and reserve the room
388379
DEBUG(cycle,out).print("registering primary unit for reassignment to zone %d (%s): %d %s\n",
389380
zone_id, ENUM_KEY_STR(civzone_type, zone->type).c_str(), unit->id,
390-
Units::getReadableName(unit).c_str());
381+
DF2CONSOLE(Units::getReadableName(unit)).c_str());
391382
pending_reassignment[hfid].push_back(zone_id);
392383
reserved_zones[zone_id].push_back(hfid);
393384
if (share_with_spouse && spouse_hfid > -1) {
@@ -396,6 +387,12 @@ static void handle_missing_assignments(color_ostream &out,
396387
pending_reassignment[spouse_hfid].push_back(zone_id);
397388
reserved_zones[zone_id].push_back(spouse_hfid);
398389
}
390+
INFO(cycle,out).print("preserve-rooms: reserving %s for the return of %s%s%s\n",
391+
toLower_cp437(ENUM_KEY_STR(civzone_type, zone->type)).c_str(),
392+
DF2CONSOLE(Units::getReadableName(unit)).c_str(),
393+
spouse_hf ? " or their spouse, " : "",
394+
spouse_hf ? DF2CONSOLE(Units::getReadableName(spouse_hf)).c_str() : "");
395+
399396
zone->spec_sub_flag.bits.active = false;
400397
}
401398
}
@@ -481,22 +478,24 @@ static void on_new_active_unit(color_ostream& out, void* data) {
481478
if (!unit || unit->hist_figure_id < 0)
482479
return;
483480
TRACE(event,out).print("unit %d (%s) arrived on map (hfid: %d, in pending: %d)\n",
484-
unit->id, Units::getReadableName(unit).c_str(), unit->hist_figure_id,
481+
unit->id, DF2CONSOLE(Units::getReadableName(unit)).c_str(), unit->hist_figure_id,
485482
pending_reassignment.contains(unit->hist_figure_id));
486483
auto hfid = unit->hist_figure_id;
487484
auto it = pending_reassignment.find(hfid);
488485
if (it == pending_reassignment.end())
489486
return;
490487
INFO(event,out).print("preserve-rooms: restoring room ownership for %s\n",
491-
Units::getReadableName(unit).c_str());
488+
DF2CONSOLE(Units::getReadableName(unit)).c_str());
492489
for (auto zone_id : it->second) {
493490
reserved_zones.erase(zone_id);
494491
auto zone = virtual_cast<df::building_civzonest>(df::building::find(zone_id));
495-
if (!zone || zone->assigned_unit || spouse_has_sharable_room(out, hfid, zone->type))
492+
if (!zone)
496493
continue;
497-
DEBUG(event,out).print("assigning and activating zone %d\n", zone->id);
498-
Buildings::setOwner(zone, unit);
499494
zone->spec_sub_flag.bits.active = true;
495+
if (zone->assigned_unit || spouse_has_sharable_room(out, hfid, zone->type))
496+
continue;
497+
DEBUG(event,out).print("reassigning zone %d\n", zone->id);
498+
Buildings::setOwner(zone, unit);
500499
}
501500
pending_reassignment.erase(it);
502501
}
@@ -615,7 +614,12 @@ static int preserve_rooms_getState(lua_State *L) {
615614
features.emplace("track-roles", config.get_bool(CONFIG_TRACK_ROLES));
616615
Lua::Push(L, features);
617616

618-
return 1;
617+
unordered_map<string, size_t> stats;
618+
stats.emplace("travelers", pending_reassignment.size());
619+
stats.emplace("reservations", reserved_zones.size());
620+
Lua::Push(L, stats);
621+
622+
return 2;
619623
}
620624

621625
DFHACK_PLUGIN_LUA_FUNCTIONS{

0 commit comments

Comments
 (0)