text.c 40 KB

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