Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function NTA_GetToOptimalPosition(target, distance, coll) {
- var n, i, j, k, l, cx, cy, t, rooms, index,
- map = [],
- coords = [],
- goodCoords = [],
- angles = [0, 45, 90, 135, 180, 225, 270, 305];
- t = getTickCount();
- rooms = getRoom(target.x, target.y).getNearby();
- for (n = 0; n < 3; n += 1) {
- if (n > 0) {
- distance = Math.round(distance / 2);
- }
- for (i = 0; i < angles.length; i += 1) {
- cx = Math.round((Math.cos(angles[i] * Math.PI / 180)) * distance + target.x);
- cy = Math.round((Math.sin(angles[i] * Math.PI / 180)) * distance + target.y);
- index = GetRoomIndex(cx, cy, rooms);
- if (!map[index]) {
- map[index] = rooms[index].getCollision();
- }
- if (getColl(cx, cy, rooms[index], map[index]) & 0x1) {
- continue;
- }
- coords.push([cx, cy, angles[i]]); // push angles to be sorted
- }
- print("ÿc9potential spots: ÿc2" + coords.length);
- if (coords.length > 0) {
- coords.sort(NTA_SortRoomInt); // sort angles by final spot distance
- } else { // no good final spots
- print("reducing optimal spot range");
- continue;
- }
- MainLoop: for (i = 0; i < coords.length; i += 1) { // sorted angles are coords[i][2]
- for (j = 0; j < distance; j += 1) {
- cx = Math.round((Math.cos(coords[i][2] * Math.PI / 180)) * j + target.x);
- cy = Math.round((Math.sin(coords[i][2] * Math.PI / 180)) * j + target.y);
- for (k = cx - 1; k < cx + 2; k += 1) { // check thicker line
- for (l = cy - 1; l < cy + 2; l += 1) {
- index = GetRoomIndex(k, l, rooms);
- if (!map[index]) {
- map[index] = rooms[index].getCollision();
- }
- if (getColl(k, l, rooms[index], map[index]) & coll) {
- continue MainLoop;
- }
- }
- }
- /*index = GetRoomIndex(cx, cy, rooms);
- if (!map[index]) {
- map[index] = rooms[index].getCollision();
- }
- if (getColl(cx, cy, rooms[index], map[index]) & coll) {
- continue MainLoop;
- }*/
- }
- print("ÿc9optimal pos build time: ÿc2" + (getTickCount() - t) + " ÿc9distance from target: ÿc2" + getDistance(cx, cy, target.x, target.y));
- return NTM_MoveTo(cx, cy);
- }
- }
- print("optimal position my ass");
- return false;
- }
- function getColl(x, y, room, map) {
- var j = x - room.x * 5,
- i = y - room.y * 5;
- if (typeof(map[i][j]) === "undefined") {
- return 5;
- }
- return map[i][j];
- }
- var GetRoomIndex = function (x, y, rooms) {
- for (var i = 0; i < rooms.length; i += 1) {
- 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) {
- return i;
- }
- }
- return undefined;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement