function rolldamage()
{
    global $badguy, $session, $creatureattack, $creatureatkmod, $adjustment;
    global $creaturedefmod, $defmod, $atkmod, $buffset, $atk, $def, $options;
    if ($badguy['creaturehealth'] > 0 && $session['user']['hitpoints'] > 0) {
        if ($options['type'] == 'pvp') {
            $adjustedcreaturedefense = $badguy['creaturedefense'];
        } else {
            $adjustedcreaturedefense = $creaturedefmod * $badguy['creaturedefense'] / ($adjustment * $adjustment);
        }
        $creatureattack = $badguy['creatureattack'] * $creatureatkmod;
        $adjustedselfdefense = $session['user']['defense'] * $adjustment * $defmod;
        /*
        debug("Base creature defense: " . $badguy['creaturedefense']);
        debug("Creature defense mod: $creaturedefmod");
        debug("Adjustment: $adjustment");
        debug("Adjusted creature defense: $adjustedcreaturedefense");
        debug("Adjusted creature attack: $creatureattack");
        debug("Adjusted self defense: $adjustedselfdefense");
        */
        while (!isset($creaturedmg) || !isset($selfdmg) || $creaturedmg == 0 && $selfdmg == 0) {
            $atk = $session['user']['attack'] * $atkmod;
            if (e_rand(1, 20) == 1 && $options['type'] != "pvp") {
                $atk *= 3;
            }
            /*
            debug("Attack score: $atk");
            */
            $patkroll = bell_rand(0, $atk);
            /*
            debug("Player Attack roll: $patkroll");
            */
            // Set up for crit detection
            $atk = $patkroll;
            $catkroll = bell_rand(0, $adjustedcreaturedefense);
            /*
            debug("Creature defense roll: $catkroll");
            */
            $creaturedmg = 0 - (int) ($catkroll - $patkroll);
            if ($creaturedmg < 0) {
                $creaturedmg = (int) ($creaturedmg / 2);
                $creaturedmg = round($buffset['badguydmgmod'] * $creaturedmg, 0);
            }
            if ($creaturedmg > 0) {
                $creaturedmg = round($buffset['dmgmod'] * $creaturedmg, 0);
            }
            $pdefroll = bell_rand(0, $adjustedselfdefense);
            $catkroll = bell_rand(0, $creatureattack);
            /*
              debug("Creature attack roll: $catkroll");
              debug("Player defense roll: $pdefroll");
            */
            $selfdmg = 0 - (int) ($pdefroll - $catkroll);
            if ($selfdmg < 0) {
                $selfdmg = (int) ($selfdmg / 2);
                $selfdmg = round($selfdmg * $buffset['dmgmod'], 0);
            }
            if ($selfdmg > 0) {
                $selfdmg = round($selfdmg * $buffset['badguydmgmod'], 0);
            }
        }
    } else {
        $creaturedmg = 0;
        $selfdmg = 0;
    }
    // Handle god mode's invulnerability
    if ($buffset['invulnerable']) {
        $creaturedmg = abs($creaturedmg);
        $selfdmg = -abs($selfdmg);
    }
    return array("creaturedmg" => isset($creaturedmg) ? $creaturedmg : 0, "selfdmg" => isset($selfdmg) ? $selfdmg : 0);
}
function insults_roll($type)
{
    require_once "lib/bell_rand.php";
    require_once "modules/staminasystem/lib/lib.php";
    //Coarse are more predictably average, Classy can go really well or really poorly, Confusing is somewhere in the middle.
    //Insult efficacy is affected by Action level.
    if ($type == "Classy") {
        $r = bell_rand(100);
        $a = get_player_action("Insults - Classy");
        $r += $a['lvl'] / 5;
    }
    if ($type == "Confusing") {
        $r = bell_rand(10, 90);
        $a = get_player_action("Insults - Confusing");
        $r += $a['lvl'] / 5;
    }
    if ($type == "Coarse") {
        $r = bell_rand(20, 80);
        $a = get_player_action("Insults - Coarse");
        $r += $a['lvl'] / 5;
    }
    if ($r <= 0) {
        return 1;
    }
    if ($r <= 20) {
        return 2;
    }
    if ($r <= 40) {
        return 3;
    }
    if ($r <= 60) {
        return 4;
    }
    if ($r <= 80) {
        return 5;
    }
    if ($r <= 100) {
        return 6;
    }
    return 7;
}