3

我正在开发一个多人游戏的服务器,该游戏必须控制数千个在世界各地运行的生物。每个生物都有一个带有心跳方法的 AI,如果玩家在附近,每隔几毫秒/秒就会调用一次,这样他们就可以做出反应。

目前,人工智能使用枚举器作为“例程”,例如

IEnumerable WanderAround(int radius)
{
    // Do something
}

它们从“状态方法”中调用,这些方法在 foreachs 中调用,在心跳中产生,因此您在每个滴答声中都回到同一个位置。

void OnHeartbeat()
{
    // Do checks, maybe select a new state method...
    // Then continue the current sequence
    currentState.MoveNext();
}

自然也必须在循环中调用例程,因为否则它们不会执行。但由于我不是编写这些 AI 的人,而是不一定是程序员的新手,所以我在服务器启动之前预编译 AI(简单的 .cs 文件)。这给了我这样的 AI 脚本:

override IEnumerable Idle()
{
    Do(WanderAround(400));
    Do(Wait(3000));
}

override IEnumerable Aggro()
{
    Do(Attack());
    Do(Wait(3000));
}

替换Do为迭代例程调用的 foreach 。

我真的很喜欢这种设计,因为 AI 易于理解,但功能强大。这不是简单的状态,但也不难理解/编写行为树。

现在到我实际的“问题”,我不喜欢Do包装器,我不喜欢预编译我的脚本。但是我想不出没有循环的任何其他方法来实现它,由于冗长和要编写这些脚本的人的技能水平,我想隐藏它。

foreach(var r in Attack()) yield return r;

我希望有一种方法可以在没有显式循环的情况下调用例程,但这是不可能的,因为我必须从 state 方法中让步。

而且我不能使用 async/await,因为它不符合我所依赖的 tick 设计(AI 可能非常复杂,老实说,我不知道如何使用 async 来实现它)。另外,我只是交易Do()await并没有太大的改进。

所以我的问题是:谁能想到摆脱那个循环包装器的方法?我愿意使用其他可以用作脚本的.NET语言(在服务器启动时编译它们),如果有一种以某种方式支持它的话。

4

2 回答 2

0

您可以尝试通过使用服务器中的事件并让各个 AI 订阅它们来寻求 .NET 框架的帮助。如果服务器正在维护心跳,这将起作用。

服务器

服务器公布 AI 可以订阅的事件。在 heartbeat 方法中,您将调用OnIdleandOnAggro方法来引发IdleandAggro事件。

public class GameServer
{
    // You can change the type of these if you need to pass arguments to the handlers.
    public event EventHandler Idle;
    public event EventHandler Aggro;

    void OnIdle()
    {
        EventHandler RaiseIdleEvent = Idle;
        if (null != RaiseIdleEvent)
        {
            // Change the EventArgs.Empty to an appropriate value to pass arguments to the handlers
            RaiseIdleEvent(this, EventArgs.Empty);
        }
    }

    void OnAggro()
    {
        EventHandler RaiseAggroEvent = Aggro;
        if (null != RaiseAggroEvent)
        {
            // Change the EventArgs.Empty to an appropriate value to pass arguments to the handlers
            RaiseAggroEvent(this, EventArgs.Empty);
        }
    }
}

Generic CreatureAI

您的所有开发人员都将基于此类实现他们的生物 AI。构造函数接受一个GameServer引用参数以允许挂钩事件。这是一个未保存引用的简化示例。在实践中,您将保存引用并允许 AI 实现者根据其 AI 所处的状态订阅和取消订阅事件。例如,仅当玩家试图窃取您的鸡蛋时才订阅 Aggro 事件。

public abstract class CreatureAI
{
    // For the specific derived class AI to implement
    protected abstract void IdleEventHandler(object theServer, EventArgs args);
    protected abstract void AggroEventHandler(object theServer, EventArgs args);

    // Prevent default construction
    private CreatureAI() { }

    // The derived classes should call this
    protected CreatureAI(GameServer theServer)
    {
        // Subscribe to the Idle AND Aggro events.
        // You probably won't want to do this, but it shows how.
        theServer.Idle += this.IdleEventHandler;
        theServer.Aggro += this.AggroEventHandler;
    }

    // You might put in methods to subscribe to the event handlers to prevent a 
    //single instance of a creature from being subscribe to more than one event at once.
}

人工智能本身

它们派生自通用CreatureAI基类并实现特定于生物的事件处理程序。

public class ChickenAI : CreatureAI
{
    public ChickenAI(GameServer theServer) :
        base(theServer)
    {
        // Do ChickenAI construction
    }

    protected override void IdleEventHandler(object theServer, EventArgs args)
    {
        // Do ChickenAI Idle actions
    }

    protected override void AggroEventHandler(object theServer, EventArgs args)
    {
        // Do ChickenAI Aggro actions
    }
}
于 2015-01-15T13:33:19.700 回答
0

每个生物都有一个带有心跳方法的 AI,每隔几毫秒/秒调用一次,

为什么不使用完整的天网并让每个生物对自己的心跳负责?

例如使用计时器创建每个生物(可以说是具有特定心跳的心脏)。当每个计时器跳动时,它会按照设计的方式进行操作,但还会检查游戏是否需要关闭、空闲、徘徊或其他项目。

通过分散循环,您已经摆脱了循环,您只需向订阅者(生物)广播在全局/基本级别上做什么。新手无法访问该代码,但可以理解它在概念级别上的作用。

于 2015-01-14T21:10:08.533 回答