Qestion: its a Math matter tho!

03/15/2011 03:03 { Angelius }#1
is there any better way to define the Range?
PHP Code:
for (int i 018i++)
{
var 
Add from d in Dictionary.Clients
where Math
.Max(Math.Abs(d.Value.Client.X), Math.Abs(d.Value.Client.Y)) < i
select 
(d.Key);
foreach (
uint Char in Add)
if (!
Clients.Contains(Char))
Clients.Add(Char);
//i need to find another way to do it so i dont have to loop + foreach at the same time !

03/15/2011 03:15 pro4never#2
I don't see what the for 18 loop is being used for in that case...


What are you trying to actually do?

You're pulling distance and then lowering distance every time?... Depending on what it is you're trying to do I don't see what use the for loop has... You want to pull things within 18 dist... great then why do you keep looping over and over while lowering the range?
03/15/2011 03:33 { Angelius }#3
Quote:
Originally Posted by pro4never View Post
I don't see what the for 18 loop is being used for in that case...


What are you trying to actually do?

You're pulling distance and then lowering distance every time?... Depending on what it is you're trying to do I don't see what use the for loop has... You want to pull things within 18 dist... great then why do you keep looping over and over while lowering the range?
well here is the thing .

Code:
for (int i = 0; i < 18; i++)
{
var Add = from d in Dictionary.Clients
[COLOR="Red"]where Math.Max(Math.Abs(d.Value.X - Client.X), Math.Abs(d.Value.Y - Client.Y)) < i[/COLOR]
select (d.Key);
foreach (uint Char in Add)
if (!Clients.Contains(Char))
Clients.Add(Char);
the red line must pick from the Dictionary that contains all the game clients where the distance = i
so i dont have to foreach all the game clients looking for an = distance
that way it picks all the in range (18) players and add them to a list
and if i dident for loop than it'll pick only the whatever distance i ask it to look for

annnd when im done coding the whole thing i was like damn i did all that work to avoid the LONG foreach to end up with an = or maybe greater foreach time :P

and so i had to ask somebody
03/15/2011 03:38 Nullable#4
The first loop is useless.
Code:
var Add = from d in Dictionary.Clients
where Math.Max(Math.Abs(d.Value.X - Client.X), Math.Abs(d.Value.Y - Client.Y)) < [color="red"]18[/color]
select d.Key;
//continue your stuff here.
03/15/2011 03:42 _tao4229_#5
^
||


var query = from d in Dictionary.Clients
where Distance(d.Value.X, d.Value.Y, Client.X, Client.Y) <= 18
select d.Key;

Clients.AddRange(query); // (depending on the kind of collection clients is)

The way you're doing it looks at clients WITHIN 1 distance, then WITHIN 2 distance (inclusive of 1 distance, obviously), then 3.. then 4.. then 5.. all greater values are inclusive. You don't need to loop; the < operator works the way it should.
03/15/2011 04:20 { Angelius }#6
all right guys thanks for the quick help/ answers but..

how come when i had the for loops placed all over the spawns voids it took like 0 to 1 cpu usage peer jump and when i removed it all it went up to 2/3 cpu usage peer jump ?
03/15/2011 20:28 Korvacs#7
The system your using is never going to be fast, because you query everyone either on the map or on the server, you need a spatial positioning system if you want it to be quick.
03/15/2011 21:08 { Angelius }#8
Quote:
Originally Posted by Korvacs View Post
The system your using is never going to be fast, because you query everyone either on the map or on the server, you need a spatial positioning system if you want it to be quick.
yes Korvacs i figured that thought but this is how im doing it by now i guess this way the i can control/edit/pull/only the current map clients/npcs/etc

i'll be providing this collection in every map
Quote:
public Map()
{
Mobs = new Dictionary<uint, Mob>();
Npcs = new Dictionary<uint, Npc>();
Clients = new Dictionary<uint, Character>();
Items = new Queue<Items.ItemGround>();
}

on creation lets say npcs i go like
Quote:
if (!Dictionary.Maps.ContainsKey(NPC.Map))
Dictionary.Maps.Add(NPC.Map, new Map(0));
if (!Dictionary.Maps[NPC.Map].Npcs.ContainsKey(NPC.UID))
{
Dictionary.Maps[NPC.Map].Npcs.Add(NPC.UID, NPC);
}
and when i need to pull the local npcs im doing like
Quote:
var Adds = from NPC in Dictionary.Maps[Client.Map].Npcs
where Math.Max(Math.Abs(NPC.Value.X - Client.X), Math.Abs(NPC.Value.Y - Client.Y)) < 18
select NPC.Value;
foreach (Npc N in Adds)
{
if (!Client.LocalNpcs.ContainsKey(N.UID))
{
Client.LocalNpcs.Add(N.UID, N);
Contacts.Send(Packets.NewNpc(N), Client);
}
}
and so it gose the same for players/mobs/etc

i dont know but thats what it seems to be the best way to deal with it
03/17/2011 19:43 -PhysiX-#9
that plus the screen system in saints or hybrids source