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