File: shapeid.cc

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (581 lines) | stat: -rw-r--r-- 16,651 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 *  Copyright (C) 2000-2022  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

#include "shapeid.h"

#include "Configuration.h"
#include "Flex.h"
#include "U7file.h"
#include "U7fileman.h"
#include "data/exult_bg_flx.h"
#include "data/exult_si_flx.h"
#include "exceptions.h"
#include "fnames.h"
#include "fontvga.h"
#include "game.h"
#include "gamewin.h"
#include "miscinf.h"
#include "u7drag.h"
#include "utils.h"
#include "vgafile.h"

#include <fstream>
#include <memory>
#include <utility>
#include <vector>

using std::cerr;
using std::endl;
using std::make_unique;
using std::pair;
using std::string;
using std::unique_ptr;
using std::vector;

Shape_manager* Shape_manager::instance = nullptr;

/*
 *  Singletons:
 */
Game_window*     Game_singletons::gwin      = nullptr;
Game_map*        Game_singletons::gmap      = nullptr;
Effects_manager* Game_singletons::eman      = nullptr;
Shape_manager*   Game_singletons::sman      = nullptr;
Usecode_machine* Game_singletons::ucmachine = nullptr;
Game_clock*      Game_singletons::gclock    = nullptr;
Palette*         Game_singletons::pal       = nullptr;
Gump_manager*    Game_singletons::gumpman   = nullptr;
Party_manager*   Game_singletons::partyman  = nullptr;

void Game_singletons::init(Game_window* g) {
	gwin      = g;
	gmap      = g->get_map();
	eman      = g->get_effects();
	sman      = Shape_manager::get_instance();
	ucmachine = g->get_usecode();
	gclock    = g->get_clock();
	pal       = g->get_pal();
	gumpman   = g->get_gump_man();
	partyman  = g->get_party_man();
}

/*
 *  Create shape manager.
 */
Shape_manager::Shape_manager() {
	assert(instance == nullptr);
	instance = this;
}

/*
 *  Read in shape-file info.
 */

void Shape_manager::read_shape_info() {
	// Want space for extra shapes if BG multiracial enabled.
	shapes.init();
	// Read in shape information.
	shapes.read_info(Game::get_game_type(), Game::is_editing());
	// Fixup Avatar shapes (1024-1035 in default SI).
	const Shape_info& male
			= shapes.get_info(Shapeinfo_lookup::GetMaleAvShape());
	const Shape_info& female
			= shapes.get_info(Shapeinfo_lookup::GetFemaleAvShape());

	vector<Skin_data>* skins = Shapeinfo_lookup::GetSkinList();
	for (auto& skin : *skins) {
		if (skin.copy_info) {
			shapes.copy_info(skin.shape_num, skin.is_female ? female : male);
			shapes.copy_info(skin.naked_shape, skin.is_female ? female : male);
		}
	}
}

/*
 *  Load files.
 */

