*Feel free to read only parts of the post and reply concerning only specific algorithms*

Hello there, I have recently started making a simple web browser game (Coded in VB) in order to make time go by. I have moderate development finished (details shortly) and need some help with my algorithms in particular.

At the moment, I have the inventory, quest, leveling, combat and map systems running. I plan future implementation of extra stuff but would like to get through these pesky algorithms first.

The most important at the moment are my combat algorithms. I have 1 for combat damage dealt to an enemy, combat damage from an enemy and a critical hit calculator. Note, I currently have 2 classes for the game, Rogue and Warrior and currently only have 2 attributes that affect damage, strength and agility. As you see in the code below, rogues gain more from having agility and warriors from having strength. (Rogues have higher crit chance, though less health than warriors.)

Type: r=Rogue, w=Warrior
astr/aagi: Character strength/agility
weapRNG: A random number between the minimum and maximum damage of the equipped weapon
ddef: Enemy defense
tagi: used to calculate critical hit chance
dmg: Damage dealt

``````If a.type = "r" Then
dmg = (((astr / 2) + aagi) + weapRNG) - ddef
tagi = (aagi - (dagi / 2)) * 4
ElseIf a.type = "w" Then
dmg = ((astr + (aagi / 2)) + weapRNG) - ddef
tagi = (aagi - (dagi / 2)) * 2
End If``````

The damage just doesn't always seem all that random sometimes. Maybe with better implementation and extra attributes things could go smoother? (I would like to implement a "Miss" algorithm as well, currently it's 100% hit rate.)

Now the similar enemy damage algorithm:
*Note: Why divide the str and agi instead of just creating enemies with half the values? Because I just recently notice I was not incorporating agility into the damage calculation (Doh!) and quickly added it and have not had the time to balance the attributes.

``````Dim dmg As Integer = ((astr / 2) + (aagi / 2)) - ddef
Dim tagi As Integer = aagi * 5``````

I find it very hard to calibrate this puny thing in order to have balance damage for every monster that I have. I have some weak, some strong, some bosses, and I would like to be able to have 1 algorithm that balances the damage they all do accordingly. Mine just seems very sloppy and hard-cut.

Next, the crit calculator, which is the same for everyone save the damage that a critical hit does. Players get *3 damage while enemies get *2 damage:

the tagi variable is used to randomly generate a number between tagi and tagi +100. Using my very simple logic, here's an example: tagi = 5. Generate a number between 5-105. If the number is above 100, a crit occurs. Therefore, there is a 5% chance a crit will occur. Very sloppy indeed.

``````Dim random As New Random
Dim crit As Integer = random.Next(tagi, 100 + tagi)
If crit >= 100 Then
dmg = dmg * 3
msg = "It was a crit! "
End If``````

Id you have any suggestions regarding these algorithms, including adding additional variables (which I plan to anyways) please let loose, ideas spark other ideas, even if one thinks their idea is not the best.

Now, I have my "random encounter" algorithm as such:
Dif: Difficulty setting, 20 default.
prob: Starts at 0, increments by 5 every time a combat is not initiated, resets after a combat.
battle: random number generated between 1 and 50 (1-49, 50 is excluded limit)
Add them all up and if it's greater than or equal to 100, a battle occurs.

``````Dim random As New Random
Dim battle As Integer = random.Next(1, 50)
battle = battle + prob + dif
If battle >= 100 Then
Return True
Else
Return False
End If``````

It's the first thing I came up with and is very dirty, as are most of my other ones. This is the one I am least worried about however considering it gives me 6-8 steps before getting into an encounter. And yes, for now all terrains give same chances but that can easily be modified with an extra variable. Any advice on this?

That is all for the moment. Thank you for taking the time to read any of this and helping me out with my first game! Leave as many comments as desired so we may discuss at length!

## All 6 Replies

Here are some thoughts, in no particular order:

Next, the crit calculator, which is the same for everyone save the damage that a critical hit does. Players get *3 damage while enemies get *2 damage:

