path.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Peter Unold 1992 *
  24. * recoded by Igor van den Hoven 2004 *
  25. ******************************************************************************/
  26. #include "tintin.h"
  27. DO_COMMAND(do_path)
  28. {
  29. int cnt;
  30. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  31. if (*arg1 == 0)
  32. {
  33. info:
  34. tintin_header(ses, " PATH OPTIONS ");
  35. for (cnt = 0 ; *path_table[cnt].fun != NULL ; cnt++)
  36. {
  37. if (*path_table[cnt].desc)
  38. {
  39. tintin_printf2(ses, " [%-13s] %s", path_table[cnt].name, path_table[cnt].desc);
  40. }
  41. }
  42. tintin_header(ses, "");
  43. return ses;
  44. }
  45. else
  46. {
  47. for (cnt = 0 ; *path_table[cnt].name ; cnt++)
  48. {
  49. if (is_abbrev(arg1, path_table[cnt].name))
  50. {
  51. break;
  52. }
  53. }
  54. if (*path_table[cnt].name == 0)
  55. {
  56. goto info;
  57. }
  58. else
  59. {
  60. path_table[cnt].fun(ses, arg);
  61. }
  62. }
  63. return ses;
  64. }
  65. DO_PATH(path_create)
  66. {
  67. struct listroot *root = ses->list[LIST_PATH];
  68. kill_list(root);
  69. root->update = 0;
  70. show_message(ses, LIST_COMMAND, "#PATH CREATE: YOU START MAPPING A NEW PATH.");
  71. SET_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  72. }
  73. DO_PATH(path_destroy)
  74. {
  75. struct listroot *root = ses->list[LIST_PATH];
  76. kill_list(root);
  77. root->update = 0;
  78. DEL_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  79. show_message(ses, LIST_COMMAND, "#PATH DESTROY: PATH DESTROYED.");
  80. }
  81. DO_PATH(path_start)
  82. {
  83. if (HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  84. {
  85. show_message(ses, LIST_COMMAND, "#PATH START: ERROR: YOU ARE ALREADY MAPPING A PATH.");
  86. }
  87. else
  88. {
  89. SET_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  90. show_message(ses, LIST_COMMAND, "#PATH START: YOU START MAPPING A PATH.");
  91. }
  92. }
  93. DO_PATH(path_stop)
  94. {
  95. int index;
  96. struct listroot *root = ses->list[LIST_PATH];
  97. if (HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  98. {
  99. show_message(ses, LIST_COMMAND, "#PATH STOP: YOU STOP MAPPING A PATH.");
  100. DEL_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  101. }
  102. else
  103. {
  104. if (root->list[root->update]->val64)
  105. {
  106. for (index = 0 ; index < root->used ; index++)
  107. {
  108. root->list[index]->val64 = 0;
  109. }
  110. show_message(ses, LIST_COMMAND, "#PATH STOP: YOU STOP RUNNING A PATH.");
  111. }
  112. else
  113. {
  114. show_message(ses, LIST_COMMAND, "#PATH STOP: YOU ARE NOT MAPPING OR RUNNING A PATH.");
  115. }
  116. }
  117. }
  118. DO_PATH(path_describe)
  119. {
  120. char *dirs[] = {
  121. "west", "west-southwest", "southwest", "south-southwest",
  122. "south", "south-southeast", "southeast", "east-southeast",
  123. "east", "east-northeast", "northeast", "north-northeast",
  124. "north", "north-northwest", "northwest", "west-northwest",
  125. "west" };
  126. char *slopes[] = {
  127. "a steep upward slope", "a steady upward slope", "a slight upward slope",
  128. "the same level",
  129. "a slight downward slope", "a steady downward slope", "a steep downward slope" };
  130. struct listroot *root = ses->list[LIST_PATH];
  131. struct listnode *node;
  132. int a, d, i, s, x, y, z;
  133. i = x = y = z = 0;
  134. i = root->update;
  135. while (i < root->used)
  136. {
  137. node = search_node_list(ses->list[LIST_PATHDIR], root->list[i++]->arg1);
  138. if (node)
  139. {
  140. x += (HAS_BIT(atoi(node->arg3), MAP_EXIT_E) ? 1 : HAS_BIT(atoi(node->arg3), MAP_EXIT_W) ? -1 : 0);
  141. y += (HAS_BIT(atoi(node->arg3), MAP_EXIT_N) ? 1 : HAS_BIT(atoi(node->arg3), MAP_EXIT_S) ? -1 : 0);
  142. z += (HAS_BIT(atoi(node->arg3), MAP_EXIT_U) ? 1 : HAS_BIT(atoi(node->arg3), MAP_EXIT_D) ? -1 : 0);
  143. }
  144. }
  145. a = sqrt(x * x + y * y);
  146. a = sqrt(a * a + z * z);
  147. d = round(16 * (atan2(y, x) + M_PI) / (M_PI * 2));
  148. s = round(12 * (atan2(a, z) - M_PI / 4) / M_PI);
  149. if (x == 0 && y == 0)
  150. {
  151. if (z == 0)
  152. {
  153. if (root->used > 2)
  154. {
  155. tintin_printf2(ses, "The path is %d rooms long and leads back to where you are at.", root->used);
  156. }
  157. else
  158. {
  159. tintin_printf2(ses, "The path is %d rooms long and the destination is right where you are at.", root->used);
  160. }
  161. }
  162. else
  163. {
  164. tintin_printf2(ses, "The path is %d rooms long and the destination lies %d rooms %s of you.", root->used, abs(z), z < 0 ? "below" : "above", s);
  165. }
  166. }
  167. else
  168. {
  169. tintin_printf2(ses, "The path is %d rooms long and the destination lies %d rooms to the %s of you at %s.", root->used, a, dirs[d], slopes[s]);
  170. }
  171. if (root->update == 0)
  172. {
  173. tintin_printf2(ses, "You are at the start of the path.");
  174. }
  175. else if (root->update == root->used)
  176. {
  177. tintin_printf2(ses, "You are at the end of the path.");
  178. }
  179. else
  180. {
  181. tintin_printf2(ses, "You've traversed %d out of %d steps of the path.", root->update, root->used);
  182. }
  183. }
  184. DO_PATH(path_map)
  185. {
  186. struct listroot *root = ses->list[LIST_PATH];
  187. char buf[BUFFER_SIZE];
  188. int i = 0;
  189. if (root->used == 0)
  190. {
  191. show_message(ses, LIST_COMMAND, "#PATH MAP: EMPTY PATH.");
  192. }
  193. else
  194. {
  195. sprintf(buf, "%-7s", "#PATH:");
  196. for (i = 0 ; i < root->update ; i++)
  197. {
  198. cat_sprintf(buf, " %s", root->list[i]->arg1);
  199. }
  200. if (i != root->used)
  201. {
  202. cat_sprintf(buf, " [%s]", root->list[i++]->arg1);
  203. for (i = root->update + 1 ; i < root->used ; i++)
  204. {
  205. cat_sprintf(buf, " %s", root->list[i]->arg1);
  206. }
  207. }
  208. if (root->update == root->used)
  209. {
  210. cat_sprintf(buf, " [ ]");
  211. }
  212. tintin_puts2(ses, buf);
  213. }
  214. }
  215. DO_PATH(path_get)
  216. {
  217. struct listroot *root = ses->list[LIST_PATH];
  218. char result[STRING_SIZE], arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  219. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  220. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  221. if (*arg2 == 0)
  222. {
  223. tintin_printf2(ses, " length: %d", root->used);
  224. tintin_printf2(ses, "position: %d", root->update + 1);
  225. }
  226. else if (is_abbrev(arg1, "LENGTH"))
  227. {
  228. sprintf(result, "%d", root->used);
  229. set_nest_node_ses(ses, arg2, "%s", result);
  230. show_message(ses, LIST_COMMAND, "#PATH GET: PATH LENGTH {%s} SAVED TO {%s}", result, arg2);
  231. }
  232. else if (is_abbrev(arg1, "POSITION"))
  233. {
  234. sprintf(result, "%d", root->update + 1);
  235. set_nest_node_ses(ses, arg2, "%s", result);
  236. show_message(ses, LIST_COMMAND, "#PATH GET: PATH POSITION {%s} SAVED TO {%s}", result, arg2);
  237. }
  238. else
  239. {
  240. show_error(ses, LIST_COMMAND, "#SYNTAX: #PATH GET <LENGTH|POSITION> <VARIABLE NAME>");
  241. }
  242. }
  243. DO_PATH(path_save)
  244. {
  245. struct listroot *root = ses->list[LIST_PATH];
  246. char result[STRING_SIZE], arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  247. int i;
  248. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  249. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  250. if (root->used == 0)
  251. {
  252. tintin_puts2(ses, "#PATH SAVE: LOAD OR CREATE A PATH FIRST.");
  253. }
  254. else if (*arg2 == 0)
  255. {
  256. show_error(ses, LIST_COMMAND, "#SYNTAX: #PATH SAVE <BACKWARD|FORWARD> <VARIABLE NAME>");
  257. }
  258. else if (is_abbrev(arg1, "BACKWARDS"))
  259. {
  260. result[0] = 0;
  261. for (i = root->used - 1 ; i >= 0 ; i--)
  262. {
  263. strcat(result, root->list[i]->arg2);
  264. if (i != 0)
  265. {
  266. cat_sprintf(result, "%c", COMMAND_SEPARATOR);
  267. }
  268. }
  269. set_nest_node_ses(ses, arg2, "%s", result);
  270. show_message(ses, LIST_COMMAND, "#PATH SAVE: BACKWARD PATH SAVED TO {%s}", arg2);
  271. }
  272. else if (is_abbrev(arg1, "FORWARDS"))
  273. {
  274. result[0] = 0;
  275. for (i = 0 ; i < root->used ; i++)
  276. {
  277. strcat(result, root->list[i]->arg1);
  278. if (i != root->used - 1)
  279. {
  280. cat_sprintf(result, "%c", COMMAND_SEPARATOR);
  281. }
  282. }
  283. set_nest_node_ses(ses, arg2, "%s", result);
  284. show_message(ses, LIST_COMMAND, "#PATH SAVE: FORWARD PATH SAVED TO {%s}", arg2);
  285. }
  286. else if (is_abbrev(arg1, "LENGTH"))
  287. {
  288. sprintf(result, "%d", root->used);
  289. set_nest_node_ses(ses, arg2, "%s", result);
  290. show_message(ses, LIST_COMMAND, "#PATH SAVE: PATH LENGTH SAVED TO {%s}", arg2);
  291. }
  292. else if (is_abbrev(arg1, "POSITION"))
  293. {
  294. sprintf(result, "%d", root->update + 1);
  295. set_nest_node_ses(ses, arg2, "%s", result);
  296. show_message(ses, LIST_COMMAND, "#PATH SAVE: PATH POSITION SAVED TO {%s}", arg2);
  297. }
  298. else
  299. {
  300. show_error(ses, LIST_COMMAND, "#SYNTAX: #PATH SAVE <BACKWARD|FORWARD> <VARIABLE NAME>");
  301. }
  302. }
  303. DO_PATH(path_load)
  304. {
  305. struct listroot *root = ses->list[LIST_PATH];
  306. char arg1[BUFFER_SIZE], temp[BUFFER_SIZE];
  307. struct listnode *node;
  308. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  309. if ((node = search_nest_node_ses(ses, arg1)) == NULL)
  310. {
  311. arg = arg1;
  312. }
  313. else
  314. {
  315. arg = node->arg2;
  316. }
  317. kill_list(root);
  318. root->update = 0;
  319. while (*arg)
  320. {
  321. if (*arg == ';')
  322. {
  323. arg++;
  324. }
  325. arg = get_arg_in_braces(ses, arg, temp, GET_ALL);
  326. if ((node = search_node_list(root, temp)))
  327. {
  328. create_node_list(root, node->arg1, node->arg2, "0", "");
  329. }
  330. else
  331. {
  332. create_node_list(root, temp, temp, "0", "");
  333. }
  334. }
  335. show_message(ses, LIST_COMMAND, "#PATH LOAD: PATH WITH %d NODES LOADED.", root->used);
  336. }
  337. DO_PATH(path_delete)
  338. {
  339. struct listroot *root = ses->list[LIST_PATH];
  340. if (root->used)
  341. {
  342. show_message(ses, LIST_COMMAND, "#PATH DELETE: DELETED MOVE {%s}.", root->list[root->used - 1]->arg1);
  343. delete_index_list(root, root->used - 1);
  344. if (root->update >= root->used)
  345. {
  346. root->update--;
  347. }
  348. }
  349. else
  350. {
  351. tintin_puts(ses, "#PATH DELETE: NO MOVES LEFT.");
  352. }
  353. }
  354. DO_PATH(path_insert)
  355. {
  356. struct listroot *root = ses->list[LIST_PATH];
  357. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  358. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  359. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  360. if (*arg1 == 0 && *arg2 == 0)
  361. {
  362. show_message(ses, LIST_COMMAND, "#PATH INSERT: ERROR: YOU MUST GIVE A COMMAND TO INSERT");
  363. }
  364. else
  365. {
  366. create_node_list(root, arg1, arg2, "0", "");
  367. show_message(ses, LIST_COMMAND, "#PATH INSERT: FORWARD {%s} BACKWARD {%s}.", arg1, arg2);
  368. if (HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  369. {
  370. root->update = root->used;
  371. }
  372. }
  373. }
  374. DO_PATH(path_run)
  375. {
  376. int index;
  377. struct listroot *root = ses->list[LIST_PATH];
  378. char arg1[BUFFER_SIZE];
  379. long long total, delay;
  380. push_call("path_run(%p,%p)",ses,arg);
  381. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  382. if (root->update == root->used)
  383. {
  384. tintin_puts(ses, "#PATH RUN: #END OF PATH.");
  385. }
  386. else
  387. {
  388. if (*arg1)
  389. {
  390. delay = (long long) (get_number(ses, arg1) * 1000000.0);
  391. total = 0;
  392. DEL_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  393. for (index = root->update ; index < root->used ; index++)
  394. {
  395. root->list[index]->val64 = gtd->utime + total;
  396. total += delay;
  397. }
  398. }
  399. else
  400. {
  401. while (root->update < root->used)
  402. {
  403. script_driver(ses, LIST_COMMAND, root->list[root->update++]->arg1);
  404. }
  405. }
  406. }
  407. pop_call();
  408. return;
  409. }
  410. DO_PATH(path_walk)
  411. {
  412. struct listroot *root = ses->list[LIST_PATH];
  413. char arg1[BUFFER_SIZE];
  414. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  415. DEL_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  416. if (is_abbrev(arg1, "BACKWARDS") || is_abbrev(arg1, "-1"))
  417. {
  418. if (root->update == 0)
  419. {
  420. tintin_puts(ses, "#PATH WALK: #START OF PATH.");
  421. }
  422. else
  423. {
  424. script_driver(ses, LIST_COMMAND, root->list[--root->update]->arg2);
  425. if (root->update == 0)
  426. {
  427. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "START OF PATH");
  428. }
  429. }
  430. }
  431. else if (*arg1 == 0 || is_abbrev(arg1, "FORWARDS") || is_abbrev(arg1, "+1"))
  432. {
  433. if (root->update == root->used)
  434. {
  435. tintin_puts(ses, "#PATH WALK: #END OF PATH.");
  436. }
  437. else
  438. {
  439. script_driver(ses, LIST_COMMAND, root->list[root->update++]->arg1);
  440. if (root->update == root->used)
  441. {
  442. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "END OF PATH");
  443. }
  444. }
  445. }
  446. else
  447. {
  448. show_error(ses, LIST_COMMAND, "#SYNTAX: #PATH WALK {FORWARD|BACKWARD}.");
  449. }
  450. }
  451. DO_PATH(path_swap)
  452. {
  453. struct listroot *root = ses->list[LIST_PATH];
  454. struct listnode *node;
  455. int a, z;
  456. if (root->used == 0)
  457. {
  458. show_error(ses, LIST_COMMAND, "#PATH SWAP: ERROR: PATH IS EMPTY.");
  459. return;
  460. }
  461. a = 0;
  462. z = root->used - 1;
  463. if (root->update)
  464. {
  465. root->update = root->used - root->update;
  466. }
  467. while (z > a)
  468. {
  469. arg = root->list[z]->arg1;
  470. root->list[z]->arg1 = root->list[z]->arg2;
  471. root->list[z]->arg2 = arg;
  472. arg = root->list[a]->arg1;
  473. root->list[a]->arg1 = root->list[a]->arg2;
  474. root->list[a]->arg2 = arg;
  475. node = root->list[z];
  476. root->list[z--] = root->list[a];
  477. root->list[a++] = node;
  478. }
  479. if (z == a)
  480. {
  481. arg = root->list[z]->arg1;
  482. root->list[z]->arg1 = root->list[z]->arg2;
  483. root->list[z]->arg2 = arg;
  484. }
  485. show_message(ses, LIST_COMMAND, "#PATH SWAP: PATH HAS BEEN SWAPPED.");
  486. }
  487. DO_PATH(path_zip)
  488. {
  489. struct listroot *root = ses->list[LIST_PATH];
  490. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  491. int i, cnt;
  492. cnt = 1;
  493. *arg1 = 0;
  494. *arg2 = 0;
  495. for (i = 0 ; i < root->used ; i++)
  496. {
  497. if (search_node_list(root, root->list[i]->arg1) == NULL || strlen(root->list[i]->arg1) != 1)
  498. {
  499. if (i && search_node_list(root, root->list[i - 1]->arg1) != NULL && strlen(root->list[i - 1]->arg1) == 1)
  500. {
  501. cat_sprintf(arg1, "%c", COMMAND_SEPARATOR);
  502. }
  503. cat_sprintf(arg1, "%s", root->list[i]->arg1);
  504. if (i < root->used - 1)
  505. {
  506. cat_sprintf(arg1, "%c", COMMAND_SEPARATOR);
  507. }
  508. continue;
  509. }
  510. if (i < root->used - 1 && !strcmp(root->list[i]->arg1, root->list[i + 1]->arg1))
  511. {
  512. cnt++;
  513. }
  514. else
  515. {
  516. cat_sprintf(arg1, "%d%s", cnt, root->list[i]->arg1);
  517. cnt = 1;
  518. }
  519. }
  520. for (i = root->used - 1 ; i >= 0 ; i--)
  521. {
  522. if (search_node_list(root, root->list[i]->arg2) == NULL || strlen(root->list[i]->arg2) != 1)
  523. {
  524. if (i != root->used - 1 && search_node_list(root, root->list[i + 1]->arg2) != NULL && strlen(root->list[i + 1]->arg2) == 1)
  525. {
  526. cat_sprintf(arg2, "%c", COMMAND_SEPARATOR);
  527. }
  528. cat_sprintf(arg2, "%s", root->list[i]->arg2);
  529. if (i > 0)
  530. {
  531. cat_sprintf(arg2, "%c", COMMAND_SEPARATOR);
  532. }
  533. continue;
  534. }
  535. if (i > 0 && !strcmp(root->list[i]->arg2, root->list[i - 1]->arg2))
  536. {
  537. cnt++;
  538. }
  539. else
  540. {
  541. cat_sprintf(arg2, "%d%s", cnt, root->list[i]->arg2);
  542. cnt = 1;
  543. }
  544. }
  545. root->update = root->used;
  546. kill_list(root);
  547. create_node_list(root, arg1, arg2, "0", "");
  548. show_message(ses, LIST_COMMAND, "#PATH ZIP: THE PATH HAS BEEN ZIPPED TO {%s} {%s}.", arg1, arg2);
  549. }
  550. DO_PATH(path_unzip)
  551. {
  552. struct listroot *root = ses->list[LIST_PATH];
  553. struct listnode *node;
  554. char name[BUFFER_SIZE], num[NUMBER_SIZE], *ptn;
  555. char arg1[BUFFER_SIZE];
  556. int cnt, max;
  557. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  558. if ((node = search_nest_node_ses(ses, arg1)) == NULL)
  559. {
  560. arg = arg1;
  561. }
  562. else
  563. {
  564. arg = node->arg2;
  565. }
  566. kill_list(root);
  567. root->update = 0;
  568. while (*arg)
  569. {
  570. switch (*arg)
  571. {
  572. case ';':
  573. case ' ':
  574. arg++;
  575. continue;
  576. }
  577. if (isdigit((int) *arg))
  578. {
  579. ptn = num;
  580. while (isdigit((int) *arg))
  581. {
  582. if (ptn - num < 5)
  583. {
  584. *ptn++ = *arg++;
  585. }
  586. else
  587. {
  588. arg++;
  589. }
  590. }
  591. *ptn = 0;
  592. max = atoi(num);
  593. }
  594. else
  595. {
  596. max = 1;
  597. }
  598. arg = get_arg_stop_digits(ses, arg, name, GET_ONE);
  599. if (*name == 0)
  600. {
  601. break;
  602. }
  603. node = search_node_list(ses->list[LIST_PATHDIR], name);
  604. if (node)
  605. {
  606. for (cnt = 0 ; cnt < max ; cnt++)
  607. {
  608. create_node_list(root, node->arg1, node->arg2, "0", "");
  609. }
  610. }
  611. else
  612. {
  613. for (cnt = 0 ; cnt < max ; cnt++)
  614. {
  615. create_node_list(root, name, name, "0", "");
  616. }
  617. }
  618. }
  619. /*
  620. arg = get_arg_in_braces(ses, arg, temp, GET_ALL);
  621. if (is_speedwalk(ses, temp))
  622. {
  623. char dir[2];
  624. int cnt, i;
  625. str = temp;
  626. for (dir[1] = 0 ; *str ; str++)
  627. {
  628. if (isdigit((int) *str))
  629. {
  630. sscanf(str, "%d%c", &cnt, dir);
  631. while (*str != dir[0])
  632. {
  633. str++;
  634. }
  635. }
  636. else
  637. {
  638. cnt = 1;
  639. dir[0] = *str;
  640. }
  641. for (i = 0 ; i < cnt ; i++)
  642. {
  643. if ((node = search_node_list(ses->list[LIST_PATHDIR], dir)))
  644. {
  645. create_node_list(root, node->arg1, node->arg2, "0", "");
  646. }
  647. else
  648. {
  649. create_node_list(root, dir, dir, "0", "");
  650. }
  651. }
  652. }
  653. }
  654. else
  655. {
  656. if ((node = search_node_list(ses->list[LIST_PATHDIR], temp)))
  657. {
  658. create_node_list(root, node->arg1, node->arg2, "0", "");
  659. }
  660. else
  661. {
  662. create_node_list(root, temp, temp, "0", "");
  663. }
  664. }
  665. }
  666. */
  667. show_message(ses, LIST_COMMAND, "#PATH UNZIP: PATH WITH %d NODES UNZIPPED.", root->used);
  668. }
  669. DO_PATH(path_goto)
  670. {
  671. struct listroot *root = ses->list[LIST_PATH];
  672. char arg1[BUFFER_SIZE];
  673. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  674. if (is_abbrev(arg1, "END"))
  675. {
  676. root->update = root->used;
  677. show_message(ses, LIST_COMMAND, "#PATH GOTO: POSITION SET TO {%d}.", root->update + 1);
  678. }
  679. else if (is_abbrev(arg1, "START"))
  680. {
  681. root->update = 0;
  682. show_message(ses, LIST_COMMAND, "#PATH GOTO: POSITION SET TO %d.", root->update + 1);
  683. }
  684. else if (is_math(ses, arg1))
  685. {
  686. if (get_number(ses, arg1) < 1 || get_number(ses, arg1) > root->used + 1)
  687. {
  688. show_message(ses, LIST_COMMAND, "#PATH GOTO: POSITION MUST BE BETWEEN 1 AND %d.", root->used + 1);
  689. }
  690. else
  691. {
  692. root->update = get_number(ses, arg1) - 1;
  693. show_message(ses, LIST_COMMAND, "#PATH GOTO: POSITION SET TO %d.", root->update + 1);
  694. }
  695. }
  696. }
  697. DO_PATH(path_move)
  698. {
  699. struct listroot *root = ses->list[LIST_PATH];
  700. char arg1[BUFFER_SIZE];
  701. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  702. if (is_abbrev(arg1, "BACKWARD"))
  703. {
  704. if (root->update == 0)
  705. {
  706. show_message(ses, LIST_COMMAND, "#PATH GOTO: ALREADY AT START OF PATH.");
  707. }
  708. else
  709. {
  710. root->update--;
  711. show_message(ses, LIST_COMMAND, "#PATH MOVE: POSITION SET TO %d.", root->update + 1);
  712. }
  713. }
  714. else if (is_abbrev(arg1, "FORWARD"))
  715. {
  716. if (root->update == root->used)
  717. {
  718. show_message(ses, LIST_COMMAND, "#PATH MOVE: ALREADY AT END OF PATH.");
  719. }
  720. else
  721. {
  722. root->update++;
  723. show_message(ses, LIST_COMMAND, "#PATH MOVE: POSITION SET TO %d.", root->update + 1);
  724. }
  725. }
  726. else if (is_math(ses, arg1))
  727. {
  728. int last = root->update;
  729. root->update = URANGE(0, root->update + get_number(ses, arg1), root->used);
  730. show_message(ses, LIST_COMMAND, "#PATH MOVE: POSITION MOVED FROM %d TO %d.", last + 1, root->update + 1);
  731. }
  732. }
  733. DO_PATH(path_undo)
  734. {
  735. struct listroot *root = ses->list[LIST_PATH];
  736. if (root->used == 0)
  737. {
  738. show_message(ses, LIST_COMMAND, "#PATH UNDO: ERROR: PATH IS EMPTY.");
  739. return;
  740. }
  741. if (root->update != root->used)
  742. {
  743. show_message(ses, LIST_COMMAND, "#PATH UNDO: ERROR: YOUR POSITION IS NOT AT END OF PATH.");
  744. return;
  745. }
  746. if (!HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  747. {
  748. show_message(ses, LIST_COMMAND, "#PATH UNDO: ERROR: YOU ARE NOT CURRENTLY MAPPING A PATH.");
  749. return;
  750. }
  751. DEL_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  752. script_driver(ses, LIST_COMMAND, root->list[root->used - 1]->arg2);
  753. SET_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  754. delete_index_list(root, root->used - 1);
  755. root->update = root->used;
  756. show_message(ses, LIST_COMMAND, "#PATH MOVE: POSITION SET TO %d.", root->update);
  757. }
  758. void check_append_path(struct session *ses, char *forward, char *backward, int follow)
  759. {
  760. struct listroot *root = ses->list[LIST_PATH];
  761. struct listnode *node;
  762. if (follow)
  763. {
  764. if ((node = search_node_list(ses->list[LIST_PATHDIR], forward)))
  765. {
  766. create_node_list(root, node->arg1, node->arg2, "0", "");
  767. root->update = root->used;
  768. }
  769. }
  770. else
  771. {
  772. if ((node = search_node_list(ses->list[LIST_PATHDIR], forward)))
  773. {
  774. create_node_list(root, node->arg1, node->arg2, "0", "");
  775. }
  776. else
  777. {
  778. create_node_list(root, forward, backward, "0", "");
  779. }
  780. }
  781. }
  782. DO_COMMAND(do_pathdir)
  783. {
  784. struct listnode *node;
  785. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  786. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  787. arg = sub_arg_in_braces(ses, arg, arg3, GET_ONE, SUB_VAR|SUB_FUN);
  788. if (*arg1 == 0)
  789. {
  790. show_list(ses->list[LIST_PATHDIR], 0);
  791. }
  792. else if (*arg2 == 0)
  793. {
  794. if (show_node_with_wild(ses, arg1, ses->list[LIST_PATHDIR]) == FALSE)
  795. {
  796. show_message(ses, LIST_PATHDIR, "#NO MATCH(ES) FOUND FOR {%s}.", arg1);
  797. }
  798. }
  799. else
  800. {
  801. if (*arg3 == 0)
  802. {
  803. if ((node = search_node_list(ses->list[LIST_PATHDIR], arg1)) != NULL)
  804. {
  805. strcpy(arg3, node->arg3);
  806. }
  807. else
  808. {
  809. strcpy(arg3, "0");
  810. }
  811. }
  812. else
  813. {
  814. if (!is_math(ses, arg3) || get_number(ses, arg3) < 0 || get_number(ses, arg3) >= 64)
  815. {
  816. show_message(ses, LIST_PATHDIR, "#PATHDIR: THE THIRD ARGUMENT MUST BE A NUMBER BETWEEN 0 and 63.");
  817. return ses;
  818. }
  819. get_number_string(ses, arg3, arg3);
  820. }
  821. node = update_node_list(ses->list[LIST_PATHDIR], arg1, arg2, arg3, "");
  822. node->val32[0] = atoi(arg3);
  823. show_message(ses, LIST_PATHDIR, "#OK: DIRECTION {%s} WILL BE REVERSED AS {%s} @ {%d}.", arg1, arg2, atoi(arg3));
  824. }
  825. return ses;
  826. }
  827. DO_COMMAND(do_unpathdir)
  828. {
  829. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  830. do
  831. {
  832. if (delete_nest_node(ses->list[LIST_PATHDIR], arg1))
  833. {
  834. show_message(ses, LIST_PATHDIR, "#OK. {%s} IS NO LONGER A PATHDIR.", arg1);
  835. }
  836. else
  837. {
  838. delete_node_with_wild(ses, LIST_PATHDIR, arg1);
  839. }
  840. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  841. }
  842. while (*arg1);
  843. return ses;
  844. }
  845. int exit_to_dir(struct session *ses, char *name)
  846. {
  847. struct listnode *node;
  848. node = search_node_list(ses->list[LIST_PATHDIR], name);
  849. if (node)
  850. {
  851. return atoi(node->arg3);
  852. }
  853. else
  854. {
  855. return 0;
  856. }
  857. }
  858. char *dir_to_exit(struct session *ses, int dir)
  859. {
  860. struct listroot *root;
  861. struct listnode *node;
  862. if (dir <= 0 || dir >= 64)
  863. {
  864. return "";
  865. }
  866. root = ses->list[LIST_PATHDIR];
  867. root->update = 0;
  868. while (root->update < root->used)
  869. {
  870. node = root->list[root->update];
  871. if (node->val32[0] == dir)
  872. {
  873. return node->arg1;
  874. }
  875. root->update++;
  876. }
  877. return "";
  878. }
  879. // Old commands, left for backward compatibility
  880. DO_PATH(path_new)
  881. {
  882. struct listroot *root = ses->list[LIST_PATH];
  883. if (HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  884. {
  885. show_message(ses, LIST_COMMAND, "#PATH NEW: YOU ARE ALREADY MAPPING A PATH.");
  886. }
  887. else
  888. {
  889. kill_list(root);
  890. root->update = 0;
  891. show_message(ses, LIST_COMMAND, "#PATH NEW: YOU ARE NOW MAPPING A PATH.");
  892. SET_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  893. }
  894. }
  895. DO_PATH(path_end)
  896. {
  897. if (HAS_BIT(ses->flags, SES_FLAG_PATHMAPPING))
  898. {
  899. show_message(ses, LIST_COMMAND, "#PATH END: YOU ARE NO LONGER MAPPING A PATH.");
  900. DEL_BIT(ses->flags, SES_FLAG_PATHMAPPING);
  901. }
  902. else
  903. {
  904. show_message(ses, LIST_COMMAND, "#PATH: YOU ARE NOT MAPPING A PATH.");
  905. }
  906. }