
Function DefineSearchVar()

global baseDir$ = "D:"
baseDir$ = "D:\Gamecube roms"
set dir baseDir$

global leftclicked = 0
global firstfile = 0
global filesearch = 0
global fileLevel = 0
global currentfile$
global currentFileType = 0
global prevDir$ = baseDir$
global prevDirArray = 1
global tempPrevDir$ = baseDir$ as string
global baseDir$ = baseDir$
global boxsize# as double float
global fileSize as double integer
global allowMakeBox = 0
global cityMade = 0
global lastArray = 0
global boxmade = 0
global fileMade = 0
global fileNotMade = 0
global fileNotExplored = 0
global FolderSizeCalculated = 0
global folderSize as double integer          `temporary variable to store folder size/height

`global NPCcontrol  : `  0 = auto , 1 = manual

global dim FileName$(2000000,3) as string
FileName$(1,1) = "filename"        `file name
FileName$(1,2) = "fileDir"         `the directory that this file is in
FileName$(1,3) = baseDir$          `the directory that the parent folder of this file is in

global dim FileNum(2000000,8) as double integer
FileNum(1,1) = objNum              `number of object representing this file
FileNum(1,2) = fileLevel
FileNum(1,3) = fileSize
FileNum(1,4) = boxmade             `boxmade = 1 if box reperesenting this file was made
FileNum(1,5) = indexed
FileNum(1,6) = explored
FileNum(1,7) = fileType
FileNum(1,8) = PrevFolderArrayNumber

ENDFUNCTION


'##############################################################################################################################################

