aboutsummaryrefslogtreecommitdiff
path: root/sword2/startup.cpp
blob: 43056a1f539c98414e13e18393a3be1f29f83af6 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* 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 <stdio.h>

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

//------------------------------------------------------------------------------------
uint32	total_startups=0;
uint32	total_screen_managers=0;
uint32	res;

//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
_startup	start_list[MAX_starts];
//------------------------------------------------------------------------------------
uint32	Init_start_menu(void)	//Tony13Aug96
{
//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



	Zdebug("initialising start menu");


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



//ok, we've loaded in the resource.inf file which contains a list of all the files
//now extract the filenames
	do
	{
		while(((char)*(temp->ad+j))!=13)	//item must have an #0d0a
		{
			ascii_start_ids[total_screen_managers][pos]=*(temp->ad+j);
			j++;
			pos++;

		};

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

		pos=0;	//reset position in current slot between entries
		j+=2;	//past the 0a
		total_screen_managers++;	//done another

		if	(total_screen_managers==MAX_starts)
		{	Zdebug("WARNING MAX_starts exceeded!");
			break;
		}
	}
	while(j<end);	//using this method the Gode generated resource.inf must have #0d0a on the last entry



	Zdebug("%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]);

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


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


		if (res_man.Res_check_valid(res))	// 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
			Zdebug("- resource %d ok",res);
			raw_script= (char*) (res_man.Res_open(res));	//+sizeof(_standardHeader)+sizeof(_object_hub));
			null_pc=0;	//
			RunScript ( raw_script, raw_script, &null_pc );
			res_man.Res_close(res);
		}
		else
		{
			Zdebug("- resource %d invalid",res);
		}
	}


	Zdebug("");	//line feed


	Free_mem(temp);	//release the Talloc

	return(1);
}
//------------------------------------------------------------------------------------
int32 FN_register_start_point(int32 *params)	//Tony14Oct96
{
	// param	0 id of startup script to call - key
	// param	1 pointer to ascii message


//	Zdebug(" FN_register_start_point %d %s", params[0], params[1]);

	#ifdef _SWORD2_DEBUG
	if (total_startups==MAX_starts)
		Con_fatal_error("ERROR: start_list full [%s line %u]",__FILE__,__LINE__);

	if (strlen((char*)params[1])+1 > MAX_description)	// +1 to allow for NULL terminator
		Con_fatal_error("ERROR: startup description too long [%s line %u]",__FILE__,__LINE__);
	#endif

	start_list[total_startups].start_res_id	= res;			// this objects id
	start_list[total_startups].key			= params[0];	// a key code to be passed to a script via a script var to SWITCH in the correct start
	strcpy(start_list[total_startups].description, (char*)params[1]);

	total_startups++;	//point to next

	return(1);
}
//------------------------------------------------------------------------------------
uint32	Con_print_start_menu(void)	//Tony14Oct96
{
	//the console 'starts' (or 's') command which lists out all the registered start points in the game
	uint32	j;
	int		scrolls=0;
	char	c;


	if	(!total_startups)
	{	Print_to_console("Sorry - no startup positions registered?");

		if	(!total_screen_managers)
			Print_to_console("There is a problem with startup.inf");
		else
			Print_to_console(" (%d screen managers found in startup.inf)", total_screen_managers);

	}
	else
	{
		for(j=0;j<total_startups;j++)
		{
			Print_to_console("%d  (%s)", j, start_list[j].description);
			Build_display();
			scrolls++;


			if	(scrolls==18)
			{
				Temp_print_to_console("- Press ESC to stop or any other key to continue");
				Build_display();

				do
				{
			 		//--------------------------------------------------
					// Service windows
					while (!gotTheFocus)
						if (ServiceWindows() == RDERR_APPCLOSED)
							break;

					if (ServiceWindows() == RDERR_APPCLOSED)	// if we pressed Ctrl-Q
					{
						Close_game();	//close engine systems down
						CloseAppWindow();
						exit(0);	//quit the game
					}
 					//--------------------------------------------------
				}
				while(!KeyWaiting());

				ReadKey(&c);	//kill the key we just pressed
				if	(c==27)	//ESC
					break;

				Clear_console_line();	//clear the Press Esc message ready for the new line
				scrolls=0;
			}	


		}
	}
	return(1);
}
//------------------------------------------------------------------------------------
uint32	Con_start(uint8 *input)	//Tony15Oct96
{
//if the second word id is a numeric that can be applied to a genuine startup then do it
	uint32	j=0;
	uint32	start;
	char	*raw_script;
	char	*raw_data_ad;
	uint32	null_pc;



	if (*input == 0)		// so that typing 'S' then <enter> works on NT (James26feb97)
	{
		Con_print_start_menu();
		return(1);
	}


	while(*(input+j))
	{
		if ( (*(input+j)>='0') && (*(input+j)<='9') )
			j++;
		else
			break;
	}


	if	(!*(input+j))	//didn't quit out of loop on a non numeric chr$
	{
		start = atoi((char*)input);

		if	(!total_startups)
			Print_to_console("Sorry - there are no startups!");

		else	if	(start<total_startups)	//a legal start
		{
//			do the startup as we've specified a legal start

			//--------------------------------------------------------------
			// restarting - stop sfx, music & speech!

			Clear_fx_queue();
			//---------------------------------------------
			FN_stop_music(NULL);	// fade out any music that is currently playing
			//---------------------------------------------

			g_sword2->_sound->UnpauseSpeech();
			g_sword2->_sound->StopSpeechSword2();							// halt the sample prematurely

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

			res_man.Remove_all_res();	// remove all resources from memory, including player object & global variables
			SetGlobalInterpreterVariables((int32*)(res_man.Res_open(1)+sizeof(_standardHeader)));	// reopen global variables resource & send address to interpreter - it won't be moving
			res_man.Res_close(1);

			FreeAllRouteMem();	// free all the route memory blocks from previous game

			if (speech_text_bloc_no)					// if there was speech text
			{
				Kill_text_bloc(speech_text_bloc_no);	// kill the text block
				speech_text_bloc_no=0;
			}

			//--------------------------------------------------------------

//			set the key
			raw_data_ad= (char*) (res_man.Res_open(8));	//+sizeof(_standardHeader)+sizeof(_object_hub));	//open george
			raw_script= (char*) (res_man.Res_open(start_list[start].start_res_id));	//+sizeof(_standardHeader)+sizeof(_object_hub));
			null_pc=start_list[start].key&0xffff;	//denotes script to run
			Print_to_console("running start %d", start);
			RunScript ( raw_script, raw_data_ad, &null_pc );

			res_man.Res_close(start_list[start].start_res_id);
			res_man.Res_close(8);	//close george

			FN_add_human(NULL);	// make sure thre's a mouse, in case restarting while mouse not available
		}
		else
			Print_to_console("not a legal start position");
	}
	else
	{
		Con_print_start_menu();		// so that typing 'S' then <enter> works under Win95
	}
	return(1);
}

//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------