Skip to content

03 Grouping common facts

Satoshi Teshiba edited this page Jan 15, 2021 · 4 revisions

CommandCollection class can group each defrules.
And the class can have common Facts of each defrules.

TrainVillager and ResearchLoom have a common Fact, which is current_age.
So if the common Facts contain current_age Fact, each TrainVillager and ResearchLoom don't need current_age Fact.

To make defrules that have a common current_age Fact, write the following C # code.

using System;
using LibAoe2AISharp.Framework;
using LibAoe2AISharp.Specifications;
using static LibAoe2AISharp.Specifications.Ope;

namespace Aoe2AISharpSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var darkAgeCmds = new CommandCollection("Scripts for Dark age") {
                new ResearchLoom(7),
                new TrainVillager(25),
            };
            darkAgeCmds.CommonFacts.Add(new current_age(relop.eq, age.dark_age));

            Console.WriteLine(darkAgeCmds.ToScript());
        }
    }

    public class TrainVillager : Train
    {
        public TrainVillager(int count)
            : base(unit.villager)
        {
            Facts.Add(new unit_type_count_total(unit.villager, relop.lt, count));
        }
    }
    public class ResearchLoom : Research
    {
        public ResearchLoom(short villagerCount)
            : base(ri.loom)
        {
            Facts.Add(
                 !new can_train(unit.villager)
                | new unit_type_count(unit.villager, relop.ge, villagerCount)
                );
        }

    }
}

Then, it will output the following AI script.

;===============================================================================
;description: Scripts for Dark age
;common fact: Check dark_age
; Check dark_age: Research loom
; Check dark_age: Train villager
;===============================================================================
;Check dark_age: Research loom
(defrule
    (can-research ri-loom) ;Can research loom?
    (or
        (not (can-train villager)) ;Can train villager?
        (unit-type-count villager >= 7) ;Check count : unit villager
    )
    (current-age == dark-age) ;Check dark-age
=>
    (research ri-loom) ;Research loom
)

;Check dark_age: Train villager
(defrule
    (can-train villager) ;Can train villager?
    (unit-type-count-total villager < 25) ;Check count : unit villager
    (current-age == dark-age) ;Check dark-age
=>
    (train villager) ;Train villager
)