| 1 |
/* |
| 2 |
Copyright (c) 2010 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <new> |
| 26 |
|
| 27 |
#include "lua.hpp" |
| 28 |
#include "persist_lua.h" |
| 29 |
#include "th.h" |
| 30 |
#include "th_gfx.h" |
| 31 |
#include "th_lua.h" |
| 32 |
#include "th_lua_internal.h" |
| 33 |
#include "th_map.h" |
| 34 |
|
| 35 |
class render_target; |
| 36 |
class sprite_sheet; |
| 37 |
enum class animation_effect; |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
int l_anims_new(lua_State* L) { |
| 42 |
luaT_stdnew<animation_manager>(L, luaT_environindex, true); |
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
int l_anims_set_spritesheet(lua_State* L) { |
| 47 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 48 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L, 2); |
| 49 |
lua_settop(L, 2); |
| 50 |
|
| 51 |
pAnims->set_sprite_sheet(pSheet); |
| 52 |
luaT_setenvfield(L, 1, "sprites"); |
| 53 |
return 1; |
| 54 |
} |
| 55 |
|
| 56 |
//! Set the video target for the sprites. |
| 57 |
/*! |
| 58 |
setCanvas(<video-surface>) |
| 59 |
*/ |
| 60 |
int l_anims_set_canvas(lua_State* L) { |
| 61 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 62 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 63 |
lua_settop(L, 2); |
| 64 |
|
| 65 |
pAnims->set_canvas(pCanvas); |
| 66 |
luaT_setenvfield(L, 1, "target"); |
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
int l_anims_load(lua_State* L) { |
| 71 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 72 |
size_t iStartDataLength, iFrameDataLength, iListDataLength, |
| 73 |
iElementDataLength; |
| 74 |
const uint8_t* pStartData = luaT_checkfile(L, 2, &iStartDataLength); |
| 75 |
const uint8_t* pFrameData = luaT_checkfile(L, 3, &iFrameDataLength); |
| 76 |
const uint8_t* pListData = luaT_checkfile(L, 4, &iListDataLength); |
| 77 |
const uint8_t* pElementData = luaT_checkfile(L, 5, &iElementDataLength); |
| 78 |
|
| 79 |
if (pAnims->load_from_th_file(pStartData, iStartDataLength, pFrameData, |
| 80 |
iFrameDataLength, pListData, iListDataLength, |
| 81 |
pElementData, iElementDataLength)) { |
| 82 |
lua_pushboolean(L, 1); |
| 83 |
} else { |
| 84 |
lua_pushboolean(L, 0); |
| 85 |
} |
| 86 |
|
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
//! Load custom animations. |
| 91 |
/*! |
| 92 |
loadCustom(<data-of-an-animation-file>) -> true/false |
| 93 |
*/ |
| 94 |
int l_anims_loadcustom(lua_State* L) { |
| 95 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 96 |
size_t iDataLength; |
| 97 |
const uint8_t* pData = luaT_checkfile(L, 2, &iDataLength); |
| 98 |
|
| 99 |
if (pAnims->load_custom_animations(pData, iDataLength)) { |
| 100 |
lua_pushboolean(L, 1); |
| 101 |
} else { |
| 102 |
lua_pushboolean(L, 0); |
| 103 |
} |
| 104 |
|
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
//! Lua interface for getting a set of animations by name and tile size (one for |
| 109 |
//! each view direction, 'nil' if no animation is available for a direction). |
| 110 |
/*! |
| 111 |
getAnimations(<tile-size>, <animation-name>) -> (<anim-north>, <anim-east>, |
| 112 |
<anim-south>, <anim-west>) |
| 113 |
*/ |
| 114 |
int l_anims_getanims(lua_State* L) { |
| 115 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 116 |
int iTileSize = static_cast<int>(luaL_checkinteger(L, 2)); |
| 117 |
const char* pName = luaL_checkstring(L, 3); |
| 118 |
|
| 119 |
const animation_start_frames& oFrames = |
| 120 |
pAnims->get_named_animations(pName, iTileSize); |
| 121 |
if (oFrames.north < 0) { |
| 122 |
lua_pushnil(L); |
| 123 |
} else { |
| 124 |
lua_pushnumber(L, static_cast<double>(oFrames.north)); |
| 125 |
} |
| 126 |
if (oFrames.east < 0) { |
| 127 |
lua_pushnil(L); |
| 128 |
} else { |
| 129 |
lua_pushnumber(L, static_cast<double>(oFrames.east)); |
| 130 |
} |
| 131 |
if (oFrames.south < 0) { |
| 132 |
lua_pushnil(L); |
| 133 |
} else { |
| 134 |
lua_pushnumber(L, static_cast<double>(oFrames.south)); |
| 135 |
} |
| 136 |
if (oFrames.west < 0) { |
| 137 |
lua_pushnil(L); |
| 138 |
} else { |
| 139 |
lua_pushnumber(L, static_cast<double>(oFrames.west)); |
| 140 |
} |
| 141 |
return 4; |
| 142 |
} |
| 143 |
|
| 144 |
int l_anims_getfirst(lua_State* L) { |
| 145 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 146 |
int iAnim = static_cast<int>(luaL_checkinteger(L, 2)); |
| 147 |
|
| 148 |
lua_pushinteger(L, pAnims->get_first_frame(iAnim)); |
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
int l_anims_getnext(lua_State* L) { |
| 153 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 154 |
int iFrame = static_cast<int>(luaL_checkinteger(L, 2)); |
| 155 |
|
| 156 |
lua_pushinteger(L, pAnims->get_next_frame(iFrame)); |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
int l_anims_set_alt_pal(lua_State* L) { |
| 161 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 162 |
size_t iAnimation = luaL_checkinteger(L, 2); |
| 163 |
size_t iPalLen; |
| 164 |
const uint8_t* pPal = luaT_checkfile(L, 3, &iPalLen); |
| 165 |
if (iPalLen != 256) { |
| 166 |
return luaL_argerror(L, 3, "GhostPalette string is not a valid palette"); |
| 167 |
} |
| 168 |
uint32_t iAlt32 = static_cast<uint32_t>(luaL_checkinteger(L, 4)); |
| 169 |
|
| 170 |
pAnims->set_animation_alt_palette_map(iAnimation, pPal, iAlt32); |
| 171 |
|
| 172 |
lua_getfenv(L, 1); |
| 173 |
lua_insert(L, 2); |
| 174 |
lua_settop(L, 4); |
| 175 |
lua_settable(L, 2); |
| 176 |
lua_settop(L, 1); |
| 177 |
return 1; |
| 178 |
} |
| 179 |
|
| 180 |
int l_anims_set_primary_marker(lua_State* L) { |
| 181 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 182 |
lua_pushboolean( |
| 183 |
L, pAnims->set_frame_primary_marker( |
| 184 |
luaL_checkinteger(L, 2), static_cast<int>(luaL_checkinteger(L, 3)), |
| 185 |
static_cast<int>(luaL_checkinteger(L, 4))) |
| 186 |
? 1 |
| 187 |
: 0); |
| 188 |
return 1; |
| 189 |
} |
| 190 |
|
| 191 |
int l_anims_tick(lua_State* L) { |
| 192 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 193 |
pAnims->tick(); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
int l_anims_set_secondary_marker(lua_State* L) { |
| 198 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 199 |
lua_pushboolean( |
| 200 |
L, pAnims->set_frame_secondary_marker( |
| 201 |
luaL_checkinteger(L, 2), static_cast<int>(luaL_checkinteger(L, 3)), |
| 202 |
static_cast<int>(luaL_checkinteger(L, 4))) |
| 203 |
? 1 |
| 204 |
: 0); |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
int l_anims_draw(lua_State* L) { |
| 209 |
animation_manager* pAnims = luaT_testuserdata<animation_manager>(L); |
| 210 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 211 |
size_t iFrame = luaL_checkinteger(L, 3); |
| 212 |
layers* pLayers = luaT_testuserdata<layers>(L, 4, luaT_upvalueindex(2)); |
| 213 |
int iX = static_cast<int>(luaL_checkinteger(L, 5)); |
| 214 |
int iY = static_cast<int>(luaL_checkinteger(L, 6)); |
| 215 |
int iFlags = static_cast<int>(luaL_optinteger(L, 7, 0)); |
| 216 |
|
| 217 |
pAnims->draw_frame(pCanvas, iFrame, *pLayers, iX, iY, iFlags); |
| 218 |
|
| 219 |
lua_settop(L, 1); |
| 220 |
return 1; |
| 221 |
} |
| 222 |
|
| 223 |
template <typename T> |
| 224 |
int l_anim_new(lua_State* L) { |
| 225 |
T* pAnimation = luaT_stdnew<T>(L, luaT_environindex, true); |
| 226 |
lua_rawgeti(L, luaT_environindex, 2); |
| 227 |
lua_pushlightuserdata(L, pAnimation); |
| 228 |
lua_pushvalue(L, -3); |
| 229 |
lua_rawset(L, -3); |
| 230 |
lua_pop(L, 1); |
| 231 |
return 1; |
| 232 |
} |
| 233 |
|
| 234 |
template <typename T> |
| 235 |
int l_anim_persist(lua_State* L) { |
| 236 |
T* pAnimation; |
| 237 |
if (lua_gettop(L) == 3) { |
| 238 |
pAnimation = luaT_testuserdata<T>(L, 1, luaT_environindex, false); |
| 239 |
luaT_rotate(L, 1, -1); |
| 240 |
} else { |
| 241 |
// Fast __persist call |
| 242 |
pAnimation = (T*)lua_touserdata(L, -1); |
| 243 |
} |
| 244 |
lua_persist_writer* pWriter = (lua_persist_writer*)lua_touserdata(L, 1); |
| 245 |
|
| 246 |
pAnimation->persist(pWriter); |
| 247 |
lua_rawgeti(L, luaT_environindex, 1); |
| 248 |
lua_pushlightuserdata(L, pAnimation); |
| 249 |
lua_gettable(L, -2); |
| 250 |
pWriter->write_stack_object(-1); |
| 251 |
lua_pop(L, 2); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
template <typename T> |
| 256 |
int l_anim_pre_depersist(lua_State* L) { |
| 257 |
// Note that anims and the map have nice reference cycles between them |
| 258 |
// and hence we cannot be sure which is depersisted first. To ensure that |
| 259 |
// things work nicely, we initialise all the fields of a THAnimation as |
| 260 |
// soon as possible, thus preventing issues like an anim -> map -> anim |
| 261 |
// reference chain whereby l_anim_depersist is called after l_map_depersist |
| 262 |
// (as anim references map in its environment table) causing the prev |
| 263 |
// field to be set during map depersistence, then cleared to nullptr by the |
| 264 |
// constructor during l_anim_depersist. |
| 265 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 266 |
new (pAnimation) T; // Call constructor |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
template <typename T> |
| 271 |
int l_anim_depersist(lua_State* L) { |
| 272 |
// Because anim has a pre_depersist function the userdata is already |
| 273 |
// initialized as a T. |
| 274 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 275 |
lua_settop(L, 2); |
| 276 |
lua_insert(L, 1); |
| 277 |
lua_persist_reader* pReader = (lua_persist_reader*)lua_touserdata(L, 1); |
| 278 |
|
| 279 |
lua_rawgeti(L, luaT_environindex, 2); |
| 280 |
lua_pushlightuserdata(L, pAnimation); |
| 281 |
lua_pushvalue(L, 2); |
| 282 |
lua_settable(L, -3); |
| 283 |
lua_pop(L, 1); |
| 284 |
pAnimation->depersist(pReader); |
| 285 |
lua_rawgeti(L, luaT_environindex, 1); |
| 286 |
lua_pushlightuserdata(L, pAnimation); |
| 287 |
if (!pReader->read_stack_object()) return 0; |
| 288 |
lua_settable(L, -3); |
| 289 |
lua_pop(L, 1); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
int l_anim_set_hitresult(lua_State* L) { |
| 294 |
luaL_checktype(L, 1, LUA_TUSERDATA); |
| 295 |
lua_settop(L, 2); |
| 296 |
lua_rawgeti(L, luaT_environindex, 1); |
| 297 |
lua_pushlightuserdata(L, lua_touserdata(L, 1)); |
| 298 |
lua_pushvalue(L, 2); |
| 299 |
lua_settable(L, 3); |
| 300 |
lua_settop(L, 1); |
| 301 |
return 1; |
| 302 |
} |
| 303 |
|
| 304 |
int l_anim_set_frame(lua_State* L) { |
| 305 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 306 |
pAnimation->set_frame(luaL_checkinteger(L, 2)); |
| 307 |
lua_settop(L, 1); |
| 308 |
return 1; |
| 309 |
} |
| 310 |
|
| 311 |
int l_anim_get_frame(lua_State* L) { |
| 312 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 313 |
lua_pushinteger(L, pAnimation->get_frame()); |
| 314 |
return 1; |
| 315 |
} |
| 316 |
|
| 317 |
int l_anim_set_crop(lua_State* L) { |
| 318 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 319 |
pAnimation->set_crop_column(static_cast<int>(luaL_checkinteger(L, 2))); |
| 320 |
lua_settop(L, 1); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
int l_anim_get_crop(lua_State* L) { |
| 325 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 326 |
lua_pushinteger(L, pAnimation->get_crop_column()); |
| 327 |
return 1; |
| 328 |
} |
| 329 |
|
| 330 |
int l_anim_set_anim(lua_State* L) { |
| 331 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 332 |
animation_manager* pManager = luaT_testuserdata<animation_manager>(L, 2); |
| 333 |
lua_Integer iAnim = luaL_checkinteger(L, 3); |
| 334 |
if (iAnim < 0 || |
| 335 |
iAnim >= static_cast<lua_Integer>(pManager->get_animation_count())) |
| 336 |
luaL_argerror(L, 3, "Animation index out of bounds"); |
| 337 |
|
| 338 |
if (lua_isnoneornil(L, 4)) { |
| 339 |
pAnimation->set_flags(0); |
| 340 |
} else { |
| 341 |
pAnimation->set_flags(static_cast<uint32_t>(luaL_checkinteger(L, 4))); |
| 342 |
} |
| 343 |
|
| 344 |
pAnimation->set_animation(pManager, static_cast<size_t>(iAnim)); |
| 345 |
lua_settop(L, 2); |
| 346 |
luaT_setenvfield(L, 1, "animator"); |
| 347 |
lua_pushnil(L); |
| 348 |
luaT_setenvfield(L, 1, "morph_target"); |
| 349 |
|
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
int l_anim_set_morph(lua_State* L) { |
| 354 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 355 |
animation* pMorphTarget = |
| 356 |
luaT_testuserdata<animation>(L, 2, luaT_environindex); |
| 357 |
|
| 358 |
int iDurationFactor = 1; |
| 359 |
if (!lua_isnoneornil(L, 3) && luaL_checkinteger(L, 3) > 0) { |
| 360 |
iDurationFactor = static_cast<int>(luaL_checkinteger(L, 3)); |
| 361 |
} |
| 362 |
|
| 363 |
pAnimation->set_morph_target(pMorphTarget, iDurationFactor); |
| 364 |
lua_settop(L, 2); |
| 365 |
luaT_setenvfield(L, 1, "morph_target"); |
| 366 |
|
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
int l_anim_get_anim(lua_State* L) { |
| 371 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 372 |
lua_pushinteger(L, pAnimation->get_animation()); |
| 373 |
|
| 374 |
return 1; |
| 375 |
} |
| 376 |
|
| 377 |
// setTile(anim/spr_list, map, x, y, drawing_layer). |
| 378 |
// If map is 'nil', remove animation from tile. |
| 379 |
template <typename T> |
| 380 |
int l_anim_set_tile(lua_State* L) { |
| 381 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 382 |
|
| 383 |
if (lua_isnoneornil(L, 2)) { |
| 384 |
pAnimation->remove_from_tile(); |
| 385 |
lua_pushnil(L); |
| 386 |
luaT_setenvfield(L, 1, "map"); |
| 387 |
lua_settop(L, 1); |
| 388 |
} else { |
| 389 |
level_map* pMap = luaT_testuserdata<level_map>(L, 2); |
| 390 |
int x = static_cast<int>(luaL_checkinteger(L, 3)); |
| 391 |
int y = static_cast<int>(luaL_checkinteger(L, 4)); |
| 392 |
int drawing_layer = static_cast<int>(luaL_checkinteger(L, 5)); |
| 393 |
|
| 394 |
map_tile* node = pMap->get_tile(x - 1, y - 1); |
| 395 |
if (node) { |
| 396 |
pAnimation->attach_to_tile({x - 1, y - 1}, node, drawing_layer); |
| 397 |
} else { |
| 398 |
// Off-map, report an error. |
| 399 |
std::string msg = "Map index out of bounds (" + std::to_string(x) + ", " + |
| 400 |
std::to_string(y) + ")"; |
| 401 |
luaL_argerror(L, 3, lua_pushfstring(L, msg.c_str())); |
| 402 |
} |
| 403 |
|
| 404 |
lua_settop(L, 2); |
| 405 |
luaT_setenvfield(L, 1, "map"); |
| 406 |
} |
| 407 |
|
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
int l_anim_get_tile(lua_State* L) { |
| 412 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 413 |
lua_settop(L, 1); |
| 414 |
lua_getfenv(L, 1); |
| 415 |
lua_getfield(L, 2, "map"); |
| 416 |
lua_replace(L, 2); |
| 417 |
if (lua_isnil(L, 2)) { |
| 418 |
return 0; // No map supplied. |
| 419 |
} |
| 420 |
|
| 421 |
const xy_pair& tile = pAnimation->get_tile(); |
| 422 |
if (tile.x >= 0 && tile.y >= 0) { |
| 423 |
lua_pushinteger(L, tile.x + 1); |
| 424 |
lua_pushinteger(L, tile.y + 1); |
| 425 |
} else { |
| 426 |
lua_pushnil(L); |
| 427 |
lua_pushnil(L); |
| 428 |
} |
| 429 |
return 3; // map, x, y |
| 430 |
} |
| 431 |
|
| 432 |
int l_anim_set_parent(lua_State* L) { |
| 433 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 434 |
animation* pParent = |
| 435 |
luaT_testuserdata<animation>(L, 2, luaT_environindex, false); |
| 436 |
bool use_primary = lua_isnone(L, 3); // No number means '1'. |
| 437 |
if (!use_primary) { |
| 438 |
lua_Integer value = luaL_checkinteger(L, 3); |
| 439 |
if (value < 1 || value > 2) |
| 440 |
luaL_argerror( |
| 441 |
L, 3, "Marker index out of bounds (only values 1 and 2 are allowed)"); |
| 442 |
use_primary = (value == 1); |
| 443 |
} |
| 444 |
pAnimation->set_parent(pParent, use_primary); |
| 445 |
lua_settop(L, 1); |
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
template <typename T> |
| 450 |
int l_anim_set_flag(lua_State* L) { |
| 451 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 452 |
pAnimation->set_flags(static_cast<uint32_t>(luaL_checkinteger(L, 2))); |
| 453 |
|
| 454 |
lua_settop(L, 1); |
| 455 |
return 1; |
| 456 |
} |
| 457 |
|
| 458 |
template <typename T> |
| 459 |
int l_anim_set_flag_partial(lua_State* L) { |
| 460 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 461 |
uint32_t iFlags = static_cast<uint32_t>(luaL_checkinteger(L, 2)); |
| 462 |
if (lua_isnone(L, 3) || lua_toboolean(L, 3)) { |
| 463 |
pAnimation->set_flags(pAnimation->get_flags() | iFlags); |
| 464 |
} else { |
| 465 |
pAnimation->set_flags(pAnimation->get_flags() & ~iFlags); |
| 466 |
} |
| 467 |
lua_settop(L, 1); |
| 468 |
return 1; |
| 469 |
} |
| 470 |
|
| 471 |
template <typename T> |
| 472 |
int l_anim_make_visible(lua_State* L) { |
| 473 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 474 |
pAnimation->set_flags(pAnimation->get_flags() & |
| 475 |
~static_cast<uint32_t>(thdf_alpha_50 | thdf_alpha_75)); |
| 476 |
|
| 477 |
lua_settop(L, 1); |
| 478 |
return 1; |
| 479 |
} |
| 480 |
|
| 481 |
template <typename T> |
| 482 |
int l_anim_make_invisible(lua_State* L) { |
| 483 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 484 |
pAnimation->set_flags(pAnimation->get_flags() | |
| 485 |
static_cast<uint32_t>(thdf_alpha_50 | thdf_alpha_75)); |
| 486 |
|
| 487 |
lua_settop(L, 1); |
| 488 |
return 1; |
| 489 |
} |
| 490 |
|
| 491 |
template <typename T> |
| 492 |
int l_anim_is_visible(lua_State* L) { |
| 493 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 494 |
constexpr uint32_t alpha_flags = |
| 495 |
static_cast<uint32_t>(thdf_alpha_50 | thdf_alpha_75); |
| 496 |
uint32_t object_alpha_flags = pAnimation->get_flags() & alpha_flags; |
| 497 |
lua_pushboolean(L, object_alpha_flags == alpha_flags ? 0 : 1); |
| 498 |
return 1; |
| 499 |
} |
| 500 |
|
| 501 |
template <typename T> |
| 502 |
int l_anim_get_flag(lua_State* L) { |
| 503 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 504 |
lua_pushinteger(L, pAnimation->get_flags()); |
| 505 |
|
| 506 |
return 1; |
| 507 |
} |
| 508 |
|
| 509 |
template <typename T> |
| 510 |
int l_anim_set_pixel_offset(lua_State* L) { |
| 511 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 512 |
|
| 513 |
int x = static_cast<int>(luaL_checkinteger(L, 2)); |
| 514 |
int y = static_cast<int>(luaL_checkinteger(L, 3)); |
| 515 |
pAnimation->set_pixel_offset(x, y); |
| 516 |
|
| 517 |
lua_settop(L, 1); |
| 518 |
return 1; |
| 519 |
} |
| 520 |
|
| 521 |
int l_anim_get_pixel_offset(lua_State* L) { |
| 522 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 523 |
|
| 524 |
const xy_pair& offset = pAnimation->get_pixel_offset(); |
| 525 |
lua_pushinteger(L, offset.x); |
| 526 |
lua_pushinteger(L, offset.y); |
| 527 |
|
| 528 |
return 2; |
| 529 |
} |
| 530 |
|
| 531 |
template <typename T> |
| 532 |
int l_anim_set_speed(lua_State* L) { |
| 533 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 534 |
|
| 535 |
int x = static_cast<int>(luaL_optinteger(L, 2, 0)); |
| 536 |
int y = static_cast<int>(luaL_optinteger(L, 3, 0)); |
| 537 |
pAnimation->set_speed(x, y); |
| 538 |
|
| 539 |
lua_settop(L, 1); |
| 540 |
return 1; |
| 541 |
} |
| 542 |
|
| 543 |
template <typename T> |
| 544 |
int l_anim_set_layer(lua_State* L) { |
| 545 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 546 |
|
| 547 |
pAnimation->set_layer(static_cast<int>(luaL_checkinteger(L, 2)), |
| 548 |
static_cast<int>(luaL_optinteger(L, 3, 0))); |
| 549 |
|
| 550 |
lua_settop(L, 1); |
| 551 |
return 1; |
| 552 |
} |
| 553 |
|
| 554 |
int l_anim_set_layers_from(lua_State* L) { |
| 555 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 556 |
const animation* pAnimationSrc = |
| 557 |
luaT_testuserdata<animation>(L, 2, luaT_environindex); |
| 558 |
|
| 559 |
pAnimation->set_layers_from(pAnimationSrc); |
| 560 |
|
| 561 |
lua_settop(L, 1); |
| 562 |
return 1; |
| 563 |
} |
| 564 |
|
| 565 |
int l_anim_set_tag(lua_State* L) { |
| 566 |
luaT_testuserdata<animation>(L); |
| 567 |
lua_settop(L, 2); |
| 568 |
luaT_setenvfield(L, 1, "tag"); |
| 569 |
return 1; |
| 570 |
} |
| 571 |
|
| 572 |
int l_anim_get_tag(lua_State* L) { |
| 573 |
luaT_testuserdata<animation>(L); |
| 574 |
lua_settop(L, 1); |
| 575 |
lua_getfenv(L, 1); |
| 576 |
lua_getfield(L, 2, "tag"); |
| 577 |
return 1; |
| 578 |
} |
| 579 |
|
| 580 |
int l_anim_get_primary_marker(lua_State* L) { |
| 581 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 582 |
int iX = 0; |
| 583 |
int iY = 0; |
| 584 |
pAnimation->get_primary_marker(&iX, &iY); |
| 585 |
lua_pushinteger(L, iX); |
| 586 |
lua_pushinteger(L, iY); |
| 587 |
return 2; |
| 588 |
} |
| 589 |
|
| 590 |
int l_anim_get_secondary_marker(lua_State* L) { |
| 591 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 592 |
int iX = 0; |
| 593 |
int iY = 0; |
| 594 |
pAnimation->get_secondary_marker(&iX, &iY); |
| 595 |
lua_pushinteger(L, iX); |
| 596 |
lua_pushinteger(L, iY); |
| 597 |
return 2; |
| 598 |
} |
| 599 |
|
| 600 |
template <typename T> |
| 601 |
int l_anim_tick(lua_State* L) { |
| 602 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 603 |
pAnimation->tick(); |
| 604 |
lua_settop(L, 1); |
| 605 |
return 1; |
| 606 |
} |
| 607 |
|
| 608 |
template <typename T> |
| 609 |
int l_anim_draw(lua_State* L) { |
| 610 |
T* pAnimation = luaT_testuserdata<T>(L); |
| 611 |
render_target* pCanvas = luaT_testuserdata<render_target>(L, 2); |
| 612 |
int x = static_cast<int>(luaL_checkinteger(L, 3)); |
| 613 |
int y = static_cast<int>(luaL_checkinteger(L, 4)); |
| 614 |
pAnimation->draw(pCanvas, {x, y}); |
| 615 |
|
| 616 |
lua_settop(L, 1); |
| 617 |
return 1; |
| 618 |
} |
| 619 |
|
| 620 |
int l_anim_set_patient_effect(lua_State* L) { |
| 621 |
animation* pAnimation = luaT_testuserdata<animation>(L); |
| 622 |
animation_effect flags = |
| 623 |
static_cast<animation_effect>(luaL_checkinteger(L, 2)); |
| 624 |
pAnimation->set_patient_effect(flags); |
| 625 |
lua_settop(L, 1); |
| 626 |
return 1; |
| 627 |
} |
| 628 |
|
| 629 |
template <typename T> |
| 630 |
int l_anim_set_scale_factor(lua_State* L) { |
| 631 |
T* anim = luaT_testuserdata<T>(L); |
| 632 |
anim->set_scale_factor(static_cast<int>(luaL_checkinteger(L, 2))); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
int l_srl_set_sheet(lua_State* L) { |
| 637 |
sprite_render_list* pSrl = luaT_testuserdata<sprite_render_list>(L); |
| 638 |
sprite_sheet* pSheet = luaT_testuserdata<sprite_sheet>(L, 2); |
| 639 |
pSrl->set_sheet(pSheet); |
| 640 |
|
| 641 |
lua_settop(L, 2); |
| 642 |
luaT_setenvfield(L, 1, "sheet"); |
| 643 |
return 1; |
| 644 |
} |
| 645 |
|
| 646 |
int l_srl_append(lua_State* L) { |
| 647 |
sprite_render_list* pSrl = luaT_testuserdata<sprite_render_list>(L); |
| 648 |
pSrl->append_sprite(luaL_checkinteger(L, 2), |
| 649 |
static_cast<int>(luaL_checkinteger(L, 3)), |
| 650 |
static_cast<int>(luaL_checkinteger(L, 4))); |
| 651 |
lua_settop(L, 1); |
| 652 |
return 1; |
| 653 |
} |
| 654 |
|
| 655 |
int l_srl_set_lifetime(lua_State* L) { |
| 656 |
sprite_render_list* pSrl = luaT_testuserdata<sprite_render_list>(L); |
| 657 |
pSrl->set_lifetime(static_cast<int>(luaL_checkinteger(L, 2))); |
| 658 |
lua_settop(L, 1); |
| 659 |
return 1; |
| 660 |
} |
| 661 |
|
| 662 |
int l_srl_set_use_intermediate_buffer(lua_State* L) { |
| 663 |
sprite_render_list* pSrl = luaT_testuserdata<sprite_render_list>(L); |
| 664 |
pSrl->set_use_intermediate_buffer(); |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
int l_srl_is_dead(lua_State* L) { |
| 669 |
sprite_render_list* pSrl = luaT_testuserdata<sprite_render_list>(L); |
| 670 |
lua_pushboolean(L, pSrl->is_dead() ? 1 : 0); |
| 671 |
return 1; |
| 672 |
} |
| 673 |
|
| 674 |
} // namespace |
| 675 |
|
| 676 |
void lua_register_anims(const lua_register_state* pState) { |
| 677 |
// Anims |
| 678 |
{ |
| 679 |
lua_class_binding<animation_manager> lcb(pState, "anims", l_anims_new, |
| 680 |
lua_metatable::anims); |
| 681 |
lcb.add_function(l_anims_load, "load"); |
| 682 |
lcb.add_function(l_anims_loadcustom, "loadCustom"); |
| 683 |
lcb.add_function(l_anims_set_spritesheet, "setSheet", lua_metatable::sheet); |
| 684 |
lcb.add_function(l_anims_set_canvas, "setCanvas", lua_metatable::surface); |
| 685 |
lcb.add_function(l_anims_getanims, "getAnimations"); |
| 686 |
lcb.add_function(l_anims_getfirst, "getFirstFrame"); |
| 687 |
lcb.add_function(l_anims_getnext, "getNextFrame"); |
| 688 |
lcb.add_function(l_anims_set_alt_pal, "setAnimationGhostPalette"); |
| 689 |
lcb.add_function(l_anims_set_primary_marker, "setFramePrimaryMarker"); |
| 690 |
lcb.add_function(l_anims_set_secondary_marker, "setFrameSecondaryMarker"); |
| 691 |
lcb.add_function(l_anims_draw, "draw", lua_metatable::surface, |
| 692 |
lua_metatable::layers); |
| 693 |
lcb.add_function(l_anims_tick, "tick"); |
| 694 |
lcb.add_constant("Alt32_GreyScale", thdf_alt32_grey_scale); |
| 695 |
lcb.add_constant("Alt32_BlueRedSwap", thdf_alt32_blue_red_swap); |
| 696 |
} |
| 697 |
|
| 698 |
// Weak table at AnimMetatable[1] for light UD -> object lookup |
| 699 |
// For hitTest / setHitTestResult |
| 700 |
lua_newtable(pState->L); |
| 701 |
lua_createtable(pState->L, 0, 1); |
| 702 |
lua_pushliteral(pState->L, "v"); |
| 703 |
lua_setfield(pState->L, -2, "__mode"); |
| 704 |
lua_setmetatable(pState->L, -2); |
| 705 |
lua_rawseti(pState->L, |
| 706 |
pState->metatables[static_cast<size_t>(lua_metatable::anim)], 1); |
| 707 |
|
| 708 |
// Weak table at AnimMetatable[2] for light UD -> full UD lookup |
| 709 |
// For persisting Map |
| 710 |
lua_newtable(pState->L); |
| 711 |
lua_createtable(pState->L, 0, 1); |
| 712 |
lua_pushliteral(pState->L, "v"); |
| 713 |
lua_setfield(pState->L, -2, "__mode"); |
| 714 |
lua_setmetatable(pState->L, -2); |
| 715 |
lua_rawseti(pState->L, |
| 716 |
pState->metatables[static_cast<size_t>(lua_metatable::anim)], 2); |
| 717 |
|
| 718 |
// Anim |
| 719 |
{ |
| 720 |
lua_class_binding<animation> lcb(pState, "animation", l_anim_new<animation>, |
| 721 |
lua_metatable::anim); |
| 722 |
lcb.add_metamethod(l_anim_persist<animation>, "persist"); |
| 723 |
lcb.add_metamethod(l_anim_pre_depersist<animation>, "pre_depersist"); |
| 724 |
lcb.add_metamethod(l_anim_depersist<animation>, "depersist"); |
| 725 |
lcb.add_function(l_anim_set_anim, "setAnimation", lua_metatable::anims); |
| 726 |
lcb.add_function(l_anim_set_crop, "setCrop"); |
| 727 |
lcb.add_function(l_anim_get_crop, "getCrop"); |
| 728 |
lcb.add_function(l_anim_set_morph, "setMorph"); |
| 729 |
lcb.add_function(l_anim_set_frame, "setFrame"); |
| 730 |
lcb.add_function(l_anim_get_frame, "getFrame"); |
| 731 |
lcb.add_function(l_anim_get_anim, "getAnimation"); |
| 732 |
lcb.add_function(l_anim_set_tile<animation>, "setTile", lua_metatable::map); |
| 733 |
lcb.add_function(l_anim_get_tile, "getTile"); |
| 734 |
lcb.add_function(l_anim_set_parent, "setParent"); |
| 735 |
lcb.add_function(l_anim_set_flag<animation>, "setFlag"); |
| 736 |
lcb.add_function(l_anim_set_flag_partial<animation>, "setPartialFlag"); |
| 737 |
lcb.add_function(l_anim_get_flag<animation>, "getFlag"); |
| 738 |
lcb.add_function(l_anim_is_visible<animation>, "isVisible"); |
| 739 |
lcb.add_function(l_anim_make_visible<animation>, "makeVisible"); |
| 740 |
lcb.add_function(l_anim_make_invisible<animation>, "makeInvisible"); |
| 741 |
lcb.add_function(l_anim_set_tag, "setTag"); |
| 742 |
lcb.add_function(l_anim_get_tag, "getTag"); |
| 743 |
lcb.add_function(l_anim_set_pixel_offset<animation>, "setPosition"); |
| 744 |
lcb.add_function(l_anim_get_pixel_offset, "getPosition"); |
| 745 |
lcb.add_function(l_anim_set_speed<animation>, "setSpeed"); |
| 746 |
lcb.add_function(l_anim_set_layer<animation>, "setLayer"); |
| 747 |
lcb.add_function(l_anim_set_layers_from, "setLayersFrom"); |
| 748 |
lcb.add_function(l_anim_set_scale_factor<animation>, "setScaleFactor"); |
| 749 |
lcb.add_function(l_anim_set_hitresult, "setHitTestResult"); |
| 750 |
lcb.add_function(l_anim_get_primary_marker, "getPrimaryMarker"); |
| 751 |
lcb.add_function(l_anim_get_secondary_marker, "getSecondaryMarker"); |
| 752 |
lcb.add_function(l_anim_tick<animation>, "tick"); |
| 753 |
lcb.add_function(l_anim_draw<animation>, "draw", lua_metatable::surface); |
| 754 |
lcb.add_function(l_anim_set_patient_effect, "setPatientEffect"); |
| 755 |
} |
| 756 |
|
| 757 |
// Duplicate AnimMetatable[1,2] to SpriteListMetatable[1,2] |
| 758 |
lua_rawgeti(pState->L, |
| 759 |
pState->metatables[static_cast<size_t>(lua_metatable::anim)], 1); |
| 760 |
lua_rawseti( |
| 761 |
pState->L, |
| 762 |
pState->metatables[static_cast<size_t>(lua_metatable::sprite_list)], 1); |
| 763 |
lua_rawgeti(pState->L, |
| 764 |
pState->metatables[static_cast<size_t>(lua_metatable::anim)], 2); |
| 765 |
lua_rawseti( |
| 766 |
pState->L, |
| 767 |
pState->metatables[static_cast<size_t>(lua_metatable::sprite_list)], 2); |
| 768 |
|
| 769 |
// SpriteList |
| 770 |
{ |
| 771 |
lua_class_binding<sprite_render_list> lcb(pState, "spriteList", |
| 772 |
l_anim_new<sprite_render_list>, |
| 773 |
lua_metatable::sprite_list); |
| 774 |
lcb.add_metamethod(l_anim_persist<sprite_render_list>, "persist"); |
| 775 |
lcb.add_metamethod(l_anim_pre_depersist<sprite_render_list>, |
| 776 |
"pre_depersist"); |
| 777 |
lcb.add_metamethod(l_anim_depersist<sprite_render_list>, "depersist"); |
| 778 |
lcb.add_function(l_srl_set_sheet, "setSheet", lua_metatable::sheet); |
| 779 |
lcb.add_function(l_srl_append, "append"); |
| 780 |
lcb.add_function(l_srl_set_lifetime, "setLifetime"); |
| 781 |
lcb.add_function(l_srl_set_use_intermediate_buffer, |
| 782 |
"setUseIntermediateBuffer"); |
| 783 |
lcb.add_function(l_anim_set_scale_factor<sprite_render_list>, |
| 784 |
"setScaleFactor"); |
| 785 |
lcb.add_function(l_srl_is_dead, "isDead"); |
| 786 |
lcb.add_function(l_anim_set_tile<sprite_render_list>, "setTile", |
| 787 |
lua_metatable::map); |
| 788 |
lcb.add_function(l_anim_set_flag<sprite_render_list>, "setFlag"); |
| 789 |
lcb.add_function(l_anim_set_flag_partial<sprite_render_list>, |
| 790 |
"setPartialFlag"); |
| 791 |
lcb.add_function(l_anim_get_flag<sprite_render_list>, "getFlag"); |
| 792 |
lcb.add_function(l_anim_is_visible<sprite_render_list>, "isVisible"); |
| 793 |
lcb.add_function(l_anim_make_visible<sprite_render_list>, "makeVisible"); |
| 794 |
lcb.add_function(l_anim_make_invisible<sprite_render_list>, |
| 795 |
"makeInvisible"); |
| 796 |
lcb.add_function(l_anim_set_pixel_offset<sprite_render_list>, |
| 797 |
"setPosition"); |
| 798 |
lcb.add_function(l_anim_set_speed<sprite_render_list>, "setSpeed"); |
| 799 |
lcb.add_function(l_anim_set_layer<sprite_render_list>, "setLayer"); |
| 800 |
lcb.add_function(l_anim_tick<sprite_render_list>, "tick"); |
| 801 |
lcb.add_function(l_anim_draw<sprite_render_list>, "draw", |
| 802 |
lua_metatable::surface); |
| 803 |
} |
| 804 |
} |