Jump to content

[SOLVED] PHP math problem


LOUDMOUTH

Recommended Posts

I have this code for a EXP system in a game.  The way it is right now it is gaining exp to fast per level.  So by the time you get to level 40 the exp is far too great.  I am looking for some help with a correct Algorithm to match this Ratio.

 

I have tried changing the

$a += floor($x+1500*pow(4, ($x/7)));

many times to different equations and have no luck so far at solving this.

 

Anyone have any suggestions?

 

I want level 1 to start off with 450 exp, and per level, increase by around 450.  with no max EXP

 

The below code equals this ratio

//level 2 - 500

//level 3 - 1500

//level 4 - 3500

//level 5 - 6000

 

I am looking for this ratio

//level 2 - 988

//level 3 - 1482

//level 4 - 1976

//level 5 - 2471

 

 

function experience($L) {
  $a=0;
  $end=0;
  for($x=1; $x<$L; $x++) {
    $a += floor($x+1500*pow(4, ($x/7)));
  }
  return floor($a/4);
}
function Get_The_Level($exp) {
  $a=0;
  $end =0;
  for($x=1; ($end==0 && $x<100); $x++) {
    $a += floor($x+1500*pow(4, ($x/7)));
    if ($exp >= floor($a/4)){
} else {	
  return $x;
  $end=1;
}
  }
}

function Get_Max_Exp($L){
$end=0;
if ($exp == 0){
return 457;
$end =1;
  }
for($L=1;($L<100 && $end==0);$L++) {
  $exp = experience($L);
  //echo $exp;
  if ($exp >= $user_class->exp){
	return $exp;
	$end=1;
  }
}
}

Link to comment
Share on other sites

Thanks for the help.

 

When I get to level 2 I still have the exp from the prior level towards the next level though.

this is what It shows right after I got to level 2

 

EXP:  500 / 1485 [33%]

 

How can I make it  EXP: 0/1485 [0%]  when you get to level 2

Link to comment
Share on other sites

Also this is in the same file as the EXP code

 

$this->exp = $worked['exp'];

$this->level = Get_The_Level($this->exp);

$this->maxexp = experience($this->level +1);

$this->exppercent = ($this->exp == 0) ? 0 : floor(($this->exp / $this->maxexp) * 100);

$this->formattedexp = $this->exp." / ".$this->maxexp." [".$this->exppercent."%]";

 

Link to comment
Share on other sites

Maybe what you're looking for is a function that grows logarithmically?

 

Logarithmic growth "decreases" as the independent variable grows. Or rather, the derivative decreases if you know calculus. What this practically means in your case is that higher levels would need more exp to level up than lower levels would. I assume that's the kind of behavior your looking for.

 

I've attached a graph to give you an idea of how logarithmic growth is. Specifically, that graphed function is the natural logarithm. I've also attached a graph of the function you're using now. It's essentially linear.

 

The numbers you posted in your first post can, with good approximation, be both linear, quadratic, exponential and power growth (probably others as well, I stopped checking after that), so it's a bit difficult giving you a good formula without knowing more about it.

 

[attachment deleted by admin]

Link to comment
Share on other sites

Yes I need a logarithmically based EXP system.

 

Like you said Daniel, It would be nice to have the players gradually have to gain more exp as the Level # gets higher.

 

Math isnt my strong point, so any input you may have would help a ton :)

 

right now I have a Classes.php that has the first codes I posted here.  Then I have it echo the EXP in the Index.php 

 

here is everything I have in the classes.php page that has to do with the EXP.  If I can get that behaving properly then echo is cake.

 

function experience($L) {
  $a=0;
  $end=0;
  for($x=1; $x<$L; $x++) {
    $a += floor($x+1500*pow(4, ($x/7)));
  }
  return floor($a/4);
}

 

function Get_The_Level($exp) {
  $a=0;
  $end =0;
  for($x=1; ($end==0 && $x<100); $x++) {
    $a += floor($x+1500*pow(4, ($x/7)));
    if ($exp >= floor($a/4)){
} else {
  return $x;
  $end=1;
}
  }
}

 

function Get_Max_Exp($L){
$end=0;
if ($exp == 0){
return 457;
$end =1;
  }

 

for($L=1;($L<100 && $end==0);$L++) {
  $exp = experience($L);
  //echo $exp;
  if ($exp >= $user_class->exp){
	return $exp;
	$end=1;
  }
}
}

 

Then further down the page is this

 

$this->exp = $worked['exp'];
$this->level = Get_The_Level($this->exp);
$this->maxexp = experience($this->level +1);
$this->exppercent = ($this->exp == 0) ? 0 : floor(($this->exp / $this->maxexp) * 100);
$this->formattedexp = $this->exp." / ".$this->maxexp." [".$this->exppercent."%]";

 

