Thalassicus
Bytes and Nibblers
If anyone's ever had a long, complicated chain of if-statements checking several boolean variables to figure out what actions to take, this might be helpful for you. I did something like this to solve a bug with the game's display of yield and growth modifiers in the city window (it incorrectly grouped them together and stated base yield was the surplus).
This method comes in three parts:
The truth table indicates what outputs should be produced with given inputs.
In this example, if a yield is consumed (input) it always shows the surplus yield (output) on the tooltip. This corresponds with the four rows with "Consumed" in the truth table and four matching "true" statements for surplus. The order of items in the truth table is important: the result function expects each decimal index to correspond to its binary representation (index 0 is 000, then 001, 010, 011, etc).
The reasons I chose this method of implementation are:
This method comes in three parts:
- Truth table
- Function to read the truth table
- Usage
Code:
showYieldString = {
showYieldString = {
-- output if input
-- show { base, surplus} if Consumed YieldMod SurplusMod
{ false, false}, -- - - -
{ true, false}, -- - - SurplusMod
{ true, false}, -- - YieldMod -
{ true, true}, -- - YieldMod SurplusMod
{ false, true}, -- Consumed - -
{ false, true}, -- Consumed - SurplusMod
{ true, true}, -- Consumed YieldMod -
{ true, true} -- Consumed YieldMod SurplusMod
}
function GetTruthTableResult(inputs, truthTable)
local index = 0
for k,v in pairs(inputs) do
if v then
index = index + math.pow(2, #inputs-k))
end
end
return truthTable[index + 1]
end
...
local truthiness = GetTruthTableResult({isConsumed, hasYieldMod, hasSurplusMod}, showYieldString)
local showBaseYield,showSurplusYield = truthiness[1],truthiness[2]
...
In this example, if a yield is consumed (input) it always shows the surplus yield (output) on the tooltip. This corresponds with the four rows with "Consumed" in the truth table and four matching "true" statements for surplus. The order of items in the truth table is important: the result function expects each decimal index to correspond to its binary representation (index 0 is 000, then 001, 010, 011, etc).
The reasons I chose this method of implementation are:
- Easy for me to visually understand the logic.
- Separates data from the algorithm.
- Linear execution time: no matter how complex the combinations of input and output, there's only as many conditional checks (if-statements) as there are variables.
- Changing output doesn't require rewriting any if-then conditional statements.
- The output can be anything. In this case I return two table values, but it could be strings, function calls, etc... even different output types for each row in the truth table.
- Only works for simple boolean checks as input.
- Table size is exponential to the number of inputs, so it's only feasible with four or less input variables (though output variables are not constrained).