Advertisement
tomonaoboys

Module Script : Advanced Functions(not so)

Apr 20th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.96 KB | None | 0 0
  1. local swait=function(num)if tonumber(num)then for i=1,tonumber(num)do game:service'RunService'.RenderStepped:wait()end else game:service'RunService'.RenderStepped:wait()end return true end
  2. local hwait=function(num)if tonumber(num)then for i=1,tonumber(num)do game:service'RunService'.Heartbeat:wait()end else game:service'RunService'.Heartbeat:wait()end return true end
  3. if script:IsA"Script"then
  4.     swait=hwait
  5. end
  6. if"SimplexNoise"then
  7. --[[ Example implementation of tileable fractional brownian motion (not needed for pure simplex noise)
  8.    
  9. x and z are the input coordinates.
  10. octs is the number of octaves of noise you would like.
  11. decay is how quickly the amplitude of the noise decays with each octave.
  12. ridged returns ridged fractal noise instead of regular simplex noise.  
  13.    
  14. function fbm(x,z,octs,decay,ridged)
  15.    local X=x
  16.     local Z=z
  17.     -- results seem to be the best when x and z are small (why i devide them by 300) - scale them how you please.  
  18.     x=x/300
  19.     z=z/300
  20.     local val=0;
  21.     for o=0,octs-1 do
  22.         if (ridged) then
  23.             val=val +  math.abs(-(decay^o) * shared.noise(x * 2^o,z * 2^o));
  24.         else
  25.             val=val + (-(decay^o) * shared.noise(x * 2^o,z * 2^o))
  26.         end
  27.     end
  28.     --return val
  29. end
  30.    
  31.    
  32. ]]
  33.  
  34. grad3={{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}}
  35. p={}
  36. function setnoise(seed)
  37. p={}
  38. math.randomseed(seed or 1)
  39. for i=0,255 do p[i]=math.random(0,255)end
  40.   -- To remove the need for index wrapping, double the permutation table length
  41. perm={}for i=0, 511 do perm[i]=p[i%256]end
  42. end
  43. setnoise()
  44. function fastfloor(x)if x>0 then return math.floor(x)else return math.floor(x-1)end end
  45. function dot(g,x,y)return g[1]*x + g[1]*y end
  46. -- 2D simplex noise
  47. SimplexNoise=function(xin,yin,seed)
  48.     if seed then setnoise(seed)end
  49.     local i1,j1,n0,n2=nil
  50.     xin=xin / 16
  51.     yin=yin / 16
  52.     local F2=0.5*(math.sqrt(3.0)-1.0);
  53.     local s=(xin+yin)*F2; --// Hairy factor for 2D
  54.     local i=fastfloor(xin+s);
  55.     local j=fastfloor(yin+s);
  56.     local G2=(3.0-math.sqrt(3.0))/6.0;
  57.     local t=(i+j)*G2;
  58.     local X0=i-t; --// Unskew the cell origin back to (x,y) space
  59.     local Y0=j-t;
  60.     local x0=xin-X0; --// The x,y distances from the cell origin
  61.     local y0=yin-Y0;
  62.     if x0>y0 then
  63.         i1=1
  64.         j1=0 --// lower triangle, XY order: (0,0)->(1,0)->(1,1)
  65.     else
  66.         i1=0
  67.         j1=1
  68.     end --// upper triangle, YX order: (0,0)->(0,1)->(1,1)
  69.     local x1=x0 - i1 + G2; --// Offsets for middle corner in (x,y) unskewed coords
  70.     local y1=y0 - j1 + G2;
  71.     local x2=x0 - 1.0 + 2.0 * G2 --// Offsets for last corner in (x,y) unskewed coords
  72.     local y2=y0 - 1.0 + 2.0 * G2
  73.     --// Work out the hashed gradient indices of the three simplex corners
  74.     local ii=i % 256
  75.     local jj=j % 256
  76.     local gi0=perm[ii+perm[jj]] % 12;
  77.     local gi1=perm[ii+i1+perm[jj+j1]] % 12;
  78.     local gi2=perm[ii+1+perm[jj+1]] % 12;
  79.     --// Calculate the contribution from the three corners
  80.     local t0=0.5 - x0*x0-y0*y0;
  81.     if t0<0 then
  82.     n0=0.0
  83.     else
  84.         t0=t0 * t0;
  85.         n0=t0 * t0 * dot(grad3[gi0+1], x0, y0); --// (x,y) of grad3 used for 2D gradient
  86.     end
  87.     local t1=0.5 - x1*x1-y1*y1;
  88.     local n1=nil
  89.     if t1<0 then
  90.         n1=0.0;
  91.     else
  92.       t1=t1 * t1;
  93.       n1=t1 * t1 * dot(grad3[gi1+1], x1, y1);
  94.     end
  95.     local t2=0.5 - x2*x2-y2*y2;
  96.     if (t2<0) then
  97.         n2=0.0;
  98.     else
  99.       t2=t2 * t2;
  100.       n2=t2 * t2 * dot(grad3[gi2+1], x2, y2);
  101.     end
  102.    -- Add contributions from each corner to get the final noise value.
  103.    -- The result is scaled to return values in the interval [-1,1].
  104.     return 70.0 * (n0 + n1 + n2);
  105. end
  106. end
  107. if"Triangle Function"then
  108. local wedge=Instance.new"WedgePart"wedge.TopSurface=Enum.SurfaceType.Smooth wedge.BottomSurface=Enum.SurfaceType.Smooth wedge.CanCollide=false
  109. Instance.new("SpecialMesh",wedge).MeshType="Wedge"wedge.Size=Vector3.new()
  110. function DrawTriangle(a,b,c,parent,PartStat,Collide,FadeOpt)
  111.     a=typeof(a)=="CFrame"and a.p or a b=typeof(b)=="CFrame"and b.p or b c=typeof(c)=="CFrame"and c.p or c
  112.     if not a or not b or not c then error'Attempt to use NIL value'end
  113.     local TriangleModel=Instance.new("Model",parent)
  114.     local siz=0 local edges={{longest=(c-b),other=(a-b),position=b},{longest=(a-c),other=(b-c),position=c},{longest=(b-a),other=(c-a),position=a}}
  115.     table.sort(edges,function(a,b) return a.longest.magnitude > b.longest.magnitude end)local edge=edges[1]
  116.     local theta=math.acos(edge.longest.unit:Dot(edge.other.unit))local s1=Vector2.new(edge.other.magnitude * math.cos(theta),edge.other.magnitude * math.sin(theta))
  117.     local s2=Vector2.new(edge.longest.magnitude-s1.x,s1.y)local p1=edge.position + edge.other * 0.5 local p2=edge.position + edge.longest + (edge.other-edge.longest) * 0.5
  118.     local right=edge.longest:Cross(edge.other).unit local up=right:Cross(edge.longest).unit local back=edge.longest.unit
  119.     local cf1=CFrame.new(p1.x,p1.y,p1.z,-right.x,up.x,back.x,-right.y,up.y,back.y,-right.z,up.z,back.z)local cf2=CFrame.new(p2.x,p2.y,p2.z,right.x,up.x,-back.x,right.y,up.y,-back.y,right.z,up.z,-back.z)
  120.     local w1=wedge:Clone()local w2=wedge:Clone()if not Collide then w1.Mesh.Scale=Vector3.new(siz,s1.y,s1.x)*20 w2.Mesh.Scale=Vector3.new(siz,s2.y,s2.x)*20 else w1.Size=Vector3.new(siz,s1.y,s1.x)w2.Size=Vector3.new(siz,s2.y,s2.x)end
  121.     w1.CFrame=cf1 w2.CFrame=cf2 w1.Parent=TriangleModel w2.Parent=TriangleModel
  122.     for Index, Value in pairs(PartStat or{})do w1[Index]=Value end for Index, Value in pairs(PartStat or{})do w2[Index]=Value end
  123.     w1.Name="Wedge1"w2.Name="Wedge2"
  124.     if FadeOpt then local FadingRange=FadeOpt.Range or 10 local FadeSpeed=FadeOpt.Speed or 1
  125.     local scr=(s1.x+s2.x)*s1.x/2 local fadval=scr-FadingRange if fadval<0 then fadval=0 end fadval=fadval*FadeSpeed
  126.     if w1.Transparency+fadval>=1 then w1.Transparency=1 w2.Transparency=1 else
  127.     w1.Transparency=w1.Transparency+fadval w2.Transparency=w1.Transparency end
  128.     end
  129.     TriangleModel.Parent=parent or workspace
  130.     return TriangleModel
  131. end
  132. function ReDrawTri(TriangleModel,a,b,c,PartStat,Collide,FadeOpt)
  133.     a=typeof(a)=="CFrame"and a.p or a b=typeof(b)=="CFrame"and b.p or b c=typeof(c)=="CFrame"and c.p or c
  134.     local siz=0 local edges={{longest=(c-b),other=(a-b),position=b},{longest=(a-c),other=(b-c),position=c},{longest=(b-a),other=(c-a),position=a}}
  135.     table.sort(edges,function(a,b) return a.longest.magnitude > b.longest.magnitude end)local edge=edges[1]
  136.     local theta=math.acos(edge.longest.unit:Dot(edge.other.unit))local s1=Vector2.new(edge.other.magnitude * math.cos(theta),edge.other.magnitude * math.sin(theta))
  137.     local s2=Vector2.new(edge.longest.magnitude-s1.x,s1.y)local p1=edge.position + edge.other * 0.5 local p2=edge.position + edge.longest + (edge.other-edge.longest) * 0.5
  138.     local right=edge.longest:Cross(edge.other).unit local up=right:Cross(edge.longest).unit local back=edge.longest.unit
  139.     local cf1=CFrame.new(p1.x,p1.y,p1.z,-right.x,up.x,back.x,-right.y,up.y,back.y,-right.z,up.z,back.z)local cf2=CFrame.new(p2.x,p2.y,p2.z,right.x,up.x,-back.x,right.y,up.y,-back.y,right.z,up.z,-back.z)
  140.     local w1=TriangleModel.Wedge1 local w2=TriangleModel.Wedge2 if not Collide then w1.Mesh.Scale=Vector3.new(siz,s1.y,s1.x)*20 w2.Mesh.Scale=Vector3.new(siz,s2.y,s2.x)*20 else w1.Size=Vector3.new(siz,s1.y,s1.x)w2.Size=Vector3.new(siz,s2.y,s2.x)end
  141.     w1.CFrame=cf1 w2.CFrame=cf2 w1.Parent=TriangleModel w2.Parent=TriangleModel
  142.     for Index, Value in pairs(PartStat or{})do w1[Index]=Value end for Index, Value in pairs(PartStat or{})do w2[Index]=Value end
  143.     if FadeOpt then local FadingRange=FadeOpt.Range or 10 local FadeSpeed=FadeOpt.Speed or 1
  144.     local scr=(s1.x+s2.x)*s1.x/2 local fadval=scr-FadingRange if fadval<0 then fadval=0 end fadval=fadval*FadeSpeed
  145.     if w1.Transparency+fadval>=1 then w1.Transparency=1 w2.Transparency=1 else
  146.     w1.Transparency=w1.Transparency+fadval w2.Transparency=w1.Transparency end
  147.     end
  148. end
  149. function DrawSqu(A,B,C,D,parent,PartStat,Collide,FadeOpt)
  150. local SquareModel=Instance.new("Model",parent or workspace)
  151. local mA=DrawTriangle(A,B,C,SquareModel,PartStat,Collide,FadeOpt)local mB=DrawTriangle(C,D,A,SquareModel,PartStat,Collide,FadeOpt)mA.Name="A"mB.Name="B"return SquareModel end
  152. function ReDrawSqu(m,A,B,C,D,PartStat,Collide,FadeOpt)ReDrawTri(m.A,A,B,C,PartStat,Collide,FadeOpt)ReDrawTri(m.B,C,D,A,PartStat,Collide,FadeOpt)end
  153. end
  154. if"Audio things"then
  155.    
  156. end
  157. if"Misc"then
  158.     RandomAngleCF=function()return CFrame.Angles(math.rad(math.random(360)),math.rad(math.random(360)))end
  159. end
  160. shared.AdvFunc={SimplexNoise=SimplexNoise,DrawTri=DrawTriangle,ReDrawTri=ReDrawTri,
  161. DrawSqu=DrawSqu,ReDrawSqu=ReDrawSqu,swait=swait,hwait=hwait,RandomAngleCF=RandomAngleCF,
  162. DataStorage={}}
  163. --[[
  164.     Packed Functions
  165.     Noise
  166.     >SimplexNoise(x , y , seed) ,
  167.    
  168.     Triangle Function
  169.     >DrawTri( p1 , p2 , p3 , Parent , {Part State} , isNOTmesh , Fade Option)  -draw freedom triangle , except actually broke or not even.
  170.     >ReDrawTri(TriangleModel ,p1 ,p2 ,p3 , isNOTmesh , {Part State} , Fade Option)  -anti lag, this uses model that gets returned from DrawTri.
  171.     >DrawSqu  -just extended.
  172.     >ReDrawSqu  -too.
  173.    
  174.     Variable
  175.     >DataStorage (it is able to use as a temporary data storage if you want to use. or else create your own , this is easier way.)
  176. --]]
  177.  
  178. print("Including function finished.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement