merged DT/add-troll-char

This commit is contained in:
2024-07-14 10:52:47 -04:00
13 changed files with 113 additions and 62 deletions

View File

@ -2,7 +2,8 @@
## Class Diagram
<!----
<!---
```plantuml
@startuml
class User {
@ -23,4 +24,5 @@ class User {
```
-->
Please see UML generated by Mermaid Chart as official documentation.
Archived. Please see new UML diagram uploaded in chats. Thanks!

View File

@ -201,6 +201,6 @@ result character::move(const direction dir,
return result::fine;
}
int calc_dmg(const int ATK, const int DEF) {
int character::calc_dmg(const int ATK, const int DEF) {
return ceil((100.0f / (100.0f + DEF)) * ATK);
}

View File

@ -16,7 +16,7 @@
class character; // forward declaration
// Note: player should not be in the character list
typedef std::vector<character *> character_list;
class character {
public:
@ -32,78 +32,65 @@ public:
struct attack_result {
result res;
int dmg_dealt;
int remaining_HP;
character *which; // hit whom
std::string msg;
};
struct apply_result {
result res;
potion *which; // applied which potion
std::string msg;
};
virtual result move(position_list spots, const direction &dir);
struct hit_result {
result res;
int dmg_dealt;
int remaining_HP;
};
virtual attack_result attack(const direction dir,
character_list &chlist) = 0;
virtual attack_result attack(const direction dir, character *ch) = 0;
virtual apply_result apply(const direction &dir,
potion_list &potions);
virtual hit_result get_hit(const enum race &race, const int atk,
virtual attack_result get_hit(const enum race &race, const int atk,
const fraction hitrate) = 0;
// overload for different races
virtual void print(display *out, bool player = false);
virtual void print(display *out);
result apply_effects();
virtual result calc_effects();
void discard_level_effects();
void start_turn();
virtual void start_turn();
enum race get_race() const;
position get_position() const;
position get_pos() const;
void set_pos(const position &npos);
int get_HP() const;
int get_ATK() const;
int get_DEF() const;
int get_gold() const;
float get_hitrate_real() const;
fraction get_hitrate() const;
int get_room_num() const;
void set_position(const position &npos);
void set_HP(const int nHP);
void set_ATK(const int nATK);
void set_DEF(const int nDEF);
void set_gold(const int ngold);
void set_hitrate(const fraction nhitrate);
void set_room_num(const int room);
protected:
RNG *rng;
const feature enabled_features;
const enum race race;
int HP;
position pos;
// IMPORTANT: keep track of ATK and DEF in game at turn time
int ATK;
int DEF;
fraction base_hit_rate;
position pos;
int gold; // characters spawn with gold
potion_list potions; // inventory
potion_list effects; // applied potions
int room_num;
private:
void insert_potion(potion *p);
};
potion_list effects;
int calc_dmg(const int ATK, const int DEF);
private:
void insert_effect(potion *effect);
};
typedef std::vector<character *> character_list;
#endif

View File

@ -35,22 +35,33 @@ enum game_command {game_command_terminate = 0,
game_command_pass, game_command_panic
};
enum stat_name {HP, ATK, DEF, hostile};
const int RACE_CNT = 6; // TODO: update as you go
enum race {rshade = 0, rvampire, rgoblin, rdrow, rdragon, rmerchant /* TODO: fill out the other races (including enemies) */};
// TODO: fill out the other races (including enemies)
const int MAX_HP[RACE_CNT] = {125, INF, 110, 150, 150, 30};
const int STARTING_HP[RACE_CNT] = {125, 50, 110, 150, 150, 30};
const int STARTING_ATK[RACE_CNT] = {25, 25, 15, 25, 20, 70};
const int STARTING_DEF[RACE_CNT] = {25, 25, 20, 15, 20, 5};
const char CHARACTER_REP[RACE_CNT] = {'S', 'V', 'G', 'd', 'D', 'M'};
// Character generation related
const int RACE_CNT = 12;
enum race {rshade = 0, rdrow, rvampire, rtroll,
rgoblin, rhuman, rdwarf, relf,
rorc, rmerchant, rdragon, rhalfling
};
const char *RACE_NAME[RACE_CNT] = {
"Shade", "Vampire", "Goblin", "Drow", "Dragon"
"Shade", "Drow", "Vampire", "Troll",
"Goblin", "Human", "Dwarf", "Elf",
"Orc", "Merchant", "Dragon", "Halfling"
};
const int MAX_HP[RACE_CNT] = {
125, 150, INF, 120, 110, 140, 100, 140, 180, 30, 150, 100
};
const int STARTING_HP[RACE_CNT] = {
125, 150, 50, 120, 110, 140, 100, 140, 180, 30, 150, 100
};
const int STARTING_ATK[RACE_CNT] = {
25, 25, 25, 25, 15, 20, 20, 30, 30, 70, 20, 15
};
const int STARTING_DEF[RACE_CNT] = {
25, 15, 25, 15, 20, 20, 30, 10, 25, 5, 20, 20
};
// Potion-related
const int POTION_TYPE_CNT = 6;
const int DEFAULT_POTION_TYPE_CNT = 6;
enum potion_type {restore_health = 0, boost_atk, boost_def,
@ -67,6 +78,7 @@ const int CALC_ADD_LATER = 2;
const int CALC_MUL_LATER = 3;
const int CALC_ADD_FIXED = 4;
const int DIRECTION_CNT = 8;
// IMPORTANT: east is positive for x and SOUTH is positive for y
// initializes all directions to an int

View File

@ -1,9 +1,7 @@
#include "goblin.h"
#include <algorithm>
#include <math.h>
goblin::goblin(RNG *rng, const position &pos):
character{rng, race::rshade, pos} {
character{rng, race::rgoblin, pos} {
gold = 0;
hostile = true;
}

View File

@ -1,5 +1,6 @@
#ifndef __GOBLIN_H__
#define __GOBLIN_H__
#include "characters.h"
class goblin final: public character {

View File

@ -2,8 +2,9 @@
#define __RACES_H__
#include "shade.h"
#include "goblin.h"
#include "vampire.h"
#include "troll.h"
#include "goblin.h"
#include "dragon.h"
#endif

View File

@ -1,8 +1,5 @@
#include "shade.h"
#include <algorithm>
#include <math.h>
shade::shade(RNG *rng, const position &pos):
character{rng, race::rshade, pos} {
gold = 0;

36
src/troll.cc Normal file
View File

@ -0,0 +1,36 @@
#include "troll.h"
troll::troll(RNG *rng, const position &pos):
character{rng, race::rtroll, pos} {
gold = 0;
hostile = true;
}
result troll::attack(const direction dir, character_list &chlist) {
position tmp{pos + MOVE[dir]};
if (HP + REGAIN_HP > HP_CAP) {
HP = HP_CAP;
} else {
HP += REGAIN_HP;
}
for (auto &ch : chlist)
if (tmp == ch->get_position()) {
auto res = ch->get_hit(race, ATK, base_hit_rate);
return res;
}
return result::fine;
}
result troll::get_hit(const enum race &race, const int atk,
const fraction hitrate) {
if (rng->trial(hitrate))
HP = std::max(HP - calc_dmg(atk, DEF), 0);
if (HP == 0)
return result::died;
return result::hit;
}

18
src/troll.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __TROLL_H__
#define __TROLL_H__
#include "characters.h"
class troll final: public character {
static const int REGAIN_HP = 5;
static const int HP_CAP = 120;
public:
troll(RNG *rng, const position &pos);
virtual result attack(const direction dir,
character_list &chlist) override;
virtual result get_hit(const enum race &race, const int afk,
const fraction hit_rate) override;
};
#endif

View File

@ -1,9 +1,7 @@
#include "vampire.h"
#include <algorithm>
#include <math.h>
vampire::vampire(RNG *rng, const position &pos):
character{rng, race::rshade, pos} {
character{rng, race::rvampire, pos} {
gold = 0;
hostile = true;
}

View File

@ -1,5 +1,6 @@
#ifndef __VAMPIRE_H__
#define __VAMPIRE_H__
#include "characters.h"
class vampire final: public character {