Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.89 KB | None | 0 0
  1. PrintChat("Waypoints Library by GamSterOn")
  2.  
  3. local WP = {}
  4. local Heroes = {}
  5.  
  6. OnLoad(function()
  7. table.insert(Heroes, myHero)
  8. WP[GetNetworkID(myHero)] = { time = 0, path = {}, ismoving = false, lenght = 0 }
  9. end)
  10.  
  11. OnObjectLoad(function(o)
  12. if GetObjectType(o) == Obj_AI_Hero then
  13. table.insert(Heroes, o)
  14. WP[GetNetworkID(o)] = { time = 0, path = {}, ismoving = false, lenght = 0 }
  15. end
  16. end)
  17.  
  18. function WP_Moving(unit)
  19. if WP[GetNetworkID(unit)] then return WP[GetNetworkID(unit)].ismoving end
  20. end
  21.  
  22. function WP_Time(unit)
  23. if WP[GetNetworkID(unit)] then return WP[GetNetworkID(unit)].time end
  24. end
  25.  
  26. function WP_Path(unit)
  27. if WP[GetNetworkID(unit)] and #WP[GetNetworkID(unit)].path > 0 then return WP[GetNetworkID(unit)].path end
  28. end
  29.  
  30. function WP_DistOnPath(unit)
  31. local id = GetNetworkID(unit)
  32. if WP[id] then return GetMoveSpeed(unit) * (GetTickCount() - WP[id].time) / 1000 end
  33. end
  34.  
  35. function WP_TravelDist(unit)
  36. if WP[GetNetworkID(unit)] then return WP[GetNetworkID(unit)].lenght end
  37. end
  38.  
  39. function WP_NormalizeVector(vec)
  40. local num = 1 / math.sqrt(vec.x^2 + vec.z^2)
  41. return { x = vec.x * num, z = vec.z * num }
  42. end
  43.  
  44. function WP_ExtendedPosition(vec1, vec2, s)
  45. local normalized = WP_NormalizeVector({ x = vec1.x - vec2.x, z = vec1.z - vec2.z})
  46. if normalized and normalized.x then
  47. local px = vec2.x + (normalized.x * s)
  48. local pz = vec2.z + (normalized.z * s)
  49. return { x = px, y = 0, z = pz }
  50. end
  51. end
  52.  
  53. function WP_InterceptionTime(source, startP, endP, unitspeed, spellspeed)
  54. local sx = source.x
  55. local sy = source.z
  56. local ux = startP.x
  57. local uy = startP.z
  58. local dx = endP.x - ux
  59. local dy = endP.z - uy
  60. local magnitude = math.sqrt(dx * dx + dy * dy)
  61. dx = (dx / magnitude) * unitspeed
  62. dy = (dy / magnitude) * unitspeed
  63. local a = (dx * dx) + (dy * dy) - (spellspeed * spellspeed)
  64. local b = 2 * ((ux * dx) + (uy * dy) - (sx * dx) - (sy * dy))
  65. local c = (ux * ux) + (uy * uy) + (sx * sx) + (sy * sy) - (2 * sx * ux) - (2 * sy * uy)
  66. local d = (b * b) - (4 * a * c)
  67. if d > 0 then
  68. local t1 = (-b + math.sqrt(d)) / (2 * a)
  69. local t2 = (-b - math.sqrt(d)) / (2 * a)
  70. return math.max(t1, t2)
  71. end
  72. if d >= 0 and d < 0.00001 then
  73. return -b / (2 * a)
  74. end
  75. return 0.0001
  76. end
  77.  
  78. function WP_DistPointLineSegment(a, b, c)
  79. local ax = a.x
  80. local ay = a.z
  81. local bx = b.x
  82. local by = b.z
  83. local cx = c.x
  84. local cy = c.z
  85. local dx = bx - ax
  86. local dy = by - ay
  87. local t = ((cx - ax) * dx + (cy - ay) * dy) / (dx * dx + dy * dy)
  88. if t < 0 then
  89. dx = cx - ax
  90. dy = cy - ay
  91. elseif t > 1 then
  92. dx = cx - bx
  93. dy = cy - by
  94. else
  95. dx = cx - (ax + (t * dx))
  96. dy = cy - (ay + (t * dy))
  97. end
  98. return math.sqrt( dx^2 + dy^2 )
  99. end
  100.  
  101. function WP_GetPredPointOnPath(source, unit, speed, width, delay)
  102. local id = GetNetworkID(unit)
  103. if WP[id] then
  104. local path = WP[id].path
  105. local pos = GetOrigin(unit)
  106. if not WP[id].ismoving then
  107. return pos
  108. end
  109. local ms = GetMoveSpeed(unit)
  110. local spos = GetOrigin(source)
  111. local d = 0
  112. for i = 1, #path - 1, 1 do
  113. local pi = path[i]
  114. local pi1 = path[i+1]
  115. d = d + math.sqrt( (pi.x-pi1.x)^2 + (pi.z-pi1.z)^2 )
  116. local dop = WP_DistOnPath(unit)
  117. if d > dop then
  118. local dd = math.sqrt( (pos.x-pi1.x)^2 + (pos.z-pi1.z)^2 )
  119. local t = WP_InterceptionTime(spos, pos, pi1, ms, speed) + delay
  120. local s = (ms * t) - (width/2)
  121. if dd >= s then
  122. local pos = WP_ExtendedPosition(pi1, pos, s)
  123. if pos and pos.x then
  124. return pos
  125. end
  126. end
  127. if i + 1 == #path then
  128. local pos = WP_ExtendedPosition(pi1, pos, dd)
  129. if pos and pos.x then
  130. return pos
  131. end
  132. end
  133. for j = i + 1, #path - 1, 1 do
  134. local pj = path[j]
  135. local pj1 = path[j + 1]
  136. t = WP_InterceptionTime(spos, pj, pj1, ms, speed) - (dd / ms) + delay
  137. s = (ms * t) - (width/2)
  138. dd = math.sqrt( (pj.x-pj1.x)^2 + (pj.z-pj1.z)^2 )
  139. if dd >= s then
  140. local pos = WP_ExtendedPosition(pj1, pj, s)
  141. if pos and pos.x then
  142. return pos
  143. end
  144. end
  145. if j + 1 == #path then
  146. local pos = WP_ExtendedPosition(pj1, pj, dd)
  147. if pos and pos.x then
  148. return pos
  149. end
  150. end
  151. end
  152. end
  153. end
  154. end
  155. end
  156.  
  157. function WP_MCollision(source, castpos, range, speed, width, delay)
  158. local col = {}
  159. for m, minion in pairs(minionManager.objects) do
  160. if ValidTarget(minion, range + 500) then
  161. local minionpos = WP_GetPredPointOnPath(source, minion, speed, width, delay)
  162. if minionpos and minionpos.x then
  163. local dist = WP_DistPointLineSegment(GetOrigin(source), castpos, minionpos)
  164. if dist < width + GetHitBox(minion) + ((GetLatency() / 1000) * GetMoveSpeed(minion)) + 25 then
  165. table.insert(col, minion)
  166. end
  167. end
  168. end
  169. end
  170. return col
  171. end
  172.  
  173. function WP_HCollision(source, unit, castpos, range, speed, width, delay)
  174. local col = {}
  175. local uid = GetNetworkID(unit)
  176. for t, target in pairs(GetEnemyHeroes()) do
  177. local tid = GetNetworkID(target)
  178. if ValidTarget(target, range + 500) and tid ~= uid then
  179. local targetpos = WP_GetPredPointOnPath(source, target, speed, width, delay)
  180. if targetpos and targetpos.x then
  181. local dist = WP_DistPointLineSegment(GetOrigin(source), castpos, targetpos)
  182. if dist < width + GetHitBox(target) + ((GetLatency() / 1000) * GetMoveSpeed(target)) + 25 then
  183. table.insert(col, target)
  184. end
  185. end
  186. end
  187. end
  188. return col
  189. end
  190.  
  191. function WP_GetExtendedPointOnPath(unit, s)
  192. local id = GetNetworkID(unit)
  193. if WP[id] then
  194. local path = WP[id].path
  195. local pos = GetOrigin(unit)
  196. if not WP[id].ismoving then
  197. return pos
  198. end
  199. local d = 0
  200. for i = 1, #path - 1, 1 do
  201. local pi = path[i]
  202. local pi1 = path[i+1]
  203. d = d + math.sqrt( (pi.x-pi1.x)^2 + (pi.z-pi1.z)^2 )
  204. local dop = WP_DistOnPath(unit)
  205. if d > dop then
  206. local dd = math.sqrt( (pos.x-pi1.x)^2 + (pos.z-pi1.z)^2 )
  207. if dd >= s then
  208. local pos = WP_ExtendedPosition(pi1, pos, s)
  209. if pos and pos.x then
  210. return pos
  211. end
  212. end
  213. if i + 1 == #path then
  214. local pos = WP_ExtendedPosition(pi1, pos, dd)
  215. if pos and pos.x then
  216. return pos
  217. end
  218. end
  219. local ss = s
  220. for j = i + 1, #path - 1, 1 do
  221. local pj = path[j]
  222. local pj1 = path[j + 1]
  223. ss = ss - dd
  224. dd = math.sqrt( (pj.x-pj1.x)^2 + (pj.z-pj1.z)^2 )
  225. if dd >= ss then
  226. local pos = WP_ExtendedPosition(pj1, pj, ss)
  227. if pos and pos.x then
  228. return pos
  229. end
  230. end
  231. if j + 1 == #path then
  232. local pos = WP_ExtendedPosition(pj1, pj, dd)
  233. if pos and pos.x then
  234. return pos
  235. end
  236. end
  237. end
  238. end
  239. end
  240. end
  241. end
  242.  
  243. local function StopMoveLogic(id, speed)
  244. local count = #WP[id].path
  245. if count > 1 then
  246. if GetTickCount() > WP[id].time + 50 then
  247. local d = 0
  248. for i = 1, count-1, 1 do
  249. local px = WP[id].path[i].x-WP[id].path[i+1].x
  250. local py = WP[id].path[i].z-WP[id].path[i+1].z
  251. d = d + math.sqrt( px^2 + py^2 )
  252. end
  253. WP[id].lenght = d
  254. local s = speed * (GetTickCount() - WP[id].time) / 1000
  255. if d > 0 and d - s < 15 then
  256. WP[id].ismoving = false
  257. WP[id].path = WP[id].path[count]
  258. WP[id].time = GetTickCount()
  259. --PrintChat("DEBUG STOPMOVE")
  260. end
  261. end
  262. end
  263. end
  264.  
  265. OnDeleteObj(function(object)
  266. local id = GetNetworkID(object)
  267. if WP[id] then
  268. WP[id] = nil
  269. end
  270. end)
  271.  
  272. OnProcessWaypoint(function(unit,waypoint)
  273. local id = GetNetworkID(unit)
  274. if WP[id] then
  275. if waypoint.index == 2 then
  276. --PrintChat("DEBUG OnProcessWaypoint: move")
  277. WP[id].ismoving = true
  278. end
  279. if GetTickCount() > WP[id].time + 1 then
  280. --PrintChat("DEBUG OnProcessWaypoint: new waypoint")
  281. WP[id].time = GetTickCount()
  282. WP[id].path = {}
  283. end
  284. if #WP[id].path == 0 and waypoint.index == 1 then
  285. --PrintChat("DEBUG OnProcessWaypoint: not move")
  286. WP[id].ismoving = false
  287. end
  288. table.insert(WP[id].path, waypoint.position)
  289. end
  290. end)
  291.  
  292. OnTick(function(myHero)
  293. for m, minion in pairs(minionManager.objects) do
  294. local networkID = GetNetworkID(minion)
  295. if not WP[networkID] then
  296. WP[networkID] = { time = 0, path = {}, ismoving = false, lenght = 0 }
  297. end
  298. StopMoveLogic(networkID, GetMoveSpeed(minion))
  299. end
  300. for u,unit in ipairs(Heroes) do
  301. StopMoveLogic(GetNetworkID(unit), GetMoveSpeed(unit))
  302. end
  303. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement