@@ -9,6 +9,8 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenRA
|
||||
@@ -50,28 +52,6 @@ namespace OpenRA
|
||||
return new WPos(ret.X, ret.Y, ret.Z + offset);
|
||||
}
|
||||
|
||||
public static WPos Average(params WPos[] list)
|
||||
{
|
||||
if (list == null || list.Length == 0)
|
||||
return WPos.Zero;
|
||||
|
||||
var x = 0;
|
||||
var y = 0;
|
||||
var z = 0;
|
||||
foreach(var pos in list)
|
||||
{
|
||||
x += pos.X;
|
||||
y += pos.Y;
|
||||
z += pos.Z;
|
||||
}
|
||||
|
||||
x /= list.Length;
|
||||
y /= list.Length;
|
||||
z /= list.Length;
|
||||
|
||||
return new WPos(x,y,z);
|
||||
}
|
||||
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
@@ -85,4 +65,30 @@ namespace OpenRA
|
||||
|
||||
public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); }
|
||||
}
|
||||
|
||||
public static class IEnumerableExtensions
|
||||
{
|
||||
public static WPos Average(this IEnumerable<WPos> source)
|
||||
{
|
||||
var length = source.Count();
|
||||
if (length == 0)
|
||||
return WPos.Zero;
|
||||
|
||||
var x = 0L;
|
||||
var y = 0L;
|
||||
var z = 0L;
|
||||
foreach (var pos in source)
|
||||
{
|
||||
x += pos.X;
|
||||
y += pos.Y;
|
||||
z += pos.Z;
|
||||
}
|
||||
|
||||
x /= length;
|
||||
y /= length;
|
||||
z /= length;
|
||||
|
||||
return new WPos((int)x, (int)y, (int)z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace OpenRA.Graphics
|
||||
for (var i = 0; i < length - skip - 4; i++)
|
||||
{
|
||||
var j = next - skip - i - 2;
|
||||
var nextPos = WPos.Average(trail[idx(j)], trail[idx(j-1)], trail[idx(j-2)], trail[idx(j-3)]);
|
||||
var nextPos = Average(trail[idx(j)], trail[idx(j-1)], trail[idx(j-2)], trail[idx(j-3)]);
|
||||
var nextCell = nextPos.ToCPos();
|
||||
var nextColor = Exts.ColorLerp(i * 1f / (length - 4), color, Color.Transparent);
|
||||
|
||||
@@ -90,6 +90,11 @@ namespace OpenRA.Graphics
|
||||
return j < 0 ? j + trail.Length : j;
|
||||
}
|
||||
|
||||
WPos Average(params WPos[] list)
|
||||
{
|
||||
return list.Average();
|
||||
}
|
||||
|
||||
public void Update(WPos pos)
|
||||
{
|
||||
trail[next] = pos;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2012 The OpenRA Developers (see AUTHORS)
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
@@ -11,63 +11,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using AI.Fuzzy.Library;
|
||||
using OpenRA.Mods.RA.Move;
|
||||
using OpenRA.Traits;
|
||||
using AI.Fuzzy.Library;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
class AttackOrFleeFuzzy
|
||||
{
|
||||
protected MamdaniFuzzySystem fuzzyEngine;
|
||||
protected Dictionary<FuzzyVariable, double> result;
|
||||
|
||||
public bool CanAttack
|
||||
{
|
||||
get
|
||||
{
|
||||
//not sure that this will happen (NaN), it's for the safety of
|
||||
if (result[fuzzyEngine.OutputByName("AttackOrFlee")].ToString() != "NaN")
|
||||
return result[fuzzyEngine.OutputByName("AttackOrFlee")] < 30.0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
MamdaniFuzzySystem fuzzyEngine;
|
||||
|
||||
public AttackOrFleeFuzzy()
|
||||
{
|
||||
InitializateFuzzyVariables();
|
||||
}
|
||||
|
||||
protected void AddFuzzyRule(string rule)
|
||||
{
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule(rule));
|
||||
}
|
||||
|
||||
protected void InitializateFuzzyVariables()
|
||||
{
|
||||
fuzzyEngine = new MamdaniFuzzySystem();
|
||||
|
||||
FuzzyVariable playerHealthFuzzy = new FuzzyVariable("OwnHealth", 0.0, 100.0);
|
||||
var playerHealthFuzzy = new FuzzyVariable("OwnHealth", 0.0, 100.0);
|
||||
playerHealthFuzzy.Terms.Add(new FuzzyTerm("NearDead", new TrapezoidMembershipFunction(0, 0, 20, 40)));
|
||||
playerHealthFuzzy.Terms.Add(new FuzzyTerm("Injured", new TrapezoidMembershipFunction(30, 50, 50, 70)));
|
||||
playerHealthFuzzy.Terms.Add(new FuzzyTerm("Normal", new TrapezoidMembershipFunction(50, 80, 100, 100)));
|
||||
fuzzyEngine.Input.Add(playerHealthFuzzy);
|
||||
|
||||
FuzzyVariable enemyHealthFuzzy = new FuzzyVariable("EnemyHealth", 0.0, 100.0);
|
||||
var enemyHealthFuzzy = new FuzzyVariable("EnemyHealth", 0.0, 100.0);
|
||||
enemyHealthFuzzy.Terms.Add(new FuzzyTerm("NearDead", new TrapezoidMembershipFunction(0, 0, 20, 40)));
|
||||
enemyHealthFuzzy.Terms.Add(new FuzzyTerm("Injured", new TrapezoidMembershipFunction(30, 50, 50, 70)));
|
||||
enemyHealthFuzzy.Terms.Add(new FuzzyTerm("Normal", new TrapezoidMembershipFunction(50, 80, 100, 100)));
|
||||
fuzzyEngine.Input.Add(enemyHealthFuzzy);
|
||||
|
||||
FuzzyVariable relativeAttackPowerFuzzy = new FuzzyVariable("RelativeAttackPower", 0.0, 1000.0);
|
||||
var relativeAttackPowerFuzzy = new FuzzyVariable("RelativeAttackPower", 0.0, 1000.0);
|
||||
relativeAttackPowerFuzzy.Terms.Add(new FuzzyTerm("Weak", new TrapezoidMembershipFunction(0, 0, 70, 90)));
|
||||
relativeAttackPowerFuzzy.Terms.Add(new FuzzyTerm("Equal", new TrapezoidMembershipFunction(85, 100, 100, 115)));
|
||||
relativeAttackPowerFuzzy.Terms.Add(new FuzzyTerm("Strong", new TrapezoidMembershipFunction(110, 150, 150, 1000)));
|
||||
fuzzyEngine.Input.Add(relativeAttackPowerFuzzy);
|
||||
|
||||
FuzzyVariable relativeSpeedFuzzy = new FuzzyVariable("RelativeSpeed", 0.0, 1000.0);
|
||||
var relativeSpeedFuzzy = new FuzzyVariable("RelativeSpeed", 0.0, 1000.0);
|
||||
relativeSpeedFuzzy.Terms.Add(new FuzzyTerm("Slow", new TrapezoidMembershipFunction(0, 0, 70, 90)));
|
||||
relativeSpeedFuzzy.Terms.Add(new FuzzyTerm("Equal", new TrapezoidMembershipFunction(85, 100, 100, 115)));
|
||||
relativeSpeedFuzzy.Terms.Add(new FuzzyTerm("Fast", new TrapezoidMembershipFunction(110, 150, 150, 1000)));
|
||||
fuzzyEngine.Input.Add(relativeSpeedFuzzy);
|
||||
|
||||
FuzzyVariable attackOrFleeFuzzy = new FuzzyVariable("AttackOrFlee", 0.0, 50.0);
|
||||
var attackOrFleeFuzzy = new FuzzyVariable("AttackOrFlee", 0.0, 50.0);
|
||||
attackOrFleeFuzzy.Terms.Add(new FuzzyTerm("Attack", new TrapezoidMembershipFunction(0, 15, 15, 30)));
|
||||
attackOrFleeFuzzy.Terms.Add(new FuzzyTerm("Flee", new TrapezoidMembershipFunction(25, 35, 35, 50)));
|
||||
fuzzyEngine.Output.Add(attackOrFleeFuzzy);
|
||||
@@ -79,123 +71,124 @@ namespace OpenRA.Mods.RA.AI
|
||||
|
||||
protected virtual void AddingRulesForNormalOwnHealth()
|
||||
{
|
||||
//1 OwnHealth is Normal
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Normal) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Normal) " +
|
||||
"and ((EnemyHealth is NearDead) or (EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and ((RelativeAttackPower is Weak) or (RelativeAttackPower is Equal) or (RelativeAttackPower is Strong)) " +
|
||||
"and ((RelativeSpeed is Slow) or (RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
"then AttackOrFlee is Attack");
|
||||
}
|
||||
|
||||
protected virtual void AddingRulesForInjuredOwnHealth()
|
||||
{
|
||||
//OwnHealth is Injured
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Injured) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Injured) " +
|
||||
"and (EnemyHealth is NearDead) " +
|
||||
"and ((RelativeAttackPower is Weak) or (RelativeAttackPower is Equal) or (RelativeAttackPower is Strong)) " +
|
||||
"and ((RelativeSpeed is Slow) or (RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
"then AttackOrFlee is Attack");
|
||||
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Injured) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Injured) " +
|
||||
"and ((EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and ((RelativeAttackPower is Equal) or (RelativeAttackPower is Strong)) " +
|
||||
"and ((RelativeSpeed is Slow) or (RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
"then AttackOrFlee is Attack");
|
||||
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Injured) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Injured) " +
|
||||
"and ((EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and (RelativeAttackPower is Weak) " +
|
||||
"and (RelativeSpeed is Slow)) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
"then AttackOrFlee is Attack");
|
||||
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Injured) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Injured) " +
|
||||
"and ((EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and (RelativeAttackPower is Weak) " +
|
||||
"and ((RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
"then AttackOrFlee is Flee");
|
||||
|
||||
//2
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Injured) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Injured) " +
|
||||
"and ((EnemyHealth is NearDead) or (EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and ((RelativeAttackPower is Weak) or (RelativeAttackPower is Equal) or (RelativeAttackPower is Strong)) " +
|
||||
"and (RelativeSpeed is Slow)) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
"then AttackOrFlee is Attack");
|
||||
}
|
||||
|
||||
protected virtual void AddingRulesForNearDeadOwnHealth()
|
||||
{
|
||||
//3 OwnHealth is NearDead
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is NearDead) " +
|
||||
AddFuzzyRule("if ((OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is Injured) " +
|
||||
"and (RelativeAttackPower is Equal) " +
|
||||
"and ((RelativeSpeed is Slow) or (RelativeSpeed is Equal))) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
//4
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is NearDead) " +
|
||||
"then AttackOrFlee is Attack");
|
||||
|
||||
AddFuzzyRule("if ((OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is NearDead) " +
|
||||
"and (RelativeAttackPower is Weak) " +
|
||||
"and ((RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
//5
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is NearDead) " +
|
||||
"then AttackOrFlee is Flee");
|
||||
|
||||
AddFuzzyRule("if ((OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is Injured) " +
|
||||
"and (RelativeAttackPower is Weak) " +
|
||||
"and ((RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
"then AttackOrFlee is Flee");
|
||||
|
||||
//6
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is NearDead) " +
|
||||
AddFuzzyRule("if ((OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is Normal) " +
|
||||
"and (RelativeAttackPower is Weak) " +
|
||||
"and ((RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
"then AttackOrFlee is Flee");
|
||||
|
||||
//7
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if (OwnHealth is NearDead) " +
|
||||
AddFuzzyRule("if (OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is Normal) " +
|
||||
"and (RelativeAttackPower is Equal) " +
|
||||
"and (RelativeSpeed is Fast) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
//8
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if (OwnHealth is NearDead) " +
|
||||
"then AttackOrFlee is Flee");
|
||||
|
||||
AddFuzzyRule("if (OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is Normal) " +
|
||||
"and (RelativeAttackPower is Strong) " +
|
||||
"and (RelativeSpeed is Fast) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
"then AttackOrFlee is Flee");
|
||||
|
||||
//9
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if (OwnHealth is NearDead) " +
|
||||
AddFuzzyRule("if (OwnHealth is NearDead) " +
|
||||
"and (EnemyHealth is Injured) " +
|
||||
"and (RelativeAttackPower is Equal) " +
|
||||
"and (RelativeSpeed is Fast) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
"then AttackOrFlee is Flee");
|
||||
}
|
||||
public void CalculateFuzzy(List<Actor> ownUnits, List<Actor> enemyUnits)
|
||||
|
||||
public bool CanAttack(IEnumerable<Actor> ownUnits, IEnumerable<Actor> enemyUnits)
|
||||
{
|
||||
Dictionary<FuzzyVariable, double> inputValues = new Dictionary<FuzzyVariable, double>();
|
||||
var inputValues = new Dictionary<FuzzyVariable, double>();
|
||||
inputValues.Add(fuzzyEngine.InputByName("OwnHealth"), (double)NormalizedHealth(ownUnits, 100));
|
||||
inputValues.Add(fuzzyEngine.InputByName("EnemyHealth"), (double)NormalizedHealth(enemyUnits, 100));
|
||||
inputValues.Add(fuzzyEngine.InputByName("RelativeAttackPower"), (double)RelativePower(ownUnits, enemyUnits));
|
||||
inputValues.Add(fuzzyEngine.InputByName("RelativeSpeed"), (double)RelativeSpeed(ownUnits, enemyUnits));
|
||||
|
||||
result = fuzzyEngine.Calculate(inputValues);
|
||||
var result = fuzzyEngine.Calculate(inputValues);
|
||||
var attackChance = result[fuzzyEngine.OutputByName("AttackOrFlee")];
|
||||
return !double.IsNaN(attackChance) && attackChance < 30.0;
|
||||
}
|
||||
|
||||
protected float NormalizedHealth(List<Actor> actors, float normalizeByValue)
|
||||
protected float NormalizedHealth(IEnumerable<Actor> actors, float normalizeByValue)
|
||||
{
|
||||
int sumOfMaxHp = 0;
|
||||
int sumOfHp = 0;
|
||||
var sumOfMaxHp = 0;
|
||||
var sumOfHp = 0;
|
||||
foreach (var a in actors)
|
||||
{
|
||||
if (a.HasTrait<Health>())
|
||||
{
|
||||
sumOfMaxHp += a.Trait<Health>().MaxHP;
|
||||
sumOfHp += a.Trait<Health>().HP;
|
||||
}
|
||||
if (sumOfMaxHp == 0) return 0.0f;
|
||||
}
|
||||
|
||||
if (sumOfMaxHp == 0)
|
||||
return 0.0f;
|
||||
|
||||
return (sumOfHp * normalizeByValue) / sumOfMaxHp;
|
||||
}
|
||||
|
||||
protected float RelativePower(List<Actor> own, List<Actor> enemy)
|
||||
protected float RelativePower(IEnumerable<Actor> own, IEnumerable<Actor> enemy)
|
||||
{
|
||||
return RelativeValue(own, enemy, 100, SumOfValues<AttackBase>, (Actor a) =>
|
||||
{
|
||||
@@ -208,43 +201,50 @@ namespace OpenRA.Mods.RA.AI
|
||||
});
|
||||
}
|
||||
|
||||
protected float RelativeSpeed(List<Actor> own, List<Actor> enemy)
|
||||
protected float RelativeSpeed(IEnumerable<Actor> own, IEnumerable<Actor> enemy)
|
||||
{
|
||||
return RelativeValue(own, enemy, 100, Average<Mobile>, (Actor a) => a.Trait<Mobile>().Info.Speed);
|
||||
}
|
||||
|
||||
protected float RelativeValue(List<Actor> own, List<Actor> enemy, float normalizeByValue,
|
||||
Func<List<Actor>, Func<Actor, int>, float> relativeFunc, Func<Actor, int> getValue)
|
||||
protected float RelativeValue(IEnumerable<Actor> own, IEnumerable<Actor> enemy, float normalizeByValue,
|
||||
Func<IEnumerable<Actor>, Func<Actor, int>, float> relativeFunc, Func<Actor, int> getValue)
|
||||
{
|
||||
if (enemy.Count == 0)
|
||||
if (!enemy.Any())
|
||||
return 999.0f;
|
||||
if (own.Count == 0)
|
||||
|
||||
if (!own.Any())
|
||||
return 0.0f;
|
||||
|
||||
float relative = (relativeFunc(own, getValue) / relativeFunc(enemy, getValue)) * normalizeByValue;
|
||||
var relative = (relativeFunc(own, getValue) / relativeFunc(enemy, getValue)) * normalizeByValue;
|
||||
return relative.Clamp(0.0f, 999.0f);
|
||||
}
|
||||
|
||||
protected float SumOfValues<Trait>(List<Actor> actors, Func<Actor, int> getValue)
|
||||
protected float SumOfValues<Trait>(IEnumerable<Actor> actors, Func<Actor, int> getValue)
|
||||
{
|
||||
int sum = 0;
|
||||
var sum = 0;
|
||||
foreach (var a in actors)
|
||||
if (a.HasTrait<Trait>())
|
||||
sum += getValue(a);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
protected float Average<Trait>(List<Actor> actors, Func<Actor, int> getValue)
|
||||
protected float Average<Trait>(IEnumerable<Actor> actors, Func<Actor, int> getValue)
|
||||
{
|
||||
int sum = 0;
|
||||
int countActors = 0;
|
||||
var sum = 0;
|
||||
var countActors = 0;
|
||||
foreach (var a in actors)
|
||||
{
|
||||
if (a.HasTrait<Trait>())
|
||||
{
|
||||
sum += getValue(a);
|
||||
countActors++;
|
||||
}
|
||||
if (countActors == 0) return 0.0f;
|
||||
}
|
||||
|
||||
if (countActors == 0)
|
||||
return 0.0f;
|
||||
|
||||
return sum / countActors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
@@ -9,13 +9,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Mods.RA.Buildings;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Mods.RA.Activities;
|
||||
using XRandom = OpenRA.Thirdparty.Random;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
@@ -39,7 +33,7 @@ namespace OpenRA.Mods.RA.AI
|
||||
public void Tick()
|
||||
{
|
||||
// Pick a free queue
|
||||
var queue = ai.FindQueues( category ).FirstOrDefault();
|
||||
var queue = ai.FindQueues(category).FirstOrDefault();
|
||||
if (queue == null)
|
||||
return;
|
||||
|
||||
@@ -47,7 +41,6 @@ namespace OpenRA.Mods.RA.AI
|
||||
switch (state)
|
||||
{
|
||||
case BuildState.ChooseItem:
|
||||
{
|
||||
var item = chooseItem(queue);
|
||||
if (item == null)
|
||||
{
|
||||
@@ -60,24 +53,24 @@ namespace OpenRA.Mods.RA.AI
|
||||
state = BuildState.WaitForProduction;
|
||||
ai.world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BuildState.WaitForProduction:
|
||||
if (currentBuilding == null) return; /* let it happen.. */
|
||||
if (currentBuilding == null)
|
||||
return;
|
||||
|
||||
else if (currentBuilding.Paused)
|
||||
if (currentBuilding.Paused)
|
||||
ai.world.IssueOrder(Order.PauseProduction(queue.self, currentBuilding.Item, false));
|
||||
else if (currentBuilding.Done)
|
||||
{
|
||||
state = BuildState.WaitForFeedback;
|
||||
lastThinkTick = ai.ticks;
|
||||
|
||||
/* place the building */
|
||||
BuildingType type = BuildingType.Building;
|
||||
if(Rules.Info[currentBuilding.Item].Traits.Contains<AttackBaseInfo>())
|
||||
// Place the building
|
||||
var type = BuildingType.Building;
|
||||
if (Rules.Info[currentBuilding.Item].Traits.Contains<AttackBaseInfo>())
|
||||
type = BuildingType.Defense;
|
||||
else if(Rules.Info[currentBuilding.Item].Traits.Contains<OreRefineryInfo>())
|
||||
else if (Rules.Info[currentBuilding.Item].Traits.Contains<OreRefineryInfo>())
|
||||
type = BuildingType.Refinery;
|
||||
|
||||
var location = ai.ChooseBuildLocation(currentBuilding.Item, type);
|
||||
@@ -95,6 +88,7 @@ namespace OpenRA.Mods.RA.AI
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case BuildState.WaitForFeedback:
|
||||
@@ -105,4 +99,3 @@ namespace OpenRA.Mods.RA.AI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
@@ -8,12 +8,6 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using AI.Fuzzy.Library;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
class RushFuzzy : AttackOrFleeFuzzy
|
||||
@@ -22,18 +16,17 @@ namespace OpenRA.Mods.RA.AI
|
||||
|
||||
protected override void AddingRulesForNormalOwnHealth()
|
||||
{
|
||||
//1 OwnHealth is Normal
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Normal) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Normal) " +
|
||||
"and ((EnemyHealth is NearDead) or (EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and (RelativeAttackPower is Strong) " +
|
||||
"and ((RelativeSpeed is Slow) or (RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Attack"));
|
||||
"then AttackOrFlee is Attack");
|
||||
|
||||
fuzzyEngine.Rules.Add(fuzzyEngine.ParseRule("if ((OwnHealth is Normal) " +
|
||||
AddFuzzyRule("if ((OwnHealth is Normal) " +
|
||||
"and ((EnemyHealth is NearDead) or (EnemyHealth is Injured) or (EnemyHealth is Normal)) " +
|
||||
"and ((RelativeAttackPower is Weak) or (RelativeAttackPower is Equal)) " +
|
||||
"and ((RelativeSpeed is Slow) or (RelativeSpeed is Equal) or (RelativeSpeed is Fast))) " +
|
||||
"then AttackOrFlee is Flee"));
|
||||
"then AttackOrFlee is Flee");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
84
OpenRA.Mods.RA/AI/Squad.cs
Normal file
84
OpenRA.Mods.RA/AI/Squad.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Traits;
|
||||
using XRandom = OpenRA.Thirdparty.Random;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
public enum SquadType { Assault, Air, Rush, Protection }
|
||||
|
||||
public class Squad
|
||||
{
|
||||
public List<Actor> units = new List<Actor>();
|
||||
public SquadType type;
|
||||
|
||||
internal World world;
|
||||
internal HackyAI bot;
|
||||
internal XRandom random;
|
||||
|
||||
internal Target target;
|
||||
internal StateMachine fsm;
|
||||
|
||||
//fuzzy
|
||||
internal AttackOrFleeFuzzy attackOrFleeFuzzy = new AttackOrFleeFuzzy();
|
||||
|
||||
public Squad(HackyAI bot, SquadType type) : this(bot, type, null) { }
|
||||
|
||||
public Squad(HackyAI bot, SquadType type, Actor target)
|
||||
{
|
||||
this.bot = bot;
|
||||
this.world = bot.world;
|
||||
this.random = bot.random;
|
||||
this.type = type;
|
||||
this.target = Traits.Target.FromActor(target);
|
||||
fsm = new StateMachine();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case SquadType.Assault:
|
||||
case SquadType.Rush:
|
||||
fsm.ChangeState(this, new GroundUnitsIdleState(), true);
|
||||
break;
|
||||
case SquadType.Air:
|
||||
fsm.ChangeState(this, new AirIdleState(), true);
|
||||
break;
|
||||
case SquadType.Protection:
|
||||
fsm.ChangeState(this, new UnitsForProtectionIdleState(), true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (IsValid)
|
||||
fsm.Update(this);
|
||||
}
|
||||
|
||||
public bool IsValid { get { return units.Any(); } }
|
||||
|
||||
public Actor Target
|
||||
{
|
||||
get { return target.Actor; }
|
||||
set { target = Traits.Target.FromActor(value); }
|
||||
}
|
||||
|
||||
public bool TargetIsValid
|
||||
{
|
||||
get { return target.IsValidFor(units.FirstOrDefault()) && !target.Actor.HasTrait<Husk>(); }
|
||||
}
|
||||
|
||||
public WPos CenterPosition { get { return units.Select(u => u.CenterPosition).Average(); } }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
@@ -12,68 +12,38 @@ namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
class StateMachine
|
||||
{
|
||||
//a pointer to the agent that owns this instance
|
||||
private Squad owner;
|
||||
IState currentState;
|
||||
IState previousState;
|
||||
|
||||
private IState currentState;
|
||||
|
||||
//a record of the last state the agent was in
|
||||
private IState previousState;
|
||||
|
||||
public StateMachine(Squad owner)
|
||||
public void Update(Squad squad)
|
||||
{
|
||||
this.owner = owner;
|
||||
currentState.Tick(squad);
|
||||
}
|
||||
|
||||
public IState CurrentState
|
||||
public void ChangeState(Squad squad, IState newState, bool rememberPrevious)
|
||||
{
|
||||
get { return currentState; }
|
||||
set { currentState = value; }
|
||||
}
|
||||
|
||||
public IState PreviousState
|
||||
{
|
||||
get { return previousState; }
|
||||
set { previousState = value; }
|
||||
}
|
||||
|
||||
//call this to update the FSM
|
||||
public void UpdateFsm()
|
||||
{
|
||||
currentState.Execute(owner);
|
||||
}
|
||||
|
||||
//change to a new state
|
||||
//boolean variable isSaveCurrentState respons on save or not current state
|
||||
public void ChangeState(IState newState, bool saveCurrentState)
|
||||
{
|
||||
if (saveCurrentState)
|
||||
//keep a record of the previous state
|
||||
if (rememberPrevious)
|
||||
previousState = currentState;
|
||||
|
||||
//call the exit method of the existing state
|
||||
if(currentState != null)
|
||||
currentState.Exit(owner);
|
||||
if (currentState != null)
|
||||
currentState.Deactivate(squad);
|
||||
|
||||
//change state to the new state
|
||||
if (newState != null)
|
||||
currentState = newState;
|
||||
|
||||
//call the entry method of the new state
|
||||
currentState.Enter(owner);
|
||||
currentState.Activate(squad);
|
||||
}
|
||||
|
||||
//change state back to the previous state
|
||||
public void RevertToPreviousState(bool saveCurrentState)
|
||||
public void RevertToPreviousState(Squad squad, bool saveCurrentState)
|
||||
{
|
||||
ChangeState(previousState, saveCurrentState);
|
||||
ChangeState(squad, previousState, saveCurrentState);
|
||||
}
|
||||
}
|
||||
|
||||
interface IState
|
||||
{
|
||||
void Enter(Squad bot);
|
||||
void Execute(Squad bot);
|
||||
void Exit(Squad bot);
|
||||
void Activate(Squad bot);
|
||||
void Tick(Squad bot);
|
||||
void Deactivate(Squad bot);
|
||||
}
|
||||
}
|
||||
|
||||
258
OpenRA.Mods.RA/AI/States/AirStates.cs
Normal file
258
OpenRA.Mods.RA/AI/States/AirStates.cs
Normal file
@@ -0,0 +1,258 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.RA.Air;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
abstract class AirStateBase : StateBase
|
||||
{
|
||||
protected const int MissileUnitMultiplier = 3;
|
||||
|
||||
protected static int CountAntiAirUnits(IEnumerable<Actor> units)
|
||||
{
|
||||
if (!units.Any())
|
||||
return 0;
|
||||
|
||||
var missileUnitsCount = 0;
|
||||
foreach (var unit in units)
|
||||
{
|
||||
if (unit != null && unit.HasTrait<AttackBase>() && !unit.HasTrait<Aircraft>()
|
||||
&& !unit.IsDisabled())
|
||||
{
|
||||
var arms = unit.TraitsImplementing<Armament>();
|
||||
foreach (var a in arms)
|
||||
{
|
||||
if (a.Weapon.ValidTargets.Contains("Air"))
|
||||
{
|
||||
missileUnitsCount++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return missileUnitsCount;
|
||||
}
|
||||
|
||||
protected static Actor FindDefenselessTarget(Squad owner)
|
||||
{
|
||||
Actor target = null;
|
||||
FindSafePlace(owner, out target, true);
|
||||
return target;
|
||||
}
|
||||
|
||||
protected static CPos? FindSafePlace(Squad owner, out Actor detectedEnemyTarget, bool needTarget)
|
||||
{
|
||||
var world = owner.world;
|
||||
detectedEnemyTarget = null;
|
||||
var x = (world.Map.MapSize.X % DangerRadius) == 0 ? world.Map.MapSize.X : world.Map.MapSize.X + DangerRadius;
|
||||
var y = (world.Map.MapSize.Y % DangerRadius) == 0 ? world.Map.MapSize.Y : world.Map.MapSize.Y + DangerRadius;
|
||||
|
||||
for (var i = 0; i < x; i += DangerRadius * 2)
|
||||
{
|
||||
for (var j = 0; j < y; j += DangerRadius * 2)
|
||||
{
|
||||
var pos = new CPos(i, j);
|
||||
if (NearToPosSafely(owner, pos.CenterPosition, out detectedEnemyTarget))
|
||||
{
|
||||
if (needTarget && detectedEnemyTarget == null)
|
||||
continue;
|
||||
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static bool NearToPosSafely(Squad owner, WPos loc)
|
||||
{
|
||||
Actor a;
|
||||
return NearToPosSafely(owner, loc, out a);
|
||||
}
|
||||
|
||||
protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedEnemyTarget)
|
||||
{
|
||||
detectedEnemyTarget = null;
|
||||
var unitsAroundPos = owner.world.FindActorsInCircle(loc, WRange.FromCells(DangerRadius))
|
||||
.Where(unit => owner.bot.p.Stances[unit.Owner] == Stance.Enemy).ToList();
|
||||
|
||||
if (!unitsAroundPos.Any())
|
||||
return true;
|
||||
|
||||
if (CountAntiAirUnits(unitsAroundPos) * MissileUnitMultiplier < owner.units.Count)
|
||||
{
|
||||
detectedEnemyTarget = unitsAroundPos.Random(owner.random);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static bool FullAmmo(Actor a)
|
||||
{
|
||||
var limitedAmmo = a.TraitOrDefault<LimitedAmmo>();
|
||||
return limitedAmmo != null && limitedAmmo.FullAmmo();
|
||||
}
|
||||
|
||||
protected static bool HasAmmo(Actor a)
|
||||
{
|
||||
var limitedAmmo = a.TraitOrDefault<LimitedAmmo>();
|
||||
return limitedAmmo != null && limitedAmmo.HasAmmo();
|
||||
}
|
||||
|
||||
protected static bool ReloadsAutomatically(Actor a)
|
||||
{
|
||||
return a.HasTrait<Reloads>();
|
||||
}
|
||||
|
||||
protected static bool IsRearm(Actor a)
|
||||
{
|
||||
var activity = a.GetCurrentActivity();
|
||||
if (activity == null)
|
||||
return false;
|
||||
|
||||
var type = activity.GetType();
|
||||
if (type == typeof(OpenRA.Mods.RA.Activities.Rearm) || type == typeof(ResupplyAircraft))
|
||||
return true;
|
||||
|
||||
var next = activity.NextActivity;
|
||||
if (next == null)
|
||||
return false;
|
||||
|
||||
var nextType = next.GetType();
|
||||
if (nextType == typeof(OpenRA.Mods.RA.Activities.Rearm) || nextType == typeof(ResupplyAircraft))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Checks the number of anti air enemies around units
|
||||
protected virtual bool ShouldFlee(Squad owner)
|
||||
{
|
||||
return base.ShouldFlee(owner, enemies => CountAntiAirUnits(enemies) * MissileUnitMultiplier > owner.units.Count);
|
||||
}
|
||||
}
|
||||
|
||||
class AirIdleState : AirStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new AirFleeState(), true);
|
||||
return;
|
||||
}
|
||||
|
||||
var e = FindDefenselessTarget(owner);
|
||||
if (e == null)
|
||||
return;
|
||||
|
||||
owner.Target = e;
|
||||
owner.fsm.ChangeState(owner, new AirAttackState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class AirAttackState : AirStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.TargetIsValid)
|
||||
{
|
||||
var a = owner.units.Random(owner.random);
|
||||
var closestEnemy = owner.bot.FindClosestEnemy(a.CenterPosition);
|
||||
if (closestEnemy != null)
|
||||
owner.Target = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new AirFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!NearToPosSafely(owner, owner.Target.CenterPosition))
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new AirFleeState(), true);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var a in owner.units)
|
||||
{
|
||||
if (BusyAttack(a))
|
||||
continue;
|
||||
|
||||
if (!ReloadsAutomatically(a))
|
||||
{
|
||||
if (!HasAmmo(a))
|
||||
{
|
||||
if (IsRearm(a))
|
||||
continue;
|
||||
owner.world.IssueOrder(new Order("ReturnToBase", a, false));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IsRearm(a))
|
||||
continue;
|
||||
}
|
||||
|
||||
if (owner.Target.HasTrait<ITargetable>() && CanAttackTarget(a, owner.Target))
|
||||
owner.world.IssueOrder(new Order("Attack", a, false) { TargetActor = owner.Target });
|
||||
}
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class AirFleeState : AirStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
foreach (var a in owner.units)
|
||||
{
|
||||
if (!ReloadsAutomatically(a) && !FullAmmo(a))
|
||||
{
|
||||
if (IsRearm(a))
|
||||
continue;
|
||||
|
||||
owner.world.IssueOrder(new Order("ReturnToBase", a, false));
|
||||
continue;
|
||||
}
|
||||
|
||||
owner.world.IssueOrder(new Order("Move", a, false) { TargetLocation = RandomBuildingLocation(owner) });
|
||||
}
|
||||
|
||||
owner.fsm.ChangeState(owner, new AirIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
}
|
||||
171
OpenRA.Mods.RA/AI/States/GroundStates.cs
Normal file
171
OpenRA.Mods.RA/AI/States/GroundStates.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
abstract class GroundStateBase : StateBase
|
||||
{
|
||||
protected virtual bool ShouldFlee(Squad owner)
|
||||
{
|
||||
return base.ShouldFlee(owner, enemies => !owner.attackOrFleeFuzzy.CanAttack(owner.units, enemies));
|
||||
}
|
||||
}
|
||||
|
||||
class GroundUnitsIdleState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.TargetIsValid)
|
||||
{
|
||||
var t = owner.bot.FindClosestEnemy(owner.units.FirstOrDefault().CenterPosition);
|
||||
if (t == null) return;
|
||||
owner.Target = t;
|
||||
}
|
||||
|
||||
var enemyUnits = owner.world.FindActorsInCircle(owner.Target.CenterPosition, WRange.FromCells(10))
|
||||
.Where(unit => owner.bot.p.Stances[unit.Owner] == Stance.Enemy).ToList();
|
||||
|
||||
if (enemyUnits.Any())
|
||||
{
|
||||
if (owner.attackOrFleeFuzzy.CanAttack(owner.units, enemyUnits))
|
||||
{
|
||||
foreach (var u in owner.units)
|
||||
owner.world.IssueOrder(new Order("AttackMove", u, false) { TargetLocation = owner.Target.CenterPosition.ToCPos() });
|
||||
|
||||
// We have gathered sufficient units. Attack the nearest enemy unit.
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsAttackMoveState(), true);
|
||||
return;
|
||||
}
|
||||
else
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
}
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class GroundUnitsAttackMoveState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.TargetIsValid)
|
||||
{
|
||||
var closestEnemy = owner.bot.FindClosestEnemy(owner.units.Random(owner.random).CenterPosition);
|
||||
if (closestEnemy != null)
|
||||
owner.Target = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Actor leader = owner.units.ClosestTo(owner.Target.CenterPosition);
|
||||
if (leader == null)
|
||||
return;
|
||||
var ownUnits = owner.world.FindActorsInCircle(leader.CenterPosition, WRange.FromCells(owner.units.Count) / 3)
|
||||
.Where(a => a.Owner == owner.units.FirstOrDefault().Owner && owner.units.Contains(a)).ToList();
|
||||
if (ownUnits.Count < owner.units.Count)
|
||||
{
|
||||
owner.world.IssueOrder(new Order("Stop", leader, false));
|
||||
foreach (var unit in owner.units.Where(a => !ownUnits.Contains(a)))
|
||||
owner.world.IssueOrder(new Order("AttackMove", unit, false) { TargetLocation = leader.CenterPosition.ToCPos() });
|
||||
}
|
||||
else
|
||||
{
|
||||
var enemys = owner.world.FindActorsInCircle(leader.CenterPosition, WRange.FromCells(12))
|
||||
.Where(a1 => !a1.Destroyed && !a1.IsDead()).ToList();
|
||||
var enemynearby = enemys.Where(a1 => a1.HasTrait<ITargetable>() && leader.Owner.Stances[a1.Owner] == Stance.Enemy).ToList();
|
||||
if (enemynearby.Any())
|
||||
{
|
||||
owner.Target = enemynearby.ClosestTo(leader.CenterPosition);
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsAttackState(), true);
|
||||
return;
|
||||
}
|
||||
else
|
||||
foreach (var a in owner.units)
|
||||
owner.world.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.Target.Location });
|
||||
}
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class GroundUnitsAttackState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.TargetIsValid)
|
||||
{
|
||||
var closestEnemy = owner.bot.FindClosestEnemy(owner.units.Random(owner.random).CenterPosition);
|
||||
if (closestEnemy != null)
|
||||
owner.Target = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var a in owner.units)
|
||||
if (!BusyAttack(a))
|
||||
owner.world.IssueOrder(new Order("Attack", a, false) { TargetActor = owner.bot.FindClosestEnemy(a.CenterPosition) });
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class GroundUnitsFleeState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
GoToRandomOwnBuilding(owner);
|
||||
owner.fsm.ChangeState(owner, new GroundUnitsIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { owner.units.Clear(); }
|
||||
}
|
||||
}
|
||||
66
OpenRA.Mods.RA/AI/States/ProtectionStates.cs
Normal file
66
OpenRA.Mods.RA/AI/States/ProtectionStates.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
class UnitsForProtectionIdleState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
public void Tick(Squad owner) { owner.fsm.ChangeState(owner, new UnitsForProtectionAttackState(), true); }
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class UnitsForProtectionAttackState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.TargetIsValid)
|
||||
{
|
||||
owner.Target = owner.bot.FindClosestEnemy(owner.CenterPosition, WRange.FromCells(8));
|
||||
|
||||
if (owner.Target == null)
|
||||
{
|
||||
owner.fsm.ChangeState(owner, new UnitsForProtectionFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var a in owner.units)
|
||||
owner.world.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.Target.Location });
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class UnitsForProtectionFleeState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
GoToRandomOwnBuilding(owner);
|
||||
owner.fsm.ChangeState(owner, new UnitsForProtectionIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { owner.units.Clear(); }
|
||||
}
|
||||
}
|
||||
96
OpenRA.Mods.RA/AI/States/StateBase.cs
Normal file
96
OpenRA.Mods.RA/AI/States/StateBase.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.RA.Air;
|
||||
using OpenRA.Mods.RA.Buildings;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA.AI
|
||||
{
|
||||
abstract class StateBase
|
||||
{
|
||||
protected const int DangerRadius = 10;
|
||||
|
||||
protected static void GoToRandomOwnBuilding(Squad squad)
|
||||
{
|
||||
var loc = RandomBuildingLocation(squad);
|
||||
foreach (var a in squad.units)
|
||||
squad.world.IssueOrder(new Order("Move", a, false) { TargetLocation = loc });
|
||||
}
|
||||
|
||||
protected static CPos RandomBuildingLocation(Squad squad)
|
||||
{
|
||||
var location = squad.bot.baseCenter;
|
||||
var buildings = squad.world.ActorsWithTrait<Building>()
|
||||
.Where(a => a.Actor.Owner == squad.bot.p).Select(a => a.Actor).ToArray();
|
||||
if (buildings.Length > 0)
|
||||
location = buildings.Random(squad.random).Location;
|
||||
return location;
|
||||
}
|
||||
|
||||
protected static bool BusyAttack(Actor a)
|
||||
{
|
||||
if (a.IsIdle)
|
||||
return false;
|
||||
|
||||
var type = a.GetCurrentActivity().GetType();
|
||||
if (type == typeof(OpenRA.Mods.RA.Activities.Attack) || type == typeof(FlyAttack))
|
||||
return true;
|
||||
|
||||
var next = a.GetCurrentActivity().NextActivity;
|
||||
if (next == null)
|
||||
return false;
|
||||
|
||||
var nextType = a.GetCurrentActivity().NextActivity.GetType();
|
||||
if (nextType == typeof(OpenRA.Mods.RA.Activities.Attack) || nextType == typeof(FlyAttack))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static bool CanAttackTarget(Actor a, Actor target)
|
||||
{
|
||||
if (!a.HasTrait<AttackBase>())
|
||||
return false;
|
||||
|
||||
var targetable = target.TraitOrDefault<ITargetable>();
|
||||
if (targetable == null)
|
||||
return false;
|
||||
|
||||
var arms = a.TraitsImplementing<Armament>();
|
||||
foreach (var arm in arms)
|
||||
if (arm.Weapon.ValidTargets.Intersect(targetable.TargetTypes) != null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected virtual bool ShouldFlee(Squad squad, Func<IEnumerable<Actor>, bool> flee)
|
||||
{
|
||||
if (!squad.IsValid)
|
||||
return false;
|
||||
|
||||
var u = squad.units.Random(squad.random);
|
||||
var units = squad.world.FindActorsInCircle(u.CenterPosition, WRange.FromCells(DangerRadius)).ToList();
|
||||
var ownBaseBuildingAround = units.Where(unit => unit.Owner == squad.bot.p && unit.HasTrait<Building>());
|
||||
if (ownBaseBuildingAround.Any())
|
||||
return false;
|
||||
|
||||
var enemyAroundUnit = units.Where(unit => squad.bot.p.Stances[unit.Owner] == Stance.Enemy && unit.HasTrait<AttackBase>());
|
||||
if (!enemyAroundUnit.Any())
|
||||
return false;
|
||||
|
||||
return flee(enemyAroundUnit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -466,6 +466,11 @@
|
||||
<Compile Include="Orders\EnterAlliedActorTargeter.cs" />
|
||||
<Compile Include="Render\WithIdleOverlay.cs" />
|
||||
<Compile Include="Tooltip.cs" />
|
||||
<Compile Include="AI\Squad.cs" />
|
||||
<Compile Include="AI\States\StateBase.cs" />
|
||||
<Compile Include="AI\States\GroundStates.cs" />
|
||||
<Compile Include="AI\States\ProtectionStates.cs" />
|
||||
<Compile Include="AI\States\AirStates.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
||||
@@ -519,4 +524,7 @@ cd "$(SolutionDir)thirdparty/"
|
||||
copy "FuzzyLogicLibrary.dll" "$(SolutionDir)"
|
||||
cd "$(SolutionDir)"</PostBuildEvent>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="AI\States\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user