void Shape_manager::load() {
	// Reset all caches, just in case.
	for (auto& cache : shape_cache) {
		cache.clear();
	}

	shapes.reset_imports();

	// Determine some colors based on the default palette
	Palette pal;
	// could throw!
	pal.load(PALETTES_FLX, PATCH_PALETTES, 0);
	// Get a bright green.
	special_pixels[POISON_PIXEL] = pal.find_color(4, 63, 4);
	// Get a light gray.
	special_pixels[PROTECT_PIXEL] = pal.find_color(62, 62, 55);
	// Yellow for cursed.
	special_pixels[CURSED_PIXEL] = pal.find_color(62, 62, 5);
	// Light blue for charmed.
	special_pixels[CHARMED_PIXEL] = pal.find_color(30, 40, 63);
	// Red for hit in battle.
	special_pixels[HIT_PIXEL] = pal.find_color(63, 4, 4);
	// Purple for paralyze.
	special_pixels[PARALYZE_PIXEL] = pal.find_color(49, 27, 49);
	// Black for ShortcutBar_gump
	special_pixels[BLACK_PIXEL] = pal.find_color(0, 0, 0);

	files[SF_GUMPS_VGA].load(GUMPS_VGA, PATCH_GUMPS, true);

	if (!files[SF_PAPERDOL_VGA].load(
				*Shapeinfo_lookup::GetPaperdollSources())) {
		if (GAME_SI) {
			gwin->abort("Can't open 'paperdol.vga' file.");
		} else if (GAME_BG) {    // NOT for devel. games.
			std::cerr << "Couldn't open SI 'paperdol.vga'." << std::endl
					  << "Support for SI Paperdolls in BG is disabled."
					  << std::endl;
		}
		can_have_paperdolls = false;
	} else {
		can_have_paperdolls = true;
	}

	if (GAME_SI) {
		got_si_shapes = true;
	} else if (GAME_BG) {
		// Source for importing SI data.
		pair<string, int> source;

		vector<pair<int, int>>* imports;
		if (can_have_paperdolls) {    // Do this only if SI paperdol.vga was
									  // found.
			source = pair<string, int>(
					string("<SERPENT_STATIC>/gumps.vga"), -1);
			// Gump shapes to import from SI.
			imports = Shapeinfo_lookup::GetImportedGumpShapes();

			if (!imports->empty()) {
				can_have_paperdolls
						= files[SF_GUMPS_VGA].import_shapes(source, *imports);
			} else {
				can_have_paperdolls = false;
			}

			if (can_have_paperdolls) {
				std::cout << "Support for SI Paperdolls is enabled."
						  << std::endl;
			} else {
				std::cerr << "Couldn't open SI 'gumps.vga'." << std::endl
						  << "Support for SI Paperdolls in BG is disabled."
						  << std::endl;
			}
		}

		source = pair<string, int>(string("<SERPENT_STATIC>/shapes.vga"), -1);
		// Skin shapes to import from SI.
		imports = Shapeinfo_lookup::GetImportedSkins();
		if (!imports->empty()) {
			got_si_shapes = shapes.import_shapes(source, *imports);
		} else {
			got_si_shapes = false;
		}

		if (got_si_shapes) {
			std::cout << "Support for SI Multiracial Avatars is enabled."
					  << std::endl;
		} else {
			std::cerr << "Couldn't open SI 'shapes.vga'." << std::endl
					  << "Support for SI Multiracial Avatars is disabled."
					  << std::endl;
		}
	}

	files[SF_SPRITES_VGA].load(SPRITES_VGA, PATCH_SPRITES);
	if (GAME_SIB) {
		// Lets try to import shape 0 of sprites.vga from BG or SI.
		const pair<string, int> sourcebg(
				string("<ULTIMA7_STATIC>/sprites.vga"), -1);
		const pair<string, int> sourcesi(
				string("<SERPENT_STATIC>/sprites.vga"), -1);

		vector<pair<int, int>> imports;
		imports.emplace_back(0, 0);
		if (U7exists(sourcesi.first.c_str())) {
			files[SF_SPRITES_VGA].import_shapes(sourcesi, imports);
		} else if (U7exists(sourcebg.first.c_str())) {
			files[SF_SPRITES_VGA].import_shapes(sourcebg, imports);
		} else {
			// Create lots of pixel frames.
			Shape*        shp      = files[SF_SPRITES_VGA].new_shape(0);
			unsigned char whitepix = 118;
			for (int ii = 0; ii < 28; ii++) {
				shp->add_frame(
						std::make_unique<Shape_frame>(
								&whitepix, 1, 1, 0, 0, false),
						ii);
			}
		}
	}

	vector<pair<string, int>> source;
	source.emplace_back(FACES_VGA, -1);
	if (GAME_BG) {
		// Multiracial faces.
		const str_int_pair& resource = game->get_resource("files/mrfacesvga");
		source.emplace_back(resource.str, resource.num);
	}
	source.emplace_back(PATCH_FACES, -1);
	files[SF_FACES_VGA].load(source);

	files[SF_EXULT_FLX].load(BUNDLE_CHECK(BUNDLE_EXULT_FLX, EXULT_FLX));

	const char* gamedata = game->get_resource("files/gameflx").str;
	std::cout << "Loading " << gamedata << "..." << std::endl;
	files[SF_GAME_FLX].load(gamedata);

	read_shape_info();

	fonts = make_unique<Fonts_vga_file>();
	fonts->init();

	// Get translucency tables.
	unique_ptr<unsigned char[]>
			ptr;    // We will delete THIS at the end, not blends!
	// ++++TODO: Make this file editable in ES.
	{
		const auto& blendsflexspec
				= GAME_BG ? File_spec(
						  BUNDLE_CHECK(BUNDLE_EXULT_BG_FLX, EXULT_BG_FLX),
						  EXULT_BG_FLX_BLENDS_DAT)
						  : File_spec(
								  BUNDLE_CHECK(
										  BUNDLE_EXULT_SI_FLX, EXULT_SI_FLX),
								  EXULT_SI_FLX_BLENDS_DAT);
		const U7multiobject in(BLENDS, blendsflexspec, PATCH_BLENDS, 0);
		size_t              len;
		ptr = in.retrieve(len);
	}
	unsigned char* blends;
	size_t         nblends;
	if (!ptr) {
		// All else failed.
		// Note: the files bundled in exult_XX.flx contain these values.
		// They are "good" enough, but there is probably room for
		// improvement.
		static unsigned char hard_blends[4 * 17]
				= {208, 216, 224, 192, 136, 44,  148, 198, 248, 252, 80,  211,
				   144, 148, 252, 247, 64,  216, 64,  201, 204, 60,  84,  140,
				   144, 40,  192, 128, 96,  40,  16,  128, 100, 108, 116, 192,
				   68,  132, 28,  128, 255, 208, 48,  64,  28,  52,  255, 128,
				   8,   68,  0,   128, 255, 8,   8,   118, 255, 244, 248, 128,
				   56,  40,  32,  128, 228, 224, 214, 82};
		blends  = hard_blends;
		nblends = 17;
	} else {
		blends  = ptr.get();
		nblends = *blends++;
	}
	xforms.resize(nblends);
	const size_t nxforms = nblends;
	// ++++TODO: Make this file editable in ES.
	if (U7exists(XFORMTBL) || U7exists(PATCH_XFORMS)) {
		// Read in translucency tables.
		U7multifile  xformfile(XFORMTBL, PATCH_XFORMS);
		const size_t nobjs = std::min(
				xformfile.number_of_objects(), nblends);    // Limit by blends.
		for (size_t i = 0; i < nobjs; i++) {
			auto ds = xformfile.retrieve(i);
			if (!ds.good()) {
				// No XForm data at all. Make this XForm into an
				// identity transformation.
				for (size_t j = 0; j < sizeof(xforms[0].colors); j++) {
					xforms[nxforms - 1 - i].colors[j] = static_cast<uint8>(j);
				}
			} else {
				ds.read(xforms[nxforms - 1 - i].colors,
						sizeof(xforms[0].colors));
			}
		}
	} else {    // Create algorithmically.
		gwin->get_pal()->load(PALETTES_FLX, PATCH_PALETTES, 0);
		for (size_t i = 0; i < nxforms; i++) {
			gwin->get_pal()->create_trans_table(
					blends[4 * i + 0] / 4, blends[4 * i + 1] / 4,
					blends[4 * i + 2] / 4, blends[4 * i + 3], xforms[i].colors);
		}
	}

	invis_xform = &xforms[nxforms - 1 - 0];    // ->entry 0.
}

