lua_api.cpp 95 KB

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