Advertisement
EditorRUS

Harder than hard

Jul 24th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.99 KB | None | 0 0
  1.     (setq expFindClosestGridVerticle
  2.         (lambda (origin gridID)
  3.             (block Nil
  4.                 (and
  5.                     (or
  6.                         origin
  7.                         (not (if (expDebug) (dbgLog "expFindClosestGridVerticle error, origin is Nil")))
  8.                     )
  9.                     (or
  10.                         gridID
  11.                         (not (if (expDebug) (dbgLog "expFindClosestGridVerticle error, gridID is Nil")))
  12.                     )
  13.                     (block
  14.                         (
  15.                             (angel (expGridIDAngel gridID))
  16.                             (gridOrigin (objGetData angel "origin"))
  17.                             (gridSize (objGetData angel "size"))
  18.                             (gridSpacing (objGetData angel "spacing"))
  19.                             (gridEdge (objGetData angel "gridEdge"))
  20.                             (angle (sysVectorAngle origin gridOrigin))
  21.                             (originVerticle
  22.                                 (objGetObjByID
  23.                                     (random
  24.                                         (objGetData angel "NWCorner")
  25.                                         (objGetData angel "SECorner")
  26.                                     )
  27.                                 )
  28.                             )
  29.                             (originCoord (expGetGridCoordinate originVerticle))
  30.                             (currentPos (objGetPos originVerticle))
  31.                             (closestApproach (sysVectorDistance origin originVerticle))
  32.                             (offsetCoord (list 0 0))
  33.                             (deltaCoord (list))
  34.                             (triangulationStage 0)
  35.                         )  
  36.                         (and
  37.                             (setq outOfBoundaryCondition
  38.                                 (lambda (coord1 coord2 thisGridEdge)
  39.                                     (block
  40.                                         (newCoord (list))
  41.                                         (lnkAppend newCoord (+ (@ coord1 0) (@ coord2 0)))
  42.                                         (lnkAppend newCoord (+ (@ coord1 1) (@ coord2 1)))
  43.                                         (or
  44.                                             (ls (@ newCoord 0) 0)
  45.                                             (geq (@ newCoord 0) thisGridEdge)
  46.                                             (ls (@ newCoord 1) 0)
  47.                                             (geq (@ newCoord 1) thisGridEdge)
  48.                                         )
  49.                                     )
  50.                                 )
  51.                             )
  52.  
  53.                             (if
  54.                                 (or
  55.                                     (and
  56.                                         (geq angle 0)
  57.                                         (ls angle 90)
  58.                                     )
  59.                                     (gr angle 270)
  60.                                 )
  61.                                 (lnkAppend deltaCoord True)
  62.                                 (lnkAppend deltaCoord Nil)
  63.                             )
  64.                             (if
  65.                                 (and
  66.                                     (gr angle 0)
  67.                                     (ls angle 180)
  68.                                 )
  69.                                 (lnkAppend deltaCoord True)
  70.                                 (lnkAppend deltaCoord Nil)
  71.                             )
  72.                             (loop
  73.                                 (neq triangulationStage 1)
  74.                                 (block
  75.                                     (stepAngle newPos)
  76.                                     ; Step horizontally
  77.                                     (and
  78.                                         (setq stepAngle
  79.                                             (if
  80.                                                 (@ deltaCoord 0)
  81.                                                 0 ; To the right
  82.                                                 180 ; To the left
  83.                                             )
  84.                                         )
  85.                                         (setq offsetDelta
  86.                                             (if
  87.                                                 (@ deltaCoord 0)
  88.                                                 1 ; To the right
  89.                                                 -1 ; To the left
  90.                                             )
  91.                                         )                                  
  92.                                         (setq newPos
  93.                                             (sysVectorPolarOffset
  94.                                                 currentPos
  95.                                                 stepAngle
  96.                                                 gridSpacing
  97.                                             )
  98.                                         )
  99.                                         (if (outOfBoundaryCondition originCoord offsetCoord gridEdge) (not (setq triangulationStage 1))) ; We are out of grid, no point going further
  100.                                         (if
  101.                                             (ls
  102.                                                 (sysVectorDistance
  103.                                                     newPos
  104.                                                     origin
  105.                                                 )
  106.                                                 closestApproach
  107.                                             )
  108.                                             (block Nil
  109.                                                 (setq closestApproach (sysVectorDistance newPos origin)) ; We're closer horizontally
  110.                                                 (setq currentPos newPos)
  111.                                                 (set@ offsetCoord 0 (+ (@ offsetCoord 0) offsetDelta))
  112.                                             )
  113.                                             (setq triangulationStage 1); Otherwise we've already found the closest horizontal position
  114.                                         )
  115.                                     )
  116.                                 )
  117.                             )
  118.                             (loop
  119.                                 (neq triangulationStage 2)
  120.                                 (block
  121.                                     (stepAngle newPos)
  122.                                     ; Step vertically
  123.                                     (setq stepAngle
  124.                                         (if
  125.                                             (@ deltaCoord 1)
  126.                                             90 ; To the top
  127.                                             270 ; To the bottom
  128.                                         )
  129.                                     )
  130.                                     (setq offsetDelta
  131.                                         (if
  132.                                             (@ deltaCoord 1)
  133.                                             1 ; To the top
  134.                                             -1 ; To the bottom
  135.                                         )
  136.                                     )
  137.                                     (setq newPos
  138.                                         (sysVectorPolarOffset
  139.                                             currentPos
  140.                                             stepAngle
  141.                                             gridSpacing
  142.                                         )
  143.                                     )
  144.                                     (if (outOfBoundaryCondition originCoord offsetCoord gridEdge) (not (setq triangulationStage 2))) ; We are out of grid, no point going further
  145.                                     (if
  146.                                         (ls
  147.                                             (sysVectorDistance
  148.                                                 newPos
  149.                                                 origin
  150.                                             )
  151.                                             closestApproach
  152.                                         )
  153.                                         (block Nil
  154.                                             (setq closestApproach (sysVectorDistance newPos origin)) ; We're closer verically
  155.                                             (setq currentPos newPos)
  156.                                             (set@ offsetCoord 1 (+ (@ offsetCoord 1) offsetDelta))
  157.                                         )
  158.                                         (setq triangulationStage 2); Otherwise we've already found the closest position in the grid
  159.                                     )
  160.                                 )
  161.                             )
  162.                             ; We've got offsetCoord
  163.                             ; We've got originCoord
  164.                             ; Now we can add them together and we are good to go
  165.                             (setq xx
  166.                                 (+
  167.                                     (@ originCoord 0)
  168.                                     (@ offsetCoord 0)
  169.                                 )
  170.                             )
  171.                             (setq yy
  172.                                 (+
  173.                                     (@ originCoord 1)
  174.                                     (@ offsetCoord 1)
  175.                                 )
  176.                             )
  177.                             (setq closestID ; Serialize the ID
  178.                                 (expGetIDByCoordinate (list xx yy) gridID)
  179.                             )
  180.                             (objGetObjByID closestID) ; Return the closest verticle
  181.                         )
  182.                     )
  183.                 )
  184.             )
  185.         )
  186.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement