[Scummvm-cvs-logs] SF.net SVN: scummvm:[42491] scummvm/trunk/engines/agos

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Tue Jul 14 23:46:23 CEST 2009


Revision: 42491
          http://scummvm.svn.sourceforge.net/scummvm/?rev=42491&view=rev
Author:   fingolfin
Date:     2009-07-14 21:46:23 +0000 (Tue, 14 Jul 2009)

Log Message:
-----------
AGOS: Cleanup PN code a bit, trying to make sense out of it

Modified Paths:
--------------
    scummvm/trunk/engines/agos/agos.h
    scummvm/trunk/engines/agos/script_pn.cpp

Modified: scummvm/trunk/engines/agos/agos.h
===================================================================
--- scummvm/trunk/engines/agos/agos.h	2009-07-14 19:32:25 UTC (rev 42490)
+++ scummvm/trunk/engines/agos/agos.h	2009-07-14 21:46:23 UTC (rev 42491)
@@ -1265,8 +1265,8 @@
 
 #ifdef ENABLE_PN
 class AGOSEngine_PN : public AGOSEngine {
-	struct stackframe {
-		struct stackframe *nextframe;
+	struct StackFrame {
+		StackFrame *nextframe;
 		int16 flag[6];
 		int16 param[8];
 		int16 classnum;
@@ -1276,7 +1276,7 @@
 		int16 linenum;
 		int16 process;
 		jmp_buf *savearea;
-		stackframe() { memset(this, 0, sizeof(*this)); }
+		StackFrame() { memset(this, 0, sizeof(*this)); }
 	};
 
 
@@ -1284,6 +1284,7 @@
 	void demoSeq();
 	void introSeq();
 	void setupBoxes();
+	int readfromline();
 public:
 	AGOSEngine_PN(OSystem *system);
 	~AGOSEngine_PN();
@@ -1297,10 +1298,10 @@
 	int actCallD(int n);
 
 	void opn_opcode00();
-	void opn_opcode01();
-	void opn_opcode02();
-	void opn_opcode03();
-	void opn_opcode04();
+	void opn_add();
+	void opn_sub();
+	void opn_mul();
+	void opn_div();
 	void opn_opcode05();
 	void opn_opcode06();
 	void opn_opcode07();
@@ -1313,10 +1314,10 @@
 	void opn_opcode14();
 	void opn_opcode15();
 	void opn_opcode16();
-	void opn_opcode17();
-	void opn_opcode18();
-	void opn_opcode19();
-	void opn_opcode20();
+	void opn_lt();
+	void opn_gt();
+	void opn_eq();
+	void opn_neq();
 	void opn_opcode21();
 	void opn_opcode22();
 	void opn_opcode23();
@@ -1357,14 +1358,14 @@
 	void opn_opcode62();
 	void opn_opcode63();
 
-	stackframe *_stackbase;
+	StackFrame *_stackbase;
 
 	byte *_dataBase, *_textBase;
 	uint32 _dataBaseSize, _textBaseSize;
 
 	HitArea _invHitAreas[45];
 
-        char _buffer[80];
+	char _buffer[80];
 	char _inputline[61];
 	char _saveFile[20];
 	char _sb[80];
@@ -1390,7 +1391,7 @@
 	uint16 _objects;
 	int16 _objectCountS;
 
-        int16 _bp;
+	int16 _bp;
 	int16 _xofs;
 	int16 _havinit;
 	uint16 _seed;

Modified: scummvm/trunk/engines/agos/script_pn.cpp
===================================================================
--- scummvm/trunk/engines/agos/script_pn.cpp	2009-07-14 19:32:25 UTC (rev 42490)
+++ scummvm/trunk/engines/agos/script_pn.cpp	2009-07-14 21:46:23 UTC (rev 42491)
@@ -36,11 +36,11 @@
 	static const OpcodeEntryPN opcodes[] = {
 		/* 00 */
 		OPCODE(opn_opcode00),
-		OPCODE(opn_opcode01),
-		OPCODE(opn_opcode02),
-		OPCODE(opn_opcode03),
+		OPCODE(opn_add),
+		OPCODE(opn_sub),
+		OPCODE(opn_mul),
 		/* 04 */
-		OPCODE(opn_opcode04),
+		OPCODE(opn_div),
 		OPCODE(opn_opcode05),
 		OPCODE(opn_opcode06),
 		OPCODE(opn_opcode07),
@@ -56,11 +56,11 @@
 		OPCODE(opn_opcode15),
 		/* 16 */
 		OPCODE(opn_opcode16),
-		OPCODE(opn_opcode17),
-		OPCODE(opn_opcode18),
-		OPCODE(opn_opcode19),
+		OPCODE(opn_lt),
+		OPCODE(opn_gt),
+		OPCODE(opn_eq),
 		/* 20 */
-		OPCODE(opn_opcode20),
+		OPCODE(opn_neq),
 		OPCODE(opn_opcode21),
 		OPCODE(opn_opcode22),
 		OPCODE(opn_opcode23),
@@ -125,10 +125,11 @@
 	(this->*op) ();
 }
 
-#define readfromline() (_linct-- ? (int)*_workptr++ : readoverr())
-
-int readoverr() {
-	error("readfromline: Internal Error - Line Over-run");
+int AGOSEngine_PN::readfromline() {
+	if (!_linct)
+		error("readfromline: Internal Error - Line Over-run");
+	_linct--;
+	return *_workptr++;
 }
 
 // -----------------------------------------------------------------------
@@ -142,30 +143,30 @@
 	setScriptReturn(true);
 }
 
-void AGOSEngine_PN::opn_opcode01() {
+void AGOSEngine_PN::opn_add() {
 	uint8 *str = _workptr;
 	int32 sp = varval() + varval();
 	_variableArray[12] = sp % 65536;
 	_variableArray[13] = sp / 65536;
 	if (sp > 65535)
-		sp=65535;
+		sp = 65535;
 	writeval(str, (int)sp);
 	setScriptReturn(true);
 }
 
-void AGOSEngine_PN::opn_opcode02() {
+void AGOSEngine_PN::opn_sub() {
 	uint8 *str = _workptr;
 	int32 sp = varval();
 	sp -= varval();
 	_variableArray[12] = sp % 65536;
 	_variableArray[13] = sp / 65536;
-	if(sp < 0)
+	if (sp < 0)
 		sp = 0;
 	writeval(str, (int)sp);
 	setScriptReturn(true);
 }
 
-void AGOSEngine_PN::opn_opcode03() {
+void AGOSEngine_PN::opn_mul() {
 	uint8 *str = _workptr;
 	int32 sp = varval() * varval();
 	_variableArray[12] = sp % 65536;
@@ -176,12 +177,12 @@
 	setScriptReturn(true);
 }
 
-void AGOSEngine_PN::opn_opcode04() {
+void AGOSEngine_PN::opn_div() {
 	uint8 *str = _workptr;
 	int32 sp = varval();
 	int32 sp2 = varval();
 	if (sp2 == 0)
-		error("Division by 0");
+		error("opn_div: Division by 0");
 	sp = sp / sp2;
 	_variableArray[12] = sp % 65536;
 	_variableArray[13] = sp / 65536;
@@ -234,7 +235,7 @@
 	char bf[8];
 	int a = 0;
 	sprintf(bf,"%d", varval());
-	while(bf[a])
+	while (bf[a])
 		pcf(bf[a++]);
 	setScriptReturn(true);
 }
@@ -243,7 +244,7 @@
 	char bf[8];
 	int a = 0;
 	sprintf(bf,"%d", varval());
-	while(bf[a])
+	while (bf[a])
 		pcf(bf[a++]);
 	pcf((uint8)'\n');
 	setScriptReturn(true);
@@ -271,25 +272,25 @@
 	setScriptReturn((sp >= 0 && sp <= 4));
 }
 
-void AGOSEngine_PN::opn_opcode17() {
+void AGOSEngine_PN::opn_lt() {
 	int16 v1 = varval();
 	int16 v2 = varval();
 	setScriptReturn(v1 < v2);
 }
 
-void AGOSEngine_PN::opn_opcode18() {
+void AGOSEngine_PN::opn_gt() {
 	int16 v1 = varval();
 	int16 v2 = varval();
 	setScriptReturn(v1 > v2);
 }
 
-void AGOSEngine_PN::opn_opcode19() {
+void AGOSEngine_PN::opn_eq() {
 	int16 v1 = varval();
 	int16 v2 = varval();
 	setScriptReturn(v1 == v2);
 }
 
-void AGOSEngine_PN::opn_opcode20() {
+void AGOSEngine_PN::opn_neq() {
 	int16 v1 = varval();
 	int16 v2 = varval();
 	setScriptReturn(v1 != v2);
@@ -395,7 +396,8 @@
 	char bf[60];
 	int a, slot;
 
-	if ((a = varval()) > 2) {
+	a = varval();
+	if (a > 2) {
 		setScriptReturn(true);
 		return;
 	}
@@ -451,10 +453,8 @@
 }
 
 void AGOSEngine_PN::opn_opcode36() {
-	int ct = 0;
-	while (ct < _dataBase[57] + 1)
-		_wordcp[ct++] = 0;
-	ct = 1;
+	for (int i = 0; i < _dataBase[57] + 1; ++i)
+		_wordcp[i] = 0;
 	if (isspace(*_inpp))
 		while ((*_inpp) && (isspace(*_inpp)))
 			_inpp++;
@@ -468,6 +468,8 @@
 		setScriptReturn(true);
 		return;
 	}
+
+	int ct = 1;
 	while ((*_inpp != '.') && (*_inpp != ',') && (!isspace(*_inpp)) && (*_inpp != '\0') &&
 		(*_inpp!='"')) {
 		if (ct < _dataBase[57])
@@ -734,37 +736,38 @@
 	int a;
 	int b;
 
-	if ((a = readfromline()) < 247) {
+	a = readfromline();
+	if (a < 247) {
 		return a;
 	}
 
 	switch (a) {
 		case 249:
 			b = readfromline();
-			return((int)(b + 256 * readfromline()));
+			return (int)(b + 256 * readfromline());
 			break;
 		case 250:
-			return(readfromline());
+			return readfromline();
 		case 251:
-			return((int)_variableArray[varval()]);
+			return (int)_variableArray[varval()];
 		case 252:
 			b = varval();
-			return((int)_dataBase[_quickptr[0] + b * _quickshort[0] + varval()]);
+			return (int)_dataBase[_quickptr[0] + b * _quickshort[0] + varval()];
 		case 254:
 			b = varval();
-			return((int)_dataBase[_quickptr[3] + b * _quickshort[2] + varval()]);
+			return (int)_dataBase[_quickptr[3] + b * _quickshort[2] + varval()];
 		case 247:
 			b = varval();
-			return((int)getptr(_quickptr[11] + (b * _quickshort[4]) + (2 * varval())));
+			return (int)getptr(_quickptr[11] + (b * _quickshort[4]) + (2 * varval()));
 		case 248:
 			b = varval();
-			return((int)getptr(_quickptr[12] + (b * _quickshort[5]) + (2 * varval())));
+			return (int)getptr(_quickptr[12] + (b * _quickshort[5]) + (2 * varval()));
 		case 253:
 			b = varval();
-			return(bitextract((int32)_quickptr[1] + b * _quickshort[1], varval()));
+			return bitextract((int32)_quickptr[1] + b * _quickshort[1], varval());
 		case 255:
 			b = varval();
-			return(bitextract((int32)_quickptr[4] + b * _quickshort[3], varval()));
+			return bitextract((int32)_quickptr[4] + b * _quickshort[3], varval());
 		default:
 			error("VARVAL : Illegal code %d encountered", a);
 	}
@@ -837,7 +840,7 @@
 	addstack(-1);
 	funccpy(pf);
 	setposition(n, 0);
-	return(doline(1));
+	return doline(1);
 }
 
 int AGOSEngine_PN::doaction() {
@@ -866,7 +869,10 @@
 	if (mybuf == NULL)
 		error("doline: Out of memory - stack overflow");
 
-	if ((x = setjmp(*mybuf)) > 0) {
+	x = setjmp(*mybuf);
+	// Looking at the longjmp calls below, x can be -1, 1 or 2
+	// (and of course 0 when it returns directly, as always).
+	if (x > 0) {
 		dumpstack();
 		_cjmpbuff = ljmpbuff;
 		free((char *)mybuf);
@@ -882,25 +888,25 @@
 	if (needsave)
 		_stackbase->savearea = mybuf;
 
-nln:	_linct = ((*_linebase) & 127) - 1;
-	_workptr = _linebase + 1;
-	if (*_linebase > 127) {
-		x = varval();
-		if (x != (int)_variableArray[1])
-			goto skipln;
-	}
+	do {
+		_linct = ((*_linebase) & 127) - 1;
+		_workptr = _linebase + 1;
+		if (*_linebase > 127) {
+			x = varval();
+			if (x != (int)_variableArray[1])
+				goto skipln;
+		}
 
 carryon:
-	do {
-		x = doaction();
-	} while (x && !shouldQuit());
+		do {
+			x = doaction();
+		} while (x && !shouldQuit());
 
-skipln:	_linebase += 127 & *_linebase;
-	_linembr++;
+skipln:
+		_linebase += 127 & *_linebase;
+		_linembr++;
+	} while (!shouldQuit());
 
-	if (!shouldQuit())
-		goto nln;
-
 	return 0;
 }
 
@@ -951,12 +957,8 @@
 }
 
 void AGOSEngine_PN::funccpy(int *store) {
-	int a = 0;
-	int b = 24;
-
-	while (a < 8) {
-		_variableArray[b++] = *store++;
-		a++;
+	for (int i = 24; i < 32; ++i) {
+		_variableArray[i] = *store++;
 	}
 }
 
@@ -990,23 +992,28 @@
 int AGOSEngine_PN::setposition(int process, int line) {
 	uint8 *ourptr;
 	int np;
-	int ct = 0;
-	ourptr = _dataBase + getlong(_quickptr[6] + 3L * process);
+	int ct;
+
+	ourptr = _dataBase + getlong(_quickptr[6] + 3 * process);
 	np = *ourptr++;
-	while (ct < line) {
+	for (ct = 0; ct < line; ++ct) {
 		ourptr += (127 & *ourptr);
-		ct++;
 	}
-x1:	_linebase = ourptr;
-	_linct = (127 & (*ourptr)) - 1;
-	if (*ourptr++ > 127) {
+
+	while (true) {
+		_linebase = ourptr;
+		_linct = (127 & *ourptr) - 1;
+		if (*ourptr++ <= 127)
+			break;
+
 		ct = varval();
-		if (ct != (int)_variableArray[1]) {
-			ourptr += _linct - 1;
-			line++;
-			goto x1;
-		}
+		if (ct == (int)_variableArray[1])
+			break;
+
+		ourptr += _linct - 1;
+		line++;
 	}
+
 	_linembr = line;
 	_procnum = process;
 	_variableArray[0] = process;
@@ -1035,22 +1042,20 @@
 // -----------------------------------------------------------------------
 
 void AGOSEngine_PN::addstack(int type) {
-	struct stackframe *a;
-	int pt, ct = 0;
+	StackFrame *a;
+	int i;
 
-	a = (struct stackframe *)malloc(sizeof(struct stackframe));
+	a = (StackFrame *)malloc(sizeof(StackFrame));
 	if (a == NULL)
 		error("addstack: Out of memory - stack overflow");
 
 	a->nextframe = _stackbase;
 	_stackbase = a;
-	pt = 0;
-	while (ct < 6)
-		a->flag[ct++] = _variableArray[pt++];
-	ct = 0;
-	pt = 24;
-	while (ct < 8)
-		a->param[ct++] = _variableArray[pt++];
+
+	for (i = 0; i < 6; ++i)
+		a->flag[i] = _variableArray[i];
+	for (i = 0; i < 8; ++i)
+		a->param[i] = _variableArray[24 + i];
 	a->classnum = type;
 	a->ll = _linct;
 	a->linenum = _linembr;
@@ -1060,7 +1065,7 @@
 }
 
 void AGOSEngine_PN::dumpstack() {
-	struct stackframe *a;
+	StackFrame *a;
 
 	if (_stackbase == NULL)
 		error("dumpstack: Stack underflow or unknown longjmp");
@@ -1071,7 +1076,7 @@
 }
 
 void AGOSEngine_PN::junkstack() {
-	struct stackframe *a;
+	StackFrame *a;
 
 	if (_stackbase == NULL)
 		error("junkstack: Stack underflow or unknown longjmp");
@@ -1084,7 +1089,7 @@
 }
 
 void AGOSEngine_PN::popstack(int type) {
-	int a = 0, b;
+	int i;
 
 	while ((_stackbase != NULL) && (_stackbase->classnum != type))
 		junkstack();
@@ -1097,13 +1102,10 @@
 	_workptr = _stackbase->linpos;
 	_procnum = _stackbase->process;
 	_linembr = _stackbase->linenum;
-	b = 0;
-	while (a < 6)
-		_variableArray[b++] = _stackbase->flag[a++];
-	b = 24;
-	a = 0;
-	while (a < 8)
-		_variableArray[b++] = _stackbase->param[a++];
+	for (i = 0; i < 6; ++i)
+		_variableArray[i] = _stackbase->flag[i];
+	for (i = 0; i < 8; ++i)
+		_variableArray[24 + i] = _stackbase->param[i];
 }
 
 } // End of namespace AGOS


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list