[Cleanup] Rework Lua QuestReward to not use try/catch blocks (#2417)

Quest system will catch any errors and result it in being reported where
these weren't
This commit is contained in:
Michael Cook (mackal) 2022-09-03 12:07:46 -04:00 committed by GitHub
parent 5134a0e43b
commit ba53b4144e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1683,110 +1683,52 @@ void Lua_Client::QuestReward(Lua_Mob target, luabind::adl::object reward) {
QuestReward_Struct quest_reward; QuestReward_Struct quest_reward;
quest_reward.mob_id = 0; quest_reward.mob_id = 0;
quest_reward.target_id = self->GetID(); quest_reward.target_id = self->GetID();
quest_reward.copper = 0; quest_reward.copper = luabind::type(reward["copper"]) != LUA_TNIL ? luabind::object_cast<uint32>(reward["copper"]) : 0;
quest_reward.silver = 0; quest_reward.silver = luabind::type(reward["silver"]) != LUA_TNIL ? luabind::object_cast<uint32>(reward["silver"]) : 0;
quest_reward.gold = 0; quest_reward.gold = luabind::type(reward["gold"]) != LUA_TNIL ? luabind::object_cast<uint32>(reward["gold"]) : 0;
quest_reward.platinum = 0; quest_reward.platinum = luabind::type(reward["platinum"]) != LUA_TNIL ? luabind::object_cast<uint32>(reward["platinum"]) : 0;
quest_reward.exp_reward = 0; quest_reward.exp_reward = luabind::type(reward["exp"]) != LUA_TNIL ? luabind::object_cast<uint32>(reward["exp"]) : 0;
quest_reward.faction = 0; quest_reward.faction = 0;
quest_reward.faction_mod = 0; quest_reward.faction_mod = 0;
bool faction = false; bool faction = false;
std::fill(std::begin(quest_reward.item_id), std::end(quest_reward.item_id), -1); std::fill(std::begin(quest_reward.item_id), std::end(quest_reward.item_id), -1);
auto cur = reward["copper"]; auto item_id = reward["itemid"];
if (luabind::type(cur) != LUA_TNIL) { if (luabind::type(item_id) != LUA_TNIL) {
try { quest_reward.item_id[0] = luabind::object_cast<uint32>(item_id);
quest_reward.copper = luabind::object_cast<uint32>(cur);
} catch (luabind::cast_failed &) {
}
}
cur = reward["silver"];
if (luabind::type(cur) != LUA_TNIL) {
try {
quest_reward.silver = luabind::object_cast<uint32>(cur);
} catch (luabind::cast_failed &) {
}
}
cur = reward["gold"];
if (luabind::type(cur) != LUA_TNIL) {
try {
quest_reward.gold = luabind::object_cast<uint32>(cur);
} catch (luabind::cast_failed &) {
}
}
cur = reward["platinum"];
if (luabind::type(cur) != LUA_TNIL) {
try {
quest_reward.platinum = luabind::object_cast<uint32>(cur);
} catch (luabind::cast_failed &) {
}
}
cur = reward["itemid"];
if (luabind::type(cur) != LUA_TNIL) {
try {
quest_reward.item_id[0] = luabind::object_cast<uint32>(cur);
} catch (luabind::cast_failed &) {
}
} }
// if you define both an itemid and items table, the itemid is thrown away // if you define both an itemid and items table, the itemid is thrown away
// should we error? // should we error?
cur = reward["items"]; auto items = reward["items"];
if (luabind::type(cur) == LUA_TTABLE) { if (luabind::type(items) == LUA_TTABLE) {
try { // assume they defined a compatible table
// assume they defined a compatible table for (int i = 1; i <= QUESTREWARD_COUNT; ++i) {
for (int i = 1; i <= QUESTREWARD_COUNT; ++i) { auto item = items[i];
auto item = cur[i]; int cur_value = -1;
int cur_value = -1; if (luabind::type(item) != LUA_TNIL) {
if (luabind::type(item) != LUA_TNIL) { cur_value = luabind::object_cast<uint32>(item);
try { } else {
cur_value = luabind::object_cast<uint32>(item); break;
} catch (luabind::cast_failed &) {
}
} else {
break;
}
quest_reward.item_id[i - 1] = cur_value;
} }
} catch (luabind::cast_failed &) { quest_reward.item_id[i - 1] = cur_value;
} }
} }
cur = reward["exp"]; auto lua_faction = reward["faction"];
if (luabind::type(cur) != LUA_TNIL) { if (luabind::type(lua_faction) != LUA_TNIL) {
try {
quest_reward.exp_reward = luabind::object_cast<uint32>(cur);
} catch (luabind::cast_failed &) {
}
}
cur = reward["faction"];
if (luabind::type(cur) != LUA_TNIL) {
// if it's a table it will be {faction, faction_mod} // if it's a table it will be {faction, faction_mod}
if (luabind::type(cur) == LUA_TTABLE) { if (luabind::type(lua_faction) == LUA_TTABLE) {
auto item = cur[1]; auto item = lua_faction[1];
if (luabind::type(item) != LUA_TNIL) { if (luabind::type(item) != LUA_TNIL) {
try { quest_reward.faction = luabind::object_cast<uint32>(item);
quest_reward.faction = luabind::object_cast<uint32>(item);
} catch (luabind::cast_failed &) {
}
} }
item = cur[2]; item = lua_faction[2];
if (luabind::type(item) != LUA_TNIL) { if (luabind::type(item) != LUA_TNIL) {
try { quest_reward.faction_mod = luabind::object_cast<uint32>(item);
quest_reward.faction_mod = luabind::object_cast<uint32>(item);
} catch (luabind::cast_failed &) {
}
} }
} else { } else {
try { faction = luabind::object_cast<bool>(lua_faction);
faction = luabind::object_cast<bool>(cur);
} catch (luabind::cast_failed &) {
}
} }
} }