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

Military::removeFromSquad: Add missing history event when unassigning officer #5269

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,9 @@ Military module
Removes a unit from its squad. Unsets the unit's
military information (i.e., ``unit.military.squad_id`` and
``unit.military.squad_pos``), the squad's position information (i.e.,
``squad.positions[squad_pos].occupant``), and modifies the unit's entity links
to indicate former squad membership or command.
``squad.positions[squad_pos].occupant``), modifies the unit's entity links
to indicate former squad membership or command, and creates a corresponding
world history event.

Items module
------------
Expand Down
18 changes: 18 additions & 0 deletions library/modules/Military.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "df/histfig_entity_link_squadst.h"
#include "df/historical_figure.h"
#include "df/historical_entity.h"
#include "df/history_event_remove_hf_entity_linkst.h"
#include "df/entity_position.h"
#include "df/entity_position_assignment.h"
#include "df/plotinfost.h"
Expand Down Expand Up @@ -334,6 +335,8 @@ static void remove_soldier_entity_link(df::historical_figure* hf, df::squad* squ
former_squad->link_strength = 100;

hf->entity_links.push_back(former_squad);

// Unassigning a normal soldier does not seem to generate an event record.
}

static void remove_officer_entity_link(df::historical_figure* hf, df::squad* squad)
Expand All @@ -343,6 +346,7 @@ static void remove_officer_entity_link(df::historical_figure* hf, df::squad* squ
return;

int32_t assignment_id = -1;
int32_t pos_id = -1;
for (auto& np : nps)
{
if (np.entity->id != squad->entity_id || np.assignment->squad_id != squad->id)
Expand All @@ -352,6 +356,7 @@ static void remove_officer_entity_link(df::historical_figure* hf, df::squad* squ
np.assignment->histfig2 = -1;

assignment_id = np.assignment->id;
pos_id = np.position->id;
break;
}

Expand Down Expand Up @@ -389,6 +394,19 @@ static void remove_officer_entity_link(df::historical_figure* hf, df::squad* squ
former_pos->link_strength = 100;

hf->entity_links.push_back(former_pos);

int32_t event_id = *df::global::hist_event_next_id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int32_t event_id = *df::global::hist_event_next_id;
int32_t event_id = (*df::global::hist_event_next_id)++;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should work, allowing you to remove the explicit increment below

Copy link
Contributor Author

@ong-yinggao98 ong-yinggao98 Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I tested that earlier before publishing this but could not tell which would be visually neater. if you prefer it I will do just that.

auto former_pos_event = df::allocate<df::history_event_remove_hf_entity_linkst>();
former_pos_event->year = *df::global::cur_year;
former_pos_event->seconds = *df::global::cur_year_tick;
former_pos_event->id = event_id;
former_pos_event->civ = squad->entity_id;
former_pos_event->histfig = hf->id;
former_pos_event->position_id = pos_id;
former_pos_event->link_type = df::histfig_entity_link_type::POSITION;

df::global::world->history.events.push_back(former_pos_event);
*df::global::hist_event_next_id = event_id + 1;
}

bool Military::removeFromSquad(int32_t unit_id)
Expand Down