ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
new:array [2018/02/20 23:38]
korshun created
new:array [2018/02/21 13:57] (current)
Line 2: Line 2:
 <note warning>These functions will be added in ACSUtils 1.8.0</note> <note warning>These functions will be added in ACSUtils 1.8.0</note>
 +Array sorting and shuffling functions use script names as callbacks.
 ===== SortArray ===== ===== SortArray =====
Line 7: Line 9:
 ''void SortArray(int begin, int end, str lt, str swap)'' ''void SortArray(int begin, int end, str lt, str swap)''
-Sorts an array with indices in range [begin, end).+Sorts an array with indices in range [begin, end). ''lt'' and ''swap'' are names of scripts that the function will call. 
 +  * ''script "lt" (int a, int b)'' must return the result of ''array[a] < array[b]'' using [[zdoom>SetReturnValue]]. If you return ''array[a] > array[b]'', sorting order is reversed. 
 +  * ''script "swap" (int a, int b)'' must swap ''array[a]'' with ''array[b]''.
-''lt'' is the name of a script to compare two elements. It must return comparison result using [[zdoom>SetResultValue]].+The sorted array may actually be an array of objects with multiple fields. ''lt'' can implement multi-field sorting. ''swap'' can swap all fields in the two array cells.
-''swap'' is the name of a script to swap two elements.+<note important>''SortArray'' can sort up to ~2000 elements before hitting ZDoom's 2 million instructions per tic runaway limit. Maximum element count may depend on callback complexity and the initial state of the array.</note>
-Example: sorting a simple array:+Example: sorting a simple array of numbers:
 <code> <code>
Line 39: Line 44:
 </code> </code>
-Example: sorting a scoreboard by score+Example: making a scoreboard sorted by score (uses [[swap|swap()]] for readability):
 <code> <code>
-int Players[MAX_PLAYERS];+int PlayerNumbers[MAX_PLAYERS];
 int PlayerScores[MAX_PLAYERS]; int PlayerScores[MAX_PLAYERS];
 int NumPlayers; int NumPlayers;
Line 48: Line 53:
 script "lt" (int a, int b) script "lt" (int a, int b)
 { {
-    SetResultValue(PlayerScores[a] PlayerScores[b]);+    if (PlayerScores[a] == PlayerScores[b]) 
 +    { 
 +        // Scores are same, sort by name then 
 +        if (StrCmp(PlayerName(PlayerNumbers[a]), PlayerName(PlayerNumbers[b]) == -1); 
 +            return true; 
 +    } 
 +    // Using > instead of < reverses sorting by score 
 +    return PlayerScores[a] > PlayerScores[b];
 } }
 script "swap" (int a, int b) script "swap" (int a, int b)
 { {
-    swap(Players[a], Players[b]);+    swap(PlayerNumbers[a], PlayerNumbers[b]);
     Players[a] = r1;     Players[a] = r1;
     Players[b] = r2;     Players[b] = r2;
Line 64: Line 77:
 function void BuildScoreboard(void) function void BuildScoreboard(void)
 { {
 +    // Clear scoreboard
 +    NumPlayers = 0;
     // Fill scoreboard without sorting     // Fill scoreboard without sorting
     for (int player = 0; player < MAX_PLAYERS; player++)     for (int player = 0; player < MAX_PLAYERS; player++)
Line 69: Line 85:
         if (PlayerInGame(player))         if (PlayerInGame(player))
             continue;             continue;
-        Players[NumPlayers] = player;+        PlayerNumbers[NumPlayers] = player;
         PlayerScores[NumPlayers] = GET_PLAYER_SCORE(player);         PlayerScores[NumPlayers] = GET_PLAYER_SCORE(player);
         NumPlayers++;         NumPlayers++;
Line 78: Line 94:
 } }
 </code> </code>
 +===== ShuffleArray =====
 +''void ShuffleArray(int begin, int end, str swap)''
 +Randomly reorders elements of an array with indices in range [begin, end). ''swap'' is the name of a script ''script (int a, int b)'' that must swap ''array[a]'' with ''array[b]''.
 +int array[10];
 +script "swap" (int a, int b)
 +    int temp = array[a];
 +    array[a] = array[b];
 +    array[b] = temp;
 +function void ShuffleTheArray(void)
 +    ShuffleArray(0, 10, "swap");
new/array.1519162683.txt.gz · Last modified: 2018/02/20 23:38 by korshun