@@ -477,42 +477,53 @@ bool Military::addToSquad(int32_t unit_id, int32_t squad_id, int32_t squad_pos)
477477
478478bool Military::removeFromSquad (int32_t unit_id)
479479{
480+ // based on unitst::remove_squad_info
480481 df::unit *unit = df::unit::find (unit_id);
481482 if (unit == nullptr || unit->military .squad_id == -1 || unit->military .squad_position == -1 )
482483 return false ;
483484
484- int32_t squad_id = unit->military .squad_id ;
485- df::squad* squad = df::squad::find (squad_id);
486- if (squad == nullptr )
487- return false ;
485+ // abort individual training
486+ if (unit->individual_drills .size ())
487+ unit->flags3 .bits .verify_personal_training = true ;
488488
489+ int32_t squad_id = unit->military .squad_id ;
489490 int32_t squad_pos = unit->military .squad_position ;
490- df::squad_position* pos = vector_get (squad->positions , squad_pos);
491- if (pos == nullptr )
492- return false ;
493491
494- df::historical_figure* hf = df::historical_figure::find (unit->hist_figure_id );
495- if (hf == nullptr )
496- return false ;
492+ df::squad* squad = df::squad::find (squad_id);
493+
494+ if (squad)
495+ {
496+ df::squad_position* pos = vector_get (squad->positions , squad_pos);
497+ if (pos)
498+ {
499+ // based on unitst::remove_squad_activity_info
500+ FOR_ENUM_ITEMS (squad_event_type, i)
501+ {
502+ auto activity_entry = df::activity_entry::find (pos->activities [i]);
503+ if (activity_entry)
504+ {
505+ auto activity_event = binsearch_in_vector (activity_entry->events , &df::activity_event::event_id, pos->events [i]);
506+ if (activity_event)
507+ {
508+ activity_event->removeParticipant (unit->hist_figure_id , unit->id , false );
509+ pos->activities [i] = -1 ;
510+ pos->events [i] = -1 ;
511+ }
512+ }
513+ }
497514
498- // remove from squad information
499- pos->occupant = -1 ;
515+ // remove from squad position
516+ pos->occupant = -1 ;
517+ }
518+ }
500519 // remove from unit information
501520 unit->military .squad_id = -1 ;
502521 unit->military .squad_position = -1 ;
503522
504- // abort individual training
505- if (unit->individual_drills .size ()) {
506- unit->flags3 .bits .verify_personal_training = true ;
507- }
508-
509- // remove unit from squad activities
510- auto activity_entry = binsearch_in_vector (df::global::world->activities .all ,&df::activity_entry::id,squad->activity );
511- if (activity_entry) {
512- for (auto const activity_event : activity_entry->events ) {
513- activity_event->removeParticipant (unit->hist_figure_id , unit->id , false );
514- }
515- }
523+ // remove entity squad link
524+ df::historical_figure* hf = df::historical_figure::find (unit->hist_figure_id );
525+ if (hf == nullptr || squad == nullptr )
526+ return false ;
516527
517528 if (squad_pos == 0 ) // is unit a commander?
518529 remove_officer_entity_link (hf, squad);
0 commit comments