Wednesday, September 19, 2007

Basic Strategy game - upgrade money gain (with bank)

Today I will try to explain how to create Bank structure, which would allow us to make more money per turn.

First theory of this addon:

  • each level of bank adds 5%
  • level 1 cost 500 money to upgrade
  • each level costs 30% more then last one
Now we create formulas that we will code later.
Each level of bank adds 5%, so our idea is to multiply basic amount with some coefficient. In our case this is 1.05 for level 1, level 2 will be 1.05*1.05, level 3 will be 1.05*1.05*1.05. So you can see where this is leading; we can create universal formula by using level and basic amount.
Cash we gain is then: 1.05^banklevel , in PHP this is used by pow() function.
Exactly the same we will use for costs of next level, but percentage is higher so coefficient would be 1.30 in this case.

Now to coding! Lets open config.php (from last post) and modify it.
After this:
//money amount to add for each round
$add_money = 1000;
Add:
//coefficient to use (5% addon = 1.05)
$k_money = 1.05;

Now find:
//calculate how much money user gained
$all_money = $nr_rounds * $add_money;
And change it to:
//calculate how much money user gained
$all_money = $nr_rounds * $add_money * pow($k_money, $user[bank]);

This "pow($k_money, $user[bank])" will work like we wanted to (1.05 ^ banklevel).

Now lets create new php file like upgrade.php (don't forget to include config.php in beginning of file).
//calculate money needed for next level of bank upgrade (php part of file)
$need_money = 500 * pow(1.30, $user[bank]);

//we must create form for upgrades (html part of file) with button.

<form>
Bank level: <? echo $user[bank]; ?> <br>
Next level: <input type="submit" value="<? echo number_format($need_money) ?> money" name="upgradebank">
</form>

//now we create another php part of this file (you could put that on top right after when you calculate $need_money), where we will tell what to do when button is pressed.
if ($_POST[upgradebank]) {

//we check if user has enough money to upgrade bank
if ($user[money] < $need_money) {
//we use die function to stop script execution and to show message what is wrong
die ("You don't have enough money to upgrade Bank!");
}

//everything is OK, we update our user table
mysql_query("UPDATE users SET money=money-'$need_money', bank=bank+'1' WHERE id='$user[id]'");

}


This concludes our tutorial, come back for more. :)
Next time we will create system which allows user to train units.

Any questions, ideas and comments are welcome.

3 comments:

Anonymous said...

Very nice blog :)

Tempor said...

A very nice, solid description of the required steps!

Perhaps a "mapping" tutorial would be nice. How to represent a "map" and coordinates in the database. Usually this is a tricky part.

Keep up the good work!

Saljutin said...

When I finish this tutorial (3 more steps), then I write post about how to create efficient map system.

Thanks for comments :)