1
0
mirror of https://github.com/metrostroi-repo/MetrostroiAddon.git synced 2026-05-02 00:42:29 +00:00

release branch init

This commit is contained in:
g_brzhezinskiy
2021-01-02 15:32:05 +03:00
parent 1d05caf866
commit 09566ce158
398 changed files with 8389 additions and 77275 deletions

View File

@@ -14,6 +14,22 @@ COUPLE_MAX_DISTANCE = COUPLE_MAX_DISTANCE ^ 2
COUPLE_MAX_ANGLE = math.cos(math.rad(COUPLE_MAX_ANGLE))
--------------------------------------------------------------------------------
--[[ function ENT:PreEntityCopy()
local BogeyDupe = {}
if IsValid(self.Wheels) then
BogeyDupe.Wheels = self.Wheels:EntIndex()
end
BogeyDupe.BogeyType = self.BogeyType
if WireAddon then
BogeyDupe.WireData = WireLib.BuildDupeInfo( self.Entity )
end
BogeyDupe.NoPhysics = self.NoPhysics
duplicator.StoreEntityModifier(self, "BogeyDupe", BogeyDupe)
end
duplicator.RegisterEntityModifier( "BogeyDupe" , function() end)
--Model,WheelPos,WheelAng,WheelModel,PantLPos,PantRPos,BogeyOffset,{ConnectorPositions}--]]
ENT.Types = {
["702"] = {
"models/metrostroi_train/bogey/metro_bogey_702.mdl",
@@ -59,6 +75,39 @@ ENT.Types = {
Vector(4.3,-63,-3.3),Vector(4.3,63,-3.3),
},
}
--[[ function ENT:PostEntityPaste(ply,ent,createdEntities)
local BogeyDupe = ent.EntityMods.BogeyDupe
if IsValid(self.Wheels) then
self.Wheels:SetParent()
self.Wheels:Remove()
end
self.Wheels = createdEntities[BogeyDupe.Wheels]
self.BogeyType = BogeyDupe.BogeyType
local typ = self.Types[self.BogeyType or "717"]
self:SetModel(typ and typ[1] or "models/metrostroi/metro/metro_bogey.mdl")
if IsValid(self.Wheels) then
if self.BogeyType == "tatra" then
self.Wheels:SetPos(self:LocalToWorld(Vector(0,0.0,-3)))
self.Wheels:SetAngles(self:GetAngles() + Angle(0,0,0))
else
self.Wheels:SetPos(self:LocalToWorld(Vector(0,0.0,-10)))
self.Wheels:SetAngles(self:GetAngles() + Angle(0,90,0))
end
self.Wheels.WheelType = self.BogeyType
self.Wheels.NoPhysics = BogeyDupe.NoPhysics
if self.NoPhysics then
self.Wheels:SetParent(self)
else
self.Wheels:PhysicsInit(SOLID_VPHYSICS)
self.Wheels:SetMoveType(MOVETYPE_VPHYSICS)
self.Wheels:SetSolid(SOLID_VPHYSICS)
constraint.Weld(self,self.Wheels,0,0,0,1,0)
end
if CPPI then self.Wheels:CPPISetOwner(self:CPPIGetOwner()) end
self.Wheels:SetNW2Entity("TrainBogey",self)
end
end--]]
ENT.SnakePos = Vector(-168.25,0,6.5)
ENT.SnakeAng = Angle(0,90,0)
@@ -109,6 +158,8 @@ function ENT:Initialize()
self.PneumaticBrakeForce = 100000.0
self.DisableSound = 0
self.Angle = 0
self.Variables = {}
-- Pressure in brake cylinder
@@ -119,6 +170,7 @@ function ENT:Initialize()
self.DropByPeople = 0
self.PlayTime = { 0, 0 }
self.ContactStates = { false, false }
self.ContactDisables = {false,false}
self.DisableContacts = false
self.DisableContactsManual = false
self.DisableParking = false
@@ -133,25 +185,30 @@ end
function ENT:InitializeWheels()
-- Create missing wheels
if IsValid(self.Wheels) then SafeRemoveEntity(self.Wheels) end
local wheels = ents.Create("gmod_train_wheels")
local typ = self.Types[self.BogeyType or "717"]
wheels.Model = typ[4]
if typ and typ[3] then wheels:SetAngles(self:LocalToWorldAngles(typ[3])) end
if typ and typ[2] then wheels:SetPos(self:LocalToWorld(typ[2])) end
if not IsValid(self.Wheels) then
--print(1)
local wheels = ents.Create("gmod_train_wheels")
local typ = self.Types[self.BogeyType or "717"]
wheels.Model = typ[4]
if typ and typ[3] then wheels:SetAngles(self:LocalToWorldAngles(typ[3])) end
if typ and typ[2] then wheels:SetPos(self:LocalToWorld(typ[2])) end
wheels.WheelType = self.BogeyType
wheels.NoPhysics = self.NoPhysics
wheels:Spawn()
--wheels = ents.Create("gmod_subway_wheels")
--wheels:SetPos(self:LocalToWorld(Vector(0,0.0,-10)))
--wheels:SetAngles(self:GetAngles() + Angle(0,90,0))
wheels.WheelType = self.BogeyType
wheels.NoPhysics = self.NoPhysics
wheels:Spawn()
if self.NoPhysics then
wheels:SetParent(self)
else
constraint.Weld(self,wheels,0,0,0,1,0)
if self.NoPhysics then
wheels:SetParent(self)
else
constraint.Weld(self,wheels,0,0,0,1,0)
end
if CPPI then wheels:CPPISetOwner(self:CPPIGetOwner() or self:GetNW2Entity("TrainEntity"):GetOwner()) end
wheels:SetNW2Entity("TrainBogey",self)
self.Wheels = wheels
end
if CPPI then wheels:CPPISetOwner(self:CPPIGetOwner() or self:GetNW2Entity("TrainEntity"):GetOwner()) end
wheels:SetNW2Entity("TrainBogey",self)
self.Wheels = wheels
end
function ENT:OnRemove()
@@ -179,7 +236,7 @@ function ENT:TriggerInput(iname, value)
end
end
--[[ Checks if there's an advballsocket between two entities
-- Checks if there's an advballsocket between two entities
local function AreCoupled(ent1,ent2)
if ent1.CoupledBogey or ent2.CoupledBogey then return false end
local constrainttable = constraint.FindConstraints(ent1,"AdvBallsocket")
@@ -193,7 +250,7 @@ local function AreCoupled(ent1,ent2)
end
return coupled
end]]
end
-- Adv ballsockets ents by their CouplingPointOffset
function ENT:Couple(ent)
@@ -225,10 +282,11 @@ function ENT:Couple(ent)
end
end
local function AreInCoupleDistance(ent1,ent2)
return ent2:LocalToWorld(ent2.CouplingPointOffset):DistToSqr(ent1:LocalToWorld(ent1.CouplingPointOffset)) < COUPLE_MAX_DISTANCE
local function AreInCoupleDistance(ent,self)
return self:LocalToWorld(self.CouplingPointOffset):DistToSqr(ent:LocalToWorld(ent.CouplingPointOffset)) < COUPLE_MAX_DISTANCE
end
local function AreFacingEachother(ent1,ent2)
return ent1:GetForward():Dot(ent2:GetForward()) < -COUPLE_MAX_ANGLE
end
@@ -248,8 +306,8 @@ end
local function CanCoupleTogether(ent1,ent2)
if ent1.DontHaveCoupler or ent2.DontHaveCoupler then return false end
if ent2:GetClass() ~= ent1:GetClass() then return false end
--if not (ent1.CanCouple and ent1:CanCouple()) then return false end
--if not (ent2.CanCouple and ent2:CanCouple()) then return false end
if not (ent1.CanCouple and ent1:CanCouple()) then return false end
if not (ent2.CanCouple and ent2:CanCouple()) then return false end
if not AreInCoupleDistance(ent1,ent2) then return false end
if not AreFacingEachother(ent1,ent2) then return false end
return true
@@ -381,9 +439,8 @@ function ENT:CheckContact(pos,dir,id,cpos)
if not result.Hit then return end
if result.HitWorld then return true end
local traceEnt = result.Entity
if not self.Connectors[id] and traceEnt:GetClass() == "gmod_track_udochka" then
if result.Entity:GetClass() == "gmod_track_udochka" then
if not traceEnt.Timer and traceEnt.CoupledWith ~= self then
--local vec = Vector(pos.y < 0 and 1 or 1.1,pos.y < 0 and -1 or 1.05, 1)
traceEnt:SetPos(self:LocalToWorld(cpos))
@@ -394,110 +451,105 @@ function ENT:CheckContact(pos,dir,id,cpos)
traceEnt.Coupled = self
sound.Play("udochka_connect.wav",traceEnt:GetPos())
self.Connectors[id] = traceEnt
DropEntityIfHeld(traceEnt)
--[[timer.Simple(0,function()
if not IsValid(traceEnt) or not traceEnt:IsPlayerHolding() then return end
traceEnt:ForcePlayerDrop()
if traceEnt.LastPickup and traceEnt.LastPickup:IsPlayer() then
traceEnt.LastPickup:DropObject()
timer.Simple(0,function()
if IsValid(traceEnt) and traceEnt:IsPlayerHolding() then
traceEnt:ForcePlayerDrop()
if traceEnt.LastPickup and traceEnt.LastPickup:IsPlayer() then
traceEnt.LastPickup:DropObject()
end
end
end)]]
end)
end
end
return false
elseif traceEnt:GetClass() == "player" and self.Voltage > 40 then
local pPos = traceEnt:GetPos()
elseif traceEnt:GetClass() == "player" then
--self.T:WorldToLocal(Entity(3208):GetPos())
if self.Voltage < 40 then return end
local pos = traceEnt:GetPos()
self.VoltageDropByTouch = (self.VoltageDropByTouch or 0) + 1
util.BlastDamage(traceEnt,traceEnt,pPos,64,3.0*self.Voltage)
util.BlastDamage(traceEnt,traceEnt,pos,64,3.0*self.Voltage)
local effectdata = EffectData()
effectdata:SetOrigin(pPos + Vector(0,0,-16+math.random()*(40+0)))
effectdata:SetOrigin(pos + Vector(0,0,-16+math.random()*(40+0)))
util.Effect("cball_explode",effectdata,true,true)
sound.Play("ambient/energy/zap"..math.random(1,3)..".wav",pPos,75,math.random(100,150),1.0)
sound.Play("ambient/energy/zap"..math.random(1,3)..".wav",pos,75,math.random(100,150),1.0)
return
end
return result.Hit
end
local C_Reqiure3rdRail = GetConVar("metrostroi_train_requirethirdrail")
function ENT:CheckVoltage(dT)
-- Check contact states
if (CurTime() - self.CheckTimeout) <= 0.25 then return end
self.CheckTimeout = CurTime()
local supported = C_Reqiure3rdRail:GetInt() > 0 and Metrostroi.MapHasFullSupport()
local feeder = self.Feeder and Metrostroi.Voltages[self.Feeder]
local volt = feeder or Metrostroi.Voltage or 750
if (CurTime() - self.CheckTimeout) > 0.25 then
self.CheckTimeout = CurTime()
self.VoltageDropByTouch = 0
self.NextStates[1] = not self.DisableContacts and not self.DisableContactsManual and not self.ContactDisables[1] and self:CheckContact(self.PantLPos,Vector(0,-1,0),1,self.PantLCPos)
self.NextStates[2] = not self.DisableContacts and not self.DisableContactsManual and not self.ContactDisables[2] and self:CheckContact(self.PantRPos,Vector(0, 1,0),2,self.PantRCPos)
-- Detect changes in contact states
for i=1,2 do
local state = self.NextStates[i]
if state ~= self.ContactStates[i] then
self.ContactStates[i] = state
-- Non-metrostroi maps
if not supported then
self.Voltage = volt
self.NextStates[1] = true
self.NextStates[2] = true
return
end
if state then
self.VoltageDrop = -40*(0.5 + 0.5*math.random())
self.VoltageDropByTouch = 0
self.NextStates[1] = not self.DisableContacts and not self.DisableContactsManual
and self:CheckContact(self.PantLPos,Vector(0,-1,0),1,self.PantLCPos)
self.NextStates[2] = not self.DisableContacts and not self.DisableContactsManual
and self:CheckContact(self.PantRPos,Vector(0, 1,0),2,self.PantRCPos)
local dt = CurTime() - self.PlayTime[i]
self.PlayTime[i] = CurTime()
-- Detect changes in contact states
for i=1,2 do
local state = self.NextStates[i]
if state ~= self.ContactStates[i] then
self.ContactStates[i] = state
if not state then continue end
local volume = 0.53
if dt < 1.0 then volume = 0.43 end
if i == 1 then sound.Play("subway_trains/bogey/tr_"..math.random(1,5)..".wav",self:LocalToWorld(self.PantLPos),65,math.random(90,120),volume) end
if i == 2 then sound.Play("subway_trains/bogey/tr_"..math.random(1,5)..".wav",self:LocalToWorld(self.PantRPos),65,math.random(90,120),volume) end
self.VoltageDrop = -40*(0.5 + 0.5*math.random())
-- Sparking probability
local probability = math.Clamp(1-(self.MotorPower/2),0,1)
if state and (math.random() > probability) then
local effectdata = EffectData()
if i == 1 then effectdata:SetOrigin(self:LocalToWorld(self.PantLPos)) end
if i == 2 then effectdata:SetOrigin(self:LocalToWorld(self.PantRPos)) end
effectdata:SetNormal(Vector(0,0,-1))
util.Effect("stunstickimpact", effectdata, true, true)
local dt = CurTime() - self.PlayTime[i]
self.PlayTime[i] = CurTime()
local volume = 0.53
if dt < 1.0 then volume = 0.43 end
if i == 1 then sound.Play("subway_trains/bogey/tr_"..math.random(1,5)..".wav",self:LocalToWorld(self.PantLPos),65,math.random(90,120),volume) end
if i == 2 then sound.Play("subway_trains/bogey/tr_"..math.random(1,5)..".wav",self:LocalToWorld(self.PantRPos),65,math.random(90,120),volume) end
-- Sparking probability
local probability = math.Clamp(1-(self.MotorPower/2),0,1)
if math.random() > probability then
local effectdata = EffectData()
if i == 1 then effectdata:SetOrigin(self:LocalToWorld(self.PantLPos)) end
if i == 2 then effectdata:SetOrigin(self:LocalToWorld(self.PantRPos)) end
effectdata:SetNormal(Vector(0,0,-1))
util.Effect("stunstickimpact", effectdata, true, true)
local light = ents.Create("light_dynamic")
light:SetPos(effectdata:GetOrigin())
light:SetKeyValue("_light","100 220 255")
light:SetKeyValue("style", 0)
light:SetKeyValue("distance", 256)
light:SetKeyValue("brightness", 5)
light:Spawn()
light:Fire("TurnOn","","0")
light.Time = CurTime()
timer.Simple(0.1,function()
SafeRemoveEntity(light)
end)
sound.Play("subway_trains/bogey/spark.mp3",effectdata:GetOrigin(),75,math.random(100,150),volume)
--self.Train:PlayOnce("zap",sound_source,0.7*volume,50+math.random(90,120))
local light = ents.Create("light_dynamic")
light:SetPos(effectdata:GetOrigin())
light:SetKeyValue("_light","100 220 255")
light:SetKeyValue("style", 0)
light:SetKeyValue("distance", 256)
light:SetKeyValue("brightness", 5)
light:Spawn()
light:Fire("TurnOn","","0")
light.Time = CurTime()
hook.Add("Think",light,function(self)
if CurTime()-self.Time > 0.1 then self:Remove() end
end)
sound.Play("subway_trains/bogey/spark.mp3",effectdata:GetOrigin(),75,math.random(100,150),volume)
--self.Train:PlayOnce("zap",sound_source,0.7*volume,50+math.random(90,120))
end
end
end
end
end
-- Voltage spikes
self.VoltageDrop = math.max(-30,math.min(30,self.VoltageDrop + (0 - self.VoltageDrop)*10*dT))
local feeder = self.Feeder and Metrostroi.Voltages[self.Feeder]
local volt = feeder or Metrostroi.Voltage or 750
-- Non-metrostroi maps
if ((GetConVarNumber("metrostroi_train_requirethirdrail") <= 0)) then-- or
--(not Metrostroi.MapHasFullSupport()) then
self.Voltage = volt + self.VoltageDrop
return
end
-- Detect voltage
self.Voltage = 0
self.DropByPeople = 0
for i=1,2 do
if self.ContactStates[i] then
self.Voltage = volt + self.VoltageDrop
elseif IsValid(self.Connectors[i]) and self.Connectors[i].Coupled == self then
if self.ContactStates[i] then self.Voltage = volt + self.VoltageDrop
elseif IsValid(self.Connectors[i]) and self.Connectors[i].Coupled == (i > 2 and self.Train.RearBogey or self) then
self.Voltage = self.Connectors[i].Power and Metrostroi.Voltage or 0
end
else self.Connectors[i] = nil end
end
if self.VoltageDropByTouch > 0 then
local Rperson = 0.613
@@ -508,12 +560,14 @@ end
function ENT:Think()
-- Re-initialize wheels
if not IsValid(self.Wheels) or self.Wheels:GetNW2Entity("TrainBogey") ~= self then
if (not self.Wheels) or
(not self.Wheels:IsValid()) or
(self.Wheels:GetNW2Entity("TrainBogey") ~= self) then
self:InitializeWheels()
constraint.NoCollide(self.Wheels,self,0,0)
if IsValid(self:GetNW2Entity("TrainEntity")) then
constraint.NoCollide(self.Wheels,self:GetNW2Entity("TrainEntity"),0,0)
constraint.NoCollide(self.Wheels,self,0,0)
end
end
@@ -521,12 +575,13 @@ function ENT:Think()
self.PrevTime = self.PrevTime or CurTime()
self.DeltaTime = (CurTime() - self.PrevTime)
self.PrevTime = CurTime()
self.Angle = self.Wheels.Angle
self:SetNW2Entity("TrainWheels",self.Wheels)
self:CheckVoltage(self.DeltaTime)
-- Skip physics related stuff
if self.NoPhysics or not self.Wheels:GetPhysicsObject():IsValid() then
if not IsValid(self.Wheels) or not self.Wheels:GetPhysicsObject():IsValid() or self.NoPhysics then
self:SetMotorPower(self.MotorPower or 0)
self:SetSpeed(self.Speed or 0)
self:NextThink(CurTime())
@@ -554,19 +609,24 @@ function ENT:Think()
-- Calculate motor power
local motorPower = 0.0
if self.MotorPower > 0.0 then
motorPower = math.Clamp(self.MotorPower, -1, 1)
motorPower = self.MotorPower
else
motorPower = math.Clamp(self.MotorPower*sign, -1, 1)
motorPower = self.MotorPower*sign
end
motorPower = math.max(-1.0,motorPower)
motorPower = math.min(1.0,motorPower)
-- Increace forces on slopes
local slopemul = 1
local slopemul = 1--+math.Clamp(Train:GetAngles():Forward(),0,1)*0.2
local slopemulb = 1
local pitch = self:GetAngles().pitch*sign
if motorPower < 0 and pitch > 3 then
slopemul = slopemul + math.Clamp((math.abs(pitch)-3)/3,0,1)
slopemul = slopemul + math.Clamp((math.abs(pitch)-3)/3,0,1)--[[ *(math.Clamp((self.Speed-55)/5,0,1))--]] *1.5
else
slopemul = slopemul + math.Clamp((pitch-3)/3,0,1)*1.5
end
if -3 > pitch or pitch > 3 then
slopemulb = slopemulb + math.Clamp((math.abs(pitch)-3)/3,0,1)*0.7
end
-- Final brake cylinder pressure
local pneumaticPow = self.PneumaticPow or 1
local pB = not self.DisableParking and self.ParkingBrakePressure or 0
@@ -579,14 +639,10 @@ function ENT:Think()
-- Calculate forces
local motorForce = self.MotorForce*motorPower*slopemul
local pneumaticFactor = math.Clamp(0.5*self.Speed,0,1)*(1+math.Clamp((2-self.Speed)/2,0,1)*0.5)
local pneumaticFactor = math.max(0,math.min(1,0.5*self.Speed))*(1+(math.max(0,math.min(1,(2-self.Speed)/2)))*0.5)
local pneumaticForce = 0
if BrakeCP >= 0.05 then
local slopemulBr = 1
if -3 > pitch or pitch > 3 then
slopemulBr = 1 + math.Clamp((math.abs(pitch)-3)/3,0,1)*0.7
end
pneumaticForce = -sign*pneumaticFactor*self.PneumaticBrakeForce*BrakeCP*slopemulBr
pneumaticForce = -sign*pneumaticFactor*self.PneumaticBrakeForce*BrakeCP*slopemulb
end
-- Compensate forward friction
@@ -618,12 +674,13 @@ function ENT:Think()
-- Calculate brake squeal
self.SquealSensitivity = 1
local k = ((self.SquealSensitivity or 0.5) - 0.5)*2
local BCPress = math.abs(self.BrakeCylinderPressure)
self.RattleRandom = self.RattleRandom or 0.5+math.random()*0.2
local PnF1 = math.Clamp((BCPress-0.6)/0.6,0,2)
local PnF2 = math.Clamp((BCPress-self.RattleRandom)/0.6,0,2)
--local PnF3 = math.Clamp((BCPress-self.RattleRandom+0.15)/0.6,0,1)
local brakeSqueal1 = (PnF1*PnF2)*pneumaticFactor
--local brakeSqueal2 = (PnF1*PnF3)*pneumaticFactor
-- Send parameters to client
if self.DisableSound < 1 then
@@ -631,11 +688,20 @@ function ENT:Think()
end
if self.DisableSound < 2 then
local brakeRamp = math.min(1.0,math.max(0.0,self.Speed/2.0))
if self.Speed > 2 then
--brakeRamp = 1 - math.min(1.0,math.max(0.0,(self.Speed-3)/10.0))
end
-- if brakeRamp > 0.01 and brakeSqueal > 0 then
if self:GetNWBool("Async") then
self:SetNW2Float("BrakeSqueal",(self.BrakeCylinderPressure-0.9)/1.7)
local bcP = self.BrakeCylinderPressure
self:SetNW2Float("BrakeSqueal",(bcP-0.9)/1.7)--3/(absSpeed+bcP^3)*(bcP^3)*0.5)
else
self:SetNW2Float("BrakeSqueal1",brakeSqueal1)
--self:SetNW2Float("BrakeSqueal2",brakeSqueal2)
end
--self:SetBrakeSqueal(self.BrakeSqueal or brakeSqueal)
-- end
end
if self.DisableSound < 3 then
self:SetSpeed(absSpeed)