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

Function insert_branch

src/dungeon.c:462–508  ·  view source on GitHub ↗

* Add the given branch to the branch list. The branch list is ordered * by end1 dungeon and level followed by end2 dungeon and level. If * extract_first is true, then the branch is already part of the list * but needs to be repositioned. */

Source from the content-addressed store, hash-verified

460 * but needs to be repositioned.
461 */
462void
463insert_branch(branch *new_branch, boolean extract_first)
464{
465 branch *curr, *prev;
466 long new_val, curr_val, prev_val;
467
468 if (extract_first) {
469 for (prev = 0, curr = svb.branches; curr;
470 prev = curr, curr = curr->next)
471 if (curr == new_branch)
472 break;
473
474 if (!curr)
475 panic("insert_branch: not found");
476 if (prev)
477 prev->next = curr->next;
478 else
479 svb.branches = curr->next;
480 }
481 new_branch->next = (branch *) 0;
482
483/* Convert the branch into a unique number so we can sort them. */
484#define branch_val(bp) \
485 ((((long) (bp)->end1.dnum * (MAXLEVEL + 1) + (long) (bp)->end1.dlevel) \
486 * (MAXDUNGEON + 1) * (MAXLEVEL + 1)) \
487 + ((long) (bp)->end2.dnum * (MAXLEVEL + 1) + (long) (bp)->end2.dlevel))
488
489 /*
490 * Insert the new branch into the correct place in the branch list.
491 */
492 prev = (branch *) 0;
493 prev_val = -1;
494 new_val = branch_val(new_branch);
495 for (curr = svb.branches; curr;
496 prev_val = curr_val, prev = curr, curr = curr->next) {
497 curr_val = branch_val(curr);
498 if (prev_val < new_val && new_val <= curr_val)
499 break;
500 }
501 if (prev) {
502 new_branch->next = curr;
503 prev->next = new_branch;
504 } else {
505 new_branch->next = svb.branches;
506 svb.branches = new_branch;
507 }
508}
509
510#undef branch_val
511

Callers 3

add_branchFunction · 0.85
fixup_level_locationsFunction · 0.85
mk_knox_portalFunction · 0.85

Calls 1

panicFunction · 0.50

Tested by

no test coverage detected