lua_api.cpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "color4.h"
  6. #include "console_server.h"
  7. #include "debug_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 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 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 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& id)
  918. {
  919. LuaStack stack(L);
  920. stack.push_string(id.name());
  921. return 1;
  922. }
  923. static int input_device_connected(lua_State* L, InputDevice& id)
  924. {
  925. LuaStack stack(L);
  926. stack.push_bool(id.connected());
  927. return 1;
  928. }
  929. static int input_device_num_buttons(lua_State* L, InputDevice& id)
  930. {
  931. LuaStack stack(L);
  932. stack.push_int(id.num_buttons());
  933. return 1;
  934. }
  935. static int input_device_num_axes(lua_State* L, InputDevice& id)
  936. {
  937. LuaStack stack(L);
  938. stack.push_int(id.num_axes());
  939. return 1;
  940. }
  941. static int input_device_pressed(lua_State* L, InputDevice& id)
  942. {
  943. LuaStack stack(L);
  944. stack.push_bool(id.pressed(stack.get_int(1)));
  945. return 1;
  946. }
  947. static int input_device_released(lua_State* L, InputDevice& id)
  948. {
  949. LuaStack stack(L);
  950. stack.push_bool(id.released(stack.get_int(1)));
  951. return 1;
  952. }
  953. static int input_device_any_pressed(lua_State* L, InputDevice& id)
  954. {
  955. LuaStack stack(L);
  956. stack.push_bool(id.any_pressed());
  957. return 1;
  958. }
  959. static int input_device_any_released(lua_State* L, InputDevice& id)
  960. {
  961. LuaStack stack(L);
  962. stack.push_bool(id.any_released());
  963. return 1;
  964. }
  965. static int input_device_axis(lua_State* L, InputDevice& id)
  966. {
  967. LuaStack stack(L);
  968. stack.push_vector3(id.axis(stack.get_int(1)));
  969. return 1;
  970. }
  971. static int input_device_button_name(lua_State* L, InputDevice& id)
  972. {
  973. LuaStack stack(L);
  974. stack.push_string(id.button_name(stack.get_int(1)));
  975. return 1;
  976. }
  977. static int input_device_axis_name(lua_State* L, InputDevice& id)
  978. {
  979. LuaStack stack(L);
  980. stack.push_string(id.axis_name(stack.get_int(1)));
  981. return 1;
  982. }
  983. static int input_device_button_id(lua_State* L, InputDevice& id)
  984. {
  985. LuaStack stack(L);
  986. stack.push_int(id.button_id(stack.get_string_id_32(1)));
  987. return 1;
  988. }
  989. static int input_device_axis_id(lua_State* L, InputDevice& id)
  990. {
  991. LuaStack stack(L);
  992. stack.push_int(id.axis_id(stack.get_string_id_32(1)));
  993. return 1;
  994. }
  995. #define KEYBOARD_FN(name) keyboard_##name
  996. #define MOUSE_FN(name) mouse_##name
  997. #define TOUCH_FN(name) touch_##name
  998. #define JOYPAD_FN(index, name) joypad_##name##index
  999. #define KEYBOARD(name) static int KEYBOARD_FN(name)(lua_State* L)\
  1000. { return input_device_##name(L, *device()->input_manager()->keyboard()); }
  1001. #define MOUSE(name) static int MOUSE_FN(name)(lua_State* L)\
  1002. { return input_device_##name(L, *device()->input_manager()->mouse()); }
  1003. #define TOUCH(name) static int TOUCH_FN(name)(lua_State* L)\
  1004. { return input_device_##name(L, *device()->input_manager()->touch()); }
  1005. #define JOYPAD(index, name) static int JOYPAD_FN(index, name)(lua_State* L)\
  1006. { return input_device_##name(L, *device()->input_manager()->joypad(index)); }
  1007. KEYBOARD(name)
  1008. KEYBOARD(connected)
  1009. KEYBOARD(num_buttons)
  1010. KEYBOARD(num_axes)
  1011. KEYBOARD(pressed)
  1012. KEYBOARD(released)
  1013. KEYBOARD(any_pressed)
  1014. KEYBOARD(any_released)
  1015. // KEYBOARD(axis)
  1016. KEYBOARD(button_name)
  1017. // KEYBOARD(axis_name)
  1018. KEYBOARD(button_id)
  1019. // KEYBOARD(axis_id)
  1020. MOUSE(name)
  1021. MOUSE(connected)
  1022. MOUSE(num_buttons)
  1023. MOUSE(num_axes)
  1024. MOUSE(pressed)
  1025. MOUSE(released)
  1026. MOUSE(any_pressed)
  1027. MOUSE(any_released)
  1028. MOUSE(axis)
  1029. MOUSE(button_name)
  1030. MOUSE(axis_name)
  1031. MOUSE(button_id)
  1032. MOUSE(axis_id)
  1033. TOUCH(name)
  1034. TOUCH(connected)
  1035. TOUCH(num_buttons)
  1036. TOUCH(num_axes)
  1037. TOUCH(pressed)
  1038. TOUCH(released)
  1039. TOUCH(any_pressed)
  1040. TOUCH(any_released)
  1041. TOUCH(axis)
  1042. TOUCH(button_name)
  1043. TOUCH(axis_name)
  1044. TOUCH(button_id)
  1045. TOUCH(axis_id)
  1046. JOYPAD(0, name)
  1047. JOYPAD(0, connected)
  1048. JOYPAD(0, num_buttons)
  1049. JOYPAD(0, num_axes)
  1050. JOYPAD(0, pressed)
  1051. JOYPAD(0, released)
  1052. JOYPAD(0, any_pressed)
  1053. JOYPAD(0, any_released)
  1054. JOYPAD(0, axis)
  1055. JOYPAD(0, button_name)
  1056. JOYPAD(0, axis_name)
  1057. JOYPAD(0, button_id)
  1058. JOYPAD(0, axis_id)
  1059. JOYPAD(1, name)
  1060. JOYPAD(1, connected)
  1061. JOYPAD(1, num_buttons)
  1062. JOYPAD(1, num_axes)
  1063. JOYPAD(1, pressed)
  1064. JOYPAD(1, released)
  1065. JOYPAD(1, any_pressed)
  1066. JOYPAD(1, any_released)
  1067. JOYPAD(1, axis)
  1068. JOYPAD(1, button_name)
  1069. JOYPAD(1, axis_name)
  1070. JOYPAD(1, button_id)
  1071. JOYPAD(1, axis_id)
  1072. JOYPAD(2, name)
  1073. JOYPAD(2, connected)
  1074. JOYPAD(2, num_buttons)
  1075. JOYPAD(2, num_axes)
  1076. JOYPAD(2, pressed)
  1077. JOYPAD(2, released)
  1078. JOYPAD(2, any_pressed)
  1079. JOYPAD(2, any_released)
  1080. JOYPAD(2, axis)
  1081. JOYPAD(2, button_name)
  1082. JOYPAD(2, axis_name)
  1083. JOYPAD(2, button_id)
  1084. JOYPAD(2, axis_id)
  1085. JOYPAD(3, name)
  1086. JOYPAD(3, connected)
  1087. JOYPAD(3, num_buttons)
  1088. JOYPAD(3, num_axes)
  1089. JOYPAD(3, pressed)
  1090. JOYPAD(3, released)
  1091. JOYPAD(3, any_pressed)
  1092. JOYPAD(3, any_released)
  1093. JOYPAD(3, axis)
  1094. JOYPAD(3, button_name)
  1095. JOYPAD(3, axis_name)
  1096. JOYPAD(3, button_id)
  1097. JOYPAD(3, axis_id)
  1098. static int world_spawn_unit(lua_State* L)
  1099. {
  1100. LuaStack stack(L);
  1101. const int nargs = stack.num_args();
  1102. const StringId64 name = stack.get_resource_id(2);
  1103. const Vector3& pos = nargs > 2 ? stack.get_vector3(3) : VECTOR3_ZERO;
  1104. const Quaternion& rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
  1105. LUA_ASSERT(device()->resource_manager()->can_get(RESOURCE_TYPE_UNIT, name), stack, "Unit not found");
  1106. stack.push_unit(stack.get_world(1)->spawn_unit(name, pos, rot));
  1107. return 1;
  1108. }
  1109. static int world_spawn_empty_unit(lua_State* L)
  1110. {
  1111. LuaStack stack(L);
  1112. stack.push_unit(stack.get_world(1)->spawn_empty_unit());
  1113. return 1;
  1114. }
  1115. static int world_destroy_unit(lua_State* L)
  1116. {
  1117. LuaStack stack(L);
  1118. stack.get_world(1)->destroy_unit(stack.get_unit(2));
  1119. return 0;
  1120. }
  1121. static int world_num_units(lua_State* L)
  1122. {
  1123. LuaStack stack(L);
  1124. stack.push_int(stack.get_world(1)->num_units());
  1125. return 1;
  1126. }
  1127. static int world_units(lua_State* L)
  1128. {
  1129. LuaStack stack(L);
  1130. TempAllocator1024 alloc;
  1131. Array<UnitId> units(alloc);
  1132. stack.get_world(1)->units(units);
  1133. const u32 num = array::size(units);
  1134. stack.push_table(num);
  1135. for (u32 i = 0; i < num; ++i)
  1136. {
  1137. stack.push_key_begin((s32) i + 1);
  1138. stack.push_unit(units[i]);
  1139. stack.push_key_end();
  1140. }
  1141. return 1;
  1142. }
  1143. static int world_camera(lua_State* L)
  1144. {
  1145. LuaStack stack(L);
  1146. stack.push_camera(stack.get_world(1)->camera(stack.get_unit(2)));
  1147. return 1;
  1148. }
  1149. static int camera_set_projection_type(lua_State* L)
  1150. {
  1151. LuaStack stack(L);
  1152. const char* name = stack.get_string(3);
  1153. const ProjectionType::Enum pt = name_to_projection_type(name);
  1154. LUA_ASSERT(pt != ProjectionType::COUNT, stack, "Unknown projection type: '%s'", name);
  1155. stack.get_world(1)->camera_set_projection_type(stack.get_camera(2), pt);
  1156. return 0;
  1157. }
  1158. static int camera_projection_type(lua_State* L)
  1159. {
  1160. LuaStack stack(L);
  1161. ProjectionType::Enum type = stack.get_world(1)->camera_projection_type(stack.get_camera(2));
  1162. stack.push_string(s_projection[type].name);
  1163. return 1;
  1164. }
  1165. static int camera_fov(lua_State* L)
  1166. {
  1167. LuaStack stack(L);
  1168. stack.push_float(stack.get_world(1)->camera_fov(stack.get_camera(2)));
  1169. return 1;
  1170. }
  1171. static int camera_set_fov(lua_State* L)
  1172. {
  1173. LuaStack stack(L);
  1174. stack.get_world(1)->camera_set_fov(stack.get_camera(2), stack.get_float(3));
  1175. return 0;
  1176. }
  1177. static int camera_near_clip_distance(lua_State* L)
  1178. {
  1179. LuaStack stack(L);
  1180. stack.push_float(stack.get_world(1)->camera_near_clip_distance(stack.get_camera(2)));
  1181. return 1;
  1182. }
  1183. static int camera_set_near_clip_distance(lua_State* L)
  1184. {
  1185. LuaStack stack(L);
  1186. stack.get_world(1)->camera_set_near_clip_distance(stack.get_camera(2), stack.get_float(3));
  1187. return 0;
  1188. }
  1189. static int camera_far_clip_distance(lua_State* L)
  1190. {
  1191. LuaStack stack(L);
  1192. stack.push_float(stack.get_world(1)->camera_far_clip_distance(stack.get_camera(2)));
  1193. return 1;
  1194. }
  1195. static int camera_set_far_clip_distance(lua_State* L)
  1196. {
  1197. LuaStack stack(L);
  1198. stack.get_world(1)->camera_set_far_clip_distance(stack.get_camera(2), stack.get_float(3));
  1199. return 0;
  1200. }
  1201. static int camera_set_orthographic_metrics(lua_State* L)
  1202. {
  1203. LuaStack stack(L);
  1204. stack.get_world(1)->camera_set_orthographic_metrics(stack.get_camera(2), stack.get_float(3), stack.get_float(4),
  1205. stack.get_float(5), stack.get_float(6));
  1206. return 0;
  1207. }
  1208. static int camera_screen_to_world(lua_State* L)
  1209. {
  1210. LuaStack stack(L);
  1211. stack.push_vector3(stack.get_world(1)->camera_screen_to_world(stack.get_camera(2), stack.get_vector3(3)));
  1212. return 1;
  1213. }
  1214. static int camera_world_to_screen(lua_State* L)
  1215. {
  1216. LuaStack stack(L);
  1217. stack.push_vector3(stack.get_world(1)->camera_world_to_screen(stack.get_camera(2), stack.get_vector3(3)));
  1218. return 1;
  1219. }
  1220. static int world_update_animations(lua_State* L)
  1221. {
  1222. LuaStack stack(L);
  1223. stack.get_world(1)->update_animations(stack.get_float(2));
  1224. return 0;
  1225. }
  1226. static int world_update_scene(lua_State* L)
  1227. {
  1228. LuaStack stack(L);
  1229. stack.get_world(1)->update_scene(stack.get_float(2));
  1230. return 0;
  1231. }
  1232. static int world_update(lua_State* L)
  1233. {
  1234. LuaStack stack(L);
  1235. stack.get_world(1)->update(stack.get_float(2));
  1236. return 0;
  1237. }
  1238. static int world_play_sound(lua_State* L)
  1239. {
  1240. LuaStack stack(L);
  1241. const s32 nargs = stack.num_args();
  1242. World* world = stack.get_world(1);
  1243. const StringId64 name = stack.get_resource_id(2);
  1244. const bool loop = nargs > 2 ? stack.get_bool(3) : false;
  1245. const f32 volume = nargs > 3 ? stack.get_float(4) : 1.0f;
  1246. const Vector3& pos = nargs > 4 ? stack.get_vector3(5) : VECTOR3_ZERO;
  1247. const f32 range = nargs > 5 ? stack.get_float(6) : 1000.0f;
  1248. LUA_ASSERT(device()->resource_manager()->can_get(RESOURCE_TYPE_SOUND, name), stack, "Sound not found");
  1249. stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
  1250. return 1;
  1251. }
  1252. static int world_stop_sound(lua_State* L)
  1253. {
  1254. LuaStack stack(L);
  1255. stack.get_world(1)->stop_sound(stack.get_sound_instance_id(2));
  1256. return 0;
  1257. }
  1258. static int world_link_sound(lua_State* L)
  1259. {
  1260. LuaStack stack(L);
  1261. stack.get_world(1)->link_sound(stack.get_sound_instance_id(2)
  1262. , stack.get_unit(3)
  1263. , stack.get_int(4)
  1264. );
  1265. return 0;
  1266. }
  1267. static int world_set_listener_pose(lua_State* L)
  1268. {
  1269. LuaStack stack(L);
  1270. stack.get_world(1)->set_listener_pose(stack.get_matrix4x4(2));
  1271. return 0;
  1272. }
  1273. static int world_set_sound_position(lua_State* L)
  1274. {
  1275. LuaStack stack(L);
  1276. stack.get_world(1)->set_sound_position(stack.get_sound_instance_id(2), stack.get_vector3(3));
  1277. return 0;
  1278. }
  1279. static int world_set_sound_range(lua_State* L)
  1280. {
  1281. LuaStack stack(L);
  1282. stack.get_world(1)->set_sound_range(stack.get_sound_instance_id(2), stack.get_float(3));
  1283. return 0;
  1284. }
  1285. static int world_set_sound_volume(lua_State* L)
  1286. {
  1287. LuaStack stack(L);
  1288. stack.get_world(1)->set_sound_volume(stack.get_sound_instance_id(2), stack.get_float(3));
  1289. return 0;
  1290. }
  1291. static int world_create_debug_line(lua_State* L)
  1292. {
  1293. LuaStack stack(L);
  1294. stack.push_debug_line(stack.get_world(1)->create_debug_line(stack.get_bool(2)));
  1295. return 1;
  1296. }
  1297. static int world_destroy_debug_line(lua_State* L)
  1298. {
  1299. LuaStack stack(L);
  1300. stack.get_world(1)->destroy_debug_line(*stack.get_debug_line(2));
  1301. return 0;
  1302. }
  1303. static int world_create_screen_debug_gui(lua_State* L)
  1304. {
  1305. LuaStack stack(L);
  1306. stack.push_debug_gui(stack.get_world(1)->create_screen_debug_gui(stack.get_float(2), stack.get_float(3)));
  1307. return 1;
  1308. }
  1309. static int world_destroy_gui(lua_State* L)
  1310. {
  1311. LuaStack stack(L);
  1312. stack.get_world(1)->destroy_gui(*stack.get_debug_gui(2));
  1313. return 0;
  1314. }
  1315. static int world_load_level(lua_State* L)
  1316. {
  1317. LuaStack stack(L);
  1318. const int nargs = stack.num_args();
  1319. const StringId64 name = stack.get_resource_id(2);
  1320. const Vector3& pos = nargs > 2 ? stack.get_vector3(3) : VECTOR3_ZERO;
  1321. const Quaternion& rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
  1322. LUA_ASSERT(device()->resource_manager()->can_get(RESOURCE_TYPE_LEVEL, name), stack, "Level not found");
  1323. stack.push_level(stack.get_world(1)->load_level(name, pos, rot));
  1324. return 1;
  1325. }
  1326. static int world_scene_graph(lua_State* L)
  1327. {
  1328. LuaStack stack(L);
  1329. stack.push_scene_graph(stack.get_world(1)->scene_graph());
  1330. return 1;
  1331. }
  1332. static int world_render_world(lua_State* L)
  1333. {
  1334. LuaStack stack(L);
  1335. stack.push_render_world(stack.get_world(1)->render_world());
  1336. return 1;
  1337. }
  1338. static int world_physics_world(lua_State* L)
  1339. {
  1340. LuaStack stack(L);
  1341. stack.push_physics_world(stack.get_world(1)->physics_world());
  1342. return 1;
  1343. }
  1344. static int world_sound_world(lua_State* L)
  1345. {
  1346. LuaStack stack(L);
  1347. stack.push_sound_world(stack.get_world(1)->sound_world());
  1348. return 1;
  1349. }
  1350. static int world_tostring(lua_State* L)
  1351. {
  1352. LuaStack stack(L);
  1353. World* w = stack.get_world(1);
  1354. stack.push_fstring("World (%p)", w);
  1355. return 1;
  1356. }
  1357. static int scene_graph_create(lua_State* L)
  1358. {
  1359. LuaStack stack(L);
  1360. TransformInstance ti = stack.get_scene_graph(1)->create(stack.get_unit(2)
  1361. , stack.get_vector3(3)
  1362. , stack.get_quaternion(4)
  1363. , stack.get_vector3(5)
  1364. );
  1365. stack.push_transform(ti);
  1366. return 1;
  1367. }
  1368. static int scene_graph_destroy(lua_State* L)
  1369. {
  1370. LuaStack stack(L);
  1371. stack.get_scene_graph(1)->destroy(stack.get_transform(2));
  1372. return 0;
  1373. }
  1374. static int scene_graph_transform_instances(lua_State* L)
  1375. {
  1376. LuaStack stack(L);
  1377. SceneGraph* sg = stack.get_scene_graph(1);
  1378. TransformInstance ti = sg->get(stack.get_unit(2));
  1379. if (sg->is_valid(ti))
  1380. stack.push_transform(ti);
  1381. else
  1382. stack.push_nil();
  1383. return 1;
  1384. }
  1385. static int scene_graph_local_position(lua_State* L)
  1386. {
  1387. LuaStack stack(L);
  1388. stack.push_vector3(stack.get_scene_graph(1)->local_position(stack.get_transform(2)));
  1389. return 1;
  1390. }
  1391. static int scene_graph_local_rotation(lua_State* L)
  1392. {
  1393. LuaStack stack(L);
  1394. stack.push_quaternion(stack.get_scene_graph(1)->local_rotation(stack.get_transform(2)));
  1395. return 1;
  1396. }
  1397. static int scene_graph_local_scale(lua_State* L)
  1398. {
  1399. LuaStack stack(L);
  1400. stack.push_vector3(stack.get_scene_graph(1)->local_scale(stack.get_transform(2)));
  1401. return 1;
  1402. }
  1403. static int scene_graph_local_pose(lua_State* L)
  1404. {
  1405. LuaStack stack(L);
  1406. stack.push_matrix4x4(stack.get_scene_graph(1)->local_pose(stack.get_transform(2)));
  1407. return 1;
  1408. }
  1409. static int scene_graph_world_position(lua_State* L)
  1410. {
  1411. LuaStack stack(L);
  1412. stack.push_vector3(stack.get_scene_graph(1)->world_position(stack.get_transform(2)));
  1413. return 1;
  1414. }
  1415. static int scene_graph_world_rotation(lua_State* L)
  1416. {
  1417. LuaStack stack(L);
  1418. stack.push_quaternion(stack.get_scene_graph(1)->world_rotation(stack.get_transform(2)));
  1419. return 1;
  1420. }
  1421. static int scene_graph_world_pose(lua_State* L)
  1422. {
  1423. LuaStack stack(L);
  1424. stack.push_matrix4x4(stack.get_scene_graph(1)->world_pose(stack.get_transform(2)));
  1425. return 1;
  1426. }
  1427. static int scene_graph_set_local_position(lua_State* L)
  1428. {
  1429. LuaStack stack(L);
  1430. stack.get_scene_graph(1)->set_local_position(stack.get_transform(2), stack.get_vector3(3));
  1431. return 0;
  1432. }
  1433. static int scene_graph_set_local_rotation(lua_State* L)
  1434. {
  1435. LuaStack stack(L);
  1436. stack.get_scene_graph(1)->set_local_rotation(stack.get_transform(2), stack.get_quaternion(3));
  1437. return 0;
  1438. }
  1439. static int scene_graph_set_local_scale(lua_State* L)
  1440. {
  1441. LuaStack stack(L);
  1442. stack.get_scene_graph(1)->set_local_scale(stack.get_transform(2), stack.get_vector3(3));
  1443. return 0;
  1444. }
  1445. static int scene_graph_set_local_pose(lua_State* L)
  1446. {
  1447. LuaStack stack(L);
  1448. stack.get_scene_graph(1)->set_local_pose(stack.get_transform(2), stack.get_matrix4x4(3));
  1449. return 0;
  1450. }
  1451. static int scene_graph_link(lua_State* L)
  1452. {
  1453. LuaStack stack(L);
  1454. stack.get_scene_graph(1)->link(stack.get_transform(2), stack.get_transform(3));
  1455. return 0;
  1456. }
  1457. static int scene_graph_unlink(lua_State* L)
  1458. {
  1459. LuaStack stack(L);
  1460. stack.get_scene_graph(1)->unlink(stack.get_transform(2));
  1461. return 0;
  1462. }
  1463. static int unit_manager_create(lua_State* L)
  1464. {
  1465. LuaStack stack(L);
  1466. if (stack.num_args() == 1)
  1467. stack.push_unit(device()->unit_manager()->create(*stack.get_world(1)));
  1468. else
  1469. stack.push_unit(device()->unit_manager()->create());
  1470. return 1;
  1471. }
  1472. static int unit_manager_alive(lua_State* L)
  1473. {
  1474. LuaStack stack(L);
  1475. stack.push_bool(device()->unit_manager()->alive(stack.get_unit(1)));
  1476. return 1;
  1477. }
  1478. static int render_world_mesh_create(lua_State* L)
  1479. {
  1480. LuaStack stack(L);
  1481. RenderWorld* rw = stack.get_render_world(1);
  1482. UnitId unit = stack.get_unit(2);
  1483. MeshRendererDesc desc;
  1484. desc.mesh_resource = stack.get_resource_id(3);
  1485. desc.geometry_name = stack.get_string_id_32(4);
  1486. desc.material_resource = stack.get_resource_id(5);
  1487. desc.visible = stack.get_bool(6);
  1488. Matrix4x4 pose = stack.get_matrix4x4(7);
  1489. stack.push_mesh_instance(rw->mesh_create(unit, desc, pose));
  1490. return 1;
  1491. }
  1492. static int render_world_mesh_destroy(lua_State* L)
  1493. {
  1494. LuaStack stack(L);
  1495. stack.get_render_world(1)->mesh_destroy(stack.get_mesh_instance(2));
  1496. return 0;
  1497. }
  1498. static int render_world_mesh_instances(lua_State* L)
  1499. {
  1500. LuaStack stack(L);
  1501. RenderWorld* rw = stack.get_render_world(1);
  1502. UnitId unit = stack.get_unit(2);
  1503. TempAllocator512 ta;
  1504. Array<MeshInstance> inst(ta);
  1505. rw->mesh_instances(unit, inst);
  1506. stack.push_table(array::size(inst));
  1507. for (u32 i = 0; i < array::size(inst); ++i)
  1508. {
  1509. stack.push_key_begin(i+1);
  1510. stack.push_mesh_instance(inst[i]);
  1511. stack.push_key_end();
  1512. }
  1513. return 1;
  1514. }
  1515. static int render_world_mesh_obb(lua_State* L)
  1516. {
  1517. LuaStack stack(L);
  1518. OBB obb = stack.get_render_world(1)->mesh_obb(stack.get_mesh_instance(2));
  1519. stack.push_matrix4x4(obb.tm);
  1520. stack.push_vector3(obb.half_extents);
  1521. return 2;
  1522. }
  1523. static int render_world_mesh_raycast(lua_State* L)
  1524. {
  1525. LuaStack stack(L);
  1526. RenderWorld* rw = stack.get_render_world(1);
  1527. float t = rw->mesh_raycast(stack.get_mesh_instance(2)
  1528. , stack.get_vector3(3)
  1529. , stack.get_vector3(4)
  1530. );
  1531. stack.push_float(t);
  1532. return 1;
  1533. }
  1534. static int render_world_mesh_set_visible(lua_State* L)
  1535. {
  1536. LuaStack stack(L);
  1537. stack.get_render_world(1)->mesh_set_visible(stack.get_mesh_instance(2), stack.get_bool(3));
  1538. return 0;
  1539. }
  1540. static int render_world_sprite_create(lua_State* L)
  1541. {
  1542. LuaStack stack(L);
  1543. RenderWorld* rw = stack.get_render_world(1);
  1544. UnitId unit = stack.get_unit(2);
  1545. SpriteRendererDesc desc;
  1546. desc.sprite_resource = stack.get_resource_id(3);
  1547. desc.material_resource = stack.get_resource_id(4);
  1548. desc.visible = stack.get_bool(5);
  1549. Matrix4x4 pose = stack.get_matrix4x4(6);
  1550. stack.push_sprite_instance(rw->sprite_create(unit, desc, pose));
  1551. return 1;
  1552. }
  1553. static int render_world_sprite_destroy(lua_State* L)
  1554. {
  1555. LuaStack stack(L);
  1556. stack.get_render_world(1)->sprite_destroy(stack.get_sprite_instance(2));
  1557. return 0;
  1558. }
  1559. static int render_world_sprite_instances(lua_State* L)
  1560. {
  1561. LuaStack stack(L);
  1562. RenderWorld* rw = stack.get_render_world(1);
  1563. UnitId unit = stack.get_unit(2);
  1564. TempAllocator512 ta;
  1565. Array<SpriteInstance> inst(ta);
  1566. rw->sprite_instances(unit, inst);
  1567. stack.push_table(array::size(inst));
  1568. for (u32 i = 0; i < array::size(inst); ++i)
  1569. {
  1570. stack.push_key_begin(i+1);
  1571. stack.push_sprite_instance(inst[i]);
  1572. stack.push_key_end();
  1573. }
  1574. return 1;
  1575. }
  1576. static int render_world_sprite_set_visible(lua_State* L)
  1577. {
  1578. LuaStack stack(L);
  1579. stack.get_render_world(1)->sprite_set_visible(stack.get_sprite_instance(2), stack.get_bool(3));
  1580. return 0;
  1581. }
  1582. static int render_world_sprite_set_frame(lua_State* L)
  1583. {
  1584. LuaStack stack(L);
  1585. stack.get_render_world(1)->sprite_set_frame(stack.get_sprite_instance(2), stack.get_int(3));
  1586. return 0;
  1587. }
  1588. static int render_world_light_create(lua_State* L)
  1589. {
  1590. LuaStack stack(L);
  1591. const char* name = stack.get_string(3);
  1592. const LightType::Enum lt = name_to_light_type(name);
  1593. LUA_ASSERT(lt != LightType::COUNT, stack, "Unknown light type: '%s'", name);
  1594. LightDesc ld;
  1595. ld.type = lt;
  1596. ld.range = stack.get_float(4);
  1597. ld.intensity = stack.get_float(5);
  1598. ld.spot_angle = stack.get_float(6);
  1599. ld.color = stack.get_vector3(7);
  1600. Matrix4x4 pose = stack.get_matrix4x4(8);
  1601. stack.push_light_instance(stack.get_render_world(1)->light_create(stack.get_unit(2), ld, pose));
  1602. return 1;
  1603. }
  1604. static int render_world_light_destroy(lua_State* L)
  1605. {
  1606. LuaStack stack(L);
  1607. stack.get_render_world(1)->light_destroy(stack.get_light_instance(2));
  1608. return 0;
  1609. }
  1610. static int render_world_light_instances(lua_State* L)
  1611. {
  1612. LuaStack stack(L);
  1613. stack.push_light_instance(stack.get_render_world(1)->light(stack.get_unit(2)));
  1614. return 1;
  1615. }
  1616. static int render_world_light_type(lua_State* L)
  1617. {
  1618. LuaStack stack(L);
  1619. LightType::Enum type = stack.get_render_world(1)->light_type(stack.get_light_instance(2));
  1620. stack.push_string(s_light[type].name);
  1621. return 1;
  1622. }
  1623. static int render_world_light_color(lua_State* L)
  1624. {
  1625. LuaStack stack(L);
  1626. stack.push_color4(stack.get_render_world(1)->light_color(stack.get_light_instance(2)));
  1627. return 1;
  1628. }
  1629. static int render_world_light_range(lua_State* L)
  1630. {
  1631. LuaStack stack(L);
  1632. stack.push_float(stack.get_render_world(1)->light_range(stack.get_light_instance(2)));
  1633. return 1;
  1634. }
  1635. static int render_world_light_intensity(lua_State* L)
  1636. {
  1637. LuaStack stack(L);
  1638. stack.push_float(stack.get_render_world(1)->light_intensity(stack.get_light_instance(2)));
  1639. return 1;
  1640. }
  1641. static int render_world_light_spot_angle(lua_State* L)
  1642. {
  1643. LuaStack stack(L);
  1644. stack.push_float(stack.get_render_world(1)->light_spot_angle(stack.get_light_instance(2)));
  1645. return 1;
  1646. }
  1647. static int render_world_light_debug_draw(lua_State* L)
  1648. {
  1649. LuaStack stack(L);
  1650. stack.get_render_world(1)->light_debug_draw(stack.get_light_instance(2), *stack.get_debug_line(3));
  1651. return 0;
  1652. }
  1653. static int render_world_light_set_type(lua_State* L)
  1654. {
  1655. LuaStack stack(L);
  1656. const char* name = stack.get_string(3);
  1657. const LightType::Enum lt = name_to_light_type(name);
  1658. LUA_ASSERT(lt != LightType::COUNT, stack, "Unknown light type: '%s'", name);
  1659. stack.get_render_world(1)->light_set_type(stack.get_light_instance(2), lt);
  1660. return 0;
  1661. }
  1662. static int render_world_light_set_color(lua_State* L)
  1663. {
  1664. LuaStack stack(L);
  1665. stack.get_render_world(1)->light_set_color(stack.get_light_instance(2), stack.get_color4(3));
  1666. return 0;
  1667. }
  1668. static int render_world_light_set_range(lua_State* L)
  1669. {
  1670. LuaStack stack(L);
  1671. stack.get_render_world(1)->light_set_range(stack.get_light_instance(2), stack.get_float(3));
  1672. return 0;
  1673. }
  1674. static int render_world_light_set_intensity(lua_State* L)
  1675. {
  1676. LuaStack stack(L);
  1677. stack.get_render_world(1)->light_set_intensity(stack.get_light_instance(2), stack.get_float(3));
  1678. return 0;
  1679. }
  1680. static int render_world_light_set_spot_angle(lua_State* L)
  1681. {
  1682. LuaStack stack(L);
  1683. stack.get_render_world(1)->light_set_spot_angle(stack.get_light_instance(2), stack.get_float(3));
  1684. return 0;
  1685. }
  1686. static int render_world_enable_debug_drawing(lua_State* L)
  1687. {
  1688. LuaStack stack(L);
  1689. stack.get_render_world(1)->enable_debug_drawing(stack.get_bool(2));
  1690. return 0;
  1691. }
  1692. static int physics_world_actor_instances(lua_State* L)
  1693. {
  1694. LuaStack stack(L);
  1695. ActorInstance inst = stack.get_physics_world(1)->actor(stack.get_unit(2));
  1696. if (inst.i == UINT32_MAX)
  1697. stack.push_nil();
  1698. else
  1699. stack.push_actor(inst);
  1700. return 1;
  1701. }
  1702. static int physics_world_actor_world_position(lua_State* L)
  1703. {
  1704. LuaStack stack(L);
  1705. stack.push_vector3(stack.get_physics_world(1)->actor_world_position(stack.get_actor(2)));
  1706. return 1;
  1707. }
  1708. static int physics_world_actor_world_rotation(lua_State* L)
  1709. {
  1710. LuaStack stack(L);
  1711. stack.push_quaternion(stack.get_physics_world(1)->actor_world_rotation(stack.get_actor(2)));
  1712. return 1;
  1713. }
  1714. static int physics_world_actor_world_pose(lua_State* L)
  1715. {
  1716. LuaStack stack(L);
  1717. stack.push_matrix4x4(stack.get_physics_world(1)->actor_world_pose(stack.get_actor(2)));
  1718. return 1;
  1719. }
  1720. static int physics_world_actor_teleport_world_position(lua_State* L)
  1721. {
  1722. LuaStack stack(L);
  1723. stack.get_physics_world(1)->actor_teleport_world_position(stack.get_actor(2), stack.get_vector3(3));
  1724. return 0;
  1725. }
  1726. static int physics_world_actor_teleport_world_rotation(lua_State* L)
  1727. {
  1728. LuaStack stack(L);
  1729. stack.get_physics_world(1)->actor_teleport_world_rotation(stack.get_actor(2), stack.get_quaternion(3));
  1730. return 0;
  1731. }
  1732. static int physics_world_actor_teleport_world_pose(lua_State* L)
  1733. {
  1734. LuaStack stack(L);
  1735. stack.get_physics_world(1)->actor_teleport_world_pose(stack.get_actor(2), stack.get_matrix4x4(3));
  1736. return 0;
  1737. }
  1738. static int physics_world_actor_center_of_mass(lua_State* L)
  1739. {
  1740. LuaStack stack(L);
  1741. stack.push_vector3(stack.get_physics_world(1)->actor_center_of_mass(stack.get_actor(2)));
  1742. return 1;
  1743. }
  1744. static int physics_world_actor_enable_gravity(lua_State* L)
  1745. {
  1746. LuaStack stack(L);
  1747. stack.get_physics_world(1)->actor_enable_gravity(stack.get_actor(2));
  1748. return 0;
  1749. }
  1750. static int physics_world_actor_disable_gravity(lua_State* L)
  1751. {
  1752. LuaStack stack(L);
  1753. stack.get_physics_world(1)->actor_disable_gravity(stack.get_actor(2));
  1754. return 0;
  1755. }
  1756. static int physics_world_actor_enable_collision(lua_State* L)
  1757. {
  1758. LuaStack stack(L);
  1759. stack.get_physics_world(1)->actor_enable_collision(stack.get_actor(2));
  1760. return 0;
  1761. }
  1762. static int physics_world_actor_disable_collision(lua_State* L)
  1763. {
  1764. LuaStack stack(L);
  1765. stack.get_physics_world(1)->actor_disable_collision(stack.get_actor(2));
  1766. return 0;
  1767. }
  1768. static int physics_world_actor_set_collision_filter(lua_State* L)
  1769. {
  1770. LuaStack stack(L);
  1771. stack.get_physics_world(1)->actor_set_collision_filter(stack.get_actor(2), stack.get_string_id_32(3));
  1772. return 0;
  1773. }
  1774. static int physics_world_actor_set_kinematic(lua_State* L)
  1775. {
  1776. LuaStack stack(L);
  1777. stack.get_physics_world(1)->actor_set_kinematic(stack.get_actor(2), stack.get_bool(3));
  1778. return 0;
  1779. }
  1780. static int physics_world_actor_move(lua_State* L)
  1781. {
  1782. LuaStack stack(L);
  1783. stack.get_physics_world(1)->actor_move(stack.get_actor(2), stack.get_vector3(3));
  1784. return 0;
  1785. }
  1786. static int physics_world_actor_is_static(lua_State* L)
  1787. {
  1788. LuaStack stack(L);
  1789. stack.push_bool(stack.get_physics_world(1)->actor_is_static(stack.get_actor(2)));
  1790. return 1;
  1791. }
  1792. static int physics_world_actor_is_dynamic(lua_State* L)
  1793. {
  1794. LuaStack stack(L);
  1795. stack.push_bool(stack.get_physics_world(1)->actor_is_dynamic(stack.get_actor(2)));
  1796. return 1;
  1797. }
  1798. static int physics_world_actor_is_kinematic(lua_State* L)
  1799. {
  1800. LuaStack stack(L);
  1801. stack.push_bool(stack.get_physics_world(1)->actor_is_kinematic(stack.get_actor(2)));
  1802. return 1;
  1803. }
  1804. static int physics_world_actor_is_nonkinematic(lua_State* L)
  1805. {
  1806. LuaStack stack(L);
  1807. stack.push_bool(stack.get_physics_world(1)->actor_is_nonkinematic(stack.get_actor(2)));
  1808. return 1;
  1809. }
  1810. static int physics_world_actor_linear_damping(lua_State* L)
  1811. {
  1812. LuaStack stack(L);
  1813. stack.push_float(stack.get_physics_world(1)->actor_linear_damping(stack.get_actor(2)));
  1814. return 1;
  1815. }
  1816. static int physics_world_actor_set_linear_damping(lua_State* L)
  1817. {
  1818. LuaStack stack(L);
  1819. stack.get_physics_world(1)->actor_set_linear_damping(stack.get_actor(2), stack.get_float(3));
  1820. return 0;
  1821. }
  1822. static int physics_world_actor_angular_damping(lua_State* L)
  1823. {
  1824. LuaStack stack(L);
  1825. stack.push_float(stack.get_physics_world(1)->actor_angular_damping(stack.get_actor(2)));
  1826. return 1;
  1827. }
  1828. static int physics_world_actor_set_angular_damping(lua_State* L)
  1829. {
  1830. LuaStack stack(L);
  1831. stack.get_physics_world(1)->actor_set_angular_damping(stack.get_actor(2), stack.get_float(3));
  1832. return 0;
  1833. }
  1834. static int physics_world_actor_linear_velocity(lua_State* L)
  1835. {
  1836. LuaStack stack(L);
  1837. stack.push_vector3(stack.get_physics_world(1)->actor_linear_velocity(stack.get_actor(2)));
  1838. return 1;
  1839. }
  1840. static int physics_world_actor_set_linear_velocity(lua_State* L)
  1841. {
  1842. LuaStack stack(L);
  1843. stack.get_physics_world(1)->actor_set_linear_velocity(stack.get_actor(2), stack.get_vector3(3));
  1844. return 0;
  1845. }
  1846. static int physics_world_actor_angular_velocity(lua_State* L)
  1847. {
  1848. LuaStack stack(L);
  1849. stack.push_vector3(stack.get_physics_world(1)->actor_angular_velocity(stack.get_actor(2)));
  1850. return 1;
  1851. }
  1852. static int physics_world_actor_set_angular_velocity(lua_State* L)
  1853. {
  1854. LuaStack stack(L);
  1855. stack.get_physics_world(1)->actor_set_angular_velocity(stack.get_actor(2), stack.get_vector3(3));
  1856. return 0;
  1857. }
  1858. static int physics_world_actor_add_impulse(lua_State* L)
  1859. {
  1860. LuaStack stack(L);
  1861. stack.get_physics_world(1)->actor_add_impulse(stack.get_actor(2), stack.get_vector3(3));
  1862. return 0;
  1863. }
  1864. static int physics_world_actor_add_impulse_at(lua_State* L)
  1865. {
  1866. LuaStack stack(L);
  1867. stack.get_physics_world(1)->actor_add_impulse_at(stack.get_actor(2), stack.get_vector3(3), stack.get_vector3(4));
  1868. return 0;
  1869. }
  1870. static int physics_world_actor_add_actor_torque_impulse(lua_State* L)
  1871. {
  1872. LuaStack stack(L);
  1873. stack.get_physics_world(1)->actor_add_torque_impulse(stack.get_actor(2), stack.get_vector3(3));
  1874. return 0;
  1875. }
  1876. static int physics_world_actor_push(lua_State* L)
  1877. {
  1878. LuaStack stack(L);
  1879. stack.get_physics_world(1)->actor_push(stack.get_actor(2), stack.get_vector3(3), stack.get_float(4));
  1880. return 0;
  1881. }
  1882. static int physics_world_actor_push_at(lua_State* L)
  1883. {
  1884. LuaStack stack(L);
  1885. stack.get_physics_world(1)->actor_push_at(stack.get_actor(2), stack.get_vector3(3), stack.get_float(4), stack.get_vector3(5));
  1886. return 0;
  1887. }
  1888. static int physics_world_actor_is_sleeping(lua_State* L)
  1889. {
  1890. LuaStack stack(L);
  1891. stack.push_bool(stack.get_physics_world(1)->actor_is_sleeping(stack.get_actor(2)));
  1892. return 1;
  1893. }
  1894. static int physics_world_actor_wake_up(lua_State* L)
  1895. {
  1896. LuaStack stack(L);
  1897. stack.get_physics_world(1)->actor_wake_up(stack.get_actor(2));
  1898. return 0;
  1899. }
  1900. static int physics_world_controller_instances(lua_State* L)
  1901. {
  1902. LuaStack stack(L);
  1903. stack.push_controller(stack.get_physics_world(1)->controller(stack.get_unit(2)));
  1904. return 1;
  1905. }
  1906. static int physics_world_controller_move(lua_State* L)
  1907. {
  1908. LuaStack stack(L);
  1909. stack.get_physics_world(1)->controller_move(stack.get_controller(2), stack.get_vector3(3));
  1910. return 0;
  1911. }
  1912. static int physics_world_joint_create(lua_State* L)
  1913. {
  1914. LuaStack stack(L);
  1915. JointDesc jd;
  1916. jd.type = JointType::SPRING;
  1917. jd.anchor_0 = vector3(0, -2, 0);
  1918. jd.anchor_1 = vector3(0, 2, 0);
  1919. jd.break_force = 999999.0f;
  1920. jd.hinge.axis = vector3(1, 0, 0);
  1921. jd.hinge.lower_limit = -3.14f / 4.0f;
  1922. jd.hinge.upper_limit = 3.14f / 4.0f;
  1923. jd.hinge.bounciness = 12.0f;
  1924. stack.get_physics_world(1)->joint_create(stack.get_actor(2), stack.get_actor(3), jd);
  1925. return 0;
  1926. }
  1927. static int physics_world_gravity(lua_State* L)
  1928. {
  1929. LuaStack stack(L);
  1930. stack.push_vector3(stack.get_physics_world(1)->gravity());
  1931. return 1;
  1932. }
  1933. static int physics_world_set_gravity(lua_State* L)
  1934. {
  1935. LuaStack stack(L);
  1936. stack.get_physics_world(1)->set_gravity(stack.get_vector3(2));
  1937. return 0;
  1938. }
  1939. static int physics_world_raycast(lua_State* L)
  1940. {
  1941. LuaStack stack(L);
  1942. PhysicsWorld* world = stack.get_physics_world(1);
  1943. const char* name = stack.get_string(5);
  1944. const RaycastMode::Enum mode = name_to_raycast_mode(name);
  1945. LUA_ASSERT(mode != RaycastMode::COUNT, stack, "Unknown raycast mode: '%s'", name);
  1946. TempAllocator1024 ta;
  1947. Array<RaycastHit> hits(ta);
  1948. world->raycast(stack.get_vector3(2)
  1949. , stack.get_vector3(3)
  1950. , stack.get_float(4)
  1951. , mode
  1952. , hits
  1953. );
  1954. stack.push_table();
  1955. for (u32 i = 0; i < array::size(hits); ++i)
  1956. {
  1957. stack.push_key_begin(i+1);
  1958. stack.push_actor(hits[i].actor);
  1959. stack.push_key_end();
  1960. }
  1961. return 1;
  1962. }
  1963. static int physics_world_enable_debug_drawing(lua_State* L)
  1964. {
  1965. LuaStack stack(L);
  1966. stack.get_physics_world(1)->enable_debug_drawing(stack.get_bool(2));
  1967. return 0;
  1968. }
  1969. static int physics_world_tostring(lua_State* L)
  1970. {
  1971. LuaStack stack(L);
  1972. PhysicsWorld* pw = stack.get_physics_world(1);
  1973. stack.push_fstring("PhysicsWorld (%p)", pw);
  1974. return 1;
  1975. }
  1976. static int sound_world_stop_all(lua_State* L)
  1977. {
  1978. LuaStack stack(L);
  1979. stack.get_sound_world(1)->stop_all();
  1980. return 0;
  1981. }
  1982. static int sound_world_pause_all(lua_State* L)
  1983. {
  1984. LuaStack stack(L);
  1985. stack.get_sound_world(1)->pause_all();
  1986. return 0;
  1987. }
  1988. static int sound_world_resume_all(lua_State* L)
  1989. {
  1990. LuaStack stack(L);
  1991. stack.get_sound_world(1)->resume_all();
  1992. return 0;
  1993. }
  1994. static int sound_world_is_playing(lua_State* L)
  1995. {
  1996. LuaStack stack(L);
  1997. stack.push_bool(stack.get_sound_world(1)->is_playing(stack.get_sound_instance_id(2)));
  1998. return 1;
  1999. }
  2000. static int sound_world_tostring(lua_State* L)
  2001. {
  2002. LuaStack stack(L);
  2003. SoundWorld* sw = stack.get_sound_world(1);
  2004. stack.push_fstring("SoundWorld (%p)", sw);
  2005. return 1;
  2006. }
  2007. static int device_argv(lua_State* L)
  2008. {
  2009. LuaStack stack(L);
  2010. const int argc = device()->argc();
  2011. const char** argv = device()->argv();
  2012. stack.push_table(argc);
  2013. for (int i = 0; i < argc; ++i)
  2014. {
  2015. stack.push_key_begin(i + 1);
  2016. stack.push_string(argv[i]);
  2017. stack.push_key_end();
  2018. }
  2019. return 1;
  2020. }
  2021. static int device_platform(lua_State* L)
  2022. {
  2023. LuaStack stack(L);
  2024. stack.push_string(device()->platform());
  2025. return 1;
  2026. }
  2027. static int device_architecture(lua_State* L)
  2028. {
  2029. LuaStack stack(L);
  2030. stack.push_string(device()->architecture());
  2031. return 1;
  2032. }
  2033. static int device_version(lua_State* L)
  2034. {
  2035. LuaStack stack(L);
  2036. stack.push_string(device()->version());
  2037. return 1;
  2038. }
  2039. static int device_last_delta_time(lua_State* L)
  2040. {
  2041. LuaStack stack(L);
  2042. stack.push_float(device()->last_delta_time());
  2043. return 1;
  2044. }
  2045. static int device_quit(lua_State* /*L*/)
  2046. {
  2047. device()->quit();
  2048. return 0;
  2049. }
  2050. static int device_resolution(lua_State* L)
  2051. {
  2052. LuaStack stack(L);
  2053. u16 w, h;
  2054. device()->resolution(w, h);
  2055. stack.push_int(w);
  2056. stack.push_int(h);
  2057. return 2;
  2058. }
  2059. static int device_create_world(lua_State* L)
  2060. {
  2061. LuaStack stack(L);
  2062. stack.push_world(device()->create_world());
  2063. return 1;
  2064. }
  2065. static int device_destroy_world(lua_State* L)
  2066. {
  2067. LuaStack stack(L);
  2068. device()->destroy_world(*stack.get_world(1));
  2069. return 0;
  2070. }
  2071. static int device_render(lua_State* L)
  2072. {
  2073. LuaStack stack(L);
  2074. device()->render(*stack.get_world(1), stack.get_camera(2));
  2075. return 0;
  2076. }
  2077. static int device_create_resource_package(lua_State* L)
  2078. {
  2079. LuaStack stack(L);
  2080. stack.push_resource_package(device()->create_resource_package(stack.get_resource_id(1)));
  2081. return 1;
  2082. }
  2083. static int device_destroy_resource_package(lua_State* L)
  2084. {
  2085. LuaStack stack(L);
  2086. device()->destroy_resource_package(*stack.get_resource_package(1));
  2087. return 0;
  2088. }
  2089. static void lua_dump_table(lua_State* L, int i, StringStream& json)
  2090. {
  2091. LuaStack stack(L);
  2092. bool comma = false;
  2093. int array_index = 1;
  2094. json << "{";
  2095. stack.push_nil();
  2096. while (stack.next(i) != 0)
  2097. {
  2098. if (comma)
  2099. json << ",";
  2100. comma = true;
  2101. json << "\"";
  2102. if (stack.is_string(-2) && !stack.is_number(-2))
  2103. json << stack.get_string(-2);
  2104. else
  2105. json << array_index++;
  2106. json << "\":";
  2107. if (stack.is_nil(i + 2))
  2108. {
  2109. json << "null";
  2110. }
  2111. else if (stack.is_bool(i + 2))
  2112. {
  2113. const bool b = stack.get_bool(i + 2);
  2114. json << (b ? "true" : "false");
  2115. }
  2116. else if (stack.is_number(i + 2))
  2117. {
  2118. json << stack.get_float(i + 2);
  2119. }
  2120. else if (stack.is_string(i + 2))
  2121. {
  2122. const char* str = stack.get_string(i + 2);
  2123. json << "\"";
  2124. for (; *str; ++str)
  2125. {
  2126. if (*str == '"' || *str == '\\')
  2127. json << '\\';
  2128. json << *str;
  2129. }
  2130. json << "\"";
  2131. }
  2132. else if (stack.is_vector3(i + 2))
  2133. {
  2134. const Vector3 v = stack.get_vector3(i + 2);
  2135. json << "["
  2136. << v.x << ","
  2137. << v.y << ","
  2138. << v.z
  2139. << "]"
  2140. ;
  2141. }
  2142. else if (stack.is_quaternion(i + 2))
  2143. {
  2144. const Quaternion q = stack.get_quaternion(i + 2);
  2145. json << "["
  2146. << q.x << ","
  2147. << q.y << ","
  2148. << q.z << ","
  2149. << q.w
  2150. << "]"
  2151. ;
  2152. }
  2153. else if (stack.is_matrix4x4(i + 2))
  2154. {
  2155. const Matrix4x4 m = stack.get_matrix4x4(i + 2);
  2156. json << "["
  2157. << m.x.x << ","
  2158. << m.x.y << ","
  2159. << m.x.z << ","
  2160. << m.x.w << ","
  2161. << m.y.x << ","
  2162. << m.y.y << ","
  2163. << m.y.z << ","
  2164. << m.y.w << ","
  2165. << m.z.x << ","
  2166. << m.z.y << ","
  2167. << m.z.z << ","
  2168. << m.z.w << ","
  2169. << m.t.x << ","
  2170. << m.t.y << ","
  2171. << m.t.z << ","
  2172. << m.t.w
  2173. << "]"
  2174. ;
  2175. }
  2176. else if (stack.is_table(i + 2))
  2177. {
  2178. lua_dump_table(L, i + 2, json);
  2179. }
  2180. else
  2181. {
  2182. LUA_ASSERT(false, stack, "Unsupported key value");
  2183. }
  2184. stack.pop(1);
  2185. }
  2186. json << "}";
  2187. }
  2188. static int device_console_send(lua_State* L)
  2189. {
  2190. LuaStack stack(L);
  2191. LUA_ASSERT(stack.is_table(1), stack, "Table expected");
  2192. TempAllocator1024 alloc;
  2193. StringStream json(alloc);
  2194. lua_dump_table(L, 1, json);
  2195. device()->console_server()->send(string_stream::c_str(json));
  2196. return 0;
  2197. }
  2198. static int device_can_get(lua_State* L)
  2199. {
  2200. LuaStack stack(L);
  2201. const StringId64 type(stack.get_string(1));
  2202. stack.push_bool(device()->resource_manager()->can_get(type, stack.get_resource_id(2)));
  2203. return 1;
  2204. }
  2205. static int device_enable_resource_autoload(lua_State* L)
  2206. {
  2207. LuaStack stack(L);
  2208. device()->resource_manager()->enable_autoload(stack.get_bool(1));
  2209. return 0;
  2210. }
  2211. static int device_temp_count(lua_State* L)
  2212. {
  2213. LuaStack stack(L);
  2214. u32 nv, nq, nm;
  2215. device()->lua_environment()->temp_count(nv, nq, nm);
  2216. stack.push_int(nv);
  2217. stack.push_int(nq);
  2218. stack.push_int(nm);
  2219. return 3;
  2220. }
  2221. static int device_set_temp_count(lua_State* L)
  2222. {
  2223. LuaStack stack(L);
  2224. u32 nv = stack.get_int(1);
  2225. u32 nq = stack.get_int(2);
  2226. u32 nm = stack.get_int(3);
  2227. device()->lua_environment()->set_temp_count(nv, nq, nm);
  2228. return 0;
  2229. }
  2230. static int device_guid(lua_State* L)
  2231. {
  2232. LuaStack stack(L);
  2233. TempAllocator64 ta;
  2234. DynamicString str(ta);
  2235. Guid guid = guid::new_guid();
  2236. guid::to_string(guid, str);
  2237. stack.push_string(str.c_str());
  2238. return 1;
  2239. }
  2240. static int profiler_enter_scope(lua_State* L)
  2241. {
  2242. LuaStack stack(L);
  2243. profiler::enter_profile_scope(stack.get_string(1));
  2244. return 0;
  2245. }
  2246. static int profiler_leave_scope(lua_State* L)
  2247. {
  2248. LuaStack stack(L);
  2249. profiler::leave_profile_scope();
  2250. return 0;
  2251. }
  2252. static int profiler_record(lua_State* L)
  2253. {
  2254. LuaStack stack(L);
  2255. const char* name = stack.get_string(1);
  2256. if (stack.is_number(2))
  2257. profiler::record_float(name, stack.get_float(2));
  2258. else
  2259. profiler::record_vector3(name, stack.get_vector3(2));
  2260. return 0;
  2261. }
  2262. static int debug_line_add_line(lua_State* L)
  2263. {
  2264. LuaStack stack(L);
  2265. stack.get_debug_line(1)->add_line(stack.get_vector3(2)
  2266. , stack.get_vector3(3)
  2267. , stack.get_color4(4)
  2268. );
  2269. return 0;
  2270. }
  2271. static int debug_line_add_axes(lua_State* L)
  2272. {
  2273. LuaStack stack(L);
  2274. const f32 len = stack.num_args() == 3
  2275. ? stack.get_float(3)
  2276. : 1.0f
  2277. ;
  2278. stack.get_debug_line(1)->add_axes(stack.get_matrix4x4(2), len);
  2279. return 0;
  2280. }
  2281. static int debug_line_add_circle(lua_State* L)
  2282. {
  2283. LuaStack stack(L);
  2284. const u32 segments = stack.num_args() >= 6
  2285. ? stack.get_int(6)
  2286. : DebugLine::NUM_SEGMENTS
  2287. ;
  2288. stack.get_debug_line(1)->add_circle(stack.get_vector3(2)
  2289. , stack.get_float(3)
  2290. , stack.get_vector3(4)
  2291. , stack.get_color4(5)
  2292. , segments
  2293. );
  2294. return 0;
  2295. }
  2296. static int debug_line_add_cone(lua_State* L)
  2297. {
  2298. LuaStack stack(L);
  2299. const u32 segments = stack.num_args() >= 6
  2300. ? stack.get_int(6)
  2301. : DebugLine::NUM_SEGMENTS
  2302. ;
  2303. stack.get_debug_line(1)->add_cone(stack.get_vector3(2)
  2304. , stack.get_vector3(3)
  2305. , stack.get_float(4)
  2306. , stack.get_color4(5)
  2307. , segments
  2308. );
  2309. return 0;
  2310. }
  2311. static int debug_line_add_sphere(lua_State* L)
  2312. {
  2313. LuaStack stack(L);
  2314. const u32 segments = stack.num_args() >= 5
  2315. ? stack.get_int(5)
  2316. : DebugLine::NUM_SEGMENTS
  2317. ;
  2318. stack.get_debug_line(1)->add_sphere(stack.get_vector3(2)
  2319. , stack.get_float(3)
  2320. , stack.get_color4(4)
  2321. , segments
  2322. );
  2323. return 0;
  2324. }
  2325. static int debug_line_add_obb(lua_State* L)
  2326. {
  2327. LuaStack stack(L);
  2328. stack.get_debug_line(1)->add_obb(stack.get_matrix4x4(2)
  2329. , stack.get_vector3(3)
  2330. , stack.get_color4(4)
  2331. );
  2332. return 0;
  2333. }
  2334. static int debug_line_add_unit(lua_State* L)
  2335. {
  2336. LuaStack stack(L);
  2337. stack.get_debug_line(1)->add_unit(*device()->resource_manager()
  2338. , stack.get_matrix4x4(2)
  2339. , stack.get_resource_id(3)
  2340. , stack.get_color4(4)
  2341. );
  2342. return 0;
  2343. }
  2344. static int debug_line_reset(lua_State* L)
  2345. {
  2346. LuaStack stack(L);
  2347. stack.get_debug_line(1)->reset();
  2348. return 0;
  2349. }
  2350. static int debug_line_submit(lua_State* L)
  2351. {
  2352. LuaStack stack(L);
  2353. stack.get_debug_line(1)->submit();
  2354. return 0;
  2355. }
  2356. static int debug_line_tostring(lua_State* L)
  2357. {
  2358. LuaStack stack(L);
  2359. stack.push_fstring("DebugLine (%p)", stack.get_debug_line(1));
  2360. return 1;
  2361. }
  2362. static int resource_package_load(lua_State* L)
  2363. {
  2364. LuaStack stack(L);
  2365. stack.get_resource_package(1)->load();
  2366. return 0;
  2367. }
  2368. static int resource_package_unload(lua_State* L)
  2369. {
  2370. LuaStack stack(L);
  2371. stack.get_resource_package(1)->unload();
  2372. return 0;
  2373. }
  2374. static int resource_package_flush(lua_State* L)
  2375. {
  2376. LuaStack stack(L);
  2377. stack.get_resource_package(1)->flush();
  2378. return 0;
  2379. }
  2380. static int resource_package_has_loaded(lua_State* L)
  2381. {
  2382. LuaStack stack(L);
  2383. stack.push_bool(stack.get_resource_package(1)->has_loaded());
  2384. return 1;
  2385. }
  2386. static int resource_package_tostring(lua_State* L)
  2387. {
  2388. LuaStack stack(L);
  2389. ResourcePackage* package = stack.get_resource_package(1);
  2390. stack.push_fstring("ResourcePackage (%p)", package);
  2391. return 1;
  2392. }
  2393. static int material_set_float(lua_State* L)
  2394. {
  2395. LuaStack stack(L);
  2396. stack.get_material(1)->set_float(stack.get_string_id_32(2), stack.get_float(3));
  2397. return 0;
  2398. }
  2399. static int material_set_vector2(lua_State* L)
  2400. {
  2401. LuaStack stack(L);
  2402. stack.get_material(1)->set_vector2(stack.get_string_id_32(2), stack.get_vector2(3));
  2403. return 0;
  2404. }
  2405. static int material_set_vector3(lua_State* L)
  2406. {
  2407. LuaStack stack(L);
  2408. stack.get_material(1)->set_vector3(stack.get_string_id_32(2), stack.get_vector3(3));
  2409. return 0;
  2410. }
  2411. static int gui_resolution(lua_State* L)
  2412. {
  2413. LuaStack stack(L);
  2414. const Vector2 resolution = stack.get_debug_gui(1)->resolution();
  2415. stack.push_int((s32)resolution.x);
  2416. stack.push_int((s32)resolution.y);
  2417. return 2;
  2418. }
  2419. static int gui_move(lua_State* L)
  2420. {
  2421. LuaStack stack(L);
  2422. stack.get_debug_gui(1)->move(stack.get_vector2(2));
  2423. return 0;
  2424. }
  2425. static int gui_screen_to_gui(lua_State* L)
  2426. {
  2427. LuaStack stack(L);
  2428. stack.push_vector2(stack.get_debug_gui(1)->screen_to_gui(stack.get_vector2(2)));
  2429. return 1;
  2430. }
  2431. static int gui_rect(lua_State* L)
  2432. {
  2433. LuaStack stack(L);
  2434. stack.get_debug_gui(1)->rect(stack.get_vector2(2)
  2435. , stack.get_vector2(3)
  2436. , stack.get_resource_id(4)
  2437. , stack.get_color4(5)
  2438. );
  2439. return 0;
  2440. }
  2441. static int gui_image(lua_State* L)
  2442. {
  2443. LuaStack stack(L);
  2444. stack.get_debug_gui(1)->image(stack.get_vector2(2)
  2445. , stack.get_vector2(3)
  2446. , stack.get_resource_id(4)
  2447. , stack.get_color4(5)
  2448. );
  2449. return 0;
  2450. }
  2451. static int gui_image_uv(lua_State* L)
  2452. {
  2453. LuaStack stack(L);
  2454. stack.get_debug_gui(1)->image_uv(stack.get_vector2(2)
  2455. , stack.get_vector2(3)
  2456. , stack.get_vector2(4)
  2457. , stack.get_vector2(5)
  2458. , stack.get_resource_id(6)
  2459. , stack.get_color4(7)
  2460. );
  2461. return 0;
  2462. }
  2463. static int gui_text(lua_State* L)
  2464. {
  2465. LuaStack stack(L);
  2466. stack.get_debug_gui(1)->text(stack.get_vector2(2)
  2467. , stack.get_int(3)
  2468. , stack.get_string(4)
  2469. , stack.get_resource_id(5)
  2470. , stack.get_resource_id(6)
  2471. , stack.get_color4(7)
  2472. );
  2473. return 0;
  2474. }
  2475. static int display_modes(lua_State* L)
  2476. {
  2477. LuaStack stack(L);
  2478. TempAllocator1024 ta;
  2479. Array<DisplayMode> modes(ta);
  2480. device()->display()->modes(modes);
  2481. stack.push_table(array::size(modes));
  2482. for (u32 i = 0; i < array::size(modes); ++i)
  2483. {
  2484. stack.push_key_begin(i+1);
  2485. stack.push_table(3);
  2486. {
  2487. stack.push_key_begin("id");
  2488. stack.push_int(modes[i].id);
  2489. stack.push_key_end();
  2490. stack.push_key_begin("width");
  2491. stack.push_int(modes[i].width);
  2492. stack.push_key_end();
  2493. stack.push_key_begin("height");
  2494. stack.push_int(modes[i].height);
  2495. stack.push_key_end();
  2496. }
  2497. stack.push_key_end();
  2498. }
  2499. return 1;
  2500. }
  2501. static int display_set_mode(lua_State* L)
  2502. {
  2503. LuaStack stack(L);
  2504. device()->display()->set_mode(stack.get_int(1));
  2505. return 0;
  2506. }
  2507. static int window_show(lua_State* L)
  2508. {
  2509. LuaStack stack(L);
  2510. device()->window()->show();
  2511. return 0;
  2512. }
  2513. static int window_hide(lua_State* L)
  2514. {
  2515. LuaStack stack(L);
  2516. device()->window()->hide();
  2517. return 0;
  2518. }
  2519. static int window_resize(lua_State* L)
  2520. {
  2521. LuaStack stack(L);
  2522. device()->window()->resize(stack.get_int(1), stack.get_int(2));
  2523. return 0;
  2524. }
  2525. static int window_move(lua_State* L)
  2526. {
  2527. LuaStack stack(L);
  2528. device()->window()->move(stack.get_int(1), stack.get_int(2));
  2529. return 0;
  2530. }
  2531. static int window_minimize(lua_State* /*L*/)
  2532. {
  2533. device()->window()->minimize();
  2534. return 0;
  2535. }
  2536. static int window_restore(lua_State* /*L*/)
  2537. {
  2538. device()->window()->restore();
  2539. return 0;
  2540. }
  2541. static int window_title(lua_State* L)
  2542. {
  2543. LuaStack stack(L);
  2544. stack.push_string(device()->window()->title());
  2545. return 1;
  2546. }
  2547. static int window_set_title(lua_State* L)
  2548. {
  2549. LuaStack stack(L);
  2550. device()->window()->set_title(stack.get_string(1));
  2551. return 0;
  2552. }
  2553. static int window_show_cursor(lua_State* L)
  2554. {
  2555. LuaStack stack(L);
  2556. device()->window()->show_cursor(stack.get_bool(1));
  2557. return 0;
  2558. }
  2559. void load_api(LuaEnvironment& env)
  2560. {
  2561. env.add_module_function("Math", "ray_plane_intersection", math_ray_plane_intersection);
  2562. env.add_module_function("Math", "ray_disc_intersection", math_ray_disc_intersection);
  2563. env.add_module_function("Math", "ray_sphere_intersection", math_ray_sphere_intersection);
  2564. env.add_module_function("Math", "ray_obb_intersection", math_ray_obb_intersection);
  2565. env.add_module_function("Math", "ray_triangle_intersection", math_ray_triangle_intersection);
  2566. env.add_module_function("Vector3", "x", vector3_x);
  2567. env.add_module_function("Vector3", "y", vector3_y);
  2568. env.add_module_function("Vector3", "z", vector3_z);
  2569. env.add_module_function("Vector3", "set_x", vector3_set_x);
  2570. env.add_module_function("Vector3", "set_y", vector3_set_y);
  2571. env.add_module_function("Vector3", "set_z", vector3_set_z);
  2572. env.add_module_function("Vector3", "elements", vector3_elements);
  2573. env.add_module_function("Vector3", "add", vector3_add);
  2574. env.add_module_function("Vector3", "subtract", vector3_subtract);
  2575. env.add_module_function("Vector3", "multiply", vector3_multiply);
  2576. env.add_module_function("Vector3", "dot", vector3_dot);
  2577. env.add_module_function("Vector3", "cross", vector3_cross);
  2578. env.add_module_function("Vector3", "equal", vector3_equal);
  2579. env.add_module_function("Vector3", "length", vector3_length);
  2580. env.add_module_function("Vector3", "length_squared", vector3_length_squared);
  2581. env.add_module_function("Vector3", "set_length", vector3_set_length);
  2582. env.add_module_function("Vector3", "normalize", vector3_normalize);
  2583. env.add_module_function("Vector3", "distance", vector3_distance);
  2584. env.add_module_function("Vector3", "distance_squared", vector3_distance_squared);
  2585. env.add_module_function("Vector3", "angle", vector3_angle);
  2586. env.add_module_function("Vector3", "max", vector3_max);
  2587. env.add_module_function("Vector3", "min", vector3_min);
  2588. env.add_module_function("Vector3", "lerp", vector3_lerp);
  2589. env.add_module_function("Vector3", "forward", vector3_forward);
  2590. env.add_module_function("Vector3", "backward", vector3_backward);
  2591. env.add_module_function("Vector3", "left", vector3_left);
  2592. env.add_module_function("Vector3", "right", vector3_right);
  2593. env.add_module_function("Vector3", "up", vector3_up);
  2594. env.add_module_function("Vector3", "down", vector3_down);
  2595. env.add_module_function("Vector3", "zero", vector3_zero);
  2596. env.add_module_function("Vector3", "to_string", vector3_to_string);
  2597. env.set_module_constructor("Vector3", vector3_ctor);
  2598. env.set_module_constructor("Vector2", vector2_ctor);
  2599. env.add_module_function("Vector3Box", "store", vector3box_store);
  2600. env.add_module_function("Vector3Box", "unbox", vector3box_unbox);
  2601. env.add_module_function("Vector3Box", "__index", "Vector3Box");
  2602. env.add_module_function("Vector3Box", "__tostring", vector3box_tostring);
  2603. env.set_module_constructor("Vector3Box", vector3box_ctor);
  2604. env.add_module_function("Matrix4x4", "from_quaternion", matrix4x4_from_quaternion);
  2605. env.add_module_function("Matrix4x4", "from_translation", matrix4x4_from_translation);
  2606. env.add_module_function("Matrix4x4", "from_quaternion_translation", matrix4x4_from_quaternion_translation);
  2607. env.add_module_function("Matrix4x4", "from_axes", matrix4x4_from_axes);
  2608. env.add_module_function("Matrix4x4", "copy", matrix4x4_copy);
  2609. env.add_module_function("Matrix4x4", "add", matrix4x4_add);
  2610. env.add_module_function("Matrix4x4", "subtract", matrix4x4_subtract);
  2611. env.add_module_function("Matrix4x4", "multiply", matrix4x4_multiply);
  2612. env.add_module_function("Matrix4x4", "transpose", matrix4x4_transpose);
  2613. env.add_module_function("Matrix4x4", "determinant", matrix4x4_determinant);
  2614. env.add_module_function("Matrix4x4", "invert", matrix4x4_invert);
  2615. env.add_module_function("Matrix4x4", "x", matrix4x4_x);
  2616. env.add_module_function("Matrix4x4", "y", matrix4x4_y);
  2617. env.add_module_function("Matrix4x4", "z", matrix4x4_z);
  2618. env.add_module_function("Matrix4x4", "set_x", matrix4x4_set_x);
  2619. env.add_module_function("Matrix4x4", "set_y", matrix4x4_set_y);
  2620. env.add_module_function("Matrix4x4", "set_z", matrix4x4_set_z);
  2621. env.add_module_function("Matrix4x4", "translation", matrix4x4_translation);
  2622. env.add_module_function("Matrix4x4", "set_translation", matrix4x4_set_translation);
  2623. env.add_module_function("Matrix4x4", "rotation", matrix4x4_rotation);
  2624. env.add_module_function("Matrix4x4", "set_rotation", matrix4x4_set_rotation);
  2625. env.add_module_function("Matrix4x4", "identity", matrix4x4_identity);
  2626. env.add_module_function("Matrix4x4", "transform", matrix4x4_transform);
  2627. env.add_module_function("Matrix4x4", "to_string", matrix4x4_to_string);
  2628. env.set_module_constructor("Matrix4x4", matrix4x4_ctor);
  2629. env.add_module_function("Matrix4x4Box", "store", matrix4x4box_store);
  2630. env.add_module_function("Matrix4x4Box", "unbox", matrix4x4box_unbox);
  2631. env.add_module_function("Matrix4x4Box", "__index", "Matrix4x4Box");
  2632. env.add_module_function("Matrix4x4Box", "__tostring", matrix4x4box_tostring);
  2633. env.set_module_constructor("Matrix4x4Box", matrix4x4box_ctor);
  2634. env.add_module_function("Quaternion", "from_axis_angle", quaternion_from_axis_angle);
  2635. env.add_module_function("Quaternion", "from_elements", quaternion_from_elements);
  2636. env.add_module_function("Quaternion", "negate", quaternion_negate);
  2637. env.add_module_function("Quaternion", "identity", quaternion_identity);
  2638. env.add_module_function("Quaternion", "multiply", quaternion_multiply);
  2639. env.add_module_function("Quaternion", "multiply_by_scalar", quaternion_multiply_by_scalar);
  2640. env.add_module_function("Quaternion", "dot", quaternion_dot);
  2641. env.add_module_function("Quaternion", "length", quaternion_length);
  2642. env.add_module_function("Quaternion", "normalize", quaternion_normalize);
  2643. env.add_module_function("Quaternion", "conjugate", quaternion_conjugate);
  2644. env.add_module_function("Quaternion", "inverse", quaternion_inverse);
  2645. env.add_module_function("Quaternion", "power", quaternion_power);
  2646. env.add_module_function("Quaternion", "elements", quaternion_elements);
  2647. env.add_module_function("Quaternion", "look", quaternion_look);
  2648. env.add_module_function("Quaternion", "right", quaternion_right);
  2649. env.add_module_function("Quaternion", "up", quaternion_up);
  2650. env.add_module_function("Quaternion", "forward", quaternion_forward);
  2651. env.add_module_function("Quaternion", "lerp", quaternion_lerp);
  2652. env.add_module_function("Quaternion", "to_string", quaternion_to_string);
  2653. env.set_module_constructor("Quaternion", quaternion_ctor);
  2654. env.add_module_function("QuaternionBox", "store", quaternionbox_store);
  2655. env.add_module_function("QuaternionBox", "unbox", quaternionbox_unbox);
  2656. env.add_module_function("QuaternionBox", "__index", "QuaternionBox");
  2657. env.add_module_function("QuaternionBox", "__tostring", quaternionbox_tostring);
  2658. env.set_module_constructor("QuaternionBox", quaternionbox_ctor);
  2659. env.add_module_function("Color4", "lerp", color4_lerp);
  2660. env.add_module_function("Color4", "black", color4_black);
  2661. env.add_module_function("Color4", "white", color4_white);
  2662. env.add_module_function("Color4", "red", color4_red);
  2663. env.add_module_function("Color4", "green", color4_green);
  2664. env.add_module_function("Color4", "blue", color4_blue);
  2665. env.add_module_function("Color4", "yellow", color4_yellow);
  2666. env.add_module_function("Color4", "orange", color4_orange);
  2667. env.add_module_function("Color4", "to_string", quaternion_to_string);
  2668. env.set_module_constructor("Color4", color4_ctor);
  2669. env.add_module_function("Lightuserdata_mt", "__add", lightuserdata_add);
  2670. env.add_module_function("Lightuserdata_mt", "__sub", lightuserdata_sub);
  2671. env.add_module_function("Lightuserdata_mt", "__mul", lightuserdata_mul);
  2672. env.add_module_function("Lightuserdata_mt", "__unm", lightuserdata_unm);
  2673. env.add_module_function("Lightuserdata_mt", "__index", lightuserdata_index);
  2674. env.add_module_function("Lightuserdata_mt", "__newindex", lightuserdata_newindex);
  2675. env.add_module_function("Keyboard", "name", KEYBOARD_FN(name));
  2676. env.add_module_function("Keyboard", "connected", KEYBOARD_FN(connected));
  2677. env.add_module_function("Keyboard", "num_buttons", KEYBOARD_FN(num_buttons));
  2678. env.add_module_function("Keyboard", "num_axes", KEYBOARD_FN(num_axes));
  2679. env.add_module_function("Keyboard", "pressed", KEYBOARD_FN(pressed));
  2680. env.add_module_function("Keyboard", "released", KEYBOARD_FN(released));
  2681. env.add_module_function("Keyboard", "any_pressed", KEYBOARD_FN(any_pressed));
  2682. env.add_module_function("Keyboard", "any_released", KEYBOARD_FN(any_released));
  2683. env.add_module_function("Keyboard", "button_name", KEYBOARD_FN(button_name));
  2684. env.add_module_function("Keyboard", "button_id", KEYBOARD_FN(button_id));
  2685. env.add_module_function("Mouse", "name", MOUSE_FN(name));
  2686. env.add_module_function("Mouse", "connected", MOUSE_FN(connected));
  2687. env.add_module_function("Mouse", "num_buttons", MOUSE_FN(num_buttons));
  2688. env.add_module_function("Mouse", "num_axes", MOUSE_FN(num_axes));
  2689. env.add_module_function("Mouse", "pressed", MOUSE_FN(pressed));
  2690. env.add_module_function("Mouse", "released", MOUSE_FN(released));
  2691. env.add_module_function("Mouse", "any_pressed", MOUSE_FN(any_pressed));
  2692. env.add_module_function("Mouse", "any_released", MOUSE_FN(any_released));
  2693. env.add_module_function("Mouse", "axis", MOUSE_FN(axis));
  2694. env.add_module_function("Mouse", "button_name", MOUSE_FN(button_name));
  2695. env.add_module_function("Mouse", "axis_name", MOUSE_FN(axis_name));
  2696. env.add_module_function("Mouse", "button_id", MOUSE_FN(button_id));
  2697. env.add_module_function("Mouse", "axis_id", MOUSE_FN(axis_id));
  2698. env.add_module_function("Touch", "name", TOUCH_FN(name));
  2699. env.add_module_function("Touch", "connected", TOUCH_FN(connected));
  2700. env.add_module_function("Touch", "num_buttons", TOUCH_FN(num_buttons));
  2701. env.add_module_function("Touch", "num_axes", TOUCH_FN(num_axes));
  2702. env.add_module_function("Touch", "pressed", TOUCH_FN(pressed));
  2703. env.add_module_function("Touch", "released", TOUCH_FN(released));
  2704. env.add_module_function("Touch", "any_pressed", TOUCH_FN(any_pressed));
  2705. env.add_module_function("Touch", "any_released", TOUCH_FN(any_released));
  2706. env.add_module_function("Touch", "axis", TOUCH_FN(axis));
  2707. env.add_module_function("Touch", "button_name", TOUCH_FN(button_name));
  2708. env.add_module_function("Touch", "axis_name", TOUCH_FN(axis_name));
  2709. env.add_module_function("Touch", "button_id", TOUCH_FN(button_id));
  2710. env.add_module_function("Touch", "axis_id", TOUCH_FN(axis_id));
  2711. env.add_module_function("Pad1", "name", JOYPAD_FN(0, name));
  2712. env.add_module_function("Pad1", "connected", JOYPAD_FN(0, connected));
  2713. env.add_module_function("Pad1", "num_buttons", JOYPAD_FN(0, num_buttons));
  2714. env.add_module_function("Pad1", "num_axes", JOYPAD_FN(0, num_axes));
  2715. env.add_module_function("Pad1", "pressed", JOYPAD_FN(0, pressed));
  2716. env.add_module_function("Pad1", "released", JOYPAD_FN(0, released));
  2717. env.add_module_function("Pad1", "any_pressed", JOYPAD_FN(0, any_pressed));
  2718. env.add_module_function("Pad1", "any_released", JOYPAD_FN(0, any_released));
  2719. env.add_module_function("Pad1", "axis", JOYPAD_FN(0, axis));
  2720. env.add_module_function("Pad1", "button_name", JOYPAD_FN(0, button_name));
  2721. env.add_module_function("Pad1", "axis_name", JOYPAD_FN(0, axis_name));
  2722. env.add_module_function("Pad1", "button_id", JOYPAD_FN(0, button_id));
  2723. env.add_module_function("Pad1", "axis_id", JOYPAD_FN(0, axis_id));
  2724. env.add_module_function("Pad2", "name", JOYPAD_FN(1, name));
  2725. env.add_module_function("Pad2", "connected", JOYPAD_FN(1, connected));
  2726. env.add_module_function("Pad2", "num_buttons", JOYPAD_FN(1, num_buttons));
  2727. env.add_module_function("Pad2", "num_axes", JOYPAD_FN(1, num_axes));
  2728. env.add_module_function("Pad2", "pressed", JOYPAD_FN(1, pressed));
  2729. env.add_module_function("Pad2", "released", JOYPAD_FN(1, released));
  2730. env.add_module_function("Pad2", "any_pressed", JOYPAD_FN(1, any_pressed));
  2731. env.add_module_function("Pad2", "any_released", JOYPAD_FN(1, any_released));
  2732. env.add_module_function("Pad2", "axis", JOYPAD_FN(1, axis));
  2733. env.add_module_function("Pad2", "button_name", JOYPAD_FN(1, button_name));
  2734. env.add_module_function("Pad2", "axis_name", JOYPAD_FN(1, axis_name));
  2735. env.add_module_function("Pad2", "button_id", JOYPAD_FN(1, button_id));
  2736. env.add_module_function("Pad2", "axis_id", JOYPAD_FN(1, axis_id));
  2737. env.add_module_function("Pad3", "name", JOYPAD_FN(2, name));
  2738. env.add_module_function("Pad3", "connected", JOYPAD_FN(2, connected));
  2739. env.add_module_function("Pad3", "num_buttons", JOYPAD_FN(2, num_buttons));
  2740. env.add_module_function("Pad3", "num_axes", JOYPAD_FN(2, num_axes));
  2741. env.add_module_function("Pad3", "pressed", JOYPAD_FN(2, pressed));
  2742. env.add_module_function("Pad3", "released", JOYPAD_FN(2, released));
  2743. env.add_module_function("Pad3", "any_pressed", JOYPAD_FN(2, any_pressed));
  2744. env.add_module_function("Pad3", "any_released", JOYPAD_FN(2, any_released));
  2745. env.add_module_function("Pad3", "axis", JOYPAD_FN(2, axis));
  2746. env.add_module_function("Pad3", "button_name", JOYPAD_FN(2, button_name));
  2747. env.add_module_function("Pad3", "axis_name", JOYPAD_FN(2, axis_name));
  2748. env.add_module_function("Pad3", "button_id", JOYPAD_FN(2, button_id));
  2749. env.add_module_function("Pad3", "axis_id", JOYPAD_FN(2, axis_id));
  2750. env.add_module_function("Pad4", "name", JOYPAD_FN(3, name));
  2751. env.add_module_function("Pad4", "connected", JOYPAD_FN(3, connected));
  2752. env.add_module_function("Pad4", "num_buttons", JOYPAD_FN(3, num_buttons));
  2753. env.add_module_function("Pad4", "num_axes", JOYPAD_FN(3, num_axes));
  2754. env.add_module_function("Pad4", "pressed", JOYPAD_FN(3, pressed));
  2755. env.add_module_function("Pad4", "released", JOYPAD_FN(3, released));
  2756. env.add_module_function("Pad4", "any_pressed", JOYPAD_FN(3, any_pressed));
  2757. env.add_module_function("Pad4", "any_released", JOYPAD_FN(3, any_released));
  2758. env.add_module_function("Pad4", "axis", JOYPAD_FN(3, axis));
  2759. env.add_module_function("Pad4", "button_name", JOYPAD_FN(3, button_name));
  2760. env.add_module_function("Pad4", "axis_name", JOYPAD_FN(3, axis_name));
  2761. env.add_module_function("Pad4", "button_id", JOYPAD_FN(3, button_id));
  2762. env.add_module_function("Pad4", "axis_id", JOYPAD_FN(3, axis_id));
  2763. env.add_module_function("World", "spawn_unit", world_spawn_unit);
  2764. env.add_module_function("World", "spawn_empty_unit", world_spawn_empty_unit);
  2765. env.add_module_function("World", "destroy_unit", world_destroy_unit);
  2766. env.add_module_function("World", "num_units", world_num_units);
  2767. env.add_module_function("World", "units", world_units);
  2768. env.add_module_function("World", "camera", world_camera);
  2769. env.add_module_function("World", "camera_set_projection_type", camera_set_projection_type);
  2770. env.add_module_function("World", "camera_projection_type", camera_projection_type);
  2771. env.add_module_function("World", "camera_fov", camera_fov);
  2772. env.add_module_function("World", "camera_set_fov", camera_set_fov);
  2773. env.add_module_function("World", "camera_near_clip_distance", camera_near_clip_distance);
  2774. env.add_module_function("World", "camera_set_near_clip_distance", camera_set_near_clip_distance);
  2775. env.add_module_function("World", "camera_far_clip_distance", camera_far_clip_distance);
  2776. env.add_module_function("World", "camera_set_far_clip_distance", camera_set_far_clip_distance);
  2777. env.add_module_function("World", "camera_set_orthographic_metrics", camera_set_orthographic_metrics);
  2778. env.add_module_function("World", "camera_screen_to_world", camera_screen_to_world);
  2779. env.add_module_function("World", "camera_world_to_screen", camera_world_to_screen);
  2780. env.add_module_function("World", "update_animations", world_update_animations);
  2781. env.add_module_function("World", "update_scene", world_update_scene);
  2782. env.add_module_function("World", "update", world_update);
  2783. env.add_module_function("World", "play_sound", world_play_sound);
  2784. env.add_module_function("World", "stop_sound", world_stop_sound);
  2785. env.add_module_function("World", "link_sound", world_link_sound);
  2786. env.add_module_function("World", "set_listener_pose", world_set_listener_pose);
  2787. env.add_module_function("World", "set_sound_position", world_set_sound_position);
  2788. env.add_module_function("World", "set_sound_range", world_set_sound_range);
  2789. env.add_module_function("World", "set_sound_volume", world_set_sound_volume);
  2790. env.add_module_function("World", "create_debug_line", world_create_debug_line);
  2791. env.add_module_function("World", "destroy_debug_line", world_destroy_debug_line);
  2792. env.add_module_function("World", "create_screen_debug_gui", world_create_screen_debug_gui);
  2793. env.add_module_function("World", "destroy_gui", world_destroy_gui);
  2794. env.add_module_function("World", "load_level", world_load_level);
  2795. env.add_module_function("World", "scene_graph", world_scene_graph);
  2796. env.add_module_function("World", "render_world", world_render_world);
  2797. env.add_module_function("World", "physics_world", world_physics_world);
  2798. env.add_module_function("World", "sound_world", world_sound_world);
  2799. env.add_module_function("World", "__index", "World");
  2800. env.add_module_function("World", "__tostring", world_tostring);
  2801. env.add_module_function("SceneGraph", "create", scene_graph_create);
  2802. env.add_module_function("SceneGraph", "destroy", scene_graph_destroy);
  2803. env.add_module_function("SceneGraph", "transform_instances", scene_graph_transform_instances);
  2804. env.add_module_function("SceneGraph", "local_position", scene_graph_local_position);
  2805. env.add_module_function("SceneGraph", "local_rotation", scene_graph_local_rotation);
  2806. env.add_module_function("SceneGraph", "local_scale", scene_graph_local_scale);
  2807. env.add_module_function("SceneGraph", "local_pose", scene_graph_local_pose);
  2808. env.add_module_function("SceneGraph", "world_position", scene_graph_world_position);
  2809. env.add_module_function("SceneGraph", "world_rotation", scene_graph_world_rotation);
  2810. env.add_module_function("SceneGraph", "world_pose", scene_graph_world_pose);
  2811. env.add_module_function("SceneGraph", "set_local_position", scene_graph_set_local_position);
  2812. env.add_module_function("SceneGraph", "set_local_rotation", scene_graph_set_local_rotation);
  2813. env.add_module_function("SceneGraph", "set_local_scale", scene_graph_set_local_scale);
  2814. env.add_module_function("SceneGraph", "set_local_pose", scene_graph_set_local_pose);
  2815. env.add_module_function("SceneGraph", "link", scene_graph_link);
  2816. env.add_module_function("SceneGraph", "unlink", scene_graph_unlink);
  2817. env.add_module_function("UnitManager", "create", unit_manager_create);
  2818. env.add_module_function("UnitManager", "alive", unit_manager_alive);
  2819. env.add_module_function("RenderWorld", "mesh_create", render_world_mesh_create);
  2820. env.add_module_function("RenderWorld", "mesh_destroy", render_world_mesh_destroy);
  2821. env.add_module_function("RenderWorld", "mesh_instances", render_world_mesh_instances);
  2822. env.add_module_function("RenderWorld", "mesh_obb", render_world_mesh_obb);
  2823. env.add_module_function("RenderWorld", "mesh_raycast", render_world_mesh_raycast);
  2824. env.add_module_function("RenderWorld", "mesh_set_visible", render_world_mesh_set_visible);
  2825. env.add_module_function("RenderWorld", "sprite_create", render_world_sprite_create);
  2826. env.add_module_function("RenderWorld", "sprite_destroy", render_world_sprite_destroy);
  2827. env.add_module_function("RenderWorld", "sprite_instances", render_world_sprite_instances);
  2828. env.add_module_function("RenderWorld", "sprite_set_frame", render_world_sprite_set_frame);
  2829. env.add_module_function("RenderWorld", "sprite_set_visible", render_world_sprite_set_visible);
  2830. env.add_module_function("RenderWorld", "light_create", render_world_light_create);
  2831. env.add_module_function("RenderWorld", "light_destroy", render_world_light_destroy);
  2832. env.add_module_function("RenderWorld", "light_instances", render_world_light_instances);
  2833. env.add_module_function("RenderWorld", "light_type", render_world_light_type);
  2834. env.add_module_function("RenderWorld", "light_color", render_world_light_color);
  2835. env.add_module_function("RenderWorld", "light_range", render_world_light_range);
  2836. env.add_module_function("RenderWorld", "light_intensity", render_world_light_intensity);
  2837. env.add_module_function("RenderWorld", "light_spot_angle", render_world_light_spot_angle);
  2838. env.add_module_function("RenderWorld", "light_debug_draw", render_world_light_debug_draw);
  2839. env.add_module_function("RenderWorld", "light_set_type", render_world_light_set_type);
  2840. env.add_module_function("RenderWorld", "light_set_color", render_world_light_set_color);
  2841. env.add_module_function("RenderWorld", "light_set_range", render_world_light_set_range);
  2842. env.add_module_function("RenderWorld", "light_set_intensity", render_world_light_set_intensity);
  2843. env.add_module_function("RenderWorld", "light_set_spot_angle", render_world_light_set_spot_angle);
  2844. env.add_module_function("RenderWorld", "enable_debug_drawing", render_world_enable_debug_drawing);
  2845. env.add_module_function("PhysicsWorld", "actor_instances", physics_world_actor_instances);
  2846. env.add_module_function("PhysicsWorld", "actor_world_position", physics_world_actor_world_position);
  2847. env.add_module_function("PhysicsWorld", "actor_world_rotation", physics_world_actor_world_rotation);
  2848. env.add_module_function("PhysicsWorld", "actor_world_pose", physics_world_actor_world_pose);
  2849. env.add_module_function("PhysicsWorld", "actor_teleport_world_position", physics_world_actor_teleport_world_position);
  2850. env.add_module_function("PhysicsWorld", "actor_teleport_world_rotation", physics_world_actor_teleport_world_rotation);
  2851. env.add_module_function("PhysicsWorld", "actor_teleport_world_pose", physics_world_actor_teleport_world_pose);
  2852. env.add_module_function("PhysicsWorld", "actor_center_of_mass", physics_world_actor_center_of_mass);
  2853. env.add_module_function("PhysicsWorld", "actor_enable_gravity", physics_world_actor_enable_gravity);
  2854. env.add_module_function("PhysicsWorld", "actor_disable_gravity", physics_world_actor_disable_gravity);
  2855. env.add_module_function("PhysicsWorld", "actor_enable_collision", physics_world_actor_enable_collision);
  2856. env.add_module_function("PhysicsWorld", "actor_disable_collision", physics_world_actor_disable_collision);
  2857. env.add_module_function("PhysicsWorld", "actor_set_collision_filter", physics_world_actor_set_collision_filter);
  2858. env.add_module_function("PhysicsWorld", "actor_set_kinematic", physics_world_actor_set_kinematic);
  2859. env.add_module_function("PhysicsWorld", "actor_move", physics_world_actor_move);
  2860. env.add_module_function("PhysicsWorld", "actor_is_static", physics_world_actor_is_static);
  2861. env.add_module_function("PhysicsWorld", "actor_is_dynamic", physics_world_actor_is_dynamic);
  2862. env.add_module_function("PhysicsWorld", "actor_is_kinematic", physics_world_actor_is_kinematic);
  2863. env.add_module_function("PhysicsWorld", "actor_is_nonkinematic", physics_world_actor_is_nonkinematic);
  2864. env.add_module_function("PhysicsWorld", "actor_linear_damping", physics_world_actor_linear_damping);
  2865. env.add_module_function("PhysicsWorld", "actor_set_linear_damping", physics_world_actor_set_linear_damping);
  2866. env.add_module_function("PhysicsWorld", "actor_angular_damping", physics_world_actor_angular_damping);
  2867. env.add_module_function("PhysicsWorld", "actor_set_angular_damping", physics_world_actor_set_angular_damping);
  2868. env.add_module_function("PhysicsWorld", "actor_linear_velocity", physics_world_actor_linear_velocity);
  2869. env.add_module_function("PhysicsWorld", "actor_set_linear_velocity", physics_world_actor_set_linear_velocity);
  2870. env.add_module_function("PhysicsWorld", "actor_angular_velocity", physics_world_actor_angular_velocity);
  2871. env.add_module_function("PhysicsWorld", "actor_set_angular_velocity", physics_world_actor_set_angular_velocity);
  2872. env.add_module_function("PhysicsWorld", "actor_add_impulse", physics_world_actor_add_impulse);
  2873. env.add_module_function("PhysicsWorld", "actor_add_impulse_at", physics_world_actor_add_impulse_at);
  2874. env.add_module_function("PhysicsWorld", "actor_add_actor_torque_impulse",physics_world_actor_add_actor_torque_impulse);
  2875. env.add_module_function("PhysicsWorld", "actor_push", physics_world_actor_push);
  2876. env.add_module_function("PhysicsWorld", "actor_push_at", physics_world_actor_push_at);
  2877. env.add_module_function("PhysicsWorld", "actor_is_sleeping", physics_world_actor_is_sleeping);
  2878. env.add_module_function("PhysicsWorld", "actor_wake_up", physics_world_actor_wake_up);
  2879. env.add_module_function("PhysicsWorld", "controller_instances", physics_world_controller_instances);
  2880. env.add_module_function("PhysicsWorld", "controller_move", physics_world_controller_move);
  2881. env.add_module_function("PhysicsWorld", "joint_create", physics_world_joint_create);
  2882. env.add_module_function("PhysicsWorld", "gravity", physics_world_gravity);
  2883. env.add_module_function("PhysicsWorld", "set_gravity", physics_world_set_gravity);
  2884. env.add_module_function("PhysicsWorld", "raycast", physics_world_raycast);
  2885. env.add_module_function("PhysicsWorld", "enable_debug_drawing", physics_world_enable_debug_drawing);
  2886. env.add_module_function("PhysicsWorld", "__index", "PhysicsWorld");
  2887. env.add_module_function("PhysicsWorld", "__tostring", physics_world_tostring);
  2888. env.add_module_function("SoundWorld", "stop_all", sound_world_stop_all);
  2889. env.add_module_function("SoundWorld", "pause_all", sound_world_pause_all);
  2890. env.add_module_function("SoundWorld", "resume_all", sound_world_resume_all);
  2891. env.add_module_function("SoundWorld", "is_playing", sound_world_is_playing);
  2892. env.add_module_function("SoundWorld", "__index", "SoundWorld");
  2893. env.add_module_function("SoundWorld", "__tostring", sound_world_tostring);
  2894. env.add_module_function("Device", "argv", device_argv);
  2895. env.add_module_function("Device", "platform", device_platform);
  2896. env.add_module_function("Device", "architecture", device_architecture);
  2897. env.add_module_function("Device", "version", device_version);
  2898. env.add_module_function("Device", "last_delta_time", device_last_delta_time);
  2899. env.add_module_function("Device", "quit", device_quit);
  2900. env.add_module_function("Device", "resolution", device_resolution);
  2901. env.add_module_function("Device", "create_world", device_create_world);
  2902. env.add_module_function("Device", "destroy_world", device_destroy_world);
  2903. env.add_module_function("Device", "render", device_render);
  2904. env.add_module_function("Device", "create_resource_package", device_create_resource_package);
  2905. env.add_module_function("Device", "destroy_resource_package", device_destroy_resource_package);
  2906. env.add_module_function("Device", "console_send", device_console_send);
  2907. env.add_module_function("Device", "can_get", device_can_get);
  2908. env.add_module_function("Device", "enable_resource_autoload", device_enable_resource_autoload);
  2909. env.add_module_function("Device", "temp_count", device_temp_count);
  2910. env.add_module_function("Device", "set_temp_count", device_set_temp_count);
  2911. env.add_module_function("Device", "guid", device_guid);
  2912. env.add_module_function("Profiler", "enter_scope", profiler_enter_scope);
  2913. env.add_module_function("Profiler", "leave_scope", profiler_leave_scope);
  2914. env.add_module_function("Profiler", "record", profiler_record);
  2915. env.add_module_function("DebugLine", "add_line", debug_line_add_line);
  2916. env.add_module_function("DebugLine", "add_axes", debug_line_add_axes);
  2917. env.add_module_function("DebugLine", "add_circle", debug_line_add_circle);
  2918. env.add_module_function("DebugLine", "add_cone", debug_line_add_cone);
  2919. env.add_module_function("DebugLine", "add_sphere", debug_line_add_sphere);
  2920. env.add_module_function("DebugLine", "add_obb", debug_line_add_obb);
  2921. env.add_module_function("DebugLine", "add_unit", debug_line_add_unit);
  2922. env.add_module_function("DebugLine", "reset", debug_line_reset);
  2923. env.add_module_function("DebugLine", "submit", debug_line_submit);
  2924. env.add_module_function("DebugLine", "__index", "DebugLine");
  2925. env.add_module_function("DebugLine", "__tostring", debug_line_tostring);
  2926. env.add_module_function("ResourcePackage", "load", resource_package_load);
  2927. env.add_module_function("ResourcePackage", "unload", resource_package_unload);
  2928. env.add_module_function("ResourcePackage", "flush", resource_package_flush);
  2929. env.add_module_function("ResourcePackage", "has_loaded", resource_package_has_loaded);
  2930. env.add_module_function("ResourcePackage", "__index", "ResourcePackage");
  2931. env.add_module_function("ResourcePackage", "__tostring", resource_package_tostring);
  2932. env.add_module_function("Material", "set_float", material_set_float);
  2933. env.add_module_function("Material", "set_vector2", material_set_vector2);
  2934. env.add_module_function("Material", "set_vector3", material_set_vector3);
  2935. env.add_module_function("DebugGui", "resolution", gui_resolution);
  2936. env.add_module_function("DebugGui", "move", gui_move);
  2937. env.add_module_function("DebugGui", "screen_to_gui", gui_screen_to_gui);
  2938. env.add_module_function("DebugGui", "rect", gui_rect);
  2939. env.add_module_function("DebugGui", "image", gui_image);
  2940. env.add_module_function("DebugGui", "image_uv", gui_image_uv);
  2941. env.add_module_function("DebugGui", "text", gui_text);
  2942. env.add_module_function("Display", "modes", display_modes);
  2943. env.add_module_function("Display", "set_mode", display_set_mode);
  2944. env.add_module_function("Window", "show", window_show);
  2945. env.add_module_function("Window", "hide", window_hide);
  2946. env.add_module_function("Window", "resize", window_resize);
  2947. env.add_module_function("Window", "move", window_move);
  2948. env.add_module_function("Window", "minimize", window_minimize);
  2949. env.add_module_function("Window", "restore", window_restore);
  2950. env.add_module_function("Window", "title", window_title);
  2951. env.add_module_function("Window", "set_title", window_set_title);
  2952. env.add_module_function("Window", "show_cursor", window_show_cursor);
  2953. }
  2954. } // namespace crown