[Add] Few more mission functions. Also fixed invalid memory free.
This commit is contained in:
parent
6fc39a69e0
commit
faff7b2193
@ -22,10 +22,12 @@ static Mission* cur_mission = NULL;
|
||||
static int misn_setTitle(lua_State* L);
|
||||
static int misn_setDesc(lua_State* L);
|
||||
static int misn_setReward(lua_State* L);
|
||||
static int misn_factions(lua_State* L);
|
||||
static const luaL_Reg misn_methods[] = {
|
||||
{ "setTitle", misn_setTitle },
|
||||
{ "setDesc", misn_setDesc },
|
||||
{ "setReward", misn_setReward },
|
||||
{ "factions", misn_factions },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
@ -36,6 +38,19 @@ static const luaL_Reg space_methods[] = {
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Player.
|
||||
static int player_freeSpace(lua_State* L);
|
||||
static int player_addCargo(lua_State* L);
|
||||
static int player_rmCargo(lua_State* L);
|
||||
static int player_pay(lua_State* L);
|
||||
static const luaL_Reg player_methods[] = {
|
||||
{ "freeCargo", player_freeSpace },
|
||||
{ "addCargo", player_addCargo },
|
||||
{ "rmCargo", player_rmCargo },
|
||||
{ "pay", player_pay },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// RNG.
|
||||
static int rnd_int(lua_State*L);
|
||||
static const luaL_Reg rnd_methods[] = {
|
||||
@ -53,6 +68,7 @@ static const luaL_Reg hook_methods[] = {
|
||||
int misn_loadLibs(lua_State* L) {
|
||||
luaL_register(L, "misn", misn_methods);
|
||||
luaL_register(L, "space", space_methods);
|
||||
luaL_register(L, "player", player_methods);
|
||||
luaL_register(L, "rnd", rnd_methods);
|
||||
luaL_register(L, "hook", hook_methods);
|
||||
return 0;
|
||||
@ -83,7 +99,7 @@ static int misn_setTitle(lua_State* L) {
|
||||
if(cur_mission->title)
|
||||
// Cleanup the old title.
|
||||
free(cur_mission->title);
|
||||
cur_mission->title = strdup(lua_tostring(L, -1));
|
||||
cur_mission->title = strdup((char*)lua_tostring(L, -1));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -94,7 +110,7 @@ static int misn_setDesc(lua_State* L) {
|
||||
if(cur_mission->title)
|
||||
// Cleanup the old description.
|
||||
free(cur_mission->desc);
|
||||
cur_mission->desc = strdup(lua_tostring(L, -1));
|
||||
cur_mission->desc = strdup((char*)lua_tostring(L, -1));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -105,13 +121,74 @@ static int misn_setReward(lua_State* L) {
|
||||
if(cur_mission->reward)
|
||||
// Cleanup the old reward.
|
||||
free(cur_mission->reward);
|
||||
cur_mission->reward = strdup(lua_tostring(L, -1));
|
||||
cur_mission->reward = strdup((char*)lua_tostring(L, -1));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int misn_factions(lua_State* L) {
|
||||
(void) L;
|
||||
// TODO: proper misn.factions() implementation.
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int space_getPlanet(lua_State* L) {
|
||||
(void)L;
|
||||
// TODO: Proper getPlanet implementation.
|
||||
lua_pushstring(L, "Arrakis");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Player. --
|
||||
static int player_freeSpace(lua_State* L) {
|
||||
lua_pushnumber(L, pilot_freeCargo(player));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int player_addCargo(lua_State* L) {
|
||||
Commodity* cargo;
|
||||
int quantity, ret;
|
||||
|
||||
MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -1)) cargo = commodity_get((char*) lua_tostring(L, -1));
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -2)) quantity = (int)lua_tonumber(L, -2);
|
||||
else return 0;
|
||||
|
||||
ret = pilot_addCargo(player, cargo, quantity);
|
||||
|
||||
lua_pushnumber(L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int player_rmCargo(lua_State* L) {
|
||||
Commodity* cargo;
|
||||
int quantity, ret;
|
||||
|
||||
MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -1)) cargo = commodity_get((char*) lua_tostring(L, -1));
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -2)) quantity = (int) lua_tonumber(L, -2);
|
||||
else return 0;
|
||||
|
||||
ret = pilot_rmCargo(player, cargo, quantity);
|
||||
|
||||
lua_pushnumber(L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int player_pay(lua_State* L) {
|
||||
int money;
|
||||
|
||||
MIN_ARGS(1);
|
||||
|
||||
if(lua_isnumber(L, -1)) money = (int) lua_tonumber(L, -1);
|
||||
else return 0;
|
||||
|
||||
player->credits += money;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,11 @@ static int mission_init(Mission* mission, MissionData* misn) {
|
||||
mission->id = ++mission_id;
|
||||
mission->data = misn;
|
||||
|
||||
// Sane defaults.
|
||||
mission->title = NULL;
|
||||
mission->desc = NULL;
|
||||
mission->reward = NULL;
|
||||
|
||||
// Init lua.
|
||||
mission->L = luaL_newstate();
|
||||
if(mission->L == NULL) {
|
||||
@ -169,6 +174,7 @@ Mission* missions_computer(int* n, int faction, char* planet, char* system) {
|
||||
Mission* tmp;
|
||||
MissionData* misn;
|
||||
|
||||
tmp = NULL;
|
||||
m = 0;
|
||||
for(i = 0; i < mission_nstack; i++) {
|
||||
misn = &mission_stack[i];
|
||||
|
@ -603,6 +603,11 @@ static void pilot_calcStats(Pilot* pilot) {
|
||||
pilot->energy = ec * pilot->energy_max;
|
||||
}
|
||||
|
||||
// Pilot free cargo space.
|
||||
int pilot_freeCargo(Pilot* p) {
|
||||
return p->cargo_free;
|
||||
}
|
||||
|
||||
// Try to add quantity of cargo to pilot, return quantity actually added.
|
||||
int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
int i, q;
|
||||
|
@ -135,10 +135,11 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter,
|
||||
void pilot_setSecondary(Pilot* p, const char* secondary);
|
||||
void pilot_setAmmo(Pilot* p);
|
||||
double pilot_face(Pilot* p, const float dir);
|
||||
int pilot_freeSpace(Pilot* p);
|
||||
int pilot_freeSpace(Pilot* p); // Pilot space.
|
||||
int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity);
|
||||
int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity);
|
||||
char* pilot_getOutfits(Pilot* pilot);
|
||||
int pilot_freeCargo(Pilot* p); // Cargo space.
|
||||
int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity);
|
||||
int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user