AI modding

本页面所适用的版本可能已经过时,最后更新于1.5
A999999讨论 | 贡献2022年6月19日 (日) 10:49的版本


钢铁雄心4中的 AI 可以在许多方面修改. 以下文件主要处理 AI:

  • /Hearts of Iron IV/common/ai_areas
  • /Hearts of Iron IV/common/ai_focuses
  • /Hearts of Iron IV/common/ai_peace
  • /Hearts of Iron IV/common/ai_strategy
  • /Hearts of Iron IV/common/ai_strategy_plans
  • /Hearts of Iron IV/common/ai_templates
  • /Hearts of Iron IV/common/ai_attitudes.txt
  • /Hearts of Iron IV/common/ai_personalities.txt

许多其他给玩家提供选择的地方应附有 AI 倾向来表达 AI 对各选项的权衡,这些地方使用ai_will_do。

AI 倾向

正如在许多文件中看到的那样,ai_will_do mb是一个为 AI 提供决策时权重的参数。在功能上它与 mean_time_to_happen 非常相似。它与其使用相同的修正系统,尽管它对 basefactor 有不同的解释。

注意:AI 对这个参数的运算结果不是完全遵从的,而通常与 probability-proportional-to-size sampling 类似。

base

(Type: scoped variable, default: 1) 在应用其他修改前的基础数值。

factor

(Type: number (with 3 decimal places), default: 1) 应用于计算的系数,

例子

ai_will_do = {
    base = 10.5  # may also be a variable
    
    # If the country is Germany, set the value to 0,
    # causing an early end of the evaluation.
    modifier = { tag = GER factor = 0 }

    modifier = { is_major = yes add = 1 }

    modifier = {
        factor = 3
        add = 2.5
        tag = FRA
    }

    factor = 2
}

假设正常计算,对于GER(德国) 结果为0,对于FRA(法国)结果为74,对于其余主要国家为23,对于次要国家则为21。

AI 区域

/Hearts of Iron IV/common/ai_areas 直接控制了陆地省份在有命名的 AI 区域的分配。但它们只被下文定义的 AI 策略 area_priority 使用。 每个省份只被分配到一个 AI 区域,并且是第一个匹配的区域。没必要将每个省份都分给 AI 区域,但未被分配的不能被 area_priority 策略调用。

在省份的 debug tooltip 可以看到 AI 区域。

这是一个通用示例:

<area name> = {
    # Used to add continents
    continents = {
        <continent>
    }
    
    # Used to add specific strategic regions
    strategic_regions = {
        <strategic region ids>
    }
}

AI 国策

参考 AI focuses#Modding

AI 和平策略

/Hearts of Iron IV/common/ai_peace 中的文件控制了 AI 如何完成和平会议。

There are five types of peace behaviour一共有 5 种 AI 和平策略:

  • 内战
  • 民主主义
  • 共产主义
  • 法西斯主义
  • 默认

和平会议一共有 5 种和平行动:

  • 吞并国家
  • 解放国家
  • 傀儡国家
  • 获取土地
  • 改变政府(原版民主主义专属)

这是一个通用示例:

<type>_peace = {
    # Determines when this peace AI is used by the AI (ROOT is taker, FROM is giver)
    enable = {
        <triggers>
    }
    
    # Random factors applied to AI decision making when determining which
    # peace option to use.
    annex_randomness = <int>
    liberate_randomness = <int>
    puppet_randomness = <int>
    take_states_randomness = <int>
    force_government_randomness = <int>
    
    # Weighting towards the option. (ROOT is taker, FROM is giver)
    <peace option> = {
        factor = <float>
        
        <triggers>
        
        modifier = {
            factor = <float>
            
            <triggers>
        }
    }
}

AI 策略

AI 策略影响在特定策略考量 (types) 中,其他 AI 组件中的决策形成,这也可以是定向的。例如一个国家可以在策略上对保护另一国家有兴趣,而不考虑当前外交与同盟关系;或者它更倾向于训练某一特定类型的师。

