Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Heretic86 on April 16, 2015, 07:52:06 am

Title: Custom Array Sort Order...
Post by: Heretic86 on April 16, 2015, 07:52:06 am
Workin on another script for Mirroring event movement.  Im having some trouble in getting a small array of Mirror Events to sort properly.  Im not even sure what the goal should be.

Basics:
0 = Vertical Mirror
1 = Horizontal Mirror
2 = Mirror Both
3 = Copy Player

I wrote a custom Comparator, but its not giving me the right sort order.

        # Sort by farthest Event of Movement Direction
        $game_map.mirror_events.sort! {|a,b| mirror_comparator(a, b, d)}


Then, this is what I have for my Mirror Comparator:

  #      a : first event to compare
  #      b : second event to compare
  #      d : direction
  #  - <=> returns 1 if a > b, 0 if a == b, -1 if a < b for Array sort method
  #--------------------------------------------------------------------------
  def mirror_comparator(a, b, d)
    # Shorthand - Mirror Values and Logical Coordinates
    am, bm, ax, bx, ay, by = a.mirror, b.mirror, a.x, b.x, a.y, b.y
    # Compare X and Y values depending on Direction and Mirror Values
    if d == 2    # Down
      return ([0,2].include?(am) or [0,2].include?(bm)) ? ay <=> by : by <=> ay
    elsif d == 8 # Right
      return ([0,2].include?(am) or [0,2].include?(bm)) ? by <=> ay : ay <=> by     
    elsif d == 4 # Left
      return ([1,2].include?(am) or [1,2].include?(bm)) ? bx <=> ax : ax <=> bx
    elsif d == 6 # Right
      return ([1,2].include?(am) or [1,2].include?(bm)) ? ax <=> bx : bx <=> ax
    end
    # Default (0 means the two objects compared are equal)
    return 0
  end


The intention of the code is to sort by the events coordinates.  When mirroring the player moving to the Right, code puts the event farthest on the Right first (I think), unless one of the two compared events (a and b) moves the other way, then it should sort by event farthest to the left first.  The events are sorted so they do not collide by moving the event farthest on the right to the right first, then each sequential event to the left.  Well, thats the plan anyway...

Im running into trouble sorting 3 events.  2 works fine.  The first time they move, the events are sorted improperly causing a collision, but the second time they move, they are sorted correctly.  The two Mirror Events that are bumping into each other moving Right (which is what I do not want) are .mirror 0 and 3 so they should be falling into the else loop in the code above.

Ive tried resorting several times and changing the direction.  I need something that is consistent.

Replacing d == 6 with just bx <=> ax works, but only the first time, then it causes improper sorting later.

Suggestions?
Title: Re: Custom Array Sort Order...
Post by: KK20 on April 21, 2015, 02:00:55 pm
I just want to say I don't really have a clue how this script is being used. Your comparison method also makes no sense as it is doing the same thing regardless of the events' mirror value.

Using the following setup:
(E2) (Player) (E1) (E3)

while also assuming the player is moving right and all 3 events are mirroring horizontally (and should thus all move left), your sorting would result as follows:
E1 <=> E2 : 1
E1 <=> E3 : -1
E2 <=> E3 : -1
[E2, E1, E3]

Which I would then presume that you then evaluate left to right in moving each event to the left.

Now if the direction was the player moving left, all events should move right.
E1 <=> E2 : 1
E1 <=> E3 : -1
E2 <=> E3 : -1
[E2, E1, E3]

Oh look, it's the exact same thing. Use negative values instead.
-E1 <=> -E2 : -1
-E1 <=> -E3 : 1
-E2 <=> -E3 : 1
[E3, E1, E2]
Title: Re: Custom Array Sort Order...
Post by: Heretic86 on April 29, 2015, 11:35:14 am
No worries, already got it.  I was stumped there for a day or so, but the conflict came from elsewhere.  Solution was to split up the events organized by opposing coordinates.  IE, moving vertical, organize horizontally first.  All good tho.  Check it out in Mirror Movements when I upload like a mo-fo tomorrow.

Um, theres probably gonna be a mod overwhelmed by me tomorrow.  Posting 9 new scripts...
Title: Re: Custom Array Sort Order...
Post by: Vipervenone on May 14, 2015, 11:47:53 pm
it is hard to understand I was studying.