Peoplemon  0.1.0
Peoplemon 3 game source documentation
Battler.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Util/Random.hpp>
6 
7 namespace core
8 {
9 namespace battle
10 {
12 : currentPeoplemon(0) {}
13 
14 void Battler::init(std::vector<core::pplmn::BattlePeoplemon>&& t,
15  std::unique_ptr<BattlerController>&& c) {
16  team = t;
17  controller.swap(c);
18  controller->setOwner(*this);
19 }
20 
22 
24 
26  if (substate.chargingMove >= 0) return true;
27  if (substate.encoreTurnsLeft > 0) return true;
28  return controller->actionSelected();
29 }
30 
32  if (substate.chargingMove >= 0) return;
33  if (substate.encoreTurnsLeft > 0) return;
34  controller->pickAction();
35 }
36 
37 void Battler::pickPeoplemon(bool ff, bool ro) { controller->pickPeoplemon(ff, ro); }
38 
40  if (substate.chargingMove >= 0) { return TurnAction::Fight; }
41  if (substate.encoreTurnsLeft > 0) { return TurnAction::Fight; }
42  if (substate.gotBaked) { return TurnAction::Fight; }
43  return controller->chosenAction();
44 }
45 
46 int Battler::chosenMove() const {
47  if (substate.chargingMove >= 0) return substate.chargingMove;
48  if (substate.encoreMove >= 0) return substate.encoreMove;
49  if (substate.gotBaked) { return -1; }
50  return controller->chosenMove();
51 }
52 
53 core::item::Id Battler::chosenItem() const { return controller->chosenItem(); }
54 
55 std::uint8_t Battler::chosenPeoplemon() const { return controller->chosenPeoplemon(); }
56 
57 std::vector<core::pplmn::BattlePeoplemon>& Battler::peoplemon() { return team; }
58 
59 core::pplmn::BattlePeoplemon& Battler::activePeoplemon() { return team[currentPeoplemon]; }
60 
61 void Battler::setActivePeoplemon(unsigned int i) {
62  currentPeoplemon = i;
63  substate.notifySwitch();
64 }
65 
66 unsigned int Battler::outNowIndex() const { return currentPeoplemon; }
67 
68 const std::string& Battler::name() const { return controller->name(); }
69 
70 unsigned int Battler::getPriority() const {
71  switch (chosenAction()) {
72  case TurnAction::Fight:
73  return pplmn::Move::priority(team[currentPeoplemon].base().knownMoves()[chosenMove()].id);
74  case TurnAction::Item:
75  case TurnAction::Run:
76  case TurnAction::Switch:
77  return 10000000;
78  default:
79  BL_LOG_ERROR << "Unknown turn action: " << chosenAction();
80  return 0;
81  }
82 }
83 
84 bool Battler::canFight() const {
85  for (const auto& ppl : team) {
86  if (ppl.base().currentHp() > 0) return true;
87  }
88  return false;
89 }
90 
91 bool Battler::canSwitch() const {
92  for (const auto& ppl : team) {
93  if (ppl.base().currentHp() > 0 && &ppl != &team[currentPeoplemon]) return true;
94  }
95  return false;
96 }
97 
98 void Battler::refresh() { controller->refresh(); }
99 
100 BattlerSubstate& Battler::getSubstate() { return substate; }
101 
102 const BattlerSubstate& Battler::getSubstate() const { return substate; }
103 
104 unsigned int Battler::selectRandomPeoplemon() const {
105  if (!canSwitch()) return currentPeoplemon;
106 
107  unsigned int r = 0;
108  do {
109  r = bl::util::Random::get<unsigned int>(0, team.size());
110  } while (team[r].base().currentHp() == 0 || r == currentPeoplemon);
111  return r;
112 }
113 
114 bool Battler::shouldContinue() const { return controller->shouldContinue(); }
115 
116 void Battler::startChooseToContinue() { controller->chooseToContinue(); }
117 
118 int Battler::prizeMoney() const {
119  const int base = substate.trainer ? substate.trainer->basePayout() : 40;
120  return base * team.back().base().currentLevel();
121 }
122 
124  int c = 0;
125  for (const auto& ppl : team) {
126  if (ppl.hasSeenBattle()) ++c;
127  if (ppl.base().hasExpShare()) ++c;
128  }
129  return c;
130 }
131 
133  for (auto& ppl : team) { ppl.resetSawBattle(); }
134 }
135 
137  int i = 0;
138  for (const auto& ppl : team) {
139  if (ppl.hasSeenBattle() && ppl.base().currentLevel() < 100) return i;
140  ++i;
141  }
142  return -1;
143 }
144 
146  const unsigned int bi = ci;
147  if (bi >= team.size() - 1) return -1;
148  for (unsigned int i = bi + 1; i < team.size(); ++i) {
149  if (team[i].hasSeenBattle() && team[i].base().currentLevel() < 100) return i;
150  }
151  return -1;
152 }
153 
154 int Battler::getBroCount() const {
155  int c = 0;
156  for (unsigned int i = 0; i < team.size(); ++i) {
157  if (team[i].currentAbility() == pplmn::SpecialAbility::TotalBro) { ++c; }
158  }
159  return c;
160 }
161 
162 bool Battler::removeItem(item::Id item) { return controller->removeItem(item); }
163 
164 bool Battler::isHost() const { return controller->isHost(); }
165 
166 } // namespace battle
167 } // namespace core
std::uint32_t base
Definition: WildIntro.cpp:26
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
Core classes and functionality for both the editor and game.
TurnAction
Represents an action that a battler may take on their turn.
Definition: TurnAction.hpp:16
void notifyTurnEnd()
Notifies the battler that a turn has ended.
Definition: Battler.cpp:23
void startChooseToContinue()
Begins to decide whether or not to continue.
Definition: Battler.cpp:116
void init(std::vector< core::pplmn::BattlePeoplemon > &&team, std::unique_ptr< BattlerController > &&controller)
Initializes the battler with a team and a controller.
Definition: Battler.cpp:14
void pickPeoplemon(bool fromFaint, bool reviveOnly)
Initiates the process of selecting a replacement peoplemon if the current one faints.
Definition: Battler.cpp:37
unsigned int outNowIndex() const
Returns the index of the peoplemon that is out now.
Definition: Battler.cpp:66
void refresh()
Calls refresh on the controller. Should be called every update.
Definition: Battler.cpp:98
bool actionSelected() const
Returns whether or not the battler has chosen what to do on this turn.
Definition: Battler.cpp:25
bool removeItem(item::Id item)
Removes the given item from the battler's inventory.
Definition: Battler.cpp:162
Battler()
Creates an uninitialized battler.
Definition: Battler.cpp:11
int getNextXpEarnerIndex(int ci)
Returns the index of the next peoplemon that should earn XP.
Definition: Battler.cpp:145
bool shouldContinue() const
Returns whether or not the player has chosen to continue.
Definition: Battler.cpp:114
BattlerSubstate & getSubstate()
Returns the state of this battler.
Definition: Battler.cpp:100
bool canFight() const
Returns true if the battler has at least one non-fainted peoplemon.
Definition: Battler.cpp:84
core::pplmn::BattlePeoplemon & activePeoplemon()
Returns the peoplemon that is currently out.
Definition: Battler.cpp:59
bool isHost() const
Returns whether or not this battler is the host.
Definition: Battler.cpp:164
TurnAction chosenAction() const
Returns the action the battler is using this turn.
Definition: Battler.cpp:39
int xpEarnerCount() const
Returns how many of the current peoplemon should earn exp.
Definition: Battler.cpp:123
std::uint8_t chosenPeoplemon() const
Returns the peoplemon the battler is switching to this turn.
Definition: Battler.cpp:55
bool canSwitch() const
Returns whether or not the battler has another peoplemon besides the active one.
Definition: Battler.cpp:91
unsigned int selectRandomPeoplemon() const
Returns the index of a random, living Peoplemon that is not currently out.
Definition: Battler.cpp:104
int prizeMoney() const
Returns the amount of prize money awarded for defeating this battler.
Definition: Battler.cpp:118
int chosenMove() const
Returns the move the battler is using this turn.
Definition: Battler.cpp:46
core::item::Id chosenItem() const
Returns the item the battler is using this turn.
Definition: Battler.cpp:53
int getFirstXpEarner() const
Returns the index of the first peoplemon that should earn XP.
Definition: Battler.cpp:136
void pickAction()
Initiates the process of selecting what to do on this turn.
Definition: Battler.cpp:31
unsigned int getPriority() const
Returns the priority of the battler's action. Fight returns the move priority and other choices retur...
Definition: Battler.cpp:70
const std::string & name() const
Returns the name of the battler.
Definition: Battler.cpp:68
void resetXpEarners()
Resets who earns XP.
Definition: Battler.cpp:132
std::vector< core::pplmn::BattlePeoplemon > & peoplemon()
Returns all the peoplemon held by this battler.
Definition: Battler.cpp:57
int getBroCount() const
Returns the number of total bros in the party, excluding the active peoplemon.
Definition: Battler.cpp:154
void notifyTurnBegin()
Notifies the battler that a turn has began.
Definition: Battler.cpp:21
void setActivePeoplemon(unsigned int index)
Sets the Peoplemon that is out now.
Definition: Battler.cpp:61
Struct containing state specific to each battler. These states facilitate logic that takes place over...
void notifyTurnBegin()
Resets and updates part of the state that depend on turns elapsing.
void notifyTurnEnd(TurnAction action, pplmn::BattlePeoplemon &outNow)
Resets and updates part of the state that depend on turns elapsing.
void notifySwitch()
Updates state that is unique to the currently out Peoplemon.
component::Trainer * trainer
std::uint8_t basePayout() const
Returns the base payout of the trainer.
Definition: Trainer.cpp:61
Represents a Peoplemon in battle. Stores some extra state that only exists in battle....
static int priority(MoveId move)
Returns the priority of the given move.
Definition: Move.cpp:96