15#include "nlua_commodity.h"
20#include "nlua_faction.h"
22#include "nlua_system.h"
102 return *((
Commodity**) lua_touserdata(L,ind));
115 luaL_typerror(L, ind, COMMODITY_METATABLE);
131 else if (lua_isstring(L, ind))
132 o = commodity_get( lua_tostring(L, ind) );
134 luaL_typerror(L, ind, COMMODITY_METATABLE);
139 NLUA_ERROR(L, _(
"Commodity is invalid."));
155 luaL_getmetatable(L, COMMODITY_METATABLE);
156 lua_setmetatable(L, -2);
170 if (lua_getmetatable(L,ind)==0)
172 lua_getfield(L, LUA_REGISTRYINDEX, COMMODITY_METATABLE);
175 if (lua_rawequal(L, -1, -2))
198 lua_pushboolean(L,1);
200 lua_pushboolean(L,0);
216 const char *name = luaL_checkstring(L,1);
219 Commodity *commodity = commodity_get( name );
220 if (commodity == NULL) {
221 NLUA_ERROR(L,_(
"Commodity '%s' not found!"), name);
239 Commodity **standard = standard_commodities();
244 lua_rawseti( L, -2, i+1 );
261 lua_pushboolean(L, commodity_isFlag(
c,COMMODITY_FLAG_STANDARD));
262 lua_setfield(L, -2,
"standard");
264 lua_pushboolean(L, commodity_isFlag(
c,COMMODITY_FLAG_ALWAYS_CAN_SELL));
265 lua_setfield(L, -2,
"always_can_sell");
267 lua_pushboolean(L, commodity_isFlag(
c,COMMODITY_FLAG_PRICE_CONSTANT));
268 lua_setfield(L, -2,
"price_constant");
289 lua_pushstring(L, _(
c->name));
310 lua_pushstring(L,
c->name);
326 lua_pushnumber(L,
c->price);
350 if (sysname == NULL) {
351 NLUA_ERROR( L, _(
"Spob '%s' does not belong to a system."), p->name );
356 NLUA_ERROR( L, _(
"Spob '%s' can not find its system '%s'."), p->name, sysname );
386 if (sysname == NULL) {
387 NLUA_ERROR( L, _(
"Spob '%s' does not belong to a system."), p->name );
392 NLUA_ERROR( L, _(
"Spob '%s' can not find its system '%s'."), p->name, sysname );
424 if (commodity_isFlag(
c, COMMODITY_FLAG_ALWAYS_CAN_SELL )) {
425 lua_pushboolean(L,1);
432 if (spob_hasCommodity(
c, s->spobs[i] )) {
433 lua_pushboolean(L,1);
440 lua_pushboolean(L, spob_hasCommodity(
c, s ) );
444 lua_pushboolean(L,0);
464 if (spob_hasCommodity(
c, s->spobs[i] )) {
465 lua_pushboolean(L,1);
472 lua_pushboolean(L, spob_hasCommodity(
c, s ) );
476 lua_pushboolean(L,0);
490 if (
c->gfx_store==NULL)
506 if (
c->description==NULL)
508 lua_pushstring(L,
c->description);
527 const char *cname, *cdesc, *
buf;
528 char str[STRMAX_SHORT];
532 cname = luaL_checkstring(L,1);
533 cdesc = luaL_checkstring(L,2);
535 cargo = commodity_getW(cname);
536 if ((cargo != NULL) && !cargo->
istemp)
537 NLUA_ERROR(L,_(
"Trying to create new cargo '%s' that would shadow existing non-temporary cargo!"), cname);
540 cargo = commodity_newTemp( cname, cdesc );
542 if (!lua_isnoneornil(L,3)) {
543 lua_getfield(L,3,
"gfx_space");
544 buf = luaL_optstring(L,-1,NULL);
547 snprintf( str,
sizeof(str), COMMODITY_GFX_PATH
"space/%s", buf );
566 if (lua_istable(L,2)) {
568 while (lua_next(L,-2) != 0) {
570 commodity_tempIllegalto(
c, f );
576 commodity_tempIllegalto(
c, f );
594 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.
Header file with generic functions and naev-specifics.
static char buf[NEWS_MAX_LENGTH]
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.
char * spob_getSystem(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,...