menu_script.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "menu_script.h"
  5. #include "menu_def.h"
  6. #include "3d_all.h"
  7. extern char pDataDir[MAX_FILENAME];
  8. //------------------------------------------------------------------------------------------------
  9. // najde prvni platny znak
  10. //------------------------------------------------------------------------------------------------
  11. int Find_First_Valid_Char(int start, char *text)
  12. {
  13. int l = strlen(text);
  14. int i;
  15. for (i = start; i < l; i++)
  16. if (text[i] != 32 && text[i] != '(' && text[i] != 9 && text[i] != ',') {
  17. if (text[i] != ')')
  18. return i;
  19. else
  20. return -1;
  21. }
  22. return -1;
  23. }
  24. int Find_First_Valid_CharU(int start, WCHAR * text)
  25. {
  26. int l = wcslen(text);
  27. int i;
  28. assert(l);
  29. for (i = start; i < l; i++)
  30. if (text[i] != MAKEWORD(32, 0) &&
  31. text[i] != MAKEWORD('(', 0) &&
  32. text[i] != MAKEWORD(9, 0) && text[i] != MAKEWORD(',', 0)) {
  33. if (text[i] != MAKEWORD(')', 0)) {
  34. if (i > 0)
  35. if (text[i - 1] == MAKEWORD('\\', 0))
  36. continue;
  37. return i;
  38. }
  39. else
  40. return -1;
  41. }
  42. return -1;
  43. }
  44. //------------------------------------------------------------------------------------------------
  45. // najde prvni platny oddelovac
  46. //------------------------------------------------------------------------------------------------
  47. int Find_First_Separator(int start, char *text)
  48. {
  49. int l = strlen(text);
  50. int i;
  51. for (i = start; i < l; i++)
  52. if (text[i] == 32 ||
  53. text[i] == '(' ||
  54. text[i] == 9 ||
  55. text[i] == ')' || text[i] == ',' || text[i] == 10 || text[i] == 13)
  56. return i;
  57. return -1;
  58. }
  59. int Find_First_SeparatorU(int start, WCHAR * text)
  60. {
  61. int l = wcslen(text);
  62. int i;
  63. for (i = start; i < l; i++)
  64. if (text[i] == MAKEWORD(32, 0) ||
  65. text[i] == MAKEWORD('(', 0) ||
  66. text[i] == MAKEWORD(9, 0) ||
  67. text[i] == MAKEWORD(')', 0) ||
  68. text[i] == MAKEWORD(',', 0) ||
  69. text[i] == MAKEWORD(10, 0) || text[i] == MAKEWORD(13, 0)) {
  70. if (i > 0)
  71. if (text[i - 1] == MAKEWORD('\\', 0))
  72. continue;
  73. return i;
  74. }
  75. return -1;
  76. }
  77. //------------------------------------------------------------------------------------------------
  78. // najde prvni platny vyraz
  79. //------------------------------------------------------------------------------------------------
  80. int Find_Next_Expresion(char *p_Command, int start, char *p_Expresion)
  81. {
  82. int first, last;
  83. if (start == -1)
  84. return -1;
  85. if (p_Command[start] == 10 || p_Command[start] == 13)
  86. return -1;
  87. first = Find_First_Valid_Char(start, p_Command);
  88. if (first == -1)
  89. return -1;
  90. last = Find_First_Separator(first, p_Command);
  91. if (last == -1)
  92. return -1;
  93. strncpy(p_Expresion, &p_Command[first], last - first);
  94. p_Expresion[last - first] = '\0';
  95. return last;
  96. }
  97. int Find_Next_ExpresionU(WCHAR * p_Command, int start, WCHAR * p_Expresion)
  98. {
  99. int first, last;
  100. if (start == -1)
  101. return -1;
  102. if (p_Command[start] == 10 || p_Command[start] == 13)
  103. return -1;
  104. first = Find_First_Valid_CharU(start, p_Command);
  105. if (first == -1)
  106. return -1;
  107. last = Find_First_SeparatorU(first, p_Command);
  108. if (last == -1)
  109. return -1;
  110. wcsncpy(p_Expresion, &p_Command[first], last - first);
  111. p_Expresion[last - first] = '\0';
  112. return last;
  113. }
  114. //------------------------------------------------------------------------------------------------
  115. // prevede string na prikaz
  116. //------------------------------------------------------------------------------------------------
  117. int String2Command(char *string)
  118. {
  119. if (string == NULL)
  120. return COM_NOCOMMAND;
  121. if (string[0] == '\0')
  122. return COM_NOCOMMAND;
  123. if (!strcmp(string, "Draw"))
  124. return COM_DRAW;
  125. if (!strcmp(string, "Flip"))
  126. return COM_FLIP;
  127. if (!strcmp(string, "OnAbove"))
  128. return COM_ONABOVE;
  129. if (!strcmp(string, "OnClick"))
  130. return COM_ONCLICK;
  131. if (!strcmp(string, "RandomAnimation"))
  132. return COM_RANDOMANIMATION;
  133. if (!strcmp(string, "RunLevel"))
  134. return COM_RUNLEVEL;
  135. if (!strcmp(string, "Comics"))
  136. return COM_COMICS;
  137. if (!strcmp(string, "BindSound"))
  138. return COM_BINDSOUND;
  139. if (!strcmp(string, "LoadTextures"))
  140. return COM_LOADTEXTURES;
  141. if (!strcmp(string, "InitOpenGL"))
  142. return COM_INITOPENGL;
  143. if (!strcmp(string, "ReleaseOpenGL"))
  144. return COM_RELEASEOPENGL;
  145. if (!strcmp(string, "RunAnimation"))
  146. return COM_RUNANIMATION;
  147. if (!strcmp(string, "BindExitAnimation"))
  148. return COM_BINDEXITANIMATION;
  149. if (!strcmp(string, "BindAnimation"))
  150. return COM_BINDANIMATION;
  151. if (!strcmp(string, "CreateButton"))
  152. return COM_CREATEBUTTON;
  153. if (!strcmp(string, "SetRect"))
  154. return COM_SETRECT;
  155. return COM_UNKNOWN;
  156. }
  157. //------------------------------------------------------------------------------------------------
  158. // naparsuje radek
  159. //------------------------------------------------------------------------------------------------
  160. void Parse_Line(FILE * file, int *result, int res_size, char *comfile1,
  161. char *comfile2)
  162. {
  163. char text[256], expression[256];
  164. int p = 0, r = 1;
  165. int i;
  166. if (!fgets(text, 256, file)) {
  167. result[0] = COM_NOCOMMAND;
  168. return;
  169. }
  170. for (i = 0; i < res_size; i++)
  171. result[i] = -1;
  172. strcpy(comfile1, "");
  173. strcpy(comfile2, "");
  174. p = Find_Next_Expresion(text, p, expression);
  175. if (p != -1)
  176. result[0] = String2Command(expression);
  177. else
  178. result[0] = COM_NOCOMMAND;
  179. if (result[0] == COM_NOCOMMAND ||
  180. result[0] == COM_UNKNOWN || result[0] == COM_FLIP) {
  181. result[0] = -1;
  182. return;
  183. }
  184. while (p != -1) {
  185. p = Find_Next_Expresion(text, p, expression);
  186. switch (result[0]) {
  187. case COM_DRAW:
  188. if ((p != -1) && (r < res_size))
  189. result[r] = atoi(expression);
  190. break;
  191. case COM_SETRECT:
  192. case COM_BINDSOUND:
  193. case COM_RANDOMANIMATION:
  194. case COM_ONABOVE:
  195. case COM_ONCLICK:
  196. case COM_BINDEXITANIMATION:
  197. case COM_BINDANIMATION:
  198. if (p != -1) {
  199. if (r == 5)
  200. strcpy(comfile1, expression);
  201. else if (r == 6)
  202. strcpy(comfile2, expression);
  203. else if (r < res_size)
  204. result[r] = atoi(expression);
  205. }
  206. break;
  207. case COM_RUNANIMATION:
  208. if (p != -1) {
  209. if (r == 1)
  210. strcpy(comfile1, expression);
  211. else if (r == 2)
  212. strcpy(comfile2, expression);
  213. }
  214. break;
  215. case COM_CREATEBUTTON:
  216. if (p != -1) {
  217. if (r == 1 || r == 2)
  218. result[r] = atoi(expression);
  219. else if (r == 3)
  220. strcpy(comfile1, expression);
  221. else if (r == 4)
  222. strcpy(comfile2, expression);
  223. }
  224. break;
  225. }
  226. r++;
  227. }
  228. }
  229. void Parse_LineT(char *ftext, int *result, int res_size, char *comfile1,
  230. char *comfile2)
  231. {
  232. char text[256], expression[256];
  233. int p = 0, r = 1;
  234. //fgets(text, 256, file);
  235. strcpy(text, ftext);
  236. p = Find_Next_Expresion(text, p, expression);
  237. if (p != -1)
  238. result[0] = String2Command(expression);
  239. else
  240. result[0] = COM_NOCOMMAND;
  241. if (result[0] == COM_NOCOMMAND ||
  242. result[0] == COM_UNKNOWN || result[0] == COM_FLIP)
  243. return;
  244. while (p != -1) {
  245. p = Find_Next_Expresion(text, p, expression);
  246. switch (result[0]) {
  247. case COM_DRAW:
  248. if ((p != -1) && (r < res_size))
  249. result[r] = atoi(expression);
  250. break;
  251. case COM_SETRECT:
  252. case COM_BINDSOUND:
  253. case COM_RANDOMANIMATION:
  254. case COM_ONABOVE:
  255. case COM_ONCLICK:
  256. case COM_BINDEXITANIMATION:
  257. case COM_BINDANIMATION:
  258. if (p != -1) {
  259. if (r == 5)
  260. strcpy(comfile1, expression);
  261. else if (r == 6)
  262. strcpy(comfile2, expression);
  263. else if (r < res_size)
  264. result[r] = atoi(expression);
  265. }
  266. break;
  267. case COM_RUNANIMATION:
  268. if (p != -1) {
  269. if (r == 1)
  270. strcpy(comfile1, expression);
  271. else if (r == 2)
  272. strcpy(comfile2, expression);
  273. }
  274. break;
  275. case COM_CREATEBUTTON:
  276. if (p != -1) {
  277. if (r == 1 || r == 2)
  278. result[r] = atoi(expression);
  279. else if (r == 3)
  280. strcpy(comfile1, expression);
  281. else if (r == 4)
  282. strcpy(comfile2, expression);
  283. }
  284. break;
  285. }
  286. r++;
  287. }
  288. }
  289. void Parse_RandomLine(char *expression, int *result, int res_size)
  290. {
  291. char text[32];
  292. int c = 11, i = 0;
  293. int done = 0;
  294. while (!done) {
  295. result[c] = atoi(&expression[i]);
  296. i += strlen(itoa(result[c], text, 10));
  297. if (expression[i] != ';' || i >= (int) strlen(expression))
  298. break;
  299. i++;
  300. c++;
  301. if (c >= res_size - 1)
  302. break;
  303. }
  304. c++;
  305. for (i = c; c < res_size; c++)
  306. result[i] = -1;
  307. result[17] = c - 12;
  308. }
  309. //------------------------------------------------------------------------------------------------
  310. // naparsuje radek animace
  311. //------------------------------------------------------------------------------------------------
  312. void Parse_AnimLine(FILE * file, int *result, int res_size)
  313. {
  314. char text[256], expression[256];
  315. int p = 0, r = 0, i;
  316. if (!fgets(text, 256, file)) {
  317. result[0] = COM_NOCOMMAND;
  318. return;
  319. }
  320. for (i = 0; i < res_size; i++)
  321. result[i] = -1;
  322. while ((p != -1) && (r < res_size)) {
  323. p = Find_Next_Expresion(text, p, expression);
  324. /*if(p != -1)
  325. result[r] = atoi(expression); */
  326. if (p == -1)
  327. break;
  328. if (r < 11)
  329. result[r] = atoi(expression);
  330. else if (expression[0] == '?')
  331. Parse_RandomLine(&expression[1], result, res_size);
  332. r++;
  333. }
  334. }
  335. //------------------------------------------------------------------------------------------------
  336. // naparsuje radek skriptu hry
  337. //------------------------------------------------------------------------------------------------
  338. void Parse_ScenarioLine(FILE * file, int *result, int res_size,
  339. char *comfile1, char *comfile2)
  340. {
  341. char text[256], expression[256];
  342. int p = 0, r = 1;
  343. if (fgets(text, 256, file) == NULL) {
  344. result[0] = COM_NOCOMMAND;
  345. return;
  346. }
  347. p = Find_Next_Expresion(text, p, expression);
  348. if (p != -1)
  349. result[0] = String2Command(expression);
  350. else
  351. result[0] = COM_NOCOMMAND;
  352. while (p != -1) {
  353. p = Find_Next_Expresion(text, p, expression);
  354. switch (r) {
  355. case 1:
  356. strcpy(comfile1, expression);
  357. break;
  358. case 2:
  359. strcpy(comfile2, expression);
  360. break;
  361. }
  362. r++;
  363. }
  364. }
  365. void LoadMenuScript(char *p_File_Name, CMD_LINE * res, int *lastcmd)
  366. {
  367. char filename[MAX_FILENAME];
  368. FILE *file;
  369. construct_path(filename, MAX_FILENAME, 2, pDataDir, p_File_Name);
  370. file = fopen(filename, "r");
  371. if (file) {
  372. while (!feof(file)) {
  373. Parse_Line(file, res[*lastcmd].iParam, 6, res[*lastcmd].cParam[0],
  374. res[*lastcmd].cParam[1]);
  375. res[*lastcmd].uiTimerID = 0;
  376. res[*lastcmd].iLastfrm = 1;
  377. res[*lastcmd].iCounter = 0;
  378. (*lastcmd)++;
  379. }
  380. fclose(file);
  381. }
  382. (*lastcmd)--;
  383. }
  384. void LoadAnimationMenuScript(CMD_LINE * res, int i, int *lastanm)
  385. {
  386. char filename[MAX_FILENAME];
  387. FILE *file;
  388. construct_path(filename, MAX_FILENAME, 2,
  389. pDataDir, res[i].cParam[0]);
  390. file = fopen(filename, "r");
  391. if (file) {
  392. while (!feof(file)) {
  393. Parse_AnimLine(file, res[i].iAnim[*lastanm], 18);
  394. (*lastanm)++;
  395. }
  396. fclose(file);
  397. }
  398. }