c_converter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. /******************************************************************************/
  17. /******************************************************************************/
  18. /* WARNING!!!! do NOT add this header in any include file or any code file */
  19. /* not being a script/modapi file!!!!!!!! */
  20. /******************************************************************************/
  21. /******************************************************************************/
  22. #pragma once
  23. #include <vector>
  24. #include <unordered_map>
  25. #include "irrlichttypes_bloated.h"
  26. #include "common/c_types.h"
  27. extern "C" {
  28. #include <lua.h>
  29. }
  30. std::string getstringfield_default(lua_State *L, int table,
  31. const char *fieldname, const std::string &default_);
  32. bool getboolfield_default(lua_State *L, int table,
  33. const char *fieldname, bool default_);
  34. float getfloatfield_default(lua_State *L, int table,
  35. const char *fieldname, float default_);
  36. int getintfield_default(lua_State *L, int table,
  37. const char *fieldname, int default_);
  38. bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname);
  39. template<typename T>
  40. bool getintfield(lua_State *L, int table,
  41. const char *fieldname, T &result)
  42. {
  43. lua_getfield(L, table, fieldname);
  44. bool got = false;
  45. if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)){
  46. result = lua_tointeger(L, -1);
  47. got = true;
  48. }
  49. lua_pop(L, 1);
  50. return got;
  51. }
  52. // Retrieve an v3s16 where all components are optional (falls back to default)
  53. v3s16 getv3s16field_default(lua_State *L, int table,
  54. const char *fieldname, v3s16 default_);
  55. bool getstringfield(lua_State *L, int table,
  56. const char *fieldname, std::string &result);
  57. size_t getstringlistfield(lua_State *L, int table,
  58. const char *fieldname,
  59. std::vector<std::string> *result);
  60. void read_groups(lua_State *L, int index,
  61. std::unordered_map<std::string, int> &result);
  62. bool getboolfield(lua_State *L, int table,
  63. const char *fieldname, bool &result);
  64. bool getfloatfield(lua_State *L, int table,
  65. const char *fieldname, float &result);
  66. void setstringfield(lua_State *L, int table,
  67. const char *fieldname, const std::string &value);
  68. void setintfield(lua_State *L, int table,
  69. const char *fieldname, int value);
  70. void setfloatfield(lua_State *L, int table,
  71. const char *fieldname, float value);
  72. void setboolfield(lua_State *L, int table,
  73. const char *fieldname, bool value);
  74. v3f checkFloatPos (lua_State *L, int index);
  75. v2f check_v2f (lua_State *L, int index);
  76. v3f check_v3f (lua_State *L, int index);
  77. v3s16 check_v3s16 (lua_State *L, int index);
  78. v3f read_v3f (lua_State *L, int index);
  79. v2f read_v2f (lua_State *L, int index);
  80. v2s16 read_v2s16 (lua_State *L, int index);
  81. v2s32 read_v2s32 (lua_State *L, int index);
  82. video::SColor read_ARGB8 (lua_State *L, int index);
  83. bool read_color (lua_State *L, int index,
  84. video::SColor *color);
  85. bool is_color_table (lua_State *L, int index);
  86. aabb3f read_aabb3f (lua_State *L, int index, f32 scale);
  87. v3s16 read_v3s16 (lua_State *L, int index);
  88. std::vector<aabb3f> read_aabb3f_vector (lua_State *L, int index, f32 scale);
  89. size_t read_stringlist (lua_State *L, int index,
  90. std::vector<std::string> *result);
  91. void push_v2s16 (lua_State *L, v2s16 p);
  92. void push_v2s32 (lua_State *L, v2s32 p);
  93. void push_v3s16 (lua_State *L, v3s16 p);
  94. void push_aabb3f (lua_State *L, aabb3f box);
  95. void push_ARGB8 (lua_State *L, video::SColor color);
  96. void pushFloatPos (lua_State *L, v3f p);
  97. void push_v3f (lua_State *L, v3f p);
  98. void push_v2f (lua_State *L, v2f p);
  99. void warn_if_field_exists(lua_State *L, int table,
  100. const char *fieldname,
  101. const std::string &message);
  102. size_t write_array_slice_float(lua_State *L, int table_index, float *data,
  103. v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);
  104. // This must match the implementation in builtin/game/misc_s.lua
  105. // Note that this returns a floating point result as Lua integers are 32-bit
  106. inline lua_Number hash_node_position(v3s16 pos)
  107. {
  108. return (((s64)pos.Z + 0x8000L) << 32)
  109. | (((s64)pos.Y + 0x8000L) << 16)
  110. | ((s64)pos.X + 0x8000L);
  111. }