@name PortalGun v6 2
@persist LC RC UK Fire Which CanUsePortals PortalsOn
@persist Aim:vector Ang:angle SurfAng:angle Rotat

# Created by NikoKun

interval(10)

if (first()) {

holoCreate(0) #dummy hologram to receive Pos bug
holoAlpha(0,0)
holoAng(0,owner():attachmentAng("anim_attachment_RH"))
holoPos(0,owner():attachmentPos("anim_attachment_RH"))
holoParentAttachment(0,owner(),"anim_attachment_RH")
holoScale(0,vec(0.01,0.01,0.01)) 
holoPos(0,holoEntity(0):toWorld(vec(0,0,0)))
holoAng(0,holoEntity(0):toWorld(ang(0,0,0)))

# large round white body
holoCreate(1)
holoModel(1,"hqicosphere2")
holoAng(1,owner():attachmentAng("anim_attachment_RH"))
holoPos(1,owner():attachmentPos("anim_attachment_RH"))
holoParentAttachment(1,owner(),"anim_attachment_RH")
holoScale(1,vec(1.04,0.66,0.66)) 
holoColor(1,vec(175,175,175))
holoMaterial(1,"models/shiny")
holoPos(1,holoEntity(1):toWorld(vec(9.4,0,3)))
holoAng(1,holoEntity(1):toWorld(ang(-12,-20,20)))

# extending black middle part
holoCreate(2)
holoModel(2,"hqcylinder2")
holoScale(2,vec(0.27,0.27,0.55))
holoColor(2,vec(24,24,24))
holoMaterial(2,"models/shiny") #"phoenix_storms/black_chrome"
holoPos(2,holoEntity(1):toWorld(vec(8.2,0,0)))
holoAng(2,holoEntity(1):toWorld(ang(90,0,0)))
holoParent(2,1)

# front end dome
holoCreate(3)
holoModel(3,"dome")
holoScale(3,vec(0.284,0.284,0.284))
holoColor(3,vec(24,24,24))
holoMaterial(3,"models/shiny")
holoPos(3,holoEntity(1):toWorld(vec(11.97,0,0)))
holoAng(3,holoEntity(1):toWorld(ang(-90,0,0)))
holoParent(3,1)

# large cupshape dome
holoCreate(4)
holoModel(4,"dome2")
holoScale(4,vec(0.31,0.31,0.3))
holoColor(4,vec(24,24,24))
holoMaterial(4,"models/shiny")
holoPos(4,holoEntity(1):toWorld(vec(10.87,0,0)))
holoAng(4,holoEntity(1):toWorld(ang(-90,0,0)))
holoParent(4,1)

# medium cupshape dome
holoCreate(5)
holoModel(5,"dome2")
holoScale(5,vec(0.37,0.37,0.3))
holoColor(5,vec(24,24,24))
holoMaterial(5,"models/shiny")
holoPos(5,holoEntity(1):toWorld(vec(10.1,0,0)))
holoAng(5,holoEntity(1):toWorld(ang(-90,0,0)))
holoParent(5,1)

# small cupshape dome
holoCreate(6)
holoModel(6,"dome2")
holoScale(6,vec(0.4,0.4,0.3))
holoColor(6,vec(24,24,24))
holoMaterial(6,"models/shiny")
holoPos(6,holoEntity(1):toWorld(vec(9.2,0,0)))
holoAng(6,holoEntity(1):toWorld(ang(-90,0,0)))
holoParent(6,1)

# secondary hand grip spot
holoCreate(7)
holoModel(7,"dome2")
holoScale(7,vec(0.47,0.4,0.3))
holoColor(7,vec(175,175,175))
holoMaterial(7,"models/shiny")
holoPos(7,holoEntity(1):toWorld(vec(8.92,0,-0.6)))
holoAng(7,holoEntity(1):toWorld(ang(190,0,0)))
holoParent(7,1)

# top arm
holoCreate(8)
holoModel(8,"cube")
holoScale(8,vec(0.04,0.04,0.4))
holoColor(8,vec(24,24,24))
holoMaterial(8,"models/shiny")
holoPos(8,holoEntity(6):toWorld(vec(3.2,0,-1)))
holoAng(8,holoEntity(6):toWorld(ang(142,0,0)))
holoParent(8,6)

holoCreate(9)
holoModel(9,"pyramid")
holoScale(9,vec(0.04,0.04,0.34))
holoColor(9,vec(24,24,24))
holoMaterial(9,"models/shiny")
holoPos(9,holoEntity(8):toWorld(vec(1.5,0,2.7)))
holoAng(9,holoEntity(8):toWorld(ang(75,0,0)))
holoParent(9,8)

# right arm
holoCreate(10)
holoModel(10,"cube")
holoScale(10,vec(0.04,0.04,0.4))
holoColor(10,vec(24,24,24))
holoMaterial(10,"models/shiny")
holoPos(10,holoEntity(6):toWorld(vec(-1.8,3.2,-1)))
holoAng(10,holoEntity(6):toWorld(ang(142,120,0)))
holoParent(10,6)  #pyramid

holoCreate(11)
holoModel(11,"pyramid")
holoScale(11,vec(0.04,0.04,0.34))
holoColor(11,vec(24,24,24))
holoMaterial(11,"models/shiny")
holoPos(11,holoEntity(10):toWorld(vec(1.5,0,2.7)))
holoAng(11,holoEntity(10):toWorld(ang(75,0,0)))
holoParent(11,10)

# left arm
holoCreate(12)
holoModel(12,"cube")
holoScale(12,vec(0.04,0.04,0.4))
holoColor(12,vec(24,24,24))
holoMaterial(12,"models/shiny")
holoPos(12,holoEntity(6):toWorld(vec(-1.8,-3.2,-1)))
holoAng(12,holoEntity(6):toWorld(ang(142,-120,0)))
holoParent(12,6)

holoCreate(13)
holoModel(13,"pyramid")
holoScale(13,vec(0.04,0.04,0.34))
holoColor(13,vec(24,24,24))
holoMaterial(13,"models/shiny")
holoPos(13,holoEntity(12):toWorld(vec(1.5,0,2.7)))
holoAng(13,holoEntity(12):toWorld(ang(75,0,0)))
holoParent(13,12)

# little wire top
holoCreate(14)
holoModel(14,"hqtorus2")
holoScale(14,vec(0.224,0.324,0.12))
holoColor(14,vec(24,24,24))
holoPos(14,holoEntity(2):toWorld(vec(-2,0,-1.4)))
holoAng(14,holoEntity(2):toWorld(ang(0,0,90)))
holoParent(14,2)
#right
holoCreate(15)
holoModel(15,"hqtorus2")
holoScale(15,vec(0.3,0.32,0.12))
holoColor(15,vec(24,24,24))
holoPos(15,holoEntity(2):toWorld(vec(1.04,-1.8,-1.44)))
holoAng(15,holoEntity(2):toWorld(ang(0,120,90)))
holoParent(15,2)
#left
holoCreate(16)
holoModel(16,"hqtorus2")
holoScale(16,vec(0.3,0.32,0.12))
holoColor(16,vec(24,24,24))
holoPos(16,holoEntity(2):toWorld(vec(1.04,1.8,-1.44)))
holoAng(16,holoEntity(2):toWorld(ang(0,-120,90)))
holoParent(16,2)

# Creating Portals
    holoCreate(17,entity():pos())
    holoModel(17,"hqcylinder2")
    holoColor(17,vec(24,24,242))
    holoScale(17,vec(8,8,0.04))
    holoAlpha(17,0)

    holoCreate(18,holoEntity(17):toWorld(vec(0,0,0.4)))
    holoModel(18,"hqcylinder2")
    holoScale(18,vec(9,9,0.04))
    holoMaterial(18,"models/effects/portalrift_sheet")
    holoParent(18,17)
    holoAlpha(18,0)

    holoCreate(19,entity():pos())
    holoModel(19,"hqcylinder2")
    holoColor(19,vec(242,100,24))
    holoScale(19,vec(8,8,0.04))
    holoAlpha(19,0)

    holoCreate(20,holoEntity(19):toWorld(vec(0,0,0.4)))
    holoModel(20,"hqcylinder2")
    holoScale(20,vec(9,9,0.04))
    holoMaterial(20,"models/effects/portalrift_sheet")
    holoParent(20,19)
    holoAlpha(20,0)

# Portal Indicator
holoCreate(21)
holoModel(21,"hqcylinder2")
holoScale(21,vec(0.2,0.2,0.07))
holoColor(21,vec(24,24,24))
holoPos(21,holoEntity(1):toWorld(vec(2,0,3.7)))
holoAng(21,holoEntity(1):toWorld(ang(10,0,0)))
holoParent(21,1)

# Teleport Parts
    holoCreate(23,holoEntity(17):toWorld(vec(0,0,20)))
    holoAlpha(23,0)
    holoParent(23,17)
    
    holoCreate(24,holoEntity(19):toWorld(vec(0,0,20)))
    holoAlpha(24,0)
    holoParent(24,19)
    
    holoCreate(25,holoEntity(17):toWorld(vec(0,0,80)))
    holoAlpha(25,0)
    holoParent(25,17)
    
    holoCreate(26,holoEntity(19):toWorld(vec(0,0,80)))
    holoAlpha(26,0)
    holoParent(26,19)

Fire=0
Rotat=0
}

