################
# > Minecraft Builder: Build things out of blocks, like Minecraft.
# > How to use: Type '!blocks' for a list of blocks. Right click to spawn a block. Left click to mine a block.
################

@name Minecraft Builder
@persist User:entity LS:string Model:string Scale Material:string Colour:vector
@persist Block:entity NotSolid
@persist IDS:array Materials:array Colours:array NotSolids:array Mat2Col:table
@persist SunCol Brightness BlocksList:array
@persist TorchAng:angle

interval(50)

if (first()|duped()) {
    #Model="models/hunter/blocks/cube075x075x075.mdl"
    #Scale=35.4
    User=findPlayerByName("neo")
    Model="models/hunter/blocks/cube1x1x1.mdl"
    Scale=47.4
    Materials=array("models/props_wasteland/rockcliff02b.mdl",
    "models/props/de_inferno/infflra",
    "brick/brick_model",
    "models/props_wasteland/dirtwall001a.mdl",
    "phoenix_storms/wood",
    "models/props/CS_militia/roofbeams01",
    "models/props_lab/xencrystal_sheet",
    "phoenix_storms/pack2/glass",
    "phoenix_storms/pack2/darkblue",
    "phoenix_storms/ps_grass",
    "models/antlion/antlion_innards.mdl",
    "phoenix_storms/gear.mdl",
    "phoenix_storms/Indenttiles1",
    "phoenix_storms/Indenttiles2"
    )
    IDS=array("stone","cobblestone","brick","dirt","wood","tree",
    "foliage","glass","water","grass","lava","sand",
    "gold")
    C=vec(255,255,255)
    Colours=array(C,C,C,C,C,C,C,C,C,C,vec(255,5,0),vec(240,200,0),vec(255,215,0))
    NotSolids=array(0,0,0,0,0,0,0,0,1,0,1,0,0)
    for (I=1,Materials:count()) {
        Mat2Col[Materials[I,string],vector]=Colours[I,vector]
    }
    propSpawnEffect(0)
    Brightness=0.5
    SunCol=0.5
    print(".:Minecraft Builder by Neo:.")
    print("Type '!blocks' for a list of blocks.")
    print("Type '!torch' or '!deletetorch' to spawn and delete torches.")
}

LS = User:lastSaid():lower()

if (changed(LS)) {
    if (LS:left(1)!="!") {
    for (I=1,IDS:count()) {
        if (LS==IDS[I,string]) {
            Material=Materials[I,string]
            Colour=Colours[I,vector]
            NotSolid=NotSolids[I,number]
            print("Block: '"+IDS[I,string]+"' selected.")
        }
    }
    }
    elseif (LS:left(1)=="!") {
        if (LS=="!torch") {
            TorchAng=(User:aimNormal()*90):toAngle()
            Block=propSpawn("models/props_wasteland/prison_cagedlight001a.mdl",User:aimPos(),1)
            Block:setAng(TorchAng)
            findByModel(Model)
            BlocksList=findToArray()
            for (I=1,BlocksList:count()) {
                Brightness=1.5-(BlocksList[I,entity]:pos():distance(Block:pos())/240)
                Brightness=clamp(Brightness,SunCol,999)
                BlocksList[I,entity]:setColor(BlocksList[I,entity]:getColor()*Brightness)
            }
        }
        elseif (LS=="!deletetorch") {
            Block=User:aimEntity()
            findByModel(Model)
            BlocksList=findToArray()
            for (I=1,BlocksList:count()) {
                BlocksList[I,entity]:setColor(Mat2Col[BlocksList[I,entity]:getMaterial(),vector]*SunCol)
            }
            User:aimEntity():propDelete()
        }
        elseif (LS=="!blocks"){
            printTable(IDS)
            print("Type the block name to select it. Example: 'grass'")
        }
    }
}

AimEnt=User:aimEntity()
M1=User:keyAttack1()
M2=User:keyAttack2()
if (changed(M2)&M2&User:weapon():type()!="gmod_tool") {
    if (AimEnt&round(AimEnt:angles())==ang(0,0,0)) {
        Block = propSpawn(Model,AimEnt:pos()+User:aimNormal()*Scale,1)
        Block:setMaterial(Material)
        Block:setColor(Colour*SunCol)
        Block:propNotSolid(NotSolid)
    }
    elseif (!AimEnt) {
        Block = propSpawn(Model,(round(User:aimPos()/Scale)*Scale)+vec(0,0,Scale/2),1)
        Block:setMaterial(Material)
        Block:setColor(Colour*SunCol)
        Block:propNotSolid(NotSolid)
    }
    if (round(AimEnt:angles())) { print("ERROR! Block is not straight.") }
}

if (changed(M1)&M1&AimEnt:model()==Model) {
    AimAlpha=AimEnt:getAlpha()
    AimEnt:setAlpha(AimAlpha-25.5)
    if (AimAlpha<=0) {
        AimEnt:propDelete()
    }
}
