Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- A very basic ReuseQueue class.
- -- Pass a create function, which shall create a new instance of the object you
- -- want to dequeue.
- -- This will only be called when the queue is empty.
- local ReuseQueue = {}
- function ReuseQueue.Create(createFunc)
- local this = {}
- local queue = {}
- function this:Dequeue()
- local dequeued = table.remove(queue)
- if not dequeued then
- dequeued = createFunc()
- end
- return dequeued
- end
- function this:Enqueue(toEnqueue)
- table.insert(queue, toEnqueue)
- end
- return this
- end
- planet.getnearbynodes = function(maxnum)
- local cfr = cam.CFrame;
- local l = cfr.lookVector;
- local pos = cfr.p+(v3(1,1,1)*l*50);
- local r = r3(pos-v3(400,400,400),pos+v3(400,400,400));
- 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.
- local t = {};
- for n,v in pairs(parts) do
- ins(t,tonumber(v.Name),v);
- end;
- return t;
- end;
- -- Dequeue/Enqueue functions.
- -- dequeueNature(natureBase): Returns an instance of the nature object you want.
- -- enqueueNature(natureBase, natureInstance): Enqueue an instace of the nature object for reuse later.
- -- You will have to handle parenting/reparenting/re-CFraming of objects outside of this.
- -- Implementation detail: a reuse queue is indexed by it's "base" (i.e. the thing you clone)
- local natureQueues = {}
- local function dequeueNature(natureBase)
- local natureQueue = natureQueues[natureBase]
- if not natureQueue then
- natureQueue = ReuseQueue.Create(function() return natureBase:Clone() end)
- natureQueues[natureBase] = natureQueue
- end
- return natureQueue:Dequeue()
- end
- local function enqueueNature(natureBase, natureInstance)
- natureQueues[natureBase]:Enqueue(natureInstance)
- end
- spawn(function()
- local occt = {}; --Occupied table
- local prevcfr;
- local Info = info(.2,style.Linear);
- while wait(.1) do
- if (prevcfr==nil) or (prevcfr~=cam.CFrame) then --Camera has moved
- if (planet.cons.condedobounce) then break; end;
- local pt = planet.getnearbynodes(400); --Get positions closest to camera to place foliage
- local wn = 0;
- for i,v in pairs(occt) do --Loop through all the visible trees first
- wn = wn + 1;
- if not pt[i] then --Visible tree is no longer in camera range, time to delete it.
- local occupied = wfc(v,'occupied');
- local obj = wfc(v,'obj');
- local nature = wfc(v,'nature');
- occupied.Value = false;
- if obj.Value then
- local val = obj.Value;
- val.Parent = nil
- enqueueNature(nature.Vaue, val)
- obj.Value = nil
- end;
- obj.Value = nil;
- occt[i]=nil;
- end;
- if (wn/300)==floor(wn/300) then wait() end;
- end;
- for i,v in pairs(pt) do --Makes new agriculture on every node.
- wn = wn + 1;
- local occupied =(v['occupied']);
- local nature = (v['nature']);
- local obj = (v['obj']);
- if (not occupied.Value) then --If there isn't already agriculture on that node then
- occupied.Value = true;
- local na = dequeueNature(nature.Value)
- for i,im in pairs(desc(na)) do
- if isa(im,'ImageLabel') then
- im.ImageTransparency = 1;
- local g = {ImageTransparency = .5};
- local t = tservice:Create(im,Info,g);
- t:Play(); --Tweens the transparency of the tree.
- end;
- end;
- na.Parent = naturef;
- obj.Value = na;
- na:SetPrimaryPartCFrame(v.CFrame);
- ins(occt,tonumber(v.Name),v); --Added to the occupied table.
- end;
- if (wn/300)==floor(wn/300) then wait() end;
- end;
- prevcfr = cam.CFrame;
- end;
- end;
- end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement