Ohhhh. :-)
Dim Highscore_Name$(10, 2)
Dim Highscore_Level(10, 2)
Dim Highscore_Score(10, 2)
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function checks the player's current score against the high score list and inserts their name into the list if they have achived a new high score.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Update_Highscores()
Local Loop, Loop2
Load_Highscores()
; Loop through the highscore list from the highest score to the lowest.
For Loop = 0 To 9
; If this player's score is greater than the highscore at this location in the list...
If Savegame_Score(GLOBAL_Editing_Savegame, GLOBAL_Game_Mode) > Highscore_Score(Loop, GLOBAL_Game_Mode)
; Move each highscore, starting at this one, down by one, pushing the last one off the list.
For Loop2 = Loop To 8
Highscore_Name$(Loop2+1, GLOBAL_Game_Mode) = Highscore_Name$(Loop2, GLOBAL_Game_Mode)
Highscore_Level(Loop2+1, GLOBAL_Game_Mode) = Highscore_Level(Loop2, GLOBAL_Game_Mode)
Highscore_Score(Loop2+1, GLOBAL_Game_Mode) = Highscore_Score(Loop2, GLOBAL_Game_Mode)
Next
; Add this player's score to the list at this location.
;
; We use the score and level variables instead of the saved score because we want the score in the middle of the level to be saved, not the score
; at the end of the last level. This score will not have the level bonus added. The player only gets that if they complete the level.
Highscore_Name$(Loop, GLOBAL_Game_Mode) = Savegame_Name$(GLOBAL_Editing_Savegame)
Highscore_Level(Loop, GLOBAL_Game_Mode) = Level
Highscore_Score(Loop, GLOBAL_Game_Mode) = Score
; Exit the loop early.
Exit
EndIf
Next
Save_Highscores()
End Function
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function loads the current high score data.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Load_Highscores()
Local Filename$
Local DataFile
Local Loop
; Attempt to open the data file for reading.
; If the operation fails, no data will be loaded, but this will just result in no high scores being displayed, so no biggie.
Filename$ = DATA_PATH$ + "highscores.dat"
DataFile = ReadFile(Filename$)
; Load the data.
For Loop = 0 To 9
Highscore_Name$(Loop, GAME_MODE_ARCADE) = ReadLine$(DataFile)
Highscore_Level(Loop, GAME_MODE_ARCADE) = ReadLine$(DataFile)
Highscore_Score(Loop, GAME_MODE_ARCADE) = ReadLine$(DataFile)
Next
For Loop = 0 To 9
Highscore_Name$(Loop, GAME_MODE_ADVANCED) = ReadLine$(DataFile)
Highscore_Level(Loop, GAME_MODE_ADVANCED) = ReadLine$(DataFile)
Highscore_Score(Loop, GAME_MODE_ADVANCED) = ReadLine$(DataFile)
Next
; Close the data file.
CloseFile DataFile
End Function
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; This function deletes any high score file that exists and then saves a new one with the current high score data.
; --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function Save_Highscores()
Local Filename$
Local DataFile
Local Loop
; Attempt to open the data file for reading.
Filename$ = DATA_PATH$ + "highscores.dat"
If FileType(Filename$) = 1 Then DeleteFile Filename$
DataFile = WriteFile(Filename$)
; Write the data.
For Loop = 0 To 9
WriteLine(DataFile, Highscore_Name$(Loop, GAME_MODE_ARCADE))
WriteLine(DataFile, Highscore_Level(Loop, GAME_MODE_ARCADE))
WriteLine(DataFile, Highscore_Score(Loop, GAME_MODE_ARCADE))
Next
For Loop = 0 To 9
WriteLine(DataFile, Highscore_Name$(Loop, GAME_MODE_ADVANCED))
WriteLine(DataFile, Highscore_Level(Loop, GAME_MODE_ADVANCED))
WriteLine(DataFile, Highscore_Score(Loop, GAME_MODE_ADVANCED))
Next
; Close the data file.
CloseFile DataFile
End Function