From 049b52fc79856e12ff7d290f9101ad2efba2cbc7 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 17 May 2013 02:25:54 +0100 Subject: [PATCH] [Change] Minor misn_lua cleanup and added pilot.rename(). --- src/misn_lua.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/misn_lua.c b/src/misn_lua.c index 66c0a05..503625e 100644 --- a/src/misn_lua.c +++ b/src/misn_lua.c @@ -15,8 +15,13 @@ #include "xml.h" #include "misn_lua.h" -#define MISN_DEBUG(str, args...) (fprintf(stdout, "Mission '%s': "str"\n", \ - cur_mission->data->name, ## args)) +#define MISN_DEBUG(str, args...) \ + (fprintf(stdout, "Mission '%s': "str"\n", cur_mission->data->name, ## args)) + +#define MISN_INVALID_PARAMETER() { \ + MISN_DEBUG("Invalid parameter."); \ + return 0; \ +} #define MIN_ARGS(n) \ if(lua_gettop(L) < n) { \ @@ -169,8 +174,10 @@ static const luaL_reg hook_methods[] = { // Pilots. static int pilot_addFleet(lua_State* L); +static int pilot_rename(lua_State* L); static const luaL_reg pilot_methods[] = { { "add", pilot_addFleet }, + { "rename", pilot_rename }, { 0, 0 } }; @@ -647,10 +654,7 @@ static int space_jumpDist(lua_State* L) { if(lua_isstring(L, -1)) start = (char*)lua_tostring(L, -1); - else { - MISN_DEBUG("invalid parameter 1"); - return 0; - } + else MISN_INVALID_PARAMETER(); if((lua_gettop(L) > 1) && lua_isstring(L, -2)) goal = (char*) lua_tostring(L, -2); @@ -889,10 +893,7 @@ static int hook_pilotDeath(lua_State* L) { unsigned int p; if(lua_isnumber(L, -2)) p = (unsigned int) lua_tonumber(L, -2); - else { - MISN_DEBUG("Invalid first param"); - return 0; - } + else MISN_INVALID_PARAMETER(); h = hook_generic(L, "death"); // We won't actually call the death stack directly. pilot_addHook(pilot_get(p), PILOT_HOOK_DEATH, h); @@ -911,10 +912,7 @@ static int pilot_addFleet(lua_State* L) { Vec2 vv, vp, vn; if(lua_isstring(L, -1)) fltname = (char*) lua_tostring(L, -1); - else { - MISN_DEBUG("Invalid parameter"); - return 0; - } + else MISN_INVALID_PARAMETER(); // Pull the fleet. flt = fleet_get(fltname); @@ -956,3 +954,20 @@ static int pilot_addFleet(lua_State* L) { return 1; } +static int pilot_rename(lua_State* L) { + MIN_ARGS(2); + char* name; + unsigned int id; + Pilot* p; + + if(lua_isnumber(L, -2)) id = (unsigned int) lua_tonumber(L, -2); + else MISN_INVALID_PARAMETER(); + if(lua_isstring(L, -1)) name = (char*) lua_tostring(L, -1); + else MISN_INVALID_PARAMETER(); + + p = pilot_get(id); + free(p->name); + p->name = strdup(name); + return 0; +} +