]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
removed redundant BattleState::AttackChoiceAt(int)
[l2e.git] / src / battle / BattleState.cpp
1 /*
2  * BattleState.cpp
3  *
4  *  Created on: Aug 5, 2012
5  *      Author: holy
6  */
7
8 #include "BattleState.h"
9
10 #include "PartyLayout.h"
11 #include "states/SelectMoveAction.h"
12 #include "states/PerformAttacks.h"
13 #include "../app/Application.h"
14 #include "../app/Input.h"
15 #include "../common/Ikari.h"
16 #include "../common/Inventory.h"
17 #include "../common/Item.h"
18 #include "../common/Spell.h"
19 #include "../geometry/operators.h"
20 #include "../graphics/Frame.h"
21 #include "../graphics/Sprite.h"
22
23 #include <algorithm>
24 #include <cassert>
25 #include <stdexcept>
26
27 using app::Application;
28 using app::Input;
29 using common::Inventory;
30 using common::Item;
31 using common::Spell;
32 using geometry::Point;
33 using geometry::Vector;
34 using graphics::Menu;
35
36 using std::vector;
37
38 namespace battle {
39
40 void BattleState::AddMonster(const Monster &m) {
41         if (monsters.size() >= monstersLayout->NumPositions()) {
42                 throw std::overflow_error("too many monsters for layout");
43         }
44         monsters.push_back(m);
45 }
46
47 void BattleState::AddHero(const Hero &h) {
48         if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
49                 throw std::overflow_error("too many heroes for layout");
50         }
51         heroes[numHeroes] = h;
52         ++numHeroes;
53 }
54
55 void BattleState::NextHero() {
56         ++activeHero;
57         while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
58                 ++activeHero;
59         }
60 }
61
62 void BattleState::PreviousHero() {
63         --activeHero;
64         while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
65                 --activeHero;
66         }
67 }
68
69 void BattleState::SwapHeroes(int lhs, int rhs) {
70         if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
71         std::swap(heroes[lhs], heroes[rhs]);
72 }
73
74
75 void BattleState::Resize(int w, int h) {
76
77 }
78
79
80 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
81         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
82         heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
83         for (int i(0); i < 4; ++i) {
84                 heroes[i].SpellMenu() = res->spellMenuPrototype;
85                 heroes[i].UpdateSpellMenu();
86                 heroes[i].IkariMenu() = res->ikariMenuPrototype;
87                 heroes[i].UpdateIkariMenu(res);
88                 heroTags[i] = HeroTag(this, i);
89                 smallHeroTags[i] = SmallHeroTag(this, i);
90         }
91
92         int tagHeight(attackTypeMenu.Height());
93         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
94         int xOffset((Width() - 2 * tagWidth) / 2);
95         heroTagPositions[0] = Point<int>(xOffset, Height() - 2 * tagHeight);
96         heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
97         heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
98         heroTagPositions[3] = Point<int>(xOffset + tagWidth, Height() - tagHeight);
99
100         tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
101         tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
102         xOffset = (Width() - 4 * tagWidth) / 2;
103         int yOffset(Height() - tagHeight);
104         smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
105         smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
106         smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
107         smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
108
109         itemMenu = res->itemMenuPrototype;
110         LoadInventory();
111 }
112
113 void BattleState::LoadInventory() {
114         const Inventory &inv(*res->inventory);
115         itemMenu.Clear();
116         itemMenu.Reserve(inv.MaxItems());
117         for (int i(0); i < inv.MaxItems(); ++i) {
118                 const Item *item(inv.ItemAt(i));
119                 if (item) {
120                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
121                 } else {
122                         itemMenu.AddEmptyEntry();
123                 }
124         }
125         ClearAllAttacks();
126 }
127
128 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
129
130 }
131
132 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
133         if (ranAway) {
134                 ctrl.PopState(); // quit the battle scene
135                 return;
136         }
137         if (Victory()) {
138                 // TODO: push victory state
139                 ctrl.PopState();
140                 return;
141         }
142         if (Defeat()) {
143                 // TODO: push defeat state
144                 ctrl.PopState();
145                 return;
146         }
147         // TODO: this should not push a state while quitting
148         if (AttackSelectionDone()) {
149                 ctrl.PushState(new PerformAttacks(this));
150         } else {
151                 ctrl.PushState(new SelectMoveAction(this));
152         }
153 }
154
155 bool BattleState::Victory() const {
156         for (int i(0); i < MaxMonsters(); ++i) {
157                 if (MonsterAt(i).Health() > 0) return false;
158         }
159         return true;
160 }
161
162 bool BattleState::Defeat() const {
163         for (int i(0); i < NumHeroes(); ++i) {
164                 if (HeroAt(i).Health() > 0) return false;
165         }
166         return true;
167 }
168
169 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
170
171 }
172
173
174 class OrderCompare {
175         public:
176                 OrderCompare(BattleState *battle) : battle(battle) { }
177                 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
178                         int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
179                         int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
180                         return lagl > ragl;
181                 }
182         private:
183                 BattleState *battle;
184 };
185
186 void BattleState::CalculateAttackOrder() {
187         attackOrder.reserve(monsters.size() + NumHeroes());
188         for (int i(0); i < NumHeroes(); ++i) {
189                 attackOrder.push_back(Order(i, false));
190         }
191         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
192                 attackOrder.push_back(Order(i, true));
193         }
194         std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
195
196         monsterAttacks.resize(monsters.size(), AttackChoice(this));
197 }
198
199 void BattleState::NextAttack() {
200         if (Victory() || Defeat()) {
201                 attackCursor = attackOrder.size();
202                 return;
203         }
204         ++attackCursor;
205         while (attackCursor < int(attackOrder.size())) {
206                 if (attackOrder[attackCursor].isMonster) {
207                         if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
208                 } else {
209                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
210                 }
211                 ++attackCursor;
212         }
213 }
214
215 bool BattleState::AttacksFinished() const {
216         return attackCursor >= int(attackOrder.size())
217                         || Victory() || Defeat();
218 }
219
220 void BattleState::CalculateDamage() {
221         AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : HeroAt(CurrentAttack().index).GetAttackChoice());
222         if (ac.GetType() == AttackChoice::DEFEND) return;
223
224         if (CurrentAttack().isMonster) {
225                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
226                 // TODO: run monster's attack script
227                 ac.SetType(AttackChoice::SWORD);
228                 ac.Selection().SelectSingle();
229                 ac.Selection().SelectHeroes();
230                 for (int i(0); i < NumHeroes(); ++i) {
231                         if (HeroAt(i).Health() > 0) {
232                                 const Stats &defenderStats(HeroAt(i).GetStats());
233                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
234                                 ac.Selection().SetBad(0, damage);
235                                 break;
236                         }
237                 }
238         } else {
239                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
240                 TargetSelection &ts(ac.Selection());
241                 bool hitSome(false);
242                 if (ts.TargetsEnemies()) {
243                         for (int i(0); i < MaxMonsters(); ++i) {
244                                 if (ts.IsSelected(i)) {
245                                         if (MonsterAt(i).Health() > 0) {
246                                                 const Stats &defenderStats(MonsterAt(i).GetStats());
247                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
248                                                 ts.SetBad(i, damage);
249                                                 hitSome = true;
250                                         } else {
251                                                 ts.Unselect(i);
252                                         }
253                                 }
254                         }
255                         if (hitSome) return;
256                         for (int i(0); i < MaxMonsters(); ++i) {
257                                 if (MonsterAt(i).Health() > 0) {
258                                         const Stats &defenderStats(MonsterAt(i).GetStats());
259                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
260                                         ts.SetBad(i, damage);
261                                         break;
262                                 }
263                         }
264                 } else {
265                         for (int i(0); i < NumHeroes(); ++i) {
266                                 if (ts.IsSelected(i)) {
267                                         if (HeroAt(i).Health() > 0) {
268                                                 const Stats &defenderStats(HeroAt(i).GetStats());
269                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
270                                                 ts.SetBad(i, damage);
271                                                 hitSome = true;
272                                         } else {
273                                                 ts.Unselect(i);
274                                         }
275                                 }
276                         }
277                         if (hitSome) return;
278                         for (int i(0); i < NumHeroes(); ++i) {
279                                 if (HeroAt(i).Health() > 0) {
280                                         const Stats &defenderStats(HeroAt(i).GetStats());
281                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
282                                         ts.SetBad(i, damage);
283                                         break;
284                                 }
285                         }
286                 }
287         }
288 }
289
290 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
291         // TODO: find out real formula and add some randomness
292         return attacker.Attack() / 2 - defender.Defense() / 4;
293 }
294
295 void BattleState::ApplyDamage() {
296         if (attackCursor < 0) return;
297         AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : HeroAt(CurrentAttack().index).GetAttackChoice());
298         TargetSelection &ts(ac.Selection());
299         if (ts.TargetsEnemies()) {
300                 for (int i(0); i < MaxMonsters(); ++i) {
301                         Monster &monster(MonsterAt(i));
302                         if (ts.IsBad(i)) {
303                                 monster.SubtractHealth(ts.GetAmount(i));
304                                 if (monster.Health() == 0) {
305                                         expReward += monster.ExpReward();
306                                         goldReward += monster.GoldReward();
307                                 }
308                         }
309                 }
310         } else {
311                 for (int i(0); i < NumHeroes(); ++i) {
312                         Hero &hero(HeroAt(i));
313                         if (ts.IsBad(i)) {
314                                 hero.SubtractHealth(ts.GetAmount(i));
315                         }
316                 }
317         }
318 }
319
320 AttackChoice &BattleState::CurrentAttackAttackChoice() {
321         if (CurrentAttack().isMonster) {
322                 return monsterAttacks[CurrentAttack().index];
323         } else {
324                 return HeroAt(CurrentAttack().index).GetAttackChoice();
325         }
326 }
327
328 void BattleState::ClearAllAttacks() {
329         attackCursor = -1;
330         activeHero = -1;
331         for (int i(0); i < numHeroes; ++i) {
332                 heroes[i].GetAttackChoice() = AttackChoice(this);
333         }
334         attackOrder.clear();
335         monsterAttacks.clear();
336 }
337
338
339 void BattleState::HandleEvents(const Input &input) {
340
341 }
342
343 void BattleState::UpdateWorld(float deltaT) {
344
345 }
346
347 void BattleState::Render(SDL_Surface *screen) {
348         assert(screen);
349         Vector<int> offset(CalculateScreenOffset(screen));
350         RenderBackground(screen, offset);
351         RenderMonsters(screen, offset);
352 }
353
354 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
355         assert(screen);
356         // black for now
357         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
358         SDL_Rect destRect;
359         destRect.x = offset.X();
360         destRect.y = offset.Y();
361         destRect.w = background->w;
362         destRect.h = background->h;
363         SDL_BlitSurface(background, 0, screen, &destRect);
364 }
365
366 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
367         assert(screen);
368         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
369                 if (MonsterPositionOccupied(i)) {
370                         if (monsters[i].GetAnimation().Running()) {
371                                 monsters[i].GetAnimation().DrawCenter(screen, monsterPositions[i] + offset);
372                         } else {
373                                 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
374                         }
375                 }
376         }
377 }
378
379 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
380         assert(screen);
381         for (int i(0); i < numHeroes; ++i) {
382                 if (heroes[i].GetAnimation().Running()) {
383                         heroes[i].GetAnimation().DrawCenter(screen, heroesPositions[i] + offset);
384                 } else {
385                         int row(heroes[i].Health() > 0 ? 0 : 2);
386                         heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
387                 }
388         }
389 }
390
391 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
392         assert(screen);
393         int tagHeight(attackTypeMenu.Height());
394         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
395
396         for (int i(0); i < numHeroes; ++i) {
397                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
398         }
399 }
400
401 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
402         assert(screen);
403         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
404         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
405
406         SDL_Rect rect;
407         rect.x = offset.X();
408         rect.y = offset.Y() + Height() - tagHeight;
409         rect.w = Width();
410         rect.h = tagHeight;
411         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
412         rect.y += res->normalFont->CharHeight() / 8;
413         rect.h -= res->normalFont->CharHeight() / 4;
414         SDL_FillRect(screen, &rect, res->heroesBgColor);
415
416         for (int i(0); i < numHeroes; ++i) {
417                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
418         }
419 }
420
421 }