Skip to content

Commit

Permalink
Finish reworking the network high score code to be independent of BuG…
Browse files Browse the repository at this point in the history
…S itself so it can be put into its own library.
  • Loading branch information
jeremysrand committed Jun 12, 2023
1 parent 2f53df9 commit 6afbf4e
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 149 deletions.
28 changes: 14 additions & 14 deletions BuGS/global.macros
Original file line number Diff line number Diff line change
Expand Up @@ -224,71 +224,71 @@ _dirtyNonGameTile_skip&SYSCNT anop

macro
_globalHighScoreRow &nthScore
ldy #2+&nthScore*SETTINGS_HIGH_SCORE_SIZE
lda highScoreResponse,y
ldy #&nthScore*SETTINGS_HIGH_SCORE_SIZE
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

lda #TILE_EMPTY
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile

iny
lda highScoreResponse,y
lda globalHighScores,y
jsl asciiToTileType
jsl setGameTile
mend
Expand Down
1 change: 0 additions & 1 deletion BuGS/globals.s
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ collisionAddr dc i2'0'
numSegments dc i2'0'
displayGlobalHighScores dc i2'0'
shouldQuit dc i2'1'
scoreIndex dc i2'0'


tileJumpTable dc a4'solid0'
Expand Down
117 changes: 114 additions & 3 deletions BuGS/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

/* Defines and macros */

#define GLOBAL_SCORE_REFRESH_TIME (15 * 60 * 60)

#define TOOLFAIL(string) \
if (toolerror()) SysFailMgr(toolerror(), (Pointer)"\p" string "\n\r Error Code -> $");

Expand All @@ -38,6 +40,11 @@ unsigned int randomSeed;
// This symbol is used also from assembly directly so be careful with it...
char globalScoreInfo[GAME_NUM_TILES_WIDE + 1];

tNSGSHighScores globalHighScores;
Boolean hasGlobalHighScores = FALSE;
Word globalScoreAge = 0;
unsigned int scoreIndex = 0;


/* Implementation */

Expand All @@ -50,7 +57,7 @@ word randomMushroomOffset(void)
}


void uploadSpin(int val)
static void uploadSpin(int val)
{
switch (val)
{
Expand All @@ -73,7 +80,7 @@ void uploadSpin(int val)
}


void scorePosition(unsigned int position, unsigned int numberOfScores)
static void scorePosition(unsigned int position, unsigned int numberOfScores)
{
int i;

Expand All @@ -87,16 +94,118 @@ void scorePosition(unsigned int position, unsigned int numberOfScores)
for (i = 6 * 60; i > 0; i--) {
waitForVbl();
}

globalScoreAge = 0;
}


void showConnectionString(BOOLEAN display)
static void showConnectionString(BOOLEAN display)
{
if (display)
displayConnectionString();
}


static char hexDigitToAscii(Word digit)
{
digit &= 0xf;
if (digit < 10)
return '0' + digit;

if (digit > 15)
return 'X';

return 'A' + digit - 10;
}


static void displayString(Word row, char * string)
{
strcpy(&(globalHighScores.score[row].scoreText[2]), string);
}

static void displayNetworkError(char * line1, char * line2, unsigned int errorCode)
{
Word row;
Word column;

for (row = 0; row < sizeof(globalHighScores.score) / sizeof(globalHighScores.score[0]); row++) {
for (column = 0;
column < sizeof(globalHighScores.score[0].scoreText) / sizeof(globalHighScores.score[0].scoreText[0]);
column++) {
globalHighScores.score[row].scoreText[column] = ' ';
}
for (column = 0;
column < sizeof(globalHighScores.score[0].who) / sizeof(globalHighScores.score[0].who[0]);
column++) {
globalHighScores.score[row].who[column] = ' ';
}
}

displayString(1, "NETWORK");
displayString(2, "PROBLEM");

displayString(4, line1);
displayString(5, line2);

globalHighScores.score[7].scoreText[0] = 'C';
globalHighScores.score[7].scoreText[1] = 'O';
globalHighScores.score[7].scoreText[2] = 'D';
globalHighScores.score[7].scoreText[3] = 'E';
globalHighScores.score[7].scoreText[4] = ':';
globalHighScores.score[7].scoreText[5] = ' ';
globalHighScores.score[7].scoreText[6] = hexDigitToAscii(errorCode >> 12);
globalHighScores.score[7].scoreText[7] = hexDigitToAscii(errorCode >> 8);
globalHighScores.score[7].scoreText[8] = hexDigitToAscii(errorCode >> 4);
globalHighScores.score[7].scoreText[9] = hexDigitToAscii(errorCode);

hasGlobalHighScores = TRUE;
globalScoreAge = 0;
}

static void displayError(tNSGSErrorType errorType, unsigned int errorCode)
{
switch (errorType) {
case NSGS_CONNECT_ERROR:
displayNetworkError("CONNECT", "FAILED", errorCode);
break;

case NSGS_LOOKUP_ERROR:
displayNetworkError("LOOKUP", "FAILED", errorCode);
break;

case NSGS_SOCKET_ERROR:
displayNetworkError("SOCKET", "ERROR", errorCode);
break;

case NSGS_PROTOCOL_ERROR:
displayNetworkError("PROTOCOL", "FAILED", errorCode);
break;
}
}


static void setHighScores(const tNSGSHighScores * highScores)
{
memcpy(&globalHighScores, highScores, sizeof(globalHighScores));
hasGlobalHighScores = TRUE;
globalScoreAge = GLOBAL_SCORE_REFRESH_TIME;
}


static BOOLEAN shouldRefreshHighScores(void)
{
return globalScoreAge == 0;
}


BOOLEAN sendHighScore(void)
{
const tNSGSHighScore * highScore = getHighScore(scoreIndex / sizeof(tNSGSHighScore));
return NSGS_SendHighScore(highScore->who, highScore->score);
}


int main(void)
{
static tNSGSHighScoreInitParams highScoreInitParams;
Expand Down Expand Up @@ -137,6 +246,8 @@ int main(void)
highScoreInitParams.waitForVbl = waitForVbl;
highScoreInitParams.uploadSpin = uploadSpin;
highScoreInitParams.scorePosition = scorePosition;
highScoreInitParams.displayError = displayError;
highScoreInitParams.setHighScores = setHighScores;

NSGS_InitNetwork(&highScoreInitParams);

Expand Down
Loading

0 comments on commit 6afbf4e

Please sign in to comment.