]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
2ae4bb362082c00a62fb3c0a6f70404cd7d56a92
[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: check for turn advantage, see #26
302                                 // TODO: other transition
303                                 BattleState *battleState(new BattleState(game, map->BattleBackgroundAt(ToInt((*e)->Position())), (*e)->PartyLayout()));
304                                 for (int i(0); i < 4; ++i) {
305                                         if (game->state->party[i]) {
306                                                 battleState->AddHero(*game->state->party[i]);
307                                         }
308                                 }
309                                 if (game->state->capsule) {
310                                         battleState->SetCapsule(&game->state->GetCapsule());
311                                 }
312                                 for (battle::Monster **monster((*e)->MonstersBegin()); monster != (*e)->MonstersEnd(); ++monster) {
313                                         battleState->AddMonster(**monster);
314                                 }
315
316                                 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
317                                 fadeIn->SetLeadInTime(500);
318                                 ColorFade *fadeOut(new ColorFade(this, 0, 500));
319                                 fadeOut->SetLeadOutTime(500);
320
321                                 Ctrl().PushState(fadeIn);
322                                 Ctrl().PushState(battleState);
323                                 Ctrl().PushState(fadeOut);
324                                 // TODO: move entity erase to happen after the transition or battle
325                                 entities.erase(e);
326                                 return true;
327                         }
328                 }
329         }
330         return false;
331 }
332
333 bool MapState::CheckLockTrigger() {
334         Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
335         if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
336         RunTrigger(*trigger);
337         return true;
338 }
339
340 void MapState::OnMove(bool realMove) {
341         if (CheckMoveTrigger()) {
342                 return;
343         }
344         // TODO: evaluate monster movements
345         if (realMove) {
346                 UpdateFollower(*controlled);
347         } else {
348                 StopFollowers(*controlled);
349         }
350 }
351
352 bool MapState::CheckMoveTrigger() {
353         Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
354         if (!trigger || int(trigger->GetType()) != nextDirection) return false;
355         RunTrigger(*trigger);
356         return true;
357 }
358
359 void MapState::RunTrigger(Trigger &trigger) {
360         if (!trigger.HasScript()) return;
361         runner.Run(*this, trigger.GetScript());
362 }
363
364 void MapState::UpdateFollower(Entity &e) {
365         if (!e.Follower()) return;
366
367         Entity &f(*e.Follower());
368         UpdateFollower(f);
369
370         Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
371         Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
372         Vector<int> direction(coords - fCoords);
373
374         if (direction.Y() < 0) {
375                 f.SetDirection(Entity::ORIENTATION_NORTH);
376                 f.SetSpeed(walkingSpeed);
377                 f.StartAnimation(*this);
378         } else if (direction.X() > 0) {
379                 f.SetDirection(Entity::ORIENTATION_EAST);
380                 f.SetSpeed(walkingSpeed);
381                 f.StartAnimation(*this);
382         } else if (direction.Y() > 0) {
383                 f.SetDirection(Entity::ORIENTATION_SOUTH);
384                 f.SetSpeed(walkingSpeed);
385                 f.StartAnimation(*this);
386         } else if (direction.X() < 0) {
387                 f.SetDirection(Entity::ORIENTATION_WEST);
388                 f.SetSpeed(walkingSpeed);
389                 f.StartAnimation(*this);
390         } else {
391                 f.SetSpeed(0);
392                 f.StopAnimation();
393         }
394 }
395
396 void MapState::StopFollowers(Entity &e) {
397         for (Entity *f(e.Follower()); f; f = f->Follower()) {
398                 f->SetSpeed(0);
399                 f->StopAnimation();
400         }
401 }
402
403
404 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
405         UnloadMap();
406         Vector<int> position(coordinates * map->Tileset()->Size());
407         for (Entity *e(controlled); e; e = e->Follower()) {
408                 e->Position() = position;
409                 e->SetDirection(controlled->GetDirection());
410         }
411         LoadMap(newMap);
412         skipLock = true;
413 }
414
415 void MapState::UnloadMap() {
416         entities.clear();
417 }
418
419 void MapState::LoadMap(Map *m) {
420         map = m;
421         for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
422                 entities.push_back(e);
423                 e->ResetPosition(map->Tileset()->Size());
424         }
425         for (Entity *e(controlled); e; e = e->Follower()) {
426                 entities.push_back(e);
427         }
428 }
429
430
431 void MapState::Render(SDL_Surface *screen) {
432         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
433
434         Vector<int> offset(camera.CalculateOffset());
435         map->Render(screen, offset);
436
437         if (debug) {
438                 map->RenderDebug(screen, offset);
439         }
440
441         std::sort(entities.begin(), entities.end(), ZCompare);
442         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
443                 (*i)->Render(screen, offset);
444         }
445 }
446
447
448 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
449         return lhs->Position().Y() < rhs->Position().Y();
450 }
451
452
453 void MapState::HandleSyscall(common::ScriptRunner &r) {
454         switch (r.IntegerRegister(0)) {
455                 case TRANSITION: {
456                         Ctrl().PushState(new ColorFade(this, 0, 500, true));
457                         Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
458                         ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
459                         fadeOut->SetLeadOutTime(500);
460                         Ctrl().PushState(fadeOut);
461                         break;
462                 }
463         }
464 }
465
466 }