@name RC TinyTank
@inputs Pod:wirelink Cam:wirelink 
@persist Self:entity Offset:vector Speed Ranger:ranger Height Distance Pos:vector Angle:angle Turning TurnResistance SpeedResistance HoloPos6:vector
@persist GL:array 
@outputs GT:array Start 
@model models/hunter/blocks/cube025x05x025.mdl
 
#    To Do List
# 1. Better Physics
# 2. Switchable Flying/Drone Like Mode(Optional)
 
#Sounds
StartSound = "vehicles/airboat/fan_blade_idle_loop1.wav"
StopSound = "vehicles/junker/jnk_stop1.wav"
Horn = "pm_addon/car_horn1.mp3"

local Seat = Pod["Entity",entity]
local Driver = Seat:driver()
local W = Driver:keyForward()
local A = Driver:keyLeft()
local S = Driver:keyBack()
local D = Driver:keyRight()
local R = Driver:keyReload()
local Mouse1 = Driver:keyAttack1()
local Mouse2 = Driver:keyAttack2()
local Shift = Driver:keySprint()
local Active = Driver:isPlayer()
local On = Active
Cam["Activated", number] = Active

if(changed(On) & On){soundPlay(1,0,StartSound)}
 
if(changed(On) & !On){soundPlay(2,0,StopSound) soundStop(1)}

if(changed(R) & R){soundPlay(3,0,Horn)}

if (first())
{
    #Handling
    Speed = 13 #Higher is faster
    SpeedResistance = 20 #Higher is less resistant
    Turning = 25
    TurnResistance = 5
   
   
   
    Height = 25
    Offset = vec(-6, 0, 3)
    Self = entity()
    Cam["Parent", entity] = Self
    Self:propFreeze(0)
    Cam["Position", vector] = Offset
    Self:setColor(vec(), 0)
 
    # Holo 1 = Tank Body
    holoCreate(1)
    holoModel(1,"models/props_vehicles/apc001.mdl")
    holoScale(1,vec(0.1,0.1,0.1))
    holoPos(1, Self:toWorld(Offset))
    holoParent(1, Self)
    holoClipEnabled(1,1)
    holoClip(1,vec(0,0,5),vec(0,0,-1),0)
    # Holo 2 = Right Front Wheel
    holoCreate(2)
    holoModel(2,"models/NatesWheel/nateswheelwide.mdl")
    holoScale(2,vec(0.19,0.19,0.19))
    holoPos(2, Self:toWorld(vec(0.08,7,0)))
    holoParent(2, Self)
    # Holo 3 = Right Back Wheel
    holoCreate(3)
    holoModel(3,"models/NatesWheel/nateswheelwide.mdl")
    holoScale(3,vec(0.19,0.19,0.19))
    holoPos(3, Self:toWorld(vec(0.08,-7,0)))
    holoParent(3, Self)
    # Holo 4 = Left Front Wheel
    holoCreate(4)
    holoModel(4,"models/NatesWheel/nateswheelwide.mdl")
    holoScale(4,vec(0.19,0.19,0.19))
    holoPos(4, Self:toWorld(vec(-12,7,0)))
    holoParent(4, Self)
    # Holo 5 = Left Back Wheel
    holoCreate(5)
    holoModel(5,"models/NatesWheel/nateswheelwide.mdl")
    holoScale(5,vec(0.19,0.19,0.19))
    holoPos(5, Self:toWorld(vec(-12,-7,0)))
    holoParent(5, Self)
    # Antena
    holoCreate(12)
    holoModel(12,"models/props_c17/signpole001.mdl")
    holoMaterial(12,"models/debug/debugwhite")
    holoColor(12,vec(0,0,0))
    holoScale(12,vec(0.1,0.1,0.08))
    holoPos(12, Self:toWorld(vec(-2,-13,7)))
    holoParent(12, entity())

    #Turret
    holoCreate(6)
    holoModel(6,"models/props_vehicles/apc001.mdl")
    holoParent(6,holoEntity(1))
    HoloPos6 = vec(-6,2,3)
    holoPos(6, entity():toWorld(HoloPos6))
    holoScale(6,vec(0.1,0.1,0.1))
    ScaleValue = holoScale(6)

    HoloEnt = holoEntity(6)
    VecClip1Start = HoloEnt:angles():up()*47.5*ScaleValue
    VecClip1Ang = HoloEnt:angles()+ang(270,0,0)
    VecClip2Start = HoloEnt:angles():up()*20*ScaleValue+HoloEnt:angles():forward()*20*ScaleValue
    VecClip2Ang = HoloEnt:angles()+ang(295,270,0)
    VecClip3Start = vec(0,0,0)
    VecClip3Ang = HoloEnt:angles()+ang(330,90,0)
    holoClipEnabled(6,1,1)
    holoClipEnabled(6,2,1)
    holoClipEnabled(6,3,1)

    holoClip(6,1,VecClip1Start,VecClip1Ang:forward(),0)
    holoClip(6,2,VecClip2Start,VecClip2Ang:forward(),0)
    holoClip(6,3,VecClip3Start,VecClip3Ang:forward(),0)
    Turret = holoEntity(6)
   
    holoCreate(7)
    holoModel(7,"models/props_c17/signpole001.mdl")
    holoParent(7,Turret)
    holoScale(7,vec(0.1,0.1,0.045))
    holoAng(7,ang(0,90,0))
    holoPos(7,entity():toWorld(vec(-5.5,2,10)))
    holoEntity(7):setColor(vec(), 0)
    
    runOnTick(1)
}

