Files
OpenRA/OpenRA.Mods.Common/Scripting/Properties/CombatProperties.cs
RoosterDragon ab28e6a75a Improve Lua type documentation and bindings.
The ExtractEmmyLuaAPI utility command, invoked with `--emmy-lua-api`, produces a documentation file that is used by the [OpenRA Lua Language Extension](https://marketplace.visualstudio.com/items?itemName=openra.vscode-openra-lua) to provide documentation and type information is VSCode and VSCode compatible editors when editing the Lua scripts.

We improve the documentation and types produced by this utility in a few ways:
- Require descriptions to be provided for all items.
- Fix the type definitions of the base engine types (cpos, wpos, wangle, wdist, wvec, cvec) to match with the actual bindings on the C# side. Add some extra bindings for these types to increase their utility.
- Introduce ScriptEmmyTypeOverrideAttribute to allow the C# side of the bindings to provide a more specific type. The utility command now requires this to be used to avoid accidentally exporting poor type information.
- Fix a handful of scripts where the new type information revealed warnings.

The ability to ScriptEmmyTypeOverrideAttribute allows parameters and return types to provide a more specific type compared to the previous, weak, type definition. For example LuaValue mapped to `any`, LuaTable mapped to `table`, and LuaFunction mapped to `function`. These types are all non-specific. `any` can be anything, `table` is a table without known types for its keys or values, `function` is a function with an unknown signature.

Now, we can provide specific types. , e.g. instead of `table`, ReinforcementsGlobal.ReinforceWithTransport is able to specify `{ [1]: actor, [2]: actor[] }` - a table with keys 1 and 2, whose values are an actor, and a table of actors respectively. The callback functions in MapGlobal now have signatures, e.g. instead of `function` we have `fun(a: actor):boolean`. In UtilsGlobal, we also make use of generic types. These work in a similar fashion to generics in C#. These methods operate on collections, we can introduce a generic parameter named `T` for the type of the items in those collections. Now the return type and callback parameters can also use that generic type. This means the return type or callback functions operate on the same type as whatever type is in the collection you pass in. e.g. Utils.Do accepts a collection typed as `T[]` with a callback function invoked on each item typed as `fun(item: T)`. If you pass in actors, the callback operates on an actor. If you pass in strings, the callback operates on a string, etc.

Overall, these changes should result in an improved user experience for those editing OpenRA Lua scripts in a compatible IDE.
2024-08-03 19:12:51 +03:00

111 lines
3.8 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* 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.Linq;
using Eluant;
using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Scripting
{
[ScriptPropertyGroup("Combat")]
public class CombatProperties : ScriptActorProperties, Requires<AttackBaseInfo>, Requires<IMoveInfo>
{
readonly IMove move;
public CombatProperties(ScriptContext context, Actor self)
: base(context, self)
{
move = self.Trait<IMove>();
}
[ScriptActorPropertyActivity]
[Desc("Ignoring visibility, find the closest hostile target and attack move to within 2 cells of it.")]
public void Hunt()
{
Self.QueueActivity(new Hunt(Self));
}
[ScriptActorPropertyActivity]
[Desc("Move to a cell, but stop and attack anything within range on the way. " +
"closeEnough defines an optional range (in cells) that will be considered " +
"close enough to complete the activity.")]
public void AttackMove(CPos cell, int closeEnough = 0)
{
Self.QueueActivity(new AttackMoveActivity(Self, () => move.MoveTo(cell, closeEnough)));
}
[ScriptActorPropertyActivity]
[Desc("Patrol along a set of given waypoints. The action is repeated by default, " +
"and the actor will wait for `wait` ticks at each waypoint.")]
public void Patrol(CPos[] waypoints, bool loop = true, int wait = 0)
{
foreach (var wpt in waypoints)
{
Self.QueueActivity(new AttackMoveActivity(Self, () => move.MoveTo(wpt, 2)));
Self.QueueActivity(new Wait(wait));
}
if (loop)
Self.QueueActivity(new CallFunc(() => Patrol(waypoints, loop, wait)));
}
[ScriptActorPropertyActivity]
[Desc("Patrol along a set of given waypoints until a condition becomes true. " +
"The actor will wait for `wait` ticks at each waypoint. " +
"The callback function will be called as func(self: actor):boolean.")]
public void PatrolUntil(CPos[] waypoints, [ScriptEmmyTypeOverride("fun(self: actor):boolean")] LuaFunction func, int wait = 0)
{
Patrol(waypoints, false, wait);
var repeat = func.Call(Self.ToLuaValue(Context)).First().ToBoolean();
if (repeat)
using (var f = func.CopyReference() as LuaFunction)
Self.QueueActivity(new CallFunc(() => PatrolUntil(waypoints, f, wait)));
}
}
[ScriptPropertyGroup("Combat")]
public class GeneralCombatProperties : ScriptActorProperties, Requires<AttackBaseInfo>
{
readonly AttackBase[] attackBases;
public GeneralCombatProperties(ScriptContext context, Actor self)
: base(context, self)
{
attackBases = self.TraitsImplementing<AttackBase>().ToArray();
}
[Desc("Attack the target actor. The target actor needs to be visible.")]
public void Attack(Actor targetActor, bool allowMove = true, bool forceAttack = false)
{
var target = Target.FromActor(targetActor);
if (!target.IsValidFor(Self))
Log.Write("lua", $"{targetActor} is an invalid target for {Self}!");
if (!targetActor.Info.HasTraitInfo<FrozenUnderFogInfo>() && !targetActor.CanBeViewedByPlayer(Self.Owner))
Log.Write("lua", $"{targetActor} is not revealed for player {Self.Owner}!");
foreach (var attack in attackBases)
attack.AttackTarget(target, AttackSource.Default, true, allowMove, forceAttack);
}
[Desc("Checks if the targeted actor is a valid target for this actor.")]
public bool CanTarget(Actor targetActor)
{
return Target.FromActor(targetActor).IsValidFor(Self);
}
}
}