Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[preserve-rooms] don't reserve a room for expelled units #4953

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Template for new versions:
## New Features

## Fixes
- `preserve-rooms`: don't reserve a room for citizens that you expel from the fort

## Misc Improvements

Expand Down
25 changes: 24 additions & 1 deletion plugins/preserve-rooms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
#include "modules/Units.h"
#include "modules/World.h"

#include "df/army.h"
#include "df/army_controller.h"
#include "df/building_civzonest.h"
#include "df/historical_figure.h"
#include "df/historical_figure_info.h"
#include "df/histfig_hf_link.h"
#include "df/state_profilest.h"
#include "df/unit.h"
#include "df/world.h"

Expand Down Expand Up @@ -404,6 +408,15 @@ static void scrub_reservations(color_ostream &out) {
pending_reassignment.erase(hfid);
}

static bool was_expelled(df::historical_figure *hf) {
if (!hf || !hf->info || !hf->info->whereabouts)
return false;
auto army = df::army::find(hf->info->whereabouts->army_id);
if (!army || !army->controller)
return false;
return army->controller->goal == df::army_controller_goal_type::MOVE_TO_SITE;
}

// handles when units disappear from their assignments compared to the last scan
static void handle_missing_assignments(color_ostream &out,
const unordered_set<int32_t> & active_unit_ids,
Expand All @@ -421,6 +434,10 @@ static void handle_missing_assignments(color_ostream &out,
int32_t hfid = it->second.first;
int32_t spouse_hfid = it->second.second;
auto hf = df::historical_figure::find(hfid);
if (!hf) {
// if the historical figure was culled, bail
continue;
}
auto unit = df::unit::find(hf->unit_id);
if (!unit) {
// if unit data is completely gone, then they're not likely to come back
Expand All @@ -430,8 +447,14 @@ static void handle_missing_assignments(color_ostream &out,
// unit is still alive on the map; assume the unassigment was intentional/expected
continue;
}
if (!Units::isCitizen(unit, true) && !Units::isResident(unit, true))
if (!Units::isCitizen(unit, true) && !Units::isResident(unit, true)) {
// ignore units that are not members of the fort
continue;
}
if (was_expelled(hf)) {
// ignore expelled units
continue;
}
auto zone = virtual_cast<df::building_civzonest>(df::building::find(zone_id));
if (!zone)
continue;
Expand Down