l_localplayer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. Minetest
  3. Copyright (C) 2017 Dumbeldor, Vincent Glize <vincent.glize@live.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "l_localplayer.h"
  17. #include "l_internal.h"
  18. #include "lua_api/l_item.h"
  19. #include "script/common/c_converter.h"
  20. #include "client/localplayer.h"
  21. #include "hud.h"
  22. #include "common/c_content.h"
  23. #include "client/content_cao.h"
  24. LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
  25. {
  26. }
  27. void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m)
  28. {
  29. lua_getglobal(L, "core");
  30. luaL_checktype(L, -1, LUA_TTABLE);
  31. int objectstable = lua_gettop(L);
  32. lua_getfield(L, -1, "localplayer");
  33. // Duplication check
  34. if (lua_type(L, -1) == LUA_TUSERDATA) {
  35. lua_pop(L, 1);
  36. return;
  37. }
  38. LuaLocalPlayer *o = new LuaLocalPlayer(m);
  39. *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
  40. luaL_getmetatable(L, className);
  41. lua_setmetatable(L, -2);
  42. lua_pushvalue(L, lua_gettop(L));
  43. lua_setfield(L, objectstable, "localplayer");
  44. }
  45. int LuaLocalPlayer::l_get_velocity(lua_State *L)
  46. {
  47. LocalPlayer *player = getobject(L, 1);
  48. push_v3f(L, player->getSpeed() / BS);
  49. return 1;
  50. }
  51. int LuaLocalPlayer::l_get_hp(lua_State *L)
  52. {
  53. LocalPlayer *player = getobject(L, 1);
  54. lua_pushinteger(L, player->hp);
  55. return 1;
  56. }
  57. int LuaLocalPlayer::l_get_name(lua_State *L)
  58. {
  59. LocalPlayer *player = getobject(L, 1);
  60. lua_pushstring(L, player->getName());
  61. return 1;
  62. }
  63. // get_wield_index(self)
  64. int LuaLocalPlayer::l_get_wield_index(lua_State *L)
  65. {
  66. LocalPlayer *player = getobject(L, 1);
  67. lua_pushinteger(L, player->getWieldIndex());
  68. return 1;
  69. }
  70. // get_wielded_item(self)
  71. int LuaLocalPlayer::l_get_wielded_item(lua_State *L)
  72. {
  73. LocalPlayer *player = getobject(L, 1);
  74. ItemStack selected_item;
  75. player->getWieldedItem(&selected_item, nullptr);
  76. LuaItemStack::create(L, selected_item);
  77. return 1;
  78. }
  79. int LuaLocalPlayer::l_is_attached(lua_State *L)
  80. {
  81. LocalPlayer *player = getobject(L, 1);
  82. lua_pushboolean(L, player->getParent() != nullptr);
  83. return 1;
  84. }
  85. int LuaLocalPlayer::l_is_touching_ground(lua_State *L)
  86. {
  87. LocalPlayer *player = getobject(L, 1);
  88. lua_pushboolean(L, player->touching_ground);
  89. return 1;
  90. }
  91. int LuaLocalPlayer::l_is_in_liquid(lua_State *L)
  92. {
  93. LocalPlayer *player = getobject(L, 1);
  94. lua_pushboolean(L, player->in_liquid);
  95. return 1;
  96. }
  97. int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
  98. {
  99. LocalPlayer *player = getobject(L, 1);
  100. lua_pushboolean(L, player->in_liquid_stable);
  101. return 1;
  102. }
  103. int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
  104. {
  105. LocalPlayer *player = getobject(L, 1);
  106. lua_pushinteger(L, player->liquid_viscosity);
  107. return 1;
  108. }
  109. int LuaLocalPlayer::l_is_climbing(lua_State *L)
  110. {
  111. LocalPlayer *player = getobject(L, 1);
  112. lua_pushboolean(L, player->is_climbing);
  113. return 1;
  114. }
  115. int LuaLocalPlayer::l_swimming_vertical(lua_State *L)
  116. {
  117. LocalPlayer *player = getobject(L, 1);
  118. lua_pushboolean(L, player->swimming_vertical);
  119. return 1;
  120. }
  121. // get_physics_override(self)
  122. int LuaLocalPlayer::l_get_physics_override(lua_State *L)
  123. {
  124. LocalPlayer *player = getobject(L, 1);
  125. lua_newtable(L);
  126. lua_pushnumber(L, player->physics_override_speed);
  127. lua_setfield(L, -2, "speed");
  128. lua_pushnumber(L, player->physics_override_jump);
  129. lua_setfield(L, -2, "jump");
  130. lua_pushnumber(L, player->physics_override_gravity);
  131. lua_setfield(L, -2, "gravity");
  132. lua_pushboolean(L, player->physics_override_sneak);
  133. lua_setfield(L, -2, "sneak");
  134. lua_pushboolean(L, player->physics_override_sneak_glitch);
  135. lua_setfield(L, -2, "sneak_glitch");
  136. lua_pushboolean(L, player->physics_override_new_move);
  137. lua_setfield(L, -2, "new_move");
  138. return 1;
  139. }
  140. int LuaLocalPlayer::l_get_last_pos(lua_State *L)
  141. {
  142. LocalPlayer *player = getobject(L, 1);
  143. push_v3f(L, player->last_position / BS);
  144. return 1;
  145. }
  146. int LuaLocalPlayer::l_get_last_velocity(lua_State *L)
  147. {
  148. LocalPlayer *player = getobject(L, 1);
  149. push_v3f(L, player->last_speed);
  150. return 1;
  151. }
  152. int LuaLocalPlayer::l_get_last_look_vertical(lua_State *L)
  153. {
  154. LocalPlayer *player = getobject(L, 1);
  155. lua_pushnumber(L, -1.0 * player->last_pitch * core::DEGTORAD);
  156. return 1;
  157. }
  158. int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L)
  159. {
  160. LocalPlayer *player = getobject(L, 1);
  161. lua_pushnumber(L, (player->last_yaw + 90.) * core::DEGTORAD);
  162. return 1;
  163. }
  164. // get_control(self)
  165. int LuaLocalPlayer::l_get_control(lua_State *L)
  166. {
  167. LocalPlayer *player = getobject(L, 1);
  168. const PlayerControl &c = player->getPlayerControl();
  169. auto set = [L] (const char *name, bool value) {
  170. lua_pushboolean(L, value);
  171. lua_setfield(L, -2, name);
  172. };
  173. lua_createtable(L, 0, 12);
  174. set("jump", c.jump);
  175. set("aux1", c.aux1);
  176. set("sneak", c.sneak);
  177. set("zoom", c.zoom);
  178. set("dig", c.dig);
  179. set("place", c.place);
  180. // Player movement in polar coordinates and non-binary speed
  181. set("movement_speed", c.movement_speed);
  182. set("movement_direction", c.movement_direction);
  183. // Provide direction keys to ensure compatibility
  184. set("up", player->keyPressed & (1 << 0)); // Up, down, left, and right were removed in favor of
  185. set("down", player->keyPressed & (1 << 1)); // analog direction indicators and are therefore not
  186. set("left", player->keyPressed & (1 << 2)); // available as booleans anymore. The corresponding values
  187. set("right", player->keyPressed & (1 << 3)); // can still be read from the keyPressed bits though.
  188. return 1;
  189. }
  190. // get_breath(self)
  191. int LuaLocalPlayer::l_get_breath(lua_State *L)
  192. {
  193. LocalPlayer *player = getobject(L, 1);
  194. lua_pushinteger(L, player->getBreath());
  195. return 1;
  196. }
  197. // get_pos(self)
  198. int LuaLocalPlayer::l_get_pos(lua_State *L)
  199. {
  200. LocalPlayer *player = getobject(L, 1);
  201. push_v3f(L, player->getPosition() / BS);
  202. return 1;
  203. }
  204. // get_movement_acceleration(self)
  205. int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L)
  206. {
  207. LocalPlayer *player = getobject(L, 1);
  208. lua_newtable(L);
  209. lua_pushnumber(L, player->movement_acceleration_default);
  210. lua_setfield(L, -2, "default");
  211. lua_pushnumber(L, player->movement_acceleration_air);
  212. lua_setfield(L, -2, "air");
  213. lua_pushnumber(L, player->movement_acceleration_fast);
  214. lua_setfield(L, -2, "fast");
  215. return 1;
  216. }
  217. // get_movement_speed(self)
  218. int LuaLocalPlayer::l_get_movement_speed(lua_State *L)
  219. {
  220. LocalPlayer *player = getobject(L, 1);
  221. lua_newtable(L);
  222. lua_pushnumber(L, player->movement_speed_walk);
  223. lua_setfield(L, -2, "walk");
  224. lua_pushnumber(L, player->movement_speed_crouch);
  225. lua_setfield(L, -2, "crouch");
  226. lua_pushnumber(L, player->movement_speed_fast);
  227. lua_setfield(L, -2, "fast");
  228. lua_pushnumber(L, player->movement_speed_climb);
  229. lua_setfield(L, -2, "climb");
  230. lua_pushnumber(L, player->movement_speed_jump);
  231. lua_setfield(L, -2, "jump");
  232. return 1;
  233. }
  234. // get_movement(self)
  235. int LuaLocalPlayer::l_get_movement(lua_State *L)
  236. {
  237. LocalPlayer *player = getobject(L, 1);
  238. lua_newtable(L);
  239. lua_pushnumber(L, player->movement_liquid_fluidity);
  240. lua_setfield(L, -2, "liquid_fluidity");
  241. lua_pushnumber(L, player->movement_liquid_fluidity_smooth);
  242. lua_setfield(L, -2, "liquid_fluidity_smooth");
  243. lua_pushnumber(L, player->movement_liquid_sink);
  244. lua_setfield(L, -2, "liquid_sink");
  245. lua_pushnumber(L, player->movement_gravity);
  246. lua_setfield(L, -2, "gravity");
  247. return 1;
  248. }
  249. // get_armor_groups(self)
  250. int LuaLocalPlayer::l_get_armor_groups(lua_State *L)
  251. {
  252. LocalPlayer *player = getobject(L, 1);
  253. push_groups(L, player->getCAO()->getGroups());
  254. return 1;
  255. }
  256. // hud_add(self, form)
  257. int LuaLocalPlayer::l_hud_add(lua_State *L)
  258. {
  259. LocalPlayer *player = getobject(L, 1);
  260. HudElement *elem = new HudElement;
  261. read_hud_element(L, elem);
  262. u32 id = player->addHud(elem);
  263. if (id == U32_MAX) {
  264. delete elem;
  265. return 0;
  266. }
  267. lua_pushnumber(L, id);
  268. return 1;
  269. }
  270. // hud_remove(self, id)
  271. int LuaLocalPlayer::l_hud_remove(lua_State *L)
  272. {
  273. LocalPlayer *player = getobject(L, 1);
  274. u32 id = luaL_checkinteger(L, 2);
  275. HudElement *element = player->removeHud(id);
  276. if (!element)
  277. lua_pushboolean(L, false);
  278. else
  279. lua_pushboolean(L, true);
  280. delete element;
  281. return 1;
  282. }
  283. // hud_change(self, id, stat, data)
  284. int LuaLocalPlayer::l_hud_change(lua_State *L)
  285. {
  286. LocalPlayer *player = getobject(L, 1);
  287. u32 id = luaL_checkinteger(L, 2);
  288. HudElement *element = player->getHud(id);
  289. if (!element)
  290. return 0;
  291. HudElementStat stat;
  292. void *unused;
  293. bool ok = read_hud_change(L, stat, element, &unused);
  294. lua_pushboolean(L, ok);
  295. return 1;
  296. }
  297. // hud_get(self, id)
  298. int LuaLocalPlayer::l_hud_get(lua_State *L)
  299. {
  300. LocalPlayer *player = getobject(L, 1);
  301. u32 id = luaL_checkinteger(L, -1);
  302. HudElement *e = player->getHud(id);
  303. if (!e) {
  304. lua_pushnil(L);
  305. return 1;
  306. }
  307. push_hud_element(L, e);
  308. return 1;
  309. }
  310. LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
  311. {
  312. luaL_checktype(L, narg, LUA_TUSERDATA);
  313. void *ud = luaL_checkudata(L, narg, className);
  314. if (!ud)
  315. luaL_typerror(L, narg, className);
  316. return *(LuaLocalPlayer **)ud;
  317. }
  318. LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
  319. {
  320. return ref->m_localplayer;
  321. }
  322. LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
  323. {
  324. LuaLocalPlayer *ref = checkobject(L, narg);
  325. assert(ref);
  326. LocalPlayer *player = getobject(ref);
  327. assert(player);
  328. return player;
  329. }
  330. int LuaLocalPlayer::gc_object(lua_State *L)
  331. {
  332. LuaLocalPlayer *o = *(LuaLocalPlayer **)(lua_touserdata(L, 1));
  333. delete o;
  334. return 0;
  335. }
  336. void LuaLocalPlayer::Register(lua_State *L)
  337. {
  338. lua_newtable(L);
  339. int methodtable = lua_gettop(L);
  340. luaL_newmetatable(L, className);
  341. int metatable = lua_gettop(L);
  342. lua_pushliteral(L, "__metatable");
  343. lua_pushvalue(L, methodtable);
  344. lua_settable(L, metatable); // hide metatable from lua getmetatable()
  345. lua_pushliteral(L, "__index");
  346. lua_pushvalue(L, methodtable);
  347. lua_settable(L, metatable);
  348. lua_pushliteral(L, "__gc");
  349. lua_pushcfunction(L, gc_object);
  350. lua_settable(L, metatable);
  351. lua_pop(L, 1); // Drop metatable
  352. luaL_register(L, nullptr, methods); // fill methodtable
  353. lua_pop(L, 1); // Drop methodtable
  354. }
  355. const char LuaLocalPlayer::className[] = "LocalPlayer";
  356. const luaL_Reg LuaLocalPlayer::methods[] = {
  357. luamethod(LuaLocalPlayer, get_velocity),
  358. luamethod(LuaLocalPlayer, get_hp),
  359. luamethod(LuaLocalPlayer, get_name),
  360. luamethod(LuaLocalPlayer, get_wield_index),
  361. luamethod(LuaLocalPlayer, get_wielded_item),
  362. luamethod(LuaLocalPlayer, is_attached),
  363. luamethod(LuaLocalPlayer, is_touching_ground),
  364. luamethod(LuaLocalPlayer, is_in_liquid),
  365. luamethod(LuaLocalPlayer, is_in_liquid_stable),
  366. luamethod(LuaLocalPlayer, get_liquid_viscosity),
  367. luamethod(LuaLocalPlayer, is_climbing),
  368. luamethod(LuaLocalPlayer, swimming_vertical),
  369. luamethod(LuaLocalPlayer, get_physics_override),
  370. // TODO: figure our if these are useful in any way
  371. luamethod(LuaLocalPlayer, get_last_pos),
  372. luamethod(LuaLocalPlayer, get_last_velocity),
  373. luamethod(LuaLocalPlayer, get_last_look_horizontal),
  374. luamethod(LuaLocalPlayer, get_last_look_vertical),
  375. //
  376. luamethod(LuaLocalPlayer, get_control),
  377. luamethod(LuaLocalPlayer, get_breath),
  378. luamethod(LuaLocalPlayer, get_pos),
  379. luamethod(LuaLocalPlayer, get_movement_acceleration),
  380. luamethod(LuaLocalPlayer, get_movement_speed),
  381. luamethod(LuaLocalPlayer, get_movement),
  382. luamethod(LuaLocalPlayer, get_armor_groups),
  383. luamethod(LuaLocalPlayer, hud_add),
  384. luamethod(LuaLocalPlayer, hud_remove),
  385. luamethod(LuaLocalPlayer, hud_change),
  386. luamethod(LuaLocalPlayer, hud_get),
  387. {0, 0}
  388. };