lua_environment.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "config.h"
  7. #include "types.h"
  8. #include "resource_types.h"
  9. #include "math_types.h"
  10. #include <lua.hpp>
  11. namespace crown
  12. {
  13. enum LuaArgumentType
  14. {
  15. ARGUMENT_FLOAT
  16. };
  17. // HACK
  18. struct Actor;
  19. struct Unit;
  20. /// LuaEnvironment is a wrapper of a subset of Lua functions and
  21. /// provides utilities for extending Lua
  22. struct LuaEnvironment
  23. {
  24. LuaEnvironment();
  25. ~LuaEnvironment();
  26. void load_libs();
  27. void execute(const LuaResource* lr);
  28. /// Loads and executes the given @a s lua string.
  29. void execute_string(const char* s);
  30. /// Loads the function with the given @a name and @a func into the table @a module.
  31. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  32. void load_module_function(const char* module, const char* name, const char* value);
  33. void load_module_constructor(const char* module, const lua_CFunction func);
  34. /// Loads the enum with the given @a name and @a value into the table @a module.
  35. void load_module_enum(const char* module, const char* name, uint32_t value);
  36. /// Calls the global function @a func with @a argc argument number.
  37. /// Each argument is a pair (type, value).
  38. /// Example call:
  39. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  40. /// Returns true if success, false otherwise
  41. void call_global(const char* func, uint8_t argc, ...);
  42. void clear_temporaries();
  43. Vector3* next_vector3(const Vector3& v);
  44. Quaternion* next_quaternion(const Quaternion& q);
  45. Matrix4x4* next_matrix4x4(const Matrix4x4& m);
  46. bool is_vector3(int i);
  47. bool is_quaternion(int i);
  48. bool is_matrix4x4(int i);
  49. // FIXME
  50. void call_physics_callback(Actor* actor_0, Actor* actor_1, Unit* unit_0, Unit* unit_1, const Vector3& where, const Vector3& normal, const char* type);
  51. void call_trigger_callback(Actor* trigger, Actor* other, const char* type);
  52. private:
  53. lua_State* L;
  54. uint32_t _vec3_used;
  55. Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
  56. uint32_t _quat_used;
  57. Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
  58. uint32_t _mat4_used;
  59. Matrix4x4 s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
  60. private:
  61. // Disable copying
  62. LuaEnvironment(const LuaEnvironment&);
  63. LuaEnvironment& operator=(const LuaEnvironment&);
  64. };
  65. } // namespace crown