hack.makemon.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* $NetBSD: hack.makemon.c,v 1.6 2003/04/02 18:36:37 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.makemon.c,v 1.6 2003/04/02 18:36:37 jsm Exp $");
  64. #endif /* not lint */
  65. #include "hack.h"
  66. #include "extern.h"
  67. struct monst zeromonst;
  68. /*
  69. * called with [x,y] = coordinates;
  70. * [0,0] means anyplace
  71. * [u.ux,u.uy] means: call mnexto (if !in_mklev)
  72. *
  73. * In case we make an Orc or killer bee, we make an entire horde (swarm);
  74. * note that in this case we return only one of them (the one at [x,y]).
  75. */
  76. struct monst *
  77. makemon(const struct permonst *ptr, int x, int y)
  78. {
  79. struct monst *mtmp;
  80. int tmp, ct;
  81. boolean anything = (!ptr);
  82. if (x != 0 || y != 0)
  83. if (m_at(x, y))
  84. return ((struct monst *) 0);
  85. if (ptr) {
  86. if (strchr(fut_geno, ptr->mlet))
  87. return ((struct monst *) 0);
  88. } else {
  89. ct = CMNUM - strlen(fut_geno);
  90. if (strchr(fut_geno, 'm'))
  91. ct++; /* make only 1 minotaur */
  92. if (strchr(fut_geno, '@'))
  93. ct++;
  94. if (ct <= 0)
  95. return (0); /* no more monsters! */
  96. tmp = rn2(ct * dlevel / 24 + 7);
  97. if (tmp < dlevel - 4)
  98. tmp = rn2(ct * dlevel / 24 + 12);
  99. if (tmp >= ct)
  100. tmp = rn1(ct - ct / 2, ct / 2);
  101. for (ct = 0; ct < CMNUM; ct++) {
  102. ptr = &mons[ct];
  103. if (strchr(fut_geno, ptr->mlet))
  104. continue;
  105. if (!tmp--)
  106. goto gotmon;
  107. }
  108. panic("makemon?");
  109. }
  110. gotmon:
  111. mtmp = newmonst(ptr->pxlth);
  112. *mtmp = zeromonst; /* clear all entries in structure */
  113. for (ct = 0; ct < (int)ptr->pxlth; ct++)
  114. ((char *) &(mtmp->mextra[0]))[ct] = 0;
  115. mtmp->nmon = fmon;
  116. fmon = mtmp;
  117. mtmp->m_id = flags.ident++;
  118. mtmp->data = ptr;
  119. mtmp->mxlth = ptr->pxlth;
  120. if (ptr->mlet == 'D')
  121. mtmp->mhpmax = mtmp->mhp = 80;
  122. else if (!ptr->mlevel)
  123. mtmp->mhpmax = mtmp->mhp = rnd(4);
  124. else
  125. mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
  126. mtmp->mx = x;
  127. mtmp->my = y;
  128. mtmp->mcansee = 1;
  129. if (ptr->mlet == 'M') {
  130. mtmp->mimic = 1;
  131. mtmp->mappearance = ']';
  132. }
  133. if (!in_mklev) {
  134. if (x == u.ux && y == u.uy && ptr->mlet != ' ')
  135. mnexto(mtmp);
  136. if (x == 0 && y == 0)
  137. rloc(mtmp);
  138. }
  139. if (ptr->mlet == 's' || ptr->mlet == 'S') {
  140. mtmp->mhide = mtmp->mundetected = 1;
  141. if (in_mklev)
  142. if (mtmp->mx && mtmp->my)
  143. (void) mkobj_at(0, mtmp->mx, mtmp->my);
  144. }
  145. if (ptr->mlet == ':') {
  146. mtmp->cham = 1;
  147. (void) newcham(mtmp, &mons[dlevel + 14 + rn2(CMNUM - 14 - dlevel)]);
  148. }
  149. if (ptr->mlet == 'I' || ptr->mlet == ';')
  150. mtmp->minvis = 1;
  151. if (ptr->mlet == 'L' || ptr->mlet == 'N'
  152. || (in_mklev && strchr("&w;", ptr->mlet) && rn2(5))
  153. )
  154. mtmp->msleep = 1;
  155. #ifndef NOWORM
  156. if (ptr->mlet == 'w' && getwn(mtmp))
  157. initworm(mtmp);
  158. #endif /* NOWORM */
  159. if (anything)
  160. if (ptr->mlet == 'O' || ptr->mlet == 'k') {
  161. coord mm;
  162. int cnt = rnd(10);
  163. mm.x = x;
  164. mm.y = y;
  165. while (cnt--) {
  166. mm = enexto(mm.x, mm.y);
  167. (void) makemon(ptr, mm.x, mm.y);
  168. }
  169. }
  170. return (mtmp);
  171. }
  172. coord
  173. enexto(xx, yy)
  174. xchar xx, yy;
  175. {
  176. xchar x, y;
  177. coord foo[15], *tfoo;
  178. int range;
  179. tfoo = foo;
  180. range = 1;
  181. do { /* full kludge action. */
  182. for (x = xx - range; x <= xx + range; x++)
  183. if (goodpos(x, yy - range)) {
  184. tfoo->x = x;
  185. tfoo++->y = yy - range;
  186. if (tfoo == &foo[15])
  187. goto foofull;
  188. }
  189. for (x = xx - range; x <= xx + range; x++)
  190. if (goodpos(x, yy + range)) {
  191. tfoo->x = x;
  192. tfoo++->y = yy + range;
  193. if (tfoo == &foo[15])
  194. goto foofull;
  195. }
  196. for (y = yy + 1 - range; y < yy + range; y++)
  197. if (goodpos(xx - range, y)) {
  198. tfoo->x = xx - range;
  199. tfoo++->y = y;
  200. if (tfoo == &foo[15])
  201. goto foofull;
  202. }
  203. for (y = yy + 1 - range; y < yy + range; y++)
  204. if (goodpos(xx + range, y)) {
  205. tfoo->x = xx + range;
  206. tfoo++->y = y;
  207. if (tfoo == &foo[15])
  208. goto foofull;
  209. }
  210. range++;
  211. } while (tfoo == foo);
  212. foofull:
  213. return (foo[rn2(tfoo - foo)]);
  214. }
  215. int
  216. goodpos(int x, int y)
  217. { /* used only in mnexto and rloc */
  218. return (
  219. !(x < 1 || x > COLNO - 2 || y < 1 || y > ROWNO - 2 ||
  220. m_at(x, y) || !ACCESSIBLE(levl[x][y].typ)
  221. || (x == u.ux && y == u.uy)
  222. || sobj_at(ENORMOUS_ROCK, x, y)
  223. ));
  224. }
  225. void
  226. rloc(mtmp)
  227. struct monst *mtmp;
  228. {
  229. int tx, ty;
  230. char ch = mtmp->data->mlet;
  231. #ifndef NOWORM
  232. if (ch == 'w' && mtmp->mx)
  233. return; /* do not relocate worms */
  234. #endif /* NOWORM */
  235. do {
  236. tx = rn1(COLNO - 3, 2);
  237. ty = rn2(ROWNO);
  238. } while (!goodpos(tx, ty));
  239. mtmp->mx = tx;
  240. mtmp->my = ty;
  241. if (u.ustuck == mtmp) {
  242. if (u.uswallow) {
  243. u.ux = tx;
  244. u.uy = ty;
  245. docrt();
  246. } else
  247. u.ustuck = 0;
  248. }
  249. pmon(mtmp);
  250. }
  251. struct monst *
  252. mkmon_at(let, x, y)
  253. char let;
  254. int x, y;
  255. {
  256. int ct;
  257. const struct permonst *ptr;
  258. for (ct = 0; ct < CMNUM; ct++) {
  259. ptr = &mons[ct];
  260. if (ptr->mlet == let)
  261. return (makemon(ptr, x, y));
  262. }
  263. return (0);
  264. }