// Read in files needed to display gumps.
bool Shape_manager::load_gumps_minimal() {
	U7FileManager::get_ptr()->reset();    // Cache no longer valid.
	shape_cache[SF_GUMPS_VGA].clear();
	shape_cache[SF_EXULT_FLX].clear();
	try {
		if (!files[SF_GUMPS_VGA].load(GUMPS_VGA, PATCH_GUMPS, true)) {
			std::cerr << "Couldn't open 'gumps.vga'." << std::endl;
			return false;
		}
	} catch (exult_exception& ex) {
		std::cerr << ex.what() << std::endl;
		return false;
	}

	try {
		if (!files[SF_EXULT_FLX].load(
					BUNDLE_CHECK(BUNDLE_EXULT_FLX, EXULT_FLX))) {
			std::cerr << "Couldn't open 'exult.flx'." << std::endl;
			return false;
		}
	} catch (exult_exception& ex) {
		std::cerr << ex.what() << std::endl;
		return false;
	}

	// if (fonts) delete fonts;
	// fonts = new Fonts_vga_file();
	// fonts->init();

	return true;
}

/*
 *  Reload one of the shape files (msg. from ExultStudio).
 */

void Shape_manager::reload_shapes(int shape_kind    // Type from u7drag.h.
) {
	U7FileManager::get_ptr()->reset();    // Cache no longer valid.
	shape_cache[shape_kind].clear();
	switch (shape_kind) {
	case U7_SHAPE_SHAPES:
		read_shape_info();
		// ++++Reread text?
		break;
	case U7_SHAPE_GUMPS:
		files[SF_GUMPS_VGA].load(GUMPS_VGA, PATCH_GUMPS);
		break;
	case U7_SHAPE_FONTS:
		fonts->init();
		break;
	case U7_SHAPE_FACES:
		files[SF_FACES_VGA].load(FACES_VGA, PATCH_FACES);
		break;
	case U7_SHAPE_SPRITES:
		files[SF_SPRITES_VGA].load(SPRITES_VGA, PATCH_SPRITES);
		break;
	case U7_SHAPE_PAPERDOL:
		files[SF_PAPERDOL_VGA].load(PAPERDOL, PATCH_PAPERDOL);
		break;
	default:
		cerr << "Type not supported:  " << shape_kind << endl;
		break;
	}
}

