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