From 28ec61e47cc0a61d191aadd6ad72ce09fdf913a0 Mon Sep 17 00:00:00 2001 From: jyelon Date: Fri, 22 Jul 2022 16:22:04 -0400 Subject: [PATCH] tangible.animate uses the new keyword parser. --- luprex/core/cpp/animqueue.cpp | 74 ++++++++++++++---------------- luprex/core/cpp/animqueue.hpp | 8 ++-- luprex/core/cpp/world-accessor.cpp | 22 +++++---- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/luprex/core/cpp/animqueue.cpp b/luprex/core/cpp/animqueue.cpp index 5e4b7c2a..76c773b0 100644 --- a/luprex/core/cpp/animqueue.cpp +++ b/luprex/core/cpp/animqueue.cpp @@ -146,7 +146,7 @@ void AnimStep::read_from(StreamBuffer *sb) { } -void AnimStep::from_lua_store_string(lua_State *L, int idx, eng::string *target, int16_t bits, const char *name) { +void AnimStep::config_store_string(lua_State *L, int idx, eng::string *target, int16_t bits, const char *name) { if ((bits_ & bits)||(*target != "")) { luaL_error(L, "specified %s twice", name); } @@ -157,7 +157,7 @@ void AnimStep::from_lua_store_string(lua_State *L, int idx, eng::string *target, bits_ |= bits; } -void AnimStep::from_lua_store_number(lua_State *L, int idx, float *target, float offset, int16_t bits, const char *name) { +void AnimStep::config_store_number(lua_State *L, int idx, float *target, float offset, int16_t bits, const char *name) { if (bits_ & bits) { luaL_error(L, "specified %s twice", name); } @@ -168,44 +168,40 @@ void AnimStep::from_lua_store_number(lua_State *L, int idx, float *target, float bits_ |= bits; } -void AnimStep::from_lua(lua_State *L, int idx, bool ignex, const AnimStep &qback) { - LuaSpecial tab(idx); - LuaVar key, value; - LuaStack LS(L, key, value); - if (!LS.istable(tab)) { - luaL_error(L, "animation spec must be a table"); +void AnimStep::configure(LuaKeywordParser &kp, const AnimStep &aqback) { + lua_State *L = kp.state(); + LuaVar value; + LuaStack LS(L, value); + + if (kp.parse(value, "action")) { + config_store_string(L, value.index(), &action_, 0, "action"); } - LS.set(key, LuaNil); - while (LS.next(tab, key, value)) { - if (!LS.isstring(key)) { - luaL_error(L, "animation specs must be key/value where key is a string"); - } - eng::string skey = LS.ckstring(key); - if (skey == "action") { - from_lua_store_string(L, value.index(), &action_, 0, "action"); - } else if (skey == "graphic") { - from_lua_store_string(L, value.index(), &graphic_, HAS_GRAPHIC, "graphic"); - } else if (skey == "plane") { - from_lua_store_string(L, value.index(), &plane_, HAS_PLANE, "plane"); - } else if (skey == "x") { - from_lua_store_number(L, value.index(), &xyz_.x, 0.0, HAS_X, "X coordinate"); - } else if (skey == "y") { - from_lua_store_number(L, value.index(), &xyz_.y, 0.0, HAS_Y, "Z coordinate"); - } else if (skey == "z") { - from_lua_store_number(L, value.index(), &xyz_.z, 0.0, HAS_Z, "Z coordinate"); - } else if (skey == "dx") { - from_lua_store_number(L, value.index(), &xyz_.x, qback.xyz().x, HAS_X, "X coordinate"); - } else if (skey == "dy") { - from_lua_store_number(L, value.index(), &xyz_.y, qback.xyz().y, HAS_Y, "Y coordinate"); - } else if (skey == "dz") { - from_lua_store_number(L, value.index(), &xyz_.z, qback.xyz().z, HAS_Z, "Z coordinate"); - } else if (skey == "facing") { - from_lua_store_number(L, value.index(), &facing_, 0.0, HAS_FACING, "facing"); - } else { - if (!ignex) { - luaL_error(L, "Unrecognized animation spec: %s", skey.c_str()); - } - } + if (kp.parse(value, "graphic")) { + config_store_string(L, value.index(), &graphic_, HAS_GRAPHIC, "graphic"); + } + if (kp.parse(value, "plane")) { + config_store_string(L, value.index(), &plane_, HAS_PLANE, "plane"); + } + if (kp.parse(value, "x")) { + config_store_number(L, value.index(), &xyz_.x, 0.0, HAS_X, "X coordinate"); + } + if (kp.parse(value, "y")) { + config_store_number(L, value.index(), &xyz_.y, 0.0, HAS_Y, "Z coordinate"); + } + if (kp.parse(value, "z")) { + config_store_number(L, value.index(), &xyz_.z, 0.0, HAS_Z, "Z coordinate"); + } + if (kp.parse(value, "dx")) { + config_store_number(L, value.index(), &xyz_.x, aqback.xyz().x, HAS_X, "X coordinate"); + } + if (kp.parse(value, "dy")) { + config_store_number(L, value.index(), &xyz_.y, aqback.xyz().y, HAS_Y, "Y coordinate"); + } + if (kp.parse(value, "dz")) { + config_store_number(L, value.index(), &xyz_.z, aqback.xyz().z, HAS_Z, "Z coordinate"); + } + if (kp.parse(value, "facing")) { + config_store_number(L, value.index(), &facing_, 0.0, HAS_FACING, "facing"); } } diff --git a/luprex/core/cpp/animqueue.hpp b/luprex/core/cpp/animqueue.hpp index 03f4420c..f3aaf94f 100644 --- a/luprex/core/cpp/animqueue.hpp +++ b/luprex/core/cpp/animqueue.hpp @@ -130,10 +130,10 @@ public: // graphic: "graphic" // plane: "plane" // - // qback: the animation queue back, from which relative + // aqback: the animation queue back, from which relative // moves are computed. // - void from_lua(lua_State *L, int idx, bool ignex, const AnimStep &qback); + void configure(LuaKeywordParser &kp, const AnimStep &aqback); // Make this step into a first-step of an anim queue. void keep_state_only(); @@ -161,8 +161,8 @@ private: eng::string graphic_; eng::string plane_; - void from_lua_store_string(lua_State *L, int idx, eng::string *target, int16_t bits, const char *name); - void from_lua_store_number(lua_State *L, int idx, float *target, float offset, int16_t bits, const char *name); + void config_store_string(lua_State *L, int idx, eng::string *target, int16_t bits, const char *name); + void config_store_number(lua_State *L, int idx, float *target, float offset, int16_t bits, const char *name); }; diff --git a/luprex/core/cpp/world-accessor.cpp b/luprex/core/cpp/world-accessor.cpp index c7413dce..55624272 100644 --- a/luprex/core/cpp/world-accessor.cpp +++ b/luprex/core/cpp/world-accessor.cpp @@ -58,12 +58,13 @@ LuaDefine(tangible_animate, "tan,configtable", "|action,graphic,plane,x,y,z,facing") { LuaArg tanobj, config; LuaStack LS(L, tanobj, config); + LuaKeywordParser kp(LS, config); World *w = World::fetch_global_pointer(L); Tangible *tan = w->tangible_get(LS, tanobj); int64_t id = w->alloc_id_predictable(); - const AnimStep &prev = tan->anim_queue_.back(); AnimStep step; - step.from_lua(L, config.index(), false, prev); + step.configure(kp, tan->anim_queue_.back()); + kp.check_unparsed_keywords(); if (step.action() == "") { luaL_error(L, "animation action must be specified"); } @@ -129,18 +130,20 @@ LuaDefine(tangible_delete, "tan", return LS.result(); } -LuaDefine(tangible_build, "configtable", +LuaDefine(tangible_build, "config", "|Build a new tangible object." - "|The configtable must contain: class,x,y,z,plane,graphic." - "|The configtable can optionally contain: facing.") { + "|The config table must contain: class,x,y,z,plane,graphic." + "|The config table can optionally contain: facing.") { LuaArg config; LuaVar classname, classtab, mt; LuaRet database; LuaStack LS(L, config, classname, classtab, database, mt); - - LS.checktable(config, "config"); + LuaKeywordParser kp(LS, config); + // Get the class of the new tangible. - LS.rawget(classname, config, "class"); + if (!kp.parse(classname, "class")) { + luaL_error(L, "You must specify a class for the tangible"); + } eng::string err = LS.getclass(classtab, classname); if (err != "") { luaL_error(L, "%s", err.c_str()); @@ -148,7 +151,8 @@ LuaDefine(tangible_build, "configtable", // Parse the initial animation step. AnimStep initstep, blank; - initstep.from_lua(L, config.index(), true, blank); + initstep.configure(kp, blank); + kp.check_unparsed_keywords(); if (!initstep.has_xyz()) { luaL_error(L, "You must specify (X,Y,Z) for new tangible"); }