]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
merged Point into Vector
[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 "../graphics/Frame.h"
20 #include "../graphics/Sprite.h"
21
22 #include <algorithm>
23 #include <cassert>
24 #include <cstdlib>
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::Vector;
33 using graphics::Menu;
34
35 using std::rand;
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         for (int i(0); i < 4; ++i) {
82                 heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h);
83                 heroes[i].SpellMenu() = res->spellMenuPrototype;
84                 heroes[i].UpdateSpellMenu();
85                 heroes[i].IkariMenu() = res->ikariMenuPrototype;
86                 heroes[i].UpdateIkariMenu(res);
87                 heroTags[i] = HeroTag(this, i);
88                 smallHeroTags[i] = SmallHeroTag(this, i);
89         }
90
91         for (int i(0); i < int(monsters.size()); ++i) {
92                 monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h);
93         }
94
95         int tagHeight(attackTypeMenu.Height());
96         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
97         int xOffset((Width() - 2 * tagWidth) / 2);
98         heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
99         heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
100         heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
101         heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
102
103         tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
104         tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
105         xOffset = (Width() - 4 * tagWidth) / 2;
106         int yOffset(Height() - tagHeight);
107         smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
108         smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
109         smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
110         smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
111
112         itemMenu = res->itemMenuPrototype;
113         LoadInventory();
114 }
115
116 void BattleState::LoadInventory() {
117         const Inventory &inv(*res->inventory);
118         itemMenu.Clear();
119         itemMenu.Reserve(inv.MaxItems());
120         for (int i(0); i < inv.MaxItems(); ++i) {
121                 const Item *item(inv.ItemAt(i));
122                 if (item) {
123                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
124                 } else {
125                         itemMenu.AddEmptyEntry();
126                 }
127         }
128         ClearAllAttacks();
129 }
130
131 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
132
133 }
134
135 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
136         if (ranAway) {
137                 ctrl.PopState(); // quit the battle scene
138                 return;
139         }
140         if (Victory()) {
141                 // TODO: push victory state
142                 ctrl.PopState();
143                 return;
144         }
145         if (Defeat()) {
146                 // TODO: push defeat state
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::PauseState(Application &ctrl, 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         // TODO: this only evaluates SWORD type attacks
231         if (CurrentAttack().isMonster) {
232                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
233                 CalculateDamage(attackerStats, ts);
234         } else {
235                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
236                 CalculateDamage(attackerStats, ts);
237         }
238 }
239
240 void BattleState::DecideMonsterAttack(Monster &m) const {
241         AttackChoice &ac(m.GetAttackChoice());
242         TargetSelection &ts(ac.Selection());
243         ac.Reset();
244         // TODO: run monster's attack script
245         int target(rand() % NumHeroes());
246         while (!HeroPositionOccupied(target)) {
247                 target = rand() % NumHeroes();
248         }
249         ac.SetType(AttackChoice::SWORD);
250         ts.SelectHeroes();
251         ts.SetSingle();
252         ts.Select(target);
253 }
254
255 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
256         bool hitSome(false);
257         if (ts.TargetsMonsters()) {
258                 for (int i(0); i < MaxMonsters(); ++i) {
259                         if (ts.IsSelected(i)) {
260                                 if (MonsterAt(i).Health() > 0) {
261                                         const Stats &defenderStats(MonsterAt(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 < MaxMonsters(); ++i) {
272                         if (MonsterAt(i).Health() > 0) {
273                                 const Stats &defenderStats(MonsterAt(i).GetStats());
274                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
275                                 ts.SetBad(i, damage);
276                                 break;
277                         }
278                 }
279         } else {
280                 for (int i(0); i < NumHeroes(); ++i) {
281                         if (ts.IsSelected(i)) {
282                                 if (HeroAt(i).Health() > 0) {
283                                         const Stats &defenderStats(HeroAt(i).GetStats());
284                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
285                                         ts.SetBad(i, damage);
286                                         hitSome = true;
287                                 } else {
288                                         ts.Unselect(i);
289                                 }
290                         }
291                 }
292                 if (hitSome) return;
293                 for (int i(0); i < NumHeroes(); ++i) {
294                         if (HeroAt(i).Health() > 0) {
295                                 const Stats &defenderStats(HeroAt(i).GetStats());
296                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
297                                 ts.SetBad(i, damage);
298                                 break;
299                         }
300                 }
301         }
302 }
303
304 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
305         // TODO: find out real formula and add some randomness
306         return attacker.Attack() / 2 - defender.Defense() / 4;
307 }
308
309 void BattleState::ApplyDamage() {
310         if (attackCursor < 0) return;
311         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
312         TargetSelection &ts(ac.Selection());
313         if (ts.TargetsMonsters()) {
314                 for (int i(0); i < MaxMonsters(); ++i) {
315                         Monster &monster(MonsterAt(i));
316                         if (ts.IsBad(i)) {
317                                 monster.SubtractHealth(ts.GetAmount(i));
318                                 if (monster.Health() == 0) {
319                                         expReward += monster.ExpReward();
320                                         goldReward += monster.GoldReward();
321                                 }
322                         }
323                 }
324         } else {
325                 for (int i(0); i < NumHeroes(); ++i) {
326                         Hero &hero(HeroAt(i));
327                         if (ts.IsBad(i)) {
328                                 hero.SubtractHealth(ts.GetAmount(i));
329                         }
330                 }
331         }
332 }
333
334 AttackChoice &BattleState::CurrentAttackAttackChoice() {
335         if (CurrentAttack().isMonster) {
336                 return MonsterAt(CurrentAttack().index).GetAttackChoice();
337         } else {
338                 return HeroAt(CurrentAttack().index).GetAttackChoice();
339         }
340 }
341
342 void BattleState::ClearAllAttacks() {
343         attackCursor = -1;
344         activeHero = -1;
345         for (int i(0); i < NumHeroes(); ++i) {
346                 HeroAt(i).GetAttackChoice() = AttackChoice(this);
347         }
348         for (int i(0); i < MaxMonsters(); ++i) {
349                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
350         }
351         attackOrder.clear();
352 }
353
354
355 void BattleState::HandleEvents(const Input &input) {
356
357 }
358
359 void BattleState::UpdateWorld(float deltaT) {
360
361 }
362
363 void BattleState::Render(SDL_Surface *screen) {
364         assert(screen);
365         Vector<int> offset(CalculateScreenOffset(screen));
366         RenderBackground(screen, offset);
367         RenderMonsters(screen, offset);
368 }
369
370 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
371         assert(screen);
372         // black for now
373         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
374         SDL_Rect destRect;
375         destRect.x = offset.X();
376         destRect.y = offset.Y();
377         destRect.w = background->w;
378         destRect.h = background->h;
379         SDL_BlitSurface(background, 0, screen, &destRect);
380 }
381
382 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
383         assert(screen);
384         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
385                 if (MonsterPositionOccupied(i)) {
386                         if (monsters[i].GetAnimation().Running()) {
387                                 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
388                         } else {
389                                 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
390                         }
391                 }
392         }
393 }
394
395 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
396         assert(screen);
397         for (int i(0); i < numHeroes; ++i) {
398                 if (heroes[i].GetAnimation().Running()) {
399                         heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
400                 } else {
401                         int row(heroes[i].Health() > 0 ? 0 : 2);
402                         heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
403                 }
404         }
405 }
406
407 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
408         assert(screen);
409         int tagHeight(attackTypeMenu.Height());
410         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
411
412         for (int i(0); i < numHeroes; ++i) {
413                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
414         }
415 }
416
417 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
418         assert(screen);
419         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
420         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
421
422         SDL_Rect rect;
423         rect.x = offset.X();
424         rect.y = offset.Y() + Height() - tagHeight;
425         rect.w = Width();
426         rect.h = tagHeight;
427         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
428         rect.y += res->normalFont->CharHeight() / 8;
429         rect.h -= res->normalFont->CharHeight() / 4;
430         SDL_FillRect(screen, &rect, res->heroesBgColor);
431
432         for (int i(0); i < numHeroes; ++i) {
433                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
434         }
435 }
436
437 }