text.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // Portions based on PALx Project by palxex.
  7. // Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.
  8. //
  9. // This file is part of SDLPAL.
  10. //
  11. // SDLPAL is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  25. //
  26. #include "main.h"
  27. #define FONT_COLOR_DEFAULT 0x4F
  28. #define FONT_COLOR_YELLOW 0x2D
  29. #define FONT_COLOR_RED 0x1A
  30. #define FONT_COLOR_CYAN 0x8D
  31. #define FONT_COLOR_CYAN_ALT 0x8C
  32. BOOL g_fUpdatedInBattle = FALSE;
  33. #define MESSAGE_MAX_BUFFER_SIZE 512
  34. #define INCLUDE_CODEPAGE_H
  35. #include "codepage.h"
  36. #ifndef PAL_CLASSIC
  37. static LPWSTR gc_rgszAdditionalWords[CP_MAX][PAL_ADDITIONAL_WORD_COUNT] = {
  38. { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  39. { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  40. //{ L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  41. };
  42. static LPWSTR gc_rgszDefaultAdditionalWords[PAL_ADDITIONAL_WORD_COUNT] = { NULL, L"1", L"2", L"3", L"4", L"5" };
  43. #endif
  44. LPWSTR g_rcCredits[12];
  45. typedef struct tagTEXTLIB
  46. {
  47. LPWSTR *lpWordBuf;
  48. LPWSTR *lpMsgBuf;
  49. #ifndef PAL_CLASSIC
  50. LPWSTR *lpExtraWordBuf;
  51. #endif
  52. int **lpIndexBuf;
  53. int nWords;
  54. int nMsgs;
  55. int nIndices;
  56. int nCurrentDialogLine;
  57. BYTE bCurrentFontColor;
  58. PAL_POS posIcon;
  59. PAL_POS posDialogTitle;
  60. PAL_POS posDialogText;
  61. BYTE bDialogPosition;
  62. BYTE bIcon;
  63. int iDelayTime;
  64. BOOL fUserSkip;
  65. BOOL fPlayingRNG;
  66. BYTE bufDialogIcons[282];
  67. } TEXTLIB, *LPTEXTLIB;
  68. static TEXTLIB g_TextLib;
  69. PAL_FORCE_INLINE int
  70. PAL_ParseLine(
  71. char *line,
  72. char **value,
  73. int *length,
  74. int deltrail
  75. )
  76. {
  77. //
  78. // Remove the leading spaces
  79. //
  80. while (*line && iswspace(*line)) line++;
  81. //
  82. // Skip comments starting with '#'
  83. //
  84. if (*line && *line != '#')
  85. {
  86. //
  87. // Split the index and value
  88. //
  89. LPSTR val = strchr(line, '=');
  90. if (val)
  91. {
  92. //
  93. // Remove the trailing spaces
  94. //
  95. LPSTR end = line + strlen(line);
  96. int index;
  97. if (end > line && end[-1] == '\n') *(--end) = 0;
  98. if (deltrail) while (end > line && iswspace(end[-1])) *(--end) = 0;
  99. //
  100. // Parse the index and pass out value
  101. //
  102. if (sscanf(line, "%d", &index) == 1)
  103. {
  104. *value = val + 1;
  105. *length = end - *value;
  106. return index;
  107. }
  108. }
  109. }
  110. return 0;
  111. }
  112. PAL_FORCE_INLINE char *
  113. PAL_ReadOneLine(
  114. char *temp,
  115. int limit,
  116. FILE *fp
  117. )
  118. {
  119. if (fgets(temp, limit, fp))
  120. {
  121. int n = strlen(temp);
  122. if (n == limit - 1 && temp[n - 1] != '\n' && !feof(fp))
  123. {
  124. // Line too long, try to read it as a whole
  125. int nn = 2;
  126. char *tmp = strdup(temp);
  127. while (!feof(fp))
  128. {
  129. if (!(tmp = (char *)realloc(tmp, nn * limit)))
  130. {
  131. TerminateOnError("PAL_ReadOneLine(): failed to allocate memory for long line!");
  132. }
  133. if (fgets(tmp + n, limit + 1, fp))
  134. {
  135. n += strlen(tmp + n);
  136. if (n < limit - 1 || temp[n - 1] == '\n')
  137. break;
  138. else
  139. nn++;
  140. }
  141. }
  142. if (tmp[n - 1] == '\n') tmp[n - 1] = 0;
  143. return tmp;
  144. }
  145. else
  146. {
  147. if (n > 0 && temp[n - 1] == '\n') temp[--n] = 0;
  148. if (n > 0 && temp[n - 1] == '\r') temp[--n] = 0;
  149. return temp;
  150. }
  151. }
  152. else
  153. return NULL;
  154. }
  155. static int
  156. PAL_ReadMessageFile(
  157. FILE *fp
  158. )
  159. {
  160. char temp[MESSAGE_MAX_BUFFER_SIZE];
  161. struct _msg_entry
  162. {
  163. struct _msg_entry *next;
  164. wchar_t *value;
  165. } *cur_val = NULL;
  166. struct _msg_list_entry
  167. {
  168. struct _msg_list_entry *next;
  169. struct _msg_entry *value;
  170. int index;
  171. int count;
  172. } *head = NULL, *item = NULL;
  173. struct _word_list_entry
  174. {
  175. struct _word_list_entry *next;
  176. wchar_t *value;
  177. int index;
  178. } whead = { NULL, NULL }, *witem = NULL;
  179. enum _message_state
  180. {
  181. ST_OUTSIDE,
  182. ST_DIALOG,
  183. ST_WORD,
  184. ST_CREDIT
  185. } state = ST_OUTSIDE;
  186. int idx_cnt = 0, msg_cnt = 0, word_cnt = 0, sid, eid = -1;
  187. #ifndef PAL_CLASSIC
  188. int extra_word_cnt = 0;
  189. #endif
  190. while (!feof(fp))
  191. {
  192. char *buffer;
  193. if (buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp))
  194. {
  195. switch(state)
  196. {
  197. case ST_OUTSIDE:
  198. //
  199. // Skip comments starting with '#'
  200. //
  201. if (*buffer && *buffer != '#')
  202. {
  203. if (strncmp(buffer, "[BEGIN MESSAGE]", 15) == 0 &&
  204. sscanf(buffer + 15, "%d", &sid) == 1 && sid > eid)
  205. {
  206. state = ST_DIALOG;
  207. //
  208. // First save values (converted wide string) into a linked list
  209. //
  210. if (head)
  211. {
  212. item->next = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  213. item = item->next;
  214. }
  215. else
  216. {
  217. head = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  218. item = head;
  219. }
  220. item->value = NULL; item->index = sid;
  221. item->count = 0; cur_val = NULL;
  222. if (idx_cnt < item->index) idx_cnt = item->index;
  223. }
  224. else if (strncmp(buffer, "[BEGIN WORDS]", 13) == 0 && !witem)
  225. {
  226. state = ST_WORD;
  227. //
  228. // First save values (converted wide string) into a linked list
  229. //
  230. witem = &whead;
  231. }
  232. else if (strncmp(buffer, "[BEGIN CREDITS]", 15) == 0 && !witem)
  233. {
  234. state = ST_CREDIT;
  235. }
  236. else
  237. {
  238. // Just ignore invalid lines
  239. #ifdef ENABLE_LOG
  240. UTIL_WriteLog(LOG_ERR, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", line);
  241. #endif
  242. }
  243. }
  244. break;
  245. case ST_DIALOG:
  246. //
  247. // Check if to end one dialog
  248. //
  249. if (strncmp(buffer, "[END MESSAGE]", 13) == 0 &&
  250. sscanf(buffer + 13, "%d", &eid) == 1 && eid >= sid)
  251. {
  252. // End dialog
  253. state = ST_OUTSIDE;
  254. }
  255. else
  256. {
  257. if (cur_val)
  258. {
  259. cur_val->next = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  260. cur_val = cur_val->next;
  261. }
  262. else
  263. cur_val = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  264. if (strncmp(buffer, "[CLEAR MESSAGE]", 15) == 0)
  265. {
  266. cur_val->value = NULL;
  267. }
  268. else
  269. {
  270. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, NULL, 0);
  271. cur_val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  272. PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, cur_val->value, len);
  273. msg_cnt++;
  274. }
  275. if (!item->value) item->value = cur_val;
  276. cur_val->next = NULL; item->count++;
  277. }
  278. break;
  279. case ST_WORD:
  280. //
  281. // Check if to end word list
  282. //
  283. if (strncmp(buffer, "[END WORDS]", 11) == 0)
  284. {
  285. // End word list
  286. state = ST_OUTSIDE;
  287. }
  288. else
  289. {
  290. char *v;
  291. int l, i = PAL_ParseLine(buffer, &v, &l, TRUE);
  292. if (i > 0 && i <= PAL_ADDITIONAL_WORD_LAST)
  293. {
  294. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  295. struct _word_list_entry *val = (struct _word_list_entry *)UTIL_malloc(sizeof(struct _word_list_entry));
  296. val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  297. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, val->value, len);
  298. val->index = i; val->next = NULL;
  299. witem->next = val; witem = witem->next;
  300. if (i < PAL_ADDITIONAL_WORD_FIRST && word_cnt < i) word_cnt = i;
  301. #ifndef PAL_CLASSIC
  302. if (i >= PAL_ADDITIONAL_WORD_FIRST && extra_word_cnt < i) extra_word_cnt = i;
  303. #endif
  304. }
  305. }
  306. break;
  307. case ST_CREDIT:
  308. //
  309. // Check if to end credit list
  310. //
  311. if (strncmp(buffer, "[END CREDITS]", 13) == 0)
  312. {
  313. // End credit list
  314. state = ST_OUTSIDE;
  315. }
  316. else
  317. {
  318. char *v;
  319. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  320. if ((i == 1 || (i >= 6 && i <= 11)) && !g_rcCredits[i])
  321. {
  322. int limit = (i == 1) ? 24 * 8 : 40 * 8, w = 0, j = 0, len;
  323. if (i == 6 || i == 7)
  324. {
  325. if (PAL_PLATFORM && PAL_CREDIT && PAL_PORTYEAR)
  326. {
  327. const char *templates[] = { "${platform}", "${author}", "${year}" };
  328. const char *values[] = { PAL_PLATFORM, PAL_CREDIT, PAL_PORTYEAR };
  329. const int matchlen[] = { 11, 9, 7 };
  330. const int valuelen[] = { sizeof(PAL_PLATFORM) - 1, sizeof(PAL_CREDIT) - 1, sizeof(PAL_PORTYEAR) - 1 };
  331. char *tmp = (char *)alloca(valuelen[0] + valuelen[1] + valuelen[2] + l + 1);
  332. char *dst = tmp, *src = v;
  333. while (*src)
  334. {
  335. if (*src == '$')
  336. {
  337. int k;
  338. for (k = 0; k < 3 && strncmp(src, templates[k], matchlen[k]); k++);
  339. if (k < 3)
  340. {
  341. strcpy(dst, values[k]);
  342. dst += valuelen[k];
  343. src += matchlen[k];
  344. continue;
  345. }
  346. }
  347. *dst++ = *src++;
  348. }
  349. *dst = 0;
  350. len = PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, NULL, 0);
  351. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  352. PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, g_rcCredits[i], len);
  353. }
  354. }
  355. else
  356. {
  357. len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  358. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  359. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, g_rcCredits[i], len);
  360. }
  361. if (g_rcCredits[i])
  362. {
  363. // Limit the length of texts
  364. while (w < limit && j < len - 1) w += PAL_CharWidth(g_rcCredits[i][j++]);
  365. if (w >= limit) g_rcCredits[i][w > limit ? j - 1 : j] = 0;
  366. }
  367. }
  368. }
  369. break;
  370. default:
  371. TerminateOnError("PAL_ReadMessageFile(): Reached an unknown state. Something really wrong may have happened!");
  372. break;
  373. }
  374. if (buffer != temp) free(buffer);
  375. }
  376. }
  377. if (msg_cnt > 0)
  378. {
  379. //
  380. // Move values from linked list to array
  381. //
  382. int idx_msg = 1;
  383. g_TextLib.nIndices = (idx_cnt += 1);
  384. g_TextLib.nMsgs = (msg_cnt += 1);
  385. g_TextLib.lpIndexBuf = (int **)calloc(idx_cnt, sizeof(int *));
  386. g_TextLib.lpMsgBuf = (LPWSTR *)calloc(msg_cnt, sizeof(LPWSTR));
  387. for (item = head; item; )
  388. {
  389. struct _msg_list_entry *temp = item->next;
  390. struct _msg_entry *msg = item->value;
  391. int index = 0;
  392. g_TextLib.lpIndexBuf[item->index] = (int *)calloc(item->count + 1, sizeof(int));
  393. while (msg)
  394. {
  395. struct _msg_entry *tmp = msg->next;
  396. if (msg->value)
  397. {
  398. g_TextLib.lpIndexBuf[item->index][index++] = idx_msg;
  399. g_TextLib.lpMsgBuf[idx_msg++] = msg->value;
  400. }
  401. else
  402. g_TextLib.lpIndexBuf[item->index][index++] = 0;
  403. free(msg); msg = tmp;
  404. }
  405. g_TextLib.lpIndexBuf[item->index][item->count] = -1;
  406. free(item); item = temp;
  407. }
  408. }
  409. #ifndef PAL_CLASSIC
  410. if (word_cnt > 0 && extra_word_cnt >= PAL_ADDITIONAL_WORD_FIRST && extra_word_cnt <= PAL_ADDITIONAL_WORD_LAST)
  411. #else
  412. if (word_cnt > 0)
  413. #endif
  414. {
  415. //
  416. // Move values from linked list to array
  417. //
  418. #ifndef PAL_CLASSIC
  419. int i;
  420. g_TextLib.lpExtraWordBuf = (LPWSTR *)calloc(PAL_ADDITIONAL_WORD_COUNT, sizeof(LPWSTR));
  421. #endif
  422. g_TextLib.nWords = (word_cnt += 1);
  423. g_TextLib.lpWordBuf = (LPWSTR *)calloc(word_cnt, sizeof(LPWSTR));
  424. for (witem = whead.next; witem; )
  425. {
  426. struct _word_list_entry *temp = witem->next;
  427. if (witem->index < PAL_ADDITIONAL_WORD_FIRST)
  428. g_TextLib.lpWordBuf[witem->index] = witem->value;
  429. else
  430. #ifndef PAL_CLASSIC
  431. g_TextLib.lpExtraWordBuf[witem->index - PAL_ADDITIONAL_WORD_FIRST] = witem->value;
  432. #else
  433. free(witem->value);
  434. #endif
  435. free(witem); witem = temp;
  436. }
  437. #ifndef PAL_CLASSIC
  438. for (i = 1; i < PAL_ADDITIONAL_WORD_COUNT; i++)
  439. if (!g_TextLib.lpExtraWordBuf[i])
  440. g_TextLib.lpExtraWordBuf[i] = gc_rgszDefaultAdditionalWords[i];
  441. #endif
  442. }
  443. fclose(fp);
  444. return (msg_cnt > 0 && word_cnt > 0) ? 1 : 0;
  445. }
  446. INT
  447. PAL_InitText(
  448. VOID
  449. )
  450. /*++
  451. Purpose:
  452. Initialize the in-game texts.
  453. Parameters:
  454. None.
  455. Return value:
  456. 0 = success.
  457. -1 = memory allocation error.
  458. --*/
  459. {
  460. if (gpGlobals->pszMsgName)
  461. {
  462. //
  463. // Open the message, index and word data files.
  464. //
  465. FILE *fp = UTIL_OpenRequiredFileForMode(gpGlobals->pszMsgName, "r");
  466. //
  467. // Read the contents of the message, index and word data files.
  468. //
  469. if (!PAL_ReadMessageFile(fp))
  470. {
  471. return -1;
  472. }
  473. else
  474. {
  475. DWORD dwWordLength = 0;
  476. int i;
  477. for (i = 1; i < g_TextLib.nWords; i++)
  478. {
  479. if (g_TextLib.lpWordBuf[i])
  480. {
  481. LPWSTR ptr = g_TextLib.lpWordBuf[i];
  482. DWORD n = 0;
  483. while (*ptr) n += PAL_CharWidth(*ptr++) >> 3;
  484. if (dwWordLength < n) dwWordLength = n;
  485. }
  486. }
  487. gpGlobals->dwWordLength = dwWordLength;
  488. for (i = 0; i < 12; i++)
  489. {
  490. if (!g_rcCredits[i])
  491. g_rcCredits[i] = L"";
  492. }
  493. }
  494. }
  495. else
  496. {
  497. FILE *fpMsg, *fpWord;
  498. DWORD *offsets;
  499. LPWSTR tmp;
  500. LPBYTE temp;
  501. int wpos, wlen, i;
  502. //
  503. // Open the message and word data files.
  504. //
  505. fpMsg = UTIL_OpenRequiredFile("m.msg");
  506. fpWord = UTIL_OpenRequiredFile("word.dat");
  507. //
  508. // See how many words we have
  509. //
  510. fseek(fpWord, 0, SEEK_END);
  511. i = ftell(fpWord);
  512. //
  513. // Each word has 10 or 16 bytes
  514. //
  515. g_TextLib.nWords = (i + (gpGlobals->dwWordLength - 1)) / gpGlobals->dwWordLength;
  516. //
  517. // Read the words
  518. //
  519. temp = (LPBYTE)malloc(i);
  520. if (temp == NULL)
  521. {
  522. fclose(fpWord);
  523. fclose(fpMsg);
  524. return -1;
  525. }
  526. fseek(fpWord, 0, SEEK_SET);
  527. fread(temp, i, 1, fpWord);
  528. //
  529. // Close the words file
  530. //
  531. fclose(fpWord);
  532. // Split the words and do code page conversion
  533. for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)
  534. {
  535. int base = i * gpGlobals->dwWordLength;
  536. int pos = base + gpGlobals->dwWordLength - 1;
  537. while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;
  538. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gpGlobals->dwWordLength, NULL, 0) + 1;
  539. }
  540. g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
  541. if (g_TextLib.lpWordBuf == NULL)
  542. {
  543. free(temp);
  544. fclose(fpWord);
  545. fclose(fpMsg);
  546. return -1;
  547. }
  548. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  549. if (tmp == NULL)
  550. {
  551. free(g_TextLib.lpWordBuf);
  552. free(temp);
  553. fclose(fpWord);
  554. fclose(fpMsg);
  555. return -1;
  556. }
  557. for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
  558. {
  559. int l;
  560. g_TextLib.lpWordBuf[i] = tmp + wpos;
  561. l = PAL_MultiByteToWideChar((LPCSTR)temp + i * gpGlobals->dwWordLength, gpGlobals->dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);
  562. if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')
  563. g_TextLib.lpWordBuf[i][l - 1] = 0;
  564. g_TextLib.lpWordBuf[i][l] = 0;
  565. wpos += l + 1;
  566. }
  567. free(temp);
  568. //
  569. // Read the message offsets. The message offsets are in SSS.MKF #3
  570. //
  571. i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);
  572. g_TextLib.nMsgs = i - 1;
  573. offsets = (LPDWORD)malloc(i * sizeof(DWORD));
  574. if (offsets == NULL)
  575. {
  576. free(g_TextLib.lpWordBuf);
  577. fclose(fpMsg);
  578. return -1;
  579. }
  580. PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);
  581. //
  582. // Read the messages.
  583. //
  584. fseek(fpMsg, 0, SEEK_END);
  585. i = ftell(fpMsg);
  586. temp = (LPBYTE)malloc(i);
  587. if (temp == NULL)
  588. {
  589. free(offsets);
  590. free(g_TextLib.lpWordBuf[0]);
  591. free(g_TextLib.lpWordBuf);
  592. fclose(fpMsg);
  593. return -1;
  594. }
  595. fseek(fpMsg, 0, SEEK_SET);
  596. fread(temp, 1, i, fpMsg);
  597. fclose(fpMsg);
  598. // Split messages and do code page conversion here
  599. for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)
  600. {
  601. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
  602. }
  603. g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
  604. if (g_TextLib.lpMsgBuf == NULL)
  605. {
  606. free(g_TextLib.lpWordBuf);
  607. free(offsets);
  608. return -1;
  609. }
  610. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  611. if (tmp == NULL)
  612. {
  613. free(g_TextLib.lpMsgBuf);
  614. free(g_TextLib.lpWordBuf);
  615. free(offsets);
  616. return -1;
  617. }
  618. for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
  619. {
  620. int l;
  621. g_TextLib.lpMsgBuf[i] = tmp + wpos;
  622. l = PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);
  623. g_TextLib.lpMsgBuf[i][l] = 0;
  624. wpos += l + 1;
  625. }
  626. free(temp);
  627. free(offsets);
  628. g_TextLib.lpIndexBuf = NULL;
  629. #ifndef PAL_CLASSIC
  630. g_TextLib.lpExtraWordBuf = gc_rgszAdditionalWords[gpGlobals->iCodePage];
  631. #endif
  632. }
  633. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  634. g_TextLib.bIcon = 0;
  635. g_TextLib.posIcon = 0;
  636. g_TextLib.nCurrentDialogLine = 0;
  637. g_TextLib.iDelayTime = 3;
  638. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  639. g_TextLib.posDialogText = PAL_XY(44, 26);
  640. g_TextLib.bDialogPosition = kDialogUpper;
  641. g_TextLib.fUserSkip = FALSE;
  642. PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);
  643. return 0;
  644. }
  645. VOID
  646. PAL_FreeText(
  647. VOID
  648. )
  649. /*++
  650. Purpose:
  651. Free the memory used by the texts.
  652. Parameters:
  653. None.
  654. Return value:
  655. None.
  656. --*/
  657. {
  658. int i;
  659. if (g_TextLib.lpMsgBuf != NULL)
  660. {
  661. if (gpGlobals->pszMsgName)
  662. for(i = 0; i < g_TextLib.nMsgs; i++) free(g_TextLib.lpMsgBuf[i]);
  663. else
  664. free(g_TextLib.lpMsgBuf[0]);
  665. free(g_TextLib.lpMsgBuf);
  666. g_TextLib.lpMsgBuf = NULL;
  667. }
  668. if (g_TextLib.lpWordBuf != NULL)
  669. {
  670. if (gpGlobals->pszMsgName)
  671. for(i = 0; i < g_TextLib.nWords; i++) free(g_TextLib.lpWordBuf[i]);
  672. else
  673. free(g_TextLib.lpWordBuf[0]);
  674. free(g_TextLib.lpWordBuf);
  675. g_TextLib.lpWordBuf = NULL;
  676. }
  677. if (g_TextLib.lpIndexBuf != NULL)
  678. {
  679. if (gpGlobals->pszMsgName)
  680. for(i = 0; i < g_TextLib.nIndices; i++) free(g_TextLib.lpIndexBuf[i]);
  681. else
  682. free(g_TextLib.lpIndexBuf[0]);
  683. free(g_TextLib.lpIndexBuf);
  684. g_TextLib.lpIndexBuf = NULL;
  685. }
  686. }
  687. LPCWSTR
  688. PAL_GetWord(
  689. int iNumWord
  690. )
  691. /*++
  692. Purpose:
  693. Get the specified word.
  694. Parameters:
  695. [IN] wNumWord - the number of the requested word.
  696. Return value:
  697. Pointer to the requested word. NULL if not found.
  698. --*/
  699. {
  700. #ifndef PAL_CLASSIC
  701. if (wNumWord >= PAL_ADDITIONAL_WORD_FIRST)
  702. return g_TextLib.lpExtraWordBuf[wNumWord];
  703. else
  704. #endif
  705. return (iNumWord >= g_TextLib.nWords || !g_TextLib.lpWordBuf[iNumWord]) ? L"" : g_TextLib.lpWordBuf[iNumWord];
  706. }
  707. LPCWSTR
  708. PAL_GetMsg(
  709. int iNumMsg
  710. )
  711. /*++
  712. Purpose:
  713. Get the specified message.
  714. Parameters:
  715. [IN] wNumMsg - the number of the requested message.
  716. Return value:
  717. Pointer to the requested message. NULL if not found.
  718. --*/
  719. {
  720. return (iNumMsg >= g_TextLib.nMsgs || !g_TextLib.lpMsgBuf[iNumMsg]) ? L"" : g_TextLib.lpMsgBuf[iNumMsg];
  721. }
  722. int
  723. PAL_GetMsgNum(
  724. int iIndex,
  725. int iOrder
  726. )
  727. /*++
  728. Purpose:
  729. Get the number of specified message from index & order.
  730. Parameters:
  731. [IN] iMsgIndex - index.
  732. [IN] iOrder - order inside the index.
  733. Return value:
  734. The number of message. Zero means pausing for key, and -1 means end.
  735. --*/
  736. {
  737. return (iIndex >= g_TextLib.nMsgs || !g_TextLib.lpIndexBuf[iIndex]) ? -1 : g_TextLib.lpIndexBuf[iIndex][iOrder];
  738. }
  739. VOID
  740. PAL_DrawText(
  741. LPCWSTR lpszText,
  742. PAL_POS pos,
  743. BYTE bColor,
  744. BOOL fShadow,
  745. BOOL fUpdate
  746. )
  747. /*++
  748. Purpose:
  749. Draw text on the screen.
  750. Parameters:
  751. [IN] lpszText - the text to be drawn.
  752. [IN] pos - Position of the text.
  753. [IN] bColor - Color of the text.
  754. [IN] fShadow - TRUE if the text is shadowed or not.
  755. [IN] fUpdate - TRUE if update the screen area.
  756. Return value:
  757. None.
  758. --*/
  759. {
  760. SDL_Rect rect, urect;
  761. rect.x = PAL_X(pos);
  762. rect.y = PAL_Y(pos);
  763. urect.x = rect.x;
  764. urect.y = rect.y;
  765. urect.h = gpGlobals->fUseEmbeddedFonts ? 16 : 17;
  766. urect.w = 0;
  767. while (*lpszText)
  768. {
  769. //
  770. // Draw the character
  771. //
  772. int char_width = PAL_CharWidth(*lpszText);
  773. if (fShadow)
  774. {
  775. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0);
  776. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0);
  777. }
  778. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x, rect.y), bColor);
  779. lpszText++;
  780. rect.x += char_width;
  781. urect.w += char_width;
  782. }
  783. //
  784. // Update the screen area
  785. //
  786. if (fUpdate && urect.w > 0)
  787. {
  788. if (gpGlobals->fIsWIN95)
  789. {
  790. urect.w++;
  791. if (urect.x + urect.w > 320)
  792. {
  793. urect.w = 320 - urect.x;
  794. }
  795. }
  796. VIDEO_UpdateScreen(&urect);
  797. }
  798. }
  799. VOID
  800. PAL_DialogSetDelayTime(
  801. INT iDelayTime
  802. )
  803. /*++
  804. Purpose:
  805. Set the delay time for dialog.
  806. Parameters:
  807. [IN] iDelayTime - the delay time to be set.
  808. Return value:
  809. None.
  810. --*/
  811. {
  812. g_TextLib.iDelayTime = iDelayTime;
  813. }
  814. VOID
  815. PAL_StartDialog(
  816. BYTE bDialogLocation,
  817. BYTE bFontColor,
  818. INT iNumCharFace,
  819. BOOL fPlayingRNG
  820. )
  821. /*++
  822. Purpose:
  823. Start a new dialog.
  824. Parameters:
  825. [IN] bDialogLocation - the location of the text on the screen.
  826. [IN] bFontColor - the font color of the text.
  827. [IN] iNumCharFace - number of the character face in RGM.MKF.
  828. [IN] fPlayingRNG - whether we are playing a RNG video or not.
  829. Return value:
  830. None.
  831. --*/
  832. {
  833. PAL_LARGE BYTE buf[16384];
  834. SDL_Rect rect;
  835. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  836. {
  837. //
  838. // Update the screen in battle, or the graphics may seem messed up
  839. //
  840. VIDEO_UpdateScreen(NULL);
  841. g_fUpdatedInBattle = TRUE;
  842. }
  843. g_TextLib.bIcon = 0;
  844. g_TextLib.posIcon = 0;
  845. g_TextLib.nCurrentDialogLine = 0;
  846. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  847. g_TextLib.fUserSkip = FALSE;
  848. if (bFontColor != 0)
  849. {
  850. g_TextLib.bCurrentFontColor = bFontColor;
  851. }
  852. if (fPlayingRNG && iNumCharFace)
  853. {
  854. VIDEO_BackupScreen();
  855. g_TextLib.fPlayingRNG = TRUE;
  856. }
  857. switch (bDialogLocation)
  858. {
  859. case kDialogUpper:
  860. if (iNumCharFace > 0)
  861. {
  862. //
  863. // Display the character face at the upper part of the screen
  864. //
  865. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  866. {
  867. rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);
  868. rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);
  869. rect.x = 48 - rect.w / 2;
  870. rect.y = 55 - rect.h / 2;
  871. if (rect.x < 0)
  872. {
  873. rect.x = 0;
  874. }
  875. if (rect.y < 0)
  876. {
  877. rect.y = 0;
  878. }
  879. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  880. if (rect.x < 0)
  881. {
  882. rect.x = 0;
  883. }
  884. if (rect.y < 0)
  885. {
  886. rect.y = 0;
  887. }
  888. VIDEO_UpdateScreen(&rect);
  889. }
  890. }
  891. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);
  892. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);
  893. break;
  894. case kDialogCenter:
  895. g_TextLib.posDialogText = PAL_XY(80, 40);
  896. break;
  897. case kDialogLower:
  898. if (iNumCharFace > 0)
  899. {
  900. //
  901. // Display the character face at the lower part of the screen
  902. //
  903. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  904. {
  905. rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;
  906. rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;
  907. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  908. VIDEO_UpdateScreen(NULL);
  909. }
  910. }
  911. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);
  912. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);
  913. break;
  914. case kDialogCenterWindow:
  915. g_TextLib.posDialogText = PAL_XY(160, 40);
  916. break;
  917. }
  918. g_TextLib.bDialogPosition = bDialogLocation;
  919. }
  920. static VOID
  921. PAL_DialogWaitForKey(
  922. VOID
  923. )
  924. /*++
  925. Purpose:
  926. Wait for player to press a key after showing a dialog.
  927. Parameters:
  928. None.
  929. Return value:
  930. None.
  931. --*/
  932. {
  933. PAL_LARGE SDL_Color palette[256];
  934. SDL_Color *pCurrentPalette, t;
  935. int i;
  936. //
  937. // get the current palette
  938. //
  939. pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  940. memcpy(palette, pCurrentPalette, sizeof(palette));
  941. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  942. g_TextLib.bDialogPosition != kDialogCenter)
  943. {
  944. //
  945. // show the icon
  946. //
  947. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);
  948. if (p != NULL)
  949. {
  950. SDL_Rect rect;
  951. rect.x = PAL_X(g_TextLib.posIcon);
  952. rect.y = PAL_Y(g_TextLib.posIcon);
  953. rect.w = 16;
  954. rect.h = 16;
  955. PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);
  956. VIDEO_UpdateScreen(&rect);
  957. }
  958. }
  959. PAL_ClearKeyState();
  960. while (TRUE)
  961. {
  962. UTIL_Delay(100);
  963. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  964. g_TextLib.bDialogPosition != kDialogCenter)
  965. {
  966. //
  967. // palette shift
  968. //
  969. t = palette[0xF9];
  970. for (i = 0xF9; i < 0xFE; i++)
  971. {
  972. palette[i] = palette[i + 1];
  973. }
  974. palette[0xFE] = t;
  975. VIDEO_SetPalette(palette);
  976. }
  977. if (g_InputState.dwKeyPress != 0)
  978. {
  979. break;
  980. }
  981. }
  982. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  983. g_TextLib.bDialogPosition != kDialogCenter)
  984. {
  985. PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  986. }
  987. PAL_ClearKeyState();
  988. g_TextLib.fUserSkip = FALSE;
  989. }
  990. VOID
  991. PAL_ShowDialogText(
  992. LPCWSTR lpszText
  993. )
  994. /*++
  995. Purpose:
  996. Show one line of the dialog text.
  997. Parameters:
  998. [IN] lpszText - the text to be shown.
  999. Return value:
  1000. None.
  1001. --*/
  1002. {
  1003. SDL_Rect rect;
  1004. int x, y;
  1005. PAL_ClearKeyState();
  1006. g_TextLib.bIcon = 0;
  1007. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  1008. {
  1009. //
  1010. // Update the screen in battle, or the graphics may seem messed up
  1011. //
  1012. VIDEO_UpdateScreen(NULL);
  1013. g_fUpdatedInBattle = TRUE;
  1014. }
  1015. if (g_TextLib.nCurrentDialogLine > 3)
  1016. {
  1017. //
  1018. // The rest dialogs should be shown in the next page.
  1019. //
  1020. PAL_DialogWaitForKey();
  1021. g_TextLib.nCurrentDialogLine = 0;
  1022. VIDEO_RestoreScreen();
  1023. VIDEO_UpdateScreen(NULL);
  1024. }
  1025. x = PAL_X(g_TextLib.posDialogText);
  1026. y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;
  1027. if (g_TextLib.bDialogPosition == kDialogCenterWindow)
  1028. {
  1029. //
  1030. // The text should be shown in a small window at the center of the screen
  1031. //
  1032. #ifndef PAL_CLASSIC
  1033. if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)
  1034. {
  1035. PAL_BattleUIShowText(lpszText, 1400);
  1036. }
  1037. else
  1038. #endif
  1039. {
  1040. PAL_POS pos;
  1041. LPBOX lpBox;
  1042. int i, w = wcslen(lpszText), len = 0;
  1043. for (i = 0; i < w; i++)
  1044. len += PAL_CharWidth(lpszText[i]) >> 3;
  1045. //
  1046. // Create the window box
  1047. //
  1048. pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));
  1049. lpBox = PAL_CreateSingleLineBox(pos, (len + 1) / 2, TRUE);
  1050. rect.x = PAL_X(pos);
  1051. rect.y = PAL_Y(pos);
  1052. rect.w = 320 - rect.x * 2 + 32;
  1053. rect.h = 64;
  1054. //
  1055. // Show the text on the screen
  1056. //
  1057. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1058. PAL_DrawText(lpszText, pos, 0, FALSE, FALSE);
  1059. VIDEO_UpdateScreen(&rect);
  1060. PAL_DialogWaitForKey();
  1061. //
  1062. // Delete the box
  1063. //
  1064. PAL_DeleteBox(lpBox);
  1065. VIDEO_UpdateScreen(&rect);
  1066. PAL_EndDialog();
  1067. }
  1068. }
  1069. else
  1070. {
  1071. int len = wcslen(lpszText);
  1072. if (g_TextLib.nCurrentDialogLine == 0 &&
  1073. g_TextLib.bDialogPosition != kDialogCenter &&
  1074. (lpszText[len - 1] == 0xff1a ||
  1075. lpszText[len - 1] == 0x2236 || // Special case for Pal WIN95 Simplified Chinese version
  1076. lpszText[len - 1] == ':')
  1077. )
  1078. {
  1079. //
  1080. // name of character
  1081. //
  1082. PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE);
  1083. }
  1084. else
  1085. {
  1086. //
  1087. // normal texts
  1088. //
  1089. WCHAR text[2];
  1090. if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)
  1091. {
  1092. //
  1093. // Save the screen before we show the first line of dialog
  1094. //
  1095. VIDEO_BackupScreen();
  1096. }
  1097. while (lpszText != NULL && *lpszText != '\0')
  1098. {
  1099. switch (*lpszText)
  1100. {
  1101. case '-':
  1102. //
  1103. // Set the font color to Cyan
  1104. //
  1105. if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)
  1106. {
  1107. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1108. }
  1109. else
  1110. {
  1111. g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;
  1112. }
  1113. lpszText++;
  1114. break;
  1115. #if 0
  1116. /* Not used */
  1117. case '\'':
  1118. //
  1119. // Set the font color to Red
  1120. //
  1121. if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)
  1122. {
  1123. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1124. }
  1125. else
  1126. {
  1127. g_TextLib.bCurrentFontColor = FONT_COLOR_RED;
  1128. }
  1129. lpszText++;
  1130. break;
  1131. #endif
  1132. case '\"':
  1133. //
  1134. // Set the font color to Yellow
  1135. //
  1136. if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)
  1137. {
  1138. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1139. }
  1140. else
  1141. {
  1142. g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;
  1143. }
  1144. lpszText++;
  1145. break;
  1146. case '$':
  1147. //
  1148. // Set the delay time of text-displaying
  1149. //
  1150. g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;
  1151. lpszText += 3;
  1152. break;
  1153. case '~':
  1154. //
  1155. // Delay for a period and quit
  1156. //
  1157. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  1158. g_TextLib.nCurrentDialogLine = 0;
  1159. g_TextLib.fUserSkip = FALSE;
  1160. return; // don't go further
  1161. case ')':
  1162. //
  1163. // Set the waiting icon
  1164. //
  1165. g_TextLib.bIcon = 1;
  1166. lpszText++;
  1167. break;
  1168. case '(':
  1169. //
  1170. // Set the waiting icon
  1171. //
  1172. g_TextLib.bIcon = 2;
  1173. lpszText++;
  1174. break;
  1175. case '\\':
  1176. lpszText++;
  1177. default:
  1178. text[0] = *lpszText++;
  1179. text[1] = 0;
  1180. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, TRUE);
  1181. x += PAL_CharWidth(text[0]);
  1182. if (!g_TextLib.fUserSkip)
  1183. {
  1184. PAL_ClearKeyState();
  1185. UTIL_Delay(g_TextLib.iDelayTime * 8);
  1186. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  1187. {
  1188. //
  1189. // User pressed a key to skip the dialog
  1190. //
  1191. g_TextLib.fUserSkip = TRUE;
  1192. }
  1193. }
  1194. }
  1195. }
  1196. g_TextLib.posIcon = PAL_XY(x, y);
  1197. g_TextLib.nCurrentDialogLine++;
  1198. }
  1199. }
  1200. }
  1201. VOID
  1202. PAL_ClearDialog(
  1203. BOOL fWaitForKey
  1204. )
  1205. /*++
  1206. Purpose:
  1207. Clear the state of the dialog.
  1208. Parameters:
  1209. [IN] fWaitForKey - whether wait for any key or not.
  1210. Return value:
  1211. None.
  1212. --*/
  1213. {
  1214. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  1215. {
  1216. PAL_DialogWaitForKey();
  1217. }
  1218. g_TextLib.nCurrentDialogLine = 0;
  1219. if (g_TextLib.bDialogPosition == kDialogCenter)
  1220. {
  1221. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1222. g_TextLib.posDialogText = PAL_XY(44, 26);
  1223. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1224. g_TextLib.bDialogPosition = kDialogUpper;
  1225. }
  1226. }
  1227. VOID
  1228. PAL_EndDialog(
  1229. VOID
  1230. )
  1231. /*++
  1232. Purpose:
  1233. Ends a dialog.
  1234. Parameters:
  1235. None.
  1236. Return value:
  1237. None.
  1238. --*/
  1239. {
  1240. PAL_ClearDialog(TRUE);
  1241. //
  1242. // Set some default parameters, as there are some parts of script
  1243. // which doesn't have a "start dialog" instruction before showing the dialog.
  1244. //
  1245. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1246. g_TextLib.posDialogText = PAL_XY(44, 26);
  1247. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1248. g_TextLib.bDialogPosition = kDialogUpper;
  1249. g_TextLib.fUserSkip = FALSE;
  1250. g_TextLib.fPlayingRNG = FALSE;
  1251. }
  1252. BOOL
  1253. PAL_IsInDialog(
  1254. VOID
  1255. )
  1256. /*++
  1257. Purpose:
  1258. Check if there are dialog texts on the screen.
  1259. Parameters:
  1260. None.
  1261. Return value:
  1262. TRUE if there are dialog texts on the screen, FALSE if not.
  1263. --*/
  1264. {
  1265. return (g_TextLib.nCurrentDialogLine != 0);
  1266. }
  1267. BOOL
  1268. PAL_DialogIsPlayingRNG(
  1269. VOID
  1270. )
  1271. /*++
  1272. Purpose:
  1273. Check if the script used the RNG playing parameter when displaying texts.
  1274. Parameters:
  1275. None.
  1276. Return value:
  1277. TRUE if the script used the RNG playing parameter, FALSE if not.
  1278. --*/
  1279. {
  1280. return g_TextLib.fPlayingRNG;
  1281. }
  1282. INT
  1283. PAL_MultiByteToWideCharCP(
  1284. CODEPAGE cp,
  1285. LPCSTR mbs,
  1286. int mbslength,
  1287. LPWSTR wcs,
  1288. int wcslength
  1289. )
  1290. /*++
  1291. Purpose:
  1292. Convert multi-byte string into the corresponding unicode string.
  1293. Parameters:
  1294. [IN] cp - Code page for conversion.
  1295. [IN] mbs - Pointer to the multi-byte string.
  1296. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1297. [IN] wcs - Pointer to the wide string buffer.
  1298. [IN] wcslength - Length of the wide string buffer.
  1299. Return value:
  1300. The length of converted wide string. If mbslength is set to -1, the returned
  1301. value includes the terminal null-char; otherwise, the null-char is not included.
  1302. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1303. required length of the wide string buffer.
  1304. --*/
  1305. {
  1306. int i = 0, state = 0, wlen = 0, null = 0;
  1307. if (mbslength == -1)
  1308. {
  1309. mbslength = strlen(mbs);
  1310. null = 1;
  1311. }
  1312. if (!wcs)
  1313. {
  1314. switch (cp)
  1315. {
  1316. //case CP_SHIFTJIS:
  1317. // for (i = 0; i < mbslength && mbs[i]; i++)
  1318. // {
  1319. // if (state == 0)
  1320. // {
  1321. // if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1322. // wlen++;
  1323. // else
  1324. // state = 1;
  1325. // }
  1326. // else
  1327. // {
  1328. // wlen++;
  1329. // state = 0;
  1330. // }
  1331. // }
  1332. // break;
  1333. case CP_GBK:
  1334. case CP_BIG5:
  1335. for (i = 0; i < mbslength && mbs[i]; i++)
  1336. {
  1337. if (state == 0)
  1338. {
  1339. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1340. wlen++;
  1341. else
  1342. state = 1;
  1343. }
  1344. else
  1345. {
  1346. wlen++;
  1347. state = 0;
  1348. }
  1349. }
  1350. break;
  1351. case CP_UTF_8:
  1352. for (i = 0; i < mbslength && mbs[i]; i++)
  1353. {
  1354. if (state == 0)
  1355. {
  1356. if ((BYTE)mbs[i] >= 0x80)
  1357. {
  1358. BYTE s = (BYTE)mbs[i] << 1;
  1359. while (s >= 0x80) { state++; s <<= 1; }
  1360. if (state < 1 || state > 3)
  1361. {
  1362. state = 0;
  1363. wlen++;
  1364. }
  1365. }
  1366. else
  1367. wlen++;
  1368. }
  1369. else
  1370. {
  1371. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1372. {
  1373. if (--state == 0) wlen++;
  1374. }
  1375. else
  1376. {
  1377. state = 0; wlen++;
  1378. }
  1379. }
  1380. }
  1381. break;
  1382. default:
  1383. return -1;
  1384. }
  1385. if (i < mbslength && !mbs[i]) null = 1;
  1386. return wlen + null + (state != 0);
  1387. }
  1388. else
  1389. {
  1390. WCHAR invalid_char;
  1391. switch (cp)
  1392. {
  1393. //case CP_SHIFTJIS:
  1394. // invalid_char = 0x30fb;
  1395. // for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1396. // {
  1397. // if (state == 0)
  1398. // {
  1399. // if ((BYTE)mbs[i] <= 0x80)
  1400. // wcs[wlen++] = mbs[i];
  1401. // else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1402. // wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1403. // else if ((BYTE)mbs[i] == 0xfd)
  1404. // wcs[wlen++] = 0xf8f1;
  1405. // else if ((BYTE)mbs[i] == 0xfe)
  1406. // wcs[wlen++] = 0xf8f2;
  1407. // else if ((BYTE)mbs[i] == 0xff)
  1408. // wcs[wlen++] = 0xf8f3;
  1409. // else
  1410. // state = 1;
  1411. // }
  1412. // else
  1413. // {
  1414. // if ((BYTE)mbs[i] < 0x40)
  1415. // wcs[wlen++] = 0x30fb;
  1416. // else if ((BYTE)mbs[i - 1] < 0xa0)
  1417. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1418. // else
  1419. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1420. // state = 0;
  1421. // }
  1422. // }
  1423. // break;
  1424. case CP_GBK:
  1425. invalid_char = 0x3f;
  1426. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1427. {
  1428. if (state == 0)
  1429. {
  1430. if ((BYTE)mbs[i] < 0x80)
  1431. wcs[wlen++] = mbs[i];
  1432. else if ((BYTE)mbs[i] == 0x80)
  1433. wcs[wlen++] = 0x20ac;
  1434. else if ((BYTE)mbs[i] == 0xff)
  1435. wcs[wlen++] = 0xf8f5;
  1436. else
  1437. state = 1;
  1438. }
  1439. else
  1440. {
  1441. if ((BYTE)mbs[i] < 0x40)
  1442. wcs[wlen++] = invalid_char;
  1443. else
  1444. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1445. state = 0;
  1446. }
  1447. }
  1448. break;
  1449. case CP_BIG5:
  1450. invalid_char = 0x3f;
  1451. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1452. {
  1453. if (state == 0)
  1454. {
  1455. if ((BYTE)mbs[i] <= 0x80)
  1456. wcs[wlen++] = mbs[i];
  1457. else if ((BYTE)mbs[i] == 0xff)
  1458. wcs[wlen++] = 0xf8f8;
  1459. else
  1460. state = 1;
  1461. }
  1462. else
  1463. {
  1464. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1465. wcs[wlen++] = invalid_char;
  1466. else if ((BYTE)mbs[i] <= 0x7e)
  1467. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1468. else
  1469. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1470. state = 0;
  1471. }
  1472. }
  1473. break;
  1474. case CP_UTF_8:
  1475. invalid_char = 0x3f;
  1476. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1477. {
  1478. if (state == 0)
  1479. {
  1480. if ((BYTE)mbs[i] >= 0x80)
  1481. {
  1482. BYTE s = (BYTE)mbs[i] << 1;
  1483. while (s >= 0x80) { state++; s <<= 1; }
  1484. if (state < 1 || state > 3)
  1485. {
  1486. state = 0;
  1487. wcs[wlen++] = invalid_char;
  1488. }
  1489. else
  1490. {
  1491. wcs[wlen] = s >> (state + 1);
  1492. }
  1493. }
  1494. else
  1495. wcs[wlen++] = mbs[i];
  1496. }
  1497. else
  1498. {
  1499. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1500. {
  1501. wcs[wlen] <<= 6;
  1502. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1503. if (--state == 0) wlen++;
  1504. }
  1505. else
  1506. {
  1507. state = 0;
  1508. wcs[wlen++] = invalid_char;
  1509. }
  1510. }
  1511. }
  1512. break;
  1513. default:
  1514. return -1;
  1515. }
  1516. if (state != 0 && wlen < wcslength)
  1517. {
  1518. wcs[wlen++] = invalid_char;
  1519. }
  1520. if (null || (i < mbslength && !mbs[i]))
  1521. {
  1522. if (wlen < wcslength)
  1523. wcs[wlen++] = 0;
  1524. else
  1525. wcs[wlen - 1] = 0;
  1526. }
  1527. return wlen;
  1528. }
  1529. }
  1530. INT
  1531. PAL_MultiByteToWideChar(
  1532. LPCSTR mbs,
  1533. int mbslength,
  1534. LPWSTR wcs,
  1535. int wcslength
  1536. )
  1537. /*++
  1538. Purpose:
  1539. Convert multi-byte string into the corresponding unicode string.
  1540. Parameters:
  1541. [IN] mbs - Pointer to the multi-byte string.
  1542. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1543. [IN] wcs - Pointer to the wide string buffer.
  1544. [IN] wcslength - Length of the wide string buffer.
  1545. Return value:
  1546. The length of converted wide string. If mbslength is set to -1, the returned
  1547. value includes the terminal null-char; otherwise, the null-char is not included.
  1548. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1549. required length of the wide string buffer.
  1550. --*/
  1551. {
  1552. return PAL_MultiByteToWideCharCP(gpGlobals->iCodePage, mbs, mbslength, wcs, wcslength);
  1553. }
  1554. WCHAR
  1555. PAL_GetInvalidChar(
  1556. CODEPAGE iCodePage
  1557. )
  1558. {
  1559. switch(iCodePage)
  1560. {
  1561. case CP_BIG5: return 0x3f;
  1562. case CP_GBK: return 0x3f;
  1563. //case CP_SHIFTJIS: return 0x30fb;
  1564. case CP_UTF_8: return 0x3f;
  1565. default: return 0;
  1566. }
  1567. }