game.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /* Copyright (C) 2005-2010 Valeriy Argunov (nporep AT mail DOT ru) */
  2. /*
  3. * This library is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation; either version 2.1 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include "game.h"
  18. #include "actions.h"
  19. #include "callbacks.h"
  20. #include "codetools.h"
  21. #include "coding.h"
  22. #include "common.h"
  23. #include "errors.h"
  24. #include "locations.h"
  25. #include "objects.h"
  26. #include "playlist.h"
  27. #include "statements.h"
  28. #include "text.h"
  29. #include "time_qsp.h"
  30. #include "variables.h"
  31. QSP_CHAR *qspQstPath = 0;
  32. int qspQstPathLen = 0;
  33. QSP_CHAR *qspQstFullPath = 0;
  34. int qspQstCRC = 0;
  35. QSP_CHAR *qspCurIncFiles[QSP_MAXINCFILES];
  36. int qspCurIncFilesCount = 0;
  37. int qspCurIncLocsCount = 0;
  38. int qspCRCTable[256] =
  39. {
  40. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  41. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  42. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  43. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  44. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  45. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  46. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  47. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  48. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  49. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  50. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  51. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  52. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  53. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  54. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  55. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  56. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  57. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  58. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  59. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  60. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  61. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  62. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  63. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  64. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  65. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  66. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  67. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  68. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  69. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  70. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  71. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  72. };
  73. static int qspCRC(void *, int);
  74. static void qspIncludeFile(QSP_CHAR *);
  75. static void qspOpenIncludes();
  76. static FILE *qspFileOpen(QSP_CHAR *, QSP_CHAR *);
  77. static QSP_BOOL qspCheckQuest(char **, int, QSP_BOOL);
  78. static QSP_BOOL qspCheckGameStatus(QSP_CHAR **, int);
  79. static int qspCRC(void *data, int len)
  80. {
  81. unsigned char *ptr;
  82. int crc = 0;
  83. ptr = (unsigned char *)data;
  84. while (len--)
  85. crc = (qspCRCTable[(crc & 0xFF) ^ *ptr++] ^ crc >> 8) ^ 0xD202EF8D;
  86. return crc;
  87. }
  88. QSP_CHAR *qspGetAbsFromRelPath(QSP_CHAR *path)
  89. {
  90. QSP_CHAR *absPath;
  91. absPath = qspGetNewText(qspQstPath, qspQstPathLen);
  92. return qspGetAddText(absPath, path, qspQstPathLen, -1);
  93. }
  94. QSP_CHAR *qspGetPathAsIs(QSP_CHAR *path)
  95. {
  96. QSP_CHAR *newPath;
  97. qspAddText(&newPath, path, 0, -1, QSP_TRUE);
  98. return newPath;
  99. }
  100. void qspClearIncludes(QSP_BOOL isFirst)
  101. {
  102. int i, count;
  103. if (!isFirst)
  104. {
  105. for (i = 0; i < qspCurIncFilesCount; ++i)
  106. free(qspCurIncFiles[i]);
  107. if (qspCurIncLocsCount)
  108. {
  109. count = qspLocsCount - qspCurIncLocsCount;
  110. qspCreateWorld(count, count);
  111. qspPrepareLocs();
  112. }
  113. }
  114. qspCurIncFilesCount = 0;
  115. qspCurIncLocsCount = 0;
  116. }
  117. static void qspIncludeFile(QSP_CHAR *s)
  118. {
  119. QSP_CHAR *file;
  120. int oldCurIncLocsCount;
  121. if (!qspIsAnyString(s)) return;
  122. if (qspCurIncFilesCount == QSP_MAXINCFILES)
  123. {
  124. qspSetError(QSP_ERR_CANTINCFILE);
  125. return;
  126. }
  127. oldCurIncLocsCount = qspCurIncLocsCount;
  128. file = qspGetAbsFromRelPath(s);
  129. qspOpenQuest(file, QSP_TRUE);
  130. free(file);
  131. if (qspErrorNum) return;
  132. if (qspCurIncLocsCount != oldCurIncLocsCount)
  133. qspCurIncFiles[qspCurIncFilesCount++] = qspGetNewText(s, -1);
  134. }
  135. static void qspOpenIncludes()
  136. {
  137. int i;
  138. QSP_CHAR *file;
  139. for (i = 0; i < qspCurIncFilesCount; ++i)
  140. {
  141. file = qspGetAbsFromRelPath(qspCurIncFiles[i]);
  142. qspOpenQuest(file, QSP_TRUE);
  143. free(file);
  144. if (qspErrorNum) return;
  145. }
  146. }
  147. void qspNewGame(QSP_BOOL isReset)
  148. {
  149. if (!qspLocsCount)
  150. {
  151. qspSetError(QSP_ERR_GAMENOTLOADED);
  152. return;
  153. }
  154. qspCurLoc = 0;
  155. if (isReset)
  156. {
  157. qspSetSeed((unsigned int)QSP_TIME(0));
  158. qspTimerInterval = QSP_DEFTIMERINTERVAL;
  159. qspCurIsShowObjs = qspCurIsShowActs = qspCurIsShowVars = qspCurIsShowInput = QSP_TRUE;
  160. qspMemClear(QSP_FALSE);
  161. qspResetTime(0);
  162. qspCallShowWindow(QSP_WIN_ACTS, QSP_TRUE);
  163. qspCallShowWindow(QSP_WIN_OBJS, QSP_TRUE);
  164. qspCallShowWindow(QSP_WIN_VARS, QSP_TRUE);
  165. qspCallShowWindow(QSP_WIN_INPUT, QSP_TRUE);
  166. qspCallSetInputStrText(0);
  167. qspCallShowPicture(0);
  168. qspCallCloseFile(0);
  169. qspCallSetTimer(QSP_DEFTIMERINTERVAL);
  170. }
  171. qspRefreshCurLoc(QSP_TRUE, 0, 0);
  172. }
  173. static FILE *qspFileOpen(QSP_CHAR *fileName, QSP_CHAR *fileMode)
  174. {
  175. FILE *ret;
  176. char *file, *mode;
  177. file = qspToSysString(fileName);
  178. mode = qspToSysString(fileMode);
  179. ret = fopen(file, mode);
  180. free(file);
  181. free(mode);
  182. return ret;
  183. }
  184. static QSP_BOOL qspCheckQuest(char **strs, int count, QSP_BOOL isUCS2)
  185. {
  186. int i, ind, locsCount, actsCount;
  187. QSP_BOOL isOldFormat;
  188. QSP_CHAR *buf = qspGameToQSPString(strs[0], isUCS2, QSP_FALSE);
  189. isOldFormat = qspStrsComp(buf, QSP_GAMEID) != 0;
  190. free(buf);
  191. ind = (isOldFormat ? 30 : 4);
  192. if (ind > count) return QSP_FALSE;
  193. buf = (isOldFormat ?
  194. qspGameToQSPString(strs[0], isUCS2, QSP_FALSE) : qspGameToQSPString(strs[3], isUCS2, QSP_TRUE));
  195. locsCount = qspStrToNum(buf, 0);
  196. free(buf);
  197. if (locsCount <= 0) return QSP_FALSE;
  198. for (i = 0; i < locsCount; ++i)
  199. {
  200. if ((ind += 3) > count) return QSP_FALSE;
  201. if (isOldFormat)
  202. actsCount = 20;
  203. else
  204. {
  205. if (ind + 1 > count) return QSP_FALSE;
  206. buf = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  207. actsCount = qspStrToNum(buf, 0);
  208. free(buf);
  209. if (actsCount < 0 || actsCount > QSP_MAXACTIONS) return QSP_FALSE;
  210. }
  211. if ((ind += (actsCount * (isOldFormat ? 2 : 3))) > count) return QSP_FALSE;
  212. }
  213. return QSP_TRUE;
  214. }
  215. void qspOpenQuestFromData(char *data, int dataSize, QSP_CHAR *fileName, QSP_BOOL isAddLocs)
  216. {
  217. QSP_BOOL isOldFormat, isUCS2, isAddLoc;
  218. int i, j, ind, crc, count, locsCount, actsCount, start, end;
  219. QSP_CHAR *buf, *delim;
  220. char **strs;
  221. if (dataSize < 2)
  222. {
  223. qspSetError(QSP_ERR_CANTLOADFILE);
  224. return;
  225. }
  226. if (!isAddLocs) crc = qspCRC(data, dataSize);
  227. count = qspSplitGameStr(data, isUCS2 = !data[1], QSP_STRSDELIM, &strs);
  228. if (!qspCheckQuest(strs, count, isUCS2))
  229. {
  230. qspSetError(QSP_ERR_CANTLOADFILE);
  231. qspFreeStrs(strs, count);
  232. return;
  233. }
  234. buf = qspGameToQSPString(strs[0], isUCS2, QSP_FALSE);
  235. isOldFormat = qspStrsComp(buf, QSP_GAMEID) != 0;
  236. free(buf);
  237. buf = (isOldFormat ?
  238. qspGameToQSPString(strs[0], isUCS2, QSP_FALSE) : qspGameToQSPString(strs[3], isUCS2, QSP_TRUE));
  239. locsCount = qspStrToNum(buf, 0);
  240. free(buf);
  241. if (isAddLocs)
  242. {
  243. start = qspLocsCount;
  244. end = start + locsCount;
  245. }
  246. else
  247. {
  248. qspClearIncludes(QSP_FALSE);
  249. start = 0;
  250. end = locsCount;
  251. }
  252. locsCount = qspLocsCount;
  253. qspCreateWorld(start, end);
  254. qspLocsCount = locsCount;
  255. locsCount = start;
  256. ind = (isOldFormat ? 30 : 4);
  257. for (i = start; i < end; ++i)
  258. {
  259. buf = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  260. if (isAddLoc = !isAddLocs || qspLocIndex(buf) < 0)
  261. qspLocs[locsCount].Name = buf;
  262. else
  263. free(buf);
  264. if (isAddLoc)
  265. {
  266. qspLocs[locsCount].Desc = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  267. buf = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  268. qspLocs[locsCount].OnVisitLinesCount = qspPreprocessData(buf, &qspLocs[locsCount].OnVisitLines);
  269. free(buf);
  270. }
  271. else
  272. ind += 2;
  273. if (isOldFormat)
  274. actsCount = 20;
  275. else
  276. {
  277. buf = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  278. actsCount = qspStrToNum(buf, 0);
  279. free(buf);
  280. }
  281. if (isAddLoc)
  282. {
  283. for (j = 0; j < actsCount; ++j)
  284. {
  285. qspLocs[locsCount].Actions[j].Image = (isOldFormat ? 0 : qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE));
  286. qspLocs[locsCount].Actions[j].Desc = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  287. buf = qspGameToQSPString(strs[ind++], isUCS2, QSP_TRUE);
  288. qspLocs[locsCount].Actions[j].OnPressLinesCount = qspPreprocessData(buf, &qspLocs[locsCount].Actions[j].OnPressLines);
  289. free(buf);
  290. }
  291. ++locsCount;
  292. }
  293. else
  294. ind += actsCount * (isOldFormat ? 2 : 3);
  295. }
  296. qspFreeStrs(strs, count);
  297. qspLocsCount = end;
  298. qspCreateWorld(end, locsCount);
  299. count = locsCount - start;
  300. if (count) qspPrepareLocs();
  301. if (isAddLocs)
  302. qspCurIncLocsCount += count;
  303. else
  304. {
  305. qspQstFullPath = qspGetAddText(qspQstFullPath, fileName, 0, -1);
  306. delim = qspInStrRChars(qspQstFullPath, QSP_PATHDELIMS, 0);
  307. qspQstPathLen = (delim ? (int)(delim - qspQstFullPath) + 1 : 0);
  308. qspQstPath = qspGetAddText(qspQstPath, qspQstFullPath, 0, qspQstPathLen);
  309. qspQstCRC = crc;
  310. qspCurLoc = -1;
  311. }
  312. }
  313. void qspOpenQuest(QSP_CHAR *fileName, QSP_BOOL isAddLocs)
  314. {
  315. FILE *f;
  316. char *buf;
  317. int fileSize;
  318. if (!(f = QSP_FOPEN(fileName, QSP_FMT("rb"))))
  319. {
  320. qspSetError(QSP_ERR_FILENOTFOUND);
  321. return;
  322. }
  323. fseek(f, 0, SEEK_END);
  324. fileSize = ftell(f);
  325. buf = (char *)malloc(fileSize + 3);
  326. fseek(f, 0, SEEK_SET);
  327. fread(buf, 1, fileSize, f);
  328. fclose(f);
  329. buf[fileSize] = buf[fileSize + 1] = buf[fileSize + 2] = 0;
  330. qspOpenQuestFromData(buf, fileSize + 3, fileName, isAddLocs);
  331. free(buf);
  332. }
  333. int qspSaveGameStatusToString(QSP_CHAR **buf)
  334. {
  335. QSP_CHAR *locName;
  336. QSPVar *savedVars;
  337. int i, j, len, varsCount, oldRefreshCount = qspRefreshCount;
  338. qspExecLocByVarNameWithArgs(QSP_FMT("ONGSAVE"), 0, 0);
  339. if (qspRefreshCount != oldRefreshCount || qspErrorNum) return 0;
  340. varsCount = qspPrepareLocalVars(&savedVars);
  341. if (qspErrorNum) return 0;
  342. *buf = 0;
  343. qspRefreshPlayList();
  344. locName = (qspCurLoc >= 0 ? qspLocs[qspCurLoc].Name : 0);
  345. len = qspCodeWriteVal(buf, 0, QSP_SAVEDGAMEID, QSP_FALSE);
  346. len = qspCodeWriteVal(buf, len, QSP_VER, QSP_FALSE);
  347. len = qspCodeWriteIntVal(buf, len, qspQstCRC, QSP_TRUE);
  348. len = qspCodeWriteIntVal(buf, len, qspGetTime(), QSP_TRUE);
  349. len = qspCodeWriteIntVal(buf, len, qspCurSelAction, QSP_TRUE);
  350. len = qspCodeWriteIntVal(buf, len, qspCurSelObject, QSP_TRUE);
  351. len = qspCodeWriteVal(buf, len, qspViewPath, QSP_TRUE);
  352. len = qspCodeWriteVal(buf, len, qspCurInput, QSP_TRUE);
  353. len = qspCodeWriteVal(buf, len, qspCurDesc, QSP_TRUE);
  354. len = qspCodeWriteVal(buf, len, qspCurVars, QSP_TRUE);
  355. len = qspCodeWriteVal(buf, len, locName, QSP_TRUE);
  356. len = qspCodeWriteIntVal(buf, len, (int)qspCurIsShowActs, QSP_TRUE);
  357. len = qspCodeWriteIntVal(buf, len, (int)qspCurIsShowObjs, QSP_TRUE);
  358. len = qspCodeWriteIntVal(buf, len, (int)qspCurIsShowVars, QSP_TRUE);
  359. len = qspCodeWriteIntVal(buf, len, (int)qspCurIsShowInput, QSP_TRUE);
  360. len = qspCodeWriteIntVal(buf, len, qspTimerInterval, QSP_TRUE);
  361. len = qspCodeWriteIntVal(buf, len, qspPLFilesCount, QSP_TRUE);
  362. for (i = 0; i < qspPLFilesCount; ++i)
  363. len = qspCodeWriteVal(buf, len, qspPLFiles[i], QSP_TRUE);
  364. len = qspCodeWriteIntVal(buf, len, qspCurIncFilesCount, QSP_TRUE);
  365. for (i = 0; i < qspCurIncFilesCount; ++i)
  366. len = qspCodeWriteVal(buf, len, qspCurIncFiles[i], QSP_TRUE);
  367. len = qspCodeWriteIntVal(buf, len, qspCurActionsCount, QSP_TRUE);
  368. for (i = 0; i < qspCurActionsCount; ++i)
  369. {
  370. if (qspCurActions[i].Image)
  371. len = qspCodeWriteVal(buf, len, qspCurActions[i].Image + qspQstPathLen, QSP_TRUE);
  372. else
  373. len = qspCodeWriteVal(buf, len, 0, QSP_FALSE);
  374. len = qspCodeWriteVal(buf, len, qspCurActions[i].Desc, QSP_TRUE);
  375. len = qspCodeWriteIntVal(buf, len, qspCurActions[i].OnPressLinesCount, QSP_TRUE);
  376. for (j = 0; j < qspCurActions[i].OnPressLinesCount; ++j)
  377. {
  378. len = qspCodeWriteVal(buf, len, qspCurActions[i].OnPressLines[j].Str, QSP_TRUE);
  379. len = qspCodeWriteIntVal(buf, len, qspCurActions[i].OnPressLines[j].LineNum, QSP_TRUE);
  380. }
  381. len = qspCodeWriteIntVal(buf, len, qspCurActions[i].Location, QSP_TRUE);
  382. len = qspCodeWriteIntVal(buf, len, qspCurActions[i].ActIndex, QSP_TRUE);
  383. len = qspCodeWriteIntVal(buf, len, qspCurActions[i].StartLine, QSP_TRUE);
  384. len = qspCodeWriteIntVal(buf, len, (int)qspCurActions[i].IsManageLines, QSP_TRUE);
  385. }
  386. len = qspCodeWriteIntVal(buf, len, qspCurObjectsCount, QSP_TRUE);
  387. for (i = 0; i < qspCurObjectsCount; ++i)
  388. {
  389. if (qspCurObjects[i].Image)
  390. len = qspCodeWriteVal(buf, len, qspCurObjects[i].Image + qspQstPathLen, QSP_TRUE);
  391. else
  392. len = qspCodeWriteVal(buf, len, 0, QSP_FALSE);
  393. len = qspCodeWriteVal(buf, len, qspCurObjects[i].Desc, QSP_TRUE);
  394. }
  395. len = qspCodeWriteIntVal(buf, len, qspGetVarsCount(), QSP_TRUE);
  396. for (i = 0; i < QSP_VARSCOUNT; ++i)
  397. {
  398. if (qspVars[i].Name)
  399. {
  400. len = qspCodeWriteIntVal(buf, len, i, QSP_TRUE);
  401. len = qspCodeWriteVal(buf, len, qspVars[i].Name, QSP_TRUE);
  402. len = qspCodeWriteIntVal(buf, len, qspVars[i].ValsCount, QSP_TRUE);
  403. for (j = 0; j < qspVars[i].ValsCount; ++j)
  404. {
  405. len = qspCodeWriteIntVal(buf, len, qspVars[i].Values[j].Num, QSP_TRUE);
  406. len = qspCodeWriteVal(buf, len, qspVars[i].Values[j].Str, QSP_TRUE);
  407. }
  408. len = qspCodeWriteIntVal(buf, len, qspVars[i].IndsCount, QSP_TRUE);
  409. for (j = 0; j < qspVars[i].IndsCount; ++j)
  410. {
  411. len = qspCodeWriteIntVal(buf, len, qspVars[i].Indices[j].Index, QSP_TRUE);
  412. len = qspCodeWriteVal(buf, len, qspVars[i].Indices[j].Str, QSP_TRUE);
  413. }
  414. }
  415. }
  416. qspRestoreLocalVars(savedVars, varsCount, qspSavedVarsGroups, qspSavedVarsGroupsCount);
  417. if (qspErrorNum)
  418. {
  419. free(*buf);
  420. return 0;
  421. }
  422. return len;
  423. }
  424. void qspSaveGameStatus(QSP_CHAR *fileName)
  425. {
  426. FILE *f;
  427. int len;
  428. QSP_CHAR *buf;
  429. if (!(f = QSP_FOPEN(fileName, QSP_FMT("wb"))))
  430. {
  431. qspSetError(QSP_ERR_FILENOTFOUND);
  432. return;
  433. }
  434. if (len = qspSaveGameStatusToString(&buf))
  435. {
  436. fwrite(buf, sizeof(QSP_CHAR), len, f);
  437. free(buf);
  438. }
  439. fclose(f);
  440. }
  441. static QSP_BOOL qspCheckGameStatus(QSP_CHAR **strs, int strsCount)
  442. {
  443. int i, j, ind, count, linesCount, lastInd, temp, selAction, selObject;
  444. ind = 17;
  445. if (ind > strsCount) return QSP_FALSE;
  446. if (qspStrsComp(strs[0], QSP_SAVEDGAMEID) ||
  447. qspStrsComp(strs[1], QSP_GAMEMINVER) < 0 ||
  448. qspStrsComp(strs[1], QSP_VER) > 0) return QSP_FALSE;
  449. if (!qspGetVarNumValue(QSP_FMT("DEBUG")) &&
  450. qspReCodeGetIntVal(strs[2]) != qspQstCRC) return QSP_FALSE;
  451. selAction = qspReCodeGetIntVal(strs[4]);
  452. selObject = qspReCodeGetIntVal(strs[5]);
  453. if (qspReCodeGetIntVal(strs[15]) < 0) return QSP_FALSE;
  454. temp = qspReCodeGetIntVal(strs[16]);
  455. if (temp < 0 || temp > QSP_MAXPLFILES || (ind += temp) > strsCount) return QSP_FALSE;
  456. if (ind + 1 > strsCount) return QSP_FALSE;
  457. temp = qspReCodeGetIntVal(strs[ind++]);
  458. if (temp < 0 || temp > QSP_MAXINCFILES || (ind += temp) > strsCount) return QSP_FALSE;
  459. if (ind + 1 > strsCount) return QSP_FALSE;
  460. count = qspReCodeGetIntVal(strs[ind++]);
  461. if (count < 0 || count > QSP_MAXACTIONS || selAction >= count) return QSP_FALSE;
  462. for (i = 0; i < count; ++i)
  463. {
  464. if ((ind += 2) > strsCount) return QSP_FALSE;
  465. if (ind + 1 > strsCount) return QSP_FALSE;
  466. linesCount = qspReCodeGetIntVal(strs[ind++]);
  467. if (linesCount < 0 || (ind + 2 * linesCount) > strsCount) return QSP_FALSE;
  468. for (j = 0; j < linesCount; ++j)
  469. {
  470. ++ind;
  471. if (qspReCodeGetIntVal(strs[ind++]) < 0) return QSP_FALSE;
  472. }
  473. if (ind + 1 > strsCount) return QSP_FALSE;
  474. if (qspReCodeGetIntVal(strs[ind++]) < 0) return QSP_FALSE;
  475. if (++ind > strsCount) return QSP_FALSE;
  476. if (ind + 1 > strsCount) return QSP_FALSE;
  477. if (qspReCodeGetIntVal(strs[ind++]) < 0) return QSP_FALSE;
  478. if (++ind > strsCount) return QSP_FALSE;
  479. }
  480. if (ind + 1 > strsCount) return QSP_FALSE;
  481. temp = qspReCodeGetIntVal(strs[ind++]);
  482. if (temp < 0 || temp > QSP_MAXOBJECTS || selObject >= temp || (ind += 2 * temp) > strsCount) return QSP_FALSE;
  483. if (ind + 1 > strsCount) return QSP_FALSE;
  484. count = qspReCodeGetIntVal(strs[ind++]);
  485. if (count < 0) return QSP_FALSE;
  486. lastInd = -1;
  487. for (i = 0; i < count; ++i)
  488. {
  489. if (ind + 1 > strsCount) return QSP_FALSE;
  490. temp = qspReCodeGetIntVal(strs[ind++]);
  491. if (temp <= lastInd || temp >= QSP_VARSCOUNT) return QSP_FALSE;
  492. lastInd = temp;
  493. if (++ind > strsCount) return QSP_FALSE;
  494. if (ind + 1 > strsCount) return QSP_FALSE;
  495. temp = qspReCodeGetIntVal(strs[ind++]);
  496. if (temp < 0 || (ind += 2 * temp) > strsCount) return QSP_FALSE;
  497. if (ind + 1 > strsCount) return QSP_FALSE;
  498. temp = qspReCodeGetIntVal(strs[ind++]);
  499. if (temp < 0 || (ind += 2 * temp) > strsCount) return QSP_FALSE;
  500. }
  501. return QSP_TRUE;
  502. }
  503. void qspOpenGameStatusFromString(QSP_CHAR *str)
  504. {
  505. int i, j, ind, count, varInd, varsCount, valsCount;
  506. QSP_CHAR **strs, *file, *locName;
  507. count = qspSplitStr(str, QSP_STRSDELIM, &strs);
  508. if (!qspCheckGameStatus(strs, count))
  509. {
  510. qspSetError(QSP_ERR_CANTLOADFILE);
  511. qspFreeStrs(strs, count);
  512. return;
  513. }
  514. ++qspRefreshCount;
  515. ++qspFullRefreshCount;
  516. qspMemClear(QSP_FALSE);
  517. qspResetTime(qspReCodeGetIntVal(strs[3]));
  518. qspCurSelAction = qspReCodeGetIntVal(strs[4]);
  519. qspCurSelObject = qspReCodeGetIntVal(strs[5]);
  520. if (*strs[6]) qspViewPath = qspCodeReCode(strs[6], QSP_FALSE);
  521. if (*strs[7]) qspCurInputLen = qspStrLen(qspCurInput = qspCodeReCode(strs[7], QSP_FALSE));
  522. if (*strs[8]) qspCurDescLen = qspStrLen(qspCurDesc = qspCodeReCode(strs[8], QSP_FALSE));
  523. if (*strs[9]) qspCurVarsLen = qspStrLen(qspCurVars = qspCodeReCode(strs[9], QSP_FALSE));
  524. locName = qspCodeReCode(strs[10], QSP_FALSE);
  525. qspCurIsShowActs = qspReCodeGetIntVal(strs[11]) != 0;
  526. qspCurIsShowObjs = qspReCodeGetIntVal(strs[12]) != 0;
  527. qspCurIsShowVars = qspReCodeGetIntVal(strs[13]) != 0;
  528. qspCurIsShowInput = qspReCodeGetIntVal(strs[14]) != 0;
  529. qspTimerInterval = qspReCodeGetIntVal(strs[15]);
  530. qspPLFilesCount = qspReCodeGetIntVal(strs[16]);
  531. ind = 17;
  532. for (i = 0; i < qspPLFilesCount; ++i)
  533. qspPLFiles[i] = qspCodeReCode(strs[ind++], QSP_FALSE);
  534. qspCurIncFilesCount = qspReCodeGetIntVal(strs[ind++]);
  535. for (i = 0; i < qspCurIncFilesCount; ++i)
  536. qspCurIncFiles[i] = qspCodeReCode(strs[ind++], QSP_FALSE);
  537. qspCurActionsCount = qspReCodeGetIntVal(strs[ind++]);
  538. for (i = 0; i < qspCurActionsCount; ++i)
  539. {
  540. if (*strs[ind])
  541. {
  542. str = qspCodeReCode(strs[ind], QSP_FALSE);
  543. qspCurActions[i].Image = qspGetPathAsIs(str);
  544. free(str);
  545. }
  546. else
  547. qspCurActions[i].Image = 0;
  548. ++ind;
  549. qspCurActions[i].Desc = qspCodeReCode(strs[ind++], QSP_FALSE);
  550. valsCount = qspCurActions[i].OnPressLinesCount = qspReCodeGetIntVal(strs[ind++]);
  551. if (valsCount)
  552. {
  553. qspCurActions[i].OnPressLines = (QSPLineOfCode *)malloc(valsCount * sizeof(QSPLineOfCode));
  554. for (j = 0; j < valsCount; ++j)
  555. {
  556. qspInitLineOfCode(qspCurActions[i].OnPressLines + j, qspCodeReCode(strs[ind++], QSP_FALSE), 0);
  557. qspCurActions[i].OnPressLines[j].LineNum = qspReCodeGetIntVal(strs[ind++]);
  558. }
  559. }
  560. else
  561. qspCurActions[i].OnPressLines = 0;
  562. qspCurActions[i].Location = qspReCodeGetIntVal(strs[ind++]);
  563. qspCurActions[i].ActIndex = qspReCodeGetIntVal(strs[ind++]);
  564. qspCurActions[i].StartLine = qspReCodeGetIntVal(strs[ind++]);
  565. qspCurActions[i].IsManageLines = qspReCodeGetIntVal(strs[ind++]) != 0;
  566. }
  567. qspCurObjectsCount = qspReCodeGetIntVal(strs[ind++]);
  568. for (i = 0; i < qspCurObjectsCount; ++i)
  569. {
  570. if (*strs[ind])
  571. {
  572. str = qspCodeReCode(strs[ind], QSP_FALSE);
  573. qspCurObjects[i].Image = qspGetPathAsIs(str);
  574. free(str);
  575. }
  576. else
  577. qspCurObjects[i].Image = 0;
  578. ++ind;
  579. qspCurObjects[i].Desc = qspCodeReCode(strs[ind++], QSP_FALSE);
  580. }
  581. varsCount = qspReCodeGetIntVal(strs[ind++]);
  582. for (i = 0; i < varsCount; ++i)
  583. {
  584. varInd = qspReCodeGetIntVal(strs[ind++]);
  585. qspVars[varInd].Name = qspCodeReCode(strs[ind++], QSP_FALSE);
  586. valsCount = qspReCodeGetIntVal(strs[ind++]);
  587. if (valsCount)
  588. {
  589. qspVars[varInd].ValsCount = valsCount;
  590. qspVars[varInd].Values = (QSPVarValue *)malloc(valsCount * sizeof(QSPVarValue));
  591. for (j = 0; j < valsCount; ++j)
  592. {
  593. qspVars[varInd].Values[j].Num = qspReCodeGetIntVal(strs[ind++]);
  594. qspVars[varInd].Values[j].Str = (*strs[ind] ? qspCodeReCode(strs[ind], QSP_FALSE) : 0);
  595. ++ind;
  596. }
  597. }
  598. valsCount = qspReCodeGetIntVal(strs[ind++]);
  599. if (valsCount)
  600. {
  601. qspVars[varInd].IndsBufSize = qspVars[varInd].IndsCount = valsCount;
  602. qspVars[varInd].Indices = (QSPVarIndex *)malloc(valsCount * sizeof(QSPVarIndex));
  603. for (j = 0; j < valsCount; ++j)
  604. {
  605. qspVars[varInd].Indices[j].Index = qspReCodeGetIntVal(strs[ind++]);
  606. qspVars[varInd].Indices[j].Str = qspCodeReCode(strs[ind++], QSP_FALSE);
  607. }
  608. }
  609. }
  610. qspFreeStrs(strs, count);
  611. qspIsMainDescChanged = qspIsVarsDescChanged = qspIsObjectsChanged = qspIsActionsChanged = QSP_TRUE;
  612. qspOpenIncludes();
  613. qspCurLoc = qspLocIndex(locName);
  614. free(locName);
  615. if (qspErrorNum) return;
  616. qspCallShowWindow(QSP_WIN_ACTS, qspCurIsShowActs);
  617. qspCallShowWindow(QSP_WIN_OBJS, qspCurIsShowObjs);
  618. qspCallShowWindow(QSP_WIN_VARS, qspCurIsShowVars);
  619. qspCallShowWindow(QSP_WIN_INPUT, qspCurIsShowInput);
  620. qspCallSetInputStrText(qspCurInput);
  621. if (qspViewPath)
  622. {
  623. file = qspGetPathAsIs(qspViewPath);
  624. qspCallShowPicture(file);
  625. free(file);
  626. }
  627. else
  628. qspCallShowPicture(0);
  629. qspCallCloseFile(0);
  630. qspPlayPLFiles();
  631. qspCallSetTimer(qspTimerInterval);
  632. qspExecLocByVarNameWithArgs(QSP_FMT("ONGLOAD"), 0, 0);
  633. }
  634. void qspOpenGameStatus(QSP_CHAR *fileName)
  635. {
  636. FILE *f;
  637. int fileLen;
  638. QSP_CHAR *buf;
  639. if (!(f = QSP_FOPEN(fileName, QSP_FMT("rb"))))
  640. {
  641. qspSetError(QSP_ERR_FILENOTFOUND);
  642. return;
  643. }
  644. fseek(f, 0, SEEK_END);
  645. fileLen = ftell(f) / sizeof(QSP_CHAR);
  646. buf = (QSP_CHAR *)malloc((fileLen + 1) * sizeof(QSP_CHAR));
  647. fseek(f, 0, SEEK_SET);
  648. fread(buf, sizeof(QSP_CHAR), fileLen, f);
  649. fclose(f);
  650. buf[fileLen] = 0;
  651. qspOpenGameStatusFromString(buf);
  652. free(buf);
  653. }
  654. QSP_BOOL qspStatementOpenQst(QSPVariant *args, int count, QSP_CHAR **jumpTo, int extArg)
  655. {
  656. QSP_CHAR *file;
  657. switch (extArg)
  658. {
  659. case 0:
  660. if (qspIsAnyString(QSP_STR(args[0])))
  661. {
  662. file = qspGetAbsFromRelPath(QSP_STR(args[0]));
  663. qspOpenQuest(file, QSP_FALSE);
  664. free(file);
  665. if (qspErrorNum) return QSP_FALSE;
  666. qspNewGame(QSP_FALSE);
  667. }
  668. break;
  669. case 1:
  670. qspIncludeFile(QSP_STR(args[0]));
  671. break;
  672. }
  673. return QSP_FALSE;
  674. }
  675. QSP_BOOL qspStatementOpenGame(QSPVariant *args, int count, QSP_CHAR **jumpTo, int extArg)
  676. {
  677. QSP_CHAR *file;
  678. if (count && qspIsAnyString(QSP_STR(args[0])))
  679. {
  680. file = qspGetAbsFromRelPath(QSP_STR(args[0]));
  681. qspCallOpenGame(file);
  682. free(file);
  683. }
  684. else
  685. qspCallOpenGame(0);
  686. return QSP_FALSE;
  687. }
  688. QSP_BOOL qspStatementSaveGame(QSPVariant *args, int count, QSP_CHAR **jumpTo, int extArg)
  689. {
  690. QSP_CHAR *file;
  691. if (count && qspIsAnyString(QSP_STR(args[0])))
  692. {
  693. file = qspGetAbsFromRelPath(QSP_STR(args[0]));
  694. qspCallSaveGame(file);
  695. free(file);
  696. }
  697. else
  698. qspCallSaveGame(0);
  699. return QSP_FALSE;
  700. }