Last active
February 14, 2018 21:35
-
-
Save ts12311122014/ab683023bea6563cbc178766060cf7fc to your computer and use it in GitHub Desktop.
A fillArray() function for SAMP, which fills a whole array with a single value. It is also much faster than a loop with the same functionality.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* fillArray() function for samp | |
* | |
* Copyright 2018 Tango | |
* Distributed under the terms of the GNU General Public License version 3. | |
* | |
* A procedure which fills an array with a single value. | |
* It is much faster (depending on array size,but 18-70 times faster are achievable) than a regular loop with the same functionality. | |
* | |
* It works only for linear (one-dimensional) arrays. | |
* If you want to set a value of a two-dimensional array, use a loop of fillArray()s. | |
* | |
* Just a note, if your task is to zero-out an array, maybe using "array1 = array2;" would be wiser. | |
*/ | |
stock fillArray(array[], value = 0, size = sizeof(array)) { | |
static cod; | |
new cip; | |
if (!cod) | |
cod = get_code_relative_address(); | |
size *= 4; // the emit fill instructino requires bytes, not cells | |
// change the third parameter of #emit fill, for the size variable | |
#emit load.alt cod | |
#emit lctrl 6 | |
#emit add | |
#emit add.c 56 | |
#emit stor.s.pri cip | |
#emit load.s.alt size | |
#emit sref.s.alt cip | |
// fill the array | |
#emit load.s.alt array | |
#emit load.s.pri value // pawn implementor guide has an error: FILL's parameters are not [PRI], [ALT] and number, but PRI, [ALT] and number | |
#emit fill 0 // this "0" value is the parameter that will change dynamically at runtime | |
} | |
// lref and sref use addresses relative to the DAT segment. | |
// So for addressing like lref [cod] to work, it should be actually lref [cod-dat] | |
stock get_code_relative_address() { | |
new dat, cod; | |
#emit lctrl 1 | |
#emit neg | |
#emit add.c 12 | |
#emit stor.s.pri cod | |
#emit lref.s.pri cod | |
#emit stor.s.pri cod | |
#emit lctrl 1 | |
#emit neg | |
#emit add.c 16 | |
#emit stor.s.pri dat | |
#emit lref.s.pri dat | |
#emit stor.s.pri dat | |
return cod - dat; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment