Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Math graphs and functions//
- //CREATED BY ALLISON G/CAMBEN DO NOT DISTRIBUTE//
- //FOR REFERENCE PURPOSES ONLY//
- //if needed for functions, use i for initial, f for final, s for speed/interval
- /////////////////////
- //General Functions//
- /////////////////////
- function toggle(var)
- {
- return (var+1)%2;
- }
- function averageChange(i,f) //legacy...
- {
- return average(i,f)
- }
- function average(i,f,n=128) //n is the distance bias range of 0 to 256.
- {
- if (typeof(i) == "Vector")
- {
- //printl("updating average vector")
- 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)
- return pos
- }
- local pos = ((i*(256-n)+ f*abs(n-256))/256)
- return pos
- }
- function random(n,m="") //random number between 0 and n, or between n and m if set.
- {
- if (m=="") return (rand() % n)
- if (m!="") randombetween(n,m)
- }
- function randombetween(n,m) //random number between n and m
- {
- return n+(rand() % (m-n+1))
- }
- function sign(n) //get sign/polarity of a number. return 1 or -1 or 0.
- {
- if (n==0) {return 0}
- return(n/abs(n));
- }
- function iseven(n) //return true if even, false if odd
- {
- return toggle(n%2)
- }
- /* function anglesplit(vec) //split angles into x y z for SetAngles
- {
- return (vec.x,vec.y,vec.z)
- }
- */
- /////////////////
- //Circular Math//
- /////////////////
- function circleAdd(num,offset,max=360,min = 0) //add stuff so that if it reaches max or min, it wraps back around.
- {
- local unprocessed = (num+=offset)
- if (unprocessed > max)
- {
- return (unprocessed - max);
- }
- if (unprocessed < min)
- {
- return (max - unprocessed -2);
- }
- return unprocessed;
- }
- ///////////////////////
- //Object Motion Paths//
- ///////////////////////
- function XTeleportTo(objectx,ent) //X gon teleport to ya X gon deliver it to ya
- {
- ent = EntstringtoHandle(ent);
- objectx.SetAbsOrigin( Vector(ent.GetOrigin().x,ent.GetOrigin().y,ent.GetOrigin().z) );
- }
- function avgEnttodest(ent,dest,avg=4) //bring ent closer to point dest at amount of average iterations avg
- {
- local num = 0
- local origin = dest
- //origin = average(average(average(average(origin,ent.GetOrigin()),ent.GetOrigin()),ent.GetOrigin()),ent.GetOrigin())
- while (num<avg)
- {
- origin = average(ent.GetOrigin(),origin)
- num+=1
- }
- ent.SetAbsOrigin(origin)
- return 0
- }
- function placeonground(ent)
- {
- ent.SetAbsOrigin(groundpos(ent.GetOrigin()))
- }
- function groundpos(pos)
- {
- pos = EnthandletoVector(pos)
- local maxdist = 16777216
- local endpos = Vector(pos.x, pos.y, pos.z-maxdist)
- return Vector(pos.x,pos.y,pos.z- (maxdist *TraceLine(pos,endpos,Player)))
- }
- ////////////////////////
- //Trig and Vector Math//
- ////////////////////////
- function VectorwithinRange(v1,v2,r=1) //return if 2 vectors are within a certain range on xyz axes
- {
- //r=(r/2) //set r to half
- local xr = abs(abs(v1.x) - abs((v1.x+v2.x)/2))
- local yr = abs(abs(v1.y) - abs((v1.y+v2.y)/2))
- local zr = abs(abs(v1.z) - abs((v1.z+v2.z)/2))
- printdebug (xr + " " + yr + " " + zr)
- return (xr <= r && yr <= r && zr <= r )
- }
- function remapValue(n,mult,offset) //remap n to result by * and +
- {
- local value = (n*mult)+offset
- return value;
- }
- function remapRange(lower,upper,n,mult=1,offset=0)//remap, change result between upper and lower bounds as n changes
- {
- local polarity = sign(upper-lower) //pos or negative?
- local value = remapValue(n,mult*polarity,offset)
- if(value>upper)
- {
- value=upper
- }
- if(value<lower)
- {
- value=lower
- }
- return value;
- }
- function PitchCorrect(pitch) //fix pitch to go from -90 to 90 instead of 90 to -90, return
- {
- return pitch*-1
- }
- function PitchCorrect180(pitch) //fix pitch to go from 0 to 180 instead of 90 to -90, return
- {
- return remapValue(pitch,-1,90)
- }
- function getDistance(v1,v2)
- {
- //if user inputs ents or strings instead of vectors:
- if (typeof(v1) == "instance" || typeof(v1) == "string")
- v1 = EntstringtoHandle(v1).GetOrigin()
- if (typeof(v2) == "instance" || typeof(v2) == "string")
- v2 = EntstringtoHandle(v2).GetOrigin()
- //gets the distance between 2 vectors as 1 linear value
- local xSqr = pow((v1.x - v2.x),2);
- local ySqr = pow((v1.y - v2.y),2);
- local zSqr = pow((v1.z - v2.z),2);
- return sqrt(xSqr + ySqr + zSqr);
- }
- function remapDistance(dist,ent)
- {
- //turn distance into additive (X,Y) values from a reference ent's angles.
- //takes a numeric distance "dist" as well as either an entity name string or entity handle "ent".
- ent = EntstringtoHandle(ent);
- local xdist = dist * cos(ent.GetAngles().y / 57.295800025114)*ViewPitchFloatPerp();
- local ydist = dist * sin(ent.GetAngles().y / 57.295800025114)*ViewPitchFloatPerp();
- local zdist = dist * sin(ent.GetAngles().x*-1 / 57.295800025114);
- printdebug("remapped distance "+dist+" to x: "+xdist+" y: "+ydist+" z: "+zdist+" from entity "+ent.GetName());
- return Vector(xdist,ydist,zdist);
- }
- function remapAddDistancetoEnt(dist,ent)
- {
- //remap, then add the distances to reference entity's center and return a vector.
- ent = EntstringtoHandle(ent);
- return ent.GetOrigin() + remapDistance(dist,ent)
- }
- function GetSpeedfromVectors(v1,v2,t = 0.1) //get speed from 2 vectors and a time traveled between them
- {
- return (getDistance(v1,v2))/ t
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement