1 /*
2  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include "LevelLoading.h"
10 
11 #include "RoomAccess.h"
12 #include "Room.h"
13 #include "LoadException.h"
14 #include "minmax.h"
15 
16 //-----------------------------------------------------------------
LevelLoading(RoomAccess * access)17 LevelLoading::LevelLoading(RoomAccess *access)
18 {
19     m_access = access;
20     reset();
21 }
22 //-----------------------------------------------------------------
23     void
reset()24 LevelLoading::reset()
25 {
26     m_paused = false;
27     m_replayMode = false;
28     m_loadSpeed = 1;
29     m_loadedMoves = "";
30 }
31 //-----------------------------------------------------------------
32 bool
isLoading() const33 LevelLoading::isLoading() const
34 {
35     return !m_loadedMoves.empty() || m_replayMode;
36 }
37 //-----------------------------------------------------------------
38 /**
39  * Start loading mode.
40  * @param moves saved moves to load
41  */
42     void
loadGame(const std::string & moves)43 LevelLoading::loadGame(const std::string &moves)
44 {
45     m_loadedMoves = moves;
46     m_loadSpeed = min(50, max(5, m_loadedMoves.size() / 150));
47 }
48 //-----------------------------------------------------------------
49 /**
50  * Start replay mode.
51  * @param moves saved moves to load
52  */
53     void
loadReplay(const std::string & moves)54 LevelLoading::loadReplay(const std::string &moves)
55 {
56     m_loadedMoves = moves;
57     m_loadSpeed = 1;
58     m_replayMode = true;
59 }
60 //-----------------------------------------------------------------
61 /**
62  * Load a few moves.
63  * @throws LoadException for bad load
64  */
65     void
nextLoadAction()66 LevelLoading::nextLoadAction()
67 {
68     if (m_paused) {
69         return;
70     }
71 
72     if (m_loadedMoves.empty()) {
73         m_access->room()->beginFall(false);
74         m_access->room()->finishRound(false);
75     }
76     else {
77         for (int i = 0; i < m_loadSpeed
78                 && !m_loadedMoves.empty(); ++i)
79         {
80             try {
81                 char symbol = m_loadedMoves[0];
82                 m_loadedMoves.erase(0, 1);
83 
84                 m_access->room()->loadMove(symbol);
85             }
86             catch (LoadException &e) {
87                 throw LoadException(ExInfo(e.info())
88                         .addInfo("remain", m_loadedMoves));
89             }
90         }
91     }
92 }
93 
94