Advertisement
kolton

Untitled

Nov 14th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. function NTA_IsValidSpot(x, y) {
  2. var result = getCollision(me.area, x, y);
  3.  
  4. if (result === undefined || result & 0x1) {
  5. return false;
  6. }
  7.  
  8. return true;
  9. }
  10.  
  11. function NTA_GetToOptimalPosition(target, distance, coll) {
  12. var n, i, j, cx, cy, t, room, map,
  13. coords = [],
  14. goodCoords = [],
  15. angles = [0, 45, 90, 135, 180, 225, 270, 305];
  16.  
  17. t = getTickCount();
  18. room = getRoom(target.x, target.y);
  19.  
  20. if (!room) {
  21. return false;
  22. }
  23.  
  24. map = room.getCollision();
  25.  
  26. for (n = 0; n < 3; n += 1) {
  27. if (n > 0) {
  28. distance = Math.round(distance / 2);
  29. }
  30.  
  31. for (i = 0; i < angles.length; i += 1) {
  32. cx = Math.round((Math.cos(angles[i] * Math.PI / 180)) * distance + target.x);
  33. cy = Math.round((Math.sin(angles[i] * Math.PI / 180)) * distance + target.y);
  34.  
  35. if (NTA_IsValidSpot(cx, cy)) {
  36. coords.push([cx, cy, angles[i]]); // push angles to be sorted
  37. }
  38. }
  39.  
  40. if (coords.length > 0) {
  41. coords.sort(NTA_SortRoomInt); // sort angles by final spot distance
  42. } else { // no good final spots
  43. return false;
  44. }
  45.  
  46. MainLoop: for (i = 0; i < coords.length; i += 1) { // sorted angles are coords[i][2]
  47. for (j = 1; j <= distance; j += 1) {
  48. cx = Math.round((Math.cos(coords[i][2] * Math.PI / 180)) * j + target.x);
  49. cy = Math.round((Math.sin(coords[i][2] * Math.PI / 180)) * j + target.y);
  50.  
  51. if (getCollisionEx(room.x * 5, room.y * 5, map, cx, cy) & coll) {
  52. continue MainLoop; // skip if path is blocked
  53. }
  54.  
  55. if (NTA_IsValidSpot(cx, cy)) { // just in case
  56. print(getTickCount() - t);
  57. NTM_MoveTo(cx, cy, 0, false, "gettooptimalpos"); // first good spot is the closest
  58.  
  59. if (!checkCollision(me, target, coll)) {
  60. return true;
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. return false;
  68. }
  69.  
  70. function getCollisionEx(roomx, roomy, map, x, y) {
  71. for (var i = 0; i < map.length; i += 1) {
  72. for (var j = 0; j < map[i].length; j += 1) {
  73. if (roomx + j === x && roomy + i === y) {
  74. return map[i][j];
  75. }
  76. }
  77. }
  78.  
  79. return 1;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement