Network grouping logic

Hey everyone. I’m having a hard time getting some logic down on how to send/update “groups” of objects to be sent from the server to the clients so the clients know what to load and what’s out there. Right now I have it so when the user logs in; the server will search 750 distant from that client and then send that information to the client. This works fine, but has a really bad “bug”? If the other user is not in that 750 and doesn’t move as the player moves to their location. That other user (lets call b) will see a, but seeing as b hasn’t moved, the server hasn’t sent his updated position yet, so a doesn’t see b at all.

One way around this I thought would be to somehow “cube” or grid everything and then just check the grid. The problem with that is… I’m not sure how to grid everything… and on top of that… the world is massive… so the grid also has to be able to go in an inf. any direction, or be able to make a “cell” on the grid to add objects to…

My question is… how do you code a grid, but also… is this the right way of doing it?

# order objects by theyr position
gridSize = 10
grid = dict()
for object in world:
  x = object.getPos().getX()%gridSize
  y = object.getPos().getY()%gridSize
  grid.setdefault((x,y), list())
  grid[(x,y)].append(object)

for [x, y], objectList in grid.items():
  for object in objectList:
    object.clearNeighboors()
    object.addNeighboors(objectList)
    # also add objects that are in the adjacent grid fields
    for dx in [-1,1]:
      for dy in [-1,1]:
        if (x+dx,y+dy) in grid:
          object.addNeighboors(grid[(x+dx,y+dy)])

something like that? i bet this could be done more efficient. also this doesnt need to be run every frame if the gridsize is large enough. for example if you have a gridsize of 100 and a character may maximally move 10 units in a second, updating every 10 secs would be enougth.

of course you can work with a 2d array as well. i dont know what’s more efficient.

Thank’s hypnos. This gived me the the idea. I haven’t 100% finish the code just yet, but it got me started at least =) Right, so I have it in a tree base objects selection group now…

{Area}->{x,y,z}->[Players]

This way they’re localized to the area and gridsize.

Hope it works, I haven’t be able to finish what I started on it just yet. You know… school and all.