I started writing a browser-based toy on JS (react / node).

Interested in algorithms for calculating game mechanics.

For example:

  1. There is a Persian, he has LVL: 1, Strength: 7, armor: 0, the standard of living is 35 and mirrored by the enemy, they enter into a duel. How to calculate the damage that one Persian will do differently, is there a special formula or how is it usually done in games?
  2. These same characters have an instinct (critical strike), say 20, the second has agility (uvarot from blows) - 15. How to calculate, depending on the pumping of the Persian, the probability of a critical strike or uvarot in battle?
  3. There are 2 characters, one is 1 lvl and he has 35 lives, the second is 10 lvl and he has 1500 lives, how to organize the restoration of the lives of the Persians, so that not too quickly and not too slowly, given the pumping skill regeneration, than he will more quickly restore. This is the question of which formulas or which algorithm to consider.

I am sure there are some proven formulas and algorithms for such calculations, you need a more or less balanced solution so that the battle is fair at any level and the pumping is uniform.

  • I do not know if there are standard formulas, but it seems to me that everything should depend on the properties of certain parameters, what information they contain in themselves. The same "dexterity" - is that what it is? Chance or fixed value? etc. For example, in HoMM, exceeding the attack over armor gives + 5% to damage for each item. And excess armor over attack 2.5% damage reduction, for each point. Then the skills of "Attack" and "Defense" are calculated - respectively. What standard formula apply here? - Dmytryk

1 answer 1

You can take DnD mechanics as a DnD . It is quite extensive and complex, but firstly time tested, and secondly it can be simplified.

We will consider the characteristics of the formulas
For example, we want to count the hero's attack ( For simplicity, there will be no division into a melee / ranged attack, such as a weapon. )

Melee attack: 1d20 + Mastery + POWER (BOW for elegant weapons)

1d20 means that you need to roll a d20 cube (a cube with 20 faces). In the case of code, we just need a function that will return an integer from 1 to 20

 function randomInteger(min, max) { let rand = min - 0.5 + Math.random() * (max - min + 1) rand = Math.round(rand); return rand; } 

Thus, we can calculate both the initial characteristics of the hero and the compound under the action

 function randomInteger(min, max) { return () => { let rand = min - 0.5 + Math.random() * (max - min + 1) rand = Math.round(rand); return rand; } } const randomStat = randomInteger(1, 20); class Person { constructor(random) { this.random = random; this.strength = this.random(); // сила this.agility = this.random(); // ловкость } stats() { return `Сила: ${this.strength}, ловкость: ${this.agility}` } attack() { // формула 1d20 + СИЛА return this.random() + this.strength; } } const a = new Person(randomStat); console.log(a.stats()); console.log(`Игрок нанес урон: ${a.attack()}`); console.log(`Игрок нанес урон: ${a.attack()}`); console.log(`Игрок нанес урон: ${a.attack()}`); console.log(`Игрок нанес урон: ${a.attack()}`); 

Accordingly, the chance of a spell can be calculated on the same principle. But here it is better to make bots smarter, and not just random. That is, the bot must process information about its condition, the status of the hero. And from this to make a decision, but this is another story