Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a new Lua API module for jumps.

  • Loading branch information...
commit f51a17b3212395813a6da11bd6b0f1ef18a75407 1 parent 5ae508c
@Deiz Deiz authored
View
2  src/Makefile.am
@@ -77,6 +77,7 @@ CODE_SOURCE = \
nlua_gfx.c \
nlua_gui.c \
nlua_hook.c \
+ nlua_jump.c \
nlua_misn.c \
nlua_music.c \
nlua_naev.c \
@@ -214,6 +215,7 @@ CODE_SOURCE = \
nlua_gfx.h \
nlua_gui.h \
nlua_hook.h \
+ nlua_jump.h \
nlua_misn.h \
nlua_music.h \
nlua_naev.h \
View
326 src/nlua_jump.c
@@ -0,0 +1,326 @@
+/*
+ * See Licensing and Copyright notice in naev.h
+ */
+
+/**
+ * @file nlua_jump.c
+ *
+ * @brief Lua jump module.
+ */
+
+#include "nlua_jump.h"
+
+#include "naev.h"
+
+#include <lauxlib.h>
+
+#include "nlua.h"
+#include "nluadef.h"
+#include "nlua_vec2.h"
+#include "nlua_system.h"
+#include "log.h"
+#include "rng.h"
+#include "map.h"
+#include "nmath.h"
+
+
+/* Jump metatable methods */
+static int jumpL_get( lua_State *L );
+static int jumpL_eq( lua_State *L );
+static int jumpL_position( lua_State *L );
+static int jumpL_isKnown( lua_State *L );
+static int jumpL_setKnown( lua_State *L );
+static const luaL_reg jump_methods[] = {
+ { "get", jumpL_get },
+ { "__eq", jumpL_eq },
+ { "pos", jumpL_position },
+ { "isKnown", jumpL_isKnown },
+ { "setKnown", jumpL_setKnown },
+ {0,0}
+}; /**< Jump metatable methods. */
+
+
+/**
+ * @brief Loads the jump library.
+ *
+ * @param L State to load jump library into.
+ * @param readonly Load read only functions?
+ * @return 0 on success.
+ */
+int nlua_loadJump( lua_State *L, int readonly )
+{
+ (void) readonly;
+ /* Create the metatable */
+ luaL_newmetatable(L, JUMP_METATABLE);
+
+ /* Create the access table */
+ lua_pushvalue(L,-1);
+ lua_setfield(L,-2,"__index");
+
+ /* Register the values */
+ luaL_register(L, NULL, jump_methods);
+
+ /* Clean up. */
+ lua_setfield(L, LUA_GLOBALSINDEX, JUMP_METATABLE);
+
+ return 0; /* No error */
+}
+
+
+/**
+ * @brief This module allows you to handle the jumps from Lua.
+ *
+ * Generally you do something like:
+ *
+ * @code
+ * p,s = jump.get() -- Get current jump and system
+ * if p:services()["inhabited"] > 0 then -- jump is inhabited
+ * v = p:pos() -- Get the position
+ * -- Do other stuff
+ * end
+ * @endcode
+ *
+ * @luamod jump
+ */
+/**
+ * @brief Gets jump at index.
+ *
+ * @param L Lua state to get jump from.
+ * @param ind Index position to find the jump.
+ * @return Jump found at the index in the state.
+ */
+LuaJump* lua_tojump( lua_State *L, int ind )
+{
+ return (LuaJump*) lua_touserdata(L,ind);
+}
+/**
+ * @brief Gets jump at index raising an error if isn't a jump.
+ *
+ * @param L Lua state to get jump from.
+ * @param a Index to check.
+ * @return Jump found at the index in the state.
+ */
+LuaJump* luaL_checkjump( lua_State *L, int ind )
+{
+ if (lua_isjump(L,ind))
+ return lua_tojump(L,ind);
+ luaL_typerror(L, ind, JUMP_METATABLE);
+ return NULL;
+}
+/**
+ * @brief Gets a jump directly.
+ *
+ * @param L Lua state to get jump from.
+ * @param ind Index of source system.
+ * @param b Index of destination system.
+ * @return Jump found at the index in the state.
+ */
+JumpPoint* luaL_validjump( lua_State *L, int ind )
+{
+ LuaJump *lj;
+ JumpPoint *jp;
+ StarSystem *a, *b;
+
+ if (lua_isjump(L, ind)) {
+ lj = luaL_checkjump(L, ind);
+ a = system_getIndex( lj->sysid );
+ jp = &a->jumps[lj->id];
+ }
+ else if (lua_gettop(L) > 1) {
+ if (lua_isstring(L, ind))
+ a = system_get( lua_tostring( L, ind ));
+ else if (lua_issystem(L, ind))
+ a = system_getIndex( lua_tosystem(L, ind)->id );
+
+ if (lua_isstring(L, ind+1))
+ b = system_get( lua_tostring( L, ind+1 ));
+ else if (lua_issystem(L, ind+1))
+ b = system_getIndex( lua_tosystem(L, ind+1)->id );
+
+ if (b != NULL && a != NULL)
+ jp = jump_get( b->name, a );
+ }
+ else {
+ luaL_typerror(L, ind, JUMP_METATABLE);
+ return NULL;
+ }
+
+ if (jp == NULL)
+ NLUA_ERROR(L, "Jump is invalid");
+
+ return jp;
+}
+/**
+ * @brief Pushes a jump on the stack.
+ *
+ * @param L Lua state to push jump into.
+ * @param jump Jump to push.
+ * @return Newly pushed jump.
+ */
+LuaJump* lua_pushjump( lua_State *L, LuaJump jump )
+{
+ LuaJump *j;
+ j = (LuaJump*) lua_newuserdata(L, sizeof(LuaJump));
+ *j = jump;
+ luaL_getmetatable(L, JUMP_METATABLE);
+ lua_setmetatable(L, -2);
+ return j;
+}
+/**
+ * @brief Checks to see if ind is a jump.
+ *
+ * @param L Lua state to check.
+ * @param a Index of source system.
+ * @param b Index of destination system.
+ * @return 1 if ind is a jump.
+ */
+int lua_isjump( lua_State *L, int ind )
+{
+ int ret;
+
+ if (lua_getmetatable(L,ind)==0)
+ return 0;
+ lua_getfield(L, LUA_REGISTRYINDEX, JUMP_METATABLE);
+
+ ret = 0;
+ if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
+ ret = 1;
+
+ lua_pop(L, 2); /* remove both metatables */
+ return ret;
+}
+
+
+/**
+ * @brief Gets a jump.
+ *
+ * Possible values of param: <br/>
+ * - bool : Gets a random jump. <br/>
+ * - faction : Gets random jump belonging to faction matching the number. <br/>
+ * - string : Gets the jump by name. <br/>
+ * - table : Gets random jump belonging to any of the factions in the
+ * table. <br/>
+ *
+ * @usage p,s = jump.get( "Anecu" ) -- Gets jump by name
+ * @usage p,s = jump.get( faction.get( "Empire" ) ) -- Gets random Empire jump
+ * @usage p,s = jump.get(true) -- Gets completely random jump
+ * @usage p,s = jump.get( { faction.get("Empire"), faction.get("Dvaered") } ) -- Random jump belonging to Empire or Dvaered
+ * @luaparam param See description.
+ * @luareturn Returns the jump and the system it belongs to.
+ * @luafunc get( param )
+ */
+static int jumpL_get( lua_State *L )
+{
+ LuaJump lj;
+ int i;
+ StarSystem *a, *b;
+
+ if (lua_gettop(L) > 1) {
+ if (lua_isstring(L, 1))
+ a = system_get( lua_tostring(L, 1));
+ else if (lua_issystem(L, 1))
+ a = system_getIndex( lua_tosystem(L, 1)->id );
+
+ if (lua_isstring(L, 2))
+ b = system_get( lua_tostring(L, 2));
+ else if (lua_issystem(L, 2))
+ b = system_getIndex( lua_tosystem(L, 2)->id );
+
+ if ((a == NULL) || (b == NULL)) {
+ NLUA_ERROR(L, "No matching jump points found.");
+ return 0;
+ }
+
+ lj.sysid = a->id;
+ for (i=0; i<a->njumps; i++) {
+ if (a->jumps[i].targetid == b->id) {
+ lj.id = i;
+ lua_pushjump(L, lj);
+ return 1;
+ }
+ }
+ }
+ else
+ NLUA_INVALID_PARAMETER(L);
+
+ return 0;
+}
+
+
+/**
+ * @brief You can use the '=' operator within Lua to compare jumps with this.
+ *
+ * @usage if p.__eq( jump.get( "Anecu" ) ) then -- Do something
+ * @usage if p == jump.get( "Anecu" ) then -- Do something
+ * @luaparam p Jump comparing.
+ * @luaparam comp jump to compare against.
+ * @luareturn true if both jumps are the same.
+ * @luafunc __eq( p, comp )
+ */
+static int jumpL_eq( lua_State *L )
+{
+ LuaJump *a, *b;
+ a = luaL_checkjump(L,1);
+ b = luaL_checkjump(L,2);
+ lua_pushboolean(L,(a->id == b->id));
+ return 1;
+}
+
+
+/**
+ * @brief Gets the position of the jump in the system.
+ *
+ * @usage v = p:pos()
+ * @luaparam p Jump to get the position of.
+ * @luareturn The position of the jump in the system as a vec2.
+ * @luafunc pos( p )
+ */
+static int jumpL_position( lua_State *L )
+{
+ JumpPoint *jp;
+ LuaVector v;
+ jp = luaL_validjump(L,1);
+ vectcpy(&v.vec, &jp->pos);
+ lua_pushvector(L, v);
+ return 1;
+}
+
+
+/**
+ * @brief Checks to see if a jump is known by the player.
+ *
+ * @usage b = p:isKnown()
+ *
+ * @luaparam s Jump to check if the player knows.
+ * @luareturn true if the player knows the jump.
+ * @luafunc isKnown( p )
+ */
+static int jumpL_isKnown( lua_State *L )
+{
+ JumpPoint *p = luaL_validjump(L,1);
+ lua_pushboolean(L, jp_isKnown(p));
+ return 1;
+}
+
+/**
+ * @brief Sets a jumps's known state.
+ *
+ * @usage p:setKnown( false ) -- Makes jump unknown.
+ * @luaparam p Jump to set known.
+ * @luaparam b Whether or not to set as known (defaults to false).
+ * @luafunc setKnown( p, b )
+ */
+static int jumpL_setKnown( lua_State *L )
+{
+ int b;
+ JumpPoint *p;
+
+ p = luaL_validjump(L,1);
+ b = lua_toboolean(L, 2);
+
+ if (b)
+ jp_setFlag( p, JP_KNOWN );
+ else
+ jp_rmFlag( p, JP_KNOWN );
+ return 0;
+}
View
43 src/nlua_jump.h
@@ -0,0 +1,43 @@
+/*
+ * See Licensing and Copyright notice in naev.h
+ */
+
+
+#ifndef NLUA_JUMP_H
+# define NLUA_JUMP_H
+
+
+#include <lua.h>
+
+#include "space.h"
+
+
+#define JUMP_METATABLE "jump" /**< System metatable identifier. */
+
+
+/**
+ * @brief Lua StarSystem Wrapper.
+ */
+typedef struct LuaJump_s {
+ int id; /**< Jump point ID. */
+ int sysid; /**< Star system ID. */
+} LuaJump;
+
+
+/*
+ * Load the jump library.
+ */
+int nlua_loadJump( lua_State *L, int readonly );
+
+/*
+ * Jump operations.
+ */
+LuaJump* lua_tojump( lua_State *L, int ind );
+LuaJump* luaL_checkjump( lua_State *L, int ind );
+LuaJump* lua_pushjump( lua_State *L, LuaJump jump );
+JumpPoint* luaL_validjump( lua_State *L, int ind );
+int lua_isjump( lua_State *L, int ind );
+
+
+#endif /* NLUA_SYSTEM_H */
+
View
2  src/nlua_space.c
@@ -21,6 +21,7 @@
#include "nluadef.h"
#include "nlua_planet.h"
#include "nlua_system.h"
+#include "nlua_jump.h"
/**
@@ -34,6 +35,7 @@ int nlua_loadSpace( lua_State *L, int readonly )
{
nlua_loadPlanet( L, readonly );
nlua_loadSystem( L, readonly );
+ nlua_loadJump( L, readonly );
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.