Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

level-script.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
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  * void level_save(serialized)
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  * void level_load(moves)
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  * bool level_action_move(symbol)
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     //NOTE: return sucess
00083     return 1;
00084 }
00085 //-----------------------------------------------------------------
00086 /**
00087  * bool level_action_save()
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     //NOTE: return sucess
00097     return 1;
00098 }
00099 //-----------------------------------------------------------------
00100 /**
00101  * bool level_action_load()
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     //NOTE: return sucess
00111     return 1;
00112 }
00113 //-----------------------------------------------------------------
00114 /**
00115  * bool level_action_restart()
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     //NOTE: return sucess
00125     return 1;
00126 }
00127 
00128 
00129 //-----------------------------------------------------------------
00130 /**
00131  * void level_createRoom(width, height, picture)
00132  * Example:
00133  *  createRoom(40, 50, "kitchen-bg.png")
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  * int level_getRestartCounter()
00150  * 
00151  * Returns number of attemps, starts from 1. 
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     //NOTE: return counter
00161     return 1;
00162 }
00163 //-----------------------------------------------------------------
00164 /**
00165  * int level_getDepth()
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     //NOTE: return depth
00176     return 1;
00177 }
00178 //-----------------------------------------------------------------
00179 /**
00180  * bool level_isNewRound()
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     //NOTE: return newRound
00191     return 1;
00192 }
00193 //-----------------------------------------------------------------
00194 /**
00195  * bool level_isSolved()
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     //NOTE: return solved
00206     return 1;
00207 }
00208 //-----------------------------------------------------------------
00209 /**
00210  * void level_newDemo(demofile)
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  * void level_planShow(func)
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  * bool level_isShowing()
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     //NOTE: return showing
00250     return 1;
00251 }
00252 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2