exit.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //*****************************************************************************
  2. //
  3. // exit.c
  4. //
  5. // exits are structures that keep information about the links between rooms.
  6. //
  7. //*****************************************************************************
  8. #include "mud.h"
  9. #include "utils.h"
  10. #include "storage.h"
  11. #include "exit.h"
  12. #define EX_CLOSED (1 << 0)
  13. #define EX_LOCKED (1 << 1)
  14. #define EX_CLOSABLE (1 << 2)
  15. // lockable is handled if the exit has a key
  16. struct exit_data {
  17. char *name; // what is the name of our door for descriptions?
  18. char *keywords; // what keywords can the door be referenced by?
  19. char *opposite; // what is our opposite direction, if any?
  20. char *to; // where do we exit to?
  21. char *key; // what is the key's prototype?
  22. BUFFER *desc; // what does a person see when they look at us?
  23. ROOM_DATA *room; // the room we're attached to
  24. char *spec_enter; // the message when we enter from this exit
  25. char *spec_leave; // the message when we leave through this exit
  26. bitvector_t status; // closable, closed, locked, etc...
  27. int hide_lev; // how hidden is this exit?
  28. int pick_lev; // how hard is it to pick this exit?
  29. int uid; // our unique identification number
  30. };
  31. EXIT_DATA *newExit() {
  32. EXIT_DATA *exit = malloc(sizeof(EXIT_DATA));
  33. exit->name = strdup("");
  34. exit->keywords = strdup("");
  35. exit->opposite = strdup("");
  36. exit->spec_enter = strdup("");
  37. exit->spec_leave = strdup("");
  38. exit->to = strdup("");
  39. exit->key = strdup("");
  40. exit->desc = newBuffer(1);
  41. exit->hide_lev = 0;
  42. exit->pick_lev = 0;
  43. exit->status = 0;
  44. exit->room = NULL;
  45. exit->uid = next_uid();
  46. return exit;
  47. };
  48. void deleteExit(EXIT_DATA *exit) {
  49. if(exit->name) free(exit->name);
  50. if(exit->spec_enter) free(exit->spec_enter);
  51. if(exit->spec_leave) free(exit->spec_leave);
  52. if(exit->keywords) free(exit->keywords);
  53. if(exit->opposite) free(exit->opposite);
  54. if(exit->to) free(exit->to);
  55. if(exit->key) free(exit->key);
  56. if(exit->desc) deleteBuffer(exit->desc);
  57. free(exit);
  58. };
  59. void exitCopyTo(const EXIT_DATA *from, EXIT_DATA *to) {
  60. exitSetTo (to, exitGetTo(from));
  61. exitSetName (to, exitGetName(from));
  62. exitSetKeywords (to, exitGetKeywords(from));
  63. exitSetDesc (to, exitGetDesc(from));
  64. exitSetTo (to, exitGetTo(from));
  65. exitSetPickLev (to, exitGetPickLev(from));
  66. exitSetHidden (to, exitGetHidden(from));
  67. exitSetKey (to, exitGetKey(from));
  68. exitSetLocked (to, exitIsLocked(from));
  69. exitSetClosed (to, exitIsClosed(from));
  70. exitSetClosable (to, exitIsClosable(from));
  71. exitSetSpecEnter(to, exitGetSpecEnter(from));
  72. exitSetSpecLeave(to, exitGetSpecLeave(from));
  73. exitSetOpposite (to, exitGetOpposite(from));
  74. }
  75. EXIT_DATA *exitCopy(const EXIT_DATA *exit) {
  76. EXIT_DATA *newexit = newExit();
  77. exitCopyTo(exit, newexit);
  78. return newexit;
  79. }
  80. EXIT_DATA *exitRead(STORAGE_SET *set) {
  81. EXIT_DATA *exit = newExit();
  82. exitSetName(exit, read_string(set, "name"));
  83. exitSetKeywords(exit, read_string(set, "keywords"));
  84. exitSetOpposite(exit, read_string(set, "opposite"));
  85. exitSetDesc(exit, read_string(set, "desc"));
  86. exitSetSpecEnter(exit, read_string(set, "enter"));
  87. exitSetSpecLeave(exit, read_string(set, "leave"));
  88. exitSetTo(exit, read_string(set, "to"));
  89. exitSetKey(exit, read_string(set, "key"));
  90. exitSetHidden(exit, read_int (set, "hide_level"));
  91. exitSetPickLev(exit, read_int (set, "pick_level"));
  92. exitSetClosable(exit, read_bool (set, "closable"));
  93. exitSetClosed(exit, read_bool (set, "closed"));
  94. exitSetLocked(exit, read_bool (set, "locked"));
  95. return exit;
  96. }
  97. STORAGE_SET *exitStore(EXIT_DATA *exit) {
  98. STORAGE_SET *set = new_storage_set();
  99. store_string(set, "name", exit->name);
  100. store_string(set, "keywords", exit->keywords);
  101. store_string(set, "opposite", exit->opposite);
  102. store_string(set, "desc", bufferString(exit->desc));
  103. store_string(set, "enter", exit->spec_enter);
  104. store_string(set, "leave", exit->spec_leave);
  105. store_string(set, "to", exit->to);
  106. store_string(set, "key", exit->key);
  107. store_int (set, "hide_level", exit->hide_lev);
  108. store_int (set, "pick_level", exit->pick_lev);
  109. store_bool (set, "closable", exitIsClosable(exit));
  110. store_bool (set, "closed", exitIsClosed(exit));
  111. store_bool (set, "locked", exitIsLocked(exit));
  112. return set;
  113. }
  114. //*****************************************************************************
  115. // is, get and set functions
  116. //*****************************************************************************
  117. bool exitIsClosable(const EXIT_DATA *exit) {
  118. return IS_SET(exit->status, EX_CLOSABLE);
  119. };
  120. bool exitIsClosed(const EXIT_DATA *exit) {
  121. return IS_SET(exit->status, EX_CLOSED);
  122. };
  123. bool exitIsLocked(const EXIT_DATA *exit) {
  124. return IS_SET(exit->status, EX_LOCKED);
  125. };
  126. bool exitIsName(const EXIT_DATA *exit, const char *name) {
  127. return is_keyword(exit->keywords, name, TRUE);
  128. }
  129. int exitGetUID(const EXIT_DATA *exit) {
  130. return exit->uid;
  131. }
  132. int exitGetHidden(const EXIT_DATA *exit) {
  133. return exit->hide_lev;
  134. }
  135. int exitGetPickLev(const EXIT_DATA *exit) {
  136. return exit->pick_lev;
  137. }
  138. const char *exitGetKey(const EXIT_DATA *exit) {
  139. return exit->key;
  140. }
  141. const char *exitGetTo(const EXIT_DATA *exit) {
  142. return exit->to;
  143. }
  144. const char *exitGetName(const EXIT_DATA *exit) {
  145. return exit->name;
  146. }
  147. const char *exitGetKeywords(const EXIT_DATA *exit) {
  148. return exit->keywords;
  149. }
  150. const char *exitGetOpposite(const EXIT_DATA *exit) {
  151. return exit->opposite;
  152. }
  153. const char *exitGetDesc(const EXIT_DATA *exit) {
  154. return bufferString(exit->desc);
  155. }
  156. const char *exitGetSpecEnter(const EXIT_DATA *exit) {
  157. return exit->spec_enter;
  158. }
  159. const char *exitGetSpecLeave(const EXIT_DATA *exit) {
  160. return exit->spec_leave;
  161. }
  162. BUFFER *exitGetDescBuffer(const EXIT_DATA *exit) {
  163. return exit->desc;
  164. }
  165. ROOM_DATA *exitGetRoom(const EXIT_DATA *exit) {
  166. return exit->room;
  167. }
  168. void exitSetClosable(EXIT_DATA *exit, bool closable) {
  169. if(closable) SET_BIT(exit->status, EX_CLOSABLE);
  170. else REMOVE_BIT(exit->status, EX_CLOSABLE);
  171. }
  172. void exitSetClosed(EXIT_DATA *exit, bool closed) {
  173. if(closed) SET_BIT(exit->status, EX_CLOSED);
  174. else REMOVE_BIT(exit->status, EX_CLOSED);
  175. }
  176. void exitSetLocked(EXIT_DATA *exit, bool locked) {
  177. if(locked) SET_BIT(exit->status, EX_LOCKED);
  178. else REMOVE_BIT(exit->status, EX_LOCKED);
  179. }
  180. void exitSetKey(EXIT_DATA *exit, const char *key) {
  181. if(exit->key) free(exit->key);
  182. exit->key = strdupsafe(key);
  183. }
  184. void exitSetHidden(EXIT_DATA *exit, int hide_lev) {
  185. exit->hide_lev = hide_lev;
  186. }
  187. void exitSetPickLev(EXIT_DATA *exit, int pick_lev) {
  188. exit->pick_lev = pick_lev;
  189. }
  190. void exitSetTo(EXIT_DATA *exit, const char *room) {
  191. if(exit->to) free(exit->to);
  192. exit->to = strdupsafe(room);
  193. }
  194. void exitSetName(EXIT_DATA *exit, const char *name) {
  195. if(exit->name) free(exit->name);
  196. exit->name = strdupsafe(name);
  197. }
  198. void exitSetKeywords(EXIT_DATA *exit, const char *keywords) {
  199. if(exit->keywords) free(exit->keywords);
  200. exit->keywords = strdupsafe(keywords);
  201. }
  202. void exitSetOpposite(EXIT_DATA *exit, const char *opposite) {
  203. if(exit->opposite) free(exit->opposite);
  204. exit->opposite = strdupsafe(opposite);
  205. }
  206. void exitSetDesc(EXIT_DATA *exit, const char *desc) {
  207. bufferClear(exit->desc);
  208. bufferCat(exit->desc, (desc ? desc : ""));
  209. }
  210. void exitSetSpecEnter(EXIT_DATA *exit, const char *enter) {
  211. if(exit->spec_enter) free(exit->spec_enter);
  212. exit->spec_enter = strdupsafe(enter);
  213. }
  214. void exitSetSpecLeave(EXIT_DATA *exit, const char *leave) {
  215. if(exit->spec_leave) free(exit->spec_leave);
  216. exit->spec_leave = strdupsafe(leave);
  217. }
  218. void exitSetRoom(EXIT_DATA *exit, ROOM_DATA *room) {
  219. exit->room = room;
  220. }