Advertisement
Guest User

trains addon for darkrp

a guest
May 23rd, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.18 KB | None | 0 0
  1. local factor = { -- the place to set all the random numbers
  2.     time = { 20, 40 }, -- min and max
  3.     cargowagon = { 1, 8 },
  4.     shipment = { 2, 8 },
  5. }
  6.  
  7. local allowedShipments = {
  8.     "Desert eagle",
  9.     "Fiveseven",
  10.     "Glock",
  11.     "P228",
  12.     "AK47",
  13.     "MP5",
  14.     "M4",
  15.     "Mac 10",
  16.     "Pump shotgun",
  17.     "Sniper rifle",
  18. }
  19.  
  20. local trainlocs = { -- a place to define where trains can spawn, how they spawn, what model they use and what velocity they have end when to delet the train
  21.     subway1 = { Vector(-1399,1869,-225), Angle(0,-180,0),"models/props/de_train/diesel.mdl","models/props/de_train/flatcar.mdl",Vector(200,0,0), Vector(2270,1869,-225) }
  22. }
  23.  
  24. local trains = {} -- all the current running trains
  25.  
  26. function copyVector(v)
  27.     return Vector(v.x,v.y,v.z)
  28. end
  29.  
  30. function getAABBSize(ent)
  31.     local min,max = ent:WorldSpaceAABB()
  32.     local offset=max-min
  33.     return offset
  34. end
  35.  
  36. function SpawnProp(pos, ang, model)
  37.     local ent1 = ents.Create("prop_physics")
  38.     local ang = Vector(0,0,1):Angle()
  39.     ang.pitch = ang.pitch + 90
  40.     print("Prop spawned with model: " .. model)
  41.     ent1:SetAngles(ang)
  42.     ent1:SetModel(model)
  43.     --pos.z = pos.z - ent1:OBBMaxs().z
  44.     ent1:SetPos( pos )
  45.     ent1:Spawn()
  46.     ent1:SetVelocity(Vector(0,0,0))
  47.     --ent1:GetPhysicsObject():EnableMotion(false)
  48.     ent1:GetPhysicsObject():EnableGravity(false)
  49.     ent1:SetCollisionGroup(COLLISION_GROUP_WORLD)
  50.     return ent1
  51. end
  52.  
  53. function spawnWagon(loc, pos, ang, model)
  54.     t = {}
  55.     t.train = SpawnProp(pos,ang,model)
  56.     t.worldNocollide = constraint.NoCollide( t.train, game.GetWorld(), 0, 0 )
  57.     t.loc = trainlocs[loc]
  58.     return t
  59. end
  60.  
  61. function spawnTrain(location, model, wagon, str)
  62.     if trainlocs[location] then
  63.         local train = {}
  64.         local lenght = 0
  65.         local loc = trainlocs[location]
  66.         local shipmentsleft = math.random(factor.shipment[1],factor.shipment[2])
  67.        
  68.         for i = 1,math.random(factor.cargowagon[1],factor.cargowagon[2]) do
  69.            
  70.             local pos = copyVector(loc[1])
  71.             local vel = copyVector(loc[5])
  72.            
  73.             print("xyz: " .. pos.x .. "," .. pos.y .. "," .. pos.z)
  74.            
  75.             if vel.x ~= 0 then pos.x = pos.x + ( vel.x > 0 and -lenght or lenght ) end
  76.             if vel.y ~= 0 then pos.y = pos.y + ( vel.y > 0 and -lenght or lenght ) end
  77.             if vel.z ~= 0 then pos.z = pos.z + ( vel.z > 0 and -lenght or lenght ) end
  78.            
  79.             c = spawnWagon(location, pos, trainlocs[location][2], (i > 1 and trainlocs[location][4] or trainlocs[location][3]))
  80.             table.insert(train,c)
  81.            
  82.             print("Made last cart with " .. lenght .. " offset")
  83.            
  84.             local size = getAABBSize(c.train)
  85.            
  86.             if vel.x ~= 0 then lenght = lenght + size.x print("cart size " .. size.x) end
  87.             if vel.y ~= 0 then lenght = lenght + size.y print("cart size " .. size.y) end
  88.             if vel.z ~= 0 then lenght = lenght + size.z print("cart size " .. size.z) end
  89.            
  90.             --[[
  91.             if i > 2 then
  92.                 local ships = math.random(1,shipmentsleft)
  93.                
  94.                 local ship = ents.Create("spawned_shipment")
  95.                 ship:Spawn()
  96.                 local shipsize = getAABBSize(ship)
  97.                
  98.                 local spacebetweenships
  99.                 if vel.x ~= 0 then spacebetweenships = ( size.x - ( shipsize.x * ships ) ) / ships end
  100.                 if vel.y ~= 0 then spacebetweenships = ( size.y - ( shipsize.y * ships ) ) / ships end
  101.                 if vel.z ~= 0 then spacebetweenships = ( size.z - ( shipsize.z * ships ) ) / ships end
  102.                
  103.                 local centerstart = copyVector(pos)
  104.                
  105.                 cemterstart.z = centerstart.z + size.z
  106.                
  107.                 if vel.x ~= 0 then centerstart.y = centerstart.y + ( size.y / 2 ) - ( shipsize.y / 2 ) end
  108.                 if vel.y ~= 0 then centerstart.x = centerstart.x + ( size.x / 2 ) - ( shipsize.x / 2 ) end
  109.                
  110.                 ship:Remove()
  111.                
  112.                 for j = 1, ships do
  113.                     local ship = ents.Create("spawned_shipment")
  114.                     local shippos = copyVector(centerstart)
  115.                    
  116.                     if vel.x ~= 0 then shippos.x = shippos.x + ( spacebetweenships * j ) + ( shipsize.x * ( j - 1 ) ) end
  117.                     if vel.y ~= 0 then shippos.y = shippos.y + ( spacebetweenships * j ) + ( shipsize.y * ( j - 1 ) ) end
  118.                    
  119.                     ship:SetPos(shippos)
  120.                     ship:Spawn()
  121.                    
  122.                 end
  123.                 shipmentsleft = shipmentsleft - ships
  124.             end
  125.             ]]
  126.         end
  127.        
  128.         table.insert(trains,train)
  129.         print("[Trains] Created Train " .. ( str or "" ))
  130.     else
  131.         print("No Such Location")
  132.     end
  133. end
  134.  
  135. function consoleSpawnTrain(player, command, arguments)
  136.     spawnTrain(arguments[1], trainlocs[arguments[1]][3], arguments[1]) 
  137. end
  138.  
  139. function correctTrains()
  140.     if #trains then
  141.         for k,v in pairs(trains) do
  142.             for i = 1, #v do
  143.                 if v[i].train:IsValid() then
  144.                     local loc = v[i].train:GetPos()
  145.                    
  146.                     if(v[i].loc[5].x ~= 0) then -- check if velocity is set for this axis
  147.                         if v[i].loc[1].x > v[i].loc[6].x then  -- check if the start point is bigger than the end point
  148.                             if loc.x < v[i].loc[6].x then v[i].train:Remove() print("[Trains] Removed Train") end -- check if the train has passed the end point and remove it
  149.                         else
  150.                             if loc.x > v[i].loc[6].x then v[i].train:Remove() print("[Trains] Removed Train") end -- same here but for the other direction on the same axis
  151.                         end
  152.                     end
  153.                     if(v[i].loc[5].y ~= 0) then -- same for y
  154.                         if v[i].loc[1].y > v[i].loc[6].y then
  155.                             if loc.y < v[i].loc[6].y then v[i].train:Remove() print("[Trains] Removed Train") end
  156.                         else
  157.                             if loc.y > v[i].loc[6].y then v[i].train:Remove() print("[Trains] Removed Train") end
  158.                         end
  159.                     end
  160.                     if(v[i].loc[5].z ~= 0) then -- same for z
  161.                         if v[i].loc[1].z > v[i].loc[6].z then
  162.                             if loc.z < v[i].loc[6].z then v.train:Remove() print("[Trains] Removed Train") end
  163.                         else
  164.                             if loc.z > v[i].loc[6].z then v.train:Remove() print("[Trains] Removed Train") end
  165.                         end
  166.                     end
  167.                    
  168.                     if(v[i].loc[5].x == 0) then loc.x = v[i].loc[1].x end
  169.                     if(v[i].loc[5].y == 0) then loc.y = v[i].loc[1].y end
  170.                     if(v[i].loc[5].z == 0) then loc.z = v[i].loc[1].z end
  171.                     v[i].train:GetPhysicsObject():SetPos(loc) -- ajusted position to its course
  172.                     v[i].train:GetPhysicsObject():SetVelocity(v[i].loc[5]) -- make sure the velocity is still here
  173.                     v[i].train:GetPhysicsObject():SetAngles(v[i].loc[2]) -- and of course keep it straight
  174.                 else
  175.                     table.remove(trains,k)
  176.                 end
  177.             end
  178.         end
  179.     end
  180. end
  181.  
  182. concommand.Add("spawntrain",consoleSpawnTrain)
  183. hook.Add("Tick","TrainCorrection",correctTrains)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement