aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/act.cpp
blob: 13ca56aa273145ea11661f8f19ea7a1fd9482e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/alan3/act.h"
#include "glk/alan3/alt_info.h"
#include "glk/alan3/exe.h"
#include "glk/alan3/lists.h"
#include "glk/alan3/msg.h"
#include "glk/alan3/output.h"
#include "glk/alan3/syserr.h"

namespace Glk {
namespace Alan3 {

/*----------------------------------------------------------------------*/
static void executeCommand(CONTEXT, int verb, Parameter parameters[]) {
	static AltInfo *altInfos = NULL; /* Need to survive lots of different exits...*/
	int altIndex;
	bool flag;

	/* Did we leave anything behind last time... */
	if (altInfos != NULL)
		free(altInfos);

	altInfos = findAllAlternatives(verb, parameters);

	FUNC2(anyCheckFailed, flag, altInfos, EXECUTE_CHECK_BODY_ON_FAIL)
	if (flag)
		return;

	/* Check for anything to execute... */
	if (!anythingToExecute(altInfos))
		CALL1(error, M_CANT0)

	/* Now perform actions! First try any BEFORE or ONLY from inside out */
	for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) {
		if (altInfos[altIndex].alt != 0) // TODO Can this ever be NULL? Why?
			if (altInfos[altIndex].alt->qual == (Aword)Q_BEFORE
			        || altInfos[altIndex].alt->qual == (Aword)Q_ONLY) {
				FUNC1(executedOk, flag, &altInfos[altIndex])
				if (!flag)
					CALL0(abortPlayerCommand)
				if (altInfos[altIndex].alt->qual == (Aword)Q_ONLY)
					return;
			}
	}

	/* Then execute any not declared as AFTER, i.e. the default */
	for (altIndex = 0; !altInfos[altIndex].end; altIndex++) {
		if (altInfos[altIndex].alt != 0) {
			if (altInfos[altIndex].alt->qual != (Aword)Q_AFTER) {
				FUNC1(executedOk, flag, &altInfos[altIndex])
				if (!flag)
					CALL0(abortPlayerCommand)
			}
		}
	}

	/* Finally, the ones declared as AFTER */
	for (altIndex = lastAltInfoIndex(altInfos); altIndex >= 0; altIndex--) {
		if (altInfos[altIndex].alt != 0) {
			FUNC1(executedOk, flag, &altInfos[altIndex])
			if (!flag)
				CALL0(abortPlayerCommand)
		}
	}
}


/*======================================================================

  action()

  Execute the command. Handles acting on multiple items
  such as ALL, THEM or lists of objects.

*/
void action(CONTEXT, int verb, Parameter parameters[], Parameter multipleMatches[]) {
	int multiplePosition;
	char marker[10];

	multiplePosition = findMultiplePosition(parameters);
	if (multiplePosition != -1) {
		sprintf(marker, "($%d)", multiplePosition + 1); /* Prepare a printout with $1/2/3 */
		for (int i = 0; !isEndOfArray(&multipleMatches[i]); i++) {
			copyParameter(&parameters[multiplePosition], &multipleMatches[i]);
			setGlobalParameters(parameters); /* Need to do this here since the marker use them */
			output(marker);

			// WARNING: The original did a temporary grabbing of the returnLabel setjmp point here.
			// Which is why I manually check the context return instead of using the CALL macro
			executeCommand(context, verb, parameters);
			if (context._break && context._label.hasPrefix("return"))
				context._break = false;
			if (context._break)
				return;
			
			if (multipleMatches[i + 1].instance != EOD)
				para();
		}

		parameters[multiplePosition].instance = 0;
	} else {
		setGlobalParameters(parameters);
		CALL2(executeCommand, verb, parameters)
	}
}

} // End of namespace Alan3
} // End of namespace Glk