File: readnpcs.cc

package info (click to toggle)
exult 1.2-18
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 9,436 kB
  • sloc: cpp: 99,445; sh: 7,298; ansic: 4,659; makefile: 926; yacc: 770; lex: 314; xml: 19
file content (333 lines) | stat: -rw-r--r-- 7,983 bytes parent folder | download | duplicates (8)
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
/*
 *	readnpcs.cc - Read in NPC's from npc.dat & schedule.dat.  Also writes npc.dat back out.
 *
 *  Copyright (C) 1999  Jeffrey S. Freedman
 *  Copyright (C) 2000-2004  The Exult Team
 *
 *  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.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#ifndef ALPHA_LINUX_CXX
#  include <cstring>
#endif
#include "gamewin.h"
#include "game.h"
#include "monsters.h"
#include "ucmachine.h"
#include "utils.h"
#include "fnames.h"
#include "schedule.h"
#include "databuf.h"
//#include "items.h"			/* Debugging only */

#ifndef UNDER_CE
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::ios;
using std::memset;
using std::ofstream;
#endif

/*
 *	Read in the NPC's, plus the monster info.
 */

void Game_window::read_npcs
	(
	)
{
	npcs.resize(1);			// Create main actor.
	camera_actor = npcs[0] = main_actor = new Main_actor("", 0);
	ifstream nfile_stream;
	StreamDataSource nfile(&nfile_stream);
	int num_npcs;
	bool fix_unused = false;	// Get set for old savegames.
	try
		{
		U7open(nfile_stream, NPC_DAT);
		num_npcs1 = nfile.read2();	// Get counts.
		num_npcs = num_npcs1 + nfile.read2();
		main_actor->read(&nfile, 0, false, fix_unused);
		}
	catch(exult_exception &e)
		{
		if (!Game::is_editing())
			throw e;
		num_npcs1 = num_npcs = 1; 
		if (Game::get_avname())
			main_actor->set_npc_name(Game::get_avname());
		main_actor->set_shape(721);	// FOR NOW.
		main_actor->set_invalid();	// Put in middle of world.
		main_actor->move(c_num_tiles/2, c_num_tiles/2, 0);
		}
	npcs.resize(num_npcs);
	bodies.resize(num_npcs);
	int i;

	// Don't like it... no i don't.
	center_view(main_actor->get_tile());
	for (i = 1; i < num_npcs; i++)	// Create the rest.
	{
		npcs[i] = new Npc_actor("", 0);
		npcs[i]->read(&nfile, i, i < num_npcs1, fix_unused);
		if (npcs[i]->is_unused())
			{		// Not part of the game.
			npcs[i]->remove_this(1);
			npcs[i]->set_schedule_type(Schedule::wait);
			}
		else
			npcs[i]->restore_schedule();
		CYCLE_RED_PLASMA();
	}
	nfile_stream.close();
	main_actor->set_actor_shape();
	try
	{
		U7open(nfile_stream, MONSNPCS);	// Monsters.
		// (Won't exist the first time; in this case U7open throws
		int cnt = nfile.read2();
		char tmp = nfile.read1();// Read 1 ahead to test.
		int okay = nfile_stream.good();
		nfile.skip(-1);
		while (okay && cnt--)
		{
					// Read ahead to get shape.
			nfile.skip(2);
			unsigned short shnum = nfile.read2()&0x3ff;
			okay = nfile_stream.good();
			nfile.skip(-4);
			ShapeID sid(shnum, 0);
			if (!okay || sid.get_num_frames() < 16)
				break;	// Watch for corrupted file.
			Monster_actor *act = Monster_actor::create(shnum);
			act->read(&nfile, -1, false, fix_unused);
			act->restore_schedule();
			CYCLE_RED_PLASMA();
		}
	}
	catch(exult_exception &e) {
#ifdef DEBUG
		cerr << "Error reading saved monsters.  Clearing list." << endl;
#endif
		Monster_actor::give_up();
	}
	if (moving_barge)		// Gather all NPC's on barge.
	{
		Barge_object *b = moving_barge;
		moving_barge = 0;
		set_moving_barge(b);
	}
	read_schedules();		// Now get their schedules.
	center_view(main_actor->get_tile());
}

/*
 *	Write NPC (and monster) data back out.
 *
 *	Output:	false if error, already reported.
 */

