source: Utilities/lua.cpp @ 81

Revision 81, 4.0 KB checked in by knowknowledge, 4 years ago (diff)

Testing git by fixing a bug. Yay!

Line 
1/*
2 * Filename      : lua.cpp
3 * Author(s)     : Chris Thielen (chris@epiar.net)
4 * Date Created  : Saturday, January 5, 2008
5 * Purpose       : Provides abilities to load, store, and run Lua scripts
6 * Notes         : To be used in conjunction with various other subsystems, A.I., GUI, etc.
7 */
8
9#include "Engine/console.h"
10#include "Engine/simulation.h"
11#include "Utilities/log.h"
12#include "Utilities/lua.h"
13#include "AI/ai_lua.h"
14#include "UI/ui_lua.h"
15#include "UI/ui.h"
16#include "UI/ui_window.h"
17#include "UI/ui_label.h"
18#include "UI/ui_button.h"
19#include "Sprites/Player.h"
20
21bool Lua::luaInitialized = false;
22lua_State *Lua::luaVM = NULL;
23SpriteManager *Lua::my_sprites= NULL;
24vector<string> Lua::buffer;
25
26bool Lua::Load( string filename ) {
27        if( ! luaInitialized ) {
28                if( Init() == false ) {
29                        Log::Warning( "Could not load Lua script. Unable to initialize Lua." );
30                        return( false );
31                }
32        }
33
34        // Start the lua script
35       
36        if( luaL_dofile(luaVM,filename.c_str()) ){
37                Log::Error("Could not run lua file '%s'",filename.c_str());
38                Log::Error("%s", lua_tostring(luaVM, -1));
39        } else {
40                Log::Message("Loaded the universe");
41        }
42       
43
44        return( false );
45}
46
47bool Lua::Update(){
48    // Tell the Lua State to update itself
49    lua_getglobal(luaVM, "Update");
50    if( lua_pcall(luaVM,0,0,0) != 0 ){
51                Log::Error("Could not call lua function Update");
52            Log::Error("%s", lua_tostring(luaVM, -1));
53        return (false);
54    }
55        return (true);
56}
57
58
59
60bool Lua::Run( string line ) {
61        int error = 0;
62        Log::Message("Running '%s'", (char *)line.c_str() );
63
64        if( ! luaInitialized ) {
65                if( Init() == false ) {
66                        Log::Warning( "Could not load Lua script. Unable to initialize Lua." );
67                        return( false );
68                }
69        }
70
71        error = luaL_loadbuffer(luaVM, line.c_str(), line.length(), "line") || lua_pcall(luaVM, 0, 1, 0);
72        if( error ) {
73                Console::InsertResult(lua_tostring(luaVM, -1));
74                lua_pop(luaVM, 1);  /* pop error message from the stack */
75        }
76
77        return( false );
78}
79
80// returns the output from the last lua script and deletes it from internal buffer
81vector<string> Lua::GetOutput() {
82        vector<string> ret = buffer;
83
84        buffer.clear();
85
86        return( ret );
87}
88
89bool Lua::SetSpriteList(SpriteManager* the_sprites){
90        if( ! luaInitialized ) {
91                if( Init() == false ) {
92                        Log::Warning( "Could not load Lua script. Unable to initialize Lua." );
93                        return( false );
94                }
95        }
96       
97        my_sprites = the_sprites;
98        return( true );
99}
100
101SpriteManager* Lua::GetSpriteList(){
102        if( ! luaInitialized ) {
103                if( Init() == false ) {
104                        Log::Warning( "Could not load Lua script. Unable to initialize Lua." );
105                        return( false );
106                }
107        }
108        return my_sprites;
109}
110
111bool Lua::Init() {
112        if( luaInitialized ) {
113                Log::Warning( "Cannot initialize Lua. It is already initialized." );
114                return( false );
115        }
116       
117        luaVM = lua_open();
118
119        if( !luaVM ) {
120                Log::Warning( "Could not initialize Lua VM." );
121                return( false );
122        }
123
124        luaL_openlibs( luaVM );
125
126        RegisterFunctions();
127       
128        luaInitialized = true;
129       
130        return( true );
131}
132
133bool Lua::Close() {
134        if( luaInitialized ) {
135                lua_close( luaVM );
136        } else {
137                Log::Warning( "Cannot deinitialize Lua. It is either not initialized or a script is still loaded." );
138                return( false );
139        }
140       
141        return( true );
142}
143
144void Lua::RegisterFunctions() {
145        // Register these functions to the lua global namespace
146
147        static const luaL_Reg EngineFunctions[] = {
148                {"echo", &Lua::console_echo},
149                {"pause", &Lua::pause},
150                {"unpause", &Lua::unpause},
151                {"player", &Lua::getPlayer},
152                {NULL, NULL}
153        };
154        luaL_register(luaVM,"Epiar",EngineFunctions);
155
156
157        // Register these functions to their own lua namespaces
158        AI_Lua::RegisterAI(luaVM);
159        UI_Lua::RegisterUI(luaVM);
160}
161
162int Lua::console_echo(lua_State *L) {
163        const char *str = lua_tostring(L, 1); // get argument
164
165        if(str == NULL)
166                Console::InsertResult("nil");
167        else
168                Console::InsertResult(str);
169
170        return 0;
171}
172
173int Lua::pause(lua_State *luaVM){
174        Simulation::pause();
175        return 0;
176}
177
178int Lua::unpause(lua_State *luaVM){
179        Simulation::unpause();
180        return 0;
181}
182
183int Lua::getPlayer(lua_State *luaVM){
184        Player **player = (Player**)lua_newuserdata(luaVM, sizeof(Player*));
185        *player = Player::Instance();
186        return 1;
187}
Note: See TracBrowser for help on using the repository browser.