trigers.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "3d_all.h"
  4. #include "Berusky3d_kofola_interface.h"
  5. #include "trigers.h"
  6. #include "menu_script.h"
  7. #include "game_logic.h"
  8. extern char pGDataDir[MAX_FILENAME];
  9. int Find_Next_ExpresionU(WCHAR * p_Command, int start, WCHAR * p_Expresion);
  10. int trig_Commandtoi(char *pCommand, GRAMMAR * pGr, int *iMask)
  11. {
  12. int i;
  13. for (i = 0; i < pGr->LastMask; i++)
  14. if (!strcmp(pCommand, pGr->Mask[i].cCommand)) {
  15. *iMask = i;
  16. return pGr->Mask[i].iCommand;
  17. }
  18. return -1;
  19. }
  20. void trig_Parse_Line(char *pLine, COMMAND * pCommand, GAME_TRIGER * pTriger,
  21. GRAMMAR * pGr, TRIGER_STRUCTURE * pTStruct)
  22. {
  23. char Expression[64];
  24. int cursor = 0, par = 0, iMask;
  25. // int cursoru = 1;
  26. float fValue;
  27. cursor = Find_Next_Expresion(pLine, cursor, Expression);
  28. strcpy(pCommand->cCommand, Expression);
  29. pCommand->iCommand = trig_Commandtoi(Expression, pGr, &iMask);
  30. if (pCommand->iCommand == -1)
  31. return;
  32. while (cursor != -1) {
  33. cursor = Find_Next_Expresion(pLine, cursor, Expression);
  34. // cursoru = cursor + 1;
  35. if (cursor != -1) {
  36. switch (pGr->Mask[iMask].Parametr[par].Type) {
  37. //int
  38. case 0:
  39. pCommand->Parametr[par].Type = 0;
  40. pCommand->Parametr[par].Value = atoi(Expression);
  41. break;
  42. //float
  43. case 1:
  44. pCommand->Parametr[par].Type = 1;
  45. fValue = (float) atof(Expression);
  46. memcpy((void *) &pCommand->Parametr[par].Value, (void *) &fValue,
  47. sizeof(float));
  48. break;
  49. //string
  50. case 2:
  51. pCommand->Parametr[par].Type = 2;
  52. pCommand->Parametr[par].Value = pTStruct->LastStr;
  53. strcpy(pTStruct->StrTable[pTStruct->LastStr], Expression);
  54. pTStruct->LastStr++;
  55. break;
  56. }
  57. par++;
  58. }
  59. }
  60. pCommand->LastParam = par;
  61. pTriger->lastcommand++;
  62. }
  63. void Uni2Char(WCHAR * cUni, char *cText, int ctsize)
  64. {
  65. int cu = wcslen(cUni), i;
  66. ZeroMemory(cText, ctsize);
  67. for (i = 0; i < cu; i++)
  68. cText[i] = (char) cUni[i];
  69. }
  70. void trig_Parse_LineU(WCHAR * pLine, COMMAND * pCommand,
  71. GAME_TRIGER * pTriger, GRAMMAR * pGr,
  72. TRIGER_STRUCTURE * pTStruct)
  73. {
  74. WCHAR Expression[64];
  75. char tmp[64];
  76. int cursor = 0, par = 0, iMask;
  77. float fValue;
  78. cursor = Find_Next_ExpresionU(pLine, cursor, Expression);
  79. if (cursor == -1)
  80. return;
  81. Uni2Char(Expression, pCommand->cCommand, 64);
  82. pCommand->iCommand = trig_Commandtoi(pCommand->cCommand, pGr, &iMask);
  83. if (pCommand->iCommand == -1)
  84. return;
  85. while (cursor != -1) {
  86. cursor = Find_Next_ExpresionU(pLine, cursor, Expression);
  87. if (cursor != -1) {
  88. switch (pGr->Mask[iMask].Parametr[par].Type) {
  89. //int
  90. case 0:
  91. pCommand->Parametr[par].Type = 0;
  92. Uni2Char(Expression, tmp, 64);
  93. pCommand->Parametr[par].Value = atoi(tmp);
  94. break;
  95. //float
  96. case 1:
  97. pCommand->Parametr[par].Type = 1;
  98. Uni2Char(Expression, tmp, 64);
  99. fValue = (float) atof(tmp);
  100. memcpy((void *) &pCommand->Parametr[par].Value, (void *) &fValue,
  101. sizeof(float));
  102. break;
  103. //string
  104. case 2:
  105. pCommand->Parametr[par].Type = 2;
  106. pCommand->Parametr[par].Value = pTStruct->LastStr;
  107. wcscpy((WCHAR *) pTStruct->StrTable[pTStruct->LastStr], Expression);
  108. pTStruct->LastStr++;
  109. break;
  110. //Uni Code
  111. case 3:
  112. {
  113. pCommand->Parametr[par].Type = 3;
  114. ZeroMemory((void *) &pCommand->Parametr[par].Value,
  115. sizeof(DWORD));
  116. if (Expression[0] == MAKEWORD('\\', 0))
  117. memcpy((void *) &pCommand->Parametr[par].Value,
  118. (void *) &Expression[1], 1 * sizeof(WCHAR));
  119. else
  120. memcpy((void *) &pCommand->Parametr[par].Value,
  121. (void *) Expression, 1 * sizeof(WCHAR));
  122. }
  123. break;
  124. }
  125. par++;
  126. }
  127. }
  128. pCommand->LastParam = par;
  129. pTriger->lastcommand++;
  130. }
  131. char trig_Load_Triger(char *pFile, GAME_TRIGER * pTriger, GRAMMAR * pGr,
  132. TRIGER_STRUCTURE * pTStruct)
  133. {
  134. FILE *file;
  135. char text[MAX_FILENAME];
  136. pTriger->lastcommand = 0;
  137. construct_path(text, MAX_FILENAME, 2, pGDataDir, pFile);
  138. file = fopen(text, "r");
  139. if (!file)
  140. return 0;
  141. while (!feof(file)) {
  142. if(fgets(text, 256, file)) {
  143. trig_Parse_Line(text, &pTriger->command[pTriger->lastcommand], pTriger,
  144. pGr, pTStruct);
  145. text[0] = '\0';
  146. }
  147. }
  148. fclose(file);
  149. return 1;
  150. }
  151. char trig_Load_Trigers(char *pLevel, char *pFile, TRIGER_STRUCTURE * pTStruct,
  152. GRAMMAR * pGr)
  153. {
  154. FILE *file;
  155. char text[MAX_FILENAME];
  156. int i;
  157. pTStruct->pTriger = NULL;
  158. pTStruct->sizeofT = 0;
  159. if (chdir(GAME_LEVEL_DIR))
  160. return 0;
  161. strcpy(text, pLevel);
  162. text[strlen(pLevel) - 4] = '\0'; //TODO - newline?
  163. if (chdir(text))
  164. return 0;
  165. file = fopen(pFile, "r");
  166. if (!file)
  167. return 0;
  168. if (chdir(DATA_DIR) ||
  169. fgets(text, MAX_FILENAME, file) == NULL) {
  170. fclose(file);
  171. return 0;
  172. }
  173. pTStruct->sizeofT = atoi(text);
  174. pTStruct->pTriger =
  175. (GAME_TRIGER *) malloc(pTStruct->sizeofT * sizeof(GAME_TRIGER));
  176. if (!pTStruct->pTriger) {
  177. pTStruct->sizeofT = 0;
  178. fclose(file);
  179. return 0;
  180. }
  181. pTStruct->LastStr = 0;
  182. for (i = 0; i < pTStruct->sizeofT; i++) {
  183. if (fgets(text, MAX_FILENAME, file) == NULL) {
  184. pTStruct->pTriger = NULL;
  185. pTStruct->sizeofT = 0;
  186. fclose(file);
  187. return 0;
  188. }
  189. newline_cut(text);
  190. if (!trig_Load_Triger(text, &pTStruct->pTriger[i], pGr, pTStruct))
  191. kprintf(1, "Unable to load triger %s", text);
  192. }
  193. fclose(file);
  194. return 1;
  195. }
  196. char trig_cmp(int i, char *c)
  197. {
  198. int n;
  199. if (c[0] == '?')
  200. return 1;
  201. else {
  202. n = atoi(c);
  203. if (n == i)
  204. return 1;
  205. else
  206. return 0;
  207. }
  208. }
  209. char trig_Check_ifPos_Trigers(TRIGER_STRUCTURE * pTStruct, LEVELINFO * pLevel)
  210. {
  211. int i, r;
  212. COMMAND *pCom;
  213. for (i = 0; i < pTStruct->sizeofT; i++)
  214. if (pTStruct->pTriger[i].command[0].iCommand == 1) {
  215. pCom = &pTStruct->pTriger[i].command[0];
  216. gl_Logical2Real(pCom->Parametr[2].Value, pCom->Parametr[3].Value,
  217. pCom->Parametr[4].Value, &r, pLevel);
  218. if (pLevel->Level[r])
  219. if (trig_cmp(pLevel->Level[r]->p_Object->Class,
  220. pTStruct->StrTable[pCom->Parametr[0].Value]) &&
  221. trig_cmp(pLevel->Level[r]->p_Object->SubClass,
  222. pTStruct->StrTable[pCom->Parametr[1].Value]))
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. int trig_Find_Triger(TRIGER_STRUCTURE * pTStruct, LEVELINFO * pLevel)
  228. {
  229. int i, r;
  230. COMMAND *pCom;
  231. for (i = 0; i < pTStruct->sizeofT; i++)
  232. switch (pTStruct->pTriger[i].command[0].iCommand) {
  233. case 1:
  234. {
  235. pCom = &pTStruct->pTriger[i].command[0];
  236. gl_Logical2Real(pCom->Parametr[2].Value, pCom->Parametr[3].Value,
  237. pCom->Parametr[4].Value, &r, pLevel);
  238. if (pLevel->Level[r])
  239. if (trig_cmp(pLevel->Level[r]->p_Object->Class,
  240. pTStruct->StrTable[pCom->Parametr[0].Value]) &&
  241. trig_cmp(pLevel->Level[r]->p_Object->SubClass,
  242. pTStruct->StrTable[pCom->Parametr[1].Value]))
  243. return i;
  244. }
  245. break;
  246. }
  247. return -1;
  248. }
  249. void trig_Do_MoveCameraTo(COMMAND * pCommand, int *p_flag)
  250. {
  251. BOD p_p, p_t;
  252. memcpy((void *) &p_p.x, (void *) &pCommand->Parametr[0].Value,
  253. sizeof(float));
  254. memcpy((void *) &p_p.y, (void *) &pCommand->Parametr[1].Value,
  255. sizeof(float));
  256. memcpy((void *) &p_p.z, (void *) &pCommand->Parametr[2].Value,
  257. sizeof(float));
  258. memcpy((void *) &p_t.x, (void *) &pCommand->Parametr[3].Value,
  259. sizeof(float));
  260. memcpy((void *) &p_t.y, (void *) &pCommand->Parametr[4].Value,
  261. sizeof(float));
  262. memcpy((void *) &p_t.z, (void *) &pCommand->Parametr[5].Value,
  263. sizeof(float));
  264. kam_3ds_anim(&p_p, &p_t, 0.0f, p_flag, 0, 10, 1.0f);
  265. }
  266. void trig_Do_Triger_Command(COMMAND * pCommand, int *p_flag)
  267. {
  268. switch (pCommand->iCommand) {
  269. case 2:
  270. trig_Do_MoveCameraTo(pCommand, p_flag);
  271. break;
  272. }
  273. }