在结构层面,一个 AI 策略只有 4 个参数:typevalueid,以及 target 。只有部分类型的 AI 策略使用了 id和/或 target。本来,4 个参数都是整型,但如果 type先被指定,那么id参数可以用名字来调用对应的值(参考下文表格)。target_country 是另一种设置 target的方式。它接受一个国家下标或国家 Tag,并会检查其是否指向了一个可用的国家。

AI 会自己生成动态策略,但想添加静态策略也是可行的。静态策略的主要被/Hearts of Iron IV/common/ai_strategy中的 AI 策略条目AI 策略计划 定义(见下文)。领导人特质(/Hearts of Iron IV/common/country_leader中)可以修改 AI 策略,当特质携带者成为国家领导人时。独立的策略也可以用add_ai_strategy命令直接添加(例如事件·,国策中,等等)。

一个国家有效的 AI 策略保存在保存文件中,作为该国的ai属性。

类型

定义 ai_strategy 时, 类型决定了 AI 策略改变的方面。 抵抗运动 dlc 中新增的 AI 策略可以在 common/ai_strategy/default.txt 中找到。

Type ID Type ID Description
45 added_military_to_civilian_factory_ratio Alters the Military-to-Civilian factory ratio.
28 air_factory_balance Alters the Air factory ratio.
8 alliance <tag> Nudges diplomatic AI towards forming an alliance with another specified country.
3 antagonize <tag> Nudges diplomatic AI towards being meaner to another specified country.
33 area_priority <ai_area> Encourages the military AI to target specified AI area.
4 befriend <tag> Nudges diplomatic AI towards being nicer to another specified country.
10 build_airplane Unused.
11 build_army <unit role> Orders the AI to build a type of division or equipment (as in air planes or ships) that belong to the specified unit role.
12 build_building <building> Orders the AI to build a specific building in a specific location. Uses target to specify the state location.
9 build_ship Unused.
56 building_target <building> Orders the AI to build towards specified amount of <building>. This includes those already owned and acquired by trade (Civilian Factory).
1 colonize Unused.
0 conquer <tag> Encourages the military AI to target specified tag.
6 consider_weak <tag> Nudges the AI disposition towards considering the specified tag weak.
17 contain <tag> Nudges the diplomatic AI towards taking actions against another country, such as guaranteeing their future potential enemies.
41 declare_war <tag> Nudges the AI disposition towards declaring war against the specified tag.
48 dockyard_to_military_factory_ratio Alters the Dockyard-to-Military factory ratio.
49 dont_defend_ally_borders <tag> Nudges the AI disposition towards not defending the specified ally's borders.
44 dont_join_wars_with <tag> Nudges the AI disposition towards not defending the target country when the specified tag is involved. Uses target_country to define the target country.
29 equipment_production_factor <unit type> Alters the equipment production disposition for the specified unit type.
31 equipment_production_min_factories <unit type> Sets the minimum factory count needed to start equipment production for the specified unit type.
26 equipment_stockpile_surplus_ratio Sets the base equipment stockpile surplus value.
30 equipment_variant_production_factor <equipment> Alters the equipment variant production disposition for the specified equipment.
2 explore Unused.
46 factory_build_score_factor Modifies the AI build disposition for a state. Uses target to specific the target state.
50 force_defend_ally_borders Unused.
15 garrison <integer> (scale of 100) Used in ai_strategies/SOV.txt, USA.txt, ENG.txt how much of the army should be kept on garrison duty
32 ignore <tag> Nudges the AI disposition towards ignoring the specified tag.
42 ignore_army_incompetence Sets the AI disposition towards ignoring army incompetence.
47 ignore_claim <tag> Sets AI ignorance of claims against the specified tag. Accepts 1 as a value.
7 influence Unused.
18 invade <tag> Nudges the AI disposition towards invading the specified tag.
21 land_xp_spend_priority Unused.
37 naval_avoid_region <strategic region> Alters AI avoidance of the specified strategic region navally.
35 occupation_policy <tag> Determines the occupation policy towards the specified tag. Accepts values 0 to 3.
40 pp_spend_amount
  • admiral
  • relation
  • guarantee
  • decision
  • etc.
