From b9f97f321b3b344e61c54ad769546b30e58b6ce9 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 14 Dec 2013 15:09:34 +0000 Subject: [PATCH] [Add] Implemented pilot memory tables. --- src/ai.c | 35 +++++++++++++++++++++++++++++++++++ src/pilot.c | 9 +++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/ai.c b/src/ai.c index a60ace3..3ad64a0 100644 --- a/src/ai.c +++ b/src/ai.c @@ -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; } diff --git a/src/pilot.c b/src/pilot.c index 5a1b523..18a07bf 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -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); }