MCPcopy Index your code
hub / github.com/NetHack/NetHack / cutworm

Function cutworm

src/worm.c:372–477  ·  view source on GitHub ↗

cutworm() * * Check for mon->wormno before calling this function! * * When hitting a worm (worm) at position x, y, with a weapon (weap), * there is a chance that the worm will be cut in half, and a chance * that both halves will survive. */

Source from the content-addressed store, hash-verified

370 * that both halves will survive.
371 */
372void
373cutworm(struct monst *worm, coordxy x, coordxy y,
374 boolean cuttier) /* hit is by wielded blade or axe or by thrown axe */
375{
376 struct wseg *curr, *new_tail;
377 struct monst *new_worm;
378 int wnum = worm->wormno;
379 int cut_chance, new_wnum;
380
381 if (!wnum)
382 return; /* bullet-proofing */
383
384 if (x == worm->mx && y == worm->my)
385 return; /* hit on head */
386
387 /* cutting goes best with a cuttier weapon */
388 cut_chance = rnd(20); /* Normally 1-16 does not cut, 17-20 does, */
389 if (cuttier)
390 cut_chance += 10; /* with a blade 1- 6 does not cut, 7-20 does. */
391
392 if (cut_chance < 17)
393 return; /* not good enough */
394
395 /* Find the segment that was attacked. */
396 curr = wtails[wnum];
397
398 while ((curr->wx != x) || (curr->wy != y)) {
399 curr = curr->nseg;
400 if (!curr) {
401 impossible("cutworm: no segment at (%d,%d)", (int) x, (int) y);
402 return;
403 }
404 }
405
406 /* If this is the tail segment, then the worm just loses it. */
407 if (curr == wtails[wnum]) {
408 shrink_worm(wnum);
409 return;
410 }
411
412 /*
413 * Split the worm. The tail for the new worm is the old worm's tail.
414 * The tail for the old worm is the segment that follows "curr",
415 * and "curr" becomes the dummy segment under the new head.
416 */
417 new_tail = wtails[wnum];
418 wtails[wnum] = curr->nseg;
419 curr->nseg = (struct wseg *) 0; /* split the worm */
420
421 /*
422 * At this point, the old worm is correct. Any new worm will have
423 * its head at "curr" and its tail at "new_tail". The old worm
424 * must be at least level 3 in order to produce a new worm.
425 */
426 new_worm = 0;
427 new_wnum = (worm->m_lev >= 3 && !rn2(3)) ? get_wormno() : 0;
428 if (new_wnum) {
429 remove_monster(x, y); /* clone_mon puts new head here */

Callers 2

known_hitumFunction · 0.85
thitmonstFunction · 0.85

Calls 14

rndFunction · 0.85
shrink_wormFunction · 0.85
rn2Function · 0.85
get_wormnoFunction · 0.85
clone_monFunction · 0.85
s_suffixFunction · 0.85
mon_namFunction · 0.85
YouFunction · 0.85
toss_wsegsFunction · 0.85
dFunction · 0.85
place_wsegsFunction · 0.85
MonnamFunction · 0.85

Tested by

no test coverage detected