/*
 *  Just reload info. files.
 */

void Shape_manager::reload_shape_info() {
	shapes.reload_info(Game::get_game_type());
}

/*
 *  Clean up.
 */
Shape_manager::~Shape_manager() {
	assert(this == instance);
	instance = nullptr;
}

/*
 *  Text-drawing methods:
 */
int Shape_manager::paint_text_box(
		int fontnum, const char* text, int x, int y, int w, int h,
		int vert_lead, bool pbreak, bool center, int shading,
		Cursor_info* cursor) {
	if (shading >= 0) {
		gwin->get_win()->fill_translucent8(0, w, h, x, y, xforms[shading]);
	}
	return fonts->paint_text_box(
			gwin->get_win()->get_ib8(), fontnum, text, x, y, w, h, vert_lead,
			pbreak, center, cursor);
}

int Shape_manager::paint_text(
		int fontnum, const char* text, int xoff, int yoff) {
	return fonts->paint_text(
			gwin->get_win()->get_ib8(), fontnum, text, xoff, yoff);
}

int Shape_manager::paint_text(
		int fontnum, const char* text, int textlen, int xoff, int yoff) {
	return fonts->paint_text(
			gwin->get_win()->get_ib8(), fontnum, text, textlen, xoff, yoff);
}

int Shape_manager::paint_text(
		std::shared_ptr<Font> font, const char* text, int xoff, int yoff) {
	return font->paint_text(gwin->get_win()->get_ib8(), text, xoff, yoff);
}

int Shape_manager::paint_text(
		std::shared_ptr<Font> font, const char* text, int textlen, int xoff,
		int yoff) {
	return font->paint_text(
			gwin->get_win()->get_ib8(), text, textlen, xoff, yoff);
}

int Shape_manager::get_text_width(int fontnum, const char* text) {
	return fonts->get_text_width(fontnum, text);
}

int Shape_manager::get_text_width(int fontnum, const char* text, int textlen) {
	return fonts->get_text_width(fontnum, text, textlen);
}

int Shape_manager::get_text_height(int fontnum) {
	return fonts->get_text_height(fontnum);
}

int Shape_manager::get_text_baseline(int fontnum) {
	return fonts->get_text_baseline(fontnum);
}

