[Add] Initial Mission work.

This commit is contained in:
Allanis 2013-03-28 15:55:58 +00:00
parent 8808864e56
commit 60e08a9e36
8 changed files with 296 additions and 4 deletions

17
dat/missions/.yaml Normal file
View File

@ -0,0 +1,17 @@
- Name : Welcome
Lua : welcome
Avail :
Location : None
- Name : Cargo Freight
Lua : cargo
Avail :
Location : Computer
Alliance : Neutral
Alliance : Empire United
- Name : Empire Recruitment
Lua : empire
Avail :
Location : Bar
Faction : Empire

17
dat/missions/cargo.lua Normal file
View File

@ -0,0 +1,17 @@
function create()
-- Target destination.
planet = space.getPlanet(misn.factions())
-- Missions generic.
misn_type = "Rush"
misn_setTitle("Rush Delivery to " .. planet)
-- More mission specifics.
carg_mass = rnd.int(10, 30)
carg.type = rnd.setDesc(string.format(
"%s needs a rush delivery of %d tons of %s by %s.",
planet, carg_mass, carg_type, "SOMEDAY"))
misn_setReward(string.format("%d Scred",
carg_mass * 1000 + rnd.int(0, 5000)))
end

View File

@ -135,8 +135,8 @@ static void map_render(double bx, double by, double w, double h) {
glColour* col;
r = 5.;
x = (bx - map_xpos + w/2) * 1.; // Map zoom.
y = (by - map_ypos + h/2) * 1.; // Map zoom.
x = (bx - map_xpos + w/2) * 1.;
y = (by - map_ypos + h/2) * 1.;
// Background
COLOUR(cBlack);
glBegin(GL_QUADS);

133
src/misn_lua.c Normal file
View File

@ -0,0 +1,133 @@
#include "lua.h"
#include "lauxlib.h"
#include "mission.h"
#include "log.h"
#include "lephisto.h"
#include "rng.h"
#include "space.h"
#include "misn_lua.h"
// FUCK LUA!!!
#define luaL_register(L,n,l) (luaL_openlib(L, (n),(l), 0))
#define MIN_ARGS(n) if(lua_gettop(L) < n) return 0
static Mission* cur_mission = NULL;
// -- Libraries. --
// Mission.
static int misn_setTitle(lua_State* L);
static int misn_setDesc(lua_State* L);
static int misn_setReward(lua_State* L);
static const luaL_Reg misn_methods[] = {
{ "setTitle", misn_setTitle },
{ "setDesc", misn_setDesc },
{ "setReward", misn_setReward },
{ 0, 0 }
};
// Space.
static int space_getPlanet(lua_State* L);
static const luaL_Reg space_methods[] = {
{ "getPlanet", space_getPlanet },
{ 0, 0 }
};
// RNG.
static int rnd_int(lua_State*L);
static const luaL_Reg rnd_methods[] = {
{ "int", rnd_int },
{ 0, 0 }
};
// Register all the libaries.
int misn_loadLibs(lua_State* L) {
luaL_register(L, "misn", misn_methods);
luaL_register(L, "space", space_methods);
luaL_register(L, "rnd", rnd_methods);
return 0;
}
// Run a mission function.
int misn_run(Mission* misn, char* func) {
int ret;
cur_mission = misn;
lua_getglobal(misn->L, func);
if((ret = lua_pcall(misn->L, 0, 0, 0)))
// Did an oops.
WARN("Mission '%s' -> '%s' : %s",
cur_mission->data->name, func, lua_tostring(misn->L, -1));
cur_mission = NULL;
return ret;
}
// -- Mission. --
static int misn_setTitle(lua_State* L) {
MIN_ARGS(1);
if(lua_isstring(L, -1)) {
if(cur_mission->title)
// Cleanup the old title.
free(cur_mission->title);
cur_mission->title = strdup(lua_tostring(L, -1));
}
return 0;
}
static int misn_setDesc(lua_State* L) {
MIN_ARGS(1);
if(lua_isstring(L, -1)) {
if(cur_mission->title)
// Cleanup the old description.
free(cur_mission->desc);
cur_mission->desc = strdup(lua_tostring(L, -1));
}
return 0;
}
static int misn_setReward(lua_State* L) {
MIN_ARGS(1);
if(lua_isstring(L, -1)) {
if(cur_mission->reward)
// Cleanup the old reward.
free(cur_mission->reward);
cur_mission->reward = strdup(lua_tostring(L, -1));
}
return 0;
}
static int space_getPlanet(lua_State* L) {
(void)L;
return 0;
}
static rnd_int(lua_State* L) {
int o;
o = lua_gettop(L);
if(o == 0) lua_pushnumber(L, RNGF()); // Random double o <= x <= 1.
else if(o == 1) {
// Random int 0 <= x <= param.
if(lua_isnumber(L, -1))
lua_pushnumber(L, RNG(0, (int)lua_tonumber(L, -1)));
else return 0;
}
else if(o >= 2) {
// Random int param 1 <= x <= param 2.
if(lua_isnumber(L, -1) && lua_isnumber(L, -2))
lua_pushnumber(L, RNG((int)lua_tonumber(L, -2), (int)lua_tonumber(L,-1)));
else return 0;
}
else return 0;
// Unless it's returned 0 already it'll always return param.
return 1;
}

7
src/misn_lua.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include "lua.h"
// Load the libraries for a lua state.
int misn_loadLibs(lua_State* L);

68
src/mission.c Normal file
View File

@ -0,0 +1,68 @@
#include <string.h>
#include <malloc.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "lephisto.h"
#include "log.h"
#include "mission.h"
// Current player missions.
Mission player_missions[MISSION_MAX];
// Mission stack.
//static Mission* mission_stack = NULL; // Unmuteable after creation.
//static int mission_nstack = 0;
extern int misn_run(Mission* misn, char* func);
// Create a mission.
int mission_create(MissionData* misn) {
int i;
// Find last mission.
for(i = 0; i < MISSION_MAX; i++)
if(player_missions[i].data == NULL) break;
// No missions left.
if(i >= MISSION_MAX) return -1;
player_missions[i].data = misn;
// Init lua.
player_missions[i].L = luaL_newstate();
luaopen_string(player_missions[i].L); // String.format can be useful..
misn_loadLibs(player_missions[i].L); // Load our custom libraries.
return 0;
}
// Load/Free.
int missions_load(void) {
#if 0
yaml_parser_t parser;
yaml_event_t input_event;
memset(&parser, 0, sizeof(parser));
memset(&input_event, 0, sizeof(input_event));
// Set up parser.
if(!yaml_parser_initialize(&parser)) {
ERR("Could not initialize the parser object");
return -1;
}
// Cleanup parser.
yaml_event_delete(&input_event);
yaml_parser_delete(&parser);
#endif
return 0;
}
void missions_free(void) {
}

51
src/mission.h Normal file
View File

@ -0,0 +1,51 @@
#pragma once
#include "faction.h"
#include "misn_lua.h"
#define MIS_AVAIL_COMPUTER 1
#define MIS_AVAIL_BAR 2
#define MIS_AVAIL_OUTFIT 3
#define MIS_AVAIL_SHIPYARD 4
#define MIS_AVAIL_LAND 5
// Flags.
#define mis_isFlag(m,f) ((m)->flags & (f))
#define mis_setFlag(m,f) ((m)->flags |= (f))
#define mis_rmFlag(m,f) ((m)->flags ^= (f))
typedef struct MissionData_ {
char* name; // the name of the mission.
// Availability.
struct {
int loc; // Location.
double chance; // Chance of it appearing.
// For specific cases.
char* planet;
char* system;
// For generic cases.
Faction* factions;
int nfactions;
} avail;
} MissionData;
// Active mission.
typedef struct Mission_ {
MissionData* data;
char* title; // Not to be confused with name..
char* desc; // Description of the mission.
char* reward; // Rewards - in text.
lua_State* L;
} Mission;
#define MISSION_MAX 6 // No sense in the player having unlimited missions..
extern Mission player_mission[MISSION_MAX];
// Load/Quit.
int missions_load(void);
void missions_free(void);

View File

@ -1,7 +1,6 @@
#pragma once
#include <limits.h>
#define RNG(L,H) ((int)L + (int)((double)(H-L+1) * (randfp()))
#define RNG(L,H) ((int)L + (int)((double)(H-L+1) * randfp()))
#define RNGF() (randfp())
void rng_init(void);