1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/lua/lua_area.hh"
|
---|
8 |
|
---|
9 | #include "nv/lua/lua_raw.hh"
|
---|
10 | #include "nv/core/random.hh"
|
---|
11 |
|
---|
12 | const char* nv::lua::detail::AREA_METATABLE = "area";
|
---|
13 |
|
---|
14 | using nv::lua::detail::is_coord;
|
---|
15 | using nv::lua::detail::to_coord;
|
---|
16 | using nv::lua::detail::to_pcoord;
|
---|
17 | using nv::lua::detail::push_coord;
|
---|
18 |
|
---|
19 | using nv::lua::detail::is_area;
|
---|
20 | using nv::lua::detail::to_area;
|
---|
21 | using nv::lua::detail::to_parea;
|
---|
22 | using nv::lua::detail::push_area;
|
---|
23 |
|
---|
24 | static void nlua_area_construct( lua_State* L, int sidx )
|
---|
25 | {
|
---|
26 | if ( is_coord( L, sidx ) )
|
---|
27 | {
|
---|
28 | if ( is_coord( L, sidx+1 ) )
|
---|
29 | {
|
---|
30 | nv::rectangle a( to_coord( L, sidx ), to_coord( L, sidx+1 ) );
|
---|
31 | push_area( L, a );
|
---|
32 | return;
|
---|
33 | }
|
---|
34 | // TODO: coord, width, height
|
---|
35 | }
|
---|
36 |
|
---|
37 | nv::rectangle a(
|
---|
38 | nv::ivec2( lua_tointeger( L, sidx ), lua_tointeger( L, sidx+1 ) ),
|
---|
39 | nv::ivec2( lua_tointeger( L, sidx+2 ), lua_tointeger( L, sidx+3 ) )
|
---|
40 | );
|
---|
41 | push_area( L, a );
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | static int nlua_area_new( lua_State* L )
|
---|
46 | {
|
---|
47 | nlua_area_construct( L, 1 );
|
---|
48 | return 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | static int nlua_area_call( lua_State* L )
|
---|
52 | {
|
---|
53 | nlua_area_construct( L, 2 );
|
---|
54 | return 1;
|
---|
55 | }
|
---|
56 |
|
---|
57 | static int nlua_area_eq( lua_State* L )
|
---|
58 | {
|
---|
59 | lua_pushboolean( L, to_area( L, 1 ) == to_area( L, 2 ) );
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int nlua_area_get( lua_State* L )
|
---|
64 | {
|
---|
65 | nv::rectangle r( to_area( L, 1 ) );
|
---|
66 | push_coord( L, r.ul );
|
---|
67 | push_coord( L, r.lr );
|
---|
68 | return 2;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static int nlua_area_clone( lua_State* L )
|
---|
72 | {
|
---|
73 | push_area( L, *to_parea( L, 1 ) );
|
---|
74 | return 1;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static int nlua_area_index( lua_State* L )
|
---|
78 | {
|
---|
79 | nv::rectangle* a = to_parea( L, 1 );
|
---|
80 | nv::size_t l;
|
---|
81 | const char* index = lua_tolstring( L, 2, &l );
|
---|
82 | if ( l == 1 && index[0] == 'a' )
|
---|
83 | {
|
---|
84 | push_coord( L, a->ul );
|
---|
85 | }
|
---|
86 | else if ( l == 1 && index[0] == 'b' )
|
---|
87 | {
|
---|
88 | push_coord( L, a->lr );
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | luaL_getmetafield( L, 1, "__functions" );
|
---|
93 | lua_pushvalue( L, 2 );
|
---|
94 | lua_rawget( L, -2 );
|
---|
95 | }
|
---|
96 | return 1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static int nlua_area_newindex( lua_State* L )
|
---|
100 | {
|
---|
101 | nv::rectangle* a = to_parea( L, 1 );
|
---|
102 | nv::size_t l;
|
---|
103 | const char* index = lua_tolstring( L, 2, &l );
|
---|
104 | nv::ivec2 value( to_coord( L, 3 ) );
|
---|
105 | if ( l == 1 && index[0] == 'a' )
|
---|
106 | {
|
---|
107 | a->ul = value;
|
---|
108 | }
|
---|
109 | else if ( l == 1 && index[0] == 'b' )
|
---|
110 | {
|
---|
111 | a->lr = value;
|
---|
112 | }
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static int lua_area_coords_closure( lua_State* L )
|
---|
117 | {
|
---|
118 | nv::rectangle* a( to_parea( L, lua_upvalueindex(1) ) );
|
---|
119 | nv::ivec2* c( to_pcoord( L, lua_upvalueindex(2) ) );
|
---|
120 |
|
---|
121 | c->x++;
|
---|
122 |
|
---|
123 | if ( c->x > a->lr.x )
|
---|
124 | {
|
---|
125 | c->x = a->ul.x;
|
---|
126 | c->y++;
|
---|
127 | if (c->y > a->lr.y)
|
---|
128 | {
|
---|
129 | lua_pushnil( L );
|
---|
130 | return 1;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | push_coord( L, *c );
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static int nlua_area_coords( lua_State* L )
|
---|
139 | {
|
---|
140 | nv::rectangle* a( to_parea( L, 1 ) );
|
---|
141 | nv::ivec2 c( a->ul );
|
---|
142 | c.x--;
|
---|
143 | push_coord( L, c );
|
---|
144 | lua_pushcclosure( L, lua_area_coords_closure, 2 );
|
---|
145 | return 1;
|
---|
146 | }
|
---|
147 |
|
---|
148 | static int nlua_area_edges_closure( lua_State* L )
|
---|
149 | {
|
---|
150 | nv::rectangle* a( to_parea( L, lua_upvalueindex(1) ) );
|
---|
151 | nv::ivec2* c( to_pcoord( L, lua_upvalueindex(2) ) );
|
---|
152 |
|
---|
153 | c->x++;
|
---|
154 |
|
---|
155 | if ( c->x > a->lr.x )
|
---|
156 | {
|
---|
157 | c->x = a->ul.x;
|
---|
158 | c->y++;
|
---|
159 | if (c->y > a->lr.y)
|
---|
160 | {
|
---|
161 | lua_pushnil( L );
|
---|
162 | return 1;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | if (c->y != a->ul.y && c->y != a->lr.y && c->x == a->ul.x + 1 ) c->x = a->ul.x;
|
---|
166 |
|
---|
167 |
|
---|
168 | push_coord( L, *c );
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | static int nlua_area_edges( lua_State* L )
|
---|
173 | {
|
---|
174 | nv::rectangle* a( to_parea( L, 1 ) );
|
---|
175 | nv::ivec2 c( a->ul );
|
---|
176 | c.x--;
|
---|
177 | push_coord( L, c );
|
---|
178 | lua_pushcclosure( L, nlua_area_edges_closure, 2 );
|
---|
179 | return 1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int nlua_area_corners_closure( lua_State* L )
|
---|
183 | {
|
---|
184 | int index = static_cast< int >( lua_tointeger( L, lua_upvalueindex(2) ) + 1 );
|
---|
185 | lua_pushinteger( L, index );
|
---|
186 | lua_replace( L, lua_upvalueindex(2) ); // update
|
---|
187 | lua_rawgeti( L, lua_upvalueindex(1), index ); // get value
|
---|
188 | return 1;
|
---|
189 | }
|
---|
190 |
|
---|
191 | static int nlua_area_corners( lua_State* L )
|
---|
192 | {
|
---|
193 | nv::rectangle* a = to_parea( L, 1 );
|
---|
194 |
|
---|
195 | lua_createtable(L, 4, 0);
|
---|
196 | push_coord( L, a->ul );
|
---|
197 | lua_rawseti( L, -2, 1 );
|
---|
198 | push_coord( L, a->ur() );
|
---|
199 | lua_rawseti( L, -2, 2 );
|
---|
200 | push_coord( L, a->ll() );
|
---|
201 | lua_rawseti( L, -2, 3 );
|
---|
202 | push_coord( L, a->lr );
|
---|
203 | lua_rawseti( L, -2, 4 );
|
---|
204 |
|
---|
205 | lua_pushinteger(L, 0);
|
---|
206 | lua_pushcclosure( L, nlua_area_corners_closure, 2 );
|
---|
207 | return 1;
|
---|
208 | }
|
---|
209 |
|
---|
210 | static int nlua_area_shrink( lua_State* L )
|
---|
211 | {
|
---|
212 | nv::rectangle* a = to_parea( L, 1 );
|
---|
213 | a->shrink( static_cast< int >( lua_tointeger( L, 2 ) ) );
|
---|
214 | return 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static int nlua_area_shrinked( lua_State* L )
|
---|
218 | {
|
---|
219 | nv::rectangle* a = to_parea( L, 1 );
|
---|
220 | push_area( L, a->shrinked( static_cast< int >( lua_tointeger( L, 2 ) ) ) );
|
---|
221 | return 1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static int nlua_area_expand( lua_State* L )
|
---|
225 | {
|
---|
226 | nv::rectangle* a = to_parea( L, 1 );
|
---|
227 | a->expand( static_cast< int >( lua_tointeger( L, 2 ) ) );
|
---|
228 | return 0;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static int nlua_area_expanded( lua_State* L )
|
---|
232 | {
|
---|
233 | nv::rectangle* a = to_parea( L, 1 );
|
---|
234 | push_area( L, a->expanded( static_cast< int >( lua_tointeger( L, 2 ) ) ) );
|
---|
235 | return 1;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static int nlua_area_clamp( lua_State* L )
|
---|
239 | {
|
---|
240 | nv::rectangle* a1 = to_parea( L, 1 );
|
---|
241 | nv::rectangle* a2 = to_parea( L, 2 );
|
---|
242 | a1->clamp_to( *a2 );
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | static int nlua_area_clamped( lua_State* L )
|
---|
247 | {
|
---|
248 | nv::rectangle a1 = to_area( L, 1 );
|
---|
249 | nv::rectangle* a2 = to_parea( L, 2 );
|
---|
250 | a1.clamp_to( *a2 );
|
---|
251 | push_area( L, a1 );
|
---|
252 | return 1;
|
---|
253 | }
|
---|
254 |
|
---|
255 | static int nlua_area_clamp_coord( lua_State* L )
|
---|
256 | {
|
---|
257 | nv::rectangle* a = to_parea( L, 1 );
|
---|
258 | nv::ivec2* c = to_pcoord( L, 2 );
|
---|
259 | *c = glm::clamp( *c, a->ul, a->lr );
|
---|
260 | return 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | static int nlua_area_clamped_coord( lua_State* L )
|
---|
264 | {
|
---|
265 | nv::rectangle* a = to_parea( L, 1 );
|
---|
266 | nv::ivec2* c = to_pcoord( L, 2 );
|
---|
267 | push_coord( L, glm::clamp( *c, a->ul, a->lr ) );
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | static int nlua_area_fix( lua_State* L )
|
---|
272 | {
|
---|
273 | nv::rectangle* a = to_parea( L, 1 );
|
---|
274 | if ( a->ul.x > a->lr.x ) a->ul.x = a->lr.x;
|
---|
275 | if ( a->ul.y > a->lr.y ) a->ul.y = a->lr.y;
|
---|
276 | return 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static int nlua_area_proper( lua_State* L )
|
---|
280 | {
|
---|
281 | nv::rectangle* a = to_parea( L, 1 );
|
---|
282 | lua_pushboolean( L, a->ul.x <= a->lr.x && a->ul.y <= a->lr.y );
|
---|
283 | return 1;
|
---|
284 | }
|
---|
285 |
|
---|
286 | static int nlua_area_dim( lua_State* L )
|
---|
287 | {
|
---|
288 | nv::rectangle* a = to_parea( L, 1 );
|
---|
289 | push_coord( L, a->ul - a->lr + nv::ivec2(1,1) );
|
---|
290 | return 1;
|
---|
291 | }
|
---|
292 |
|
---|
293 | static int nlua_area_size( lua_State* L )
|
---|
294 | {
|
---|
295 | nv::rectangle* a = to_parea( L, 1 );
|
---|
296 | lua_pushinteger( L, a->get_enclosed_area() );
|
---|
297 | return 1;
|
---|
298 | }
|
---|
299 |
|
---|
300 | static int nlua_area_contains( lua_State* L )
|
---|
301 | {
|
---|
302 | lua_pushboolean( L, to_parea( L, 1 )->contains( to_coord( L, 2 ) ) );
|
---|
303 | return 1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static int nlua_area_is_edge( lua_State* L )
|
---|
307 | {
|
---|
308 | nv::rectangle a = to_area( L, 1 );
|
---|
309 | nv::ivec2 c = to_coord( L, 2 );
|
---|
310 | lua_pushboolean( L, a.contains( c ) && ( c.x == a.ul.x || c.x == a.lr.x || c.y == a.ul.y || c.y == a.lr.y ) );
|
---|
311 | return 1;
|
---|
312 | }
|
---|
313 |
|
---|
314 | static int nlua_area_around( lua_State* L )
|
---|
315 | {
|
---|
316 | nv::ivec2 c = to_coord( L, 1 );
|
---|
317 | int amount = static_cast< int >( lua_tointeger( L, 1 ) );
|
---|
318 | nv::ivec2 shift( amount, amount );
|
---|
319 | push_area( L, nv::rectangle( c - shift, c + shift ) );
|
---|
320 | return 1;
|
---|
321 | }
|
---|
322 |
|
---|
323 | static int nlua_area_tostring( lua_State* L )
|
---|
324 | {
|
---|
325 | nv::rectangle a = to_area( L, 1 );
|
---|
326 | lua_pushfstring( L, "(%d,%dx%d,%d)", a.ul.x, a.ul.y, a.lr.x, a.lr.y );
|
---|
327 | return 1;
|
---|
328 | }
|
---|
329 |
|
---|
330 | static int nlua_area_random_edge_coord( lua_State* L )
|
---|
331 | {
|
---|
332 | nv::rectangle area = to_area( L, 1 );
|
---|
333 | nv::ivec2 a = area.ul;
|
---|
334 | nv::ivec2 b = area.lr;
|
---|
335 | nv::sint32 xs = ( b.x - a.x ) + 1;
|
---|
336 | nv::sint32 ys = ( b.y - a.y ) - 1;
|
---|
337 | nv::sint32 roll = nv::random::get().srand( 2 * xs + 2 * ys );
|
---|
338 | nv::ivec2 result;
|
---|
339 |
|
---|
340 | if ( roll < 2 * xs )
|
---|
341 | {
|
---|
342 | result = ( roll < xs ) ? nv::ivec2( a.x + roll, a.y ) : nv::ivec2( a.x + roll - xs, b.y );
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | roll -= 2 * xs;
|
---|
347 | result = ( roll < ys ) ? nv::ivec2( a.x, a.y + roll + 1 ) : nv::ivec2( b.x, a.y + roll - ys + 1);
|
---|
348 | }
|
---|
349 | push_coord( L, result );
|
---|
350 | return 1;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static int nlua_area_random_inner_edge_coord( lua_State* L )
|
---|
354 | {
|
---|
355 | nv::rectangle area = to_area( L, 1 );
|
---|
356 | nv::ivec2 a = area.ul;
|
---|
357 | nv::ivec2 b = area.lr;
|
---|
358 | nv::sint32 xs = ( b.x - a.x ) - 1;
|
---|
359 | nv::sint32 ys = ( b.y - a.y ) - 1;
|
---|
360 | nv::sint32 roll = nv::random::get().srand( 2 * xs + 2 * ys );
|
---|
361 | nv::ivec2 result;
|
---|
362 |
|
---|
363 | if ( roll < 2 * xs )
|
---|
364 | {
|
---|
365 | result = ( roll < xs ) ? nv::ivec2( a.x + roll + 1, a.y ) : nv::ivec2( a.x + roll - xs + 1, b.y );
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | roll -= 2 * xs;
|
---|
370 | result = ( roll < ys ) ? nv::ivec2( a.x, a.y + roll + 1 ) : nv::ivec2( b.x, a.y + roll - ys + 1);
|
---|
371 | }
|
---|
372 | push_coord( L, result );
|
---|
373 | return 1;
|
---|
374 | }
|
---|
375 |
|
---|
376 | static int nlua_area_random_coord( lua_State* L )
|
---|
377 | {
|
---|
378 | nv::rectangle area = to_area( L, 1 );
|
---|
379 | push_coord( L, nv::random::get().range( area.ul, area.lr ) );
|
---|
380 | return 1;
|
---|
381 | }
|
---|
382 |
|
---|
383 | static int nlua_area_random_subarea( lua_State* L )
|
---|
384 | {
|
---|
385 | nv::rectangle area = to_area( L, 1 );
|
---|
386 | nv::ivec2 dim = to_coord( L, 2 );
|
---|
387 | nv::ivec2 start = nv::random::get().range( area.ul, area.lr - dim );
|
---|
388 | push_area( L, nv::rectangle( start, start + dim ) );
|
---|
389 | return 1;
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | static int luaopen_area( lua_State * L )
|
---|
394 | {
|
---|
395 | NV_LUA_STACK_ASSERT( L, 1 );
|
---|
396 | static const struct luaL_Reg nlua_area_sf [] = {
|
---|
397 | { "new", nlua_area_new },
|
---|
398 | { "around", nlua_area_around },
|
---|
399 | {NULL, NULL}
|
---|
400 | };
|
---|
401 |
|
---|
402 | static const struct luaL_Reg nlua_area_f [] = {
|
---|
403 | { "get", nlua_area_get },
|
---|
404 | { "clone", nlua_area_clone },
|
---|
405 |
|
---|
406 | { "coords", nlua_area_coords },
|
---|
407 | { "edges", nlua_area_edges },
|
---|
408 | { "corners", nlua_area_corners },
|
---|
409 |
|
---|
410 | { "tostring", nlua_area_tostring },
|
---|
411 | { "shrink", nlua_area_shrink },
|
---|
412 | { "shrinked", nlua_area_shrinked },
|
---|
413 | { "expand", nlua_area_expand },
|
---|
414 | { "expanded", nlua_area_expanded },
|
---|
415 | { "clamp", nlua_area_clamp },
|
---|
416 | { "clamped", nlua_area_clamped },
|
---|
417 | { "clamp_coord", nlua_area_clamp_coord },
|
---|
418 | { "clamped_coord", nlua_area_clamped_coord },
|
---|
419 | { "fix", nlua_area_fix },
|
---|
420 | { "proper", nlua_area_proper },
|
---|
421 | { "dim", nlua_area_dim },
|
---|
422 | { "size", nlua_area_size },
|
---|
423 | { "contains", nlua_area_contains },
|
---|
424 | { "is_edge", nlua_area_is_edge },
|
---|
425 |
|
---|
426 | { "random_subarea", nlua_area_random_subarea },
|
---|
427 | { "random_coord", nlua_area_random_coord },
|
---|
428 | { "random_edge_coord", nlua_area_random_edge_coord },
|
---|
429 | { "random_inner_edge_coord", nlua_area_random_inner_edge_coord },
|
---|
430 |
|
---|
431 | {NULL, NULL}
|
---|
432 | };
|
---|
433 |
|
---|
434 | static const struct luaL_Reg nlua_area_sm [] = {
|
---|
435 | { "__call", nlua_area_call },
|
---|
436 | {NULL, NULL}
|
---|
437 | };
|
---|
438 |
|
---|
439 | static const struct luaL_Reg nlua_area_m [] = {
|
---|
440 | { "__eq", nlua_area_eq },
|
---|
441 | { "__call", nlua_area_coords },
|
---|
442 | { "__index", nlua_area_index },
|
---|
443 | { "__newindex", nlua_area_newindex },
|
---|
444 | { "__tostring", nlua_area_tostring },
|
---|
445 | {NULL, NULL}
|
---|
446 | };
|
---|
447 |
|
---|
448 | luaL_newmetatable( L, nv::lua::detail::AREA_METATABLE );
|
---|
449 | nlua_register( L, nlua_area_m, -1 );
|
---|
450 | lua_createtable( L, 0, 0 );
|
---|
451 | nlua_register( L, nlua_area_f, -1 );
|
---|
452 | lua_setfield(L, -2, "__functions" );
|
---|
453 | lua_pop( L, 1 );
|
---|
454 |
|
---|
455 | lua_createtable( L, 0, 0 );
|
---|
456 | nlua_register( L, nlua_area_sf, -1 );
|
---|
457 | lua_createtable( L, 0, 0 );
|
---|
458 | nlua_register( L, nlua_area_sm, -1 );
|
---|
459 | lua_setmetatable( L, -2 );
|
---|
460 | return 1;
|
---|
461 | }
|
---|
462 |
|
---|
463 | void nv::lua::register_area( lua_State* L )
|
---|
464 | {
|
---|
465 | int stack = lua_gettop( L );
|
---|
466 | luaL_requiref(L, "area", luaopen_area, 1);
|
---|
467 | lua_settop( L, stack );
|
---|
468 | }
|
---|
469 |
|
---|