Advertisement
dahhoovy

trainrunner.lua

Sep 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. if CLIENT then return end
  2.  
  3. trainSpeed = 60
  4.  
  5. trainTrack = {}
  6. trainTrack[1] = {pos = Vector(2275.99,-1536.68,-262), ang = Angle(0,90,0), tele = true, busstop = false}
  7. trainTrack[2] = {pos = Vector(2272.5,-4714.65,-262), ang = Angle(0,90,0), tele =false, busstop = true}
  8.  
  9. trainSegment = 1
  10.  
  11.  
  12.  
  13. // scrubbing time!
  14. for k,v in pairs(trainTrack) do
  15. track2 = trainTrack[1]
  16. if trainTrack[k+1] then
  17. track2 = trainTrack[k+1]
  18. end
  19. v.pos = Vector(math.Round(v.pos.x), math.Round(v.pos.y), math.Round(v.pos.z))
  20. v.ang = Angle(math.Round(v.ang.p), math.Round(v.ang.y), math.Round(v.ang.r))
  21. if v.ang == track2.ang then
  22. local dir = (v.pos - track2.pos):GetNormal()
  23. if dir.x==1 then track2.pos[x]=v.pos[x] end
  24. if dir.y==1 then track2.pos[y]=v.pos[y] end
  25. if dir.z==1 then track2.pos[z]=v.pos[z] end
  26. end
  27.  
  28. trainPositions = {}
  29. trainPositions[1] = table.Copy(trainTrack[1])
  30. trainPositions[2] = table.Copy(trainTrack[2])
  31.  
  32. end
  33.  
  34. function writeRedirectorInfo()
  35.  
  36. if not trainTrack then return end
  37.  
  38. local S = ""
  39. for i,place in pairs(trainTrack) do
  40. if (place.busstop==true) then
  41. //print("truthfound")
  42. end
  43. S = S .. "$" .. tostring(place.tele) ..") " .. tostring(place.busstop) .. ") "
  44. S = S .. math.Round(place.pos.x,3) .. ";" .. math.Round(place.pos.y,3) .. ";" .. math.Round(place.pos.z,3) .. ";".. math.Round(place.ang.p,3) .. ";" .. math.Round(place.ang.y,3) .. ";" .. math.Round(place.ang.r,3)
  45. end
  46. file.Write("trainredirectors_"..game.GetMap()..".txt",S)
  47. end
  48.  
  49.  
  50. function readRedirectorInfo()
  51.  
  52. local str = file.Read("trainredirectors_"..game.GetMap()..".txt") || ""
  53.  
  54. local lines = string.Explode("$",str)
  55. //PrintTable(st)
  56. trainTrack = {}
  57.  
  58. for k,v in pairs(lines) do
  59. if v != "" then
  60. local rtab = string.Explode(") ",v)
  61. local tel = rtab[1]
  62. local bstop = rtab[2]
  63.  
  64. local coords = string.Explode(";",rtab[3])
  65.  
  66.  
  67.  
  68.  
  69. local trackPiece = {ang = Angle(coords[4],coords[5],coords[6]), pos = Vector(coords[1],coords[2],coords[3]), tele = tobool(tel), busstop = tobool(bstop)}
  70. //print(bstop)
  71. //if bstop==true then
  72. //print("truth")
  73. //end
  74.  
  75.  
  76. // dunno why i have to do this
  77. trackPiece.ang.p = coords[4]
  78. trackPiece.ang.y = coords[5]
  79. trackPiece.ang.r = coords[6]
  80. table.insert(trainTrack, trackPiece)
  81. end
  82. end
  83. for k,plyy in pairs(player.GetAll()) do
  84. if plyy:IsSuperAdmin() then
  85. sendJoinTrains(plyy)
  86. end
  87. end
  88. end
  89.  
  90.  
  91.  
  92.  
  93.  
  94. // how many times we're going to move when we go between two redirects
  95. movementSections = 35
  96. movementSectionCurrent = 1
  97.  
  98. function moveTrain()
  99. if not GAMEMODE.busStop then GAMEMODE.busStop=false end
  100.  
  101. if not trainTrack[1] || not trainTrack[2] then print("No track, probably no file to read either.") return end
  102.  
  103. if not IsValid(trainMain) then // Create a train if there ever isn't one
  104.  
  105. GAMEMODE.trackComingFrom = 1
  106. GAMEMODE.trackGoingTo = 2
  107.  
  108. tp = trainTrack[GAMEMODE.trackComingFrom].pos
  109. ta = trainTrack[GAMEMODE.trackGoingTo].ang
  110.  
  111. //trainMain = nil
  112. trainMain = ents.Create("prop_physics")
  113. trainMain:SetModel("models/inaki/vehicles/borderlands_bus.mdl")
  114. trainMain:SetPos(tp)
  115. trainMain:SetAngles(ta)
  116. trainMain:SetNoDraw(false)
  117. trainMain:Spawn()
  118. trainMain:SetCollisionGroup( COLLISION_GROUP_WORLD )
  119. trainMain.seats = {}
  120.  
  121. // this is just for shorthand
  122. //ta = trainPositions[1].ang
  123. //tp = trainPositions[1].pos
  124. local seatsToSpawn = {
  125. tp + ta:Up()*50 + ta:Forward()*-20,
  126. tp + ta:Up()*50 + ta:Right()*130 + ta:Forward()*-20,
  127. tp + ta:Up()*50 + ta:Right()*130,
  128. tp + ta:Up()*50 + ta:Right()*130 + ta:Forward()*20,
  129. tp + ta:Up()*50 + ta:Right()*60 + ta:Forward()*-20,
  130. tp + ta:Up()*50 + ta:Right()*30 + ta:Forward()*-20,
  131. tp + ta:Up()*50 + ta:Right()*-30 + ta:Forward()*-20,
  132. tp + ta:Up()*50 + ta:Right()*-60 + ta:Forward()*-20
  133. }
  134.  
  135. for k,v in pairs(seatsToSpawn) do
  136. trainMain.seats[k] = ents.Create("prop_vehicle_prisoner_pod")
  137. trainMain.seats[k]:SetModel("models/nova/jeep_seat.mdl")
  138. trainMain.seats[k]:SetPos(v)
  139. trainMain.seats[k]:SetAngles(ta)
  140. trainMain.seats[k]:SetParent(trainMain)
  141. trainMain.seats[k]:Spawn()
  142. trainMain.seats[k]:SetCollisionGroup( COLLISION_GROUP_WORLD )
  143. if not trainMain.seats[k].DoorData then
  144. trainMain.seats[k].DoorData = {}
  145. end
  146. trainMain.seats[k]:setKeysNonOwnable(true)
  147. //trainMain.seats[k]:Activate()
  148. end
  149.  
  150. local phys = trainMain:GetPhysicsObject()
  151. if IsValid(phys) then
  152. phys:EnableMotion(false)
  153. end
  154.  
  155. readRedirectorInfo()
  156. end
  157.  
  158. if not IsValid(trainMain) || not istable(trainTrack) then return end
  159.  
  160. if not trainTrack[GAMEMODE.trackComingFrom] || not trainTrack[GAMEMODE.trackGoingTo] then
  161. print("No track! Attempting to read redirector info...")
  162. readRedirectorInfo()
  163. return
  164. end
  165.  
  166. movementSectionCurrent = movementSectionCurrent+1
  167.  
  168. // If the train has gone further than the next redirect, or it's a teleport
  169. if trainMain:GetPos():Distance(trainTrack[GAMEMODE.trackComingFrom].pos) > trainTrack[GAMEMODE.trackComingFrom].pos:Distance(trainTrack[GAMEMODE.trackGoingTo].pos) || trainTrack[GAMEMODE.trackGoingTo].tele==true then
  170. //print("nextpiece")
  171. trainMain:SetPos(trainTrack[GAMEMODE.trackGoingTo].pos)
  172.  
  173. // adjust 90 here for the model being off
  174. //local angtrainTrack[GAMEMODE.trackGoingTo].ang
  175. ta = trainTrack[GAMEMODE.trackGoingTo].ang
  176. local fixedFinalTurn = Angle(ta.p,ta.y,ta.r)
  177. fixedFinalTurn:RotateAroundAxis(fixedFinalTurn:Up(),90)
  178. trainMain:SetAngles(fixedFinalTurn)
  179.  
  180.  
  181. GAMEMODE.trackComingFrom = GAMEMODE.trackComingFrom + 1
  182. GAMEMODE.trackGoingTo = GAMEMODE.trackGoingTo + 1
  183. // we bumped up both, now we check if either overran, to loop back to 1, now we have our current track
  184. if GAMEMODE.trackComingFrom>table.Count(trainTrack) then
  185. GAMEMODE.trackComingFrom = 1
  186. end
  187. if GAMEMODE.trackGoingTo>table.Count(trainTrack) then
  188. GAMEMODE.trackGoingTo = 1
  189. end
  190.  
  191. movementSectionCurrent = 1
  192.  
  193. if trainTrack[GAMEMODE.trackComingFrom].busstop==true then
  194. GAMEMODE.busStop = true
  195. timer.Simple(8,function() GAMEMODE.busStop = false end)
  196. end
  197.  
  198. end
  199.  
  200. // take a break once we change redirectors if the new one is a busstop
  201.  
  202.  
  203. if GAMEMODE.busStop == false then
  204. // this will be a local vector of the section of movement
  205. local movementPiece = (trainTrack[GAMEMODE.trackGoingTo].pos - trainTrack[GAMEMODE.trackComingFrom].pos) * (1/movementSections)
  206. //print(movementPiece)
  207. trainMain:SetPos( trainMain:GetPos() + movementPiece )
  208.  
  209. local fixturn = Angle(ta.p,ta.y,ta.r)
  210. fixturn = LerpAngle( movementSectionCurrent/movementSections, trainTrack[GAMEMODE.trackComingFrom].ang, trainTrack[GAMEMODE.trackGoingTo].ang )
  211. //print(fixturn)
  212. fixturn:RotateAroundAxis(fixturn:Up(), 90)
  213. //turnPiece:Normalize()
  214.  
  215.  
  216. trainMain:SetAngles( fixturn )
  217.  
  218. end
  219.  
  220.  
  221. //if not trainTrack[trackComingFrom] then trackComingFrom
  222.  
  223.  
  224.  
  225.  
  226. // same as movement but for angle
  227.  
  228. // rotate the current angle around the current up direction by: trackyawdifference /sections
  229. //
  230. //local trackyawdifference = trainTrack[GAMEMODE.trackGoingTo].ang.y - trainTrack[GAMEMODE.trackComingFrom].ang.y
  231.  
  232.  
  233.  
  234.  
  235. //local difference = trainMain:GetPos()-lastPos
  236. //if not difference then difference = Vector(0,0,0) end
  237. //difference = Vector(difference.x,difference.y,0)
  238.  
  239. end
  240.  
  241. hook.Add ( "Think", "startTrainMotion", moveTrain)
  242. //hook.Add("InitPostEntity","startTrainMotion",function () timer.Simple(3,function() moveTrain() end) end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement