Advertisement
Guest User

Math.nut

a guest
Apr 22nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. //Math graphs and functions//
  2. //CREATED BY ALLISON G/CAMBEN DO NOT DISTRIBUTE//
  3. //FOR REFERENCE PURPOSES ONLY//
  4.  
  5.  
  6. //if needed for functions, use i for initial, f for final, s for speed/interval
  7.  
  8. /////////////////////
  9. //General Functions//
  10. /////////////////////
  11.  
  12. function toggle(var)
  13. {
  14.     return (var+1)%2;
  15. }
  16.  
  17. function averageChange(i,f) //legacy...
  18. {
  19.     return average(i,f)
  20. }
  21.  
  22. function average(i,f,n=128) //n is the distance bias range of 0 to 256.
  23. {
  24.     if (typeof(i) == "Vector")
  25.     {
  26.         //printl("updating average vector")
  27.         local pos = Vector((i.x*(256-n)+f.x*abs(n-256))/256, (i.y*(256-n)+f.y*abs(n-256))/256, (i.z*(256-n)+f.z*abs(n-256))/256)
  28.         return pos
  29.     }
  30.    
  31.     local pos = ((i*(256-n)+ f*abs(n-256))/256)
  32.     return pos
  33. }
  34.  
  35. function random(n,m="") //random number between 0 and n, or between n and m if set.
  36. {
  37.     if (m=="") return (rand() % n)
  38.        
  39.     if (m!="") randombetween(n,m)
  40. }
  41.  
  42. function randombetween(n,m) //random number between n and m
  43. {
  44.     return n+(rand() % (m-n+1))
  45. }
  46.  
  47. function sign(n) //get sign/polarity of a number. return 1 or -1 or 0.
  48. {
  49.     if (n==0) {return 0}
  50.     return(n/abs(n));
  51. }
  52.  
  53. function iseven(n) //return true if even, false if odd
  54. {
  55.     return toggle(n%2)
  56. }
  57.  
  58. /* function anglesplit(vec) //split angles into x y z for SetAngles
  59. {
  60.     return (vec.x,vec.y,vec.z)
  61. }
  62.  */
  63.  
  64. /////////////////
  65. //Circular Math//
  66. /////////////////
  67.  
  68. function circleAdd(num,offset,max=360,min = 0) //add stuff so that if it reaches max or min, it wraps back around.
  69. {
  70.     local unprocessed = (num+=offset)
  71.     if (unprocessed > max)
  72.     {
  73.         return (unprocessed - max);
  74.     }
  75.     if (unprocessed < min)
  76.     {
  77.         return (max - unprocessed -2);
  78.     }
  79.     return unprocessed;
  80. }
  81.  
  82. ///////////////////////
  83. //Object Motion Paths//
  84. ///////////////////////
  85.  
  86. function XTeleportTo(objectx,ent) //X gon teleport to ya  X gon deliver it to ya
  87. {
  88.     ent = EntstringtoHandle(ent);
  89.     objectx.SetAbsOrigin( Vector(ent.GetOrigin().x,ent.GetOrigin().y,ent.GetOrigin().z) );
  90. }
  91.  
  92. function avgEnttodest(ent,dest,avg=4) //bring ent closer to point dest at amount of average iterations avg
  93. {
  94.     local num = 0
  95.     local origin = dest
  96.    
  97.     //origin = average(average(average(average(origin,ent.GetOrigin()),ent.GetOrigin()),ent.GetOrigin()),ent.GetOrigin())
  98.     while (num<avg)
  99.     {
  100.         origin = average(ent.GetOrigin(),origin)
  101.         num+=1
  102.     }
  103.        
  104.     ent.SetAbsOrigin(origin)
  105.    
  106.    
  107.     return 0
  108. }
  109.  
  110. function placeonground(ent)
  111. {
  112.     ent.SetAbsOrigin(groundpos(ent.GetOrigin()))
  113. }
  114.  
  115. function groundpos(pos)
  116. {
  117.     pos = EnthandletoVector(pos)
  118.    
  119.     local maxdist = 16777216
  120.     local endpos = Vector(pos.x, pos.y, pos.z-maxdist)
  121.    
  122.     return Vector(pos.x,pos.y,pos.z- (maxdist *TraceLine(pos,endpos,Player)))
  123. }
  124.  
  125. ////////////////////////
  126. //Trig and Vector Math//
  127. ////////////////////////
  128.  
  129. function VectorwithinRange(v1,v2,r=1) //return if 2 vectors are within a certain range on xyz axes
  130. {
  131.     //r=(r/2) //set r to half
  132.     local xr = abs(abs(v1.x) - abs((v1.x+v2.x)/2))
  133.     local yr = abs(abs(v1.y) - abs((v1.y+v2.y)/2))
  134.     local zr = abs(abs(v1.z) - abs((v1.z+v2.z)/2))
  135.    
  136.     printdebug (xr + " " + yr + " " + zr)
  137.    
  138.     return (xr <= r && yr <= r && zr <= r )
  139. }
  140.  
  141. function remapValue(n,mult,offset) //remap n to result by * and +
  142. {
  143.     local value = (n*mult)+offset
  144.     return value;
  145. }
  146.  
  147. function remapRange(lower,upper,n,mult=1,offset=0)//remap, change result between upper and lower bounds as n changes
  148. {
  149.     local polarity = sign(upper-lower) //pos or negative?
  150.    
  151.     local value = remapValue(n,mult*polarity,offset)
  152.    
  153.     if(value>upper)
  154.     {
  155.         value=upper
  156.     }
  157.     if(value<lower)
  158.     {
  159.         value=lower
  160.     }
  161.     return value;
  162. }
  163.  
  164. function PitchCorrect(pitch) //fix pitch to go from -90 to 90 instead of 90 to -90, return
  165. {
  166.     return pitch*-1
  167. }
  168.  
  169. function PitchCorrect180(pitch) //fix pitch to go from 0 to 180 instead of 90 to -90, return
  170. {
  171.     return remapValue(pitch,-1,90)
  172. }
  173.  
  174.  
  175. function getDistance(v1,v2)
  176. {
  177.     //if user inputs ents or strings instead of vectors:
  178.     if (typeof(v1) == "instance" || typeof(v1)  == "string")
  179.         v1 = EntstringtoHandle(v1).GetOrigin()
  180.  
  181.     if (typeof(v2) == "instance" || typeof(v2)  == "string")
  182.         v2 = EntstringtoHandle(v2).GetOrigin()
  183.    
  184.    
  185.     //gets the distance between 2 vectors as 1 linear value
  186.     local xSqr = pow((v1.x - v2.x),2);
  187.     local ySqr = pow((v1.y - v2.y),2);
  188.     local zSqr = pow((v1.z - v2.z),2);
  189.     return sqrt(xSqr + ySqr + zSqr);
  190. }
  191.  
  192. function remapDistance(dist,ent)
  193. {
  194.    
  195.     //turn distance into additive (X,Y) values from a reference ent's angles.
  196.     //takes a numeric distance "dist" as well as either an entity name string or entity handle "ent".
  197.    
  198.     ent = EntstringtoHandle(ent);
  199.    
  200.    
  201.     local xdist = dist * cos(ent.GetAngles().y / 57.295800025114)*ViewPitchFloatPerp();
  202.     local ydist = dist * sin(ent.GetAngles().y / 57.295800025114)*ViewPitchFloatPerp();
  203.     local zdist = dist * sin(ent.GetAngles().x*-1 / 57.295800025114);
  204.    
  205.     printdebug("remapped distance "+dist+" to  x: "+xdist+" y: "+ydist+" z: "+zdist+" from entity "+ent.GetName());
  206.     return Vector(xdist,ydist,zdist);
  207. }
  208.  
  209. function remapAddDistancetoEnt(dist,ent)
  210. {
  211.     //remap, then add the distances to reference entity's center and return a vector.
  212.     ent = EntstringtoHandle(ent);
  213.     return ent.GetOrigin() + remapDistance(dist,ent)
  214. }
  215.  
  216.  
  217. function GetSpeedfromVectors(v1,v2,t = 0.1) //get speed from 2 vectors and a time traveled between them
  218. {
  219.     return (getDistance(v1,v2))/ t
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement