Skip to content

Fixed freezing after siege due to Infinite Loop bug in BKSettlementBehaviour - #205

Open
tenttomi wants to merge 1 commit into
R-Vaccari:mainfrom
tenttomi:fix-siege-consequences-freeze
Open

Fixed freezing after siege due to Infinite Loop bug in BKSettlementBehaviour#205
tenttomi wants to merge 1 commit into
R-Vaccari:mainfrom
tenttomi:fix-siege-consequences-freeze

Conversation

@tenttomi

@tenttomi tenttomi commented Feb 3, 2025

Copy link
Copy Markdown

In the SiegeConsequences method in BKSettlementBehaviour class, an infinite loop could occur when attempting to reduce the population after a siege, resulting the game freezing. This happened due to a specific edge case in population type selection:

  1. The game selects which population type to decrease using weighted random selection.
  2. One of the population types had a weight of 0, meaning it could never be chosen.
  3. All other types had non-zero weights but had a population count of 0.
  4. Since ChooseWeighted() only picks from weighted options, it never selected the only type that had remaining population.
  5. This resulted in an infinite loop where finalNum = 0 every iteration, preventing toKill from decreasing.

Screenshot 2025-02-03 132951

weights is apparently derived from demand, and it is possible for a type to have 0 from the returned number

example of values of this case hapenning from debugger
Screenshot 2025-02-02 102315
Screenshot 2025-02-02 102542

The fix changes the way population is decreased. Instead of iteratively, now the kill distribution is calculated based on the weight proportions of each population type, still adhering to weights used before. Previously, If a population type runs out, the while loop continues which contributes to the issue discussed above. Now instead of loop, any unallocated kills due to low population counts (i.e. when kill for a type is more than that type count) are redistributed proportionally across all remaining population classes, to deal with edge cases like above.

@tenttomi tenttomi changed the title Fixed freezing due to Infinite Loop bug in BKSettlementBehaviour Fixed freezing after siege due to Infinite Loop bug in BKSettlementBehaviour Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant