1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ultima/nuvie/core/nuvie_defs.h"
24 
25 #include "ultima/nuvie/conf/configuration.h"
26 #include "ultima/nuvie/gui/widgets/console.h"
27 #include "ultima/nuvie/files/nuvie_io_file.h"
28 #include "ultima/nuvie/files/u6_shape.h"
29 #include "ultima/nuvie/core/game.h"
30 
31 #include "ultima/nuvie/actors/actor_manager.h"
32 #include "ultima/nuvie/actors/actor.h"
33 #include "ultima/nuvie/portraits/portrait_md.h"
34 #include "ultima/nuvie/misc/u6_misc.h"
35 #include "ultima/nuvie/save/obj_list.h"
36 #include "ultima/nuvie/script/script.h"
37 
38 namespace Ultima {
39 namespace Nuvie {
40 
init()41 bool PortraitMD::init() {
42 	Std::string filename;
43 
44 	avatar_portrait_num = 0;
45 
46 	width = 76;
47 	height = 83;
48 
49 	config_get_path(config, "mdfaces.lzc", filename);
50 	if (faces.open(filename, 4) == false) {
51 		ConsoleAddError("Opening " + filename);
52 		return false;
53 	}
54 
55 	return true;
56 }
57 
load(NuvieIO * objlist)58 bool PortraitMD::load(NuvieIO *objlist) {
59 	objlist->seek(OBJLIST_OFFSET_MD_GENDER);
60 
61 	avatar_portrait_num = objlist->read1() == 0 ? 1 : 0; //read in the avatar portrait number from objlist.
62 //if(avatar_portrait_num > 0)
63 //  avatar_portrait_num--;
64 
65 	return true;
66 }
67 
get_portrait_num(Actor * actor)68 uint8 PortraitMD::get_portrait_num(Actor *actor) {
69 	if (actor == NULL)
70 		return NO_PORTRAIT_FOUND;
71 
72 	uint8 num = Game::get_game()->get_script()->call_get_portrait_number(actor);
73 	if (num != NO_PORTRAIT_FOUND)
74 		num++;
75 	return num;
76 }
77 
get_portrait_data(Actor * actor)78 unsigned char *PortraitMD::get_portrait_data(Actor *actor) {
79 	uint8 num = get_portrait_num(actor);
80 	if (num == NO_PORTRAIT_FOUND)
81 		return NULL;
82 
83 	U6Shape *bg_shp = get_background_shape(num);
84 
85 	unsigned char *temp_buf = faces.get_item(num);
86 	if (!temp_buf)
87 		return NULL;
88 	U6Shape *p_shp = new U6Shape();
89 	p_shp->load(temp_buf + 8);
90 	free(temp_buf);
91 
92 	uint16 w, h;
93 	bg_shp->get_size(&w, &h);
94 	unsigned char *bg_data = bg_shp->get_data();
95 	unsigned char *p_data = p_shp->get_data();
96 	for (int i = 0; i < w * h; i++) {
97 		if (p_data[i] != 255) {
98 			bg_data[i] = p_data[i];
99 		}
100 	}
101 
102 	p_data = (unsigned char *)malloc(w * h);
103 	memcpy(p_data, bg_data, w * h);
104 
105 	delete bg_shp;
106 	delete p_shp;
107 
108 	return p_data;
109 }
110 
get_background_shape(uint8 actor_num)111 U6Shape *PortraitMD::get_background_shape(uint8 actor_num) {
112 	U6Lib_n file;
113 	U6Shape *bg = new U6Shape();
114 	Std::string filename;
115 	config_get_path(config, "mdback.lzc", filename);
116 	file.open(filename, 4, NUVIE_GAME_MD);
117 	unsigned char *temp_buf = file.get_item(get_background_shape_num(actor_num));
118 	bg->load(temp_buf + 8);
119 	free(temp_buf);
120 
121 	return bg;
122 }
123 
get_background_shape_num(uint8 actor_num)124 uint8 PortraitMD::get_background_shape_num(uint8 actor_num) {
125 	const uint8 bg_tbl[] = {
126 		0x22, 0x17, 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x56, 0x27, 0x0, 0x0, 0x55, 0x45,
127 		0x70, 0x0, 0x53, 0x25, 0x45, 0x15, 0x17, 0x37, 0x45, 0x32, 0x24,
128 		0x53, 0x21, 0x42, 0x13, 0x66, 0x61, 0x20, 0x67, 0x23, 0x15, 0x60,
129 		0x0, 0x0, 0x0, 0x0, 0x37, 0x45, 0x32, 0x24, 0x75, 0x73, 0x50, 0x12,
130 		0x51, 0x2, 0x65, 0x61, 0x45, 0x46, 0x31, 0x0, 0x24, 0x77, 0x6, 0x50,
131 		0
132 	};
133 
134 	actor_num--;
135 
136 	//FIXME add logic word_4115A  return 0;
137 
138 	if (actor_num > 121) {
139 		return NUVIE_RAND() % 7;
140 	}
141 
142 	uint8 v = bg_tbl[actor_num / 2];
143 
144 	if ((actor_num & 1) == 0) {
145 		return v >> 4;
146 	}
147 
148 	return v & 0xf;
149 }
150 
151 } // End of namespace Nuvie
152 } // End of namespace Ultima
153