133151Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
233151Sbostic /* hack.makemon.c - version 1.0.2 */
333151Sbostic
433151Sbostic #include "hack.h"
533151Sbostic extern char fut_geno[];
633151Sbostic extern char *index();
733151Sbostic extern struct obj *mkobj_at();
833151Sbostic struct monst zeromonst;
933151Sbostic
1033151Sbostic /*
1133151Sbostic * called with [x,y] = coordinates;
1233151Sbostic * [0,0] means anyplace
1333151Sbostic * [u.ux,u.uy] means: call mnexto (if !in_mklev)
1433151Sbostic *
1533151Sbostic * In case we make an Orc or killer bee, we make an entire horde (swarm);
1633151Sbostic * note that in this case we return only one of them (the one at [x,y]).
1733151Sbostic */
1833151Sbostic struct monst *
makemon(ptr,x,y)1933151Sbostic makemon(ptr,x,y)
2033151Sbostic register struct permonst *ptr;
2133151Sbostic {
2233151Sbostic register struct monst *mtmp;
2333151Sbostic register tmp, ct;
2433151Sbostic boolean anything = (!ptr);
25*33152Sbostic extern boolean in_mklev;
2633151Sbostic
2733151Sbostic if(x != 0 || y != 0) if(m_at(x,y)) return((struct monst *) 0);
2833151Sbostic if(ptr){
2933151Sbostic if(index(fut_geno, ptr->mlet)) return((struct monst *) 0);
3033151Sbostic } else {
3133151Sbostic ct = CMNUM - strlen(fut_geno);
3233151Sbostic if(index(fut_geno, 'm')) ct++; /* make only 1 minotaur */
3333151Sbostic if(index(fut_geno, '@')) ct++;
3433151Sbostic if(ct <= 0) return(0); /* no more monsters! */
3533151Sbostic tmp = rn2(ct*dlevel/24 + 7);
3633151Sbostic if(tmp < dlevel - 4) tmp = rn2(ct*dlevel/24 + 12);
3733151Sbostic if(tmp >= ct) tmp = rn1(ct - ct/2, ct/2);
3833151Sbostic for(ct = 0; ct < CMNUM; ct++){
3933151Sbostic ptr = &mons[ct];
4033151Sbostic if(index(fut_geno, ptr->mlet))
4133151Sbostic continue;
4233151Sbostic if(!tmp--) goto gotmon;
4333151Sbostic }
4433151Sbostic panic("makemon?");
4533151Sbostic }
4633151Sbostic gotmon:
4733151Sbostic mtmp = newmonst(ptr->pxlth);
4833151Sbostic *mtmp = zeromonst; /* clear all entries in structure */
4933151Sbostic for(ct = 0; ct < ptr->pxlth; ct++)
5033151Sbostic ((char *) &(mtmp->mextra[0]))[ct] = 0;
5133151Sbostic mtmp->nmon = fmon;
5233151Sbostic fmon = mtmp;
5333151Sbostic mtmp->m_id = flags.ident++;
5433151Sbostic mtmp->data = ptr;
5533151Sbostic mtmp->mxlth = ptr->pxlth;
5633151Sbostic if(ptr->mlet == 'D') mtmp->mhpmax = mtmp->mhp = 80;
5733151Sbostic else if(!ptr->mlevel) mtmp->mhpmax = mtmp->mhp = rnd(4);
5833151Sbostic else mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
5933151Sbostic mtmp->mx = x;
6033151Sbostic mtmp->my = y;
6133151Sbostic mtmp->mcansee = 1;
6233151Sbostic if(ptr->mlet == 'M'){
6333151Sbostic mtmp->mimic = 1;
6433151Sbostic mtmp->mappearance = ']';
6533151Sbostic }
6633151Sbostic if(!in_mklev) {
6733151Sbostic if(x == u.ux && y == u.uy && ptr->mlet != ' ')
6833151Sbostic mnexto(mtmp);
6933151Sbostic if(x == 0 && y == 0)
7033151Sbostic rloc(mtmp);
71*33152Sbostic }
7233151Sbostic if(ptr->mlet == 's' || ptr->mlet == 'S') {
7333151Sbostic mtmp->mhide = mtmp->mundetected = 1;
7433151Sbostic if(in_mklev)
7533151Sbostic if(mtmp->mx && mtmp->my)
7633151Sbostic (void) mkobj_at(0, mtmp->mx, mtmp->my);
7733151Sbostic }
7833151Sbostic if(ptr->mlet == ':') {
7933151Sbostic mtmp->cham = 1;
8033151Sbostic (void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
8133151Sbostic }
8233151Sbostic if(ptr->mlet == 'I' || ptr->mlet == ';')
8333151Sbostic mtmp->minvis = 1;
8433151Sbostic if(ptr->mlet == 'L' || ptr->mlet == 'N'
8533151Sbostic || (in_mklev && index("&w;", ptr->mlet) && rn2(5))
8633151Sbostic ) mtmp->msleep = 1;
8733151Sbostic
8833151Sbostic #ifndef NOWORM
8933151Sbostic if(ptr->mlet == 'w' && getwn(mtmp))
9033151Sbostic initworm(mtmp);
9133151Sbostic #endif NOWORM
9233151Sbostic
9333151Sbostic if(anything) if(ptr->mlet == 'O' || ptr->mlet == 'k') {
9433151Sbostic coord enexto();
9533151Sbostic coord mm;
9633151Sbostic register int cnt = rnd(10);
9733151Sbostic mm.x = x;
9833151Sbostic mm.y = y;
9933151Sbostic while(cnt--) {
10033151Sbostic mm = enexto(mm.x, mm.y);
10133151Sbostic (void) makemon(ptr, mm.x, mm.y);
10233151Sbostic }
10333151Sbostic }
10433151Sbostic
10533151Sbostic return(mtmp);
10633151Sbostic }
10733151Sbostic
10833151Sbostic coord
enexto(xx,yy)10933151Sbostic enexto(xx,yy)
11033151Sbostic register xchar xx,yy;
11133151Sbostic {
11233151Sbostic register xchar x,y;
11333151Sbostic coord foo[15], *tfoo;
11433151Sbostic int range;
11533151Sbostic
11633151Sbostic tfoo = foo;
11733151Sbostic range = 1;
11833151Sbostic do { /* full kludge action. */
11933151Sbostic for(x = xx-range; x <= xx+range; x++)
12033151Sbostic if(goodpos(x, yy-range)) {
12133151Sbostic tfoo->x = x;
12233151Sbostic tfoo++->y = yy-range;
12333151Sbostic if(tfoo == &foo[15]) goto foofull;
12433151Sbostic }
12533151Sbostic for(x = xx-range; x <= xx+range; x++)
12633151Sbostic if(goodpos(x,yy+range)) {
12733151Sbostic tfoo->x = x;
12833151Sbostic tfoo++->y = yy+range;
12933151Sbostic if(tfoo == &foo[15]) goto foofull;
13033151Sbostic }
13133151Sbostic for(y = yy+1-range; y < yy+range; y++)
13233151Sbostic if(goodpos(xx-range,y)) {
13333151Sbostic tfoo->x = xx-range;
13433151Sbostic tfoo++->y = y;
13533151Sbostic if(tfoo == &foo[15]) goto foofull;
13633151Sbostic }
13733151Sbostic for(y = yy+1-range; y < yy+range; y++)
13833151Sbostic if(goodpos(xx+range,y)) {
13933151Sbostic tfoo->x = xx+range;
14033151Sbostic tfoo++->y = y;
14133151Sbostic if(tfoo == &foo[15]) goto foofull;
14233151Sbostic }
14333151Sbostic range++;
14433151Sbostic } while(tfoo == foo);
14533151Sbostic foofull:
14633151Sbostic return( foo[rn2(tfoo-foo)] );
14733151Sbostic }
14833151Sbostic
goodpos(x,y)14933151Sbostic goodpos(x,y) /* used only in mnexto and rloc */
15033151Sbostic {
15133151Sbostic return(
15233151Sbostic ! (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 ||
15333151Sbostic m_at(x,y) || !ACCESSIBLE(levl[x][y].typ)
15433151Sbostic || (x == u.ux && y == u.uy)
15533151Sbostic || sobj_at(ENORMOUS_ROCK, x, y)
15633151Sbostic ));
15733151Sbostic }
15833151Sbostic
15933151Sbostic rloc(mtmp)
16033151Sbostic struct monst *mtmp;
16133151Sbostic {
16233151Sbostic register tx,ty;
16333151Sbostic register char ch = mtmp->data->mlet;
16433151Sbostic
16533151Sbostic #ifndef NOWORM
16633151Sbostic if(ch == 'w' && mtmp->mx) return; /* do not relocate worms */
16733151Sbostic #endif NOWORM
16833151Sbostic do {
16933151Sbostic tx = rn1(COLNO-3,2);
17033151Sbostic ty = rn2(ROWNO);
17133151Sbostic } while(!goodpos(tx,ty));
17233151Sbostic mtmp->mx = tx;
17333151Sbostic mtmp->my = ty;
17433151Sbostic if(u.ustuck == mtmp){
17533151Sbostic if(u.uswallow) {
17633151Sbostic u.ux = tx;
17733151Sbostic u.uy = ty;
17833151Sbostic docrt();
17933151Sbostic } else u.ustuck = 0;
18033151Sbostic }
18133151Sbostic pmon(mtmp);
18233151Sbostic }
18333151Sbostic
18433151Sbostic struct monst *
mkmon_at(let,x,y)18533151Sbostic mkmon_at(let,x,y)
18633151Sbostic char let;
18733151Sbostic register int x,y;
18833151Sbostic {
18933151Sbostic register int ct;
19033151Sbostic register struct permonst *ptr;
19133151Sbostic
19233151Sbostic for(ct = 0; ct < CMNUM; ct++) {
19333151Sbostic ptr = &mons[ct];
19433151Sbostic if(ptr->mlet == let)
19533151Sbostic return(makemon(ptr,x,y));
19633151Sbostic }
19733151Sbostic return(0);
19833151Sbostic }
199