if(owner():weapon():type()=="weapon_physgun" | owner():weapon():type()=="weapon_physcannon")
{
    owner():weapon():setMaterial("models/effects/vol_light001") # null was causing errors
    holoAlpha(1,255)
    holoAlpha(2,255)
    holoAlpha(3,255)
    holoAlpha(4,255)
    holoAlpha(5,255)
    holoAlpha(6,255)
    holoAlpha(7,255)
    holoAlpha(8,255)
    holoAlpha(9,255)
    holoAlpha(10,255)
    holoAlpha(11,255)
    holoAlpha(12,255)
    holoAlpha(13,255)
    holoAlpha(14,255)
    holoAlpha(15,255)
    holoAlpha(16,255)
    holoAlpha(21,255)

    CanUsePortals=1
}
else{
    holoAlpha(1,0)
    holoAlpha(2,0)
    holoAlpha(3,0)
    holoAlpha(4,0)
    holoAlpha(5,0)
    holoAlpha(6,0)
    holoAlpha(7,0)
    holoAlpha(8,0)
    holoAlpha(9,0)
    holoAlpha(10,0)
    holoAlpha(11,0)
    holoAlpha(12,0)
    holoAlpha(13,0)
    holoAlpha(14,0)
    holoAlpha(15,0)
    holoAlpha(16,0)
    holoColor(21,vec(24,24,24))
    holoAlpha(21,0)
    holoDelete(22)

    CanUsePortals=0
}

