hack.engrave.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* $NetBSD: hack.engrave.c,v 1.6 2003/04/02 18:36:36 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  4. * Amsterdam
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * - Neither the name of the Stichting Centrum voor Wiskunde en
  19. * Informatica, nor the names of its contributors may be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. The name of the author may not be used to endorse or promote products
  48. * derived from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  51. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  52. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  53. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  56. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  57. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  58. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  59. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. */
  61. #include <sys/cdefs.h>
  62. #ifndef lint
  63. __RCSID("$NetBSD: hack.engrave.c,v 1.6 2003/04/02 18:36:36 jsm Exp $");
  64. #endif /* not lint */
  65. #include <stdlib.h>
  66. #include "hack.h"
  67. #include "extern.h"
  68. struct engr {
  69. struct engr *nxt_engr;
  70. char *engr_txt;
  71. xchar engr_x, engr_y;
  72. unsigned engr_lth; /* for save & restore; not length of
  73. * text */
  74. long engr_time; /* moment engraving was (will be)
  75. * finished */
  76. xchar engr_type;
  77. #define DUST 1
  78. #define ENGRAVE 2
  79. #define BURN 3
  80. } *head_engr;
  81. struct engr *
  82. engr_at(x, y)
  83. xchar x, y;
  84. {
  85. struct engr *ep = head_engr;
  86. while (ep) {
  87. if (x == ep->engr_x && y == ep->engr_y)
  88. return (ep);
  89. ep = ep->nxt_engr;
  90. }
  91. return ((struct engr *) 0);
  92. }
  93. int
  94. sengr_at(s, x, y)
  95. const char *s;
  96. xchar x, y;
  97. {
  98. struct engr *ep = engr_at(x, y);
  99. char *t;
  100. int n;
  101. if (ep && ep->engr_time <= moves) {
  102. t = ep->engr_txt;
  103. /*
  104. if(!strcmp(s,t)) return(1);
  105. */
  106. n = strlen(s);
  107. while (*t) {
  108. if (!strncmp(s, t, n))
  109. return (1);
  110. t++;
  111. }
  112. }
  113. return (0);
  114. }
  115. void
  116. u_wipe_engr(cnt)
  117. int cnt;
  118. {
  119. if (!u.uswallow && !Levitation)
  120. wipe_engr_at(u.ux, u.uy, cnt);
  121. }
  122. void
  123. wipe_engr_at(x, y, cnt)
  124. xchar x, y, cnt;
  125. {
  126. struct engr *ep = engr_at(x, y);
  127. int lth, pos;
  128. char ch;
  129. if (ep) {
  130. if ((ep->engr_type != DUST) || Levitation) {
  131. cnt = rn2(1 + 50 / (cnt + 1)) ? 0 : 1;
  132. }
  133. lth = strlen(ep->engr_txt);
  134. if (lth && cnt > 0) {
  135. while (cnt--) {
  136. pos = rn2(lth);
  137. if ((ch = ep->engr_txt[pos]) == ' ')
  138. continue;
  139. ep->engr_txt[pos] = (ch != '?') ? '?' : ' ';
  140. }
  141. }
  142. while (lth && ep->engr_txt[lth - 1] == ' ')
  143. ep->engr_txt[--lth] = 0;
  144. while (ep->engr_txt[0] == ' ')
  145. ep->engr_txt++;
  146. if (!ep->engr_txt[0])
  147. del_engr(ep);
  148. }
  149. }
  150. void
  151. read_engr_at(x, y)
  152. int x, y;
  153. {
  154. struct engr *ep = engr_at(x, y);
  155. if (ep && ep->engr_txt[0]) {
  156. switch (ep->engr_type) {
  157. case DUST:
  158. pline("Something is written here in the dust.");
  159. break;
  160. case ENGRAVE:
  161. pline("Something is engraved here on the floor.");
  162. break;
  163. case BURN:
  164. pline("Some text has been burned here in the floor.");
  165. break;
  166. default:
  167. impossible("Something is written in a very strange way.");
  168. }
  169. pline("You read: \"%s\".", ep->engr_txt);
  170. }
  171. }
  172. void
  173. make_engr_at(x, y, s)
  174. int x, y;
  175. const char *s;
  176. {
  177. struct engr *ep;
  178. if ((ep = engr_at(x, y)) != NULL)
  179. del_engr(ep);
  180. ep = (struct engr *)
  181. alloc((unsigned) (sizeof(struct engr) + strlen(s) + 1));
  182. ep->nxt_engr = head_engr;
  183. head_engr = ep;
  184. ep->engr_x = x;
  185. ep->engr_y = y;
  186. ep->engr_txt = (char *) (ep + 1);
  187. (void) strcpy(ep->engr_txt, s);
  188. ep->engr_time = 0;
  189. ep->engr_type = DUST;
  190. ep->engr_lth = strlen(s) + 1;
  191. }
  192. int
  193. doengrave()
  194. {
  195. int len;
  196. char *sp;
  197. struct engr *ep, *oep = engr_at(u.ux, u.uy);
  198. char buf[BUFSZ];
  199. xchar type;
  200. int spct; /* number of leading spaces */
  201. struct obj *otmp;
  202. multi = 0;
  203. if (u.uswallow) {
  204. pline("You're joking. Hahaha!"); /* riv05!a3 */
  205. return (0);
  206. }
  207. /* one may write with finger, weapon or wand */
  208. otmp = getobj("#-)/", "write with");
  209. if (!otmp)
  210. return (0);
  211. if (otmp == &zeroobj)
  212. otmp = 0;
  213. if (otmp && otmp->otyp == WAN_FIRE && otmp->spe) {
  214. type = BURN;
  215. otmp->spe--;
  216. } else {
  217. /* first wield otmp */
  218. if (otmp != uwep) {
  219. if (uwep && uwep->cursed) {
  220. /* Andreas Bormann */
  221. pline("Since your weapon is welded to your hand,");
  222. pline("you use the %s.", aobjnam(uwep, (char *) 0));
  223. otmp = uwep;
  224. } else {
  225. if (!otmp)
  226. pline("You are now empty-handed.");
  227. else if (otmp->cursed)
  228. pline("The %s %s to your hand!",
  229. aobjnam(otmp, "weld"),
  230. (otmp->quan == 1) ? "itself" : "themselves");
  231. else
  232. pline("You now wield %s.", doname(otmp));
  233. setuwep(otmp);
  234. }
  235. }
  236. if (!otmp)
  237. type = DUST;
  238. else if (otmp->otyp == DAGGER || otmp->otyp == TWO_HANDED_SWORD ||
  239. otmp->otyp == CRYSKNIFE ||
  240. otmp->otyp == LONG_SWORD || otmp->otyp == AXE) {
  241. type = ENGRAVE;
  242. if ((int) otmp->spe <= -3) {
  243. type = DUST;
  244. pline("Your %s too dull for engraving.",
  245. aobjnam(otmp, "are"));
  246. if (oep && oep->engr_type != DUST)
  247. return (1);
  248. }
  249. } else
  250. type = DUST;
  251. }
  252. if (Levitation && type != BURN) { /* riv05!a3 */
  253. pline("You can't reach the floor!");
  254. return (1);
  255. }
  256. if (oep && oep->engr_type == DUST) {
  257. pline("You wipe out the message that was written here.");
  258. del_engr(oep);
  259. oep = 0;
  260. }
  261. if (type == DUST && oep) {
  262. pline("You cannot wipe out the message that is %s in the rock.",
  263. (oep->engr_type == BURN) ? "burned" : "engraved");
  264. return (1);
  265. }
  266. pline("What do you want to %s on the floor here? ",
  267. (type == ENGRAVE) ? "engrave" : (type == BURN) ? "burn" : "write");
  268. getlin(buf);
  269. clrlin();
  270. spct = 0;
  271. sp = buf;
  272. while (*sp == ' ')
  273. spct++, sp++;
  274. len = strlen(sp);
  275. if (!len || *buf == '\033') {
  276. if (type == BURN)
  277. otmp->spe++;
  278. return (0);
  279. }
  280. switch (type) {
  281. case DUST:
  282. case BURN:
  283. if (len > 15) {
  284. multi = -(len / 10);
  285. nomovemsg = "You finished writing.";
  286. }
  287. break;
  288. case ENGRAVE: /* here otmp != 0 */
  289. {
  290. int len2 = (otmp->spe + 3) * 2 + 1;
  291. pline("Your %s dull.", aobjnam(otmp, "get"));
  292. if (len2 < len) {
  293. len = len2;
  294. sp[len] = 0;
  295. otmp->spe = -3;
  296. nomovemsg = "You cannot engrave more.";
  297. } else {
  298. otmp->spe -= len / 2;
  299. nomovemsg = "You finished engraving.";
  300. }
  301. multi = -len;
  302. }
  303. break;
  304. }
  305. if (oep)
  306. len += strlen(oep->engr_txt) + spct;
  307. ep = (struct engr *) alloc((unsigned) (sizeof(struct engr) + len + 1));
  308. ep->nxt_engr = head_engr;
  309. head_engr = ep;
  310. ep->engr_x = u.ux;
  311. ep->engr_y = u.uy;
  312. sp = (char *) (ep + 1); /* (char *)ep + sizeof(struct engr) */
  313. ep->engr_txt = sp;
  314. if (oep) {
  315. (void) strcpy(sp, oep->engr_txt);
  316. (void) strcat(sp, buf);
  317. del_engr(oep);
  318. } else
  319. (void) strcpy(sp, buf);
  320. ep->engr_lth = len + 1;
  321. ep->engr_type = type;
  322. ep->engr_time = moves - multi;
  323. /* kludge to protect pline against excessively long texts */
  324. if (len > BUFSZ - 20)
  325. sp[BUFSZ - 20] = 0;
  326. return (1);
  327. }
  328. void
  329. save_engravings(fd)
  330. int fd;
  331. {
  332. struct engr *ep = head_engr;
  333. while (ep) {
  334. if (!ep->engr_lth || !ep->engr_txt[0]) {
  335. ep = ep->nxt_engr;
  336. continue;
  337. }
  338. bwrite(fd, (char *) &(ep->engr_lth), sizeof(ep->engr_lth));
  339. bwrite(fd, (char *) ep, sizeof(struct engr) + ep->engr_lth);
  340. ep = ep->nxt_engr;
  341. }
  342. bwrite(fd, (char *) nul, sizeof(unsigned));
  343. head_engr = 0;
  344. }
  345. void
  346. rest_engravings(fd)
  347. int fd;
  348. {
  349. struct engr *ep;
  350. unsigned lth;
  351. head_engr = 0;
  352. while (1) {
  353. mread(fd, (char *) &lth, sizeof(unsigned));
  354. if (lth == 0)
  355. return;
  356. ep = (struct engr *) alloc(sizeof(struct engr) + lth);
  357. mread(fd, (char *) ep, sizeof(struct engr) + lth);
  358. ep->nxt_engr = head_engr;
  359. ep->engr_txt = (char *) (ep + 1); /* Andreas Bormann */
  360. head_engr = ep;
  361. }
  362. }
  363. void
  364. del_engr(ep)
  365. struct engr *ep;
  366. {
  367. struct engr *ept;
  368. if (ep == head_engr)
  369. head_engr = ep->nxt_engr;
  370. else {
  371. for (ept = head_engr; ept; ept = ept->nxt_engr) {
  372. if (ept->nxt_engr == ep) {
  373. ept->nxt_engr = ep->nxt_engr;
  374. goto fnd;
  375. }
  376. }
  377. impossible("Error in del_engr?");
  378. return;
  379. fnd: ;
  380. }
  381. free((char *) ep);
  382. }