public
Created

A WIP advanced gyropod replacement for garrys mod/spacebuild 3

  • Download Gist
spacesguo_gyro_replacement.txt.lua
Lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
@name Spaceship Gyro Replacement
@inputs Active User:entity Forward Backward Up Down StrifeLeft StrifeRight KeepLevel
@outputs TotalMass TotalSpeed ShipID:number
@persist Ship:table Parent:entity UprightAng:angle PropCache:array TotalMass:number
@trigger all
 
function entity getPlayer()
{
if ( User:isVehicle() ) { return User:driver() }
return User
}
 
function array recalcMass( Base:entity )
{
TotalMass = 0
Constraints = Base:getConstraints()
Constraints:pushEntity( Base )
for(I=1,Constraints:count(),1)
{
TotalMass += Constraints[I, entity]:mass()
if ( Constraints[I,entity]:parent() != Parent )
{
if ( Constraints[I,entity]:parent():isValid() ) { Constraints[I,entity]:deparent() }
Constraints[I,entity]:parentTo( Parent )
}
}
return Constraints
}
 
function enableGravity( Constraints:array, Value:number )
{
for(I=1,Constraints:count(),1)
{
local Self = Constraints[I, entity]
Self:propGravity( (Value == 0 ? 0 : 1) )
}
}
 
function doForce( Constraints:array, Force:vector )
{
for(I=1,Constraints:count(),1)
{
local Self = Constraints[I, entity]
Self:applyOffsetForce( Force, MassCenter )
}
}
 
if ( first() )
{
Parent = entity():isWeldedTo()
ParentCache = array()
UprightAngle = Parent:angles()
PropCache = recalcMass( Parent )
Inertia = shiftL(ang(ShipInertia)):setYaw(0)
}
 
if ( first() | ~ShipName ) {
Ship["id", number] = entity():id()
}
 
 
if ( (~Active && Active) | (Active && first()) ) {
PropCache = recalcMass( Parent )
runOnTick(1)
timer("recalcMass", 1000)
soundPlay(1,0,"/ambient/atmosphere/quiet_cellblock_amb.wav")
soundPlay(2,0,"/ambient/alarms/razortrain_horn1.wav")
ShipForce = Parent:vel()
enableGravity( PropCache, 0 )
}
 
if ( clk("recalcMass") )
{
PropCache = recalcMass( Parent )
}
 
 
if ( Active )
{
if ( tickClk() )
{
Driver = getPlayer()
if ( Forward ) { ShipForce += (Parent:forward()*TotalMass) }
if ( Backward ) { ShipForce -= (Parent:forward()*TotalMass) }
if ( Up ) { ShipForce += (Parent:up()*TotalMass) }
if ( Down ) { ShipForce -= (Parent:up()*TotalMass) }
if ( StrifeRight ) { ShipForce += (Parent:right()*TotalMass) }
if ( StrifeLeft ) { ShipForce -= (Parent:right()*TotalMass) }
}
if ( !Parent:isFrozen() & !Parent:isPlayerHolding() )
{
PAngles = Parent:angles()
if ( round(PAngles:pitch()) | round(PAngles:roll()) )
{
Parent:setAng( ang(KeepLevel ? 0 : PAngles:pitch(), PAngles:yaw(), KeepLevel ? 0 : PAngles:roll()) )
#Parent:rerotate( ang(KeepLevel ? 0 : PAngles:pitch(), Parent:angles():yaw(), KeepLevel ? 0 : PAngles:roll()) )
}
}
Parent:applyForce( ShipForce * 100.0 )
}
 
if( ~Active && !Active )
{
runOnTick(0)
enableGravity( PropCache, 1 )
stoptimer("recalcMass")
soundStop(1)
soundPlay(2, 0, "/ambient/energy/whiteflash.wav")
}
 
ShipAngles = Parent:angles()
 
TotalSpeed = Parent:vel():length()
 
ShipID = Ship["id",number]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.