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"
|
2018-01-23 22:54:02 +01:00
|
|
|
#include "util.h"
|
2018-08-04 23:52:52 +02:00
|
|
|
#include "defines.h"
|
2017-12-15 08:08:45 +01:00
|
|
|
|
2018-08-05 11:00:43 +02:00
|
|
|
static int
|
|
|
|
get_attack_roll(Stats *attacker)
|
2017-12-15 08:08:45 +01:00
|
|
|
{
|
2018-08-05 11:00:43 +02:00
|
|
|
int roll;
|
|
|
|
int firstRoll = get_random(19) + 1;
|
|
|
|
int secondRoll = get_random(19) + 1;
|
2017-12-15 08:08:45 +01:00
|
|
|
|
2018-08-04 23:52:52 +02:00
|
|
|
if (attacker->advantage)
|
2018-08-05 11:00:43 +02:00
|
|
|
roll = max(firstRoll, secondRoll);
|
2018-08-04 23:52:52 +02:00
|
|
|
else if (attacker->disadvantage)
|
2018-08-05 11:00:43 +02:00
|
|
|
roll = min(firstRoll, secondRoll);
|
2018-08-04 23:52:52 +02:00
|
|
|
else
|
2018-08-05 11:00:43 +02:00
|
|
|
roll = firstRoll;
|
2018-08-04 23:52:52 +02:00
|
|
|
|
2018-08-05 11:00:43 +02:00
|
|
|
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;
|
2018-08-04 23:52:52 +02:00
|
|
|
|
|
|
|
if (defender->advantage)
|
2018-08-05 11:00:43 +02:00
|
|
|
roll = max(firstRoll, secondRoll);
|
2018-08-04 23:52:52 +02:00
|
|
|
else if (defender->disadvantage)
|
2018-08-05 11:00:43 +02:00
|
|
|
roll = min(firstRoll, secondRoll);
|
2018-08-04 23:52:52 +02:00
|
|
|
else
|
2018-08-05 11:00:43 +02:00
|
|
|
roll = firstRoll;
|
|
|
|
|
|
|
|
return roll + defender->def;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
stats_fight(Stats *attacker, Stats *defender)
|
|
|
|
{
|
|
|
|
bool critical = false;
|
|
|
|
|
|
|
|
int atkRoll = get_attack_roll(attacker);
|
|
|
|
int defRoll = get_defence_roll(defender);
|
2017-12-15 08:08:45 +01:00
|
|
|
|
2018-08-22 13:13:54 +02:00
|
|
|
if (atkRoll - attacker->atk == 20)
|
|
|
|
critical = get_attack_roll(attacker) > defRoll;
|
|
|
|
|
2018-08-05 11:00:43 +02:00
|
|
|
int dmgRoll = 0;
|
2018-02-20 14:22:26 +01:00
|
|
|
if (atkRoll >= defRoll) {
|
|
|
|
if (attacker->dmg > 0)
|
|
|
|
dmgRoll = get_random(attacker->dmg - 1) + 1;
|
2018-08-22 20:54:39 +02:00
|
|
|
else
|
|
|
|
dmgRoll = 1;
|
2018-02-20 14:22:26 +01:00
|
|
|
|
|
|
|
if (critical) {
|
2018-02-15 14:45:20 +01:00
|
|
|
dmgRoll = dmgRoll * 2;
|
2018-02-28 09:32:37 +01:00
|
|
|
gui_log("CRITICAL HIT!");
|
2018-02-20 14:22:26 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:45:20 +01:00
|
|
|
defender->hp -= dmgRoll;
|
2017-12-15 08:08:45 +01:00
|
|
|
}
|
2017-12-18 09:59:01 +01:00
|
|
|
|
2017-12-15 15:03:29 +01:00
|
|
|
return dmgRoll;
|
2017-12-15 08:08:45 +01:00
|
|
|
}
|