naev 0.12.6
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"
14
15#include "nlua_commodity.h"
16
17#include "array.h"
18#include "ndata.h"
19#include "nlua_faction.h"
20#include "nlua_spob.h"
21#include "nlua_system.h"
22#include "nlua_tex.h"
23#include "nlua_time.h"
24#include "nluadef.h"
25
26/* Commodity metatable methods. */
27static int commodityL_eq( lua_State *L );
28static int commodityL_get( lua_State *L );
29static int commodityL_getStandard( lua_State *L );
30static int commodityL_flags( lua_State *L );
31static int commodityL_name( lua_State *L );
32static int commodityL_nameRaw( lua_State *L );
33static int commodityL_price( lua_State *L );
34static int commodityL_priceAt( lua_State *L );
35static int commodityL_priceAtTime( lua_State *L );
36static int commodityL_canSell( lua_State *L );
37static int commodityL_canBuy( lua_State *L );
38static int commodityL_icon( lua_State *L );
39static int commodityL_description( lua_State *L );
40static int commodityL_new( lua_State *L );
41static int commodityL_illegalto( lua_State *L );
42static int commodityL_illegality( lua_State *L );
43
44static const luaL_Reg commodityL_methods[] = {
45 { "__tostring", commodityL_name },
46 { "__eq", commodityL_eq },
47 { "get", commodityL_get },
48 { "getStandard", commodityL_getStandard },
49 { "flags", commodityL_flags },
50 { "name", commodityL_name },
51 { "nameRaw", commodityL_nameRaw },
52 { "price", commodityL_price },
53 { "priceAt", commodityL_priceAt },
54 { "priceAtTime", commodityL_priceAtTime },
55 { "canSell", commodityL_canSell },
56 { "canBuy", commodityL_canBuy },
57 { "icon", commodityL_icon },
58 { "description", commodityL_description },
59 { "new", commodityL_new },
60 { "illegalto", commodityL_illegalto },
61 { "illegality", commodityL_illegality },
62 { 0, 0 } };
63
70int nlua_loadCommodity( nlua_env env )
71{
72 nlua_register( env, COMMODITY_METATABLE, commodityL_methods, 1 );
73 return 0;
74}
75
98Commodity *lua_tocommodity( lua_State *L, int ind )
99{
100 return *( (Commodity **)lua_touserdata( L, ind ) );
101}
102
110Commodity *luaL_checkcommodity( lua_State *L, int ind )
111{
112 if ( lua_iscommodity( L, ind ) )
113 return lua_tocommodity( L, ind );
114 luaL_typerror( L, ind, COMMODITY_METATABLE );
115 return NULL;
116}
117
124Commodity *luaL_validcommodity( lua_State *L, int ind )
125{
126 Commodity *o;
127
128 if ( lua_iscommodity( L, ind ) )
129 o = luaL_checkcommodity( L, ind );
130 else if ( lua_isstring( L, ind ) )
131 o = commodity_get( lua_tostring( L, ind ) );
132 else {
133 luaL_typerror( L, ind, COMMODITY_METATABLE );
134 return NULL;
135 }
136
137 if ( o == NULL )
138 NLUA_ERROR( L, _( "Commodity is invalid." ) );
139
140 return o;
141}
142
149Commodity **lua_pushcommodity( lua_State *L, Commodity *commodity )
150{
151 Commodity **o = (Commodity **)lua_newuserdata( L, sizeof( Commodity * ) );
152 *o = commodity;
153 luaL_getmetatable( L, COMMODITY_METATABLE );
154 lua_setmetatable( L, -2 );
155 return o;
156}
157
164int lua_iscommodity( lua_State *L, int ind )
165{
166 int ret;
167
168 if ( lua_getmetatable( L, ind ) == 0 )
169 return 0;
170 lua_getfield( L, LUA_REGISTRYINDEX, COMMODITY_METATABLE );
171
172 ret = 0;
173 if ( lua_rawequal( L, -1, -2 ) ) /* does it have the correct mt? */
174 ret = 1;
175
176 lua_pop( L, 2 ); /* remove both metatables */
177 return ret;
178}
179
190static int commodityL_eq( lua_State *L )
191{
192 const Commodity *a, *b;
193 a = luaL_checkcommodity( L, 1 );
194 b = luaL_checkcommodity( L, 2 );
195 if ( a == b )
196 lua_pushboolean( L, 1 );
197 else
198 lua_pushboolean( L, 0 );
199 return 1;
200}
201
211static int commodityL_get( lua_State *L )
212{
213 /* Handle parameters. */
214 const char *name = luaL_checkstring( L, 1 );
215
216 /* Get commodity. */
217 Commodity *commodity = commodity_get( name );
218 if ( commodity == NULL ) {
219 return NLUA_ERROR( L, _( "Commodity '%s' not found!" ), name );
220 }
221
222 /* Push. */
223 lua_pushcommodity( L, commodity );
224 return 1;
225}
226
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
255static int commodityL_flags( lua_State *L )
256{
257 const Commodity *c = luaL_validcommodity( L, 1 );
258 lua_newtable( L );
259
260 lua_pushboolean( L, commodity_isFlag( c, COMMODITY_FLAG_STANDARD ) );
261 lua_setfield( L, -2, "standard" );
262
263 lua_pushboolean( L, commodity_isFlag( c, COMMODITY_FLAG_ALWAYS_CAN_SELL ) );
264 lua_setfield( L, -2, "always_can_sell" );
265
266 lua_pushboolean( L, commodity_isFlag( c, COMMODITY_FLAG_PRICE_CONSTANT ) );
267 lua_setfield( L, -2, "price_constant" );
268
269 return 1;
270}
271
285static int commodityL_name( lua_State *L )
286{
287 const Commodity *c = luaL_validcommodity( L, 1 );
288 lua_pushstring( L, _( c->name ) );
289 return 1;
290}
291
306static int commodityL_nameRaw( lua_State *L )
307{
308 const Commodity *c = luaL_validcommodity( L, 1 );
309 lua_pushstring( L, c->name );
310 return 1;
311}
312
322static int commodityL_price( lua_State *L )
323{
324 const Commodity *c = luaL_validcommodity( L, 1 );
325 lua_pushnumber( L, c->price );
326 return 1;
327}
328
340static int commodityL_priceAt( lua_State *L )
341{
342 const Commodity *c;
343 const Spob *p;
344 const StarSystem *sys;
345 const char *sysname;
346
347 c = luaL_validcommodity( L, 1 );
348 p = luaL_validspob( L, 2 );
349 sysname = spob_getSystemName( p->name );
350 if ( sysname == NULL )
351 return NLUA_ERROR( L, _( "Spob '%s' does not belong to a system." ),
352 p->name );
353 sys = system_get( sysname );
354 if ( sys == NULL )
355 return NLUA_ERROR( L, _( "Spob '%s' can not find its system '%s'." ),
356 p->name, sysname );
357
358 lua_pushnumber( L, spob_commodityPrice( p, c ) );
359 return 1;
360}
361
374static int commodityL_priceAtTime( lua_State *L )
375{
376 const Commodity *c;
377 const Spob *p;
378 const StarSystem *sys;
379 const char *sysname;
380 ntime_t t;
381 c = luaL_validcommodity( L, 1 );
382 p = luaL_validspob( L, 2 );
383 t = luaL_validtime( L, 3 );
384 sysname = spob_getSystemName( p->name );
385 if ( sysname == NULL )
386 return NLUA_ERROR( L, _( "Spob '%s' does not belong to a system." ),
387 p->name );
388 sys = system_get( sysname );
389 if ( sys == NULL )
390 return NLUA_ERROR( L, _( "Spob '%s' can not find its system '%s'." ),
391 p->name, sysname );
392
393 lua_pushnumber( L, spob_commodityPriceAtTime( p, c, t ) );
394 return 1;
395}
396
397static int spob_hasCommodity( const Commodity *c, const Spob *s )
398{
399 for ( int i = 0; i < array_size( s->commodities ); i++ ) {
400 const Commodity *sc = s->commodities[i];
401 if ( sc == c )
402 return 1;
403 }
404
405 return 0;
406}
407
419static int commodityL_canSell( lua_State *L )
420{
421 const Commodity *c = luaL_validcommodity( L, 1 );
422
423 if ( commodity_isFlag( c, COMMODITY_FLAG_ALWAYS_CAN_SELL ) ) {
424 lua_pushboolean( L, 1 );
425 return 1;
426 }
427
428 if ( lua_issystem( L, 2 ) ) {
429 const StarSystem *s = luaL_validsystem( L, 2 );
430 for ( int i = 0; i < array_size( s->spobs ); i++ ) {
431 if ( spob_hasCommodity( c, s->spobs[i] ) ) {
432 lua_pushboolean( L, 1 );
433 return 1;
434 }
435 }
436 } else {
437 const Spob *s = luaL_validspob( L, 2 );
438 lua_pushboolean( L, spob_hasCommodity( c, s ) );
439 return 1;
440 }
441
442 lua_pushboolean( L, 0 );
443 return 1;
444}
445
457static int commodityL_canBuy( lua_State *L )
458{
459 const Commodity *c = luaL_validcommodity( L, 1 );
460
461 if ( lua_issystem( L, 2 ) ) {
462 const 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 } else {
470 const Spob *s = luaL_validspob( L, 2 );
471 lua_pushboolean( L, spob_hasCommodity( c, s ) );
472 return 1;
473 }
474
475 lua_pushboolean( L, 0 );
476 return 1;
477}
478
487static int commodityL_icon( lua_State *L )
488{
489 const Commodity *c = luaL_validcommodity( L, 1 );
490 if ( c->gfx_store == NULL )
491 return 0;
492 lua_pushtex( L, gl_dupTexture( c->gfx_store ) );
493 return 1;
494}
495
504static int commodityL_description( lua_State *L )
505{
506 const Commodity *c = luaL_validcommodity( L, 1 );
507 if ( c->description == NULL )
508 return 0;
509 lua_pushstring( L, c->description );
510 return 1;
511}
512
531static int commodityL_new( lua_State *L )
532{
533 const char *cname, *cdesc;
534 char str[STRMAX_SHORT];
535 Commodity *cargo;
536
537 /* Parameters. */
538 cname = luaL_checkstring( L, 1 );
539 cdesc = luaL_checkstring( L, 2 );
540
541 cargo = commodity_getW( cname );
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!" ),
546 cname );
547
548 if ( cargo == NULL )
549 cargo = commodity_newTemp( cname, cdesc );
550
551 if ( !lua_isnoneornil( L, 3 ) ) {
552 const char *buf;
553 lua_getfield( L, 3, "gfx_space" );
554 buf = luaL_optstring( L, -1, NULL );
555 if ( buf ) {
556 gl_freeTexture( cargo->gfx_space );
557 snprintf( str, sizeof( str ), COMMODITY_GFX_PATH "space/%s", buf );
558 cargo->gfx_space = gl_newImage( str, 0 );
559 }
560 }
561
562 lua_pushcommodity( L, cargo );
563 return 1;
564}
565
574static int commodityL_illegalto( lua_State *L )
575{
577 if ( lua_istable( L, 2 ) ) {
578 lua_pushnil( L ); /* nil */
579 while ( lua_next( L, -2 ) != 0 ) { /* k, v */
580 int f = luaL_validfaction( L, -1 );
582 lua_pop( L, 1 ); /* k */
583 }
584 } else {
585 int f = luaL_validfaction( L, 2 );
587 }
588 return 0;
589}
590
598static int commodityL_illegality( lua_State *L )
599{
600 const Commodity *c = luaL_validcommodity( L, 1 );
601 lua_newtable( L );
602 for ( int i = 0; i < array_size( c->illegalto ); i++ ) {
603 lua_pushfaction( L, c->illegalto[i] );
604 lua_rawseti( L, -2, i + 1 );
605 }
606 return 1;
607}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
Commodity * commodity_get(const char *name)
Gets a commodity by name.
Definition commodity.c:151
Commodity * commodity_newTemp(const char *name, const char *desc)
Creates a new temporary commodity.
Definition commodity.c:457
Commodity ** standard_commodities(void)
Return an array (array.h) of standard commodities. Free with array_free. (Don't free contents....
Definition commodity.c:267
Commodity * commodity_getW(const char *name)
Gets a commodity by name without warning.
Definition commodity.c:166
int commodity_tempIllegalto(Commodity *com, int faction)
Makes a temporary commodity illegal to something.
Definition commodity.c:474
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:177
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:129
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
Definition nlua_time.c:112
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
Definition opengl_tex.c:891
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
Definition opengl_tex.c:587
void gl_freeTexture(glTexture *texture)
Frees a texture.
Definition opengl_tex.c:835
static const double c[]
Definition rng.c:256
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition space.c:1007
const char * spob_getSystemName(const char *spobname)
Get the name of a system from a spobname.
Definition space.c:1082
credits_t spob_commodityPrice(const Spob *p, const Commodity *c)
Gets the price of a commodity at a spob.
Definition space.c:293
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:307
Represents a commodity.
Definition commodity.h:57
glTexture * gfx_space
Definition commodity.h:69
int istemp
Definition commodity.h:74
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:102
Commodity ** commodities
Definition space.h:134