Sets the amount allowed for spending Political Power on the specified action.
22 pp_spend_priority
  • admiral
  • relation
  • guarantee
  • decision
  • etc.
Sets the priority for spending Political Power on the specified action.
38 prepare_for_war <tag> it nudges the AI to plan an attack ahead of time for a possible war with target
5 protect <tag> Nudges the diplomatic AI of a country to try to defend another specified country.
13 research_tech <technology> Orders the AI to research a specific technology, if possible.
20 role_ratio <unit role> Out of the entire army, this is the ratio (in %) of that unit role that the army should have. Roles can be defined arbitrarily in ai_templates.
39 save_equipment <equipment> Makes the AI save equipment of the specified type to the set value.
34 send_volunteers_desire <tag> Alters AI disposition towards sending volunteers to the specified tag.
43 spare_unit_factor Sets the spare unit factor. Used to encourage AI to send more units off to assist in allied fronts and not garrison their home territory.
36 support <tag> Alters AI disposition towards supporting the specified tag against partisans.
23 template_prio <unit type> Sets the template priority of the specified unit type for the AI.
24 template_xp_reserve Sets the XP reserved needed for the AI to consider template changes.
61 force_build_armies Added in 1.6.2, to force AI build armies... Mainly used for China to prepare for war against Japan
19 unit_ratio <unit type> Out of the entire army, this is the ratio (in %) of that unit type that the army should have.
25 equipment_design_priority
58 production_upgrade_desire_offset <equipment>
put_unit_buffers See ai_strategy/default.txt Used to tell AI to garrison certain states

AI 策略条目

一个策略条目包含了一个或多个 AI 策略,并定义了应用的时机和方式。在一个国家检查一个条目的时候,只有在enable 为真以及abort 为空或假时才会激活这个条目。条目将在abort 非空且为真时,或者填写了abort_when_not_enabledenable为假时被废除。

一个reversed) 条目工作的方法却不同。如果条目在ai_strategy中作为id引用且enable_reverse为空或真,条目将对源国家激活。然后此条目将对目标国家检查。如果target_array和/或targets 有设置,它们将用作可能的目标;否则,所有国家都作为可能的目标。enableabort 触发器将对每个目标检查。如果一个国家通过了检查,那么条目中所有ai_strategy的将对源国家激活,同时从目标中移除他们的id

属性/参数

  • allowed: (type: AND trigger) 在游戏开始时只评估一次,因此条件应该是静态的 (例如 tag = GERis_historical_focus_on = yes). 如果未通过条目,它将不再被检查以提高性能。
  • enable: (type: AND trigger) 激活条目 (除非 abort 非空且为真).
  • abort: (type: AND trigger) 若非空且为真,废除该条目。
  • abort_when_not_enabled: (type: yes/no, default: no) 若为真,则在enable为假时废除该条目。
  • ai_strategy: (type: AI strategy, may appear multiple times) 在条目激活后应用的 AI 策略。
  • reversed: (type: yes/no, default: no) 使用上文所述的逆语义。
  • enable_reverse: type: AND trigger) 源国家通过逆触发器向目标国家传递的另一个触发器
  • target_array: (type: variable) 包含反向目标的数组变量, 例如 neighbors, exilesenemies.
  • targets: (type: list of variables) 独立的反向目标列表, 例如 { GER ENG FRA }

AI 策略计划

/Hearts of Iron IV/common/ai_strategy_plans 中的文件定义了特定 Tag 的全面 AI 行为,用以模拟历史上的战略计划。

这是一个通用示例:

