]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
apply damage indicated by attack selection
[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 <stdexcept>
25
26 using app::Application;
27 using app::Input;
28 using common::Inventory;
29 using common::Item;
30 using common::Spell;
31 using geometry::Point;
32 using geometry::Vector;
33 using graphics::Menu;
34
35 using std::vector;
36
37 namespace battle {
38
39 void BattleState::AddMonster(const Monster &m) {
40         if (monsters.size() >= monstersLayout->NumPositions()) {
41                 throw std::overflow_error("too many monsters for layout");
42         }
43         monsters.push_back(m);
44 }
45
46 void BattleState::AddHero(const Hero &h) {
47         if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) {
48                 throw std::overflow_error("too many heroes for layout");
49         }
50         heroes[numHeroes] = h;
51         ++numHeroes;
52 }
53
54 void BattleState::NextHero() {
55         ++activeHero;
56         while (activeHero < numHeroes && heroes[activeHero].Health() == 0) {
57                 ++activeHero;
58         }
59 }
60
61 void BattleState::SwapHeroes(int lhs, int rhs) {
62         if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return;
63         std::swap(heroes[lhs], heroes[rhs]);
64 }
65
66
67 void BattleState::Resize(int w, int h) {
68
69 }
70
71
72 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
73         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
74         heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
75         for (int i(0); i < 4; ++i) {
76                 spellMenus[i] = res->spellMenuPrototype;
77                 LoadSpellMenu(i);
78                 ikariMenus[i] = res->ikariMenuPrototype;
79                 LoadIkariMenu(i);
80                 heroTags[i] = HeroTag(this, i);
81                 smallHeroTags[i] = SmallHeroTag(this, i);
82         }
83
84         int tagHeight(attackTypeMenu.Height());
85         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
86         int xOffset((Width() - 2 * tagWidth) / 2);
87         heroTagPositions[0] = Point<int>(xOffset, Height() - 2 * tagHeight);
88         heroTagPositions[1] = Point<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
89         heroTagPositions[2] = Point<int>(xOffset, Height() - tagHeight);
90         heroTagPositions[3] = Point<int>(xOffset + tagWidth, Height() - tagHeight);
91
92         tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
93         tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
94         xOffset = (Width() - 4 * tagWidth) / 2;
95         int yOffset(Height() - tagHeight);
96         smallHeroTagPositions[0] = Point<int>(xOffset, yOffset);
97         smallHeroTagPositions[1] = Point<int>(xOffset + 2 * tagWidth, yOffset);
98         smallHeroTagPositions[2] = Point<int>(xOffset + tagWidth, yOffset);
99         smallHeroTagPositions[3] = Point<int>(xOffset + 3 * tagWidth, yOffset);
100
101         itemMenu = res->itemMenuPrototype;
102         LoadInventory();
103 }
104
105 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
106         spellMenus[index].Clear();
107         spellMenus[index].Reserve(HeroAt(index).Spells().size());
108         for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
109                 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
110                 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
111         }
112 }
113
114 void BattleState::LoadIkariMenu(vector<Hero>::size_type index) {
115         ikariMenus[index].Clear();
116         ikariMenus[index].Reserve(6);
117
118         if (HeroAt(index).HasWeapon()) {
119                 ikariMenus[index].Add(
120                                 HeroAt(index).Weapon()->Name(),
121                                 HeroAt(index).Weapon(),
122                                 HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(),
123                                 res->weaponMenuIcon,
124                                 0,
125                                 HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : "");
126         } else {
127                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon);
128         }
129
130         if (HeroAt(index).HasArmor()) {
131                 ikariMenus[index].Add(
132                                 HeroAt(index).Armor()->Name(),
133                                 HeroAt(index).Armor(),
134                                 HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(),
135                                 res->armorMenuIcon,
136                                 0,
137                                 HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : "");
138         } else {
139                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon);
140         }
141
142         if (HeroAt(index).HasShield()) {
143                 ikariMenus[index].Add(
144                                 HeroAt(index).Shield()->Name(),
145                                 HeroAt(index).Shield(),
146                                 HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(),
147                                 res->shieldMenuIcon,
148                                 0,
149                                 HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : "");
150         } else {
151                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon);
152         }
153
154         if (HeroAt(index).HasHelmet()) {
155                 ikariMenus[index].Add(
156                                 HeroAt(index).Helmet()->Name(),
157                                 HeroAt(index).Helmet(),
158                                 HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(),
159                                 res->helmetMenuIcon,
160                                 0,
161                                 HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : "");
162         } else {
163                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon);
164         }
165
166         if (HeroAt(index).HasRing()) {
167                 ikariMenus[index].Add(
168                                 HeroAt(index).Ring()->Name(),
169                                 HeroAt(index).Ring(),
170                                 HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(),
171                                 res->ringMenuIcon,
172                                 0,
173                                 HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : "");
174         } else {
175                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon);
176         }
177
178         if (HeroAt(index).HasJewel()) {
179                 ikariMenus[index].Add(
180                                 HeroAt(index).Jewel()->Name(),
181                                 HeroAt(index).Jewel(),
182                                 HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(),
183                                 res->jewelMenuIcon,
184                                 0,
185                                 HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : "");
186         } else {
187                 ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon);
188         }
189 }
190
191 void BattleState::LoadInventory() {
192         const Inventory &inv(*res->inventory);
193         itemMenu.Clear();
194         itemMenu.Reserve(inv.MaxItems());
195         for (int i(0); i < inv.MaxItems(); ++i) {
196                 const Item *item(inv.ItemAt(i));
197                 if (item) {
198                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
199                 } else {
200                         itemMenu.AddEmptyEntry();
201                 }
202         }
203         ClearAllAttacks();
204 }
205
206 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
207
208 }
209
210 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
211         // TODO: check for victory or defeat
212         if (ranAway) {
213                 ctrl.PopState(); // quit the battle scene
214                 return;
215         }
216         // TODO: this should not push a state while quitting
217         if (AttackSelectionDone()) {
218                 ctrl.PushState(new PerformAttacks(this));
219         } else {
220                 ctrl.PushState(new SelectMoveAction(this));
221         }
222 }
223
224 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
225
226 }
227
228
229 class OrderCompare {
230         public:
231                 OrderCompare(BattleState *battle) : battle(battle) { }
232                 bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
233                         int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).Agility() : battle->HeroAt(lhs.index).Agility());
234                         int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).Agility() : battle->HeroAt(rhs.index).Agility());
235                         return lagl > ragl;
236                 }
237         private:
238                 BattleState *battle;
239 };
240
241 void BattleState::CalculateAttackOrder() {
242         attackOrder.reserve(monsters.size() + NumHeroes());
243         for (int i(0); i < numHeroes; ++i) {
244                 attackOrder.push_back(Order(i, false));
245         }
246         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
247                 attackOrder.push_back(Order(i, true));
248         }
249         std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this));
250
251         monsterAttacks.resize(monsters.size(), AttackChoice(this));
252 }
253
254 void BattleState::NextAttack() {
255         ++attackCursor;
256         while (attackCursor < int(attackOrder.size())) {
257                 if (attackOrder[attackCursor].isMonster) {
258                         if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break;
259                 } else {
260                         if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break;
261                 }
262                 ++attackCursor;
263         }
264 }
265
266 void BattleState::CalculateDamage() {
267         if (CurrentAttack().isMonster) {
268                 // TODO: run monster's attack script
269                 monsterAttacks[CurrentAttack().index].SetType(AttackChoice::SWORD);
270                 monsterAttacks[CurrentAttack().index].Selection().SelectSingle();
271                 monsterAttacks[CurrentAttack().index].Selection().SelectHeroes();
272                 monsterAttacks[CurrentAttack().index].Selection().SetBad(0, 15);
273         } else {
274                 TargetSelection &ts(AttackChoiceAt(CurrentAttack().index).Selection());
275                 if (ts.TargetsEnemies()) {
276                         for (int i(0); i < MaxMonsters(); ++i) {
277                                 if (ts.IsSelected(i) && MonsterAt(i).Health() > 0) {
278                                         ts.SetBad(i, 15);
279                                 }
280                         }
281                 } else {
282                         for (int i(0); i < NumHeroes(); ++i) {
283                                 if (ts.IsSelected(i) && HeroAt(i).Health() > 0) {
284                                         ts.SetBad(i, 15);
285                                 }
286                         }
287                 }
288         }
289 }
290
291 void BattleState::ApplyDamage() {
292         if (attackCursor < 0) return;
293         AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
294         TargetSelection &ts(ac.Selection());
295         if (ts.TargetsEnemies()) {
296                 for (int i(0); i < MaxMonsters(); ++i) {
297                         if (ts.IsBad(i)) {
298                                 MonsterAt(i).SubtractHealth(ts.GetAmount(i));
299                         }
300                 }
301         } else {
302                 for (int i(0); i < NumHeroes(); ++i) {
303                         if (ts.IsSelected(i) && MonsterAt(i).Health() > 0) {
304                                 HeroAt(i).SubtractHealth(ts.GetAmount(i));
305                         }
306                 }
307         }
308 }
309
310 void BattleState::ClearAllAttacks() {
311         attackCursor = -1;
312         activeHero = -1;
313         for (int i(0); i < numHeroes; ++i) {
314                 attackChoices[i] = AttackChoice(this);
315         }
316         attackOrder.clear();
317         monsterAttacks.clear();
318 }
319
320
321 void BattleState::HandleEvents(const Input &input) {
322
323 }
324
325 void BattleState::UpdateWorld(float deltaT) {
326
327 }
328
329 void BattleState::Render(SDL_Surface *screen) {
330         Vector<int> offset(CalculateScreenOffset(screen));
331         RenderBackground(screen, offset);
332         RenderMonsters(screen, offset);
333 }
334
335 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
336         // black for now
337         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
338         SDL_Rect destRect;
339         destRect.x = offset.X();
340         destRect.y = offset.Y();
341         destRect.w = background->w;
342         destRect.h = background->h;
343         SDL_BlitSurface(background, 0, screen, &destRect);
344 }
345
346 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
347         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
348                 monsters[i].Sprite()->DrawCenter(screen, monsterPositions[i] + offset);
349         }
350 }
351
352 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
353         for (int i(0); i < numHeroes; ++i) {
354                 if (heroes[i].AttackAnimation() && heroes[i].AttackAnimation()->Running()) {
355                         heroes[i].AttackAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
356                 } else if (heroes[i].SpellAnimation() && heroes[i].SpellAnimation()->Running()) {
357                         heroes[i].SpellAnimation()->DrawCenter(screen, heroesPositions[i] + offset);
358                 } else {
359                         int row(heroes[i].Health() > 0 ? 0 : 2);
360                         heroes[i].Sprite()->DrawCenter(screen, heroesPositions[i] + offset, 1, row);
361                 }
362         }
363 }
364
365 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
366         int tagHeight(attackTypeMenu.Height());
367         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
368
369         for (int i(0); i < numHeroes; ++i) {
370                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
371         }
372 }
373
374 void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
375         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
376         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
377
378         SDL_Rect rect;
379         rect.x = offset.X();
380         rect.y = offset.Y() + Height() - tagHeight;
381         rect.w = Width();
382         rect.h = tagHeight;
383         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
384         rect.y += res->normalFont->CharHeight() / 8;
385         rect.h -= res->normalFont->CharHeight() / 4;
386         SDL_FillRect(screen, &rect, res->heroesBgColor);
387
388         for (int i(0); i < numHeroes; ++i) {
389                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
390         }
391 }
392
393 }