@name FailCake HTTP Image Parser V1
@inputs Screen:wirelink
@persist [E]:entity Load Data:string Said:array
@persist [ImageData Res]:array ResX ResY Start Sd:string
@persist TmpData:array Pixels:table Index X Y W H Percent Percent2 Mode
@persist Check Datass:string ListData:array String:array Oks:array Kk:string
@persist PlayList:array CurrentImage 
@trigger all 
@model models/beer/wiremod/watersensor.mdl

function void printList(FF:array){
printColor(vec(255,255,255),"########## ",vec(0,255,0),"List",vec(255,255,255)," ##########")  

for(I = 8,FF:count()-3){
String = FF[I,string]:explode("</a>")
Oks = String[1,string]:explode(">")
Kk = Oks[3,string]:replace("<a href=",""):replace(toChar(34),"")

if(Kk:find("img_")){
printColor(vec(0,0,255),"[List]",vec(0,255,0),"[Image] ",vec(255,255,255),Kk)
}else{
printColor(vec(0,0,255),"[List]",vec(255,0,0),"[Unknown] ",vec(255,255,255),Kk)
}
}

printColor(vec(255,255,255),"##########################")         
}



if(first() | duped())
{
runOnTick(1)
runOnHTTP(1)
runOnChat(1)

E = entity() 

ResX = 32
ResY = 32

Screen[1048572]= ResX # Set Vertical Resolution
Screen[1048573]= ResY # Set Horizontal Resolution
Screen[1048569]= 1 # Set Color Mode
Screen[1048574]= 0  # Hardware Clear screen

## Sphere thing Counter (loading)
holoCreate(1)
holoModel(1,"dome")
holoPos(1,E:toWorld(vec(0,0,0.6)))
holoScaleUnits(1,vec(1.2,1.2,1))
holoColor(1,vec(1,1,1))
holoParent(1,E)

holoCreate(2)
holoModel(2,"pyramid")
holoPos(2,E:toWorld(vec(-1,0,0.6)))
holoScaleUnits(2,vec(0.5,1,2.3))
holoColor(2,vec(1,1,1))
holoAng(2,E:toWorld(ang(-90,0,0)))
holoParent(2,1)

holoCreate(3)
holoModel(3,"models/props_junk/sawblade001a.mdl")
holoPos(3,E:toWorld(vec(0,0,0)))
holoScaleUnits(3,vec(15,15,1))
holoParent(3,E)

setName("FailCake HTTP Image Parser V1")    
}

if(chatClk(owner())){
Said = owner():lastSaid():explode(" ")

if(Said[1,string]:lower() == "-load" && Start == 0 && Check == 0){
Name = Said[2,string]    
Start = 1
E:soundPlay(1,0,"/buttons/button14.wav")
}

if(Said[1,string]:lower() == "-list" && Check == 0 && Start == 0){
Check = 1
}

if(Said[1,string]:lower() == "-mode" && Start == 0){
Mode = Said[2,string]:toNumber() 
printColor(vec(0,255,0),"[Info] ",vec(255,255,255)," Mode Changed To ",vec(0,0,255),Mode)   
}

if(Said[1,string]:lower() == "-clear"){
Screen[1048574]= 0  # Hardware Clear screen
printColor(vec(0,255,0),"[Info] ",vec(255,255,255)," Screen Cleared!")
} 

if(Said[1,string]:lower() == "-cancel" && Start == 1){
printColor(vec(0,255,0),"[Info] ",vec(255,255,255)," Image Loading Canceled!")
reset() ## Cancels it =P
} 
   
}

if(changed(Start) & Start){
httpRequest("http://failcakemidi.uphero.com/img_"+Name)   
Load = 1
setName("FailCake HTTP Image Parser V1 [Loading]")
}


if(changed(Check) & Check == 1){
httpRequest("http://failcakemidi.uphero.com")    
}

if(Check == 1){  
if(httpClk())    
{
Datass = httpData()
ListData = Datass:explode("\n")
Check = 2        
}
    
}

if(Check == 2){
printList(ListData)
Check = 0   
}



