]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
0171e1480f56a7d52b905a86e577525d0a2597a0
[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::SetCapsule(const Capsule &c) {
51         capsule = c;
52 }
53
54 void BattleState::NextHero() {
55         ++activeHero;
56         while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
57                 ++activeHero;
58         }
59 }
60
61 void BattleState::PreviousHero() {
62         --activeHero;
63         while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
64                 --activeHero;
65         }
66 }
67
68 void BattleState::SwapHeroes(int lhs, int rhs) {
69         if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
70         std::swap(heroes[lhs], heroes[rhs]);
71 }
72
73
74 void BattleState::OnResize(int w, int h) {
75
76 }
77
78
79 void BattleState::OnEnterState(SDL_Surface *screen) {
80         for (int i(0); i < 4; ++i) {
81                 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
82                 heroes[i].SpellMenu() = *res->spellMenuProperties;
83                 heroes[i].UpdateSpellMenu();
84                 heroes[i].IkariMenu() = *res->ikariMenuProperties;
85                 heroes[i].UpdateIkariMenu(res);
86                 heroTags[i] = HeroTag(this, i);
87                 smallHeroTags[i] = SmallHeroTag(this, i);
88         }
89
90         capsule.Position() = heroesLayout->CalculatePosition(4, background->w, background->h);
91
92         for (int i(0); i < int(monsters.size()); ++i) {
93                 monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
94         }
95
96         int tagHeight(attackTypeMenu.Height());
97         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
98         int xOffset((Width() - 2 * tagWidth) / 2);
99         heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
100         heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
101         heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
102         heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
103
104         tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
105         tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
106         xOffset = (Width() - 4 * tagWidth) / 2;
107         int yOffset(Height() - tagHeight);
108         smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
109         smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
110         smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
111         smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
112
113         itemMenu = *res->itemMenuProperties;
114         LoadInventory();
115         ClearAllAttacks();
116 }
117
118 void BattleState::LoadInventory() {
119         const Inventory &inv(game->state->inventory);
120         itemMenu.Clear();
121         itemMenu.Reserve(inv.MaxItems());
122         for (int i(0); i < inv.MaxItems(); ++i) {
123                 const Item *item(inv.ItemAt(i));
124                 if (item) {
125                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
126                 } else {
127                         itemMenu.AddEmptyEntry();
128                 }
129         }
130 }
131
132 void BattleState::OnExitState(SDL_Surface *screen) {
133
134 }
135
136 void BattleState::OnResumeState(SDL_Surface *screen) {
137         if (ranAway) {
138                 Ctrl().PopState(); // quit the battle scene
139                 return;
140         }
141         if (Victory()) {
142                 Ctrl().PopState();
143                 return;
144         }
145         if (Defeat()) {
146                 Ctrl().PopState();
147                 return;
148         }
149         // TODO: this should not push a state while quitting
150         if (AttackSelectionDone()) {
151                 Ctrl().PushState(new PerformAttacks(this));
152         } else {
153                 Ctrl().PushState(new SelectMoveAction(this));
154         }
155 }
156
157 bool BattleState::Victory() const {
158         for (int i(0); i < MaxMonsters(); ++i) {
159                 if (MonsterAt(i).Health() > 0) return false;
160         }
161         return true;
162 }
163
164 bool BattleState::Defeat() const {
165         for (int i(0); i < NumHeroes(); ++i) {
166                 if (HeroAt(i).Health() > 0) return false;
167         }
168         return true;
169 }
170
171 void BattleState::OnPauseState(SDL_Surface *screen) {
172
173 }
174
175
176 class OrderCompare {
177         public:
178                 OrderCompare(BattleState *battle) : battle(battle) { }
179                 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
180                         return lhs.GetStats(*battle).Agility() > rhs.GetStats(*battle).Agility();
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(Order::HERO, i));
190         }
191         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
192                 attackOrder.push_back(Order(Order::MONSTER, i));
193                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
194         }
195         if (capsule.Health() > 0) {
196                 attackOrder.push_back(Order(Order::CAPSULE));
197         }
198         std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
199 }
200
201 void BattleState::NextAttack() {
202         if (Victory() || Defeat()) {
203                 attackCursor = attackOrder.size();
204                 return;
205         }
206         ++attackCursor;
207         while (attackCursor < int(attackOrder.size())) {
208                 if (attackOrder[attackCursor].IsMonster()) {
209                         if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
210                 } else if (attackOrder[attackCursor].IsHero()) {
211                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
212                 } else {
213                         if (capsule.Health() > 0) break;
214                 }
215                 ++attackCursor;
216         }
217 }
218
219 bool BattleState::AttacksFinished() const {
220         return attackCursor >= int(attackOrder.size())
221                         || Victory() || Defeat();
222 }
223
224 void BattleState::CalculateDamage() {
225         if (CurrentAttack().IsMonster()) {
226                 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
227         } else if (CurrentAttack().IsCapsule()) {
228                 DecideCapsuleAttack();
229         }
230         AttackChoice &ac = CurrentAttack().GetAttackChoice(*this);
231         if (ac.GetType() == AttackChoice::DEFEND) return;
232         TargetSelection &ts(ac.Selection());
233
234         const Stats &attackerStats = CurrentAttack().GetStats(*this);
235         CalculateDamage(attackerStats, ts);
236 }
237
238 void BattleState::DecideMonsterAttack(Monster &m) {
239         AttackChoice &ac(m.GetAttackChoice());
240         TargetSelection &ts(ac.Selection());
241         ac.Reset();
242         int target(rand() % NumHeroes());
243         while (!HeroPositionOccupied(target)) {
244                 target = rand() % NumHeroes();
245         }
246         ac.SetType(AttackChoice::SWORD);
247         ts.SelectHeroes();
248         ts.SetSingle();
249         ts.Select(target);
250 }
251
252 void BattleState::DecideCapsuleAttack() {
253         AttackChoice &ac(capsule.GetAttackChoice());
254         TargetSelection &ts(ac.Selection());
255         ac.Reset();
256         int target(rand() % monsters.size());
257         while (!MonsterPositionOccupied(target)) {
258                 target = rand() % monsters.size();
259         }
260         ac.SetType(AttackChoice::SWORD);
261         ts.SelectMonsters();
262         ts.SetSingle();
263         ts.Select(target);
264 }
265
266 AttackChoice &BattleState::Order::GetAttackChoice(BattleState &b) const {
267         switch (by) {
268                 case HERO:
269                         return b.HeroAt(index).GetAttackChoice();
270                 case CAPSULE:
271                         return b.GetCapsule().GetAttackChoice();
272                 case MONSTER:
273                         return b.MonsterAt(index).GetAttackChoice();
274                 default:
275                         throw std::runtime_error("invalid case in BttleStats::Order::GetAttackChoice()");
276         }
277 }
278
279 Stats &BattleState::Order::GetStats(BattleState &b) const {
280         switch (by) {
281                 case HERO:
282                         return b.HeroAt(index).GetStats();
283                 case CAPSULE:
284                         return b.GetCapsule().GetStats();
285                 case MONSTER:
286                         return b.MonsterAt(index).GetStats();
287                 default:
288                         throw std::runtime_error("invalid case in BttleStats::Order::GetAttackChoice()");
289         }
290 }
291
292 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
293         bool hitSome(false);
294         if (ts.TargetsMonsters()) {
295                 for (int i(0); i < MaxMonsters(); ++i) {
296                         if (ts.IsSelected(i)) {
297                                 if (MonsterAt(i).Health() > 0) {
298                                         const Stats &defenderStats(MonsterAt(i).GetStats());
299                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
300                                         ts.SetBad(i, damage);
301                                         hitSome = true;
302                                 } else {
303                                         ts.Unselect(i);
304                                 }
305                         }
306                 }
307                 if (hitSome) return;
308                 for (int i(0); i < MaxMonsters(); ++i) {
309                         if (MonsterAt(i).Health() > 0) {
310                                 const Stats &defenderStats(MonsterAt(i).GetStats());
311                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
312                                 ts.SetBad(i, damage);
313                                 break;
314                         }
315                 }
316         } else {
317                 for (int i(0); i < NumHeroes(); ++i) {
318                         if (ts.IsSelected(i)) {
319                                 if (HeroAt(i).Health() > 0) {
320                                         const Stats &defenderStats(HeroAt(i).GetStats());
321                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
322                                         ts.SetBad(i, damage);
323                                         hitSome = true;
324                                 } else {
325                                         ts.Unselect(i);
326                                 }
327                         }
328                 }
329                 if (hitSome) return;
330                 for (int i(0); i < NumHeroes(); ++i) {
331                         if (HeroAt(i).Health() > 0) {
332                                 const Stats &defenderStats(HeroAt(i).GetStats());
333                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
334                                 ts.SetBad(i, damage);
335                                 break;
336                         }
337                 }
338         }
339 }
340
341 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
342         return attacker.Attack() / 2 - defender.Defense() / 4;
343 }
344
345 void BattleState::ApplyDamage() {
346         if (attackCursor < 0) return;
347         AttackChoice &ac = CurrentAttack().GetAttackChoice(*this);
348         TargetSelection &ts(ac.Selection());
349         if (ts.TargetsMonsters()) {
350                 for (int i(0); i < MaxMonsters(); ++i) {
351                         Monster &monster(MonsterAt(i));
352                         if (ts.IsBad(i)) {
353                                 monster.SubtractHealth(ts.GetAmount(i));
354                                 if (monster.Health() == 0) {
355                                         expReward += monster.ExpReward();
356                                         goldReward += monster.GoldReward();
357                                 }
358                         }
359                 }
360         } else {
361                 for (int i(0); i < NumHeroes(); ++i) {
362                         Hero &hero(HeroAt(i));
363                         if (ts.IsBad(i)) {
364                                 hero.SubtractHealth(ts.GetAmount(i));
365                         }
366                 }
367         }
368 }
369
370 AttackChoice &BattleState::CurrentAttackAttackChoice() {
371         return CurrentAttack().GetAttackChoice(*this);
372 }
373
374 void BattleState::ClearAllAttacks() {
375         attackCursor = -1;
376         activeHero = -1;
377         for (int i(0); i < NumHeroes(); ++i) {
378                 HeroAt(i).GetAttackChoice() = AttackChoice(this);
379         }
380         for (int i(0); i < MaxMonsters(); ++i) {
381                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
382         }
383         capsule.GetAttackChoice() = AttackChoice(this);
384         attackOrder.clear();
385 }
386
387
388 void BattleState::HandleEvents(const Input &input) {
389
390 }
391
392 void BattleState::UpdateWorld(float deltaT) {
393
394 }
395
396 void BattleState::Render(SDL_Surface *screen) {
397         assert(screen);
398         Vector<int> offset(CalculateScreenOffset(screen));
399         RenderBackground(screen, offset);
400         RenderMonsters(screen, offset);
401 }
402
403 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
404         assert(screen);
405         // black for now
406         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
407         SDL_Rect destRect;
408         destRect.x = offset.X();
409         destRect.y = offset.Y();
410         destRect.w = background->w;
411         destRect.h = background->h;
412         SDL_BlitSurface(background, 0, screen, &destRect);
413 }
414
415 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
416         assert(screen);
417         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
418                 if (MonsterPositionOccupied(i)) {
419                         if (monsters[i].GetAnimation().Running()) {
420                                 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
421                         } else {
422                                 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
423                         }
424                 }
425         }
426 }
427
428 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
429         assert(screen);
430         for (int i(0); i < numHeroes; ++i) {
431                 if (heroes[i].GetAnimation().Running()) {
432                         heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
433                 } else {
434                         int row(heroes[i].Health() > 0 ? 0 : 2);
435                         heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
436                 }
437         }
438 }
439
440 void BattleState::RenderCapsule(SDL_Surface *screen, const Vector<int> &offset) {
441         if (capsule.Health() <= 0) return;
442         if (capsule.GetAnimation().Running()) {
443                 capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset);
444         } else {
445                 capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset);
446         }
447 }
448
449 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
450         assert(screen);
451         int tagHeight(attackTypeMenu.Height());
452         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
453
454         for (int i(0); i < numHeroes; ++i) {
455                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
456         }
457 }
458
459 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
460         assert(screen);
461         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
462         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
463
464         SDL_Rect rect;
465         rect.x = offset.X();
466         rect.y = offset.Y() + Height() - tagHeight;
467         rect.w = Width();
468         rect.h = tagHeight;
469         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
470         rect.y += res->normalFont->CharHeight() / 8;
471         rect.h -= res->normalFont->CharHeight() / 4;
472         SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
473
474         for (int i(0); i < numHeroes; ++i) {
475                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
476         }
477 }
478
479 }