Zet0rz / nZombies-Unlimited

nZombies Unlimited
MIT License
43 stars 15 forks source link

Lightweight Logic Reduction #24

Open Zet0rz opened 6 years ago

Zet0rz commented 6 years ago

The logic system is an extensive system that enabled custom object types to provide functionality and logic to any given situation. However during actual gameplay, the only things that matter are the functions they perform on their inputs. Since they cannot be edited or change during play, they can be reduced into just a chain of functions instead of whole networked objects.

In doing so, the Logic Map gets reduced to just a network of chained functions. It is unknown whether the overhead removed from this is worth the reduction, but the idea is as follows:

An example of how the system would change, using arguments and settings:

LOGIC.Inputs = {
    ["Print"] = {
        Function = function(self, activator, caller, args, ...)
            print(args)
            print("Says " .. self:GetSetting("Name") .. " through " .. caller:Nick())
            self:LogicOutput("AfterPrint", args)
        end
    }
}

Since settings can no longer change during play, the function can be reduced to just a static print. This is then created by the FunctionConstructor:

LOGIC.Inputs = {
    ["Print"] = {
        FunctionConstructor = function(self)
            local str = "Says " ... self:GetSetting("Name") .. " through "
            local func = function(outputs, activator, caller, args, ...)
                print(args)
                print(str .. caller:Nick())
                outputs.AfterPrint(args) -- This function provides the chaining
            end

            return func
        end
    }
}

The function generated by FunctionConstructor is referenced by any Logic Unit who has an output linked to it through its outputs table, which contain functions that run loop through all connections by this output and call the respective Logic Unit's constructed function much the same way the above would.

Zet0rz commented 5 years ago

Alternatively, the reduction could incorporate a setup similar to FunctionConstructor where self doesn't exist, but where two arguments outputs and settings carry the information of the individual logic unit.

Whether this actual reduction is possible will depend on whether I can find a way to support event listening, perhaps using hooks, and broadcasting these events across all chained functions in one go.