Debian Codegraph @

src/level/LevelStatus.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
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 "LevelStatus.h"
10
11 #include "def-script.h"
12 #include "Path.h"
13 #include "ScriptState.h"
14 #include "ScriptException.h"
15 #include "DemoMode.h"
16
17 extern "C" {
18 #include "lua.h"
19 }
20 #include <stdio.h>
21
22 //-----------------------------------------------------------------
23 inline LevelStatus *
24 getStatus(lua_State *L)
25 {
26 return dynamic_cast<LevelStatus*>(script_getLeader(L));
27 }
28
29
30 //-----------------------------------------------------------------
31 /**
32 * void status_readMoves(saved_moves)
33 */
34 static int
35 script_status_readMoves(lua_State *L) throw()
36 {
37 BEGIN_NOEXCEPTION;
38 const char *saved_moves = luaL_checkstring(L, 1);
39 getStatus(L)->readMoves(saved_moves);
40 END_NOEXCEPTION;
41 return 0;
42 }
43
44 //-----------------------------------------------------------------
45 LevelStatus::LevelStatus()
46 {
47 m_bestMoves = -1;
48 m_complete = false;
49 m_wasRunning = false;
50 m_script->registerFunc("status_readMoves", script_status_readMoves);
51 }
52 //-----------------------------------------------------------------
53 void
54 LevelStatus::readMoves(const std::string &savedMoves)
55 {
56 m_savedMoves = savedMoves;
57 }
58 //-----------------------------------------------------------------
59 void
60 LevelStatus::prepareRun(const std::string &codename, const std::string &poster,
61 int bestMoves, const std::string &bestAuthor)
62 {
63 m_complete = false;
64 m_wasRunning = false;
65 m_codename = codename;
66 m_poster = poster;
67 m_bestMoves = bestMoves;
68 m_bestAuthor = bestAuthor;
69 }
70 //-----------------------------------------------------------------
71 std::string
72 LevelStatus::getSolutionFilename(const std::string &codename)
73 {
74 return "solved/" + codename + ".lua";
75 }
76 //-----------------------------------------------------------------
77 std::string
78 LevelStatus::getSolutionFilename() const
79 {
80 return getSolutionFilename(m_codename);
81 }
82 //-----------------------------------------------------------------
83 /**
84 * Read the best solution.
85 * @return saved_moves or empty string
86 */
87 std::string
88 LevelStatus::readSolvedMoves()
89 {
90 m_savedMoves = "";
91
92 Path oldSolution = Path::dataReadPath(getSolutionFilename());
93 if (oldSolution.exists()) {
94 try {
95 scriptDo("saved_moves=nil");
96 scriptInclude(oldSolution);
97 scriptDo("status_readMoves(saved_moves)");
98 }
99 catch (ScriptException &e) {
100 LOG_WARNING(e.info());
101 }
102 }
103
104 return m_savedMoves;
105 }
106 //-----------------------------------------------------------------
107 /**
108 * Write best solution to the file.
109 * Save moves and models state.
110 */
111 void
112 LevelStatus::writeSolvedMoves(const std::string &moves)
113 {
114 std::string prevMoves = readSolvedMoves();
115
116 if (prevMoves.empty() || moves.size() < prevMoves.size()) {
117 Path file = Path::dataWritePath(getSolutionFilename());
118 FILE *saveFile = fopen(file.getNative().c_str(), "w");
119 if (saveFile) {
120 fputs("\nsaved_moves = '", saveFile);
121 fputs(moves.c_str(), saveFile);
122 fputs("'\n", saveFile);
123 fclose(saveFile);
124 }
125 else {
126 LOG_WARNING(ExInfo("cannot save solution")
127 .addInfo("file", file.getNative())
128 .addInfo("moves", moves));
129 }
130 }
131 }
132 //-----------------------------------------------------------------
133 /**
134 * Returns DemoMode or NULL.
135 */
136 GameState *
137 LevelStatus::createPoster() const
138 {
139 DemoMode *result = NULL;
140 if (!m_poster.empty()) {
141 result = new DemoMode(Path::dataReadPath(m_poster));
142 }
143 return result;
144 }
145 //-----------------------------------------------------------------
146 /**
147 * Compares this player and the best one.
148 * @returns -1 (this is worse), 0 (equals) or 1 (the best)
149 */
150 int
151 LevelStatus::compareToBest()
152 {
153 int moves = readSolvedMoves().size();
154 int result = 1;
155 if (m_bestMoves > 0) {
156 if (m_bestMoves < moves) {
157 result = -1;
158 }
159 else if (m_bestMoves == moves) {
160 result = 0;
161 }
162 }
163 return result;
164 }
165