_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
script.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2006 by Lutz Sammer and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __SCRIPT_H__
31 #define __SCRIPT_H__
32 
34 
35 /*----------------------------------------------------------------------------
36 -- Includes
37 ----------------------------------------------------------------------------*/
38 
39 #include <string>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 #include <lua.h>
44 #include <lauxlib.h>
45 #include <lualib.h>
46 #ifdef __cplusplus
47 }
48 #endif
49 
50 /*----------------------------------------------------------------------------
51 -- Declarations
52 ----------------------------------------------------------------------------*/
53 
54 class CUnit;
55 class CUnitType;
56 class CFile;
57 class CFont;
58 
59 struct LuaUserData {
60  int Type;
61  void *Data;
62 };
63 
64 enum {
65  LuaUnitType = 100,
67 };
68 
69 extern lua_State *Lua;
70 
71 extern int LuaLoadFile(const std::string &file, const std::string &strArg = "");
72 extern int LuaCall(int narg, int clear, bool exitOnError = true);
73 
74 #define LuaError(l, args) \
75  do { \
76  PrintFunction(); \
77  fprintf(stdout, args); \
78  fprintf(stdout, "\n"); \
79  lua_pushfstring(l, args); lua_error(l); \
80  } while (0)
81 
82 #define LuaCheckArgs(l, args) \
83  do { \
84  if (lua_gettop(l) != args) { \
85  LuaError(l, "incorrect argument"); \
86  } \
87  } while (0)
88 
89 #if LUA_VERSION_NUM <= 501
90 
91 inline size_t lua_rawlen(lua_State *l, int index)
92 {
93  return luaL_getn(l, index);
94 }
95 
96 #endif
97 
99 enum ENumber {
109 
116 
119 
122 
124 
126 };
127 
129 enum EUnit {
131  // FIXME: add others.
132 };
133 
135 enum EString {
146  // add more...
147 };
148 
152 };
153 
164 };
165 
169 enum EnumUnit {
175 };
176 
182 struct NumberDesc;
183 
188 struct UnitDesc;
189 
194 struct StringDesc;
195 
197 struct BinOp {
200 };
201 
205 struct NumberDesc {
207  union {
208  unsigned int Index;
209  int Val;
212  struct {
214  int Index;
216  int Loc;
217  } UnitStat;
218  struct {
220  int Index;
222  int Loc;
223  } TypeStat;
224  struct {
227  } VideoTextLength;
228  struct {
229  StringDesc *String;
230  char C;
231  } StringFind;
232  struct {
236  } NumIf;
237  struct {
241  } PlayerData;
242  } D;
243 };
244 
248 struct UnitDesc {
250  union {
252  } D;
253 };
254 
258 struct StringDesc {
260  union {
261  unsigned int Index;
262  char *Val;
263  struct {
265  int n;
266  } Concat;
270  struct {
274  } If;
275  struct {
276  StringDesc *String;
279  } SubString;
280  struct {
281  StringDesc *String;
285  } Line;
288  } D;
289 };
290 
291 /*----------------------------------------------------------------------------
292 -- Variables
293 ----------------------------------------------------------------------------*/
294 
295 extern int CclInConfigFile;
296 
297 /*----------------------------------------------------------------------------
298 -- Functions
299 ----------------------------------------------------------------------------*/
300 
301 extern const char *LuaToString(lua_State *l, int narg);
302 extern int LuaToNumber(lua_State *l, int narg);
303 extern unsigned int LuaToUnsignedNumber(lua_State *l, int narg);
304 extern bool LuaToBoolean(lua_State *l, int narg);
305 
306 extern const char *LuaToString(lua_State *l, int index, int subIndex);
307 extern int LuaToNumber(lua_State *l, int index, int subIndex);
308 extern unsigned int LuaToUnsignedNumber(lua_State *l, int index, int subIndex);
309 extern bool LuaToBoolean(lua_State *l, int index, int subIndex);
310 
311 extern void LuaGarbageCollect();
312 extern void InitLua();
313 extern void LoadCcl(const std::string &filename, const std::string &luaArgStr = "");
314 extern void SavePreferences();
315 extern int CclCommand(const std::string &command, bool exitOnError = true);
316 
317 extern void ScriptRegister();
318 
319 extern std::string SaveGlobal(lua_State *l);
320 
321 CUnit *CclGetUnitFromRef(lua_State *l);
322 
330 template <typename T>
331 static void CclGetPos(lua_State *l, T *x , T *y, const int offset = -1)
332 {
333  if (!lua_istable(l, offset) || lua_rawlen(l, offset) != 2) {
334  LuaError(l, "incorrect argument");
335  }
336  *x = LuaToNumber(l, offset, 1);
337  *y = LuaToNumber(l, offset, 2);
338 }
339 
340 extern NumberDesc *Damage;
341 
343 extern EnumVariable Str2EnumVariable(lua_State *l, const char *s);
344 extern NumberDesc *CclParseNumberDesc(lua_State *l);
345 extern UnitDesc *CclParseUnitDesc(lua_State *l);
346 extern CUnitType **CclParseTypeDesc(lua_State *l);
347 StringDesc *CclParseStringDesc(lua_State *l);
348 
349 extern int EvalNumber(const NumberDesc *numberdesc);
350 extern CUnit *EvalUnit(const UnitDesc *unitdesc);
351 std::string EvalString(const StringDesc *s);
352 
353 void FreeNumberDesc(NumberDesc *number);
354 void FreeUnitDesc(UnitDesc *unitdesc);
355 void FreeStringDesc(StringDesc *s);
356 
358 
359 #endif // !__SCRIPT_H__
Definition: script.h:59
Rand(a) : number in [0..a-1].
Definition: script.h:110
StringDesc * String
Definition: script.h:225
NumberDesc * Line
Original string.
Definition: script.h:282
struct StringDesc::@29::@31 If
Unit desciption.
union NumberDesc::@21 D
which number.
int LuaToNumber(lua_State *l, int narg)
Definition: script.cpp:313
std::string EvalString(const StringDesc *s)
Evaluate the unit.
Definition: script.cpp:1045
a <= b.
Definition: script.h:114
If cond then Number1 else Number2.
Definition: script.h:125
Unit->Container.
Definition: script.h:173
CUnitType ** CclParseTypeDesc(lua_State *l)
Parse a unit description.
Definition: script.cpp:507
EnumVariable Component
Which index variable.
Definition: script.h:215
Definition: script.h:170
NumberDesc * Left
Definition: script.h:198
static void CclGetPos(lua_State *l, T *x, T *y, const int offset=-1)
Definition: script.h:331
int Val
index of the lua function.
Definition: script.h:209
NumberDesc * Right
Left operand.
Definition: script.h:199
Convert number in string.
Definition: script.h:140
Definition: script.h:65
Property of Unit.
Definition: script.h:121
struct StringDesc::@29::@30 Concat
Direct value.
unit->Data.Built.Worker
Definition: script.h:174
Definition: script.h:136
size_t lua_rawlen(lua_State *l, int index)
Definition: script.h:91
(100 * Value / Max)
Definition: script.h:163
NumberDesc * Damage
True while config file parsing.
Definition: script.cpp:62
NumberDesc * Begin
Original string.
Definition: script.h:277
a - b.
Definition: script.h:104
#define LuaError(l, args)
Definition: script.h:74
a <> b.
Definition: script.h:117
NumberDesc * BTrue
Branch condition.
Definition: script.h:234
UnitDesc * CclParseUnitDesc(lua_State *l)
Parse a number description.
Definition: script.cpp:485
NumberDesc * Cond
Definition: script.h:233
struct StringDesc::@29::@32 SubString
conditional string.
Definition: script.h:100
struct NumberDesc::@21::@25 StringFind
void InitLua()
Perform garbage collection.
Definition: script.cpp:2104
void ScriptRegister()
Definition: script.cpp:2413
void SavePreferences()
Load ccl config file.
Definition: script.cpp:2353
EnumUnit
Definition: script.h:169
struct NumberDesc::@21::@22 UnitStat
For binary operand.
a / b.
Definition: script.h:106
The big unit structure.
Definition: unit.h:119
Definition: script.h:205
a + b [+ c ...].
Definition: script.h:139
StringDesc * DataType
Number of player.
Definition: script.h:239
EUnit
All possible value for a unit.
Definition: script.h:129
a + b.
Definition: script.h:103
NumberDesc * Cond
Definition: script.h:271
Definition: script.h:66
Definition: script.h:248
ES_GameInfo
All possible value for a game info string.
Definition: script.h:150
EString e
Definition: script.h:259
Value of the variable.
Definition: script.h:159
Max(a, b).
Definition: script.h:108
lua_State * Lua
Definition: script.cpp:58
NumberDesc * Number
for Concat two string.
Definition: script.h:267
int CclInConfigFile
Structure to work with lua files.
Definition: script.cpp:60
Property of UnitType.
Definition: script.h:123
unsigned int Index
Definition: script.h:208
struct NumberDesc::@21::@26 NumIf
NumberDesc * Player
Definition: script.h:238
directly a string.
Definition: script.h:138
a lua function.
Definition: script.h:101
StringDesc ** Strings
Definition: script.h:264
bool LuaToBoolean(lua_State *l, int narg)
Definition: script.cpp:343
Definition: unittype.h:467
Definition: iolib.h:101
If cond then String1 else String2.
Definition: script.h:142
a > b.
Definition: script.h:111
Font definition.
Definition: font.h:73
Unit direct reference.
Definition: script.h:130
Increase value of the variable.
Definition: script.h:161
Max of the variable.
Definition: script.h:160
a == b.
Definition: script.h:115
UnitDesc * Unit
Definition: script.h:213
int n
Array of operands.
Definition: script.h:265
Definition: script.h:158
StringDesc * String
Number.
Definition: script.h:268
ENumber e
Definition: script.h:206
All Objectives of the game.
Definition: script.h:151
StringDesc * ResType
Player's data.
Definition: script.h:240
CUnit * CclGetUnitFromRef(lua_State *l)
For saving lua state.
Definition: script_unit.cpp:148
directly a number.
Definition: script.h:102
StringDesc * BTrue
Branch condition.
Definition: script.h:272
Definition: script.h:258
VideoTextLength(font, string).
Definition: script.h:118
EUnit e
Definition: script.h:249
SubString.
Definition: script.h:144
NumberDesc * MaxLen
Line number.
Definition: script.h:283
int Index
Which unit.
Definition: script.h:214
StringDesc * BFalse
String if Cond is true.
Definition: script.h:273
const char * LuaToString(lua_State *l, int narg)
True while config file parsing.
Definition: script.cpp:298
NumberDesc * N
Direct value.
Definition: script.h:210
void FreeUnitDesc(UnitDesc *unitdesc)
Free number description content. (no pointer itself).
Definition: script.cpp:1145
int CclCommand(const std::string &command, bool exitOnError=true)
Save user preferences.
Definition: script.cpp:2083
unsigned int LuaToUnsignedNumber(lua_State *l, int narg)
Definition: script.cpp:328
StringDesc * CclParseStringDesc(lua_State *l)
Parse a unit type description.
Definition: script.cpp:786
a * b.
Definition: script.h:105
EnumVariable
Definition: script.h:157
void FreeNumberDesc(NumberDesc *number)
Evaluate the string.
Definition: script.cpp:1159
struct NumberDesc::@21::@27 PlayerData
conditional string.
int LuaLoadFile(const std::string &file, const std::string &strArg="")
Definition: script.cpp:212
CUnit * EvalUnit(const UnitDesc *unitdesc)
Evaluate the number.
Definition: script.cpp:900
(Max - Value)
Definition: script.h:162
UnitType Name.
Definition: script.h:143
int Loc
Which component.
Definition: script.h:216
struct NumberDesc::@21::@23 TypeStat
CUnit ** AUnit
Definition: script.h:251
ES_GameInfo GameInfoType
For specific line.
Definition: script.h:286
void * Data
Definition: script.h:61
unit.
Definition: script.h:171
NumberDesc * BFalse
Number if Cond is true.
Definition: script.h:235
EnumVariable Str2EnumVariable(lua_State *l, const char *s)
Damage calculation for missile.
Definition: script_ui.cpp:427
struct NumberDesc::@21::@24 VideoTextLength
Min(a, b).
Definition: script.h:107
union StringDesc::@29 D
which number.
Inverse video for the string ("a" -> "~").
Definition: script.h:141
EString
All possible value for a string.
Definition: script.h:135
a >= b.
Definition: script.h:112
int LuaCall(int narg, int clear, bool exitOnError=true)
Definition: script.cpp:159
NumberDesc * End
Begin of result string.
Definition: script.h:278
UnitDesc * Unit
String.
Definition: script.h:269
unit->Inside.
Definition: script.h:172
ENumber
All possible value for a number.
Definition: script.h:99
char C
String.
Definition: script.h:230
void LuaGarbageCollect()
Definition: script.cpp:389
unsigned int Index
Definition: script.h:261
CFont * Font
Max length of line.
Definition: script.h:284
BinOp binOp
Other number.
Definition: script.h:211
NumberDesc * CclParseNumberDesc(lua_State *l)
Definition: script.cpp:595
std::string SaveGlobal(lua_State *l)
Definition: script.cpp:2343
strchr(string, char) - s.
Definition: script.h:120
int Type
Definition: script.h:60
CFont * Font
String.
Definition: script.h:226
for Bin operand a ?? b
Definition: script.h:197
a < b.
Definition: script.h:113
a lua function.
Definition: script.h:137
void LoadCcl(const std::string &filename, const std::string &luaArgStr="")
Initialise Lua.
Definition: script.cpp:2397
void FreeStringDesc(StringDesc *s)
Free unit description content. (no pointer itself).
Definition: script.cpp:1229
NumberDesc * PlayerName
Definition: script.h:287
union UnitDesc::@28 D
which unit;
int EvalNumber(const NumberDesc *numberdesc)
Parse a string description.
Definition: script.cpp:925
CUnitType ** Type
Definition: script.h:219
char * Val
index of the lua function.
Definition: script.h:262
line n of the string.
Definition: script.h:145
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.