Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local factor = { -- the place to set all the random numbers
- time = { 20, 40 }, -- min and max
- cargowagon = { 1, 8 },
- shipment = { 2, 8 },
- }
- local allowedShipments = {
- "Desert eagle",
- "Fiveseven",
- "Glock",
- "P228",
- "AK47",
- "MP5",
- "M4",
- "Mac 10",
- "Pump shotgun",
- "Sniper rifle",
- }
- 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
- 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) }
- }
- local trains = {} -- all the current running trains
- function copyVector(v)
- return Vector(v.x,v.y,v.z)
- end
- function getAABBSize(ent)
- local min,max = ent:WorldSpaceAABB()
- local offset=max-min
- return offset
- end
- function SpawnProp(pos, ang, model)
- local ent1 = ents.Create("prop_physics")
- local ang = Vector(0,0,1):Angle()
- ang.pitch = ang.pitch + 90
- print("Prop spawned with model: " .. model)
- ent1:SetAngles(ang)
- ent1:SetModel(model)
- --pos.z = pos.z - ent1:OBBMaxs().z
- ent1:SetPos( pos )
- ent1:Spawn()
- ent1:SetVelocity(Vector(0,0,0))
- --ent1:GetPhysicsObject():EnableMotion(false)
- ent1:GetPhysicsObject():EnableGravity(false)
- ent1:SetCollisionGroup(COLLISION_GROUP_WORLD)
- return ent1
- end
- function spawnWagon(loc, pos, ang, model)
- t = {}
- t.train = SpawnProp(pos,ang,model)
- t.worldNocollide = constraint.NoCollide( t.train, game.GetWorld(), 0, 0 )
- t.loc = trainlocs[loc]
- return t
- end
- function spawnTrain(location, model, wagon, str)
- if trainlocs[location] then
- local train = {}
- local lenght = 0
- local loc = trainlocs[location]
- local shipmentsleft = math.random(factor.shipment[1],factor.shipment[2])
- for i = 1,math.random(factor.cargowagon[1],factor.cargowagon[2]) do
- local pos = copyVector(loc[1])
- local vel = copyVector(loc[5])
- print("xyz: " .. pos.x .. "," .. pos.y .. "," .. pos.z)
- if vel.x ~= 0 then pos.x = pos.x + ( vel.x > 0 and -lenght or lenght ) end
- if vel.y ~= 0 then pos.y = pos.y + ( vel.y > 0 and -lenght or lenght ) end
- if vel.z ~= 0 then pos.z = pos.z + ( vel.z > 0 and -lenght or lenght ) end
- c = spawnWagon(location, pos, trainlocs[location][2], (i > 1 and trainlocs[location][4] or trainlocs[location][3]))
- table.insert(train,c)
- print("Made last cart with " .. lenght .. " offset")
- local size = getAABBSize(c.train)
- if vel.x ~= 0 then lenght = lenght + size.x print("cart size " .. size.x) end
- if vel.y ~= 0 then lenght = lenght + size.y print("cart size " .. size.y) end
- if vel.z ~= 0 then lenght = lenght + size.z print("cart size " .. size.z) end
- --[[
- if i > 2 then
- local ships = math.random(1,shipmentsleft)
- local ship = ents.Create("spawned_shipment")
- ship:Spawn()
- local shipsize = getAABBSize(ship)
- local spacebetweenships
- if vel.x ~= 0 then spacebetweenships = ( size.x - ( shipsize.x * ships ) ) / ships end
- if vel.y ~= 0 then spacebetweenships = ( size.y - ( shipsize.y * ships ) ) / ships end
- if vel.z ~= 0 then spacebetweenships = ( size.z - ( shipsize.z * ships ) ) / ships end
- local centerstart = copyVector(pos)
- cemterstart.z = centerstart.z + size.z
- if vel.x ~= 0 then centerstart.y = centerstart.y + ( size.y / 2 ) - ( shipsize.y / 2 ) end
- if vel.y ~= 0 then centerstart.x = centerstart.x + ( size.x / 2 ) - ( shipsize.x / 2 ) end
- ship:Remove()
- for j = 1, ships do
- local ship = ents.Create("spawned_shipment")
- local shippos = copyVector(centerstart)
- if vel.x ~= 0 then shippos.x = shippos.x + ( spacebetweenships * j ) + ( shipsize.x * ( j - 1 ) ) end
- if vel.y ~= 0 then shippos.y = shippos.y + ( spacebetweenships * j ) + ( shipsize.y * ( j - 1 ) ) end
- ship:SetPos(shippos)
- ship:Spawn()
- end
- shipmentsleft = shipmentsleft - ships
- end
- ]]
- end
- table.insert(trains,train)
- print("[Trains] Created Train " .. ( str or "" ))
- else
- print("No Such Location")
- end
- end
- function consoleSpawnTrain(player, command, arguments)
- spawnTrain(arguments[1], trainlocs[arguments[1]][3], arguments[1])
- end
- function correctTrains()
- if #trains then
- for k,v in pairs(trains) do
- for i = 1, #v do
- if v[i].train:IsValid() then
- local loc = v[i].train:GetPos()
- if(v[i].loc[5].x ~= 0) then -- check if velocity is set for this axis
- if v[i].loc[1].x > v[i].loc[6].x then -- check if the start point is bigger than the end point
- 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
- else
- 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
- end
- end
- if(v[i].loc[5].y ~= 0) then -- same for y
- if v[i].loc[1].y > v[i].loc[6].y then
- if loc.y < v[i].loc[6].y then v[i].train:Remove() print("[Trains] Removed Train") end
- else
- if loc.y > v[i].loc[6].y then v[i].train:Remove() print("[Trains] Removed Train") end
- end
- end
- if(v[i].loc[5].z ~= 0) then -- same for z
- if v[i].loc[1].z > v[i].loc[6].z then
- if loc.z < v[i].loc[6].z then v.train:Remove() print("[Trains] Removed Train") end
- else
- if loc.z > v[i].loc[6].z then v.train:Remove() print("[Trains] Removed Train") end
- end
- end
- if(v[i].loc[5].x == 0) then loc.x = v[i].loc[1].x end
- if(v[i].loc[5].y == 0) then loc.y = v[i].loc[1].y end
- if(v[i].loc[5].z == 0) then loc.z = v[i].loc[1].z end
- v[i].train:GetPhysicsObject():SetPos(loc) -- ajusted position to its course
- v[i].train:GetPhysicsObject():SetVelocity(v[i].loc[5]) -- make sure the velocity is still here
- v[i].train:GetPhysicsObject():SetAngles(v[i].loc[2]) -- and of course keep it straight
- else
- table.remove(trains,k)
- end
- end
- end
- end
- end
- concommand.Add("spawntrain",consoleSpawnTrain)
- hook.Add("Tick","TrainCorrection",correctTrains)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement