ENIGMA Forums

Contributing to ENIGMA => Function Peer Review => Topic started by: polygone on February 28, 2011, 05:47:52 am

Title: move_wrap
Post by: polygone on February 28, 2011, 05:47:52 am
Code: (C++) [Select]
void move_wrap(bool hor, bool vert, double margin)
{
  enigma::object_planar* const inst = ((enigma::object_planar*)enigma::instance_event_iterator->inst);
  if (hor)
  {
    const double wdis = room_width + margin*2;
    if (inst->x < -margin)
    {
      inst->x += wdis*ceil(((-margin) - inst->x)/wdis);
    }
    if (inst->x > room_width + margin)
    {
      inst->x -= wdis*ceil((inst->x - (room_width + margin))/wdis);
    }
  }
  if (vert)
  {
    const double hdis = room_height + margin*2;
    if (inst->y < -margin)
    {
      inst->y += hdis*ceil(((-margin) - inst->y)/hdis);
    }
    if (inst->y > room_height + margin)
    {
      inst->y -= hdis*ceil((inst->y - (room_height + margin))/hdis);
    }
  }
}
Title: Re: move_wrap
Post by: Fede-lasse on March 01, 2011, 09:47:03 am
I. Did. This. One. FFS.

And more efficiently.

Code: (Something that looks like C++ and is easily portable) [Select]
//int move_wrap(double hor,double vert,double margin)
int move_wrap(double hor,double vert,double margin) {
  //not sure whether two IFs are faster than one big calculation in a single IF
  if (hor) {
    if (X < -margin || X > room_width+margin) {
      X = -(X-room_width);
    }
  }
  if (vert) {
    if (Y < -margin || Y > room_height+margin) {
      Y = -(Y-room_height);
    }
  }
  return (int)0;
}

Since Josh fails to remember or care (<_<), I post this code.
Title: Re: move_wrap
Post by: polygone on March 01, 2011, 01:32:01 pm
I. Did. This. One. FFS.

And more efficiently.

Code: (Something that looks like C++ and is easily portable) [Select]
//int move_wrap(double hor,double vert,double margin)
int move_wrap(double hor,double vert,double margin) {
  //not sure whether two IFs are faster than one big calculation in a single IF
  if (hor) {
    if (X < -margin || X > room_width+margin) {
      X = -(X-room_width);
    }
  }
  if (vert) {
    if (Y < -margin || Y > room_height+margin) {
      Y = -(Y-room_height);
    }
  }
  return (int)0;
}

Since Josh fails to remember or care (<_<), I post this code.
a) That's not more efficient, it's different. And by being different it does not behave how the function behaves in GM. I don't just stick shit into code randomly :)
b) It's not written properly. Wtf are X and Y going to do?
Title: Re: move_wrap
Post by: RetroX on March 01, 2011, 10:44:47 pm
Dear Fede-lasse,

Please do not complain that you have already done functions when you posted them months ago and we don't know where they are.

If you wish to have them actually looked at, please post them or point out where you posted them.

Sincerely,
RetroX and everyone else.

P.S.: (int)0 is redundant because 0 is an int, and it would have been auto-casted either way.
Title: Re: move_wrap
Post by: Fede-lasse on March 06, 2011, 03:05:42 pm
@polygone
How is it less efficient? I do way less IF and math than you do.
Title: Re: move_wrap
Post by: polygone on March 06, 2011, 03:10:19 pm
I never said it was less efficient, I said it was different. My code is 'less efficient' for a reason: because it works better.
Title: Re: move_wrap
Post by: Fede-lasse on March 08, 2011, 03:54:57 am
How does it work better? Because mine is not C++? Just convert it to C++. I'm sure you or the others wouldn't find that too hard. My version mimics the behaviour in GM, so I don't see the problem.

Also @Retro, I once sent Josh all the functions I had done. He probably forgot about them.
Title: Re: move_wrap
Post by: polygone on March 08, 2011, 05:34:48 am
My version mimics the behaviour in GM, so I don't see the problem.
It doesn't mimic the behaviour in GM. Try moving an instance's x position 3*room_width and see if it wraps correctly using your code. If you actually read what code I use you will see I am specifically catering for this.

Though actually my code doesn't completely mimic GM's, it in fact works better. I believe GM's code goes more like this:

