]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
removed some outdated TODOs
[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                 spellMenus[i] = res->spellMenuPrototype;
78                 LoadSpellMenu(i);
79                 ikariMenus[i] = res->ikariMenuPrototype;
80                 LoadIkariMenu(i);
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::LoadSpellMenu(vector<Hero>::size_type index) {
107         assert(index >= 0 && index < 4);
108         spellMenus[index].Clear();
109         spellMenus[index].Reserve(HeroAt(index).Spells().size());
110         for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
111                 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
112                 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
113         }
114 }
115
116 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
117         assert(index >= 0 && index < 4);
118         ikariMenus[index].Clear();
119         ikariMenus[index].Reserve(6);
120
121         if (HeroAt(index).HasWeapon()) {
122                 ikariMenus[index].Add(
123                                 HeroAt(index).Weapon()->Name(),
124                                 HeroAt(index).Weapon(),
125                                 HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(),
126                                 res->weaponMenuIcon,
127                                 0,
128                                 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
129         } else {
130                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
131         }
132
133         if (HeroAt(index).HasArmor()) {
134                 ikariMenus[index].Add(
135                                 HeroAt(index).Armor()->Name(),
136                                 HeroAt(index).Armor(),
137                                 HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(),
138                                 res->armorMenuIcon,
139                                 0,
140                                 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
141         } else {
142                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
143         }
144
145         if (HeroAt(index).HasShield()) {
146                 ikariMenus[index].Add(
147                                 HeroAt(index).Shield()->Name(),
148                                 HeroAt(index).Shield(),
149                                 HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(),
150                                 res->shieldMenuIcon,
151                                 0,
152                                 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
153         } else {
154                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
155         }
156
157         if (HeroAt(index).HasHelmet()) {
158                 ikariMenus[index].Add(
159                                 HeroAt(index).Helmet()->Name(),
160                                 HeroAt(index).Helmet(),
161                                 HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(),
162                                 res->helmetMenuIcon,
163                                 0,
164                                 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
165         } else {
166                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
167         }
168
169         if (HeroAt(index).HasRing()) {
170                 ikariMenus[index].Add(
171                                 HeroAt(index).Ring()->Name(),
172                                 HeroAt(index).Ring(),
173                                 HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(),
174                                 res->ringMenuIcon,
175                                 0,
176                                 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
177         } else {
178                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
179         }
180
181         if (HeroAt(index).HasJewel()) {
182                 ikariMenus[index].Add(
183                                 HeroAt(index).Jewel()->Name(),
184                                 HeroAt(index).Jewel(),
185                                 HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(),
186                                 res->jewelMenuIcon,
187                                 0,
188                                 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
189         } else {
190                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
191         }
192 }
193
194 void BattleState::LoadInventory() {
195         const Inventory &inv(*res->inventory);
196         itemMenu.Clear();
197         itemMenu.Reserve(inv.MaxItems());
198         for (int i(0); i < inv.MaxItems(); ++i) {
199                 const Item *item(inv.ItemAt(i));
200                 if (item) {
201                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
202                 } else {
203                         itemMenu.AddEmptyEntry();
204                 }
205         }
206         ClearAllAttacks();
207 }
208
209 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
210
211 }
212
213 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
214         if (ranAway) {
215                 ctrl.PopState(); // quit the battle scene
216                 return;
217         }
218         if (Victory()) {
219                 // TODO: push victory state
220                 ctrl.PopState();
221                 return;
222         }
223         if (Defeat()) {
224                 // TODO: push defeat state
225                 ctrl.PopState();
226                 return;
227         }
228         // TODO: this should not push a state while quitting
229         if (AttackSelectionDone()) {
230                 ctrl.PushState(new PerformAttacks(this));
231         } else {
232                 ctrl.PushState(new SelectMoveAction(this));
233         }
234 }
235
236 bool BattleState::Victory() const {
237         for (int i(0); i < MaxMonsters(); ++i) {
238                 if (MonsterAt(i).Health() > 0) return false;
239         }
240         return true;
241 }
242
243 bool BattleState::Defeat() const {
244         for (int i(0); i < NumHeroes(); ++i) {
245                 if (HeroAt(i).Health() > 0) return false;
246         }
247         return true;
248 }
249
250 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
251
252 }
253
254
255 class OrderCompare {
256         public:
257                 OrderCompare(BattleState *battle) : battle(battle) { }
258                 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
259                         int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
260                         int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
261                         return lagl > ragl;
262                 }
263         private:
264                 BattleState *battle;
265 };
266
267 void BattleState::CalculateAttackOrder() {
268         attackOrder.reserve(monsters.size() + NumHeroes());
269         for (int i(0); i < NumHeroes(); ++i) {
270                 attackOrder.push_back(Order(i, false));
271         }
272         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
273                 attackOrder.push_back(Order(i, true));
274         }
275         std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
276
277         monsterAttacks.resize(monsters.size(), AttackChoice(this));
278 }
279
280 void BattleState::NextAttack() {
281         ++attackCursor;
282         while (attackCursor < int(attackOrder.size())) {
283                 if (attackOrder[attackCursor].isMonster) {
284                         if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
285                 } else {
286                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
287                 }
288                 ++attackCursor;
289         }
290 }
291
292 bool BattleState::AttacksFinished() const {
293         return attackCursor >= int(attackOrder.size())
294                         || Victory() || Defeat();
295 }
296
297 void BattleState::CalculateDamage() {
298         AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
299         if (ac.GetType() == AttackChoice::DEFEND) return;
300
301         if (CurrentAttack().isMonster) {
302                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
303                 // TODO: run monster's attack script
304                 ac.SetType(AttackChoice::SWORD);
305                 ac.Selection().SelectSingle();
306                 ac.Selection().SelectHeroes();
307                 for (int i(0); i < NumHeroes(); ++i) {
308                         if (HeroAt(i).Health() > 0) {
309                                 const Stats &defenderStats(HeroAt(i).GetStats());
310                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
311                                 ac.Selection().SetBad(0, damage);
312                                 break;
313                         }
314                 }
315         } else {
316                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
317                 TargetSelection &ts(ac.Selection());
318                 bool hitSome(false);
319                 if (ts.TargetsEnemies()) {
320                         for (int i(0); i < MaxMonsters(); ++i) {
321                                 if (ts.IsSelected(i)) {
322                                         if (MonsterAt(i).Health() > 0) {
323                                                 const Stats &defenderStats(MonsterAt(i).GetStats());
324                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
325                                                 ts.SetBad(i, damage);
326                                                 hitSome = true;
327                                         } else {
328                                                 ts.Unselect(i);
329                                         }
330                                 }
331                         }
332                         if (hitSome) return;
333                         for (int i(0); i < MaxMonsters(); ++i) {
334                                 if (MonsterAt(i).Health() > 0) {
335                                         const Stats &defenderStats(MonsterAt(i).GetStats());
336                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
337                                         ts.SetBad(i, damage);
338                                         break;
339                                 }
340                         }
341                 } else {
342                         for (int i(0); i < NumHeroes(); ++i) {
343                                 if (ts.IsSelected(i)) {
344                                         if (HeroAt(i).Health() > 0) {
345                                                 const Stats &defenderStats(HeroAt(i).GetStats());
346                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
347                                                 ts.SetBad(i, damage);
348                                                 hitSome = true;
349                                         } else {
350                                                 ts.Unselect(i);
351                                         }
352                                 }
353                         }
354                         if (hitSome) return;
355                         for (int i(0); i < NumHeroes(); ++i) {
356                                 if (HeroAt(i).Health() > 0) {
357                                         const Stats &defenderStats(HeroAt(i).GetStats());
358                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
359                                         ts.SetBad(i, damage);
360                                         break;
361                                 }
362                         }
363                 }
364         }
365 }
366
367 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
368         // TODO: find out real formula and add some randomness
369         return attacker.Attack() / 2 - defender.Defense() / 4;
370 }
371
372 void BattleState::ApplyDamage() {
373         if (attackCursor < 0) return;
374         AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
375         TargetSelection &ts(ac.Selection());
376         if (ts.TargetsEnemies()) {
377                 for (int i(0); i < MaxMonsters(); ++i) {
378                         if (ts.IsBad(i)) {
379                                 MonsterAt(i).SubtractHealth(ts.GetAmount(i));
380                                 // TODO: collect reward if dead
381                         }
382                 }
383         } else {
384                 for (int i(0); i < NumHeroes(); ++i) {
385                         if (ts.IsBad(i)) {
386                                 HeroAt(i).SubtractHealth(ts.GetAmount(i));
387                         }
388                 }
389         }
390 }
391
392 AttackChoice &BattleState::CurrentAttackAttackChoice() {
393         if (CurrentAttack().isMonster) {
394                 return monsterAttacks[CurrentAttack().index];
395         } else {
396                 return AttackChoiceAt(CurrentAttack().index);
397         }
398 }
399
400 void BattleState::ClearAllAttacks() {
401         attackCursor = -1;
402         activeHero = -1;
403         for (int i(0); i < numHeroes; ++i) {
404                 attackChoices[i] = AttackChoice(this);
405         }
406         attackOrder.clear();
407         monsterAttacks.clear();
408 }
409
410
411 void BattleState::HandleEvents(const Input &input) {
412
413 }
414
415 void BattleState::UpdateWorld(float deltaT) {
416
417 }
418
419 void BattleState::Render(SDL_Surface *screen) {
420         assert(screen);
421         Vector<int> offset(CalculateScreenOffset(screen));
422         RenderBackground(screen, offset);
423         RenderMonsters(screen, offset);
424 }
425
426 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
427         assert(screen);
428         // black for now
429         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
430         SDL_Rect destRect;
431         destRect.x = offset.X();
432         destRect.y = offset.Y();
433         destRect.w = background->w;
434         destRect.h = background->h;
435         SDL_BlitSurface(background, 0, screen, &destRect);
436 }
437
438 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
439         assert(screen);
440         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
441                 if (MonsterPositionOccupied(i)) {
442                         // TODO: better solution for running animations
443                         if (monsters[i].AttackAnimation() && monsters[i].AttackAnimation()->Running()) {
444                                 monsters[i].AttackAnimation()->DrawCenter(screen, monsterPositions[i] + offset);
445                         } else if (monsters[i].SpellAnimation() && monsters[i].SpellAnimation()->Running()) {
446                                 monsters[i].SpellAnimation()->DrawCenter(screen, monsterPositions[i] + offset);
447                         } else {
448                                 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
449                         }
450                 }
451         }
452 }
453
454 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
455         assert(screen);
456         for (int i(0); i < numHeroes; ++i) {
457                 if (heroes[i].AttackAnimation() && heroes[i].AttackAnimation()->Running()) {
458                         heroes[i].AttackAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
459                 } else if (heroes[i].SpellAnimation() && heroes[i].SpellAnimation()->Running()) {
460                         heroes[i].SpellAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
461                 } else {
462                         int row(heroes[i].Health() > 0 ? 0 : 2);
463                         heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
464                 }
465         }
466 }
467
468 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
469         assert(screen);
470         int tagHeight(attackTypeMenu.Height());
471         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
472
473         for (int i(0); i < numHeroes; ++i) {
474                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
475         }
476 }
477
478 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
479         assert(screen);
480         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
481         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
482
483         SDL_Rect rect;
484         rect.x = offset.X();
485         rect.y = offset.Y() + Height() - tagHeight;
486         rect.w = Width();
487         rect.h = tagHeight;
488         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
489         rect.y += res->normalFont->CharHeight() / 8;
490         rect.h -= res->normalFont->CharHeight() / 4;
491         SDL_FillRect(screen, &rect, res->heroesBgColor);
492
493         for (int i(0); i < numHeroes; ++i) {
494                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
495         }
496 }
497
498 }