Function SearchDriveInitialize()
        CATCH
        perform checklist for files  `Check files current directory              
                 
                 `Register files info in base directory
                  If (checklist quantity() > 0)   

                       FOR t = (1) to (checklist quantity() )
                          
                                                  
                           `Register first file info
                           If (t = 1)  
                               find first
                               currentfile$ = get file name$()
                               `currentpath$ = get dir$()
                               currentpath$ = baseDir$
                               pathExist =  path exist(baseDir$+"/"+currentfile$)
                               `pathExist =  path exist(get dir$()+"/"+currentfile$)
                               currentFileType = get file type()
                              
                              IF (currentfile$ = ".") or (currentfile$ = "..") or (currentfile$ = "System Volume Information") `If current folder is . or .. , skip and open other folder
                                  `Don't do anything
                                   ELSE `if other name, allow set directory
                                       
                                           `Register this file info in array  
                                           If currentFileType = 0 or currentFileType = 1
                                               lastobj = lastobj + 1
                                           Endif
                                                                          
                                           FileNum(lastobj,1) = lastobj
                                           FileNum(lastobj,2) = fileLevel
                                           If (file size(currentfile$) < 0)
                                               fileSize =   2147483647 + (file size(currentfile$)+2147483648)
                                               ELSE
                                               fileSize =  file size(currentfile$)
                                           ENDIF
                                           FileNum(lastobj,3) = fileSize          `file size(currentfile$)
                                          `FileNum(lastobj,4) = lastobj           `prev directory array address
                                           FileNum(lastobj,5) = 1                 `mark this file as indexed
                                           FileNum(lastobj,6) = 0                 `mark this file NOT explored yet
                                           FileNum(lastobj,7) = currentFileType
                                           FileName$(lastobj,1) = currentfile$
                                           FileName$(lastobj,2) = currentpath$
                                           FileName$(lastobj,3) = prevDir$
                                                            
                                ENDIF   `IF NOT (currentfile$ = ".") or (currentfile$ = "..")                            
                                                                               
                               
                               If (checklist quantity()>1)
                                   find next 
                               endif
                              
                           Endif   `If (t = 1) 
                           
                           
                           `Register next files info
                           If (t > 1) 
                               currentfile$ = get file name$()
                               `currentpath$ = get dir$()
                               `newpath$ = currentpath$+currentfile$
                               `pathExist =  path exist (currentpath$+"\"+currentfile$)
                               currentpath$ = baseDir$
                               pathExist =  path exist(baseDir$+"/"+currentfile$)
                               currentFileType = get file type()
                               
                               
                                  IF (currentfile$ = ".") or (currentfile$ = "..") or (currentfile$ = "System Volume Information") `If current folder is . or .. , skip 
                                        `Don't do anything
                                         ELSE `if other name, allow  index file
                                                                                      
                                           
                                                 If currentFileType = 0 or currentFileType = 1
                                                 lastobj = lastobj + 1
                                                 Endif
                                          
                                                 FileNum(lastobj,1) = lastobj
                                                 FileNum(lastobj,2) = fileLevel
                                                 If (file size(currentfile$) <0)
                                                     fileSize =   2147483647 + (file size(currentfile$)+2147483648)
                                                     ELSE
                                                     fileSize =  file size(currentfile$)
                                                 ENDIF
                                                 FileNum(lastobj,3) = fileSize              `file size(currentfile$)
                                                `FileNum(lastobj,4) = lastobj               `prev directory array address
                                                 FileNum(lastobj,5) = 1                     `mark this file as indexed
                                                 FileNum(lastobj,6) = 0                     `mark this file NOT explored yet
                                                 FileNum(lastobj,7) = currentFileType
                                                 FileName$(lastobj,1) = currentfile$
                                                 FileName$(lastobj,2) = currentpath$
                                                 FileName$(lastobj,3) = prevDir$
                                                                                                              
                                  ENDIF    `IF (currentfile$ = ".") or (currentfile$ = "..")
                                        
                                 
                                  If (t < checklist quantity())
                                      find next 
                                  Endif
                                 
                                
                           Endif    `If (t > 1) 
                                                
                           
                                                          
                       NEXT t    `FOR t = (1) to (checklist quantity() )
                                                  
                        `empty checklist           
                        `Set dir baseDir$
                        `Perform checklist for files
                       lastArray = lastObj
                       fileNotMade = lastObj              
                   ENDIF      `If (checklist quantity() > 0)
              ENDCATCH      
              
              fileSearch = 1
                     
ENDFUNCTION 

'##############################################################################################################################################
'##############################################################################################################################################

Function SearchDrive()
    If filesearch = 1
        
        `Check if all file explored
        fileNotExplored = 0
        For y = (endObj +1) to lastArray
            If (FileNum(y,7) = 1) and (FileNum(y,6) = 0)   `if folder not explored, then mark filenotmade as still exist
                fileNotExplored = fileNotExplored + 1
            Endif
        Next y
       `Allow make box after all folders explored
        If  fileNotExplored = 0
            allowMakeBox = 1
            filesearch = 0
            Else
            allowMakeBox = 0
            filesearch = 1
        Endif
        If  fileNotExplored > 0
            fileLevel = fileLevel +1
        Endif
        
        
        
        
        
        IF filesearch = 1
        
            `If  fileNotExplored > 0
            `fileLevel = fileLevel + 1
            `Endif
        
       `Explore folders that was registered in array (will NOT explore unregistered folders)         
        For x = (endObj +1) to lastArray
        
           `If file is a folder   and  unexplored,      then explore the folder 
            If (FileNum(x,7) = 1) and (FileNum(x,6) = 0)
            
            Catch  
                PrevDir$ = FileName$(x,2)
                CD FileName$(x,2)+"\"+FileName$(x,1)
                perform checklist for files
                
            
                 `Register files info
                  If (filesearch = 1) and (checklist quantity() > 0)   

                       FOR t = (1) to (checklist quantity() )
                          
                                                  
                           `Register first file info
                           If (t = 1)  
                               find first
                               currentfile$ = get file name$()
                               currentpath$ = get dir$()
                               pathExist =  path exist(get dir$()+"/"+currentfile$)
                               currentFileType = get file type()
                              
                              `If files are not . or .. or invalid files, register files info in array
                              IF (currentfile$ = ".") or (currentfile$ = "..") or (currentfile$ = "System Volume Information")`If current folder is . or .. , skip and open other folder
                                   `Don't do anything
                                   ELSE `if other name, allow explore and set directory
                                                                             
                                           If currentFileType = 0 or currentFileType = 1
                                               lastobj = lastobj + 1
                                               fileNotMade = fileNotMade + 1
                                           Endif
                               
                                           FileNum(lastobj,1) = lastobj
                                           
                                           FileNum(lastobj,2) = fileLevel
                                          `Calculate real file size 
                                           If (file size(currentfile$) < 0)
                                               fileSize =   2147483647 + (file size(currentfile$)+2147483648)
                                               ELSE
                                               fileSize =  file size(currentfile$)
                                           ENDIF
                                           FileNum(lastobj,3) = fileSize          `file size(currentfile$)
                                           `FileNum(lastobj,4) = boxmade          `boxmade = 1 if box reperesenting this file was made
                                           FileNum(lastobj,5) = 1                 `mark file as indexed
                                           FileNum(lastobj,6) = 0                 `mark this file NOT explored yet
                                           FileNum(lastobj,7) = currentFileType
                                           FileNum(lastobj,8) = x                 `record previous file number in the array
                                           FileName$(lastobj,1) = currentfile$
                                           FileName$(lastobj,2) = currentpath$
                                           FileName$(lastobj,3) = prevDir$
                                                                     
                                ENDIF   `IF NOT (currentfile$ = ".") or (currentfile$ = "..")                            
                                                                                
                               
                               If (checklist quantity()>1)
                                   find next 
                               endif
                           Endif    `If (t = 1) 
                           
                           
                           `Register next files info
                           If (t > 1) and (filesearch = 1) 
                               currentfile$ = get file name$()
                               currentpath$ = get dir$()
                               `newpath$ = currentpath$+currentfile$
                               pathExist =  path exist (currentpath$+"\"+currentfile$)
                               currentFileType = get file type()
                               
                               
                                  IF (currentfile$ = ".") or (currentfile$ = "..") or (currentfile$ = "System Volume Information")`If current folder is . or .. , skip and open other folder
                                        `Don't do anything
                                         ELSE `if other name, allow indexing
                                                                                      
                                           
                                                 If currentFileType = 0 or currentFileType = 1
                                                 lastobj = lastobj + 1
                                                 fileNotMade = fileNotMade + 1
                                                 Endif
                                          
                                                 FileNum(lastobj,1) = lastobj
                                                 
                                                 FileNum(lastobj,2) = fileLevel
                                                 
                                                 If (file size(currentfile$) <0)
                                                     fileSize =   2147483647 + (file size(currentfile$)+2147483648)
                                                     ELSE
                                                     fileSize =  file size(currentfile$)
                                                 ENDIF
                                                 FileNum(lastobj,3) = fileSize `file size(currentfile$)
                                                 `FileNum(lastobj,4) = boxmade          `boxmade = 1 if box reperesenting this file was made
                                                 FileNum(lastobj,5) = 1
                                                 FileNum(lastobj,6) = 0                 `mark this file NOT explored yet
                                                 FileNum(lastobj,7) = currentFileType
                                                 FileNum(lastobj,8) = x                 `record previous file number in the array
                                                 FileName$(lastobj,1) = currentfile$
                                                 FileName$(lastobj,2) = currentpath$
                                                `FileName$(lastobj,3) = prevDir$
                                                                                                              
                                  ENDIF    `IF (currentfile$ = ".") or (currentfile$ = "..")
                                        
                                 
                                  If (t < checklist quantity())
                                      find next 
                                  Endif
                           Endif    `If (t > 1) and (filesearch = 1)
                                                
                           
                                                          
                       NEXT t   `FOR t = (1) to (checklist quantity() )
                       `empty checklist           
                                    
                   ENDIF    `If (filesearch = 1) and (checklist quantity() > 0)
            ENDCATCH
            
            Endif     `If file is a folder and unexplored
            FileNum(x,6) = 1      `mark folder as explored

        NEXT x     `For x = (endObj +1) to lastArray
        lastArray = lastobj
        ENDIF     `If filesearch = 1
        
    ENDIF    `If filesearch =1
                  
                  `Calculate folder sizes
                  `Calculate individual folder sizes
                   IF (filesearch = 0) and (allowMakeBox = 1) and (FolderSizeCalculated = 0)
                   FOR s = (endObj +1) to lastobj
                       IF (FileNum(s,7) = 0)    `If file, then add this file size to previous folder size
                           prevFolder = FileNum(s,8)
                           FileNum(prevFolder,3) = FileNum(prevFolder,3) + FileNum(s,3)
                           remstart
                           For t = (endObj +1) to lastobj
                              `If directory of the folder + folder name = current file directory
                               If (FileName$(t,2)+"\"+FileName$(t,1)) = FileName$(s,2)
                                   `folder size = folder size + file size it contain
                                   FileNum(t,3) = FileNum(t,3) + FileNum(s,3)
                               Endif
                           NEXT
                           remend
                       ENDIF
                   NEXT s
                   
                  `Pass folder sizes to previous folders - start calculation from last array, work up to biggest/base folders
                   FOR s = lastobj to (endObj +1) STEP -1
                       IF (FileNum(s,7) = 1)    `If folder
                           prevFolder = FileNum(s,8)
                           FileNum(prevFolder,3) = FileNum(prevFolder,3) + FileNum(s,3)
                           remstart
                           For t = (endObj +1) to lastobj
                              `If directory of the folder + folder name = current file directory
                               If (FileName$(t,2)+"\"+FileName$(t,1)) = FileName$(s,2)
                                   `folder size = folder size + file size it contain
                                   FileNum(t,3) = FileNum(t,3) + FileNum(s,3)
                               Endif
                           NEXT
                           remend
                       ENDIF
                   NEXT s
                   FolderSizeCalculated = 1
                   ENDIF
                   
ENDFUNCTION
'##############################################################################################################################################

FUNCTION MakeCityWhole()
    

     `Make boxes one by one
               filemade = 0    `set fileMade = 0 to allow box to be made
               `enableLimb = 1   `dont make collision limb for boxes
               If (filemade = 0) and (allowMakeBox = 1) and (FolderSizeCalculated = 1)  and (screen fps() > 10)
                   ```````If (filemade = 0) and (allowMakeBox = 1) `and (mouseClick() = 2)
                   FOR z = (endObj +1 + (lastobj-fileNotMade)) to lastobj
                       
                       If (FileNum(z,4) = 0) and fileMade = 0    `If no box was made for this file, then  made a box
                           
                           If (FileNum(z,7) = 0)    `If file, colour green
                           `boxSize# = sqrt((file size(currentfile$))/1000)
                           boxSize# = sqrt( (FileNum(z,3)) /1000)
                         
                           `get previous folder info - to place the box made
                           FOR x = (endObj +1) to lastobj
                              `If directory of the folder + folder name = current file directory
                               If (FileName$(x,2)+"\"+FileName$(x,1)) = FileName$(z,2)
                                   folderSize =  (sqrt(FileNum(x,3)/1000))/2/2    `use this size to determine height of  folder this file is in
                                   folderLocationY = object position y(x)
                                   folderLocationX = object position x(x)
                                   folderLocationZ = object position z(x)
                               ENDIF
                           NEXT
                                   
                           `makebox( z , z- terrainNum , z- terrainNum  , (boxsize#)+50 , (boxsize#*3)+50, (boxsize#)+50 , 100 , 100, 200 , 150, -20000 + RND(19000) , (((boxsize#*3)+50)*0.5) + (folderSize) + folderLocationY , 0 + RND(20000) )
                           IF FileNum(z,2) > 0    `If filelevel > 0
                               makebox( z , z- terrainNum , z- terrainNum  , (boxsize#)+1 , (boxsize#*3)+1, (boxsize#)+1 , 100 , 100, 200 , 150, folderLocationX - (folderSize*2) + RND(folderSize*4), (((boxsize#*3)+1)*0.5) + (folderSize) + folderLocationY , folderLocationZ - (folderSize*2) + RND(folderSize*4) )
                           ENDIF
                           IF FileNum(z,2) = 0    `If filelevel = 0
                               makebox( z , z- terrainNum , z- terrainNum  , (boxsize#)+1 , (boxsize#*3)+1, (boxsize#)+1 , 100 , 100, 200 , 150, -20000 + RND(40000), (((boxsize#*3)+1)*0.5) + (folderSize) + folderLocationY ,  -20000 + RND(40000) )
                           ENDIF
                           `set shadow shading on z,-1,1000,1
                           Endif
                           
                           
                               
                           If (FileNum(z,7) = 1)    `If folder, colour blue
                          `boxSize# = sqrt((file size(currentfile$))/1000)
                           boxSize# = sqrt( (FileNum(z,3)) /1000)
                           
                          `get previous folder info - to place the box made
                           FOR x = (endObj +1) to lastobj
                              `If directory of the folder + folder name = current file directory
                               If (FileName$(x,2)+"\"+FileName$(x,1)) = FileName$(z,2)
                                   folderSize =  (sqrt(FileNum(x,3)/1000)) /2/2    `use this size to determine height of  folder this file is
                                   folderLocationY = object position y(x)
                                   folderLocationX = object position x(x)
                                   folderLocationZ = object position z(x)
                               ENDIF
                           NEXT
                           `makebox( z , z- terrainNum , z- terrainNum   , (boxsize#)+50 , (boxsize#)+50, (boxsize#)+50 , 100 , 100, 150 , 250, -20000 + RND(19000) , (((boxsize#)+50)*0.5) + (folderSize) + folderLocationY , 0 + RND(20000) )
                           IF FileNum(z,2) > 0    `If filelevel > 0
                               makebox( z , z- terrainNum , z- terrainNum  , (boxsize#)+1 , (boxsize#/2)+1, (boxsize#)+1 , 100 , 100, 150 , 250, folderLocationX + RND(folderSize) , (((boxsize#)+1)*0.5*0.5) + (folderSize) + folderLocationY , folderLocationZ + RND(folderSize) )
                           ENDIF
                           IF FileNum(z,2) = 0    `If filelevel = 0
                               makebox( z , z- terrainNum , z- terrainNum  , (boxsize#)+1 , (boxsize#/2)+1, (boxsize#)+1 , 100 , 100, 150 , 250, -20000 + RND(40000) , (((boxsize#)+1)*0.5*0.5)  ,   -20000 + RND(40000) )
                           ENDIF
                           `set shadow shading on z,-1,1000,1
                           Endif
                           
                           FileNum(z,4) = 1
                           fileNotMade = fileNotMade - 1
                           filemade = 1    `set filemade = 1 to prevent another box being made in one loop
                       Endif
                       
                   NEXT z
                   `filemade = 1
                   Endif
ENDFUNCTION

'##############################################################################################################################################
'##############################################################################################################################################

FUNCTION makeBox( boxObjNum, limbNum , meshNum , width# , height# , depth# , alpha , red , green , blue , x# , y# , z# )
`Make boxes as part of terrain 
        
           `Make new box to attach as limb to terrain object
            make object box boxObjNum, width#, height#, depth#
            `instance object boxObjNum, 400
            color object boxObjNum, RGB(red,green,blue)
            set object diffuse boxObjNum, RGB(red,green,blue)
            ```````set object specular boxObjNum, RGB(red,green,blue)
            set object ambience boxObjNum, RGB(red,green,blue)
            ```````texture object boxObjNum, 1
            set alpha mapping on boxObjNum, alpha
            position object boxObjNum, x#,y#,z#
            set shadow shading on boxObjNum,-1,10000,1
            `hide object boxObjNum
            `Xeno Make Rigid Body Static Box boxObjNum
    
        
           `Make limb from mesh
            `make mesh from object meshNum, boxObjNum
            `add limb terrainNum, limbNum, meshNum
            add limb terrainNum, limbNum, originBox
            scale limb terrainNum, limbNum, 100*width#, 100*height#, 100*depth#
            offset limb terrainNum, limbNum, x#, y#, z#
            `color limb terrainNum, limbNum, RGB(red,green,blue)
            hide limb terrainNum, limbNum
            `delete mesh meshNum
        
        `Set up collision
            make object collision box boxObjNum, -20 -((width#)*0.5), -(height#)*0.5,  -70 -((depth#)*0.50) , 20 + ((width#)*0.5) , (height#)*0.5 , 80+ ((depth#)*0.5) , 0
ENDFUNCTION
'##############################################################################################################################################
