Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Calin Leafshade on Wed 25/11/2009 00:59:25

Title: Sorting Algorithm
Post by: Calin Leafshade on Wed 25/11/2009 00:59:25
I'm having a kind of mental block.
I know this is a simple thing to do but for some reason my brain isnt working.

I have an array of characters with various attributes (agility etc. Characters[0].Agl )
however they also have a boolean flag which indicates whether or not they have had their turn in combat yet. (Characters[0].HadTurn)

I need to find which character in the array has the highest agility but also has not had their turn yet.

Helpz? lolz ^___^ kthxbi
Title: Re: Sorting Algorithm
Post by: Crimson Wizard on Wed 25/11/2009 01:07:21
Why, thats terribly simple.

int i;
int MaxAgl;
int iChar = -1;

while (i < MAXCHARACTERS)
{
  if (!Characters[ i ].HadTurn)
 {
     if (Characters[ i ].Agl > MaxAgl)
     {
          MaxAgl = Characters[ i ].Agl;
          iChar = i;
     }
 }
 i++;
}

iChar will be index of character you need, or -1 if all characters had their turn.
Title: Re: Sorting Algorithm
Post by: Calin Leafshade on Wed 25/11/2009 01:09:47
much obliged wizard.. that is alarmingly simple.

I feel stupid now.
Title: Re: Sorting Algorithm
Post by: Crimson Wizard on Wed 25/11/2009 12:08:04
Oh, and yes, you may also make condition

     if (Characters[ i ].Agl > MaxAgl || Characters[ i ].Agl == MaxAgl && Random(99) < 50)

This will make an action order between characters with same Agility a bit randomized ;)
(Otherwise they will act in order of their appearance in characters array, which is not really fair, IMO)