/** * @brief Gets the presence in the system. * * Possible parameters are besides a faction:<br/> * - "all": Gets the sum of all the presences.<br /> * - "friendly": Gets the sum of all the friendly presences.<br /> * - "hostile": Gets the sum of all the hostile presences.<br /> * - "neutral": Gets the sum of all the neutral presences.<br /> * * @usage p = sys:presence( f ) -- Gets the presence of a faction f * @usage p = sys:presence( "all" ) -- Gets the sum of all the presences * @usage if sys:presence("friendly") > sys:presence("hostile") then -- Checks to see if the system is dominantly friendly * * @luaparam s System to get presence level of. * @luareturn The presence level in sys (absolute value). * @luafunc presence( s ) */ static int systemL_presence( lua_State *L ) { StarSystem *sys; LuaFaction *lf; int *fct; int nfct; double presence; int i; const char *cmd; /* Get parameters. */ sys = luaL_validsystem(L, 1); /* Get the second parameter. */ if (lua_isstring(L, 2)) { /* A string command has been given. */ cmd = lua_tostring(L, 2); nfct = 0; /* Check the command string and get the appropriate faction group.*/ if(strcmp(cmd, "all") == 0) fct = faction_getGroup(&nfct, 0); else if(strcmp(cmd, "friendly") == 0) fct = faction_getGroup(&nfct, 1); else if(strcmp(cmd, "hostile") == 0) fct = faction_getGroup(&nfct, 3); else if(strcmp(cmd, "neutral") == 0) fct = faction_getGroup(&nfct, 2); else /* Invalid command string. */ NLUA_INVALID_PARAMETER(L); } else if (lua_isfaction(L, 2)) { /* A faction id was given. */ lf = lua_tofaction(L, 2); nfct = 1; fct = malloc(sizeof(int) * nfct); fct[0] = lf->f; } else NLUA_INVALID_PARAMETER(L); /* Add up the presence values. */ presence = 0; for(i=0; i<nfct; i++) presence += system_getPresence( sys, fct[i] ); /* Clean up after ourselves. */ free(fct); /* Push it back to Lua. */ lua_pushnumber(L, presence); return 1; }
/** * @brief Gets jump distance from current system, or to another. * * Does different things depending on the parameter type: * - nil : Gets distance from current system. * - string : Gets distance from system matching name. * - system : Gets distance from system * * @usage d = sys:jumpDist() -- Distance from current system. * @usage d = sys:jumpDist( "Draygar" ) -- Distance from system Draygar. * @usage d = sys:jumpDist( another_sys ) -- Distance from system another_sys. * * @luaparam s System to get distance from. * @luaparam param See description. * @luaparam hidden Whether or not to consider hidden jumps. * @luareturn Number of jumps to system. * @luafunc jumpDist( s, param, hidden ) */ static int systemL_jumpdistance( lua_State *L ) { StarSystem *sys, *sysp; StarSystem **s; int jumps; const char *start, *goal; int h; sys = luaL_validsystem(L,1); start = sys->name; h = lua_toboolean(L,3); if (lua_gettop(L) > 1) { if (lua_isstring(L,2)) goal = lua_tostring(L,2); else if (lua_issystem(L,2)) { sysp = luaL_validsystem(L,2); goal = sysp->name; } else NLUA_INVALID_PARAMETER(L); } else goal = cur_system->name; s = map_getJumpPath( &jumps, start, goal, 1, h, NULL ); free(s); lua_pushnumber(L,jumps); return 1; }
/** * @brief Gets a system. * * Behaves differently depending on what you pass as param: <br/> * - string : Gets the system by name. <br/> * - planet : Gets the system by planet. <br/> * * @usage sys = system.get( p ) -- Gets system where planet 'p' is located. * @usage sys = system.get( "Gamma Polaris" ) -- Gets the system by name. * * @luaparam param Read description for details. * @luareturn System metatable matching param. * @luafunc get( param ) */ static int systemL_get( lua_State *L ) { LuaSystem sys; StarSystem *ss; Planet *pnt; /* Invalid by default. */ sys.id = -1; /* Passing a string (systemname) */ if (lua_isstring(L,1)) { ss = system_get( lua_tostring(L,1) ); if (ss != NULL) sys.id = system_index( ss ); } /* Passing a planet */ else if (lua_isplanet(L,1)) { pnt = luaL_validplanet(L,1); ss = system_get( planet_getSystem( pnt->name ) ); if (ss != NULL) sys.id = system_index( ss ); } else NLUA_INVALID_PARAMETER(L); /* Error checking. */ if (sys.id < 0) { NLUA_ERROR(L, "No matching systems found."); return 0; } /* return the system */ lua_pushsystem(L,sys); return 1; }
/** * @brief Gets jump path from current system, or to another. * * Does different things depending on the parameter type: * - nil : Gets path from current system. * - string : Gets path from system matching name. * - system : Gets path from system * * @usage jumps = sys:jumpPath() -- Path to current system. * @usage jumps = sys:jumpPath( "Draygar" ) -- Path from current sys to Draygar. * @usage jumps = system.jumpPath( "Draygar", another_sys ) -- Path from Draygar to another_sys. * * @luatparam System s System to get path from. * @luatparam nil|string|System param See description. * @luatparam[opt=false] boolean hidden Whether or not to consider hidden jumps. * @luatreturn {Jump,...} Table of jumps. * @luafunc jumpPath( s, param, hidden ) */ static int systemL_jumpPath( lua_State *L ) { LuaJump lj; StarSystem *sys, *sysp; StarSystem **s; int i, sid, jumps, pushed, h; const char *start, *goal; h = lua_toboolean(L,3); /* Foo to Bar */ if (lua_gettop(L) > 1) { sys = luaL_validsystem(L,1); start = sys->name; sid = sys->id; if (lua_isstring(L,2)) goal = lua_tostring(L,2); else if (lua_issystem(L,2)) { sysp = luaL_validsystem(L,2); goal = sysp->name; } else NLUA_INVALID_PARAMETER(L); } /* Current to Foo */ else { start = cur_system->name; sid = cur_system->id; sys = luaL_validsystem(L,1); goal = sys->name; } s = map_getJumpPath( &jumps, start, goal, 1, h, NULL ); if (s == NULL) return 0; /* Create the jump table. */ lua_newtable(L); pushed = 0; /* Map path doesn't contain the start system, push it manually. */ lj.srcid = sid; lj.destid = s[0]->id; lua_pushnumber(L, ++pushed); /* key. */ lua_pushjump(L, lj); /* value. */ lua_rawset(L, -3); for (i=0; i<(jumps - 1); i++) { lj.srcid = s[i]->id; lj.destid = s[i+1]->id; lua_pushnumber(L, ++pushed); /* key. */ lua_pushjump(L, lj); /* value. */ lua_rawset(L, -3); } free(s); return 1; }
/** * @brief Sets the pilot's faction. * * @usage p:setFaction( "Empire" ) * @usage p:setFaction( faction.get( "Dvaered" ) ) * * @luaparam p Pilot to change faction of. * @luaparam faction Faction to set by name or faction. * @luafunc setFaction( faction ) */ static int pilotL_setFaction( lua_State *L ) { Pilot *p; LuaPilot *lp; LuaFaction *f; int fid; const char *faction; /* Parse parameters. */ lp = luaL_checkpilot(L,1); if (lua_isstring(L,2)) { faction = lua_tostring(L,2); fid = faction_get(faction); } else if (lua_isfaction(L,2)) { f = lua_tofaction(L,2); fid = f->f; } else NLUA_INVALID_PARAMETER(); /* Get pilot/faction. */ p = pilot_get(lp->pilot); if (p==NULL) { NLUA_ERROR(L,"Pilot is invalid."); return 0; } /* Set the new faction. */ p->faction = fid; return 0; }
/** * @brief Checks to see if a faction has presence in a system. * * This checks to see if the faction has a possibility of having any ships at all * be randomly generated in the system. * * @usage if sys:hasPresence( "Empire" ) then -- Checks to see if Empire has ships in the system * @usage if sys:hasPresence( faction.get("Pirate") ) then -- Checks to see if the Pirate has ships in the system * * @luaparam s System to check to see if has presence of a certain faction. * @luaparam f Faction or name of faction to check to see if has presence in the system. * @luareturn true If faction has presence in the system, false otherwise. * @luafunc hasPresence( s, f ) */ static int systemL_hasPresence( lua_State *L ) { LuaFaction *lf; StarSystem *s; int fct; int i, found; s = luaL_validsystem(L,1); /* Get the second parameter. */ if (lua_isstring(L,2)) { fct = faction_get( lua_tostring(L,2) ); } else if (lua_isfaction(L,2)) { lf = lua_tofaction(L,2); fct = lf->f; } else NLUA_INVALID_PARAMETER(L); /* Try to find a fleet of the faction. */ found = 0; for (i=0; i<s->npresence; i++) { if (s->presence[i].faction == fct) { found = 1; break; } } lua_pushboolean(L, found); return 1; }
static int vectorL_sub__( lua_State *L ) { Vector2d *v1, *v2; double x, y; /* Get self. */ v1 = luaL_checkvector(L,1); /* Get rest of parameters. */ v2 = NULL; if (lua_isvector(L,2)) { v2 = lua_tovector(L,2); x = v2->x; y = v2->y; } else if ((lua_gettop(L) > 2) && lua_isnumber(L,2) && lua_isnumber(L,3)) { x = lua_tonumber(L,2); y = lua_tonumber(L,3); } else { NLUA_INVALID_PARAMETER(L); return 0; } /* Actually add it */ vect_cset( v1, v1->x - x, v1->y - y ); lua_pushvector( L, *v1 ); return 1; }
/** * @brief Gets a colour. * * @usage colour.new( "Red" ) -- Gets colour by name * @usage colour.new( "Red", 0.5 ) -- Gets colour by name with alpha 0.5 * @usage colour.new() -- Creates a white (blank) colour * @usage colour.new( 1., 0., 0. ) -- Creates a bright red colour * @usage colour.new( 1., 0., 0., 0.5 ) -- Creates a bright red colour with alpha 0.5 * * @luaparam r Red value of the colour. * @luaparam g Green value of the colour. * @luaparam b Blue value of the colour. * @luaparam a Alpha value of the colour. * @luareturn A newly created colour. * @luafunc new( r, g, b, a ) */ static int colL_new( lua_State *L ) { glColour *col; LuaColour lc; if (lua_gettop(L)==0) { lc.col.r = lc.col.g = lc.col.b = lc.col.a = 1.; } else if (lua_isnumber(L,1)) { lc.col.r = luaL_checknumber(L,1); lc.col.g = luaL_checknumber(L,2); lc.col.b = luaL_checknumber(L,3); if (lua_isnumber(L,4)) lc.col.a = luaL_checknumber(L,4); else lc.col.a = 1.; } else if (lua_isstring(L,1)) { col = col_fromName( lua_tostring(L,1) ); if (col == NULL) { NLUA_ERROR( L, "Colour '%s' does not exist!", lua_tostring(L,1) ); return 0; } memcpy( &lc.col, col, sizeof(glColour) ); if (lua_isnumber(L,2)) lc.col.a = luaL_checknumber(L,2); else lc.col.a = 1.; } else NLUA_INVALID_PARAMETER(L); lua_pushcolour( L, lc ); return 1; }
/** * @brief Sets the active land window. * * Valid windows are:<br/> * - main<br/> * - bar<br/> * - missions<br/> * - outfits<br/> * - shipyard<br/> * - equipment<br/> * - commodity<br/> * * @usage player.landWindow( "outfits" ) * @luaparam winname Name of the window. * @luareturn True on success. * @luafunc landwindow( winname ) */ static int playerL_landWindow( lua_State *L ) { int ret; const char *str; int win; if (!landed) { NLUA_ERROR(L, "Must be landed to set the active land window."); return 0; } str = luaL_checkstring(L,1); if (strcasecmp(str,"main")==0) win = LAND_WINDOW_MAIN; else if (strcasecmp(str,"bar")==0) win = LAND_WINDOW_BAR; else if (strcasecmp(str,"missions")==0) win = LAND_WINDOW_MISSION; else if (strcasecmp(str,"outfits")==0) win = LAND_WINDOW_OUTFITS; else if (strcasecmp(str,"shipyard")==0) win = LAND_WINDOW_SHIPYARD; else if (strcasecmp(str,"equipment")==0) win = LAND_WINDOW_EQUIPMENT; else if (strcasecmp(str,"commodity")==0) win = LAND_WINDOW_COMMODITY; else NLUA_INVALID_PARAMETER(L); /* Sets the window. */ ret = land_setWindow( win ); lua_pushboolean( L, !ret ); return 1; }
/** * @brief Subtracts two vectors or a vector and some cartesian coordinates. * * If x is a vector it subtracts both vectors, otherwise it subtracts cartesian * coordinates to the vector. * * @usage my_vec = my_vec - your_vec * @usage my_vec:sub( your_vec ) * @usage my_vec:sub( 5, 3 ) * * @luaparam v Vector getting stuff subtracted from. * @luaparam x X coordinate or vector to subtract. * @luaparam y Y coordinate or nil to subtract. * @luareturn The result of the vector operation. * @luafunc sub( v, x, y ) */ static int vectorL_sub( lua_State *L ) { LuaVector vout, *v1, *v2; double x, y; /* Get self. */ v1 = luaL_checkvector(L,1); /* Get rest of parameters. */ v2 = NULL; if (lua_isvector(L,2)) { v2 = lua_tovector(L,2); x = v2->vec.x; y = v2->vec.y; } else if ((lua_gettop(L) > 2) && lua_isnumber(L,2) && lua_isnumber(L,3)) { x = lua_tonumber(L,2); y = lua_tonumber(L,3); } else NLUA_INVALID_PARAMETER(L); /* Actually add it */ vect_cset( &vout.vec, v1->vec.x - x, v1->vec.y - y ); lua_pushvector( L, vout ); return 1; }
/** * @brief Gets the standing of the player with a certain faction. * * @luaparam faction Faction to get the standing of. * @luareturn The faction standing. * @luafunc getFaction( faction ) */ static int playerL_getFaction( lua_State *L ) { NLUA_MIN_ARGS(1); int f; if (lua_isstring(L,1)) f = faction_get( lua_tostring(L,1) ); else NLUA_INVALID_PARAMETER(); lua_pushnumber(L, faction_getPlayer(f)); return 1; }
/** * @brief Tries to claim systems. * * Claiming systems is a way to avoid mission collisions preemptively. * * Note it does not actually claim the systems if it fails to claim. It also * does not work more then once. * * @usage if not misn.claim( { system.get("Gamma Polaris") } ) then misn.finish( false ) end * @usage if not misn.claim( system.get("Gamma Polaris") ) then misn.finish( false ) end * * @luaparam systems Table of systems to claim or a single system. * @luareturn true if was able to claim, false otherwise. * @luafunc claim( systems ) */ static int misn_claim( lua_State *L ) { int i, l; LuaSystem *ls; SysClaim_t *claim; Mission *cur_mission; /* Get mission. */ cur_mission = misn_getFromLua(L); /* Check to see if already claimed. */ if (cur_mission->claims != NULL) { NLUA_ERROR(L, "Mission trying to claim but already has."); return 0; } /* Create the claim. */ claim = claim_create(); if (lua_istable(L,1)) { /* Iterate over table. */ l = lua_objlen(L,1); for (i=0; i<l; i++) { lua_pushnumber(L,i+1); lua_gettable(L,1); if (lua_issystem(L,-1)) { ls = lua_tosystem( L, -1 ); claim_add( claim, ls->id ); } lua_pop(L,1); } } else if (lua_issystem(L, 1)) { ls = lua_tosystem( L, 1 ); claim_add( claim, ls->id ); } else NLUA_INVALID_PARAMETER(L); /* Test claim. */ if (claim_test( claim )) { claim_destroy( claim ); lua_pushboolean(L,0); return 1; } /* Set the claim. */ cur_mission->claims = claim; claim_activate( claim ); lua_pushboolean(L,1); return 1; }
/** * @brief Increases the player's standing to a faction by a fixed amount without * touching other faction standings. * * @luaparam faction Name of the faction. * @luaparam mod Amount to modify standing by. * @luafunc modFactionRaw( faction, mod ) */ static int playerL_modFactionRaw( lua_State *L ) { NLUA_MIN_ARGS(2); int f; double mod; if (lua_isstring(L,1)) f = faction_get( lua_tostring(L,1) ); else NLUA_INVALID_PARAMETER(); mod = luaL_checknumber(L,2); faction_modPlayerRaw( f, mod ); return 0; }
/** * @brief Tries to claim systems. * * Claiming systems is a way to avoid mission/event collisions preemptively. * * Note it does not actually claim the systems if it fails to claim. It also * does not work more then once. * * @usage if not evt.claim( { system.get("Gamma Polaris") } ) then evt.finish( false ) end * @usage if not evt.claim( system.get("Gamma Polaris") ) then evt.finish( false ) end * * @luaparam systems Table of systems to claim or a single system. * @luareturn true if was able to claim, false otherwise. * @luafunc claim( systems ) */ static int evt_claim( lua_State *L ) { LuaSystem *ls; SysClaim_t *claim; Event_t *cur_event; /* Get current event. */ cur_event = event_getFromLua(L); /* Check to see if already claimed. */ if (cur_event->claims != NULL) { NLUA_ERROR(L, "Event trying to claim but already has."); return 0; } /* Create the claim. */ claim = claim_create(); /* Handle parameters. */ if (lua_istable(L,1)) { /* Iterate over table. */ lua_pushnil(L); while (lua_next(L, 1) != 0) { if (lua_issystem(L,-1)) { ls = lua_tosystem( L, -1 ); claim_add( claim, ls->id ); } lua_pop(L,1); } } else if (lua_issystem(L, 1)) { ls = lua_tosystem( L, 1 ); claim_add( claim, ls->id ); } else NLUA_INVALID_PARAMETER(L); /* Test claim. */ if (claim_test( claim )) { claim_destroy( claim ); lua_pushboolean(L,0); return 1; } /* Set the claim. */ cur_event->claims = claim; claim_activate( claim ); lua_pushboolean(L,1); return 1; }
/** * @brief Gets all matching articles in a table. * @luaparam characteristic characteristic to match, or no parameter for all articles * @luareturn a table with matching articles * @luafunc get(characteristic) */ int newsL_get( lua_State *L ) { LuaArticle Larticle; ntime_t date; news_t *article_ptr; char *characteristic; int k, print_all; date = -1; article_ptr = news_list; characteristic = NULL; print_all = 0; if (lua_isnil(L, 1) || lua_gettop(L) == 0) /* Case no argument */ print_all = 1; else if (lua_isnumber(L, 1)) date = (ntime_t)lua_tonumber(L, 1); else if (lua_isstring(L, 1)) characteristic = strdup(lua_tostring(L, 1)); else NLUA_INVALID_PARAMETER(L); /* Bad Parameter */ /* Now put all the matching articles in a table. */ lua_newtable(L); k = 1; do { if (article_ptr->title == NULL || article_ptr->desc == NULL || article_ptr->faction == NULL) continue; if (print_all || date == article_ptr->date || (characteristic && (!strcmp(article_ptr->title, characteristic) || !strcmp(article_ptr->desc, characteristic) || !strcmp(article_ptr->faction, characteristic))) || (article_ptr->tag != NULL && !strcmp(article_ptr->tag, characteristic))) { lua_pushnumber(L, k++); /* key */ Larticle = article_ptr->id; lua_pusharticle(L, Larticle); /* value */ lua_rawset(L, -3); /* table[key] = value */ } } while ((article_ptr = article_ptr->next) != NULL); free(characteristic); return 1; }
/** * @brief Increases the player's standing to a faction by an amount. This will * affect player's standing with that faction's allies and enemies also. * * @luaparam faction Name of the faction. * @luaparam mod Amount to modify standing by. * @luafunc modFaction( faction, mod ) */ static int playerL_modFaction( lua_State *L ) { int f; double mod; if (lua_isstring(L,1)) f = faction_get( lua_tostring(L,1) ); else { NLUA_INVALID_PARAMETER(L); return 0; } mod = luaL_checknumber(L,2); faction_modPlayer( f, mod ); return 0; }
/** * @brief Gets a jump. * * Possible values of params: <br/> * - string : Gets the jump by system name. <br/> * - system : Gets the jump by system. <br/> * * @usage j,r = jump.get( "Ogat", "Goddard" ) -- Returns the Ogat to Goddard and Goddard to Ogat jumps. * @luaparam param See description. * @luareturn Returns the jump and the inverse (where it exits). * @luafunc get( param ) */ static int jumpL_get( lua_State *L ) { LuaJump lj; StarSystem *a, *b; /* Defaults. */ a = NULL; b = NULL; 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) ); 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) ); if ((a == NULL) || (b == NULL)) { NLUA_ERROR(L, "No matching jump points found."); return 0; } if (jump_getTarget(b, a) != NULL) { lj.srcid = a->id; lj.destid = b->id; lua_pushjump(L, lj); /* The inverse. If it doesn't exist, there are bigger problems. */ lj.srcid = b->id; lj.destid = a->id; lua_pushjump(L, lj); return 2; } } else NLUA_INVALID_PARAMETER(L); return 0; }
/** * @brief Gets a random number. With no parameters it returns a random float between * 0 and 1. * * With one parameter it returns a whole number between 0 and that number * (both included). With two parameters it returns a whole number between * both parameters (both included). * * @usage n = rnd() -- Number in range [0:1]. * @usage n = rnd(5) -- Number in range [0:5]. * @usage n = rnd(3,5) -- Number in range [3,5]. * * @luaparam x First parameter, read description for details. * @luaparam y Second parameter, read description for details. * @luareturn A randomly generated number, read description for details. * @luafunc rnd( x, y ) */ static int rnd_int( lua_State *L ) { int o; int l,h; o = lua_gettop(L); if (o==0) lua_pushnumber(L, RNGF() ); /* random double 0 <= x <= 1 */ else if (o==1) { /* random int 0 <= x <= parameter */ l = luaL_checkint(L,1); lua_pushnumber(L, RNG(0, l)); } else if (o>=2) { /* random int parameter 1 <= x <= parameter 2 */ l = luaL_checkint(L,1); h = luaL_checkint(L,2); lua_pushnumber(L, RNG(l,h)); } else NLUA_INVALID_PARAMETER(L); return 1; /* unless it's returned 0 already it'll always return a parameter */ }
/** * @brief Teleports the player to a new system (only if not landed). * * Does not change the position nor velocity of the player.p, which will probably be wrong in the new system. * * @usage player.teleport( system.get("Arcanis") ) -- Teleports the player to arcanis. * @usage player.teleport( "Arcanis" ) -- Teleports the player to arcanis. * * @luaparam sys System or name of a system to teleport the player to. * @luafunc teleport( sys ) */ static int playerL_teleport( lua_State *L ) { LuaSystem *sys; const char *name; /* Must not be landed. */ if (landed) NLUA_ERROR(L,"Can not teleport the player while landed!"); if (comm_isOpen()) NLUA_ERROR(L,"Can not teleport the player while the comm is open!"); if (player_isBoarded()) NLUA_ERROR(L,"Can not teleport the player while he is boarded!"); /* Get a system. */ if (lua_issystem(L,1)) { sys = lua_tosystem(L,1); name = system_getIndex(sys->id)->name; } else if (lua_isstring(L,1)) name = lua_tostring(L,1); else NLUA_INVALID_PARAMETER(L); /* Check if system exists. */ if (!system_exists( name )) { NLUA_ERROR( L, "System '%s' does not exist.", name ); return 0; } /* Jump out hook is run first. */ hooks_run( "jumpout" ); /* Just in case remove hyperspace flags. */ pilot_rmFlag( player.p, PILOT_HYPERSPACE ); pilot_rmFlag( player.p, PILOT_HYP_BEGIN ); pilot_rmFlag( player.p, PILOT_HYP_BRAKE ); pilot_rmFlag( player.p, PILOT_HYP_PREP ); /* Free graphics. */ space_gfxUnload( cur_system ); /* Go to the new system. */ space_init( name ); /* Map gets deformed when jumping this way. */ map_clear(); /* Add the escorts. */ player_addEscorts(); /* Run hooks - order is important. */ hooks_run( "jumpin" ); hooks_run( "enter" ); events_trigger( EVENT_TRIGGER_ENTER ); /* Reset targets when teleporting */ player_targetPlanetSet( -1 ); player_targetHyperspaceSet( -1 ); gui_setNav(); return 0; }
/** * @brief Teleports the player to a new planet or system (only if not landed). * * If the destination is a system, the coordinates of the player will not change. * If the destination is a planet, the player will be placed over that planet. * * @usage player.teleport( system.get("Arcanis") ) -- Teleports the player to Arcanis. * @usage player.teleport( "Arcanis" ) -- Teleports the player to Arcanis. * @usage player.teleport( "Dvaer Prime" ) -- Teleports the player to Dvaer, and relocates him to Dvaer Prime. * * @luaparam dest System or name of a system or planet or name of a planet to teleport the player to. * @luafunc teleport( dest ) */ static int playerL_teleport( lua_State *L ) { Planet *pnt; StarSystem *sys; const char *name, *pntname; /* Must not be landed. */ if (landed) NLUA_ERROR(L,"Can not teleport the player while landed!"); if (comm_isOpen()) NLUA_ERROR(L,"Can not teleport the player while the comm is open!"); if (player_isBoarded()) NLUA_ERROR(L,"Can not teleport the player while he is boarded!"); pnt = NULL; /* Get a system. */ if (lua_issystem(L,1)) { sys = luaL_validsystem(L,1); name = system_getIndex(sys->id)->name; } /* Get a planet. */ else if (lua_isplanet(L,1)) { pnt = luaL_validplanet(L,1); name = planet_getSystem( pnt->name ); if (name == NULL) { NLUA_ERROR( L, "Planet '%s' does not belong to a system..", pnt->name ); return 0; } } /* Get destination from string. */ else if (lua_isstring(L,1)) { name = lua_tostring(L,1); if (!system_exists( name )) { if (!planet_exists( name )) { NLUA_ERROR( L, "'%s' is not a valid teleportation target.", name ); return 0; } /* No system found, assume destination string is the name of a planet. */ pntname = name; name = planet_getSystem( name ); pnt = planet_get( pntname ); if (name == NULL) { NLUA_ERROR( L, "Planet '%s' does not belong to a system..", pntname ); return 0; } } } else NLUA_INVALID_PARAMETER(L); /* Check if system exists. */ if (!system_exists( name )) { NLUA_ERROR( L, "System '%s' does not exist.", name ); return 0; } /* Jump out hook is run first. */ hooks_run( "jumpout" ); /* Just in case remove hyperspace flags. */ pilot_rmFlag( player.p, PILOT_HYPERSPACE ); pilot_rmFlag( player.p, PILOT_HYP_BEGIN ); pilot_rmFlag( player.p, PILOT_HYP_BRAKE ); pilot_rmFlag( player.p, PILOT_HYP_PREP ); /* Free graphics. */ space_gfxUnload( cur_system ); /* Go to the new system. */ space_init( name ); /* Map gets deformed when jumping this way. */ map_clear(); /* Add the escorts. */ player_addEscorts(); /* Run hooks - order is important. */ hooks_run( "jumpin" ); hooks_run( "enter" ); events_trigger( EVENT_TRIGGER_ENTER ); missions_run( MIS_AVAIL_SPACE, -1, NULL, NULL ); /* Reset targets when teleporting */ player_targetPlanetSet( -1 ); player_targetHyperspaceSet( -1 ); gui_setNav(); /* Move to planet. */ if (pnt != NULL) vectcpy( &player.p->solid->pos, &pnt->pos ); return 0; }
/** * @brief Gets a planet. * * Possible values of param: * - nil : Gets the current landed planet or nil if there is none. * - bool : Gets a random planet. * - faction : Gets random planet belonging to faction matching the number. * - string : Gets the planet by name. * - table : Gets random planet belonging to any of the factions in the * table. * * @usage p,s = planet.get( "Anecu" ) -- Gets planet by name * @usage p,s = planet.get( faction.get( "Empire" ) ) -- Gets random Empire planet * @usage p,s = planet.get(true) -- Gets completely random planet * @usage p,s = planet.get( { faction.get("Empire"), faction.get("Dvaered") } ) -- Random planet belonging to Empire or Dvaered * @luaparam param See description. * @luareturn Returns the planet and the system it belongs to. * @luafunc get( param ) */ static int planetL_get( lua_State *L ) { int i; int *factions; int nfactions; char **planets; int nplanets; const char *rndplanet; LuaPlanet planet; LuaSystem sys; LuaFaction *f; rndplanet = NULL; nplanets = 0; /* Get the landed planet */ if (lua_gettop(L) == 0) { if (land_planet != NULL) { planet.p = land_planet; lua_pushplanet(L,planet); sys.s = system_get( planet_getSystem(land_planet->name) ); lua_pushsystem(L,sys); return 2; } NLUA_ERROR(L,"Attempting to get landed planet when player not landed."); return 0; /* Not landed. */ } /* If boolean return random. */ else if (lua_isboolean(L,1)) { planet.p = planet_get( space_getRndPlanet() ); lua_pushplanet(L,planet); sys.s = system_get( planet_getSystem(land_planet->name) ); lua_pushsystem(L,sys); return 2; } /* Get a planet by faction */ else if (lua_isfaction(L,1)) { f = lua_tofaction(L,1); planets = space_getFactionPlanet( &nplanets, &f->f, 1 ); } /* Get a planet by name */ else if (lua_isstring(L,1)) { rndplanet = lua_tostring(L,1); } /* Get a planet from faction list */ else if (lua_istable(L,1)) { /* Get table length and preallocate. */ nfactions = (int) lua_objlen(L,1); factions = malloc( sizeof(int) * nfactions ); /* Load up the table. */ lua_pushnil(L); i = 0; while (lua_next(L, -2) != 0) { f = lua_tofaction(L, -1); factions[i++] = f->f; lua_pop(L,1); } /* get the planets */ planets = space_getFactionPlanet( &nplanets, factions, nfactions ); free(factions); } else NLUA_INVALID_PARAMETER(); /* Bad Parameter */ /* No suitable planet found */ if ((rndplanet == NULL) && (nplanets == 0)) { free(planets); return 0; } /* Pick random planet */ else if (rndplanet == NULL) { rndplanet = planets[RNG(0,nplanets-1)]; free(planets); } /* Push the planet */ planet.p = planet_get(rndplanet); /* The real planet */ lua_pushplanet(L,planet); sys.s = system_get( planet_getSystem(rndplanet) ); lua_pushsystem(L,sys); return 2; }
static int planetL_getBackend( lua_State *L, int landable ) { int i; int *factions; int nfactions; char **planets; int nplanets; const char *rndplanet; LuaSystem luasys; LuaFaction f; Planet *pnt; StarSystem *sys; char *sysname; rndplanet = NULL; planets = NULL; nplanets = 0; /* If boolean return random. */ if (lua_isboolean(L,1)) { pnt = planet_get( space_getRndPlanet(landable, 0, NULL) ); lua_pushplanet(L,planet_index( pnt )); luasys = system_index( system_get( planet_getSystem(pnt->name) ) ); lua_pushsystem(L,luasys); return 2; } /* Get a planet by faction */ else if (lua_isfaction(L,1)) { f = lua_tofaction(L,1); planets = space_getFactionPlanet( &nplanets, &f, 1, landable ); } /* Get a planet by name */ else if (lua_isstring(L,1)) { rndplanet = lua_tostring(L,1); if (landable) { pnt = planet_get( rndplanet ); if (pnt == NULL) { NLUA_ERROR(L, _("Planet '%s' not found in stack"), rndplanet); return 0; } /* Check if can land. */ planet_updateLand( pnt ); if (!pnt->can_land) return 0; } } /* Get a planet from faction list */ else if (lua_istable(L,1)) { /* Get table length and preallocate. */ nfactions = (int) lua_objlen(L,1); factions = malloc( sizeof(int) * nfactions ); /* Load up the table. */ lua_pushnil(L); i = 0; while (lua_next(L, -2) != 0) { if (lua_isfaction(L, -1)) factions[i++] = lua_tofaction(L, -1); lua_pop(L,1); } /* get the planets */ planets = space_getFactionPlanet( &nplanets, factions, nfactions, landable ); free(factions); } else NLUA_INVALID_PARAMETER(L); /* Bad Parameter */ /* No suitable planet found */ if ((rndplanet == NULL) && ((planets == NULL) || nplanets == 0)) return 0; /* Pick random planet */ else if (rndplanet == NULL) { planets = (char**) arrayShuffle( (void**)planets, nplanets ); for (i=0; i<nplanets; i++) { if (landable) { /* Check landing. */ pnt = planet_get( planets[i] ); if (pnt == NULL) continue; planet_updateLand( pnt ); if (!pnt->can_land) continue; } rndplanet = planets[i]; break; } free(planets); } /* Push the planet */ pnt = planet_get(rndplanet); /* The real planet */ if (pnt == NULL) { NLUA_ERROR(L, _("Planet '%s' not found in stack"), rndplanet); return 0; } sysname = planet_getSystem(rndplanet); if (sysname == NULL) { NLUA_ERROR(L, _("Planet '%s' is not placed in a system"), rndplanet); return 0; } sys = system_get( sysname ); if (sys == NULL) { NLUA_ERROR(L, _("Planet '%s' can't find system '%s'"), rndplanet, sysname); return 0; } lua_pushplanet(L,planet_index( pnt )); luasys = system_index( sys ); lua_pushsystem(L,luasys); return 2; }