PDA

View Full Version : Manipulating Variables



SUCCESSOR
03-19-2015, 08:13 AM
I threw this together real quick. It's some functions to make use of variables especially in places where they are limited such as FFC arguments, Misc arrays, etc. I am not claiming ownership of this. This is free to use, dissect, expand as you see fit. If you do choose to redistribute in part or whole just don't claim as your own. Please post issues, suggestions, additions, and feedback.

I have a header for Equipment that I will be sharing soon. A WIP that has been in progress for quite sometime. That is a thread for another day.


//variables.zh

//truncate to Integer
int Int(float arg){
return arg<<0;
}
//return "Remainder" (fraction value)
float Remainder(int arg){
return arg - Int(arg);
}
//"Remainder" converted to Integer
int RemainderAsInt(int arg){ //function courtesy of Gleeok
return Remainder(arg) * 10000;
}
//Integer value converted to "Remainder"
float IntAsRemainder(int arg){
return Remainder(Int(arg) / 10000;);
}
//up to 18 "natural" bits to use for flags;
bool GetBit(int arg, int bit){
if(bit > 17 || bit < 0) return false;
return arg & (1<<bit) != 0;
}
//13 extra bits from decimal fraction values
bool GetRemainderBit(int arg, int bit){
if(bit > 12 || bit < 0) return false;
return GetBit(RemainderAsInt(arg)) != 0;
}
//Setting 18th bit (17) to true is unsafe(can set/unset other bits)
int SetBit(int arg, int bit, bool state){
if(bit > 17 || bit < 0) return arg;
if(state) return arg | (1 << bit);
return arg & ~(1 << bit);
}
int SetRemainderBit(int arg, int bit, bool state){
if(bit > 12 || bit < 0) return false;
int INT = Int(arg);
arg = IntAsRemainder(SetBit(RemainderAsInt(arg), bit, state));
return INT + arg;
}
//Combine 2 8bit (0 to 255 such as item IDs) numbers into one value to store in a single variable
int Combine8bitArgs(int high, int low){
low &= 0xFF;
high = (high & 0xFF)<<8;
return high|low;
}
//returns the leftmost 8 bits of a 16bit number from a variable as an 8bit number
int High8bitArg(int sixteenBit){
return (sixteenBit & 0xFF00)>>8;
}
//returns only the value of the first 8 bits
int Low8bitArg(int sixteenBit){
return sixteenBit & 0xFF;
}
//combine absolute values of an Integer and Remainder as one argument
float CombineIntAndRemainder(int INT, int REM){
int sign = 1;
if (INT < 0) sign = -1; //use the sign of the "Integer" sign of fraction is lost
INT = Abs(INT); REM = Abs(REM);
return (Int(INT) + Remainder(REM)) * sign;//fraction value of INT and integer value of REM are lost
}
//Grabs an integer value from
int GetPartialValueAsInt(int arg, int place, int digits){
place = Clamp(place, -4, 5);
int r;
int adj = 1;
for(int i = digits-1; i > -1; i--){
if(place - i < -4) break;
r += GetDigitAsInt(arg, place - i) * adj;
adj *= 10;
}
return r;
}
//get a single digit from variable
int GetDigitAsInt(int arg, int place){//based on function by Gleeok
place = Clamp(place, -4, 5);
if(place < 0){
arg = RemainderAsInt(arg);
place += 4;
}
return Int((arg / Pow(10, place)) % 10);
}