if(CanUsePortals==1){
    LC = owner():keyAttack1()
    RC = owner():keyAttack2()
    UK = owner():keyUse()

    if(LC & $LC & !Fire){
        owner():soundPlay("portalshootblue",0,"weapons/portalgun/portalgun_shoot_blue1.wav")
        Aim = owner():aimPos()
        Ang = (Aim - holoEntity(3):pos()):toAngle()
        SurfAng = owner():aimNormal():toAngle()+ang(90,0,0)
        holoCreate(22,holoEntity(3):pos())
        holoAng(22,Ang)
        holoModel(22,"hqicosphere2")
        holoScale(22,vec(0.7,0.4,0.4))
        holoColor(22,vec(24,24,242),142)
        Fire = 1
        Which = 1
        holoColor(21,vec(24,24,242))
    }
    if(RC & $RC & !Fire){
        owner():soundPlay("portalshootred",0,"weapons/portalgun/portalgun_shoot_red1.wav")
        Aim = owner():aimPos()
        Ang = (Aim - holoEntity(3):pos()):toAngle()
        SurfAng = owner():aimNormal():toAngle()+ang(90,0,0)
        holoCreate(22,holoEntity(3):pos())
        holoAng(22,Ang)
        holoModel(22,"hqicosphere2")
        holoScale(22,vec(0.7,0.4,0.4))
        holoColor(22,vec(242,100,24),142)
        Fire = 1
        Which = 2
        holoColor(21,vec(242,100,24))
    }
    if(UK & $UK){
        holoAlpha(17,0)
        holoAlpha(18,0)
        holoAlpha(19,0)
        holoAlpha(20,0)
        holoDelete(22)
        owner():soundPlay(0,0,"weapons/portalgun/portal_close1.wav")
        holoColor(21,vec(24,24,24))
        Fire = 0
        PortalsOn = 0
    }
    if(Fire & Which==1) {
        Distance=holoEntity(22):pos():distance(Aim)
        holoPos(22,holoEntity(22):toWorld(vec(20,0,0)))
        if(Distance<10){
            holoDelete(22)
            holoAlpha(17,100)
            holoAlpha(18,255)
            holoPos(17,Aim)
            holoAng(17,SurfAng)
            holoEntity(17):soundPlay(17,0,"weapons/portalgun/portal_open3.wav")
            Fire=0
            PortalsOn = 1
        }
    }
    if(Fire & Which==2) {
        Distance=holoEntity(22):pos():distance(Aim)
        holoPos(22,holoEntity(22):toWorld(vec(20,0,0)))
        if(Distance<10){
            holoDelete(22)
            holoAlpha(19,100)
            holoAlpha(20,255)
            holoPos(19,Aim)
            holoAng(19,SurfAng)
            holoEntity(19):soundPlay(19,0,"weapons/portalgun/portal_open2.wav")
            Fire=0
            PortalsOn = 1
        }
    }
}

if(PortalsOn){  # Later make a PortalOn IF for each portal seperately
    holoPos(23,(holoEntity(17):toWorld(vec(sin(Rotat)*30,cos(Rotat)*30,20)))) # 30 is width of the spin
    holoPos(24,(holoEntity(19):toWorld(vec(sin(Rotat)*30,cos(Rotat)*30,20)))) # 20 is height of ranger
    Rotat=Rotat+40  # rotate speed
    if (Rotat>320){Rotat=0} #the > number should be 360 minus the rotat

    BluTel = rangerOffset(holoEntity(17):pos(),holoEntity(23):pos())
    BluEnt = BluTel:entity()
    if(BluEnt != noentity()){
        BluEnt:setPos(holoEntity(26):pos())
        #if(BluEnt==owner()){setEye((holoEntity(26):pos()-holoEntity(19):pos()):toAngle())}
    }
# Created by NikoKun
    OrangTel = rangerOffset(holoEntity(19):pos(),holoEntity(24):pos())
    OrangEnt = OrangTel:entity()
    if(OrangEnt != noentity()){
        OrangEnt:setPos(holoEntity(25):pos())
        #if(OrangEnt==owner()){setEye((holoEntity(25):pos()-holoEntity(17):pos()):toAngle())}
    }
}
