Avara Plugin Scorer source code

#include 
#include "AvaraScoreInterface.h"

#define kLineLength				50
#define kDefaultChatBufferSize	1024
void UpdateBuffer (void);

typedef enum {
	kFileNotYetSelected,
	kFileSelected,
	kFileCancelled
} fileHandlingStatus;


fileHandlingStatus			fileFlag = kFileNotYetSelected;	
StandardFileReply			theReply;
Str63						thePrompt = "\pLog results to:", theName = "\pAvara Results Log";

Str63						players[6];
Str255						tempNumStr;

char						textBuf[(kLineLength + 1) * 7 + 1] = "Name                Total Shot Grnd Misl OthrLives\r",
								cr[1] = "\r",
								sp[1] = " ",
								livesStr[6] = "ееее+";

short						totalPoints[6], shotPoints[6], grenadePoints[6], missilePoints[6], otherPoints[6]
								, lives[6], playerJoined[6], noPlayers,
								chatBufPtr = 0, chatBufLength = 0;

Handle						chatBuffer = NULL;

unsigned long				currTime, nextTickCount;
							
ScoreInterfaceReasons		lastCall;
								
pascal void	main(ScoreInterfaceRecord *rec)
{

	short					fileRefNum, i;
	long					textSize;
	OSErr					myErr;
	
//	Handle					tempTextHandle;
	
	EnterCodeResource();

	switch (rec->command) {
		case ksiLevelStarted:
			if (!chatBufLength) {
				chatBuffer = NewHandle(kDefaultChatBufferSize);
				if (chatBuffer) {
					chatBufLength = kDefaultChatBufferSize;
				} else {
					chatBufLength = -1;
				}
			} else {
				if (chatBuffer) {
					// write buffer
				}
			}
			for (i=0; i<6; i++) {
				totalPoints[i] = shotPoints[i] = grenadePoints[i] = missilePoints[i] = otherPoints[i] = playerJoined[i] = lives[i] = 0;
			}
			noPlayers = 0;
			break;
		case ksiPlayerIntro:
			lastCall = ksiPlayerIntro;
			if (rec->playerID == -1) {
				ExitCodeResource();
				return;
			}
			textSize = *(rec->playerName) + 1;
			if (textSize > 63) textSize = 63;
			BlockMoveData (rec->playerName,&(players[rec->playerID]), textSize);
			playerJoined[rec->playerID] = 1;
			lives[rec->playerID] = rec->playerLives;
			noPlayers++;
			break;
		case ksiScore:
			lastCall = ksiScore;
			if (rec->playerID == -1) {
				ExitCodeResource();
				return;
			}
			switch (rec->scoreReason) {
				case ksiShotHit:
					shotPoints[rec->playerID] += rec->scorePoints;
					break;
				case ksiGrenadeHit:
					grenadePoints[rec->playerID] += rec->scorePoints;
					break;
				case ksiMissileHit:
					missilePoints[rec->playerID] += rec->scorePoints;
					break;
				default:
					otherPoints[rec->playerID] += rec->scorePoints;
					break;
			}
			totalPoints[rec->playerID] += rec->scorePoints;
			lives[rec->playerID] = rec->playerLives;
			if (TickCount() < nextTickCount) {
				ExitCodeResource();
				return;
			}
			nextTickCount = TickCount() + 60;
			UpdateBuffer();
				textSize = ((noPlayers + 1) * (kLineLength + 1)) + 1;
			if (textSize != GetHandleSize (rec->resultsHandle)) {
				SetHandleSize (rec->resultsHandle, textSize);
			}
			BlockMoveData (&textBuf, *(rec->resultsHandle), textSize);
			rec->resultsChanged = true;
			break;
		case ksiResultsUpdate:
			if (lastCall != ksiResultsUpdate) {
				UpdateBuffer();

				textSize = ((noPlayers + 1) * (kLineLength + 1)) + 1;
				if (textSize != GetHandleSize (rec->resultsHandle)) {
					SetHandleSize (rec->resultsHandle, textSize);
				}
				BlockMoveData (&textBuf, *(rec->resultsHandle), textSize);
				rec->resultsChanged = true;
			} else {				
				rec->resultsChanged = false;
			}
			lastCall = ksiResultsUpdate;
			break;
		case ksiConsoleText:
			if (!chatBufLength) {
				chatBuffer = NewHandle(kDefaultChatBufferSize);
				if (chatBuffer) {
					chatBufLength = kDefaultChatBufferSize;
				} else {
					chatBufLength = -1;
				}
			}
			if (chatBuffer) {
				textSize = *(rec->consoleLine);
				if (chatBufPtr + textSize > chatBufLength) {
					SetHandleSize(chatBuffer, chatBufLength + kDefaultChatBufferSize);
					chatBufLength = GetHandleSize(chatBuffer);
				}
				if (chatBufPtr + textSize < chatBufLength) {
					*(*chatBuffer + chatBufPtr) = *sp;
					BlockMoveData ((rec->consoleLine) + 1, *chatBuffer + chatBufPtr + 1, textSize);
					*(*chatBuffer + chatBufPtr + textSize + 1) = *cr;
					chatBufPtr += textSize + 2;
				}
			}
			break;
		case ksiLevelEnded:
			lastCall = ksiLevelEnded;
			switch (fileFlag) {
				case kFileCancelled:
					ExitCodeResource();
					return;
					break;
				case kFileNotYetSelected:
					StandardPutFile( (ConstStr255Param) &thePrompt, (ConstStr255Param) &theName, &theReply);
						if (!theReply.sfGood) {
						fileFlag = kFileCancelled;
						ExitCodeResource();
						return;
			 		}
					if (!theReply.sfReplacing) {
						myErr = FSpCreate(&theReply.sfFile, 'R*ch', 'TEXT', theReply.sfScript);
						if (myErr != noErr) {
							ExitCodeResource();
							return;
						}
					}
					fileFlag = kFileSelected;

				case kFileSelected:
					myErr = FSpOpenDF (&theReply.sfFile, fsWrPerm, &fileRefNum);
					if (myErr != noErr) {
						ExitCodeResource();
						return;
					}

					myErr = SetFPos (fileRefNum, fsFromLEOF, 0);
					if (myErr != noErr) {
						ExitCodeResource();
						return;
					}

					textSize = *(rec->levelName) + 1;
					BlockMoveData (rec->levelName,&tempNumStr, textSize);
					textSize--;
					/* myErr = */ FSWrite (fileRefNum, &textSize, tempNumStr + 1);
					textSize = 1;
					/* myErr = */ FSWrite (fileRefNum, &textSize, cr);

					GetDateTime (&currTime);
					DateString (currTime, longDate, tempNumStr, nil);
					textSize = *tempNumStr;
					/* myErr = */ FSWrite (fileRefNum, &textSize, tempNumStr + 1);
					textSize = 1;
					/* myErr = */ FSWrite (fileRefNum, &textSize, sp);
					TimeString (currTime, true, tempNumStr, nil);
					textSize = *tempNumStr;
					/* myErr = */ FSWrite (fileRefNum, &textSize, tempNumStr + 1);
					textSize = 1;
					/* myErr = */ FSWrite (fileRefNum, &textSize, cr);

					textSize = chatBufPtr;
					HLock(chatBuffer);
					/* myErr = */ FSWrite (fileRefNum, &textSize, *chatBuffer);
					HUnlock(chatBuffer);
					chatBufPtr = 0;

					UpdateBuffer();
					textSize = ((noPlayers + 1) * (kLineLength + 1)) + 1;
					/* myErr = */ FSWrite (fileRefNum, &textSize, textBuf);
					myErr = FSClose (fileRefNum);
			}
			break;
/* disabled as we leave Avara to update the custom results window.

		case ksiResultsDraw:
			resHand = GetResource ('PICT', 3456);
			if (resHand) {
				DrawPicture ((PicHandle)resHand, &(rec->resultsRect));
				ReleaseResource (resHand);
			}
			break;
*/
	}
	ExitCodeResource();
}

