客户端 - 服务器游戏算法

Cli*_* Ok 15 algorithm game-loop

对于独立游戏,基本的游戏循环是(来源:维基百科)

while( user doesn't exit )
  check for user input
  run AI
  move enemies
  resolve collisions
  draw graphics
  play sounds
end while
Run Code Online (Sandbox Code Playgroud)

但是,如果我开发类似客户端服务器的游戏,如Quake,Ragnarock,Trackmania等,

客户端和游戏服务器部分的循环/算法是什么?

Dav*_*ave 19

它会是这样的

客户:

while( user does not exit )
    check for user input
    send commands to the server
    receive updates about the game from the server
    draw graphics
    play sounds
end
Run Code Online (Sandbox Code Playgroud)

服务器:

while( true )
    check for client commands
    run AI
    move all entities
    resolve collisions
    send updates about the game to the clients
end
Run Code Online (Sandbox Code Playgroud)


小智 12

客户:

connect to server
while( user does not exit && connection live)
    check for user input
    send commands to the server
    estimate outcome and update world data with 'best guess'
    draw graphics
    play sounds
    receive updates about the game from the server
    correct any errors in world data
    draw graphics
    play sounds
end
Run Code Online (Sandbox Code Playgroud)

服务器:

while( true )
    check for and handle new player connections
    check for client commands
    sanity check client commands
    run AI
    move all entities
    resolve collisions
    sanity check world data
    send updates about the game to the clients
    handle client disconnects
end
Run Code Online (Sandbox Code Playgroud)

对客户端命令和世界数据的完整性检查是为了消除由故意作弊(移动太快,通过墙壁等)或滞后(通过客户认为打开的门,但服务器知道)引起的任何"不可能"情况关闭等).

为了处理客户端和服务器之间的延迟,客户端必须最好地猜测接下来会发生什么(使用它的当前世界数据和客户端命令) - 然后客户端必须处理它预测会发生什么之间的任何差异以及服务器后来告诉它实际发生了什么.通常这将足够接近玩家没有注意到差异 - 但如果滞后很重要,或者客户端和服务器不同步(例如由于作弊),那么客户端将需要进行突然修正它从服务器接收数据.

将这些流程的各个部分拆分为单独的线程以优化响应时间也存在许多问题.

最好的方法之一是从其中一个拥有活跃modding社区的游戏中获取SDK - 深入研究其工作原理将很好地概述应该如何完成.