aboutsummaryrefslogtreecommitdiff
path: root/sword2/startup.cpp
blob: 674ff093197dad0f32954732cc163b79e37dd28b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Copyright (C) 1994-2003 Revolution Software Ltd
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "sword2/sword2.h"		// for CloseGame()
#include "sword2/build_display.h"
#include "sword2/console.h"
#include "sword2/debug.h"
#include "sword2/defs.h"
#include "sword2/header.h"
#include "sword2/interpreter.h"
#include "sword2/logic.h"
#include "sword2/maketext.h"	// for Kill_text_bloc()
#include "sword2/memory.h"
#include "sword2/mouse.h"
#include "sword2/object.h"
#include "sword2/resman.h"
#include "sword2/router.h"
#include "sword2/sound.h"
#include "sword2/speech.h"		// for 'speech_text_bloc_no' - so that speech text can be cleared when running a new start-script
#include "sword2/startup.h"
#include "sword2/sync.h"
#include "sword2/tony_gsdk.h"

namespace Sword2 {

uint32 total_startups = 0;
uint32 total_screen_managers = 0;
uint32 res;

_startup start_list[MAX_starts];

uint32 Init_start_menu(void) {
	// Print out a list of all the start points available.
	// There should be a linc produced file called startup.txt.
	// This file should contain ascii numbers of all the resource game
	// objects that are screen managers.
	// We query each in turn and setup an array of start structures.
	// If the file doesn't exist then we say so and return a 0.

	uint32 end;
	mem *temp;
	uint32 pos = 0;
	uint32 j = 0;
	char *raw_script;
	uint32 null_pc;

	char ascii_start_ids[MAX_starts][7];

	// ok, load in the master screen manager file

	total_startups = 0;	// no starts

	debug(5, "initialising start menu");

	if (!(end = Read_file("startup.inf", &temp, UID_temp))) {
		debug(5, "Init_start_menu cannot open startup.inf");
		return 0;	// meaning no start menu available
	}

	// Ok, we've loaded in the startup.inf file which contains a list of
	// all the files now extract the filenames

	do {
		while (temp->ad[j] != 13) {	// item must have an #0d0a
			ascii_start_ids[total_screen_managers][pos] = temp->ad[j];
			j++;
			pos++;
		}

		// NULL terminate our extracted string
		ascii_start_ids[total_screen_managers][pos] = 0;

		// reset position in current slot between entries
		pos = 0;

		// past the 0a
		j += 2;

		// done another
		total_screen_managers++;

		if (total_screen_managers == MAX_starts) {
			debug(5, "WARNING MAX_starts exceeded!");
			break;
		}
	} while (j <end);

	// using this method the Gode generated resource.inf must have #0d0a
	// on the last entry

	debug(5, "%d screen manager objects", total_screen_managers);

	// Open each object and make a query call. The object must fill in a
	// startup structure. It may fill in several if it wishes - for
	// instance a startup could be set for later in the game where
	// specific vars are set

	for (j = 0; j < total_screen_managers; j++) {
		res = atoi(ascii_start_ids[j]);

		debug(5, "+querying screen manager %d", res);

		// resopen each one and run through the interpretter
		// script 0 is the query request script

		// if the resource number is within range & it's not a null
		// resource (James 12mar97)
		// - need to check in case un-built sections included in
		// start list

		if (res_man.checkValid(res)) {
			debug(5, "- resource %d ok", res);
			raw_script = (char*) res_man.open(res);
			null_pc = 0;
			g_logic.runScript(raw_script, raw_script, &null_pc);
			res_man.close(res);
		} else
			debug(5, "- resource %d invalid", res);
	}

	memory.freeMemory(temp);

	return 1;
}

int32 Logic::fnRegisterStartPoint(int32 *params) {
	// params:	0 id of startup script to call - key
	// 		1 pointer to ascii message

#ifdef _SWORD2_DEBUG
	if (total_startups == MAX_starts)
		error("ERROR: start_list full");

	// +1 to allow for NULL terminator
	if (strlen((char*) params[1]) + 1 > MAX_description)
		error("ERROR: startup description too long");
#endif

	// this objects id
	start_list[total_startups].start_res_id	= res;

	// a key code to be passed to a script via a script var to SWITCH in
	// the correct start
	start_list[total_startups].key = params[0];

	strcpy(start_list[total_startups].description, (char*) params[1]);

	// point to next
	total_startups++;

	return 1;
}

void Con_print_start_menu(void) {
	// the console 'starts' (or 's') command which lists out all the
	// registered start points in the game

	if (!total_startups) {
		Debug_Printf("Sorry - no startup positions registered?\n");

		if (!total_screen_managers)
			Debug_Printf("There is a problem with startup.inf\n");
		else
			Debug_Printf(" (%d screen managers found in startup.inf)\n", total_screen_managers);
	} else {
		for (uint i = 0; i < total_startups; i++)
			Debug_Printf("%d  (%s)\n", i, start_list[i].description);
	}
}

void Con_start(int start) {
	char *raw_script;
	char *raw_data_ad;
	uint32 null_pc;

	if (!total_startups)
		Debug_Printf("Sorry - there are no startups!\n");
	else if (start >= 0 && start < (int) total_startups) {
		// do the startup as we've specified a legal start

		// restarting - stop sfx, music & speech!

		Clear_fx_queue();

		// fade out any music that is currently playing
		g_logic.fnStopMusic(NULL);

		// halt the sample prematurely
		g_sound->unpauseSpeech();
		g_sound->stopSpeech();

		// clean out all resources & flags, ready for a total
		// restart (James24mar97)

		// remove all resources from memory, including player
		// object & global variables

		res_man.removeAll();

		// reopen global variables resource & send address to
		// interpreter - it won't be moving
		g_logic.setGlobalInterpreterVariables((int32 *) (res_man.open(1) + sizeof(_standardHeader)));
		res_man.close(1);

		// free all the route memory blocks from previous game
		router.freeAllRouteMem();

		// if there was speech text, kill the text block
		if (speech_text_bloc_no) {
			fontRenderer.killTextBloc(speech_text_bloc_no);
			speech_text_bloc_no = 0;
		}

		// set the key

		// Open George
		raw_data_ad = (char *) res_man.open(8);
		raw_script = (char *) res_man.open(start_list[start].start_res_id);

		// denotes script to run
		null_pc = start_list[start].key & 0xffff;

		Debug_Printf("Running start %d\n", start);
		g_logic.runScript(raw_script, raw_data_ad, &null_pc);

		res_man.close(start_list[start].start_res_id);

		// Close George
		res_man.close(8);

		// make sure thre's a mouse, in case restarting while
		// mouse not available
		g_logic.fnAddHuman(NULL);
	} else
		Debug_Printf("Not a legal start position\n");
}

} // End of namespace Sword2