MajorMUD Experience Calculation Formula

General MajorMUD Discussion
Post Reply
User avatar
syntax
Site Admin
Posts: 525
Joined: Tue Jun 02, 2009 10:02 am

MajorMUD Experience Calculation Formula

Post by syntax »

Don't know if this has been published before, but I just translated the code to PHP:

https://pastebin.com/VmgC0dgS

https://onlinephp.io/c/f361d

Code: Select all

<?php
 
$race = 45; //race exp table percentage
$class = 250; //class exp table percentage
$startlevel = 25; //return exp needed starting at this level
$numlevels = 10; //return this many levels starting from startlevel

$exptable = GetExpTable($race+$class, $startlevel, $numlevels);
echo '<pre>'.print_r($exptable, true).'</pre>';
 
function GetExpTable($exptable, $startlevel=1, $numlevels=100) {
    $i = 0; $potential_new_exp = 0; $ret = array(); $lastexp = 0; $startexp = 0;
    $running_exp_tabulation = 0; $billions_tabulator = 0; $MAX_UINT = 4294967295;
	$exp_multiplier = 0; $exp_divisor = 0; $ALTERNATE_NEW_EXP = 0; $accurate_exp = array();
	
    // Calculate experience
    for ($i = 1; $i <= ($startlevel+$numlevels-1); $i++) {
        $startexp = $lastexp;
		if ($i == 1) {
            $running_exp_tabulation = 0;
        } elseif ($i == 2) {
            $running_exp_tabulation = $exptable * 10;
        } else {
			
			if ($i <= 26) { //levels 1-26
				list($exp_multiplier, $exp_divisor) = GetExpModifiers($i);
			} elseif ($i <= 55) { //levels 27-55
				$exp_multiplier = 115;
				$exp_divisor = 100;
			} elseif ($i <= 58) { //levels 56-58
				$exp_multiplier = 109;
				$exp_divisor = 100;
			} else { //levels 59+
				$exp_multiplier = 108;
				$exp_divisor = 100;
			}
			
            if ($exp_multiplier == 0 || $exp_divisor == 0) {
                $potential_new_exp = 0;
            } else {
                $potential_new_exp = $running_exp_tabulation * $exp_multiplier;
            }
			
			if ($potential_new_exp > $MAX_UINT) { //UINT ROLLOVER #1
				$num_divides = 0;
				while ($potential_new_exp > $MAX_UINT) {
					$running_exp_tabulation = floor($running_exp_tabulation / 100);
					$potential_new_exp = $running_exp_tabulation * $exp_multiplier;
					$num_divides++;
				}
				if ($num_divides > 1) {
					$ALTERNATE_NEW_EXP = floor(($running_exp_tabulation * $exp_multiplier * 100) / $exp_divisor);
				} else {
					$ALTERNATE_NEW_EXP = floor($potential_new_exp / $exp_divisor);
				}
				while ($num_divides > 0) {
					$ALTERNATE_NEW_EXP *= 100;
					$num_divides--;
				}
			} else {
				$ALTERNATE_NEW_EXP = floor($potential_new_exp / $exp_divisor);
			}
            
			$j = (1000000 * $exp_multiplier * $billions_tabulator);
			while ($j > $MAX_UINT) { $j -= $MAX_UINT+1; } //UINT ROLLOVER #2
			for ($j; $j >= 1000000000; $billions_tabulator++) {
				$j -= 1000000000;
			}
			
			$k = ($j + $ALTERNATE_NEW_EXP);
			for ($k; $k >= 1000000000; $billions_tabulator++) {
				$k -= 1000000000;
			}
			
			$running_exp_tabulation = $k;
        }
		
		$lastexp = $running_exp_tabulation + ($billions_tabulator*1000000000);
		//$neededexp = $lastexp - $startexp;
		
		if ($i >= $startlevel) {
			$ret[$i] = $lastexp;
		}
    }
	
	return $ret;
}

// Returns the experience modifiers for a given level.
function GetExpModifiers($iLevel) {
    switch ($iLevel) {
        case 3:
            return [40, 20];
        case 4:
        case 5:
            return [44, 24];
        case 6:
        case 7:
            return [48, 28];
        case 8:
        case 9:
            return [52, 32];
        case 10:
        case 11:
            return [56, 36];
        case 12:
        case 13:
            return [60, 40];
        case 14:
        case 15:
            return [65, 45];
        case 16:
        case 17:
            return [70, 50];
        case 18:
            return [75, 55];
        default:
            if ($iLevel <= 26) {
                return [50, 40];
            } else {
                return [23, 20];
            }
    }
}

?>

Output from above (which is level 25 plus 9 more levels for a stock dark-elf ranger):

Code: Select all

Array
(
    [25] => 22276322
    [26] => 27845402
    [27] => 32022212
    [28] => 36825543
    [29] => 42349374
    [30] => 48701600
    [31] => 56006800
    [32] => 64407800
    [33] => 74068900
    [34] => 85179200
)


User avatar
syntax
Site Admin
Posts: 525
Joined: Tue Jun 02, 2009 10:02 am

Re: MajorMUD Experience Calculation Formula

Post by syntax »

fyi-- Discovered it would miscalculate around level 55. Then discovered it miscalculating when exp rollovers occurred due to unsigned 32-bit integers in mmud. These issue have all been corrected.


Post Reply