| 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 "level-script.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "Path.h" |
| 13 |
#include "V2.h" |
| 14 |
#include "Level.h" |
| 15 |
#include "LevelScript.h" |
| 16 |
#include "Room.h" |
| 17 |
#include "Picture.h" |
| 18 |
|
| 19 |
#include "def-script.h" |
| 20 |
|
| 21 |
//----------------------------------------------------------------- |
| 22 |
inline LevelScript * |
| 23 |
getLevelScript(lua_State *L) |
| 24 |
{ |
| 25 |
return dynamic_cast<LevelScript*>(script_getLeader(L)); |
| 26 |
} |
| 27 |
//----------------------------------------------------------------- |
| 28 |
inline Level * |
| 29 |
getLevel(lua_State *L) |
| 30 |
{ |
| 31 |
return getLevelScript(L)->level(); |
| 32 |
} |
| 33 |
|
| 34 |
//----------------------------------------------------------------- |
| 35 |
/** |
| 36 |
* void level_save(serialized) |
| 37 |
*/ |
| 38 |
int |
| 39 |
script_level_save(lua_State *L) throw() |
| 40 |
{ |
| 41 |
BEGIN_NOEXCEPTION; |
| 42 |
const char *serialized = luaL_checkstring(L, 1); |
| 43 |
getLevel(L)->saveGame(serialized); |
| 44 |
END_NOEXCEPTION; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
//----------------------------------------------------------------- |
| 48 |
/** |
| 49 |
* void level_load(moves) |
| 50 |
*/ |
| 51 |
int |
| 52 |
script_level_load(lua_State *L) throw() |
| 53 |
{ |
| 54 |
BEGIN_NOEXCEPTION; |
| 55 |
const char *moves = luaL_checkstring(L, 1); |
| 56 |
getLevel(L)->loadGame(moves); |
| 57 |
END_NOEXCEPTION; |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
//----------------------------------------------------------------- |
| 62 |
/** |
| 63 |
* bool level_action_move(symbol) |
| 64 |
*/ |
| 65 |
int |
| 66 |
script_level_action_move(lua_State *L) throw() |
| 67 |
{ |
| 68 |
BEGIN_NOEXCEPTION; |
| 69 |
size_t size; |
| 70 |
const char *symbol = luaL_checklstring(L, 1, &size); |
| 71 |
if (size != 1) { |
| 72 |
ExInfo error = ExInfo("bad symbol length") |
| 73 |
.addInfo("length", size) |
| 74 |
.addInfo("symbol", symbol); |
| 75 |
LOG_WARNING(error); |
| 76 |
luaL_error(L, error.what()); |
| 77 |
} |
| 78 |
|
| 79 |
bool sucess = getLevel(L)->action_move(symbol[0]); |
| 80 |
lua_pushboolean(L, sucess); |
| 81 |
END_NOEXCEPTION; |
| 82 |
//NOTE: return sucess |
| 83 |
return 1; |
| 84 |
} |
| 85 |
//----------------------------------------------------------------- |
| 86 |
/** |
| 87 |
* bool level_action_save() |
| 88 |
*/ |
| 89 |
int |
| 90 |
script_level_action_save(lua_State *L) throw() |
| 91 |
{ |
| 92 |
BEGIN_NOEXCEPTION; |
| 93 |
bool sucess = getLevel(L)->action_save(); |
| 94 |
lua_pushboolean(L, sucess); |
| 95 |
END_NOEXCEPTION; |
| 96 |
//NOTE: return sucess |
| 97 |
return 1; |
| 98 |
} |
| 99 |
//----------------------------------------------------------------- |
| 100 |
/** |
| 101 |
* bool level_action_load() |
| 102 |
*/ |
| 103 |
int |
| 104 |
script_level_action_load(lua_State *L) throw() |
| 105 |
{ |
| 106 |
BEGIN_NOEXCEPTION; |
| 107 |
bool sucess = getLevel(L)->action_load(); |
| 108 |
lua_pushboolean(L, sucess); |
| 109 |
END_NOEXCEPTION; |
| 110 |
//NOTE: return sucess |
| 111 |
return 1; |
| 112 |
} |
| 113 |
//----------------------------------------------------------------- |
| 114 |
/** |
| 115 |
* bool level_action_restart() |
| 116 |
*/ |
| 117 |
int |
| 118 |
script_level_action_restart(lua_State *L) throw() |
| 119 |
{ |
| 120 |
BEGIN_NOEXCEPTION; |
| 121 |
bool sucess = getLevel(L)->action_restart(1); |
| 122 |
lua_pushboolean(L, sucess); |
| 123 |
END_NOEXCEPTION; |
| 124 |
//NOTE: return sucess |
| 125 |
return 1; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
//----------------------------------------------------------------- |
| 130 |
/** |
| 131 |
* void level_createRoom(width, height, picture) |
| 132 |
* Example: |
| 133 |
* createRoom(40, 50, "kitchen-bg.png") |
| 134 |
*/ |
| 135 |
int |
| 136 |
script_level_createRoom(lua_State *L) throw() |
| 137 |
{ |
| 138 |
BEGIN_NOEXCEPTION; |
| 139 |
int w = luaL_checkint(L, 1); |
| 140 |
int h = luaL_checkint(L, 2); |
| 141 |
const char *picture = luaL_checkstring(L, 3); |
| 142 |
|
| 143 |
getLevel(L)->createRoom(w, h, picture); |
| 144 |
END_NOEXCEPTION; |
| 145 |
return 0; |
| 146 |
} |
| 147 |
//----------------------------------------------------------------- |
| 148 |
/** |
| 149 |
* int level_getRestartCounter() |
| 150 |
* |
| 151 |
* Returns number of attemps, starts from 1. |
| 152 |
*/ |
| 153 |
int |
| 154 |
script_level_getRestartCounter(lua_State *L) throw() |
| 155 |
{ |
| 156 |
BEGIN_NOEXCEPTION; |
| 157 |
int counter = getLevel(L)->getRestartCounter(); |
| 158 |
lua_pushnumber(L, counter); |
| 159 |
END_NOEXCEPTION; |
| 160 |
//NOTE: return counter |
| 161 |
return 1; |
| 162 |
} |
| 163 |
//----------------------------------------------------------------- |
| 164 |
/** |
| 165 |
* int level_getDepth() |
| 166 |
* |
| 167 |
*/ |
| 168 |
int |
| 169 |
script_level_getDepth(lua_State *L) throw() |
| 170 |
{ |
| 171 |
BEGIN_NOEXCEPTION; |
| 172 |
int depth = getLevel(L)->getDepth(); |
| 173 |
lua_pushnumber(L, depth); |
| 174 |
END_NOEXCEPTION; |
| 175 |
//NOTE: return depth |
| 176 |
return 1; |
| 177 |
} |
| 178 |
//----------------------------------------------------------------- |
| 179 |
/** |
| 180 |
* bool level_isNewRound() |
| 181 |
* |
| 182 |
*/ |
| 183 |
int |
| 184 |
script_level_isNewRound(lua_State *L) throw() |
| 185 |
{ |
| 186 |
BEGIN_NOEXCEPTION; |
| 187 |
bool newRound = getLevel(L)->isNewRound(); |
| 188 |
lua_pushboolean(L, newRound); |
| 189 |
END_NOEXCEPTION; |
| 190 |
//NOTE: return newRound |
| 191 |
return 1; |
| 192 |
} |
| 193 |
//----------------------------------------------------------------- |
| 194 |
/** |
| 195 |
* bool level_isSolved() |
| 196 |
* |
| 197 |
*/ |
| 198 |
int |
| 199 |
script_level_isSolved(lua_State *L) throw() |
| 200 |
{ |
| 201 |
BEGIN_NOEXCEPTION; |
| 202 |
bool solved = getLevelScript(L)->room()->isSolved(); |
| 203 |
lua_pushboolean(L, solved); |
| 204 |
END_NOEXCEPTION; |
| 205 |
//NOTE: return solved |
| 206 |
return 1; |
| 207 |
} |
| 208 |
//----------------------------------------------------------------- |
| 209 |
/** |
| 210 |
* void level_newDemo(demofile) |
| 211 |
*/ |
| 212 |
int |
| 213 |
script_level_newDemo(lua_State *L) throw() |
| 214 |
{ |
| 215 |
BEGIN_NOEXCEPTION; |
| 216 |
const char *demofile = luaL_checkstring(L, 1); |
| 217 |
|
| 218 |
getLevel(L)->newDemo(Path::dataReadPath(demofile)); |
| 219 |
END_NOEXCEPTION; |
| 220 |
return 0; |
| 221 |
} |
| 222 |
//----------------------------------------------------------------- |
| 223 |
/** |
| 224 |
* void level_planShow(func) |
| 225 |
*/ |
| 226 |
int |
| 227 |
script_level_planShow(lua_State *L) throw() |
| 228 |
{ |
| 229 |
BEGIN_NOEXCEPTION; |
| 230 |
luaL_checktype(L, 1, LUA_TFUNCTION); |
| 231 |
int funcRef = luaL_ref(L, LUA_REGISTRYINDEX); |
| 232 |
|
| 233 |
Command *command = getLevelScript(L)->createCommand(funcRef); |
| 234 |
getLevel(L)->planShow(command); |
| 235 |
END_NOEXCEPTION; |
| 236 |
return 0; |
| 237 |
} |
| 238 |
//----------------------------------------------------------------- |
| 239 |
/** |
| 240 |
* bool level_isShowing() |
| 241 |
*/ |
| 242 |
int |
| 243 |
script_level_isShowing(lua_State *L) throw() |
| 244 |
{ |
| 245 |
BEGIN_NOEXCEPTION; |
| 246 |
bool showing = getLevel(L)->isShowing(); |
| 247 |
lua_pushboolean(L, showing); |
| 248 |
END_NOEXCEPTION; |
| 249 |
//NOTE: return showing |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|