]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
switched geometric scalars from floating to fixed
[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         if (nextDirection >= 0) {
122                 if (afterLock) {
123                         bool blocked(CheckBlocking());
124                         OnMove(!blocked);
125                         controlled->SetOrientation(Entity::Orientation(nextDirection));
126                         if (!blocked) {
127                                 afterLock = false;
128                                 controlled->SetSpeed(walkingSpeed);
129                                 moveTimer.Clear();
130                                 if (pushed) {
131                                         pushed->SetOrientation(Entity::Orientation(nextDirection));
132                                         pushed->SetSpeed(walkingSpeed);
133                                         controlled->SetPushing();
134                                 } else {
135                                         controlled->SetHandsFree();
136                                 }
137                         } else {
138                                 controlled->SetSpeed(0);
139                                 StopFollowers(*controlled);
140                                 if (!moveTimer.Running()) {
141                                         int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
142                                         moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed.Int());
143                                 }
144                                 pushed = 0;
145                         }
146                         if (!controlled->AnimationRunning()) {
147                                 controlled->StartAnimation(*this);
148                         }
149                 }
150         } else {
151                 controlled->SetSpeed(0);
152                 StopFollowers(*controlled);
153                 controlled->StopAnimation();
154                 moveTimer.Clear();
155                 if (pushed) {
156                         pushed->SetSpeed(0);
157                         pushed = 0;
158                 }
159         }
160
161         lastLock = nowLock;
162 }
163
164 bool MapState::CheckBlocking() {
165         if (pushed) {
166                 pushed->SetSpeed(0);
167                 pushed = 0;
168         }
169         const Tile *tile(map->TileAt(ToInt(controlled->Position())));
170         Vector<int> direction;
171         switch (nextDirection) {
172                 case Entity::ORIENTATION_NORTH:
173                         if (tile && tile->BlocksNorth()) {
174                                 return true;
175                         } else {
176                                 direction = Vector<int>(0, -map->Tileset()->Height());
177                         }
178                         break;
179                 case Entity::ORIENTATION_EAST:
180                         if (tile && tile->BlocksEast()) {
181                                 return true;
182                         } else {
183                                 direction = Vector<int>(map->Tileset()->Width(), 0);
184                         }
185                         break;
186                 case Entity::ORIENTATION_SOUTH:
187                         if (tile && tile->BlocksSouth()) {
188                                 return true;
189                         } else {
190                                 direction = Vector<int>(0, map->Tileset()->Height());
191                         }
192                         break;
193                 case Entity::ORIENTATION_WEST:
194                         if (tile && tile->BlocksWest()) {
195                                 return true;
196                         } else {
197                                 direction = Vector<int>(-map->Tileset()->Width(), 0);
198                         }
199                         break;
200                 default:
201                         return false;
202         }
203         Vector<int> nextTilePosition(direction + ToInt(controlled->Position()));
204         Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
205         for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
206                 const Entity &e(**i);
207                 if (map->TileCoordinates(ToInt(e.Position())) != nextTileCoords) continue;
208                 if (!e.Blocking()) continue;
209                 if (!pushing || !e.Pushable()) return true;
210                 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
211                 pushed = *i;
212         }
213         return false;
214 }
215
216 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
217         const Tile *tile(map->TileAt(position));
218         Vector<int> directionVector;
219         switch (direction) {
220                 case Entity::ORIENTATION_NORTH:
221                         if (tile && tile->BlocksNorth()) {
222                                 return true;
223                         } else {
224                                 directionVector = Vector<int>(0, -map->Tileset()->Height());
225                         }
226                         break;
227                 case Entity::ORIENTATION_EAST:
228                         if (tile && tile->BlocksEast()) {
229                                 return true;
230                         } else {
231                                 directionVector = Vector<int>(map->Tileset()->Width(), 0);
232                         }
233                         break;
234                 case Entity::ORIENTATION_SOUTH:
235                         if (tile && tile->BlocksSouth()) {
236                                 return true;
237                         } else {
238                                 directionVector = Vector<int>(0, map->Tileset()->Height());
239                         }
240                         break;
241                 case Entity::ORIENTATION_WEST:
242                         if (tile && tile->BlocksWest()) {
243                                 return true;
244                         } else {
245                                 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
246                         }
247                         break;
248                 default:
249                         return false;
250         }
251         Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
252         for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
253                 const Entity &e(**i);
254                 if (map->TileCoordinates(ToInt(e.Position())) == nextTileCoords && e.Blocking()) {
255                         return true;
256                 }
257         }
258         return false;
259 }
260
261 bool MapState::OnGridLock() {
262         if (skipLock) {
263                 skipLock = false;
264                 return false;
265         } else {
266                 LockEntities();
267                 return CheckMonster() || CheckLockTrigger();
268         }
269 }
270
271 void MapState::LockEntities() {
272         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
273                 if (*i == controlled) {
274                         // don't lock player
275                         continue;
276                 }
277                 (*i)->Position().Lock(map->Tileset()->Size());
278         }
279 }
280
281 bool MapState::CheckMonster() {
282         Vector<int> coords(map->TileCoordinates(ToInt(controlled->Position())));
283         Vector<int> neighbor[4];
284         neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
285         neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
286         neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
287         neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
288
289         for (int i(0); i < 4; ++i) {
290                 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
291                         if ((*e)->Hostile() && map->TileCoordinates(ToInt((*e)->Position())) == neighbor[i]) {
292                                 // TODO: check for turn advantage, see #26
293                                 // TODO: other transition
294                                 BattleState *battleState(new BattleState(game, map->BattleBackgroundAt(ToInt((*e)->Position())), (*e)->PartyLayout()));
295                                 for (int i(0); i < 4; ++i) {
296                                         if (game->state->party[i]) {
297                                                 battleState->AddHero(*game->state->party[i]);
298                                         }
299                                 }
300                                 if (game->state->capsule) {
301                                         battleState->SetCapsule(&game->state->GetCapsule());
302                                 }
303                                 for (battle::Monster **monster((*e)->MonstersBegin()); monster != (*e)->MonstersEnd(); ++monster) {
304                                         battleState->AddMonster(**monster);
305                                 }
306
307                                 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
308                                 fadeIn->SetLeadInTime(500);
309                                 ColorFade *fadeOut(new ColorFade(this, 0, 500));
310                                 fadeOut->SetLeadOutTime(500);
311
312                                 Ctrl().PushState(fadeIn);
313                                 Ctrl().PushState(battleState);
314                                 Ctrl().PushState(fadeOut);
315                                 // TODO: move entity erase to happen after the transition or battle
316                                 entities.erase(e);
317                                 return true;
318                         }
319                 }
320         }
321         return false;
322 }
323
324 bool MapState::CheckLockTrigger() {
325         Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
326         if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
327         RunTrigger(*trigger);
328         return true;
329 }
330
331 void MapState::OnMove(bool realMove) {
332         if (CheckMoveTrigger()) {
333                 return;
334         }
335         // TODO: evaluate monster movements
336         if (realMove) {
337                 UpdateFollower(*controlled);
338         } else {
339                 StopFollowers(*controlled);
340         }
341 }
342
343 bool MapState::CheckMoveTrigger() {
344         Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
345         if (!trigger || int(trigger->GetType()) != nextDirection) return false;
346         RunTrigger(*trigger);
347         return true;
348 }
349
350 void MapState::RunTrigger(Trigger &trigger) {
351         if (!trigger.HasScript()) return;
352         runner.Run(*this, trigger.GetScript());
353 }
354
355 void MapState::UpdateFollower(Entity &e) {
356         if (!e.Follower()) return;
357
358         Entity &f(*e.Follower());
359         UpdateFollower(f);
360
361         Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
362         Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
363         Vector<int> direction(coords - fCoords);
364
365         if (direction.Y() < 0) {
366                 f.SetOrientation(Entity::ORIENTATION_NORTH);
367                 f.SetSpeed(walkingSpeed);
368                 f.StartAnimation(*this);
369         } else if (direction.X() > 0) {
370                 f.SetOrientation(Entity::ORIENTATION_EAST);
371                 f.SetSpeed(walkingSpeed);
372                 f.StartAnimation(*this);
373         } else if (direction.Y() > 0) {
374                 f.SetOrientation(Entity::ORIENTATION_SOUTH);
375                 f.SetSpeed(walkingSpeed);
376                 f.StartAnimation(*this);
377         } else if (direction.X() < 0) {
378                 f.SetOrientation(Entity::ORIENTATION_WEST);
379                 f.SetSpeed(walkingSpeed);
380                 f.StartAnimation(*this);
381         } else {
382                 f.SetSpeed(0);
383                 f.StopAnimation();
384         }
385 }
386
387 void MapState::StopFollowers(Entity &e) {
388         for (Entity *f(e.Follower()); f; f = f->Follower()) {
389                 f->SetSpeed(0);
390                 f->StopAnimation();
391         }
392 }
393
394
395 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
396         UnloadMap();
397         Vector<int> position(coordinates * map->Tileset()->Size());
398         for (Entity *e(controlled); e; e = e->Follower()) {
399                 e->Position() = position;
400                 e->SetOrientation(controlled->GetOrientation());
401         }
402         LoadMap(newMap);
403         skipLock = true;
404 }
405
406 void MapState::UnloadMap() {
407         entities.clear();
408 }
409
410 void MapState::LoadMap(Map *m) {
411         map = m;
412         for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
413                 entities.push_back(e);
414                 e->ResetPosition(map->Tileset()->Size());
415         }
416         for (Entity *e(controlled); e; e = e->Follower()) {
417                 entities.push_back(e);
418         }
419 }
420
421
422 void MapState::Render(SDL_Surface *screen) {
423         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
424
425         Vector<int> offset(camera.CalculateOffset());
426         map->Render(screen, offset);
427
428         if (debug) {
429                 map->RenderDebug(screen, offset);
430         }
431
432         std::sort(entities.begin(), entities.end(), ZCompare);
433         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
434                 (*i)->Render(screen, offset);
435         }
436 }
437
438
439 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
440         return lhs->Position().Y() < rhs->Position().Y();
441 }
442
443
444 void MapState::HandleSyscall(common::ScriptRunner &r) {
445         switch (r.IntegerRegister(0)) {
446                 case TRANSITION: {
447                         Ctrl().PushState(new ColorFade(this, 0, 500, true));
448                         Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
449                         ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
450                         fadeOut->SetLeadOutTime(500);
451                         Ctrl().PushState(fadeOut);
452                         break;
453                 }
454         }
455 }
456
457 }