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