ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


new:array

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
new:array [2018/02/21 00:06] korshunnew:array [2018/02/21 13:57] korshun
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 13: Line 15:
  
 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. 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.
 +
 +<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 of numbers: Example: sorting a simple array of numbers:
Line 40: Line 46:
 </code> </code>
  
-Example: making a scoreboard sorted by score:+Example: making a scoreboard sorted by score (uses [[swap|swap()]] for readability):
  
 <code> <code>
Line 73: Line 79:
 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 87: Line 96:
 } }
 </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]''.
 +
 +Example:
 +
 +<code>
 +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");
 +}
 +</code>
 +
 +
new/array.txt · Last modified: 2018/02/21 13:57 by korshun