]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
649470e894dd20e9db70c690a02d39cc938c15ac
[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                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
194         }
195         std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
196 }
197
198 void BattleState::NextAttack() {
199         if (Victory() || Defeat()) {
200                 attackCursor = attackOrder.size();
201                 return;
202         }
203         ++attackCursor;
204         while (attackCursor < int(attackOrder.size())) {
205                 if (attackOrder[attackCursor].isMonster) {
206                         if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
207                 } else {
208                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
209                 }
210                 ++attackCursor;
211         }
212 }
213
214 bool BattleState::AttacksFinished() const {
215         return attackCursor >= int(attackOrder.size())
216                         || Victory() || Defeat();
217 }
218
219 void BattleState::CalculateDamage() {
220         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
221         if (ac.GetType() == AttackChoice::DEFEND) return;
222
223         if (CurrentAttack().isMonster) {
224                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
225                 // TODO: run monster's attack script
226                 ac.SetType(AttackChoice::SWORD);
227                 ac.Selection().SelectSingle();
228                 ac.Selection().SelectHeroes();
229                 for (int i(0); i < NumHeroes(); ++i) {
230                         if (HeroAt(i).Health() > 0) {
231                                 const Stats &defenderStats(HeroAt(i).GetStats());
232                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
233                                 ac.Selection().SetBad(0, damage);
234                                 break;
235                         }
236                 }
237         } else {
238                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
239                 TargetSelection &ts(ac.Selection());
240                 bool hitSome(false);
241                 if (ts.TargetsEnemies()) {
242                         for (int i(0); i < MaxMonsters(); ++i) {
243                                 if (ts.IsSelected(i)) {
244                                         if (MonsterAt(i).Health() > 0) {
245                                                 const Stats &defenderStats(MonsterAt(i).GetStats());
246                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
247                                                 ts.SetBad(i, damage);
248                                                 hitSome = true;
249                                         } else {
250                                                 ts.Unselect(i);
251                                         }
252                                 }
253                         }
254                         if (hitSome) return;
255                         for (int i(0); i < MaxMonsters(); ++i) {
256                                 if (MonsterAt(i).Health() > 0) {
257                                         const Stats &defenderStats(MonsterAt(i).GetStats());
258                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
259                                         ts.SetBad(i, damage);
260                                         break;
261                                 }
262                         }
263                 } else {
264                         for (int i(0); i < NumHeroes(); ++i) {
265                                 if (ts.IsSelected(i)) {
266                                         if (HeroAt(i).Health() > 0) {
267                                                 const Stats &defenderStats(HeroAt(i).GetStats());
268                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
269                                                 ts.SetBad(i, damage);
270                                                 hitSome = true;
271                                         } else {
272                                                 ts.Unselect(i);
273                                         }
274                                 }
275                         }
276                         if (hitSome) return;
277                         for (int i(0); i < NumHeroes(); ++i) {
278                                 if (HeroAt(i).Health() > 0) {
279                                         const Stats &defenderStats(HeroAt(i).GetStats());
280                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
281                                         ts.SetBad(i, damage);
282                                         break;
283                                 }
284                         }
285                 }
286         }
287 }
288
289 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
290         // TODO: find out real formula and add some randomness
291         return attacker.Attack() / 2 - defender.Defense() / 4;
292 }
293
294 void BattleState::ApplyDamage() {
295         if (attackCursor < 0) return;
296         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
297         TargetSelection &ts(ac.Selection());
298         if (ts.TargetsEnemies()) {
299                 for (int i(0); i < MaxMonsters(); ++i) {
300                         Monster &monster(MonsterAt(i));
301                         if (ts.IsBad(i)) {
302                                 monster.SubtractHealth(ts.GetAmount(i));
303                                 if (monster.Health() == 0) {
304                                         expReward += monster.ExpReward();
305                                         goldReward += monster.GoldReward();
306                                 }
307                         }
308                 }
309         } else {
310                 for (int i(0); i < NumHeroes(); ++i) {
311                         Hero &hero(HeroAt(i));
312                         if (ts.IsBad(i)) {
313                                 hero.SubtractHealth(ts.GetAmount(i));
314                         }
315                 }
316         }
317 }
318
319 AttackChoice &BattleState::CurrentAttackAttackChoice() {
320         if (CurrentAttack().isMonster) {
321                 return MonsterAt(CurrentAttack().index).GetAttackChoice();
322         } else {
323                 return HeroAt(CurrentAttack().index).GetAttackChoice();
324         }
325 }
326
327 void BattleState::ClearAllAttacks() {
328         attackCursor = -1;
329         activeHero = -1;
330         for (int i(0); i < NumHeroes(); ++i) {
331                 HeroAt(i).GetAttackChoice() = AttackChoice(this);
332         }
333         for (int i(0); i < MaxMonsters(); ++i) {
334                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
335         }
336         attackOrder.clear();
337 }
338
339
340 void BattleState::HandleEvents(const Input &input) {
341
342 }
343
344 void BattleState::UpdateWorld(float deltaT) {
345
346 }
347
348 void BattleState::Render(SDL_Surface *screen) {
349         assert(screen);
350         Vector<int> offset(CalculateScreenOffset(screen));
351         RenderBackground(screen, offset);
352         RenderMonsters(screen, offset);
353 }
354
355 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
356         assert(screen);
357         // black for now
358         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
359         SDL_Rect destRect;
360         destRect.x = offset.X();
361         destRect.y = offset.Y();
362         destRect.w = background->w;
363         destRect.h = background->h;
364         SDL_BlitSurface(background, 0, screen, &destRect);
365 }
366
367 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
368         assert(screen);
369         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
370                 if (MonsterPositionOccupied(i)) {
371                         if (monsters[i].GetAnimation().Running()) {
372                                 monsters[i].GetAnimation().DrawCenter(screen, monsterPositions[i] + offset);
373                         } else {
374                                 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
375                         }
376                 }
377         }
378 }
379
380 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
381         assert(screen);
382         for (int i(0); i < numHeroes; ++i) {
383                 if (heroes[i].GetAnimation().Running()) {
384                         heroes[i].GetAnimation().DrawCenter(screen, heroesPositions[i] + offset);
385                 } else {
386                         int row(heroes[i].Health() > 0 ? 0 : 2);
387                         heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
388                 }
389         }
390 }
391
392 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
393         assert(screen);
394         int tagHeight(attackTypeMenu.Height());
395         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
396
397         for (int i(0); i < numHeroes; ++i) {
398                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
399         }
400 }
401
402 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
403         assert(screen);
404         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
405         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
406
407         SDL_Rect rect;
408         rect.x = offset.X();
409         rect.y = offset.Y() + Height() - tagHeight;
410         rect.w = Width();
411         rect.h = tagHeight;
412         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
413         rect.y += res->normalFont->CharHeight() / 8;
414         rect.h -= res->normalFont->CharHeight() / 4;
415         SDL_FillRect(screen, &rect, res->heroesBgColor);
416
417         for (int i(0); i < numHeroes; ++i) {
418                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
419         }
420 }
421
422 }