[Add] Implemented pilot memory tables.

This commit is contained in:
Allanis 2013-12-14 15:09:34 +00:00
parent 980282f655
commit b9f97f321b
2 changed files with 42 additions and 2 deletions

View File

@ -99,6 +99,7 @@ static void ai_freetask(Task* t);
void ai_attacked(Pilot* attacked, const unsigned int attacker); /* weapon.c */
void ai_create(Pilot* pilot);
/* C routines made external. */
void ai_pinit(Pilot* p, AI_Profile* ai);
void ai_destroy(Pilot* p);
void ai_think(Pilot* pilot);
@ -240,6 +241,12 @@ static int ai_status = AI_STATUS_NORMAL;
* @param[in] funcname Function to run.
*/
static void ai_run(lua_State* L, const char* funcname) {
/* Set the pilot's memory. */
lua_getglobal(L, "pilotmem");
lua_pushnumber(L, cur_pilot->id);
lua_gettable(L, -2);
lua_setglobal(L, "mem");
lua_getglobal(L, funcname);
if(lua_pcall(L, 0, 0, 0))
/* Errors accured. */
@ -247,6 +254,20 @@ static void ai_run(lua_State* L, const char* funcname) {
cur_pilot->name, funcname, lua_tostring(L,-1));
}
/**
* @fn
*/
void ai_pinit(Pilot* p, AI_Profile* ai) {
lua_State* L;
L = ai->L;
/* Adds a new pilot memory in the memory table. */
lua_getglobal(L, "pilotmem");
lua_pushnumber(L, p->id);
lua_newtable(L);
lua_settable(L, -3);
}
/**
* @fn void ai_destroy(Pilot* p)
*
@ -255,6 +276,16 @@ static void ai_run(lua_State* L, const char* funcname) {
* @param[in] p Pilot to destroy it's AI part.
*/
void ai_destroy(Pilot* p) {
lua_State* L;
L = p->ai->L;
/* Get rid of pilot's memory. */
lua_getglobal(L, "pilotmem");
lua_pushnumber(L, p->id);
lua_pushnil(L);
lua_settable(L, -3);
/* Clean up tasks. */
if(p->task)
ai_freetask(p->task);
}
@ -347,6 +378,10 @@ static int ai_loadProfile(char* filename) {
free(buf);
/* Add the player memory. */
lua_newtable(L);
lua_setglobal(L, "pilotmem");
return 0;
}

View File

@ -46,6 +46,7 @@ static int nfleets = 0; /** Number of fleets. */
/* External. */
/* AI. */
extern void ai_pinit(Pilot* p, AI_Profile* ai);
extern void ai_destroy(Pilot* p);
extern void ai_think(Pilot* pilot);
extern void ai_create(Pilot* pilot);
@ -1212,7 +1213,10 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, int faction,
pilot->faction = faction;
/* AI. */
pilot->ai = ai;
if(ai != NULL) {
ai_pinit(pilot, ai);
pilot->ai = ai;
}
/* Solid. */
pilot->solid = solid_create(ship->mass, dir, pos, vel);
@ -1368,12 +1372,13 @@ Pilot* pilot_copy(Pilot* src) {
* @param p Pilot to free.
*/
void pilot_free(Pilot* p) {
if(p->ai != NULL)
ai_destroy(p); /* Must be destroyed first if applicable. */
if(player == p) player = NULL;
solid_free(p->solid);
if(p->outfits) free(p->outfits);
free(p->name);
if(p->commodities) free(p->commodities);
ai_destroy(p);
free(p);
}