Simplified error handling. Fixed index in messages.

This commit is contained in:
Guillaume Melquiond 2010-02-27 09:17:56 +00:00
parent 43d2d3c9a1
commit 8234e9604b

View file

@ -2145,62 +2145,42 @@ static int transform_ai_action(lua_State *L, ai::action_result_ptr action_result
return 1;
}
static map_location to_map_location(lua_State *L, int &index, bool &err)
static bool to_map_location(lua_State *L, int &index, map_location &res)
{
if (false) {
error_call_destructors:
err = true;
return map_location::null_location;
}
map_location loc;
if (lua_isuserdata(L, index))
{
if (!luaW_hasmetatable(L, index, getunitKey))
goto error_call_destructors;
if (!luaW_hasmetatable(L, index, getunitKey)) return false;
size_t id = *static_cast<size_t *>(lua_touserdata(L, index));
unit_map::const_unit_iterator ui = (*resources::units).find(id);
if (!ui.valid())
goto error_call_destructors;
loc = ui->first;
unit_map::const_unit_iterator ui = resources::units->find(id);
if (!ui.valid()) return false;
res = ui->first;
++index;
}
else
{
if (!lua_isnumber(L, index))
goto error_call_destructors;
loc.x = lua_tointeger(L, index) - 1;
if (!lua_isnumber(L, index)) return false;
res.x = lua_tointeger(L, index) - 1;
++index;
if (!lua_isnumber(L, index))
goto error_call_destructors;
loc.y = lua_tointeger(L, index) - 1;
if (!lua_isnumber(L, index)) return false;
res.y = lua_tointeger(L, index) - 1;
++index;
}
return loc;
return true;
}
static int impl_ai_execute_move(lua_State *L, bool remove_movement)
{
int index = 2;
if (false) {
error_call_destructors_1:
return luaL_argerror(L, index-1, "wrong 'from' location");
error_call_destructors_2:
return luaL_argerror(L, index-1, "wrong 'to' location");
error_call_destructors:
return luaL_typerror(L, index, "location (unit/integers)");
}
int side = lua_tointeger(L,lua_upvalueindex(1));
bool err = false;
map_location from = to_map_location(L,index,err);
if (err) {
goto error_call_destructors_1;
}
map_location to = to_map_location(L,index,err);
if (err) {
goto error_call_destructors_2;
}
map_location from, to;
if (!to_map_location(L, index, from)) goto error_call_destructors;
if (!to_map_location(L, index, to)) goto error_call_destructors;
ai::move_result_ptr move_result = ai::actions::execute_move_action(side,true,from,to,remove_movement);
return transform_ai_action(L,move_result);
}
@ -2222,22 +2202,14 @@ static int impl_ai_execute_attack(lua_State *L)
{
int index = 2;
if (false) {
error_call_destructors_1:
return luaL_argerror(L, index-1, "wrong attacker location");
error_call_destructors_2:
return luaL_argerror(L, index-1, "wrong defender location");
error_call_destructors:
return luaL_typerror(L, index, "location (unit/integers)");
}
int side = lua_tointeger(L,lua_upvalueindex(1));
bool err = false;
map_location attacker = to_map_location(L,index,err);
if (err) {
goto error_call_destructors_1;
}
map_location defender = to_map_location(L,index,err);
if (err) {
goto error_call_destructors_2;
}
map_location attacker, defender;
if (!to_map_location(L, index, attacker)) goto error_call_destructors;
if (!to_map_location(L, index, defender)) goto error_call_destructors;
int attacker_weapon = -1;//-1 means 'select what is best'
double aggression = 0.5;//TODO: replace with side agression
@ -2260,15 +2232,12 @@ static int impl_ai_execute_stopunit_select(lua_State *L, bool remove_movement, b
int index = 2;
if (false) {
error_call_destructors:
return luaL_argerror(L, index-1, "wrong unit location");
return luaL_typerror(L, index, "location (unit/integers)");
}
int side = lua_tointeger(L,lua_upvalueindex(1));
bool err = false;
map_location loc = to_map_location(L,index,err);
if (err) {
goto error_call_destructors;
}
map_location loc;
if (!to_map_location(L, index, loc)) goto error_call_destructors;
ai::stopunit_result_ptr stopunit_result = ai::actions::execute_stopunit_action(side,true,loc,remove_movement,remove_attacks);
return transform_ai_action(L,stopunit_result);
@ -2293,21 +2262,12 @@ static int impl_ai_execute_stopunit_all(lua_State *L)
static int impl_ai_execute_recruit(lua_State *L)
{
int index = 3;
if (false) {
error_call_destructors:
return luaL_argerror(L, index-1, "wrong recruit location");
}
const char *unit_name = luaL_checkstring(L,2);
int side = lua_tointeger(L,lua_upvalueindex(1));
bool err = false;
map_location where = map_location::null_location;
if (!lua_isnoneornil(L,index)) {
where = to_map_location(L,index,err);
if (err) {
goto error_call_destructors;
}
map_location where;
if (!lua_isnoneornil(L, 3)) {
where.x = lua_tonumber(L, 3) - 1;
where.y = lua_tonumber(L, 4) - 1;
}
ai::recruit_result_ptr recruit_result = ai::actions::execute_recruit_action(side,true,std::string(unit_name),where);
@ -2317,21 +2277,12 @@ static int impl_ai_execute_recruit(lua_State *L)
static int impl_ai_execute_recall(lua_State *L)
{
int index = 3;
if (false) {
error_call_destructors:
return luaL_argerror(L, index-1, "wrong recall location");
}
const char *unit_id = luaL_checkstring(L,2);
int side = lua_tointeger(L,lua_upvalueindex(1));
bool err = false;
map_location where = map_location::null_location;
if (!lua_isnoneornil(L,index)) {
where = to_map_location(L,index,err);
if (err) {
goto error_call_destructors;
}
map_location where;
if (!lua_isnoneornil(L, 3)) {
where.x = lua_tonumber(L, 3) - 1;
where.y = lua_tonumber(L, 4) - 1;
}
ai::recall_result_ptr recall_result = ai::actions::execute_recall_action(side,true,std::string(unit_id),where);