@name C4TTT
@persist [E]:entity
@persist [RedWire BlueWire GreenWire OrangeWire]:entity
@persist BombCode Speed BombTime E2_STATUS:string
@persist Bleep TickSpeed HasDefused
@persist 
@persist 
@trigger all
@model models/weapons/w_c4_planted.mdl

## By FailCake :D - P1 - Video Here : https://www.youtube.com/watch?v=C31AvjYHao0
## By FailCake :D - P2 - Video Here : https://www.youtube.com/watch?v=-MJNEfXqfCY

###############
## FUNCTIONS ##
###############
if(first() || duped()){
    
    ## WIRE LABEL
    # 1 = Red
    # 2 = Blue
    # 3 = Green
    # 4 = Orange
    
    function number generateBombCode(){
        return randint(1,4)
    }
    
    function void createWires(){
        
        
      RedWire = propSpawn("models/Gibs/wood_gib01e.mdl",E:toWorld(vec(-2,2,8.7)),E:toWorld(ang(-25,90,0)),1)
      RedWire:parentTo(E)
      RedWire:setAlpha(0)
    
      BlueWire = propSpawn("models/Gibs/wood_gib01e.mdl",E:toWorld(vec(0,2,8.7)),E:toWorld(ang(-25,90,0)),1)
      BlueWire:parentTo(E)
      BlueWire:setAlpha(0)
    
      GreenWire = propSpawn("models/Gibs/wood_gib01e.mdl",E:toWorld(vec(2,2,8.7)),E:toWorld(ang(-25,90,0)),1)
      GreenWire:parentTo(E)
      GreenWire:setAlpha(0)
    
      OrangeWire = propSpawn("models/Gibs/wood_gib01e.mdl",E:toWorld(vec(4,2,8.7)),E:toWorld(ang(-25,90,0)),1)
      OrangeWire:parentTo(E)
      OrangeWire:setAlpha(0)
    
      holoCreate(1)
      holoModel(1,"hqcylinder")
      holoPos(1,RedWire:pos())
      holoAng(1,RedWire:angles() + ang(90,0,0))
      holoParent(1,RedWire)
      holoScaleUnits(1,vec(0.4,0.4,8))
      holoMaterial(1,"models/debug/debugwhite")
      holoColor(1,vec(255,0,0))
    
      holoCreate(2)
      holoModel(2,"hqcylinder")
      holoPos(2,BlueWire:pos())
      holoAng(2,BlueWire:angles() + ang(90,0,0))
      holoParent(2,BlueWire)
      holoScaleUnits(2,vec(0.4,0.4,8))
      holoMaterial(2,"models/debug/debugwhite")
      holoColor(2,vec(0,0,255))
    
      holoCreate(3)
      holoModel(3,"hqcylinder")
      holoPos(3,GreenWire:pos())
      holoAng(3,GreenWire:angles() + ang(90,0,0))
      holoParent(3,GreenWire)
      holoScaleUnits(3,vec(0.4,0.4,8))
      holoMaterial(3,"models/debug/debugwhite")
      holoColor(3,vec(0,255,0))
    
      holoCreate(4)
      holoModel(4,"hqcylinder")
      holoPos(4,OrangeWire:pos())
      holoAng(4,OrangeWire:angles() + ang(90,0,0))
      holoParent(4,OrangeWire)
      holoScaleUnits(4,vec(0.4,0.4,8))
      holoMaterial(4,"models/debug/debugwhite")
      holoColor(4,vec(255,155,0))
    
      holoCreate(5)
      holoModel(5,"models/props_lab/reciever01d.mdl")
      holoPos(5,E:toWorld(vec(1.2,6,8)))
      holoAng(5,E:toWorld(ang(-90,90,0)))
      holoParent(5,E)
      holoScale(5,vec(1,1,1))
    
      holoCreate(6)
      holoModel(6,"hqicosphere")
      holoPos(6,E:toWorld(vec(4,-7,8.8)))
      holoParent(6,E)
      holoMaterial(6,"models/debug/debugwhite")
      holoScaleUnits(6,vec(1,1,1))
      holoColor(6,vec(255,0,0))
      holoAlpha(6,0)
    
      holoCreate(7)
      holoModel(7,"models/weapons/w_knife_t.mdl")
      holoPos(7,E:toWorld(vec(-2,1,20)))
      holoAng(7,E:toWorld(ang(145,0,0)))
      holoParent(7,E)
      holoScale(7,vec(1,1,1))
      holoAlpha(7,0)
    
    }
    
    function void explodeBomb(){
                
     local Bomb = propSpawn("models/props_c17/oildrum001_explosive.mdl",E:pos(),E:angles(),1)
        findByClass("player")
        local Player2 = findClosest(E:pos()) 
        
        Player2:ignite()
        
     Bomb:propBreak()
     Bomb:setAlpha(0)
    
     
     soundPurge()
        
     E:soundPlay(1,4,"radio/terwin.wav") 
     E:soundPlay(2,2,"weapons/c4/c4_exp_deb1.wav")   
     E:soundPlay(3,3,"weapons/c4/c4_exp_deb2.wav") 
     E:soundPlay(4,4,"weapons/c4/c4_explode1.wav") 
    
     stoptimer("bombSound")
     stoptimer("bombTick")
        
     selfDestructAll()
    
    }
    
    function void bombThink(){
        
        findByClass("player")
        local Player = findClosest(E:pos())
        local AimEnt = 0
        
        if(Player:pos():distance(E:pos()) < 50 && HasDefused == 0){
         
           if(Player:aimEntity() == RedWire){
                AimEnt = 1
                holoPos(7,E:toWorld(vec(-8,1,20)))
                holoAlpha(7,255)
           }elseif(Player:aimEntity() == BlueWire){
                AimEnt = 2
                holoPos(7,E:toWorld(vec(-6,1,20)))
                holoAlpha(7,255)
           }elseif(Player:aimEntity() == GreenWire){
                AimEnt = 3
                holoPos(7,E:toWorld(vec(-4,1,20)))
                holoAlpha(7,255)
           }elseif(Player:aimEntity() == OrangeWire){
                AimEnt = 4
                holoPos(7,E:toWorld(vec(-2,1,20)))
                holoAlpha(7,255)
           }else{
                holoAlpha(7,0)
           }
         
           if(changed(Player:keyUse()) && Player:keyUse() && AimEnt != 0){
                 
                 HasDefused = 1
                
                 if(AimEnt == BombCode){
                    
                  soundPurge()
                  E:soundPlay(1,5,"radio/bombdef.wav")  
                  
                  holoDelete(AimEnt)
                  holoDelete(7)
                  
                  holoColor(6,vec(0,255,0))
                  holoAlpha(6,255)
                
                  E2_STATUS = "DISABLED"
                  timer("destroyBomb",3000)
                
                 }else{
                
                  TickSpeed = 200
                  E:soundPlay(23,3,"buttons/button8.wav")
                  holoDelete(AimEnt)
                  holoDelete(7) 
                
                 }
                
           }   
        }
        
        
    }
    
}

