-
Notifications
You must be signed in to change notification settings - Fork 0
08 Manage goal id and goal value
Satoshi Teshiba edited this page Jan 31, 2021
·
1 revision
Goal-id is defined using GoalId class.
Goal value is defined using a derived class of GoalValue.
These instances must be defined using static members of the DefconstCollection derived class.
An instance of DefconstCollection derived class must be set to the return value of the GetDefconsts method of AIFile class.
The following C # code outputs three files.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using LibAoe2AISharp.Framework;
using LibAoe2AISharp.Specifications;
using static Aoe2AISharpSample.SampleGoalDef;
using static LibAoe2AISharp.Framework.EachAgesCommandCollection;
using set_goal = LibAoe2AISharp.Framework.set_goal;
namespace Aoe2AISharpSample
{
class Program
{
static void Main(string[] args)
{
new AIFileSample(
@"C:\Program Files (x86)\Steam\steamapps\common\AoE2DE\resources\_common\ai",
"Author Name"
).OutputFiles();
}
}
public class SampleGoalValue : GoalValue
{
public SampleGoalValue(string constantName)
: base(constantName, $"{constantName} comment")
{
}
}
public class SampleGoalDef : DefconstCollection
{
public static readonly GoalId Goal1 = new GoalId("Goal1", "Goal1 comment");
public static readonly SampleGoalValue Goal1Value1 = new SampleGoalValue("Goal1Value1");
public static readonly SampleGoalValue Goal1Value2 = new SampleGoalValue("Goal1Value2");
public static readonly SampleGoalValue Goal1Value3 = new SampleGoalValue("Goal1Value3");
}
public class AIFileSample : AIFile
{
public AIFileSample(string pathName, string author)
: base(pathName, author, new PerFileSample())
{
}
protected override Collection<DefconstCollection> GetDefconsts()
{
return new Collection<DefconstCollection>() {
new SampleGoalDef(),
};
}
protected override Dictionary<GoalId, GoalValue> GetGoalIdInitValues()
{
return new Dictionary<GoalId, GoalValue>() {
{ Goal1, Goal1Value1}
};
}
}
class PerFileSample : PerFile
{
protected override CommandGroupCollection GetCommandGroups()
{
var darkAgeOnly = new EachAgesCommandCollection(GroupType.DarkAge, "Rules are in the dark age only.") {
new SampleGoalSetRule()
};
return new CommandGroupCollection() {
darkAgeOnly,
};
}
}
public class SampleGoalSetRule : defrule
{
public SampleGoalSetRule()
{
Facts.Add(new goal(Goal1, Goal1Value1));
Actions.Add(new set_goal(Goal1, Goal1Value2));
}
}
}
The AIFileSample.per file contains the following script.
;===============================================================================
;description: User defined defconsts.
; Initialize user definition goal-id.
;===============================================================================
(defconst Goal1 2) ;goal-id[2] Goal1 comment
(defconst Goal1Value1 2) ;goal-id value[2] Goal1Value1 comment
(defconst Goal1Value2 3) ;goal-id value[3] Goal1Value2 comment
(defconst Goal1Value3 4) ;goal-id value[4] Goal1Value3 comment
;Initialize user definition goal-id.
(defrule
(true)
=>
(set-goal Goal1 Goal1Value1)
(disable-self)
)
The AIFileSamplePerFileSample.per file contains the following script.
;===============================================================================
;description: dark_age NotResearching: Rules are in the dark age only.
;common fact: [DarkAge]
; [DarkAge]: SampleGoalSetRule
;===============================================================================
;[DarkAge]: SampleGoalSetRule
(defrule
(goal Goal1 Goal1Value1)
(current-age == dark-age) ;[DarkAge]
=>
(set-goal Goal1 Goal1Value2)
)