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

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 = "", bool exitOnError = true);
72 extern int LuaCall(int narg, int clear, bool exitOnError = true);
73 extern int LuaCall(lua_State *L, int narg, int nresults, int base, bool exitOnError = true);
74 
75 #define LuaError(l, args) \
76  do { \
77  PrintFunction(); \
78  fprintf(stdout, args); \
79  fprintf(stdout, "\n"); \
80  lua_pushfstring(l, args); lua_error(l); \
81  } while (0)
82 
83 #define LuaCheckArgs(l, args) \
84  do { \
85  if (lua_gettop(l) != args) { \
86  LuaError(l, "incorrect argument"); \
87  } \
88  } while (0)
89 
90 #if LUA_VERSION_NUM <= 501
91 
92 inline size_t lua_rawlen(lua_State *l, int index)
93 {
94  return luaL_getn(l, index);
95 }
96 
97 #endif
98 
100 enum ENumber {
110 
117 
120 
123 
125 
127 };
128 
130 enum EUnit {
132  // FIXME: add others.
133 };
134 
136 enum EString {
147  // add more...
148 };
149 
153 };
154 
165 };
166 
170 enum EnumUnit {
176 };
177 
183 struct NumberDesc;
184 
189 struct UnitDesc;
190 
195 struct StringDesc;
196 
198 struct BinOp {
201 };
202 
206 struct NumberDesc {
208  union {
209  unsigned int Index;
210  int Val;
213  struct {
215  int Index;
217  int Loc;
218  } UnitStat;
219  struct {
221  int Index;
223  int Loc;
224  } TypeStat;
225  struct {
228  } VideoTextLength;
229  struct {
230  StringDesc *String;
231  char C;
232  } StringFind;
233  struct {
237  } NumIf;
238  struct {
242  } PlayerData;
243  } D;
244 };
245 
249 struct UnitDesc {
251  union {
253  } D;
254 };
255 
259 struct StringDesc {
261  union {
262  unsigned int Index;
263  char *Val;
264  struct {
266  int n;
267  } Concat;
271  struct {
275  } If;
276  struct {
277  StringDesc *String;
280  } SubString;
281  struct {
282  StringDesc *String;
286  } Line;
289  } D;
290 };
291 
292 /*----------------------------------------------------------------------------
293 -- Variables
294 ----------------------------------------------------------------------------*/
295 
296 extern int CclInConfigFile;
297 
298 /*----------------------------------------------------------------------------
299 -- Functions
300 ----------------------------------------------------------------------------*/
301 
302 extern const char *LuaToString(lua_State *l, int narg);
303 extern int LuaToNumber(lua_State *l, int narg);
304 extern float LuaToFloat(lua_State *l, int narg);
305 extern unsigned int LuaToUnsignedNumber(lua_State *l, int narg);
306 extern bool LuaToBoolean(lua_State *l, int narg);
307 
308 extern const char *LuaToString(lua_State *l, int index, int subIndex);
309 extern int LuaToNumber(lua_State *l, int index, int subIndex);
310 extern unsigned int LuaToUnsignedNumber(lua_State *l, int index, int subIndex);
311 extern bool LuaToBoolean(lua_State *l, int index, int subIndex);
312 
313 extern void LuaGarbageCollect();
314 extern void InitLua();
315 extern void LoadCcl(const std::string &filename, const std::string &luaArgStr = "");
316 extern void SavePreferences();
317 extern int CclCommand(const std::string &command, bool exitOnError = true);
318 
319 extern void ScriptRegister();
320 
321 extern std::string SaveGlobal(lua_State *l);
322 
323 CUnit *CclGetUnitFromRef(lua_State *l);
324 
332 template <typename T>
333 static void CclGetPos(lua_State *l, T *x , T *y, const int offset = -1)
334 {
335  if (!lua_istable(l, offset) || lua_rawlen(l, offset) != 2) {
336  LuaError(l, "incorrect argument");
337  }
338  *x = LuaToNumber(l, offset, 1);
339  *y = LuaToNumber(l, offset, 2);
340 }
341 
342 extern NumberDesc *Damage;
343 
345 extern EnumVariable Str2EnumVariable(lua_State *l, const char *s);
346 extern NumberDesc *CclParseNumberDesc(lua_State *l);
347 extern UnitDesc *CclParseUnitDesc(lua_State *l);
348 extern CUnitType **CclParseTypeDesc(lua_State *l);
349 StringDesc *CclParseStringDesc(lua_State *l);
350 
351 extern int EvalNumber(const NumberDesc *numberdesc);
352 extern CUnit *EvalUnit(const UnitDesc *unitdesc);
353 std::string EvalString(const StringDesc *s);
354 
355 void FreeNumberDesc(NumberDesc *number);
356 void FreeUnitDesc(UnitDesc *unitdesc);
357 void FreeStringDesc(StringDesc *s);
358 
360 
361 #endif // !__SCRIPT_H__
NumberDesc::C
char C
String.
Definition: script.h:231
CclParseUnitDesc
UnitDesc * CclParseUnitDesc(lua_State *l)
Parse a number description.
Definition: script.cpp:550
StringDesc::Val
char * Val
index of the lua function.
Definition: script.h:263
CclParseNumberDesc
NumberDesc * CclParseNumberDesc(lua_State *l)
Definition: script.cpp:660
ENumber_Lua
@ ENumber_Lua
Definition: script.h:101
BinOp::Left
NumberDesc * Left
Definition: script.h:199
ENumber_Min
@ ENumber_Min
a / b.
Definition: script.h:107
ENumber_GtEq
@ ENumber_GtEq
a > b.
Definition: script.h:112
NumberDesc::Cond
NumberDesc * Cond
Definition: script.h:234
LoadCcl
void LoadCcl(const std::string &filename, const std::string &luaArgStr="")
Initialise Lua.
StringDesc::Begin
NumberDesc * Begin
Original string.
Definition: script.h:278
Str2EnumVariable
EnumVariable Str2EnumVariable(lua_State *l, const char *s)
Damage calculation for missile.
Definition: script_ui.cpp:456
LuaToNumber
int LuaToNumber(lua_State *l, int narg)
Definition: script.cpp:363
NumberDesc::PlayerData
struct NumberDesc::@21::@27 PlayerData
conditional string.
StringDesc::SubString
struct StringDesc::@29::@32 SubString
conditional string.
LuaToFloat
float LuaToFloat(lua_State *l, int narg)
Definition: script.cpp:378
EString_Dir
@ EString_Dir
a lua function.
Definition: script.h:138
lua_rawlen
size_t lua_rawlen(lua_State *l, int index)
Definition: script.h:92
EString_Lua
@ EString_Lua
Definition: script.h:137
ENumber_Mul
@ ENumber_Mul
a - b.
Definition: script.h:105
VariableMax
@ VariableMax
Value of the variable.
Definition: script.h:160
LuaCall
int LuaCall(int narg, int clear, bool exitOnError=true)
Definition: script.cpp:172
VariablePercent
@ VariablePercent
(Max - Value)
Definition: script.h:163
StringDesc::Line
NumberDesc * Line
Original string.
Definition: script.h:283
NumberDesc::BTrue
NumberDesc * BTrue
Branch condition.
Definition: script.h:235
Lua
lua_State * Lua
Definition: script.cpp:68
StringDesc::e
EString e
Definition: script.h:260
UnitRefGoal
@ UnitRefGoal
unit->Data.Built.Worker
Definition: script.h:175
ScriptRegister
void ScriptRegister()
EString_String
@ EString_String
a + b [+ c ...].
Definition: script.h:140
UnitRefContainer
@ UnitRefContainer
unit->Inside.
Definition: script.h:173
NumberDesc::Val
int Val
index of the lua function.
Definition: script.h:210
VariableValue
@ VariableValue
Definition: script.h:159
ENumber_NumIf
@ ENumber_NumIf
Property of UnitType.
Definition: script.h:124
LuaLoadFile
int LuaLoadFile(const std::string &file, const std::string &strArg="", bool exitOnError=true)
Definition: script.cpp:250
Damage
NumberDesc * Damage
True while config file parsing.
Definition: script.cpp:72
CUnitType
Definition: unittype.h:508
StringDesc::Concat
struct StringDesc::@29::@30 Concat
Direct value.
ENumber_Add
@ ENumber_Add
directly a number.
Definition: script.h:103
ENumber_UnitStat
@ ENumber_UnitStat
strchr(string, char) - s.
Definition: script.h:121
VariableIncrease
@ VariableIncrease
Max of the variable.
Definition: script.h:161
SavePreferences
void SavePreferences()
Load ccl config file.
NumberDesc::e
ENumber e
Definition: script.h:207
StringDesc::n
int n
Array of operands.
Definition: script.h:266
ENumber_Rand
@ ENumber_Rand
Max(a, b).
Definition: script.h:109
StringDesc::String
StringDesc * String
Number.
Definition: script.h:269
NumberDesc::Index
unsigned int Index
Definition: script.h:209
LuaUserData::Data
void * Data
Definition: script.h:61
EUnit_Ref
@ EUnit_Ref
Unit direct reference.
Definition: script.h:131
StringDesc::Cond
NumberDesc * Cond
Definition: script.h:272
ENumber_Gt
@ ENumber_Gt
Rand(a) : number in [0..a-1].
Definition: script.h:111
StringDesc::BTrue
StringDesc * BTrue
Branch condition.
Definition: script.h:273
EString_If
@ EString_If
Inverse video for the string ("a" -> "~<a~>").
Definition: script.h:142
StringDesc::Strings
StringDesc ** Strings
Definition: script.h:265
CclInConfigFile
int CclInConfigFile
Structure to work with lua files.
Definition: script.cpp:70
UnitDesc
Definition: script.h:249
LuaUnitType
@ LuaUnitType
Definition: script.h:65
StringDesc::Number
NumberDesc * Number
for Concat two string.
Definition: script.h:268
EUnit
EUnit
All possible value for a unit.
Definition: script.h:130
NumberDesc::DataType
StringDesc * DataType
Number of player.
Definition: script.h:240
ENumber_Max
@ ENumber_Max
Min(a, b).
Definition: script.h:108
NumberDesc::StringFind
struct NumberDesc::@21::@25 StringFind
LuaSoundType
@ LuaSoundType
Definition: script.h:66
StringDesc::If
struct StringDesc::@29::@31 If
Unit desciption.
EvalUnit
CUnit * EvalUnit(const UnitDesc *unitdesc)
Evaluate the number.
Definition: script.cpp:965
StringDesc::MaxLen
NumberDesc * MaxLen
Line number.
Definition: script.h:284
NumberDesc::Player
NumberDesc * Player
Definition: script.h:239
ENumber_PlayerData
@ ENumber_PlayerData
If cond then Number1 else Number2.
Definition: script.h:126
BinOp
for Bin operand a ?? b
Definition: script.h:198
EString_SubString
@ EString_SubString
UnitType Name.
Definition: script.h:144
ENumber_Lt
@ ENumber_Lt
a >= b.
Definition: script.h:113
CclParseStringDesc
StringDesc * CclParseStringDesc(lua_State *l)
Parse a unit type description.
Definition: script.cpp:851
NumberDesc::N
NumberDesc * N
Direct value.
Definition: script.h:211
ENumber_LtEq
@ ENumber_LtEq
a < b.
Definition: script.h:114
NumberDesc
Definition: script.h:206
VariableDiff
@ VariableDiff
Increase value of the variable.
Definition: script.h:162
UnitRefInside
@ UnitRefInside
unit.
Definition: script.h:172
LuaUserData::Type
int Type
Definition: script.h:60
ENumber_TypeStat
@ ENumber_TypeStat
Property of Unit.
Definition: script.h:122
StringDesc::BFalse
StringDesc * BFalse
String if Cond is true.
Definition: script.h:274
ES_GameInfo
ES_GameInfo
All possible value for a game info string.
Definition: script.h:151
CclGetUnitFromRef
CUnit * CclGetUnitFromRef(lua_State *l)
For saving lua state.
Definition: script_unit.cpp:178
StringDesc::GameInfoType
ES_GameInfo GameInfoType
For specific line.
Definition: script.h:287
ES_GameInfo_Objectives
@ ES_GameInfo_Objectives
All Objectives of the game.
Definition: script.h:152
LuaToBoolean
bool LuaToBoolean(lua_State *l, int narg)
Definition: script.cpp:408
CclGetPos
static void CclGetPos(lua_State *l, T *x, T *y, const int offset=-1)
Definition: script.h:333
EString_InverseVideo
@ EString_InverseVideo
Convert number in string.
Definition: script.h:141
FreeNumberDesc
void FreeNumberDesc(NumberDesc *number)
Evaluate the string.
Definition: script.cpp:1224
CclCommand
int CclCommand(const std::string &command, bool exitOnError=true)
Save user preferences.
Definition: script.cpp:2191
StringDesc
Definition: script.h:259
ENumber_Dir
@ ENumber_Dir
a lua function.
Definition: script.h:102
ENumber_StringFind
@ ENumber_StringFind
VideoTextLength(font, string).
Definition: script.h:119
ENumber_Sub
@ ENumber_Sub
a + b.
Definition: script.h:104
NumberDesc::TypeStat
struct NumberDesc::@21::@23 TypeStat
NumberDesc::Unit
UnitDesc * Unit
Definition: script.h:214
EnumUnit
EnumUnit
Definition: script.h:170
NumberDesc::Index
int Index
Which unit.
Definition: script.h:215
VariableName
@ VariableName
(100 * Value / Max)
Definition: script.h:164
BinOp::Right
NumberDesc * Right
Left operand.
Definition: script.h:200
StringDesc::Unit
UnitDesc * Unit
String.
Definition: script.h:270
StringDesc::D
union StringDesc::@29 D
which number.
ENumber
ENumber
All possible value for a number.
Definition: script.h:100
NumberDesc::D
union NumberDesc::@21 D
which number.
ENumber_Eq
@ ENumber_Eq
a <= b.
Definition: script.h:115
EString_Concat
@ EString_Concat
directly a string.
Definition: script.h:139
LuaError
#define LuaError(l, args)
Definition: script.h:75
LuaGarbageCollect
void LuaGarbageCollect()
Definition: script.cpp:454
NumberDesc::Loc
int Loc
Which component.
Definition: script.h:217
ENumber_Div
@ ENumber_Div
a * b.
Definition: script.h:106
LuaUserData
Definition: script.h:59
FreeUnitDesc
void FreeUnitDesc(UnitDesc *unitdesc)
Free number description content. (no pointer itself).
Definition: script.cpp:1210
ENumber_VideoTextLength
@ ENumber_VideoTextLength
a <> b.
Definition: script.h:118
StringDesc::Font
CFont * Font
Max length of line.
Definition: script.h:285
NumberDesc::ResType
StringDesc * ResType
Player's data.
Definition: script.h:241
StringDesc::Index
unsigned int Index
Definition: script.h:262
SaveGlobal
std::string SaveGlobal(lua_State *l)
NumberDesc::UnitStat
struct NumberDesc::@21::@22 UnitStat
For binary operand.
EString_PlayerName
@ EString_PlayerName
line n of the string.
Definition: script.h:146
NumberDesc::BFalse
NumberDesc * BFalse
Number if Cond is true.
Definition: script.h:236
NumberDesc::Font
CFont * Font
String.
Definition: script.h:227
NumberDesc::binOp
BinOp binOp
Other number.
Definition: script.h:212
StringDesc::PlayerName
NumberDesc * PlayerName
Definition: script.h:288
InitLua
void InitLua()
Perform garbage collection.
Definition: script.cpp:2212
EString_Line
@ EString_Line
SubString.
Definition: script.h:145
NumberDesc::Type
CUnitType ** Type
Definition: script.h:220
NumberDesc::VideoTextLength
struct NumberDesc::@21::@24 VideoTextLength
NumberDesc::NumIf
struct NumberDesc::@21::@26 NumIf
UnitDesc::AUnit
CUnit ** AUnit
Definition: script.h:252
EvalNumber
int EvalNumber(const NumberDesc *numberdesc)
Parse a string description.
Definition: script.cpp:990
EString_UnitName
@ EString_UnitName
If cond then String1 else String2.
Definition: script.h:143
LuaToString
const char * LuaToString(lua_State *l, int narg)
True while config file parsing.
Definition: script.cpp:348
NumberDesc::String
StringDesc * String
Definition: script.h:226
UnitDesc::D
union UnitDesc::@28 D
which unit;
FreeStringDesc
void FreeStringDesc(StringDesc *s)
Free unit description content. (no pointer itself).
Definition: script.cpp:1294
UnitRefWorker
@ UnitRefWorker
Unit->Container.
Definition: script.h:174
LuaToUnsignedNumber
unsigned int LuaToUnsignedNumber(lua_State *l, int narg)
Definition: script.cpp:393
ENumber_NEq
@ ENumber_NEq
a == b.
Definition: script.h:116
UnitDesc::e
EUnit e
Definition: script.h:250
EnumVariable
EnumVariable
Definition: script.h:158
CclParseTypeDesc
CUnitType ** CclParseTypeDesc(lua_State *l)
Parse a unit description.
Definition: script.cpp:572
UnitRefItSelf
@ UnitRefItSelf
Definition: script.h:171
EString
EString
All possible value for a string.
Definition: script.h:136
EvalString
std::string EvalString(const StringDesc *s)
Evaluate the unit.
Definition: script.cpp:1110
CFile
Definition: iolib.h:102
CUnit
The big unit structure.
Definition: unit.h:135
StringDesc::End
NumberDesc * End
Begin of result string.
Definition: script.h:279
CFont
Font definition.
Definition: font.h:73
NumberDesc::Component
EnumVariable Component
Which index variable.
Definition: script.h:216
(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.