lua_api.cpp 98 KB

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