2020-06-11 01:47:00 +00:00
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
* BuGS
|
|
|
|
*
|
|
|
|
* Created by Jeremy Rand on 2020-06-10.
|
|
|
|
* Copyright (c) 2020 Jeremy Rand. All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-06-08 05:15:54 +00:00
|
|
|
#include <stdio.h>
|
2020-07-15 04:32:14 +00:00
|
|
|
#include <stdlib.h>
|
2023-06-08 05:15:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2020-06-11 01:47:00 +00:00
|
|
|
|
2023-06-08 05:15:54 +00:00
|
|
|
#include <orca.h>
|
2020-06-11 01:47:00 +00:00
|
|
|
#include <Memory.h>
|
|
|
|
#include <Locator.h>
|
|
|
|
#include <MiscTool.h>
|
2020-12-09 06:38:03 +00:00
|
|
|
|
2020-06-11 01:47:00 +00:00
|
|
|
#include "main.h"
|
2020-07-15 04:32:14 +00:00
|
|
|
#include "game.h"
|
2023-06-09 00:04:22 +00:00
|
|
|
#include "netScores.h"
|
2023-06-08 05:15:54 +00:00
|
|
|
#include "settings.h"
|
2020-11-05 03:48:18 +00:00
|
|
|
#include "tileData.h"
|
2020-06-11 01:47:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Defines and macros */
|
|
|
|
|
2023-06-12 02:51:21 +00:00
|
|
|
#define GLOBAL_SCORE_REFRESH_TIME (15 * 60 * 60)
|
2023-06-12 03:33:47 +00:00
|
|
|
#define SHUTDOWN_NETWORK_TIMEOUT (2 * 60)
|
|
|
|
#define TCP_CONNECT_TIMEOUT (8 * 60)
|
|
|
|
#define READ_NETWORK_TIMEOUT (5 * 60)
|
|
|
|
#define NETWORK_RETRY_TIMEOUT (3 * 60 * 60)
|
2023-06-12 02:51:21 +00:00
|
|
|
|
2020-06-11 01:47:00 +00:00
|
|
|
#define TOOLFAIL(string) \
|
2023-06-08 05:15:54 +00:00
|
|
|
if (toolerror()) SysFailMgr(toolerror(), (Pointer)"\p" string "\n\r Error Code -> $");
|
2020-06-11 01:47:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Globals */
|
|
|
|
|
2021-06-02 03:13:14 +00:00
|
|
|
unsigned int myUserId;
|
2020-07-19 19:15:45 +00:00
|
|
|
unsigned int randomSeed;
|
2020-06-11 01:47:00 +00:00
|
|
|
|
2022-05-26 02:58:53 +00:00
|
|
|
// This symbol is used also from assembly directly so be careful with it...
|
|
|
|
char globalScoreInfo[GAME_NUM_TILES_WIDE + 1];
|
|
|
|
|
2023-06-12 02:51:21 +00:00
|
|
|
tNSGSHighScores globalHighScores;
|
|
|
|
Boolean hasGlobalHighScores = FALSE;
|
|
|
|
Word globalScoreAge = 0;
|
|
|
|
unsigned int scoreIndex = 0;
|
|
|
|
|
2020-06-11 01:47:00 +00:00
|
|
|
|
|
|
|
/* Implementation */
|
|
|
|
|
|
|
|
|
2020-11-05 03:48:18 +00:00
|
|
|
word randomMushroomOffset(void)
|
2020-11-03 05:11:22 +00:00
|
|
|
{
|
|
|
|
/* We do not put mushrooms in the bottom tile so we subtract the width here to find
|
|
|
|
a tile number above that last line */
|
2020-11-05 03:48:18 +00:00
|
|
|
return (rand() % (NUM_GAME_TILES - GAME_NUM_TILES_WIDE)) * SIZEOF_TILE_INFO;
|
2020-11-03 05:11:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 07:31:18 +00:00
|
|
|
|
2023-06-12 02:51:21 +00:00
|
|
|
static void uploadSpin(int val)
|
2022-05-26 01:50:38 +00:00
|
|
|
{
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
uploadSpin1();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
uploadSpin2();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
uploadSpin3();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
uploadSpin2();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 07:31:18 +00:00
|
|
|
|
2023-06-12 02:51:21 +00:00
|
|
|
static void scorePosition(unsigned int position, unsigned int numberOfScores)
|
2022-05-26 02:58:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sprintf(globalScoreInfo, " %u OF %u SCORES", position, numberOfScores);
|
2023-06-08 05:15:54 +00:00
|
|
|
for (i = (int)strlen(globalScoreInfo); i < sizeof(globalScoreInfo); i++) {
|
2022-05-26 02:58:53 +00:00
|
|
|
globalScoreInfo[i] = ' ';
|
|
|
|
}
|
|
|
|
globalScoreInfo[GAME_NUM_TILES_WIDE] = '\0';
|
|
|
|
displayScorePosition();
|
|
|
|
|
|
|
|
for (i = 6 * 60; i > 0; i--) {
|
|
|
|
waitForVbl();
|
|
|
|
}
|
2023-06-12 02:51:21 +00:00
|
|
|
|
|
|
|
globalScoreAge = 0;
|
2022-05-26 02:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-12 02:51:21 +00:00
|
|
|
static void showConnectionString(BOOLEAN display)
|
2022-05-26 02:58:53 +00:00
|
|
|
{
|
|
|
|
if (display)
|
|
|
|
displayConnectionString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-12 02:51:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-11 01:47:00 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
2023-06-09 00:04:22 +00:00
|
|
|
static tNSGSHighScoreInitParams highScoreInitParams;
|
2020-06-11 01:47:00 +00:00
|
|
|
int event;
|
|
|
|
Ref toolStartupRef;
|
|
|
|
|
2021-06-02 03:13:14 +00:00
|
|
|
myUserId = MMStartUp();
|
2020-06-11 01:47:00 +00:00
|
|
|
TOOLFAIL("Unable to start memory manager");
|
|
|
|
|
|
|
|
TLStartUp();
|
|
|
|
TOOLFAIL("Unable to start tool locator");
|
|
|
|
|
2021-06-02 03:13:14 +00:00
|
|
|
toolStartupRef = StartUpTools(myUserId, refIsResource, TOOL_STARTUP);
|
2020-06-11 01:47:00 +00:00
|
|
|
TOOLFAIL("Unable to start tools");
|
|
|
|
|
2020-06-11 03:51:53 +00:00
|
|
|
CompactMem();
|
2020-06-23 03:45:25 +00:00
|
|
|
/* Allocate $1000 extra before the SHR screen so I can write sprites above the start of the
|
|
|
|
screen without overwriting memory I do not own. */
|
2021-06-02 03:13:14 +00:00
|
|
|
NewHandle(0x9000, myUserId, attrLocked | attrFixed | attrAddr | attrBank, (Pointer)0x011000);
|
2020-06-11 03:51:53 +00:00
|
|
|
TOOLFAIL("Unable to allocate SHR screen");
|
2020-06-11 01:47:00 +00:00
|
|
|
|
2020-07-19 19:15:45 +00:00
|
|
|
randomSeed = (int)time(NULL);
|
|
|
|
if (randomSeed == 0)
|
|
|
|
randomSeed = 1;
|
|
|
|
srand(randomSeed);
|
|
|
|
randInit();
|
2020-07-15 04:32:14 +00:00
|
|
|
|
2020-10-29 03:46:32 +00:00
|
|
|
InitMouse(0);
|
|
|
|
SetMouse(transparent);
|
2020-12-30 04:56:51 +00:00
|
|
|
|
2022-05-26 01:50:38 +00:00
|
|
|
highScoreInitParams.userId = myUserId;
|
2023-06-08 05:15:54 +00:00
|
|
|
highScoreInitParams.scoreServer = (const char *)"\p" NETWORK_SERVER;
|
2022-05-26 01:50:38 +00:00
|
|
|
highScoreInitParams.scorePort = NETWORK_SERVERPORT;
|
|
|
|
highScoreInitParams.secret1 = NETWORK_SERVERSECRET1;
|
|
|
|
highScoreInitParams.secret2 = NETWORK_SERVERSECRET2;
|
2023-06-12 03:33:47 +00:00
|
|
|
highScoreInitParams.shutdownTimeout = SHUTDOWN_NETWORK_TIMEOUT;
|
|
|
|
highScoreInitParams.connectTimeout = TCP_CONNECT_TIMEOUT;
|
|
|
|
highScoreInitParams.readTimeout = READ_NETWORK_TIMEOUT;
|
|
|
|
highScoreInitParams.retryTimeout = NETWORK_RETRY_TIMEOUT;
|
2022-05-26 01:50:38 +00:00
|
|
|
|
2022-05-26 02:58:53 +00:00
|
|
|
highScoreInitParams.displayConnectionString = showConnectionString;
|
2022-05-26 01:50:38 +00:00
|
|
|
highScoreInitParams.waitForVbl = waitForVbl;
|
|
|
|
highScoreInitParams.uploadSpin = uploadSpin;
|
2022-05-26 02:58:53 +00:00
|
|
|
highScoreInitParams.scorePosition = scorePosition;
|
2023-06-12 02:51:21 +00:00
|
|
|
highScoreInitParams.displayError = displayError;
|
|
|
|
highScoreInitParams.setHighScores = setHighScores;
|
2022-05-26 01:50:38 +00:00
|
|
|
|
2023-06-09 00:04:22 +00:00
|
|
|
NSGS_InitNetwork(&highScoreInitParams);
|
2021-06-01 04:35:47 +00:00
|
|
|
|
2020-12-30 04:56:51 +00:00
|
|
|
if (!loadSettings())
|
|
|
|
saveSettings();
|
2020-11-05 03:48:18 +00:00
|
|
|
|
2020-06-11 03:51:53 +00:00
|
|
|
game();
|
2020-06-11 01:47:00 +00:00
|
|
|
|
2023-06-09 00:04:22 +00:00
|
|
|
NSGS_ShutdownNetwork();
|
2021-06-01 04:35:47 +00:00
|
|
|
|
2020-06-11 01:47:00 +00:00
|
|
|
ShutDownTools(refIsHandle, toolStartupRef);
|
|
|
|
TOOLFAIL("Unable to shutdown tools");
|
|
|
|
|
|
|
|
TLShutDown();
|
|
|
|
TOOLFAIL("Unable to shutdown tool locator");
|
|
|
|
|
2021-06-02 03:13:14 +00:00
|
|
|
MMShutDown(myUserId);
|
2020-06-11 01:47:00 +00:00
|
|
|
TOOLFAIL("Unable to shutdown memory manager");
|
|
|
|
}
|