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