]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
moved monster's position to Monster
[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::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] = Point<int>(xOffset, Height() - 2 * tagHeight);
99         heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
100         heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
101         heroTagPositions[3] = Point<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] = Point<int>(xOffset, yOffset);
108         smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
109         smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
110         smallHeroTagPositions[3] = Point<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         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
224         if (ac.GetType() == AttackChoice::DEFEND) return;
225
226         if (CurrentAttack().isMonster) {
227                 const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
228                 // TODO: run monster's attack script
229                 ac.SetType(AttackChoice::SWORD);
230                 ac.Selection().SelectSingle();
231                 ac.Selection().SelectHeroes();
232                 for (int i(0); i < NumHeroes(); ++i) {
233                         if (HeroAt(i).Health() > 0) {
234                                 const Stats &defenderStats(HeroAt(i).GetStats());
235                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
236                                 ac.Selection().SetBad(0, damage);
237                                 break;
238                         }
239                 }
240         } else {
241                 const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
242                 TargetSelection &ts(ac.Selection());
243                 bool hitSome(false);
244                 if (ts.TargetsEnemies()) {
245                         for (int i(0); i < MaxMonsters(); ++i) {
246                                 if (ts.IsSelected(i)) {
247                                         if (MonsterAt(i).Health() > 0) {
248                                                 const Stats &defenderStats(MonsterAt(i).GetStats());
249                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
250                                                 ts.SetBad(i, damage);
251                                                 hitSome = true;
252                                         } else {
253                                                 ts.Unselect(i);
254                                         }
255                                 }
256                         }
257                         if (hitSome) return;
258                         for (int i(0); i < MaxMonsters(); ++i) {
259                                 if (MonsterAt(i).Health() > 0) {
260                                         const Stats &defenderStats(MonsterAt(i).GetStats());
261                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
262                                         ts.SetBad(i, damage);
263                                         break;
264                                 }
265                         }
266                 } else {
267                         for (int i(0); i < NumHeroes(); ++i) {
268                                 if (ts.IsSelected(i)) {
269                                         if (HeroAt(i).Health() > 0) {
270                                                 const Stats &defenderStats(HeroAt(i).GetStats());
271                                                 Uint16 damage(CalculateDamage(attackerStats, defenderStats));
272                                                 ts.SetBad(i, damage);
273                                                 hitSome = true;
274                                         } else {
275                                                 ts.Unselect(i);
276                                         }
277                                 }
278                         }
279                         if (hitSome) return;
280                         for (int i(0); i < NumHeroes(); ++i) {
281                                 if (HeroAt(i).Health() > 0) {
282                                         const Stats &defenderStats(HeroAt(i).GetStats());
283                                         Uint16 damage(CalculateDamage(attackerStats, defenderStats));
284                                         ts.SetBad(i, damage);
285                                         break;
286                                 }
287                         }
288                 }
289         }
290 }
291
292 Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
293         // TODO: find out real formula and add some randomness
294         return attacker.Attack() / 2 - defender.Defense() / 4;
295 }
296
297 void BattleState::ApplyDamage() {
298         if (attackCursor < 0) return;
299         AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
300         TargetSelection &ts(ac.Selection());
301         if (ts.TargetsEnemies()) {
302                 for (int i(0); i < MaxMonsters(); ++i) {
303                         Monster &monster(MonsterAt(i));
304                         if (ts.IsBad(i)) {
305                                 monster.SubtractHealth(ts.GetAmount(i));
306                                 if (monster.Health() == 0) {
307                                         expReward += monster.ExpReward();
308                                         goldReward += monster.GoldReward();
309                                 }
310                         }
311                 }
312         } else {
313                 for (int i(0); i < NumHeroes(); ++i) {
314                         Hero &hero(HeroAt(i));
315                         if (ts.IsBad(i)) {
316                                 hero.SubtractHealth(ts.GetAmount(i));
317                         }
318                 }
319         }
320 }
321
322 AttackChoice &BattleState::CurrentAttackAttackChoice() {
323         if (CurrentAttack().isMonster) {
324                 return MonsterAt(CurrentAttack().index).GetAttackChoice();
325         } else {
326                 return HeroAt(CurrentAttack().index).GetAttackChoice();
327         }
328 }
329
330 void BattleState::ClearAllAttacks() {
331         attackCursor = -1;
332         activeHero = -1;
333         for (int i(0); i < NumHeroes(); ++i) {
334                 HeroAt(i).GetAttackChoice() = AttackChoice(this);
335         }
336         for (int i(0); i < MaxMonsters(); ++i) {
337                 MonsterAt(i).GetAttackChoice() = AttackChoice(this);
338         }
339         attackOrder.clear();
340 }
341
342
343 void BattleState::HandleEvents(const Input &input) {
344
345 }
346
347 void BattleState::UpdateWorld(float deltaT) {
348
349 }
350
351 void BattleState::Render(SDL_Surface *screen) {
352         assert(screen);
353         Vector<int> offset(CalculateScreenOffset(screen));
354         RenderBackground(screen, offset);
355         RenderMonsters(screen, offset);
356 }
357
358 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
359         assert(screen);
360         // black for now
361         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
362         SDL_Rect destRect;
363         destRect.x = offset.X();
364         destRect.y = offset.Y();
365         destRect.w = background->w;
366         destRect.h = background->h;
367         SDL_BlitSurface(background, 0, screen, &destRect);
368 }
369
370 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
371         assert(screen);
372         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
373                 if (MonsterPositionOccupied(i)) {
374                         if (monsters[i].GetAnimation().Running()) {
375                                 monsters[i].GetAnimation().DrawCenter(screen, monsters[i].Position() + offset);
376                         } else {
377                                 monsters[i].Sprite()->DrawCenter(screen, monsters[i].Position() + offset);
378                         }
379                 }
380         }
381 }
382
383 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
384         assert(screen);
385         for (int i(0); i < numHeroes; ++i) {
386                 if (heroes[i].GetAnimation().Running()) {
387                         heroes[i].GetAnimation().DrawCenter(screen, heroes[i].Position() + offset);
388                 } else {
389                         int row(heroes[i].Health() > 0 ? 0 : 2);
390                         heroes[i].Sprite()->DrawCenter(screen, heroes[i].Position() + offset, 1, row);
391                 }
392         }
393 }
394
395 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
396         assert(screen);
397         int tagHeight(attackTypeMenu.Height());
398         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
399
400         for (int i(0); i < numHeroes; ++i) {
401                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
402         }
403 }
404
405 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
406         assert(screen);
407         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
408         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
409
410         SDL_Rect rect;
411         rect.x = offset.X();
412         rect.y = offset.Y() + Height() - tagHeight;
413         rect.w = Width();
414         rect.h = tagHeight;
415         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
416         rect.y += res->normalFont->CharHeight() / 8;
417         rect.h -= res->normalFont->CharHeight() / 4;
418         SDL_FillRect(screen, &rect, res->heroesBgColor);
419
420         for (int i(0); i < numHeroes; ++i) {
421                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
422         }
423 }
424
425 }