]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/common/c_converter.cpp
Move the clamping of hp/breath when their maximums change to read_object_properties...
[dragonfireclient.git] / src / script / common / c_converter.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 extern "C" {
21 #include "lua.h"
22 #include "lauxlib.h"
23 }
24
25 #include "util/numeric.h"
26 #include "util/serialize.h"
27 #include "util/string.h"
28 #include "common/c_converter.h"
29 #include "common/c_internal.h"
30 #include "constants.h"
31
32
33 #define CHECK_TYPE(index, name, type) { \
34                 int t = lua_type(L, (index)); \
35                 if (t != (type)) { \
36                         std::string traceback = script_get_backtrace(L); \
37                         throw LuaError(std::string("Invalid ") + (name) + \
38                                 " (expected " + lua_typename(L, (type)) + \
39                                 " got " + lua_typename(L, t) + ").\n" + traceback); \
40                 } \
41         }
42 #define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
43 #define CHECK_FLOAT_RANGE(value, name) \
44 if (value < F1000_MIN || value > F1000_MAX) { \
45         std::ostringstream error_text; \
46         error_text << "Invalid float vector dimension range '" name "' " << \
47         "(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \
48         " got " << value << ")." << std::endl; \
49         throw LuaError(error_text.str()); \
50 }
51 #define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
52
53
54 void push_float_string(lua_State *L, float value)
55 {
56         std::stringstream ss;
57         std::string str;
58         ss << value;
59         str = ss.str();
60         lua_pushstring(L, str.c_str());
61 }
62
63 void push_v3f(lua_State *L, v3f p)
64 {
65         lua_newtable(L);
66         lua_pushnumber(L, p.X);
67         lua_setfield(L, -2, "x");
68         lua_pushnumber(L, p.Y);
69         lua_setfield(L, -2, "y");
70         lua_pushnumber(L, p.Z);
71         lua_setfield(L, -2, "z");
72 }
73
74 void push_v2f(lua_State *L, v2f p)
75 {
76         lua_newtable(L);
77         lua_pushnumber(L, p.X);
78         lua_setfield(L, -2, "x");
79         lua_pushnumber(L, p.Y);
80         lua_setfield(L, -2, "y");
81 }
82
83 void push_v3_float_string(lua_State *L, v3f p)
84 {
85         lua_newtable(L);
86         push_float_string(L, p.X);
87         lua_setfield(L, -2, "x");
88         push_float_string(L, p.Y);
89         lua_setfield(L, -2, "y");
90         push_float_string(L, p.Z);
91         lua_setfield(L, -2, "z");
92 }
93
94 void push_v2_float_string(lua_State *L, v2f p)
95 {
96         lua_newtable(L);
97         push_float_string(L, p.X);
98         lua_setfield(L, -2, "x");
99         push_float_string(L, p.Y);
100         lua_setfield(L, -2, "y");
101 }
102
103 v2s16 read_v2s16(lua_State *L, int index)
104 {
105         v2s16 p;
106         CHECK_POS_TAB(index);
107         lua_getfield(L, index, "x");
108         p.X = lua_tonumber(L, -1);
109         lua_pop(L, 1);
110         lua_getfield(L, index, "y");
111         p.Y = lua_tonumber(L, -1);
112         lua_pop(L, 1);
113         return p;
114 }
115
116 void push_v2s16(lua_State *L, v2s16 p)
117 {
118         lua_newtable(L);
119         lua_pushnumber(L, p.X);
120         lua_setfield(L, -2, "x");
121         lua_pushnumber(L, p.Y);
122         lua_setfield(L, -2, "y");
123 }
124
125 void push_v2s32(lua_State *L, v2s32 p)
126 {
127         lua_newtable(L);
128         lua_pushnumber(L, p.X);
129         lua_setfield(L, -2, "x");
130         lua_pushnumber(L, p.Y);
131         lua_setfield(L, -2, "y");
132 }
133
134 v2s32 read_v2s32(lua_State *L, int index)
135 {
136         v2s32 p;
137         CHECK_POS_TAB(index);
138         lua_getfield(L, index, "x");
139         p.X = lua_tonumber(L, -1);
140         lua_pop(L, 1);
141         lua_getfield(L, index, "y");
142         p.Y = lua_tonumber(L, -1);
143         lua_pop(L, 1);
144         return p;
145 }
146
147 v2f read_v2f(lua_State *L, int index)
148 {
149         v2f p;
150         CHECK_POS_TAB(index);
151         lua_getfield(L, index, "x");
152         p.X = lua_tonumber(L, -1);
153         lua_pop(L, 1);
154         lua_getfield(L, index, "y");
155         p.Y = lua_tonumber(L, -1);
156         lua_pop(L, 1);
157         return p;
158 }
159
160 v2f check_v2f(lua_State *L, int index)
161 {
162         v2f p;
163         CHECK_POS_TAB(index);
164         lua_getfield(L, index, "x");
165         CHECK_POS_COORD("x");
166         p.X = lua_tonumber(L, -1);
167         lua_pop(L, 1);
168         lua_getfield(L, index, "y");
169         CHECK_POS_COORD("y");
170         p.Y = lua_tonumber(L, -1);
171         lua_pop(L, 1);
172         return p;
173 }
174
175 v3f read_v3f(lua_State *L, int index)
176 {
177         v3f pos;
178         CHECK_POS_TAB(index);
179         lua_getfield(L, index, "x");
180         pos.X = lua_tonumber(L, -1);
181         lua_pop(L, 1);
182         lua_getfield(L, index, "y");
183         pos.Y = lua_tonumber(L, -1);
184         lua_pop(L, 1);
185         lua_getfield(L, index, "z");
186         pos.Z = lua_tonumber(L, -1);
187         lua_pop(L, 1);
188         return pos;
189 }
190
191 v3f check_v3f(lua_State *L, int index)
192 {
193         v3f pos;
194         CHECK_POS_TAB(index);
195         lua_getfield(L, index, "x");
196         CHECK_POS_COORD("x");
197         pos.X = lua_tonumber(L, -1);
198         CHECK_FLOAT_RANGE(pos.X, "x")
199         lua_pop(L, 1);
200         lua_getfield(L, index, "y");
201         CHECK_POS_COORD("y");
202         pos.Y = lua_tonumber(L, -1);
203         CHECK_FLOAT_RANGE(pos.Y, "y")
204         lua_pop(L, 1);
205         lua_getfield(L, index, "z");
206         CHECK_POS_COORD("z");
207         pos.Z = lua_tonumber(L, -1);
208         CHECK_FLOAT_RANGE(pos.Z, "z")
209         lua_pop(L, 1);
210         return pos;
211 }
212
213 v3d read_v3d(lua_State *L, int index)
214 {
215         v3d pos;
216         CHECK_POS_TAB(index);
217         lua_getfield(L, index, "x");
218         pos.X = lua_tonumber(L, -1);
219         lua_pop(L, 1);
220         lua_getfield(L, index, "y");
221         pos.Y = lua_tonumber(L, -1);
222         lua_pop(L, 1);
223         lua_getfield(L, index, "z");
224         pos.Z = lua_tonumber(L, -1);
225         lua_pop(L, 1);
226         return pos;
227 }
228
229 v3d check_v3d(lua_State *L, int index)
230 {
231         v3d pos;
232         CHECK_POS_TAB(index);
233         lua_getfield(L, index, "x");
234         CHECK_POS_COORD("x");
235         pos.X = lua_tonumber(L, -1);
236         CHECK_FLOAT_RANGE(pos.X, "x")
237         lua_pop(L, 1);
238         lua_getfield(L, index, "y");
239         CHECK_POS_COORD("y");
240         pos.Y = lua_tonumber(L, -1);
241         CHECK_FLOAT_RANGE(pos.Y, "y")
242         lua_pop(L, 1);
243         lua_getfield(L, index, "z");
244         CHECK_POS_COORD("z");
245         pos.Z = lua_tonumber(L, -1);
246         CHECK_FLOAT_RANGE(pos.Z, "z")
247         lua_pop(L, 1);
248         return pos;
249 }
250
251 void push_ARGB8(lua_State *L, video::SColor color)
252 {
253         lua_newtable(L);
254         lua_pushnumber(L, color.getAlpha());
255         lua_setfield(L, -2, "a");
256         lua_pushnumber(L, color.getRed());
257         lua_setfield(L, -2, "r");
258         lua_pushnumber(L, color.getGreen());
259         lua_setfield(L, -2, "g");
260         lua_pushnumber(L, color.getBlue());
261         lua_setfield(L, -2, "b");
262 }
263
264 void pushFloatPos(lua_State *L, v3f p)
265 {
266         p /= BS;
267         push_v3f(L, p);
268 }
269
270 v3f checkFloatPos(lua_State *L, int index)
271 {
272         return check_v3f(L, index) * BS;
273 }
274
275 void push_v3s16(lua_State *L, v3s16 p)
276 {
277         lua_newtable(L);
278         lua_pushnumber(L, p.X);
279         lua_setfield(L, -2, "x");
280         lua_pushnumber(L, p.Y);
281         lua_setfield(L, -2, "y");
282         lua_pushnumber(L, p.Z);
283         lua_setfield(L, -2, "z");
284 }
285
286 v3s16 read_v3s16(lua_State *L, int index)
287 {
288         // Correct rounding at <0
289         v3d pf = read_v3d(L, index);
290         return doubleToInt(pf, 1.0);
291 }
292
293 v3s16 check_v3s16(lua_State *L, int index)
294 {
295         // Correct rounding at <0
296         v3d pf = check_v3d(L, index);
297         return doubleToInt(pf, 1.0);
298 }
299
300 bool read_color(lua_State *L, int index, video::SColor *color)
301 {
302         if (lua_istable(L, index)) {
303                 *color = read_ARGB8(L, index);
304         } else if (lua_isnumber(L, index)) {
305                 color->set(lua_tonumber(L, index));
306         } else if (lua_isstring(L, index)) {
307                 video::SColor parsed_color;
308                 if (!parseColorString(lua_tostring(L, index), parsed_color, true))
309                         return false;
310
311                 *color = parsed_color;
312         } else {
313                 return false;
314         }
315
316         return true;
317 }
318
319 video::SColor read_ARGB8(lua_State *L, int index)
320 {
321         video::SColor color(0);
322         CHECK_TYPE(index, "ARGB color", LUA_TTABLE);
323         lua_getfield(L, index, "a");
324         color.setAlpha(lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0xFF);
325         lua_pop(L, 1);
326         lua_getfield(L, index, "r");
327         color.setRed(lua_tonumber(L, -1));
328         lua_pop(L, 1);
329         lua_getfield(L, index, "g");
330         color.setGreen(lua_tonumber(L, -1));
331         lua_pop(L, 1);
332         lua_getfield(L, index, "b");
333         color.setBlue(lua_tonumber(L, -1));
334         lua_pop(L, 1);
335         return color;
336 }
337
338 aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
339 {
340         aabb3f box;
341         if(lua_istable(L, index)){
342                 lua_rawgeti(L, index, 1);
343                 box.MinEdge.X = lua_tonumber(L, -1) * scale;
344                 lua_pop(L, 1);
345                 lua_rawgeti(L, index, 2);
346                 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
347                 lua_pop(L, 1);
348                 lua_rawgeti(L, index, 3);
349                 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
350                 lua_pop(L, 1);
351                 lua_rawgeti(L, index, 4);
352                 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
353                 lua_pop(L, 1);
354                 lua_rawgeti(L, index, 5);
355                 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
356                 lua_pop(L, 1);
357                 lua_rawgeti(L, index, 6);
358                 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
359                 lua_pop(L, 1);
360         }
361         box.repair();
362         return box;
363 }
364
365 void push_aabb3f(lua_State *L, aabb3f box)
366 {
367         lua_newtable(L);
368         lua_pushnumber(L, box.MinEdge.X);
369         lua_rawseti(L, -2, 1);
370         lua_pushnumber(L, box.MinEdge.Y);
371         lua_rawseti(L, -2, 2);
372         lua_pushnumber(L, box.MinEdge.Z);
373         lua_rawseti(L, -2, 3);
374         lua_pushnumber(L, box.MaxEdge.X);
375         lua_rawseti(L, -2, 4);
376         lua_pushnumber(L, box.MaxEdge.Y);
377         lua_rawseti(L, -2, 5);
378         lua_pushnumber(L, box.MaxEdge.Z);
379         lua_rawseti(L, -2, 6);
380 }
381
382 std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
383 {
384         std::vector<aabb3f> boxes;
385         if(lua_istable(L, index)){
386                 int n = lua_objlen(L, index);
387                 // Check if it's a single box or a list of boxes
388                 bool possibly_single_box = (n == 6);
389                 for(int i = 1; i <= n && possibly_single_box; i++){
390                         lua_rawgeti(L, index, i);
391                         if(!lua_isnumber(L, -1))
392                                 possibly_single_box = false;
393                         lua_pop(L, 1);
394                 }
395                 if(possibly_single_box){
396                         // Read a single box
397                         boxes.push_back(read_aabb3f(L, index, scale));
398                 } else {
399                         // Read a list of boxes
400                         for(int i = 1; i <= n; i++){
401                                 lua_rawgeti(L, index, i);
402                                 boxes.push_back(read_aabb3f(L, -1, scale));
403                                 lua_pop(L, 1);
404                         }
405                 }
406         }
407         return boxes;
408 }
409
410 size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result)
411 {
412         if (index < 0)
413                 index = lua_gettop(L) + 1 + index;
414
415         size_t num_strings = 0;
416
417         if (lua_istable(L, index)) {
418                 lua_pushnil(L);
419                 while (lua_next(L, index)) {
420                         if (lua_isstring(L, -1)) {
421                                 result->push_back(lua_tostring(L, -1));
422                                 num_strings++;
423                         }
424                         lua_pop(L, 1);
425                 }
426         } else if (lua_isstring(L, index)) {
427                 result->push_back(lua_tostring(L, index));
428                 num_strings++;
429         }
430
431         return num_strings;
432 }
433
434 /*
435         Table field getters
436 */
437
438 bool getstringfield(lua_State *L, int table,
439                 const char *fieldname, std::string &result)
440 {
441         lua_getfield(L, table, fieldname);
442         bool got = false;
443         if(lua_isstring(L, -1)){
444                 size_t len = 0;
445                 const char *ptr = lua_tolstring(L, -1, &len);
446                 if (ptr) {
447                         result.assign(ptr, len);
448                         got = true;
449                 }
450         }
451         lua_pop(L, 1);
452         return got;
453 }
454
455 bool getfloatfield(lua_State *L, int table,
456                 const char *fieldname, float &result)
457 {
458         lua_getfield(L, table, fieldname);
459         bool got = false;
460         if(lua_isnumber(L, -1)){
461                 result = lua_tonumber(L, -1);
462                 got = true;
463         }
464         lua_pop(L, 1);
465         return got;
466 }
467
468 bool getboolfield(lua_State *L, int table,
469                 const char *fieldname, bool &result)
470 {
471         lua_getfield(L, table, fieldname);
472         bool got = false;
473         if(lua_isboolean(L, -1)){
474                 result = lua_toboolean(L, -1);
475                 got = true;
476         }
477         lua_pop(L, 1);
478         return got;
479 }
480
481 size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
482                 std::vector<std::string> *result)
483 {
484         lua_getfield(L, table, fieldname);
485
486         size_t num_strings_read = read_stringlist(L, -1, result);
487
488         lua_pop(L, 1);
489         return num_strings_read;
490 }
491
492 std::string checkstringfield(lua_State *L, int table,
493                 const char *fieldname)
494 {
495         lua_getfield(L, table, fieldname);
496         CHECK_TYPE(-1, std::string("field \"") + fieldname + '"', LUA_TSTRING);
497         size_t len;
498         const char *s = lua_tolstring(L, -1, &len);
499         lua_pop(L, 1);
500         return std::string(s, len);
501 }
502
503 std::string getstringfield_default(lua_State *L, int table,
504                 const char *fieldname, const std::string &default_)
505 {
506         std::string result = default_;
507         getstringfield(L, table, fieldname, result);
508         return result;
509 }
510
511 int getintfield_default(lua_State *L, int table,
512                 const char *fieldname, int default_)
513 {
514         int result = default_;
515         getintfield(L, table, fieldname, result);
516         return result;
517 }
518
519 float getfloatfield_default(lua_State *L, int table,
520                 const char *fieldname, float default_)
521 {
522         float result = default_;
523         getfloatfield(L, table, fieldname, result);
524         return result;
525 }
526
527 bool getboolfield_default(lua_State *L, int table,
528                 const char *fieldname, bool default_)
529 {
530         bool result = default_;
531         getboolfield(L, table, fieldname, result);
532         return result;
533 }
534
535 v3s16 getv3s16field_default(lua_State *L, int table,
536                 const char *fieldname, v3s16 default_)
537 {
538         getv3intfield(L, table, fieldname, default_);
539         return default_;
540 }
541
542 void setstringfield(lua_State *L, int table,
543                 const char *fieldname, const std::string &value)
544 {
545         lua_pushlstring(L, value.c_str(), value.length());
546         if(table < 0)
547                 table -= 1;
548         lua_setfield(L, table, fieldname);
549 }
550
551 void setintfield(lua_State *L, int table,
552                 const char *fieldname, int value)
553 {
554         lua_pushinteger(L, value);
555         if(table < 0)
556                 table -= 1;
557         lua_setfield(L, table, fieldname);
558 }
559
560 void setfloatfield(lua_State *L, int table,
561                 const char *fieldname, float value)
562 {
563         lua_pushnumber(L, value);
564         if(table < 0)
565                 table -= 1;
566         lua_setfield(L, table, fieldname);
567 }
568
569 void setboolfield(lua_State *L, int table,
570                 const char *fieldname, bool value)
571 {
572         lua_pushboolean(L, value);
573         if(table < 0)
574                 table -= 1;
575         lua_setfield(L, table, fieldname);
576 }
577
578
579 ////
580 //// Array table slices
581 ////
582
583 size_t write_array_slice_float(
584         lua_State *L,
585         int table_index,
586         float *data,
587         v3u16 data_size,
588         v3u16 slice_offset,
589         v3u16 slice_size)
590 {
591         v3u16 pmin, pmax(data_size);
592
593         if (slice_offset.X > 0) {
594                 slice_offset.X--;
595                 pmin.X = slice_offset.X;
596                 pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
597         }
598
599         if (slice_offset.Y > 0) {
600                 slice_offset.Y--;
601                 pmin.Y = slice_offset.Y;
602                 pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
603         }
604
605         if (slice_offset.Z > 0) {
606                 slice_offset.Z--;
607                 pmin.Z = slice_offset.Z;
608                 pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
609         }
610
611         const u32 ystride = data_size.X;
612         const u32 zstride = data_size.X * data_size.Y;
613
614         u32 elem_index = 1;
615         for (u32 z = pmin.Z; z != pmax.Z; z++)
616         for (u32 y = pmin.Y; y != pmax.Y; y++)
617         for (u32 x = pmin.X; x != pmax.X; x++) {
618                 u32 i = z * zstride + y * ystride + x;
619                 lua_pushnumber(L, data[i]);
620                 lua_rawseti(L, table_index, elem_index);
621                 elem_index++;
622         }
623
624         return elem_index - 1;
625 }
626
627
628 size_t write_array_slice_u16(
629         lua_State *L,
630         int table_index,
631         u16 *data,
632         v3u16 data_size,
633         v3u16 slice_offset,
634         v3u16 slice_size)
635 {
636         v3u16 pmin, pmax(data_size);
637
638         if (slice_offset.X > 0) {
639                 slice_offset.X--;
640                 pmin.X = slice_offset.X;
641                 pmax.X = MYMIN(slice_offset.X + slice_size.X, data_size.X);
642         }
643
644         if (slice_offset.Y > 0) {
645                 slice_offset.Y--;
646                 pmin.Y = slice_offset.Y;
647                 pmax.Y = MYMIN(slice_offset.Y + slice_size.Y, data_size.Y);
648         }
649
650         if (slice_offset.Z > 0) {
651                 slice_offset.Z--;
652                 pmin.Z = slice_offset.Z;
653                 pmax.Z = MYMIN(slice_offset.Z + slice_size.Z, data_size.Z);
654         }
655
656         const u32 ystride = data_size.X;
657         const u32 zstride = data_size.X * data_size.Y;
658
659         u32 elem_index = 1;
660         for (u32 z = pmin.Z; z != pmax.Z; z++)
661         for (u32 y = pmin.Y; y != pmax.Y; y++)
662         for (u32 x = pmin.X; x != pmax.X; x++) {
663                 u32 i = z * zstride + y * ystride + x;
664                 lua_pushinteger(L, data[i]);
665                 lua_rawseti(L, table_index, elem_index);
666                 elem_index++;
667         }
668
669         return elem_index - 1;
670 }