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