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