Code: [Select]
  if (hor)
  {
    const double wdis = room_width + margin*2;
    if (inst->x > room_width + margin)
    {
      inst->x -= wdis*ceil((inst->x - (room_width + margin))/wdis);
    }
    else if (inst->x < -margin)
    {
      inst->x += wdis*ceil(((-margin) - inst->x)/wdis);
    }
  }
  if (vert)
  {
    const double hdis = room_height + margin*2;
    if (inst->y > room_height + margin)
    {
      inst->y -= hdis*ceil((inst->y - (room_height + margin))/hdis);
    }
    else if (inst->y < -margin)
    {
      inst->y += hdis*ceil(((-margin) - inst->y)/hdis);
    }
This means that when the margin is set to a high negative value, ie more than half the room_width or something and both if statements are true it doesn't actually wrap and just moves the instance continuously left/up. By leaving the else out it wraps correctly mirrored which is a better way to handle things.
Title: Re: move_wrap
Post by: RetroX on March 08, 2011, 04:09:56 pm
Also @Retro, I once sent Josh all the functions I had done. He probably forgot about them.
Josh != everyone else
Title: Re: move_wrap
Post by: Fede-lasse on March 09, 2011, 09:14:31 am
@polygone
Whatever works for you, I guess...

@Retro
Also @Retro, I once sent Josh all the functions I had done. He probably forgot about them.
Josh != everyone else
Sincerely,
RetroX and everyone else.
Title: Re: move_wrap
Post by: RetroX on March 09, 2011, 06:51:19 pm
Also @Retro, I once sent Josh all the functions I had done. He probably forgot about them.
Josh != everyone else
Sincerely,
RetroX and everyone else.
Yes, thank you for enforcing my point.  Please post them if you want them to be reviewed by everyone else.
Title: Re: move_wrap
Post by: Fede-lasse on March 10, 2011, 11:40:42 am
Also @Retro, I once sent Josh all the functions I had done. He probably forgot about them.
Josh != everyone else
Sincerely,
RetroX and everyone else.
Yes, thank you for enforcing my point.  Please post them if you want them to be reviewed by everyone else.
I didn't reinforce your point, silly. Your own post was against the point that you made. By saying "sincerely RetroX and everyone else," you say that no one but me knows of them. That is not true.

Either way, polygone seems more interested than I am in coding these functions.
Title: Re: move_wrap
Post by: RetroX on March 10, 2011, 08:50:11 pm
Also @Retro, I once sent Josh all the functions I had done. He probably forgot about them.
Josh != everyone else
Sincerely,
RetroX and everyone else.
Yes, thank you for enforcing my point.  Please post them if you want them to be reviewed by everyone else.
I didn't reinforce your point, silly. Your own post was against the point that you made. By saying "sincerely RetroX and everyone else," you say that no one but me knows of them. That is not true.

Either way, polygone seems more interested than I am in coding these functions.
Yes, and only you and Josh know.
Josh != everyone else
Title: Re: move_wrap
Post by: Fede-lasse on March 11, 2011, 05:18:24 am
I could keep on citing your post, but I think we're just gonna stop here. You fail to get my point and I fail to get yours.
Title: Re: move_wrap
Post by: Josh @ Dreamland on March 11, 2011, 09:01:49 am
...
His point is, if you have functions, post them here. I'm not going to do anything with them, and no one else gives a shit about your functions unless they're posted here. So post them, or drop them.
Title: Re: move_wrap
Post by: Fede-lasse on March 12, 2011, 06:43:42 am
You're not going to do anything with them? Lier. The reason I even got angered is that you just ignored the work I did. <_<

Erm...

Code: [Select]
//bool place_snapped(double hsnap,double vsnap)
bool place_snapped(double hsnap,double vsnap) {
  return (bool)(X == X%hsnap) && (Y == Y%vsnap);
}

//int move_random(double hsnap,double vsnap)
int move_random(double hsnap,double vsnap) {
  X = floor(random(room_width)/hsnap)*hsnap;
  Y = floor(random(room_height)/vsnap)*vsnap;
  return (int)0;
}

//int move_snap(double hsnap,double vsnap)
int move_snap(double hsnap,double vsnap) {
  X = floor(X/hsnap)*hsnap;
  Y = floor(Y/vsnap)*vsnap;
  return (int)0;
}

//int move_wrap(double hor,double vert,double margin)
int move_wrap(double hor,double vert,double margin) {
  //not sure whether two IFs are faster than one big calculation in a single IF
  if (hor) {
    if (X < -margin || X > room_width+margin) {
      X = -(X-room_width);
    }
  }
  if (vert) {
    if (Y < -margin || Y > room_height+margin) {
      Y = -(Y-room_height);
    }
  }
  return (int)0;
}

//int move_towards_point(double x,double y,double sp)
int move_towards_point(double x,double y,double sp) {
  DIRECTION = point_direction(X,Y,x,y);
  SPEED = sp;
  return (int)0;
}

//double distance_to_point(double x,double y)
double distance_to_point(double x,double y) {
  return (double)point_distance(median(BBOX_LEFT,x,BBOX_RIGHT),median(BBOX_TOP,y,BBOX_BOTTOM),x,y);
}

//double distance_to_object(int obj)
double distance_to_object(int obj) {
  double _edgeX,_edgeY;
  _edgeX = median(BBOX_LEFT,obj.X,BBOX_RIGHT);
  _edgeY = median(BBOX_TOP,obj.Y,BBOX_BOTTOM);
  if (_edgeX > obj.BBOX_LEFT && _edgeX < obj.BBOX_RIGHT && _edgeY > obj.BBOX_TOP && _edgeY < obj.BBOX_BOTTOM) {
    return (double)0;
  }
  return (double)point_distance(_edgeX,_edgeY,median(obj.BBOX_LEFT,X,obj.BBOX_RIGHT),median(obj.BBOX_TOP,Y,obj.BBOX_BOTTOM));
}

There were some more, but I found these to be most useful. Again, bear with my poor C++ failing-to-depend-on-legacy-rules skills. Things like X and Y are supposed to be the equialivents of what they would be in GM. I remember Josh saying okay to that; it would be corrected later.