/
nlua_commodity.c
298 lines (256 loc) · 6.85 KB
/
nlua_commodity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file nlua_commodity.c
*
* @brief Handles the Lua commodity bindings.
*/
#include "nlua_commodity.h"
#include "naev.h"
#include <lauxlib.h>
#include "nlua.h"
#include "nluadef.h"
#include "nlua_planet.h"
#include "log.h"
#include "rng.h"
/* Commodity metatable methods. */
static int commodityL_eq( lua_State *L );
static int commodityL_get( lua_State *L );
static int commodityL_name( lua_State *L );
static int commodityL_price( lua_State *L );
static int commodityL_priceAt( lua_State *L );
static const luaL_reg commodityL_methods[] = {
{ "__tostring", commodityL_name },
{ "__eq", commodityL_eq },
{ "get", commodityL_get },
{ "name", commodityL_name },
{ "price", commodityL_price },
{ "priceAt", commodityL_priceAt },
{0,0}
}; /**< Commodity metatable methods. */
/**
* @brief Loads the commodity library.
*
* @param L State to load commodity library into.
* @return 0 on success.
*/
int nlua_loadCommodity( lua_State *L, int readonly )
{
(void) readonly; /* Everything is readonly. */
/* Create the metatable */
luaL_newmetatable(L, COMMODITY_METATABLE);
/* Create the access table */
lua_pushvalue(L,-1);
lua_setfield(L,-2,"__index");
/* Register the values */
luaL_register(L, NULL, commodityL_methods);
/* Clean up. */
lua_setfield(L, LUA_GLOBALSINDEX, COMMODITY_METATABLE);
return 0;
}
/**
* @brief Lua bindings to interact with commodities.
*
* This will allow you to create and manipulate commodities in-game.
*
* An example would be:
* @code
* c = commodity.get( "Food" ) -- Gets the commodity by name
* if c:price() > 500 then
* -- Do something with high price
* end
* @endcode
*
* @luamod commodity
*/
/**
* @brief Gets commodity at index.
*
* @param L Lua state to get commodity from.
* @param ind Index position to find the commodity.
* @return Commodity found at the index in the state.
*/
LuaCommodity* lua_tocommodity( lua_State *L, int ind )
{
return (LuaCommodity*) lua_touserdata(L,ind);
}
/**
* @brief Gets commodity at index or raises error if there is no commodity at index.
*
* @param L Lua state to get commodity from.
* @param ind Index position to find commodity.
* @return Commodity found at the index in the state.
*/
LuaCommodity* luaL_checkcommodity( lua_State *L, int ind )
{
if (lua_iscommodity(L,ind))
return lua_tocommodity(L,ind);
luaL_typerror(L, ind, COMMODITY_METATABLE);
return NULL;
}
/**
* @brief Makes sure the commodity is valid or raises a Lua error.
*
* @param L State currently running.
* @param ind Index of the commodity to validate.
* @return The commodity (doesn't return if fails - raises Lua error ).
*/
Commodity* luaL_validcommodity( lua_State *L, int ind )
{
LuaCommodity *lo;
Commodity *o;
/* Get the commodity. */
lo = luaL_checkcommodity(L,ind);
o = lo->commodity;
if (o==NULL) {
NLUA_ERROR(L,"Commodity is invalid.");
return NULL;
}
return o;
}
/**
* @brief Pushes a commodity on the stack.
*
* @param L Lua state to push commodity into.
* @param commodity Commodity to push.
* @return Newly pushed commodity.
*/
LuaCommodity* lua_pushcommodity( lua_State *L, LuaCommodity commodity )
{
LuaCommodity *o;
o = (LuaCommodity*) lua_newuserdata(L, sizeof(LuaCommodity));
*o = commodity;
luaL_getmetatable(L, COMMODITY_METATABLE);
lua_setmetatable(L, -2);
return o;
}
/**
* @brief Checks to see if ind is a commodity.
*
* @param L Lua state to check.
* @param ind Index position to check.
* @return 1 if ind is a commodity.
*/
int lua_iscommodity( lua_State *L, int ind )
{
int ret;
if (lua_getmetatable(L,ind)==0)
return 0;
lua_getfield(L, LUA_REGISTRYINDEX, COMMODITY_METATABLE);
ret = 0;
if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
ret = 1;
lua_pop(L, 2); /* remove both metatables */
return ret;
}
/**
* @brief Checks to see if two commodities are the same.
*
* @usage if o1 == o2 then -- Checks to see if commodity o1 and o2 are the same
*
* @luaparam o1 First commodity to compare.
* @luaparam o2 Second commodity to compare.
* @luareturn true if both commodities are the same.
* @luafunc __eq( o1, o2 )
*/
static int commodityL_eq( lua_State *L )
{
LuaCommodity *a, *b;
a = luaL_checkcommodity(L,1);
b = luaL_checkcommodity(L,2);
if (a->commodity == b->commodity)
lua_pushboolean(L,1);
else
lua_pushboolean(L,0);
return 1;
}
/**
* @brief Gets a commodity.
*
* @usage s = commodity.get( "Hyena" ) -- Gets the hyena
*
* @luaparam s Name of the commodity to get.
* @luareturn The commodity matching name or nil if error.
* @luafunc get( s )
*/
static int commodityL_get( lua_State *L )
{
const char *name;
LuaCommodity lo;
/* Handle paremeters. */
name = luaL_checkstring(L,1);
/* Get commodity. */
lo.commodity = commodity_get( name );
if (lo.commodity == NULL) {
NLUA_ERROR(L,"Commodity '%s' not found!", name);
return 0;
}
/* Push. */
lua_pushcommodity(L, lo);
return 1;
}
/**
* @brief Gets the name of the commodity's commodity.
*
* @usage commodityname = s:name()
*
* @luaparam s Commodity to get commodity name.
* @luareturn The name of the commodity's commodity.
* @luafunc name( s )
*/
static int commodityL_name( lua_State *L )
{
Commodity *c;
/* Get the commodity. */
c = luaL_validcommodity(L,1);
/** Return the commodity name. */
lua_pushstring(L, c->name);
return 1;
}
/**
* @brief Gets the base price of an commodity.
*
* @usage print( o:price() ) -- Prints the base price of the commodity
*
* @luaparam o Commodity to get information of.
* @luareturn The base price of the commodity.
* @luafunc price( o )
*/
static int commodityL_price( lua_State *L )
{
Commodity *c = luaL_validcommodity(L,1);
lua_pushnumber(L, c->price);
return 1;
}
/**
* @brief Gets the base price of an commodity at a certain planet.
*
* @usage if o:priceAt( planet.get("Polaris Prime") ) > 100 then -- Checks price of an outfit at polaris prime
*
* @luaparam o Commodity to get information of.
* @luaparam p Planet to get price at.
* @luareturn The price of the commodity at the planet.
* @luafunc priceAt( o, p )
*/
static int commodityL_priceAt( lua_State *L )
{
Commodity *c;
Planet *p;
StarSystem *sys;
char *sysname;
c = luaL_validcommodity(L,1);
p = luaL_validplanet(L,2);
sysname = planet_getSystem( p->name );
if (sysname == NULL) {
NLUA_ERROR( L, "Planet '%s' does not belong to a system", p->name );
return 0;
}
sys = system_get( sysname );
if (sys == NULL) {
NLUA_ERROR( L, "Planet '%s' can not find it's system '%s'", p->name, sysname );
return 0;
}
lua_pushnumber( L, planet_commodityPrice( p, c ) );
return 1;
}