the tagi variable is used to randomly generate a number between tagi and tagi +100. Using my very simple logic, here's an example: tagi = 5. Generate a number between 5-105. If the number is above 100, a crit occurs. Therefore, there is a 5% chance a crit will occur. Very sloppy indeed.

``````Dim random As New Random
Dim crit As Integer = random.Next(tagi, 100 + tagi)
If crit >= 100 Then
dmg = dmg * 3
msg = "It was a crit! "
End If``````

This might have been just for the sake of your example there, but in case it wasn't: You don't want to `Dim random As New Random` every time; just do it once and then stash `random` somewhere the rest of your code can get at it.

Generating a number from 5-105 is a little confusing, and it's more complicated than it needs to be. How about just sticking with 1-100, and less than or equal to 5 is your 5%? One more step: because of the way `random.Next` works, get a number from 0-99, and less than 5 is your critical hit:

``````If random.Next(0, 100) < 5 Then
dmg *= 3
msg = "It was a crit! "
End If``````

Now, I have my "random encounter" algorithm as such:
Dif: Difficulty setting, 20 default.
prob: Starts at 0, increments by 5 every time a combat is not initiated, resets after a combat.
battle: random number generated between 1 and 50 (1-49, 50 is excluded limit)
Add them all up and if it's greater than or equal to 100, a battle occurs.

It's the first thing I came up with and is very dirty, as are most of my other ones. This is the one I am least worried about however considering it gives me 6-8 steps before getting into an encounter. And yes, for now all terrains give same chances but that can easily be modified with an extra variable. Any advice on this?

So. In less difficult areas there are at least a few guaranteed "safe" steps between battles to recover and do other stuff, there's a bit of randomness in there so the battles aren't completely predictable, and eventually you *will* have to fight something. You'll want to tune the difficulty and other numbers if it doesn't feel right, but I think the basic idea is fine.

Different difficulties for different terrain types would certainly add some variety to the game. Also, consider other variables per terrain or special locations. For example, you might have a higher chance of encountering a mummy in a tomb, but finding mummies would be much less likely in, say, an airport. Unless it's an airport tomb. :)

The damage just doesn't always seem all that random sometimes. Maybe with better implementation and extra attributes things could go smoother?

All of your damage randomness is in `weapRNG` --what's a typical damage range for a weapon? If it's small, you won't see much variation in damage. Larger ranges will seem more random, if that's what you want.

Just brainstorming here... maybe instead of using attributes to add a fixed amount to damage, they could add more range to the value. This would get you a wider range of damages, and could simulate/abstract other things as well. A few ways this might work:

• Extend the range by increasing the maximum. Example: 2-8 extended +2 becomes 2-10.
• Shift the range by increasing minimum and maximum by the same value. Example: 2-8 shifted +2 becomes 4-10.
• Modify the range by adding different values from the minimum and maximum. Example: 2-8 modified +1-3 becomes 3-11.
• Multiply the range values. Example: 2-9 times 2 becomes 4-18.

I would like to implement a "Miss" algorithm as well, currently it's 100% hit rate.

Random suggestion: Take a base "to hit" percentage, add some for player skill/attributes/whatever, subtract some for enemy defense/level/whatever, and there's your chance of hitting. Maybe allow a small chance to miss no matter what, that way buffed-out players won't always hit everything. Many RPG combat systems basically work this way, though it might be disguised by various funny-shaped dice.

Now the similar enemy damage algorithm:
*Note: Why divide the str and agi instead of just creating enemies with half the values? Because I just recently notice I was not incorporating agility into the damage calculation (Doh!) and quickly added it and have not had the time to balance the attributes.

(Toggle Plain Text)
Dim dmg As Integer = ((astr / 2) + (aagi / 2)) - ddef
Dim tagi As Integer = aagi * 5

I find it very hard to calibrate this puny thing in order to have balance damage for every monster that I have. I have some weak, some strong, some bosses, and I would like to be able to have 1 algorithm that balances the damage they all do accordingly. Mine just seems very sloppy and hard-cut.

A simple thing would be to boost the attributes of your "strong" and "boss" monsters, though that seems like a bit of a hack.