ForwardVector = -holoEntity(1):angles():right()
ScaleValue = holoScale(1)
HoloPos6World = holoEntity(1):pos() + holoEntity(1):angles():right() * HoloPos6:x() + holoEntity(1):angles():forward() * HoloPos6:y() + holoEntity(1):angles():up() * HoloPos6:z()
    + holoEntity(1):angles():right() * 4 - holoEntity(1):angles():up() * 3 - holoEntity(1):angles():forward() * 2
if(On) {
    WorldAng = holoEntity(1):toWorld(ang(0,-90+Driver:eyeAngles():yaw(),0))
    LocalAng = holoEntity(1):toLocal(WorldAng)
    holoAng(6, WorldAng)
    CosValue = cos(LocalAng:yaw())
    SinValue = sin(LocalAng:yaw())
    RightVector = holoEntity(1):angles():forward()
    holoPos(6, HoloPos6World - ForwardVector * CosValue * ScaleValue * 21 + RightVector * SinValue * ScaleValue * 20)
} else {
    holoAng(6, holoEntity(1):angles():forward():toAngle())
    holoPos(6, HoloPos6World - ForwardVector * ScaleValue * 20)
}
 
soundPitch(1, 100 + entity():vel():length()/4 + 5)
 
if (W)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,0,holoEntity(2):angles():roll()-7)))
    holoAng(3,holoEntity(1):toWorld(ang(0,0,holoEntity(3):angles():roll()-7)))
    holoAng(4,holoEntity(1):toWorld(ang(0,0,holoEntity(4):angles():roll()-7)))
    holoAng(5,holoEntity(1):toWorld(ang(0,0,holoEntity(5):angles():roll()-7)))
    Self:applyForce(Self:right() * -Speed)
}
if (S)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,0,holoEntity(2):angles():roll()+7)))
    holoAng(3,holoEntity(1):toWorld(ang(0,0,holoEntity(3):angles():roll()+7)))
    holoAng(4,holoEntity(1):toWorld(ang(0,0,holoEntity(4):angles():roll()+7)))
    holoAng(5,holoEntity(1):toWorld(ang(0,0,holoEntity(5):angles():roll()+7)))
    Self:applyForce(Self:right() * Speed)
}
if (A)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,35,holoEntity(2):angles():roll()-7)))
    holoAng(4,holoEntity(1):toWorld(ang(0,35,holoEntity(4):angles():roll()-7)))
    Self:applyTorque(vec(0, 0, Turning))
}
if (D)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,-35,holoEntity(2):angles():roll()-7)))
    holoAng(4,holoEntity(1):toWorld(ang(0,-35,holoEntity(4):angles():roll()-7)))
    Self:applyTorque(vec(0, 0, -Turning))
}
if (Shift)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,0,holoEntity(2):angles():roll()-10)))
    holoAng(3,holoEntity(1):toWorld(ang(0,0,holoEntity(3):angles():roll()-10)))
    holoAng(4,holoEntity(1):toWorld(ang(0,0,holoEntity(4):angles():roll()-10)))
    holoAng(5,holoEntity(1):toWorld(ang(0,0,holoEntity(5):angles():roll()-10)))
    Self:applyForce(Self:right() * -Speed*1.6)
}
if (Shift & D)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,-35,holoEntity(2):angles():roll()-7)))
    holoAng(4,holoEntity(1):toWorld(ang(0,-35,holoEntity(4):angles():roll()-7)))
}
if (Shift & A)
{
    holoAng(2,holoEntity(1):toWorld(ang(0,35,holoEntity(2):angles():roll()-7)))
    holoAng(4,holoEntity(1):toWorld(ang(0,35,holoEntity(4):angles():roll()-7)))
}

#Stabilization
Angle = -Self:angles() * ang(0.5, 0, 0)
Self:applyAngForce((Angle + $Angle * 20) - Self:angVel() / TurnResistance)
#Hovering
rangerFilter(Self)
rangerFilter(players())
Ranger = rangerOffset(Height, Self:pos(), -Self:up())
if (Ranger:hit())
{
    Distance = Ranger:distance()
    Pos = (Ranger:pos() + vec(0, 0, Height)) - Self:pos()
    Self:applyForce((Pos + $Pos * 20) - Self:vel() / SpeedResistance)
}

if (Mouse1&GL:count()<0.01){timer("boom",100)
}
if (clk("boom")){
    soundPlay(3,0.9,"/weapons/boxing_gloves_hit3.wav")
    Boom=propSpawn("models/Items/AR2_Grenade.mdl",holoEntity(6):pos()+vec(0,0,10),holoEntity(7):angles(),0)
    Boom:setTrails(15,0,2,"trails/smoke",vec(255,255,255),255)
    Boom:applyForce((Boom:forward())*99999)
    GL:pushEntity(Boom)
    GT:pushNumber(Start)
}

for(I=1,GL:count()){
GT[I,number]=GT[I,number]+1
if (GT[I,number]>100){GT:remove(I)
    #Des=propSpawn("models/props_phx/amraam.mdl",GL[I,entity]:pos(),1)
    #Des:propBreak()
    GL[I,entity]:propDelete()
    GL:remove(I)
}
}

#######################################
###        Credits                  ###
##   Wrain - Holos $ Sounds          ##
##    Ott - Movement & Cam           ##
## Stinkmeaner - Rotation of turret  ##
##                                   ##
###                                 ###
#######################################
