[Fix] Fixed up all the bugs for missions, they are now working.
This commit is contained in:
parent
bf6577ccc1
commit
80ad3081f0
@ -26,14 +26,12 @@ function accept()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function land()
|
function land()
|
||||||
if planet.name() == planet then
|
if space.landName() == planet then
|
||||||
player.rmCargo(carg_type, carg_mass)
|
player.rmCargo(carg_type, carg_mass)
|
||||||
player.pay(misn_reward)
|
player.pay(misn_reward)
|
||||||
tk.msg("Mission Accomplished",
|
tk.msg("Mission Accomplished",
|
||||||
string.format("The workers unload the %s at the docks.", carg_type))
|
string.format("The workers unload the %s at the docks.", carg_type))
|
||||||
|
misn_finish()
|
||||||
-- Set the hooks.
|
|
||||||
hook.land("land")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
29
src/hook.c
29
src/hook.c
@ -8,8 +8,7 @@
|
|||||||
// The hook.
|
// The hook.
|
||||||
typedef struct Hook_ {
|
typedef struct Hook_ {
|
||||||
int id;
|
int id;
|
||||||
lua_State* L;
|
Mission* misn;
|
||||||
unsigned int parent;
|
|
||||||
char* func;
|
char* func;
|
||||||
char* stack;
|
char* stack;
|
||||||
} Hook;
|
} Hook;
|
||||||
@ -20,22 +19,20 @@ static Hook* hook_stack = NULL;
|
|||||||
static int hook_mstack = 0;
|
static int hook_mstack = 0;
|
||||||
static int hook_nstack = 0;
|
static int hook_nstack = 0;
|
||||||
|
|
||||||
|
// Extern.
|
||||||
|
extern int misn_run(Mission* misn, char* func);
|
||||||
|
|
||||||
int hook_run(Hook* hook) {
|
int hook_run(Hook* hook) {
|
||||||
lua_State* L;
|
if(misn_run(hook->misn, hook->func))
|
||||||
|
|
||||||
L = hook->L;
|
|
||||||
|
|
||||||
lua_getglobal(L, hook->func);
|
|
||||||
if(lua_pcall(L, 0, 0, 0))
|
|
||||||
// Error has accured.
|
// Error has accured.
|
||||||
WARN("Hook [%s] '%d' -> '%s' : %s", hook->stack,
|
WARN("Hook [%s] '%d' -> '%s' failed", hook->stack,
|
||||||
hook->parent, hook->func, lua_tostring(L, -1));
|
hook->id, hook->func);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add/Remove hooks.
|
// Add/Remove hooks.
|
||||||
int hook_add(lua_State* L, unsigned int parent, char* func, char* stack) {
|
int hook_add(Mission* misn, char* func, char* stack) {
|
||||||
Hook* new_hook;
|
Hook* new_hook;
|
||||||
|
|
||||||
// If the memory must grow.
|
// If the memory must grow.
|
||||||
@ -47,8 +44,7 @@ int hook_add(lua_State* L, unsigned int parent, char* func, char* stack) {
|
|||||||
// Create the new hook.
|
// Create the new hook.
|
||||||
new_hook = &hook_stack[hook_nstack];
|
new_hook = &hook_stack[hook_nstack];
|
||||||
new_hook->id = ++hook_id;
|
new_hook->id = ++hook_id;
|
||||||
new_hook->L = L;
|
new_hook->misn = misn;
|
||||||
new_hook->parent = parent;
|
|
||||||
new_hook->func = func;
|
new_hook->func = func;
|
||||||
new_hook->stack = stack;
|
new_hook->stack = stack;
|
||||||
|
|
||||||
@ -69,7 +65,10 @@ void hook_rm(int id) {
|
|||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(m == (hook_nstack-1)) return;
|
if(m == (hook_nstack-1)) {
|
||||||
|
hook_nstack--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Move it!
|
// Move it!
|
||||||
memmove(&hook_stack[m+1], &hook_stack[m+2], sizeof(Hook)*(hook_nstack-(m+2)));
|
memmove(&hook_stack[m+1], &hook_stack[m+2], sizeof(Hook)*(hook_nstack-(m+2)));
|
||||||
@ -79,7 +78,7 @@ void hook_rm(int id) {
|
|||||||
void hook_rmParent(unsigned int parent) {
|
void hook_rmParent(unsigned int parent) {
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < hook_nstack; i++)
|
for(i = 0; i < hook_nstack; i++)
|
||||||
if(parent == hook_stack[i].parent) {
|
if(parent == hook_stack[i].misn->id) {
|
||||||
hook_rm(hook_stack[i].id);
|
hook_rm(hook_stack[i].id);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "lua.h"
|
#include "mission.h"
|
||||||
|
|
||||||
// Add/Run hooks.
|
// Add/Run hooks.
|
||||||
int hook_add(lua_State* L, unsigned int parent, char* func, char* stack);
|
int hook_add(Mission* misn, char* func, char* stack);
|
||||||
void hook_rm(int id);
|
void hook_rm(int id);
|
||||||
void hook_rmParent(unsigned int parent);
|
void hook_rmParent(unsigned int parent);
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "space.h"
|
#include "space.h"
|
||||||
|
|
||||||
extern int landed;
|
extern int landed;
|
||||||
|
extern Planet* land_planet;
|
||||||
|
|
||||||
void land(Planet* p);
|
void land(Planet* p);
|
||||||
void takeoff(void);
|
void takeoff(void);
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
#include "space.h"
|
#include "space.h"
|
||||||
#include "toolkit.h"
|
#include "toolkit.h"
|
||||||
|
#include "land.h"
|
||||||
#include "misn_lua.h"
|
#include "misn_lua.h"
|
||||||
|
|
||||||
// FUCK LUA!!!
|
// FUCK LUA!!!
|
||||||
@ -17,6 +18,7 @@
|
|||||||
#define MIN_ARGS(n) if(lua_gettop(L) < n) return 0
|
#define MIN_ARGS(n) if(lua_gettop(L) < n) return 0
|
||||||
|
|
||||||
static Mission* cur_mission = NULL;
|
static Mission* cur_mission = NULL;
|
||||||
|
static int misn_delete = 0; // If 1 delete current mission.
|
||||||
|
|
||||||
// -- Libraries. --
|
// -- Libraries. --
|
||||||
|
|
||||||
@ -25,18 +27,22 @@ static int misn_setTitle(lua_State* L);
|
|||||||
static int misn_setDesc(lua_State* L);
|
static int misn_setDesc(lua_State* L);
|
||||||
static int misn_setReward(lua_State* L);
|
static int misn_setReward(lua_State* L);
|
||||||
static int misn_factions(lua_State* L);
|
static int misn_factions(lua_State* L);
|
||||||
|
static int misn_finish(lua_State* L);
|
||||||
static const luaL_Reg misn_methods[] = {
|
static const luaL_Reg misn_methods[] = {
|
||||||
{ "setTitle", misn_setTitle },
|
{ "setTitle", misn_setTitle },
|
||||||
{ "setDesc", misn_setDesc },
|
{ "setDesc", misn_setDesc },
|
||||||
{ "setReward", misn_setReward },
|
{ "setReward", misn_setReward },
|
||||||
{ "factions", misn_factions },
|
{ "factions", misn_factions },
|
||||||
|
{ "finish", misn_finish },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Space.
|
// Space.
|
||||||
static int space_getPlanet(lua_State* L);
|
static int space_getPlanet(lua_State* L);
|
||||||
|
static int space_landName(lua_State* L);
|
||||||
static const luaL_Reg space_methods[] = {
|
static const luaL_Reg space_methods[] = {
|
||||||
{ "getPlanet", space_getPlanet },
|
{ "getPlanet", space_getPlanet },
|
||||||
|
{ "landName", space_landName },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -91,15 +97,22 @@ int misn_loadLibs(lua_State* L) {
|
|||||||
// Run a mission function.
|
// Run a mission function.
|
||||||
int misn_run(Mission* misn, char* func) {
|
int misn_run(Mission* misn, char* func) {
|
||||||
int ret;
|
int ret;
|
||||||
|
char* err;
|
||||||
|
|
||||||
cur_mission = misn;
|
cur_mission = misn;
|
||||||
|
|
||||||
lua_getglobal(misn->L, func);
|
lua_getglobal(misn->L, func);
|
||||||
if((ret = lua_pcall(misn->L, 0, 0, 0)))
|
if((ret = lua_pcall(misn->L, 0, 0, 0))) {
|
||||||
// Did an oops.
|
// Did an oops.
|
||||||
WARN("Mission '%s' -> '%s' : %s",
|
err = (char*) lua_tostring(misn->L, -1);
|
||||||
cur_mission->data->name, func, lua_tostring(misn->L, -1));
|
if(strcmp(err, "Mission Finished"))
|
||||||
|
WARN("Mission '%s' -> '%s' : %s",
|
||||||
|
cur_mission->data->name, func, err);
|
||||||
|
else ret = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mission is finished.
|
||||||
|
if(misn_delete) mission_cleanup(cur_mission);
|
||||||
cur_mission = NULL;
|
cur_mission = NULL;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -146,13 +159,29 @@ static int misn_factions(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int misn_finish(lua_State* L) {
|
||||||
|
misn_delete = 1;
|
||||||
|
|
||||||
|
lua_pushstring(L, "Mission Finished");
|
||||||
|
lua_error(L); // Should not return..
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int space_getPlanet(lua_State* L) {
|
static int space_getPlanet(lua_State* L) {
|
||||||
(void)L;
|
|
||||||
// TODO: Proper getPlanet implementation.
|
// TODO: Proper getPlanet implementation.
|
||||||
lua_pushstring(L, "SaraCraft");
|
lua_pushstring(L, "KonoSphere");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int space_landName(lua_State* L) {
|
||||||
|
if(landed) {
|
||||||
|
lua_pushstring(L, land_planet->name);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// -- Player. --
|
// -- Player. --
|
||||||
static int player_freeSpace(lua_State* L) {
|
static int player_freeSpace(lua_State* L) {
|
||||||
lua_pushnumber(L, pilot_freeCargo(player));
|
lua_pushnumber(L, pilot_freeCargo(player));
|
||||||
@ -289,7 +318,7 @@ static int hook_land(lua_State* L) {
|
|||||||
cur_mission->data->name);
|
cur_mission->data->name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
hook_add(L, cur_mission->id, func, "land");
|
hook_add(cur_mission, func, "land");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,9 +59,6 @@ extern int pilots;
|
|||||||
// Space stuff for GUI.
|
// Space stuff for GUI.
|
||||||
extern StarSystem* systems_stack;
|
extern StarSystem* systems_stack;
|
||||||
|
|
||||||
// Land stuff for the player_stack.
|
|
||||||
extern Planet* land_planet;
|
|
||||||
|
|
||||||
// GUI crap.
|
// GUI crap.
|
||||||
typedef struct Radar_ {
|
typedef struct Radar_ {
|
||||||
double x,y; // Position.
|
double x,y; // Position.
|
||||||
|
@ -147,7 +147,7 @@ void spfx_clear(void) {
|
|||||||
|
|
||||||
static void spfx_destroy(SPFX* layer, int* nlayer, int spfx) {
|
static void spfx_destroy(SPFX* layer, int* nlayer, int spfx) {
|
||||||
(*nlayer)--;
|
(*nlayer)--;
|
||||||
memmove(&layer[spfx], &layer[spfx+1], (*nlayer-spfx)*sizeof(SPFX));
|
memmove(&layer[spfx], &layer[spfx+1], (*nlayer-spfx-1)*sizeof(SPFX));
|
||||||
}
|
}
|
||||||
|
|
||||||
void spfx_update(const double dt) {
|
void spfx_update(const double dt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user