MCPcopy Create free account
hub / github.com/NetHack/NetHack / give_to_nearby_mon

Function give_to_nearby_mon

src/bones.c:225–255  ·  view source on GitHub ↗

Give object to a random object-liking monster on or adjacent to x,y but skipping hero's location. If no such monster, place object on floor at x,y. */

Source from the content-addressed store, hash-verified

223 but skipping hero's location.
224 If no such monster, place object on floor at x,y. */
225staticfn void
226give_to_nearby_mon(struct obj *otmp, coordxy x, coordxy y)
227{
228 struct monst *mtmp;
229 struct monst *selected = (struct monst *) 0;
230 int nmon = 0, xx, yy;
231
232 for (xx = x - 1; xx <= x + 1; ++xx) {
233 for (yy = y - 1; yy <= y + 1; ++yy) {
234 if (!isok(xx, yy))
235 continue;
236 if (u_at(xx, yy))
237 continue;
238 if (!(mtmp = m_at(xx, yy)))
239 continue;
240 /* This doesn't do any checks on otmp to see that it matches the
241 * likes_* property, intentionally. Assume that the monster is
242 * rifling through and taking things that look interesting. */
243 if (!(likes_gold(mtmp->data) || likes_gems(mtmp->data)
244 || likes_objs(mtmp->data) || likes_magic(mtmp->data)))
245 continue;
246 nmon++;
247 if (!rn2(nmon))
248 selected = mtmp;
249 }
250 }
251 if (selected && can_carry(selected, otmp))
252 add_to_minv(selected, otmp);
253 else
254 place_object(otmp, x, y);
255}
256
257/* called by savebones(); also by finish_paybill(shk.c) */
258void

Callers 1

drop_upon_deathFunction · 0.85

Calls 5

isokFunction · 0.85
rn2Function · 0.85
can_carryFunction · 0.85
add_to_minvFunction · 0.85
place_objectFunction · 0.85

Tested by

no test coverage detected