Adding Experience to our Combat System(Perl)

I was recently e-mailed by Building Browsergames reader Mehmet Simsek – who noticed something that I completely missed: our combat system doesn’t have any experience or levelling built into it!

When a player kills a monster in the forest using our current codebase, they get gold and an item – but they don’t actually gain any experience(or levels) for killing the monster. Luckily for us, adding experience to our combat system is a quick (and fairly painless) task: we’ll start off by adding a few more stats to the database:

INSERT INTO stats(display_name, short_name) VALUES
    ('Experience','exp'),
    ('Experience Remaining', 'exp_rem');

We will use these two stats to keep track of the experience a player still needs for their next level, and the experience that a monster gives after it has been killed. Exactly what happens when a player levels up is up to you, the game developer.

We will also need to insert the values for the experience stat for our monsters into our database:

INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE short_name='exp'),(SELECT id FROM monsters WHERE name='Crazy Eric'),5,'Monster');
INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE short_name='exp'),(SELECT id FROM monsters WHERE name='Lazy Russell'),10,'Monster');
INSERT INTO entity_stats(stat_id,entity_id,value,entity_type) VALUES ((SELECT id FROM stats WHERE short_name='exp'),(SELECT id FROM monsters WHERE name='Hard Hitting Louis'),15,'Monster');

As you can see, we are inserting rows into our entity_stats table, so that each of the monsters in our game will give different amounts of experience. With these database changes made, it’s just a matter of opening up forest.cgi and making some changes:

109
110
111
112
113
114
115
116
117
my $monster_exp = monsterstats::getMonsterStat('exp',$monsterID);
$parameters{exp} = $monster_exp;
my $exp_rem = stats::getStat('exp_rem',$userID);
$exp_rem -= $monster_exp;
if($exp_rem <= 0) {
    $exp_rem = 100;
    $parameters{level_up} = 1;
}
stats::setStat('exp_rem',$userID,$exp_rem);

You may have noticed that we’re setting some new variables for our template – so we’ll need to modify forest.tmpl as well:

20
21
22
23
<p>You killed <strong><!--tmpl_var name='monster'--></strong>! You gained <strong><!--tmpl_var name='gold'--></strong> gold, and <strong><!--tmpl_var name='exp'--></strong> experience.</p>
<tmpl_if name='level_up'>
	<p><strong>You gained a level!</strong></p>
</tmpl_if>

With all your changes made, try playing your game – you should notice the message “You gained a level!” appearing whenever your character levels up.

Extra Credit

  • Change the forest template so that in the post-combat summary, the player will also be told how much experience remains before their next level.
  • Modify the code given so that your game will track the total experience a player has collected – and also display it on the main page.

Wish there was more?

I'm considering writing an ebook - click here.

.

Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.

Tags: , , ,

Thursday, January 8th, 2009 buildingbrowsergames, medieval
blog comments powered by Disqus

About

Building Browsergames is a blog about browsergames(also known as PBBG's). It's geared towards the beginner to intermediate developer who has an interest in building their own browsergame.

Sponsors

Got Something to Say?

Send an e-mail to luke@buildingbrowsergames.com, or get in touch through Twitter at http://twitter.com/bbrowsergames