Saturday 15 August 2015

c# - Get data from a X,Y,Z array within given radius -



c# - Get data from a X,Y,Z array within given radius -

i developing game server , need able spectators within area, fear 1 using ugly , "slow" haven't experienced performance hits yet testing locally not on live server.

this getspectators function:

public void getspectators(ref hashset<player> players, coordinate_t coordinate, bool multifloor = false) { (int x = coordinate.x - 11; x != coordinate.x + 11; x++) { (int y = coordinate.y - 11; y != coordinate.y + 11; y++) { if (multifloor) { (int z = coordinate.z - 2; z != coordinate.z + 2; z++) { tile tile = gettile(x, y, z); if (tile != null) { foreach (player p in tile.creatures) { players.add(p); } } } } else { tile tile = gettile(x, y, coordinate.z); if (tile != null) { foreach (player p in tile.creatures) { players.add(p); } } } } } }

i have class map holds other dictionary class tile, each tile represented x, y, , z coordinates, each tile holds list of class called player, tiles have players don't.

i need way , not ugly e.g:

all players within x=100, y=100, z=7 in radius 11 example.

i think smart reference tiles in player class if you're not doing so, , pass players getspectators() method...

something like...

public class player { // reference tile player on. public tile currenttile { get; set; } }

this allow loop through players instead of many tiles. , should cleaner , more efficient find players way want without loop nesting. ex:

public list<player> getspectators(hashset<player> playersingame, coordinate coord) { var playersinrange = new list<player>(); // iterate through each player. foreach (var p in playersingame) { // check if tile player sitting on in range of radius given. if ((p.currenttile.x < coord.x + 6 || p.currenttile.x > coord.x - 6) && (p.currenttile.y < coord.y + 6 || p.currenttile.y > coord.y - 6)) { // player within radius. playersinrange.add(p); } } homecoming playersinrange; }

you can add together in additional check z coordinate, , other conditional statements. can see, allow utilize 1 loop rather 3 nested loops. may or may not find useful. hope helps.

c# arrays radius

No comments:

Post a Comment