<td width='35%'><?php echo $user_class->formattedexp; ?></td>

 

 

Link to comment
Share on other sites

Maybe a function like this:

 

[tex]f(x) = \mbox{floor}\left( \left(\frac{\log_{10}\left(\frac{x}{200}\right)}{0.3}\right)^{1.12} \right)+1[/tex]

 

Or in php:

 

function getLevel($exp)
{
return floor(pow(log10($exp) / 200, 1.12)) + 1;
}

 

That function will grow as in the attached graph.

 

It will not be the same numbers as you requested, but the levels would be like this:

Level 2: 400

Level 3: 722

Level 4: 1263

Level 5: 2165

Level 6: 3660

Level 7: 6119

Level 8: 10135

 

[attachment deleted by admin]

Link to comment
Share on other sites

Using this I was able to get the EXP to set 495 exp per level

function experience($L) {
  return $L * 495;
}

function Get_The_Level($exp) {
   return floor($exp / 495) + 1;
}

 

This echo is returning 0/495 EXP at all times now, 

echo 'EXP: '.($exp - experience($L)).' / '.experience($L+1);

 

 

I just need their total exp subtracted from the exp they have.

 

If I can get the echo to work properly I think we have got it, any suggestions?

Link to comment
Share on other sites

I tried

echo 'EXP: '.($this->exp - experience($this->level)).' / '.experience($this->level+1);

 

I got a syntax error

OK, give us something to work with.

I'm having to guess the variable names that you're using, and have no idea whether they exist, or they're in scope.

 

What error are you getting?

Link to comment
Share on other sites

This is showing 0/495 EXP no matter how much exp I gain right now

echo 'EXP: '.($exp - experience($L)).' / '.experience($L+1);

 

 

This one show syntax error

echo 'EXP: '.($this->exp - experience($this->level)).' / '.experience($this->level+1);

 

This one shows 495/1485 EXP  <--close but I need it to set the exp to 0 towards next Levels' EXP requirement when you gain a level.

<?php echo $user_class->formattedexp; ?></td>

 

 

 

This is everything on the classes.php that has the exp codes in it.

 

//level 2 - 500

//level 3 - 1500

//level 4 - 3500

//level 5 - 6000






function experience($L) {

  $a=0;

  $end=0;

  for($x=1; $x<$L; $x++) {

    $a += floor($x+1500*pow(4, ($x/7)));

  }

  return floor($a/4);

}



function Get_The_Level($exp) {

  $a=0;

  $end =0;

  for($x=1; ($end==0 && $x<100); $x++) {

    $a += floor($x+1500*pow(4, ($x/7)));

    if ($exp >= floor($a/4)){

} else {

  return $x;

  $end=1;

}

  }

}



function Get_Max_Exp($L){

$end=0;

if ($exp == 0){

return 457;

$end =1;

  }

for($L=1;($L<100 && $end==0);$L++) {

  $exp = experience($L);

  //echo $exp;

  if ($exp >= $user_class->exp){

	return $exp;

	$end=1;

  }

}

}

 

class User {
$this->exp = $worked['exp'];

$this->level = Get_The_Level($this->exp);

$this->maxexp = experience($this->level +1);

$this->exppercent = ($this->exp == 0) ? 0 : floor(($this->exp / $this->maxexp) * 100);

$this->formattedexp = $this->exp." / ".$this->maxexp." [".$this->exppercent."%]";

 

 

 

Link to comment
Share on other sites

This is working great to give me 495 exp per Level, but I need it to also add the prior level's exp to the new goal exp.

 

Meaning if you are Level 1 you have  0/495 exp and when you are level 2 you have 0/900 exp, Level 3 you would have 0/1350 exp.

 

function experience($L) {
  return $L * 495;
}

function Get_The_Level($exp) {
   return floor($exp / 495) + 1;
}

 

can anyone help get this right?

Link to comment
Share on other sites

try

<?php
function my_level($exp){
$per_lev = 495;
$cur_lev = 0;
while ($exp >= $cur_lev * $per_lev){
	$exp -= $cur_lev * $per_lev;
	$cur_lev++;
}
return array('lev' => $cur_lev, 'exp' => $exp, 'for_next_lev' => $cur_lev * $per_lev);
}
$x = my_level(1500);
echo 'you are on level ', $x['lev'], ', ', $x['exp'],
' / ', $x['for_next_lev'] , ' (',round($x['exp'] /  $x['for_next_lev'] *100, 2), '%)'; 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.