Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.47 KB | None | 0 0
  1. --
  2. --  Add trees to map
  3. --
  4. function AddTrees(cell)
  5.    
  6.     print("Adding trees.");
  7.     for x=0, cell:getWidth()-1 do
  8.         for y=0, cell:getHeight()-1 do
  9.             local square = cell:getGridSquare(x, y, 0);
  10.             n = cell:getGridSquare(x, y-1, 0);
  11.             s = cell:getGridSquare(x, y+1, 0);
  12.             e = cell:getGridSquare(x+1, y, 0);
  13.             w = cell:getGridSquare(x-1, y, 0);
  14.             if(ZombRand(5)==0) then
  15.            
  16.                 local rand = ZombRand(6);
  17.                
  18.                 if(rand==0 or rand==1 or rand==2 or rand==3) then
  19.                     rand = -1;
  20.                 end
  21.                 if(rand==4) then
  22.                     rand = 0;
  23.                 end
  24.                 if(rand==5) then
  25.                     rand = 16;
  26.                 end
  27.                
  28.                 if(rand >= 0) then
  29.                     local vegitation =  IsoTree.new(square, "TileTrees_"..rand);
  30.                    
  31.                     square:AddTileObject(vegitation);
  32.                 end
  33.             end
  34.         end
  35.     end
  36.  
  37. end
  38.  
  39. --
  40. --  Add vegitation to map
  41. --
  42. function AddVegitation(cell)
  43.  
  44.     print("Adding grass.");
  45.     for x=0, cell:getWidth()-1 do
  46.         for y=0, cell:getHeight()-1 do
  47.             local square = cell:getGridSquare(x, y, 0);
  48.             for z=0, cell:getMaxFloors()-1 do
  49.                 local square2 = cell:getGridSquare(x, y, z);
  50.                 if square2 ~= nil then
  51.                     square2:ClearTileObjects();        
  52.                 end
  53.             end
  54.            
  55.             local floor =  IsoObject.new(square, "TileFloorExt_3", false);
  56.            
  57.             square:AddTileObject(floor);
  58.             local rand = ZombRand(2, 7);
  59.            
  60.             if(rand>3) then
  61.                 local vegitation =  IsoObject.new(square, "TileTrees_"..rand, false);
  62.                
  63.                 square:AddTileObject(vegitation);
  64.                
  65.             end
  66.         end
  67.     end
  68.  
  69. end
  70.  
  71. southFacingLots = {}
  72. northFacingLots = {}
  73.  
  74. function LoadLots()
  75.     southFacingLots[0] = IsoLot.new("media/lots/Lot_Diner_17x29.lot");
  76.     southFacingLots[1] = IsoLot.new("media/lots/Lot_HouseS_1_22x22.lot");
  77.     southFacingLots[2] = IsoLot.new("media/lots/Lot_Bar12x15.lot");
  78.     southFacingLots[3] = IsoLot.new("media/lots/Lot_HouseGarage_30x36.lot");
  79.     southFacingLots[4] = IsoLot.new("media/lots/Lot_Spiffos_64x34.lot");
  80.    
  81.     northFacingLots[0] = IsoLot.new("media/lots/Lot_Diner_17x29.lot"); 
  82. end
  83.  
  84. function AddRoads(cell)
  85.     print("Adding road.");
  86.    
  87.     local roadWE = IsoLot.new("media/lots/Lot_RoadEW_14x14.lot");
  88.     local roadNS = IsoLot.new("media/lots/Lot_RoadNS_14x14.lot");
  89.  
  90.     local y = ZombRand(20, cell:getHeight()-40);
  91.    
  92.     for x=0, cell:getWidth()-1, 14 do
  93.         cell:PlaceLot(roadWE, x, y, 0, true);
  94.     end
  95.    
  96.     local canDoNorth = false;
  97.     local canDoSouth = false;
  98.     if y > 30 then
  99.         canDoNorth = true;
  100.     end
  101.     if y < cell:getHeight()-30 then
  102.         canDoSouth = true;
  103.     end
  104.    
  105.     x = 20;
  106.     if canDoNorth then
  107.         while x < cell:getWidth() do
  108.             if(ZombRand(3)==0) then
  109.                 x = x + ZombRand(30);
  110.             else
  111.                 local lot = southFacingLots[ZombRand(3)];
  112.                 local ly = y - (lot:getHeight()-1);
  113.                 cell:PlaceLot(lot, x, ly, 0, true);
  114.                 x = x + lot:getWidth();
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. function DoWERoad(cell, y)
  121.  
  122.     local roadWE = IsoLot.new("media/lots/Lot_RoadEW_14x14.lot");
  123.    
  124.     for x=0, cell:getWidth()-1, 14 do
  125.         cell:PlaceLot(roadWE, x, y, 0, true);
  126.     end
  127.  
  128. end
  129.  
  130. function DoNSRoadPart(cell, x, y1, y2)
  131.  
  132.     local roadWE = IsoLot.new("media/lots/Lot_RoadEW_14x14.lot");
  133.     local roadNS = IsoLot.new("media/lots/Lot_RoadNS_14x14.lot");
  134.     local roadNSB = IsoLot.new("media/lots/Lot_RoadTN_14x14.lot");
  135.     local roadNST = IsoLot.new("media/lots/Lot_RoadTS_14x14.lot");
  136.    
  137.     for y=y1, y2, 14 do
  138.         if y2 - y > 4 then  --  don't put down road unless it will meet up with the other road
  139.             cell:PlaceLot(roadNS, x, y, 0, true);
  140.         end
  141.     end
  142.  
  143.     cell:PlaceLot(roadWE, x, y2, 0, true);
  144.  
  145.     cell:PlaceLot(roadNSB, x-3, y2-3, 0, true);
  146.     cell:PlaceLot(roadNST, x-3, y1-3, 0, true);
  147.  
  148. end
  149.  
  150. function DoSquare(cell, x1, x2, y1, y2)
  151.  
  152.     local wid = x2 - x1;
  153.     local hei = y2 - y1;
  154.    
  155.     local x = x1;
  156.     local y = y1;
  157.    
  158.     while x < x2 do    
  159.         x = x + ZombRand(10);
  160.         local lot = northFacingLots[0];
  161.         local ly = y1;--y - (lot:getHeight()-1);
  162.         if(lot:getWidth() + x <= x2 and lot:getHeight() <= hei) then
  163.             --cell:PlaceLot(lot, x, ly, 0, true);
  164.             x = x + lot:getWidth();
  165.         end
  166.     end
  167.  
  168.     if hei > 40 then
  169.         x = x1;
  170.         while x < x2 do    
  171.             x = x + ZombRand(10);
  172.             local lot = southFacingLots[ZombRand(5)];
  173.             local ly = y2 - (lot:getHeight()-2);
  174.             if(lot:getWidth() + x <= x2 and lot:getHeight() <= hei) then
  175.                 cell:PlaceLot(lot, x, ly, 0, true);
  176.                 x = x + lot:getWidth();
  177.             end
  178.         end
  179.  
  180.     end
  181.    
  182.    
  183.     for x=x1, x2 do
  184.         for y=y1, y2 do
  185.             local square = cell:getGridSquare(x, y, 0);
  186.             if(square ~= nil) then
  187.                 --square:ClearTileObjects();
  188.                 --local fl =  IsoObject.new(square, "TileFloorExt_2", false);
  189.                    
  190.                 --square:AddTileObject(fl);
  191.             end
  192.         end
  193.     end
  194. end
  195.  
  196. function DoYSection(cell, y1, y2)
  197.  
  198.     local roadsX = {};
  199.     local x = 0;
  200.     local n = 1;
  201.     while(x < cell:getWidth()) do
  202.         x = x + ZombRand(90) + 30;
  203.         DoNSRoadPart(cell, x, y1, y2);
  204.         roadsX[n] = x;
  205.         n = n + 1;
  206.     end
  207.    
  208.     local last = -1;
  209.     for k, x in ipairs(roadsX) do
  210.         if last ~= -1 and k ~= n - 1 then
  211.             DoSquare(cell, last + 8, x-1, y1+1, y2-1);
  212.         end
  213.         last = x;
  214.     end
  215.  
  216. end
  217.  
  218. function DoRoadNetwork(cell)
  219.  
  220.     local roadsY = {};
  221.     local y1 = 0;
  222.     local n = 1;
  223.     while(y1 < cell:getHeight()) do
  224.         y1 = y1 + ZombRand(90) + 30;
  225.         DoWERoad(cell, y1);
  226.         roadsY[n] = y1;
  227.         n = n + 1;
  228.     end
  229.    
  230.     local last = -1;
  231.     for k, y in ipairs(roadsY) do
  232.         if last ~= -1  and k ~= n - 1 then
  233.             DoYSection(cell, last + 7, y);
  234.         end
  235.         last = y;
  236.     end
  237.  
  238. end
  239.  
  240. function GenerateMap(cell)
  241.  
  242.     cell:DeleteAllMovingObjects();
  243.     print("Generating map.");
  244.     LoadLots();
  245.     AddVegitation(cell);
  246.     AddTrees(cell);
  247.  
  248.     --AddRoads(cell);
  249.     DoRoadNetwork(cell);
  250.    
  251. end
  252.  
  253. Events.OnPostMapLoad.Add(GenerateMap);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement