15#include "nlua_commodity.h"
19#include "nlua_faction.h"
21#include "nlua_system.h"
100 return *( (
Commodity **)lua_touserdata( L, ind ) );
114 luaL_typerror( L, ind, COMMODITY_METATABLE );
130 else if ( lua_isstring( L, ind ) )
133 luaL_typerror( L, ind, COMMODITY_METATABLE );
138 NLUA_ERROR( L, _(
"Commodity is invalid." ) );
153 luaL_getmetatable( L, COMMODITY_METATABLE );
154 lua_setmetatable( L, -2 );
168 if ( lua_getmetatable( L, ind ) == 0 )
170 lua_getfield( L, LUA_REGISTRYINDEX, COMMODITY_METATABLE );
173 if ( lua_rawequal( L, -1, -2 ) )
196 lua_pushboolean( L, 1 );
198 lua_pushboolean( L, 0 );
214 const char *name = luaL_checkstring( L, 1 );
218 if ( commodity == NULL ) {
219 return NLUA_ERROR( L, _(
"Commodity '%s' not found!" ), name );
240 for (
int i = 0; i <
array_size( standard ); i++ ) {
242 lua_rawseti( L, -2, i + 1 );
260 lua_pushboolean( L, commodity_isFlag(
c, COMMODITY_FLAG_STANDARD ) );
261 lua_setfield( L, -2,
"standard" );
263 lua_pushboolean( L, commodity_isFlag(
c, COMMODITY_FLAG_ALWAYS_CAN_SELL ) );
264 lua_setfield( L, -2,
"always_can_sell" );
266 lua_pushboolean( L, commodity_isFlag(
c, COMMODITY_FLAG_PRICE_CONSTANT ) );
267 lua_setfield( L, -2,
"price_constant" );
288 lua_pushstring( L, _(
c->name ) );
309 lua_pushstring( L,
c->name );
325 lua_pushnumber( L,
c->price );
344 const StarSystem *sys;
350 if ( sysname == NULL )
351 return NLUA_ERROR( L, _(
"Spob '%s' does not belong to a system." ),
355 return NLUA_ERROR( L, _(
"Spob '%s' can not find its system '%s'." ),
378 const StarSystem *sys;
385 if ( sysname == NULL )
386 return NLUA_ERROR( L, _(
"Spob '%s' does not belong to a system." ),
390 return NLUA_ERROR( L, _(
"Spob '%s' can not find its system '%s'." ),
423 if ( commodity_isFlag(
c, COMMODITY_FLAG_ALWAYS_CAN_SELL ) ) {
424 lua_pushboolean( L, 1 );
430 for (
int i = 0; i <
array_size( s->spobs ); i++ ) {
431 if ( spob_hasCommodity(
c, s->spobs[i] ) ) {
432 lua_pushboolean( L, 1 );
438 lua_pushboolean( L, spob_hasCommodity(
c, s ) );
442 lua_pushboolean( L, 0 );
463 for (
int i = 0; i <
array_size( s->spobs ); i++ ) {
464 if ( spob_hasCommodity(
c, s->spobs[i] ) ) {
465 lua_pushboolean( L, 1 );
471 lua_pushboolean( L, spob_hasCommodity(
c, s ) );
475 lua_pushboolean( L, 0 );
490 if (
c->gfx_store == NULL )
507 if (
c->description == NULL )
509 lua_pushstring( L,
c->description );
533 const char *cname, *cdesc;
534 char str[STRMAX_SHORT];
538 cname = luaL_checkstring( L, 1 );
539 cdesc = luaL_checkstring( L, 2 );
542 if ( ( cargo != NULL ) && !cargo->
istemp )
543 return NLUA_ERROR( L,
544 _(
"Trying to create new cargo '%s' that would shadow "
545 "existing non-temporary cargo!" ),
551 if ( !lua_isnoneornil( L, 3 ) ) {
553 lua_getfield( L, 3,
"gfx_space" );
554 buf = luaL_optstring( L, -1, NULL );
557 snprintf( str,
sizeof( str ), COMMODITY_GFX_PATH
"space/%s", buf );
577 if ( lua_istable( L, 2 ) ) {
579 while ( lua_next( L, -2 ) != 0 ) {
602 for (
int i = 0; i <
array_size(
c->illegalto ); i++ ) {
604 lua_rawseti( L, -2, i + 1 );
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Commodity * commodity_get(const char *name)
Gets a commodity by name.
Commodity * commodity_newTemp(const char *name, const char *desc)
Creates a new temporary commodity.
Commodity ** standard_commodities(void)
Return an array (array.h) of standard commodities. Free with array_free. (Don't free contents....
Commodity * commodity_getW(const char *name)
Gets a commodity by name without warning.
int commodity_tempIllegalto(Commodity *com, int faction)
Makes a temporary commodity illegal to something.
Header file with generic functions and naev-specifics.
static int commodityL_eq(lua_State *L)
Checks to see if two commodities are the same.
static int commodityL_price(lua_State *L)
Gets the base price of an commodity.
static int commodityL_name(lua_State *L)
Gets the translated name of the commodity.
static int commodityL_canBuy(lua_State *L)
Sees if a commodity can be bought at either a spob or system.
static int commodityL_icon(lua_State *L)
Gets the store icon of a commodity if it exists.
Commodity * lua_tocommodity(lua_State *L, int ind)
Lua bindings to interact with commodities.
static int commodityL_description(lua_State *L)
Gets the description of a commodity if it exists.
static int commodityL_get(lua_State *L)
Gets a commodity.
static int commodityL_flags(lua_State *L)
Gets the flags that are set for a commodity.
static int commodityL_nameRaw(lua_State *L)
Gets the raw (untranslated) name of the commodity.
static int commodityL_priceAt(lua_State *L)
Gets the base price of an commodity on a certain spob.
int nlua_loadCommodity(nlua_env env)
Loads the commodity library.
Commodity ** lua_pushcommodity(lua_State *L, Commodity *commodity)
Pushes a commodity on the stack.
static int commodityL_illegality(lua_State *L)
Gets the factions to which the commodity is illegal to.
static int commodityL_illegalto(lua_State *L)
Makes a temporary commodity illegal to a faction.
static const luaL_Reg commodityL_methods[]
Commodity * luaL_validcommodity(lua_State *L, int ind)
Makes sure the commodity is valid or raises a Lua error.
static int commodityL_new(lua_State *L)
Creates a new temporary commodity. If a temporary commodity with the same name exists,...
static int commodityL_canSell(lua_State *L)
Sees if a commodity can be sold at either a spob or system.
int lua_iscommodity(lua_State *L, int ind)
Checks to see if ind is a commodity.
static int commodityL_getStandard(lua_State *L)
Gets the list of standard commodities.
static int commodityL_priceAtTime(lua_State *L)
Gets the price of an commodity on a certain spob at a certain time.
Commodity * luaL_checkcommodity(lua_State *L, int ind)
Gets commodity at index or raises error if there is no commodity at index.
LuaFaction * lua_pushfaction(lua_State *L, LuaFaction faction)
Pushes a faction on the stack.
LuaFaction luaL_validfaction(lua_State *L, int ind)
Gets faction (or faction name) at index, raising an error if type isn't a valid faction.
Spob * luaL_validspob(lua_State *L, int ind)
Gets a spob directly.
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
void gl_freeTexture(glTexture *texture)
Frees a texture.
StarSystem * system_get(const char *sysname)
Get the system from its name.
const char * spob_getSystemName(const char *spobname)
Get the name of a system from a spobname.
credits_t spob_commodityPrice(const Spob *p, const Commodity *c)
Gets the price of a commodity at a spob.
credits_t spob_commodityPriceAtTime(const Spob *p, const Commodity *c, ntime_t t)
Gets the price of a commodity at a spob at given time.
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...