naev 0.11.5
nlua_commodity.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_commodity.h"
16
17#include "array.h"
18#include "log.h"
19#include "ndata.h"
20#include "nlua_faction.h"
21#include "nlua_spob.h"
22#include "nlua_system.h"
23#include "nlua_time.h"
24#include "nlua_tex.h"
25#include "nluadef.h"
26#include "rng.h"
27
28/* Commodity metatable methods. */
29static int commodityL_eq( lua_State *L );
30static int commodityL_get( lua_State *L );
31static int commodityL_getStandard( lua_State *L );
32static int commodityL_flags( lua_State *L );
33static int commodityL_name( lua_State *L );
34static int commodityL_nameRaw( lua_State *L );
35static int commodityL_price( lua_State *L );
36static int commodityL_priceAt( lua_State *L );
37static int commodityL_priceAtTime( lua_State *L );
38static int commodityL_canSell( lua_State *L );
39static int commodityL_canBuy( lua_State *L );
40static int commodityL_icon( lua_State *L );
41static int commodityL_description( lua_State *L );
42static int commodityL_new( lua_State *L );
43static int commodityL_illegalto( lua_State *L );
44static int commodityL_illegality( lua_State *L );
45static const luaL_Reg commodityL_methods[] = {
46 { "__tostring", commodityL_name },
47 { "__eq", commodityL_eq },
48 { "get", commodityL_get },
49 { "getStandard", commodityL_getStandard },
50 { "flags", commodityL_flags },
51 { "name", commodityL_name },
52 { "nameRaw", commodityL_nameRaw },
53 { "price", commodityL_price },
54 { "priceAt", commodityL_priceAt },
55 { "priceAtTime", commodityL_priceAtTime },
56 { "canSell", commodityL_canSell },
57 { "canBuy", commodityL_canBuy },
58 { "icon", commodityL_icon },
59 { "description", commodityL_description },
60 { "new", commodityL_new },
61 { "illegalto", commodityL_illegalto },
62 { "illegality", commodityL_illegality },
63 {0,0}
64};
72int nlua_loadCommodity( nlua_env env )
73{
74 nlua_register(env, COMMODITY_METATABLE, commodityL_methods, 1);
75 return 0;
76}
77
100Commodity* lua_tocommodity( lua_State *L, int ind )
101{
102 return *((Commodity**) lua_touserdata(L,ind));
103}
111Commodity* luaL_checkcommodity( lua_State *L, int ind )
112{
113 if (lua_iscommodity(L,ind))
114 return lua_tocommodity(L,ind);
115 luaL_typerror(L, ind, COMMODITY_METATABLE);
116 return NULL;
117}
125Commodity* luaL_validcommodity( lua_State *L, int ind )
126{
127 Commodity *o;
128
129 if (lua_iscommodity(L, ind))
130 o = luaL_checkcommodity(L, ind);
131 else if (lua_isstring(L, ind))
132 o = commodity_get( lua_tostring(L, ind) );
133 else {
134 luaL_typerror(L, ind, COMMODITY_METATABLE);
135 return NULL;
136 }
137
138 if (o == NULL)
139 NLUA_ERROR(L, _("Commodity is invalid."));
140
141 return o;
142}
150Commodity** lua_pushcommodity( lua_State *L, Commodity* commodity )
151{
152 Commodity **o = (Commodity**) lua_newuserdata(L, sizeof(Commodity*));
153 *o = commodity;
154 luaL_getmetatable(L, COMMODITY_METATABLE);
155 lua_setmetatable(L, -2);
156 return o;
157}
165int lua_iscommodity( lua_State *L, int ind )
166{
167 int ret;
168
169 if (lua_getmetatable(L,ind)==0)
170 return 0;
171 lua_getfield(L, LUA_REGISTRYINDEX, COMMODITY_METATABLE);
172
173 ret = 0;
174 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
175 ret = 1;
176
177 lua_pop(L, 2); /* remove both metatables */
178 return ret;
179}
180
191static int commodityL_eq( lua_State *L )
192{
193 const Commodity *a, *b;
194 a = luaL_checkcommodity(L,1);
195 b = luaL_checkcommodity(L,2);
196 if (a == b)
197 lua_pushboolean(L,1);
198 else
199 lua_pushboolean(L,0);
200 return 1;
201}
202
212static int commodityL_get( lua_State *L )
213{
214 /* Handle parameters. */
215 const char *name = luaL_checkstring(L,1);
216
217 /* Get commodity. */
218 Commodity *commodity = commodity_get( name );
219 if (commodity == NULL) {
220 return NLUA_ERROR(L,_("Commodity '%s' not found!"), name);
221 }
222
223 /* Push. */
224 lua_pushcommodity(L, commodity);
225 return 1;
226}
227
234static int commodityL_getStandard( lua_State *L )
235{
236 /* Get commodity. */
237 Commodity **standard = standard_commodities();
238 /* Push. */
239 lua_newtable( L );
240 for (int i=0; i<array_size(standard); i++) {
241 lua_pushcommodity( L, standard[i] );
242 lua_rawseti( L, -2, i+1 );
243 }
244 array_free( standard );
245 return 1;
246}
247
254static int commodityL_flags( lua_State *L )
255{
256 const Commodity *c = luaL_validcommodity(L,1);
257 lua_newtable(L);
258
259 lua_pushboolean(L, commodity_isFlag(c,COMMODITY_FLAG_STANDARD));
260 lua_setfield(L, -2, "standard");
261
262 lua_pushboolean(L, commodity_isFlag(c,COMMODITY_FLAG_ALWAYS_CAN_SELL));
263 lua_setfield(L, -2, "always_can_sell");
264
265 lua_pushboolean(L, commodity_isFlag(c,COMMODITY_FLAG_PRICE_CONSTANT));
266 lua_setfield(L, -2, "price_constant");
267
268 return 1;
269}
270
284static int commodityL_name( lua_State *L )
285{
286 const Commodity *c = luaL_validcommodity(L,1);
287 lua_pushstring(L, _(c->name));
288 return 1;
289}
290
305static int commodityL_nameRaw( lua_State *L )
306{
307 const Commodity *c = luaL_validcommodity(L,1);
308 lua_pushstring(L, c->name);
309 return 1;
310}
311
321static int commodityL_price( lua_State *L )
322{
323 const Commodity *c = luaL_validcommodity(L,1);
324 lua_pushnumber(L, c->price);
325 return 1;
326}
327
338static int commodityL_priceAt( lua_State *L )
339{
340 const Commodity *c;
341 const Spob *p;
342 const StarSystem *sys;
343 const char *sysname;
344
345 c = luaL_validcommodity(L,1);
346 p = luaL_validspob(L,2);
347 sysname = spob_getSystem( p->name );
348 if (sysname == NULL)
349 return NLUA_ERROR( L, _("Spob '%s' does not belong to a system."), p->name );
350 sys = system_get( sysname );
351 if (sys == NULL)
352 return NLUA_ERROR( L, _("Spob '%s' can not find its system '%s'."), p->name, sysname );
353
354 lua_pushnumber( L, spob_commodityPrice( p, c ) );
355 return 1;
356}
357
369static int commodityL_priceAtTime( lua_State *L )
370{
371 const Commodity *c;
372 const Spob *p;
373 const StarSystem *sys;
374 const char *sysname;
375 ntime_t t;
376 c = luaL_validcommodity(L,1);
377 p = luaL_validspob(L,2);
378 t = luaL_validtime(L, 3);
379 sysname = spob_getSystem( p->name );
380 if (sysname == NULL)
381 return NLUA_ERROR( L, _("Spob '%s' does not belong to a system."), p->name );
382 sys = system_get( sysname );
383 if (sys == NULL)
384 return NLUA_ERROR( L, _("Spob '%s' can not find its system '%s'."), p->name, sysname );
385
386 lua_pushnumber( L, spob_commodityPriceAtTime( p, c, t ) );
387 return 1;
388}
389
390static int spob_hasCommodity( const Commodity *c, const Spob *s )
391{
392 for (int i=0; i<array_size(s->commodities); i++) {
393 const Commodity *sc = s->commodities[i];
394 if (sc==c)
395 return 1;
396 }
397
398 return 0;
399}
400
410static int commodityL_canSell( lua_State *L )
411{
412 const Commodity *c = luaL_validcommodity(L,1);
413
414 if (commodity_isFlag( c, COMMODITY_FLAG_ALWAYS_CAN_SELL )) {
415 lua_pushboolean(L,1);
416 return 1;
417 }
418
419 if (lua_issystem(L,2)) {
420 const StarSystem *s = luaL_validsystem(L,2);
421 for (int i=0; i<array_size(s->spobs); i++) {
422 if (spob_hasCommodity( c, s->spobs[i] )) {
423 lua_pushboolean(L,1);
424 return 1;
425 }
426 }
427 }
428 else {
429 const Spob *s = luaL_validspob(L,2);
430 lua_pushboolean(L, spob_hasCommodity( c, s ) );
431 return 1;
432 }
433
434 lua_pushboolean(L,0);
435 return 1;
436}
437
447static int commodityL_canBuy( lua_State *L )
448{
449 const Commodity *c = luaL_validcommodity(L,1);
450
451 if (lua_issystem(L,2)) {
452 const StarSystem *s = luaL_validsystem(L,2);
453 for (int i=0; i<array_size(s->spobs); i++) {
454 if (spob_hasCommodity( c, s->spobs[i] )) {
455 lua_pushboolean(L,1);
456 return 1;
457 }
458 }
459 }
460 else {
461 const Spob *s = luaL_validspob(L,2);
462 lua_pushboolean(L, spob_hasCommodity( c, s ) );
463 return 1;
464 }
465
466 lua_pushboolean(L,0);
467 return 1;
468}
469
477static int commodityL_icon( lua_State *L )
478{
479 const Commodity *c = luaL_validcommodity(L,1);
480 if (c->gfx_store==NULL)
481 return 0;
482 lua_pushtex(L,gl_dupTexture(c->gfx_store));
483 return 1;
484}
485
493static int commodityL_description( lua_State *L )
494{
495 const Commodity *c = luaL_validcommodity(L,1);
496 if (c->description==NULL)
497 return 0;
498 lua_pushstring(L,c->description);
499 return 1;
500}
501
515static int commodityL_new( lua_State *L )
516{
517 const char *cname, *cdesc;
518 char str[STRMAX_SHORT];
519 Commodity *cargo;
520
521 /* Parameters. */
522 cname = luaL_checkstring(L,1);
523 cdesc = luaL_checkstring(L,2);
524
525 cargo = commodity_getW(cname);
526 if ((cargo != NULL) && !cargo->istemp)
527 return NLUA_ERROR(L,_("Trying to create new cargo '%s' that would shadow existing non-temporary cargo!"), cname);
528
529 if (cargo==NULL)
530 cargo = commodity_newTemp( cname, cdesc );
531
532 if (!lua_isnoneornil(L,3)) {
533 const char *buf;
534 lua_getfield(L,3,"gfx_space");
535 buf = luaL_optstring(L,-1,NULL);
536 if (buf) {
538 snprintf( str, sizeof(str), COMMODITY_GFX_PATH"space/%s", buf );
539 cargo->gfx_space = gl_newImage( str, 0 );
540 }
541 }
542
543 lua_pushcommodity(L, cargo);
544 return 1;
545}
546
554static int commodityL_illegalto( lua_State *L )
555{
557 if (lua_istable(L,2)) {
558 lua_pushnil(L); /* nil */
559 while (lua_next(L,-2) != 0) { /* k, v */
560 int f = luaL_validfaction(L,-1);
562 lua_pop(L,1); /* k */
563 }
564 }
565 else {
566 int f = luaL_validfaction(L,2);
568 }
569 return 0;
570}
571
579static int commodityL_illegality( lua_State *L )
580{
581 const Commodity *c = luaL_validcommodity(L,1);
582 lua_newtable(L);
583 for (int i=0; i<array_size(c->illegalto); i++) {
584 lua_pushfaction( L, c->illegalto[i] );
585 lua_rawseti( L, -2, i+1 );
586 }
587 return 1;
588}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:158
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
Commodity * commodity_get(const char *name)
Gets a commodity by name.
Definition commodity.c:127
Commodity * commodity_newTemp(const char *name, const char *desc)
Creates a new temporary commodity.
Definition commodity.c:419
Commodity ** standard_commodities(void)
Return an array (array.h) of standard commodities. Free with array_free. (Don't free contents....
Definition commodity.c:240
Commodity * commodity_getW(const char *name)
Gets a commodity by name without warning.
Definition commodity.c:142
int commodity_tempIllegalto(Commodity *com, int faction)
Makes a temporary commodity illegal to something.
Definition commodity.c:436
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.
Definition nlua_spob.c:174
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.
Definition nlua_tex.c:130
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
Definition nlua_time.c:115
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
Definition opengl_tex.c:917
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
Definition opengl_tex.c:675
void gl_freeTexture(glTexture *texture)
Frees a texture.
Definition opengl_tex.c:862
static const double c[]
Definition rng.c:264
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition space.c:960
const char * spob_getSystem(const char *spobname)
Get the name of a system from a spobname.
Definition space.c:1025
credits_t spob_commodityPrice(const Spob *p, const Commodity *c)
Gets the price of a commodity at a spob.
Definition space.c:276
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.
Definition space.c:290
Represents a commodity.
Definition commodity.h:43
glTexture * gfx_space
Definition commodity.h:54
int istemp
Definition commodity.h:58
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:89
Commodity ** commodities
Definition space.h:116