From 60e08a9e360845fa5015574131b6a640e4f39b7a Mon Sep 17 00:00:00 2001 From: Allanis Date: Thu, 28 Mar 2013 15:55:58 +0000 Subject: [PATCH] [Add] Initial Mission work. --- dat/missions/.yaml | 17 ++++++ dat/missions/cargo.lua | 17 ++++++ src/map.c | 4 +- src/misn_lua.c | 133 +++++++++++++++++++++++++++++++++++++++++ src/misn_lua.h | 7 +++ src/mission.c | 68 +++++++++++++++++++++ src/mission.h | 51 ++++++++++++++++ src/rng.h | 3 +- 8 files changed, 296 insertions(+), 4 deletions(-) create mode 100644 dat/missions/.yaml create mode 100644 dat/missions/cargo.lua create mode 100644 src/misn_lua.c create mode 100644 src/misn_lua.h create mode 100644 src/mission.c create mode 100644 src/mission.h diff --git a/dat/missions/.yaml b/dat/missions/.yaml new file mode 100644 index 0000000..ba60c47 --- /dev/null +++ b/dat/missions/.yaml @@ -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 + + diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua new file mode 100644 index 0000000..2d669b0 --- /dev/null +++ b/dat/missions/cargo.lua @@ -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 + diff --git a/src/map.c b/src/map.c index 194c46f..508410f 100644 --- a/src/map.c +++ b/src/map.c @@ -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); diff --git a/src/misn_lua.c b/src/misn_lua.c new file mode 100644 index 0000000..98ab2a2 --- /dev/null +++ b/src/misn_lua.c @@ -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; +} + diff --git a/src/misn_lua.h b/src/misn_lua.h new file mode 100644 index 0000000..5b98897 --- /dev/null +++ b/src/misn_lua.h @@ -0,0 +1,7 @@ +#pragma once + +#include "lua.h" + +// Load the libraries for a lua state. +int misn_loadLibs(lua_State* L); + diff --git a/src/mission.c b/src/mission.c new file mode 100644 index 0000000..cfcae6f --- /dev/null +++ b/src/mission.c @@ -0,0 +1,68 @@ +#include +#include + +#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) { + +} + diff --git a/src/mission.h b/src/mission.h new file mode 100644 index 0000000..2d16d0d --- /dev/null +++ b/src/mission.h @@ -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); + diff --git a/src/rng.h b/src/rng.h index e017a75..212e4eb 100644 --- a/src/rng.h +++ b/src/rng.h @@ -1,7 +1,6 @@ #pragma once -#include -#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);