PageRenderTime 82ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/slashem-0.0.8E0F1/win/proxy/callback.c

#
C | 409 lines | 381 code | 24 blank | 4 comment | 92 complexity | 167379542052957bf964711ec4489fdd MD5 | raw file
Possible License(s): LGPL-2.0
  1. /* $Id: callback.c,v 1.28 2005/02/02 09:42:13 j_ali Exp $ */
  2. /* Copyright (c) Slash'EM Development Team 2001-2004 */
  3. /* NetHack may be freely redistributed. See license for details. */
  4. /* #define DEBUG */
  5. #include <nhproxy/nhproxy.h>
  6. #include "hack.h"
  7. #include "func_tab.h"
  8. #include "winproxy.h"
  9. static void
  10. cb_display_inventory()
  11. {
  12. display_inventory((char *)0, FALSE);
  13. }
  14. #ifndef FILE_AREAS
  15. #define SET_FILE(f, a) if (1) { file = f; } else
  16. #else
  17. #define SET_FILE(f, a) if (1) { file = f; area = a; } else
  18. #endif
  19. static int
  20. cb_dlbh_fopen(path, mode)
  21. const char *path, *mode;
  22. {
  23. const char *file, *subname;
  24. #ifdef FILE_AREAS
  25. const char *area;
  26. #endif
  27. char *s;
  28. char *buf = NULL;
  29. char *name = NULL;
  30. int i, retval = 0;
  31. if (path[0] != '$' || path[1] != '(')
  32. retval = -1;
  33. else {
  34. for(i = 0; path[i] && path[i] != ')'; i++)
  35. ;
  36. if (!path[i])
  37. retval = -1;
  38. else {
  39. name = (char *)alloc(i + 1);
  40. memcpy(name, path, i);
  41. name[i] = '\0';
  42. subname = path + i + 1;
  43. if (*subname=='/')
  44. subname++;
  45. else if (*subname)
  46. retval = -1;
  47. }
  48. }
  49. if (strcmp(mode, "r") && strcmp(mode, "rb"))
  50. retval = -1;
  51. if (!retval && *subname) {
  52. if (!strcmp(name + 2, "TILEDIR") || !strcmp(name + 2, "TILEMAPDIR")) {
  53. #ifdef MAXNOTILESETS
  54. for(i = 0; i < no_tilesets; i++)
  55. if (!strcmp(tilesets[i].file, subname))
  56. break;
  57. if (i == no_tilesets)
  58. retval = -1;
  59. else
  60. #else
  61. if (strcmp(tile_file, subname))
  62. retval = -1;
  63. else
  64. #endif
  65. if (!strcmp(name + 2, "TILEDIR"))
  66. SET_FILE(subname, FILE_AREA_SHARE);
  67. else {
  68. s = strrchr(subname, '.');
  69. if (s) {
  70. buf = (char *)alloc(s - subname + 5);
  71. (void)strncpy(buf, subname, s - subname);
  72. buf[s - subname] = '\0';
  73. } else {
  74. buf = (char *)alloc(strlen(subname) + 5);
  75. Strcpy(buf, subname);
  76. }
  77. Strcat(buf, ".map");
  78. SET_FILE(buf, FILE_AREA_SHARE);
  79. }
  80. } else
  81. retval = -1;
  82. } else if (!retval) {
  83. if (!strcmp(name + 2, "RECORD"))
  84. SET_FILE(NH_RECORD, NH_RECORD_AREA);
  85. else if (!strcmp(name + 2, "HELP"))
  86. SET_FILE(NH_HELP, NH_HELP_AREA);
  87. else if (!strcmp(name + 2, "SHELP"))
  88. SET_FILE(NH_SHELP, NH_SHELP_AREA);
  89. else if (!strcmp(name + 2, "DEBUGHELP"))
  90. SET_FILE(NH_DEBUGHELP, NH_DEBUGHELP_AREA);
  91. #if 0
  92. else if (!strcmp(name + 2, "RUMORFILE"))
  93. SET_FILE(NH_RUMORFILE, NH_RUMORAREA);
  94. else if (!strcmp(name + 2, "ORACLEFILE"))
  95. SET_FILE(NH_ORACLEFILE, NH_ORACLEAREA);
  96. #endif
  97. else if (!strcmp(name + 2, "DATAFILE"))
  98. SET_FILE(NH_DATAFILE, NH_DATAAREA);
  99. else if (!strcmp(name + 2, "CMDHELPFILE"))
  100. SET_FILE(NH_CMDHELPFILE, NH_CMDHELPAREA);
  101. else if (!strcmp(name + 2, "HISTORY"))
  102. SET_FILE(NH_HISTORY, NH_HISTORY_AREA);
  103. else if (!strcmp(name + 2, "LICENSE"))
  104. SET_FILE(NH_LICENSE, NH_LICENSE_AREA);
  105. else if (!strcmp(name + 2, "OPTIONFILE"))
  106. SET_FILE(NH_OPTIONFILE, NH_OPTIONAREA);
  107. else if (!strcmp(name + 2, "OPTIONS_USED"))
  108. SET_FILE(NH_OPTIONS_USED, NH_OPTIONS_USED_AREA);
  109. else if (!strcmp(name + 2, "GUIDEBOOK"))
  110. SET_FILE(NH_GUIDEBOOK, NH_GUIDEBOOK_AREA);
  111. else
  112. retval = -1;
  113. }
  114. if (!retval) {
  115. dlb_init();
  116. #ifndef FILE_AREAS
  117. retval = dlbh_fopen(file, mode);
  118. #else
  119. retval = dlbh_fopen_area(area, file, mode);
  120. #endif
  121. }
  122. if (buf)
  123. free(buf);
  124. free(name);
  125. return retval;
  126. }
  127. #undef SET_FILE
  128. static void
  129. cb_flush_screen()
  130. {
  131. extern int proxy_curs_on_u;
  132. flush_screen(proxy_curs_on_u);
  133. }
  134. static void
  135. cb_interface_mode(mode)
  136. long mode;
  137. {
  138. proxy_interface_mode = mode;
  139. bot_set_handler(mode & NHPROXY_EXT_IM_STATUS ?
  140. nhproxy_proc_status : (void (*)())0L);
  141. }
  142. static void
  143. cb_parse_options(char *opts)
  144. {
  145. parseoptions(opts, FALSE, FALSE);
  146. }
  147. static struct nhproxy_cb_get_player_choices_res *
  148. cb_get_player_choices()
  149. {
  150. int i;
  151. static const char *aligns_adj[ROLE_ALIGNS], *genders_adj[ROLE_GENDERS];
  152. struct nhproxy_cb_get_player_choices_res *choices;
  153. choices =
  154. (struct nhproxy_cb_get_player_choices_res *)alloc(sizeof(*choices));
  155. choices->n_aligns = ROLE_ALIGNS;
  156. choices->aligns = aligns_adj;
  157. for(i = 0; i < ROLE_ALIGNS; i++)
  158. aligns_adj[i] = aligns[i].adj;
  159. choices->n_genders = ROLE_GENDERS;
  160. choices->genders = genders_adj;
  161. for(i = 0; i < ROLE_GENDERS; i++)
  162. genders_adj[i] = genders[i].adj;
  163. for(i = 0; races[i].noun; i++)
  164. ;
  165. choices->n_races = i;
  166. choices->races = (const char **)alloc(i * sizeof(const char *));
  167. for(i = 0; i < choices->n_races; i++)
  168. choices->races[i] = races[i].noun;
  169. for(i = 0; roles[i].name.m; i++)
  170. ;
  171. choices->n_roles = i;
  172. choices->roles = (struct nhproxy_cb_get_player_choices_res_role *)
  173. alloc(i * sizeof(struct nhproxy_cb_get_player_choices_res_role));
  174. for(i = 0; i < choices->n_roles; i++) {
  175. choices->roles[i].male = roles[i].name.m;
  176. choices->roles[i].female = roles[i].name.f ? roles[i].name.f : "";
  177. }
  178. return choices;
  179. }
  180. static void
  181. cb_free_player_choices(choices)
  182. struct nhproxy_cb_get_player_choices_res *choices;
  183. {
  184. free(choices->races);
  185. free(choices->roles);
  186. free(choices);
  187. }
  188. #define SKIP(n) do { \
  189. i += (n) / pack; \
  190. k += ((n) % pack) * ROLE_GENDERS; \
  191. if (k >= pack * ROLE_GENDERS) { \
  192. i++; \
  193. k -= pack * ROLE_GENDERS; \
  194. } \
  195. } while(0)
  196. static struct nhproxy_cb_get_valid_selections_res *
  197. cb_get_valid_selections()
  198. {
  199. int role, race, gend, align;
  200. const int pack = 32 / ROLE_GENDERS; /* No. masks packed in each element */
  201. int i, k;
  202. struct nhproxy_cb_get_valid_selections_res *vs;
  203. vs = (struct nhproxy_cb_get_valid_selections_res *)alloc(sizeof(*vs));
  204. for(vs->no_roles = 0; roles[vs->no_roles].name.m; vs->no_roles++)
  205. ;
  206. for(vs->no_races = 0; races[vs->no_races].noun; vs->no_races++)
  207. ;
  208. vs->no_aligns = ROLE_ALIGNS;
  209. vs->no_genders = ROLE_GENDERS;
  210. vs->n_masks = (vs->no_roles * vs->no_races * ROLE_ALIGNS + pack - 1) / pack;
  211. vs->masks = (unsigned long *)alloc(vs->n_masks * sizeof(unsigned long));
  212. memset((genericptr_t)vs->masks, 0, vs->n_masks * sizeof(unsigned long));
  213. for(role = 0, i = 0, k = 0; role < vs->no_roles; role++)
  214. if (!validrole(role))
  215. SKIP(vs->no_races * ROLE_ALIGNS);
  216. else
  217. for(race = 0; race < vs->no_races; race++)
  218. if (!validrace(role, race))
  219. SKIP(ROLE_ALIGNS);
  220. else
  221. for(align = 0; align < ROLE_ALIGNS; align++) {
  222. if (!validalign(role, race, align))
  223. k += ROLE_GENDERS;
  224. else
  225. for(gend = 0; gend < ROLE_GENDERS; gend++, k++)
  226. if (validgend(role, race, gend))
  227. vs->masks[i] |= 1L << k;
  228. if (k >= pack * ROLE_GENDERS) {
  229. i++;
  230. k = 0;
  231. }
  232. }
  233. if (i != (k ? vs->n_masks - 1 : vs->n_masks))
  234. panic("cb_get_valid_selections: Bad packing (%d, %d, %d)",
  235. i, k, vs->n_masks);
  236. return vs;
  237. }
  238. static void
  239. cb_free_valid_selections(vs)
  240. struct nhproxy_cb_get_valid_selections_res *vs;
  241. {
  242. free(vs->masks);
  243. free(vs);
  244. }
  245. #undef SKIP
  246. static void
  247. cb_quit_game()
  248. {
  249. if (program_state.something_worth_saving)
  250. done2();
  251. else {
  252. clearlocks();
  253. exit_nhwindows(NULL);
  254. terminate(EXIT_SUCCESS);
  255. }
  256. }
  257. static void
  258. cb_display_score()
  259. {
  260. extern int proxy_rawprint_win;
  261. char *argv[] = {
  262. "nethack",
  263. "-sall",
  264. };
  265. proxy_rawprint_win = create_toptenwin();
  266. prscore(2, argv);
  267. display_nhwindow(proxy_rawprint_win, TRUE);
  268. destroy_toptenwin();
  269. dlb_init(); /* Re-initialise DLB */
  270. proxy_rawprint_win = WIN_ERR;
  271. }
  272. static struct nhproxy_cb_get_extended_commands_res *
  273. cb_get_extended_commands()
  274. {
  275. int i;
  276. extern struct ext_func_tab extcmdlist[];
  277. struct nhproxy_cb_get_extended_commands_res *list;
  278. list = (struct nhproxy_cb_get_extended_commands_res *)alloc(sizeof(*list));
  279. for(i = 0; extcmdlist[i].ef_txt; i++)
  280. ;
  281. list->n_commands = i;
  282. list->commands = (const char **)alloc(i * sizeof(const char *));
  283. for(i = 0; i < list->n_commands; i++)
  284. list->commands[i] = extcmdlist[i].ef_txt;
  285. return list;
  286. }
  287. static void
  288. cb_free_extended_commands(struct nhproxy_cb_get_extended_commands_res *list)
  289. {
  290. free(list->commands);
  291. free(list);
  292. }
  293. static int
  294. cb_get_standard_winid(const char *window)
  295. {
  296. int retval;
  297. if (!strcmp(window, "MESSAGE"))
  298. retval = WIN_MESSAGE;
  299. else if (!strcmp(window, "STATUS"))
  300. retval = WIN_STATUS;
  301. else if (!strcmp(window, "MAP"))
  302. retval = WIN_MAP;
  303. else if (!strcmp(window, "INVEN"))
  304. retval = WIN_INVEN;
  305. else
  306. retval = -1;
  307. return retval;
  308. }
  309. static struct nhproxy_cb_get_tilesets_res *
  310. cb_get_tilesets()
  311. {
  312. int i;
  313. char *file;
  314. struct nhproxy_cb_get_tilesets_res *list;
  315. list = (struct nhproxy_cb_get_tilesets_res *)alloc(sizeof(*list));
  316. #ifdef MAXNOTILESETS
  317. list->n_tilesets = no_tilesets;
  318. list->tilesets = (struct nhproxy_cb_get_tilesets_res_tileset *)
  319. alloc(no_tilesets * sizeof(*list->tilesets));
  320. for(i = 0; i < list->n_tilesets; i++) {
  321. list->tilesets[i].name = tilesets[i].name;
  322. file = (char *)alloc(strlen(tilesets[i].file) + 12);
  323. sprintf(file, "$(TILEDIR)/%s", tilesets[i].file);
  324. list->tilesets[i].file = file;
  325. file = (char *)alloc(strlen(tilesets[i].file) + 16);
  326. sprintf(file, "$(TILEMAPDIR)/%s", tilesets[i].file);
  327. list->tilesets[i].mapfile = file;
  328. list->tilesets[i].flags = tilesets[i].flags;
  329. }
  330. #else
  331. list->n_tilesets = 1;
  332. list->tilesets = (struct nhproxy_cb_get_tilesets_res_tileset *)
  333. alloc(1 * sizeof(*list->tilesets));
  334. list->tilesets[0].name = "Default";
  335. file = (char *)alloc(strlen(tile_file) + 12);
  336. sprintf(file, "$(TILEDIR)/%s", tile_file);
  337. list->tilesets[0].file = file;
  338. file = (char *)alloc(strlen(tile_file) + 16);
  339. sprintf(file, "$(TILEMAPDIR)/%s", tile_file);
  340. list->tilesets[0].mapfile = file;
  341. list->tilesets[0].flags = 0;
  342. #endif
  343. return list;
  344. }
  345. static void
  346. cb_free_tilesets(struct nhproxy_cb_get_tilesets_res *list)
  347. {
  348. int i;
  349. for(i = 0; i < list->n_tilesets; i++)
  350. free((char *)list->tilesets[i].file);
  351. free(list->tilesets);
  352. free(list);
  353. }
  354. struct nhproxy_serv_callbacks win_proxy_callbacks = {
  355. cb_display_inventory,
  356. cb_dlbh_fopen,
  357. dlbh_fgets,
  358. dlbh_fread,
  359. 0, /* placeholder for dlbh_fwrite */
  360. dlbh_fclose,
  361. cb_flush_screen,
  362. (void (*)())doredraw,
  363. cb_interface_mode,
  364. cb_parse_options,
  365. get_option,
  366. cb_get_player_choices,
  367. cb_free_player_choices,
  368. cb_get_valid_selections,
  369. cb_free_valid_selections,
  370. cb_quit_game,
  371. cb_display_score,
  372. (void (*)())doset,
  373. cb_get_extended_commands,
  374. cb_free_extended_commands,
  375. (char NHPROXY_FDECL(*, (NHPROXY_CHAR_P)))map_menu_cmd,
  376. cb_get_standard_winid,
  377. cb_get_tilesets,
  378. cb_free_tilesets,
  379. get_glyph_mapping,
  380. free_glyph_mapping,
  381. set_option_mod_status,
  382. #if NHPROXY_CHECK_VERSION(0,9,1)
  383. get_no_glyph,
  384. #endif
  385. };