void UpdateBuffer () {

	short					i, j, k, lineStart, strSize;

	Ptr						sourcePtr, destPtr;


	j = 0;
	for (i = 0; i < 6; i++) {
		if (playerJoined[i]) {
			j++;
			lineStart = (kLineLength + 1) * j;
			for (k = 0;k < kLineLength;k++) {
				*(textBuf + lineStart + k) = (char)' ';
			}
			*(textBuf + lineStart + kLineLength) = (char)'\r';
			
			if (*players[i] > 20) {
				strSize = 20;
			} else {
				strSize = *players[i];
			}
			sourcePtr = (char*) players[i] + 1;
			destPtr = textBuf + lineStart;
			BlockMoveData (sourcePtr, destPtr, strSize);

			NumToString ((long)totalPoints[i], tempNumStr);
			sourcePtr = (char*) tempNumStr + 1;
			destPtr = textBuf + lineStart + 25 - *(tempNumStr);
			BlockMoveData (sourcePtr, destPtr, *(tempNumStr));

			NumToString ((long)shotPoints[i], tempNumStr);
			destPtr = textBuf + lineStart + 30 - *(tempNumStr);
			BlockMoveData (sourcePtr, destPtr, *(tempNumStr));

			NumToString ((long)grenadePoints[i], tempNumStr);
			destPtr = textBuf + lineStart + 35 - *(tempNumStr);
			BlockMoveData (sourcePtr, destPtr, *(tempNumStr));

			NumToString ((long)missilePoints[i], tempNumStr);
			destPtr = textBuf + lineStart + 40 - *(tempNumStr);
			BlockMoveData (sourcePtr, destPtr, *(tempNumStr));

			NumToString ((long)otherPoints[i], tempNumStr);
			destPtr = textBuf + lineStart + 45 - *(tempNumStr);
			BlockMoveData (sourcePtr, destPtr, *(tempNumStr));
					
			if (lives[i] > 0) {
				if (lives[i] > 5) {
					strSize = 5;
				} else {
					strSize = lives[i];
				}
				sourcePtr = (char*) livesStr;
				destPtr = textBuf + lineStart + 45;
				BlockMoveData (sourcePtr, destPtr, strSize);
			}

		}			
	}
	*(textBuf + lineStart + (kLineLength + 1)) = (char)'\r';
	*(textBuf + lineStart + (kLineLength + 2)) = (char)0;
}

© John Blackburne, johnb@hk.super.net, 10th June 1997


up to John's Home Page