You could also use damage ranges like you are for the player, and maybe add in some modifications like I suggested above. Nastier monsters could have extended or shifted ranges, while evil boss monsters might be multiplied.

I'd suggest making the monster type (normal, strong, boss) separate from the attributes. That way you could have, for example, normal Nose Goblins, but for Boss Booger, you could use the same stats as the other goblins, but with a special multiplier, or whatever you end up doing for bosses.

Anyway, have fun with this, and remember: Playtest, playtest, playtest. That's how you'll know if you got it right.

Thank you greatly, I could use a lot of this for future development! I'd love any other ideas for any other game aspects you might have, such as stat building, talent points and abilities and etc.

Thanks again

Thank you greatly, I could use a lot of this for future development! I'd love any other ideas for any other game aspects you might have, such as stat building, talent points and abilities and etc.

Thanks again

Half the fun is making it your own, and I don't want to prejudice you toward my RPG system preferences... but if you have a specific idea you've started to develop, I'd be happy to comment on it.

Also, you might have a look a Jared Presler's PORTAL system; he's got some interesting ideas in there about how stats and abilities relate.

I haven't read through all the above posts, but here's my ideas:

1) Use structs for each player, and struct members (fields) for each attribute you need.

2) KISS. Don't go Rube Goldberg here! You don't need a damage attribute, because when a character is damaged, his strength or life force, is simply dropped a bit.

If you don't keep the game organization simple, you program will be much harder to extend & de-bug, and will run slower.

A good game has a good story to present - not a zillion variables and do-da's.

Half the fun is making it your own, and I don't want to prejudice you toward my RPG system preferences... but if you have a specific idea you've started to develop, I'd be happy to comment on it.

My basic idea was to make a simple RPG. For now, I'm building the functionality of all the aspects in the game, such as quests, how battles occur, how special events occur, etc. I am almost done this part, thanks to some of your help I will be able to improve some of my code and move on to my other desired implementations.

Currently it's a quest-by-area style of game where you clear monsters in 1 area while doing quests provided by an NPC and then one you reach a certain level you can move on to the next area. There isn't much of a compelling story yet and there might never be, because I'm not very artistic and creative, I just like to program! Sorry, I'm digressing...

I want to implement stat poitns when you level up, Diablo style, putting the points where you prefer (between health, str, agi, and eventually mana) and then gaining a talent point, Diablo/WoW style, putting your point into a tree of abilities that gives you skills or passive damage and such. I later want to add mana as it is not there yet as well as a mage class. My idea with adding the mage class will be that it is based around those abilities in the tree as well as mana, so I could implement all 3 at once.

I plan on simply adding extra maps and areas to the collection of places you can wander to, all ranging for certain levels. Basically a simple adventure RPG.

Shortly put, I'm building a very simple 2D diablo with virtually no story line hehe. I'm more focused around developping the code first than creating a compelling story, considering this is my first game and I'm only creating it for personal use. (Though that can always change in the future)

1) Use structs for each player, and struct members (fields) for each attribute you need.

Thank you for commenting, I know it's a long read as well haha. However, I'm not sure what a "struct" is for players.. Currently I don't have a database implemented for the system but once I get it set up it'll be loads of fun to work with. (I also use objects for things in my game such as the character and enemies.. Is that what you meant?)

2) KISS. Don't go Rube Goldberg here! You don't need a damage attribute, because when a character is damaged, his strength or life force, is simply dropped a bit.

If you don't keep the game organization simple, you program will be much harder to extend & de-bug, and will run slower.

A good game has a good story to present - not a zillion variables and do-da's.

I do agree with this, because it is getting a little bit crazy to add things to my game right now what with the thousand line code-behind pages I've been creating. I do plan on simplifying everything once I have my initial implementation done.

For now I'm sort of in the Alpha stages, getting everything set up to work with. Once that has been achieved, I'll be able to clean some things up and then move on to Beta and expand my maps and quests and etc.

Is there anything in particular you would suggest to simplify things? Because I like to keep as much as possible in 1 page to reduce page loads. But ideas are always welcome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.