-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathdogmove.c
More file actions
1434 lines (1300 loc) · 48.8 KB
/
Copy pathdogmove.c
File metadata and controls
1434 lines (1300 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 3.6 dogmove.c $NHDT-Date: 1557094801 2019/05/05 22:20:01 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.74 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "mfndpos.h"
extern boolean notonhead;
STATIC_DCL boolean FDECL(dog_hunger, (struct monst *, struct edog *));
STATIC_DCL int FDECL(dog_invent, (struct monst *, struct edog *, int));
STATIC_DCL int FDECL(dog_goal, (struct monst *, struct edog *, int, int, int));
STATIC_DCL struct monst *FDECL(find_targ, (struct monst *, int, int, int));
STATIC_OVL int FDECL(find_friends, (struct monst *, struct monst *, int));
STATIC_DCL struct monst *FDECL(best_target, (struct monst *));
STATIC_DCL long FDECL(score_targ, (struct monst *, struct monst *));
STATIC_DCL boolean FDECL(can_reach_location, (struct monst *, XCHAR_P,
XCHAR_P, XCHAR_P, XCHAR_P));
STATIC_DCL boolean FDECL(could_reach_item, (struct monst *, XCHAR_P, XCHAR_P));
STATIC_DCL void FDECL(quickmimic, (struct monst *));
/* pick a carried item for pet to drop */
struct obj *
droppables(mon)
struct monst *mon;
{
struct obj *obj, *wep, dummy, *pickaxe, *unihorn, *key;
dummy = zeroobj;
dummy.otyp = GOLD_PIECE; /* not STRANGE_OBJECT or tools of interest */
dummy.oartifact = 1; /* so real artifact won't override "don't keep it" */
pickaxe = unihorn = key = (struct obj *) 0;
wep = MON_WEP(mon);
if (is_animal(mon->data) || mindless(mon->data)) {
/* won't hang on to any objects of these types */
pickaxe = unihorn = key = &dummy; /* act as if already have them */
} else {
/* don't hang on to pick-axe if can't use one or don't need one */
if (!tunnels(mon->data) || !needspick(mon->data))
pickaxe = &dummy;
/* don't hang on to key if can't open doors */
if (nohands(mon->data) || verysmall(mon->data))
key = &dummy;
}
if (wep) {
if (is_pick(wep))
pickaxe = wep;
if (wep->otyp == UNICORN_HORN)
unihorn = wep;
/* don't need any wielded check for keys... */
}
for (obj = mon->minvent; obj; obj = obj->nobj) {
switch (obj->otyp) {
case DWARVISH_MATTOCK:
/* reject mattock if couldn't wield it */
if (which_armor(mon, W_ARMS))
break;
/* keep mattock in preference to pick unless pick is already
wielded or is an artifact and mattock isn't */
if (pickaxe && pickaxe->otyp == PICK_AXE && pickaxe != wep
&& (!pickaxe->oartifact || obj->oartifact))
return pickaxe; /* drop the one we earlier decided to keep */
/*FALLTHRU*/
case PICK_AXE:
if (!pickaxe || (obj->oartifact && !pickaxe->oartifact)) {
if (pickaxe)
return pickaxe;
pickaxe = obj; /* keep this digging tool */
continue;
}
break;
case UNICORN_HORN:
/* reject cursed unicorn horns */
if (obj->cursed)
break;
/* keep artifact unihorn in preference to ordinary one */
if (!unihorn || (obj->oartifact && !unihorn->oartifact)) {
if (unihorn)
return unihorn;
unihorn = obj; /* keep this unicorn horn */
continue;
}
break;
case SKELETON_KEY:
/* keep key in preference to lock-pick */
if (key && key->otyp == LOCK_PICK
&& (!key->oartifact || obj->oartifact))
return key; /* drop the one we earlier decided to keep */
/*FALLTHRU*/
case LOCK_PICK:
/* keep lock-pick in preference to credit card */
if (key && key->otyp == CREDIT_CARD
&& (!key->oartifact || obj->oartifact))
return key;
/*FALLTHRU*/
case CREDIT_CARD:
if (!key || (obj->oartifact && !key->oartifact)) {
if (key)
return key;
key = obj; /* keep this unlocking tool */
continue;
}
break;
default:
break;
}
if (!obj->owornmask && obj != wep)
return obj;
}
return (struct obj *) 0; /* don't drop anything */
}
static NEARDATA const char nofetch[] = { BALL_CLASS, CHAIN_CLASS, ROCK_CLASS,
0 };
STATIC_VAR xchar gtyp, gx, gy; /* type and position of dog's current goal */
STATIC_PTR void FDECL(wantdoor, (int, int, genericptr_t));
boolean
cursed_object_at(x, y)
int x, y;
{
struct obj *otmp;
for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
if (otmp->cursed)
return TRUE;
return FALSE;
}
int
dog_nutrition(mtmp, obj)
struct monst *mtmp;
struct obj *obj;
{
int nutrit;
/*
* It is arbitrary that the pet takes the same length of time to eat
* as a human, but gets more nutritional value.
*/
if (obj->oclass == FOOD_CLASS) {
if (obj->otyp == CORPSE) {
mtmp->meating = 3 + (mons[obj->corpsenm].cwt >> 6);
nutrit = mons[obj->corpsenm].cnutrit;
} else {
mtmp->meating = objects[obj->otyp].oc_delay;
nutrit = objects[obj->otyp].oc_nutrition;
}
switch (mtmp->data->msize) {
case MZ_TINY:
nutrit *= 8;
break;
case MZ_SMALL:
nutrit *= 6;
break;
default:
case MZ_MEDIUM:
nutrit *= 5;
break;
case MZ_LARGE:
nutrit *= 4;
break;
case MZ_HUGE:
nutrit *= 3;
break;
case MZ_GIGANTIC:
nutrit *= 2;
break;
}
if (obj->oeaten) {
mtmp->meating = eaten_stat(mtmp->meating, obj);
nutrit = eaten_stat(nutrit, obj);
}
} else if (obj->oclass == COIN_CLASS) {
mtmp->meating = (int) (obj->quan / 2000) + 1;
if (mtmp->meating < 0)
mtmp->meating = 1;
nutrit = (int) (obj->quan / 20);
if (nutrit < 0)
nutrit = 0;
} else {
/* Unusual pet such as gelatinous cube eating odd stuff.
* meating made consistent with wild monsters in mon.c.
* nutrit made consistent with polymorphed player nutrit in
* eat.c. (This also applies to pets eating gold.)
*/
mtmp->meating = obj->owt / 20 + 1;
nutrit = 5 * objects[obj->otyp].oc_nutrition;
}
return nutrit;
}
/* returns 2 if pet dies, otherwise 1 */
int
dog_eat(mtmp, obj, x, y, devour)
register struct monst *mtmp;
register struct obj *obj; /* if unpaid, then thrown or kicked by hero */
int x, y; /* dog's starting location, might be different from current */
boolean devour;
{
register struct edog *edog = EDOG(mtmp);
boolean poly, grow, heal, eyes, slimer, deadmimic;
int nutrit;
long oprice;
char objnambuf[BUFSZ];
objnambuf[0] = '\0';
if (edog->hungrytime < monstermoves)
edog->hungrytime = monstermoves;
nutrit = dog_nutrition(mtmp, obj);
deadmimic = (obj->otyp == CORPSE && (obj->corpsenm == PM_SMALL_MIMIC
|| obj->corpsenm == PM_LARGE_MIMIC
|| obj->corpsenm == PM_GIANT_MIMIC));
slimer = (obj->otyp == CORPSE && obj->corpsenm == PM_GREEN_SLIME);
poly = polyfodder(obj);
grow = mlevelgain(obj);
heal = mhealup(obj);
eyes = (obj->otyp == CARROT);
if (devour) {
if (mtmp->meating > 1)
mtmp->meating /= 2;
if (nutrit > 1)
nutrit = (nutrit * 3) / 4;
}
edog->hungrytime += nutrit;
mtmp->mconf = 0;
if (edog->mhpmax_penalty) {
/* no longer starving */
mtmp->mhpmax += edog->mhpmax_penalty;
edog->mhpmax_penalty = 0;
}
if (mtmp->mflee && mtmp->mfleetim > 1)
mtmp->mfleetim /= 2;
if (mtmp->mtame < 20)
mtmp->mtame++;
if (x != mtmp->mx || y != mtmp->my) { /* moved & ate on same turn */
newsym(x, y);
newsym(mtmp->mx, mtmp->my);
}
/* food items are eaten one at a time; entire stack for other stuff */
if (obj->quan > 1L && obj->oclass == FOOD_CLASS)
obj = splitobj(obj, 1L);
if (obj->unpaid)
iflags.suppress_price++;
if (is_pool(x, y) && !Underwater) {
/* Don't print obj */
/* TODO: Reveal presence of sea monster (especially sharks) */
} else {
/* food is at monster's current location, <mx,my>;
<x,y> was monster's location at start of this turn;
they might be the same but will be different when
the monster is moving+eating on same turn */
boolean seeobj = cansee(mtmp->mx, mtmp->my),
sawpet = cansee(x, y) && mon_visible(mtmp);
/* Observe the action if either the food location or the pet
itself is in view. When pet which was in view moves to an
unseen spot to eat the food there, avoid referring to that
pet as "it". However, we want "it" if invisible/unsensed
pet eats visible food. */
if (sawpet || (seeobj && canspotmon(mtmp))) {
if (tunnels(mtmp->data))
pline("%s digs in.", noit_Monnam(mtmp));
else
pline("%s %s %s.", noit_Monnam(mtmp),
devour ? "devours" : "eats", distant_name(obj, doname));
} else if (seeobj)
pline("It %s %s.", devour ? "devours" : "eats",
distant_name(obj, doname));
}
if (obj->unpaid) {
Strcpy(objnambuf, xname(obj));
iflags.suppress_price--;
}
/* It's a reward if it's DOGFOOD and the player dropped/threw it.
We know the player had it if invlet is set. -dlc */
if (dogfood(mtmp, obj) == DOGFOOD && obj->invlet)
#ifdef LINT
edog->apport = 0;
#else
edog->apport += (int) (200L / ((long) edog->dropdist + monstermoves
- edog->droptime));
#endif
if (mtmp->data == &mons[PM_RUST_MONSTER] && obj->oerodeproof) {
/* The object's rustproofing is gone now */
if (obj->unpaid)
costly_alteration(obj, COST_DEGRD);
obj->oerodeproof = 0;
mtmp->mstun = 1;
if (canseemon(mtmp) && flags.verbose) {
pline("%s spits %s out in disgust!", Monnam(mtmp),
distant_name(obj, doname));
}
} else if (obj == uball) {
unpunish();
delobj(obj); /* we assume this can't be unpaid */
} else if (obj == uchain) {
unpunish();
} else {
if (obj->unpaid) {
/* edible item owned by shop has been thrown or kicked
by hero and caught by tame or food-tameable monst */
oprice = unpaid_cost(obj, TRUE);
pline("That %s will cost you %ld %s.", objnambuf, oprice,
currency(oprice));
/* delobj->obfree will handle actual shop billing update */
}
delobj(obj);
}
#if 0 /* pet is eating, so slime recovery is not feasible... */
/* turning into slime might be cureable */
if (slimer && munslime(mtmp, FALSE)) {
/* but the cure (fire directed at self) might be fatal */
if (DEADMONSTER(mtmp))
return 2;
slimer = FALSE; /* sliming is avoided, skip polymorph */
}
#endif
if (poly || slimer) {
struct permonst *ptr = slimer ? &mons[PM_GREEN_SLIME] : 0;
(void) newcham(mtmp, ptr, FALSE, cansee(mtmp->mx, mtmp->my));
}
/* limit "instant" growth to prevent potential abuse */
if (grow && (int) mtmp->m_lev < (int) mtmp->data->mlevel + 15) {
if (!grow_up(mtmp, (struct monst *) 0))
return 2;
}
if (heal)
mtmp->mhp = mtmp->mhpmax;
if ((eyes || heal) && !mtmp->mcansee)
mcureblindness(mtmp, canseemon(mtmp));
if (deadmimic)
quickmimic(mtmp);
return 1;
}
/* hunger effects -- returns TRUE on starvation */
STATIC_OVL boolean
dog_hunger(mtmp, edog)
struct monst *mtmp;
struct edog *edog;
{
if (monstermoves > edog->hungrytime + 500) {
if (!carnivorous(mtmp->data) && !herbivorous(mtmp->data)) {
edog->hungrytime = monstermoves + 500;
/* but not too high; it might polymorph */
} else if (!edog->mhpmax_penalty) {
/* starving pets are limited in healing */
int newmhpmax = mtmp->mhpmax / 3;
mtmp->mconf = 1;
edog->mhpmax_penalty = mtmp->mhpmax - newmhpmax;
mtmp->mhpmax = newmhpmax;
if (mtmp->mhp > mtmp->mhpmax)
mtmp->mhp = mtmp->mhpmax;
if (DEADMONSTER(mtmp))
goto dog_died;
if (cansee(mtmp->mx, mtmp->my))
pline("%s is confused from hunger.", Monnam(mtmp));
else if (couldsee(mtmp->mx, mtmp->my))
beg(mtmp);
else
You_feel("worried about %s.", y_monnam(mtmp));
stop_occupation();
} else if (monstermoves > edog->hungrytime + 750
|| DEADMONSTER(mtmp)) {
dog_died:
if (mtmp->mleashed && mtmp != u.usteed)
Your("leash goes slack.");
else if (cansee(mtmp->mx, mtmp->my))
pline("%s starves.", Monnam(mtmp));
else
You_feel("%s for a moment.",
Hallucination ? "bummed" : "sad");
mondied(mtmp);
return TRUE;
}
}
return FALSE;
}
/* do something with object (drop, pick up, eat) at current position
* returns 1 if object eaten (since that counts as dog's move), 2 if died
*/
STATIC_OVL int
dog_invent(mtmp, edog, udist)
register struct monst *mtmp;
register struct edog *edog;
int udist;
{
register int omx, omy, carryamt = 0;
struct obj *obj, *otmp;
if (mtmp->msleeping || !mtmp->mcanmove)
return 0;
omx = mtmp->mx;
omy = mtmp->my;
/* If we are carrying something then we drop it (perhaps near @).
* Note: if apport == 1 then our behaviour is independent of udist.
* Use udist+1 so steed won't cause divide by zero.
*/
if (droppables(mtmp)) {
if (!rn2(udist + 1) || !rn2(edog->apport))
if (rn2(10) < edog->apport) {
relobj(mtmp, (int) mtmp->minvis, TRUE);
if (edog->apport > 1)
edog->apport--;
edog->dropdist = udist; /* hpscdi!jon */
edog->droptime = monstermoves;
}
} else {
if ((obj = level.objects[omx][omy]) != 0
&& !index(nofetch, obj->oclass)
#ifdef MAIL
&& obj->otyp != SCR_MAIL
#endif
) {
int edible = dogfood(mtmp, obj);
if ((edible <= CADAVER
/* starving pet is more aggressive about eating */
|| (edog->mhpmax_penalty && edible == ACCFOOD))
&& could_reach_item(mtmp, obj->ox, obj->oy))
return dog_eat(mtmp, obj, omx, omy, FALSE);
carryamt = can_carry(mtmp, obj);
if (carryamt > 0 && !obj->cursed
&& could_reach_item(mtmp, obj->ox, obj->oy)) {
if (rn2(20) < edog->apport + 3) {
if (rn2(udist) || !rn2(edog->apport)) {
otmp = obj;
if (carryamt != obj->quan)
otmp = splitobj(obj, carryamt);
if (cansee(omx, omy) && flags.verbose)
pline("%s picks up %s.", Monnam(mtmp),
distant_name(otmp, doname));
obj_extract_self(otmp);
newsym(omx, omy);
(void) mpickobj(mtmp, otmp);
if (attacktype(mtmp->data, AT_WEAP)
&& mtmp->weapon_check == NEED_WEAPON) {
mtmp->weapon_check = NEED_HTH_WEAPON;
(void) mon_wield_item(mtmp);
}
m_dowear(mtmp, FALSE);
}
}
}
}
}
return 0;
}
/* set dog's goal -- gtyp, gx, gy;
returns -1/0/1 (dog's desire to approach player) or -2 (abort move) */
STATIC_OVL int
dog_goal(mtmp, edog, after, udist, whappr)
register struct monst *mtmp;
struct edog *edog;
int after, udist, whappr;
{
register int omx, omy;
boolean in_masters_sight, dog_has_minvent;
register struct obj *obj;
xchar otyp;
int appr;
/* Steeds don't move on their own will */
if (mtmp == u.usteed)
return -2;
omx = mtmp->mx;
omy = mtmp->my;
in_masters_sight = couldsee(omx, omy);
dog_has_minvent = (droppables(mtmp) != 0);
if (!edog || mtmp->mleashed) { /* he's not going anywhere... */
gtyp = APPORT;
gx = u.ux;
gy = u.uy;
} else {
#define DDIST(x, y) (dist2(x, y, omx, omy))
#define SQSRCHRADIUS 5
int min_x, max_x, min_y, max_y;
register int nx, ny;
gtyp = UNDEF; /* no goal as yet */
gx = gy = 0; /* suppress 'used before set' message */
if ((min_x = omx - SQSRCHRADIUS) < 1)
min_x = 1;
if ((max_x = omx + SQSRCHRADIUS) >= COLNO)
max_x = COLNO - 1;
if ((min_y = omy - SQSRCHRADIUS) < 0)
min_y = 0;
if ((max_y = omy + SQSRCHRADIUS) >= ROWNO)
max_y = ROWNO - 1;
/* nearby food is the first choice, then other objects */
for (obj = fobj; obj; obj = obj->nobj) {
nx = obj->ox;
ny = obj->oy;
if (nx >= min_x && nx <= max_x && ny >= min_y && ny <= max_y) {
otyp = dogfood(mtmp, obj);
/* skip inferior goals */
if (otyp > gtyp || otyp == UNDEF)
continue;
/* avoid cursed items unless starving */
if (cursed_object_at(nx, ny)
&& !(edog->mhpmax_penalty && otyp < MANFOOD))
continue;
/* skip completely unreachable goals */
if (!could_reach_item(mtmp, nx, ny)
|| !can_reach_location(mtmp, mtmp->mx, mtmp->my, nx, ny))
continue;
if (otyp < MANFOOD) {
if (otyp < gtyp || DDIST(nx, ny) < DDIST(gx, gy)) {
gx = nx;
gy = ny;
gtyp = otyp;
}
} else if (gtyp == UNDEF && in_masters_sight
&& !dog_has_minvent
&& (!levl[omx][omy].lit || levl[u.ux][u.uy].lit)
&& (otyp == MANFOOD || m_cansee(mtmp, nx, ny))
&& edog->apport > rn2(8)
&& can_carry(mtmp, obj) > 0) {
gx = nx;
gy = ny;
gtyp = APPORT;
}
}
}
}
/* follow player if appropriate */
if (gtyp == UNDEF || (gtyp != DOGFOOD && gtyp != APPORT
&& monstermoves < edog->hungrytime)) {
gx = u.ux;
gy = u.uy;
if (after && udist <= 4 && gx == u.ux && gy == u.uy)
return -2;
appr = (udist >= 9) ? 1 : (mtmp->mflee) ? -1 : 0;
if (udist > 1) {
if (!IS_ROOM(levl[u.ux][u.uy].typ) || !rn2(4) || whappr
|| (dog_has_minvent && rn2(edog->apport)))
appr = 1;
}
/* if you have dog food it'll follow you more closely */
if (appr == 0)
for (obj = invent; obj; obj = obj->nobj)
if (dogfood(mtmp, obj) == DOGFOOD) {
appr = 1;
break;
}
} else
appr = 1; /* gtyp != UNDEF */
if (mtmp->mconf)
appr = 0;
#define FARAWAY (COLNO + 2) /* position outside screen */
if (gx == u.ux && gy == u.uy && !in_masters_sight) {
register coord *cp;
cp = gettrack(omx, omy);
if (cp) {
gx = cp->x;
gy = cp->y;
if (edog)
edog->ogoal.x = 0;
} else {
/* assume master hasn't moved far, and reuse previous goal */
if (edog && edog->ogoal.x
&& (edog->ogoal.x != omx || edog->ogoal.y != omy)) {
gx = edog->ogoal.x;
gy = edog->ogoal.y;
edog->ogoal.x = 0;
} else {
int fardist = FARAWAY * FARAWAY;
gx = gy = FARAWAY; /* random */
do_clear_area(omx, omy, 9, wantdoor, (genericptr_t) &fardist);
/* here gx == FARAWAY e.g. when dog is in a vault */
if (gx == FARAWAY || (gx == omx && gy == omy)) {
gx = u.ux;
gy = u.uy;
} else if (edog) {
edog->ogoal.x = gx;
edog->ogoal.y = gy;
}
}
}
} else if (edog) {
edog->ogoal.x = 0;
}
return appr;
}
STATIC_OVL struct monst *
find_targ(mtmp, dx, dy, maxdist)
register struct monst *mtmp;
int dx, dy;
int maxdist;
{
struct monst *targ = 0;
int curx = mtmp->mx, cury = mtmp->my;
int dist = 0;
/* Walk outwards */
for ( ; dist < maxdist; ++dist) {
curx += dx;
cury += dy;
if (!isok(curx, cury))
break;
/* FIXME: Check if we hit a wall/door/boulder to
* short-circuit unnecessary subsequent checks
*/
/* If we can't see up to here, forget it - will this
* mean pets in corridors don't breathe at monsters
* in rooms? If so, is that necessarily bad?
*/
if (!m_cansee(mtmp, curx, cury))
break;
if (curx == mtmp->mux && cury == mtmp->muy)
return &youmonst;
if ((targ = m_at(curx, cury)) != 0) {
/* Is the monster visible to the pet? */
if ((!targ->minvis || perceives(mtmp->data))
&& !targ->mundetected)
break;
/* If the pet can't see it, it assumes it aint there */
targ = 0;
}
}
return targ;
}
STATIC_OVL int
find_friends(mtmp, mtarg, maxdist)
struct monst *mtmp, *mtarg;
int maxdist;
{
struct monst *pal;
int dx = sgn(mtarg->mx - mtmp->mx),
dy = sgn(mtarg->my - mtmp->my);
int curx = mtarg->mx, cury = mtarg->my;
int dist = distmin(mtarg->mx, mtarg->my, mtmp->mx, mtmp->my);
for ( ; dist <= maxdist; ++dist) {
curx += dx;
cury += dy;
if (!isok(curx, cury))
return 0;
/* If the pet can't see beyond this point, don't
* check any farther
*/
if (!m_cansee(mtmp, curx, cury))
return 0;
/* Does pet think you're here? */
if (mtmp->mux == curx && mtmp->muy == cury)
return 1;
pal = m_at(curx, cury);
if (pal) {
if (pal->mtame) {
/* Pet won't notice invisible pets */
if (!pal->minvis || perceives(mtmp->data))
return 1;
} else {
/* Quest leaders and guardians are always seen */
if (pal->data->msound == MS_LEADER
|| pal->data->msound == MS_GUARDIAN)
return 1;
}
}
}
return 0;
}
STATIC_OVL long
score_targ(mtmp, mtarg)
struct monst *mtmp, *mtarg;
{
long score = 0L;
/* If the monster is confused, normal scoring is disrupted -
* anything may happen
*/
/* Give 1 in 3 chance of safe breathing even if pet is confused or
* if you're on the quest start level */
if (!mtmp->mconf || !rn2(3) || Is_qstart(&u.uz)) {
int mtmp_lev;
aligntyp align1 = A_NONE, align2 = A_NONE; /* For priests, minions */
boolean faith1 = TRUE, faith2 = TRUE;
if (mtmp->isminion)
align1 = EMIN(mtmp)->min_align;
else if (mtmp->ispriest)
align1 = EPRI(mtmp)->shralign;
else
faith1 = FALSE;
if (mtarg->isminion)
align2 = EMIN(mtarg)->min_align; /* MAR */
else if (mtarg->ispriest)
align2 = EPRI(mtarg)->shralign; /* MAR */
else
faith2 = FALSE;
/* Never target quest friendlies */
if (mtarg->data->msound == MS_LEADER
|| mtarg->data->msound == MS_GUARDIAN)
return -5000L;
/* D: Fixed angelic beings using gaze attacks on coaligned priests */
if (faith1 && faith2 && align1 == align2 && mtarg->mpeaceful) {
score -= 5000L;
return score;
}
/* Is monster adjacent? */
if (distmin(mtmp->mx, mtmp->my, mtarg->mx, mtarg->my) <= 1) {
score -= 3000L;
return score;
}
/* Is the monster peaceful or tame? */
if (/*mtarg->mpeaceful ||*/ mtarg->mtame || mtarg == &youmonst) {
/* Pets will never be targeted */
score -= 3000L;
return score;
}
/* Is master/pet behind monster? Check up to 15 squares beyond pet. */
if (find_friends(mtmp, mtarg, 15)) {
score -= 3000L;
return score;
}
/* Target hostile monsters in preference to peaceful ones */
if (!mtarg->mpeaceful)
score += 10;
/* Is the monster passive? Don't waste energy on it, if so */
if (mtarg->data->mattk[0].aatyp == AT_NONE)
score -= 1000;
/* Even weak pets with breath attacks shouldn't take on very
low-level monsters. Wasting breath on lichens is ridiculous. */
if ((mtarg->m_lev < 2 && mtmp->m_lev > 5)
|| (mtmp->m_lev > 12 && mtarg->m_lev < mtmp->m_lev - 9
&& u.ulevel > 8 && mtarg->m_lev < u.ulevel - 7))
score -= 25;
/* for strength purposes, a vampshifter in weak form (vampire bat,
fog cloud, maybe wolf) will attack as if in vampire form;
otherwise if won't do much and usually wouldn't suffer enough
damage (from counterattacks) to switch back to vampire form;
make it be more aggressive by behaving as if stronger */
mtmp_lev = mtmp->m_lev;
if (is_vampshifter(mtmp) && mtmp->data->mlet != S_VAMPIRE) {
/* is_vampshifter() implies (mtmp->cham >= LOW_PM) */
mtmp_lev = mons[mtmp->cham].mlevel;
/* actual vampire level would range from 1.0*mlvl to 1.5*mlvl */
mtmp_lev += rn2(mtmp_lev / 2 + 1);
/* we don't expect actual level in weak form to exceed
base level of strong form, but handle that if it happens */
if (mtmp->m_lev > mtmp_lev)
mtmp_lev = mtmp->m_lev;
}
/* And pets will hesitate to attack vastly stronger foes.
This penalty will be discarded if master's in trouble. */
if (mtarg->m_lev > mtmp_lev + 4L)
score -= (mtarg->m_lev - mtmp_lev) * 20L;
/* All things being the same, go for the beefiest monster. This
bonus should not be large enough to override the pet's aversion
to attacking much stronger monsters. */
score += mtarg->m_lev * 2 + mtarg->mhp / 3;
}
/* Fuzz factor to make things less predictable when very
similar targets are abundant. */
score += rnd(5);
/* Pet may decide not to use ranged attack when confused */
if (mtmp->mconf && !rn2(3))
score -= 1000;
return score;
}
STATIC_OVL struct monst *
best_target(mtmp)
struct monst *mtmp; /* Pet */
{
int dx, dy;
long bestscore = -40000L, currscore;
struct monst *best_targ = 0, *temp_targ = 0;
/* Help! */
if (!mtmp)
return 0;
/* If the pet is blind, it's not going to see any target */
if (!mtmp->mcansee)
return 0;
/* Search for any monsters lined up with the pet, within an arbitrary
* distance from the pet (7 squares, even along diagonals). Monsters
* are assigned scores and the best score is chosen.
*/
for (dy = -1; dy < 2; ++dy) {
for (dx = -1; dx < 2; ++dx) {
if (!dx && !dy)
continue;
/* Traverse the line to find the first monster within 7
* squares. Invisible monsters are skipped (if the
* pet doesn't have see invisible).
*/
temp_targ = find_targ(mtmp, dx, dy, 7);
/* Nothing in this line? */
if (!temp_targ)
continue;
/* Decide how attractive the target is */
currscore = score_targ(mtmp, temp_targ);
if (currscore > bestscore) {
bestscore = currscore;
best_targ = temp_targ;
}
}
}
/* Filter out targets the pet doesn't like */
if (bestscore < 0L)
best_targ = 0;
return best_targ;
}
/* return 0 (no move), 1 (move) or 2 (dead) */
int
dog_move(mtmp, after)
register struct monst *mtmp;
int after; /* this is extra fast monster movement */
{
int omx, omy; /* original mtmp position */
int appr, whappr, udist;
int i, j, k;
register struct edog *edog = EDOG(mtmp);
struct obj *obj = (struct obj *) 0;
xchar otyp;
boolean has_edog, cursemsg[9], do_eat = FALSE;
boolean better_with_displacing = FALSE;
xchar nix, niy; /* position mtmp is (considering) moving to */
register int nx, ny; /* temporary coordinates */
xchar cnt, uncursedcnt, chcnt;
int chi = -1, nidist, ndist;
coord poss[9];
long info[9], allowflags;
#define GDIST(x, y) (dist2(x, y, gx, gy))
/*
* Tame Angels have isminion set and an ispriest structure instead of
* an edog structure. Fortunately, guardian Angels need not worry
* about mundane things like eating and fetching objects, and can
* spend all their energy defending the player. (They are the only
* monsters with other structures that can be tame.)
*/
has_edog = !mtmp->isminion;
omx = mtmp->mx;
omy = mtmp->my;
if (has_edog && dog_hunger(mtmp, edog))
return 2; /* starved */
udist = distu(omx, omy);
/* Let steeds eat and maybe throw rider during Conflict */
if (mtmp == u.usteed) {
if (Conflict && !resist(mtmp, RING_CLASS, 0, 0)) {
dismount_steed(DISMOUNT_THROWN);
return 1;
}
udist = 1;
} else if (!udist)
/* maybe we tamed him while being swallowed --jgm */
return 0;
nix = omx; /* set before newdogpos */
niy = omy;
cursemsg[0] = FALSE; /* lint suppression */
info[0] = 0; /* ditto */
if (has_edog) {
j = dog_invent(mtmp, edog, udist);
if (j == 2)
return 2; /* died */
else if (j == 1)
goto newdogpos; /* eating something */
whappr = (monstermoves - edog->whistletime < 5);
} else
whappr = 0;
appr = dog_goal(mtmp, has_edog ? edog : (struct edog *) 0, after, udist,
whappr);
if (appr == -2)
return 0;
allowflags = ALLOW_M | ALLOW_TRAPS | ALLOW_SSM | ALLOW_SANCT;
if (passes_walls(mtmp->data))
allowflags |= (ALLOW_ROCK | ALLOW_WALL);
if (passes_bars(mtmp->data))
allowflags |= ALLOW_BARS;
if (throws_rocks(mtmp->data))
allowflags |= ALLOW_ROCK;
if (is_displacer(mtmp->data))
allowflags |= ALLOW_MDISP;
if (Conflict && !resist(mtmp, RING_CLASS, 0, 0)) {
allowflags |= ALLOW_U;
if (!has_edog) {
/* Guardian angel refuses to be conflicted; rather,
* it disappears, angrily, and sends in some nasties
*/
lose_guardian_angel(mtmp);
return 2; /* current monster is gone */
}
}
#if 0 /* [this is now handled in dochug()] */
if (!Conflict && !mtmp->mconf
&& mtmp == u.ustuck && !sticks(youmonst.data)) {
unstuck(mtmp); /* swallowed case handled above */
You("get released!");
}
#endif
if (!nohands(mtmp->data) && !verysmall(mtmp->data)) {
allowflags |= OPENDOOR;
if (monhaskey(mtmp, TRUE))
allowflags |= UNLOCKDOOR;
/* note: the Wizard and Riders can unlock doors without a key;
they won't use that ability if someone manages to tame them */
}
if (is_giant(mtmp->data))
allowflags |= BUSTDOOR;
if (tunnels(mtmp->data)
&& !Is_rogue_level(&u.uz)) /* same restriction as m_move() */
allowflags |= ALLOW_DIG;
cnt = mfndpos(mtmp, poss, info, allowflags);
/* Normally dogs don't step on cursed items, but if they have no
* other choice they will. This requires checking ahead of time
* to see how many uncursed item squares are around.
*/
uncursedcnt = 0;
for (i = 0; i < cnt; i++) {
nx = poss[i].x;
ny = poss[i].y;
if (MON_AT(nx, ny) && !((info[i] & ALLOW_M) || info[i] & ALLOW_MDISP))
continue;
if (cursed_object_at(nx, ny))
continue;
uncursedcnt++;
}
better_with_displacing = should_displace(mtmp, poss, info, cnt, gx, gy);
chcnt = 0;
chi = -1;
nidist = GDIST(nix, niy);
for (i = 0; i < cnt; i++) {
nx = poss[i].x;
ny = poss[i].y;
cursemsg[i] = FALSE;
/* if leashed, we drag him along. */
if (mtmp->mleashed && distu(nx, ny) > 4)
continue;
/* if a guardian, try to stay close by choice */
if (!has_edog && (j = distu(nx, ny)) > 16 && j >= udist)