Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
4.9 KiB

  1. #include <stdio.h>
  2. #if !defined(OSX) && !defined (LINUX)
  3. extern "C"
  4. {
  5. #endif
  6. #include "lua.h"
  7. #include "lauxlib.h"
  8. #if !defined(OSX) && !defined (LINUX)
  9. }
  10. #endif
  11. #include "vec3.h"
  12. #define VEC3_TYPE "Vec3"
  13. #define VEC3_NAME "Vec3"
  14. Vector *lua_getvec3( lua_State *pState, int i )
  15. {
  16. if ( luaL_checkudata( pState, i, VEC3_TYPE ) == NULL )
  17. {
  18. luaL_typerror( pState, i, VEC3_TYPE );
  19. }
  20. return ( Vector * )lua_touserdata( pState, i );
  21. }
  22. Vector lua_getvec3ByValue( lua_State *pState, int i )
  23. {
  24. if ( lua_isnumber( pState, i ) )
  25. {
  26. float flValue = lua_tonumber( pState, i );
  27. return Vector( flValue, flValue, flValue );
  28. }
  29. if ( luaL_checkudata( pState, i, VEC3_TYPE ) == NULL )
  30. {
  31. luaL_typerror( pState, i, VEC3_TYPE );
  32. }
  33. return *( Vector * )lua_touserdata( pState, i );
  34. }
  35. static Vector *lua_allocvec3( lua_State *pState )
  36. {
  37. Vector *v =( Vector *)lua_newuserdata( pState, sizeof( Vector ) );
  38. luaL_getmetatable( pState, VEC3_TYPE );
  39. lua_setmetatable( pState, -2 );
  40. return v;
  41. }
  42. Vector *lua_newvec3( lua_State *pState, const Vector *Value )
  43. {
  44. Vector *v;
  45. v = lua_allocvec3( pState );
  46. v->x = Value->x;
  47. v->y = Value->y;
  48. v->z = Value->z;
  49. return v;
  50. }
  51. static int vec3_new( lua_State *pState )
  52. {
  53. Vector *v;
  54. lua_settop( pState, 3 );
  55. v = lua_allocvec3( pState );
  56. v->x = luaL_optnumber( pState, 1, 0 );
  57. v->y = luaL_optnumber( pState, 2, 0 );
  58. v->z = luaL_optnumber( pState, 3, 0 );
  59. return 1;
  60. }
  61. static int vec3_index( lua_State *pState )
  62. {
  63. const char *pszKey = luaL_checkstring( pState, 2 );
  64. if ( pszKey[ 1 ] == '\0' )
  65. {
  66. Vector *v = lua_getvec3( pState, 1 );
  67. switch ( pszKey[ 0 ] )
  68. {
  69. case '1': case 'x': case 'r':
  70. lua_pushnumber( pState, v->x );
  71. return 1;
  72. case '2': case 'y': case 'g':
  73. lua_pushnumber( pState, v->y );
  74. return 1;
  75. case '3': case 'z': case 'b':
  76. lua_pushnumber( pState, v->z );
  77. return 1;
  78. }
  79. }
  80. lua_getfield( pState, LUA_REGISTRYINDEX, VEC3_TYPE );
  81. lua_pushstring( pState, pszKey );
  82. lua_rawget( pState, -2 );
  83. return 1;
  84. }
  85. static int vec3_newindex( lua_State *pState )
  86. {
  87. const char *pszKey = luaL_checkstring( pState, 2 );
  88. if ( pszKey[ 1 ] == '\0' )
  89. {
  90. Vector *v = lua_getvec3( pState, 1 );
  91. float flValue = luaL_checknumber( pState, 3 );
  92. switch ( pszKey[ 0 ] )
  93. {
  94. case '1': case 'x': case 'r':
  95. v->x = flValue;
  96. break;
  97. case '2': case 'y': case 'g':
  98. v->y = flValue;
  99. break;
  100. case '3': case 'z': case 'b':
  101. v->z = flValue;
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. return 1;
  108. }
  109. static int vec3_tostring( lua_State *pState )
  110. {
  111. char s[ 64 ];
  112. Vector *v = lua_getvec3( pState, 1 );
  113. sprintf( s, "%s %p", VEC3_TYPE, ( void * )v );
  114. lua_pushstring( pState, s );
  115. return 1;
  116. }
  117. static int vec3_add( lua_State *pState )
  118. {
  119. Vector v1 = lua_getvec3ByValue( pState, 1 );
  120. Vector v2 = lua_getvec3ByValue( pState, 2 );
  121. Vector vResult = v1 + v2;
  122. lua_newvec3( pState, &vResult );
  123. return 1;
  124. }
  125. static int vec3_subtract( lua_State *pState )
  126. {
  127. Vector v1 = lua_getvec3ByValue( pState, 1 );
  128. Vector v2 = lua_getvec3ByValue( pState, 2 );
  129. Vector vResult = v1 - v2;
  130. lua_newvec3( pState, &vResult );
  131. return 1;
  132. }
  133. static int vec3_multiply( lua_State *pState )
  134. {
  135. Vector v1 = lua_getvec3ByValue( pState, 1 );
  136. Vector v2 = lua_getvec3ByValue( pState, 2 );
  137. Vector vResult = v1 * v2;
  138. lua_newvec3( pState, &vResult );
  139. return 1;
  140. }
  141. static int vec3_divide( lua_State *pState )
  142. {
  143. Vector v1 = lua_getvec3ByValue( pState, 1 );
  144. Vector v2 = lua_getvec3ByValue( pState, 2 );
  145. Vector vResult = v1 / v2;
  146. lua_newvec3( pState, &vResult );
  147. return 1;
  148. }
  149. static int vec3_length( lua_State *pState )
  150. {
  151. Vector v1 = lua_getvec3ByValue( pState, 1 );
  152. float flResult = v1.Length();
  153. lua_pushnumber( pState, flResult );
  154. return 1;
  155. }
  156. static int vec3_equal( lua_State *pState )
  157. {
  158. Vector v1 = lua_getvec3ByValue( pState, 1 );
  159. Vector v2 = lua_getvec3ByValue( pState, 2 );
  160. return ( v1 == v2 ? 1 : 0 );
  161. }
  162. static int vec3_dot( lua_State *pState )
  163. {
  164. Vector v1 = lua_getvec3ByValue( pState, 1 );
  165. Vector v2 = lua_getvec3ByValue( pState, 2 );
  166. float flResult = v1.Dot( v2 );
  167. lua_pushnumber( pState, flResult );
  168. return 1;
  169. }
  170. static int vec3_cross( lua_State *pState )
  171. {
  172. Vector v1 = lua_getvec3ByValue( pState, 1 );
  173. Vector v2 = lua_getvec3ByValue( pState,2 );
  174. Vector vResult = v1,Cross( v2 );
  175. lua_newvec3( pState, &vResult );
  176. return 1;
  177. }
  178. static const luaL_reg Registrations[] =
  179. {
  180. { "__index", vec3_index },
  181. { "__newindex", vec3_newindex },
  182. { "__tostring", vec3_tostring },
  183. { "__add", vec3_add },
  184. { "__sub", vec3_subtract },
  185. { "__mul", vec3_multiply },
  186. { "__div", vec3_divide },
  187. { "__len", vec3_length },
  188. { "__eq", vec3_equal },
  189. { "dot", vec3_dot },
  190. { "cross", vec3_cross },
  191. { NULL, NULL }
  192. };
  193. LUALIB_API int luaopen_vec3( lua_State *pState )
  194. {
  195. luaL_newmetatable( pState, VEC3_TYPE );
  196. luaL_register( pState, NULL, Registrations );
  197. lua_register( pState, VEC3_NAME, vec3_new );
  198. return 1;
  199. }