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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
//
// New find_word algorithm by Thomas Akesson <tapilot@home.se>
//
#include "agi/agi.h"
#include "common/textconsole.h"
namespace Agi {
static uint8 *words; // words in the game
static uint32 wordsFlen; // length of word memory
//
// Local implementation to avoid problems with strndup() used by
// gcc 3.2 Cygwin (see #635984)
//
static char *myStrndup(const char *src, int n) {
char *tmp = strncpy((char *)malloc(n + 1), src, n);
tmp[n] = 0;
return tmp;
}
int AgiEngine::loadWords(const char *fname) {
Common::File fp;
uint32 flen;
uint8 *mem = NULL;
words = NULL;
if (!fp.open(fname)) {
warning("loadWords: can't open %s", fname);
return errOK; // err_BadFileOpen
}
debug(0, "Loading dictionary: %s", fname);
fp.seek(0, SEEK_END);
flen = fp.pos();
wordsFlen = flen;
fp.seek(0, SEEK_SET);
if ((mem = (uint8 *)calloc(1, flen + 32)) == NULL) {
fp.close();
return errNotEnoughMemory;
}
fp.read(mem, flen);
fp.close();
words = mem;
return errOK;
}
void AgiEngine::unloadWords() {
free(words);
words = NULL;
}
/**
* Find a word in the dictionary
* Uses an algorithm hopefully like the one Sierra used. Returns the ID
* of the word and the length in flen. Returns -1 if not found.
*
* Thomas Akesson, November 2001
*/
int AgiEngine::findWord(const char *word, int *flen) {
int mchr = 0; // matched chars
int len, fchr, id = -1;
const uint8 *p = words;
const uint8 *q = words + wordsFlen;
*flen = 0;
debugC(2, kDebugLevelScripts, "find_word(%s)", word);
if (word[0] >= 'a' && word[0] <= 'z')
fchr = word[0] - 'a';
else
return -1;
len = strlen(word);
// Get the offset to the first word beginning with the
// right character
p += READ_BE_UINT16(p + 2 * fchr);
while (p[0] >= mchr) {
if (p[0] == mchr) {
p++;
// Loop through all matching characters
while ((p[0] ^ word[mchr]) == 0x7F && mchr < len) {
mchr++;
p++;
}
// Check if this is the last character of the word
// and if it matches
if ((p[0] ^ word[mchr]) == 0xFF && mchr < len) {
mchr++;
if (word[mchr] == 0 || word[mchr] == 0x20) {
id = READ_BE_UINT16(p + 1);
*flen = mchr;
}
}
}
if (p >= q)
return -1;
// Step to the next word
while (p[0] < 0x80)
p++;
p += 3;
}
return id;
}
void AgiEngine::dictionaryWords(char *msg) {
char *p = NULL;
char *q = NULL;
int wid, wlen;
debugC(2, kDebugLevelScripts, "msg = \"%s\"", msg);
cleanInput();
for (p = msg; p && *p && getvar(vWordNotFound) == 0;) {
if (*p == 0x20)
p++;
if (*p == 0)
break;
wid = findWord(p, &wlen);
debugC(2, kDebugLevelScripts, "find_word(p) == %d", wid);
switch (wid) {
case -1:
debugC(2, kDebugLevelScripts, "unknown word");
_game.egoWords[_game.numEgoWords].word = strdup(p);
q = _game.egoWords[_game.numEgoWords].word;
_game.egoWords[_game.numEgoWords].id = 19999;
setvar(vWordNotFound, 1 + _game.numEgoWords);
_game.numEgoWords++;
p += strlen(p);
break;
case 0:
// ignore this word
debugC(2, kDebugLevelScripts, "ignore word");
p += wlen;
q = NULL;
break;
default:
// an OK word
debugC(3, kDebugLevelScripts, "ok word (%d)", wid);
_game.egoWords[_game.numEgoWords].id = wid;
_game.egoWords[_game.numEgoWords].word = myStrndup(p, wlen);
_game.numEgoWords++;
p += wlen;
break;
}
if (p != NULL && *p) {
debugC(2, kDebugLevelScripts, "p = %s", p);
*p = 0;
p++;
}
if (q != NULL) {
for (; (*q != 0 && *q != 0x20); q++)
;
if (*q) {
*q = 0;
q++;
}
}
}
debugC(4, kDebugLevelScripts, "num_ego_words = %d", _game.numEgoWords);
if (_game.numEgoWords > 0) {
setflag(fEnteredCli, true);
setflag(fSaidAcceptedInput, false);
}
}
} // End of namespace Agi
|