lua_api.cpp 90 KB

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