Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function NTA_IsValidSpot(x, y) {
- var result = getCollision(me.area, x, y);
- if (result === undefined || result & 0x1) {
- return false;
- }
- return true;
- }
- function NTA_GetToOptimalPosition(target, distance, coll) {
- var n, i, j, cx, cy, t, room, map,
- coords = [],
- goodCoords = [],
- angles = [0, 45, 90, 135, 180, 225, 270, 305];
- t = getTickCount();
- room = getRoom(target.x, target.y);
- if (!room) {
- return false;
- }
- map = room.getCollision();
- 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);
- if (NTA_IsValidSpot(cx, cy)) {
- coords.push([cx, cy, angles[i]]); // push angles to be sorted
- }
- }
- if (coords.length > 0) {
- coords.sort(NTA_SortRoomInt); // sort angles by final spot distance
- } else { // no good final spots
- return false;
- }
- MainLoop: for (i = 0; i < coords.length; i += 1) { // sorted angles are coords[i][2]
- for (j = 1; 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);
- if (getCollisionEx(room.x * 5, room.y * 5, map, cx, cy) & coll) {
- continue MainLoop; // skip if path is blocked
- }
- if (NTA_IsValidSpot(cx, cy)) { // just in case
- print(getTickCount() - t);
- NTM_MoveTo(cx, cy, 0, false, "gettooptimalpos"); // first good spot is the closest
- if (!checkCollision(me, target, coll)) {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- function getCollisionEx(roomx, roomy, map, x, y) {
- for (var i = 0; i < map.length; i += 1) {
- for (var j = 0; j < map[i].length; j += 1) {
- if (roomx + j === x && roomy + i === y) {
- return map[i][j];
- }
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement