Search Unity

Question Using clients to do pathfinding calculations

Discussion in 'Scripting' started by Wieditleestisgek, Apr 16, 2023.

  1. Wieditleestisgek

    Wieditleestisgek

    Joined:
    Dec 18, 2016
    Posts:
    43
    So i was thinking the average human response time is 250ms
    would it be feasible to take 2 clients with good ping,ethernet width and spare cpu power and let them do the calculation of a couple of paths for u?

    then have one send the first parts of the paths back to the server and the other a pseudorandom checksum of the parts so u can check for malicious client interference.

    if someone has like 100 ping it will probably return in -+120ms right? probably smooth enough for pathing?
    and enough time left to let the server do the path if issues arise.

    do u think this would work?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    What pathfinding problem are you trying to solve here?

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. Wieditleestisgek

    Wieditleestisgek

    Joined:
    Dec 18, 2016
    Posts:
    43
    I am thinking of maybe making such a system in the future but as it would be rather big i'd wanna make sure its even possible.

    I think if the server cpu is almost at capacity it may be able to unload some of the work onto the clients
    thus freeing up peformance on other things allowing for example even more mobs to be spawned.
    and since creatures have reaction time offloading pathing seems like a way that may be possible naturally.

    i dont have any problems currently.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Is it possible? Sure. You have your code running on host/server and client, you can distribute however you like. The complexity of which is limited mainly by your ability to code it. You're going to have to do things like determine a way to serailize the results between both and make sure both server and client are working on the same dataset.

    Will it be performant? That's a whole other story.

    Ping is variable... are you going to be constantly testing RTT/ping so you can have the latest info about each client? Have you considered the overhead of that on the system?

    We also don't necessarily know how fast each client can actually be. If the tasks timesout from your preferred duration... well, now you have to be monitoring that and redirecting that task in an appropriate amount of time. And well... since your RTT/ping is a huge portion of your desired results, that redirected target is likely going to be the server itself. And if the server had the resources laying about to do the task anyways... why didn't it do it in the first place?

    A quick way to reduce this overhead I might consider doing for a naive first version is create a jobs pool that every client is aware of. When a job is made available, all clients are told about it (in the case of pathfinding it could just be the start point, end point, and entity id needing the path). Each client can assess if they have the resources to perform the job and attempt starting it. Then when it finishes it, it tells the server it's done.

    This does mean every client may very well calculate the path... doing more work than necessary. But, so what. This can be used for validation purposes.

    When the server receives a result it'll validate however you want (fast validation... though honestly, this could easily be subverted by malicious clients if that's a concern). You can wait a small moment if you want, or start pathing at this moment... or some combination of overlap in times. If another result comes in, that one could be used for more confident validation.

    After all clients respond OR some timeout, you tell all clients that job has been finished, and they can stop taking it into consideration (including halting calculation on it, if they're mid work).

    If this is massive multiplayer, this could be distributed in groups/pools of players. So maybe not every single player. But all the players in the region of the entity that is pathing since they're the only one's who really care about that entity.

    ...

    Of course... I could think of other ways to optimize. For example pre-calculating major/long routes from key points of interest (intersections of roads/paths, center of town, etc). Then rely on memory to hold all these major routes, and when you have a target find nearest points of interest near them, lookup the appropriate path, and then just use local pathfinding to get to the point of interest.

    The idea here being on large maps things like A* get slow for large distances because it has to resolve a lot of nodes. But for paths of short distances, it's pretty performant, since it uses heuristics to guess the next best node.
     
  5. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    if the server provides the npc initially and then hands it over to the client that managed to get it's attention. Client can pass back the npc info when he's finished. Sounds like a fair system. Server provides the enemy spawn point' hands it over and then the client tells us what that instanced npc is doing. Maybe if the client disconnected he'd reconnect and still have that npc on him and continue to pass it back to the server notifying it of an npc that his instance spawned or reloaded with. This way maybe client can say hey here's my position and here's that npc I'm withs position. And you can dish that out to the other clients so they can interact or at least see this battle taking place. One thing imagine if the server was popular and the server provided all the npc. There may not be enough to go around, so is nice to inject a client only npc. Maybe not for the behaviour boss spawns but i think it could be best for the style to have a few npc that spawn in a area on the server and they are just given to players who are in range of them. Client takes ownership and his instance controls and computes the npc and delivers the info back. :) Sounds like a realistic plan to me. If another client helped kill another clients monster if the server was told of that client in that cell and was told of the other client damaging this cell. Then I guess coop could be workable though not 100% on the approach being flawless. And you should consider loot distribution. Who gets the kill. Who computed the npc. That's the type of thing the server could provide. Server makes that decision. Well client 6 just delivered 350 damage or has dealt 75% of the damage done to this enemy. So therefore client 6 should gain it's attention and compute it's ai

    Either way I would say the client doing as much as possible is the way id go if I was doing the same thing.