• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua] Boolexpr Override

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Lua:
--[[ boolexproverride.lua v1.0.0 by AGD | https://www.hiveworkshop.com/threads/339629/


  Description:

    Overrides the native boolexpr functions Filter(), Not(), And(), and Or().
    With the native functions, evaluating a nested boolexpr such as:

    local nestedExpr = And(Or(Filter(funcA), Filter(funcB)), Or(Filter(funcC), Filter(funcD)))
    local t = CreateTrigger()
    TriggerAddCondition(t, nestedExpr)
    TriggerEvaluate(t)

    will have 7 boolexpr evaluations in total. Using these new overrides,
    the 7 boolexpr evaluations will be replaced by 7 function calls + a
    constant overhead of 1 boolexpr evaluation.

    Aside from these overrides, this script additionally provides a function
    for retrieving the corresponding function from a given boolexpr.

  Requirements:

    - None

]] --
--[[

  API:

    function FilterToFunc(filter_expr: boolexpr): function
    - Returns the corresponding function for the given boolexpr

]] --
do
  local expr_func = {}

  local oldFilter = Filter
  function Filter(filter_func)
    local filter_expr = oldFilter(filter_func)
    expr_func[filter_expr] = filter_func
    return filter_expr
  end

  Condition = Filter

  function Not(filter_expr)
    return Filter(function()
      return not expr_func[filter_expr]()
    end)
  end

  function And(left_filter_expr, right_filter_expr)
    return Filter(function()
      return expr_func[left_filter_expr]() and expr_func[right_filter_expr]()
    end)
  end

  function Or(left_filter_expr, right_filter_expr)
    return Filter(function()
      return expr_func[left_filter_expr]() or expr_func[right_filter_expr]()
    end)
  end

  --- Returns the corresponding function for the given boolexpr
  ---@param filter_expr boolexpr
  ---@return function
  function FilterToFunc(filter_expr)
    return expr_func[filter_expr]
  end
end
 
Last edited:

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Since this is using brand new syntax that I haven't seen outside of some of Nestharus's attempts at making a "fastest execution possible", I recommend not overloading Filter but rather creating the native that never existed: "BoolExpr". There is a DestroyBoolExpr but not a BoolExpr itself, since both Filter and Condition are the only ways to create one without passing a second boolexpr.

I'd like to keep "Filter" unhooked in any scenario possible.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Upon further review, I find that this resource is strictly superior to the regular Filter/Condition/etc, while not diminishing performance of the actual callbacks therein.

However, I'd like to take this time to highlight something very important to users who are using Filter in a dynamic environment: Filter(function() end) will create a new Filter each time, because the function itself is different each time it's called. Therefore, it is necessary to cache those functions and/or filters to avoid re-creating functions and Filters without necessity.

Approved.
 
Top