tyridge77

Untitled

Sep 30th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. local module = {}
  2.  
  3. local Network = shared.Network;
  4. local NetworkInit = shared.NetworkInit;
  5. local LoadSharedModule = shared.LoadSharedModule;
  6.  
  7. local SharedUtils = LoadSharedModule("Utils");
  8.  
  9. local Resources = shared.Resources;
  10. local BuildingAssets = Resources:WaitForChild("Building");
  11.  
  12. local Ignore = workspace:WaitForChild("Ignore");
  13. local Terrain = workspace:WaitForChild("Terrain");
  14. local NewRay = Ray.new;
  15.  
  16. local Cells = {};
  17.  
  18. local floor = math.floor;
  19.  
  20. local function Round(x,int)
  21.     return floor(x/int + 0.5) * int;
  22. end
  23. local function Round3D(v3)
  24.     return Round(v3.x,12),Round(v3.y,12),Round(v3.z,12);
  25. end
  26.  
  27.  
  28.  
  29. local Cell = {};
  30. function Cell.new(x,y,z)
  31.     local self = setmetatable({},{__index=Cell});
  32.    
  33.     self.x=x;
  34.     self.y=y;
  35.     self.z=z;
  36.    
  37.     self.touchingblocks=0;
  38.    
  39.     return self;
  40. end
  41. function Cell:RemoveGhost()
  42.     if self.ghost then
  43.         self.ghost:Destroy();
  44.         self.ghost=nil;
  45.     end
  46. end
  47. function Cell:GiveGhost()
  48.     if not self.block and not self.ghost then -- If there isn't a block or a ghost here
  49.         local part1 = Instance.new("Part");
  50.         part1.Parent=workspace;
  51.         part1.Transparency=1;
  52.         part1.Name="Ghost";
  53.         part1.Anchored=true;
  54.         part1.CanCollide=false;
  55.         part1.Size=Vector3.new(12,1,12);
  56.         part1.CFrame=CFrame.new(self.x,self.y,self.z);
  57.        
  58.         self.ghost=part1;
  59.            
  60.     end
  61. end
  62.  
  63. local function CreateCell(x,y,z)
  64.     if not Cells[x] then
  65.         Cells[x]={};
  66.     end
  67.     if not Cells[x][y] then
  68.         Cells[x][y] = {};
  69.     end
  70.     if not Cells[x][y][z] then
  71.         local cell = Cell.new(x,y,z);
  72.         Cells[x][y][z]=cell;
  73.         return cell;
  74.     end
  75. end
  76.  
  77. local function GetCell(x,y,z)
  78.     local cellx = Cells[x];
  79.     if cellx then
  80.         local celly = cellx[y];
  81.         if celly then
  82.             local cell = celly[z];
  83.             if cell then
  84.                 return cell;
  85.             end
  86.         end
  87.     end
  88.    
  89.     return CreateCell(x,y,z);
  90. end
  91. local function GetAdjacentCells2D(x,y,z)
  92.     local cells = {};
  93.    
  94.     local t = {{x+12,y,z},{x-12,y,z},{x,y,z+12},{x,y,z-12}};
  95.     for _,v in pairs(t) do
  96.         cells[#cells+1]=GetCell(unpack(v));
  97.     end
  98.     return cells;
  99. end
  100. local function GetAdjacentCells3D(x,y,z)
  101.     local cells = {};
  102.    
  103.     local t = {{x+12,y,z},{x-12,y,z},{x,y,z+12},{x,y,z-12},{x,y-12,z},{x,y+12,z}};
  104.     for _,v in pairs(t) do
  105.         cells[#cells+1]=GetCell(unpack(v));
  106.     end
  107.     return cells;
  108. end
  109.  
  110.  
  111. -- Start at cell x,y,z
  112. -- Recursively scan adjacent cells if cell has a block and cell isn't already marked by a raft
  113.    
  114.    
  115. local ThreeDScan;
  116.  
  117. local function ScanAdjacent(rafts,raftindex,adjacent)
  118.    
  119.    
  120.     local block = adjacent.block;
  121.     if block then
  122.         local marked;
  123.         for i,raft in pairs(rafts) do
  124.             if raft[block] then
  125.                 marked=true;
  126.             end
  127.         end
  128.        
  129.         if not marked then
  130.             rafts[raftindex][block]=true;
  131.             ThreeDScan(rafts,raftindex,adjacent.x,adjacent.y,adjacent.z);
  132.         end
  133.            
  134.     end
  135. end
  136.  
  137. function ThreeDScan(rafts,raftindex,x,y,z)
  138.     local adjacents = GetAdjacentCells3D(x,y,z);
  139.     for _,adjacent in pairs(adjacents) do
  140.        
  141.         ScanAdjacent(rafts,raftindex,adjacent)
  142.        
  143.     end
  144. end
  145.  
  146. local function MarkRafts(x,y,z)
  147.    
  148.     local adjacents = GetAdjacentCells2D(x,y,z);
  149.    
  150.     local rafts = {};
  151.    
  152.     for i = 1,#adjacents do
  153.         local adjacent = adjacents[i];
  154.        
  155.         rafts[i]={};
  156.         ScanAdjacent(rafts,i,adjacent);
  157.     end
  158.    
  159.    
  160.     local markedrafts = {};
  161.     for _,raft in pairs(rafts) do
  162.         if next(raft) ~= nil then
  163.             markedrafts[#markedrafts+1]=raft;
  164.         end
  165.     end
  166.    
  167.     print("Marked rafts: ",#markedrafts);
  168.    
  169. end
  170.  
  171.  
  172. local function GiveGhostParts(model)
  173.     local cf = model:GetModelCFrame();
  174.    
  175.     local tab = {Vector3.new(12,0,0),Vector3.new(-12,0,0),Vector3.new(0,0,12),Vector3.new(0,0,-12)};   
  176.    
  177.     for _,v in pairs(tab) do
  178.            
  179.         local cf = CFrame.new(cf.p+v);
  180.    
  181.         local x,y,z = Round3D(cf.p);
  182.        
  183.         local cell = GetCell(x,y,z);
  184.        
  185.         cell:GiveGhost();
  186.        
  187.     end
  188.    
  189. end
  190.  
  191.  
  192.  
  193. local function PlaceObject(name,cf)
  194.     local obj = BuildingAssets:FindFirstChild(name,true);
  195.     if obj then
  196.         obj = obj:clone();
  197.         obj:SetPrimaryPartCFrame(cf);
  198.        
  199.         GiveGhostParts(obj);
  200.        
  201.        
  202.         local x,y,z = Round3D(cf.p);
  203.        
  204.        
  205.         local cell = GetCell(x,y,z);
  206.        
  207.         cell.Active=true;
  208.         cell.block=obj;
  209.        
  210.         cell:RemoveGhost();
  211.        
  212.         local adjacents = GetAdjacentCells2D(x,y,z);
  213.         for _,adjacent in pairs(adjacents) do
  214.             adjacent.touchingblocks=adjacent.touchingblocks+1;
  215.         end
  216.            
  217.         SharedUtils.OnObjectRemoved(obj,function()
  218.            
  219.             cell.block=nil;
  220.            
  221.             for _,adjacent in pairs(adjacents) do
  222.                 adjacent.touchingblocks=adjacent.touchingblocks-1;
  223.                
  224.                 if adjacent.touchingblocks < 1 then
  225.                     adjacent:RemoveGhost();
  226.                 end
  227.             end
  228.            
  229.            
  230.             for _,adjacent in pairs(adjacents) do
  231.                
  232.                 if adjacent.block then
  233.                     for _,v in pairs(GetAdjacentCells2D(adjacent.x,adjacent.y,adjacent.z)) do
  234.                         v:GiveGhost();
  235.                     end
  236.                 end
  237.                
  238.             end
  239.                    
  240.                    
  241.                
  242.         end)
  243.        
  244.         obj.Parent=workspace;
  245.     end
  246. end
  247.  
  248. Network:BindEvents(NetworkInit,
  249.     {
  250.      PlaceObject=function(clientState,client,name,cf)
  251.         PlaceObject(name,cf);
  252.      end,
  253.      RemoveObject=function(clientState,client,obj)
  254.        
  255.         local p = obj:GetModelCFrame().p;
  256.         local x,y,z = Round3D(p);
  257.        
  258.        
  259.         obj:Destroy(); 
  260.    
  261.        
  262.        
  263.         MarkRafts(x,y,z);
  264.  
  265.        
  266.      end
  267.     }
  268. )
  269.  
  270.  
  271. return module
Advertisement
Add Comment
Please, Sign In to add comment