[Add] Finally! Some better freaking lua error checking.

This commit is contained in:
Allanis 2013-03-16 22:32:22 +00:00
parent abb1a6e647
commit 62232d0596

View File

@ -1,4 +1,3 @@
// Woot, LUA!!!!!!
#include "lauxlib.h"
#include "lualib.h"
@ -50,8 +49,6 @@
#define luaL_register(L,n,l) (luaL_openlib(L, (n),(l), 0))
// Creates a new lua table.
#define newtable(L) (lua_newtable(L), lua_gettop(L))
// Call the AI function with name f.
#define AI_LCALL(f) (lua_getglobal(L, f), lua_pcall(L, 0, 0, 0))
// Register a number constant n to name s (syntax is just like lua_regfunc).
#define lua_regnumber(l,s,n) (lua_pushnumber(l,n), lua_setglobal(l,s))
// Registers a C function.
@ -88,6 +85,7 @@ static int ai_minbrakedist(lua_State* L); // Minimal breaking distance.
static int ai_accel(lua_State* L); // Accelerate.
// Internal C routines.
static void ai_run(const char* funcname);
static int ai_loadProfile(char* filename);
static void ai_freetask(Task* t);
// External C routines.
@ -212,6 +210,14 @@ static int pilot_target = 0;
#define AI_STATUS_CREATE 2
static int ai_status = AI_STATUS_NORMAL;
// Attempt to run a function.
static void ai_run(const char* funcname) {
lua_getglobal(L, funcname);
if(lua_pcall(L, 0, 0, 0))
// Errors accured.
WARN("%s", lua_tostring(L, -1));
}
// Destroy the AI part of the pilot.
void ai_destroy(Pilot* p) {
if(p->task)
@ -326,13 +332,13 @@ void ai_think(Pilot* pilot) {
// Control function if pilot is idle or tick is up.
if((cur_pilot->tcontrol < SDL_GetTicks()) || (cur_pilot->task == NULL)) {
AI_LCALL("control"); // Run control.
ai_run("control"); // Run control.
lua_getglobal(L, "control_rate");
cur_pilot->tcontrol = SDL_GetTicks() + 1000*(int)lua_tonumber(L, -1);
}
if(cur_pilot->task != NULL)
// Pilot has a currently running task.
AI_LCALL(cur_pilot->task->name);
ai_run(cur_pilot->task->name);
// Make sure pilot_acc and pilot_turn are legal moves.
if(pilot_acc > 1.) pilot_acc = 1.; // Value must be <= 1.
@ -364,7 +370,7 @@ void ai_create(Pilot* pilot) {
cur_pilot = pilot;
L = cur_pilot->ai->L;
ai_status = AI_STATUS_CREATE;
AI_LCALL("create");
ai_run("create");
ai_status = AI_STATUS_NORMAL;
}