##########
## MAIN ##
##########
if(first() || duped()){
    
    runOnLast(1)
    runOnTick(1)
    
    propSpawnEffect(0)
    
    E = entity()
    BombCode = generateBombCode()
    print(BombCode)
    
    E:soundPlay(1,3,"weapons/c4/c4_plant.wav")
    E:soundPlay(2,3,"radio/bombpl.wav")
    
    Speed = 1000
    BombTime = 45
    HasDefused = 0
    TickSpeed = 1000
    
    E2_STATUS = "ACTIVE"
    
    createWires()
    timer("bombTick",TickSpeed)
    timer("bombSound",Speed)
}

if(E2_STATUS == "ACTIVE"){
    
    if(clk("bombTick")){
        
        timer("bombTick",TickSpeed)
        BombTime -= 1
        
        if(BombTime <= 0){   
            
         E2_STATUS = "DISABLED"
         explodeBomb()   
        }
        
        if(BombTime == 10){
            Speed = 600
        }elseif(BombTime == 8){
            Speed = 400
        }elseif(BombTime == 5){
            Speed = 200
        }elseif(BombTime == 2){
            Speed = 100
        }
     
    }
    
    bombThink()
    
    
    if(clk("bombSound")){
        timer("bombSound",Speed)
        E:soundPlay(1,2,"weapons/c4/c4_click.wav")
        
        if(Bleep){
            holoAlpha(6,0)
        }else{
            holoAlpha(6,255)
        }
        Bleep = !Bleep
    }

}

if(clk("destroyBomb")){
 selfDestructAll()   
}

if(last()){
    RedWire:propDelete()
    BlueWire:propDelete()
    GreenWire:propDelete()
    OrangeWire:propDelete()
}





















