plainc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Copyright © 2015 Hein-Pieter van Braam <hp@tmm.cx>
  2. * This work is free. You can redistribute it and/or modify it under the
  3. * terms of the Do What The Fuck You Want To Public License, Version 2,
  4. * as published by Sam Hocevar. See the COPYING file for more details.
  5. *
  6. * Text copied from http://www.firthworks.com/roger/cloak/
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <readline/readline.h>
  12. #include <readline/history.h>
  13. #define BOLD "\033[1m"
  14. #define NO_BOLD "\033[22m"
  15. int room;
  16. int new_room;
  17. int message_disturbed;
  18. struct noun;
  19. typedef void (*do_verb) (struct noun * noun);
  20. void do_n(struct noun *noun);
  21. void do_e(struct noun *noun);
  22. void do_s(struct noun *noun);
  23. void do_w(struct noun *noun);
  24. void do_look(struct noun *noun);
  25. void do_take(struct noun *noun);
  26. void do_drop(struct noun *noun);
  27. void do_inventory(struct noun *noun);
  28. void do_hang(struct noun *noun);
  29. void do_read(struct noun *noun);
  30. enum location_tokens {
  31. PLAYER,
  32. FOYER,
  33. BAR,
  34. CLOAKROOM
  35. };
  36. struct room_direction {
  37. int location_token;
  38. const char *message;
  39. };
  40. struct room {
  41. int location_token;
  42. const char *name;
  43. const char *description;
  44. struct room_direction north;
  45. struct room_direction east;
  46. struct room_direction south;
  47. struct room_direction west;
  48. };
  49. struct room rooms[] = {
  50. {
  51. FOYER,
  52. "Foyer of the Opera House",
  53. "You are standing in a spacious hall, splendidly decorated in red and gold, with glittering chandeliers overhead. The entrance from the street is to the north, and there are doorways south and west.",
  54. {-1, "You've only just arrived, and besides, the weather outside seems to be getting worse."},
  55. {-1, NULL},
  56. {BAR, NULL},
  57. {CLOAKROOM, NULL},
  58. },
  59. {
  60. BAR,
  61. "Foyer Bar",
  62. "The bar, much rougher than you'd have guessed after the opulence of the foyer to the north, is completely empty. There seems to be some sort of message scrawled in the sawdust on the floor.",
  63. {FOYER, NULL},
  64. {-1, NULL},
  65. {-1, NULL},
  66. {-1, NULL},
  67. },
  68. {
  69. CLOAKROOM,
  70. "The Cloakroom",
  71. "The walls of this small room were clearly once lined with hooks, though now only one remains. The exit is a door to the east.",
  72. {-1, NULL},
  73. {FOYER, NULL},
  74. {-1, NULL},
  75. {-1, NULL},
  76. },
  77. {
  78. 0,
  79. NULL,
  80. NULL,
  81. {0, NULL},
  82. {0, NULL},
  83. {0, NULL},
  84. {0, NULL},
  85. },
  86. };
  87. enum verb_tokens {
  88. NORTH,
  89. EAST,
  90. SOUTH,
  91. WEST,
  92. LOOK,
  93. EXAMINE,
  94. TAKE,
  95. DROP,
  96. INVENTORY,
  97. HANG,
  98. READ,
  99. };
  100. struct verb {
  101. const char *verb_string;
  102. int verb_token;
  103. do_verb function;
  104. };
  105. struct verb verbs[] = {
  106. {"n", NORTH, do_n},
  107. {"north", NORTH, do_n},
  108. {"e", EAST, do_e},
  109. {"east", EAST, do_e},
  110. {"s", SOUTH, do_s},
  111. {"south", SOUTH, do_s},
  112. {"w", WEST, do_w},
  113. {"west", WEST, do_w},
  114. {"look", LOOK, do_look},
  115. {"examine", LOOK, do_look},
  116. {"take", TAKE, do_take},
  117. {"drop", DROP, do_drop},
  118. {"i", INVENTORY, do_inventory},
  119. {"inventory", INVENTORY, do_inventory},
  120. {"hang", HANG, do_hang},
  121. {"read", READ, do_read},
  122. {NULL, 0, NULL}
  123. };
  124. enum noun_tokens {
  125. HOOK,
  126. CLOAK,
  127. MESSAGE
  128. };
  129. struct noun {
  130. int noun_token;
  131. int location_token;
  132. int takeable;
  133. const char *name;
  134. const char *description;
  135. };
  136. struct noun nouns[] = {
  137. {
  138. HOOK,
  139. CLOAKROOM,
  140. 0,
  141. "hook",
  142. "It's just a small brass hook"},
  143. {
  144. CLOAK,
  145. PLAYER,
  146. 1,
  147. "cloak",
  148. "A handsome cloak, of velvet trimmed with satin, and slightly splattered with raindrops. Its blackness is so deep that it almost seems to suck light from the room."},
  149. {
  150. MESSAGE,
  151. BAR,
  152. 0,
  153. "message",
  154. "A message in the sawdust"},
  155. {
  156. 0,
  157. 0,
  158. 0,
  159. NULL,
  160. NULL}
  161. };
  162. struct room *get_room() {
  163. struct room *r;
  164. for (r = rooms; r->name; ++r) {
  165. if (r->location_token == room)
  166. break;
  167. }
  168. return r;
  169. }
  170. struct noun *find_noun(int noun) {
  171. struct noun *n;
  172. for (n = nouns; n->name; ++n) {
  173. if (n->noun_token == noun) {
  174. break;
  175. }
  176. }
  177. return n;
  178. }
  179. void do_look(struct noun *noun) {
  180. struct room *r = get_room();
  181. struct noun *cloak = find_noun(CLOAK);
  182. if ((room == BAR) && cloak->location_token == PLAYER) {
  183. printf("It is pitch dark, and you can't see a thing.\n");
  184. return;
  185. }
  186. if (noun == NULL) {
  187. printf("%s\n", r->description);
  188. return;
  189. }
  190. if ((noun->location_token == PLAYER) || noun->location_token == room) {
  191. printf("%s\n", noun->description);
  192. } else {
  193. printf("The %s isn't here.\n", noun->name);
  194. }
  195. }
  196. void do_inventory( __attribute__ ((unused))
  197. struct noun *noun) {
  198. struct noun *n;
  199. printf("You are carrying:\n");
  200. for (n = nouns; n->name; ++n) {
  201. if (n->location_token == PLAYER) {
  202. printf("\t%s\n", n->name);
  203. }
  204. }
  205. }
  206. void do_take(struct noun *noun) {
  207. if (noun == NULL) {
  208. printf("Take what?\n");
  209. return;
  210. }
  211. if (noun->location_token == room) {
  212. if (noun->takeable) {
  213. printf("You took the %s.\n", noun->name);
  214. noun->location_token = PLAYER;
  215. } else {
  216. printf("You can't take the %s.\n", noun->name);
  217. }
  218. } else if (noun->location_token == PLAYER) {
  219. printf("You already have the %s.\n", noun->name);
  220. } else {
  221. printf("There's no %s here.\n", noun->name);
  222. }
  223. }
  224. void do_drop(struct noun *noun) {
  225. if (noun == NULL) {
  226. printf("Drop what?\n");
  227. return;
  228. }
  229. if ((noun->noun_token == CLOAK) && (room != CLOAKROOM)) {
  230. printf("This isn't the best place to leave a smart cloak lying around.\n");
  231. return;
  232. }
  233. if (noun->location_token == PLAYER) {
  234. printf("You dropped the %s to the floor.\n", noun->name);
  235. noun->location_token = room;
  236. } else {
  237. printf("You're not carrying the %s\n", noun->name);
  238. }
  239. }
  240. void do_move(int direction) {
  241. struct room *r = get_room();
  242. struct room_direction *d;
  243. switch (direction) {
  244. case NORTH:
  245. d = &r->north;
  246. break;
  247. case EAST:
  248. d = &r->east;
  249. break;
  250. case SOUTH:
  251. d = &r->south;
  252. break;
  253. case WEST:
  254. d = &r->west;
  255. break;
  256. }
  257. if (d->location_token > 0) {
  258. room = d->location_token;
  259. new_room = 1;
  260. } else {
  261. if (d->message) {
  262. printf("%s\n", d->message);
  263. } else {
  264. printf("You can't go that way.\n");
  265. }
  266. }
  267. }
  268. void do_hang(struct noun *noun) {
  269. if (noun == NULL) {
  270. printf("Hang what?\n");
  271. return;
  272. }
  273. if ((noun->noun_token == CLOAK) && (room == CLOAKROOM)) {
  274. printf("You hang the coat on the hook.\n");
  275. noun->location_token = room;
  276. return;
  277. }
  278. if (noun->location_token == PLAYER) {
  279. printf("You can't hang a %s.\n", noun->name);
  280. } else {
  281. printf("You're not carrying the %s\n", noun->name);
  282. }
  283. }
  284. void do_read(struct noun *noun) {
  285. if (noun == NULL) {
  286. printf("Read what?\n");
  287. return;
  288. }
  289. if ((noun->location_token == room) && noun->noun_token == MESSAGE) {
  290. if (message_disturbed == 0) {
  291. printf("The message, neatly marked in the sawdust, reads...\n");
  292. printf("You win!\n");
  293. exit(0);
  294. }
  295. if (message_disturbed == 1) {
  296. printf("The message, slightly scuffed, in the sawdust, reads...\n");
  297. printf("You win!\n");
  298. exit(0);
  299. }
  300. printf("The message has been carelessly trampled, making it difficult to read. You can just distinguish the words...\n");
  301. printf("You have lost!\n");
  302. exit(0);
  303. }
  304. printf("You can't read a %s\n", noun->name);
  305. }
  306. void do_n( __attribute__ ((unused))
  307. struct noun *noun) {
  308. do_move(NORTH);
  309. }
  310. void do_e( __attribute__ ((unused))
  311. struct noun *noun) {
  312. do_move(EAST);
  313. }
  314. void do_s( __attribute__ ((unused))
  315. struct noun *noun) {
  316. do_move(SOUTH);
  317. }
  318. void do_w( __attribute__ ((unused))
  319. struct noun *noun) {
  320. do_move(WEST);
  321. }
  322. void parse(const char *input) {
  323. int found_verb = -1;
  324. int found_noun = -1;
  325. int verb_length = 0;
  326. int has_noun = 0;
  327. if (!strlen(input)) {
  328. return;
  329. }
  330. char *i = strstr(input, " ");
  331. if (i) {
  332. verb_length = i - input;
  333. has_noun = 1;
  334. } else {
  335. verb_length = strlen(input);
  336. }
  337. struct verb *v;
  338. for (v = verbs; v->verb_string; ++v) {
  339. if (strncasecmp(v->verb_string, input, verb_length) == 0) {
  340. found_verb = v->verb_token;
  341. break;
  342. }
  343. }
  344. struct noun *n;
  345. if (has_noun) {
  346. for (n = nouns; n->name; ++n) {
  347. if (strcasecmp(n->name, input + verb_length + 1) == 0) {
  348. found_noun = n->noun_token;
  349. break;
  350. }
  351. }
  352. }
  353. if (found_noun < 0) {
  354. n = NULL;
  355. }
  356. if (found_verb < 0) {
  357. printf("I don't know how to \"%s\"\n", input);
  358. } else {
  359. if ((has_noun) && (found_noun < 0)) {
  360. printf("You don't see a \"%s\" here\n", input + verb_length + 1);
  361. } else {
  362. struct noun *cloak = find_noun(CLOAK);
  363. if ((room == BAR) && (found_verb != NORTH) && cloak->location_token == PLAYER) {
  364. ++message_disturbed;
  365. if (found_verb == EAST || found_verb == SOUTH || found_verb == WEST) {
  366. printf("Blundering around in the dark isn't a good idea!\n");
  367. } else {
  368. printf("In the dark? You could easily disturb something.\n");
  369. }
  370. } else {
  371. v->function(n);
  372. }
  373. }
  374. }
  375. }
  376. int main() {
  377. char *input;
  378. char *prompt = "> ";
  379. new_room = 1;
  380. room = FOYER;
  381. message_disturbed = 0;
  382. printf("Hurrying through the rainswept November night, you're glad to see the bright lights of the Opera House. It's surprising that there aren't more people about but, hey, what do you expect in a cheap demo game...?\n");
  383. for (;;) {
  384. printf("\n");
  385. struct room *r = get_room();
  386. printf(BOLD "%s\n" NO_BOLD, r->name);
  387. if (new_room) {
  388. do_look(NULL);
  389. new_room = 0;
  390. }
  391. input = readline(prompt);
  392. if (!input)
  393. break;
  394. add_history(input);
  395. parse(input);
  396. free(input);
  397. }
  398. printf("\n");
  399. return 0;
  400. }