Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////
- //Begin Grid Positioning
- ///////////////////////////////
- const string GRID_BASE_NAME = "SAGrid_";
- //Arguments to this function should be the x,y,z position of the node (0,0,0) & the x/y/z scale of this node.
- void Grid_Initiate(float originX, float originY, float originZ, float scaleFactorX, float scaleFactorY, float scaleFactorZ)
- {
- SetLocalVarInt("GP_ORIGIN_X",int(1000.0f*originX));
- SetLocalVarInt("GP_ORIGIN_Y",int(1000.0f*originY));
- SetLocalVarInt("GP_ORIGIN_Z",int(1000.0f*originZ));
- SetLocalVarInt("GP_SCALE_X",int(1000.0f*scaleFactorX));
- SetLocalVarInt("GP_SCALE_Y",int(1000.0f*scaleFactorY));
- SetLocalVarInt("GP_SCALE_Z",int(1000.0f*scaleFactorZ));
- //Min/Max Coords
- SetLocalVarInt("GP_MIN_X",_Grid_GetMinX()); SetLocalVarInt("GP_MAX_X",_Grid_GetMaxX());
- SetLocalVarInt("GP_MIN_Y",_Grid_GetMinY()); SetLocalVarInt("GP_MAX_Y",_Grid_GetMaxY());
- SetLocalVarInt("GP_MIN_Z",_Grid_GetMinZ()); SetLocalVarInt("GP_MAX_Z",_Grid_GetMaxZ());
- AddDebugMessage("" + GetLocalVarInt("GP_MIN_X") + " -> " + GetLocalVarInt("GP_MAX_X") + " + " + GetLocalVarInt("GP_SCALE_X"),false);
- AddDebugMessage("" + GetLocalVarInt("GP_MIN_Y") + " -> " + GetLocalVarInt("GP_MAX_Y") + " + " + GetLocalVarInt("GP_SCALE_Y"),false);
- AddDebugMessage("" + GetLocalVarInt("GP_MIN_Z") + " -> " + GetLocalVarInt("GP_MAX_Z") + " + " + GetLocalVarInt("GP_SCALE_Z"),false);
- }
- //Start tracking an entity (Does NOT support *, but does support Player)
- void Grid_StartTracking(string &in asEntity)
- {
- for(int xx=GetLocalVarInt("GP_MIN_X"); xx<GetLocalVarInt("GP_MAX_X"); xx+= GetLocalVarInt("GP_SCALE_X"))
- for(int yy=GetLocalVarInt("GP_MIN_Y"); yy<GetLocalVarInt("GP_MAX_Y"); yy+= GetLocalVarInt("GP_SCALE_Y"))
- for(int zz=GetLocalVarInt("GP_MIN_Z"); zz<GetLocalVarInt("GP_MAX_Z"); zz+= GetLocalVarInt("GP_SCALE_Z"))
- {
- string cellName = GRID_BASE_NAME+"("+_GCTS(xx)+","+_GCTS(yy)+","+_GCTS(zz)+")";
- AddEntityCollideCallback(asEntity,cellName,"_cb_Grid_OnMove",false,0);
- }
- SetLocalVarInt(asEntity+"_C",0);
- SetLocalVarFloat(asEntity+"_X",0);
- SetLocalVarFloat(asEntity+"_Y",0);
- SetLocalVarFloat(asEntity+"_Z",0);
- }
- //Returns where an object is as an array of positions [x,y,z]
- float[] Grid_GetTrackedObjectPosition(string &in asEntity)
- {
- float[] position = { 0,0,0 };
- int c = GetLocalVarInt(asEntity+"_C");
- if(c<=0) { AddDebugMessage("Warning: Untracked " + asEntity + " or oob? c=" + c,false); return position; }
- //Get all the tracking entities that this object intersects
- position[0] = GetLocalVarFloat(asEntity+"_X") / c;
- position[1] = GetLocalVarFloat(asEntity+"_Y") / c;
- position[2] = GetLocalVarFloat(asEntity+"_Z") / c;
- return position;
- }
- //Translate world coords into a grid entity name
- string Grid_WorldCoordToGrid(float x, float y, float z)
- {
- //Translate to scale int form
- int sX = int(1000.0f*x); int sY = int(1000.0f*y); int sZ = int(1000.0f*z);
- //Clamp to grid
- sX = sX - sX % GetLocalVarInt("GP_SCALE_X");
- sY = sY - sY % GetLocalVarInt("GP_SCALE_Y");
- sZ = sZ - sZ % GetLocalVarInt("GP_SCALE_Z");
- //Return name
- return GRID_BASE_NAME + "(" + _GCTS(sX) +","+ _GCTS(sY) +","+ _GCTS(sZ) + ")";
- }
- void _cb_Grid_OnMove(string &in asParent, string &in asChild, int alState)
- {
- //Determine scaled xyz coords
- float[] xyz = parseStringToFloatArray(asChild);
- if(xyz.length() < 3) { AddDebugMessage("Error parsing: " + asChild,false); return; }
- //Translate into object world coord sum & store for getter function
- AddLocalVarFloat(asParent + "_X", (xyz[0] + float(GetLocalVarInt("GP_ORIGIN_X")) * 0.001f) * alState);
- AddLocalVarFloat(asParent + "_Y", (xyz[1] + float(GetLocalVarInt("GP_ORIGIN_Y")) * 0.001f) * alState);
- AddLocalVarFloat(asParent + "_Z", (xyz[2] + float(GetLocalVarInt("GP_ORIGIN_Z")) * 0.001f) * alState);
- AddLocalVarInt(asParent + "_C",alState);
- }
- //Min/Max coords in scaled form. Assumes 0,0,0 (origin) exists.
- int _Grid_GetMinX()
- {
- int x = 0;
- while(GetEntityExists(GRID_BASE_NAME+"("+_GCTS(x)+",0.000,0.000)")) x -= GetLocalVarInt("GP_SCALE_X");
- return x+GetLocalVarInt("GP_SCALE_X");
- }
- int _Grid_GetMinY()
- {
- int x = 0;
- while(GetEntityExists(GRID_BASE_NAME+"(0.000,"+_GCTS(x)+",0.000)")) x -= GetLocalVarInt("GP_SCALE_Y");
- return x+GetLocalVarInt("GP_SCALE_Y");
- }
- int _Grid_GetMinZ()
- {
- int x = 0;
- while(GetEntityExists(GRID_BASE_NAME+"(0.000,0.000,"+_GCTS(x)+")")) x -= GetLocalVarInt("GP_SCALE_Z");
- return x+GetLocalVarInt("GP_SCALE_Z");
- }
- int _Grid_GetMaxX()
- {
- int x = 0;
- while(GetEntityExists(GRID_BASE_NAME+"("+_GCTS(x)+",0.000,0.000)")) x += GetLocalVarInt("GP_SCALE_X");
- return x-GetLocalVarInt("GP_SCALE_X");
- }
- int _Grid_GetMaxY()
- {
- int x = 0;
- while(GetEntityExists(GRID_BASE_NAME+"(0.000,"+_GCTS(x)+",0.000)")) x += GetLocalVarInt("GP_SCALE_Y");
- return x-GetLocalVarInt("GP_SCALE_Y");
- }
- int _Grid_GetMaxZ()
- {
- int x = 0;
- while(GetEntityExists(GRID_BASE_NAME+"(0.000,0.000,"+_GCTS(x)+")")) x += GetLocalVarInt("GP_SCALE_Z");
- return x-GetLocalVarInt("GP_SCALE_Z");
- }
- //Coords to string for grid
- string _GCTS(int coord)
- {
- int integer = (coord/1000);
- string decimal = "" + int(coord - integer * 1000);
- while(decimal.length() < 3) decimal += "0";
- return "" + integer + "." + decimal;
- }
- ///////////////////////////////
- //End Grid Positioning
- ///////////////////////////////
- ///////////////////////////////
- //Begin String Parsing
- ///////////////////////////////
- /* String Parsing functions V3.0 *\
- *
- * float[] parseStringToFloatArray(string &in asString)- parses all floats out of a string. Signs, exponent & decimals supported
- * int[] parseStringToIntArray(string &in asString) - Parses all integers out of a string. Signs & exponent supported.
- * uint parseStringUInt(string &in asString) - Fast UINT parser over entire string (Extracts digits)
- *
- \* Written by: Apjjm */
- ///////////////////////////////
- //+ Float Parsing
- ///////////////////////////////
- float[] parseStringToFloatArray(string &in asString)
- {
- float[] output = {};
- int integer = 0; //Integer part of the number
- float decimal = 0; //Decimal part of the number
- int exponent = 0; //Exponent of the number
- bool negativeInteger = false; //Is the integer part negative
- bool negativeExponent = false; //Is the exponent part negative
- double dMul = 1; //Multiplier for the decimal
- bool readNumber = false; //Read a number yet or not (stops +. etc returning 0's).
- //Parsing loop
- int state = 0;
- string temp = asString + "!"; //Add an invalid character to end of string
- for(uint i=0; i<temp.length(); i++)
- {
- //Determine properties about this character
- uint8 chr = temp[i];
- int digit = _parseDigit(chr);
- int sign = _parseSign(chr);
- bool isExponent = _parseHasExponent(chr);
- bool isDecimalP = _parseHasDecimalPoint(chr);
- bool isWhitesp = _parseHasWhitespace(chr);
- bool isDigit = digit != -1;
- switch(state)
- {
- case 0: //Initial state
- {
- if(sign != 0) { negativeInteger=(sign<0); integer =0; state=1; } //Read +/-XX
- else if(isDigit) { integer = digit; state = 1; readNumber=true; } //Read N
- else if(isDecimalP){ state = 2; } //Read .XX -> Assume 0.XX
- break;
- }
- case 1: //Read the number part of the float
- {
- if(isDigit) { integer = (integer * 10) + digit; readNumber=true; } //Read NNXX
- else if(isDecimalP) { state = 2; } //Read NN.XX
- else if(isExponent && readNumber) { state = 3; } //Read NNeXX -> assume NN.0eXX
- else if(readNumber) { state = -1; } //Finished reading the number.
- else { state = 0; }
- break;
- }
- case 2: //Read the decimal part of the float
- {
- if(isDigit) { decimal += float(digit) * dMul; dMul *= 0.1; readNumber=true; }
- else if(isExponent && readNumber) { state = 3; }
- else if(readNumber) { state = -1; }
- else { state = 0; }
- break;
- }
- case 3: //Ignore a single whitespace char after the exponent.
- {
- if(isDigit) { exponent = digit; state = 5; } //Read NNeXX
- else if(isWhitesp) { state = 4; } //Read NNe XX
- else if(sign != 0) { negativeExponent=(sign<0); state = 5; }
- else { state = -1; }
- break;
- }
- case 4: //Read a +/- Sign or a digit for the exponent
- {
- if(isDigit) { exponent = digit; state = 5; } //Read NNe NX
- else if(sign != 0) { negativeExponent=(sign<0); state = 5; } //Read NNe +/-XX
- else { state = -1; }
- break;
- }
- case 5: //Reading the exponent
- {
- if(isDigit) { exponent = (exponent * 10) + digit; } //Read NNe NX
- else { state = -1; }
- break;
- }
- }
- //At the end of a number?
- if(state < 0)
- {
- //Construct the number & add it to the output array
- int index = output.length(); output.resize(index+1);
- output[index] = _parseFloatHelper(integer,decimal,exponent,negativeInteger,negativeExponent);
- //Reset parsing state
- state = 0; integer=0; decimal=0; exponent=0; dMul =1;
- negativeInteger=false; negativeExponent=false; readNumber=false;
- //This terminating char was possibly the start of next number. Start from this character
- if(isDecimalP || sign!=0) i-=1;
- }
- }
- return output;
- }
- float _parseFloatHelper(int integer, float decimal, int exponent, bool negI, bool negE)
- {
- //Calculate the number with no exponent applied
- float x = float(integer) + decimal * 0.1f;
- //Apply 10^E
- float mul = negE?0.1f:10.0f;
- for(int i=0; i<exponent; i++)
- x = x*mul;
- //Apply sign of the number
- return negI?-x:x;
- }
- ///////////////////////////////
- //- Float Parsing
- ///////////////////////////////
- ///////////////////////////////
- //+ Integer Parsing
- ///////////////////////////////
- int[] parseStringToIntArray(string &in asString)
- {
- int[] output = {};
- int integer = 0; //Integer part of the number
- int exponent = 0; //Exponent of the number
- bool negativeInteger = false; //Is the integer part negative
- bool negativeExponent = false; //Is the exponent part negative
- bool readNumber = false; //Read a number yet or not (stops +e etc returning 0's).
- //Parsing loop
- int state = 0;
- string temp = asString + "!"; //Add an invalid character to end of string
- for(uint i=0; i<temp.length(); i++)
- {
- //Determine properties about this character
- uint8 chr = temp[i];
- int digit = _parseDigit(chr);
- int sign = _parseSign(chr);
- bool isExponent = _parseHasExponent(chr);
- bool isWhitesp = _parseHasWhitespace(chr);
- bool isDigit = digit!=-1;
- switch(state)
- {
- case 0: //Initial state
- {
- if(sign != 0) { negativeInteger=(sign<0); integer =0; state=1; } //Read +/-XX
- else if(isDigit) { integer = digit; state = 1; readNumber=true; } //Read N
- break;
- }
- case 1: //Read the number part
- {
- if(isDigit) { integer = (integer * 10) + digit; readNumber=true; } //Read NNXX
- else if(isExponent && readNumber) { state = 3; } //Read NNeXX -> assume NN.0eXX
- else if(readNumber) { state = -1; } //Finished reading the number.
- else { state = 0; }
- break;
- }
- case 3: //Ignore a single whitespace char after the exponent.
- {
- if(isDigit) { exponent = digit; state = 5; } //Read NNeXX
- else if(isWhitesp) { state = 4; } //Read NNe XX
- else if(sign != 0) { negativeExponent=(sign<0); state = 5; }
- else { state = -1; }
- break;
- }
- case 4: //Read a +/- Sign or a digit for the exponent
- {
- if(isDigit) { exponent = digit; state = 5; } //Read NNe NX
- else if(sign != 0) { negativeExponent=(sign<0); state = 5; } //Read NNe +/-XX
- else { state = -1; }
- break;
- }
- case 5: //Reading the exponent
- {
- if(isDigit) { exponent = (exponent * 10) + digit; } //Read NNe NX
- else { state = -1; }
- break;
- }
- }
- //At the end of a number?
- if(state < 0)
- {
- //Construct the number & add it to the output array
- int index = output.length(); output.resize(index+1);
- output[index] = _parseIntHelper(integer,exponent,negativeInteger,negativeExponent);
- //Reset parsing state
- state = 0; integer=0; exponent=0; readNumber=false;
- negativeInteger=false; negativeExponent=false;
- //This terminating char was possibly the start of next number. Start from this character
- if(sign!=0) i-=1;
- }
- }
- return output;
- }
- int _parseIntHelper(int integer, int exponent, bool negI, bool negE)
- {
- //Calculate the number with no exponent applied
- int x = integer;
- //Apply 10^E
- for(int i=0; i<exponent; i++)
- x = negE?(x/10):(x*10);
- //Apply sign of the number
- return negI?-x:x;
- }
- ///////////////////////////////
- //- Integer Parsing
- ///////////////////////////////
- ///////////////////////////////
- //+ Fast UINT Parse
- ///////////////////////////////
- uint parseStringUInt(string &in asString) {
- uint output = 0;
- for(uint i=0; i<asString.length(); i++) {
- int digit = _parseDigit(asString[i]);
- output = (digit != -1)?(10*output+digit):(output); }
- return output;
- }
- ///////////////////////////////
- //-
- ///////////////////////////////
- //+ Parsing Helper Functions
- int _parseDigit(uint8 digit) {
- int d = digit-48; //48 is ASCII code for 0
- return ((d >= 0)&&(d<=9)) ? d : -1;
- }
- int _parseSign(uint8 chr) {
- if (chr == 45) return -1;
- else if(chr == 43) return 1;
- else return 0;
- }
- bool _parseHasExponent(uint8 chr) { return (chr == 69) || (chr == 101); }
- bool _parseHasDecimalPoint(uint8 chr) { return (chr==46); }
- bool _parseHasWhitespace(uint8 chr) { return (chr == 43)||(chr == 32); }
- //-
- ///////////////////////////////
- //End String Parsing
- ///////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement