@name CT Thin Ice 2
@persist [Scr Forcer]:wirelink
@persist Settings:table
#include "lib/func"

Global=gTable("ThinIce2")
runOnChat(1)
interval(100)

if(first()) {
    echoSetName("ThinIce2")
    echoSetColor(Cyan)
    echo(array(Green,"Thin Ice ",White,"is being loaded."))
    Settings["Size",number]=12
    Settings["BuildSpeed",number]=10
    if (!Global["Plates",array]) {Global["Plates",array]=array()}
    
    function ghostLayout() {
        holoCreate(1)
        holoModel(1,"models/hunter/plates/plate8x8.mdl")
        holoScale(1,vec(Settings["Size",number]/8,Settings["Size",number]/8,0))
        holoPos(1,Entity:toWorld(vec(0,0,-10)))
        holoParent(1,Entity)
        holoMaterial(1,"debug/debugdrawflat")
        holoAlpha(1,128)
        
        holoCreate(2)
        holoPos(2,Entity:toWorld(vec(24*Settings["Size",number]+8,0,-10)))
        holoModel(2,"models/hunter/plates/plate05x05.mdl")
        holoParent(2,Entity)
        holoScale(2,vec(0.8,0.8,0))
        holoColor(2,vec(0,255,0))
        holoMaterial(2,"debug/debugdrawflat")
    }
    
    function setupEGP() {
        findByClass("gmod_wire_egp_emitter")
        local Ent=noentity()
        foreach(I,V:entity=findToArray()) {
            if(V:owner()==Owner) {
                Ent=V
                break
            }
        }
        if (Ent) {
            echo(array("Moving EGP to location."))
            Scr=Ent:wirelink()
            local Center=Settings["TotalVec",vector]/Settings["Size",number]^2
            local Offset=vec(24*Settings["Size",number]+8,0,0)
            Ent:setPos(Center+Offset)
            Ent:setAng(Offset:toAngle()+ang(0,90,0))
            
        } else {
            setTimeout("setupEGP",1000)
        }
    }
    
    function spawnArenaProp() {
        local Prop=propSpawn("models/hunter/plates/plate1x1.mdl",1)
        local Size=Prop:aabbSize()
        Prop:setPos(
            holoEntity(1):toWorld(
                (Size/2)+Size
                    *vec(Settings["Row",number]-1,Settings["Column",number]-1,0.5)
                    +vec(-(397/16)*Settings["Size",number],-(397/16)*Settings["Size",number],0)
                )
            )
        Settings["TotalVec",vector]=Settings["TotalVec",vector]+Prop:pos()
        Global["Plates",array]:pushEntity(Prop)
        Settings["Column",number]=Settings["Column",number]+1
        if(Settings["Column",number]>Settings["Size",number]) {
            Settings["Column",number]=1
            Settings["Row",number]=Settings["Row",number]+1
            if(Settings["Row",number]>Settings["Size",number]) {
                echo(array("Spawned all plates."))
                echo(array("Spawn an EGP."))
                setTimeout("setupEGP",1000)
            } else {
                setTimeout("spawnArenaProp",Settings["BuildSpeed",number])
            }
        } else {
            setTimeout("spawnArenaProp",Settings["BuildSpeed",number])
        }
    }
    
    function setupArena(Args:array) {
        deleteChatCommand("setup")
        deleteChatCommand("confirm")
        echo(array("Building a new arena..."))
        Settings["Row",number]=1
        Settings["Column",number]=1
        Settings["TotalVec",vector]=vec()
        Settings["BasePos",vector]=holoEntity(1):pos()-vec(-24*Settings["Size",number],-24*Settings["Size",number],0)
        Settings["EgpPos",vector]=holoEntity(2):pos()
        #holoDelete(1)
        #holoDelete(2)
        setTimeout("spawnArenaProp",Settings["BuildSpeed",number])
    }
    
    function arenaFinished(Args:array) {
        deleteChatCommand("setup")
        deleteChatCommand("confirm")
        echo(array("Arena confirmed."))
    }
    
    echo(array("Props: ",toString(Global["Plates",array]:count())))
    if (Global["Plates",array]:count()<Settings["Size",number]^2) {
        echo(array("Layout ghosted.  Say ",Green,".setup",White," to build the arena."))
        echo(array("Materials needed:"))
        echo(array(Green,"1x EGP v3"))
        echo(array(Green,Settings["Size",number]^2+" Props"))
        foreach(I,V:entity=Global["Plates",array]) {V:propDelete()}
        Global["Plates",array]:clear()
        ghostLayout()
        registerChatCommand("setup","setupArena",".setupArena","Builds a new arena.")
    } else {
        echo(array(Green,toString(Global["Plates",array]:count()),White," plates found.  Say .confirm to use this set."))
        registerChatCommand("confirm","arenaFinished",".confirm","Confirm a pre-existing arena.")
    }
}
