コード例 #1
0
ファイル: lib_table.cpp プロジェクト: Xetrill/XsLuaJIT
/// table.retain(table, selector[, raw = false])
///
/// Iterates over a *table* invoking a *selector* function to decide whether
/// each key-value pair will be retained or be removed. Leaving only key-value
/// pairs that passed the selector check remaining.
///
/// The selector has the following signature: *selector(key, value)* and must
/// return a boolean.
/// Returning *true* will cause the key to remain, returning
/// *false* and the key will be removed.
///
/// Keys are removed *raw* if the third parameter is *true*, bypassing any
/// eventual metatable.
///
/// Example usage:
///     table.retain(
///         {
///             ['a'] = 1,
///             ['b'] = 2,
///             ['c'] = 3
///         },
///         function (k, v)
///             return v % 2 == 0
///         end
///     )
///     --> { ['b'] = 2 }
static int libE_retain(lua_State* const L)
{
    luaL_checktype(L, 1, LUA_TTABLE);
    luaL_checktype(L, 2, LUA_TFUNCTION);
    bool raw = luaL_optbool(L, 3, false);

    lua_settop(L, 2);

    lua_pushnil(L);
    while (lua_next(L, 1))
    {
        lua_pushvalue(L, -2);
        lua_insert(L, -2);
        lua_pushvalue(L, -2);
        lua_insert(L, -2);
        lua_pushvalue(L, 2);
        lua_insert(L, -3);
        lua_call(L, 2, 1);

        if (lua_toboolean(L, -1) == 0)
        {
            lua_pushnil(L);
            if (raw)
                lua_rawset(L, 1);
            else
                lua_settable(L, 1);
        }

        lua_pop(L, 1);
    }

    lua_settop(L, 1);
    lxs_assert_stack_at(L, 1);
    return 1;
}
コード例 #2
0
//void CvTeam::setForcePeace(TeamTypes eIndex, bool bNewValue)
int CvLuaTeam::lSetForcePeace(lua_State* L)
{
	CvTeam* pkTeam = GetInstance(L);
	const TeamTypes eTeam = (TeamTypes) lua_tointeger(L, 2);
	const bool bValue = luaL_optbool(L, 3, false);

	pkTeam->setForcePeace(eTeam, bValue);
	return 0;
}
コード例 #3
0
// ---------------------------------------------------------------------
// void DoBecomeVassal(TeamTypes eTeam)
int CvLuaTeam::lDoBecomeVassal(lua_State *L)
{
	CvTeam* pkTeam = GetInstance(L);
	const TeamTypes eTeam = (TeamTypes) lua_tointeger(L, 2);
	const bool bVoluntary = luaL_optbool(L, 3, false);

	pkTeam->DoBecomeVassal(eTeam, bVoluntary);
	return 0;
}
コード例 #4
0
//void SetAllowsOpenBordersToTeam(eOtherTeam, bool bValue)
int CvLuaTeam::lSetOpenBorders(lua_State* L)
{
	CvTeam* pkTeam = GetInstance(L);
	const TeamTypes eTeam = (TeamTypes) lua_tointeger(L, 2);
	const bool bValue = luaL_optbool(L, 3, false);

	pkTeam->SetAllowsOpenBordersToTeam(eTeam, bValue);
	return 0;
}
コード例 #5
0
//------------------------------------------------------------------------------
//void setHasTech(TechTypes eIndex, bool bNewValue, PlayerTypes ePlayer, bool bFirst, bool bAnnounce);
int CvLuaTeam::lSetHasTech(lua_State* L)
{
	CvTeam* pkTeam = GetInstance(L);
	const TechTypes eIndex = (TechTypes)lua_tointeger(L, 2);
	const bool bNewValue = lua_toboolean(L, 3);
	const PlayerTypes ePlayer = (PlayerTypes)lua_tointeger(L, 4);
	const bool bFirst = lua_toboolean(L, 4);
	const bool bAnnounce = lua_toboolean(L, 5);
#if defined(MOD_BALANCE_CORE)
	const bool bNoBonus = luaL_optbool(L, 6, false);
	pkTeam->setHasTech(eIndex, bNewValue, ePlayer, bFirst, bAnnounce, bNoBonus);
#else
	pkTeam->setHasTech(eIndex, bNewValue, ePlayer, bFirst, bAnnounce);
#endif
	return 0;
}