Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MaxTraceDistance=10
  2. var TraceLoopIncrease=10
  3. var LimitTrace=20
  4. var Speed=2
  5. var StepTimeOut=500
  6.  
  7. var CheckCnt=1
  8. var DynamicPause=30
  9.  
  10. #Return True if END should be forced
  11. Sub CustomIsEnd(GoX,GoY,Prec)
  12.   return false
  13. end sub
  14.  
  15. Sub Walker(GoX,GoY,Prec)
  16.   UO.SetGlobal("Logging","0")
  17.   UO.SetGlobal("BlindWalk","0")
  18.   var LastDir
  19.   var MoveRes
  20.   var TracerCnt
  21.   var TraceDirection
  22.   UO.SetGlobal("GlobalGoX",str(GoX))
  23.   UO.SetGlobal("GlobalGoY",str(GoY))
  24.   while not IsEnd(GoX,GoY,Prec)
  25.     LogInfo('Just walking')
  26.     LastDir=GoUntilHit(GoX,GoY,Prec)
  27.  
  28.     if IsEnd(GoX,GoY,Prec) then
  29.         return 1
  30.     end if
  31.  
  32.     TraceDirection=CountDirection(GoX,GoY,CurDir,Prec)
  33.     TracerCnt=MaxTraceDistance
  34.     repeat
  35.       LogInfo('Tracing')
  36.      MoveRes = FullAroundTrace(LastDir,TraceDirection,GoX,GoY,Prec,TracerCnt)
  37.       TracerCnt = TracerCnt + TraceLoopIncrease
  38.       TraceDirection = -TraceDirection
  39.     until (MoveRes == 1) OR (TracerCnt > LimitTrace)
  40.   wend
  41.   return 1
  42. end sub
  43.  
  44. Sub FullAroundTrace(StartDir,Direction,GoX,GoY,Prec,MaxTrace)
  45.   var LineX=UO.GetX()
  46.   var LineY=UO.GetY()
  47.   var CurDir=StartDir
  48.   var StartX,StartY
  49.   var MovesDone=0
  50.   repeat
  51.     StartX=UO.GetX()
  52.     StartY=UO.GetY()
  53.     CurDir=Tracer(CurDir,Direction,Prec)
  54.     if (CurDir==GetDir(GoX,GoY,Prec)) then
  55.       return 1
  56.     endif
  57.     if MovesDone>0 and PrevMoveCross(GetDirToDir(UO.GetDir(),Prec),LineX,LineY,GoX,GoY,StartX,StartY,Prec) then
  58.       return 1
  59.     end if
  60.     MovesDone=MovesDone+1
  61.     CurDir=AddToDir(CurDir,-Direction,Prec)
  62.   until MovesDone>MaxTrace
  63.   return 0
  64. end sub
  65.  
  66. Sub CountDirection(GoX,GoY,StartDir,Prec)
  67.   var GoDir=GetDir(GoX,GoY,Prec)
  68.   var MyX=UO.GetX()
  69.   var MyY=UO.GetY()
  70.   if GoDir<StartDir then
  71.     return -1
  72.   end if
  73.   if GoDir>StartDir then
  74.     return 1
  75.   end if
  76.   if Abs(MyX-GoX)>Abs(MyY-GoY) then
  77.     if (GoDir==3) OR (GoDir==7) then
  78.       return -1
  79.     else
  80.       return 1
  81.     end if
  82.   else
  83.     if (GoDir==1) or (GoDir==5) then
  84.       return -1
  85.     else
  86.       return 1
  87.     end if
  88.   end if
  89. end sub
  90.  
  91. Sub PrevMoveCross(Dir,x1,y1,x2,y2,StartX,StartY,Prec)
  92.   var x3,y3,x4,y4
  93.   x3=StartX
  94.   y3=StartY
  95.   x4=StartX+(XFromDir(Dir)-StartX)*Speed
  96.   y4=StartY+(YFromDir(Dir)-StartY)*Speed
  97.   return LinesCrossing(x1,y1,x2,y2,x3,y3,x4,y4,Prec)
  98. end sub
  99.  
  100. Sub LinesCrossing(x1,y1,x2,y2,x3,y3,x4,y4,Prec)
  101.   if x1==x3 and y1==y3 then
  102.     return false
  103.   end if
  104.  
  105.    LogInfo('Start cross check')
  106.  
  107.   var ua1=(x4-x3)*(y1-y3)-(y4-y3)*(x1-x3)
  108.   var ub1=(y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
  109.  
  110.   var ua2=(x2-x1)*(y1-y3)-(y2-y1)*(x1-x3)
  111.   var ub2=(y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
  112.  
  113.   #Check for parallel lines
  114.   if (ub1 == 0) or (ub2 == 0) then
  115.    return false
  116.   end if
  117.  
  118.   var u1=ua1/ub1
  119.   var u2=ua2/ub2
  120.  
  121.   if IsDiap(u1,0,1,0.01) and IsDiap(u2,0,1,0.01) then
  122.     LogInfo('Vectors crossing info:')
  123.    LogInfo('x1= '+str(x1))
  124.    LogInfo('y1= '+str(y1))
  125.    LogInfo('x2= '+str(x2))
  126.    LogInfo('y2= '+str(y2))
  127.    LogInfo('x3= '+str(x3))
  128.    LogInfo('y3= '+str(y3))
  129.    LogInfo('x4= '+str(x4))
  130.    LogInfo('y4= '+str(y4))
  131.    return true
  132.   else
  133.   LogInfo('End cross check')
  134.    return false
  135.   end if
  136. end sub
  137.  
  138. Sub IsDiap(X,X0,X1,Prec)
  139.   if X<=X0+Prec and X>=X1-Prec OR X>=X0-Prec and X<=X1+Prec then
  140.     return true
  141.   end if
  142.   return false
  143. end sub
  144.  
  145. Sub GoUntilHit(GoX,GoY,Prec)
  146.   var LastDir
  147.   LastDir=GetDir(GoX,GoY,Prec)
  148.   var CouldMove
  149.   repeat
  150.     LastDir=GetDir(GoX,GoY,Prec)
  151.     if LastDir<>0 and not IsEnd(GoX,GoY,Prec) then
  152.       CouldMove=TryDir(LastDir,Prec)
  153.     end if
  154.   until LastDir==0 or IsEnd(GoX,GoY,Prec) or not CouldMove
  155.   return LastDir
  156. end sub
  157.  
  158. Sub IsEnd(GoX,GoY,Prec)
  159.   if (CustomIsEnd(GoX, GoY, Prec) == true) then
  160.     return true
  161.   end if
  162.   if Abs(UO.GetX()-GoX)<=Prec and Abs(UO.GetY()-GoY)<=Prec then
  163.     LogInfo('END FOUND')
  164.    return true
  165.   else
  166.     return false
  167.   end if
  168. end sub
  169.  
  170. Sub GetDir(GoX,GoY,Prec)
  171.   var MyX=UO.GetX()
  172.   var MyY=UO.GetY()
  173.   var DiffX=Abs(UO.GetX()-GoX)
  174.   var DiffY=Abs(UO.GetY()-GoY)
  175.   var GoDir=0
  176.   if (DiffX/(DiffY+0.1))>=2 then
  177.     if (MyX>GoX) then
  178.       GoDir=7
  179.     else
  180.       GoDir=3
  181.     end if
  182.   else
  183.  
  184.   if (DiffY/(DiffX+0.1))>=2 then
  185.     if (MyY>GoY) then
  186.       GoDir=5
  187.     else
  188.       GoDir=1
  189.     end if
  190.   else
  191.  
  192.   if (MyX>GoX) and (MyY>GoY) then
  193.     GoDir=6
  194.   else
  195.   if (MyX>GoX) and (MyY<GoY) then
  196.     GoDir=8
  197.   else
  198.   if (MyX<GoX) and (MyY>GoY) then
  199.     GoDir=4
  200.   else
  201.   if (MyX<GoX) and (MyY<GoY) then
  202.     GoDir=2
  203.   end if
  204.   end if
  205.   end if
  206.   end if
  207.   end if
  208.   end if
  209.   return GoDir
  210. end sub
  211.  
  212. Sub Tracer(StartDir,Direction,Prec)
  213.   var CurDir=StartDir
  214.   var Result
  215.   repeat
  216.     Result=TryDir(CurDir,Prec)
  217.     if not Result then
  218.       CurDir=AddToDir(CurDir,Direction,Prec)
  219.     end if
  220.   until Result
  221.   return Result
  222. end sub
  223.  
  224. Sub AddToDir(Dir,Cnt,Prec)
  225.   var NewDir=Dir
  226.   NewDir=NewDir+Cnt
  227.   while NewDir>8
  228.     NewDir=NewDir-8
  229.   wend
  230.   while NewDir<1
  231.     NewDir=NewDir+8
  232.   wend
  233.   return NewDir
  234. end sub
  235.  
  236. Sub TryDir(Dir,Prec)
  237.   var BegX=UO.GetX() # Ia?aeuiia O
  238.   var BegY=UO.GetY() # Ia?aeuiia Y
  239.   var Counter=0
  240.   var GoX=BegX
  241.   var GoY=BegY
  242.   GoX=XFromDir(Dir)
  243.   GoY=YFromDir(Dir)
  244.   if not IsPass(GoX,GoY) then
  245.     LogInfo(str(GoX)+':'+str(GoY)+' is not passable')
  246.    return false
  247.   end if
  248.   return TurnAndGoDir(Dir,Prec)
  249. end sub
  250.  
  251. Sub XFromDir(Dir)
  252.   if Dir==2 OR Dir==3 Or Dir==4 then
  253.     return (UO.GetX()+1)
  254.   end if
  255.   if Dir==6 OR Dir==7 Or Dir==8 then
  256.     return (UO.GetX()-1)
  257.   end if
  258.   return (UO.GetX())
  259. end sub
  260.  
  261. Sub YFromDir(Dir)
  262.   if Dir==8 OR Dir==1 Or Dir==2 then
  263.     return (UO.GetY()+1)
  264.   end if
  265.   if Dir==4 OR Dir==5 Or Dir==6 then
  266.     return (UO.GetY()-1)
  267.   end if
  268.   return (UO.GetY())
  269. end sub
  270.  
  271. Sub TurnAndGoDir(Dir,Prec)
  272.   var StartDir=GetDirToDir(UO.GetDir(),Prec)
  273.   var StartX=UO.GetX()
  274.   var StartY=UO.GetY()
  275.   var EndDir
  276.   var Counter=0
  277.   var TestCounter=CheckCnt
  278.  
  279. #Direction Test
  280.   repeat
  281.     Counter = 0
  282.     LogInfo('Dir: '+str(StartDir)+' Counter: '+str(Counter))
  283.    PressDir(Dir)
  284.     repeat
  285.       EndDir=GetDirToDir(UO.GetDir(),Prec)
  286.       wait(DynamicPause)
  287.       Counter=Counter+1
  288.     until StartDir<>EndDir or StartY<>UO.GetY() or StartX<>UO.GetX() or Counter>=StepTimeOut/DynamicPause
  289.     TestCounter = TestCounter - 1
  290.     LogInfo('Dir: '+str(EndDir)+' Counter: '+str(Counter))
  291.  until TestCounter <= 0 or Counter < StepTimeOut/DynamicPause
  292.  
  293.   if Counter>=StepTimeOut/DynamicPause  then
  294.      LogInfo('Direction timeout reached')
  295.     return 0
  296.   end if
  297.   #End direction Test
  298.  
  299.   if StartY<>UO.GetY() or StartX<>UO.GetX() then
  300.     return Dir
  301.   end if
  302.  
  303.   #Start Primary test
  304.   TestCounter=CheckCnt
  305.   repeat
  306.     GoDir(Dir,Prec)
  307.     Counter=0
  308.     repeat
  309.       wait(DynamicPause)
  310.       Counter=Counter+1
  311.     until StartY<>UO.GetY() or StartX<>UO.GetX() or Counter>=StepTimeOut/DynamicPause
  312.     TestCounter = TestCounter - 1
  313.   until TestCounter <= 0 or Counter < StepTimeOut/DynamicPause
  314.   #End primary test
  315.  
  316.   if Counter>=StepTimeOut/DynamicPause then
  317.     LogInfo('Step timeout reached')
  318.    return 0
  319.   else
  320.     return Dir
  321.   end if
  322. end sub
  323.  
  324. Sub GetDirToDir(GotDir,Prec)
  325.    var ChangedDir=-GotDir
  326.    ChangedDir=AddToDir(ChangedDir,5,Prec)
  327.    return ChangedDir
  328. end sub
  329.  
  330. Sub DirToInj(Dir)
  331.    dim Dirs[9]
  332.    Dirs[1]=1
  333.    Dirs[2]=2
  334.    Dirs[3]=3
  335.    Dirs[4]=6
  336.    Dirs[5]=9
  337.    Dirs[6]=8
  338.    Dirs[7]=7
  339.    Dirs[8]=4
  340.  
  341.    return (Dirs[Dir])
  342. end sub
  343.  
  344. Sub PressDir(Dir)
  345.    dim Dirs[9]
  346.    Dirs[1]=35
  347.    Dirs[2]=40
  348.    Dirs[3]=34
  349.    Dirs[4]=39
  350.    Dirs[5]=33
  351.    Dirs[6]=38
  352.    Dirs[7]=36
  353.    Dirs[8]=37
  354.  
  355.    UO.Press(Dirs[Dir],1)
  356. end sub
  357.  
  358. Sub GoDir(Dir,Prec)
  359.    dim Dirs[9]
  360.    Dirs[1]=35
  361.    Dirs[2]=40
  362.    Dirs[3]=34
  363.    Dirs[4]=39
  364.    Dirs[5]=33
  365.    Dirs[6]=38
  366.    Dirs[7]=36
  367.    Dirs[8]=37
  368.  
  369.    var DistanceX=Abs(UO.GetX()-val(UO.GetGlobal("GlobalGoX")))
  370.    var DistanceY=Abs(UO.GetY()-val(UO.GetGlobal("GlobalGoY")))
  371.    var GoDistance
  372.    if (DistanceX-Prec)<Speed then
  373.       GoDistance=DistanceX-Prec
  374.    else
  375.       if (DistanceY-Prec)<Speed then
  376.          GoDistance=DistanceY-Prec
  377.       else
  378.          GoDistance=Speed
  379.       endif
  380.    endif
  381.  
  382.    UO.Press(Dirs[Dir],GoDistance)
  383. end sub
  384.  
  385. Sub IsPass(X,Y)
  386.   if UO.GetGlobal("BlindWalk") then
  387.     return true
  388.   endif
  389.  
  390.    dim Types[60]
  391.    Types[1]=3
  392.    Types[2]=25
  393.    Types[3]=51
  394.    Types[4]=63
  395.    Types[5]=113
  396.    Types[6]=140
  397.    Types[7]=172
  398.    Types[8]=219
  399.    Types[9]=232
  400.    Types[10]=235
  401.    Types[11]=239
  402.    Types[12]=243
  403.    Types[13]=248
  404.    Types[14]=251
  405.    Types[15]=264
  406.    Types[16]=267
  407.    Types[17]=282
  408.    Types[18]=289
  409.    Types[19]=321
  410.    Types[20]=379
  411.    Types[21]=420
  412.    Types[22]=440
  413.    Types[23]=476
  414.    Types[24]=499
  415.    Types[25]=513
  416.    Types[26]=542
  417.    Types[27]=578
  418.    Types[28]=586
  419.    Types[29]=622
  420.    Types[30]=700
  421.    Types[31]=804
  422.    Types[32]=1740
  423.    Types[33]=1758
  424.    Types[34]=1770
  425.    Types[35]=1779
  426.    Types[36]=1779
  427.    Types[37]=1881
  428.    Types[38]=1886
  429.    Types[39]=1801
  430.    Types[40]=1805
  431.    Types[41]=1813
  432.    Types[42]=1820
  433.    Types[43]=1831
  434.    Types[44]=1833
  435.    Types[45]=1843
  436.    Types[46]=1850
  437.    Types[47]=1873
  438.    Types[48]=1876
  439.    Types[49]=1885
  440.    Types[50]=1888
  441.    Types[51]=1946
  442.    Types[52]=1969
  443.    Types[53]=2500
  444.    Types[54]=2539
  445.  
  446.   for var i=1 TO 53 STEP 2
  447.     if UO.PrivateGetTile(X,Y,-1,Types[i],Types[i+1]) then
  448.       return true
  449.     end if
  450.   next
  451.   return false
  452. end sub
  453.  
  454. Sub LogInfo(Line)
  455.   if not UO.GetGlobal("Logging")=="0" then
  456.     UO.TextOpen()
  457.     UO.TextPrint(str(UO.GetX())+":"+str(UO.GetY())+" - "+Line);
  458.   end if
  459. end sub
  460.  
  461. Sub Abs(X)
  462.   if X>0 then
  463.     return X
  464.   else
  465.     return (-X)
  466.   end if
  467. end sub
  468. ########################################## ????? ??????? 3 ##########################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement