Advertisement
Apjjm

Amnesia TDD: Script Area Positioning

Oct 21st, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.77 KB | None | 0 0
  1. ///////////////////////////////
  2. //Begin Grid Positioning
  3. ///////////////////////////////
  4. const string GRID_BASE_NAME = "SAGrid_";
  5. //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.
  6. void Grid_Initiate(float originX, float originY, float originZ, float scaleFactorX, float scaleFactorY, float scaleFactorZ)
  7.  {
  8.     SetLocalVarInt("GP_ORIGIN_X",int(1000.0f*originX));
  9.     SetLocalVarInt("GP_ORIGIN_Y",int(1000.0f*originY));
  10.     SetLocalVarInt("GP_ORIGIN_Z",int(1000.0f*originZ));
  11.     SetLocalVarInt("GP_SCALE_X",int(1000.0f*scaleFactorX));
  12.     SetLocalVarInt("GP_SCALE_Y",int(1000.0f*scaleFactorY));
  13.     SetLocalVarInt("GP_SCALE_Z",int(1000.0f*scaleFactorZ));
  14.    
  15.     //Min/Max Coords
  16.     SetLocalVarInt("GP_MIN_X",_Grid_GetMinX()); SetLocalVarInt("GP_MAX_X",_Grid_GetMaxX());
  17.     SetLocalVarInt("GP_MIN_Y",_Grid_GetMinY()); SetLocalVarInt("GP_MAX_Y",_Grid_GetMaxY());
  18.     SetLocalVarInt("GP_MIN_Z",_Grid_GetMinZ()); SetLocalVarInt("GP_MAX_Z",_Grid_GetMaxZ());
  19.     AddDebugMessage("" + GetLocalVarInt("GP_MIN_X") + " -> " + GetLocalVarInt("GP_MAX_X") + " + " + GetLocalVarInt("GP_SCALE_X"),false);
  20.     AddDebugMessage("" + GetLocalVarInt("GP_MIN_Y") + " -> " + GetLocalVarInt("GP_MAX_Y") + " + " + GetLocalVarInt("GP_SCALE_Y"),false);
  21.     AddDebugMessage("" + GetLocalVarInt("GP_MIN_Z") + " -> " + GetLocalVarInt("GP_MAX_Z") + " + " + GetLocalVarInt("GP_SCALE_Z"),false);
  22.  }
  23.  
  24. //Start tracking an entity (Does NOT support *, but does support Player)
  25. void Grid_StartTracking(string &in asEntity)
  26.  {
  27.     for(int xx=GetLocalVarInt("GP_MIN_X"); xx<GetLocalVarInt("GP_MAX_X"); xx+= GetLocalVarInt("GP_SCALE_X"))
  28.      for(int yy=GetLocalVarInt("GP_MIN_Y"); yy<GetLocalVarInt("GP_MAX_Y"); yy+= GetLocalVarInt("GP_SCALE_Y"))
  29.       for(int zz=GetLocalVarInt("GP_MIN_Z"); zz<GetLocalVarInt("GP_MAX_Z"); zz+= GetLocalVarInt("GP_SCALE_Z"))
  30.       {
  31.         string cellName = GRID_BASE_NAME+"("+_GCTS(xx)+","+_GCTS(yy)+","+_GCTS(zz)+")";
  32.         AddEntityCollideCallback(asEntity,cellName,"_cb_Grid_OnMove",false,0);
  33.       }
  34.     SetLocalVarInt(asEntity+"_C",0);
  35.     SetLocalVarFloat(asEntity+"_X",0);
  36.     SetLocalVarFloat(asEntity+"_Y",0);
  37.     SetLocalVarFloat(asEntity+"_Z",0);
  38.  }
  39.  
  40. //Returns where an object is as an array of positions [x,y,z]
  41. float[] Grid_GetTrackedObjectPosition(string &in asEntity)
  42.  {
  43.     float[] position = { 0,0,0 };
  44.     int c = GetLocalVarInt(asEntity+"_C");
  45.     if(c<=0) { AddDebugMessage("Warning: Untracked " + asEntity + " or oob? c=" + c,false); return position; }
  46.    
  47.     //Get all the tracking entities that this object intersects
  48.     position[0] = GetLocalVarFloat(asEntity+"_X") / c;
  49.     position[1] = GetLocalVarFloat(asEntity+"_Y") / c;
  50.     position[2] = GetLocalVarFloat(asEntity+"_Z") / c;
  51.    
  52.     return position;
  53.  }
  54.  
  55. //Translate world coords into a grid entity name
  56. string Grid_WorldCoordToGrid(float x, float y, float z)
  57.  {
  58.     //Translate to scale int form
  59.     int sX = int(1000.0f*x); int sY = int(1000.0f*y); int sZ = int(1000.0f*z);
  60.     //Clamp to grid
  61.     sX = sX - sX % GetLocalVarInt("GP_SCALE_X");
  62.     sY = sY - sY % GetLocalVarInt("GP_SCALE_Y");
  63.     sZ = sZ - sZ % GetLocalVarInt("GP_SCALE_Z");
  64.     //Return name
  65.     return GRID_BASE_NAME + "(" + _GCTS(sX) +","+ _GCTS(sY) +","+ _GCTS(sZ) + ")";
  66.  }
  67.  
  68. void _cb_Grid_OnMove(string &in asParent, string &in asChild, int alState)
  69.  {
  70.     //Determine scaled xyz coords
  71.     float[] xyz = parseStringToFloatArray(asChild);
  72.     if(xyz.length() < 3) { AddDebugMessage("Error parsing: " + asChild,false); return; }
  73.     //Translate into object world coord sum & store for getter function
  74.     AddLocalVarFloat(asParent + "_X", (xyz[0] + float(GetLocalVarInt("GP_ORIGIN_X")) * 0.001f) * alState);
  75.     AddLocalVarFloat(asParent + "_Y", (xyz[1] + float(GetLocalVarInt("GP_ORIGIN_Y")) * 0.001f) * alState);
  76.     AddLocalVarFloat(asParent + "_Z", (xyz[2] + float(GetLocalVarInt("GP_ORIGIN_Z")) * 0.001f) * alState);
  77.     AddLocalVarInt(asParent + "_C",alState);
  78.  }
  79.  
  80. //Min/Max coords in scaled form. Assumes 0,0,0 (origin) exists.
  81. int _Grid_GetMinX()
  82.  {
  83.     int x = 0;
  84.     while(GetEntityExists(GRID_BASE_NAME+"("+_GCTS(x)+",0.000,0.000)")) x -= GetLocalVarInt("GP_SCALE_X");
  85.     return x+GetLocalVarInt("GP_SCALE_X");
  86.  }
  87. int _Grid_GetMinY()
  88.  {
  89.     int x = 0;
  90.     while(GetEntityExists(GRID_BASE_NAME+"(0.000,"+_GCTS(x)+",0.000)")) x -= GetLocalVarInt("GP_SCALE_Y");
  91.     return x+GetLocalVarInt("GP_SCALE_Y");
  92.  }
  93. int _Grid_GetMinZ()
  94.  {
  95.     int x = 0;
  96.     while(GetEntityExists(GRID_BASE_NAME+"(0.000,0.000,"+_GCTS(x)+")")) x -= GetLocalVarInt("GP_SCALE_Z");
  97.     return x+GetLocalVarInt("GP_SCALE_Z");
  98.  }
  99. int _Grid_GetMaxX()
  100.  {
  101.     int x = 0;
  102.     while(GetEntityExists(GRID_BASE_NAME+"("+_GCTS(x)+",0.000,0.000)")) x += GetLocalVarInt("GP_SCALE_X");
  103.     return x-GetLocalVarInt("GP_SCALE_X");
  104.  }
  105. int _Grid_GetMaxY()
  106.  {
  107.     int x = 0;
  108.     while(GetEntityExists(GRID_BASE_NAME+"(0.000,"+_GCTS(x)+",0.000)")) x += GetLocalVarInt("GP_SCALE_Y");
  109.     return x-GetLocalVarInt("GP_SCALE_Y");
  110.  }
  111. int _Grid_GetMaxZ()
  112.  {
  113.     int x = 0;
  114.     while(GetEntityExists(GRID_BASE_NAME+"(0.000,0.000,"+_GCTS(x)+")")) x += GetLocalVarInt("GP_SCALE_Z");
  115.     return x-GetLocalVarInt("GP_SCALE_Z");
  116.  }
  117.  
  118. //Coords to string for grid
  119. string _GCTS(int coord)
  120.  {
  121.     int integer = (coord/1000);
  122.     string decimal = "" + int(coord - integer * 1000);
  123.     while(decimal.length() < 3) decimal += "0";
  124.     return "" + integer + "." + decimal;
  125.  }
  126.  
  127. ///////////////////////////////
  128. //End Grid Positioning
  129. ///////////////////////////////
  130. ///////////////////////////////
  131. //Begin String Parsing
  132. ///////////////////////////////
  133. /* String Parsing functions V3.0 *\
  134.  *
  135.  * float[] parseStringToFloatArray(string &in asString)- parses all floats out of a string. Signs, exponent & decimals supported
  136.  * int[] parseStringToIntArray(string &in asString)    - Parses all integers out of a string. Signs & exponent supported.
  137.  * uint parseStringUInt(string &in asString)           - Fast UINT parser over entire string (Extracts digits)
  138.  *
  139. \* Written by: Apjjm             */
  140.  
  141. ///////////////////////////////
  142. //+ Float Parsing
  143. ///////////////////////////////
  144. float[] parseStringToFloatArray(string &in asString)
  145.  {
  146.    float[] output = {};
  147.    int integer = 0;      //Integer part of the number
  148.    float decimal = 0;      //Decimal part of the number
  149.    int exponent = 0;      //Exponent of the number
  150.    bool negativeInteger = false; //Is the integer part negative
  151.    bool negativeExponent = false; //Is the exponent part negative
  152.    double dMul = 1;             //Multiplier for the decimal
  153.    bool readNumber = false;      //Read a number yet or not (stops +. etc returning 0's).
  154.    
  155.    //Parsing loop
  156.    int state = 0;
  157.    string temp = asString + "!"; //Add an invalid character to end of string
  158.    for(uint i=0; i<temp.length(); i++)
  159.     {
  160.       //Determine properties about this character
  161.       uint8 chr = temp[i];
  162.       int  digit = _parseDigit(chr);
  163.       int sign     = _parseSign(chr);
  164.       bool isExponent = _parseHasExponent(chr);
  165.       bool isDecimalP = _parseHasDecimalPoint(chr);
  166.       bool isWhitesp  = _parseHasWhitespace(chr);
  167.       bool isDigit    = digit != -1;
  168.      
  169.       switch(state)
  170.       {
  171.          case 0: //Initial state
  172.             {
  173.                if(sign != 0)      { negativeInteger=(sign<0); integer =0; state=1; } //Read +/-XX
  174.                else if(isDigit)   { integer = digit; state = 1; readNumber=true; } //Read N
  175.                else if(isDecimalP){ state = 2; } //Read .XX -> Assume 0.XX
  176.                break;
  177.             }
  178.          case 1: //Read the number part of the float
  179.             {
  180.                if(isDigit)         { integer = (integer * 10) + digit; readNumber=true; } //Read NNXX
  181.                else if(isDecimalP) { state = 2; }            //Read NN.XX
  182.                else if(isExponent && readNumber) { state = 3; } //Read NNeXX -> assume NN.0eXX
  183.                else if(readNumber) { state = -1; }            //Finished reading the number.
  184.                else                { state = 0;  }
  185.                break;
  186.             }
  187.          case 2: //Read the decimal part of the float
  188.             {
  189.                if(isDigit)         { decimal += float(digit) * dMul; dMul *= 0.1; readNumber=true; }
  190.                else if(isExponent && readNumber) { state = 3; }
  191.                else if(readNumber) { state = -1; }
  192.                else                { state = 0;  }
  193.                break;
  194.             }
  195.          case 3: //Ignore a single whitespace char after the exponent.
  196.             {
  197.                if(isDigit)         { exponent = digit; state = 5; } //Read NNeXX
  198.                else if(isWhitesp)  { state = 4; } //Read NNe XX
  199.                else if(sign != 0)  { negativeExponent=(sign<0); state = 5; }
  200.                else                { state = -1; }
  201.                break;
  202.             }
  203.          case 4: //Read a +/- Sign or a digit for the exponent
  204.             {
  205.                if(isDigit)         { exponent = digit; state = 5; } //Read NNe NX
  206.                else if(sign != 0)  { negativeExponent=(sign<0); state = 5; } //Read NNe +/-XX
  207.                else                { state = -1; }
  208.                break;
  209.             }
  210.          case 5: //Reading the exponent
  211.             {
  212.                if(isDigit) { exponent = (exponent * 10) + digit; } //Read NNe NX
  213.                else        { state = -1; }
  214.                break;
  215.             }
  216.       }
  217.       //At the end of a number?
  218.       if(state < 0)
  219.        {
  220.           //Construct the number & add it to the output array
  221.          int index = output.length(); output.resize(index+1);
  222.          output[index] = _parseFloatHelper(integer,decimal,exponent,negativeInteger,negativeExponent);
  223.          //Reset parsing state
  224.          state = 0; integer=0; decimal=0; exponent=0; dMul =1;
  225.          negativeInteger=false; negativeExponent=false; readNumber=false;
  226.          //This terminating char was possibly the start of next number. Start from this character
  227.          if(isDecimalP || sign!=0) i-=1;
  228.        }
  229.    }
  230.    
  231.    return output;
  232.  }
  233.  
  234. float _parseFloatHelper(int integer, float decimal, int exponent, bool negI, bool negE)
  235.  {
  236.    //Calculate the number with no exponent applied
  237.    float x = float(integer) + decimal * 0.1f;
  238.    //Apply 10^E
  239.    float mul = negE?0.1f:10.0f;
  240.    for(int i=0; i<exponent; i++)
  241.       x = x*mul;
  242.    //Apply sign of the number
  243.    return negI?-x:x;
  244.  }
  245. ///////////////////////////////
  246. //- Float Parsing
  247. ///////////////////////////////
  248. ///////////////////////////////
  249. //+ Integer Parsing
  250. ///////////////////////////////
  251. int[] parseStringToIntArray(string &in asString)
  252.  {
  253.    int[] output = {};
  254.    int integer = 0;      //Integer part of the number
  255.    int exponent = 0;      //Exponent of the number
  256.    bool negativeInteger = false; //Is the integer part negative
  257.    bool negativeExponent = false; //Is the exponent part negative
  258.    bool readNumber = false;      //Read a number yet or not (stops +e etc returning 0's).
  259.    
  260.    //Parsing loop
  261.    int state = 0;
  262.    string temp = asString + "!"; //Add an invalid character to end of string
  263.    for(uint i=0; i<temp.length(); i++)
  264.     {
  265.       //Determine properties about this character
  266.       uint8 chr = temp[i];
  267.       int  digit = _parseDigit(chr);
  268.       int sign     = _parseSign(chr);
  269.       bool isExponent = _parseHasExponent(chr);
  270.       bool isWhitesp  = _parseHasWhitespace(chr);
  271.       bool isDigit    = digit!=-1;
  272.      
  273.       switch(state)
  274.       {
  275.          case 0: //Initial state
  276.             {
  277.                if(sign != 0)      { negativeInteger=(sign<0); integer =0; state=1; } //Read +/-XX
  278.                else if(isDigit)   { integer = digit; state = 1; readNumber=true; } //Read N
  279.                break;
  280.             }
  281.          case 1: //Read the number part
  282.             {
  283.                if(isDigit)           { integer = (integer * 10) + digit; readNumber=true; } //Read NNXX
  284.                else if(isExponent && readNumber) { state = 3; } //Read NNeXX -> assume NN.0eXX
  285.                else if(readNumber)   { state = -1; }            //Finished reading the number.
  286.                else                  { state = 0;  }
  287.                break;
  288.             }
  289.          case 3: //Ignore a single whitespace char after the exponent.
  290.             {
  291.                if(isDigit)         { exponent = digit; state = 5; } //Read NNeXX
  292.                else if(isWhitesp)  { state = 4; } //Read NNe XX
  293.                else if(sign != 0)  { negativeExponent=(sign<0); state = 5; }
  294.                else                { state = -1; }
  295.                break;
  296.             }
  297.          case 4: //Read a +/- Sign or a digit for the exponent
  298.             {
  299.                if(isDigit)         { exponent = digit; state = 5; } //Read NNe NX
  300.                else if(sign != 0)  { negativeExponent=(sign<0); state = 5; } //Read NNe +/-XX
  301.                else                { state = -1; }
  302.                break;
  303.             }
  304.          case 5: //Reading the exponent
  305.             {
  306.                if(isDigit)  { exponent = (exponent * 10) + digit; } //Read NNe NX
  307.                else         { state = -1; }
  308.                break;
  309.             }
  310.       }
  311.       //At the end of a number?
  312.       if(state < 0)
  313.        {
  314.           //Construct the number & add it to the output array
  315.          int index = output.length(); output.resize(index+1);
  316.          output[index] = _parseIntHelper(integer,exponent,negativeInteger,negativeExponent);
  317.          //Reset parsing state
  318.          state = 0; integer=0; exponent=0; readNumber=false;
  319.          negativeInteger=false; negativeExponent=false;
  320.          //This terminating char was possibly the start of next number. Start from this character
  321.          if(sign!=0) i-=1;
  322.        }
  323.    }
  324.    
  325.    return output;
  326.  }
  327.  
  328. int _parseIntHelper(int integer, int exponent, bool negI, bool negE)
  329.  {
  330.    //Calculate the number with no exponent applied
  331.    int x = integer;
  332.    //Apply 10^E
  333.    for(int i=0; i<exponent; i++)
  334.       x = negE?(x/10):(x*10);
  335.    //Apply sign of the number
  336.    return negI?-x:x;
  337.  }
  338. ///////////////////////////////
  339. //- Integer Parsing
  340. ///////////////////////////////
  341. ///////////////////////////////
  342. //+ Fast UINT Parse
  343. ///////////////////////////////
  344. uint parseStringUInt(string &in asString) {
  345.     uint output = 0;
  346.     for(uint i=0; i<asString.length(); i++) {
  347.       int digit = _parseDigit(asString[i]);
  348.       output = (digit != -1)?(10*output+digit):(output); }
  349.     return output;
  350. }
  351. ///////////////////////////////
  352. //-
  353. ///////////////////////////////
  354.  
  355. //+ Parsing Helper Functions
  356. int _parseDigit(uint8 digit) {
  357.     int d = digit-48; //48 is ASCII code for 0
  358.     return ((d >= 0)&&(d<=9)) ? d : -1;
  359. }
  360. int _parseSign(uint8 chr) {
  361.  if (chr == 45) return -1;
  362.  else if(chr == 43) return 1;
  363.  else return 0;
  364. }
  365. bool _parseHasExponent(uint8 chr) { return (chr == 69) || (chr == 101); }
  366. bool _parseHasDecimalPoint(uint8 chr) { return (chr==46); }
  367. bool _parseHasWhitespace(uint8 chr) { return (chr == 43)||(chr == 32); }
  368. //-
  369.  
  370. ///////////////////////////////
  371. //End String Parsing
  372. ///////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement