Thread: testnpc
View Single Post
  #8  
Old 01-18-2006, 02:47 AM
jake13jake jake13jake is offline
Former Classic Staff
jake13jake's Avatar
Join Date: Dec 2002
Location: Northwest Vermont
Posts: 1,452
jake13jake will become famous soon enough
1. getnearestnpcs() doesn't exist.
2. getareanpcs() doesn't exist clientside.

3. The following works... however... lags, would be the same for the player.
PHP Code:
function onNPC(tx,ty,w,h) {
  for (
i=0i<npcs.size(); i++)
    if (
npcs[i].width>&& npcs[i].height>0)
      if (
overlaps(tx,ty,w,h,npcs[i].x,npcs[i].y,npcs[i].width,npcs[i].height))
        return 
1;
  return 
0;

Now, if the array was a bit smaller, it wouldn't be so much of a lag problem. Thus, if 1 and/or 2 were available clientside, I wouldn't be having this problem. It's not such a problem on inside levels, but it's a HUGE problem on gmaps.

Oh, by the way, it's a bit less simple to check for the overlapping of an NPC onto the area being checked.
PHP Code:
function overlaps(x1,y1,w1,h1,x2,y2,w2,h2) {
  if (
x1 in |x2,x2+w2> && y1 in |y2,y2+h2>)       return 1;
  if (
x1+w1 in <x2,x2+w2> && y1 in |y2,y2+h2>)    return 1;
  if (
x1+w1 in <x2,x2+w2> && y1+h1 in <y2,y2+h2>) return 1;
  if (
x1 in |x2,x2+w2> && y1+h1 in <y2,y2+h2>)    return 1;
  if (
x2 in |x1,x1+w1> && y2 in |y1,y1+h1>)       return 1;
  if (
x2+w2 in <x1,x1+w1> && y2 in |y1,y1+h1>)    return 1;
  if (
x2+w2 in <x1,x1+w1> && y2+h2 in <y1,y1+h1>) return 1;
  if (
x2 in |x1,x1+w1> && y2+h2 in <y1,y1+h1>)    return 1;
  return 
0;

Unfortunately, the in operator has only so much functionality. I wish I could get rid of the last four lines at the least, but there's always the possibility of the first rectangle completely enclosing the second rectangle.

Last edited by jake13jake; 01-18-2006 at 03:00 AM..
Reply With Quote