<strategy_plan> = {
    name = <name>       # Name of the plan
    desc = <desc>       # Description of the plan
    
    # When this plan is enabled.
    enable = {
        <triggers>
    }
    
    # When this plan is aborted.
    abort = {
        <triggers>
    }
    
    # Defines which national focuses the AI picks first during this plan.
    ai_national_focuses = {
        <focus> # AI picks top downwards
    }
    
    # Defines which technology categories the AI has weighting towards/against during this plan.
    research = {
        <category> = <weight>
    }
    
    # Defines which ideas the AI has weighting towards/against during this plan.
    ideas = {
        <idea> = <weight>
    }
    
    # Defines which traits the AI has weighting towards/against during this plan.
    traits = {
        <trait> = <weight>
    }
    
    # Defines AI strategies to apply during this plan. 
    ai_strategy = {
        <strategy attributes>
    }
    
    # Defines pick weights for specific focuses during this plan.
    focus_factors = {
        <focus> = <weight>
    }
    
    # Keep small, as it is used as a factor for some things (such as research needs)
    # Recommended around 1.0. Useful for relation between plans
    weight = {
        factor = 1.0
        modifier = {
            factor = 1.0
        }
    }
}

AI 模板

/Hearts of Iron IV/common/ai_templates中的文件定义了 AI 角色所使用的模板,以指明其使用的模板。

角色是 AI 模板系统的核心。它们可以任意创建,允许 Modders 创建自定义角色。

分配给某个角色的每个模板将组成一个组,然后以每个可用模板的适应度得来确定在特定时刻为角色使用哪个模板。

这是一个通用示例:

<role_template_name> = {
    # Defines the actual role(s) the template covers.
    roles = {
        <roles>
    }
    
    # Restricts the template to only the specified tags. Optional.
    available_for = {
        <tags>
    }
    
    # Blocks the template for the specified tags. Optional.
    blocked_for = {
        <tags>
    }
    
    # Determines which existing templates are included in this role
    match_to_count = <float> # By default 0.5
    
    # Determines the importance of the role template, highest importance is the role used by the AI.
    upgrade_prio = {
        factor = <float> # Base factor
        
        # Modifies the base factor
        modifier = {
            factor = <float>
            
            <triggers>
        }
    }
    
    # A template that belongs to this role. Multiple are allowed per role template.
    <template_name> = {
        # Determines the importance of the template amoungst other templates listed within the same role.
        upgrade_prio = {
            factor = <float> # Base factor
            
            # Modifies the base factor
            modifier = {
                factor = <float>
                
                <triggers>
            }
        }
        
        # Determines which template the AI will assign to a deployment line for a role. Highest score wins.
        production_prio = {
            factor = <float> # Base factor
            
            # Modifies the base factor
            modifier = {
                factor = <float>
                
                <triggers>
            }
        }
        
        # Determines the requirement to upgrade in field
        can_upgrade_in_field = {
            <triggers>
        }
        
        custom_icon = <int>
        reinforce_prio = <int>
        target_width = <float>
        width_weight = <float>
        column_swap_factor = <float> 

        # Determines the weights for each stat for this template.
        # Order determines which stat the value affects.
        stat_weights = {
            <float> #-- default_morale
            <float> #-- defense
            <float> #-- breakthrough
            <float> #-- hardness
            <float> #-- soft_attack
            <float> #-- hard_attack
            <float> #-- recon
            <float> #-- entrenchment
            <float> #-- initiative
            <float> #-- casualty_trickleback
            <float> #-- supply_consumption_factor
            <float> #-- supply_consumption
            <float> #-- suppression
            <float> #-- suppression_factor
            <float> #-- experience_loss_factor
            <float> #-- equipment_capture_factor
            
            #-- Air Values
            <float> #-- air_attack
            
            #-- Common Values
            <float> #-- max_organisation
            <float> #-- max_strength
            <float> #-- maximum_speed
            <float> #-- armor_value
            <float> #-- ap_attack
            <float> #-- reliability
            <float> #-- reliability_factor
            <float> #-- weight
            <float> #-- build_cost_ic
        }
        
        # 
        target_template = {
            weight = <float>
            match_value = <float>
            
            support = {
                <support unit> = <amount>
            }
            
            regiments = {
                <unit> = <amount>
            }
        }
        
        allowed_types = {
            <units>
        }
        
        replace_at_match = <float>
        replace_with = <template>
        target_min_match = <float>
    }
}