Files
OpenRA/OpenRA.Mods.Common/AI/States/StateBase.cs
RoosterDragon 5bd5a384b7 Use a BitSet for representing target types.
- Rename Bits<T> to BitSet<T>.
- Implement set based helpers for BitSet<T>.
- When representing TargetTypes of ITargetable in various traits, use a BitSet<TargetableType> instead of HashSet<string> for better performance & reduced memory usage.
- Fix FieldLoader to trim input values when generating a BitSet<T>.
- Require T in BitSet<T> and BitSetAllocator<T> to be a class since it's just a marker value. This allows the JIT to instantiate generic code for these classes once, as they don't benefit from specialized code for T. (Typically JITs will generate shared code for all reference types, and unique code for every value type encountered).
2018-03-21 12:07:44 +01:00

107 lines
2.9 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2018 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
{
abstract class StateBase
{
protected static void GoToRandomOwnBuilding(Squad squad)
{
var loc = RandomBuildingLocation(squad);
foreach (var a in squad.Units)
squad.Bot.QueueOrder(new Order("Move", a, Target.FromCell(squad.World, loc), false));
}
protected static CPos RandomBuildingLocation(Squad squad)
{
var location = squad.Bot.GetRandomBaseCenter();
var buildings = squad.World.ActorsHavingTrait<Building>()
.Where(a => a.Owner == squad.Bot.Player).ToList();
if (buildings.Count > 0)
location = buildings.Random(squad.Random).Location;
return location;
}
protected static bool BusyAttack(Actor a)
{
if (a.IsIdle)
return false;
var activity = a.CurrentActivity;
var type = activity.GetType();
if (type == typeof(Attack) || type == typeof(FlyAttack))
return true;
var next = activity.NextActivity;
if (next == null)
return false;
var nextType = next.GetType();
if (nextType == typeof(Attack) || nextType == typeof(FlyAttack))
return true;
return false;
}
protected static bool CanAttackTarget(Actor a, Actor target)
{
if (!a.Info.HasTraitInfo<AttackBaseInfo>())
return false;
var targetTypes = target.GetEnabledTargetTypes();
if (targetTypes.IsEmpty)
return false;
var arms = a.TraitsImplementing<Armament>();
foreach (var arm in arms)
{
if (arm.IsTraitDisabled)
continue;
if (arm.Weapon.IsValidTarget(targetTypes))
return true;
}
return false;
}
protected virtual bool ShouldFlee(Squad squad, Func<IEnumerable<Actor>, bool> flee)
{
if (!squad.IsValid)
return false;
var randomSquadUnit = squad.Units.Random(squad.Random);
var dangerRadius = squad.Bot.Info.DangerScanRadius;
var units = squad.World.FindActorsInCircle(randomSquadUnit.CenterPosition, WDist.FromCells(dangerRadius)).ToList();
// If there are any own buildings within the DangerRadius, don't flee
// PERF: Avoid LINQ
foreach (var u in units)
if (u.Owner == squad.Bot.Player && u.Info.HasTraitInfo<BuildingInfo>())
return false;
var enemyAroundUnit = units.Where(unit => squad.Bot.Player.Stances[unit.Owner] == Stance.Enemy && unit.Info.HasTraitInfo<AttackBaseInfo>());
if (!enemyAroundUnit.Any())
return false;
return flee(enemyAroundUnit);
}
}
}