@name CTFeller
@inputs Scr:wirelink Door1:entity Door2:entity User:entity
@outputs Fling Nc Nc2 Fling2
@persist Players:array Prize Speed Plates:array Inside:array User2:entity
@persist Available:array TimeStart Starting Ready Price Opos:vector On SA
@persist Startinga End Win Stale Reward Removedp:entity
@persist Get Ent:entity
@persist Fail:array
interval(1000)
Restore=100-43
if (first()) {
    function number isPlaying(E:entity) {
        for (I=1, Players:count()) {
            if (E==Players[I,entity]) {
                return 1
            }
        }
        return 0
    }
    function number setHealth(E:entity,H) {
        concmd("FAdmin")
        local P=toChar(34)
        Name=E:name()
        CMD="fadmin sethealth "+P+(Name)+P+" "+H
        print(CMD)
        concmd(CMD)
        if (E:health()==H) {
            return 1
        } else {
            return 0
        }
    }
    soundPurge()
    timer("soundstart",500)
    function sound(S:string) {
        SA++
        if (SA>7) {SA=1}
        Scr:entity():soundPlay(SA,0,S)
    }
    Startinga=1
    Price=50
    SA=3
    Fail=array(
        "/ct_forcedownload/ctgame_localfail1.wav",
        "/ct_forcedownload/ctgame_localfail2.wav",
        "/ct_forcedownload/ctgame_localfail3.wav"
        )
    TimeQue=array(
        30="/ct_forcedownload/ctb_30sec.wav",
        20="/ct_forcedownload/ctb_20sec.wav",
        10="/ct_forcedownload/ctb_10sec.wav",
        5="/ct_forcedownload/ct_5sec.wav",
        4="/ct_forcedownload/ct_4sec.wav",
        3="/ct_forcedownload/ct_3sec.wav",
        2="/ct_forcedownload/ct_2sec.wav",
        1="/ct_forcedownload/ct_1sec.wav"
    )
    #phoenix_storms/cube
    findByClass("prop_physics")
    Plates=findToArray()
    Removed=0
    print("[FELLER] Finding plates...")
    for (I=1,Plates:count()) {
        if (Plates[I-Removed,entity]:getMaterial()!="phoenix_storms/cube") {
            #print("[FELLER] Removed entity from table, material "+(Plates[I-Removed,entity]:getMaterial()))
            Plates:removeEntity(I-Removed)
            Removed++
    }
    }
    for (I=1, Plates:count()) {
        Plates[I,entity]:propNotSolid(0)
    }
    print("[FELLER] Found "+(Plates:count())+" plates.")
}
if (User & !User2 & !On & !Stale) {
    User2=User
    if (TimeStart) {
    #moneyRequest(User,Price,TimeStart,"[FELLER]")
} else {
    #moneyRequest(User,Price,0,"[FELLER]")
    }
}

if (User & !isPlaying(User) & !On) {
    User2=User
    concmd("FAdmin")
    setHealth(User,1)
    print("[FELLER] "+(User2:name())+" has joined the game.")
    Reward+=50
    Players[Players:count()+1,entity]=User2
    User2=noentity()
    Door1:setAlpha(0)
    Door1:propNotSolid(1)
    Opos=Door1:pos()
    Door1:setPos(vec(0,0,0))
    Fling=10000
    Fling2=-10000
    Nc=1
    TimeStart+=5
    sound("/ct_forcedownload/ctb_timeadded.wav")
    soundStop(0)
    stoptimer("soundstart")
    timer("soundstart",2700)
    sound("/ct_forcedownload/ctgame_entergame.mp3")
    stoptimer("close")
    timer("close",750)
}
#if (moneyNoClk() | moneyTimeout()) {
#    User2=noentity()
#}

if (clk("close")) {
    Door1:setAlpha(255)
    Door1:propNotSolid(0)
    Door1:setPos(Opos)
    Fling=0
    Fling2=0
    Nc=0
}