int Shape_manager::find_cursor(
		int fontnum, const char* text, int x, int y, int w, int h, int cx,
		int cy, int vert_lead) {
	return fonts->find_cursor(fontnum, text, x, y, w, h, cx, cy, vert_lead);
}

std::shared_ptr<Font> Shape_manager::get_font(int fontnum) {
	return fonts->get_font(fontnum);
}

Shape_manager::Cached_shape Shape_manager::cache_shape(
		int shape_kind, int shapenum, int framenum) {
	Cached_shape cache{nullptr, false};
	if (framenum == -1) {
		return cache;
	}
	using cache_key = std::pair<int, int>;
	auto iter = shape_cache[shape_kind].find(cache_key(shapenum, framenum));
	if (iter != shape_cache[shape_kind].cend()) {
		return iter->second;
	}
	if (shape_kind == SF_SHAPES_VGA) {
		cache.shape     = sman->shapes.get_shape(shapenum, framenum);
		cache.has_trans = sman->shapes.get_info(shapenum).has_translucency();
	} else if (shape_kind < SF_OTHER) {
		cache.shape = sman->files[shape_kind].get_shape(shapenum, framenum);
		if (shape_kind == SF_SPRITES_VGA) {
			cache.has_trans = true;
		}
	} else {
		std::cerr << "Error! Wrong ShapeFile!" << std::endl;
		return cache;
	}
	shape_cache[shape_kind][cache_key(shapenum, framenum)] = cache;
	return cache;
}

/*
 *  Read in shape.
 */
Shape_manager::Cached_shape ShapeID::cache_shape() const {
	return sman->cache_shape(shapefile, shapenum, framenum);
}

int ShapeID::get_num_frames() const {
	if (!shapefile) {
		return sman->shapes.get_num_frames(shapenum);
	} else if (shapefile < SF_OTHER) {
		if (!sman->files[static_cast<int>(shapefile)].is_good()) {
			return 0;
		}
		return sman->files[static_cast<int>(shapefile)].get_num_frames(
				shapenum);
	}
	std::cerr << "Error! Wrong ShapeFile!" << std::endl;
	return 0;
}

bool ShapeID::is_frame_empty() const {
	auto* const shp = get_shape();
	return shp == nullptr || shp->is_empty();
}

uint8* ShapeID::Get_palette_transform_table(uint8 table[256]) const {
	uint16 xform = palette_transform & PT_xForm;
	uint16 index = palette_transform & 0xff;
	if (palette_transform & PT_RampRemap) {
		uint16 to   = palette_transform & 31;
		uint16 from = ((palette_transform & ~32768) >> 5);

		int remaps[32];

		if (from > std::size(remaps) - 1 && from != 255) {
			return nullptr;
		}
		for (unsigned int i = 0; i < std::size(remaps); i++) {
			if (from == 255) {
				remaps[i] = to;
			} else if (i == from) {
				remaps[i]     = to;
				remaps[i + 1] = -1;
				break;
			} else {
				remaps[i] = i;
			}
		}
		remaps[31] = -1;

		gwin->get_pal()->Generate_remap_xformtable(table, remaps);
	} else if (xform == 0) {
		for (int i = 0; i < 256; i++) {
			table[i] = (i + index) & 0xff;
		}
		// keep index 0 and 255 as themselves as this is more useful
		table[0]   = 0;
		table[255] = 255;
		// Bound check xform table
	} else if (xform && index < sman->get_xforms_cnt()) {
		return sman->get_xform(index).colors;
	}
	return table;
}

ImageBufferPaintable::ImageBufferPaintable() : x(0), y(0) {
	auto gwin = Game_window::get_instance();
	auto iwin = gwin->get_win();
	buffer    = iwin->create_buffer(
            iwin->get_full_width(), iwin->get_full_height());
	iwin->get(buffer.get(), x, y);
}

void ImageBufferPaintable::paint() {
	auto gwin = Game_window::get_instance();
	auto iwin = gwin->get_win();
	iwin->put(buffer.get(), x, y);
}