if(Load == 1){
    
if(httpClk()){
Data = httpData()

if(Data != ""){
ImageData = Data:explode("\n")
Sd = ImageData[ImageData:count()-1,string]

if(Sd == "K"){
Load = 2

Screen[1048569]= 1 # Set Color Mode
Screen[1048574]= 0  # Hardware Clear screen
TmpData = ImageData[1,string]:explode(" ") 
X = Y = 1

setName("FailCake HTTP Image Parser V1 [Loading Resolution]")
printColor(vec(132,0,132),"[Working] ",vec(255,255,255)," Image Found! Loading...")

}else{
Load = 0
Index = 0
Start = 0
Address = 0
Pixels:clear()
TmpData:clear()
setName("FailCake HTTP Image Parser V1")
printColor(vec(255,0,0),"[Error] ",vec(255,255,255)," Image Not Found!")
}

}
}
    
}




if(Load == 2){    
Res = ImageData[ImageData:count(),string]:explode(" ")

if(Res[1,string] == "D"){
ResX = Res[2,string]:toNumber()
ResY = Res[3,string]:toNumber()

if(ResX != ResY){
    
if(ResX < ResY){
if(Mode == 1){
ResX = ResY   
}else{
ResY = ResX
}
}elseif(ResX > ResY){
if(Mode == 1){
ResY = ResX   
}else{
ResX = ResY
}
}
    
}


Screen[1048572] = ResX # Set Vertical Resolution
Screen[1048573] = ResY # Set Horizontal Resolution
printColor(vec(132,0,132),"[Working] ",vec(255,255,255)," Screen Resolution Ajusted!")
Load = 3
setName("FailCake HTTP Image Parser V1 [Parsing Image]")  
}else{
printColor(vec(255,0,0),"[Error] ",vec(255,255,255)," Screen Resolution Not Found!")
Load = 0
Start = 0
setName("FailCake HTTP Image Parser V1")
}

}




if(Load == 3){ 
    
while(perf()){ 
    
        Colors = TmpData[X, string]:explode(",")
        
        R = Colors[1,string]:toNumber()
        G = Colors[2,string]:toNumber()
        B = Colors[3,string]:toNumber()
        
        Percent = Y*360/ResY
        Percent2 = Y*100/ResY
        setName("FailCake HTTP Image Parser V1 [Parsing Image][ "+floor(Percent2,2)+"% ]") 
        holoAng(1,E:toWorld(ang(0,Percent,0)))   
        holoAng(3,E:toWorld(ang(0,-Percent,0)))
           
        Pixels:pushTable(table(R,G,B))
        
        X++
        
        if (X > ResX)
        {
            X = 1            
            Y++
            
            TmpData = ImageData[Y, string]:explode(" ")
                        
            if (Y > ResY)
            {
                printColor(vec(132,0,132),"[Working] ",vec(255,255,255)," Done Parsing! Now Printing!")
                
                #free up the memory as we don't need it anymore
                ImageData:clear()
                TmpData:clear()   
                Load = 4
                setName("FailCake HTTP Image Parser V1 [Printing Image]")  
                break
            }   
        }       
}

}

if (Load == 4)
{
    
    while (perf())
    {
        Pixel             = Pixels[Index+1, table]
        Address           = Index*3
        Screen[Address]   = Pixel[1,number]
        Screen[Address+1] = Pixel[2,number]
        Screen[Address+2] = Pixel[3,number]
                
        Index++
         
        
        Percent = Index*360/(ResX*ResY)
        Percent2 = Index*100/(ResX*ResY)
        
        setName("FailCake HTTP Image Parser V1 [Printing Image][ "+floor(Percent2,2)+"% ]") 
        holoAng(1,E:toWorld(ang(0,-Percent,0)))   
        holoAng(3,E:toWorld(ang(0,Percent,0)))
        
        if (Index > ResX*ResY)
        {
            printColor(vec(0,255,0),"[Info] ",vec(255,255,255)," Image Printed!")
            setName("FailCake HTTP Image Parser V1") 
            Load = 0
            Index = 0
            Start = 0
            Address = 0
            Pixels:clear()
            TmpData:clear()
            break
        }
    }
}


