$treeview $search $mathjax
Stratagus
2.2.7
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 // _________ __ __ 00002 // / _____// |_____________ _/ |______ ____ __ __ ______ 00003 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00004 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00005 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00006 // \/ \/ \//_____/ \/ 00007 // ______________________ ______________________ 00008 // T H E W A R B E G I N S 00009 // Stratagus - A free fantasy real time strategy game engine 00010 // 00012 // 00013 // (c) Copyright 1998-2006 by Lutz Sammer and Jimmy Salmon 00014 // 00015 // This program is free software; you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation; only version 2 of the License. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 // GNU General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00029 00030 #ifndef __SCRIPT_H__ 00031 #define __SCRIPT_H__ 00032 00034 00035 /*---------------------------------------------------------------------------- 00036 -- Includes 00037 ----------------------------------------------------------------------------*/ 00038 00039 #include <string> 00040 #ifdef __cplusplus 00041 extern "C" { 00042 #endif 00043 #include <lua.h> 00044 #include <lauxlib.h> 00045 #include <lualib.h> 00046 #ifdef __cplusplus 00047 } 00048 #endif 00049 00050 /*---------------------------------------------------------------------------- 00051 -- Declarations 00052 ----------------------------------------------------------------------------*/ 00053 00054 class CUnit; 00055 class CUnitType; 00056 class CFile; 00057 class CFont; 00058 00059 typedef struct _lua_user_data_ { 00060 int Type; 00061 void *Data; 00062 } LuaUserData; 00063 00064 enum { 00065 LuaUnitType = 100, 00066 LuaSoundType 00067 }; 00068 00069 extern lua_State *Lua; 00070 00071 extern int LuaLoadFile(const std::string &file); 00072 extern int LuaCall(int narg, int clear, bool exitOnError = true); 00073 00074 #define LuaError(l, args) \ 00075 do { \ 00076 PrintFunction(); \ 00077 fprintf(stdout, args); \ 00078 fprintf(stdout, "\n"); \ 00079 lua_pushfstring(l, args); lua_error(l); \ 00080 } while (0) 00081 00082 #define LuaCheckArgs(l, args) \ 00083 do { \ 00084 if (lua_gettop(l) != args) { \ 00085 LuaError(l, "incorrect argument"); \ 00086 } \ 00087 } while (0) 00088 00089 #if LUA_VERSION_NUM <= 501 00090 00091 inline size_t lua_rawlen(lua_State *l, int index) 00092 { 00093 return luaL_getn(l, index); 00094 } 00095 00096 #endif 00097 00098 typedef enum { 00099 ENumber_Lua, 00100 ENumber_Dir, 00101 ENumber_Add, 00102 ENumber_Sub, 00103 ENumber_Mul, 00104 ENumber_Div, 00105 ENumber_Min, 00106 ENumber_Max, 00107 ENumber_Rand, 00108 00109 ENumber_Gt, 00110 ENumber_GtEq, 00111 ENumber_Lt, 00112 ENumber_LtEq, 00113 ENumber_Eq, 00114 ENumber_NEq, 00115 00116 ENumber_VideoTextLength, 00117 ENumber_StringFind, 00118 00119 ENumber_UnitStat, 00120 ENumber_TypeStat 00121 } ENumber; 00122 00123 00124 typedef enum { 00125 EUnit_Ref 00126 // FIXME: add others. 00127 } EUnit; 00128 00129 typedef enum { 00130 EString_Lua, 00131 EString_Dir, 00132 EString_Concat, 00133 EString_String, 00134 EString_InverseVideo, 00135 EString_If, 00136 EString_UnitName, 00137 EString_SubString, 00138 EString_Line 00139 // FIXME: add others. 00140 } EString; 00141 00142 typedef enum { 00143 ES_GameInfo_Objectives 00144 } ES_GameInfo; 00145 00149 typedef enum { 00150 VariableValue = 0, 00151 VariableMax, 00152 VariableIncrease, 00153 VariableDiff, 00154 VariablePercent, 00155 VariableName 00156 } EnumVariable; 00157 00162 typedef enum { 00163 UnitRefItSelf = 0, 00164 UnitRefInside, 00165 UnitRefContainer, 00166 UnitRefWorker, 00167 UnitRefGoal 00168 } EnumUnit; 00169 00170 00175 typedef struct _NumberDesc_ NumberDesc; 00176 00181 typedef struct _UnitDesc_ UnitDesc; 00182 00183 00188 typedef struct _StringDesc_ StringDesc; 00189 00190 00191 typedef struct _binop_ { 00192 NumberDesc *Left; 00193 NumberDesc *Right; 00194 } BinOp; 00195 00199 struct _NumberDesc_ { 00200 ENumber e; 00201 union { 00202 unsigned int Index; 00203 int Val; 00204 NumberDesc *N; 00205 struct _binop_ BinOp; 00206 struct { 00207 UnitDesc *Unit; 00208 int Index; 00209 EnumVariable Component; 00210 int Loc; 00211 } UnitStat; 00212 struct { 00213 CUnitType **Type; 00214 int Index; 00215 EnumVariable Component; 00216 } TypeStat; 00217 struct { 00218 StringDesc *String; 00219 CFont *Font; 00220 } VideoTextLength; 00221 struct { 00222 StringDesc *String; 00223 char C; 00224 } StringFind; 00225 00226 } D; 00227 }; 00228 00232 struct _UnitDesc_ { 00233 EUnit e; 00234 union { 00235 CUnit **AUnit; 00236 } D; 00237 }; 00238 00242 struct _StringDesc_ { 00243 EString e; 00244 union { 00245 unsigned int Index; 00246 char *Val; 00247 struct { 00248 StringDesc **Strings; 00249 int n; 00250 } Concat; 00251 NumberDesc *Number; 00252 StringDesc *String; 00253 UnitDesc *Unit; 00254 struct { 00255 NumberDesc *Cond; 00256 StringDesc *True; 00257 StringDesc *False; 00258 } If; 00259 struct { 00260 StringDesc *String; 00261 NumberDesc *Begin; 00262 NumberDesc *End; 00263 } SubString; 00264 struct { 00265 StringDesc *String; 00266 NumberDesc *Line; 00267 NumberDesc *MaxLen; 00268 CFont *Font; 00269 } Line; 00270 ES_GameInfo GameInfoType; 00271 } D; 00272 }; 00273 00274 /*---------------------------------------------------------------------------- 00275 -- Variables 00276 ----------------------------------------------------------------------------*/ 00277 00278 extern int CclInConfigFile; 00279 00280 /*---------------------------------------------------------------------------- 00281 -- Functions 00282 ----------------------------------------------------------------------------*/ 00283 00284 extern const char *LuaToString(lua_State *l, int narg); 00285 extern int LuaToNumber(lua_State *l, int narg); 00286 extern bool LuaToBoolean(lua_State *l, int narg); 00287 00288 extern void CclGarbageCollect(int fast); 00289 extern void InitLua(); 00290 extern void LoadCcl(const std::string &filename); 00291 extern void SavePreferences(); 00292 extern int CclCommand(const std::string &command, bool exitOnError = true); 00293 00294 extern void ScriptRegister(); 00295 00296 extern std::string SaveGlobal(lua_State *l); 00297 00298 CUnit *CclGetUnitFromRef(lua_State *l); 00299 00309 template <typename T> 00310 static void CclGetPos(lua_State *l, T *x , T *y, const int offset = -1) 00311 { 00312 if (!lua_istable(l, offset) || lua_rawlen(l, offset) != 2) { 00313 LuaError(l, "incorrect argument"); 00314 } 00315 lua_rawgeti(l, offset, 1); 00316 *x = LuaToNumber(l, -1); 00317 lua_pop(l, 1); 00318 lua_rawgeti(l, offset, 2); 00319 *y = LuaToNumber(l, -1); 00320 lua_pop(l, 1); 00321 } 00322 00323 extern NumberDesc *Damage; 00324 00326 extern EnumVariable Str2EnumVariable(lua_State *l, const char *s); 00327 extern NumberDesc *CclParseNumberDesc(lua_State *l); 00328 extern UnitDesc *CclParseUnitDesc(lua_State *l); 00329 extern CUnitType **CclParseTypeDesc(lua_State *l); 00330 StringDesc *CclParseStringDesc(lua_State *l); 00331 00332 extern int EvalNumber(const NumberDesc *numberdesc); 00333 extern CUnit *EvalUnit(const UnitDesc *unitdesc); 00334 std::string EvalString(const StringDesc *s); 00335 00336 void FreeNumberDesc(NumberDesc *number); 00337 void FreeUnitDesc(UnitDesc *unitdesc); 00338 void FreeStringDesc(StringDesc *s); 00339 00341 00342 #endif // !__SCRIPT_H__