naev 0.10.4
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;
153 o = (Commodity**) lua_newuserdata(L, sizeof(Commodity*));
154 *o = commodity;
155 luaL_getmetatable(L, COMMODITY_METATABLE);
156 lua_setmetatable(L, -2);
157 return o;
158}
166int lua_iscommodity( lua_State *L, int ind )
167{
168 int ret;
169
170 if (lua_getmetatable(L,ind)==0)
171 return 0;
172 lua_getfield(L, LUA_REGISTRYINDEX, COMMODITY_METATABLE);
173
174 ret = 0;
175 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
176 ret = 1;
177
178 lua_pop(L, 2); /* remove both metatables */
179 return ret;
180}
181
192static int commodityL_eq( lua_State *L )
193{
194 Commodity *a, *b;
195 a = luaL_checkcommodity(L,1);
196 b = luaL_checkcommodity(L,2);
197 if (a == b)
198 lua_pushboolean(L,1);
199 else
200 lua_pushboolean(L,0);
201 return 1;
202}
203
213static int commodityL_get( lua_State *L )
214{
215 /* Handle parameters. */
216 const char *name = luaL_checkstring(L,1);
217
218 /* Get commodity. */
219 Commodity *commodity = commodity_get( name );
220 if (commodity == NULL) {
221 NLUA_ERROR(L,_("Commodity '%s' not found!"), name);
222 return 0;
223 }
224
225 /* Push. */
226 lua_pushcommodity(L, commodity);
227 return 1;
228}
229
236static int commodityL_getStandard( lua_State *L )
237{
238 /* Get commodity. */
239 Commodity **standard = standard_commodities();
240 /* Push. */
241 lua_newtable( L );
242 for (int i=0; i<array_size(standard); i++) {
243 lua_pushcommodity( L, standard[i] );
244 lua_rawseti( L, -2, i+1 );
245 }
246 array_free( standard );
247 return 1;
248}
249
256static int commodityL_flags( lua_State *L )
257{
259 lua_newtable(L);
260
261 lua_pushboolean(L, commodity_isFlag(c,COMMODITY_FLAG_STANDARD));
262 lua_setfield(L, -2, "standard");
263
264 lua_pushboolean(L, commodity_isFlag(c,COMMODITY_FLAG_ALWAYS_CAN_SELL));
265 lua_setfield(L, -2, "always_can_sell");
266
267 lua_pushboolean(L, commodity_isFlag(c,COMMODITY_FLAG_PRICE_CONSTANT));
268 lua_setfield(L, -2, "price_constant");
269
270 return 1;
271}
272
286static int commodityL_name( lua_State *L )
287{
289 lua_pushstring(L, _(c->name));
290 return 1;
291}
292
307static int commodityL_nameRaw( lua_State *L )
308{
310 lua_pushstring(L, c->name);
311 return 1;
312}
313
323static int commodityL_price( lua_State *L )
324{
326 lua_pushnumber(L, c->price);
327 return 1;
328}
329
340static int commodityL_priceAt( lua_State *L )
341{
342 Commodity *c;
343 Spob *p;
344 StarSystem *sys;
345 char *sysname;
346
347 c = luaL_validcommodity(L,1);
348 p = luaL_validspob(L,2);
349 sysname = spob_getSystem( p->name );
350 if (sysname == NULL) {
351 NLUA_ERROR( L, _("Spob '%s' does not belong to a system."), p->name );
352 return 0;
353 }
354 sys = system_get( sysname );
355 if (sys == NULL) {
356 NLUA_ERROR( L, _("Spob '%s' can not find its system '%s'."), p->name, sysname );
357 return 0;
358 }
359
360 lua_pushnumber( L, spob_commodityPrice( p, c ) );
361 return 1;
362}
363
375static int commodityL_priceAtTime( lua_State *L )
376{
377 Commodity *c;
378 Spob *p;
379 StarSystem *sys;
380 char *sysname;
381 ntime_t t;
382 c = luaL_validcommodity(L,1);
383 p = luaL_validspob(L,2);
384 t = luaL_validtime(L, 3);
385 sysname = spob_getSystem( p->name );
386 if (sysname == NULL) {
387 NLUA_ERROR( L, _("Spob '%s' does not belong to a system."), p->name );
388 return 0;
389 }
390 sys = system_get( sysname );
391 if (sys == NULL) {
392 NLUA_ERROR( L, _("Spob '%s' can not find its system '%s'."), p->name, sysname );
393 return 0;
394 }
395
396 lua_pushnumber( L, spob_commodityPriceAtTime( p, c, t ) );
397 return 1;
398}
399
400static int spob_hasCommodity( const Commodity *c, const Spob *s )
401{
402 for (int i=0; i<array_size(s->commodities); i++) {
403 const Commodity *sc = s->commodities[i];
404 if (sc==c)
405 return 1;
406 }
407
408 return 0;
409}
410
420static int commodityL_canSell( lua_State *L )
421{
423
424 if (commodity_isFlag( c, COMMODITY_FLAG_ALWAYS_CAN_SELL )) {
425 lua_pushboolean(L,1);
426 return 1;
427 }
428
429 if (lua_issystem(L,2)) {
430 StarSystem *s = luaL_validsystem(L,2);
431 for (int i=0; i<array_size(s->spobs); i++) {
432 if (spob_hasCommodity( c, s->spobs[i] )) {
433 lua_pushboolean(L,1);
434 return 1;
435 }
436 }
437 }
438 else {
439 Spob *s = luaL_validspob(L,2);
440 lua_pushboolean(L, spob_hasCommodity( c, s ) );
441 return 1;
442 }
443
444 lua_pushboolean(L,0);
445 return 1;
446}
447
457static int commodityL_canBuy( lua_State *L )
458{
460
461 if (lua_issystem(L,2)) {
462 StarSystem *s = luaL_validsystem(L,2);
463 for (int i=0; i<array_size(s->spobs); i++) {
464 if (spob_hasCommodity( c, s->spobs[i] )) {
465 lua_pushboolean(L,1);
466 return 1;
467 }
468 }
469 }
470 else {
471 Spob *s = luaL_validspob(L,2);
472 lua_pushboolean(L, spob_hasCommodity( c, s ) );
473 return 1;
474 }
475
476 lua_pushboolean(L,0);
477 return 1;
478}
479
487static int commodityL_icon( lua_State *L )
488{
490 if (c->gfx_store==NULL)
491 return 0;
492 lua_pushtex(L,gl_dupTexture(c->gfx_store));
493 return 1;
494}
495
503static int commodityL_description( lua_State *L )
504{
506 if (c->description==NULL)
507 return 0;
508 lua_pushstring(L,c->description);
509 return 1;
510}
511
525static int commodityL_new( lua_State *L )
526{
527 const char *cname, *cdesc, *buf;
528 char str[STRMAX_SHORT];
529 Commodity *cargo;
530
531 /* Parameters. */
532 cname = luaL_checkstring(L,1);
533 cdesc = luaL_checkstring(L,2);
534
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);
538
539 if (cargo==NULL)
540 cargo = commodity_newTemp( cname, cdesc );
541
542 if (!lua_isnoneornil(L,3)) {
543 lua_getfield(L,3,"gfx_space");
544 buf = luaL_optstring(L,-1,NULL);
545 if (buf) {
547 snprintf( str, sizeof(str), COMMODITY_GFX_PATH"space/%s", buf );
548 cargo->gfx_space = gl_newImage( str, 0 );
549 }
550 }
551
552 lua_pushcommodity(L, cargo);
553 return 1;
554}
555
563static int commodityL_illegalto( lua_State *L )
564{
566 if (lua_istable(L,2)) {
567 lua_pushnil(L); /* nil */
568 while (lua_next(L,-2) != 0) { /* k, v */
569 int f = luaL_validfaction(L,-1);
570 commodity_tempIllegalto( c, f );
571 lua_pop(L,1); /* k */
572 }
573 }
574 else {
575 int f = luaL_validfaction(L,2);
576 commodity_tempIllegalto( c, f );
577 }
578 return 0;
579}
580
588static int commodityL_illegality( lua_State *L )
589{
591 lua_newtable(L);
592 for (int i=0; i<array_size(c->illegalto); i++) {
593 lua_pushfaction( L, c->illegalto[i] );
594 lua_rawseti( L, -2, i+1 );
595 }
596 return 1;
597}
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
Header file with generic functions and naev-specifics.
static char buf[NEWS_MAX_LENGTH]
Definition: news.c:45
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.
Definition: nlua_faction.c:192
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.
Definition: nlua_faction.c:177
Spob * luaL_validspob(lua_State *L, int ind)
Gets a spob directly.
Definition: nlua_spob.c:164
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
Definition: nlua_system.c:156
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
Definition: nlua_system.c:201
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:809
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
Definition: opengl_tex.c:570
void gl_freeTexture(glTexture *texture)
Frees a texture.
Definition: opengl_tex.c:755
static const double c[]
Definition: rng.c:264
static const double a[]
Definition: rng.c:247
static const double b[]
Definition: rng.c:256
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition: space.c:914
char * spob_getSystem(const char *spobname)
Get the name of a system from a spobname.
Definition: space.c:980
credits_t spob_commodityPrice(const Spob *p, const Commodity *c)
Gets the price of a commodity at a spob.
Definition: space.c:272
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:286
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:88
Commodity ** commodities
Definition: space.h:114