Advertisement
Xavion

Untitled

Dec 21st, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Fill the empty space with the minimum number of rectangles.
  2. // The grid size is 1 meter, but the smallest wall/floor tile is 4 meters.
  3. // If you can do better than one rectangle for every tile, let us know!
  4. // We'll help you find a programming job (if you want one).
  5. // Check the guide for more info, and press Contact below to report success.
  6.  
  7. var realGrid = this.getNavGrid().grid;
  8. var tileSize = 4;
  9. var grid = [];
  10. var x, y,
  11.     tileWidth = Math.floor(realGrid[0].length/tileSize),
  12.     tileHeight = Math.floor(realGrid.length/tileSize);
  13. for (y = -1; y <= tileHeight; y++) {
  14.     grid[y] = [];
  15.     for (x = -1; x <= tileWidth; x++) {
  16.         if (y < 0 || x < 0 || y == tileHeight || x == tileWidth || realGrid[(y+0.5)*tileSize][(x+0.5)*tileSize].length > 0) {
  17.             grid[y][x] = {type: -1, ind: -1};
  18.         } else {
  19.             grid[y][x] = {type: 0, ind: -1};
  20.         }
  21.     }
  22. }
  23.  
  24. function isEmpty(cell) {
  25.     return cell.type === 0;
  26. }
  27.  
  28. var addRect = this.addRect;
  29. var spawnedRectangles = this.spawnedRectangles;
  30. function myAddRect(x, y, w, h) {
  31.     var i, j;
  32.     var ind = spawnedRectangles.length;
  33.     addRect((x+w/2)*tileSize, (y+h/2)*tileSize, w*tileSize, h*tileSize);
  34.     if (h == 1 && w == 1) {
  35.         grid[y][x].type = 5;
  36.         grid[y][x].rect = spawnedRectangles[ind];
  37.     } else if (h == 1 && w > 1) {
  38.         grid[y][x].type = 4;
  39.         grid[y][x+w-1].type = 6;
  40.         for (i = x+1; i < x+w-1; i++) grid[y][i].type = 5;
  41.         for (i = x; i < x+w; i++) grid[y][i].rect = spawnedRectangles[ind];
  42.     } else if (h > 1 && w == 1) {
  43.         grid[y][x].type = 2;
  44.         grid[y+h-1][x].type = 8;
  45.         for (i = y+1; i < y+h-1; i++) grid[i][x].type = 5;
  46.         for (i = x; i < y+h; i++) grid[i][x].rect = spawnedRectangles[ind];
  47.     } else if (h > 1 && w > 1) {
  48.         grid[y][x].type = 1;
  49.         grid[y][x+w-1].type = 3;
  50.         grid[y+h-1][x].type = 7;
  51.         grid[y+h-1][x+w-1].type = 9;
  52.         for (i = x+1; i < x+w-1; i++) {
  53.             grid[y][i].type = 2;
  54.             grid[y+h-1][i].type = 8;
  55.         }
  56.         for (i = y+1; i < y+h-1; i++) {
  57.             grid[i][x].type = 4;
  58.             grid[i][x+w-1].type = 6;
  59.         }
  60.         for (i = y+1; i < y+h-1; i++) {
  61.             for (j = x+1; j < x+w-1; j++) {
  62.                 grid[i][j].type = 5;
  63.             }
  64.         }
  65.         for (i = y; i < y+h; i++) {
  66.             for (j = x; j < x+w; j++) {
  67.                 grid[i][j].rect = spawnedRectangles[ind];
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. function toRect(rect) {
  74.     var r = {'width':rect.width/tileSize,
  75.              'height':rect.height/tileSize};
  76.     r.pos = {'x':rect.pos.x/tileSize-r.width/2,
  77.              'y':rect.pos.y/tileSize-r.height/2};
  78.     return r;
  79. }
  80.  
  81. var removeRectAt = this.removeRectAt;
  82. var say = this.say;
  83. function myRemoveRect(rect) {
  84.     var i, j;
  85.     // say(rect.pos.x+','+rect.pos.y);
  86.     for (i = rect.pos.y; i < rect.pos.y+rect.height; i++) {
  87.         for (j = rect.pos.x; j < rect.pos.x+rect.width; j++) {
  88.             grid[i][j].type = 0;
  89.             grid[i][j].rect = undefined;
  90.         }
  91.     }
  92.     removeRectAt((rect.pos.x+rect.width/2)*tileSize, (rect.pos.y+rect.height/2)*tileSize);
  93. }
  94.  
  95. var w, h;
  96. for (y = 0; y < tileHeight; y++) {
  97.     for (x = 0; x < tileWidth; x++) {
  98.         if (grid[y][x].type === 0) {
  99.             h = 1;
  100.             w = 1;
  101.             while (isEmpty(grid[y][x+w])) w++;
  102.             while (grid[y+h].slice(x,x+w).every(isEmpty)) h++;
  103.             myAddRect(x, y, w, h);
  104.             x += w-1;
  105.             this.wait();
  106.         }
  107.     }
  108. }
  109.  
  110. var i, j, k, rect, rect2;
  111. var broken1 = false, broken2 = false, brokenX = true;
  112. // this.say(spawnedRectangles.length);
  113. while (brokenX) {
  114.     brokenX = false;
  115.     spawnedRectangles = this.spawnedRectangles;
  116.     for (i = 1; i < spawnedRectangles.length; i++) {
  117.         if (!brokenX) {
  118.             rect = toRect(spawnedRectangles[i]);
  119.             broken1 = false;
  120.             for (j = 0; j < i; j++) {
  121.                 if (!broken1) {
  122.                     rect2 = toRect(spawnedRectangles[j]);
  123.                     if (rect.pos.y == rect2.pos.y &&
  124.                         rect.height == rect2.height) {
  125.                         broken1 = true;
  126.                     }
  127.                 }
  128.             }
  129.             if (rect.pos.y == rect2.pos.y &&
  130.                 rect.height == rect2.height) {
  131.                 // this.say((i+1)+':testing');
  132.                 // this.say((i+1)+':'+rect.pos.x+','+rect.pos.y+','+rect.height);
  133.                 // this.say((i)+':'+rect2.pos.x+','+rect2.pos.y+','+rect2.height);
  134.                 var pass = true;
  135.                 var rectsToTarget = [];
  136.                 broken1 = false;
  137.                 for (j = rect2.pos.x+rect2.width; j < rect.pos.x; j++) {
  138.                     if (!broken1) {
  139.                         if (rect.height == 1) {
  140.                             // this.say('cell:'+grid[rect.pos.y][j].type);
  141.                             if ([1,2,3,7,8,9].indexOf(grid[rect.pos.y][j].type) > -1) {
  142.                                 // this.say(j+','+rect.pos.y);
  143.                                 rectsToTarget.push(toRect(grid[rect.pos.y][j].rect));
  144.                             } else {
  145.                                 pass = false;
  146.                                 broken1 = true;
  147.                             }
  148.                         } else {
  149.                             if (grid[rect.pos.y][j].type === 1) {
  150.                                 var p = true;
  151.                                 broken2 = false;
  152.                                 for (k = rect.pos.y+1; k < rect.pos.y+rect.height; k++) {
  153.                                     if (!broken2) {
  154.                                         if (grid[k][j] !== 4) {
  155.                                             p = false;
  156.                                             broken2 = true;
  157.                                         }
  158.                                     }
  159.                                 }
  160.                                 if (p) {
  161.                                     rectsToTarget.push(toRect(grid[rect.pos.y][j].rect));
  162.                                     while (grid[rect.pos.y][j] !== 3) j++;
  163.                                 } else {
  164.                                     pass = false;
  165.                                     broken1 = true;
  166.                                 }
  167.                             } else if (grid[rect.pos.y][j].type === 4) {
  168.                                 if (grid[rect.pos.y+rect.height-1][j] !== 7) {
  169.                                     pass = false;
  170.                                     broken1 = true;
  171.                                 } else {
  172.                                     var p = true;
  173.                                     broken2 = false;
  174.                                     for (k = rect.pos.y; k < rect.pos.y+rect.height-1; k++) {
  175.                                         if (!broken2) {
  176.                                             if (grid[k][j] !== 4) {
  177.                                                 p = false;
  178.                                                 broken2 = true;
  179.                                             }
  180.                                         }
  181.                                     }
  182.                                     if (p) {
  183.                                         rectsToTarget.push(toRect(grid[rect.pos.y][j].rect));
  184.                                         while (grid[rect.pos.y][j] !== 6) j++;
  185.                                     } else {
  186.                                         pass = false;
  187.                                         broken1 = true;
  188.                                     }
  189.                                 }
  190.                             } else {
  191.                                 pass = false;
  192.                                 broken1 = true;
  193.                             }
  194.                         }
  195.                     }
  196.                 }
  197.                 // this.say((i+1)+':'+pass);
  198.                 if (pass) {
  199.                     brokenX = true;
  200.                     var rectsToMake = [];
  201.                     for (j = 0; j < rectsToTarget.length; j++) {
  202.                         if (rectsToTarget[j].pos.y === rect.pos.y) {
  203.                             rectsToMake.push([rectsToTarget[j].pos.x,
  204.                                              rectsToTarget[j].pos.y + rect.height,
  205.                                              rectsToTarget[j].width,
  206.                                              rectsToTarget[j].height - rect.height]);
  207.                             myRemoveRect(rectsToTarget[j]);
  208.                         } else if (rectsToTarget[j].pos.y < rect.pos.y) {
  209.                             rectsToMake.push([rectsToTarget[j].pos.x,
  210.                                               rectsToTarget[j].pos.y,
  211.                                               rectsToTarget[j].width,
  212.                                               rectsToTarget[j].height - rect.height]);
  213.                             myRemoveRect(rectsToTarget[j]);
  214.                         } else {
  215.                             // this.say('erk'); // REMEMBER CONVERT FROM THEIRS TO MINE!
  216.                         }
  217.                     }
  218.                     rectsToMake.push([rect2.pos.x,
  219.                                       rect.pos.y,
  220.                                       rect.pos.x + rect.width - rect2.pos.x,
  221.                                       rect.height]);
  222.                     myRemoveRect(rect);
  223.                     myRemoveRect(rect2);
  224.                     for (j = 0; j < rectsToMake.length; j++) {
  225.                         myAddRect(rectsToMake[j][0],
  226.                                   rectsToMake[j][1],
  227.                                   rectsToMake[j][2],
  228.                                   rectsToMake[j][3]);
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement