Merge branch 'paul' of peisongxiao.com:~/cs246/a5 into paul
This commit is contained in:
@ -33,7 +33,7 @@ int character::get_gold() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float character::get_hitrate() const {
|
float character::get_hitrate() const {
|
||||||
return base_hitrate;
|
return base_hit_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool character::is_hostile() const {
|
bool character::is_hostile() const {
|
||||||
@ -61,7 +61,7 @@ void character::set_gold(const int ngold) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void character::set_hitrate(const float nhitrate) {
|
void character::set_hitrate(const float nhitrate) {
|
||||||
base_hitrate = nhitrate;
|
base_hit_rate = nhitrate;
|
||||||
}
|
}
|
||||||
|
|
||||||
void character::set_hostile(const bool is_hostile) {
|
void character::set_hostile(const bool is_hostile) {
|
||||||
@ -174,3 +174,9 @@ result character::move_or_attack(const direction dir,
|
|||||||
|
|
||||||
return this->attack(dir, chlist);
|
return this->attack(dir, chlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int calc_dmg(const int ATK, const int DEF) {
|
||||||
|
return ceil((100 / (100 + DEF)) * ATK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -9,28 +9,21 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "layer.h"
|
#include "layer.h"
|
||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
|
|
||||||
// #include "inventory.h" // Reserved for later
|
// #include "inventory.h" // Reserved for later
|
||||||
|
|
||||||
class character; // forward declaration
|
class character; // forward declaration
|
||||||
extern RNG rng;
|
extern RNG rng;
|
||||||
|
|
||||||
// Note: player should not be in the character list
|
// Note: player should not be in the character list
|
||||||
class character_list final: public layer {
|
typedef std::vector<character> character_list;
|
||||||
private:
|
|
||||||
std::vector<std::unique_ptr<character>> characters;
|
|
||||||
public:
|
|
||||||
character_list();
|
|
||||||
void print() const;
|
|
||||||
void print(display &display) const;
|
|
||||||
|
|
||||||
std::vector<std::unique_ptr<character>>::const_iterator begin() const;
|
|
||||||
std::vector<std::unique_ptr<character>>::const_iterator end() const;
|
|
||||||
};
|
|
||||||
|
|
||||||
class character {
|
class character {
|
||||||
public:
|
public:
|
||||||
@ -45,7 +38,7 @@ public:
|
|||||||
virtual result move(const direction dir,
|
virtual result move(const direction dir,
|
||||||
const position_list &available_positions);
|
const position_list &available_positions);
|
||||||
virtual result attack(const direction dir,
|
virtual result attack(const direction dir,
|
||||||
const character_list &chlist) = 0;
|
character_list &chlist) = 0;
|
||||||
virtual result move_or_attack(const direction dir,
|
virtual result move_or_attack(const direction dir,
|
||||||
const position_list &available_positions,
|
const position_list &available_positions,
|
||||||
const character_list &chlist);
|
const character_list &chlist);
|
||||||
@ -87,7 +80,7 @@ protected:
|
|||||||
int gold; // characters spawn with gold
|
int gold; // characters spawn with gold
|
||||||
// inventory inventory; // Reserved
|
// inventory inventory; // Reserved
|
||||||
|
|
||||||
float base_hitrate; // requires: between [0,1]
|
float base_hit_rate; // requires: between [0,1]
|
||||||
|
|
||||||
bool hostile;
|
bool hostile;
|
||||||
};
|
};
|
||||||
@ -96,4 +89,7 @@ protected:
|
|||||||
position_list remove_from_list(const position_list &sorted_positions,
|
position_list remove_from_list(const position_list &sorted_positions,
|
||||||
position_list excluded);
|
position_list excluded);
|
||||||
|
|
||||||
|
|
||||||
|
int calc_dmg(const int ATK, const int DEF);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,12 +4,12 @@
|
|||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
|
||||||
// IMPORTANT: panic is reserved for invalid results
|
const int INF = 0x3F3F3F3F;
|
||||||
|
|
||||||
enum error {none};
|
enum error {none};
|
||||||
|
|
||||||
// TODO: update result to include subject
|
// TODO: update result to include subject
|
||||||
enum result {fine, died, go_down, hit, moved};
|
enum result {fine, died, go_down, hit, moved, miss};
|
||||||
|
|
||||||
enum game_status {terminated, main_menu, in_game, options};
|
enum game_status {terminated, main_menu, in_game, options};
|
||||||
|
|
||||||
@ -34,16 +34,18 @@ enum stat_name {HP, ATK, DEF, hostile};
|
|||||||
const int LAYER_CNT = 4; // TODO: update as you go
|
const int LAYER_CNT = 4; // TODO: update as you go
|
||||||
enum layer_num {map = 0, objects, characters, shop};
|
enum layer_num {map = 0, objects, characters, shop};
|
||||||
|
|
||||||
const int RACE_CNT = 2; // TODO: update as you go
|
const int RACE_CNT = 4; // TODO: update as you go
|
||||||
|
|
||||||
enum race {unknown = 0, rshade /* TODO: fill out the other races (including enemies) */};
|
enum race {unknown = 0, rshade, rvampire, goblin /* TODO: fill out the other races (including enemies) */};
|
||||||
|
|
||||||
// TODO: fill out the other races (including enemies)
|
// TODO: fill out the other races (including enemies)
|
||||||
const int MAX_HP[RACE_CNT] = {0, 125};
|
const int MAX_HP[RACE_CNT] = {0, 125, INF, 110};
|
||||||
const int STARTING_HP[RACE_CNT] = {0, 125};
|
const int STARTING_HP[RACE_CNT] = {0, 125, 50, 110};
|
||||||
const int STARTING_ATK[RACE_CNT] = {0, 25};
|
const int STARTING_ATK[RACE_CNT] = {0, 25, 25, 15};
|
||||||
const int STARTING_DEF[RACE_CNT] = {0, 25};
|
const int STARTING_DEF[RACE_CNT] = {0, 25, 25, 20};
|
||||||
const char CHARACTER_REP[RACE_CNT] = {'@', 'S'};
|
const char CHARACTER_REP[RACE_CNT] = {'@', 'S', 'V', 'G'};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const int DIRECTION_CNT = 8;
|
const int DIRECTION_CNT = 8;
|
||||||
// IMPORTANT: east is positive for x and SOUTH is positive for y
|
// IMPORTANT: east is positive for x and SOUTH is positive for y
|
||||||
|
30
src/goblin.cc
Normal file
30
src/goblin.cc
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include "goblin.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
|
result goblin::attack(const direction dir, const character_list &chlist) {
|
||||||
|
position tmp{pos + MOVE[dir]};
|
||||||
|
|
||||||
|
for (auto &ch : chlist)
|
||||||
|
if (tmp == ch->get_position()) {
|
||||||
|
auto res = ch->get_hit(race, ATK, base_hit_rate);
|
||||||
|
if (res == result::died) {
|
||||||
|
gold += GAIN_GOLD;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result::fine;
|
||||||
|
}
|
||||||
|
|
||||||
|
result goblin::get_hit(const enum race &race, const int atk,
|
||||||
|
const float hitrate) {
|
||||||
|
if (rng.rand_num() <= hitrate * (float)RAND_MAX)
|
||||||
|
HP = std::max(HP - calc_dmg(atk, DEF), 0);
|
||||||
|
|
||||||
|
if (HP == 0)
|
||||||
|
return result::died;
|
||||||
|
|
||||||
|
return result::hit;
|
||||||
|
}
|
16
src/goblin.h
Normal file
16
src/goblin.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef __GOBLIN_H__
|
||||||
|
#define __GOBLIN_H__
|
||||||
|
#include "characters.h"
|
||||||
|
|
||||||
|
const int GAIN_GOLD = 5;
|
||||||
|
|
||||||
|
class goblin final: public character {
|
||||||
|
public:
|
||||||
|
goblin(const position_list &available_positions); // spawn at a random place
|
||||||
|
virtual result attack(const direction dir,
|
||||||
|
const character_list &chlist) override;
|
||||||
|
virtual result get_hit(const enum race &race, const int atk,
|
||||||
|
const float hit_rate) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
10
src/inventory.h
Normal file
10
src/inventory.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef __INVENTORY_H__
|
||||||
|
#define __INVENTORY_H__
|
||||||
|
|
||||||
|
class inventory final {
|
||||||
|
private:
|
||||||
|
object_list objects;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -18,6 +18,11 @@ public:
|
|||||||
void print(display &display) {}
|
void print(display &display) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class object_list final: public layer {
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: throw potion into another header
|
// TODO: throw potion into another header
|
||||||
|
|
||||||
class potion final: public object {
|
class potion final: public object {
|
||||||
|
17
src/races.h
17
src/races.h
@ -6,20 +6,9 @@
|
|||||||
|
|
||||||
#ifndef __RACES_H__
|
#ifndef __RACES_H__
|
||||||
#define __RACES_H__
|
#define __RACES_H__
|
||||||
#include "characters.h"
|
|
||||||
|
|
||||||
// IMPORTANT: assumes all available positions have excluded chlist
|
#include "shade.h"
|
||||||
|
#include "goblin.h"
|
||||||
class shade final: public character {
|
#include "vampire.h"
|
||||||
public:
|
|
||||||
shade(const position_list &available_positions); // spawn at a random place
|
|
||||||
virtual result attack(const direction dir,
|
|
||||||
const character_list &chlist) override;
|
|
||||||
virtual result get_hit(const enum race &race, const int atk,
|
|
||||||
const float hitrate) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: fill out the other races
|
|
||||||
// TODO: implement enemie movement
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
1
src/room.cc
Normal file
1
src/room.cc
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "room.h"
|
6
src/room.h
Normal file
6
src/room.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef __ROOM_H__
|
||||||
|
#define __ROOM_H__
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -1,4 +1,4 @@
|
|||||||
#include "races.h"
|
#include "shade.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@ -10,21 +10,17 @@ shade::shade(const position_list &available_positions):
|
|||||||
hostile = true;
|
hostile = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
result shade::attack(const direction dir, const character_list &chlist) {
|
result shade::attack(const direction dir, character_list &chlist) {
|
||||||
position tmp{pos + MOVE[dir]};
|
position tmp{pos + MOVE[dir]};
|
||||||
|
|
||||||
for (auto &ch : chlist)
|
for (auto &ch : chlist)
|
||||||
if (tmp == ch->get_position()) {
|
if (tmp == ch.get_position()) {
|
||||||
return ch->get_hit(race, ATK, base_hitrate);
|
return ch.get_hit(race, ATK, base_hit_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result::fine;
|
return result::fine;
|
||||||
}
|
}
|
||||||
|
|
||||||
int calc_dmg(const int ATK, const int DEF) {
|
|
||||||
return ceil((100 / (100 + DEF)) * ATK);
|
|
||||||
}
|
|
||||||
|
|
||||||
result shade::get_hit(const enum race &race, const int atk,
|
result shade::get_hit(const enum race &race, const int atk,
|
||||||
const float hitrate) {
|
const float hitrate) {
|
||||||
if (rng.rand_num() <= hitrate * (float)RAND_MAX) // This is a hit!
|
if (rng.rand_num() <= hitrate * (float)RAND_MAX) // This is a hit!
|
||||||
@ -35,3 +31,4 @@ result shade::get_hit(const enum race &race, const int atk,
|
|||||||
|
|
||||||
return result::hit;
|
return result::hit;
|
||||||
}
|
}
|
||||||
|
|
15
src/shade.h
Normal file
15
src/shade.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef __SHADE_H__
|
||||||
|
#define __SHADE_H__
|
||||||
|
|
||||||
|
#include "characters.h"
|
||||||
|
|
||||||
|
class shade final: public character {
|
||||||
|
public:
|
||||||
|
shade(const position_list &available_positions); // spawn at a random place
|
||||||
|
virtual result attack(const direction dir,
|
||||||
|
character_list &chlist) override;
|
||||||
|
virtual result get_hit(const enum race &race, const int atk,
|
||||||
|
const float hit_rate) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
30
src/vampire.cc
Normal file
30
src/vampire.cc
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include "vampire.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
|
result vampire::attack(const direction dir, const character_list &chlist) {
|
||||||
|
position tmp{pos + MOVE[dir]};
|
||||||
|
|
||||||
|
for (auto &ch : chlist)
|
||||||
|
if (tmp == ch->get_position()) {
|
||||||
|
auto res = ch->get_hit(race, ATK, base_hit_rate);
|
||||||
|
if (res != result::miss) {
|
||||||
|
HP += GAIN_HP;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result::fine;
|
||||||
|
}
|
||||||
|
|
||||||
|
result vampire::get_hit(const enum race &race, const int atk,
|
||||||
|
const float hitrate) {
|
||||||
|
if (rng.rand_num() <= hitrate * (float)RAND_MAX)
|
||||||
|
HP = std::max(HP - calc_dmg(atk, DEF), 0);
|
||||||
|
|
||||||
|
if (HP == 0)
|
||||||
|
return result::died;
|
||||||
|
|
||||||
|
return result::hit;
|
||||||
|
}
|
16
src/vampire.h
Normal file
16
src/vampire.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef __VAMPIRE_H__
|
||||||
|
#define __VAMPIRE_H__
|
||||||
|
#include "characters.h"
|
||||||
|
|
||||||
|
const int GAIN_HP = 5;
|
||||||
|
|
||||||
|
class vampire final: public character {
|
||||||
|
public:
|
||||||
|
vampire(const position_list &available_positions); // spawn at a random place
|
||||||
|
virtual result attack(const direction dir,
|
||||||
|
const character_list &chlist) override;
|
||||||
|
virtual result get_hit(const enum race &race, const int atk,
|
||||||
|
const float hit_rate) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
BIN
tools/astyle
Executable file
BIN
tools/astyle
Executable file
Binary file not shown.
Reference in New Issue
Block a user