if (TimeStart & !Startinga) {
    TimeStart--
    Win=0
    #print("[FELLER] Mission begins in "+TimeStart+" seconds.")
    if (TimeStart==30) {
        sound("/ct_forcedownload/ctb_30sec.wav")
} elseif (TimeStart==20) {sound("/ct_forcedownload/ctb_20sec.wav")}
elseif (TimeStart==10) {sound("/ct_forcedownload/ctb_10sec.wav")}
elseif (TimeStart<=5) {sound("/ct_forcedownload/ct_"+TimeStart+"sec.wav")}
} elseif (!On & !Startinga) {
    On=1 
    Starting=1 
    local Int=randint(1,Plates:count())
    Removedp=Plates[Int,entity]
    holoDeleteAll()
    holoCreate(1,Plates[Int,entity]:toWorld(vec(0,0,75)))
    holoScale(1,vec(2,2,2))
    holoModel(1,"hq_cone")
    holoMaterial(1,"debug/debugdrawflat")
    holoColor(1,vec(0,255,0))
    holoAng(1,ang(0,0,180))
    holoVisible(1,players(),0)
    holoVisible(1,owner(),1)
    Plates:removeEntity(Int)
}
if (clk("soundstart")) {
    Scr:entity():soundPlay(0,0,"/ct_forcedownload/ctgame_longloop.wav")
    if (On) {Starting=0}
}
if (On & changed(On)) {
    #StartQue
    soundStop(0)
    Stale=0
    timer("soundstart",22000)
    sound("/ct_forcedownload/ctgame_beginningintro.mp3")
}
setName("[FELLER]
Starting in: "+TimeStart+" seconds.
Props available: "+(Available:count()))
if (On) {
    Nc2=0
    Door2:setAlpha(255)
} else {
    Nc2=1
    Door2:setAlpha(0)
}

if (On) {
    for (I=1, Players:count()) {
        if (Players[I,entity]:pos():z()<Plates[1,entity]:pos():z() & Players[I,entity]:isValid()) {
            sound(Fail[random(Fail:count()),string])
            print("[FELLER] "+(Players[I,entity]:name())+" has failed!")
            Players:removeEntity(I)
        }
    }
}

if (first() | changed(Scr)) {#Emit.egp
    Scr:egpClear()
    Scr:egpText(1,"Feller",vec2(256,0))
    Scr:egpAlign(1,1,0)
    Scr:egpSize(1,70)
    Scr:egpColor(1,vec(0,0,255))
    Scr:egpCircle(3,vec2(128,256),vec2(80,80))
    Scr:egpCircle(2,vec2(128,256),vec2(64,64))
    Scr:egpColor(2,vec(128,128,128))
    Scr:egpCircle(6,vec2(512-128,256),vec2(90,90))
    Scr:egpCircle(5,vec2(512-128,256),vec2(64,64))
    Scr:egpColor(5,vec(128,128,128))
    Scr:egpText(8,"SPEED",vec2(512-128,256-64-10))
    Scr:egpSize(8,30)
    Scr:egpColor(8,vec(0,0,0))
    Scr:egpAlign(8,1,1)

    
}

Scr:egpText(4,TimeStart:toString(),Scr:egpPos(2))
Scr:egpAlign(4,1,1)
Scr:egpSize(4,50)
if (On | Startinga) {
    for (I=1, 3) {
    Scr:egpAlpha(I+1,0)}
} else {
    for (I=1, 3) {
    Scr:egpAlpha(I+1,255)}

}
if (On & Starting) {
    Scr:egpText(399,"Prepare to Begin",vec2(256,60))
    Scr:egpSize(399,30)
    Scr:egpAlign(399,1)
    Speed=randint(3,8)
    Scr:egpText(7,""+Speed,Scr:egpPos(5))
    Scr:egpAlign(7,1,1)
    Scr:egpSize(7,50)
    Scr:egpAlpha(8,255)
    Scr:egpAlpha(6,255)
    Scr:egpAlpha(5,255)
    Scr:egpColor(7,vec(255,255,255))
} elseif (!On) {
    Scr:egpAlpha(6,0)
    Scr:egpAlpha(5,0)
    Scr:egpAlpha(8,0)
} else {
    Scr:egpColor(7,vec(0,255,0))
    Scr:egpRemove(399)
}

if (On & !Starting & changed(Starting)) {
    sound("/ct_forcedownload/ctb_gamestart.wav")
    Available=Plates
    timer("plate",(10-Speed)*250)
}

if (clk("plate")) {
    Get=randint(1,Available:count())
    Ent=Available[Get,entity]
    Ent:setColor(vec(128,0,0))
    timer("removeplate",(10-Speed)*250)
}
if (clk("removeplate")) {
    if (Ent:getAlpha()>0) {Available:removeEntity(Get)}
    Ent:setAlpha(0)
    Ent:propNotSolid(1)
    timer("plate",(10-Speed)*250)
}
    
if (!On & changed(On)) {
    for (I=1, Plates:count()) {
        Plates[I,entity]:setAlpha(255)
        Plates[I,entity]:setColor(vec(255,255,255))
    }
}
#control start
if (Players:count()>1) {Startinga=0} else {Startinga=1}
if (changed(Startinga) & !Startinga) {TimeStart=31}
if (Startinga) {
    Scr:egpText(398,"Waiting for Players",vec2(256,80))
    Scr:egpAlign(398,1,1)
    Scr:egpSize(398,30)
} else {Scr:egpRemove(398)}

if ((TimeStart | Startinga) & !Stale) {
    for (I=1, Plates:count()) {
        Plates[I,entity]:setColor(vec(randint(255),randint(255),randint(255)))
    }
} elseif (changed(Startinga) | Stale) {
    for (I=1, Plates:count()) {
        Plates[I,entity]:setColor(vec(255,255,255))
    }
}

if (Available:count()<1 & Stale & changed(Available:count())) {Stale=0 Win=0}
if (On & !Starting & Available:count()<1 & Players:count()>1 & !Win & !TimeStart) {
    Win=1
    sound("/ct_forcedownload/ctgame_gameloss2.mp3")
    sound("/ct_forcedownload/ctb_stalemate.wav")
    #3000
    timer("stalemate",3000)
    
}

if (On & !Starting & Players:count()==1 & !End & !TimeStart) {
    End=1
    sound("/ct_forcedownload/ctb_victory.wav")
    sound("/ct_forcedownload/ctgame_end.mp3")
    soundStop(0)
    for (I=1, Plates:count()) {
        Plates[I,entity]:setAlpha(0)
        Plates[I,entity]:propNotSolid(1)
    }
    Removedp:propNotSolid(1)
    Removedp:setAlpha(0)
    #moneyGive(Players[1,entity],Reward)
    setHealth(Players[1,entity],100+Restore)
    timer("resetall",10000)
}
if (clk("resetall")) {
    for (I=1, Plates:count()) {
        Plates[I,entity]:setAlpha(0)
        Plates[I,entity]:setColor(vec(255,255,255))
        Plates[I,entity]:propNotSolid(0)
    }
    timer("resetchip",1000)
}
if (clk("resetchip")) {
    reset()
}

if (clk("stalemate") & !Stale) {
    for (I=1, Plates:count()) {
        Plates[I,entity]:setAlpha(0)
        Plates[I,entity]:propNotSolid(1)
    }
    Reward=Reward/Players:count()
    for (I=1, Players:count()) {
        #moneyGive(Players[I,entity],Reward)
        setHealth(Players[I,entity],100+Restore)
    }
    timer("resetchip",2000)
    
}
if (!On) {
    Scr:egpRemove(7)
}
Scr:egpText(300,"Reward: $"+Reward,vec2(256,412))
Scr:egpAlign(300,1,1)
Scr:egpSize(300,40)
Door2:propNotSolid(!On)
