[DEI] AI Population Framework (1.3.5)
Due to a misunderstanding, this simple script is not meant to be a fully featured mod (but can be used as such); it is intended only as a basic way to force the AI to use population. It was created as a foundation for anyone who wants to make the AI population-dependent in DEI. The script includes additional commands for testing and is highly modular, ensuring it has little to no impact on existing DEI population files and can be easily incorporated into other mods. The developers of DEI considered forcing AI to use the population system long time ago, but they decided against it due to feedback of how it handicaps the AI and they lack a full understanding of how the population system works. The provided script uses a similar mechanic to base DEI; however, base DEI restricts unit recruitment exclusively through the "REFORMS" system by calling add_event_restricted_unit_record at the start of the game and removing it once the "REFORMS" date is reached.
# DEAI Population – Technical Overview (ALPHA)
**Disclaimer:** This mod is currently in ALPHA and has not been extensively tested. Use with caution in long-term campaigns.
This mod modifies the population system in Divide et Impera (DEI) to apply manpower constraints to AI factions.
## Comparison: Base DEI vs. DEAI Population
### Base DEI AI Behavior
* **Recruitment:** AI can recruit any unit regardless of the region’s population levels.
* **Manpower Cost:** Population is not subtracted from regions when the AI recruits or replenishes units.
* **Replenishment:** AI units replenish even if the local population classes required for that unit are depleted.
### This Mod’s AI Behavior
* **Recruitment Restrictions:** AI recruitment is dynamically restricted based on available population in the current region.
* **Manpower Cost:** When an AI unit is recruited, the cost is subtracted from both the UI population tables and the backend manpower ledger.
## Implementation Details
The system is designed to be lightweight, consisting of approximately 100 lines of core logic code. It operates on a "Snapshot and Cache" logic to minimize performance impact:
1. **Trigger:** The check occurs at the start of every AI character’s turn.
2. **Context:** The script identifies the region where the character is currently standing.
3. **Validation:** Local population counts are compared against the faction’s unit costs.
4. **Restriction:** If population is insufficient, the `add_event_restricted_unit_record_for_faction` function is called for that specific unit/faction.
5. **Caching:** A state machine cache tracks which units are already restricted to avoid redundant game engine calls.
## Known Limitations
* **AOR Units:** Area of Recruitment (AOR) units are currently **excluded** from the restriction system. Checking ~200+ AOR units for every AI character caused significant end-turn lag. AOR units still cost population when recruited, but the AI is not prevented from recruiting them if pop is low.
* **Movement Inconsistency:** The recruitment check happens at the *start* of the character’s turn. If an AI character moves from a low-population region to a high-population region within the same turn, they will remain restricted based on the starting region.
* **Multiple Recruitment Loops:** Because the engine does not provide a "pre-recruitment" check for AI, if an AI has enough population for one unit, it is "unrestricted." If the AI has high recruitment capacity, it may recruit multiple units in one turn before the population is deducted.
* **Foreign Regions:** If an AI character starts its turn in a region owned by another faction (e.g., during an invasion), the script may use that region’s population to determine recruitment eligibility.
*Note: Some of these limitations—specifically tracking region ownership and movement updates—are addressable and may be improved in future versions.*
## Known Bugs & Quirks
* **Cache Persistence:** The restriction cache currently clears by unrestricting all units on Turn 1. This can cause a temporary hang at the start of a campaign. A save/load system for the cache is planned to resolve this.
* **UI Sync:** While the backend population is correctly subtracted, there can be a 1-turn delay in the UI updating the numbers for the player to see if they are viewing AI-held regions.
## Testing & Debugging (Consul Scriptum)
* **/reset_population**: Resets the population of all regions to 0.
* **Performance Warning**: Using this on the Grand Campaign map can lead to extremely long turn times as the engine recalculates manpower for every region.
* **/set_pop <region_key> <class_idx> <amount>**: Sets a specific population class (1-4) for a specific region.
* **/reset_pop_region <region_key>**: Resets all population classes to 0 for a specific region.
* **/show_shroud**: Disables the fog of war, allowing you to monitor AI movements and recruitment behavior across the entire map.
* **Log File**: The mod generates a `DEAI_Module_Log.txt` file in the game’s root directory, providing a real-time trace of restriction checks, deductions, and cache updates.
*(Note: Consul Scriptum is not required for standard operation but is needed for these commands).*
Required items:
Click the title to search on this site.
Divide et Impera Part 1 — Steam Workshop
Divide et Impera Part 2 — Steam Workshop
Divide et Impera Part 3 — Steam Workshop