]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
ca1dafb0542691556627858c482d755de24005ee
[l2e.git] / src / map / MapState.cpp
1 #include "MapState.h"
2
3 #include "Map.h"
4 #include "Tile.h"
5 #include "TransitionState.h"
6 #include "Trigger.h"
7 #include "../app/Application.h"
8 #include "../app/Input.h"
9 #include "../battle/BattleState.h"
10 #include "../common/GameConfig.h"
11 #include "../common/GameState.h"
12 #include "../graphics/ColorFade.h"
13 #include "../menu/PartyMenu.h"
14
15 #include <algorithm>
16
17 using app::Application;
18 using app::Input;
19 using battle::BattleState;
20 using common::GameConfig;
21 using math::Fixed;
22 using math::Vector;
23 using graphics::ColorFade;
24 using menu::PartyMenu;
25
26 namespace map {
27
28 MapState::MapState(GameConfig *g, Map *map)
29 : game(g)
30 , map(map)
31 , controlled(0)
32 , pushed(0)
33 , lastLock(-1, -1)
34 , camera(100, 100, 0)
35 , walkingSpeed(64)
36 , nextDirection(-1)
37 , afterLock(false)
38 , skipLock(false)
39 , pushing(false)
40 , debug(false) {
41
42 }
43
44
45 void MapState::OnEnterState(SDL_Surface *screen) {
46         camera.Resize(screen->w, screen->h);
47         LoadMap(map);
48 }
49
50 void MapState::OnExitState(SDL_Surface *screen) {
51
52 }
53
54 void MapState::OnResumeState(SDL_Surface *screen) {
55         camera.Resize(screen->w, screen->h);
56 }
57
58 void MapState::OnPauseState(SDL_Surface *screen) {
59
60 }
61
62 void MapState::OnResize(int width, int height) {
63         camera.Resize(width, height);
64 }
65
66
67 void MapState::HandleEvents(const Input &input) {
68         if (input.JustPressed(Input::ACTION_X)) {
69                 Ctrl().PushState(new PartyMenu(game));
70                 return;
71         }
72
73         if (!controlled) return;
74
75         if (input.IsDown(Input::PAD_UP)) {
76                 nextDirection = Entity::ORIENTATION_NORTH;
77         } else if (input.IsDown(Input::PAD_RIGHT)) {
78                 nextDirection = Entity::ORIENTATION_EAST;
79         } else if (input.IsDown(Input::PAD_DOWN)) {
80                 nextDirection = Entity::ORIENTATION_SOUTH;
81         } else if (input.IsDown(Input::PAD_LEFT)) {
82                 nextDirection = Entity::ORIENTATION_WEST;
83         } else {
84                 nextDirection = -1;
85         }
86
87         pushing = input.IsDown(Input::ACTION_A);
88
89         if (input.JustPressed(Input::DEBUG_1)) {
90                 debug = !debug;
91         }
92 }
93
94 void MapState::UpdateWorld(Uint32 deltaT) {
95         if (controlled && controlled->TileLock(map->Tileset()->Size())) {
96                 OnTileLock();
97         }
98         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
99                 (*i)->Update(deltaT);
100         }
101 }
102
103 void MapState::OnTileLock() {
104         if (moveTimer.Running() && !moveTimer.JustHit()) return;
105
106         Vector<int> nowLock(ToInt(controlled->Position()));
107         bool event(false);
108         if (nowLock != lastLock) {
109                 event = OnGridLock();
110                 afterLock = true;
111                 moveTimer.Clear();
112         } else if (moveTimer.JustHit()) {
113                 event = OnGridLock();
114                 afterLock = true;
115         }
116
117         if (event) {
118                 return;
119         }
120
121         const Tile *tile(map->TileAt(nowLock));
122
123         if (nextDirection >= 0) {
124                 if (afterLock) {
125                         bool blocked(CheckBlocking());
126                         OnMove(!blocked);
127                         controlled->SetDirection(Entity::Orientation(nextDirection));
128                         if (!blocked) {
129                                 afterLock = false;
130                                 controlled->SetSpeed(walkingSpeed);
131                                 moveTimer.Clear();
132                                 if (pushed) {
133                                         pushed->SetDirection(Entity::Orientation(nextDirection));
134                                         pushed->SetSpeed(walkingSpeed);
135                                         controlled->SetPushing();
136                                 } else {
137                                         controlled->SetHandsFree();
138                                 }
139                         } else {
140                                 controlled->SetSpeed(0);
141                                 StopFollowers(*controlled);
142                                 if (!moveTimer.Running()) {
143                                         int tileSize((controlled->GetDirection() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
144                                         Fixed<8> walkingInterval(tileSize);
145                                         walkingInterval /= walkingSpeed;
146                                         moveTimer = PhysicsTimers().StartInterval(walkingInterval.Int());
147                                 }
148                                 pushed = 0;
149                         }
150                         if (!controlled->AnimationRunning()) {
151                                 controlled->StartAnimation(*this);
152                         }
153                 }
154         } else {
155                 controlled->SetSpeed(0);
156                 StopFollowers(*controlled);
157                 controlled->StopAnimation();
158                 moveTimer.Clear();
159                 if (pushed) {
160                         pushed->SetSpeed(0);
161                         pushed = 0;
162                 }
163         }
164
165         if (controlled->GetDirection() == Entity::ORIENTATION_SOUTH
166                         && tile && tile->IsLadder()) {
167                 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
168         }
169
170         lastLock = nowLock;
171 }
172
173 bool MapState::CheckBlocking() {
174         if (pushed) {
175                 pushed->SetSpeed(0);
176                 pushed = 0;
177         }
178         const Tile *tile(map->TileAt(ToInt(controlled->Position())));
179         Vector<int> direction;
180         switch (nextDirection) {
181                 case Entity::ORIENTATION_NORTH:
182                         if (tile && tile->BlocksNorth()) {
183                                 return true;
184                         } else {
185                                 direction = Vector<int>(0, -map->Tileset()->Height());
186                         }
187                         break;
188                 case Entity::ORIENTATION_EAST:
189                         if (tile && tile->BlocksEast()) {
190                                 return true;
191                         } else {
192                                 direction = Vector<int>(map->Tileset()->Width(), 0);
193                         }
194                         break;
195                 case Entity::ORIENTATION_SOUTH:
196                         if (tile && tile->BlocksSouth()) {
197                                 return true;
198                         } else {
199                                 direction = Vector<int>(0, map->Tileset()->Height());
200                         }
201                         break;
202                 case Entity::ORIENTATION_WEST:
203                         if (tile && tile->BlocksWest()) {
204                                 return true;
205                         } else {
206                                 direction = Vector<int>(-map->Tileset()->Width(), 0);
207                         }
208                         break;
209                 default:
210                         return false;
211         }
212         Vector<int> nextTilePosition(direction + ToInt(controlled->Position()));
213         Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
214         for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
215                 const Entity &e(**i);
216                 if (map->TileCoordinates(ToInt(e.Position())) != nextTileCoords) continue;
217                 if (!e.Blocking()) continue;
218                 if (!pushing || !e.Pushable()) return true;
219                 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
220                 pushed = *i;
221         }
222         return false;
223 }
224
225 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
226         const Tile *tile(map->TileAt(position));
227         Vector<int> directionVector;
228         switch (direction) {
229                 case Entity::ORIENTATION_NORTH:
230                         if (tile && tile->BlocksNorth()) {
231                                 return true;
232                         } else {
233                                 directionVector = Vector<int>(0, -map->Tileset()->Height());
234                         }
235                         break;
236                 case Entity::ORIENTATION_EAST:
237                         if (tile && tile->BlocksEast()) {
238                                 return true;
239                         } else {
240                                 directionVector = Vector<int>(map->Tileset()->Width(), 0);
241                         }
242                         break;
243                 case Entity::ORIENTATION_SOUTH:
244                         if (tile && tile->BlocksSouth()) {
245                                 return true;
246                         } else {
247                                 directionVector = Vector<int>(0, map->Tileset()->Height());
248                         }
249                         break;
250                 case Entity::ORIENTATION_WEST:
251                         if (tile && tile->BlocksWest()) {
252                                 return true;
253                         } else {
254                                 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
255                         }
256                         break;
257                 default:
258                         return false;
259         }
260         Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
261         for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
262                 const Entity &e(**i);
263                 if (map->TileCoordinates(ToInt(e.Position())) == nextTileCoords && e.Blocking()) {
264                         return true;
265                 }
266         }
267         return false;
268 }
269
270 bool MapState::OnGridLock() {
271         if (skipLock) {
272                 skipLock = false;
273                 return false;
274         } else {
275                 LockEntities();
276                 return CheckMonster() || CheckLockTrigger();
277         }
278 }
279
280 void MapState::LockEntities() {
281         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
282                 if (*i == controlled) {
283                         // don't lock player
284                         continue;
285                 }
286                 (*i)->Position().Lock(map->Tileset()->Size());
287         }
288 }
289
290 bool MapState::CheckMonster() {
291         Vector<int> coords(map->TileCoordinates(ToInt(controlled->Position())));
292         Vector<int> neighbor[4];
293         neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
294         neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
295         neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
296         neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
297
298         for (int i(0); i < 4; ++i) {
299                 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
300                         if ((*e)->Hostile() && map->TileCoordinates(ToInt((*e)->Position())) == neighbor[i]) {
301                                 // TODO: move entity erase to happen after the transition or battle
302                                 entities.erase(e);
303                                 return true;
304                         }
305                 }
306         }
307         return false;
308 }
309
310 void MapState::LoadBattle(Entity &hero, Entity &monster) {
311         SDL_Surface *bg = map->BattleBackgroundAt(ToInt(monster.Position()));
312         BattleState *battleState(new BattleState(game, bg, monster.PartyLayout()));
313         for (int i(0); i < 4; ++i) {
314                 if (game->state->party[i]) {
315                         battleState->AddHero(*game->state->party[i]);
316                 }
317         }
318         if (game->state->capsule) {
319                 battleState->SetCapsule(&game->state->GetCapsule());
320         }
321         for (battle::Monster **m(monster.MonstersBegin()); m != monster.MonstersEnd(); ++m) {
322                 battleState->AddMonster(**m);
323         }
324
325         // TODO: pass turn advantage to battle, see #26
326         Entity::Orientation faceDirection;
327         if (monster.Position().Y() < hero.Position().Y()) {
328                 faceDirection = Entity::ORIENTATION_NORTH;
329         } else if (monster.Position().X() > hero.Position().X()) {
330                 faceDirection = Entity::ORIENTATION_EAST;
331         } else if (monster.Position().Y() > hero.Position().Y()) {
332                 faceDirection = Entity::ORIENTATION_SOUTH;
333         } else {
334                 faceDirection = Entity::ORIENTATION_WEST;
335         }
336         if (hero.GetOrientation() == monster.GetOrientation()
337                         && hero.GetOrientation() == faceDirection) {
338                 // advantage hero
339         } else if (hero.GetOrientation() == monster.GetOrientation()
340                         && hero.GetOrientation() == ((faceDirection + 2) % 4)) {
341                 // advantage monster
342         } else if (((monster.GetOrientation() == faceDirection && (hero.GetOrientation() % 2) != (faceDirection % 2))
343                         || (hero.GetOrientation() == faceDirection && (monster.GetOrientation() % 2) != (faceDirection % 2)))
344                         && rand() % 2) {
345                 // 50% advantage chance hero
346         } else if ((monster.GetOrientation() == (faceDirection + 2) % 4)
347                         && ((hero.GetOrientation() % 2) != (faceDirection % 2))
348                         && rand() % 2) {
349                 // 50% advantage chance monster
350         }
351
352         // TODO: other transition
353         ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
354         fadeIn->SetLeadInTime(500);
355         ColorFade *fadeOut(new ColorFade(this, 0, 500));
356         fadeOut->SetLeadOutTime(500);
357
358         Ctrl().PushState(fadeIn);
359         Ctrl().PushState(battleState);
360         Ctrl().PushState(fadeOut);
361 }
362
363 bool MapState::CheckLockTrigger() {
364         Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
365         if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
366         RunTrigger(*trigger);
367         return true;
368 }
369
370 void MapState::OnMove(bool realMove) {
371         if (CheckMoveTrigger()) {
372                 return;
373         }
374         // TODO: evaluate monster movements
375         if (realMove) {
376                 UpdateFollower(*controlled);
377         } else {
378                 StopFollowers(*controlled);
379         }
380 }
381
382 bool MapState::CheckMoveTrigger() {
383         Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
384         if (!trigger || int(trigger->GetType()) != nextDirection) return false;
385         RunTrigger(*trigger);
386         return true;
387 }
388
389 void MapState::RunTrigger(Trigger &trigger) {
390         if (!trigger.HasScript()) return;
391         runner.Run(*this, trigger.GetScript());
392 }
393
394 void MapState::UpdateFollower(Entity &e) {
395         if (!e.Follower()) return;
396
397         Entity &f(*e.Follower());
398         UpdateFollower(f);
399
400         Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
401         Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
402         Vector<int> direction(coords - fCoords);
403
404         if (direction.Y() < 0) {
405                 f.SetDirection(Entity::ORIENTATION_NORTH);
406                 f.SetSpeed(walkingSpeed);
407                 f.StartAnimation(*this);
408         } else if (direction.X() > 0) {
409                 f.SetDirection(Entity::ORIENTATION_EAST);
410                 f.SetSpeed(walkingSpeed);
411                 f.StartAnimation(*this);
412         } else if (direction.Y() > 0) {
413                 f.SetDirection(Entity::ORIENTATION_SOUTH);
414                 f.SetSpeed(walkingSpeed);
415                 f.StartAnimation(*this);
416         } else if (direction.X() < 0) {
417                 f.SetDirection(Entity::ORIENTATION_WEST);
418                 f.SetSpeed(walkingSpeed);
419                 f.StartAnimation(*this);
420         } else {
421                 f.SetSpeed(0);
422                 f.StopAnimation();
423         }
424 }
425
426 void MapState::StopFollowers(Entity &e) {
427         for (Entity *f(e.Follower()); f; f = f->Follower()) {
428                 f->SetSpeed(0);
429                 f->StopAnimation();
430         }
431 }
432
433
434 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
435         UnloadMap();
436         Vector<int> position(coordinates * map->Tileset()->Size());
437         for (Entity *e(controlled); e; e = e->Follower()) {
438                 e->Position() = position;
439                 e->SetDirection(controlled->GetDirection());
440         }
441         LoadMap(newMap);
442         skipLock = true;
443 }
444
445 void MapState::UnloadMap() {
446         entities.clear();
447 }
448
449 void MapState::LoadMap(Map *m) {
450         map = m;
451         for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
452                 entities.push_back(e);
453                 e->ResetPosition(map->Tileset()->Size());
454         }
455         for (Entity *e(controlled); e; e = e->Follower()) {
456                 entities.push_back(e);
457         }
458 }
459
460
461 void MapState::Render(SDL_Surface *screen) {
462         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
463
464         Vector<int> offset(camera.CalculateOffset());
465         map->Render(screen, offset);
466
467         if (debug) {
468                 map->RenderDebug(screen, offset);
469         }
470
471         std::sort(entities.begin(), entities.end(), ZCompare);
472         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
473                 (*i)->Render(screen, offset);
474         }
475 }
476
477
478 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
479         return lhs->Position().Y() < rhs->Position().Y();
480 }
481
482
483 void MapState::HandleSyscall(common::ScriptRunner &r) {
484         switch (r.IntegerRegister(0)) {
485                 case TRANSITION: {
486                         Ctrl().PushState(new ColorFade(this, 0, 500, true));
487                         Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
488                         ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
489                         fadeOut->SetLeadOutTime(500);
490                         Ctrl().PushState(fadeOut);
491                         break;
492                 }
493         }
494 }
495
496 }