]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
Merge branch 'loader'
[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->spellMenuProperties;
84                 heroes[i].UpdateSpellMenu();
85                 heroes[i].IkariMenu() = *res->ikariMenuProperties;
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->itemMenuProperties;
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                 ctrl.PopState();
142                 return;
143         }
144         if (Defeat()) {
145                 ctrl.PopState();
146                 return;
147         }
148         // TODO: this should not push a state while quitting
149         if (AttackSelectionDone()) {
150                 ctrl.PushState(new PerformAttacks(this));
151         } else {
152                 ctrl.PushState(new SelectMoveAction(this));
153         }
154 }
155
156 bool BattleState::Victory() const {
157         for (int i(0); i < MaxMonsters(); ++i) {
158                 if (MonsterAt(i).Health() > 0) return false;
159         }
160         return true;
161 }
162
163 bool BattleState::Defeat() const {
164         for (int i(0); i < NumHeroes(); ++i) {
165                 if (HeroAt(i).Health() > 0) return false;
166         }
167         return true;
168 }
169
170 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
171
172 }
173
174
175 class OrderCompare {
176         public:
177                 OrderCompare(BattleState *battle) : battle(battle) { }
178                 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
179                         int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
180                         int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
181                         return lagl > ragl;
182                 }
183         private:
184                 BattleState *battle;
185 };
186
187 void BattleState::CalculateAttackOrder() {
188         attackOrder.reserve(monsters.size() + NumHeroes());
189         for (int i(0); i < NumHeroes(); ++i) {
190                 attackOrder.push_back(Order(i, false));
191         }
192         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
193                 attackOrder.push_back(Order(i, true));
194                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
195         }
196         std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
197 }
198
199 void BattleState::NextAttack() {
200         if (Victory() || Defeat()) {
201                 attackCursor = attackOrder.size();
202                 return;
203         }
204         ++attackCursor;
205         while (attackCursor < int(attackOrder.size())) {
206                 if (attackOrder[attackCursor].isMonster) {
207                         if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
208                 } else {
209                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
210                 }
211                 ++attackCursor;
212         }
213 }
214
215 bool BattleState::AttacksFinished() const {
216         return attackCursor >= int(attackOrder.size())
217                         || Victory() || Defeat();
218 }
219
220 void BattleState::CalculateDamage() {
221         if (CurrentAttack().isMonster) {
222                 DecideMonsterAttack(MonsterAt(CurrentAttack().index));
223         }
224         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
225         if (ac.GetType() == AttackChoice::DEFEND) return;
226         TargetSelection &ts(ac.Selection());
227
228         if (CurrentAttack().isMonster) {
229                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
230                 CalculateDamage(attackerStats, ts);
231         } else {
232                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
233                 CalculateDamage(attackerStats, ts);
234         }
235 }
236
237 void BattleState::DecideMonsterAttack(Monster &m) const {
238         AttackChoice &ac(m.GetAttackChoice());
239         TargetSelection &ts(ac.Selection());
240         ac.Reset();
241         int target(rand() % NumHeroes());
242         while (!HeroPositionOccupied(target)) {
243                 target = rand() % NumHeroes();
244         }
245         ac.SetType(AttackChoice::SWORD);
246         ts.SelectHeroes();
247         ts.SetSingle();
248         ts.Select(target);
249 }
250
251 void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
252         bool hitSome(false);
253         if (ts.TargetsMonsters()) {
254                 for (int i(0); i < MaxMonsters(); ++i) {
255                         if (ts.IsSelected(i)) {
256                                 if (MonsterAt(i).Health() > 0) {
257                                         const Stats &defenderStats(MonsterAt(i).GetStats());
258                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
259                                         ts.SetBad(i, damage);
260                                         hitSome = true;
261                                 } else {
262                                         ts.Unselect(i);
263                                 }
264                         }
265                 }
266                 if (hitSome) return;
267                 for (int i(0); i < MaxMonsters(); ++i) {
268                         if (MonsterAt(i).Health() > 0) {
269                                 const Stats &defenderStats(MonsterAt(i).GetStats());
270                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
271                                 ts.SetBad(i, damage);
272                                 break;
273                         }
274                 }
275         } else {
276                 for (int i(0); i < NumHeroes(); ++i) {
277                         if (ts.IsSelected(i)) {
278                                 if (HeroAt(i).Health() > 0) {
279                                         const Stats &defenderStats(HeroAt(i).GetStats());
280                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
281                                         ts.SetBad(i, damage);
282                                         hitSome = true;
283                                 } else {
284                                         ts.Unselect(i);
285                                 }
286                         }
287                 }
288                 if (hitSome) return;
289                 for (int i(0); i < NumHeroes(); ++i) {
290                         if (HeroAt(i).Health() > 0) {
291                                 const Stats &defenderStats(HeroAt(i).GetStats());
292                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
293                                 ts.SetBad(i, damage);
294                                 break;
295                         }
296                 }
297         }
298 }
299
300 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
301         return attacker.Attack() / 2 - defender.Defense() / 4;
302 }
303
304 void BattleState::ApplyDamage() {
305         if (attackCursor < 0) return;
306         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
307         TargetSelection &ts(ac.Selection());
308         if (ts.TargetsMonsters()) {
309                 for (int i(0); i < MaxMonsters(); ++i) {
310                         Monster &monster(MonsterAt(i));
311                         if (ts.IsBad(i)) {
312                                 monster.SubtractHealth(ts.GetAmount(i));
313                                 if (monster.Health() == 0) {
314                                         expReward += monster.ExpReward();
315                                         goldReward += monster.GoldReward();
316                                 }
317                         }
318                 }
319         } else {
320                 for (int i(0); i < NumHeroes(); ++i) {
321                         Hero &hero(HeroAt(i));
322                         if (ts.IsBad(i)) {
323                                 hero.SubtractHealth(ts.GetAmount(i));
324                         }
325                 }
326         }
327 }
328
329 AttackChoice &BattleState::CurrentAttackAttackChoice() {
330         if (CurrentAttack().isMonster) {
331                 return MonsterAt(CurrentAttack().index).GetAttackChoice();
332         } else {
333                 return HeroAt(CurrentAttack().index).GetAttackChoice();
334         }
335 }
336
337 void BattleState::ClearAllAttacks() {
338         attackCursor = -1;
339         activeHero = -1;
340         for (int i(0); i < NumHeroes(); ++i) {
341                 HeroAt(i).GetAttackChoice() = AttackChoice(this);
342         }
343         for (int i(0); i < MaxMonsters(); ++i) {
344                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
345         }
346         attackOrder.clear();
347 }
348
349
350 void BattleState::HandleEvents(const Input &input) {
351
352 }
353
354 void BattleState::UpdateWorld(float deltaT) {
355
356 }
357
358 void BattleState::Render(SDL_Surface *screen) {
359         assert(screen);
360         Vector<int> offset(CalculateScreenOffset(screen));
361         RenderBackground(screen, offset);
362         RenderMonsters(screen, offset);
363 }
364
365 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
366         assert(screen);
367         // black for now
368         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
369         SDL_Rect destRect;
370         destRect.x = offset.X();
371         destRect.y = offset.Y();
372         destRect.w = background->w;
373         destRect.h = background->h;
374         SDL_BlitSurface(background, 0, screen, &destRect);
375 }
376
377 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
378         assert(screen);
379         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
380                 if (MonsterPositionOccupied(i)) {
381                         if (monsters[i].GetAnimation().Running()) {
382                                 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
383                         } else {
384                                 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
385                         }
386                 }
387         }
388 }
389
390 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
391         assert(screen);
392         for (int i(0); i < numHeroes; ++i) {
393                 if (heroes[i].GetAnimation().Running()) {
394                         heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
395                 } else {
396                         int row(heroes[i].Health() > 0 ? 0 : 2);
397                         heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
398                 }
399         }
400 }
401
402 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
403         assert(screen);
404         int tagHeight(attackTypeMenu.Height());
405         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
406
407         for (int i(0); i < numHeroes; ++i) {
408                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
409         }
410 }
411
412 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
413         assert(screen);
414         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
415         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
416
417         SDL_Rect rect;
418         rect.x = offset.X();
419         rect.y = offset.Y() + Height() - tagHeight;
420         rect.w = Width();
421         rect.h = tagHeight;
422         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
423         rect.y += res->normalFont->CharHeight() / 8;
424         rect.h -= res->normalFont->CharHeight() / 4;
425         SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
426
427         for (int i(0); i < numHeroes; ++i) {
428                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
429         }
430 }
431
432 }