void Game_window::write_npcs
	(
	)
	{
	int num_npcs = npcs.size();
	ofstream nfile_stream;
	U7open(nfile_stream, NPC_DAT);
	StreamDataSource nfile(&nfile_stream);

	nfile.write2(num_npcs1);	// Start with counts.
	nfile.write2(num_npcs - num_npcs1);
	int i;
	for (i = 0; i < num_npcs; i++)
		npcs[i]->write(&nfile);
	nfile_stream.flush();
	bool result = nfile_stream.good();
	if (!result)
		throw file_write_exception(NPC_DAT);
	nfile_stream.close();
	write_schedules();		// Write schedules
					// Now write out monsters in world.
	U7open(nfile_stream, MONSNPCS);
	int cnt = 0;
	nfile.write2(0);		// Write 0 as a place holder.
	for (Monster_actor *mact = Monster_actor::get_first_in_world();
					mact; mact = mact->get_next_in_world())
		if (!mact->is_dead())	// Alive?
			{
			mact->write(&nfile);
			cnt++;
			}
	nfile.seek(0);			// Back to start.
	nfile.write2(cnt);		// Write actual count.
	nfile_stream.flush();
	result = nfile_stream.good();
	nfile_stream.close();
	if (!result)
		throw file_write_exception(NPC_DAT);
	}

/*
 *	Read NPC schedules.
 */

void Game_window::read_schedules
	(
	)
	{
	ifstream sfile_stream;
	int num_npcs = 0;
	try
	{
		U7open(sfile_stream, GSCHEDULE);
	}
	catch(exult_exception e)
	{
#ifdef DEBUG
		cerr << "Couldn't open " << GSCHEDULE << ". Falling back to "
			 << SCHEDULE_DAT << "." << endl;
#endif
		try
		{
			U7open(sfile_stream, SCHEDULE_DAT);
		}
		catch(exult_exception e1)
		{
		if (!Game::is_editing())
			throw e1;
		else
			return;
		}
	}
	StreamDataSource sfile(&sfile_stream);

	num_npcs = sfile.read4();	// # of NPC's, not include Avatar.

	short *offsets = new short[num_npcs];
	int i;				// Read offsets with list of scheds.
	for (i = 0; i < num_npcs; i++)
		offsets[i] = sfile.read2();
	for (i = 0; i < num_npcs - 1; i++)	// Do each NPC, except Avatar.
		{
					// Avatar isn't included here.
		Actor *npc = npcs[i + 1];
		int cnt = offsets[i + 1] - offsets[i];
					// Read schedules into this array.
		Schedule_change *schedules = cnt?new Schedule_change[cnt]:0;
		
		for (int j = 0; j < cnt; j++)
			{
			unsigned char ent[4];
			sfile.read(reinterpret_cast<char*>(ent), 4);
			schedules[j].set(ent);
			}
					// Store in NPC.
		if (npc)
			npc->set_schedules(schedules, cnt);
		else
			delete schedules;
		CYCLE_RED_PLASMA();
		}
	delete [] offsets;		// Done with this.
	cout.flush();
	}


/*
 *	Write NPC schedules.
 */

void Game_window::write_schedules ()
{

	ofstream sfile_stream;
	Schedule_change *schedules;
	int cnt;
	short offset = 0;
	int i;
	int num;

	// So do I allow for all NPCs (type1 and type2) - Yes i will
	num = npcs.size();

	U7open(sfile_stream, GSCHEDULE);
	StreamDataSource sfile(&sfile_stream);

	sfile.write4(num);		// # of NPC's, not include Avatar.
	sfile.write2(0);		// First offfset

	for (i = 1; i < num; i++)	// write offsets with list of scheds.
	{
		npcs[i]->get_schedules(schedules, cnt);
		offset += cnt;
		sfile.write2(offset);
	}

	for (i = 1; i < num; i++)	// Do each NPC, except Avatar.
	{
		npcs[i]->get_schedules(schedules, cnt);
		for (int j = 0; j < cnt; j++)
		{
			unsigned char ent[4];
			schedules[j].get(ent);
			sfile.write(reinterpret_cast<char*>(ent), 4);
		}
	}
}

void Game_window::revert_schedules(Actor *npc)
{
	// Can't do this if <= 0
	if (npc->get_npc_num() <= 0) return;

	int i;
	ifstream sfile_stream;

	U7open(sfile_stream, SCHEDULE_DAT);
	StreamDataSource sfile(&sfile_stream);

	// # of NPC's, not include Avatar.
	int num_npcs = sfile.read4();
	short *offsets = new short[num_npcs];
	for (i = 0; i < num_npcs; i++) offsets[i] = sfile.read2();

	// Seek to the right place
	sfile.skip(offsets[npc->get_npc_num()-1]*4);

	// Get the count that we want to use
	int cnt = offsets[npc->get_npc_num()] - offsets[npc->get_npc_num()-1];

	// Read schedules into this array.
	Schedule_change *schedules = cnt?new Schedule_change[cnt]:0;

	for (i = 0; i < cnt; i++)
	{
		unsigned char ent[4];
		sfile.read(reinterpret_cast<char*>(ent), 4);
		schedules[i].set(ent);
	}
	// Store in NPC.
	npc->set_schedules(schedules, cnt);

	// Done
	delete [] offsets;
}