breakhack/src/stats.c

91 lines
2.1 KiB
C
Raw Normal View History

2018-02-16 18:11:26 +01:00
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-12-15 08:08:45 +01:00
#include <stdio.h>
2017-12-19 22:51:00 +01:00
#include <stdlib.h>
2017-12-15 08:08:45 +01:00
#include <stdbool.h>
2018-02-21 00:29:21 +01:00
#include <SDL_ttf.h>
2017-12-15 15:03:29 +01:00
2018-02-20 14:22:26 +01:00
#include "gui.h"
2017-12-15 08:08:45 +01:00
#include "stats.h"
2017-12-17 13:43:41 +01:00
#include "random.h"
#include "util.h"
#include "defines.h"
2017-12-15 08:08:45 +01:00
static int
get_attack_roll(Stats *attacker)
2017-12-15 08:08:45 +01:00
{
int roll;
int firstRoll = get_random(19) + 1;
int secondRoll = get_random(19) + 1;
2017-12-15 08:08:45 +01:00
if (attacker->advantage)
roll = max(firstRoll, secondRoll);
else if (attacker->disadvantage)
roll = min(firstRoll, secondRoll);
else
roll = firstRoll;
return roll + attacker->atk;
}
static int
get_defence_roll(Stats *defender)
{
int roll;
int firstRoll = get_random(19) + 1;
int secondRoll = get_random(19) + 1;
if (defender->advantage)
roll = max(firstRoll, secondRoll);
else if (defender->disadvantage)
roll = min(firstRoll, secondRoll);
else
roll = firstRoll;
return roll + defender->def;
}
2019-05-14 10:26:28 +02:00
CombatResult
stats_fight(Stats *attacker, Stats *defender)
{
2019-05-14 10:26:28 +02:00
CombatResult result = { 0, false };
int atkRoll = get_attack_roll(attacker);
int defRoll = get_defence_roll(defender);
2017-12-15 08:08:45 +01:00
if (atkRoll - attacker->atk == 20)
2019-05-14 10:26:28 +02:00
result.critical = get_attack_roll(attacker) > defRoll;
2018-02-20 14:22:26 +01:00
if (atkRoll >= defRoll) {
if (attacker->dmg > 0)
2019-05-14 10:26:28 +02:00
result.dmg = get_random(attacker->dmg - 1) + 1;
else
2019-05-14 10:26:28 +02:00
result.dmg = 1;
2018-02-20 14:22:26 +01:00
2019-05-14 10:26:28 +02:00
if (result.critical) {
result.dmg = result.dmg * 2;
2018-02-28 09:32:37 +01:00
gui_log("CRITICAL HIT!");
2018-02-20 14:22:26 +01:00
}
2019-05-14 10:26:28 +02:00
defender->hp -= result.dmg;
2017-12-15 08:08:45 +01:00
}
2019-05-14 10:26:28 +02:00
return result;
2017-12-15 08:08:45 +01:00
}