Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. -- A very basic ReuseQueue class.
  2. -- Pass a create function, which shall create a new instance of the object you
  3. -- want to dequeue.
  4. -- This will only be called when the queue is empty.
  5. local ReuseQueue = {}
  6. function ReuseQueue.Create(createFunc)
  7. local this = {}
  8. local queue = {}
  9.  
  10. function this:Dequeue()
  11. local dequeued = table.remove(queue)
  12. if not dequeued then
  13. dequeued = createFunc()
  14. end
  15.  
  16. return dequeued
  17. end
  18.  
  19. function this:Enqueue(toEnqueue)
  20. table.insert(queue, toEnqueue)
  21. end
  22.  
  23. return this
  24. end
  25.  
  26. planet.getnearbynodes = function(maxnum)
  27. local cfr = cam.CFrame;
  28. local l = cfr.lookVector;
  29. local pos = cfr.p+(v3(1,1,1)*l*50);
  30. local r = r3(pos-v3(400,400,400),pos+v3(400,400,400));
  31. local parts = workspace:FindPartsInRegion3WithWhiteList(r,{foliaget},huge); --foliaget is a folder that consists of 5,000+ baseparts that are pre-calculated nodes for where the foliage should go, and what type.
  32. local t = {};
  33. for n,v in pairs(parts) do
  34. ins(t,tonumber(v.Name),v);
  35. end;
  36. return t;
  37. end;
  38.  
  39.  
  40. -- Dequeue/Enqueue functions.
  41. -- dequeueNature(natureBase): Returns an instance of the nature object you want.
  42. -- enqueueNature(natureBase, natureInstance): Enqueue an instace of the nature object for reuse later.
  43. -- You will have to handle parenting/reparenting/re-CFraming of objects outside of this.
  44. -- Implementation detail: a reuse queue is indexed by it's "base" (i.e. the thing you clone)
  45. local natureQueues = {}
  46. local function dequeueNature(natureBase)
  47. local natureQueue = natureQueues[natureBase]
  48. if not natureQueue then
  49. natureQueue = ReuseQueue.Create(function() return natureBase:Clone() end)
  50. natureQueues[natureBase] = natureQueue
  51. end
  52.  
  53. return natureQueue:Dequeue()
  54. end
  55.  
  56. local function enqueueNature(natureBase, natureInstance)
  57. natureQueues[natureBase]:Enqueue(natureInstance)
  58. end
  59.  
  60. spawn(function()
  61. local occt = {}; --Occupied table
  62. local prevcfr;
  63. local Info = info(.2,style.Linear);
  64. while wait(.1) do
  65. if (prevcfr==nil) or (prevcfr~=cam.CFrame) then --Camera has moved
  66. if (planet.cons.condedobounce) then break; end;
  67. local pt = planet.getnearbynodes(400); --Get positions closest to camera to place foliage
  68. local wn = 0;
  69. for i,v in pairs(occt) do --Loop through all the visible trees first
  70. wn = wn + 1;
  71. if not pt[i] then --Visible tree is no longer in camera range, time to delete it.
  72. local occupied = wfc(v,'occupied');
  73. local obj = wfc(v,'obj');
  74. local nature = wfc(v,'nature');
  75. occupied.Value = false;
  76. if obj.Value then
  77. local val = obj.Value;
  78. val.Parent = nil
  79. enqueueNature(nature.Vaue, val)
  80. obj.Value = nil
  81. end;
  82. obj.Value = nil;
  83. occt[i]=nil;
  84. end;
  85. if (wn/300)==floor(wn/300) then wait() end;
  86. end;
  87. for i,v in pairs(pt) do --Makes new agriculture on every node.
  88. wn = wn + 1;
  89. local occupied =(v['occupied']);
  90. local nature = (v['nature']);
  91. local obj = (v['obj']);
  92. if (not occupied.Value) then --If there isn't already agriculture on that node then
  93. occupied.Value = true;
  94. local na = dequeueNature(nature.Value)
  95. for i,im in pairs(desc(na)) do
  96. if isa(im,'ImageLabel') then
  97. im.ImageTransparency = 1;
  98. local g = {ImageTransparency = .5};
  99. local t = tservice:Create(im,Info,g);
  100. t:Play(); --Tweens the transparency of the tree.
  101. end;
  102. end;
  103. na.Parent = naturef;
  104. obj.Value = na;
  105. na:SetPrimaryPartCFrame(v.CFrame);
  106. ins(occt,tonumber(v.Name),v); --Added to the occupied table.
  107. end;
  108. if (wn/300)==floor(wn/300) then wait() end;
  109. end;
  110. prevcfr = cam.CFrame;
  111. end;
  112. end;
  113. end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement