lua_api.cpp 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "color4.h"
  6. #include "console_server.h"
  7. #include "debug_line.h"
  8. #include "device.h"
  9. #include "gui.h"
  10. #include "input_device.h"
  11. #include "input_manager.h"
  12. #include "intersection.h"
  13. #include "lua_environment.h"
  14. #include "lua_stack.h"
  15. #include "material.h"
  16. #include "math_types.h"
  17. #include "math_utils.h"
  18. #include "matrix4x4.h"
  19. #include "physics_world.h"
  20. #include "plane.h"
  21. #include "profiler.h"
  22. #include "quaternion.h"
  23. #include "render_world.h"
  24. #include "resource_manager.h"
  25. #include "resource_package.h"
  26. #include "scene_graph.h"
  27. #include "sound_world.h"
  28. #include "string_stream.h"
  29. #include "temp_allocator.h"
  30. #include "unit_manager.h"
  31. #include "vector2.h"
  32. #include "vector3.h"
  33. #include "world.h"
  34. namespace crown
  35. {
  36. struct LightInfo
  37. {
  38. const char* name;
  39. LightType::Enum type;
  40. };
  41. static LightInfo s_light[] =
  42. {
  43. { "directional", LightType::DIRECTIONAL },
  44. { "omni", LightType::OMNI },
  45. { "spot", LightType::SPOT }
  46. };
  47. CE_STATIC_ASSERT(CE_COUNTOF(s_light) == LightType::COUNT);
  48. struct ProjectionInfo
  49. {
  50. const char* name;
  51. ProjectionType::Enum type;
  52. };
  53. static ProjectionInfo s_projection[] =
  54. {
  55. { "orthographic", ProjectionType::ORTHOGRAPHIC },
  56. { "perspective", ProjectionType::PERSPECTIVE }
  57. };
  58. CE_STATIC_ASSERT(CE_COUNTOF(s_projection) == ProjectionType::COUNT);
  59. struct RaycastInfo
  60. {
  61. const char* name;
  62. RaycastMode::Enum mode;
  63. };
  64. static RaycastInfo s_raycast[] =
  65. {
  66. { "closest", RaycastMode::CLOSEST },
  67. { "all", RaycastMode::ALL }
  68. };
  69. CE_STATIC_ASSERT(CE_COUNTOF(s_raycast) == RaycastMode::COUNT);
  70. static LightType::Enum name_to_light_type(const char* name)
  71. {
  72. for (u32 i = 0; i < CE_COUNTOF(s_light); ++i)
  73. {
  74. if (strcmp(s_light[i].name, name) == 0)
  75. return s_light[i].type;
  76. }
  77. return LightType::COUNT;
  78. }
  79. static ProjectionType::Enum name_to_projection_type(const char* name)
  80. {
  81. for (u32 i = 0; i < CE_COUNTOF(s_projection); ++i)
  82. {
  83. if (strcmp(s_projection[i].name, name) == 0)
  84. return s_projection[i].type;
  85. }
  86. return ProjectionType::COUNT;
  87. }
  88. static RaycastMode::Enum name_to_raycast_mode(const char* name)
  89. {
  90. for (u32 i = 0; i < CE_COUNTOF(s_raycast); ++i)
  91. {
  92. if (strcmp(s_raycast[i].name, name) == 0)
  93. return s_raycast[i].mode;
  94. }
  95. return RaycastMode::COUNT;
  96. }
  97. static int math_ray_plane_intersection(lua_State* L)
  98. {
  99. LuaStack stack(L);
  100. const Plane p = plane::from_point_and_normal(stack.get_vector3(3)
  101. , stack.get_vector3(4)
  102. );
  103. const f32 t = ray_plane_intersection(stack.get_vector3(1)
  104. , stack.get_vector3(2)
  105. , p
  106. );
  107. stack.push_float(t);
  108. return 1;
  109. }
  110. static int math_ray_disc_intersection(lua_State* L)
  111. {
  112. LuaStack stack(L);
  113. const f32 t = ray_disc_intersection(stack.get_vector3(1)
  114. , stack.get_vector3(2)
  115. , stack.get_vector3(3)
  116. , stack.get_float(4)
  117. , stack.get_vector3(5)
  118. );
  119. stack.push_float(t);
  120. return 1;
  121. }
  122. static int math_ray_sphere_intersection(lua_State* L)
  123. {
  124. LuaStack stack(L);
  125. Sphere s;
  126. s.c = stack.get_vector3(3);
  127. s.r = stack.get_float(4);
  128. const f32 t = ray_sphere_intersection(stack.get_vector3(1)
  129. , stack.get_vector3(2)
  130. , s
  131. );
  132. stack.push_float(t);
  133. return 1;
  134. }
  135. static int math_ray_obb_intersection(lua_State* L)
  136. {
  137. LuaStack stack(L);
  138. const f32 t = ray_obb_intersection(stack.get_vector3(1)
  139. , stack.get_vector3(2)
  140. , stack.get_matrix4x4(3)
  141. , stack.get_vector3(4)
  142. );
  143. stack.push_float(t);
  144. return 1;
  145. }
  146. static int math_ray_triangle_intersection(lua_State* L)
  147. {
  148. LuaStack stack(L);
  149. const f32 t = ray_triangle_intersection(stack.get_vector3(1)
  150. , stack.get_vector3(2)
  151. , stack.get_vector3(3)
  152. , stack.get_vector3(4)
  153. , stack.get_vector3(5)
  154. );
  155. stack.push_float(t);
  156. return 1;
  157. }
  158. static int vector3_new(lua_State* L)
  159. {
  160. LuaStack stack(L);
  161. Vector3 v;
  162. v.x = stack.get_float(1);
  163. v.y = stack.get_float(2);
  164. v.z = stack.get_float(3);
  165. stack.push_vector3(v);
  166. return 1;
  167. }
  168. static int vector3_ctor(lua_State* L)
  169. {
  170. LuaStack stack(L);
  171. Vector3 v;
  172. v.x = stack.get_float(1 + 1);
  173. v.y = stack.get_float(2 + 1);
  174. v.z = stack.get_float(3 + 1);
  175. stack.push_vector3(v);
  176. return 1;
  177. }
  178. static int vector3_x(lua_State* L)
  179. {
  180. LuaStack stack(L);
  181. stack.push_float(stack.get_vector3(1).x);
  182. return 1;
  183. }
  184. static int vector3_y(lua_State* L)
  185. {
  186. LuaStack stack(L);
  187. stack.push_float(stack.get_vector3(1).y);
  188. return 1;
  189. }
  190. static int vector3_z(lua_State* L)
  191. {
  192. LuaStack stack(L);
  193. stack.push_float(stack.get_vector3(1).z);
  194. return 1;
  195. }
  196. static int vector3_set_x(lua_State* L)
  197. {
  198. LuaStack stack(L);
  199. stack.get_vector3(1).x = stack.get_float(2);
  200. return 0;
  201. }
  202. static int vector3_set_y(lua_State* L)
  203. {
  204. LuaStack stack(L);
  205. stack.get_vector3(1).y = stack.get_float(2);
  206. return 0;
  207. }
  208. static int vector3_set_z(lua_State* L)
  209. {
  210. LuaStack stack(L);
  211. stack.get_vector3(1).z = stack.get_float(2);
  212. return 0;
  213. }
  214. static int vector3_elements(lua_State* L)
  215. {
  216. LuaStack stack(L);
  217. Vector3& a = stack.get_vector3(1);
  218. stack.push_float(a.x);
  219. stack.push_float(a.y);
  220. stack.push_float(a.z);
  221. return 3;
  222. }
  223. static int vector3_add(lua_State* L)
  224. {
  225. LuaStack stack(L);
  226. stack.push_vector3(stack.get_vector3(1) + stack.get_vector3(2));
  227. return 1;
  228. }
  229. static int vector3_subtract(lua_State* L)
  230. {
  231. LuaStack stack(L);
  232. stack.push_vector3(stack.get_vector3(1) - stack.get_vector3(2));
  233. return 1;
  234. }
  235. static int vector3_multiply(lua_State* L)
  236. {
  237. LuaStack stack(L);
  238. stack.push_vector3(stack.get_vector3(1) * stack.get_float(2));
  239. return 1;
  240. }
  241. static int vector3_dot(lua_State* L)
  242. {
  243. LuaStack stack(L);
  244. stack.push_float(dot(stack.get_vector3(1), stack.get_vector3(2)));
  245. return 1;
  246. }
  247. static int vector3_cross(lua_State* L)
  248. {
  249. LuaStack stack(L);
  250. stack.push_vector3(cross(stack.get_vector3(1), stack.get_vector3(2)));
  251. return 1;
  252. }
  253. static int vector3_equal(lua_State* L)
  254. {
  255. LuaStack stack(L);
  256. stack.push_bool(stack.get_vector3(1) == stack.get_vector3(2));
  257. return 1;
  258. }
  259. static int vector3_length(lua_State* L)
  260. {
  261. LuaStack stack(L);
  262. stack.push_float(length(stack.get_vector3(1)));
  263. return 1;
  264. }
  265. static int vector3_length_squared(lua_State* L)
  266. {
  267. LuaStack stack(L);
  268. stack.push_float(length_squared(stack.get_vector3(1)));
  269. return 1;
  270. }
  271. static int vector3_set_length(lua_State* L)
  272. {
  273. LuaStack stack(L);
  274. set_length(stack.get_vector3(1), stack.get_float(2));
  275. return 0;
  276. }
  277. static int vector3_normalize(lua_State* L)
  278. {
  279. LuaStack stack(L);
  280. stack.push_vector3(normalize(stack.get_vector3(1)));
  281. return 1;
  282. }
  283. static int vector3_distance(lua_State* L)
  284. {
  285. LuaStack stack(L);
  286. stack.push_float(distance(stack.get_vector3(1), stack.get_vector3(2)));
  287. return 1;
  288. }
  289. static int vector3_distance_squared(lua_State* L)
  290. {
  291. LuaStack stack(L);
  292. stack.push_float(distance_squared(stack.get_vector3(1), stack.get_vector3(2)));
  293. return 1;
  294. }
  295. static int vector3_angle(lua_State* L)
  296. {
  297. LuaStack stack(L);
  298. stack.push_float(angle(stack.get_vector3(1), stack.get_vector3(2)));
  299. return 1;
  300. }
  301. static int vector3_max(lua_State* L)
  302. {
  303. LuaStack stack(L);
  304. stack.push_vector3(max(stack.get_vector3(1), stack.get_vector3(2)));
  305. return 1;
  306. }
  307. static int vector3_min(lua_State* L)
  308. {
  309. LuaStack stack(L);
  310. stack.push_vector3(min(stack.get_vector3(1), stack.get_vector3(2)));
  311. return 1;
  312. }
  313. static int vector3_lerp(lua_State* L)
  314. {
  315. LuaStack stack(L);
  316. stack.push_vector3(lerp(stack.get_vector3(1), stack.get_vector3(2), stack.get_float(3)));
  317. return 1;
  318. }
  319. static int vector3_forward(lua_State* L)
  320. {
  321. LuaStack stack(L);
  322. stack.push_vector3(VECTOR3_FORWARD);
  323. return 1;
  324. }
  325. static int vector3_backward(lua_State* L)
  326. {
  327. LuaStack stack(L);
  328. stack.push_vector3(VECTOR3_BACKWARD);
  329. return 1;
  330. }
  331. static int vector3_left(lua_State* L)
  332. {
  333. LuaStack stack(L);
  334. stack.push_vector3(VECTOR3_LEFT);
  335. return 1;
  336. }
  337. static int vector3_right(lua_State* L)
  338. {
  339. LuaStack stack(L);
  340. stack.push_vector3(VECTOR3_RIGHT);
  341. return 1;
  342. }
  343. static int vector3_up(lua_State* L)
  344. {
  345. LuaStack stack(L);
  346. stack.push_vector3(VECTOR3_UP);
  347. return 1;
  348. }
  349. static int vector3_down(lua_State* L)
  350. {
  351. LuaStack stack(L);
  352. stack.push_vector3(VECTOR3_DOWN);
  353. return 1;
  354. }
  355. static int vector3_zero(lua_State* L)
  356. {
  357. LuaStack stack(L);
  358. stack.push_vector3(VECTOR3_ZERO);
  359. return 1;
  360. }
  361. static int vector3_to_string(lua_State* L)
  362. {
  363. LuaStack stack(L);
  364. const Vector3 v = stack.get_vector3(1);
  365. char buf[256];
  366. snprintf(buf, sizeof(buf), "%.4f %.4f %.4f", v.x, v.y, v.z);
  367. stack.push_string(buf);
  368. return 1;
  369. }
  370. static int vector2_new(lua_State* L)
  371. {
  372. LuaStack stack(L);
  373. Vector2 v;
  374. v.x = stack.get_float(1);
  375. v.y = stack.get_float(2);
  376. stack.push_vector2(v);
  377. return 1;
  378. }
  379. static int vector2_ctor(lua_State* L)
  380. {
  381. LuaStack stack(L);
  382. Vector2 v;
  383. v.x = stack.get_float(1 + 1);
  384. v.y = stack.get_float(2 + 1);
  385. stack.push_vector2(v);
  386. return 1;
  387. }
  388. static int vector3box_new(lua_State* L)
  389. {
  390. LuaStack stack(L);
  391. if (stack.num_args() == 0)
  392. {
  393. stack.push_vector3box(VECTOR3_ZERO);
  394. }
  395. else if (stack.num_args() == 1)
  396. {
  397. stack.push_vector3box(stack.get_vector3(1));
  398. }
  399. else
  400. {
  401. Vector3 v;
  402. v.x = stack.get_float(1);
  403. v.y = stack.get_float(2);
  404. v.z = stack.get_float(3);
  405. stack.push_vector3box(v);
  406. }
  407. return 1;
  408. }
  409. static int vector3box_ctor(lua_State* L)
  410. {
  411. LuaStack stack(L);
  412. if (stack.num_args() == 0 + 1)
  413. {
  414. stack.push_vector3box(VECTOR3_ZERO);
  415. }
  416. else if (stack.num_args() == 1 + 1)
  417. {
  418. stack.push_vector3box(stack.get_vector3(1 + 1));
  419. }
  420. else
  421. {
  422. Vector3 v;
  423. v.x = stack.get_float(1 + 1);
  424. v.y = stack.get_float(2 + 1);
  425. v.z = stack.get_float(3 + 1);
  426. stack.push_vector3box(v);
  427. }
  428. return 1;
  429. }
  430. static int vector3box_store(lua_State* L)
  431. {
  432. LuaStack stack(L);
  433. Vector3& v = stack.get_vector3box(1);
  434. if (stack.num_args() == 2)
  435. v = stack.get_vector3(2);
  436. else
  437. v = vector3(stack.get_float(2)
  438. , stack.get_float(3)
  439. , stack.get_float(4));
  440. return 0;
  441. }
  442. static int vector3box_unbox(lua_State* L)
  443. {
  444. LuaStack stack(L);
  445. stack.push_vector3(stack.get_vector3box(1));
  446. return 1;
  447. }
  448. static int vector3box_tostring(lua_State* L)
  449. {
  450. LuaStack stack(L);
  451. Vector3& v = stack.get_vector3box(1);
  452. stack.push_fstring("Vector3Box (%p)", &v);
  453. return 1;
  454. }
  455. static int matrix4x4_new(lua_State* L)
  456. {
  457. LuaStack stack(L);
  458. Matrix4x4 m;
  459. m.x.x = stack.get_float( 1);
  460. m.x.y = stack.get_float( 2);
  461. m.x.z = stack.get_float( 3);
  462. m.x.w = stack.get_float( 4);
  463. m.y.x = stack.get_float( 5);
  464. m.y.y = stack.get_float( 6);
  465. m.y.z = stack.get_float( 7);
  466. m.y.w = stack.get_float( 8);
  467. m.z.x = stack.get_float( 9);
  468. m.z.y = stack.get_float(10);
  469. m.z.z = stack.get_float(11);
  470. m.z.w = stack.get_float(12);
  471. m.t.x = stack.get_float(13);
  472. m.t.y = stack.get_float(14);
  473. m.t.z = stack.get_float(15);
  474. m.t.w = stack.get_float(16);
  475. stack.push_matrix4x4(m);
  476. return 1;
  477. }
  478. static int matrix4x4_ctor(lua_State* L)
  479. {
  480. LuaStack stack(L);
  481. Matrix4x4 m;
  482. m.x.x = stack.get_float( 1 + 1);
  483. m.x.y = stack.get_float( 2 + 1);
  484. m.x.z = stack.get_float( 3 + 1);
  485. m.x.w = stack.get_float( 4 + 1);
  486. m.y.x = stack.get_float( 5 + 1);
  487. m.y.y = stack.get_float( 6 + 1);
  488. m.y.z = stack.get_float( 7 + 1);
  489. m.y.w = stack.get_float( 8 + 1);
  490. m.z.x = stack.get_float( 9 + 1);
  491. m.z.y = stack.get_float(10 + 1);
  492. m.z.z = stack.get_float(11 + 1);
  493. m.z.w = stack.get_float(12 + 1);
  494. m.t.x = stack.get_float(13 + 1);
  495. m.t.y = stack.get_float(14 + 1);
  496. m.t.z = stack.get_float(15 + 1);
  497. m.t.w = stack.get_float(16 + 1);
  498. stack.push_matrix4x4(m);
  499. return 1;
  500. }
  501. static int matrix4x4_from_quaternion(lua_State* L)
  502. {
  503. LuaStack stack(L);
  504. stack.push_matrix4x4(matrix4x4(stack.get_quaternion(1), vector3(0, 0, 0)));
  505. return 1;
  506. }
  507. static int matrix4x4_from_translation(lua_State* L)
  508. {
  509. LuaStack stack(L);
  510. stack.push_matrix4x4(matrix4x4(QUATERNION_IDENTITY, stack.get_vector3(1)));
  511. return 1;
  512. }
  513. static int matrix4x4_from_quaternion_translation(lua_State* L)
  514. {
  515. LuaStack stack(L);
  516. stack.push_matrix4x4(matrix4x4(stack.get_quaternion(1), stack.get_vector3(2)));
  517. return 1;
  518. }
  519. static int matrix4x4_from_axes(lua_State* L)
  520. {
  521. LuaStack stack(L);
  522. stack.push_matrix4x4(matrix4x4(stack.get_vector3(1), stack.get_vector3(2), stack.get_vector3(3), stack.get_vector3(4)));
  523. return 1;
  524. }
  525. static int matrix4x4_copy(lua_State* L)
  526. {
  527. LuaStack stack(L);
  528. stack.push_matrix4x4(stack.get_matrix4x4(1));
  529. return 1;
  530. }
  531. static int matrix4x4_add(lua_State* L)
  532. {
  533. LuaStack stack(L);
  534. stack.push_matrix4x4(stack.get_matrix4x4(1) + stack.get_matrix4x4(2));
  535. return 1;
  536. }
  537. static int matrix4x4_subtract(lua_State* L)
  538. {
  539. LuaStack stack(L);
  540. stack.push_matrix4x4(stack.get_matrix4x4(1) - stack.get_matrix4x4(2));
  541. return 1;
  542. }
  543. static int matrix4x4_multiply(lua_State* L)
  544. {
  545. LuaStack stack(L);
  546. stack.push_matrix4x4(stack.get_matrix4x4(1) * stack.get_matrix4x4(2));
  547. return 1;
  548. }
  549. static int matrix4x4_transpose(lua_State* L)
  550. {
  551. LuaStack stack(L);
  552. stack.push_matrix4x4(transpose(stack.get_matrix4x4(1)));
  553. return 1;
  554. }
  555. static int matrix4x4_determinant(lua_State* L)
  556. {
  557. LuaStack stack(L);
  558. stack.push_float(determinant(stack.get_matrix4x4(1)));
  559. return 1;
  560. }
  561. static int matrix4x4_invert(lua_State* L)
  562. {
  563. LuaStack stack(L);
  564. stack.push_matrix4x4(invert(stack.get_matrix4x4(1)));
  565. return 1;
  566. }
  567. static int matrix4x4_x(lua_State* L)
  568. {
  569. LuaStack stack(L);
  570. stack.push_vector3(x(stack.get_matrix4x4(1)));
  571. return 1;
  572. }
  573. static int matrix4x4_y(lua_State* L)
  574. {
  575. LuaStack stack(L);
  576. stack.push_vector3(y(stack.get_matrix4x4(1)));
  577. return 1;
  578. }
  579. static int matrix4x4_z(lua_State* L)
  580. {
  581. LuaStack stack(L);
  582. stack.push_vector3(z(stack.get_matrix4x4(1)));
  583. return 1;
  584. }
  585. static int matrix4x4_set_x(lua_State* L)
  586. {
  587. LuaStack stack(L);
  588. set_x(stack.get_matrix4x4(1), stack.get_vector3(2));
  589. return 0;
  590. }
  591. static int matrix4x4_set_y(lua_State* L)
  592. {
  593. LuaStack stack(L);
  594. set_y(stack.get_matrix4x4(1), stack.get_vector3(2));
  595. return 0;
  596. }
  597. static int matrix4x4_set_z(lua_State* L)
  598. {
  599. LuaStack stack(L);
  600. set_z(stack.get_matrix4x4(1), stack.get_vector3(2));
  601. return 0;
  602. }
  603. static int matrix4x4_translation(lua_State* L)
  604. {
  605. LuaStack stack(L);
  606. stack.push_vector3(translation(stack.get_matrix4x4(1)));
  607. return 1;
  608. }
  609. static int matrix4x4_set_translation(lua_State* L)
  610. {
  611. LuaStack stack(L);
  612. set_translation(stack.get_matrix4x4(1), stack.get_vector3(2));
  613. return 0;
  614. }
  615. static int matrix4x4_rotation(lua_State* L)
  616. {
  617. LuaStack stack(L);
  618. stack.push_quaternion(rotation(stack.get_matrix4x4(1)));
  619. return 1;
  620. }
  621. static int matrix4x4_set_rotation(lua_State* L)
  622. {
  623. LuaStack stack(L);
  624. set_rotation(stack.get_matrix4x4(1), stack.get_quaternion(2));
  625. return 0;
  626. }
  627. static int matrix4x4_identity(lua_State* L)
  628. {
  629. LuaStack stack(L);
  630. stack.push_matrix4x4(MATRIX4X4_IDENTITY);
  631. return 1;
  632. }
  633. static int matrix4x4_transform(lua_State* L)
  634. {
  635. LuaStack stack(L);
  636. stack.push_vector3(stack.get_vector3(2) * stack.get_matrix4x4(1));
  637. return 1;
  638. }
  639. static int matrix4x4_to_string(lua_State* L)
  640. {
  641. LuaStack stack(L);
  642. Matrix4x4& a = stack.get_matrix4x4(1);
  643. char buf[1024];
  644. snprintf(buf, sizeof(buf),
  645. "%.4f, %.4f, %.4f, %.4f\n"
  646. "%.4f, %.4f, %.4f, %.4f\n"
  647. "%.4f, %.4f, %.4f, %.4f\n"
  648. "%.4f, %.4f, %.4f, %.4f"
  649. , a.x.x, a.x.y, a.x.z, a.y.w
  650. , a.y.x, a.y.y, a.y.z, a.y.w
  651. , a.z.x, a.z.y, a.z.z, a.z.w
  652. , a.t.x, a.t.y, a.t.z, a.t.w
  653. );
  654. stack.push_string(buf);
  655. return 1;
  656. }
  657. static int matrix4x4box_new(lua_State* L)
  658. {
  659. LuaStack stack(L);
  660. if (stack.num_args() == 0)
  661. stack.push_matrix4x4(MATRIX4X4_IDENTITY);
  662. else
  663. stack.push_matrix4x4box(stack.get_matrix4x4(1));
  664. return 1;
  665. }
  666. static int matrix4x4box_ctor(lua_State* L)
  667. {
  668. LuaStack stack(L);
  669. if (stack.num_args() == 0 + 1)
  670. stack.push_matrix4x4(MATRIX4X4_IDENTITY);
  671. else
  672. stack.push_matrix4x4box(stack.get_matrix4x4(1 + 1));
  673. return 1;
  674. }
  675. static int matrix4x4box_store(lua_State* L)
  676. {
  677. LuaStack stack(L);
  678. stack.get_matrix4x4box(1) = stack.get_matrix4x4(2);
  679. return 0;
  680. }
  681. static int matrix4x4box_unbox(lua_State* L)
  682. {
  683. LuaStack stack(L);
  684. stack.push_matrix4x4(stack.get_matrix4x4box(1));
  685. return 1;
  686. }
  687. static int matrix4x4box_tostring(lua_State* L)
  688. {
  689. LuaStack stack(L);
  690. Matrix4x4& m = stack.get_matrix4x4box(1);
  691. stack.push_fstring("Matrix4x4Box (%p)", &m);
  692. return 1;
  693. }
  694. static int quaternion_new(lua_State* L)
  695. {
  696. LuaStack stack(L);
  697. stack.push_quaternion(quaternion(stack.get_vector3(1), stack.get_float(2)));
  698. return 1;
  699. }
  700. static int quaternion_ctor(lua_State* L)
  701. {
  702. LuaStack stack(L);
  703. stack.push_quaternion(quaternion(stack.get_vector3(1 + 1), stack.get_float(2 + 1)));
  704. return 1;
  705. }
  706. static int quaternion_from_elements(lua_State* L)
  707. {
  708. LuaStack stack(L);
  709. stack.push_quaternion(quaternion(stack.get_float(1), stack.get_float(2), stack.get_float(3), stack.get_float(4)));
  710. return 1;
  711. }
  712. static int quaternion_negate(lua_State* L)
  713. {
  714. LuaStack stack(L);
  715. stack.push_quaternion(-stack.get_quaternion(1));
  716. return 1;
  717. }
  718. static int quaternion_identity(lua_State* L)
  719. {
  720. LuaStack stack(L);
  721. stack.push_quaternion(QUATERNION_IDENTITY);
  722. return 1;
  723. }
  724. static int quaternion_dot(lua_State* L)
  725. {
  726. LuaStack stack(L);
  727. stack.push_float(dot(stack.get_quaternion(1), stack.get_quaternion(2)));
  728. return 1;
  729. }
  730. static int quaternion_length(lua_State* L)
  731. {
  732. LuaStack stack(L);
  733. stack.push_float(length(stack.get_quaternion(1)));
  734. return 1;
  735. }
  736. static int quaternion_normalize(lua_State* L)
  737. {
  738. LuaStack stack(L);
  739. stack.push_quaternion(normalize(stack.get_quaternion(1)));
  740. return 1;
  741. }
  742. static int quaternion_conjugate(lua_State* L)
  743. {
  744. LuaStack stack(L);
  745. stack.push_quaternion(conjugate(stack.get_quaternion(1)));
  746. return 1;
  747. }
  748. static int quaternion_inverse(lua_State* L)
  749. {
  750. LuaStack stack(L);
  751. stack.push_quaternion(inverse(stack.get_quaternion(1)));
  752. return 1;
  753. }
  754. static int quaternion_multiply(lua_State* L)
  755. {
  756. LuaStack stack(L);
  757. stack.push_quaternion(stack.get_quaternion(1) * stack.get_quaternion(2));
  758. return 1;
  759. }
  760. static int quaternion_multiply_by_scalar(lua_State* L)
  761. {
  762. LuaStack stack(L);
  763. stack.push_quaternion(stack.get_quaternion(1) * stack.get_float(2));
  764. return 1;
  765. }
  766. static int quaternion_power(lua_State* L)
  767. {
  768. LuaStack stack(L);
  769. stack.push_quaternion(power(stack.get_quaternion(1), stack.get_float(2)));
  770. return 1;
  771. }
  772. static int quaternion_elements(lua_State* L)
  773. {
  774. LuaStack stack(L);
  775. const Quaternion& q = stack.get_quaternion(1);
  776. stack.push_float(q.x);
  777. stack.push_float(q.y);
  778. stack.push_float(q.z);
  779. stack.push_float(q.w);
  780. return 4;
  781. }
  782. static int quaternion_look(lua_State* L)
  783. {
  784. LuaStack stack(L);
  785. const Vector3 up = stack.num_args() == 2 ? stack.get_vector3(2) : VECTOR3_YAXIS;
  786. stack.push_quaternion(look(stack.get_vector3(1), up));
  787. return 1;
  788. }
  789. static int quaternion_right(lua_State* L)
  790. {
  791. LuaStack stack(L);
  792. stack.push_vector3(right(stack.get_quaternion(1)));
  793. return 1;
  794. }
  795. static int quaternion_up(lua_State* L)
  796. {
  797. LuaStack stack(L);
  798. stack.push_vector3(up(stack.get_quaternion(1)));
  799. return 1;
  800. }
  801. static int quaternion_forward(lua_State* L)
  802. {
  803. LuaStack stack(L);
  804. stack.push_vector3(forward(stack.get_quaternion(1)));
  805. return 1;
  806. }
  807. static int quaternion_lerp(lua_State* L)
  808. {
  809. LuaStack stack(L);
  810. stack.push_quaternion(lerp(stack.get_quaternion(1), stack.get_quaternion(2), stack.get_float(3)));
  811. return 1;
  812. }
  813. static int quaternion_to_string(lua_State* L)
  814. {
  815. LuaStack stack(L);
  816. const Quaternion q = stack.get_quaternion(1);
  817. char buf[256];
  818. snprintf(buf, sizeof(buf), "%.4f %.4f %.4f %.4f", q.x, q.y, q.z, q.w);
  819. stack.push_string(buf);
  820. return 1;
  821. }
  822. static int quaternionbox_new(lua_State* L)
  823. {
  824. LuaStack stack(L);
  825. if (stack.num_args() == 0)
  826. {
  827. stack.push_quaternionbox(QUATERNION_IDENTITY);
  828. }
  829. else if (stack.num_args() == 1)
  830. {
  831. stack.push_quaternionbox(stack.get_quaternion(1));
  832. }
  833. else
  834. {
  835. Quaternion q = quaternion(stack.get_float(1)
  836. , stack.get_float(2)
  837. , stack.get_float(3)
  838. , stack.get_float(4)
  839. );
  840. stack.push_quaternionbox(q);
  841. }
  842. return 1;
  843. }
  844. static int quaternionbox_ctor(lua_State* L)
  845. {
  846. LuaStack stack(L);
  847. if (stack.num_args() == 0 + 1)
  848. {
  849. stack.push_quaternionbox(QUATERNION_IDENTITY);
  850. }
  851. else if (stack.num_args() == 1 + 1)
  852. {
  853. stack.push_quaternionbox(stack.get_quaternion(1 + 1));
  854. }
  855. else
  856. {
  857. Quaternion q = quaternion(stack.get_float(1 + 1)
  858. , stack.get_float(2 + 1)
  859. , stack.get_float(3 + 1)
  860. , stack.get_float(4 + 1)
  861. );
  862. stack.push_quaternionbox(q);
  863. }
  864. return 1;
  865. }
  866. static int quaternionbox_store(lua_State* L)
  867. {
  868. LuaStack stack(L);
  869. Quaternion& q = stack.get_quaternionbox(1);
  870. if (stack.num_args() == 2)
  871. q = stack.get_quaternion(2);
  872. else
  873. q = quaternion(stack.get_float(2)
  874. , stack.get_float(3)
  875. , stack.get_float(4)
  876. , stack.get_float(5));
  877. return 0;
  878. }
  879. static int quaternionbox_unbox(lua_State* L)
  880. {
  881. LuaStack stack(L);
  882. Quaternion& q = stack.get_quaternionbox(1);
  883. stack.push_quaternion(q);
  884. return 1;
  885. }
  886. static int quaternionbox_tostring(lua_State* L)
  887. {
  888. LuaStack stack(L);
  889. Quaternion& q = stack.get_quaternionbox(1);
  890. stack.push_fstring("QuaternionBox (%p)", &q);
  891. return 1;
  892. }
  893. static int color4_new(lua_State* L)
  894. {
  895. LuaStack stack(L);
  896. Color4 c;
  897. c.x = stack.get_float(1);
  898. c.y = stack.get_float(2);
  899. c.z = stack.get_float(3);
  900. c.w = stack.get_float(4);
  901. stack.push_color4(c);
  902. return 1;
  903. }
  904. static int color4_ctor(lua_State* L)
  905. {
  906. LuaStack stack(L);
  907. Color4 c;
  908. c.x = stack.get_float(1 + 1);
  909. c.y = stack.get_float(2 + 1);
  910. c.z = stack.get_float(3 + 1);
  911. c.w = stack.get_float(4 + 1);
  912. stack.push_color4(c);
  913. return 1;
  914. }
  915. static int color4_black(lua_State* L)
  916. {
  917. LuaStack stack(L);
  918. stack.push_color4(COLOR4_BLACK);
  919. return 1;
  920. }
  921. static int color4_white(lua_State* L)
  922. {
  923. LuaStack stack(L);
  924. stack.push_color4(COLOR4_WHITE);
  925. return 1;
  926. }
  927. static int color4_red(lua_State* L)
  928. {
  929. LuaStack stack(L);
  930. stack.push_color4(COLOR4_RED);
  931. return 1;
  932. }
  933. static int color4_green(lua_State* L)
  934. {
  935. LuaStack stack(L);
  936. stack.push_color4(COLOR4_GREEN);
  937. return 1;
  938. }
  939. static int color4_blue(lua_State* L)
  940. {
  941. LuaStack stack(L);
  942. stack.push_color4(COLOR4_BLUE);
  943. return 1;
  944. }
  945. static int color4_yellow(lua_State* L)
  946. {
  947. LuaStack stack(L);
  948. stack.push_color4(COLOR4_YELLOW);
  949. return 1;
  950. }
  951. static int color4_orange(lua_State* L)
  952. {
  953. LuaStack stack(L);
  954. stack.push_color4(COLOR4_ORANGE);
  955. return 1;
  956. }
  957. static int lightuserdata_add(lua_State* L)
  958. {
  959. LuaStack stack(L);
  960. const Vector3& a = stack.get_vector3(1);
  961. const Vector3& b = stack.get_vector3(2);
  962. stack.push_vector3(a + b);
  963. return 1;
  964. }
  965. static int lightuserdata_sub(lua_State* L)
  966. {
  967. LuaStack stack(L);
  968. const Vector3& a = stack.get_vector3(1);
  969. const Vector3& b = stack.get_vector3(2);
  970. stack.push_vector3(a - b);
  971. return 1;
  972. }
  973. static int lightuserdata_mul(lua_State* L)
  974. {
  975. LuaStack stack(L);
  976. const int i = stack.is_number(1) ? 1 : 2;
  977. stack.push_vector3(stack.get_float(i) * stack.get_vector3(3-i));
  978. return 1;
  979. }
  980. static int lightuserdata_unm(lua_State* L)
  981. {
  982. LuaStack stack(L);
  983. stack.push_vector3(-stack.get_vector3(1));
  984. return 1;
  985. }
  986. static int lightuserdata_index(lua_State* L)
  987. {
  988. LuaStack stack(L);
  989. Vector3& v = stack.get_vector3(1);
  990. const char* s = stack.get_string(2);
  991. switch (s[0])
  992. {
  993. case 'x': stack.push_float(v.x); return 1;
  994. case 'y': stack.push_float(v.y); return 1;
  995. case 'z': stack.push_float(v.z); return 1;
  996. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  997. }
  998. return 0;
  999. }
  1000. static int lightuserdata_newindex(lua_State* L)
  1001. {
  1002. LuaStack stack(L);
  1003. Vector3& v = stack.get_vector3(1);
  1004. const char* s = stack.get_string(2);
  1005. const f32 value = stack.get_float(3);
  1006. switch (s[0])
  1007. {
  1008. case 'x': v.x = value; break;
  1009. case 'y': v.y = value; break;
  1010. case 'z': v.z = value; break;
  1011. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  1012. }
  1013. return 0;
  1014. }
  1015. static int input_device_name(lua_State* L, InputDevice& id)
  1016. {
  1017. LuaStack stack(L);
  1018. stack.push_string(id.name());
  1019. return 1;
  1020. }
  1021. static int input_device_connected(lua_State* L, InputDevice& id)
  1022. {
  1023. LuaStack stack(L);
  1024. stack.push_bool(id.connected());
  1025. return 1;
  1026. }
  1027. static int input_device_num_buttons(lua_State* L, InputDevice& id)
  1028. {
  1029. LuaStack stack(L);
  1030. stack.push_int(id.num_buttons());
  1031. return 1;
  1032. }
  1033. static int input_device_num_axes(lua_State* L, InputDevice& id)
  1034. {
  1035. LuaStack stack(L);
  1036. stack.push_int(id.num_axes());
  1037. return 1;
  1038. }
  1039. static int input_device_pressed(lua_State* L, InputDevice& id)
  1040. {
  1041. LuaStack stack(L);
  1042. stack.push_bool(id.pressed(stack.get_int(1)));
  1043. return 1;
  1044. }
  1045. static int input_device_released(lua_State* L, InputDevice& id)
  1046. {
  1047. LuaStack stack(L);
  1048. stack.push_bool(id.released(stack.get_int(1)));
  1049. return 1;
  1050. }
  1051. static int input_device_any_pressed(lua_State* L, InputDevice& id)
  1052. {
  1053. LuaStack stack(L);
  1054. stack.push_bool(id.any_pressed());
  1055. return 1;
  1056. }
  1057. static int input_device_any_released(lua_State* L, InputDevice& id)
  1058. {
  1059. LuaStack stack(L);
  1060. stack.push_bool(id.any_released());
  1061. return 1;
  1062. }
  1063. static int input_device_axis(lua_State* L, InputDevice& id)
  1064. {
  1065. LuaStack stack(L);
  1066. stack.push_vector3(id.axis(stack.get_int(1)));
  1067. return 1;
  1068. }
  1069. static int input_device_button_name(lua_State* L, InputDevice& id)
  1070. {
  1071. LuaStack stack(L);
  1072. stack.push_string(id.button_name(stack.get_int(1)));
  1073. return 1;
  1074. }
  1075. static int input_device_axis_name(lua_State* L, InputDevice& id)
  1076. {
  1077. LuaStack stack(L);
  1078. stack.push_string(id.axis_name(stack.get_int(1)));
  1079. return 1;
  1080. }
  1081. static int input_device_button_id(lua_State* L, InputDevice& id)
  1082. {
  1083. LuaStack stack(L);
  1084. stack.push_int(id.button_id(stack.get_string_id_32(1)));
  1085. return 1;
  1086. }
  1087. static int input_device_axis_id(lua_State* L, InputDevice& id)
  1088. {
  1089. LuaStack stack(L);
  1090. stack.push_int(id.axis_id(stack.get_string_id_32(1)));
  1091. return 1;
  1092. }
  1093. #define KEYBOARD_FN(name) keyboard_##name
  1094. #define MOUSE_FN(name) mouse_##name
  1095. #define TOUCH_FN(name) touch_##name
  1096. #define JOYPAD_FN(index, name) joypad_##name##index
  1097. #define KEYBOARD(name) static int KEYBOARD_FN(name)(lua_State* L)\
  1098. { return input_device_##name(L, *device()->input_manager()->keyboard()); }
  1099. #define MOUSE(name) static int MOUSE_FN(name)(lua_State* L)\
  1100. { return input_device_##name(L, *device()->input_manager()->mouse()); }
  1101. #define TOUCH(name) static int TOUCH_FN(name)(lua_State* L)\
  1102. { return input_device_##name(L, *device()->input_manager()->touch()); }
  1103. #define JOYPAD(index, name) static int JOYPAD_FN(index, name)(lua_State* L)\
  1104. { return input_device_##name(L, *device()->input_manager()->joypad(index)); }
  1105. KEYBOARD(name)
  1106. KEYBOARD(connected)
  1107. KEYBOARD(num_buttons)
  1108. KEYBOARD(num_axes)
  1109. KEYBOARD(pressed)
  1110. KEYBOARD(released)
  1111. KEYBOARD(any_pressed)
  1112. KEYBOARD(any_released)
  1113. // KEYBOARD(axis)
  1114. KEYBOARD(button_name)
  1115. // KEYBOARD(axis_name)
  1116. KEYBOARD(button_id)
  1117. // KEYBOARD(axis_id)
  1118. MOUSE(name)
  1119. MOUSE(connected)
  1120. MOUSE(num_buttons)
  1121. MOUSE(num_axes)
  1122. MOUSE(pressed)
  1123. MOUSE(released)
  1124. MOUSE(any_pressed)
  1125. MOUSE(any_released)
  1126. MOUSE(axis)
  1127. MOUSE(button_name)
  1128. MOUSE(axis_name)
  1129. MOUSE(button_id)
  1130. MOUSE(axis_id)
  1131. TOUCH(name)
  1132. TOUCH(connected)
  1133. TOUCH(num_buttons)
  1134. TOUCH(num_axes)
  1135. TOUCH(pressed)
  1136. TOUCH(released)
  1137. TOUCH(any_pressed)
  1138. TOUCH(any_released)
  1139. TOUCH(axis)
  1140. TOUCH(button_name)
  1141. TOUCH(axis_name)
  1142. TOUCH(button_id)
  1143. TOUCH(axis_id)
  1144. JOYPAD(0, name)
  1145. JOYPAD(0, connected)
  1146. JOYPAD(0, num_buttons)
  1147. JOYPAD(0, num_axes)
  1148. JOYPAD(0, pressed)
  1149. JOYPAD(0, released)
  1150. JOYPAD(0, any_pressed)
  1151. JOYPAD(0, any_released)
  1152. JOYPAD(0, axis)
  1153. JOYPAD(0, button_name)
  1154. JOYPAD(0, axis_name)
  1155. JOYPAD(0, button_id)
  1156. JOYPAD(0, axis_id)
  1157. JOYPAD(1, name)
  1158. JOYPAD(1, connected)
  1159. JOYPAD(1, num_buttons)
  1160. JOYPAD(1, num_axes)
  1161. JOYPAD(1, pressed)
  1162. JOYPAD(1, released)
  1163. JOYPAD(1, any_pressed)
  1164. JOYPAD(1, any_released)
  1165. JOYPAD(1, axis)
  1166. JOYPAD(1, button_name)
  1167. JOYPAD(1, axis_name)
  1168. JOYPAD(1, button_id)
  1169. JOYPAD(1, axis_id)
  1170. JOYPAD(2, name)
  1171. JOYPAD(2, connected)
  1172. JOYPAD(2, num_buttons)
  1173. JOYPAD(2, num_axes)
  1174. JOYPAD(2, pressed)
  1175. JOYPAD(2, released)
  1176. JOYPAD(2, any_pressed)
  1177. JOYPAD(2, any_released)
  1178. JOYPAD(2, axis)
  1179. JOYPAD(2, button_name)
  1180. JOYPAD(2, axis_name)
  1181. JOYPAD(2, button_id)
  1182. JOYPAD(2, axis_id)
  1183. JOYPAD(3, name)
  1184. JOYPAD(3, connected)
  1185. JOYPAD(3, num_buttons)
  1186. JOYPAD(3, num_axes)
  1187. JOYPAD(3, pressed)
  1188. JOYPAD(3, released)
  1189. JOYPAD(3, any_pressed)
  1190. JOYPAD(3, any_released)
  1191. JOYPAD(3, axis)
  1192. JOYPAD(3, button_name)
  1193. JOYPAD(3, axis_name)
  1194. JOYPAD(3, button_id)
  1195. JOYPAD(3, axis_id)
  1196. static int world_spawn_unit(lua_State* L)
  1197. {
  1198. LuaStack stack(L);
  1199. const int nargs = stack.num_args();
  1200. const StringId64 name = stack.get_resource_id(2);
  1201. const Vector3& pos = nargs > 2 ? stack.get_vector3(3) : VECTOR3_ZERO;
  1202. const Quaternion& rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
  1203. LUA_ASSERT(device()->resource_manager()->can_get(RESOURCE_TYPE_UNIT, name), stack, "Unit not found");
  1204. stack.push_unit(stack.get_world(1)->spawn_unit(name, pos, rot));
  1205. return 1;
  1206. }
  1207. static int world_spawn_empty_unit(lua_State* L)
  1208. {
  1209. LuaStack stack(L);
  1210. stack.push_unit(stack.get_world(1)->spawn_empty_unit());
  1211. return 1;
  1212. }
  1213. static int world_destroy_unit(lua_State* L)
  1214. {
  1215. LuaStack stack(L);
  1216. stack.get_world(1)->destroy_unit(stack.get_unit(2));
  1217. return 0;
  1218. }
  1219. static int world_num_units(lua_State* L)
  1220. {
  1221. LuaStack stack(L);
  1222. stack.push_int(stack.get_world(1)->num_units());
  1223. return 1;
  1224. }
  1225. static int world_units(lua_State* L)
  1226. {
  1227. LuaStack stack(L);
  1228. TempAllocator1024 alloc;
  1229. Array<UnitId> units(alloc);
  1230. stack.get_world(1)->units(units);
  1231. const u32 num = array::size(units);
  1232. stack.push_table(num);
  1233. for (u32 i = 0; i < num; ++i)
  1234. {
  1235. stack.push_key_begin((s32) i + 1);
  1236. stack.push_unit(units[i]);
  1237. stack.push_key_end();
  1238. }
  1239. return 1;
  1240. }
  1241. static int world_camera(lua_State* L)
  1242. {
  1243. LuaStack stack(L);
  1244. stack.push_camera(stack.get_world(1)->camera(stack.get_unit(2)));
  1245. return 1;
  1246. }
  1247. static int camera_set_projection_type(lua_State* L)
  1248. {
  1249. LuaStack stack(L);
  1250. const char* name = stack.get_string(3);
  1251. const ProjectionType::Enum pt = name_to_projection_type(name);
  1252. LUA_ASSERT(pt != ProjectionType::COUNT, stack, "Unknown projection type: '%s'", name);
  1253. stack.get_world(1)->set_camera_projection_type(stack.get_camera(2), pt);
  1254. return 0;
  1255. }
  1256. static int camera_projection_type(lua_State* L)
  1257. {
  1258. LuaStack stack(L);
  1259. ProjectionType::Enum type = stack.get_world(1)->camera_projection_type(stack.get_camera(2));
  1260. stack.push_string(s_projection[type].name);
  1261. return 1;
  1262. }
  1263. static int camera_fov(lua_State* L)
  1264. {
  1265. LuaStack stack(L);
  1266. stack.push_float(stack.get_world(1)->camera_fov(stack.get_camera(2)));
  1267. return 1;
  1268. }
  1269. static int camera_set_fov(lua_State* L)
  1270. {
  1271. LuaStack stack(L);
  1272. stack.get_world(1)->set_camera_fov(stack.get_camera(2), stack.get_float(3));
  1273. return 0;
  1274. }
  1275. static int camera_aspect(lua_State* L)
  1276. {
  1277. LuaStack stack(L);
  1278. stack.push_float(stack.get_world(1)->camera_aspect(stack.get_camera(2)));
  1279. return 1;
  1280. }
  1281. static int camera_set_aspect(lua_State* L)
  1282. {
  1283. LuaStack stack(L);
  1284. stack.get_world(1)->set_camera_aspect(stack.get_camera(2), stack.get_float(3));
  1285. return 0;
  1286. }
  1287. static int camera_near_clip_distance(lua_State* L)
  1288. {
  1289. LuaStack stack(L);
  1290. stack.push_float(stack.get_world(1)->camera_near_clip_distance(stack.get_camera(2)));
  1291. return 1;
  1292. }
  1293. static int camera_set_near_clip_distance(lua_State* L)
  1294. {
  1295. LuaStack stack(L);
  1296. stack.get_world(1)->set_camera_near_clip_distance(stack.get_camera(2), stack.get_float(3));
  1297. return 0;
  1298. }
  1299. static int camera_far_clip_distance(lua_State* L)
  1300. {
  1301. LuaStack stack(L);
  1302. stack.push_float(stack.get_world(1)->camera_far_clip_distance(stack.get_camera(2)));
  1303. return 1;
  1304. }
  1305. static int camera_set_far_clip_distance(lua_State* L)
  1306. {
  1307. LuaStack stack(L);
  1308. stack.get_world(1)->set_camera_far_clip_distance(stack.get_camera(2), stack.get_float(3));
  1309. return 0;
  1310. }
  1311. static int camera_set_orthographic_metrics(lua_State* L)
  1312. {
  1313. LuaStack stack(L);
  1314. stack.get_world(1)->set_camera_orthographic_metrics(stack.get_camera(2), stack.get_float(3), stack.get_float(4),
  1315. stack.get_float(5), stack.get_float(6));
  1316. return 0;
  1317. }
  1318. static int camera_screen_to_world(lua_State* L)
  1319. {
  1320. LuaStack stack(L);
  1321. stack.push_vector3(stack.get_world(1)->camera_screen_to_world(stack.get_camera(2), stack.get_vector3(3)));
  1322. return 1;
  1323. }
  1324. static int camera_world_to_screen(lua_State* L)
  1325. {
  1326. LuaStack stack(L);
  1327. stack.push_vector3(stack.get_world(1)->camera_world_to_screen(stack.get_camera(2), stack.get_vector3(3)));
  1328. return 1;
  1329. }
  1330. static int world_update_animations(lua_State* L)
  1331. {
  1332. LuaStack stack(L);
  1333. stack.get_world(1)->update_animations(stack.get_float(2));
  1334. return 0;
  1335. }
  1336. static int world_update_scene(lua_State* L)
  1337. {
  1338. LuaStack stack(L);
  1339. stack.get_world(1)->update_scene(stack.get_float(2));
  1340. return 0;
  1341. }
  1342. static int world_update(lua_State* L)
  1343. {
  1344. LuaStack stack(L);
  1345. stack.get_world(1)->update(stack.get_float(2));
  1346. return 0;
  1347. }
  1348. static int world_play_sound(lua_State* L)
  1349. {
  1350. LuaStack stack(L);
  1351. const s32 nargs = stack.num_args();
  1352. World* world = stack.get_world(1);
  1353. const StringId64 name = stack.get_resource_id(2);
  1354. const bool loop = nargs > 2 ? stack.get_bool(3) : false;
  1355. const f32 volume = nargs > 3 ? stack.get_float(4) : 1.0f;
  1356. const Vector3& pos = nargs > 4 ? stack.get_vector3(5) : VECTOR3_ZERO;
  1357. const f32 range = nargs > 5 ? stack.get_float(6) : 1000.0f;
  1358. LUA_ASSERT(device()->resource_manager()->can_get(RESOURCE_TYPE_SOUND, name), stack, "Sound not found");
  1359. stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
  1360. return 1;
  1361. }
  1362. static int world_stop_sound(lua_State* L)
  1363. {
  1364. LuaStack stack(L);
  1365. stack.get_world(1)->stop_sound(stack.get_sound_instance_id(2));
  1366. return 0;
  1367. }
  1368. static int world_link_sound(lua_State* L)
  1369. {
  1370. LuaStack stack(L);
  1371. stack.get_world(1)->link_sound(stack.get_sound_instance_id(2)
  1372. , stack.get_unit(3)
  1373. , stack.get_int(4)
  1374. );
  1375. return 0;
  1376. }
  1377. static int world_set_listener_pose(lua_State* L)
  1378. {
  1379. LuaStack stack(L);
  1380. stack.get_world(1)->set_listener_pose(stack.get_matrix4x4(2));
  1381. return 0;
  1382. }
  1383. static int world_set_sound_position(lua_State* L)
  1384. {
  1385. LuaStack stack(L);
  1386. stack.get_world(1)->set_sound_position(stack.get_sound_instance_id(2), stack.get_vector3(3));
  1387. return 0;
  1388. }
  1389. static int world_set_sound_range(lua_State* L)
  1390. {
  1391. LuaStack stack(L);
  1392. stack.get_world(1)->set_sound_range(stack.get_sound_instance_id(2), stack.get_float(3));
  1393. return 0;
  1394. }
  1395. static int world_set_sound_volume(lua_State* L)
  1396. {
  1397. LuaStack stack(L);
  1398. stack.get_world(1)->set_sound_volume(stack.get_sound_instance_id(2), stack.get_float(3));
  1399. return 0;
  1400. }
  1401. static int world_create_debug_line(lua_State* L)
  1402. {
  1403. LuaStack stack(L);
  1404. stack.push_debug_line(stack.get_world(1)->create_debug_line(stack.get_bool(2)));
  1405. return 1;
  1406. }
  1407. static int world_destroy_debug_line(lua_State* L)
  1408. {
  1409. LuaStack stack(L);
  1410. stack.get_world(1)->destroy_debug_line(*stack.get_debug_line(2));
  1411. return 0;
  1412. }
  1413. static int world_create_screen_gui(lua_State* L)
  1414. {
  1415. LuaStack stack(L);
  1416. stack.push_gui(stack.get_world(1)->create_screen_gui(stack.get_float(2), stack.get_float(3)));
  1417. return 1;
  1418. }
  1419. static int world_destroy_gui(lua_State* L)
  1420. {
  1421. LuaStack stack(L);
  1422. stack.get_world(1)->destroy_gui(*stack.get_gui(2));
  1423. return 0;
  1424. }
  1425. static int world_load_level(lua_State* L)
  1426. {
  1427. LuaStack stack(L);
  1428. const int nargs = stack.num_args();
  1429. const StringId64 name = stack.get_resource_id(2);
  1430. const Vector3& pos = nargs > 2 ? stack.get_vector3(3) : VECTOR3_ZERO;
  1431. const Quaternion& rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
  1432. LUA_ASSERT(device()->resource_manager()->can_get(RESOURCE_TYPE_LEVEL, name), stack, "Level not found");
  1433. stack.push_level(stack.get_world(1)->load_level(name, pos, rot));
  1434. return 1;
  1435. }
  1436. static int world_scene_graph(lua_State* L)
  1437. {
  1438. LuaStack stack(L);
  1439. stack.push_scene_graph(stack.get_world(1)->scene_graph());
  1440. return 1;
  1441. }
  1442. static int world_render_world(lua_State* L)
  1443. {
  1444. LuaStack stack(L);
  1445. stack.push_render_world(stack.get_world(1)->render_world());
  1446. return 1;
  1447. }
  1448. static int world_physics_world(lua_State* L)
  1449. {
  1450. LuaStack stack(L);
  1451. stack.push_physics_world(stack.get_world(1)->physics_world());
  1452. return 1;
  1453. }
  1454. static int world_sound_world(lua_State* L)
  1455. {
  1456. LuaStack stack(L);
  1457. stack.push_sound_world(stack.get_world(1)->sound_world());
  1458. return 1;
  1459. }
  1460. static int world_tostring(lua_State* L)
  1461. {
  1462. LuaStack stack(L);
  1463. World* w = stack.get_world(1);
  1464. stack.push_fstring("World (%p)", w);
  1465. return 1;
  1466. }
  1467. static int scene_graph_create(lua_State* L)
  1468. {
  1469. LuaStack stack(L);
  1470. TransformInstance ti = stack.get_scene_graph(1)->create(stack.get_unit(2)
  1471. , stack.get_vector3(3)
  1472. , stack.get_quaternion(4)
  1473. , stack.get_vector3(5)
  1474. );
  1475. stack.push_transform(ti);
  1476. return 1;
  1477. }
  1478. static int scene_graph_destroy(lua_State* L)
  1479. {
  1480. LuaStack stack(L);
  1481. stack.get_scene_graph(1)->destroy(stack.get_transform(2));
  1482. return 0;
  1483. }
  1484. static int scene_graph_transform_instances(lua_State* L)
  1485. {
  1486. LuaStack stack(L);
  1487. SceneGraph* sg = stack.get_scene_graph(1);
  1488. TransformInstance ti = sg->get(stack.get_unit(2));
  1489. if (sg->is_valid(ti))
  1490. stack.push_transform(ti);
  1491. else
  1492. stack.push_nil();
  1493. return 1;
  1494. }
  1495. static int scene_graph_local_position(lua_State* L)
  1496. {
  1497. LuaStack stack(L);
  1498. stack.push_vector3(stack.get_scene_graph(1)->local_position(stack.get_transform(2)));
  1499. return 1;
  1500. }
  1501. static int scene_graph_local_rotation(lua_State* L)
  1502. {
  1503. LuaStack stack(L);
  1504. stack.push_quaternion(stack.get_scene_graph(1)->local_rotation(stack.get_transform(2)));
  1505. return 1;
  1506. }
  1507. static int scene_graph_local_scale(lua_State* L)
  1508. {
  1509. LuaStack stack(L);
  1510. stack.push_vector3(stack.get_scene_graph(1)->local_scale(stack.get_transform(2)));
  1511. return 1;
  1512. }
  1513. static int scene_graph_local_pose(lua_State* L)
  1514. {
  1515. LuaStack stack(L);
  1516. stack.push_matrix4x4(stack.get_scene_graph(1)->local_pose(stack.get_transform(2)));
  1517. return 1;
  1518. }
  1519. static int scene_graph_world_position(lua_State* L)
  1520. {
  1521. LuaStack stack(L);
  1522. stack.push_vector3(stack.get_scene_graph(1)->world_position(stack.get_transform(2)));
  1523. return 1;
  1524. }
  1525. static int scene_graph_world_rotation(lua_State* L)
  1526. {
  1527. LuaStack stack(L);
  1528. stack.push_quaternion(stack.get_scene_graph(1)->world_rotation(stack.get_transform(2)));
  1529. return 1;
  1530. }
  1531. static int scene_graph_world_pose(lua_State* L)
  1532. {
  1533. LuaStack stack(L);
  1534. stack.push_matrix4x4(stack.get_scene_graph(1)->world_pose(stack.get_transform(2)));
  1535. return 1;
  1536. }
  1537. static int scene_graph_set_local_position(lua_State* L)
  1538. {
  1539. LuaStack stack(L);
  1540. stack.get_scene_graph(1)->set_local_position(stack.get_transform(2), stack.get_vector3(3));
  1541. return 0;
  1542. }
  1543. static int scene_graph_set_local_rotation(lua_State* L)
  1544. {
  1545. LuaStack stack(L);
  1546. stack.get_scene_graph(1)->set_local_rotation(stack.get_transform(2), stack.get_quaternion(3));
  1547. return 0;
  1548. }
  1549. static int scene_graph_set_local_scale(lua_State* L)
  1550. {
  1551. LuaStack stack(L);
  1552. stack.get_scene_graph(1)->set_local_scale(stack.get_transform(2), stack.get_vector3(3));
  1553. return 0;
  1554. }
  1555. static int scene_graph_set_local_pose(lua_State* L)
  1556. {
  1557. LuaStack stack(L);
  1558. stack.get_scene_graph(1)->set_local_pose(stack.get_transform(2), stack.get_matrix4x4(3));
  1559. return 0;
  1560. }
  1561. static int scene_graph_link(lua_State* L)
  1562. {
  1563. LuaStack stack(L);
  1564. stack.get_scene_graph(1)->link(stack.get_transform(2), stack.get_transform(3));
  1565. return 0;
  1566. }
  1567. static int scene_graph_unlink(lua_State* L)
  1568. {
  1569. LuaStack stack(L);
  1570. stack.get_scene_graph(1)->unlink(stack.get_transform(2));
  1571. return 0;
  1572. }
  1573. static int unit_manager_create(lua_State* L)
  1574. {
  1575. LuaStack stack(L);
  1576. if (stack.num_args() == 1)
  1577. stack.push_unit(device()->unit_manager()->create(*stack.get_world(1)));
  1578. else
  1579. stack.push_unit(device()->unit_manager()->create());
  1580. return 1;
  1581. }
  1582. static int unit_manager_alive(lua_State* L)
  1583. {
  1584. LuaStack stack(L);
  1585. stack.push_bool(device()->unit_manager()->alive(stack.get_unit(1)));
  1586. return 1;
  1587. }
  1588. static int render_world_create_mesh(lua_State* L)
  1589. {
  1590. LuaStack stack(L);
  1591. RenderWorld* rw = stack.get_render_world(1);
  1592. UnitId unit = stack.get_unit(2);
  1593. MeshRendererDesc desc;
  1594. desc.mesh_resource = stack.get_resource_id(3);
  1595. desc.geometry_name = stack.get_string_id_32(4);
  1596. desc.material_resource = stack.get_resource_id(5);
  1597. desc.visible = stack.get_bool(6);
  1598. Matrix4x4 pose = stack.get_matrix4x4(7);
  1599. stack.push_mesh_instance(rw->create_mesh(unit, desc, pose));
  1600. return 1;
  1601. }
  1602. static int render_world_destroy_mesh(lua_State* L)
  1603. {
  1604. LuaStack stack(L);
  1605. stack.get_render_world(1)->destroy_mesh(stack.get_mesh_instance(2));
  1606. return 0;
  1607. }
  1608. static int render_world_mesh_instances(lua_State* L)
  1609. {
  1610. LuaStack stack(L);
  1611. RenderWorld* rw = stack.get_render_world(1);
  1612. UnitId unit = stack.get_unit(2);
  1613. TempAllocator512 ta;
  1614. Array<MeshInstance> inst(ta);
  1615. rw->mesh_instances(unit, inst);
  1616. stack.push_table(array::size(inst));
  1617. for (u32 i = 0; i < array::size(inst); ++i)
  1618. {
  1619. stack.push_key_begin(i+1);
  1620. stack.push_mesh_instance(inst[i]);
  1621. stack.push_key_end();
  1622. }
  1623. return 1;
  1624. }
  1625. static int render_world_mesh_obb(lua_State* L)
  1626. {
  1627. LuaStack stack(L);
  1628. OBB obb = stack.get_render_world(1)->mesh_obb(stack.get_mesh_instance(2));
  1629. stack.push_matrix4x4(obb.tm);
  1630. stack.push_vector3(obb.half_extents);
  1631. return 2;
  1632. }
  1633. static int render_world_set_mesh_visible(lua_State* L)
  1634. {
  1635. LuaStack stack(L);
  1636. stack.get_render_world(1)->set_mesh_visible(stack.get_mesh_instance(2), stack.get_bool(3));
  1637. return 0;
  1638. }
  1639. static int render_world_create_sprite(lua_State* L)
  1640. {
  1641. LuaStack stack(L);
  1642. RenderWorld* rw = stack.get_render_world(1);
  1643. UnitId unit = stack.get_unit(2);
  1644. SpriteRendererDesc desc;
  1645. desc.sprite_resource = stack.get_resource_id(3);
  1646. desc.material_resource = stack.get_resource_id(4);
  1647. desc.visible = stack.get_bool(5);
  1648. Matrix4x4 pose = stack.get_matrix4x4(6);
  1649. stack.push_sprite_instance(rw->create_sprite(unit, desc, pose));
  1650. return 1;
  1651. }
  1652. static int render_world_destroy_sprite(lua_State* L)
  1653. {
  1654. LuaStack stack(L);
  1655. stack.get_render_world(1)->destroy_sprite(stack.get_sprite_instance(2));
  1656. return 0;
  1657. }
  1658. static int render_world_sprite_instances(lua_State* L)
  1659. {
  1660. LuaStack stack(L);
  1661. RenderWorld* rw = stack.get_render_world(1);
  1662. UnitId unit = stack.get_unit(2);
  1663. TempAllocator512 ta;
  1664. Array<SpriteInstance> inst(ta);
  1665. rw->sprite_instances(unit, inst);
  1666. stack.push_table(array::size(inst));
  1667. for (u32 i = 0; i < array::size(inst); ++i)
  1668. {
  1669. stack.push_key_begin(i+1);
  1670. stack.push_sprite_instance(inst[i]);
  1671. stack.push_key_end();
  1672. }
  1673. return 1;
  1674. }
  1675. static int render_world_set_sprite_visible(lua_State* L)
  1676. {
  1677. LuaStack stack(L);
  1678. stack.get_render_world(1)->set_sprite_visible(stack.get_sprite_instance(2), stack.get_bool(3));
  1679. return 0;
  1680. }
  1681. static int render_world_set_sprite_frame(lua_State* L)
  1682. {
  1683. LuaStack stack(L);
  1684. stack.get_render_world(1)->set_sprite_frame(stack.get_sprite_instance(2), stack.get_int(3));
  1685. return 0;
  1686. }
  1687. static int render_world_create_light(lua_State* L)
  1688. {
  1689. LuaStack stack(L);
  1690. const char* name = stack.get_string(3);
  1691. const LightType::Enum lt = name_to_light_type(name);
  1692. LUA_ASSERT(lt != LightType::COUNT, stack, "Unknown light type: '%s'", name);
  1693. LightDesc ld;
  1694. ld.type = lt;
  1695. ld.range = stack.get_float(4);
  1696. ld.intensity = stack.get_float(5);
  1697. ld.spot_angle = stack.get_float(6);
  1698. ld.color = stack.get_vector3(7);
  1699. Matrix4x4 pose = stack.get_matrix4x4(8);
  1700. stack.push_light_instance(stack.get_render_world(1)->create_light(stack.get_unit(2), ld, pose));
  1701. return 1;
  1702. }
  1703. static int render_world_destroy_light(lua_State* L)
  1704. {
  1705. LuaStack stack(L);
  1706. stack.get_render_world(1)->destroy_light(stack.get_light_instance(2));
  1707. return 0;
  1708. }
  1709. static int render_world_light_instances(lua_State* L)
  1710. {
  1711. LuaStack stack(L);
  1712. stack.push_light_instance(stack.get_render_world(1)->light(stack.get_unit(2)));
  1713. return 1;
  1714. }
  1715. static int render_world_light_type(lua_State* L)
  1716. {
  1717. LuaStack stack(L);
  1718. LightType::Enum type = stack.get_render_world(1)->light_type(stack.get_light_instance(2));
  1719. stack.push_string(s_light[type].name);
  1720. return 1;
  1721. }
  1722. static int render_world_light_color(lua_State* L)
  1723. {
  1724. LuaStack stack(L);
  1725. stack.push_color4(stack.get_render_world(1)->light_color(stack.get_light_instance(2)));
  1726. return 1;
  1727. }
  1728. static int render_world_light_range(lua_State* L)
  1729. {
  1730. LuaStack stack(L);
  1731. stack.push_float(stack.get_render_world(1)->light_range(stack.get_light_instance(2)));
  1732. return 1;
  1733. }
  1734. static int render_world_light_intensity(lua_State* L)
  1735. {
  1736. LuaStack stack(L);
  1737. stack.push_float(stack.get_render_world(1)->light_intensity(stack.get_light_instance(2)));
  1738. return 1;
  1739. }
  1740. static int render_world_light_spot_angle(lua_State* L)
  1741. {
  1742. LuaStack stack(L);
  1743. stack.push_float(stack.get_render_world(1)->light_spot_angle(stack.get_light_instance(2)));
  1744. return 1;
  1745. }
  1746. static int render_world_set_light_type(lua_State* L)
  1747. {
  1748. LuaStack stack(L);
  1749. const char* name = stack.get_string(3);
  1750. const LightType::Enum lt = name_to_light_type(name);
  1751. LUA_ASSERT(lt != LightType::COUNT, stack, "Unknown light type: '%s'", name);
  1752. stack.get_render_world(1)->set_light_type(stack.get_light_instance(2), lt);
  1753. return 0;
  1754. }
  1755. static int render_world_set_light_color(lua_State* L)
  1756. {
  1757. LuaStack stack(L);
  1758. stack.get_render_world(1)->set_light_color(stack.get_light_instance(2), stack.get_color4(3));
  1759. return 0;
  1760. }
  1761. static int render_world_set_light_range(lua_State* L)
  1762. {
  1763. LuaStack stack(L);
  1764. stack.get_render_world(1)->set_light_range(stack.get_light_instance(2), stack.get_float(3));
  1765. return 0;
  1766. }
  1767. static int render_world_set_light_intensity(lua_State* L)
  1768. {
  1769. LuaStack stack(L);
  1770. stack.get_render_world(1)->set_light_intensity(stack.get_light_instance(2), stack.get_float(3));
  1771. return 0;
  1772. }
  1773. static int render_world_set_light_spot_angle(lua_State* L)
  1774. {
  1775. LuaStack stack(L);
  1776. stack.get_render_world(1)->set_light_spot_angle(stack.get_light_instance(2), stack.get_float(3));
  1777. return 0;
  1778. }
  1779. static int render_world_enable_debug_drawing(lua_State* L)
  1780. {
  1781. LuaStack stack(L);
  1782. stack.get_render_world(1)->enable_debug_drawing(stack.get_bool(2));
  1783. return 0;
  1784. }
  1785. static int physics_world_actor_instances(lua_State* L)
  1786. {
  1787. LuaStack stack(L);
  1788. ActorInstance inst = stack.get_physics_world(1)->actor(stack.get_unit(2));
  1789. if (inst.i == UINT32_MAX)
  1790. stack.push_nil();
  1791. else
  1792. stack.push_actor(inst);
  1793. return 1;
  1794. }
  1795. static int physics_world_actor_world_position(lua_State* L)
  1796. {
  1797. LuaStack stack(L);
  1798. stack.push_vector3(stack.get_physics_world(1)->actor_world_position(stack.get_actor(2)));
  1799. return 1;
  1800. }
  1801. static int physics_world_actor_world_rotation(lua_State* L)
  1802. {
  1803. LuaStack stack(L);
  1804. stack.push_quaternion(stack.get_physics_world(1)->actor_world_rotation(stack.get_actor(2)));
  1805. return 1;
  1806. }
  1807. static int physics_world_actor_world_pose(lua_State* L)
  1808. {
  1809. LuaStack stack(L);
  1810. stack.push_matrix4x4(stack.get_physics_world(1)->actor_world_pose(stack.get_actor(2)));
  1811. return 1;
  1812. }
  1813. static int physics_world_teleport_actor_world_position(lua_State* L)
  1814. {
  1815. LuaStack stack(L);
  1816. stack.get_physics_world(1)->teleport_actor_world_position(stack.get_actor(2), stack.get_vector3(3));
  1817. return 0;
  1818. }
  1819. static int physics_world_teleport_actor_world_rotation(lua_State* L)
  1820. {
  1821. LuaStack stack(L);
  1822. stack.get_physics_world(1)->teleport_actor_world_rotation(stack.get_actor(2), stack.get_quaternion(3));
  1823. return 0;
  1824. }
  1825. static int physics_world_teleport_actor_world_pose(lua_State* L)
  1826. {
  1827. LuaStack stack(L);
  1828. stack.get_physics_world(1)->teleport_actor_world_pose(stack.get_actor(2), stack.get_matrix4x4(3));
  1829. return 0;
  1830. }
  1831. static int physics_world_actor_center_of_mass(lua_State* L)
  1832. {
  1833. LuaStack stack(L);
  1834. stack.push_vector3(stack.get_physics_world(1)->actor_center_of_mass(stack.get_actor(2)));
  1835. return 1;
  1836. }
  1837. static int physics_world_enable_actor_gravity(lua_State* L)
  1838. {
  1839. LuaStack stack(L);
  1840. stack.get_physics_world(1)->enable_actor_gravity(stack.get_actor(2));
  1841. return 0;
  1842. }
  1843. static int physics_world_disable_actor_gravity(lua_State* L)
  1844. {
  1845. LuaStack stack(L);
  1846. stack.get_physics_world(1)->disable_actor_gravity(stack.get_actor(2));
  1847. return 0;
  1848. }
  1849. static int physics_world_enable_actor_collision(lua_State* L)
  1850. {
  1851. LuaStack stack(L);
  1852. stack.get_physics_world(1)->enable_actor_collision(stack.get_actor(2));
  1853. return 0;
  1854. }
  1855. static int physics_world_disable_actor_collision(lua_State* L)
  1856. {
  1857. LuaStack stack(L);
  1858. stack.get_physics_world(1)->disable_actor_collision(stack.get_actor(2));
  1859. return 0;
  1860. }
  1861. static int physics_world_set_actor_collision_filter(lua_State* L)
  1862. {
  1863. LuaStack stack(L);
  1864. stack.get_physics_world(1)->set_actor_collision_filter(stack.get_actor(2), stack.get_string_id_32(3));
  1865. return 0;
  1866. }
  1867. static int physics_world_set_actor_kinematic(lua_State* L)
  1868. {
  1869. LuaStack stack(L);
  1870. stack.get_physics_world(1)->set_actor_kinematic(stack.get_actor(2), stack.get_bool(3));
  1871. return 0;
  1872. }
  1873. static int physics_world_move_actor(lua_State* L)
  1874. {
  1875. LuaStack stack(L);
  1876. stack.get_physics_world(1)->move_actor(stack.get_actor(2), stack.get_vector3(3));
  1877. return 0;
  1878. }
  1879. static int physics_world_is_static(lua_State* L)
  1880. {
  1881. LuaStack stack(L);
  1882. stack.push_bool(stack.get_physics_world(1)->is_static(stack.get_actor(2)));
  1883. return 1;
  1884. }
  1885. static int physics_world_is_dynamic(lua_State* L)
  1886. {
  1887. LuaStack stack(L);
  1888. stack.push_bool(stack.get_physics_world(1)->is_dynamic(stack.get_actor(2)));
  1889. return 1;
  1890. }
  1891. static int physics_world_is_kinematic(lua_State* L)
  1892. {
  1893. LuaStack stack(L);
  1894. stack.push_bool(stack.get_physics_world(1)->is_kinematic(stack.get_actor(2)));
  1895. return 1;
  1896. }
  1897. static int physics_world_is_nonkinematic(lua_State* L)
  1898. {
  1899. LuaStack stack(L);
  1900. stack.push_bool(stack.get_physics_world(1)->is_nonkinematic(stack.get_actor(2)));
  1901. return 1;
  1902. }
  1903. static int physics_world_actor_linear_damping(lua_State* L)
  1904. {
  1905. LuaStack stack(L);
  1906. stack.push_float(stack.get_physics_world(1)->actor_linear_damping(stack.get_actor(2)));
  1907. return 1;
  1908. }
  1909. static int physics_world_set_actor_linear_damping(lua_State* L)
  1910. {
  1911. LuaStack stack(L);
  1912. stack.get_physics_world(1)->set_actor_linear_damping(stack.get_actor(2), stack.get_float(3));
  1913. return 0;
  1914. }
  1915. static int physics_world_actor_angular_damping(lua_State* L)
  1916. {
  1917. LuaStack stack(L);
  1918. stack.push_float(stack.get_physics_world(1)->actor_angular_damping(stack.get_actor(2)));
  1919. return 1;
  1920. }
  1921. static int physics_world_set_actor_angular_damping(lua_State* L)
  1922. {
  1923. LuaStack stack(L);
  1924. stack.get_physics_world(1)->set_actor_angular_damping(stack.get_actor(2), stack.get_float(3));
  1925. return 0;
  1926. }
  1927. static int physics_world_actor_linear_velocity(lua_State* L)
  1928. {
  1929. LuaStack stack(L);
  1930. stack.push_vector3(stack.get_physics_world(1)->actor_linear_velocity(stack.get_actor(2)));
  1931. return 1;
  1932. }
  1933. static int physics_world_set_actor_linear_velocity(lua_State* L)
  1934. {
  1935. LuaStack stack(L);
  1936. stack.get_physics_world(1)->set_actor_linear_velocity(stack.get_actor(2), stack.get_vector3(3));
  1937. return 0;
  1938. }
  1939. static int physics_world_actor_angular_velocity(lua_State* L)
  1940. {
  1941. LuaStack stack(L);
  1942. stack.push_vector3(stack.get_physics_world(1)->actor_angular_velocity(stack.get_actor(2)));
  1943. return 1;
  1944. }
  1945. static int physics_world_set_actor_angular_velocity(lua_State* L)
  1946. {
  1947. LuaStack stack(L);
  1948. stack.get_physics_world(1)->set_actor_angular_velocity(stack.get_actor(2), stack.get_vector3(3));
  1949. return 0;
  1950. }
  1951. static int physics_world_add_actor_impulse(lua_State* L)
  1952. {
  1953. LuaStack stack(L);
  1954. stack.get_physics_world(1)->add_actor_impulse(stack.get_actor(2), stack.get_vector3(3));
  1955. return 0;
  1956. }
  1957. static int physics_world_add_actor_impulse_at(lua_State* L)
  1958. {
  1959. LuaStack stack(L);
  1960. stack.get_physics_world(1)->add_actor_impulse_at(stack.get_actor(2), stack.get_vector3(3), stack.get_vector3(4));
  1961. return 0;
  1962. }
  1963. static int physics_world_add_actor_torque_impulse(lua_State* L)
  1964. {
  1965. LuaStack stack(L);
  1966. stack.get_physics_world(1)->add_actor_torque_impulse(stack.get_actor(2), stack.get_vector3(3));
  1967. return 0;
  1968. }
  1969. static int physics_world_push_actor(lua_State* L)
  1970. {
  1971. LuaStack stack(L);
  1972. stack.get_physics_world(1)->push_actor(stack.get_actor(2), stack.get_vector3(3), stack.get_float(4));
  1973. return 0;
  1974. }
  1975. static int physics_world_push_actor_at(lua_State* L)
  1976. {
  1977. LuaStack stack(L);
  1978. stack.get_physics_world(1)->push_actor_at(stack.get_actor(2), stack.get_vector3(3), stack.get_float(4), stack.get_vector3(5));
  1979. return 0;
  1980. }
  1981. static int physics_world_is_sleeping(lua_State* L)
  1982. {
  1983. LuaStack stack(L);
  1984. stack.push_bool(stack.get_physics_world(1)->is_sleeping(stack.get_actor(2)));
  1985. return 1;
  1986. }
  1987. static int physics_world_wake_up(lua_State* L)
  1988. {
  1989. LuaStack stack(L);
  1990. stack.get_physics_world(1)->wake_up(stack.get_actor(2));
  1991. return 0;
  1992. }
  1993. static int physics_world_controller_instances(lua_State* L)
  1994. {
  1995. LuaStack stack(L);
  1996. stack.push_controller(stack.get_physics_world(1)->controller(stack.get_unit(2)));
  1997. return 1;
  1998. }
  1999. static int physics_world_move_controller(lua_State* L)
  2000. {
  2001. LuaStack stack(L);
  2002. stack.get_physics_world(1)->move_controller(stack.get_controller(2), stack.get_vector3(3));
  2003. return 0;
  2004. }
  2005. static int physics_world_create_joint(lua_State* L)
  2006. {
  2007. LuaStack stack(L);
  2008. JointDesc jd;
  2009. jd.type = JointType::SPRING;
  2010. jd.anchor_0 = vector3(0, -2, 0);
  2011. jd.anchor_1 = vector3(0, 2, 0);
  2012. jd.break_force = 999999.0f;
  2013. jd.hinge.axis = vector3(1, 0, 0);
  2014. jd.hinge.lower_limit = -3.14f / 4.0f;
  2015. jd.hinge.upper_limit = 3.14f / 4.0f;
  2016. jd.hinge.bounciness = 12.0f;
  2017. stack.get_physics_world(1)->create_joint(stack.get_actor(2), stack.get_actor(3), jd);
  2018. return 0;
  2019. }
  2020. static int physics_world_gravity(lua_State* L)
  2021. {
  2022. LuaStack stack(L);
  2023. stack.push_vector3(stack.get_physics_world(1)->gravity());
  2024. return 1;
  2025. }
  2026. static int physics_world_set_gravity(lua_State* L)
  2027. {
  2028. LuaStack stack(L);
  2029. stack.get_physics_world(1)->set_gravity(stack.get_vector3(2));
  2030. return 0;
  2031. }
  2032. static int physics_world_raycast(lua_State* L)
  2033. {
  2034. LuaStack stack(L);
  2035. PhysicsWorld* world = stack.get_physics_world(1);
  2036. const char* name = stack.get_string(5);
  2037. const RaycastMode::Enum mode = name_to_raycast_mode(name);
  2038. LUA_ASSERT(mode != RaycastMode::COUNT, stack, "Unknown raycast mode: '%s'", name);
  2039. TempAllocator1024 ta;
  2040. Array<RaycastHit> hits(ta);
  2041. world->raycast(stack.get_vector3(2)
  2042. , stack.get_vector3(3)
  2043. , stack.get_float(4)
  2044. , mode
  2045. , hits
  2046. );
  2047. stack.push_table();
  2048. for (u32 i = 0; i < array::size(hits); ++i)
  2049. {
  2050. stack.push_key_begin(i+1);
  2051. stack.push_actor(hits[i].actor);
  2052. stack.push_key_end();
  2053. }
  2054. return 1;
  2055. }
  2056. static int physics_world_enable_debug_drawing(lua_State* L)
  2057. {
  2058. LuaStack stack(L);
  2059. stack.get_physics_world(1)->enable_debug_drawing(stack.get_bool(2));
  2060. return 0;
  2061. }
  2062. static int physics_world_tostring(lua_State* L)
  2063. {
  2064. LuaStack stack(L);
  2065. PhysicsWorld* pw = stack.get_physics_world(1);
  2066. stack.push_fstring("PhysicsWorld (%p)", pw);
  2067. return 1;
  2068. }
  2069. static int sound_world_stop_all(lua_State* L)
  2070. {
  2071. LuaStack stack(L);
  2072. stack.get_sound_world(1)->stop_all();
  2073. return 0;
  2074. }
  2075. static int sound_world_pause_all(lua_State* L)
  2076. {
  2077. LuaStack stack(L);
  2078. stack.get_sound_world(1)->pause_all();
  2079. return 0;
  2080. }
  2081. static int sound_world_resume_all(lua_State* L)
  2082. {
  2083. LuaStack stack(L);
  2084. stack.get_sound_world(1)->resume_all();
  2085. return 0;
  2086. }
  2087. static int sound_world_is_playing(lua_State* L)
  2088. {
  2089. LuaStack stack(L);
  2090. stack.push_bool(stack.get_sound_world(1)->is_playing(stack.get_sound_instance_id(2)));
  2091. return 1;
  2092. }
  2093. static int sound_world_tostring(lua_State* L)
  2094. {
  2095. LuaStack stack(L);
  2096. SoundWorld* sw = stack.get_sound_world(1);
  2097. stack.push_fstring("SoundWorld (%p)", sw);
  2098. return 1;
  2099. }
  2100. static int device_argv(lua_State* L)
  2101. {
  2102. LuaStack stack(L);
  2103. const int argc = device()->argc();
  2104. const char** argv = device()->argv();
  2105. stack.push_table(argc);
  2106. for (int i = 0; i < argc; ++i)
  2107. {
  2108. stack.push_key_begin(i + 1);
  2109. stack.push_string(argv[i]);
  2110. stack.push_key_end();
  2111. }
  2112. return 1;
  2113. }
  2114. static int device_platform(lua_State* L)
  2115. {
  2116. LuaStack stack(L);
  2117. stack.push_string(device()->platform());
  2118. return 1;
  2119. }
  2120. static int device_architecture(lua_State* L)
  2121. {
  2122. LuaStack stack(L);
  2123. stack.push_string(device()->architecture());
  2124. return 1;
  2125. }
  2126. static int device_version(lua_State* L)
  2127. {
  2128. LuaStack stack(L);
  2129. stack.push_string(device()->version());
  2130. return 1;
  2131. }
  2132. static int device_last_delta_time(lua_State* L)
  2133. {
  2134. LuaStack stack(L);
  2135. stack.push_float(device()->last_delta_time());
  2136. return 1;
  2137. }
  2138. static int device_quit(lua_State* /*L*/)
  2139. {
  2140. device()->quit();
  2141. return 0;
  2142. }
  2143. static int device_resolution(lua_State* L)
  2144. {
  2145. LuaStack stack(L);
  2146. u16 w, h;
  2147. device()->resolution(w, h);
  2148. stack.push_int(w);
  2149. stack.push_int(h);
  2150. return 2;
  2151. }
  2152. static int device_create_world(lua_State* L)
  2153. {
  2154. LuaStack stack(L);
  2155. stack.push_world(device()->create_world());
  2156. return 1;
  2157. }
  2158. static int device_destroy_world(lua_State* L)
  2159. {
  2160. LuaStack stack(L);
  2161. device()->destroy_world(*stack.get_world(1));
  2162. return 0;
  2163. }
  2164. static int device_render(lua_State* L)
  2165. {
  2166. LuaStack stack(L);
  2167. device()->render(*stack.get_world(1), stack.get_camera(2));
  2168. return 0;
  2169. }
  2170. static int device_create_resource_package(lua_State* L)
  2171. {
  2172. LuaStack stack(L);
  2173. stack.push_resource_package(device()->create_resource_package(stack.get_resource_id(1)));
  2174. return 1;
  2175. }
  2176. static int device_destroy_resource_package(lua_State* L)
  2177. {
  2178. LuaStack stack(L);
  2179. device()->destroy_resource_package(*stack.get_resource_package(1));
  2180. return 0;
  2181. }
  2182. static void lua_dump_table(lua_State* L, int i, StringStream& json)
  2183. {
  2184. LuaStack stack(L);
  2185. bool comma = false;
  2186. int array_index = 1;
  2187. json << "{";
  2188. stack.push_nil();
  2189. while (stack.next(i) != 0)
  2190. {
  2191. if (comma)
  2192. json << ",";
  2193. comma = true;
  2194. json << "\"";
  2195. if (stack.is_string(-2) && !stack.is_number(-2))
  2196. json << stack.get_string(-2);
  2197. else
  2198. json << array_index++;
  2199. json << "\":";
  2200. if (stack.is_nil(i + 2))
  2201. {
  2202. json << "null";
  2203. }
  2204. else if (stack.is_bool(i + 2))
  2205. {
  2206. const bool b = stack.get_bool(i + 2);
  2207. json << (b ? "true" : "false");
  2208. }
  2209. else if (stack.is_number(i + 2))
  2210. {
  2211. json << stack.get_float(i + 2);
  2212. }
  2213. else if (stack.is_string(i + 2))
  2214. {
  2215. const char* str = stack.get_string(i + 2);
  2216. json << "\"";
  2217. for (; *str; ++str)
  2218. {
  2219. if (*str == '"' || *str == '\\')
  2220. json << '\\';
  2221. json << *str;
  2222. }
  2223. json << "\"";
  2224. }
  2225. else if (stack.is_vector3(i + 2))
  2226. {
  2227. const Vector3 v = stack.get_vector3(i + 2);
  2228. json << "["
  2229. << v.x << ","
  2230. << v.y << ","
  2231. << v.z
  2232. << "]"
  2233. ;
  2234. }
  2235. else if (stack.is_quaternion(i + 2))
  2236. {
  2237. const Quaternion q = stack.get_quaternion(i + 2);
  2238. json << "["
  2239. << q.x << ","
  2240. << q.y << ","
  2241. << q.z << ","
  2242. << q.w
  2243. << "]"
  2244. ;
  2245. }
  2246. else if (stack.is_matrix4x4(i + 2))
  2247. {
  2248. const Matrix4x4 m = stack.get_matrix4x4(i + 2);
  2249. json << "["
  2250. << m.x.x << ","
  2251. << m.x.y << ","
  2252. << m.x.z << ","
  2253. << m.x.w << ","
  2254. << m.y.x << ","
  2255. << m.y.y << ","
  2256. << m.y.z << ","
  2257. << m.y.w << ","
  2258. << m.z.x << ","
  2259. << m.z.y << ","
  2260. << m.z.z << ","
  2261. << m.z.w << ","
  2262. << m.t.x << ","
  2263. << m.t.y << ","
  2264. << m.t.z << ","
  2265. << m.t.w
  2266. << "]"
  2267. ;
  2268. }
  2269. else if (stack.is_table(i + 2))
  2270. {
  2271. lua_dump_table(L, i + 2, json);
  2272. }
  2273. else
  2274. {
  2275. LUA_ASSERT(false, stack, "Unsupported key value");
  2276. }
  2277. stack.pop(1);
  2278. }
  2279. json << "}";
  2280. }
  2281. static int device_console_send(lua_State* L)
  2282. {
  2283. LuaStack stack(L);
  2284. LUA_ASSERT(stack.is_table(1), stack, "Table expected");
  2285. TempAllocator1024 alloc;
  2286. StringStream json(alloc);
  2287. lua_dump_table(L, 1, json);
  2288. device()->console_server()->send(string_stream::c_str(json));
  2289. return 0;
  2290. }
  2291. static int device_can_get(lua_State* L)
  2292. {
  2293. LuaStack stack(L);
  2294. const StringId64 type(stack.get_string(1));
  2295. stack.push_bool(device()->resource_manager()->can_get(type, stack.get_resource_id(2)));
  2296. return 1;
  2297. }
  2298. static int device_enable_resource_autoload(lua_State* L)
  2299. {
  2300. LuaStack stack(L);
  2301. device()->resource_manager()->enable_autoload(stack.get_bool(1));
  2302. return 0;
  2303. }
  2304. static int device_temp_count(lua_State* L)
  2305. {
  2306. LuaStack stack(L);
  2307. u32 nv, nq, nm;
  2308. device()->lua_environment()->temp_count(nv, nq, nm);
  2309. stack.push_int(nv);
  2310. stack.push_int(nq);
  2311. stack.push_int(nm);
  2312. return 3;
  2313. }
  2314. static int device_set_temp_count(lua_State* L)
  2315. {
  2316. LuaStack stack(L);
  2317. u32 nv = stack.get_int(1);
  2318. u32 nq = stack.get_int(2);
  2319. u32 nm = stack.get_int(3);
  2320. device()->lua_environment()->set_temp_count(nv, nq, nm);
  2321. return 0;
  2322. }
  2323. static int profiler_enter_scope(lua_State* L)
  2324. {
  2325. LuaStack stack(L);
  2326. profiler::enter_profile_scope(stack.get_string(1));
  2327. return 0;
  2328. }
  2329. static int profiler_leave_scope(lua_State* L)
  2330. {
  2331. LuaStack stack(L);
  2332. profiler::leave_profile_scope();
  2333. return 0;
  2334. }
  2335. static int profiler_record(lua_State* L)
  2336. {
  2337. LuaStack stack(L);
  2338. const char* name = stack.get_string(1);
  2339. if (stack.is_number(2))
  2340. profiler::record_float(name, stack.get_float(2));
  2341. else
  2342. profiler::record_vector3(name, stack.get_vector3(2));
  2343. return 0;
  2344. }
  2345. static int debug_line_add_line(lua_State* L)
  2346. {
  2347. LuaStack stack(L);
  2348. stack.get_debug_line(1)->add_line(stack.get_vector3(2)
  2349. , stack.get_vector3(3)
  2350. , stack.get_color4(4)
  2351. );
  2352. return 0;
  2353. }
  2354. static int debug_line_add_axes(lua_State* L)
  2355. {
  2356. LuaStack stack(L);
  2357. const f32 len = stack.num_args() == 3
  2358. ? stack.get_float(3)
  2359. : 1.0f
  2360. ;
  2361. stack.get_debug_line(1)->add_axes(stack.get_matrix4x4(2), len);
  2362. return 0;
  2363. }
  2364. static int debug_line_add_circle(lua_State* L)
  2365. {
  2366. LuaStack stack(L);
  2367. const u32 segments = stack.num_args() >= 6
  2368. ? stack.get_int(6)
  2369. : DebugLine::NUM_SEGMENTS
  2370. ;
  2371. stack.get_debug_line(1)->add_circle(stack.get_vector3(2)
  2372. , stack.get_float(3)
  2373. , stack.get_vector3(4)
  2374. , stack.get_color4(5)
  2375. , segments
  2376. );
  2377. return 0;
  2378. }
  2379. static int debug_line_add_cone(lua_State* L)
  2380. {
  2381. LuaStack stack(L);
  2382. const u32 segments = stack.num_args() >= 6
  2383. ? stack.get_int(6)
  2384. : DebugLine::NUM_SEGMENTS
  2385. ;
  2386. stack.get_debug_line(1)->add_cone(stack.get_vector3(2)
  2387. , stack.get_vector3(3)
  2388. , stack.get_float(4)
  2389. , stack.get_color4(5)
  2390. , segments
  2391. );
  2392. return 0;
  2393. }
  2394. static int debug_line_add_sphere(lua_State* L)
  2395. {
  2396. LuaStack stack(L);
  2397. const u32 segments = stack.num_args() >= 5
  2398. ? stack.get_int(5)
  2399. : DebugLine::NUM_SEGMENTS
  2400. ;
  2401. stack.get_debug_line(1)->add_sphere(stack.get_vector3(2)
  2402. , stack.get_float(3)
  2403. , stack.get_color4(4)
  2404. , segments
  2405. );
  2406. return 0;
  2407. }
  2408. static int debug_line_add_obb(lua_State* L)
  2409. {
  2410. LuaStack stack(L);
  2411. stack.get_debug_line(1)->add_obb(stack.get_matrix4x4(2)
  2412. , stack.get_vector3(3)
  2413. , stack.get_color4(4)
  2414. );
  2415. return 0;
  2416. }
  2417. static int debug_line_add_unit(lua_State* L)
  2418. {
  2419. LuaStack stack(L);
  2420. stack.get_debug_line(1)->add_unit(*device()->resource_manager()
  2421. , stack.get_matrix4x4(2)
  2422. , stack.get_resource_id(3)
  2423. , stack.get_color4(4)
  2424. );
  2425. return 0;
  2426. }
  2427. static int debug_line_reset(lua_State* L)
  2428. {
  2429. LuaStack stack(L);
  2430. stack.get_debug_line(1)->reset();
  2431. return 0;
  2432. }
  2433. static int debug_line_submit(lua_State* L)
  2434. {
  2435. LuaStack stack(L);
  2436. stack.get_debug_line(1)->submit();
  2437. return 0;
  2438. }
  2439. static int debug_line_tostring(lua_State* L)
  2440. {
  2441. LuaStack stack(L);
  2442. stack.push_fstring("DebugLine (%p)", stack.get_debug_line(1));
  2443. return 1;
  2444. }
  2445. static int resource_package_load(lua_State* L)
  2446. {
  2447. LuaStack stack(L);
  2448. stack.get_resource_package(1)->load();
  2449. return 0;
  2450. }
  2451. static int resource_package_unload(lua_State* L)
  2452. {
  2453. LuaStack stack(L);
  2454. stack.get_resource_package(1)->unload();
  2455. return 0;
  2456. }
  2457. static int resource_package_flush(lua_State* L)
  2458. {
  2459. LuaStack stack(L);
  2460. stack.get_resource_package(1)->flush();
  2461. return 0;
  2462. }
  2463. static int resource_package_has_loaded(lua_State* L)
  2464. {
  2465. LuaStack stack(L);
  2466. stack.push_bool(stack.get_resource_package(1)->has_loaded());
  2467. return 1;
  2468. }
  2469. static int resource_package_tostring(lua_State* L)
  2470. {
  2471. LuaStack stack(L);
  2472. ResourcePackage* package = stack.get_resource_package(1);
  2473. stack.push_fstring("ResourcePackage (%p)", package);
  2474. return 1;
  2475. }
  2476. static int material_set_float(lua_State* L)
  2477. {
  2478. LuaStack stack(L);
  2479. stack.get_material(1)->set_float(stack.get_string(2), stack.get_float(3));
  2480. return 0;
  2481. }
  2482. static int material_set_vector2(lua_State* L)
  2483. {
  2484. LuaStack stack(L);
  2485. stack.get_material(1)->set_vector2(stack.get_string(2), stack.get_vector2(3));
  2486. return 0;
  2487. }
  2488. static int material_set_vector3(lua_State* L)
  2489. {
  2490. LuaStack stack(L);
  2491. stack.get_material(1)->set_vector3(stack.get_string(2), stack.get_vector3(3));
  2492. return 0;
  2493. }
  2494. static int gui_resolution(lua_State* L)
  2495. {
  2496. LuaStack stack(L);
  2497. const Vector2 resolution = stack.get_gui(1)->resolution();
  2498. stack.push_int((s32)resolution.x);
  2499. stack.push_int((s32)resolution.y);
  2500. return 2;
  2501. }
  2502. static int gui_move(lua_State* L)
  2503. {
  2504. LuaStack stack(L);
  2505. stack.get_gui(1)->move(stack.get_vector2(2));
  2506. return 0;
  2507. }
  2508. static int gui_screen_to_gui(lua_State* L)
  2509. {
  2510. LuaStack stack(L);
  2511. stack.push_vector2(stack.get_gui(1)->screen_to_gui(stack.get_vector2(2)));
  2512. return 1;
  2513. }
  2514. static int gui_rect(lua_State* L)
  2515. {
  2516. LuaStack stack(L);
  2517. stack.get_gui(1)->rect(stack.get_vector2(2)
  2518. , stack.get_vector2(3)
  2519. , stack.get_resource_id(4)
  2520. , stack.get_color4(5)
  2521. );
  2522. return 0;
  2523. }
  2524. static int gui_image(lua_State* L)
  2525. {
  2526. LuaStack stack(L);
  2527. stack.get_gui(1)->image(stack.get_vector2(2)
  2528. , stack.get_vector2(3)
  2529. , stack.get_resource_id(4)
  2530. , stack.get_color4(5)
  2531. );
  2532. return 0;
  2533. }
  2534. static int gui_image_uv(lua_State* L)
  2535. {
  2536. LuaStack stack(L);
  2537. stack.get_gui(1)->image_uv(stack.get_vector2(2)
  2538. , stack.get_vector2(3)
  2539. , stack.get_vector2(4)
  2540. , stack.get_vector2(5)
  2541. , stack.get_resource_id(6)
  2542. , stack.get_color4(7)
  2543. );
  2544. return 0;
  2545. }
  2546. static int gui_text(lua_State* L)
  2547. {
  2548. LuaStack stack(L);
  2549. stack.get_gui(1)->text(stack.get_vector2(2)
  2550. , stack.get_int(3)
  2551. , stack.get_string(4)
  2552. , stack.get_resource_id(5)
  2553. , stack.get_resource_id(6)
  2554. , stack.get_color4(7)
  2555. );
  2556. return 0;
  2557. }
  2558. static int display_modes(lua_State* L)
  2559. {
  2560. LuaStack stack(L);
  2561. TempAllocator1024 ta;
  2562. Array<DisplayMode> modes(ta);
  2563. device()->display()->modes(modes);
  2564. stack.push_table(array::size(modes));
  2565. for (u32 i = 0; i < array::size(modes); ++i)
  2566. {
  2567. stack.push_key_begin(i+1);
  2568. stack.push_table(3);
  2569. {
  2570. stack.push_key_begin("id");
  2571. stack.push_int(modes[i].id);
  2572. stack.push_key_end();
  2573. stack.push_key_begin("width");
  2574. stack.push_int(modes[i].width);
  2575. stack.push_key_end();
  2576. stack.push_key_begin("height");
  2577. stack.push_int(modes[i].height);
  2578. stack.push_key_end();
  2579. }
  2580. stack.push_key_end();
  2581. }
  2582. return 1;
  2583. }
  2584. static int display_set_mode(lua_State* L)
  2585. {
  2586. LuaStack stack(L);
  2587. device()->display()->set_mode(stack.get_int(1));
  2588. return 0;
  2589. }
  2590. static int window_show(lua_State* L)
  2591. {
  2592. LuaStack stack(L);
  2593. device()->window()->show();
  2594. return 0;
  2595. }
  2596. static int window_hide(lua_State* L)
  2597. {
  2598. LuaStack stack(L);
  2599. device()->window()->hide();
  2600. return 0;
  2601. }
  2602. static int window_resize(lua_State* L)
  2603. {
  2604. LuaStack stack(L);
  2605. device()->window()->resize(stack.get_int(1), stack.get_int(2));
  2606. return 0;
  2607. }
  2608. static int window_move(lua_State* L)
  2609. {
  2610. LuaStack stack(L);
  2611. device()->window()->move(stack.get_int(1), stack.get_int(2));
  2612. return 0;
  2613. }
  2614. static int window_minimize(lua_State* /*L*/)
  2615. {
  2616. device()->window()->minimize();
  2617. return 0;
  2618. }
  2619. static int window_restore(lua_State* /*L*/)
  2620. {
  2621. device()->window()->restore();
  2622. return 0;
  2623. }
  2624. static int window_title(lua_State* L)
  2625. {
  2626. LuaStack stack(L);
  2627. stack.push_string(device()->window()->title());
  2628. return 1;
  2629. }
  2630. static int window_set_title(lua_State* L)
  2631. {
  2632. LuaStack stack(L);
  2633. device()->window()->set_title(stack.get_string(1));
  2634. return 0;
  2635. }
  2636. static int window_show_cursor(lua_State* L)
  2637. {
  2638. LuaStack stack(L);
  2639. device()->window()->show_cursor(stack.get_bool(1));
  2640. return 0;
  2641. }
  2642. void load_api(LuaEnvironment& env)
  2643. {
  2644. env.add_module_function("Math", "ray_plane_intersection", math_ray_plane_intersection);
  2645. env.add_module_function("Math", "ray_disc_intersection", math_ray_disc_intersection);
  2646. env.add_module_function("Math", "ray_sphere_intersection", math_ray_sphere_intersection);
  2647. env.add_module_function("Math", "ray_obb_intersection", math_ray_obb_intersection);
  2648. env.add_module_function("Math", "ray_triangle_intersection", math_ray_triangle_intersection);
  2649. env.add_module_function("Vector3", "new", vector3_new);
  2650. env.add_module_function("Vector3", "x", vector3_x);
  2651. env.add_module_function("Vector3", "y", vector3_y);
  2652. env.add_module_function("Vector3", "z", vector3_z);
  2653. env.add_module_function("Vector3", "set_x", vector3_set_x);
  2654. env.add_module_function("Vector3", "set_y", vector3_set_y);
  2655. env.add_module_function("Vector3", "set_z", vector3_set_z);
  2656. env.add_module_function("Vector3", "elements", vector3_elements);
  2657. env.add_module_function("Vector3", "add", vector3_add);
  2658. env.add_module_function("Vector3", "subtract", vector3_subtract);
  2659. env.add_module_function("Vector3", "multiply", vector3_multiply);
  2660. env.add_module_function("Vector3", "dot", vector3_dot);
  2661. env.add_module_function("Vector3", "cross", vector3_cross);
  2662. env.add_module_function("Vector3", "equal", vector3_equal);
  2663. env.add_module_function("Vector3", "length", vector3_length);
  2664. env.add_module_function("Vector3", "length_squared", vector3_length_squared);
  2665. env.add_module_function("Vector3", "set_length", vector3_set_length);
  2666. env.add_module_function("Vector3", "normalize", vector3_normalize);
  2667. env.add_module_function("Vector3", "distance", vector3_distance);
  2668. env.add_module_function("Vector3", "distance_squared", vector3_distance_squared);
  2669. env.add_module_function("Vector3", "angle", vector3_angle);
  2670. env.add_module_function("Vector3", "max", vector3_max);
  2671. env.add_module_function("Vector3", "min", vector3_min);
  2672. env.add_module_function("Vector3", "lerp", vector3_lerp);
  2673. env.add_module_function("Vector3", "forward", vector3_forward);
  2674. env.add_module_function("Vector3", "backward", vector3_backward);
  2675. env.add_module_function("Vector3", "left", vector3_left);
  2676. env.add_module_function("Vector3", "right", vector3_right);
  2677. env.add_module_function("Vector3", "up", vector3_up);
  2678. env.add_module_function("Vector3", "down", vector3_down);
  2679. env.add_module_function("Vector3", "zero", vector3_zero);
  2680. env.add_module_function("Vector3", "to_string", vector3_to_string);
  2681. env.set_module_constructor("Vector3", vector3_ctor);
  2682. env.add_module_function("Vector2", "new", vector2_new);
  2683. env.set_module_constructor("Vector2", vector2_ctor);
  2684. env.add_module_function("Vector3Box", "new", vector3box_new);
  2685. env.add_module_function("Vector3Box", "store", vector3box_store);
  2686. env.add_module_function("Vector3Box", "unbox", vector3box_unbox);
  2687. env.add_module_function("Vector3Box", "__index", "Vector3Box");
  2688. env.add_module_function("Vector3Box", "__tostring", vector3box_tostring);
  2689. env.set_module_constructor("Vector3Box", vector3box_ctor);
  2690. env.add_module_function("Matrix4x4", "new", matrix4x4_new);
  2691. env.add_module_function("Matrix4x4", "from_quaternion", matrix4x4_from_quaternion);
  2692. env.add_module_function("Matrix4x4", "from_translation", matrix4x4_from_translation);
  2693. env.add_module_function("Matrix4x4", "from_quaternion_translation", matrix4x4_from_quaternion_translation);
  2694. env.add_module_function("Matrix4x4", "from_axes", matrix4x4_from_axes);
  2695. env.add_module_function("Matrix4x4", "copy", matrix4x4_copy);
  2696. env.add_module_function("Matrix4x4", "add", matrix4x4_add);
  2697. env.add_module_function("Matrix4x4", "subtract", matrix4x4_subtract);
  2698. env.add_module_function("Matrix4x4", "multiply", matrix4x4_multiply);
  2699. env.add_module_function("Matrix4x4", "transpose", matrix4x4_transpose);
  2700. env.add_module_function("Matrix4x4", "determinant", matrix4x4_determinant);
  2701. env.add_module_function("Matrix4x4", "invert", matrix4x4_invert);
  2702. env.add_module_function("Matrix4x4", "x", matrix4x4_x);
  2703. env.add_module_function("Matrix4x4", "y", matrix4x4_y);
  2704. env.add_module_function("Matrix4x4", "z", matrix4x4_z);
  2705. env.add_module_function("Matrix4x4", "set_x", matrix4x4_set_x);
  2706. env.add_module_function("Matrix4x4", "set_y", matrix4x4_set_y);
  2707. env.add_module_function("Matrix4x4", "set_z", matrix4x4_set_z);
  2708. env.add_module_function("Matrix4x4", "translation", matrix4x4_translation);
  2709. env.add_module_function("Matrix4x4", "set_translation", matrix4x4_set_translation);
  2710. env.add_module_function("Matrix4x4", "rotation", matrix4x4_rotation);
  2711. env.add_module_function("Matrix4x4", "set_rotation", matrix4x4_set_rotation);
  2712. env.add_module_function("Matrix4x4", "identity", matrix4x4_identity);
  2713. env.add_module_function("Matrix4x4", "transform", matrix4x4_transform);
  2714. env.add_module_function("Matrix4x4", "to_string", matrix4x4_to_string);
  2715. env.set_module_constructor("Matrix4x4", matrix4x4_ctor);
  2716. env.add_module_function("Matrix4x4Box", "new", matrix4x4box_new);
  2717. env.add_module_function("Matrix4x4Box", "store", matrix4x4box_store);
  2718. env.add_module_function("Matrix4x4Box", "unbox", matrix4x4box_unbox);
  2719. env.add_module_function("Matrix4x4Box", "__index", "Matrix4x4Box");
  2720. env.add_module_function("Matrix4x4Box", "__tostring", matrix4x4box_tostring);
  2721. env.set_module_constructor("Matrix4x4Box", matrix4x4box_ctor);
  2722. env.add_module_function("Quaternion", "new", quaternion_new);
  2723. env.add_module_function("Quaternion", "from_elements", quaternion_from_elements);
  2724. env.add_module_function("Quaternion", "negate", quaternion_negate);
  2725. env.add_module_function("Quaternion", "identity", quaternion_identity);
  2726. env.add_module_function("Quaternion", "multiply", quaternion_multiply);
  2727. env.add_module_function("Quaternion", "multiply_by_scalar", quaternion_multiply_by_scalar);
  2728. env.add_module_function("Quaternion", "dot", quaternion_dot);
  2729. env.add_module_function("Quaternion", "length", quaternion_length);
  2730. env.add_module_function("Quaternion", "normalize", quaternion_normalize);
  2731. env.add_module_function("Quaternion", "conjugate", quaternion_conjugate);
  2732. env.add_module_function("Quaternion", "inverse", quaternion_inverse);
  2733. env.add_module_function("Quaternion", "power", quaternion_power);
  2734. env.add_module_function("Quaternion", "elements", quaternion_elements);
  2735. env.add_module_function("Quaternion", "look", quaternion_look);
  2736. env.add_module_function("Quaternion", "right", quaternion_right);
  2737. env.add_module_function("Quaternion", "up", quaternion_up);
  2738. env.add_module_function("Quaternion", "forward", quaternion_forward);
  2739. env.add_module_function("Quaternion", "lerp", quaternion_lerp);
  2740. env.add_module_function("Quaternion", "to_string", quaternion_to_string);
  2741. env.set_module_constructor("Quaternion", quaternion_ctor);
  2742. env.add_module_function("QuaternionBox", "new", quaternionbox_new);
  2743. env.add_module_function("QuaternionBox", "store", quaternionbox_store);
  2744. env.add_module_function("QuaternionBox", "unbox", quaternionbox_unbox);
  2745. env.add_module_function("QuaternionBox", "__index", "QuaternionBox");
  2746. env.add_module_function("QuaternionBox", "__tostring", quaternionbox_tostring);
  2747. env.set_module_constructor("QuaternionBox", quaternionbox_ctor);
  2748. env.add_module_function("Color4", "new", color4_new);
  2749. env.add_module_function("Color4", "black", color4_black);
  2750. env.add_module_function("Color4", "white", color4_white);
  2751. env.add_module_function("Color4", "red", color4_red);
  2752. env.add_module_function("Color4", "green", color4_green);
  2753. env.add_module_function("Color4", "blue", color4_blue);
  2754. env.add_module_function("Color4", "yellow", color4_yellow);
  2755. env.add_module_function("Color4", "orange", color4_orange);
  2756. env.add_module_function("Color4", "to_string", quaternion_to_string);
  2757. env.set_module_constructor("Color4", color4_ctor);
  2758. env.add_module_function("Lightuserdata_mt", "__add", lightuserdata_add);
  2759. env.add_module_function("Lightuserdata_mt", "__sub", lightuserdata_sub);
  2760. env.add_module_function("Lightuserdata_mt", "__mul", lightuserdata_mul);
  2761. env.add_module_function("Lightuserdata_mt", "__unm", lightuserdata_unm);
  2762. env.add_module_function("Lightuserdata_mt", "__index", lightuserdata_index);
  2763. env.add_module_function("Lightuserdata_mt", "__newindex", lightuserdata_newindex);
  2764. env.add_module_function("Keyboard", "name", KEYBOARD_FN(name));
  2765. env.add_module_function("Keyboard", "connected", KEYBOARD_FN(connected));
  2766. env.add_module_function("Keyboard", "num_buttons", KEYBOARD_FN(num_buttons));
  2767. env.add_module_function("Keyboard", "num_axes", KEYBOARD_FN(num_axes));
  2768. env.add_module_function("Keyboard", "pressed", KEYBOARD_FN(pressed));
  2769. env.add_module_function("Keyboard", "released", KEYBOARD_FN(released));
  2770. env.add_module_function("Keyboard", "any_pressed", KEYBOARD_FN(any_pressed));
  2771. env.add_module_function("Keyboard", "any_released", KEYBOARD_FN(any_released));
  2772. env.add_module_function("Keyboard", "button_name", KEYBOARD_FN(button_name));
  2773. env.add_module_function("Keyboard", "button_id", KEYBOARD_FN(button_id));
  2774. env.add_module_function("Mouse", "name", MOUSE_FN(name));
  2775. env.add_module_function("Mouse", "connected", MOUSE_FN(connected));
  2776. env.add_module_function("Mouse", "num_buttons", MOUSE_FN(num_buttons));
  2777. env.add_module_function("Mouse", "num_axes", MOUSE_FN(num_axes));
  2778. env.add_module_function("Mouse", "pressed", MOUSE_FN(pressed));
  2779. env.add_module_function("Mouse", "released", MOUSE_FN(released));
  2780. env.add_module_function("Mouse", "any_pressed", MOUSE_FN(any_pressed));
  2781. env.add_module_function("Mouse", "any_released", MOUSE_FN(any_released));
  2782. env.add_module_function("Mouse", "axis", MOUSE_FN(axis));
  2783. env.add_module_function("Mouse", "button_name", MOUSE_FN(button_name));
  2784. env.add_module_function("Mouse", "axis_name", MOUSE_FN(axis_name));
  2785. env.add_module_function("Mouse", "button_id", MOUSE_FN(button_id));
  2786. env.add_module_function("Mouse", "axis_id", MOUSE_FN(axis_id));
  2787. env.add_module_function("Touch", "name", TOUCH_FN(name));
  2788. env.add_module_function("Touch", "connected", TOUCH_FN(connected));
  2789. env.add_module_function("Touch", "num_buttons", TOUCH_FN(num_buttons));
  2790. env.add_module_function("Touch", "num_axes", TOUCH_FN(num_axes));
  2791. env.add_module_function("Touch", "pressed", TOUCH_FN(pressed));
  2792. env.add_module_function("Touch", "released", TOUCH_FN(released));
  2793. env.add_module_function("Touch", "any_pressed", TOUCH_FN(any_pressed));
  2794. env.add_module_function("Touch", "any_released", TOUCH_FN(any_released));
  2795. env.add_module_function("Touch", "axis", TOUCH_FN(axis));
  2796. env.add_module_function("Touch", "button_name", TOUCH_FN(button_name));
  2797. env.add_module_function("Touch", "axis_name", TOUCH_FN(axis_name));
  2798. env.add_module_function("Touch", "button_id", TOUCH_FN(button_id));
  2799. env.add_module_function("Touch", "axis_id", TOUCH_FN(axis_id));
  2800. env.add_module_function("Pad1", "name", JOYPAD_FN(0, name));
  2801. env.add_module_function("Pad1", "connected", JOYPAD_FN(0, connected));
  2802. env.add_module_function("Pad1", "num_buttons", JOYPAD_FN(0, num_buttons));
  2803. env.add_module_function("Pad1", "num_axes", JOYPAD_FN(0, num_axes));
  2804. env.add_module_function("Pad1", "pressed", JOYPAD_FN(0, pressed));
  2805. env.add_module_function("Pad1", "released", JOYPAD_FN(0, released));
  2806. env.add_module_function("Pad1", "any_pressed", JOYPAD_FN(0, any_pressed));
  2807. env.add_module_function("Pad1", "any_released", JOYPAD_FN(0, any_released));
  2808. env.add_module_function("Pad1", "axis", JOYPAD_FN(0, axis));
  2809. env.add_module_function("Pad1", "button_name", JOYPAD_FN(0, button_name));
  2810. env.add_module_function("Pad1", "axis_name", JOYPAD_FN(0, axis_name));
  2811. env.add_module_function("Pad1", "button_id", JOYPAD_FN(0, button_id));
  2812. env.add_module_function("Pad1", "axis_id", JOYPAD_FN(0, axis_id));
  2813. env.add_module_function("Pad2", "name", JOYPAD_FN(1, name));
  2814. env.add_module_function("Pad2", "connected", JOYPAD_FN(1, connected));
  2815. env.add_module_function("Pad2", "num_buttons", JOYPAD_FN(1, num_buttons));
  2816. env.add_module_function("Pad2", "num_axes", JOYPAD_FN(1, num_axes));
  2817. env.add_module_function("Pad2", "pressed", JOYPAD_FN(1, pressed));
  2818. env.add_module_function("Pad2", "released", JOYPAD_FN(1, released));
  2819. env.add_module_function("Pad2", "any_pressed", JOYPAD_FN(1, any_pressed));
  2820. env.add_module_function("Pad2", "any_released", JOYPAD_FN(1, any_released));
  2821. env.add_module_function("Pad2", "axis", JOYPAD_FN(1, axis));
  2822. env.add_module_function("Pad2", "button_name", JOYPAD_FN(1, button_name));
  2823. env.add_module_function("Pad2", "axis_name", JOYPAD_FN(1, axis_name));
  2824. env.add_module_function("Pad2", "button_id", JOYPAD_FN(1, button_id));
  2825. env.add_module_function("Pad2", "axis_id", JOYPAD_FN(1, axis_id));
  2826. env.add_module_function("Pad3", "name", JOYPAD_FN(2, name));
  2827. env.add_module_function("Pad3", "connected", JOYPAD_FN(2, connected));
  2828. env.add_module_function("Pad3", "num_buttons", JOYPAD_FN(2, num_buttons));
  2829. env.add_module_function("Pad3", "num_axes", JOYPAD_FN(2, num_axes));
  2830. env.add_module_function("Pad3", "pressed", JOYPAD_FN(2, pressed));
  2831. env.add_module_function("Pad3", "released", JOYPAD_FN(2, released));
  2832. env.add_module_function("Pad3", "any_pressed", JOYPAD_FN(2, any_pressed));
  2833. env.add_module_function("Pad3", "any_released", JOYPAD_FN(2, any_released));
  2834. env.add_module_function("Pad3", "axis", JOYPAD_FN(2, axis));
  2835. env.add_module_function("Pad3", "button_name", JOYPAD_FN(2, button_name));
  2836. env.add_module_function("Pad3", "axis_name", JOYPAD_FN(2, axis_name));
  2837. env.add_module_function("Pad3", "button_id", JOYPAD_FN(2, button_id));
  2838. env.add_module_function("Pad3", "axis_id", JOYPAD_FN(2, axis_id));
  2839. env.add_module_function("Pad4", "name", JOYPAD_FN(3, name));
  2840. env.add_module_function("Pad4", "connected", JOYPAD_FN(3, connected));
  2841. env.add_module_function("Pad4", "num_buttons", JOYPAD_FN(3, num_buttons));
  2842. env.add_module_function("Pad4", "num_axes", JOYPAD_FN(3, num_axes));
  2843. env.add_module_function("Pad4", "pressed", JOYPAD_FN(3, pressed));
  2844. env.add_module_function("Pad4", "released", JOYPAD_FN(3, released));
  2845. env.add_module_function("Pad4", "any_pressed", JOYPAD_FN(3, any_pressed));
  2846. env.add_module_function("Pad4", "any_released", JOYPAD_FN(3, any_released));
  2847. env.add_module_function("Pad4", "axis", JOYPAD_FN(3, axis));
  2848. env.add_module_function("Pad4", "button_name", JOYPAD_FN(3, button_name));
  2849. env.add_module_function("Pad4", "axis_name", JOYPAD_FN(3, axis_name));
  2850. env.add_module_function("Pad4", "button_id", JOYPAD_FN(3, button_id));
  2851. env.add_module_function("Pad4", "axis_id", JOYPAD_FN(3, axis_id));
  2852. env.add_module_function("World", "spawn_unit", world_spawn_unit);
  2853. env.add_module_function("World", "spawn_empty_unit", world_spawn_empty_unit);
  2854. env.add_module_function("World", "destroy_unit", world_destroy_unit);
  2855. env.add_module_function("World", "num_units", world_num_units);
  2856. env.add_module_function("World", "units", world_units);
  2857. env.add_module_function("World", "camera", world_camera);
  2858. env.add_module_function("World", "set_camera_projection_type", camera_set_projection_type);
  2859. env.add_module_function("World", "camera_projection_type", camera_projection_type);
  2860. env.add_module_function("World", "camera_fov", camera_fov);
  2861. env.add_module_function("World", "set_camera_fov", camera_set_fov);
  2862. env.add_module_function("World", "camera_aspect", camera_aspect);
  2863. env.add_module_function("World", "set_camera_aspect", camera_set_aspect);
  2864. env.add_module_function("World", "camera_near_clip_distance", camera_near_clip_distance);
  2865. env.add_module_function("World", "set_camera_near_clip_distance", camera_set_near_clip_distance);
  2866. env.add_module_function("World", "camera_far_clip_distance", camera_far_clip_distance);
  2867. env.add_module_function("World", "set_camera_far_clip_distance", camera_set_far_clip_distance);
  2868. env.add_module_function("World", "set_camera_orthographic_metrics", camera_set_orthographic_metrics);
  2869. env.add_module_function("World", "camera_screen_to_world", camera_screen_to_world);
  2870. env.add_module_function("World", "camera_world_to_screen", camera_world_to_screen);
  2871. env.add_module_function("World", "update_animations", world_update_animations);
  2872. env.add_module_function("World", "update_scene", world_update_scene);
  2873. env.add_module_function("World", "update", world_update);
  2874. env.add_module_function("World", "play_sound", world_play_sound);
  2875. env.add_module_function("World", "stop_sound", world_stop_sound);
  2876. env.add_module_function("World", "link_sound", world_link_sound);
  2877. env.add_module_function("World", "set_listener_pose", world_set_listener_pose);
  2878. env.add_module_function("World", "set_sound_position", world_set_sound_position);
  2879. env.add_module_function("World", "set_sound_range", world_set_sound_range);
  2880. env.add_module_function("World", "set_sound_volume", world_set_sound_volume);
  2881. env.add_module_function("World", "create_debug_line", world_create_debug_line);
  2882. env.add_module_function("World", "destroy_debug_line", world_destroy_debug_line);
  2883. env.add_module_function("World", "create_screen_gui", world_create_screen_gui);
  2884. env.add_module_function("World", "destroy_gui", world_destroy_gui);
  2885. env.add_module_function("World", "load_level", world_load_level);
  2886. env.add_module_function("World", "scene_graph", world_scene_graph);
  2887. env.add_module_function("World", "render_world", world_render_world);
  2888. env.add_module_function("World", "physics_world", world_physics_world);
  2889. env.add_module_function("World", "sound_world", world_sound_world);
  2890. env.add_module_function("World", "__index", "World");
  2891. env.add_module_function("World", "__tostring", world_tostring);
  2892. env.add_module_function("SceneGraph", "create", scene_graph_create);
  2893. env.add_module_function("SceneGraph", "destroy", scene_graph_destroy);
  2894. env.add_module_function("SceneGraph", "transform_instances", scene_graph_transform_instances);
  2895. env.add_module_function("SceneGraph", "local_position", scene_graph_local_position);
  2896. env.add_module_function("SceneGraph", "local_rotation", scene_graph_local_rotation);
  2897. env.add_module_function("SceneGraph", "local_scale", scene_graph_local_scale);
  2898. env.add_module_function("SceneGraph", "local_pose", scene_graph_local_pose);
  2899. env.add_module_function("SceneGraph", "world_position", scene_graph_world_position);
  2900. env.add_module_function("SceneGraph", "world_rotation", scene_graph_world_rotation);
  2901. env.add_module_function("SceneGraph", "world_pose", scene_graph_world_pose);
  2902. env.add_module_function("SceneGraph", "set_local_position", scene_graph_set_local_position);
  2903. env.add_module_function("SceneGraph", "set_local_rotation", scene_graph_set_local_rotation);
  2904. env.add_module_function("SceneGraph", "set_local_scale", scene_graph_set_local_scale);
  2905. env.add_module_function("SceneGraph", "set_local_pose", scene_graph_set_local_pose);
  2906. env.add_module_function("SceneGraph", "link", scene_graph_link);
  2907. env.add_module_function("SceneGraph", "unlink", scene_graph_unlink);
  2908. env.add_module_function("UnitManager", "create", unit_manager_create);
  2909. env.add_module_function("UnitManager", "alive", unit_manager_alive);
  2910. env.add_module_function("RenderWorld", "create_mesh", render_world_create_mesh);
  2911. env.add_module_function("RenderWorld", "destroy_mesh", render_world_destroy_mesh);
  2912. env.add_module_function("RenderWorld", "mesh_instances", render_world_mesh_instances);
  2913. env.add_module_function("RenderWorld", "mesh_obb", render_world_mesh_obb);
  2914. env.add_module_function("RenderWorld", "set_mesh_visible", render_world_set_mesh_visible);
  2915. env.add_module_function("RenderWorld", "create_sprite", render_world_create_sprite);
  2916. env.add_module_function("RenderWorld", "destroy_sprite", render_world_destroy_sprite);
  2917. env.add_module_function("RenderWorld", "sprite_instances", render_world_sprite_instances);
  2918. env.add_module_function("RenderWorld", "set_sprite_frame", render_world_set_sprite_frame);
  2919. env.add_module_function("RenderWorld", "set_sprite_visible", render_world_set_sprite_visible);
  2920. env.add_module_function("RenderWorld", "create_light", render_world_create_light);
  2921. env.add_module_function("RenderWorld", "destroy_light", render_world_destroy_light);
  2922. env.add_module_function("RenderWorld", "light_instances", render_world_light_instances);
  2923. env.add_module_function("RenderWorld", "light_type", render_world_light_type);
  2924. env.add_module_function("RenderWorld", "light_color", render_world_light_color);
  2925. env.add_module_function("RenderWorld", "light_range", render_world_light_range);
  2926. env.add_module_function("RenderWorld", "light_intensity", render_world_light_intensity);
  2927. env.add_module_function("RenderWorld", "light_spot_angle", render_world_light_spot_angle);
  2928. env.add_module_function("RenderWorld", "set_light_type", render_world_set_light_type);
  2929. env.add_module_function("RenderWorld", "set_light_color", render_world_set_light_color);
  2930. env.add_module_function("RenderWorld", "set_light_range", render_world_set_light_range);
  2931. env.add_module_function("RenderWorld", "set_light_intensity", render_world_set_light_intensity);
  2932. env.add_module_function("RenderWorld", "set_light_spot_angle", render_world_set_light_spot_angle);
  2933. env.add_module_function("RenderWorld", "enable_debug_drawing", render_world_enable_debug_drawing);
  2934. env.add_module_function("PhysicsWorld", "actor_instances", physics_world_actor_instances);
  2935. env.add_module_function("PhysicsWorld", "actor_world_position", physics_world_actor_world_position);
  2936. env.add_module_function("PhysicsWorld", "actor_world_rotation", physics_world_actor_world_rotation);
  2937. env.add_module_function("PhysicsWorld", "actor_world_pose", physics_world_actor_world_pose);
  2938. env.add_module_function("PhysicsWorld", "teleport_actor_world_position", physics_world_teleport_actor_world_position);
  2939. env.add_module_function("PhysicsWorld", "teleport_actor_world_rotation", physics_world_teleport_actor_world_rotation);
  2940. env.add_module_function("PhysicsWorld", "teleport_actor_world_pose", physics_world_teleport_actor_world_pose);
  2941. env.add_module_function("PhysicsWorld", "actor_center_of_mass", physics_world_actor_center_of_mass);
  2942. env.add_module_function("PhysicsWorld", "enable_actor_gravity", physics_world_enable_actor_gravity);
  2943. env.add_module_function("PhysicsWorld", "disable_actor_gravity", physics_world_disable_actor_gravity);
  2944. env.add_module_function("PhysicsWorld", "enable_actor_collision", physics_world_enable_actor_collision);
  2945. env.add_module_function("PhysicsWorld", "disable_actor_collision", physics_world_disable_actor_collision);
  2946. env.add_module_function("PhysicsWorld", "set_actor_collision_filter", physics_world_set_actor_collision_filter);
  2947. env.add_module_function("PhysicsWorld", "set_actor_kinematic", physics_world_set_actor_kinematic);
  2948. env.add_module_function("PhysicsWorld", "move_actor", physics_world_move_actor);
  2949. env.add_module_function("PhysicsWorld", "is_static", physics_world_is_static);
  2950. env.add_module_function("PhysicsWorld", "is_dynamic", physics_world_is_dynamic);
  2951. env.add_module_function("PhysicsWorld", "is_kinematic", physics_world_is_kinematic);
  2952. env.add_module_function("PhysicsWorld", "is_nonkinematic", physics_world_is_nonkinematic);
  2953. env.add_module_function("PhysicsWorld", "actor_linear_damping", physics_world_actor_linear_damping);
  2954. env.add_module_function("PhysicsWorld", "set_actor_linear_damping", physics_world_set_actor_linear_damping);
  2955. env.add_module_function("PhysicsWorld", "actor_angular_damping", physics_world_actor_angular_damping);
  2956. env.add_module_function("PhysicsWorld", "set_actor_angular_damping", physics_world_set_actor_angular_damping);
  2957. env.add_module_function("PhysicsWorld", "actor_linear_velocity", physics_world_actor_linear_velocity);
  2958. env.add_module_function("PhysicsWorld", "set_actor_linear_velocity", physics_world_set_actor_linear_velocity);
  2959. env.add_module_function("PhysicsWorld", "actor_angular_velocity", physics_world_actor_angular_velocity);
  2960. env.add_module_function("PhysicsWorld", "set_actor_angular_velocity", physics_world_set_actor_angular_velocity);
  2961. env.add_module_function("PhysicsWorld", "add_actor_impulse", physics_world_add_actor_impulse);
  2962. env.add_module_function("PhysicsWorld", "add_actor_impulse_at", physics_world_add_actor_impulse_at);
  2963. env.add_module_function("PhysicsWorld", "add_actor_torque_impulse", physics_world_add_actor_torque_impulse);
  2964. env.add_module_function("PhysicsWorld", "push_actor", physics_world_push_actor);
  2965. env.add_module_function("PhysicsWorld", "push_actor_at", physics_world_push_actor_at);
  2966. env.add_module_function("PhysicsWorld", "is_sleeping", physics_world_is_sleeping);
  2967. env.add_module_function("PhysicsWorld", "wake_up", physics_world_wake_up);
  2968. env.add_module_function("PhysicsWorld", "controller_instances", physics_world_controller_instances);
  2969. env.add_module_function("PhysicsWorld", "move_controller", physics_world_move_controller);
  2970. env.add_module_function("PhysicsWorld", "create_joint", physics_world_create_joint);
  2971. env.add_module_function("PhysicsWorld", "gravity", physics_world_gravity);
  2972. env.add_module_function("PhysicsWorld", "set_gravity", physics_world_set_gravity);
  2973. env.add_module_function("PhysicsWorld", "raycast", physics_world_raycast);
  2974. env.add_module_function("PhysicsWorld", "enable_debug_drawing", physics_world_enable_debug_drawing);
  2975. env.add_module_function("PhysicsWorld", "__index", "PhysicsWorld");
  2976. env.add_module_function("PhysicsWorld", "__tostring", physics_world_tostring);
  2977. env.add_module_function("SoundWorld", "stop_all", sound_world_stop_all);
  2978. env.add_module_function("SoundWorld", "pause_all", sound_world_pause_all);
  2979. env.add_module_function("SoundWorld", "resume_all", sound_world_resume_all);
  2980. env.add_module_function("SoundWorld", "is_playing", sound_world_is_playing);
  2981. env.add_module_function("SoundWorld", "__index", "SoundWorld");
  2982. env.add_module_function("SoundWorld", "__tostring", sound_world_tostring);
  2983. env.add_module_function("Device", "argv", device_argv);
  2984. env.add_module_function("Device", "platform", device_platform);
  2985. env.add_module_function("Device", "architecture", device_architecture);
  2986. env.add_module_function("Device", "version", device_version);
  2987. env.add_module_function("Device", "last_delta_time", device_last_delta_time);
  2988. env.add_module_function("Device", "quit", device_quit);
  2989. env.add_module_function("Device", "resolution", device_resolution);
  2990. env.add_module_function("Device", "create_world", device_create_world);
  2991. env.add_module_function("Device", "destroy_world", device_destroy_world);
  2992. env.add_module_function("Device", "render", device_render);
  2993. env.add_module_function("Device", "create_resource_package", device_create_resource_package);
  2994. env.add_module_function("Device", "destroy_resource_package", device_destroy_resource_package);
  2995. env.add_module_function("Device", "console_send", device_console_send);
  2996. env.add_module_function("Device", "can_get", device_can_get);
  2997. env.add_module_function("Device", "enable_resource_autoload", device_enable_resource_autoload);
  2998. env.add_module_function("Device", "temp_count", device_temp_count);
  2999. env.add_module_function("Device", "set_temp_count", device_set_temp_count);
  3000. env.add_module_function("Profiler", "enter_scope", profiler_enter_scope);
  3001. env.add_module_function("Profiler", "leave_scope", profiler_leave_scope);
  3002. env.add_module_function("Profiler", "record", profiler_record);
  3003. env.add_module_function("DebugLine", "add_line", debug_line_add_line);
  3004. env.add_module_function("DebugLine", "add_axes", debug_line_add_axes);
  3005. env.add_module_function("DebugLine", "add_circle", debug_line_add_circle);
  3006. env.add_module_function("DebugLine", "add_cone", debug_line_add_cone);
  3007. env.add_module_function("DebugLine", "add_sphere", debug_line_add_sphere);
  3008. env.add_module_function("DebugLine", "add_obb", debug_line_add_obb);
  3009. env.add_module_function("DebugLine", "add_unit", debug_line_add_unit);
  3010. env.add_module_function("DebugLine", "reset", debug_line_reset);
  3011. env.add_module_function("DebugLine", "submit", debug_line_submit);
  3012. env.add_module_function("DebugLine", "__index", "DebugLine");
  3013. env.add_module_function("DebugLine", "__tostring", debug_line_tostring);
  3014. env.add_module_function("ResourcePackage", "load", resource_package_load);
  3015. env.add_module_function("ResourcePackage", "unload", resource_package_unload);
  3016. env.add_module_function("ResourcePackage", "flush", resource_package_flush);
  3017. env.add_module_function("ResourcePackage", "has_loaded", resource_package_has_loaded);
  3018. env.add_module_function("ResourcePackage", "__index", "ResourcePackage");
  3019. env.add_module_function("ResourcePackage", "__tostring", resource_package_tostring);
  3020. env.add_module_function("Material", "set_float", material_set_float);
  3021. env.add_module_function("Material", "set_vector2", material_set_vector2);
  3022. env.add_module_function("Material", "set_vector3", material_set_vector3);
  3023. env.add_module_function("Gui", "resolution", gui_resolution);
  3024. env.add_module_function("Gui", "move", gui_move);
  3025. env.add_module_function("Gui", "screen_to_gui", gui_screen_to_gui);
  3026. env.add_module_function("Gui", "rect", gui_rect);
  3027. env.add_module_function("Gui", "image", gui_image);
  3028. env.add_module_function("Gui", "image_uv", gui_image_uv);
  3029. env.add_module_function("Gui", "text", gui_text);
  3030. env.add_module_function("Display", "modes", display_modes);
  3031. env.add_module_function("Display", "set_mode", display_set_mode);
  3032. env.add_module_function("Window", "show", window_show);
  3033. env.add_module_function("Window", "hide", window_hide);
  3034. env.add_module_function("Window", "resize", window_resize);
  3035. env.add_module_function("Window", "move", window_move);
  3036. env.add_module_function("Window", "minimize", window_minimize);
  3037. env.add_module_function("Window", "restore", window_restore);
  3038. env.add_module_function("Window", "title", window_title);
  3039. env.add_module_function("Window", "set_title", window_set_title);
  3040. env.add_module_function("Window", "show_cursor", window_show_cursor);
  3041. }
  3042. } // namespace crown