00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "level-script.h"
00010
00011 #include "Log.h"
00012 #include "Path.h"
00013 #include "V2.h"
00014 #include "Level.h"
00015 #include "LevelScript.h"
00016 #include "Room.h"
00017 #include "Picture.h"
00018
00019 #include "def-script.h"
00020
00021
00022 inline LevelScript *
00023 getLevelScript(lua_State *L)
00024 {
00025 return dynamic_cast<LevelScript*>(script_getLeader(L));
00026 }
00027
00028 inline Level *
00029 getLevel(lua_State *L)
00030 {
00031 return getLevelScript(L)->level();
00032 }
00033
00034
00035
00036
00037
00038 int
00039 script_level_save(lua_State *L) throw()
00040 {
00041 BEGIN_NOEXCEPTION;
00042 const char *serialized = luaL_checkstring(L, 1);
00043 getLevel(L)->saveGame(serialized);
00044 END_NOEXCEPTION;
00045 return 0;
00046 }
00047
00048
00049
00050
00051 int
00052 script_level_load(lua_State *L) throw()
00053 {
00054 BEGIN_NOEXCEPTION;
00055 const char *moves = luaL_checkstring(L, 1);
00056 getLevel(L)->loadGame(moves);
00057 END_NOEXCEPTION;
00058 return 0;
00059 }
00060
00061
00062
00063
00064
00065 int
00066 script_level_action_move(lua_State *L) throw()
00067 {
00068 BEGIN_NOEXCEPTION;
00069 size_t size;
00070 const char *symbol = luaL_checklstring(L, 1, &size);
00071 if (size != 1) {
00072 ExInfo error = ExInfo("bad symbol length")
00073 .addInfo("length", size)
00074 .addInfo("symbol", symbol);
00075 LOG_WARNING(error);
00076 luaL_error(L, error.what());
00077 }
00078
00079 bool sucess = getLevel(L)->action_move(symbol[0]);
00080 lua_pushboolean(L, sucess);
00081 END_NOEXCEPTION;
00082
00083 return 1;
00084 }
00085
00086
00087
00088
00089 int
00090 script_level_action_save(lua_State *L) throw()
00091 {
00092 BEGIN_NOEXCEPTION;
00093 bool sucess = getLevel(L)->action_save();
00094 lua_pushboolean(L, sucess);
00095 END_NOEXCEPTION;
00096
00097 return 1;
00098 }
00099
00100
00101
00102
00103 int
00104 script_level_action_load(lua_State *L) throw()
00105 {
00106 BEGIN_NOEXCEPTION;
00107 bool sucess = getLevel(L)->action_load();
00108 lua_pushboolean(L, sucess);
00109 END_NOEXCEPTION;
00110
00111 return 1;
00112 }
00113
00114
00115
00116
00117 int
00118 script_level_action_restart(lua_State *L) throw()
00119 {
00120 BEGIN_NOEXCEPTION;
00121 bool sucess = getLevel(L)->action_restart();
00122 lua_pushboolean(L, sucess);
00123 END_NOEXCEPTION;
00124
00125 return 1;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135 int
00136 script_level_createRoom(lua_State *L) throw()
00137 {
00138 BEGIN_NOEXCEPTION;
00139 int w = luaL_checkint(L, 1);
00140 int h = luaL_checkint(L, 2);
00141 const char *picture = luaL_checkstring(L, 3);
00142
00143 getLevel(L)->createRoom(w, h, Path::dataReadPath(picture));
00144 END_NOEXCEPTION;
00145 return 0;
00146 }
00147
00148
00149
00150
00151
00152
00153 int
00154 script_level_getRestartCounter(lua_State *L) throw()
00155 {
00156 BEGIN_NOEXCEPTION;
00157 int counter = getLevel(L)->getRestartCounter();
00158 lua_pushnumber(L, counter);
00159 END_NOEXCEPTION;
00160
00161 return 1;
00162 }
00163
00164
00165
00166
00167
00168 int
00169 script_level_getDepth(lua_State *L) throw()
00170 {
00171 BEGIN_NOEXCEPTION;
00172 int depth = getLevel(L)->getDepth();
00173 lua_pushnumber(L, depth);
00174 END_NOEXCEPTION;
00175
00176 return 1;
00177 }
00178
00179
00180
00181
00182
00183 int
00184 script_level_isNewRound(lua_State *L) throw()
00185 {
00186 BEGIN_NOEXCEPTION;
00187 bool newRound = getLevel(L)->isNewRound();
00188 lua_pushboolean(L, newRound);
00189 END_NOEXCEPTION;
00190
00191 return 1;
00192 }
00193
00194
00195
00196
00197
00198 int
00199 script_level_isSolved(lua_State *L) throw()
00200 {
00201 BEGIN_NOEXCEPTION;
00202 bool solved = getLevelScript(L)->room()->isSolved();
00203 lua_pushboolean(L, solved);
00204 END_NOEXCEPTION;
00205
00206 return 1;
00207 }
00208
00209
00210
00211
00212 int
00213 script_level_newDemo(lua_State *L) throw()
00214 {
00215 BEGIN_NOEXCEPTION;
00216 const char *demofile = luaL_checkstring(L, 1);
00217
00218 getLevel(L)->newDemo(Path::dataReadPath(demofile));
00219 END_NOEXCEPTION;
00220 return 0;
00221 }
00222
00223
00224
00225
00226 int
00227 script_level_planShow(lua_State *L) throw()
00228 {
00229 BEGIN_NOEXCEPTION;
00230 luaL_checktype(L, 1, LUA_TFUNCTION);
00231 int funcRef = luaL_ref(L, LUA_REGISTRYINDEX);
00232
00233 Command *command = getLevelScript(L)->createCommand(funcRef);
00234 getLevel(L)->planShow(command);
00235 END_NOEXCEPTION;
00236 return 0;
00237 }
00238
00239
00240
00241
00242 int
00243 script_level_isShowing(lua_State *L) throw()
00244 {
00245 BEGIN_NOEXCEPTION;
00246 bool showing = getLevel(L)->isShowing();
00247 lua_pushboolean(L, showing);
00248 END_NOEXCEPTION;
00249
00250 return 1;
00251 }
00252