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 #include "ultima/nuvie/conf/configuration.h"
25 #include "ultima/nuvie/files/nuvie_io_file.h"
26 #include "ultima/nuvie/misc/u6_misc.h"
27 #include "ultima/nuvie/files/u6_lib_n.h"
28 #include "ultima/nuvie/files/u6_lzw.h"
29 #include "ultima/nuvie/core/look.h"
30 
31 namespace Ultima {
32 namespace Nuvie {
33 
Look(Configuration * cfg)34 Look::Look(Configuration *cfg)
35 	: look_data(NULL), desc_buf(NULL) {
36 	config = cfg;
37 
38 	look_tbl[2047] = NULL;
39 	max_len = 0;
40 }
41 
~Look()42 Look::~Look() {
43 	free(look_data);
44 	free(desc_buf);
45 }
46 
init()47 bool Look::init() {
48 	Std::string filename;
49 	U6Lzw lzw;
50 	uint32 decomp_size;
51 	unsigned char *ptr;
52 	const char *s;
53 	uint16 i, j;
54 	unsigned int len;
55 	int game_type;
56 	NuvieIOFileRead look_file;
57 
58 	config->value("config/GameType", game_type);
59 
60 	switch (game_type) {
61 	case NUVIE_GAME_U6 :
62 		config_get_path(config, "look.lzd", filename);
63 		look_data = lzw.decompress_file(filename, decomp_size);
64 		if (look_data == NULL)
65 			return false;
66 		break;
67 	case NUVIE_GAME_MD :
68 	case NUVIE_GAME_SE :
69 		U6Lib_n lib_file;
70 		config_get_path(config, "look.lzc", filename);
71 		if (lib_file.open(filename, 4, game_type) == false)
72 			return false;
73 		look_data = lib_file.get_item(0);
74 		break;
75 	}
76 
77 	ptr = look_data;
78 // i: current string pos, j: last string pos
79 	for (i = 0, j = 0; i < 2048;) {
80 		// get number of string
81 		i = ptr[0] + (ptr[1] << 8);
82 
83 		if (i >= 2048)
84 			break;
85 
86 		// store pointer to look_data buffer
87 		look_tbl[i] = s = reinterpret_cast<char *>(&ptr[2]);
88 
89 		// update max_len
90 		len = strlen(s);
91 		if (max_len < len)
92 			max_len = len;
93 
94 		ptr += len + 3;
95 
96 		// fill all empty strings between current and last one
97 		for (; j <= i; j++)
98 			look_tbl[j] = s;
99 	}
100 
101 // fill remaining strings with "Nothing"
102 	for (i = j; i < 2048; i++) {
103 		look_tbl[i] = look_tbl[0]; // nothing
104 	}
105 
106 // allocate space for description buffer
107 	desc_buf = (char *)malloc(max_len + 1);
108 	if (desc_buf == NULL)
109 		return false;
110 
111 	return true;
112 }
113 
get_description(uint16 tile_num,bool * plural)114 const char *Look::get_description(uint16 tile_num, bool *plural) {
115 	const char *desc;
116 	char c;
117 	uint16 i, j;
118 	uint16 len;
119 	bool has_plural = false;
120 
121 	if (tile_num >= 2048)
122 		return NULL;
123 
124 	desc = look_tbl[tile_num];
125 
126 	len = strlen(desc);
127 
128 	for (i = 0, j = 0; i < len;) {
129 		if (desc[i] == '\\' || desc[i] == '/') {
130 			has_plural = true;
131 			c = desc[i];
132 			for (i++; Common::isAlpha(desc[i]) && i < len; i++) {
133 
134 				if ((*plural && c == '\\') || (!*plural && c == '/')) {
135 					desc_buf[j] = desc[i];
136 					j++;
137 				}
138 			}
139 		} else {
140 			desc_buf[j] = desc[i];
141 			i++;
142 			j++;
143 		}
144 	}
145 
146 	desc_buf[j] = desc[i];
147 
148 	*plural = has_plural; //we return if this string contained a plural form.
149 
150 	return desc_buf;
151 }
152 
has_plural(uint16 tile_num)153 bool Look::has_plural(uint16 tile_num) {
154 	const char *desc;
155 
156 	if (tile_num >= 2048)
157 		return false;
158 
159 	desc = look_tbl[tile_num];
160 
161 	if (desc == NULL)
162 		return false;
163 
164 	for (; *desc != '\0'; desc++) {
165 		if (*desc == '\\')
166 			return true;
167 	}
168 
169 	return false;
170 }
171 
get_max_len()172 uint16 Look::get_max_len() {
173 	return max_len;
174 }
175 
print()176 void Look::print() {
177 	uint16 i;
178 
179 	for (i = 0; i < 2048; i++) {
180 		DEBUG(0, LEVEL_DEBUGGING, "%04d :: %s\n", i, look_tbl[i]);
181 	}
182 
183 	return;
184 }
185 
186 } // End of namespace Nuvie
187 } // End of namespace Ultima
188