Advertisement
kolton

Untitled

Dec 7th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function NTA_GetToOptimalPosition(target, distance, coll) {
  2.     var n, i, j, k, l, cx, cy, t, rooms, index,
  3.         map = [],
  4.         coords = [],
  5.         goodCoords = [],
  6.         angles = [0, 45, 90, 135, 180, 225, 270, 305];
  7.        
  8.     t = getTickCount();
  9.     rooms = getRoom(target.x, target.y).getNearby();
  10.    
  11.     for (n = 0; n < 3; n += 1) {
  12.         if (n > 0) {
  13.             distance = Math.round(distance / 2);
  14.         }
  15.        
  16.         for (i = 0; i < angles.length; i += 1) {
  17.             cx = Math.round((Math.cos(angles[i] * Math.PI / 180)) * distance + target.x);
  18.             cy = Math.round((Math.sin(angles[i] * Math.PI / 180)) * distance + target.y);
  19.            
  20.             index = GetRoomIndex(cx, cy, rooms);
  21.            
  22.             if (!map[index]) {
  23.                 map[index] = rooms[index].getCollision();
  24.             }
  25.            
  26.             if (getColl(cx, cy, rooms[index], map[index]) & 0x1) {
  27.                 continue;
  28.             }
  29.            
  30.             coords.push([cx, cy, angles[i]]); // push angles to be sorted
  31.         }
  32.        
  33.         print("ÿc9potential spots: ÿc2" + coords.length);
  34.        
  35.         if (coords.length > 0) {
  36.             coords.sort(NTA_SortRoomInt); // sort angles by final spot distance
  37.         } else { // no good final spots
  38.             print("reducing optimal spot range");
  39.             continue;
  40.         }
  41.        
  42.         MainLoop: for (i = 0; i < coords.length; i += 1) { // sorted angles are coords[i][2]           
  43.             for (j = 0; j < distance; j += 1) {
  44.                 cx = Math.round((Math.cos(coords[i][2] * Math.PI / 180)) * j + target.x);
  45.                 cy = Math.round((Math.sin(coords[i][2] * Math.PI / 180)) * j + target.y);
  46.                
  47.                 for (k = cx - 1; k < cx + 2; k += 1) { // check thicker line
  48.                     for (l = cy - 1; l < cy + 2; l += 1) {
  49.                         index = GetRoomIndex(k, l, rooms);
  50.                        
  51.                         if (!map[index]) {
  52.                             map[index] = rooms[index].getCollision();
  53.                         }
  54.                        
  55.                         if (getColl(k, l, rooms[index], map[index]) & coll) {
  56.                             continue MainLoop;
  57.                         }
  58.                     }
  59.                 }
  60.                
  61.                 /*index = GetRoomIndex(cx, cy, rooms);
  62.                
  63.                 if (!map[index]) {
  64.                     map[index] = rooms[index].getCollision();
  65.                 }
  66.                
  67.                 if (getColl(cx, cy, rooms[index], map[index]) & coll) {
  68.                     continue MainLoop;
  69.                 }*/
  70.             }
  71.            
  72.             print("ÿc9optimal pos build time: ÿc2" + (getTickCount() - t) + " ÿc9distance from target: ÿc2" + getDistance(cx, cy, target.x, target.y));
  73.             return NTM_MoveTo(cx, cy);
  74.         }
  75.     }
  76.    
  77.     print("optimal position my ass");
  78.     return false;
  79. }
  80.  
  81. function getColl(x, y, room, map) {
  82.     var j = x - room.x * 5,
  83.         i = y - room.y * 5;
  84.        
  85.     if (typeof(map[i][j]) === "undefined") {
  86.         return 5;
  87.     }
  88.    
  89.     return map[i][j];
  90. }
  91.  
  92. var GetRoomIndex = function (x, y, rooms) {
  93.     for (var i = 0; i < rooms.length; i += 1) {
  94.         if (x >= rooms[i].x * 5 && x < rooms[i].x * 5 + rooms[i].xsize && y >= rooms[i].y * 5 && y < rooms[i].y * 5 + rooms[i].ysize) {
  95.             return i;
  96.         }
  97.     }
  98.    
  99.     return undefined;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement