]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
7c8b3306e8c29947c9a4868c8de8c2d20c8f408f
[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/GameState.h"
16 #include "../common/Ikari.h"
17 #include "../common/Inventory.h"
18 #include "../common/Item.h"
19 #include "../common/Spell.h"
20 #include "../graphics/Frame.h"
21 #include "../graphics/Sprite.h"
22
23 #include <algorithm>
24 #include <cassert>
25 #include <cstdlib>
26 #include <stdexcept>
27
28 using app::Application;
29 using app::Input;
30 using common::Inventory;
31 using common::Item;
32 using common::Spell;
33 using common::Stats;
34 using geometry::Vector;
35 using graphics::Menu;
36
37 using std::rand;
38 using std::vector;
39
40 namespace battle {
41
42 void BattleState::AddMonster(const Monster &m) {
43         if (monsters.size() >= monstersLayout->NumPositions()) {
44                 throw std::overflow_error("too many monsters for layout");
45         }
46         monsters.push_back(m);
47 }
48
49 void BattleState::AddHero(const Hero &h) {
50         if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
51                 throw std::overflow_error("too many heroes for layout");
52         }
53         heroes[numHeroes] = h;
54         ++numHeroes;
55 }
56
57 void BattleState::NextHero() {
58         ++activeHero;
59         while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
60                 ++activeHero;
61         }
62 }
63
64 void BattleState::PreviousHero() {
65         --activeHero;
66         while (activeHero >= 0 && heroes[activeHero].Health() == 0) {
67                 --activeHero;
68         }
69 }
70
71 void BattleState::SwapHeroes(int lhs, int rhs) {
72         if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
73         std::swap(heroes[lhs], heroes[rhs]);
74 }
75
76
77 void BattleState::OnResize(int w, int h) {
78
79 }
80
81
82 void BattleState::OnEnterState(SDL_Surface *screen) {
83         for (int i(0); i < 4; ++i) {
84                 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
85                 heroes[i].SpellMenu() = *res->spellMenuProperties;
86                 heroes[i].UpdateSpellMenu();
87                 heroes[i].IkariMenu() = *res->ikariMenuProperties;
88                 heroes[i].UpdateIkariMenu(res);
89                 heroTags[i] = HeroTag(this, i);
90                 smallHeroTags[i] = SmallHeroTag(this, i);
91         }
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                         int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
182                         int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
183                         return lagl > ragl;
184                 }
185         private:
186                 BattleState *battle;
187 };
188
189 void BattleState::CalculateAttackOrder() {
190         attackOrder.reserve(monsters.size() + NumHeroes());
191         for (int i(0); i < NumHeroes(); ++i) {
192                 attackOrder.push_back(Order(i, false));
193         }
194         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
195                 attackOrder.push_back(Order(i, true));
196                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
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 {
211                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
212                 }
213                 ++attackCursor;
214         }
215 }
216
217 bool BattleState::AttacksFinished() const {
218         return attackCursor >= int(attackOrder.size())
219                         || Victory() || Defeat();
220 }
221
222 void BattleState::CalculateDamage() {
223         if (CurrentAttack().isMonster) {
224                 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
225         }
226         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
227         if (ac.GetType() == AttackChoice::DEFEND) return;
228         TargetSelection &ts(ac.Selection());
229
230         if (CurrentAttack().isMonster) {
231                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
232                 CalculateDamage(attackerStats, ts);
233         } else {
234                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
235                 CalculateDamage(attackerStats, ts);
236         }
237 }
238
239 void BattleState::DecideMonsterAttack(Monster &m) const {
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::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
254         bool hitSome(false);
255         if (ts.TargetsMonsters()) {
256                 for (int i(0); i < MaxMonsters(); ++i) {
257                         if (ts.IsSelected(i)) {
258                                 if (MonsterAt(i).Health() > 0) {
259                                         const Stats &defenderStats(MonsterAt(i).GetStats());
260                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
261                                         ts.SetBad(i, damage);
262                                         hitSome = true;
263                                 } else {
264                                         ts.Unselect(i);
265                                 }
266                         }
267                 }
268                 if (hitSome) return;
269                 for (int i(0); i < MaxMonsters(); ++i) {
270                         if (MonsterAt(i).Health() > 0) {
271                                 const Stats &defenderStats(MonsterAt(i).GetStats());
272                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
273                                 ts.SetBad(i, damage);
274                                 break;
275                         }
276                 }
277         } else {
278                 for (int i(0); i < NumHeroes(); ++i) {
279                         if (ts.IsSelected(i)) {
280                                 if (HeroAt(i).Health() > 0) {
281                                         const Stats &defenderStats(HeroAt(i).GetStats());
282                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
283                                         ts.SetBad(i, damage);
284                                         hitSome = true;
285                                 } else {
286                                         ts.Unselect(i);
287                                 }
288                         }
289                 }
290                 if (hitSome) return;
291                 for (int i(0); i < NumHeroes(); ++i) {
292                         if (HeroAt(i).Health() > 0) {
293                                 const Stats &defenderStats(HeroAt(i).GetStats());
294                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
295                                 ts.SetBad(i, damage);
296                                 break;
297                         }
298                 }
299         }
300 }
301
302 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
303         return attacker.Attack() / 2 - defender.Defense() / 4;
304 }
305
306 void BattleState::ApplyDamage() {
307         if (attackCursor < 0) return;
308         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
309         TargetSelection &ts(ac.Selection());
310         if (ts.TargetsMonsters()) {
311                 for (int i(0); i < MaxMonsters(); ++i) {
312                         Monster &monster(MonsterAt(i));
313                         if (ts.IsBad(i)) {
314                                 monster.SubtractHealth(ts.GetAmount(i));
315                                 if (monster.Health() == 0) {
316                                         expReward += monster.ExpReward();
317                                         goldReward += monster.GoldReward();
318                                 }
319                         }
320                 }
321         } else {
322                 for (int i(0); i < NumHeroes(); ++i) {
323                         Hero &hero(HeroAt(i));
324                         if (ts.IsBad(i)) {
325                                 hero.SubtractHealth(ts.GetAmount(i));
326                         }
327                 }
328         }
329 }
330
331 AttackChoice &BattleState::CurrentAttackAttackChoice() {
332         if (CurrentAttack().isMonster) {
333                 return MonsterAt(CurrentAttack().index).GetAttackChoice();
334         } else {
335                 return HeroAt(CurrentAttack().index).GetAttackChoice();
336         }
337 }
338
339 void BattleState::ClearAllAttacks() {
340         attackCursor = -1;
341         activeHero = -1;
342         for (int i(0); i < NumHeroes(); ++i) {
343                 HeroAt(i).GetAttackChoice() = AttackChoice(this);
344         }
345         for (int i(0); i < MaxMonsters(); ++i) {
346                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
347         }
348         attackOrder.clear();
349 }
350
351
352 void BattleState::HandleEvents(const Input &input) {
353
354 }
355
356 void BattleState::UpdateWorld(float deltaT) {
357
358 }
359
360 void BattleState::Render(SDL_Surface *screen) {
361         assert(screen);
362         Vector<int> offset(CalculateScreenOffset(screen));
363         RenderBackground(screen, offset);
364         RenderMonsters(screen, offset);
365 }
366
367 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
368         assert(screen);
369         // black for now
370         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
371         SDL_Rect destRect;
372         destRect.x = offset.X();
373         destRect.y = offset.Y();
374         destRect.w = background->w;
375         destRect.h = background->h;
376         SDL_BlitSurface(background, 0, screen, &destRect);
377 }
378
379 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
380         assert(screen);
381         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
382                 if (MonsterPositionOccupied(i)) {
383                         if (monsters[i].GetAnimation().Running()) {
384                                 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
385                         } else {
386                                 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
387                         }
388                 }
389         }
390 }
391
392 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
393         assert(screen);
394         for (int i(0); i < numHeroes; ++i) {
395                 if (heroes[i].GetAnimation().Running()) {
396                         heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
397                 } else {
398                         int row(heroes[i].Health() > 0 ? 0 : 2);
399                         heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
400                 }
401         }
402 }
403
404 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
405         assert(screen);
406         int tagHeight(attackTypeMenu.Height());
407         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
408
409         for (int i(0); i < numHeroes; ++i) {
410                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
411         }
412 }
413
414 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
415         assert(screen);
416         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
417         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
418
419         SDL_Rect rect;
420         rect.x = offset.X();
421         rect.y = offset.Y() + Height() - tagHeight;
422         rect.w = Width();
423         rect.h = tagHeight;
424         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
425         rect.y += res->normalFont->CharHeight() / 8;
426         rect.h -= res->normalFont->CharHeight() / 4;
427         SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
428
429         for (int i(0); i < numHeroes; ++i) {
430                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
431         }
432 }
433
434 }