lua_api.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  1. /*
  2. * Copyright (c) 2012-2023 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "core/containers/hash_set.inl"
  6. #include "core/guid.h"
  7. #include "core/math/color4.inl"
  8. #include "core/math/constants.h"
  9. #include "core/math/intersection.h"
  10. #include "core/math/math.h"
  11. #include "core/math/matrix4x4.inl"
  12. #include "core/math/obb.inl"
  13. #include "core/math/plane3.inl"
  14. #include "core/math/quaternion.inl"
  15. #include "core/math/types.h"
  16. #include "core/math/vector2.inl"
  17. #include "core/math/vector3.inl"
  18. #include "core/memory/temp_allocator.inl"
  19. #include "core/strings/dynamic_string.inl"
  20. #include "core/strings/string_id.inl"
  21. #include "core/strings/string_stream.inl"
  22. #include "device/console_server.h"
  23. #include "device/device.h"
  24. #include "device/input_device.h"
  25. #include "device/input_manager.h"
  26. #include "device/profiler.h"
  27. #include "lua/lua_environment.h"
  28. #include "lua/lua_stack.inl"
  29. #include "resource/resource_id.inl"
  30. #include "resource/resource_manager.h"
  31. #include "resource/resource_package.h"
  32. #include "world/animation_state_machine.h"
  33. #include "world/debug_line.h"
  34. #include "world/gui.h"
  35. #include "world/material.h"
  36. #include "world/physics_world.h"
  37. #include "world/render_world.h"
  38. #include "world/scene_graph.h"
  39. #include "world/sound_world.h"
  40. #include "world/unit_manager.h"
  41. #include "world/world.h"
  42. namespace crown
  43. {
  44. struct LightInfo
  45. {
  46. const char *name;
  47. LightType::Enum type;
  48. };
  49. static const LightInfo s_light[] =
  50. {
  51. { "directional", LightType::DIRECTIONAL },
  52. { "omni", LightType::OMNI },
  53. { "spot", LightType::SPOT }
  54. };
  55. CE_STATIC_ASSERT(countof(s_light) == LightType::COUNT);
  56. struct ProjectionInfo
  57. {
  58. const char *name;
  59. ProjectionType::Enum type;
  60. };
  61. static const ProjectionInfo s_projection[] =
  62. {
  63. { "orthographic", ProjectionType::ORTHOGRAPHIC },
  64. { "perspective", ProjectionType::PERSPECTIVE }
  65. };
  66. CE_STATIC_ASSERT(countof(s_projection) == ProjectionType::COUNT);
  67. struct MouseCursorInfo
  68. {
  69. const char *name;
  70. MouseCursor::Enum type;
  71. };
  72. static const MouseCursorInfo s_cursor[] =
  73. {
  74. { "arrow", MouseCursor::ARROW },
  75. { "hand", MouseCursor::HAND },
  76. { "text_input", MouseCursor::TEXT_INPUT },
  77. { "corner_top_left", MouseCursor::CORNER_TOP_LEFT },
  78. { "corner_top_right", MouseCursor::CORNER_TOP_RIGHT },
  79. { "corner_bottom_left", MouseCursor::CORNER_BOTTOM_LEFT },
  80. { "corner_bottom_right", MouseCursor::CORNER_BOTTOM_RIGHT },
  81. { "size_horizontal", MouseCursor::SIZE_HORIZONTAL },
  82. { "size_vertical", MouseCursor::SIZE_VERTICAL },
  83. { "wait", MouseCursor::WAIT }
  84. };
  85. CE_STATIC_ASSERT(countof(s_cursor) == MouseCursor::COUNT);
  86. struct CursorModeInfo
  87. {
  88. const char *name;
  89. CursorMode::Enum type;
  90. };
  91. static const CursorModeInfo s_mode[] =
  92. {
  93. { "disabled", CursorMode::DISABLED },
  94. { "normal", CursorMode::NORMAL }
  95. };
  96. CE_STATIC_ASSERT(countof(s_mode) == CursorMode::COUNT);
  97. static LightType::Enum name_to_light_type(const char *name)
  98. {
  99. for (u32 i = 0; i < countof(s_light); ++i) {
  100. if (strcmp(s_light[i].name, name) == 0)
  101. return s_light[i].type;
  102. }
  103. return LightType::COUNT;
  104. }
  105. static ProjectionType::Enum name_to_projection_type(const char *name)
  106. {
  107. for (u32 i = 0; i < countof(s_projection); ++i) {
  108. if (strcmp(s_projection[i].name, name) == 0)
  109. return s_projection[i].type;
  110. }
  111. return ProjectionType::COUNT;
  112. }
  113. static MouseCursor::Enum name_to_mouse_cursor(const char *name)
  114. {
  115. for (u32 i = 0; i < countof(s_cursor); ++i) {
  116. if (strcmp(s_cursor[i].name, name) == 0)
  117. return s_cursor[i].type;
  118. }
  119. return MouseCursor::COUNT;
  120. }
  121. static CursorMode::Enum name_to_cursor_mode(const char *name)
  122. {
  123. for (u32 i = 0; i < countof(s_mode); ++i) {
  124. if (strcmp(s_mode[i].name, name) == 0)
  125. return s_mode[i].type;
  126. }
  127. return CursorMode::COUNT;
  128. }
  129. static int vector3box_store(lua_State *L)
  130. {
  131. LuaStack stack(L);
  132. Vector3 &v = stack.get_vector3box(1);
  133. if (stack.num_args() == 2)
  134. v = stack.get_vector3(2);
  135. else
  136. v = vector3(stack.get_float(2)
  137. , stack.get_float(3)
  138. , stack.get_float(4));
  139. return 0;
  140. }
  141. static int vector3box_unbox(lua_State *L)
  142. {
  143. LuaStack stack(L);
  144. stack.push_vector3(stack.get_vector3box(1));
  145. return 1;
  146. }
  147. static int matrix4x4box_store(lua_State *L)
  148. {
  149. LuaStack stack(L);
  150. stack.get_matrix4x4box(1) = stack.get_matrix4x4(2);
  151. return 0;
  152. }
  153. static int matrix4x4box_unbox(lua_State *L)
  154. {
  155. LuaStack stack(L);
  156. stack.push_matrix4x4(stack.get_matrix4x4box(1));
  157. return 1;
  158. }
  159. static int quaternion_to_string(lua_State *L)
  160. {
  161. LuaStack stack(L);
  162. char buf[256];
  163. stack.push_string(to_string(buf, sizeof(buf), stack.get_quaternion(1)));
  164. return 1;
  165. }
  166. static int quaternionbox_store(lua_State *L)
  167. {
  168. LuaStack stack(L);
  169. Quaternion &q = stack.get_quaternionbox(1);
  170. if (stack.num_args() == 2)
  171. q = stack.get_quaternion(2);
  172. else
  173. q = from_elements(stack.get_float(2)
  174. , stack.get_float(3)
  175. , stack.get_float(4)
  176. , stack.get_float(5)
  177. );
  178. return 0;
  179. }
  180. static int quaternionbox_unbox(lua_State *L)
  181. {
  182. LuaStack stack(L);
  183. const Quaternion &q = stack.get_quaternionbox(1);
  184. stack.push_quaternion(q);
  185. return 1;
  186. }
  187. static int input_device_name(lua_State *L, const InputDevice &dev)
  188. {
  189. LuaStack stack(L);
  190. stack.push_string(dev.name());
  191. return 1;
  192. }
  193. static int input_device_connected(lua_State *L, const InputDevice &dev)
  194. {
  195. LuaStack stack(L);
  196. stack.push_bool(dev.connected());
  197. return 1;
  198. }
  199. static int input_device_num_buttons(lua_State *L, const InputDevice &dev)
  200. {
  201. LuaStack stack(L);
  202. stack.push_int(dev.num_buttons());
  203. return 1;
  204. }
  205. static int input_device_num_axes(lua_State *L, const InputDevice &dev)
  206. {
  207. LuaStack stack(L);
  208. stack.push_int(dev.num_axes());
  209. return 1;
  210. }
  211. static int input_device_pressed(lua_State *L, const InputDevice &dev)
  212. {
  213. LuaStack stack(L);
  214. stack.push_bool(dev.pressed(stack.get_int(1)));
  215. return 1;
  216. }
  217. static int input_device_released(lua_State *L, const InputDevice &dev)
  218. {
  219. LuaStack stack(L);
  220. stack.push_bool(dev.released(stack.get_int(1)));
  221. return 1;
  222. }
  223. static int input_device_any_pressed(lua_State *L, const InputDevice &dev)
  224. {
  225. LuaStack stack(L);
  226. const u8 button = dev.any_pressed();
  227. if (button != UINT8_MAX)
  228. stack.push_int(button);
  229. else
  230. stack.push_nil();
  231. return 1;
  232. }
  233. static int input_device_any_released(lua_State *L, const InputDevice &dev)
  234. {
  235. LuaStack stack(L);
  236. const u8 button = dev.any_released();
  237. if (button != UINT8_MAX)
  238. stack.push_int(button);
  239. else
  240. stack.push_nil();
  241. return 1;
  242. }
  243. static int input_device_button(lua_State *L, const InputDevice &dev)
  244. {
  245. LuaStack stack(L);
  246. stack.push_float(dev.button(stack.get_int(1)));
  247. return 1;
  248. }
  249. static int input_device_axis(lua_State *L, const InputDevice &dev)
  250. {
  251. LuaStack stack(L);
  252. stack.push_vector3(dev.axis(stack.get_int(1)));
  253. return 1;
  254. }
  255. static int input_device_button_name(lua_State *L, const InputDevice &dev)
  256. {
  257. LuaStack stack(L);
  258. const char *name = dev.button_name(stack.get_int(1));
  259. if (name != NULL)
  260. stack.push_string(name);
  261. else
  262. stack.push_nil();
  263. return 1;
  264. }
  265. static int input_device_axis_name(lua_State *L, const InputDevice &dev)
  266. {
  267. LuaStack stack(L);
  268. const char *name = dev.axis_name(stack.get_int(1));
  269. if (name != NULL)
  270. stack.push_string(name);
  271. else
  272. stack.push_nil();
  273. return 1;
  274. }
  275. static int input_device_button_id(lua_State *L, const InputDevice &dev)
  276. {
  277. LuaStack stack(L);
  278. const u8 id = dev.button_id(stack.get_string_id_32(1));
  279. if (id != UINT8_MAX)
  280. stack.push_int(id);
  281. else
  282. stack.push_nil();
  283. return 1;
  284. }
  285. static int input_device_axis_id(lua_State *L, const InputDevice &dev)
  286. {
  287. LuaStack stack(L);
  288. const u8 id = dev.axis_id(stack.get_string_id_32(1));
  289. if (id != UINT8_MAX)
  290. stack.push_int(id);
  291. else
  292. stack.push_nil();
  293. return 1;
  294. }
  295. static int input_device_deadzone(lua_State *L, const InputDevice &dev)
  296. {
  297. LuaStack stack(L);
  298. int axis_id = stack.get_int(1);
  299. DeadzoneMode::Enum mode;
  300. f32 size = dev.deadzone(axis_id, &mode);
  301. if (mode != DeadzoneMode::COUNT) {
  302. stack.push_int(mode);
  303. stack.push_float(size);
  304. } else {
  305. stack.push_nil();
  306. stack.push_nil();
  307. }
  308. return 2;
  309. }
  310. static int input_device_set_deadzone(lua_State *L, InputDevice &dev)
  311. {
  312. LuaStack stack(L);
  313. int axis_id = stack.get_int(1);
  314. DeadzoneMode::Enum mode = (DeadzoneMode::Enum)stack.get_int(2);
  315. f32 size = stack.get_float(3);
  316. dev.set_deadzone(axis_id, mode, size);
  317. return 0;
  318. }
  319. static void lua_dump_table(lua_State *L, int i, StringStream &json)
  320. {
  321. LuaStack stack(L);
  322. bool comma = false;
  323. int array_index = 1;
  324. json << "{";
  325. stack.push_nil();
  326. while (stack.next(i) != 0) {
  327. if (comma)
  328. json << ",";
  329. comma = true;
  330. json << "\"";
  331. if (stack.is_string(-2) && !stack.is_number(-2))
  332. json << stack.get_string(-2);
  333. else
  334. json << array_index++;
  335. json << "\":";
  336. if (stack.is_nil(i + 2)) {
  337. json << "null";
  338. } else if (stack.is_bool(i + 2)) {
  339. const bool b = stack.get_bool(i + 2);
  340. json << (b ? "true" : "false");
  341. } else if (stack.is_number(i + 2)) {
  342. json << stack.get_float(i + 2);
  343. } else if (stack.is_string(i + 2)) {
  344. const char *str = stack.get_string(i + 2);
  345. json << "\"";
  346. for (; *str; ++str) {
  347. if (*str == '"' || *str == '\\')
  348. json << '\\';
  349. json << *str;
  350. }
  351. json << "\"";
  352. } else if (stack.is_vector3(i + 2)) {
  353. const Vector3 v = stack.get_vector3(i + 2);
  354. json << "[";
  355. json << v.x << ",";
  356. json << v.y << ",";
  357. json << v.z;
  358. json << "]";
  359. } else if (stack.is_quaternion(i + 2)) {
  360. const Quaternion q = stack.get_quaternion(i + 2);
  361. json << "[";
  362. json << q.x << ",";
  363. json << q.y << ",";
  364. json << q.z << ",";
  365. json << q.w;
  366. json << "]";
  367. } else if (stack.is_matrix4x4(i + 2)) {
  368. const Matrix4x4 m = stack.get_matrix4x4(i + 2);
  369. json << "[";
  370. json << m.x.x << ",";
  371. json << m.x.y << ",";
  372. json << m.x.z << ",";
  373. json << m.x.w << ",";
  374. json << m.y.x << ",";
  375. json << m.y.y << ",";
  376. json << m.y.z << ",";
  377. json << m.y.w << ",";
  378. json << m.z.x << ",";
  379. json << m.z.y << ",";
  380. json << m.z.z << ",";
  381. json << m.z.w << ",";
  382. json << m.t.x << ",";
  383. json << m.t.y << ",";
  384. json << m.t.z << ",";
  385. json << m.t.w;
  386. json << "]";
  387. } else if (stack.is_table(i + 2)) {
  388. lua_dump_table(L, i + 2, json);
  389. } else {
  390. LUA_ASSERT(false, stack, "Unsupported key value");
  391. }
  392. stack.pop(1);
  393. }
  394. json << "}";
  395. }
  396. void load_api(LuaEnvironment &env)
  397. {
  398. // code-format off
  399. env.add_module_function("Math", "ray_plane_intersection", [](lua_State *L) {
  400. LuaStack stack(L);
  401. const Plane3 p = plane3::from_point_and_normal(stack.get_vector3(3)
  402. , stack.get_vector3(4)
  403. );
  404. const f32 t = ray_plane_intersection(stack.get_vector3(1)
  405. , stack.get_vector3(2)
  406. , p
  407. );
  408. stack.push_float(t);
  409. return 1;
  410. });
  411. env.add_module_function("Math", "ray_disc_intersection", [](lua_State *L) {
  412. LuaStack stack(L);
  413. const f32 t = ray_disc_intersection(stack.get_vector3(1)
  414. , stack.get_vector3(2)
  415. , stack.get_vector3(3)
  416. , stack.get_float(4)
  417. , stack.get_vector3(5)
  418. );
  419. stack.push_float(t);
  420. return 1;
  421. });
  422. env.add_module_function("Math", "ray_sphere_intersection", [](lua_State *L) {
  423. LuaStack stack(L);
  424. Sphere s;
  425. s.c = stack.get_vector3(3);
  426. s.r = stack.get_float(4);
  427. const f32 t = ray_sphere_intersection(stack.get_vector3(1)
  428. , stack.get_vector3(2)
  429. , s
  430. );
  431. stack.push_float(t);
  432. return 1;
  433. });
  434. env.add_module_function("Math", "ray_obb_intersection", [](lua_State *L) {
  435. LuaStack stack(L);
  436. const f32 t = ray_obb_intersection(stack.get_vector3(1)
  437. , stack.get_vector3(2)
  438. , stack.get_matrix4x4(3)
  439. , stack.get_vector3(4)
  440. );
  441. stack.push_float(t);
  442. return 1;
  443. });
  444. env.add_module_function("Math", "ray_triangle_intersection", [](lua_State *L) {
  445. LuaStack stack(L);
  446. const f32 t = ray_triangle_intersection(stack.get_vector3(1)
  447. , stack.get_vector3(2)
  448. , stack.get_vector3(3)
  449. , stack.get_vector3(4)
  450. , stack.get_vector3(5)
  451. );
  452. stack.push_float(t);
  453. return 1;
  454. });
  455. env.add_module_function("Math", "obb_intersects_frustum", [](lua_State *L) {
  456. LuaStack stack(L);
  457. u32 param_i = 1;
  458. OBB obb;
  459. obb.tm = stack.get_matrix4x4(param_i++);
  460. obb.half_extents = stack.get_vector3(param_i++);
  461. Frustum f;
  462. for (u32 ii = 0; ii < countof(f.planes); ++ii) {
  463. f.planes[ii].n = stack.get_vector3(param_i++);
  464. f.planes[ii].d = stack.get_float(param_i++);
  465. }
  466. stack.push_bool(obb_intersects_frustum(obb, f));
  467. return 1;
  468. });
  469. env.add_module_function("Math", "obb_vertices", [](lua_State *L) {
  470. LuaStack stack(L);
  471. Matrix4x4 tm = stack.get_matrix4x4(1);
  472. Vector3 half_extents = stack.get_vector3(2);
  473. Vector3 vertices[8];
  474. obb::to_vertices(vertices, { tm, half_extents });
  475. for (u32 ii = 0; ii < 8; ++ii)
  476. stack.push_vector3(vertices[ii]);
  477. return 8;
  478. });
  479. env.add_module_function("Vector3", "x", [](lua_State *L) {
  480. LuaStack stack(L);
  481. stack.push_float(stack.get_vector3(1).x);
  482. return 1;
  483. });
  484. env.add_module_function("Vector3", "y", [](lua_State *L) {
  485. LuaStack stack(L);
  486. stack.push_float(stack.get_vector3(1).y);
  487. return 1;
  488. });
  489. env.add_module_function("Vector3", "z", [](lua_State *L) {
  490. LuaStack stack(L);
  491. stack.push_float(stack.get_vector3(1).z);
  492. return 1;
  493. });
  494. env.add_module_function("Vector3", "set_x", [](lua_State *L) {
  495. LuaStack stack(L);
  496. stack.get_vector3(1).x = stack.get_float(2);
  497. return 0;
  498. });
  499. env.add_module_function("Vector3", "set_y", [](lua_State *L) {
  500. LuaStack stack(L);
  501. stack.get_vector3(1).y = stack.get_float(2);
  502. return 0;
  503. });
  504. env.add_module_function("Vector3", "set_z", [](lua_State *L) {
  505. LuaStack stack(L);
  506. stack.get_vector3(1).z = stack.get_float(2);
  507. return 0;
  508. });
  509. env.add_module_function("Vector3", "elements", [](lua_State *L) {
  510. LuaStack stack(L);
  511. const Vector3 &a = stack.get_vector3(1);
  512. stack.push_float(a.x);
  513. stack.push_float(a.y);
  514. stack.push_float(a.z);
  515. return 3;
  516. });
  517. env.add_module_function("Vector3", "add", [](lua_State *L) {
  518. LuaStack stack(L);
  519. stack.push_vector3(stack.get_vector3(1) + stack.get_vector3(2));
  520. return 1;
  521. });
  522. env.add_module_function("Vector3", "subtract", [](lua_State *L) {
  523. LuaStack stack(L);
  524. stack.push_vector3(stack.get_vector3(1) - stack.get_vector3(2));
  525. return 1;
  526. });
  527. env.add_module_function("Vector3", "multiply", [](lua_State *L) {
  528. LuaStack stack(L);
  529. stack.push_vector3(stack.get_vector3(1) * stack.get_float(2));
  530. return 1;
  531. });
  532. env.add_module_function("Vector3", "dot", [](lua_State *L) {
  533. LuaStack stack(L);
  534. stack.push_float(dot(stack.get_vector3(1), stack.get_vector3(2)));
  535. return 1;
  536. });
  537. env.add_module_function("Vector3", "cross", [](lua_State *L) {
  538. LuaStack stack(L);
  539. stack.push_vector3(cross(stack.get_vector3(1), stack.get_vector3(2)));
  540. return 1;
  541. });
  542. env.add_module_function("Vector3", "equal", [](lua_State *L) {
  543. LuaStack stack(L);
  544. stack.push_bool(stack.get_vector3(1) == stack.get_vector3(2));
  545. return 1;
  546. });
  547. env.add_module_function("Vector3", "length", [](lua_State *L) {
  548. LuaStack stack(L);
  549. stack.push_float(length(stack.get_vector3(1)));
  550. return 1;
  551. });
  552. env.add_module_function("Vector3", "length_squared", [](lua_State *L) {
  553. LuaStack stack(L);
  554. stack.push_float(length_squared(stack.get_vector3(1)));
  555. return 1;
  556. });
  557. env.add_module_function("Vector3", "set_length", [](lua_State *L) {
  558. LuaStack stack(L);
  559. set_length(stack.get_vector3(1), stack.get_float(2));
  560. return 0;
  561. });
  562. env.add_module_function("Vector3", "normalize", [](lua_State *L) {
  563. LuaStack stack(L);
  564. stack.push_vector3(normalize(stack.get_vector3(1)));
  565. return 1;
  566. });
  567. env.add_module_function("Vector3", "distance", [](lua_State *L) {
  568. LuaStack stack(L);
  569. stack.push_float(distance(stack.get_vector3(1), stack.get_vector3(2)));
  570. return 1;
  571. });
  572. env.add_module_function("Vector3", "distance_squared", [](lua_State *L) {
  573. LuaStack stack(L);
  574. stack.push_float(distance_squared(stack.get_vector3(1), stack.get_vector3(2)));
  575. return 1;
  576. });
  577. env.add_module_function("Vector3", "angle", [](lua_State *L) {
  578. LuaStack stack(L);
  579. stack.push_float(angle(stack.get_vector3(1), stack.get_vector3(2)));
  580. return 1;
  581. });
  582. env.add_module_function("Vector3", "max", [](lua_State *L) {
  583. LuaStack stack(L);
  584. stack.push_vector3(max(stack.get_vector3(1), stack.get_vector3(2)));
  585. return 1;
  586. });
  587. env.add_module_function("Vector3", "min", [](lua_State *L) {
  588. LuaStack stack(L);
  589. stack.push_vector3(min(stack.get_vector3(1), stack.get_vector3(2)));
  590. return 1;
  591. });
  592. env.add_module_function("Vector3", "lerp", [](lua_State *L) {
  593. LuaStack stack(L);
  594. stack.push_vector3(lerp(stack.get_vector3(1), stack.get_vector3(2), stack.get_float(3)));
  595. return 1;
  596. });
  597. env.add_module_function("Vector3", "forward", [](lua_State *L) {
  598. LuaStack stack(L);
  599. stack.push_vector3(VECTOR3_FORWARD);
  600. return 1;
  601. });
  602. env.add_module_function("Vector3", "backward", [](lua_State *L) {
  603. LuaStack stack(L);
  604. stack.push_vector3(VECTOR3_BACKWARD);
  605. return 1;
  606. });
  607. env.add_module_function("Vector3", "left", [](lua_State *L) {
  608. LuaStack stack(L);
  609. stack.push_vector3(VECTOR3_LEFT);
  610. return 1;
  611. });
  612. env.add_module_function("Vector3", "right", [](lua_State *L) {
  613. LuaStack stack(L);
  614. stack.push_vector3(VECTOR3_RIGHT);
  615. return 1;
  616. });
  617. env.add_module_function("Vector3", "up", [](lua_State *L) {
  618. LuaStack stack(L);
  619. stack.push_vector3(VECTOR3_UP);
  620. return 1;
  621. });
  622. env.add_module_function("Vector3", "down", [](lua_State *L) {
  623. LuaStack stack(L);
  624. stack.push_vector3(VECTOR3_DOWN);
  625. return 1;
  626. });
  627. env.add_module_function("Vector3", "zero", [](lua_State *L) {
  628. LuaStack stack(L);
  629. stack.push_vector3(VECTOR3_ZERO);
  630. return 1;
  631. });
  632. env.add_module_function("Vector3", "to_string", [](lua_State *L) {
  633. LuaStack stack(L);
  634. char buf[256];
  635. stack.push_string(to_string(buf, sizeof(buf), stack.get_vector3(1)));
  636. return 1;
  637. });
  638. env.add_module_metafunction("Vector3", "__call", [](lua_State *L) {
  639. LuaStack stack(L);
  640. Vector3 v;
  641. v.x = stack.get_float(1 + 1);
  642. v.y = stack.get_float(2 + 1);
  643. v.z = stack.get_float(3 + 1);
  644. stack.push_vector3(v);
  645. return 1;
  646. });
  647. env.add_module_metafunction("Vector2", "__call", [](lua_State *L) {
  648. LuaStack stack(L);
  649. Vector2 v;
  650. v.x = stack.get_float(1 + 1);
  651. v.y = stack.get_float(2 + 1);
  652. stack.push_vector2(v);
  653. return 1;
  654. });
  655. env.add_module_function("Vector3Box", "store", vector3box_store);
  656. env.add_module_function("Vector3Box", "unbox", vector3box_unbox);
  657. env.add_module_metafunction("Vector3Box", "store", vector3box_store);
  658. env.add_module_metafunction("Vector3Box", "unbox", vector3box_unbox);
  659. env.add_module_metafunction("Vector3Box", "__index", NULL);
  660. env.add_module_metafunction("Vector3Box", "__tostring", [](lua_State *L) {
  661. LuaStack stack(L);
  662. Vector3 &v = stack.get_vector3box(1);
  663. stack.push_fstring("Vector3Box (%p)", &v);
  664. return 1;
  665. });
  666. env.add_module_metafunction("Vector3Box", "__call", [](lua_State *L) {
  667. LuaStack stack(L);
  668. if (stack.num_args() == 0 + 1) {
  669. stack.push_vector3box(VECTOR3_ZERO);
  670. } else if (stack.num_args() == 1 + 1) {
  671. stack.push_vector3box(stack.get_vector3(1 + 1));
  672. } else {
  673. Vector3 v;
  674. v.x = stack.get_float(1 + 1);
  675. v.y = stack.get_float(2 + 1);
  676. v.z = stack.get_float(3 + 1);
  677. stack.push_vector3box(v);
  678. }
  679. return 1;
  680. });
  681. env.add_module_function("Matrix4x4", "from_quaternion", [](lua_State *L) {
  682. LuaStack stack(L);
  683. stack.push_matrix4x4(from_quaternion_translation(stack.get_quaternion(1), VECTOR3_ZERO));
  684. return 1;
  685. });
  686. env.add_module_function("Matrix4x4", "from_translation", [](lua_State *L) {
  687. LuaStack stack(L);
  688. stack.push_matrix4x4(from_translation(stack.get_vector3(1)));
  689. return 1;
  690. });
  691. env.add_module_function("Matrix4x4", "from_quaternion_translation", [](lua_State *L) {
  692. LuaStack stack(L);
  693. stack.push_matrix4x4(from_quaternion_translation(stack.get_quaternion(1), stack.get_vector3(2)));
  694. return 1;
  695. });
  696. env.add_module_function("Matrix4x4", "from_axes", [](lua_State *L) {
  697. LuaStack stack(L);
  698. stack.push_matrix4x4(from_axes(stack.get_vector3(1), stack.get_vector3(2), stack.get_vector3(3), stack.get_vector3(4)));
  699. return 1;
  700. });
  701. env.add_module_function("Matrix4x4", "copy", [](lua_State *L) {
  702. LuaStack stack(L);
  703. stack.push_matrix4x4(stack.get_matrix4x4(1));
  704. return 1;
  705. });
  706. env.add_module_function("Matrix4x4", "add", [](lua_State *L) {
  707. LuaStack stack(L);
  708. stack.push_matrix4x4(stack.get_matrix4x4(1) + stack.get_matrix4x4(2));
  709. return 1;
  710. });
  711. env.add_module_function("Matrix4x4", "subtract", [](lua_State *L) {
  712. LuaStack stack(L);
  713. stack.push_matrix4x4(stack.get_matrix4x4(1) - stack.get_matrix4x4(2));
  714. return 1;
  715. });
  716. env.add_module_function("Matrix4x4", "multiply", [](lua_State *L) {
  717. LuaStack stack(L);
  718. stack.push_matrix4x4(stack.get_matrix4x4(1) * stack.get_matrix4x4(2));
  719. return 1;
  720. });
  721. env.add_module_function("Matrix4x4", "equal", [](lua_State *L) {
  722. LuaStack stack(L);
  723. stack.push_bool(stack.get_matrix4x4(1) == stack.get_matrix4x4(2));
  724. return 1;
  725. });
  726. env.add_module_function("Matrix4x4", "transpose", [](lua_State *L) {
  727. LuaStack stack(L);
  728. stack.push_matrix4x4(transpose(stack.get_matrix4x4(1)));
  729. return 1;
  730. });
  731. env.add_module_function("Matrix4x4", "invert", [](lua_State *L) {
  732. LuaStack stack(L);
  733. stack.push_matrix4x4(invert(stack.get_matrix4x4(1)));
  734. return 1;
  735. });
  736. env.add_module_function("Matrix4x4", "x", [](lua_State *L) {
  737. LuaStack stack(L);
  738. stack.push_vector3(x(stack.get_matrix4x4(1)));
  739. return 1;
  740. });
  741. env.add_module_function("Matrix4x4", "y", [](lua_State *L) {
  742. LuaStack stack(L);
  743. stack.push_vector3(y(stack.get_matrix4x4(1)));
  744. return 1;
  745. });
  746. env.add_module_function("Matrix4x4", "z", [](lua_State *L) {
  747. LuaStack stack(L);
  748. stack.push_vector3(z(stack.get_matrix4x4(1)));
  749. return 1;
  750. });
  751. env.add_module_function("Matrix4x4", "set_x", [](lua_State *L) {
  752. LuaStack stack(L);
  753. set_x(stack.get_matrix4x4(1), stack.get_vector3(2));
  754. return 0;
  755. });
  756. env.add_module_function("Matrix4x4", "set_y", [](lua_State *L) {
  757. LuaStack stack(L);
  758. set_y(stack.get_matrix4x4(1), stack.get_vector3(2));
  759. return 0;
  760. });
  761. env.add_module_function("Matrix4x4", "set_z", [](lua_State *L) {
  762. LuaStack stack(L);
  763. set_z(stack.get_matrix4x4(1), stack.get_vector3(2));
  764. return 0;
  765. });
  766. env.add_module_function("Matrix4x4", "translation", [](lua_State *L) {
  767. LuaStack stack(L);
  768. stack.push_vector3(translation(stack.get_matrix4x4(1)));
  769. return 1;
  770. });
  771. env.add_module_function("Matrix4x4", "set_translation", [](lua_State *L) {
  772. LuaStack stack(L);
  773. set_translation(stack.get_matrix4x4(1), stack.get_vector3(2));
  774. return 0;
  775. });
  776. env.add_module_function("Matrix4x4", "rotation", [](lua_State *L) {
  777. LuaStack stack(L);
  778. stack.push_quaternion(rotation(stack.get_matrix4x4(1)));
  779. return 1;
  780. });
  781. env.add_module_function("Matrix4x4", "set_rotation", [](lua_State *L) {
  782. LuaStack stack(L);
  783. set_rotation(stack.get_matrix4x4(1), stack.get_quaternion(2));
  784. return 0;
  785. });
  786. env.add_module_function("Matrix4x4", "scale", [](lua_State *L) {
  787. LuaStack stack(L);
  788. stack.push_vector3(scale(stack.get_matrix4x4(1)));
  789. return 1;
  790. });
  791. env.add_module_function("Matrix4x4", "set_scale", [](lua_State *L) {
  792. LuaStack stack(L);
  793. set_scale(stack.get_matrix4x4(1), stack.get_vector3(2));
  794. return 0;
  795. });
  796. env.add_module_function("Matrix4x4", "identity", [](lua_State *L) {
  797. LuaStack stack(L);
  798. stack.push_matrix4x4(MATRIX4X4_IDENTITY);
  799. return 1;
  800. });
  801. env.add_module_function("Matrix4x4", "transform", [](lua_State *L) {
  802. LuaStack stack(L);
  803. stack.push_vector3(stack.get_vector3(2) * stack.get_matrix4x4(1));
  804. return 1;
  805. });
  806. env.add_module_function("Matrix4x4", "to_string", [](lua_State *L) {
  807. LuaStack stack(L);
  808. char buf[1024];
  809. stack.push_string(to_string(buf, sizeof(buf), stack.get_matrix4x4(1)));
  810. return 1;
  811. });
  812. env.add_module_metafunction("Matrix4x4", "__call", [](lua_State *L) {
  813. LuaStack stack(L);
  814. Matrix4x4 m;
  815. m.x.x = stack.get_float(1 + 1);
  816. m.x.y = stack.get_float(2 + 1);
  817. m.x.z = stack.get_float(3 + 1);
  818. m.x.w = stack.get_float(4 + 1);
  819. m.y.x = stack.get_float(5 + 1);
  820. m.y.y = stack.get_float(6 + 1);
  821. m.y.z = stack.get_float(7 + 1);
  822. m.y.w = stack.get_float(8 + 1);
  823. m.z.x = stack.get_float(9 + 1);
  824. m.z.y = stack.get_float(10 + 1);
  825. m.z.z = stack.get_float(11 + 1);
  826. m.z.w = stack.get_float(12 + 1);
  827. m.t.x = stack.get_float(13 + 1);
  828. m.t.y = stack.get_float(14 + 1);
  829. m.t.z = stack.get_float(15 + 1);
  830. m.t.w = stack.get_float(16 + 1);
  831. stack.push_matrix4x4(m);
  832. return 1;
  833. });
  834. env.add_module_function("Matrix4x4Box", "store", matrix4x4box_store);
  835. env.add_module_function("Matrix4x4Box", "unbox", matrix4x4box_unbox);
  836. env.add_module_metafunction("Matrix4x4Box", "store", matrix4x4box_store);
  837. env.add_module_metafunction("Matrix4x4Box", "unbox", matrix4x4box_unbox);
  838. env.add_module_metafunction("Matrix4x4Box", "__index", NULL);
  839. env.add_module_metafunction("Matrix4x4Box", "__tostring", [](lua_State *L) {
  840. LuaStack stack(L);
  841. Matrix4x4 &m = stack.get_matrix4x4box(1);
  842. stack.push_fstring("Matrix4x4Box (%p)", &m);
  843. return 1;
  844. });
  845. env.add_module_metafunction("Matrix4x4Box", "__call", [](lua_State *L) {
  846. LuaStack stack(L);
  847. if (stack.num_args() == 0 + 1)
  848. stack.push_matrix4x4box(MATRIX4X4_IDENTITY);
  849. else
  850. stack.push_matrix4x4box(stack.get_matrix4x4(1 + 1));
  851. return 1;
  852. });
  853. env.add_module_function("Quaternion", "from_axis_angle", [](lua_State *L) {
  854. LuaStack stack(L);
  855. stack.push_quaternion(from_axis_angle(stack.get_vector3(1), stack.get_float(2)));
  856. return 1;
  857. });
  858. env.add_module_function("Quaternion", "from_elements", [](lua_State *L) {
  859. LuaStack stack(L);
  860. stack.push_quaternion(from_elements(stack.get_float(1), stack.get_float(2), stack.get_float(3), stack.get_float(4)));
  861. return 1;
  862. });
  863. env.add_module_function("Quaternion", "negate", [](lua_State *L) {
  864. LuaStack stack(L);
  865. stack.push_quaternion(-stack.get_quaternion(1));
  866. return 1;
  867. });
  868. env.add_module_function("Quaternion", "identity", [](lua_State *L) {
  869. LuaStack stack(L);
  870. stack.push_quaternion(QUATERNION_IDENTITY);
  871. return 1;
  872. });
  873. env.add_module_function("Quaternion", "multiply", [](lua_State *L) {
  874. LuaStack stack(L);
  875. stack.push_quaternion(stack.get_quaternion(1) * stack.get_quaternion(2));
  876. return 1;
  877. });
  878. env.add_module_function("Quaternion", "multiply_by_scalar", [](lua_State *L) {
  879. LuaStack stack(L);
  880. stack.push_quaternion(stack.get_quaternion(1) * stack.get_float(2));
  881. return 1;
  882. });
  883. env.add_module_function("Quaternion", "dot", [](lua_State *L) {
  884. LuaStack stack(L);
  885. stack.push_float(dot(stack.get_quaternion(1), stack.get_quaternion(2)));
  886. return 1;
  887. });
  888. env.add_module_function("Quaternion", "length", [](lua_State *L) {
  889. LuaStack stack(L);
  890. stack.push_float(length(stack.get_quaternion(1)));
  891. return 1;
  892. });
  893. env.add_module_function("Quaternion", "normalize", [](lua_State *L) {
  894. LuaStack stack(L);
  895. stack.push_quaternion(normalize(stack.get_quaternion(1)));
  896. return 1;
  897. });
  898. env.add_module_function("Quaternion", "conjugate", [](lua_State *L) {
  899. LuaStack stack(L);
  900. stack.push_quaternion(conjugate(stack.get_quaternion(1)));
  901. return 1;
  902. });
  903. env.add_module_function("Quaternion", "inverse", [](lua_State *L) {
  904. LuaStack stack(L);
  905. stack.push_quaternion(inverse(stack.get_quaternion(1)));
  906. return 1;
  907. });
  908. env.add_module_function("Quaternion", "power", [](lua_State *L) {
  909. LuaStack stack(L);
  910. stack.push_quaternion(power(stack.get_quaternion(1), stack.get_float(2)));
  911. return 1;
  912. });
  913. env.add_module_function("Quaternion", "elements", [](lua_State *L) {
  914. LuaStack stack(L);
  915. const Quaternion &q = stack.get_quaternion(1);
  916. stack.push_float(q.x);
  917. stack.push_float(q.y);
  918. stack.push_float(q.z);
  919. stack.push_float(q.w);
  920. return 4;
  921. });
  922. env.add_module_function("Quaternion", "look", [](lua_State *L) {
  923. LuaStack stack(L);
  924. const Vector3 up = stack.num_args() == 2 ? stack.get_vector3(2) : VECTOR3_YAXIS;
  925. stack.push_quaternion(look(stack.get_vector3(1), up));
  926. return 1;
  927. });
  928. env.add_module_function("Quaternion", "right", [](lua_State *L) {
  929. LuaStack stack(L);
  930. stack.push_vector3(right(stack.get_quaternion(1)));
  931. return 1;
  932. });
  933. env.add_module_function("Quaternion", "up", [](lua_State *L) {
  934. LuaStack stack(L);
  935. stack.push_vector3(up(stack.get_quaternion(1)));
  936. return 1;
  937. });
  938. env.add_module_function("Quaternion", "forward", [](lua_State *L) {
  939. LuaStack stack(L);
  940. stack.push_vector3(forward(stack.get_quaternion(1)));
  941. return 1;
  942. });
  943. env.add_module_function("Quaternion", "lerp", [](lua_State *L) {
  944. LuaStack stack(L);
  945. stack.push_quaternion(lerp(stack.get_quaternion(1), stack.get_quaternion(2), stack.get_float(3)));
  946. return 1;
  947. });
  948. env.add_module_function("Quaternion", "to_string", quaternion_to_string);
  949. env.add_module_metafunction("Quaternion", "__call", [](lua_State *L) {
  950. LuaStack stack(L);
  951. stack.push_quaternion(from_axis_angle(stack.get_vector3(1 + 1), stack.get_float(2 + 1)));
  952. return 1;
  953. });
  954. env.add_module_function("QuaternionBox", "store", quaternionbox_store);
  955. env.add_module_function("QuaternionBox", "unbox", quaternionbox_unbox);
  956. env.add_module_metafunction("QuaternionBox", "store", quaternionbox_store);
  957. env.add_module_metafunction("QuaternionBox", "unbox", quaternionbox_unbox);
  958. env.add_module_metafunction("QuaternionBox", "__index", NULL);
  959. env.add_module_metafunction("QuaternionBox", "__tostring", [](lua_State *L) {
  960. LuaStack stack(L);
  961. Quaternion &q = stack.get_quaternionbox(1);
  962. stack.push_fstring("QuaternionBox (%p)", &q);
  963. return 1;
  964. });
  965. env.add_module_metafunction("QuaternionBox", "__call", [](lua_State *L) {
  966. LuaStack stack(L);
  967. if (stack.num_args() == 0 + 1) {
  968. stack.push_quaternionbox(QUATERNION_IDENTITY);
  969. } else if (stack.num_args() == 1 + 1) {
  970. stack.push_quaternionbox(stack.get_quaternion(1 + 1));
  971. } else {
  972. Quaternion q = from_elements(stack.get_float(1 + 1)
  973. , stack.get_float(2 + 1)
  974. , stack.get_float(3 + 1)
  975. , stack.get_float(4 + 1)
  976. );
  977. stack.push_quaternionbox(q);
  978. }
  979. return 1;
  980. });
  981. env.add_module_function("Color4", "lerp", [](lua_State *L) {
  982. LuaStack stack(L);
  983. stack.push_color4(lerp(stack.get_color4(1), stack.get_color4(2), stack.get_float(3)));
  984. return 1;
  985. });
  986. env.add_module_function("Color4", "black", [](lua_State *L) {
  987. LuaStack stack(L);
  988. stack.push_color4(COLOR4_BLACK);
  989. return 1;
  990. });
  991. env.add_module_function("Color4", "white", [](lua_State *L) {
  992. LuaStack stack(L);
  993. stack.push_color4(COLOR4_WHITE);
  994. return 1;
  995. });
  996. env.add_module_function("Color4", "red", [](lua_State *L) {
  997. LuaStack stack(L);
  998. stack.push_color4(COLOR4_RED);
  999. return 1;
  1000. });
  1001. env.add_module_function("Color4", "green", [](lua_State *L) {
  1002. LuaStack stack(L);
  1003. stack.push_color4(COLOR4_GREEN);
  1004. return 1;
  1005. });
  1006. env.add_module_function("Color4", "blue", [](lua_State *L) {
  1007. LuaStack stack(L);
  1008. stack.push_color4(COLOR4_BLUE);
  1009. return 1;
  1010. });
  1011. env.add_module_function("Color4", "yellow", [](lua_State *L) {
  1012. LuaStack stack(L);
  1013. stack.push_color4(COLOR4_YELLOW);
  1014. return 1;
  1015. });
  1016. env.add_module_function("Color4", "orange", [](lua_State *L) {
  1017. LuaStack stack(L);
  1018. stack.push_color4(COLOR4_ORANGE);
  1019. return 1;
  1020. });
  1021. env.add_module_function("Color4", "to_string", quaternion_to_string);
  1022. env.add_module_metafunction("Color4", "__call", [](lua_State *L) {
  1023. LuaStack stack(L);
  1024. u8 r = (u8)stack.get_int(1 + 1);
  1025. u8 g = (u8)stack.get_int(2 + 1);
  1026. u8 b = (u8)stack.get_int(3 + 1);
  1027. u8 a = (u8)stack.get_int(4 + 1);
  1028. stack.push_color4(from_rgba(r, g, b, a));
  1029. return 1;
  1030. });
  1031. env.add_module_metafunction("Lightuserdata_mt", "__add", [](lua_State *L) {
  1032. LuaStack stack(L);
  1033. const Vector3 &a = stack.get_vector3(1);
  1034. const Vector3 &b = stack.get_vector3(2);
  1035. stack.push_vector3(a + b);
  1036. return 1;
  1037. });
  1038. env.add_module_metafunction("Lightuserdata_mt", "__sub", [](lua_State *L) {
  1039. LuaStack stack(L);
  1040. const Vector3 &a = stack.get_vector3(1);
  1041. const Vector3 &b = stack.get_vector3(2);
  1042. stack.push_vector3(a - b);
  1043. return 1;
  1044. });
  1045. env.add_module_metafunction("Lightuserdata_mt", "__mul", [](lua_State *L) {
  1046. LuaStack stack(L);
  1047. const int i = stack.is_number(1) ? 1 : 2;
  1048. stack.push_vector3(stack.get_float(i) * stack.get_vector3(3 - i));
  1049. return 1;
  1050. });
  1051. env.add_module_metafunction("Lightuserdata_mt", "__unm", [](lua_State *L) {
  1052. LuaStack stack(L);
  1053. stack.push_vector3(-stack.get_vector3(1));
  1054. return 1;
  1055. });
  1056. env.add_module_metafunction("Lightuserdata_mt", "__index", [](lua_State *L) {
  1057. LuaStack stack(L);
  1058. const Vector3 &v = stack.get_vector3(1);
  1059. const char *s = stack.get_string(2);
  1060. switch (s[0]) {
  1061. case 'x': stack.push_float(v.x); return 1;
  1062. case 'y': stack.push_float(v.y); return 1;
  1063. case 'z': stack.push_float(v.z); return 1;
  1064. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  1065. }
  1066. return 0;
  1067. });
  1068. env.add_module_metafunction("Lightuserdata_mt", "__newindex", [](lua_State *L) {
  1069. LuaStack stack(L);
  1070. Vector3 &v = stack.get_vector3(1);
  1071. const char *s = stack.get_string(2);
  1072. const f32 value = stack.get_float(3);
  1073. switch (s[0]) {
  1074. case 'x': v.x = value; break;
  1075. case 'y': v.y = value; break;
  1076. case 'z': v.z = value; break;
  1077. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  1078. }
  1079. return 0;
  1080. });
  1081. env.add_module_metafunction("Lightuserdata_mt", "__tostring", [](lua_State *L) {
  1082. LuaStack stack(L);
  1083. if (stack.is_vector3(1)) {
  1084. char buf[256];
  1085. to_string(buf, sizeof(buf), stack.get_vector3(1));
  1086. stack.push_fstring("Vector3: %s", buf);
  1087. } else if (stack.is_quaternion(1)) {
  1088. char buf[256];
  1089. to_string(buf, sizeof(buf), stack.get_quaternion(1));
  1090. stack.push_fstring("Quaternion: %s", buf);
  1091. } else if (stack.is_matrix4x4(1)) {
  1092. char buf[1024];
  1093. to_string(buf, sizeof(buf), stack.get_matrix4x4(1));
  1094. stack.push_fstring("Matrix4x4: %s", buf);
  1095. } else if (stack.is_unit(1)) {
  1096. stack.push_fstring("UnitId: %u", stack.get_unit(1)._idx);
  1097. } else {
  1098. const void *ptr = stack.get_pointer(1);
  1099. const u32 marker = *(u32 *)ptr;
  1100. if (marker == DEBUG_LINE_MARKER)
  1101. stack.push_fstring("DebugLine: %p", ptr);
  1102. else if (marker == DEBUG_GUI_MARKER)
  1103. stack.push_fstring("DebugGui: %p", ptr);
  1104. else if (marker == LEVEL_MARKER)
  1105. stack.push_fstring("Level: %p", ptr);
  1106. else if (marker == RENDER_WORLD_MARKER)
  1107. stack.push_fstring("RenderWorld: %p", ptr);
  1108. else if (marker == RESOURCE_PACKAGE_MARKER)
  1109. stack.push_fstring("ResourcePackage: %p", ptr);
  1110. else if (marker == SCENE_GRAPH_MARKER)
  1111. stack.push_fstring("SceneGraph: %p", ptr);
  1112. else if (marker == WORLD_MARKER)
  1113. stack.push_fstring("World: %p", ptr);
  1114. else if (marker == SCRIPT_WORLD_MARKER)
  1115. stack.push_fstring("ScriptWorld: %p", ptr);
  1116. else if (marker == SOUND_WORLD_MARKER)
  1117. stack.push_fstring("SoundWorld: %p", ptr);
  1118. else if (marker == PHYSICS_WORLD_MARKER)
  1119. stack.push_fstring("PhysicsWorld: %p", ptr);
  1120. else if (marker == ANIMATION_STATE_MACHINE_MARKER)
  1121. stack.push_fstring("AnimationStateMachine: %p", ptr);
  1122. else
  1123. stack.push_fstring("lightuserdata: %p", ptr);
  1124. }
  1125. return 1;
  1126. });
  1127. #define KEYBOARD(name) \
  1128. [](lua_State* L) \
  1129. { \
  1130. return input_device_ ## name(L, *device()->_input_manager->keyboard()); \
  1131. }
  1132. #define MOUSE(name) \
  1133. [](lua_State* L) \
  1134. { \
  1135. return input_device_ ## name(L, *device()->_input_manager->mouse()); \
  1136. }
  1137. #define TOUCH(name) \
  1138. [](lua_State* L) \
  1139. { \
  1140. return input_device_ ## name(L, *device()->_input_manager->touch()); \
  1141. }
  1142. #define PAD(index, name) \
  1143. [](lua_State* L) \
  1144. { \
  1145. return input_device_ ## name(L, *device()->_input_manager->joypad(index)); \
  1146. }
  1147. env.add_module_function("Keyboard", "name", KEYBOARD(name));
  1148. env.add_module_function("Keyboard", "connected", KEYBOARD(connected));
  1149. env.add_module_function("Keyboard", "num_buttons", KEYBOARD(num_buttons));
  1150. env.add_module_function("Keyboard", "num_axes", KEYBOARD(num_axes));
  1151. env.add_module_function("Keyboard", "pressed", KEYBOARD(pressed));
  1152. env.add_module_function("Keyboard", "released", KEYBOARD(released));
  1153. env.add_module_function("Keyboard", "any_pressed", KEYBOARD(any_pressed));
  1154. env.add_module_function("Keyboard", "any_released", KEYBOARD(any_released));
  1155. env.add_module_function("Keyboard", "button", KEYBOARD(button));
  1156. env.add_module_function("Keyboard", "button_name", KEYBOARD(button_name));
  1157. env.add_module_function("Keyboard", "button_id", KEYBOARD(button_id));
  1158. lua_getglobal(env.L, "Keyboard");
  1159. device()->_input_manager->keyboard()->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1160. env.add_module_function("Mouse", "name", MOUSE(name));
  1161. env.add_module_function("Mouse", "connected", MOUSE(connected));
  1162. env.add_module_function("Mouse", "num_buttons", MOUSE(num_buttons));
  1163. env.add_module_function("Mouse", "num_axes", MOUSE(num_axes));
  1164. env.add_module_function("Mouse", "pressed", MOUSE(pressed));
  1165. env.add_module_function("Mouse", "released", MOUSE(released));
  1166. env.add_module_function("Mouse", "any_pressed", MOUSE(any_pressed));
  1167. env.add_module_function("Mouse", "any_released", MOUSE(any_released));
  1168. env.add_module_function("Mouse", "button", MOUSE(button));
  1169. env.add_module_function("Mouse", "axis", MOUSE(axis));
  1170. env.add_module_function("Mouse", "button_name", MOUSE(button_name));
  1171. env.add_module_function("Mouse", "axis_name", MOUSE(axis_name));
  1172. env.add_module_function("Mouse", "button_id", MOUSE(button_id));
  1173. env.add_module_function("Mouse", "axis_id", MOUSE(axis_id));
  1174. lua_getglobal(env.L, "Mouse");
  1175. device()->_input_manager->mouse()->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1176. env.add_module_function("Touch", "name", TOUCH(name));
  1177. env.add_module_function("Touch", "connected", TOUCH(connected));
  1178. env.add_module_function("Touch", "num_buttons", TOUCH(num_buttons));
  1179. env.add_module_function("Touch", "num_axes", TOUCH(num_axes));
  1180. env.add_module_function("Touch", "pressed", TOUCH(pressed));
  1181. env.add_module_function("Touch", "released", TOUCH(released));
  1182. env.add_module_function("Touch", "any_pressed", TOUCH(any_pressed));
  1183. env.add_module_function("Touch", "any_released", TOUCH(any_released));
  1184. env.add_module_function("Touch", "button", TOUCH(button));
  1185. env.add_module_function("Touch", "axis", TOUCH(axis));
  1186. env.add_module_function("Touch", "button_name", TOUCH(button_name));
  1187. env.add_module_function("Touch", "axis_name", TOUCH(axis_name));
  1188. env.add_module_function("Touch", "button_id", TOUCH(button_id));
  1189. env.add_module_function("Touch", "axis_id", TOUCH(axis_id));
  1190. lua_getglobal(env.L, "Touch");
  1191. device()->_input_manager->touch()->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1192. env.add_module_function("Pad1", "name", PAD(0, name));
  1193. env.add_module_function("Pad1", "connected", PAD(0, connected));
  1194. env.add_module_function("Pad1", "num_buttons", PAD(0, num_buttons));
  1195. env.add_module_function("Pad1", "num_axes", PAD(0, num_axes));
  1196. env.add_module_function("Pad1", "pressed", PAD(0, pressed));
  1197. env.add_module_function("Pad1", "released", PAD(0, released));
  1198. env.add_module_function("Pad1", "any_pressed", PAD(0, any_pressed));
  1199. env.add_module_function("Pad1", "any_released", PAD(0, any_released));
  1200. env.add_module_function("Pad1", "button", PAD(0, button));
  1201. env.add_module_function("Pad1", "axis", PAD(0, axis));
  1202. env.add_module_function("Pad1", "button_name", PAD(0, button_name));
  1203. env.add_module_function("Pad1", "axis_name", PAD(0, axis_name));
  1204. env.add_module_function("Pad1", "button_id", PAD(0, button_id));
  1205. env.add_module_function("Pad1", "axis_id", PAD(0, axis_id));
  1206. env.add_module_function("Pad1", "deadzone", PAD(0, deadzone));
  1207. env.add_module_function("Pad1", "set_deadzone", PAD(0, set_deadzone));
  1208. lua_getglobal(env.L, "Pad1");
  1209. device()->_input_manager->joypad(0)->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1210. env.add_module_function("Pad2", "name", PAD(1, name));
  1211. env.add_module_function("Pad2", "connected", PAD(1, connected));
  1212. env.add_module_function("Pad2", "num_buttons", PAD(1, num_buttons));
  1213. env.add_module_function("Pad2", "num_axes", PAD(1, num_axes));
  1214. env.add_module_function("Pad2", "pressed", PAD(1, pressed));
  1215. env.add_module_function("Pad2", "released", PAD(1, released));
  1216. env.add_module_function("Pad2", "any_pressed", PAD(1, any_pressed));
  1217. env.add_module_function("Pad2", "any_released", PAD(1, any_released));
  1218. env.add_module_function("Pad2", "button", PAD(1, button));
  1219. env.add_module_function("Pad2", "axis", PAD(1, axis));
  1220. env.add_module_function("Pad2", "button_name", PAD(1, button_name));
  1221. env.add_module_function("Pad2", "axis_name", PAD(1, axis_name));
  1222. env.add_module_function("Pad2", "button_id", PAD(1, button_id));
  1223. env.add_module_function("Pad2", "axis_id", PAD(1, axis_id));
  1224. env.add_module_function("Pad2", "deadzone", PAD(1, deadzone));
  1225. env.add_module_function("Pad2", "set_deadzone", PAD(1, set_deadzone));
  1226. lua_getglobal(env.L, "Pad2");
  1227. device()->_input_manager->joypad(1)->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1228. env.add_module_function("Pad3", "name", PAD(2, name));
  1229. env.add_module_function("Pad3", "connected", PAD(2, connected));
  1230. env.add_module_function("Pad3", "num_buttons", PAD(2, num_buttons));
  1231. env.add_module_function("Pad3", "num_axes", PAD(2, num_axes));
  1232. env.add_module_function("Pad3", "pressed", PAD(2, pressed));
  1233. env.add_module_function("Pad3", "released", PAD(2, released));
  1234. env.add_module_function("Pad3", "any_pressed", PAD(2, any_pressed));
  1235. env.add_module_function("Pad3", "any_released", PAD(2, any_released));
  1236. env.add_module_function("Pad3", "button", PAD(2, button));
  1237. env.add_module_function("Pad3", "axis", PAD(2, axis));
  1238. env.add_module_function("Pad3", "button_name", PAD(2, button_name));
  1239. env.add_module_function("Pad3", "axis_name", PAD(2, axis_name));
  1240. env.add_module_function("Pad3", "button_id", PAD(2, button_id));
  1241. env.add_module_function("Pad3", "axis_id", PAD(2, axis_id));
  1242. env.add_module_function("Pad3", "deadzone", PAD(2, deadzone));
  1243. env.add_module_function("Pad3", "set_deadzone", PAD(2, set_deadzone));
  1244. lua_getglobal(env.L, "Pad3");
  1245. device()->_input_manager->joypad(2)->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1246. env.add_module_function("Pad4", "name", PAD(3, name));
  1247. env.add_module_function("Pad4", "connected", PAD(3, connected));
  1248. env.add_module_function("Pad4", "num_buttons", PAD(3, num_buttons));
  1249. env.add_module_function("Pad4", "num_axes", PAD(3, num_axes));
  1250. env.add_module_function("Pad4", "pressed", PAD(3, pressed));
  1251. env.add_module_function("Pad4", "released", PAD(3, released));
  1252. env.add_module_function("Pad4", "any_pressed", PAD(3, any_pressed));
  1253. env.add_module_function("Pad4", "any_released", PAD(3, any_released));
  1254. env.add_module_function("Pad4", "button", PAD(3, button));
  1255. env.add_module_function("Pad4", "axis", PAD(3, axis));
  1256. env.add_module_function("Pad4", "button_name", PAD(3, button_name));
  1257. env.add_module_function("Pad4", "axis_name", PAD(3, axis_name));
  1258. env.add_module_function("Pad4", "button_id", PAD(3, button_id));
  1259. env.add_module_function("Pad4", "axis_id", PAD(3, axis_id));
  1260. env.add_module_function("Pad4", "deadzone", PAD(3, deadzone));
  1261. env.add_module_function("Pad4", "set_deadzone", PAD(3, set_deadzone));
  1262. lua_getglobal(env.L, "Pad4");
  1263. device()->_input_manager->joypad(3)->_lua_object = luaL_ref(env.L, LUA_REGISTRYINDEX);
  1264. #undef PAD
  1265. #undef TOUCH
  1266. #undef MOUSE
  1267. #undef KEYBOARD
  1268. env.add_module_function("World", "spawn_unit", [](lua_State *L) {
  1269. LuaStack stack(L);
  1270. const int nargs = stack.num_args();
  1271. World *world = stack.get_world(1);
  1272. const StringId64 name = stack.get_resource_name(2);
  1273. const Vector3 &pos = nargs > 2 ? stack.get_vector3(3) : VECTOR3_ZERO;
  1274. const Quaternion &rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
  1275. const Vector3 &scl = nargs > 4 ? stack.get_vector3(5) : VECTOR3_ONE;
  1276. char name_str[RESOURCE_ID_BUF_LEN];
  1277. LUA_ASSERT(device()->_resource_manager->can_get(RESOURCE_TYPE_UNIT, name)
  1278. , stack
  1279. , "Unit not loaded: " RESOURCE_ID_FMT_STR
  1280. , resource_id(RESOURCE_TYPE_UNIT, name).to_string(name_str, sizeof(name_str))
  1281. );
  1282. CE_UNUSED(name_str);
  1283. stack.push_unit(world->spawn_unit(name, pos, rot, scl));
  1284. return 1;
  1285. });
  1286. env.add_module_function("World", "spawn_empty_unit", [](lua_State *L) {
  1287. LuaStack stack(L);
  1288. stack.push_unit(stack.get_world(1)->spawn_empty_unit());
  1289. return 1;
  1290. });
  1291. env.add_module_function("World", "destroy_unit", [](lua_State *L) {
  1292. LuaStack stack(L);
  1293. stack.get_world(1)->destroy_unit(stack.get_unit(2));
  1294. return 0;
  1295. });
  1296. env.add_module_function("World", "num_units", [](lua_State *L) {
  1297. LuaStack stack(L);
  1298. stack.push_int(stack.get_world(1)->num_units());
  1299. return 1;
  1300. });
  1301. env.add_module_function("World", "units", [](lua_State *L) {
  1302. LuaStack stack(L);
  1303. TempAllocator1024 alloc;
  1304. Array<UnitId> units(alloc);
  1305. stack.get_world(1)->units(units);
  1306. const u32 num = array::size(units);
  1307. stack.push_table(num);
  1308. for (u32 i = 0; i < num; ++i) {
  1309. stack.push_key_begin((s32)i + 1);
  1310. stack.push_unit(units[i]);
  1311. stack.push_key_end();
  1312. }
  1313. return 1;
  1314. });
  1315. env.add_module_function("World", "unit_by_name", [](lua_State *L) {
  1316. LuaStack stack(L);
  1317. UnitId unit = stack.get_world(1)->unit_by_name(stack.get_string_id_32(2));
  1318. if (unit == UNIT_INVALID)
  1319. stack.push_nil();
  1320. else
  1321. stack.push_unit(unit);
  1322. return 1;
  1323. });
  1324. env.add_module_function("World", "camera_create", [](lua_State *L) {
  1325. LuaStack stack(L);
  1326. World *world = stack.get_world(1);
  1327. UnitId unit = stack.get_unit(2);
  1328. CameraDesc cd;
  1329. cd.type = name_to_projection_type(stack.get_string(3));
  1330. cd.fov = stack.get_float(4);
  1331. cd.near_range = stack.get_float(5);
  1332. cd.far_range = stack.get_float(6);
  1333. Matrix4x4 pose = stack.get_matrix4x4(7);
  1334. stack.push_camera(world->camera_create(unit, cd, pose));
  1335. return 1;
  1336. });
  1337. env.add_module_function("World", "camera_destroy", [](lua_State *L) {
  1338. LuaStack stack(L);
  1339. stack.get_world(1)->camera_destroy(stack.get_camera_instance(2));
  1340. return 0;
  1341. });
  1342. env.add_module_function("World", "camera_instance", [](lua_State *L) {
  1343. LuaStack stack(L);
  1344. CameraInstance inst = stack.get_world(1)->camera_instance(stack.get_unit(2));
  1345. if (is_valid(inst))
  1346. stack.push_camera(inst);
  1347. else
  1348. stack.push_nil();
  1349. return 1;
  1350. });
  1351. env.add_module_function("World", "camera_set_projection_type", [](lua_State *L) {
  1352. LuaStack stack(L);
  1353. const char *name = stack.get_string(3);
  1354. const ProjectionType::Enum pt = name_to_projection_type(name);
  1355. LUA_ASSERT(pt != ProjectionType::COUNT, stack, "Unknown projection type: '%s'", name);
  1356. stack.get_world(1)->camera_set_projection_type(stack.get_camera_instance(2), pt);
  1357. return 0;
  1358. });
  1359. env.add_module_function("World", "camera_projection_type", [](lua_State *L) {
  1360. LuaStack stack(L);
  1361. ProjectionType::Enum type = stack.get_world(1)->camera_projection_type(stack.get_camera_instance(2));
  1362. stack.push_string(s_projection[type].name);
  1363. return 1;
  1364. });
  1365. env.add_module_function("World", "camera_fov", [](lua_State *L) {
  1366. LuaStack stack(L);
  1367. stack.push_float(stack.get_world(1)->camera_fov(stack.get_camera_instance(2)));
  1368. return 1;
  1369. });
  1370. env.add_module_function("World", "camera_set_fov", [](lua_State *L) {
  1371. LuaStack stack(L);
  1372. stack.get_world(1)->camera_set_fov(stack.get_camera_instance(2), stack.get_float(3));
  1373. return 0;
  1374. });
  1375. env.add_module_function("World", "camera_near_clip_distance", [](lua_State *L) {
  1376. LuaStack stack(L);
  1377. stack.push_float(stack.get_world(1)->camera_near_clip_distance(stack.get_camera_instance(2)));
  1378. return 1;
  1379. });
  1380. env.add_module_function("World", "camera_set_near_clip_distance", [](lua_State *L) {
  1381. LuaStack stack(L);
  1382. stack.get_world(1)->camera_set_near_clip_distance(stack.get_camera_instance(2), stack.get_float(3));
  1383. return 0;
  1384. });
  1385. env.add_module_function("World", "camera_far_clip_distance", [](lua_State *L) {
  1386. LuaStack stack(L);
  1387. stack.push_float(stack.get_world(1)->camera_far_clip_distance(stack.get_camera_instance(2)));
  1388. return 1;
  1389. });
  1390. env.add_module_function("World", "camera_set_far_clip_distance", [](lua_State *L) {
  1391. LuaStack stack(L);
  1392. stack.get_world(1)->camera_set_far_clip_distance(stack.get_camera_instance(2), stack.get_float(3));
  1393. return 0;
  1394. });
  1395. env.add_module_function("World", "camera_set_orthographic_size", [](lua_State *L) {
  1396. LuaStack stack(L);
  1397. stack.get_world(1)->camera_set_orthographic_size(stack.get_camera_instance(2), stack.get_float(3));
  1398. return 0;
  1399. });
  1400. env.add_module_function("World", "camera_screen_to_world", [](lua_State *L) {
  1401. LuaStack stack(L);
  1402. stack.push_vector3(stack.get_world(1)->camera_screen_to_world(stack.get_camera_instance(2), stack.get_vector3(3)));
  1403. return 1;
  1404. });
  1405. env.add_module_function("World", "camera_world_to_screen", [](lua_State *L) {
  1406. LuaStack stack(L);
  1407. stack.push_vector3(stack.get_world(1)->camera_world_to_screen(stack.get_camera_instance(2), stack.get_vector3(3)));
  1408. return 1;
  1409. });
  1410. env.add_module_function("World", "update_animations", [](lua_State *L) {
  1411. LuaStack stack(L);
  1412. stack.get_world(1)->update_animations(stack.get_float(2));
  1413. return 0;
  1414. });
  1415. env.add_module_function("World", "update_scene", [](lua_State *L) {
  1416. LuaStack stack(L);
  1417. stack.get_world(1)->update_scene(stack.get_float(2));
  1418. return 0;
  1419. });
  1420. env.add_module_function("World", "update", [](lua_State *L) {
  1421. LuaStack stack(L);
  1422. stack.get_world(1)->update(stack.get_float(2));
  1423. return 0;
  1424. });
  1425. env.add_module_function("World", "play_sound", [](lua_State *L) {
  1426. LuaStack stack(L);
  1427. const s32 nargs = stack.num_args();
  1428. World *world = stack.get_world(1);
  1429. const StringId64 name = stack.get_resource_name(2);
  1430. const bool loop = nargs > 2 ? stack.get_bool(3) : false;
  1431. const f32 volume = nargs > 3 ? stack.get_float(4) : 1.0f;
  1432. const Vector3 &pos = nargs > 4 ? stack.get_vector3(5) : VECTOR3_ZERO;
  1433. const f32 range = nargs > 5 ? stack.get_float(6) : 1000.0f;
  1434. char name_str[RESOURCE_ID_BUF_LEN];
  1435. LUA_ASSERT(device()->_resource_manager->can_get(RESOURCE_TYPE_SOUND, name)
  1436. , stack
  1437. , "Sound not loaded: " RESOURCE_ID_FMT_STR
  1438. , resource_id(RESOURCE_TYPE_SOUND, name).to_string(name_str, sizeof(name_str))
  1439. );
  1440. CE_UNUSED(name_str);
  1441. stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
  1442. return 1;
  1443. });
  1444. env.add_module_function("World", "stop_sound", [](lua_State *L) {
  1445. LuaStack stack(L);
  1446. stack.get_world(1)->stop_sound(stack.get_sound_instance_id(2));
  1447. return 0;
  1448. });
  1449. env.add_module_function("World", "link_sound", [](lua_State *L) {
  1450. LuaStack stack(L);
  1451. stack.get_world(1)->link_sound(stack.get_sound_instance_id(2)
  1452. , stack.get_unit(3)
  1453. , stack.get_int(4)
  1454. );
  1455. return 0;
  1456. });
  1457. env.add_module_function("World", "set_listener_pose", [](lua_State *L) {
  1458. LuaStack stack(L);
  1459. stack.get_world(1)->set_listener_pose(stack.get_matrix4x4(2));
  1460. return 0;
  1461. });
  1462. env.add_module_function("World", "set_sound_position", [](lua_State *L) {
  1463. LuaStack stack(L);
  1464. stack.get_world(1)->set_sound_position(stack.get_sound_instance_id(2), stack.get_vector3(3));
  1465. return 0;
  1466. });
  1467. env.add_module_function("World", "set_sound_range", [](lua_State *L) {
  1468. LuaStack stack(L);
  1469. stack.get_world(1)->set_sound_range(stack.get_sound_instance_id(2), stack.get_float(3));
  1470. return 0;
  1471. });
  1472. env.add_module_function("World", "set_sound_volume", [](lua_State *L) {
  1473. LuaStack stack(L);
  1474. stack.get_world(1)->set_sound_volume(stack.get_sound_instance_id(2), stack.get_float(3));
  1475. return 0;
  1476. });
  1477. env.add_module_function("World", "create_debug_line", [](lua_State *L) {
  1478. LuaStack stack(L);
  1479. stack.push_debug_line(stack.get_world(1)->create_debug_line(stack.get_bool(2)));
  1480. return 1;
  1481. });
  1482. env.add_module_function("World", "destroy_debug_line", [](lua_State *L) {
  1483. LuaStack stack(L);
  1484. stack.get_world(1)->destroy_debug_line(*stack.get_debug_line(2));
  1485. return 0;
  1486. });
  1487. env.add_module_function("World", "create_screen_gui", [](lua_State *L) {
  1488. LuaStack stack(L);
  1489. stack.push_gui(stack.get_world(1)->create_screen_gui());
  1490. return 1;
  1491. });
  1492. env.add_module_function("World", "destroy_gui", [](lua_State *L) {
  1493. LuaStack stack(L);
  1494. stack.get_world(1)->destroy_gui(*stack.get_gui(2));
  1495. return 0;
  1496. });
  1497. env.add_module_function("World", "load_level", [](lua_State *L) {
  1498. LuaStack stack(L);
  1499. const int nargs = stack.num_args();
  1500. World *world = stack.get_world(1);
  1501. const StringId64 name = stack.get_resource_name(2);
  1502. const Vector3 &pos = nargs > 2 ? stack.get_vector3(3) : VECTOR3_ZERO;
  1503. const Quaternion &rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
  1504. char name_str[RESOURCE_ID_BUF_LEN];
  1505. LUA_ASSERT(device()->_resource_manager->can_get(RESOURCE_TYPE_LEVEL, name)
  1506. , stack
  1507. , "Level not loaded: " RESOURCE_ID_FMT_STR
  1508. , resource_id(RESOURCE_TYPE_LEVEL, name).to_string(name_str, sizeof(name_str))
  1509. );
  1510. CE_UNUSED(name_str);
  1511. stack.push_level(world->load_level(name, pos, rot));
  1512. return 1;
  1513. });
  1514. env.add_module_function("World", "scene_graph", [](lua_State *L) {
  1515. LuaStack stack(L);
  1516. stack.push_scene_graph(stack.get_world(1)->_scene_graph);
  1517. return 1;
  1518. });
  1519. env.add_module_function("World", "render_world", [](lua_State *L) {
  1520. LuaStack stack(L);
  1521. stack.push_render_world(stack.get_world(1)->_render_world);
  1522. return 1;
  1523. });
  1524. env.add_module_function("World", "physics_world", [](lua_State *L) {
  1525. LuaStack stack(L);
  1526. stack.push_physics_world(stack.get_world(1)->_physics_world);
  1527. return 1;
  1528. });
  1529. env.add_module_function("World", "sound_world", [](lua_State *L) {
  1530. LuaStack stack(L);
  1531. stack.push_sound_world(stack.get_world(1)->_sound_world);
  1532. return 1;
  1533. });
  1534. env.add_module_function("World", "animation_state_machine", [](lua_State *L) {
  1535. LuaStack stack(L);
  1536. stack.push_animation_state_machine(stack.get_world(1)->_animation_state_machine);
  1537. return 1;
  1538. });
  1539. env.add_module_function("World", "disable_unit_callbacks", [](lua_State *L) {
  1540. LuaStack stack(L);
  1541. stack.get_world(1)->disable_unit_callbacks();
  1542. return 0;
  1543. });
  1544. env.add_module_metafunction("World", "__tostring", [](lua_State *L) {
  1545. LuaStack stack(L);
  1546. World *w = stack.get_world(1);
  1547. stack.push_fstring("World (%p)", w);
  1548. return 1;
  1549. });
  1550. env.add_module_function("SceneGraph", "create", [](lua_State *L) {
  1551. LuaStack stack(L);
  1552. SceneGraph *sg = stack.get_scene_graph(1);
  1553. UnitId unit = stack.get_unit(2);
  1554. LUA_ASSERT(!sg->has(unit), stack, "Unit already has transform");
  1555. TransformInstance ti = sg->create(unit
  1556. , stack.get_vector3(3)
  1557. , stack.get_quaternion(4)
  1558. , stack.get_vector3(5)
  1559. );
  1560. stack.push_transform(ti);
  1561. return 1;
  1562. });
  1563. env.add_module_function("SceneGraph", "destroy", [](lua_State *L) {
  1564. LuaStack stack(L);
  1565. stack.get_scene_graph(1)->destroy(stack.get_transform_instance(2));
  1566. return 0;
  1567. });
  1568. env.add_module_function("SceneGraph", "instance", [](lua_State *L) {
  1569. LuaStack stack(L);
  1570. TransformInstance inst = stack.get_scene_graph(1)->instance(stack.get_unit(2));
  1571. if (is_valid(inst))
  1572. stack.push_transform(inst);
  1573. else
  1574. stack.push_nil();
  1575. return 1;
  1576. });
  1577. env.add_module_function("SceneGraph", "local_position", [](lua_State *L) {
  1578. LuaStack stack(L);
  1579. stack.push_vector3(stack.get_scene_graph(1)->local_position(stack.get_transform_instance(2)));
  1580. return 1;
  1581. });
  1582. env.add_module_function("SceneGraph", "local_rotation", [](lua_State *L) {
  1583. LuaStack stack(L);
  1584. stack.push_quaternion(stack.get_scene_graph(1)->local_rotation(stack.get_transform_instance(2)));
  1585. return 1;
  1586. });
  1587. env.add_module_function("SceneGraph", "local_scale", [](lua_State *L) {
  1588. LuaStack stack(L);
  1589. stack.push_vector3(stack.get_scene_graph(1)->local_scale(stack.get_transform_instance(2)));
  1590. return 1;
  1591. });
  1592. env.add_module_function("SceneGraph", "local_pose", [](lua_State *L) {
  1593. LuaStack stack(L);
  1594. stack.push_matrix4x4(stack.get_scene_graph(1)->local_pose(stack.get_transform_instance(2)));
  1595. return 1;
  1596. });
  1597. env.add_module_function("SceneGraph", "world_position", [](lua_State *L) {
  1598. LuaStack stack(L);
  1599. stack.push_vector3(stack.get_scene_graph(1)->world_position(stack.get_transform_instance(2)));
  1600. return 1;
  1601. });
  1602. env.add_module_function("SceneGraph", "world_rotation", [](lua_State *L) {
  1603. LuaStack stack(L);
  1604. stack.push_quaternion(stack.get_scene_graph(1)->world_rotation(stack.get_transform_instance(2)));
  1605. return 1;
  1606. });
  1607. env.add_module_function("SceneGraph", "world_pose", [](lua_State *L) {
  1608. LuaStack stack(L);
  1609. stack.push_matrix4x4(stack.get_scene_graph(1)->world_pose(stack.get_transform_instance(2)));
  1610. return 1;
  1611. });
  1612. env.add_module_function("SceneGraph", "set_local_position", [](lua_State *L) {
  1613. LuaStack stack(L);
  1614. stack.get_scene_graph(1)->set_local_position(stack.get_transform_instance(2), stack.get_vector3(3));
  1615. return 0;
  1616. });
  1617. env.add_module_function("SceneGraph", "set_local_rotation", [](lua_State *L) {
  1618. LuaStack stack(L);
  1619. stack.get_scene_graph(1)->set_local_rotation(stack.get_transform_instance(2), stack.get_quaternion(3));
  1620. return 0;
  1621. });
  1622. env.add_module_function("SceneGraph", "set_local_scale", [](lua_State *L) {
  1623. LuaStack stack(L);
  1624. stack.get_scene_graph(1)->set_local_scale(stack.get_transform_instance(2), stack.get_vector3(3));
  1625. return 0;
  1626. });
  1627. env.add_module_function("SceneGraph", "set_local_pose", [](lua_State *L) {
  1628. LuaStack stack(L);
  1629. stack.get_scene_graph(1)->set_local_pose(stack.get_transform_instance(2), stack.get_matrix4x4(3));
  1630. return 0;
  1631. });
  1632. env.add_module_function("SceneGraph", "link", [](lua_State *L) {
  1633. LuaStack stack(L);
  1634. const int nargs = stack.num_args();
  1635. const TransformInstance parent_ti = stack.get_transform_instance(2);
  1636. const TransformInstance child_ti = stack.get_transform_instance(3);
  1637. const Vector3 &pos = nargs > 3 ? stack.get_vector3(4) : VECTOR3_ZERO;
  1638. const Quaternion &rot = nargs > 4 ? stack.get_quaternion(5) : QUATERNION_IDENTITY;
  1639. const Vector3 &scl = nargs > 5 ? stack.get_vector3(6) : VECTOR3_ONE;
  1640. stack.get_scene_graph(1)->link(parent_ti, child_ti, pos, rot, scl);
  1641. return 0;
  1642. });
  1643. env.add_module_function("SceneGraph", "unlink", [](lua_State *L) {
  1644. LuaStack stack(L);
  1645. stack.get_scene_graph(1)->unlink(stack.get_transform_instance(2));
  1646. return 0;
  1647. });
  1648. env.add_module_function("UnitManager", "create", [](lua_State *L) {
  1649. LuaStack stack(L);
  1650. if (stack.num_args() == 1)
  1651. stack.push_unit(device()->_unit_manager->create(*stack.get_world(1)));
  1652. else
  1653. stack.push_unit(device()->_unit_manager->create());
  1654. return 1;
  1655. });
  1656. env.add_module_function("UnitManager", "alive", [](lua_State *L) {
  1657. LuaStack stack(L);
  1658. stack.push_bool(device()->_unit_manager->alive(stack.get_unit(1)));
  1659. return 1;
  1660. });
  1661. env.add_module_function("RenderWorld", "mesh_create", [](lua_State *L) {
  1662. LuaStack stack(L);
  1663. RenderWorld *rw = stack.get_render_world(1);
  1664. UnitId unit = stack.get_unit(2);
  1665. MeshRendererDesc desc;
  1666. desc.mesh_resource = stack.get_resource_name(3);
  1667. desc.geometry_name = stack.get_string_id_32(4);
  1668. desc.material_resource = stack.get_resource_name(5);
  1669. desc.visible = stack.get_bool(6);
  1670. Matrix4x4 pose = stack.get_matrix4x4(7);
  1671. stack.push_mesh_instance(rw->mesh_create(unit, desc, pose));
  1672. return 1;
  1673. });
  1674. env.add_module_function("RenderWorld", "mesh_destroy", [](lua_State *L) {
  1675. LuaStack stack(L);
  1676. stack.get_render_world(1)->mesh_destroy(stack.get_mesh_instance(2));
  1677. return 0;
  1678. });
  1679. env.add_module_function("RenderWorld", "mesh_instance", [](lua_State *L) {
  1680. LuaStack stack(L);
  1681. MeshInstance inst = stack.get_render_world(1)->mesh_instance(stack.get_unit(2));
  1682. if (is_valid(inst))
  1683. stack.push_mesh_instance(inst);
  1684. else
  1685. stack.push_nil();
  1686. return 1;
  1687. });
  1688. env.add_module_function("RenderWorld", "mesh_obb", [](lua_State *L) {
  1689. LuaStack stack(L);
  1690. OBB obb = stack.get_render_world(1)->mesh_obb(stack.get_mesh_instance(2));
  1691. stack.push_matrix4x4(obb.tm);
  1692. stack.push_vector3(obb.half_extents);
  1693. return 2;
  1694. });
  1695. env.add_module_function("RenderWorld", "mesh_cast_ray", [](lua_State *L) {
  1696. LuaStack stack(L);
  1697. RenderWorld *rw = stack.get_render_world(1);
  1698. float t = rw->mesh_cast_ray(stack.get_mesh_instance(2)
  1699. , stack.get_vector3(3)
  1700. , stack.get_vector3(4)
  1701. );
  1702. stack.push_float(t);
  1703. return 1;
  1704. });
  1705. env.add_module_function("RenderWorld", "mesh_material", [](lua_State *L) {
  1706. LuaStack stack(L);
  1707. Material *material = stack.get_render_world(1)->mesh_material(stack.get_mesh_instance(2));
  1708. stack.push_pointer(material);
  1709. return 1;
  1710. });
  1711. env.add_module_function("RenderWorld", "mesh_set_material", [](lua_State *L) {
  1712. LuaStack stack(L);
  1713. stack.get_render_world(1)->mesh_set_material(stack.get_mesh_instance(2), stack.get_string_id_64(3));
  1714. return 0;
  1715. });
  1716. env.add_module_function("RenderWorld", "mesh_set_visible", [](lua_State *L) {
  1717. LuaStack stack(L);
  1718. stack.get_render_world(1)->mesh_set_visible(stack.get_mesh_instance(2), stack.get_bool(3));
  1719. return 0;
  1720. });
  1721. env.add_module_function("RenderWorld", "sprite_create", [](lua_State *L) {
  1722. LuaStack stack(L);
  1723. RenderWorld *rw = stack.get_render_world(1);
  1724. UnitId unit = stack.get_unit(2);
  1725. SpriteRendererDesc desc;
  1726. desc.sprite_resource = stack.get_resource_name(3);
  1727. desc.material_resource = stack.get_resource_name(4);
  1728. desc.layer = stack.get_int(5);
  1729. desc.depth = stack.get_int(6);
  1730. desc.visible = stack.get_bool(7);
  1731. Matrix4x4 pose = stack.get_matrix4x4(8);
  1732. stack.push_sprite_instance(rw->sprite_create(unit, desc, pose));
  1733. return 1;
  1734. });
  1735. env.add_module_function("RenderWorld", "sprite_destroy", [](lua_State *L) {
  1736. LuaStack stack(L);
  1737. stack.get_render_world(1)->sprite_destroy(stack.get_sprite_instance(2));
  1738. return 0;
  1739. });
  1740. env.add_module_function("RenderWorld", "sprite_instance", [](lua_State *L) {
  1741. LuaStack stack(L);
  1742. SpriteInstance inst = stack.get_render_world(1)->sprite_instance(stack.get_unit(2));
  1743. if (is_valid(inst))
  1744. stack.push_sprite_instance(inst);
  1745. else
  1746. stack.push_nil();
  1747. return 1;
  1748. });
  1749. env.add_module_function("RenderWorld", "sprite_set_sprite", [](lua_State *L) {
  1750. LuaStack stack(L);
  1751. stack.get_render_world(1)->sprite_set_sprite(stack.get_sprite_instance(2), stack.get_string_id_64(3));
  1752. return 0;
  1753. });
  1754. env.add_module_function("RenderWorld", "sprite_material", [](lua_State *L) {
  1755. LuaStack stack(L);
  1756. Material *material = stack.get_render_world(1)->sprite_material(stack.get_sprite_instance(2));
  1757. stack.push_pointer(material);
  1758. return 1;
  1759. });
  1760. env.add_module_function("RenderWorld", "sprite_set_material", [](lua_State *L) {
  1761. LuaStack stack(L);
  1762. stack.get_render_world(1)->sprite_set_material(stack.get_sprite_instance(2), stack.get_string_id_64(3));
  1763. return 0;
  1764. });
  1765. env.add_module_function("RenderWorld", "sprite_set_frame", [](lua_State *L) {
  1766. LuaStack stack(L);
  1767. stack.get_render_world(1)->sprite_set_frame(stack.get_sprite_instance(2), stack.get_int(3));
  1768. return 0;
  1769. });
  1770. env.add_module_function("RenderWorld", "sprite_set_visible", [](lua_State *L) {
  1771. LuaStack stack(L);
  1772. stack.get_render_world(1)->sprite_set_visible(stack.get_sprite_instance(2), stack.get_bool(3));
  1773. return 0;
  1774. });
  1775. env.add_module_function("RenderWorld", "sprite_flip_x", [](lua_State *L) {
  1776. LuaStack stack(L);
  1777. stack.get_render_world(1)->sprite_flip_x(stack.get_sprite_instance(2), stack.get_bool(3));
  1778. return 0;
  1779. });
  1780. env.add_module_function("RenderWorld", "sprite_flip_y", [](lua_State *L) {
  1781. LuaStack stack(L);
  1782. stack.get_render_world(1)->sprite_flip_y(stack.get_sprite_instance(2), stack.get_bool(3));
  1783. return 0;
  1784. });
  1785. env.add_module_function("RenderWorld", "sprite_set_layer", [](lua_State *L) {
  1786. LuaStack stack(L);
  1787. stack.get_render_world(1)->sprite_set_layer(stack.get_sprite_instance(2), stack.get_int(3));
  1788. return 0;
  1789. });
  1790. env.add_module_function("RenderWorld", "sprite_set_depth", [](lua_State *L) {
  1791. LuaStack stack(L);
  1792. stack.get_render_world(1)->sprite_set_depth(stack.get_sprite_instance(2), stack.get_int(3));
  1793. return 0;
  1794. });
  1795. env.add_module_function("RenderWorld", "sprite_obb", [](lua_State *L) {
  1796. LuaStack stack(L);
  1797. OBB obb = stack.get_render_world(1)->sprite_obb(stack.get_sprite_instance(2));
  1798. stack.push_matrix4x4(obb.tm);
  1799. stack.push_vector3(obb.half_extents);
  1800. return 2;
  1801. });
  1802. env.add_module_function("RenderWorld", "sprite_cast_ray", [](lua_State *L) {
  1803. LuaStack stack(L);
  1804. RenderWorld *rw = stack.get_render_world(1);
  1805. u32 layer;
  1806. u32 depth;
  1807. float t = rw->sprite_cast_ray(stack.get_sprite_instance(2)
  1808. , stack.get_vector3(3)
  1809. , stack.get_vector3(4)
  1810. , layer
  1811. , depth
  1812. );
  1813. stack.push_float(t);
  1814. stack.push_int(layer);
  1815. stack.push_int(depth);
  1816. return 3;
  1817. });
  1818. env.add_module_function("RenderWorld", "light_create", [](lua_State *L) {
  1819. LuaStack stack(L);
  1820. const char *name = stack.get_string(3);
  1821. const LightType::Enum lt = name_to_light_type(name);
  1822. LUA_ASSERT(lt != LightType::COUNT, stack, "Unknown light type: '%s'", name);
  1823. LightDesc ld;
  1824. ld.type = lt;
  1825. ld.range = stack.get_float(4);
  1826. ld.intensity = stack.get_float(5);
  1827. ld.spot_angle = stack.get_float(6);
  1828. ld.color = stack.get_vector3(7);
  1829. Matrix4x4 pose = stack.get_matrix4x4(8);
  1830. stack.push_light_instance(stack.get_render_world(1)->light_create(stack.get_unit(2), ld, pose));
  1831. return 1;
  1832. });
  1833. env.add_module_function("RenderWorld", "light_destroy", [](lua_State *L) {
  1834. LuaStack stack(L);
  1835. stack.get_render_world(1)->light_destroy(stack.get_light_instance(2));
  1836. return 0;
  1837. });
  1838. env.add_module_function("RenderWorld", "light_instance", [](lua_State *L) {
  1839. LuaStack stack(L);
  1840. LightInstance inst = stack.get_render_world(1)->light_instance(stack.get_unit(2));
  1841. if (is_valid(inst))
  1842. stack.push_light_instance(inst);
  1843. else
  1844. stack.push_nil();
  1845. return 1;
  1846. });
  1847. env.add_module_function("RenderWorld", "light_type", [](lua_State *L) {
  1848. LuaStack stack(L);
  1849. LightType::Enum type = stack.get_render_world(1)->light_type(stack.get_light_instance(2));
  1850. stack.push_string(s_light[type].name);
  1851. return 1;
  1852. });
  1853. env.add_module_function("RenderWorld", "light_color", [](lua_State *L) {
  1854. LuaStack stack(L);
  1855. stack.push_color4(stack.get_render_world(1)->light_color(stack.get_light_instance(2)));
  1856. return 1;
  1857. });
  1858. env.add_module_function("RenderWorld", "light_range", [](lua_State *L) {
  1859. LuaStack stack(L);
  1860. stack.push_float(stack.get_render_world(1)->light_range(stack.get_light_instance(2)));
  1861. return 1;
  1862. });
  1863. env.add_module_function("RenderWorld", "light_intensity", [](lua_State *L) {
  1864. LuaStack stack(L);
  1865. stack.push_float(stack.get_render_world(1)->light_intensity(stack.get_light_instance(2)));
  1866. return 1;
  1867. });
  1868. env.add_module_function("RenderWorld", "light_spot_angle", [](lua_State *L) {
  1869. LuaStack stack(L);
  1870. stack.push_float(stack.get_render_world(1)->light_spot_angle(stack.get_light_instance(2)));
  1871. return 1;
  1872. });
  1873. env.add_module_function("RenderWorld", "light_set_type", [](lua_State *L) {
  1874. LuaStack stack(L);
  1875. const char *name = stack.get_string(3);
  1876. const LightType::Enum lt = name_to_light_type(name);
  1877. LUA_ASSERT(lt != LightType::COUNT, stack, "Unknown light type: '%s'", name);
  1878. stack.get_render_world(1)->light_set_type(stack.get_light_instance(2), lt);
  1879. return 0;
  1880. });
  1881. env.add_module_function("RenderWorld", "light_set_color", [](lua_State *L) {
  1882. LuaStack stack(L);
  1883. stack.get_render_world(1)->light_set_color(stack.get_light_instance(2), stack.get_color4(3));
  1884. return 0;
  1885. });
  1886. env.add_module_function("RenderWorld", "light_set_range", [](lua_State *L) {
  1887. LuaStack stack(L);
  1888. stack.get_render_world(1)->light_set_range(stack.get_light_instance(2), stack.get_float(3));
  1889. return 0;
  1890. });
  1891. env.add_module_function("RenderWorld", "light_set_intensity", [](lua_State *L) {
  1892. LuaStack stack(L);
  1893. stack.get_render_world(1)->light_set_intensity(stack.get_light_instance(2), stack.get_float(3));
  1894. return 0;
  1895. });
  1896. env.add_module_function("RenderWorld", "light_set_spot_angle", [](lua_State *L) {
  1897. LuaStack stack(L);
  1898. stack.get_render_world(1)->light_set_spot_angle(stack.get_light_instance(2), stack.get_float(3));
  1899. return 0;
  1900. });
  1901. env.add_module_function("RenderWorld", "light_debug_draw", [](lua_State *L) {
  1902. LuaStack stack(L);
  1903. stack.get_render_world(1)->light_debug_draw(stack.get_light_instance(2), *stack.get_debug_line(3));
  1904. return 0;
  1905. });
  1906. env.add_module_function("RenderWorld", "enable_debug_drawing", [](lua_State *L) {
  1907. LuaStack stack(L);
  1908. stack.get_render_world(1)->enable_debug_drawing(stack.get_bool(2));
  1909. return 0;
  1910. });
  1911. env.add_module_function("RenderWorld", "selection", [](lua_State *L) {
  1912. LuaStack stack(L);
  1913. RenderWorld *rw = stack.get_render_world(1);
  1914. UnitId unit = stack.get_unit(2);
  1915. if (stack.get_bool(3))
  1916. hash_set::insert(rw->_selection, unit);
  1917. else
  1918. hash_set::remove(rw->_selection, unit);
  1919. return 0;
  1920. });
  1921. env.add_module_function("PhysicsWorld", "actor_instance", [](lua_State *L) {
  1922. LuaStack stack(L);
  1923. ActorInstance inst = stack.get_physics_world(1)->actor(stack.get_unit(2));
  1924. if (is_valid(inst))
  1925. stack.push_actor(inst);
  1926. else
  1927. stack.push_nil();
  1928. return 1;
  1929. });
  1930. env.add_module_function("PhysicsWorld", "actor_destroy", [](lua_State *L) {
  1931. LuaStack stack(L);
  1932. stack.get_physics_world(1)->actor_destroy(stack.get_actor_instance(2));
  1933. return 0;
  1934. });
  1935. env.add_module_function("PhysicsWorld", "actor_world_position", [](lua_State *L) {
  1936. LuaStack stack(L);
  1937. stack.push_vector3(stack.get_physics_world(1)->actor_world_position(stack.get_actor_instance(2)));
  1938. return 1;
  1939. });
  1940. env.add_module_function("PhysicsWorld", "actor_world_rotation", [](lua_State *L) {
  1941. LuaStack stack(L);
  1942. stack.push_quaternion(stack.get_physics_world(1)->actor_world_rotation(stack.get_actor_instance(2)));
  1943. return 1;
  1944. });
  1945. env.add_module_function("PhysicsWorld", "actor_world_pose", [](lua_State *L) {
  1946. LuaStack stack(L);
  1947. stack.push_matrix4x4(stack.get_physics_world(1)->actor_world_pose(stack.get_actor_instance(2)));
  1948. return 1;
  1949. });
  1950. env.add_module_function("PhysicsWorld", "actor_teleport_world_position", [](lua_State *L) {
  1951. LuaStack stack(L);
  1952. stack.get_physics_world(1)->actor_teleport_world_position(stack.get_actor_instance(2), stack.get_vector3(3));
  1953. return 0;
  1954. });
  1955. env.add_module_function("PhysicsWorld", "actor_teleport_world_rotation", [](lua_State *L) {
  1956. LuaStack stack(L);
  1957. stack.get_physics_world(1)->actor_teleport_world_rotation(stack.get_actor_instance(2), stack.get_quaternion(3));
  1958. return 0;
  1959. });
  1960. env.add_module_function("PhysicsWorld", "actor_teleport_world_pose", [](lua_State *L) {
  1961. LuaStack stack(L);
  1962. stack.get_physics_world(1)->actor_teleport_world_pose(stack.get_actor_instance(2), stack.get_matrix4x4(3));
  1963. return 0;
  1964. });
  1965. env.add_module_function("PhysicsWorld", "actor_center_of_mass", [](lua_State *L) {
  1966. LuaStack stack(L);
  1967. stack.push_vector3(stack.get_physics_world(1)->actor_center_of_mass(stack.get_actor_instance(2)));
  1968. return 1;
  1969. });
  1970. env.add_module_function("PhysicsWorld", "actor_enable_gravity", [](lua_State *L) {
  1971. LuaStack stack(L);
  1972. stack.get_physics_world(1)->actor_enable_gravity(stack.get_actor_instance(2));
  1973. return 0;
  1974. });
  1975. env.add_module_function("PhysicsWorld", "actor_disable_gravity", [](lua_State *L) {
  1976. LuaStack stack(L);
  1977. stack.get_physics_world(1)->actor_disable_gravity(stack.get_actor_instance(2));
  1978. return 0;
  1979. });
  1980. env.add_module_function("PhysicsWorld", "actor_enable_collision", [](lua_State *L) {
  1981. LuaStack stack(L);
  1982. stack.get_physics_world(1)->actor_enable_collision(stack.get_actor_instance(2));
  1983. return 0;
  1984. });
  1985. env.add_module_function("PhysicsWorld", "actor_disable_collision", [](lua_State *L) {
  1986. LuaStack stack(L);
  1987. stack.get_physics_world(1)->actor_disable_collision(stack.get_actor_instance(2));
  1988. return 0;
  1989. });
  1990. env.add_module_function("PhysicsWorld", "actor_set_collision_filter", [](lua_State *L) {
  1991. LuaStack stack(L);
  1992. stack.get_physics_world(1)->actor_set_collision_filter(stack.get_actor_instance(2), stack.get_string_id_32(3));
  1993. return 0;
  1994. });
  1995. env.add_module_function("PhysicsWorld", "actor_set_kinematic", [](lua_State *L) {
  1996. LuaStack stack(L);
  1997. stack.get_physics_world(1)->actor_set_kinematic(stack.get_actor_instance(2), stack.get_bool(3));
  1998. return 0;
  1999. });
  2000. env.add_module_function("PhysicsWorld", "actor_is_static", [](lua_State *L) {
  2001. LuaStack stack(L);
  2002. stack.push_bool(stack.get_physics_world(1)->actor_is_static(stack.get_actor_instance(2)));
  2003. return 1;
  2004. });
  2005. env.add_module_function("PhysicsWorld", "actor_is_dynamic", [](lua_State *L) {
  2006. LuaStack stack(L);
  2007. stack.push_bool(stack.get_physics_world(1)->actor_is_dynamic(stack.get_actor_instance(2)));
  2008. return 1;
  2009. });
  2010. env.add_module_function("PhysicsWorld", "actor_is_kinematic", [](lua_State *L) {
  2011. LuaStack stack(L);
  2012. stack.push_bool(stack.get_physics_world(1)->actor_is_kinematic(stack.get_actor_instance(2)));
  2013. return 1;
  2014. });
  2015. env.add_module_function("PhysicsWorld", "actor_is_nonkinematic", [](lua_State *L) {
  2016. LuaStack stack(L);
  2017. stack.push_bool(stack.get_physics_world(1)->actor_is_nonkinematic(stack.get_actor_instance(2)));
  2018. return 1;
  2019. });
  2020. env.add_module_function("PhysicsWorld", "actor_linear_damping", [](lua_State *L) {
  2021. LuaStack stack(L);
  2022. stack.push_float(stack.get_physics_world(1)->actor_linear_damping(stack.get_actor_instance(2)));
  2023. return 1;
  2024. });
  2025. env.add_module_function("PhysicsWorld", "actor_set_linear_damping", [](lua_State *L) {
  2026. LuaStack stack(L);
  2027. stack.get_physics_world(1)->actor_set_linear_damping(stack.get_actor_instance(2), stack.get_float(3));
  2028. return 0;
  2029. });
  2030. env.add_module_function("PhysicsWorld", "actor_angular_damping", [](lua_State *L) {
  2031. LuaStack stack(L);
  2032. stack.push_float(stack.get_physics_world(1)->actor_angular_damping(stack.get_actor_instance(2)));
  2033. return 1;
  2034. });
  2035. env.add_module_function("PhysicsWorld", "actor_set_angular_damping", [](lua_State *L) {
  2036. LuaStack stack(L);
  2037. stack.get_physics_world(1)->actor_set_angular_damping(stack.get_actor_instance(2), stack.get_float(3));
  2038. return 0;
  2039. });
  2040. env.add_module_function("PhysicsWorld", "actor_linear_velocity", [](lua_State *L) {
  2041. LuaStack stack(L);
  2042. stack.push_vector3(stack.get_physics_world(1)->actor_linear_velocity(stack.get_actor_instance(2)));
  2043. return 1;
  2044. });
  2045. env.add_module_function("PhysicsWorld", "actor_set_linear_velocity", [](lua_State *L) {
  2046. LuaStack stack(L);
  2047. stack.get_physics_world(1)->actor_set_linear_velocity(stack.get_actor_instance(2), stack.get_vector3(3));
  2048. return 0;
  2049. });
  2050. env.add_module_function("PhysicsWorld", "actor_angular_velocity", [](lua_State *L) {
  2051. LuaStack stack(L);
  2052. stack.push_vector3(stack.get_physics_world(1)->actor_angular_velocity(stack.get_actor_instance(2)));
  2053. return 1;
  2054. });
  2055. env.add_module_function("PhysicsWorld", "actor_set_angular_velocity", [](lua_State *L) {
  2056. LuaStack stack(L);
  2057. stack.get_physics_world(1)->actor_set_angular_velocity(stack.get_actor_instance(2), stack.get_vector3(3));
  2058. return 0;
  2059. });
  2060. env.add_module_function("PhysicsWorld", "actor_add_impulse", [](lua_State *L) {
  2061. LuaStack stack(L);
  2062. stack.get_physics_world(1)->actor_add_impulse(stack.get_actor_instance(2), stack.get_vector3(3));
  2063. return 0;
  2064. });
  2065. env.add_module_function("PhysicsWorld", "actor_add_impulse_at", [](lua_State *L) {
  2066. LuaStack stack(L);
  2067. stack.get_physics_world(1)->actor_add_impulse_at(stack.get_actor_instance(2), stack.get_vector3(3), stack.get_vector3(4));
  2068. return 0;
  2069. });
  2070. env.add_module_function("PhysicsWorld", "actor_add_actor_torque_impulse", [](lua_State *L) {
  2071. LuaStack stack(L);
  2072. stack.get_physics_world(1)->actor_add_torque_impulse(stack.get_actor_instance(2), stack.get_vector3(3));
  2073. return 0;
  2074. });
  2075. env.add_module_function("PhysicsWorld", "actor_push", [](lua_State *L) {
  2076. LuaStack stack(L);
  2077. stack.get_physics_world(1)->actor_push(stack.get_actor_instance(2), stack.get_vector3(3), stack.get_float(4));
  2078. return 0;
  2079. });
  2080. env.add_module_function("PhysicsWorld", "actor_push_at", [](lua_State *L) {
  2081. LuaStack stack(L);
  2082. stack.get_physics_world(1)->actor_push_at(stack.get_actor_instance(2), stack.get_vector3(3), stack.get_float(4), stack.get_vector3(5));
  2083. return 0;
  2084. });
  2085. env.add_module_function("PhysicsWorld", "actor_is_sleeping", [](lua_State *L) {
  2086. LuaStack stack(L);
  2087. stack.push_bool(stack.get_physics_world(1)->actor_is_sleeping(stack.get_actor_instance(2)));
  2088. return 1;
  2089. });
  2090. env.add_module_function("PhysicsWorld", "actor_wake_up", [](lua_State *L) {
  2091. LuaStack stack(L);
  2092. stack.get_physics_world(1)->actor_wake_up(stack.get_actor_instance(2));
  2093. return 0;
  2094. });
  2095. env.add_module_function("PhysicsWorld", "joint_create", [](lua_State *L) {
  2096. LuaStack stack(L);
  2097. JointDesc jd;
  2098. jd.type = JointType::SPRING;
  2099. jd.anchor_0 = vector3(0, -2, 0);
  2100. jd.anchor_1 = vector3(0, 2, 0);
  2101. jd.break_force = 999999.0f;
  2102. jd.hinge.axis = vector3(1, 0, 0);
  2103. jd.hinge.lower_limit = -3.14f / 4.0f;
  2104. jd.hinge.upper_limit = 3.14f / 4.0f;
  2105. jd.hinge.bounciness = 12.0f;
  2106. stack.get_physics_world(1)->joint_create(stack.get_actor_instance(2), stack.get_actor_instance(3), jd);
  2107. return 0;
  2108. });
  2109. env.add_module_function("PhysicsWorld", "gravity", [](lua_State *L) {
  2110. LuaStack stack(L);
  2111. stack.push_vector3(stack.get_physics_world(1)->gravity());
  2112. return 1;
  2113. });
  2114. env.add_module_function("PhysicsWorld", "set_gravity", [](lua_State *L) {
  2115. LuaStack stack(L);
  2116. stack.get_physics_world(1)->set_gravity(stack.get_vector3(2));
  2117. return 0;
  2118. });
  2119. env.add_module_function("PhysicsWorld", "cast_ray", [](lua_State *L) {
  2120. LuaStack stack(L);
  2121. RaycastHit hit;
  2122. if (stack.get_physics_world(1)->cast_ray(hit
  2123. , stack.get_vector3(2)
  2124. , stack.get_vector3(3)
  2125. , stack.get_float(4)
  2126. )) {
  2127. stack.push_bool(true);
  2128. stack.push_vector3(hit.position);
  2129. stack.push_vector3(hit.normal);
  2130. stack.push_float(hit.time);
  2131. stack.push_unit(hit.unit);
  2132. stack.push_actor(hit.actor);
  2133. return 6;
  2134. }
  2135. stack.push_bool(false);
  2136. return 1;
  2137. });
  2138. env.add_module_function("PhysicsWorld", "cast_ray_all", [](lua_State *L) {
  2139. LuaStack stack(L);
  2140. TempAllocator1024 ta;
  2141. Array<RaycastHit> hits(ta);
  2142. if (stack.get_physics_world(1)->cast_ray_all(hits
  2143. , stack.get_vector3(2)
  2144. , stack.get_vector3(3)
  2145. , stack.get_float(4)
  2146. )) {
  2147. const u32 num_hits = array::size(hits);
  2148. stack.push_table(num_hits);
  2149. for (u32 i = 0; i < num_hits; ++i) {
  2150. stack.push_key_begin(i + 1);
  2151. stack.push_table();
  2152. {
  2153. stack.push_key_begin(1);
  2154. stack.push_vector3(hits[i].position);
  2155. stack.push_key_end();
  2156. stack.push_key_begin(2);
  2157. stack.push_vector3(hits[i].normal);
  2158. stack.push_key_end();
  2159. stack.push_key_begin(3);
  2160. stack.push_float(hits[i].time);
  2161. stack.push_key_end();
  2162. stack.push_key_begin(4);
  2163. stack.push_unit(hits[i].unit);
  2164. stack.push_key_end();
  2165. stack.push_key_begin(5);
  2166. stack.push_actor(hits[i].actor);
  2167. stack.push_key_end();
  2168. }
  2169. stack.push_key_end();
  2170. }
  2171. }
  2172. return 1;
  2173. });
  2174. env.add_module_function("PhysicsWorld", "cast_sphere", [](lua_State *L) {
  2175. LuaStack stack(L);
  2176. RaycastHit hit;
  2177. if (stack.get_physics_world(1)->cast_sphere(hit
  2178. , stack.get_vector3(2)
  2179. , stack.get_float(3)
  2180. , stack.get_vector3(4)
  2181. , stack.get_float(5)
  2182. )) {
  2183. stack.push_bool(true);
  2184. stack.push_vector3(hit.position);
  2185. stack.push_vector3(hit.normal);
  2186. stack.push_float(hit.time);
  2187. stack.push_unit(hit.unit);
  2188. stack.push_actor(hit.actor);
  2189. return 6;
  2190. }
  2191. stack.push_bool(false);
  2192. return 1;
  2193. });
  2194. env.add_module_function("PhysicsWorld", "cast_box", [](lua_State *L) {
  2195. LuaStack stack(L);
  2196. RaycastHit hit;
  2197. if (stack.get_physics_world(1)->cast_box(hit
  2198. , stack.get_vector3(2)
  2199. , stack.get_vector3(3)
  2200. , stack.get_vector3(4)
  2201. , stack.get_float(5)
  2202. )) {
  2203. stack.push_bool(true);
  2204. stack.push_vector3(hit.position);
  2205. stack.push_vector3(hit.normal);
  2206. stack.push_float(hit.time);
  2207. stack.push_unit(hit.unit);
  2208. stack.push_actor(hit.actor);
  2209. return 6;
  2210. }
  2211. stack.push_bool(false);
  2212. return 1;
  2213. });
  2214. env.add_module_function("PhysicsWorld", "enable_debug_drawing", [](lua_State *L) {
  2215. LuaStack stack(L);
  2216. stack.get_physics_world(1)->enable_debug_drawing(stack.get_bool(2));
  2217. return 0;
  2218. });
  2219. env.add_module_metafunction("PhysicsWorld", "__tostring", [](lua_State *L) {
  2220. LuaStack stack(L);
  2221. PhysicsWorld *pw = stack.get_physics_world(1);
  2222. stack.push_fstring("PhysicsWorld (%p)", pw);
  2223. return 1;
  2224. });
  2225. env.add_module_function("SoundWorld", "stop_all", [](lua_State *L) {
  2226. LuaStack stack(L);
  2227. stack.get_sound_world(1)->stop_all();
  2228. return 0;
  2229. });
  2230. env.add_module_function("SoundWorld", "pause_all", [](lua_State *L) {
  2231. LuaStack stack(L);
  2232. stack.get_sound_world(1)->pause_all();
  2233. return 0;
  2234. });
  2235. env.add_module_function("SoundWorld", "resume_all", [](lua_State *L) {
  2236. LuaStack stack(L);
  2237. stack.get_sound_world(1)->resume_all();
  2238. return 0;
  2239. });
  2240. env.add_module_function("SoundWorld", "is_playing", [](lua_State *L) {
  2241. LuaStack stack(L);
  2242. stack.push_bool(stack.get_sound_world(1)->is_playing(stack.get_sound_instance_id(2)));
  2243. return 1;
  2244. });
  2245. env.add_module_metafunction("SoundWorld", "__tostring", [](lua_State *L) {
  2246. LuaStack stack(L);
  2247. SoundWorld *sw = stack.get_sound_world(1);
  2248. stack.push_fstring("SoundWorld (%p)", sw);
  2249. return 1;
  2250. });
  2251. env.add_module_function("AnimationStateMachine", "instance", [](lua_State *L) {
  2252. LuaStack stack(L);
  2253. StateMachineInstance inst = stack.get_animation_state_machine(1)->instance(stack.get_unit(2));
  2254. if (is_valid(inst))
  2255. stack.push_state_machine_instance(inst);
  2256. else
  2257. stack.push_nil();
  2258. return 1;
  2259. });
  2260. env.add_module_function("AnimationStateMachine", "trigger", [](lua_State *L) {
  2261. LuaStack stack(L);
  2262. stack.get_animation_state_machine(1)->trigger(stack.get_state_machine_instance(2)
  2263. , stack.get_string_id_32(3)
  2264. );
  2265. return 0;
  2266. });
  2267. env.add_module_function("AnimationStateMachine", "variable_id", [](lua_State *L) {
  2268. LuaStack stack(L);
  2269. u32 variable_id = stack.get_animation_state_machine(1)->variable_id(stack.get_state_machine_instance(2)
  2270. , stack.get_string_id_32(3)
  2271. );
  2272. LUA_ASSERT(variable_id != UINT32_MAX, stack, "Variable does not exist");
  2273. stack.push_id(variable_id);
  2274. return 1;
  2275. });
  2276. env.add_module_function("AnimationStateMachine", "variable", [](lua_State *L) {
  2277. LuaStack stack(L);
  2278. const float v = stack.get_animation_state_machine(1)->variable(stack.get_state_machine_instance(2)
  2279. , stack.get_id(3)
  2280. );
  2281. stack.push_float(v);
  2282. return 1;
  2283. });
  2284. env.add_module_function("AnimationStateMachine", "set_variable", [](lua_State *L) {
  2285. LuaStack stack(L);
  2286. stack.get_animation_state_machine(1)->set_variable(stack.get_state_machine_instance(2)
  2287. , stack.get_id(3)
  2288. , stack.get_float(4)
  2289. );
  2290. return 0;
  2291. });
  2292. env.add_module_function("Device", "argv", [](lua_State *L) {
  2293. LuaStack stack(L);
  2294. const int argc = device()->argc();
  2295. const char **argv = device()->argv();
  2296. stack.push_table(argc);
  2297. for (int i = 0; i < argc; ++i) {
  2298. stack.push_key_begin(i + 1);
  2299. stack.push_string(argv[i]);
  2300. stack.push_key_end();
  2301. }
  2302. return 1;
  2303. });
  2304. env.add_module_function("Device", "platform", [](lua_State *L) {
  2305. LuaStack stack(L);
  2306. stack.push_string(CROWN_PLATFORM_NAME);
  2307. return 1;
  2308. });
  2309. env.add_module_function("Device", "architecture", [](lua_State *L) {
  2310. LuaStack stack(L);
  2311. stack.push_string(CROWN_ARCH_NAME);
  2312. return 1;
  2313. });
  2314. env.add_module_function("Device", "version", [](lua_State *L) {
  2315. LuaStack stack(L);
  2316. stack.push_string(CROWN_VERSION);
  2317. return 1;
  2318. });
  2319. env.add_module_function("Device", "quit", [](lua_State * /*L*/) {
  2320. device()->quit();
  2321. return 0;
  2322. });
  2323. env.add_module_function("Device", "resolution", [](lua_State *L) {
  2324. LuaStack stack(L);
  2325. u16 w, h;
  2326. device()->resolution(w, h);
  2327. stack.push_int(w);
  2328. stack.push_int(h);
  2329. return 2;
  2330. });
  2331. env.add_module_function("Device", "create_world", [](lua_State *L) {
  2332. LuaStack stack(L);
  2333. stack.push_world(device()->create_world());
  2334. return 1;
  2335. });
  2336. env.add_module_function("Device", "destroy_world", [](lua_State *L) {
  2337. LuaStack stack(L);
  2338. device()->destroy_world(*stack.get_world(1));
  2339. return 0;
  2340. });
  2341. env.add_module_function("Device", "render", [](lua_State *L) {
  2342. LuaStack stack(L);
  2343. device()->render(*stack.get_world(1), stack.get_unit(2));
  2344. return 0;
  2345. });
  2346. env.add_module_function("Device", "create_resource_package", [](lua_State *L) {
  2347. LuaStack stack(L);
  2348. stack.push_resource_package(device()->create_resource_package(stack.get_resource_name(1)));
  2349. return 1;
  2350. });
  2351. env.add_module_function("Device", "destroy_resource_package", [](lua_State *L) {
  2352. LuaStack stack(L);
  2353. device()->destroy_resource_package(*stack.get_resource_package(1));
  2354. return 0;
  2355. });
  2356. env.add_module_function("Device", "console_send", [](lua_State *L) {
  2357. LuaStack stack(L);
  2358. LUA_ASSERT(stack.is_table(1), stack, "Table expected");
  2359. TempAllocator1024 alloc;
  2360. StringStream json(alloc);
  2361. lua_dump_table(L, 1, json);
  2362. console_server()->broadcast(string_stream::c_str(json));
  2363. return 0;
  2364. });
  2365. env.add_module_function("Device", "can_get", [](lua_State *L) {
  2366. LuaStack stack(L);
  2367. const StringId64 type(stack.get_string(1));
  2368. stack.push_bool(device()->_resource_manager->can_get(type, stack.get_resource_name(2)));
  2369. return 1;
  2370. });
  2371. env.add_module_function("Device", "enable_resource_autoload", [](lua_State *L) {
  2372. LuaStack stack(L);
  2373. device()->_resource_manager->enable_autoload(stack.get_bool(1));
  2374. return 0;
  2375. });
  2376. env.add_module_function("Device", "temp_count", [](lua_State *L) {
  2377. LuaStack stack(L);
  2378. u32 nv, nq, nm;
  2379. device()->_lua_environment->temp_count(nv, nq, nm);
  2380. stack.push_int(nv);
  2381. stack.push_int(nq);
  2382. stack.push_int(nm);
  2383. return 3;
  2384. });
  2385. env.add_module_function("Device", "set_temp_count", [](lua_State *L) {
  2386. LuaStack stack(L);
  2387. u32 nv = stack.get_int(1);
  2388. u32 nq = stack.get_int(2);
  2389. u32 nm = stack.get_int(3);
  2390. device()->_lua_environment->set_temp_count(nv, nq, nm);
  2391. return 0;
  2392. });
  2393. env.add_module_function("Device", "guid", [](lua_State *L) {
  2394. LuaStack stack(L);
  2395. Guid guid = guid::new_guid();
  2396. char buf[37];
  2397. guid::to_string(buf, sizeof(buf), guid);
  2398. stack.push_string(buf);
  2399. return 1;
  2400. });
  2401. env.add_module_function("Profiler", "enter_scope", [](lua_State *L) {
  2402. LuaStack stack(L);
  2403. profiler::enter_profile_scope(stack.get_string(1));
  2404. return 0;
  2405. });
  2406. env.add_module_function("Profiler", "leave_scope", [](lua_State *L) {
  2407. LuaStack stack(L);
  2408. profiler::leave_profile_scope();
  2409. return 0;
  2410. });
  2411. env.add_module_function("Profiler", "record", [](lua_State *L) {
  2412. LuaStack stack(L);
  2413. const char *name = stack.get_string(1);
  2414. if (stack.is_number(2))
  2415. profiler::record_float(name, stack.get_float(2));
  2416. else
  2417. profiler::record_vector3(name, stack.get_vector3(2));
  2418. return 0;
  2419. });
  2420. env.add_module_function("DebugLine", "add_line", [](lua_State *L) {
  2421. LuaStack stack(L);
  2422. stack.get_debug_line(1)->add_line(stack.get_vector3(2)
  2423. , stack.get_vector3(3)
  2424. , stack.get_color4(4)
  2425. );
  2426. return 0;
  2427. });
  2428. env.add_module_function("DebugLine", "add_axes", [](lua_State *L) {
  2429. LuaStack stack(L);
  2430. const f32 len = stack.num_args() == 3
  2431. ? stack.get_float(3)
  2432. : 1.0f
  2433. ;
  2434. stack.get_debug_line(1)->add_axes(stack.get_matrix4x4(2), len);
  2435. return 0;
  2436. });
  2437. env.add_module_function("DebugLine", "add_arc", [](lua_State *L) {
  2438. LuaStack stack(L);
  2439. const u32 segments = stack.num_args() >= 7
  2440. ? stack.get_int(7)
  2441. : DebugLine::NUM_SEGMENTS
  2442. ;
  2443. stack.get_debug_line(1)->add_arc(stack.get_vector3(2)
  2444. , stack.get_float(3)
  2445. , stack.get_vector3(4)
  2446. , stack.get_vector3(5)
  2447. , stack.get_color4(6)
  2448. , segments
  2449. );
  2450. return 0;
  2451. });
  2452. env.add_module_function("DebugLine", "add_circle", [](lua_State *L) {
  2453. LuaStack stack(L);
  2454. const u32 segments = stack.num_args() >= 6
  2455. ? stack.get_int(6)
  2456. : DebugLine::NUM_SEGMENTS
  2457. ;
  2458. stack.get_debug_line(1)->add_circle(stack.get_vector3(2)
  2459. , stack.get_float(3)
  2460. , stack.get_vector3(4)
  2461. , stack.get_color4(5)
  2462. , segments
  2463. );
  2464. return 0;
  2465. });
  2466. env.add_module_function("DebugLine", "add_cone", [](lua_State *L) {
  2467. LuaStack stack(L);
  2468. const u32 segments = stack.num_args() >= 6
  2469. ? stack.get_int(6)
  2470. : DebugLine::NUM_SEGMENTS
  2471. ;
  2472. stack.get_debug_line(1)->add_cone(stack.get_vector3(2)
  2473. , stack.get_vector3(3)
  2474. , stack.get_float(4)
  2475. , stack.get_color4(5)
  2476. , segments
  2477. );
  2478. return 0;
  2479. });
  2480. env.add_module_function("DebugLine", "add_sphere", [](lua_State *L) {
  2481. LuaStack stack(L);
  2482. const u32 segments = stack.num_args() >= 5
  2483. ? stack.get_int(5)
  2484. : DebugLine::NUM_SEGMENTS
  2485. ;
  2486. stack.get_debug_line(1)->add_sphere(stack.get_vector3(2)
  2487. , stack.get_float(3)
  2488. , stack.get_color4(4)
  2489. , segments
  2490. );
  2491. return 0;
  2492. });
  2493. env.add_module_function("DebugLine", "add_obb", [](lua_State *L) {
  2494. LuaStack stack(L);
  2495. stack.get_debug_line(1)->add_obb(stack.get_matrix4x4(2)
  2496. , stack.get_vector3(3)
  2497. , stack.get_color4(4)
  2498. );
  2499. return 0;
  2500. });
  2501. env.add_module_function("DebugLine", "add_frustum", [](lua_State *L) {
  2502. LuaStack stack(L);
  2503. stack.get_debug_line(1)->add_frustum(stack.get_matrix4x4(2)
  2504. , stack.get_color4(3)
  2505. );
  2506. return 0;
  2507. });
  2508. env.add_module_function("DebugLine", "reset", [](lua_State *L) {
  2509. LuaStack stack(L);
  2510. stack.get_debug_line(1)->reset();
  2511. return 0;
  2512. });
  2513. env.add_module_function("DebugLine", "submit", [](lua_State *L) {
  2514. LuaStack stack(L);
  2515. stack.get_debug_line(1)->submit();
  2516. return 0;
  2517. });
  2518. env.add_module_metafunction("DebugLine", "__tostring", [](lua_State *L) {
  2519. LuaStack stack(L);
  2520. stack.push_fstring("DebugLine (%p)", stack.get_debug_line(1));
  2521. return 1;
  2522. });
  2523. env.add_module_function("ResourcePackage", "load", [](lua_State *L) {
  2524. LuaStack stack(L);
  2525. stack.get_resource_package(1)->load();
  2526. return 0;
  2527. });
  2528. env.add_module_function("ResourcePackage", "unload", [](lua_State *L) {
  2529. LuaStack stack(L);
  2530. stack.get_resource_package(1)->unload();
  2531. return 0;
  2532. });
  2533. env.add_module_function("ResourcePackage", "flush", [](lua_State *L) {
  2534. LuaStack stack(L);
  2535. stack.get_resource_package(1)->flush();
  2536. return 0;
  2537. });
  2538. env.add_module_function("ResourcePackage", "has_loaded", [](lua_State *L) {
  2539. LuaStack stack(L);
  2540. stack.push_bool(stack.get_resource_package(1)->has_loaded());
  2541. return 1;
  2542. });
  2543. env.add_module_metafunction("ResourcePackage", "__tostring", [](lua_State *L) {
  2544. LuaStack stack(L);
  2545. ResourcePackage *package = stack.get_resource_package(1);
  2546. stack.push_fstring("ResourcePackage (%p)", package);
  2547. return 1;
  2548. });
  2549. env.add_module_function("Material", "set_float", [](lua_State *L) {
  2550. LuaStack stack(L);
  2551. stack.get_material(1)->set_float(stack.get_string_id_32(2), stack.get_float(3));
  2552. return 0;
  2553. });
  2554. env.add_module_function("Material", "set_vector2", [](lua_State *L) {
  2555. LuaStack stack(L);
  2556. stack.get_material(1)->set_vector2(stack.get_string_id_32(2), stack.get_vector2(3));
  2557. return 0;
  2558. });
  2559. env.add_module_function("Material", "set_vector3", [](lua_State *L) {
  2560. LuaStack stack(L);
  2561. stack.get_material(1)->set_vector3(stack.get_string_id_32(2), stack.get_vector3(3));
  2562. return 0;
  2563. });
  2564. env.add_module_function("Material", "set_vector4", [](lua_State *L) {
  2565. LuaStack stack(L);
  2566. stack.get_material(1)->set_vector4(stack.get_string_id_32(2), stack.get_vector4(3));
  2567. return 0;
  2568. });
  2569. env.add_module_function("Material", "set_matrix4x4", [](lua_State *L) {
  2570. LuaStack stack(L);
  2571. stack.get_material(1)->set_matrix4x4(stack.get_string_id_32(2), stack.get_matrix4x4(3));
  2572. return 0;
  2573. });
  2574. env.add_module_function("Gui", "move", [](lua_State *L) {
  2575. LuaStack stack(L);
  2576. stack.get_gui(1)->move(stack.get_vector2(2));
  2577. return 0;
  2578. });
  2579. env.add_module_function("Gui", "triangle", [](lua_State *L) {
  2580. LuaStack stack(L);
  2581. stack.get_gui(1)->triangle(stack.get_vector2(2)
  2582. , stack.get_vector2(3)
  2583. , stack.get_vector2(4)
  2584. , stack.get_color4(5)
  2585. );
  2586. return 0;
  2587. });
  2588. env.add_module_function("Gui", "rect", [](lua_State *L) {
  2589. LuaStack stack(L);
  2590. stack.get_gui(1)->rect(stack.get_vector2(2)
  2591. , stack.get_vector2(3)
  2592. , stack.get_color4(4)
  2593. );
  2594. return 0;
  2595. });
  2596. env.add_module_function("Gui", "image", [](lua_State *L) {
  2597. LuaStack stack(L);
  2598. stack.get_gui(1)->image(stack.get_vector2(2)
  2599. , stack.get_vector2(3)
  2600. , stack.get_resource_name(4)
  2601. , stack.get_color4(5)
  2602. );
  2603. return 0;
  2604. });
  2605. env.add_module_function("Gui", "image_uv", [](lua_State *L) {
  2606. LuaStack stack(L);
  2607. stack.get_gui(1)->image_uv(stack.get_vector2(2)
  2608. , stack.get_vector2(3)
  2609. , stack.get_vector2(4)
  2610. , stack.get_vector2(5)
  2611. , stack.get_resource_name(6)
  2612. , stack.get_color4(7)
  2613. );
  2614. return 0;
  2615. });
  2616. env.add_module_function("Gui", "text", [](lua_State *L) {
  2617. LuaStack stack(L);
  2618. stack.get_gui(1)->text(stack.get_vector2(2)
  2619. , stack.get_int(3)
  2620. , stack.get_string(4)
  2621. , stack.get_resource_name(5)
  2622. , stack.get_resource_name(6)
  2623. , stack.get_color4(7)
  2624. );
  2625. return 0;
  2626. });
  2627. env.add_module_function("Display", "modes", [](lua_State *L) {
  2628. LuaStack stack(L);
  2629. TempAllocator1024 ta;
  2630. Array<DisplayMode> modes(ta);
  2631. device()->_display->modes(modes);
  2632. stack.push_table(array::size(modes));
  2633. for (u32 i = 0; i < array::size(modes); ++i) {
  2634. stack.push_key_begin(i + 1);
  2635. stack.push_table(3);
  2636. {
  2637. stack.push_key_begin("id");
  2638. stack.push_int(modes[i].id);
  2639. stack.push_key_end();
  2640. stack.push_key_begin("width");
  2641. stack.push_int(modes[i].width);
  2642. stack.push_key_end();
  2643. stack.push_key_begin("height");
  2644. stack.push_int(modes[i].height);
  2645. stack.push_key_end();
  2646. }
  2647. stack.push_key_end();
  2648. }
  2649. return 1;
  2650. });
  2651. env.add_module_function("Display", "set_mode", [](lua_State *L) {
  2652. LuaStack stack(L);
  2653. device()->_display->set_mode(stack.get_int(1));
  2654. return 0;
  2655. });
  2656. env.add_module_function("Window", "show", [](lua_State *L) {
  2657. LuaStack stack(L);
  2658. device()->_window->show();
  2659. return 0;
  2660. });
  2661. env.add_module_function("Window", "hide", [](lua_State *L) {
  2662. LuaStack stack(L);
  2663. device()->_window->hide();
  2664. return 0;
  2665. });
  2666. env.add_module_function("Window", "resize", [](lua_State *L) {
  2667. LuaStack stack(L);
  2668. device()->_window->resize(stack.get_int(1), stack.get_int(2));
  2669. return 0;
  2670. });
  2671. env.add_module_function("Window", "move", [](lua_State *L) {
  2672. LuaStack stack(L);
  2673. device()->_window->move(stack.get_int(1), stack.get_int(2));
  2674. return 0;
  2675. });
  2676. env.add_module_function("Window", "minimize", [](lua_State * /*L*/) {
  2677. device()->_window->minimize();
  2678. return 0;
  2679. });
  2680. env.add_module_function("Window", "maximize", [](lua_State * /*L*/) {
  2681. device()->_window->maximize();
  2682. return 0;
  2683. });
  2684. env.add_module_function("Window", "restore", [](lua_State * /*L*/) {
  2685. device()->_window->restore();
  2686. return 0;
  2687. });
  2688. env.add_module_function("Window", "title", [](lua_State *L) {
  2689. LuaStack stack(L);
  2690. stack.push_string(device()->_window->title());
  2691. return 1;
  2692. });
  2693. env.add_module_function("Window", "set_title", [](lua_State *L) {
  2694. LuaStack stack(L);
  2695. device()->_window->set_title(stack.get_string(1));
  2696. return 0;
  2697. });
  2698. env.add_module_function("Window", "show_cursor", [](lua_State *L) {
  2699. LuaStack stack(L);
  2700. device()->_window->show_cursor(stack.get_bool(1));
  2701. return 0;
  2702. });
  2703. env.add_module_function("Window", "set_fullscreen", [](lua_State *L) {
  2704. LuaStack stack(L);
  2705. device()->_window->set_fullscreen(stack.get_bool(1));
  2706. return 0;
  2707. });
  2708. env.add_module_function("Window", "set_cursor", [](lua_State *L) {
  2709. LuaStack stack(L);
  2710. const char *name = stack.get_string(1);
  2711. const MouseCursor::Enum mc = name_to_mouse_cursor(name);
  2712. LUA_ASSERT(mc != MouseCursor::COUNT, stack, "Unknown mouse cursor: '%s'", name);
  2713. device()->_window->set_cursor(mc);
  2714. return 0;
  2715. });
  2716. env.add_module_function("Window", "set_cursor_mode", [](lua_State *L) {
  2717. LuaStack stack(L);
  2718. const char *name = stack.get_string(1);
  2719. const CursorMode::Enum cm = name_to_cursor_mode(name);
  2720. LUA_ASSERT(cm != CursorMode::COUNT, stack, "Unknown cursor mode: '%s'", name);
  2721. device()->_window->set_cursor_mode(cm);
  2722. return 0;
  2723. });
  2724. env.add_module_function("Input", "events", [](lua_State *L) {
  2725. LuaStack stack(L);
  2726. InputEvent *events = device()->_input_manager->_events;
  2727. u32 num_events = device()->_input_manager->_num_events;
  2728. stack.push_table(num_events);
  2729. for (u32 i = 0; i < num_events; ++i) {
  2730. stack.push_key_begin(i + 1);
  2731. stack.push_table(4);
  2732. {
  2733. stack.push_key_begin("id");
  2734. stack.push_int(events[i].id);
  2735. stack.push_key_end();
  2736. stack.push_key_begin("type");
  2737. stack.push_int(events[i].type);
  2738. stack.push_key_end();
  2739. if (events[i].type == InputEventType::AXIS_CHANGED) {
  2740. stack.push_key_begin("value");
  2741. stack.push_vector3(events[i].value);
  2742. stack.push_key_end();
  2743. }
  2744. stack.push_key_begin("device");
  2745. lua_rawgeti(L, LUA_REGISTRYINDEX, events[i].device->_lua_object);
  2746. stack.push_key_end();
  2747. }
  2748. stack.push_key_end();
  2749. }
  2750. return 1;
  2751. });
  2752. env.set_module_number("InputEventType", "BUTTON_PRESSED", InputEventType::BUTTON_PRESSED);
  2753. env.set_module_number("InputEventType", "BUTTON_RELEASED", InputEventType::BUTTON_RELEASED);
  2754. env.set_module_number("InputEventType", "AXIS_CHANGED", InputEventType::AXIS_CHANGED);
  2755. // code-format on
  2756. }
  2757. } // namespace crown