├── BigInt.inc └── README.md /BigInt.inc: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | *** v1.1.3 *** 4 | * BIGINT FOR SAMP! 5 | * 6 | * Creator: HPQ123 (HPQ123#8114 discord) 7 | * 8 | */ 9 | 10 | #if defined BigIntLibrary 11 | #endinput 12 | #endif 13 | #define BigIntLibrary 14 | 15 | new bStr[32]; 16 | 17 | #define BigInt:%1; eBit:ByteVar1@%1,eBit:ByteVar2@%1; 18 | #define BT:<%1> ByteVar1@%1,ByteVar2@%1 19 | 20 | #if !defined IS_IN_RANGE 21 | #define IS_IN_RANGE(%0,%1,%2) (((%0)-((%1)+cellmin))<((%2)-((%1)+cellmin))) // credits: Y_less 22 | #endif 23 | 24 | #define Bytes1024\ 25 | (1000000000) 26 | 27 | #define formatBytes(%1)\ 28 | _formatBytes(BT:<%1>) 29 | 30 | #define resetBigInt(%1)\ 31 | ByteVar1@%1=ByteVar2@%1=eBit:0 32 | 33 | #define converBytes(%1,%2)\ 34 | extractBytes(BT:<%1>,%2) 35 | 36 | #define addBytes32(%1,%2) whereBytes(BT:<%1>,%2) 37 | stock whereBytes(&eBit:a,&eBit:b,value) { 38 | if(value<0) { 39 | value=-value; 40 | a = a - eBit:(value / Bytes1024); 41 | if(a && b < eBit:Bytes1024) b = eBit:((_:b + Bytes1024) - (value % Bytes1024)), --a; else b = eBit:(_:b - (value % Bytes1024)); 42 | if(b >= eBit:Bytes1024) ++a, b -= eBit:Bytes1024; 43 | } else a = a + eBit:((_:b + value) / Bytes1024), b = eBit:((_:b + value) % Bytes1024); 44 | return 1; 45 | } 46 | 47 | #define bytes32(%1) _bytes32(BT:<%1>) 48 | stock _bytes32(eBit:a,eBit:b) { 49 | switch(_:a) { 50 | case -1..1: return (_:a * Bytes1024) + _:b; 51 | case -2: return _:b > cellmax%Bytes1024 ? -cellmax : -((_:a * Bytes1024) + _:b); 52 | case 2: return _:b > cellmax%Bytes1024 ? cellmax : ((_:a * Bytes1024) + _:b); 53 | default: return _:a > -1 ? cellmax : -cellmax; 54 | } 55 | return 0; 56 | } 57 | #define addBytes64(%1,%2) extractBytes(BT:<%1>,%2) 58 | stock extractBytes(&eBit:a,&eBit:b,const value[],l=-1,val=0) { 59 | if(value[0] != '-') { 60 | if((l=strmid(bStr, value, 0, 20)) > 9) { 61 | b = b + eBit:strval(bStr[(l = l - 9)]), bStr[l] = '\0'; 62 | a = a + eBit:strval(bStr); 63 | if(_:b > Bytes1024 - 1) b = b - eBit:Bytes1024, ++a; 64 | return 1; 65 | } 66 | b = b + eBit:strval(bStr); 67 | if(_:b > Bytes1024 - 1) b = b - eBit:Bytes1024, ++a; 68 | } else { 69 | if((l=strmid(bStr, value, 1, 21)) > 9) { 70 | val = strval(bStr[(l = l - 9)]), bStr[l] = '\0'; 71 | a = a - eBit:strval(bStr); 72 | whereBytes(a, b, _:a>-1?-val:val); 73 | return 1; 74 | } 75 | whereBytes(a, b, _:a>-1?-strval(bStr):strval(bStr)); 76 | } 77 | return 1; 78 | } 79 | stock isBigInt(const bigInt[], i=-1) { 80 | if(bigInt[0]=='-'&&bigInt[1]) ++i; 81 | while(IS_IN_RANGE(bigInt[++i],48,58)) {} 82 | return !bigInt[i]; 83 | } 84 | 85 | #define OPByte(%1,%2,%3) bytesOperator(BT:<%1>,#%2,%3) 86 | stock bytesOperator( 87 | eBit:bOP1,eBit:OP1,const _oper[],const _oper2[], 88 | eBit:bVal=eBit:0, eBit:Val=eBit:0 89 | ) { 90 | extractBytes(bVal,Val,_oper2); 91 | switch _oper[0]+_oper[1] 92 | do { 93 | case '='+'=': if(bOP1==bVal&&OP1==Val) return true; 94 | case '>'+'=': if((bOP1>bVal||(bOP1==bVal&&OP1>Val))||bOP1==bVal&&OP1==Val) return true; 95 | case '<'+'=': if((bVal>bOP1||(bOP1==bVal&&Val>OP1))||bOP1==bVal&&OP1==Val) return true; 96 | case '!'+'=': if((bOP1==bVal&&OP1==Val)) return true; 97 | case '<': if(bVal>bOP1||(bOP1==bVal&&Val>OP1)) return true; 98 | case '>': if(bOP1>bVal||(bOP1==bVal&&OP1>Val)) return true; 99 | default: { 100 | #if defined PrintBacktrace 101 | PrintBacktrace(); 102 | #else 103 | printf("[BigInt ERROR] Invalid operator '%s'!", _oper); 104 | #endif 105 | } 106 | } 107 | return false; 108 | } 109 | 110 | #define valueBigInt(%1) stringBytes(BT:<%1>) 111 | stock stringBytes(eBit:a,eBit:b) { 112 | if(!a) format(bStr,sizeof bStr,"%d", _:b); 113 | else format(bStr,sizeof bStr,"%d%09d", _:a, _:b); 114 | return bStr; 115 | } 116 | 117 | stock _formatBytes(eBit:a,eBit:b, const chars[] = ".", l=-1) { 118 | stringBytes(a,b),l=strlen(bStr); 119 | while((l = l - 3) > 0) strins(bStr, chars, l); 120 | return bStr; 121 | } 122 | stock formatStrNumber(const int[], const chars[] = ".", l=-1) { 123 | bStr[0]='\0',l=strcat(bStr,int); 124 | while((l = l - 3) > 0) strins(bStr, chars, l); 125 | return bStr; 126 | } 127 | #if defined cache_get_value_name 128 | #define cache_get_value_name_bigint(%1,%2,%3) \ 129 | (resetBigInt(%1),cache_get_value_name(%2,%3,bStr)&&extractBytes(BT:<%1>,bStr)) 130 | #endif 131 | #if defined cache_get_field_content 132 | #define cache_get_field_content_bigint(%1,%2,%3)\ 133 | (resetBigInt(%1),cache_get_field_content(%2,%3,bStr)&&extractBytes(BT:<%1>,bStr)) 134 | #endif 135 | #if defined cache_get_value_name_bigint && !defined cache_get_value_name 136 | || defined cache_get_field_content_bigint && !defined cache_get_field_content 137 | #error The function you are using does not match mysql 138 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BIGINT for SAMP! 2 | 3 | ## Installation 4 | 5 | Include in your code and begin using the library: 6 | 7 | ```pawn 8 | #include 9 | ``` 10 | ## Usage 11 | 12 | To create a variable 13 | ```pawn 14 | new BigInt:var_name; 15 | ``` 16 | This function adds numbers in bigint (max 2147483647/callback) 17 | ```pawn 18 | addBytes32(var_name, 1231231231); 19 | ``` 20 | 21 | With this function you can add over the limit of 2147483647. It is recommended to use this function only when large numbers are needed. 22 | ```pawn 23 | addBytes64(var_name, #3453453453453525534); 24 | ``` 25 | 26 | This function formats your bigint variable. 27 | ```pawn 28 | formatBytes(var_name); 29 | 30 | output example: 31 | // 312.123.512.523 32 | ``` 33 | 34 | This function sets the variable to 0. 35 | ```pawn 36 | resetBigInt(var_name); 37 | ``` 38 | 39 | This function returns the number as a string. 40 | ```pawn 41 | valueBigInt(var_name); 42 | ``` 43 | 44 | This function converts from 64 bits to 32 bits. 45 | ```pawn 46 | bytes32(var_name); 47 | ``` 48 | 49 | With this function you can do some operations for the bigint variable. 50 | ```pawn 51 | OPByte(var_name, operator, oper1); 52 | 53 | // Operators: 54 | >= 55 | <= 56 | == 57 | != 58 | < 59 | > 60 | 61 | // example: 62 | if(OPByte(var_name, <=, #10000)) return true; 63 | ``` 64 | 65 | This function checks for string numbers. 66 | ```pawn 67 | isBigInt(const string[]); 68 | ``` 69 | 70 | This function transforms from a string to bigint. (be careful there are numbers in that string!). 71 | ```pawn 72 | converBytes(var_name, const string[]); 73 | ``` 74 | These functions are compatible on mysql r41-4 and r-39-6 75 | ```pawn 76 | cache_get_value_name_bigint(var_name, row, column[]); // r41-4 77 | cache_get_field_content_bigint(var_name, row, column[]); // r39-6 78 | ``` 79 | 80 | To make it compatible with a money system: 81 | ```pawn 82 | new BigInt:cash[MAX_PLAYERS]; 83 | 84 | #define GivePlayerCash(%0,%1) addBytes32(cash[%0],%1) 85 | #define GetPlayerCash(%0) bytes32(cash[%0]) 86 | 87 | // show money 88 | new str[50]; 89 | format(str, sizeof str, "%s", formatBytes(cash[playerid])); 90 | 91 | // login 92 | cache_get_value_name_bigint(cash[playerid],0,"Money"); 93 | 94 | // disconect save 95 | public OnPlayerDisconect(playerid) { 96 | new query[128]; 97 | mysql_format(handle, query, "UPDATE `users` SET `Money` = '%s' ...", valueBigInt(cash[playerid])); 98 | mysql_tquery(handle, query); 99 | return true; 100 | } 101 | 102 | ``` 103 | --------------------------------------------------------------------------------